jhash.h revision 1.1 1 1.1 christos // SPDX-FileCopyrightText: 2009-2012 Mathieu Desnoyers <mathieu.desnoyers (at) efficios.com>
2 1.1 christos //
3 1.1 christos // SPDX-License-Identifier: MIT
4 1.1 christos
5 1.1 christos #ifndef _JHASH_H
6 1.1 christos #define _JHASH_H
7 1.1 christos
8 1.1 christos #if defined(__FreeBSD__)
9 1.1 christos #include <sys/endian.h>
10 1.1 christos #endif
11 1.1 christos
12 1.1 christos /*
13 1.1 christos * Example hash function.
14 1.1 christos */
15 1.1 christos
16 1.1 christos /*
17 1.1 christos * Hash function
18 1.1 christos * Source: http://burtleburtle.net/bob/c/lookup3.c
19 1.1 christos * Originally Public Domain
20 1.1 christos */
21 1.1 christos
22 1.1 christos #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
23 1.1 christos
24 1.1 christos #define mix(a, b, c) \
25 1.1 christos do { \
26 1.1 christos a -= c; a ^= rot(c, 4); c += b; \
27 1.1 christos b -= a; b ^= rot(a, 6); a += c; \
28 1.1 christos c -= b; c ^= rot(b, 8); b += a; \
29 1.1 christos a -= c; a ^= rot(c, 16); c += b; \
30 1.1 christos b -= a; b ^= rot(a, 19); a += c; \
31 1.1 christos c -= b; c ^= rot(b, 4); b += a; \
32 1.1 christos } while (0)
33 1.1 christos
34 1.1 christos #define final(a, b, c) \
35 1.1 christos { \
36 1.1 christos c ^= b; c -= rot(b, 14); \
37 1.1 christos a ^= c; a -= rot(c, 11); \
38 1.1 christos b ^= a; b -= rot(a, 25); \
39 1.1 christos c ^= b; c -= rot(b, 16); \
40 1.1 christos a ^= c; a -= rot(c, 4); \
41 1.1 christos b ^= a; b -= rot(a, 14); \
42 1.1 christos c ^= b; c -= rot(b, 24); \
43 1.1 christos }
44 1.1 christos
45 1.1 christos #if (BYTE_ORDER == LITTLE_ENDIAN)
46 1.1 christos #define HASH_LITTLE_ENDIAN 1
47 1.1 christos #else
48 1.1 christos #define HASH_LITTLE_ENDIAN 0
49 1.1 christos #endif
50 1.1 christos
51 1.1 christos /*
52 1.1 christos *
53 1.1 christos * hashlittle() -- hash a variable-length key into a 32-bit value
54 1.1 christos * k : the key (the unaligned variable-length array of bytes)
55 1.1 christos * length : the length of the key, counting by bytes
56 1.1 christos * initval : can be any 4-byte value
57 1.1 christos * Returns a 32-bit value. Every bit of the key affects every bit of
58 1.1 christos * the return value. Two keys differing by one or two bits will have
59 1.1 christos * totally different hash values.
60 1.1 christos *
61 1.1 christos * The best hash table sizes are powers of 2. There is no need to do
62 1.1 christos * mod a prime (mod is sooo slow!). If you need less than 32 bits,
63 1.1 christos * use a bitmask. For example, if you need only 10 bits, do
64 1.1 christos * h = (h & hashmask(10));
65 1.1 christos * In which case, the hash table should have hashsize(10) elements.
66 1.1 christos *
67 1.1 christos * If you are hashing n strings (uint8_t **)k, do it like this:
68 1.1 christos * for (i = 0, h = 0; i < n; ++i) h = hashlittle(k[i], len[i], h);
69 1.1 christos *
70 1.1 christos * By Bob Jenkins, 2006. bob_jenkins (at) burtleburtle.net. You may use this
71 1.1 christos * code any way you wish, private, educational, or commercial. It's free.
72 1.1 christos *
73 1.1 christos * Use for hash table lookup, or anything where one collision in 2^^32 is
74 1.1 christos * acceptable. Do NOT use for cryptographic purposes.
75 1.1 christos */
76 1.1 christos static
77 1.1 christos uint32_t hashlittle(const void *key, size_t length, uint32_t initval)
78 1.1 christos {
79 1.1 christos uint32_t a, b, c; /* internal state */
80 1.1 christos union {
81 1.1 christos const void *ptr;
82 1.1 christos size_t i;
83 1.1 christos } u;
84 1.1 christos
85 1.1 christos /* Set up the internal state */
86 1.1 christos a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
87 1.1 christos
88 1.1 christos u.ptr = key;
89 1.1 christos if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
90 1.1 christos const uint32_t *k = (const uint32_t *) key; /* read 32-bit chunks */
91 1.1 christos
92 1.1 christos /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
93 1.1 christos while (length > 12) {
94 1.1 christos a += k[0];
95 1.1 christos b += k[1];
96 1.1 christos c += k[2];
97 1.1 christos mix(a, b, c);
98 1.1 christos length -= 12;
99 1.1 christos k += 3;
100 1.1 christos }
101 1.1 christos
102 1.1 christos /*----------------------------- handle the last (probably partial) block */
103 1.1 christos /*
104 1.1 christos * "k[2]&0xffffff" actually reads beyond the end of the string, but
105 1.1 christos * then masks off the part it's not allowed to read. Because the
106 1.1 christos * string is aligned, the masked-off tail is in the same word as the
107 1.1 christos * rest of the string. Every machine with memory protection I've seen
108 1.1 christos * does it on word boundaries, so is OK with this. But VALGRIND will
109 1.1 christos * still catch it and complain. The masking trick does make the hash
110 1.1 christos * noticeably faster for short strings (like English words).
111 1.1 christos */
112 1.1 christos #ifndef VALGRIND
113 1.1 christos
114 1.1 christos switch (length) {
115 1.1 christos case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
116 1.1 christos case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
117 1.1 christos case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
118 1.1 christos case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
119 1.1 christos case 8 : b+=k[1]; a+=k[0]; break;
120 1.1 christos case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
121 1.1 christos case 6 : b+=k[1]&0xffff; a+=k[0]; break;
122 1.1 christos case 5 : b+=k[1]&0xff; a+=k[0]; break;
123 1.1 christos case 4 : a+=k[0]; break;
124 1.1 christos case 3 : a+=k[0]&0xffffff; break;
125 1.1 christos case 2 : a+=k[0]&0xffff; break;
126 1.1 christos case 1 : a+=k[0]&0xff; break;
127 1.1 christos case 0 : return c; /* zero length strings require no mixing */
128 1.1 christos }
129 1.1 christos
130 1.1 christos #else /* make valgrind happy */
131 1.1 christos {
132 1.1 christos const uint8_t *k8;
133 1.1 christos
134 1.1 christos k8 = (const uint8_t *) k;
135 1.1 christos switch (length) {
136 1.1 christos case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
137 1.1 christos case 11: c+=((uint32_t) k8[10])<<16; /* fall through */
138 1.1 christos case 10: c+=((uint32_t) k8[9])<<8; /* fall through */
139 1.1 christos case 9 : c+=k8[8]; /* fall through */
140 1.1 christos case 8 : b+=k[1]; a+=k[0]; break;
141 1.1 christos case 7 : b+=((uint32_t) k8[6])<<16; /* fall through */
142 1.1 christos case 6 : b+=((uint32_t) k8[5])<<8; /* fall through */
143 1.1 christos case 5 : b+=k8[4]; /* fall through */
144 1.1 christos case 4 : a+=k[0]; break;
145 1.1 christos case 3 : a+=((uint32_t) k8[2])<<16; /* fall through */
146 1.1 christos case 2 : a+=((uint32_t) k8[1])<<8; /* fall through */
147 1.1 christos case 1 : a+=k8[0]; break;
148 1.1 christos case 0 : return c;
149 1.1 christos }
150 1.1 christos }
151 1.1 christos #endif /* !valgrind */
152 1.1 christos
153 1.1 christos } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
154 1.1 christos const uint16_t *k = (const uint16_t *) key; /* read 16-bit chunks */
155 1.1 christos const uint8_t *k8;
156 1.1 christos
157 1.1 christos /*--------------- all but last block: aligned reads and different mixing */
158 1.1 christos while (length > 12)
159 1.1 christos {
160 1.1 christos a += k[0] + (((uint32_t) k[1])<<16);
161 1.1 christos b += k[2] + (((uint32_t) k[3])<<16);
162 1.1 christos c += k[4] + (((uint32_t) k[5])<<16);
163 1.1 christos mix(a, b, c);
164 1.1 christos length -= 12;
165 1.1 christos k += 6;
166 1.1 christos }
167 1.1 christos
168 1.1 christos /*----------------------------- handle the last (probably partial) block */
169 1.1 christos k8 = (const uint8_t *) k;
170 1.1 christos switch(length)
171 1.1 christos {
172 1.1 christos case 12: c+=k[4]+(((uint32_t) k[5])<<16);
173 1.1 christos b+=k[2]+(((uint32_t) k[3])<<16);
174 1.1 christos a+=k[0]+(((uint32_t) k[1])<<16);
175 1.1 christos break;
176 1.1 christos case 11: c+=((uint32_t) k8[10])<<16; /* fall through */
177 1.1 christos case 10: c+=k[4];
178 1.1 christos b+=k[2]+(((uint32_t) k[3])<<16);
179 1.1 christos a+=k[0]+(((uint32_t) k[1])<<16);
180 1.1 christos break;
181 1.1 christos case 9 : c+=k8[8]; /* fall through */
182 1.1 christos case 8 : b+=k[2]+(((uint32_t) k[3])<<16);
183 1.1 christos a+=k[0]+(((uint32_t) k[1])<<16);
184 1.1 christos break;
185 1.1 christos case 7 : b+=((uint32_t) k8[6])<<16; /* fall through */
186 1.1 christos case 6 : b+=k[2];
187 1.1 christos a+=k[0]+(((uint32_t) k[1])<<16);
188 1.1 christos break;
189 1.1 christos case 5 : b+=k8[4]; /* fall through */
190 1.1 christos case 4 : a+=k[0]+(((uint32_t) k[1])<<16);
191 1.1 christos break;
192 1.1 christos case 3 : a+=((uint32_t) k8[2])<<16; /* fall through */
193 1.1 christos case 2 : a+=k[0];
194 1.1 christos break;
195 1.1 christos case 1 : a+=k8[0];
196 1.1 christos break;
197 1.1 christos case 0 : return c; /* zero length requires no mixing */
198 1.1 christos }
199 1.1 christos
200 1.1 christos } else { /* need to read the key one byte at a time */
201 1.1 christos const uint8_t *k = (const uint8_t *)key;
202 1.1 christos
203 1.1 christos /*--------------- all but the last block: affect some 32 bits of (a, b, c) */
204 1.1 christos while (length > 12) {
205 1.1 christos a += k[0];
206 1.1 christos a += ((uint32_t) k[1])<<8;
207 1.1 christos a += ((uint32_t) k[2])<<16;
208 1.1 christos a += ((uint32_t) k[3])<<24;
209 1.1 christos b += k[4];
210 1.1 christos b += ((uint32_t) k[5])<<8;
211 1.1 christos b += ((uint32_t) k[6])<<16;
212 1.1 christos b += ((uint32_t) k[7])<<24;
213 1.1 christos c += k[8];
214 1.1 christos c += ((uint32_t) k[9])<<8;
215 1.1 christos c += ((uint32_t) k[10])<<16;
216 1.1 christos c += ((uint32_t) k[11])<<24;
217 1.1 christos mix(a,b,c);
218 1.1 christos length -= 12;
219 1.1 christos k += 12;
220 1.1 christos }
221 1.1 christos
222 1.1 christos /*-------------------------------- last block: affect all 32 bits of (c) */
223 1.1 christos switch (length) { /* all the case statements fall through */
224 1.1 christos case 12: c+=((uint32_t) k[11])<<24; /* fall through */
225 1.1 christos case 11: c+=((uint32_t) k[10])<<16; /* fall through */
226 1.1 christos case 10: c+=((uint32_t) k[9])<<8; /* fall through */
227 1.1 christos case 9 : c+=k[8]; /* fall through */
228 1.1 christos case 8 : b+=((uint32_t) k[7])<<24; /* fall through */
229 1.1 christos case 7 : b+=((uint32_t) k[6])<<16; /* fall through */
230 1.1 christos case 6 : b+=((uint32_t) k[5])<<8; /* fall through */
231 1.1 christos case 5 : b+=k[4]; /* fall through */
232 1.1 christos case 4 : a+=((uint32_t) k[3])<<24; /* fall through */
233 1.1 christos case 3 : a+=((uint32_t) k[2])<<16; /* fall through */
234 1.1 christos case 2 : a+=((uint32_t) k[1])<<8; /* fall through */
235 1.1 christos case 1 : a+=k[0];
236 1.1 christos break;
237 1.1 christos case 0 : return c;
238 1.1 christos }
239 1.1 christos }
240 1.1 christos
241 1.1 christos final(a, b, c);
242 1.1 christos return c;
243 1.1 christos }
244 1.1 christos
245 1.1 christos static inline
246 1.1 christos uint32_t jhash(const void *key, size_t length, uint32_t seed)
247 1.1 christos {
248 1.1 christos return hashlittle(key, length, seed);
249 1.1 christos }
250 1.1 christos
251 1.1 christos #endif /* _JHASH_H */
252