1 /* Slightly modified by Lennart Poettering, to avoid name clashes, and
2 * unexport a few functions. */
7 -------------------------------------------------------------------------------
8 lookup3.c, by Bob Jenkins, May 2006, Public Domain.
10 These are functions for producing 32-bit hashes for hash table lookup.
11 hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
12 are externally useful functions. Routines to test the hash are included
13 if SELF_TEST is defined. You can use this free for any purpose. It's in
14 the public domain. It has no warranty.
16 You probably want to use hashlittle(). hashlittle() and hashbig()
17 hash byte arrays. hashlittle() is faster than hashbig() on
18 little-endian machines. Intel and AMD are little-endian machines.
19 On second thought, you probably want hashlittle2(), which is identical to
20 hashlittle() except it returns two 32-bit hashes for the price of one.
21 You could implement hashbig2() if you wanted but I haven't bothered here.
23 If you want to find a hash of, say, exactly 7 integers, do
24 a = i1; b = i2; c = i3;
26 a += i4; b += i5; c += i6;
30 then use c as the hash value. If you have a variable length array of
31 4-byte integers to hash, use hashword(). If you have a byte array (like
32 a character string), use hashlittle(). If you have several byte arrays, or
33 a mix of things, see the comments above hashlittle().
35 Why is this so big? I read 12 bytes at a time into 3 4-byte integers,
36 then mix those integers. This is fast (you can do a lot more thorough
37 mixing with 12*3 instructions on 3 integers than you can with 3 instructions
38 on 1 byte), but shoehorning those bytes into integers efficiently is messy.
39 -------------------------------------------------------------------------------
41 /* #define SELF_TEST 1 */
43 #include <stdio.h> /* defines printf for tests */
44 #include <time.h> /* defines time_t for timings in the test */
45 #include <stdint.h> /* defines uint32_t etc */
46 #include <sys/param.h> /* attempt to define endianness */
48 # include <endian.h> /* attempt to define endianness */
52 * My best guess at if you are big-endian or little-endian. This may
55 #if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
56 __BYTE_ORDER == __LITTLE_ENDIAN) || \
57 (defined(i386) || defined(__i386__) || defined(__i486__) || \
58 defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL))
59 # define HASH_LITTLE_ENDIAN 1
60 # define HASH_BIG_ENDIAN 0
61 #elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
62 __BYTE_ORDER == __BIG_ENDIAN) || \
63 (defined(sparc) || defined(POWERPC) || defined(mc68000) || defined(sel))
64 # define HASH_LITTLE_ENDIAN 0
65 # define HASH_BIG_ENDIAN 1
67 # define HASH_LITTLE_ENDIAN 0
68 # define HASH_BIG_ENDIAN 0
71 #define hashsize(n) ((uint32_t)1<<(n))
72 #define hashmask(n) (hashsize(n)-1)
73 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
76 -------------------------------------------------------------------------------
77 mix -- mix 3 32-bit values reversibly.
79 This is reversible, so any information in (a,b,c) before mix() is
80 still in (a,b,c) after mix().
82 If four pairs of (a,b,c) inputs are run through mix(), or through
83 mix() in reverse, there are at least 32 bits of the output that
84 are sometimes the same for one pair and different for another pair.
86 * pairs that differed by one bit, by two bits, in any combination
87 of top bits of (a,b,c), or in any combination of bottom bits of
89 * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
90 the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
91 is commonly produced by subtraction) look like a single 1-bit
93 * the base values were pseudorandom, all zero but one bit set, or
94 all zero plus a counter that starts at zero.
96 Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
101 Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
102 for "differ" defined as + with a one-bit base and a two-bit delta. I
103 used http://burtleburtle.net/bob/hash/avalanche.html to choose
104 the operations, constants, and arrangements of the variables.
106 This does not achieve avalanche. There are input bits of (a,b,c)
107 that fail to affect some output bits of (a,b,c), especially of a. The
108 most thoroughly mixed value is c, but it doesn't really even achieve
111 This allows some parallelism. Read-after-writes are good at doubling
112 the number of bits affected, so the goal of mixing pulls in the opposite
113 direction as the goal of parallelism. I did what I could. Rotates
114 seem to cost as much as shifts on every machine I could lay my hands
115 on, and rotates are much kinder to the top and bottom bits, so I used
117 -------------------------------------------------------------------------------
121 a -= c; a ^= rot(c, 4); c += b; \
122 b -= a; b ^= rot(a, 6); a += c; \
123 c -= b; c ^= rot(b, 8); b += a; \
124 a -= c; a ^= rot(c,16); c += b; \
125 b -= a; b ^= rot(a,19); a += c; \
126 c -= b; c ^= rot(b, 4); b += a; \
130 -------------------------------------------------------------------------------
131 final -- final mixing of 3 32-bit values (a,b,c) into c
133 Pairs of (a,b,c) values differing in only a few bits will usually
134 produce values of c that look totally different. This was tested for
135 * pairs that differed by one bit, by two bits, in any combination
136 of top bits of (a,b,c), or in any combination of bottom bits of
138 * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
139 the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
140 is commonly produced by subtraction) look like a single 1-bit
142 * the base values were pseudorandom, all zero but one bit set, or
143 all zero plus a counter that starts at zero.
145 These constants passed:
148 and these came close:
152 -------------------------------------------------------------------------------
154 #define final(a,b,c) \
156 c ^= b; c -= rot(b,14); \
157 a ^= c; a -= rot(c,11); \
158 b ^= a; b -= rot(a,25); \
159 c ^= b; c -= rot(b,16); \
160 a ^= c; a -= rot(c,4); \
161 b ^= a; b -= rot(a,14); \
162 c ^= b; c -= rot(b,24); \
166 --------------------------------------------------------------------
167 This works on all machines. To be useful, it requires
168 -- that the key be an array of uint32_t's, and
169 -- that the length be the number of uint32_t's in the key
171 The function hashword() is identical to hashlittle() on little-endian
172 machines, and identical to hashbig() on big-endian machines,
173 except that the length has to be measured in uint32_ts rather than in
174 bytes. hashlittle() is more complicated than hashword() only because
175 hashlittle() has to dance around fitting the key bytes into registers.
176 --------------------------------------------------------------------
178 uint32_t jenkins_hashword(
179 const uint32_t *k, /* the key, an array of uint32_t values */
180 size_t length, /* the length of the key, in uint32_ts */
181 uint32_t initval) /* the previous hash, or an arbitrary value */
185 /* Set up the internal state */
186 a = b = c = 0xdeadbeef + (((uint32_t)length)<<2) + initval;
188 /*------------------------------------------------- handle most of the key */
199 /*------------------------------------------- handle the last 3 uint32_t's */
200 switch(length) /* all the case statements fall through */
206 case 0: /* case 0: nothing left to add */
209 /*------------------------------------------------------ report the result */
215 --------------------------------------------------------------------
216 hashword2() -- same as hashword(), but take two seeds and return two
217 32-bit values. pc and pb must both be nonnull, and *pc and *pb must
218 both be initialized with seeds. If you pass in (*pb)==0, the output
219 (*pc) will be the same as the return value from hashword().
220 --------------------------------------------------------------------
222 void jenkins_hashword2 (
223 const uint32_t *k, /* the key, an array of uint32_t values */
224 size_t length, /* the length of the key, in uint32_ts */
225 uint32_t *pc, /* IN: seed OUT: primary hash value */
226 uint32_t *pb) /* IN: more seed OUT: secondary hash value */
230 /* Set up the internal state */
231 a = b = c = 0xdeadbeef + ((uint32_t)(length<<2)) + *pc;
234 /*------------------------------------------------- handle most of the key */
245 /*------------------------------------------- handle the last 3 uint32_t's */
246 switch(length) /* all the case statements fall through */
252 case 0: /* case 0: nothing left to add */
255 /*------------------------------------------------------ report the result */
261 -------------------------------------------------------------------------------
262 hashlittle() -- hash a variable-length key into a 32-bit value
263 k : the key (the unaligned variable-length array of bytes)
264 length : the length of the key, counting by bytes
265 initval : can be any 4-byte value
266 Returns a 32-bit value. Every bit of the key affects every bit of
267 the return value. Two keys differing by one or two bits will have
268 totally different hash values.
270 The best hash table sizes are powers of 2. There is no need to do
271 mod a prime (mod is sooo slow!). If you need less than 32 bits,
272 use a bitmask. For example, if you need only 10 bits, do
273 h = (h & hashmask(10));
274 In which case, the hash table should have hashsize(10) elements.
276 If you are hashing n strings (uint8_t **)k, do it like this:
277 for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
279 By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
280 code any way you wish, private, educational, or commercial. It's free.
282 Use for hash table lookup, or anything where one collision in 2^^32 is
283 acceptable. Do NOT use for cryptographic purposes.
284 -------------------------------------------------------------------------------
287 uint32_t jenkins_hashlittle( const void *key, size_t length, uint32_t initval)
289 uint32_t a,b,c; /* internal state */
290 union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
292 /* Set up the internal state */
293 a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
296 if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
297 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
299 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
310 /*----------------------------- handle the last (probably partial) block */
312 * "k[2]&0xffffff" actually reads beyond the end of the string, but
313 * then masks off the part it's not allowed to read. Because the
314 * string is aligned, the masked-off tail is in the same word as the
315 * rest of the string. Every machine with memory protection I've seen
316 * does it on word boundaries, so is OK with this. But VALGRIND will
317 * still catch it and complain. The masking trick does make the hash
318 * noticeably faster for short strings (like English words).
324 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
325 case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
326 case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
327 case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
328 case 8 : b+=k[1]; a+=k[0]; break;
329 case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
330 case 6 : b+=k[1]&0xffff; a+=k[0]; break;
331 case 5 : b+=k[1]&0xff; a+=k[0]; break;
332 case 4 : a+=k[0]; break;
333 case 3 : a+=k[0]&0xffffff; break;
334 case 2 : a+=k[0]&0xffff; break;
335 case 1 : a+=k[0]&0xff; break;
336 case 0 : return c; /* zero length strings require no mixing */
339 #else /* make valgrind happy */
341 const uint8_t *k8 = (const uint8_t *) k;
345 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
346 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
347 case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
348 case 9 : c+=k8[8]; /* fall through */
349 case 8 : b+=k[1]; a+=k[0]; break;
350 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
351 case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
352 case 5 : b+=k8[4]; /* fall through */
353 case 4 : a+=k[0]; break;
354 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
355 case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
356 case 1 : a+=k8[0]; break;
361 #endif /* !valgrind */
363 } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
364 const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
367 /*--------------- all but last block: aligned reads and different mixing */
370 a += k[0] + (((uint32_t)k[1])<<16);
371 b += k[2] + (((uint32_t)k[3])<<16);
372 c += k[4] + (((uint32_t)k[5])<<16);
378 /*----------------------------- handle the last (probably partial) block */
379 k8 = (const uint8_t *)k;
382 case 12: c+=k[4]+(((uint32_t)k[5])<<16);
383 b+=k[2]+(((uint32_t)k[3])<<16);
384 a+=k[0]+(((uint32_t)k[1])<<16);
386 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
388 b+=k[2]+(((uint32_t)k[3])<<16);
389 a+=k[0]+(((uint32_t)k[1])<<16);
391 case 9 : c+=k8[8]; /* fall through */
392 case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
393 a+=k[0]+(((uint32_t)k[1])<<16);
395 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
397 a+=k[0]+(((uint32_t)k[1])<<16);
399 case 5 : b+=k8[4]; /* fall through */
400 case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
402 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
407 case 0 : return c; /* zero length requires no mixing */
410 } else { /* need to read the key one byte at a time */
411 const uint8_t *k = (const uint8_t *)key;
413 /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
417 a += ((uint32_t)k[1])<<8;
418 a += ((uint32_t)k[2])<<16;
419 a += ((uint32_t)k[3])<<24;
421 b += ((uint32_t)k[5])<<8;
422 b += ((uint32_t)k[6])<<16;
423 b += ((uint32_t)k[7])<<24;
425 c += ((uint32_t)k[9])<<8;
426 c += ((uint32_t)k[10])<<16;
427 c += ((uint32_t)k[11])<<24;
433 /*-------------------------------- last block: affect all 32 bits of (c) */
434 switch(length) /* all the case statements fall through */
436 case 12: c+=((uint32_t)k[11])<<24;
437 case 11: c+=((uint32_t)k[10])<<16;
438 case 10: c+=((uint32_t)k[9])<<8;
440 case 8 : b+=((uint32_t)k[7])<<24;
441 case 7 : b+=((uint32_t)k[6])<<16;
442 case 6 : b+=((uint32_t)k[5])<<8;
444 case 4 : a+=((uint32_t)k[3])<<24;
445 case 3 : a+=((uint32_t)k[2])<<16;
446 case 2 : a+=((uint32_t)k[1])<<8;
459 * hashlittle2: return 2 32-bit hash values
461 * This is identical to hashlittle(), except it returns two 32-bit hash
462 * values instead of just one. This is good enough for hash table
463 * lookup with 2^^64 buckets, or if you want a second hash if you're not
464 * happy with the first, or if you want a probably-unique 64-bit ID for
465 * the key. *pc is better mixed than *pb, so use *pc first. If you want
466 * a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)".
468 void jenkins_hashlittle2(
469 const void *key, /* the key to hash */
470 size_t length, /* length of the key */
471 uint32_t *pc, /* IN: primary initval, OUT: primary hash */
472 uint32_t *pb) /* IN: secondary initval, OUT: secondary hash */
474 uint32_t a,b,c; /* internal state */
475 union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
477 /* Set up the internal state */
478 a = b = c = 0xdeadbeef + ((uint32_t)length) + *pc;
482 if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
483 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
485 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
496 /*----------------------------- handle the last (probably partial) block */
498 * "k[2]&0xffffff" actually reads beyond the end of the string, but
499 * then masks off the part it's not allowed to read. Because the
500 * string is aligned, the masked-off tail is in the same word as the
501 * rest of the string. Every machine with memory protection I've seen
502 * does it on word boundaries, so is OK with this. But VALGRIND will
503 * still catch it and complain. The masking trick does make the hash
504 * noticeably faster for short strings (like English words).
510 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
511 case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
512 case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
513 case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
514 case 8 : b+=k[1]; a+=k[0]; break;
515 case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
516 case 6 : b+=k[1]&0xffff; a+=k[0]; break;
517 case 5 : b+=k[1]&0xff; a+=k[0]; break;
518 case 4 : a+=k[0]; break;
519 case 3 : a+=k[0]&0xffffff; break;
520 case 2 : a+=k[0]&0xffff; break;
521 case 1 : a+=k[0]&0xff; break;
522 case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
525 #else /* make valgrind happy */
528 const uint8_t *k8 = (const uint8_t *)k;
531 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
532 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
533 case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
534 case 9 : c+=k8[8]; /* fall through */
535 case 8 : b+=k[1]; a+=k[0]; break;
536 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
537 case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
538 case 5 : b+=k8[4]; /* fall through */
539 case 4 : a+=k[0]; break;
540 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
541 case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
542 case 1 : a+=k8[0]; break;
543 case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
547 #endif /* !valgrind */
549 } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
550 const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
553 /*--------------- all but last block: aligned reads and different mixing */
556 a += k[0] + (((uint32_t)k[1])<<16);
557 b += k[2] + (((uint32_t)k[3])<<16);
558 c += k[4] + (((uint32_t)k[5])<<16);
564 /*----------------------------- handle the last (probably partial) block */
565 k8 = (const uint8_t *)k;
568 case 12: c+=k[4]+(((uint32_t)k[5])<<16);
569 b+=k[2]+(((uint32_t)k[3])<<16);
570 a+=k[0]+(((uint32_t)k[1])<<16);
572 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
574 b+=k[2]+(((uint32_t)k[3])<<16);
575 a+=k[0]+(((uint32_t)k[1])<<16);
577 case 9 : c+=k8[8]; /* fall through */
578 case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
579 a+=k[0]+(((uint32_t)k[1])<<16);
581 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
583 a+=k[0]+(((uint32_t)k[1])<<16);
585 case 5 : b+=k8[4]; /* fall through */
586 case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
588 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
593 case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
596 } else { /* need to read the key one byte at a time */
597 const uint8_t *k = (const uint8_t *)key;
599 /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
603 a += ((uint32_t)k[1])<<8;
604 a += ((uint32_t)k[2])<<16;
605 a += ((uint32_t)k[3])<<24;
607 b += ((uint32_t)k[5])<<8;
608 b += ((uint32_t)k[6])<<16;
609 b += ((uint32_t)k[7])<<24;
611 c += ((uint32_t)k[9])<<8;
612 c += ((uint32_t)k[10])<<16;
613 c += ((uint32_t)k[11])<<24;
619 /*-------------------------------- last block: affect all 32 bits of (c) */
620 switch(length) /* all the case statements fall through */
622 case 12: c+=((uint32_t)k[11])<<24;
623 case 11: c+=((uint32_t)k[10])<<16;
624 case 10: c+=((uint32_t)k[9])<<8;
626 case 8 : b+=((uint32_t)k[7])<<24;
627 case 7 : b+=((uint32_t)k[6])<<16;
628 case 6 : b+=((uint32_t)k[5])<<8;
630 case 4 : a+=((uint32_t)k[3])<<24;
631 case 3 : a+=((uint32_t)k[2])<<16;
632 case 2 : a+=((uint32_t)k[1])<<8;
635 case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
647 * This is the same as hashword() on big-endian machines. It is different
648 * from hashlittle() on all machines. hashbig() takes advantage of
649 * big-endian byte ordering.
651 uint32_t jenkins_hashbig( const void *key, size_t length, uint32_t initval)
654 union { const void *ptr; size_t i; } u; /* to cast key to (size_t) happily */
656 /* Set up the internal state */
657 a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
660 if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
661 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
663 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
674 /*----------------------------- handle the last (probably partial) block */
676 * "k[2]<<8" actually reads beyond the end of the string, but
677 * then shifts out the part it's not allowed to read. Because the
678 * string is aligned, the illegal read is in the same word as the
679 * rest of the string. Every machine with memory protection I've seen
680 * does it on word boundaries, so is OK with this. But VALGRIND will
681 * still catch it and complain. The masking trick does make the hash
682 * noticeably faster for short strings (like English words).
688 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
689 case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break;
690 case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break;
691 case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break;
692 case 8 : b+=k[1]; a+=k[0]; break;
693 case 7 : b+=k[1]&0xffffff00; a+=k[0]; break;
694 case 6 : b+=k[1]&0xffff0000; a+=k[0]; break;
695 case 5 : b+=k[1]&0xff000000; a+=k[0]; break;
696 case 4 : a+=k[0]; break;
697 case 3 : a+=k[0]&0xffffff00; break;
698 case 2 : a+=k[0]&0xffff0000; break;
699 case 1 : a+=k[0]&0xff000000; break;
700 case 0 : return c; /* zero length strings require no mixing */
703 #else /* make valgrind happy */
706 const uint8_t *k8 = (const uint8_t *)k;
707 switch(length) /* all the case statements fall through */
709 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
710 case 11: c+=((uint32_t)k8[10])<<8; /* fall through */
711 case 10: c+=((uint32_t)k8[9])<<16; /* fall through */
712 case 9 : c+=((uint32_t)k8[8])<<24; /* fall through */
713 case 8 : b+=k[1]; a+=k[0]; break;
714 case 7 : b+=((uint32_t)k8[6])<<8; /* fall through */
715 case 6 : b+=((uint32_t)k8[5])<<16; /* fall through */
716 case 5 : b+=((uint32_t)k8[4])<<24; /* fall through */
717 case 4 : a+=k[0]; break;
718 case 3 : a+=((uint32_t)k8[2])<<8; /* fall through */
719 case 2 : a+=((uint32_t)k8[1])<<16; /* fall through */
720 case 1 : a+=((uint32_t)k8[0])<<24; break;
725 #endif /* !VALGRIND */
727 } else { /* need to read the key one byte at a time */
728 const uint8_t *k = (const uint8_t *)key;
730 /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
733 a += ((uint32_t)k[0])<<24;
734 a += ((uint32_t)k[1])<<16;
735 a += ((uint32_t)k[2])<<8;
736 a += ((uint32_t)k[3]);
737 b += ((uint32_t)k[4])<<24;
738 b += ((uint32_t)k[5])<<16;
739 b += ((uint32_t)k[6])<<8;
740 b += ((uint32_t)k[7]);
741 c += ((uint32_t)k[8])<<24;
742 c += ((uint32_t)k[9])<<16;
743 c += ((uint32_t)k[10])<<8;
744 c += ((uint32_t)k[11]);
750 /*-------------------------------- last block: affect all 32 bits of (c) */
751 switch(length) /* all the case statements fall through */
754 case 11: c+=((uint32_t)k[10])<<8;
755 case 10: c+=((uint32_t)k[9])<<16;
756 case 9 : c+=((uint32_t)k[8])<<24;
758 case 7 : b+=((uint32_t)k[6])<<8;
759 case 6 : b+=((uint32_t)k[5])<<16;
760 case 5 : b+=((uint32_t)k[4])<<24;
762 case 3 : a+=((uint32_t)k[2])<<8;
763 case 2 : a+=((uint32_t)k[1])<<16;
764 case 1 : a+=((uint32_t)k[0])<<24;
777 /* used for timings */
786 for (i=0; i<256; ++i) buf[i] = 'x';
789 h = hashlittle(&buf[0],1,h);
792 if (z-a > 0) printf("time %d %.8x\n", z-a, h);
795 /* check that every input bit changes every output bit half the time */
802 uint8_t qa[MAXLEN+1], qb[MAXLEN+2], *a = &qa[0], *b = &qb[1];
803 uint32_t c[HASHSTATE], d[HASHSTATE], i=0, j=0, k, l, m=0, z;
804 uint32_t e[HASHSTATE],f[HASHSTATE],g[HASHSTATE],h[HASHSTATE];
805 uint32_t x[HASHSTATE],y[HASHSTATE];
808 printf("No more than %d trials should ever be needed \n",MAXPAIR/2);
809 for (hlen=0; hlen < MAXLEN; ++hlen)
812 for (i=0; i<hlen; ++i) /*----------------------- for each input byte, */
814 for (j=0; j<8; ++j) /*------------------------ for each input bit, */
816 for (m=1; m<8; ++m) /*------------ for serveral possible initvals, */
818 for (l=0; l<HASHSTATE; ++l)
819 e[l]=f[l]=g[l]=h[l]=x[l]=y[l]=~((uint32_t)0);
821 /*---- check that every output bit is affected by that input bit */
822 for (k=0; k<MAXPAIR; k+=2)
825 /* keys have one bit different */
826 for (l=0; l<hlen+1; ++l) {a[l] = b[l] = (uint8_t)0;}
827 /* have a and b be two keys differing in only one bit */
830 c[0] = hashlittle(a, hlen, m);
832 b[i] ^= ((k+1)>>(8-j));
833 d[0] = hashlittle(b, hlen, m);
834 /* check every bit is 1, 0, set, and not set at least once */
835 for (l=0; l<HASHSTATE; ++l)
838 f[l] &= ~(c[l]^d[l]);
843 if (e[l]|f[l]|g[l]|h[l]|x[l]|y[l]) finished=0;
850 printf("Some bit didn't change: ");
851 printf("%.8x %.8x %.8x %.8x %.8x %.8x ",
852 e[0],f[0],g[0],h[0],x[0],y[0]);
853 printf("i %d j %d m %d len %d\n", i, j, m, hlen);
855 if (z==MAXPAIR) goto done;
862 printf("Mix success %2d bytes %2d initvals ",i,m);
863 printf("required %d trials\n", z/2);
869 /* Check for reading beyond the end of the buffer and alignment problems */
872 uint8_t buf[MAXLEN+20], *b;
874 uint8_t q[] = "This is the time for all good men to come to the aid of their country...";
876 uint8_t qq[] = "xThis is the time for all good men to come to the aid of their country...";
878 uint8_t qqq[] = "xxThis is the time for all good men to come to the aid of their country...";
880 uint8_t qqqq[] = "xxxThis is the time for all good men to come to the aid of their country...";
884 printf("Endianness. These lines should all be the same (for values filled in):\n");
885 printf("%.8x %.8x %.8x\n",
886 hashword((const uint32_t *)q, (sizeof(q)-1)/4, 13),
887 hashword((const uint32_t *)q, (sizeof(q)-5)/4, 13),
888 hashword((const uint32_t *)q, (sizeof(q)-9)/4, 13));
890 printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
891 hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
892 hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
893 hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
894 hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
895 hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
896 hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
898 printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
899 hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
900 hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
901 hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
902 hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
903 hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
904 hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
906 printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
907 hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
908 hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
909 hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
910 hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
911 hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
912 hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
914 printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
915 hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
916 hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
917 hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
918 hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
919 hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
920 hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
923 /* check that hashlittle2 and hashlittle produce the same results */
925 hashlittle2(q, sizeof(q), &i, &j);
926 if (hashlittle(q, sizeof(q), 47) != i)
927 printf("hashlittle2 and hashlittle mismatch\n");
929 /* check that hashword2 and hashword produce the same results */
932 hashword2(&len, 1, &i, &j);
933 if (hashword(&len, 1, 47) != i)
934 printf("hashword2 and hashword mismatch %x %x\n",
935 i, hashword(&len, 1, 47));
937 /* check hashlittle doesn't read before or after the ends of the string */
938 for (h=0, b=buf+1; h<8; ++h, ++b)
940 for (i=0; i<MAXLEN; ++i)
943 for (j=0; j<i; ++j) *(b+j)=0;
945 /* these should all be equal */
946 ref = hashlittle(b, len, (uint32_t)1);
949 x = hashlittle(b, len, (uint32_t)1);
950 y = hashlittle(b, len, (uint32_t)1);
951 if ((ref != x) || (ref != y))
953 printf("alignment error: %.8x %.8x %.8x %d %d\n",ref,x,y,
960 /* check for problems with nulls */
964 uint32_t h,i,state[HASHSTATE];
968 for (i=0; i<HASHSTATE; ++i) state[i] = 1;
969 printf("These should all be different\n");
970 for (i=0, h=0; i<8; ++i)
972 h = hashlittle(buf, 0, h);
973 printf("%2ld 0-byte strings, hash is %.8x\n", i, h);
980 b=0, c=0, hashlittle2("", 0, &c, &b);
981 printf("hash is %.8lx %.8lx\n", c, b); /* deadbeef deadbeef */
982 b=0xdeadbeef, c=0, hashlittle2("", 0, &c, &b);
983 printf("hash is %.8lx %.8lx\n", c, b); /* bd5b7dde deadbeef */
984 b=0xdeadbeef, c=0xdeadbeef, hashlittle2("", 0, &c, &b);
985 printf("hash is %.8lx %.8lx\n", c, b); /* 9c093ccd bd5b7dde */
986 b=0, c=0, hashlittle2("Four score and seven years ago", 30, &c, &b);
987 printf("hash is %.8lx %.8lx\n", c, b); /* 17770551 ce7226e6 */
988 b=1, c=0, hashlittle2("Four score and seven years ago", 30, &c, &b);
989 printf("hash is %.8lx %.8lx\n", c, b); /* e3607cae bd371de4 */
990 b=0, c=1, hashlittle2("Four score and seven years ago", 30, &c, &b);
991 printf("hash is %.8lx %.8lx\n", c, b); /* cd628161 6cbea4b3 */
992 c = hashlittle("Four score and seven years ago", 30, 0);
993 printf("hash is %.8lx\n", c); /* 17770551 */
994 c = hashlittle("Four score and seven years ago", 30, 1);
995 printf("hash is %.8lx\n", c); /* cd628161 */
1001 driver1(); /* test that the key is hashed: used for timings */
1002 driver2(); /* test that whole key is hashed thoroughly */
1003 driver3(); /* test that nothing but the key is hashed */
1004 driver4(); /* test hashing multiple buffers (all buffers are null) */
1005 driver5(); /* test the hash against known vectors */
1009 #endif /* SELF_TEST */