libutil: add O_NOCTTY back to old pty open code
[minix.git] / lib / libc / stdlib / mi_vector_hash.c
blobb867ebe858b71ddbcea76befa442a12a717f7db7
1 /* $NetBSD: mi_vector_hash.c,v 1.3 2010/03/19 18:11:30 joerg Exp $ */
2 /*-
3 * Copyright (c) 2009 The NetBSD Foundation, Inc.
4 * All rights reserved.
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Joerg Sonnenberger.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
35 * See http://burtleburtle.net/bob/hash/doobs.html for the full description
36 * and the original version of the code. This version differs by exposing
37 * the full internal state and avoiding byte operations in the inner loop
38 * if the key is aligned correctly.
41 #if HAVE_NBTOOL_CONFIG_H
42 #include "nbtool_config.h"
43 #endif
45 #include <sys/cdefs.h>
46 __RCSID("$NetBSD: mi_vector_hash.c,v 1.3 2010/03/19 18:11:30 joerg Exp $");
48 #include "namespace.h"
50 #include <sys/endian.h>
51 #include <stdint.h>
52 #include <stdlib.h>
54 #define mix(a, b, c) do { \
55 a -= b; a -= c; a ^= (c >> 13); \
56 b -= c; b -= a; b ^= (a << 8); \
57 c -= a; c -= b; c ^= (b >> 13); \
58 a -= b; a -= c; a ^= (c >> 12); \
59 b -= c; b -= a; b ^= (a << 16); \
60 c -= a; c -= b; c ^= (b >> 5); \
61 a -= b; a -= c; a ^= (c >> 3); \
62 b -= c; b -= a; b ^= (a << 10); \
63 c -= a; c -= b; c ^= (b >> 15); \
64 } while (/* CONSTCOND */0)
66 #define FIXED_SEED 0x9e3779b9 /* Golden ratio, arbitrary constant */
68 #ifdef __weak_alias
69 __weak_alias(mi_vector_hash, _mi_vector_hash)
70 #endif
72 void
73 mi_vector_hash(const void * __restrict key, size_t len, uint32_t seed,
74 uint32_t hashes[3])
76 static const uint32_t mask[4] = {
77 0x000000ff, 0x0000ffff, 0x00ffffff, 0xffffffff
79 uint32_t orig_len, a, b, c;
80 const uint8_t *k;
82 orig_len = (uint32_t)len;
84 a = b = FIXED_SEED;
85 c = seed;
87 if ((uintptr_t)key & 3) {
88 k = key;
89 while (len >= 12) {
90 a += le32dec(k);
91 b += le32dec(k + 4);
92 c += le32dec(k + 8);
93 mix(a, b, c);
94 k += 12;
95 len -= 12;
97 c += orig_len;
99 if (len > 8) {
100 switch (len) {
101 case 11:
102 c += (uint32_t)k[10] << 24;
103 /* FALLTHROUGH */
104 case 10:
105 c += (uint32_t)k[9] << 16;
106 /* FALLTHROUGH */
107 case 9:
108 c += (uint32_t)k[8] << 8;
109 /* FALLTHROUGH */
111 b += le32dec(k + 4);
112 a += le32dec(k);
113 } else if (len > 4) {
114 switch (len) {
115 case 8:
116 b += (uint32_t)k[7] << 24;
117 /* FALLTHROUGH */
118 case 7:
119 b += (uint32_t)k[6] << 16;
120 /* FALLTHROUGH */
121 case 6:
122 b += (uint32_t)k[5] << 8;
123 /* FALLTHROUGH */
124 case 5:
125 b += k[4];
126 /* FALLTHROUGH */
128 a += le32dec(k);
129 } else if (len) {
130 switch (len) {
131 case 4:
132 a += (uint32_t)k[3] << 24;
133 /* FALLTHROUGH */
134 case 3:
135 a += (uint32_t)k[2] << 16;
136 /* FALLTHROUGH */
137 case 2:
138 a += (uint32_t)k[1] << 8;
139 /* FALLTHROUGH */
140 case 1:
141 a += k[0];
142 /* FALLTHROUGH */
145 } else {
146 const uint32_t *key32 = key;
147 while (len >= 12) {
148 a += le32toh(key32[0]);
149 b += le32toh(key32[1]);
150 c += le32toh(key32[2]);
151 mix(a, b, c);
152 key32 += 3;
153 len -= 12;
155 c += orig_len;
157 if (len > 8) {
158 c += (le32toh(key32[2]) & mask[len - 9]) << 8;
159 b += le32toh(key32[1]);
160 a += le32toh(key32[0]);
161 } else if (len > 4) {
162 b += le32toh(key32[1]) & mask[len - 5];
163 a += le32toh(key32[0]);
164 } else if (len)
165 a += le32toh(key32[0]) & mask[len - 1];
167 mix(a, b, c);
168 hashes[0] = a;
169 hashes[1] = b;
170 hashes[2] = c;