2 * Copyright (C) - Bob Jenkins, May 2006, Public Domain.
3 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * Copyright (C) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * These are functions for producing 32-bit hashes for hash table lookup.
7 * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final() are
8 * externally useful functions. Routines to test the hash are included if
9 * SELF_TEST is defined. You can use this free for any purpose. It's in the
10 * public domain. It has no warranty.
12 * You probably want to use hashlittle(). hashlittle() and hashbig() hash byte
13 * arrays. hashlittle() is is faster than hashbig() on little-endian machines.
14 * Intel and AMD are little-endian machines. On second thought, you probably
15 * want hashlittle2(), which is identical to hashlittle() except it returns two
16 * 32-bit hashes for the price of one. You could implement hashbig2() if you
17 * wanted but I haven't bothered here.
19 * If you want to find a hash of, say, exactly 7 integers, do
20 * a = i1; b = i2; c = i3;
22 * a += i4; b += i5; c += i6;
26 * then use c as the hash value. If you have a variable length array of
27 * 4-byte integers to hash, use hashword(). If you have a byte array (like
28 * a character string), use hashlittle(). If you have several byte arrays, or
29 * a mix of things, see the comments above hashlittle().
31 * Why is this so big? I read 12 bytes at a time into 3 4-byte integers, then
32 * mix those integers. This is fast (you can do a lot more thorough mixing
33 * with 12*3 instructions on 3 integers than you can with 3 instructions on 1
34 * byte), but shoehorning those bytes into integers efficiently is messy.
38 #include <endian.h> /* attempt to define endianness */
39 #include <stdint.h> /* defines uint32_t etc */
40 #include <stdio.h> /* defines printf for tests */
42 #include <sys/param.h> /* attempt to define endianness */
43 #include <time.h> /* defines time_t for timings in the test */
44 #include <urcu/compiler.h>
49 * My best guess at if you are big-endian or little-endian. This may
52 #if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
53 __BYTE_ORDER == __LITTLE_ENDIAN) || \
54 (defined(i386) || defined(__i386__) || defined(__i486__) || \
55 defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL))
56 # define HASH_LITTLE_ENDIAN 1
57 # define HASH_BIG_ENDIAN 0
58 #elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
59 __BYTE_ORDER == __BIG_ENDIAN) || \
60 (defined(sparc) || defined(POWERPC) || defined(mc68000) || defined(sel))
61 # define HASH_LITTLE_ENDIAN 0
62 # define HASH_BIG_ENDIAN 1
64 # define HASH_LITTLE_ENDIAN 0
65 # define HASH_BIG_ENDIAN 0
68 #define hashsize(n) ((uint32_t)1<<(n))
69 #define hashmask(n) (hashsize(n)-1)
70 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
73 * mix -- mix 3 32-bit values reversibly.
75 * This is reversible, so any information in (a,b,c) before mix() is
76 * still in (a,b,c) after mix().
78 * If four pairs of (a,b,c) inputs are run through mix(), or through
79 * mix() in reverse, there are at least 32 bits of the output that
80 * are sometimes the same for one pair and different for another pair.
81 * This was tested for:
82 * * pairs that differed by one bit, by two bits, in any combination
83 * of top bits of (a,b,c), or in any combination of bottom bits of
85 * * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
86 * the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
87 * is commonly produced by subtraction) look like a single 1-bit
89 * * the base values were pseudorandom, all zero but one bit set, or
90 * all zero plus a counter that starts at zero.
92 * Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
97 * Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
98 * for "differ" defined as + with a one-bit base and a two-bit delta. I
99 * used http://burtleburtle.net/bob/hash/avalanche.html to choose
100 * the operations, constants, and arrangements of the variables.
102 * This does not achieve avalanche. There are input bits of (a,b,c)
103 * that fail to affect some output bits of (a,b,c), especially of a. The
104 * most thoroughly mixed value is c, but it doesn't really even achieve
107 * This allows some parallelism. Read-after-writes are good at doubling
108 * the number of bits affected, so the goal of mixing pulls in the opposite
109 * direction as the goal of parallelism. I did what I could. Rotates
110 * seem to cost as much as shifts on every machine I could lay my hands
111 * on, and rotates are much kinder to the top and bottom bits, so I used
116 a -= c; a ^= rot(c, 4); c += b; \
117 b -= a; b ^= rot(a, 6); a += c; \
118 c -= b; c ^= rot(b, 8); b += a; \
119 a -= c; a ^= rot(c,16); c += b; \
120 b -= a; b ^= rot(a,19); a += c; \
121 c -= b; c ^= rot(b, 4); b += a; \
125 * final -- final mixing of 3 32-bit values (a,b,c) into c
127 * Pairs of (a,b,c) values differing in only a few bits will usually
128 * produce values of c that look totally different. This was tested for
129 * * pairs that differed by one bit, by two bits, in any combination
130 * of top bits of (a,b,c), or in any combination of bottom bits of
132 * * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
133 * the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
134 * is commonly produced by subtraction) look like a single 1-bit
136 * * the base values were pseudorandom, all zero but one bit set, or
137 * all zero plus a counter that starts at zero.
139 * These constants passed:
140 * 14 11 25 16 4 14 24
141 * 12 14 25 16 4 14 24
142 * and these came close:
147 #define final(a,b,c) \
149 c ^= b; c -= rot(b,14); \
150 a ^= c; a -= rot(c,11); \
151 b ^= a; b -= rot(a,25); \
152 c ^= b; c -= rot(b,16); \
153 a ^= c; a -= rot(c,4); \
154 b ^= a; b -= rot(a,14); \
155 c ^= b; c -= rot(b,24); \
159 * k - the key, an array of uint32_t values
160 * length - the length of the key, in uint32_ts
161 * initval - the previous hash, or an arbitrary value
163 static uint32_t __attribute__((unused
)) hashword(const uint32_t *k
,
164 size_t length
, uint32_t initval
)
168 /* Set up the internal state */
169 a
= b
= c
= 0xdeadbeef + (((uint32_t) length
) << 2) + initval
;
171 /*----------------------------------------- handle most of the key */
181 /*----------------------------------- handle the last 3 uint32_t's */
182 switch (length
) { /* all the case statements fall through */
187 case 0: /* case 0: nothing left to add */
190 /*---------------------------------------------- report the result */
196 * hashword2() -- same as hashword(), but take two seeds and return two 32-bit
197 * values. pc and pb must both be nonnull, and *pc and *pb must both be
198 * initialized with seeds. If you pass in (*pb)==0, the output (*pc) will be
199 * the same as the return value from hashword().
201 static void __attribute__((unused
)) hashword2(const uint32_t *k
, size_t length
,
202 uint32_t *pc
, uint32_t *pb
)
206 /* Set up the internal state */
207 a
= b
= c
= 0xdeadbeef + ((uint32_t) (length
<< 2)) + *pc
;
227 case 0: /* case 0: nothing left to add */
236 * hashlittle() -- hash a variable-length key into a 32-bit value
237 * k : the key (the unaligned variable-length array of bytes)
238 * length : the length of the key, counting by bytes
239 * initval : can be any 4-byte value
240 * Returns a 32-bit value. Every bit of the key affects every bit of
241 * the return value. Two keys differing by one or two bits will have
242 * totally different hash values.
244 * The best hash table sizes are powers of 2. There is no need to do
245 * mod a prime (mod is sooo slow!). If you need less than 32 bits,
246 * use a bitmask. For example, if you need only 10 bits, do
247 * h = (h & hashmask(10));
248 * In which case, the hash table should have hashsize(10) elements.
250 * If you are hashing n strings (uint8_t **)k, do it like this:
251 * for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
253 * By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
254 * code any way you wish, private, educational, or commercial. It's free.
256 * Use for hash table lookup, or anything where one collision in 2^^32 is
257 * acceptable. Do NOT use for cryptographic purposes.
259 static uint32_t __attribute__((unused
)) hashlittle(const void *key
,
260 size_t length
, uint32_t initval
)
266 } u
; /* needed for Mac Powerbook G4 */
268 /* Set up the internal state */
269 a
= b
= c
= 0xdeadbeef + ((uint32_t)length
) + initval
;
272 if (HASH_LITTLE_ENDIAN
&& ((u
.i
& 0x3) == 0)) {
273 const uint32_t *k
= (const uint32_t *)key
; /* read 32-bit chunks */
275 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
276 while (length
> 12) {
286 * "k[2]&0xffffff" actually reads beyond the end of the string, but
287 * then masks off the part it's not allowed to read. Because the
288 * string is aligned, the masked-off tail is in the same word as the
289 * rest of the string. Every machine with memory protection I've seen
290 * does it on word boundaries, so is OK with this. But VALGRIND will
291 * still catch it and complain. The masking trick does make the hash
292 * noticably faster for short strings (like English words).
297 case 12: c
+=k
[2]; b
+=k
[1]; a
+=k
[0]; break;
298 case 11: c
+=k
[2]&0xffffff; b
+=k
[1]; a
+=k
[0]; break;
299 case 10: c
+=k
[2]&0xffff; b
+=k
[1]; a
+=k
[0]; break;
300 case 9 : c
+=k
[2]&0xff; b
+=k
[1]; a
+=k
[0]; break;
301 case 8 : b
+=k
[1]; a
+=k
[0]; break;
302 case 7 : b
+=k
[1]&0xffffff; a
+=k
[0]; break;
303 case 6 : b
+=k
[1]&0xffff; a
+=k
[0]; break;
304 case 5 : b
+=k
[1]&0xff; a
+=k
[0]; break;
305 case 4 : a
+=k
[0]; break;
306 case 3 : a
+=k
[0]&0xffffff; break;
307 case 2 : a
+=k
[0]&0xffff; break;
308 case 1 : a
+=k
[0]&0xff; break;
309 case 0 : return c
; /* zero length strings require no mixing */
311 #else /* make valgrind happy */
314 k8
= (const uint8_t *)k
;
316 case 12: c
+=k
[2]; b
+=k
[1]; a
+=k
[0]; break;
317 case 11: c
+=((uint32_t)k8
[10])<<16; /* fall through */
318 case 10: c
+=((uint32_t)k8
[9])<<8; /* fall through */
319 case 9 : c
+=k8
[8]; /* fall through */
320 case 8 : b
+=k
[1]; a
+=k
[0]; break;
321 case 7 : b
+=((uint32_t)k8
[6])<<16; /* fall through */
322 case 6 : b
+=((uint32_t)k8
[5])<<8; /* fall through */
323 case 5 : b
+=k8
[4]; /* fall through */
324 case 4 : a
+=k
[0]; break;
325 case 3 : a
+=((uint32_t)k8
[2])<<16; /* fall through */
326 case 2 : a
+=((uint32_t)k8
[1])<<8; /* fall through */
327 case 1 : a
+=k8
[0]; break;
330 #endif /* !valgrind */
331 } else if (HASH_LITTLE_ENDIAN
&& ((u
.i
& 0x1) == 0)) {
332 const uint16_t *k
= (const uint16_t *)key
; /* read 16-bit chunks */
335 /*--------------- all but last block: aligned reads and different mixing */
336 while (length
> 12) {
337 a
+= k
[0] + (((uint32_t)k
[1])<<16);
338 b
+= k
[2] + (((uint32_t)k
[3])<<16);
339 c
+= k
[4] + (((uint32_t)k
[5])<<16);
345 k8
= (const uint8_t *)k
;
348 c
+=k
[4]+(((uint32_t)k
[5])<<16);
349 b
+=k
[2]+(((uint32_t)k
[3])<<16);
350 a
+=k
[0]+(((uint32_t)k
[1])<<16);
353 c
+=((uint32_t)k8
[10])<<16; /* fall through */
356 b
+=k
[2]+(((uint32_t)k
[3])<<16);
357 a
+=k
[0]+(((uint32_t)k
[1])<<16);
360 c
+=k8
[8]; /* fall through */
362 b
+=k
[2]+(((uint32_t)k
[3])<<16);
363 a
+=k
[0]+(((uint32_t)k
[1])<<16);
366 b
+=((uint32_t)k8
[6])<<16; /* fall through */
369 a
+=k
[0]+(((uint32_t)k
[1])<<16);
372 b
+=k8
[4]; /* fall through */
374 a
+=k
[0]+(((uint32_t)k
[1])<<16);
377 a
+=((uint32_t)k8
[2])<<16; /* fall through */
385 return c
; /* zero length requires no mixing */
388 } else { /* need to read the key one byte at a time */
389 const uint8_t *k
= (const uint8_t *)key
;
391 while (length
> 12) {
393 a
+= ((uint32_t)k
[1])<<8;
394 a
+= ((uint32_t)k
[2])<<16;
395 a
+= ((uint32_t)k
[3])<<24;
397 b
+= ((uint32_t)k
[5])<<8;
398 b
+= ((uint32_t)k
[6])<<16;
399 b
+= ((uint32_t)k
[7])<<24;
401 c
+= ((uint32_t)k
[9])<<8;
402 c
+= ((uint32_t)k
[10])<<16;
403 c
+= ((uint32_t)k
[11])<<24;
409 switch(length
) { /* all the case statements fall through */
410 case 12: c
+=((uint32_t)k
[11])<<24;
411 case 11: c
+=((uint32_t)k
[10])<<16;
412 case 10: c
+=((uint32_t)k
[9])<<8;
414 case 8: b
+=((uint32_t)k
[7])<<24;
415 case 7: b
+=((uint32_t)k
[6])<<16;
416 case 6: b
+=((uint32_t)k
[5])<<8;
418 case 4: a
+=((uint32_t)k
[3])<<24;
419 case 3: a
+=((uint32_t)k
[2])<<16;
420 case 2: a
+=((uint32_t)k
[1])<<8;
433 #if (CAA_BITS_PER_LONG == 64)
435 * Hash function for number value.
437 unsigned long hash_key_ulong(void *_key
, unsigned long seed
)
448 v
.v64
= (uint64_t) seed
;
449 key
.v64
= (uint64_t) _key
;
450 hashword2(key
.v32
, 2, &v
.v32
[0], &v
.v32
[1]);
455 * Hash function for number value.
457 unsigned long hash_key_ulong(void *_key
, unsigned long seed
)
459 uint32_t key
= (uint32_t) _key
;
461 return hashword(&key
, 1, seed
);
463 #endif /* CAA_BITS_PER_LONG */
466 * Hash function for string.
468 unsigned long hash_key_str(void *key
, unsigned long seed
)
470 return hashlittle(key
, strlen((char *) key
), seed
);
474 * Hash function compare for number value.
476 int hash_match_key_ulong(void *key1
, void *key2
)
486 * Hash compare function for string.
488 int hash_match_key_str(void *key1
, void *key2
)
490 if (strcmp(key1
, key2
) == 0) {
This page took 0.055685 seconds and 4 git commands to generate.