2 -------------------------------------------------------------------------------
3 lookup3.c, by Bob Jenkins, May 2006, Public Domain.
5 These are functions for producing 32-bit hashes for hash table lookup.
6 hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
7 are externally useful functions. Routines to test the hash are included
8 if SELF_TEST is defined. You can use this free for any purpose. It's in
9 the public domain. It has no warranty.
11 You probably want to use hashlittle(). hashlittle() and hashbig()
12 hash byte arrays. hashlittle() is is faster than hashbig() on
13 little-endian machines. Intel and AMD are little-endian machines.
14 On second thought, you probably want hashlittle2(), which is identical to
15 hashlittle() except it returns two 32-bit hashes for the price of one.
16 You could implement hashbig2() if you wanted but I haven't bothered here.
18 If you want to find a hash of, say, exactly 7 integers, do
19 a = i1; b = i2; c = i3;
21 a += i4; b += i5; c += i6;
25 then use c as the hash value. If you have a variable length array of
26 4-byte integers to hash, use hashword(). If you have a byte array (like
27 a character string), use hashlittle(). If you have several byte arrays, or
28 a mix of things, see the comments above hashlittle().
30 Why is this so big? I read 12 bytes at a time into 3 4-byte integers,
31 then mix those integers. This is fast (you can do a lot more thorough
32 mixing with 12*3 instructions on 3 integers than you can with 3 instructions
33 on 1 byte), but shoehorning those bytes into integers efficiently is messy.
34 -------------------------------------------------------------------------------
38 #include <stdio.h> /* defines printf for tests */
39 #include <time.h> /* defines time_t for timings in the test */
40 #include <stdint.h> /* defines uint32_t etc */
41 #include <sys/param.h> /* attempt to define endianness */
43 # include <endian.h> /* attempt to define endianness */
47 * My best guess at if you are big-endian or little-endian. This may
50 #if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
51 __BYTE_ORDER == __LITTLE_ENDIAN) || \
52 (defined(i386) || defined(__i386__) || defined(__i486__) || \
53 defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL))
54 # define HASH_LITTLE_ENDIAN 1
55 # define HASH_BIG_ENDIAN 0
56 #elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
57 __BYTE_ORDER == __BIG_ENDIAN) || \
58 (defined(sparc) || defined(POWERPC) || defined(mc68000) || defined(sel))
59 # define HASH_LITTLE_ENDIAN 0
60 # define HASH_BIG_ENDIAN 1
62 # define HASH_LITTLE_ENDIAN 0
63 # define HASH_BIG_ENDIAN 0
66 #define hashsize(n) ((uint32_t)1<<(n))
67 #define hashmask(n) (hashsize(n)-1)
68 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
71 -------------------------------------------------------------------------------
72 mix -- mix 3 32-bit values reversibly.
74 This is reversible, so any information in (a,b,c) before mix() is
75 still in (a,b,c) after mix().
77 If four pairs of (a,b,c) inputs are run through mix(), or through
78 mix() in reverse, there are at least 32 bits of the output that
79 are sometimes the same for one pair and different for another pair.
81 * pairs that differed by one bit, by two bits, in any combination
82 of top bits of (a,b,c), or in any combination of bottom bits of
84 * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
85 the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
86 is commonly produced by subtraction) look like a single 1-bit
88 * the base values were pseudorandom, all zero but one bit set, or
89 all zero plus a counter that starts at zero.
91 Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
96 Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
97 for "differ" defined as + with a one-bit base and a two-bit delta. I
98 used http://burtleburtle.net/bob/hash/avalanche.html to choose
99 the operations, constants, and arrangements of the variables.
101 This does not achieve avalanche. There are input bits of (a,b,c)
102 that fail to affect some output bits of (a,b,c), especially of a. The
103 most thoroughly mixed value is c, but it doesn't really even achieve
106 This allows some parallelism. Read-after-writes are good at doubling
107 the number of bits affected, so the goal of mixing pulls in the opposite
108 direction as the goal of parallelism. I did what I could. Rotates
109 seem to cost as much as shifts on every machine I could lay my hands
110 on, and rotates are much kinder to the top and bottom bits, so I used
112 -------------------------------------------------------------------------------
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 -------------------------------------------------------------------------------
126 final -- final mixing of 3 32-bit values (a,b,c) into c
128 Pairs of (a,b,c) values differing in only a few bits will usually
129 produce values of c that look totally different. This was tested for
130 * pairs that differed by one bit, by two bits, in any combination
131 of top bits of (a,b,c), or in any combination of bottom bits of
133 * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
134 the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
135 is commonly produced by subtraction) look like a single 1-bit
137 * the base values were pseudorandom, all zero but one bit set, or
138 all zero plus a counter that starts at zero.
140 These constants passed:
143 and these came close:
147 -------------------------------------------------------------------------------
149 #define final(a,b,c) \
151 c ^= b; c -= rot(b,14); \
152 a ^= c; a -= rot(c,11); \
153 b ^= a; b -= rot(a,25); \
154 c ^= b; c -= rot(b,16); \
155 a ^= c; a -= rot(c,4); \
156 b ^= a; b -= rot(a,14); \
157 c ^= b; c -= rot(b,24); \
161 --------------------------------------------------------------------
162 This works on all machines. To be useful, it requires
163 -- that the key be an array of uint32_t's, and
164 -- that the length be the number of uint32_t's in the key
166 The function hashword() is identical to hashlittle() on little-endian
167 machines, and identical to hashbig() on big-endian machines,
168 except that the length has to be measured in uint32_ts rather than in
169 bytes. hashlittle() is more complicated than hashword() only because
170 hashlittle() has to dance around fitting the key bytes into registers.
171 --------------------------------------------------------------------
174 const uint32_t *k, /* the key, an array of uint32_t values */
175 size_t length, /* the length of the key, in uint32_ts */
176 uint32_t initval) /* the previous hash, or an arbitrary value */
180 /* Set up the internal state */
181 a = b = c = 0xdeadbeef + (((uint32_t)length)<<2) + initval;
183 /*------------------------------------------------- handle most of the key */
194 /*------------------------------------------- handle the last 3 uint32_t's */
195 switch(length) /* all the case statements fall through */
201 case 0: /* case 0: nothing left to add */
204 /*------------------------------------------------------ report the result */
210 --------------------------------------------------------------------
211 hashword2() -- same as hashword(), but take two seeds and return two
212 32-bit values. pc and pb must both be nonnull, and *pc and *pb must
213 both be initialized with seeds. If you pass in (*pb)==0, the output
214 (*pc) will be the same as the return value from hashword().
215 --------------------------------------------------------------------
218 const uint32_t *k, /* the key, an array of uint32_t values */
219 size_t length, /* the length of the key, in uint32_ts */
220 uint32_t *pc, /* IN: seed OUT: primary hash value */
221 uint32_t *pb) /* IN: more seed OUT: secondary hash value */
225 /* Set up the internal state */
226 a = b = c = 0xdeadbeef + ((uint32_t)(length<<2)) + *pc;
229 /*------------------------------------------------- handle most of the key */
240 /*------------------------------------------- handle the last 3 uint32_t's */
241 switch(length) /* all the case statements fall through */
247 case 0: /* case 0: nothing left to add */
250 /*------------------------------------------------------ report the result */
256 -------------------------------------------------------------------------------
257 hashlittle() -- hash a variable-length key into a 32-bit value
258 k : the key (the unaligned variable-length array of bytes)
259 length : the length of the key, counting by bytes
260 initval : can be any 4-byte value
261 Returns a 32-bit value. Every bit of the key affects every bit of
262 the return value. Two keys differing by one or two bits will have
263 totally different hash values.
265 The best hash table sizes are powers of 2. There is no need to do
266 mod a prime (mod is sooo slow!). If you need less than 32 bits,
267 use a bitmask. For example, if you need only 10 bits, do
268 h = (h & hashmask(10));
269 In which case, the hash table should have hashsize(10) elements.
271 If you are hashing n strings (uint8_t **)k, do it like this:
272 for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
274 By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
275 code any way you wish, private, educational, or commercial. It's free.
277 Use for hash table lookup, or anything where one collision in 2^^32 is
278 acceptable. Do NOT use for cryptographic purposes.
279 -------------------------------------------------------------------------------
282 uint32_t hashlittle( const void *key, size_t length, uint32_t initval)
284 uint32_t a,b,c; /* internal state */
285 union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
287 /* Set up the internal state */
288 a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
291 if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
292 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
296 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
307 /*----------------------------- handle the last (probably partial) block */
309 * "k[2]&0xffffff" actually reads beyond the end of the string, but
310 * then masks off the part it's not allowed to read. Because the
311 * string is aligned, the masked-off tail is in the same word as the
312 * rest of the string. Every machine with memory protection I've seen
313 * does it on word boundaries, so is OK with this. But VALGRIND will
314 * still catch it and complain. The masking trick does make the hash
315 * noticably faster for short strings (like English words).
321 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
322 case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
323 case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
324 case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
325 case 8 : b+=k[1]; a+=k[0]; break;
326 case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
327 case 6 : b+=k[1]&0xffff; a+=k[0]; break;
328 case 5 : b+=k[1]&0xff; a+=k[0]; break;
329 case 4 : a+=k[0]; break;
330 case 3 : a+=k[0]&0xffffff; break;
331 case 2 : a+=k[0]&0xffff; break;
332 case 1 : a+=k[0]&0xff; break;
333 case 0 : return c; /* zero length strings require no mixing */
336 #else /* make valgrind happy */
338 k8 = (const uint8_t *)k;
341 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
342 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
343 case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
344 case 9 : c+=k8[8]; /* fall through */
345 case 8 : b+=k[1]; a+=k[0]; break;
346 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
347 case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
348 case 5 : b+=k8[4]; /* fall through */
349 case 4 : a+=k[0]; break;
350 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
351 case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
352 case 1 : a+=k8[0]; break;
356 #endif /* !valgrind */
358 } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
359 const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
362 /*--------------- all but last block: aligned reads and different mixing */
365 a += k[0] + (((uint32_t)k[1])<<16);
366 b += k[2] + (((uint32_t)k[3])<<16);
367 c += k[4] + (((uint32_t)k[5])<<16);
373 /*----------------------------- handle the last (probably partial) block */
374 k8 = (const uint8_t *)k;
377 case 12: c+=k[4]+(((uint32_t)k[5])<<16);
378 b+=k[2]+(((uint32_t)k[3])<<16);
379 a+=k[0]+(((uint32_t)k[1])<<16);
381 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
383 b+=k[2]+(((uint32_t)k[3])<<16);
384 a+=k[0]+(((uint32_t)k[1])<<16);
386 case 9 : c+=k8[8]; /* fall through */
387 case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
388 a+=k[0]+(((uint32_t)k[1])<<16);
390 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
392 a+=k[0]+(((uint32_t)k[1])<<16);
394 case 5 : b+=k8[4]; /* fall through */
395 case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
397 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
402 case 0 : return c; /* zero length requires no mixing */
405 } else { /* need to read the key one byte at a time */
406 const uint8_t *k = (const uint8_t *)key;
408 /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
412 a += ((uint32_t)k[1])<<8;
413 a += ((uint32_t)k[2])<<16;
414 a += ((uint32_t)k[3])<<24;
416 b += ((uint32_t)k[5])<<8;
417 b += ((uint32_t)k[6])<<16;
418 b += ((uint32_t)k[7])<<24;
420 c += ((uint32_t)k[9])<<8;
421 c += ((uint32_t)k[10])<<16;
422 c += ((uint32_t)k[11])<<24;
428 /*-------------------------------- last block: affect all 32 bits of (c) */
429 switch(length) /* all the case statements fall through */
431 case 12: c+=((uint32_t)k[11])<<24;
432 case 11: c+=((uint32_t)k[10])<<16;
433 case 10: c+=((uint32_t)k[9])<<8;
435 case 8 : b+=((uint32_t)k[7])<<24;
436 case 7 : b+=((uint32_t)k[6])<<16;
437 case 6 : b+=((uint32_t)k[5])<<8;
439 case 4 : a+=((uint32_t)k[3])<<24;
440 case 3 : a+=((uint32_t)k[2])<<16;
441 case 2 : a+=((uint32_t)k[1])<<8;
454 * hashlittle2: return 2 32-bit hash values
456 * This is identical to hashlittle(), except it returns two 32-bit hash
457 * values instead of just one. This is good enough for hash table
458 * lookup with 2^^64 buckets, or if you want a second hash if you're not
459 * happy with the first, or if you want a probably-unique 64-bit ID for
460 * the key. *pc is better mixed than *pb, so use *pc first. If you want
461 * a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)".
464 const void *key, /* the key to hash */
465 size_t length, /* length of the key */
466 uint32_t *pc, /* IN: primary initval, OUT: primary hash */
467 uint32_t *pb) /* IN: secondary initval, OUT: secondary hash */
469 uint32_t a,b,c; /* internal state */
470 union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
472 /* Set up the internal state */
473 a = b = c = 0xdeadbeef + ((uint32_t)length) + *pc;
477 if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
478 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
483 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
494 /*----------------------------- handle the last (probably partial) block */
496 * "k[2]&0xffffff" actually reads beyond the end of the string, but
497 * then masks off the part it's not allowed to read. Because the
498 * string is aligned, the masked-off tail is in the same word as the
499 * rest of the string. Every machine with memory protection I've seen
500 * does it on word boundaries, so is OK with this. But VALGRIND will
501 * still catch it and complain. The masking trick does make the hash
502 * noticably faster for short strings (like English words).
508 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
509 case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
510 case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
511 case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
512 case 8 : b+=k[1]; a+=k[0]; break;
513 case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
514 case 6 : b+=k[1]&0xffff; a+=k[0]; break;
515 case 5 : b+=k[1]&0xff; a+=k[0]; break;
516 case 4 : a+=k[0]; break;
517 case 3 : a+=k[0]&0xffffff; break;
518 case 2 : a+=k[0]&0xffff; break;
519 case 1 : a+=k[0]&0xff; break;
520 case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
523 #else /* make valgrind happy */
525 k8 = (const uint8_t *)k;
528 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
529 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
530 case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
531 case 9 : c+=k8[8]; /* fall through */
532 case 8 : b+=k[1]; a+=k[0]; break;
533 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
534 case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
535 case 5 : b+=k8[4]; /* fall through */
536 case 4 : a+=k[0]; break;
537 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
538 case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
539 case 1 : a+=k8[0]; break;
540 case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
543 #endif /* !valgrind */
545 } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
546 const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
549 /*--------------- all but last block: aligned reads and different mixing */
552 a += k[0] + (((uint32_t)k[1])<<16);
553 b += k[2] + (((uint32_t)k[3])<<16);
554 c += k[4] + (((uint32_t)k[5])<<16);
560 /*----------------------------- handle the last (probably partial) block */
561 k8 = (const uint8_t *)k;
564 case 12: c+=k[4]+(((uint32_t)k[5])<<16);
565 b+=k[2]+(((uint32_t)k[3])<<16);
566 a+=k[0]+(((uint32_t)k[1])<<16);
568 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
570 b+=k[2]+(((uint32_t)k[3])<<16);
571 a+=k[0]+(((uint32_t)k[1])<<16);
573 case 9 : c+=k8[8]; /* fall through */
574 case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
575 a+=k[0]+(((uint32_t)k[1])<<16);
577 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
579 a+=k[0]+(((uint32_t)k[1])<<16);
581 case 5 : b+=k8[4]; /* fall through */
582 case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
584 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
589 case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
592 } else { /* need to read the key one byte at a time */
593 const uint8_t *k = (const uint8_t *)key;
595 /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
599 a += ((uint32_t)k[1])<<8;
600 a += ((uint32_t)k[2])<<16;
601 a += ((uint32_t)k[3])<<24;
603 b += ((uint32_t)k[5])<<8;
604 b += ((uint32_t)k[6])<<16;
605 b += ((uint32_t)k[7])<<24;
607 c += ((uint32_t)k[9])<<8;
608 c += ((uint32_t)k[10])<<16;
609 c += ((uint32_t)k[11])<<24;
615 /*-------------------------------- last block: affect all 32 bits of (c) */
616 switch(length) /* all the case statements fall through */
618 case 12: c+=((uint32_t)k[11])<<24;
619 case 11: c+=((uint32_t)k[10])<<16;
620 case 10: c+=((uint32_t)k[9])<<8;
622 case 8 : b+=((uint32_t)k[7])<<24;
623 case 7 : b+=((uint32_t)k[6])<<16;
624 case 6 : b+=((uint32_t)k[5])<<8;
626 case 4 : a+=((uint32_t)k[3])<<24;
627 case 3 : a+=((uint32_t)k[2])<<16;
628 case 2 : a+=((uint32_t)k[1])<<8;
631 case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
643 * This is the same as hashword() on big-endian machines. It is different
644 * from hashlittle() on all machines. hashbig() takes advantage of
645 * big-endian byte ordering.
647 uint32_t hashbig( const void *key, size_t length, uint32_t initval)
650 union { const void *ptr; size_t i; } u; /* to cast key to (size_t) happily */
652 /* Set up the internal state */
653 a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
656 if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
657 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
661 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
672 /*----------------------------- handle the last (probably partial) block */
674 * "k[2]<<8" actually reads beyond the end of the string, but
675 * then shifts out the part it's not allowed to read. Because the
676 * string is aligned, the illegal read is in the same word as the
677 * rest of the string. Every machine with memory protection I've seen
678 * does it on word boundaries, so is OK with this. But VALGRIND will
679 * still catch it and complain. The masking trick does make the hash
680 * noticably faster for short strings (like English words).
686 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
687 case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break;
688 case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break;
689 case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break;
690 case 8 : b+=k[1]; a+=k[0]; break;
691 case 7 : b+=k[1]&0xffffff00; a+=k[0]; break;
692 case 6 : b+=k[1]&0xffff0000; a+=k[0]; break;
693 case 5 : b+=k[1]&0xff000000; a+=k[0]; break;
694 case 4 : a+=k[0]; break;
695 case 3 : a+=k[0]&0xffffff00; break;
696 case 2 : a+=k[0]&0xffff0000; break;
697 case 1 : a+=k[0]&0xff000000; break;
698 case 0 : return c; /* zero length strings require no mixing */
701 #else /* make valgrind happy */
703 k8 = (const uint8_t *)k;
704 switch(length) /* all the case statements fall through */
706 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
707 case 11: c+=((uint32_t)k8[10])<<8; /* fall through */
708 case 10: c+=((uint32_t)k8[9])<<16; /* fall through */
709 case 9 : c+=((uint32_t)k8[8])<<24; /* fall through */
710 case 8 : b+=k[1]; a+=k[0]; break;
711 case 7 : b+=((uint32_t)k8[6])<<8; /* fall through */
712 case 6 : b+=((uint32_t)k8[5])<<16; /* fall through */
713 case 5 : b+=((uint32_t)k8[4])<<24; /* fall through */
714 case 4 : a+=k[0]; break;
715 case 3 : a+=((uint32_t)k8[2])<<8; /* fall through */
716 case 2 : a+=((uint32_t)k8[1])<<16; /* fall through */
717 case 1 : a+=((uint32_t)k8[0])<<24; break;
721 #endif /* !VALGRIND */
723 } else { /* need to read the key one byte at a time */
724 const uint8_t *k = (const uint8_t *)key;
726 /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
729 a += ((uint32_t)k[0])<<24;
730 a += ((uint32_t)k[1])<<16;
731 a += ((uint32_t)k[2])<<8;
732 a += ((uint32_t)k[3]);
733 b += ((uint32_t)k[4])<<24;
734 b += ((uint32_t)k[5])<<16;
735 b += ((uint32_t)k[6])<<8;
736 b += ((uint32_t)k[7]);
737 c += ((uint32_t)k[8])<<24;
738 c += ((uint32_t)k[9])<<16;
739 c += ((uint32_t)k[10])<<8;
740 c += ((uint32_t)k[11]);
746 /*-------------------------------- last block: affect all 32 bits of (c) */
747 switch(length) /* all the case statements fall through */
750 case 11: c+=((uint32_t)k[10])<<8;
751 case 10: c+=((uint32_t)k[9])<<16;
752 case 9 : c+=((uint32_t)k[8])<<24;
754 case 7 : b+=((uint32_t)k[6])<<8;
755 case 6 : b+=((uint32_t)k[5])<<16;
756 case 5 : b+=((uint32_t)k[4])<<24;
758 case 3 : a+=((uint32_t)k[2])<<8;
759 case 2 : a+=((uint32_t)k[1])<<16;
760 case 1 : a+=((uint32_t)k[0])<<24;
773 /* used for timings */
782 for (i=0; i<256; ++i) buf[i] = 'x';
785 h = hashlittle(&buf[0],1,h);
788 if (z-a > 0) printf("time %d %.8x\n", z-a, h);
791 /* check that every input bit changes every output bit half the time */
798 uint8_t qa[MAXLEN+1], qb[MAXLEN+2], *a = &qa[0], *b = &qb[1];
799 uint32_t c[HASHSTATE], d[HASHSTATE], i=0, j=0, k, l, m=0, z;
800 uint32_t e[HASHSTATE],f[HASHSTATE],g[HASHSTATE],h[HASHSTATE];
801 uint32_t x[HASHSTATE],y[HASHSTATE];
804 printf("No more than %d trials should ever be needed \n",MAXPAIR/2);
805 for (hlen=0; hlen < MAXLEN; ++hlen)
808 for (i=0; i<hlen; ++i) /*----------------------- for each input byte, */
810 for (j=0; j<8; ++j) /*------------------------ for each input bit, */
812 for (m=1; m<8; ++m) /*------------ for serveral possible initvals, */
814 for (l=0; l<HASHSTATE; ++l)
815 e[l]=f[l]=g[l]=h[l]=x[l]=y[l]=~((uint32_t)0);
817 /*---- check that every output bit is affected by that input bit */
818 for (k=0; k<MAXPAIR; k+=2)
821 /* keys have one bit different */
822 for (l=0; l<hlen+1; ++l) {a[l] = b[l] = (uint8_t)0;}
823 /* have a and b be two keys differing in only one bit */
826 c[0] = hashlittle(a, hlen, m);
828 b[i] ^= ((k+1)>>(8-j));
829 d[0] = hashlittle(b, hlen, m);
830 /* check every bit is 1, 0, set, and not set at least once */
831 for (l=0; l<HASHSTATE; ++l)
834 f[l] &= ~(c[l]^d[l]);
839 if (e[l]|f[l]|g[l]|h[l]|x[l]|y[l]) finished=0;
846 printf("Some bit didn't change: ");
847 printf("%.8x %.8x %.8x %.8x %.8x %.8x ",
848 e[0],f[0],g[0],h[0],x[0],y[0]);
849 printf("i %d j %d m %d len %d\n", i, j, m, hlen);
851 if (z==MAXPAIR) goto done;
858 printf("Mix success %2d bytes %2d initvals ",i,m);
859 printf("required %d trials\n", z/2);
865 /* Check for reading beyond the end of the buffer and alignment problems */
868 uint8_t buf[MAXLEN+20], *b;
870 uint8_t q[] = "This is the time for all good men to come to the aid of their country...";
872 uint8_t qq[] = "xThis is the time for all good men to come to the aid of their country...";
874 uint8_t qqq[] = "xxThis is the time for all good men to come to the aid of their country...";
876 uint8_t qqqq[] = "xxxThis is the time for all good men to come to the aid of their country...";
880 printf("Endianness. These lines should all be the same (for values filled in):\n");
881 printf("%.8x %.8x %.8x\n",
882 hashword((const uint32_t *)q, (sizeof(q)-1)/4, 13),
883 hashword((const uint32_t *)q, (sizeof(q)-5)/4, 13),
884 hashword((const uint32_t *)q, (sizeof(q)-9)/4, 13));
886 printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
887 hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
888 hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
889 hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
890 hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
891 hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
892 hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
894 printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
895 hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
896 hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
897 hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
898 hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
899 hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
900 hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
902 printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
903 hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
904 hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
905 hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
906 hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
907 hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
908 hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
910 printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
911 hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
912 hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
913 hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
914 hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
915 hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
916 hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
919 /* check that hashlittle2 and hashlittle produce the same results */
921 hashlittle2(q, sizeof(q), &i, &j);
922 if (hashlittle(q, sizeof(q), 47) != i)
923 printf("hashlittle2 and hashlittle mismatch\n");
925 /* check that hashword2 and hashword produce the same results */
928 hashword2(&len, 1, &i, &j);
929 if (hashword(&len, 1, 47) != i)
930 printf("hashword2 and hashword mismatch %x %x\n",
931 i, hashword(&len, 1, 47));
933 /* check hashlittle doesn't read before or after the ends of the string */
934 for (h=0, b=buf+1; h<8; ++h, ++b)
936 for (i=0; i<MAXLEN; ++i)
939 for (j=0; j<i; ++j) *(b+j)=0;
941 /* these should all be equal */
942 ref = hashlittle(b, len, (uint32_t)1);
945 x = hashlittle(b, len, (uint32_t)1);
946 y = hashlittle(b, len, (uint32_t)1);
947 if ((ref != x) || (ref != y))
949 printf("alignment error: %.8x %.8x %.8x %d %d\n",ref,x,y,
956 /* check for problems with nulls */
960 uint32_t h,i,state[HASHSTATE];
964 for (i=0; i<HASHSTATE; ++i) state[i] = 1;
965 printf("These should all be different\n");
966 for (i=0, h=0; i<8; ++i)
968 h = hashlittle(buf, 0, h);
969 printf("%2ld 0-byte strings, hash is %.8x\n", i, h);
976 driver1(); /* test that the key is hashed: used for timings */
977 driver2(); /* test that whole key is hashed thoroughly */
978 driver3(); /* test that nothing but the key is hashed */
979 driver4(); /* test hashing multiple buffers (all buffers are null) */
983 #endif /* SELF_TEST */