1 /*
2  * http://burtleburtle.net/bob/c/lookup3.c
3  */
4 
5 /*
6  * ----------------------------------------------------------------------------
7  * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
8  *
9  * These are functions for producing 32-bit hashes for hash table lookup.
10  * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
11  * are externally useful functions.  Routines to test the hash are included
12  * if SELF_TEST is defined.  You can use this free for any purpose.  It's in
13  * the public domain.  It has no warranty.
14  *
15  * You probably want to use hashlittle().  hashlittle() and hashbig()
16  * hash byte arrays.  hashlittle() is is faster than hashbig() on
17  * little-endian machines.  Intel and AMD are little-endian machines.
18  * On second thought, you probably want hashlittle2(), which is identical to
19  * hashlittle() except it returns two 32-bit hashes for the price of one.
20  * You could implement hashbig2() if you wanted but I haven't bothered here.
21  *
22  * If you want to find a hash of, say, exactly 7 integers, do
23  *   a = i1;  b = i2;  c = i3;
24  *   mix(a,b,c);
25  *   a += i4; b += i5; c += i6;
26  *   mix(a,b,c);
27  *   a += i7;
28  *   final(a,b,c);
29  * then use c as the hash value.  If you have a variable length array of
30  * 4-byte integers to hash, use hashword().  If you have a byte array (like
31  * a character string), use hashlittle().  If you have several byte arrays, or
32  * a mix of things, see the comments above hashlittle().
33  *
34  * Why is this so big?  I read 12 bytes at a time into 3 4-byte integers,
35  * then mix those integers.  This is fast (you can do a lot more thorough
36  * mixing with 12*3 instructions on 3 integers than you can with 3 instructions
37  * on 1 byte), but shoehorning those bytes into integers efficiently is messy.
38  * ----------------------------------------------------------------------------
39  */
40 #define SELF_TEST 0
41 
42 #if SK_LITTLE_ENDIAN
43 # define HASH_LITTLE_ENDIAN 1
44 # define HASH_BIG_ENDIAN 0
45 #elif SK_BIG_ENDIAN
46 # define HASH_LITTLE_ENDIAN 0
47 # define HASH_BIG_ENDIAN 1
48 #else
49 # define HASH_LITTLE_ENDIAN 0
50 # define HASH_BIG_ENDIAN 0
51 #endif
52 
53 #define hashsize(n) ((uint32_t)1<<(n))
54 #define hashmask(n) (hashsize(n)-1)
55 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
56 
57 /*
58  * ----------------------------------------------------------------------------
59  * mix -- mix 3 32-bit values reversibly.
60  *
61  * This is reversible, so any information in (a,b,c) before mix() is
62  * still in (a,b,c) after mix().
63  *
64  * If four pairs of (a,b,c) inputs are run through mix(), or through
65  * mix() in reverse, there are at least 32 bits of the output that
66  * are sometimes the same for one pair and different for another pair.
67  * This was tested for:
68  * * pairs that differed by one bit, by two bits, in any combination
69  *   of top bits of (a,b,c), or in any combination of bottom bits of
70  *   (a,b,c).
71  * * "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
72  *   the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
73  *   is commonly produced by subtraction) look like a single 1-bit
74  *   difference.
75  * * the base values were pseudorandom, all zero but one bit set, or
76  *   all zero plus a counter that starts at zero.
77  *
78  * Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
79  * satisfy this are
80  *     4  6  8 16 19  4
81  *     9 15  3 18 27 15
82  *    14  9  3  7 17  3
83  * Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
84  * for "differ" defined as + with a one-bit base and a two-bit delta.  I
85  * used http://burtleburtle.net/bob/hash/avalanche.html to choose
86  * the operations, constants, and arrangements of the variables.
87  *
88  * This does not achieve avalanche.  There are input bits of (a,b,c)
89  * that fail to affect some output bits of (a,b,c), especially of a.  The
90  * most thoroughly mixed value is c, but it doesn't really even achieve
91  * avalanche in c.
92  *
93  * This allows some parallelism.  Read-after-writes are good at doubling
94  * the number of bits affected, so the goal of mixing pulls in the opposite
95  * direction as the goal of parallelism.  I did what I could.  Rotates
96  * seem to cost as much as shifts on every machine I could lay my hands
97  * on, and rotates are much kinder to the top and bottom bits, so I used
98  * rotates.
99  * ----------------------------------------------------------------------------
100  */
101 #define mix(a,b,c)                              \
102     {                                           \
103         a -= c;  a ^= rot(c, 4);  c += b;       \
104         b -= a;  b ^= rot(a, 6);  a += c;       \
105         c -= b;  c ^= rot(b, 8);  b += a;       \
106         a -= c;  a ^= rot(c,16);  c += b;       \
107         b -= a;  b ^= rot(a,19);  a += c;       \
108         c -= b;  c ^= rot(b, 4);  b += a;       \
109     }
110 
111 /*
112  * ----------------------------------------------------------------------------
113  * final -- final mixing of 3 32-bit values (a,b,c) into c
114  *
115  * Pairs of (a,b,c) values differing in only a few bits will usually
116  * produce values of c that look totally different.  This was tested for
117  * * pairs that differed by one bit, by two bits, in any combination
118  *   of top bits of (a,b,c), or in any combination of bottom bits of
119  *   (a,b,c).
120  * * "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
121  *   the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
122  *   is commonly produced by subtraction) look like a single 1-bit
123  *   difference.
124  * * the base values were pseudorandom, all zero but one bit set, or
125  *   all zero plus a counter that starts at zero.
126  *
127  * These constants passed:
128  *  14 11 25 16 4 14 24
129  *  12 14 25 16 4 14 24
130  * and these came close:
131  *   4  8 15 26 3 22 24
132  *  10  8 15 26 3 22 24
133  *  11  8 15 26 3 22 24
134  * ----------------------------------------------------------------------------
135  */
136 #define final(a,b,c)                            \
137     {                                           \
138         c ^= b; c -= rot(b,14);                 \
139         a ^= c; a -= rot(c,11);                 \
140         b ^= a; b -= rot(a,25);                 \
141         c ^= b; c -= rot(b,16);                 \
142         a ^= c; a -= rot(c,4);                  \
143         b ^= a; b -= rot(a,14);                 \
144         c ^= b; c -= rot(b,24);                 \
145     }
146 
147 /*
148  * --------------------------------------------------------------------
149  *  This works on all machines.  To be useful, it requires
150  *  -- that the key be an array of uint32_t's, and
151  *  -- that the length be the number of uint32_t's in the key
152  *
153  *  The function hashword() is identical to hashlittle() on little-endian
154  *  machines, and identical to hashbig() on big-endian machines,
155  *  except that the length has to be measured in uint32_ts rather than in
156  *  bytes.  hashlittle() is more complicated than hashword() only because
157  *  hashlittle() has to dance around fitting the key bytes into registers.
158  * --------------------------------------------------------------------
159  */
hashword(const uint32_t * k,size_t length,uint32_t initval)160 uint32_t hashword(
161     const uint32_t *k,       /* the key, an array of uint32_t values */
162     size_t          length,  /* the length of the key, in uint32_ts */
163     uint32_t        initval) /* the previous hash, or an arbitrary value */
164 {
165     uint32_t a,b,c;
166 
167     /* Set up the internal state */
168     a = b = c = 0xdeadbeef + (((uint32_t)length)<<2) + initval;
169 
170     /* handle most of the key */
171     while (length > 3)
172     {
173         a += k[0];
174         b += k[1];
175         c += k[2];
176         mix(a,b,c);
177         length -= 3;
178         k += 3;
179     }
180 
181     /* handle the last 3 uint32_t's */
182     switch(length)              /* all the case statements fall through */
183     {
184       case 3 : c+=k[2];         /* fall through */
185       case 2 : b+=k[1];         /* fall through */
186       case 1 : a+=k[0];
187         final(a,b,c);           /* fall through */
188       case 0:     /* case 0: nothing left to add */
189         break;
190     }
191     /* report the result */
192     return c;
193 }
194 
195 
196 /*
197  * --------------------------------------------------------------------
198  * hashword2() -- same as hashword(), but take two seeds and return two
199  * 32-bit values.  pc and pb must both be nonnull, and *pc and *pb must
200  * both be initialized with seeds.  If you pass in (*pb)==0, the output
201  * (*pc) will be the same as the return value from hashword().
202  * --------------------------------------------------------------------
203  */
hashword2(const uint32_t * k,size_t length,uint32_t * pc,uint32_t * pb)204 void hashword2(
205     const uint32_t *k,      /* the key, an array of uint32_t values */
206     size_t          length, /* the length of the key, in uint32_ts */
207     uint32_t       *pc,     /* IN: seed OUT: primary hash value */
208     uint32_t       *pb)     /* IN: more seed OUT: secondary hash value */
209 {
210     uint32_t a,b,c;
211 
212     /* Set up the internal state */
213     a = b = c = 0xdeadbeef + ((uint32_t)(length<<2)) + *pc;
214     c += *pb;
215 
216     /* handle most of the key */
217     while (length > 3)
218     {
219         a += k[0];
220         b += k[1];
221         c += k[2];
222         mix(a,b,c);
223         length -= 3;
224         k += 3;
225     }
226 
227     /* handle the last 3 uint32_t's */
228     switch(length)              /* all the case statements fall through */
229     {
230       case 3 : c+=k[2];         /* fall through */
231       case 2 : b+=k[1];         /* fall through */
232       case 1 : a+=k[0];
233       final(a,b,c);             /* fall through */
234       case 0:     /* case 0: nothing left to add */
235       break;
236     }
237     /* report the result */
238     *pc=c; *pb=b;
239 }
240 
241 
242 /*
243  * ----------------------------------------------------------------------------
244  * hashlittle() -- hash a variable-length key into a 32-bit value
245  *   k       : the key (the unaligned variable-length array of bytes)
246  *   length  : the length of the key, counting by bytes
247  *   initval : can be any 4-byte value
248  * Returns a 32-bit value.  Every bit of the key affects every bit of
249  * the return value.  Two keys differing by one or two bits will have
250  * totally different hash values.
251  *
252  * The best hash table sizes are powers of 2.  There is no need to do
253  * mod a prime (mod is sooo slow!).  If you need less than 32 bits,
254  * use a bitmask.  For example, if you need only 10 bits, do
255  *   h = (h & hashmask(10));
256  * In which case, the hash table should have hashsize(10) elements.
257  *
258  * If you are hashing n strings (uint8_t **)k, do it like this:
259  *   for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
260  *
261  * By Bob Jenkins, 2006.  bob_jenkins@burtleburtle.net.  You may use this
262  * code any way you wish, private, educational, or commercial.  It's free.
263  *
264  * Use for hash table lookup, or anything where one collision in 2^^32 is
265  * acceptable.  Do NOT use for cryptographic purposes.
266  * ----------------------------------------------------------------------------
267  */
268 
hashlittle(const void * key,size_t length,uint32_t initval)269 uint32_t hashlittle(
270     const void *key,
271     size_t      length,
272     uint32_t    initval)
273 {
274     uint32_t a,b,c; /* internal state */
275     union { const void *ptr; size_t i; } u;  /* needed for Mac Powerbook G4 */
276 
277     /* Set up the internal state */
278     a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
279 
280     u.ptr = key;
281     if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
282         /* read 32-bit chunks */
283         const uint32_t *k = (const uint32_t *)key;
284 #ifdef VALGRIND
285         const uint8_t  *k8;
286 #endif
287 
288         /* all but last block: aligned reads and affect 32 bits of (a,b,c) */
289         while (length > 12)
290         {
291             a += k[0];
292             b += k[1];
293             c += k[2];
294             mix(a,b,c);
295             length -= 12;
296             k += 3;
297         }
298 
299         /* handle the last (probably partial) block */
300         /*
301          * "k[2]&0xffffff" actually reads beyond the end of the string, but
302          * then masks off the part it's not allowed to read.  Because the
303          * string is aligned, the masked-off tail is in the same word as the
304          * rest of the string.  Every machine with memory protection I've seen
305          * does it on word boundaries, so is OK with this.  But VALGRIND will
306          * still catch it and complain.  The masking trick does make the hash
307          * noticably faster for short strings (like English words).
308          */
309 #ifndef VALGRIND
310 
311         switch(length)
312         {
313           case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
314           case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
315           case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
316           case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
317           case 8 : b+=k[1]; a+=k[0]; break;
318           case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
319           case 6 : b+=k[1]&0xffff; a+=k[0]; break;
320           case 5 : b+=k[1]&0xff; a+=k[0]; break;
321           case 4 : a+=k[0]; break;
322           case 3 : a+=k[0]&0xffffff; break;
323           case 2 : a+=k[0]&0xffff; break;
324           case 1 : a+=k[0]&0xff; break;
325           case 0 : return c; /* zero length strings require no mixing */
326         }
327 
328 #else /* make valgrind happy */
329 
330         k8 = (const uint8_t *)k;
331         switch(length)
332         {
333           case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
334           case 11: c+=((uint32_t)k8[10])<<16;  /* fall through */
335           case 10: c+=((uint32_t)k8[9])<<8;    /* fall through */
336           case 9 : c+=k8[8];                   /* fall through */
337           case 8 : b+=k[1]; a+=k[0]; break;
338           case 7 : b+=((uint32_t)k8[6])<<16;   /* fall through */
339           case 6 : b+=((uint32_t)k8[5])<<8;    /* fall through */
340           case 5 : b+=k8[4];                   /* fall through */
341           case 4 : a+=k[0]; break;
342           case 3 : a+=((uint32_t)k8[2])<<16;   /* fall through */
343           case 2 : a+=((uint32_t)k8[1])<<8;    /* fall through */
344           case 1 : a+=k8[0]; break;
345           case 0 : return c;
346         }
347 
348 #endif /* !valgrind */
349 
350     } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
351         /* read 16-bit chunks */
352         const uint16_t *k = (const uint16_t *)key;
353         const uint8_t  *k8;
354 
355         /* all but last block: aligned reads and different mixing */
356         while (length > 12)
357         {
358             a += k[0] + (((uint32_t)k[1])<<16);
359             b += k[2] + (((uint32_t)k[3])<<16);
360             c += k[4] + (((uint32_t)k[5])<<16);
361             mix(a,b,c);
362             length -= 12;
363             k += 6;
364         }
365 
366         /* handle the last (probably partial) block */
367         k8 = (const uint8_t *)k;
368         switch(length)
369         {
370           case 12: c+=k[4]+(((uint32_t)k[5])<<16);
371             b+=k[2]+(((uint32_t)k[3])<<16);
372             a+=k[0]+(((uint32_t)k[1])<<16);
373             break;
374           case 11: c+=((uint32_t)k8[10])<<16;     /* fall through */
375           case 10: c+=k[4];
376             b+=k[2]+(((uint32_t)k[3])<<16);
377             a+=k[0]+(((uint32_t)k[1])<<16);
378             break;
379           case 9 : c+=k8[8];                      /* fall through */
380           case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
381             a+=k[0]+(((uint32_t)k[1])<<16);
382             break;
383           case 7 : b+=((uint32_t)k8[6])<<16;      /* fall through */
384           case 6 : b+=k[2];
385             a+=k[0]+(((uint32_t)k[1])<<16);
386             break;
387           case 5 : b+=k8[4];                      /* fall through */
388           case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
389             break;
390           case 3 : a+=((uint32_t)k8[2])<<16;      /* fall through */
391           case 2 : a+=k[0];
392             break;
393           case 1 : a+=k8[0];
394             break;
395           case 0 : return c;    /* zero length requires no mixing */
396         }
397 
398     } else {
399         /* need to read the key one byte at a time */
400         const uint8_t *k = (const uint8_t *)key;
401 
402         /* all but the last block: affect some 32 bits of (a,b,c) */
403         while (length > 12)
404         {
405             a += k[0];
406             a += ((uint32_t)k[1])<<8;
407             a += ((uint32_t)k[2])<<16;
408             a += ((uint32_t)k[3])<<24;
409             b += k[4];
410             b += ((uint32_t)k[5])<<8;
411             b += ((uint32_t)k[6])<<16;
412             b += ((uint32_t)k[7])<<24;
413             c += k[8];
414             c += ((uint32_t)k[9])<<8;
415             c += ((uint32_t)k[10])<<16;
416             c += ((uint32_t)k[11])<<24;
417             mix(a,b,c);
418             length -= 12;
419             k += 12;
420         }
421 
422         /* last block: affect all 32 bits of (c) */
423         switch(length)      /* all the case statements fall through */
424         {
425           case 12: c+=((uint32_t)k[11])<<24; /* fall through */
426           case 11: c+=((uint32_t)k[10])<<16; /* fall through */
427           case 10: c+=((uint32_t)k[9])<<8;   /* fall through */
428           case 9 : c+=k[8];                  /* fall through */
429           case 8 : b+=((uint32_t)k[7])<<24;  /* fall through */
430           case 7 : b+=((uint32_t)k[6])<<16;  /* fall through */
431           case 6 : b+=((uint32_t)k[5])<<8;   /* fall through */
432           case 5 : b+=k[4];                  /* fall through */
433           case 4 : a+=((uint32_t)k[3])<<24;  /* fall through */
434           case 3 : a+=((uint32_t)k[2])<<16;  /* fall through */
435           case 2 : a+=((uint32_t)k[1])<<8;   /* fall through */
436           case 1 : a+=k[0];
437             break;
438           case 0 : return c;
439         }
440     }
441 
442     final(a,b,c);
443     return c;
444 }
445 
446 
447 /*
448  * hashlittle2: return 2 32-bit hash values
449  *
450  * This is identical to hashlittle(), except it returns two 32-bit hash
451  * values instead of just one.  This is good enough for hash table
452  * lookup with 2^^64 buckets, or if you want a second hash if you're not
453  * happy with the first, or if you want a probably-unique 64-bit ID for
454  * the key.  *pc is better mixed than *pb, so use *pc first.  If you want
455  * a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)".
456  */
hashlittle2(const void * key,size_t length,uint32_t * pc,uint32_t * pb)457 void hashlittle2(
458   const void *key,       /* the key to hash */
459   size_t      length,    /* length of the key */
460   uint32_t   *pc,        /* IN: primary initval, OUT: primary hash */
461   uint32_t   *pb)        /* IN: secondary initval, OUT: secondary hash */
462 {
463     uint32_t a,b,c;                         /* internal state */
464     union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
465 
466     /* Set up the internal state */
467     a = b = c = 0xdeadbeef + ((uint32_t)length) + *pc;
468     c += *pb;
469 
470     u.ptr = key;
471     if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
472         /* read 32-bit chunks */
473         const uint32_t *k = (const uint32_t *)key;
474 #ifdef VALGRIND
475         const uint8_t  *k8;
476 #endif
477 
478         /* all but last block: aligned reads and affect 32 bits of (a,b,c) */
479         while (length > 12)
480         {
481             a += k[0];
482             b += k[1];
483             c += k[2];
484             mix(a,b,c);
485             length -= 12;
486             k += 3;
487         }
488 
489         /* handle the last (probably partial) block */
490         /*
491          * "k[2]&0xffffff" actually reads beyond the end of the string, but
492          * then masks off the part it's not allowed to read.  Because the
493          * string is aligned, the masked-off tail is in the same word as the
494          * rest of the string.  Every machine with memory protection I've seen
495          * does it on word boundaries, so is OK with this.  But VALGRIND will
496          * still catch it and complain.  The masking trick does make the hash
497          * noticably faster for short strings (like English words).
498          */
499 #ifndef VALGRIND
500 
501         switch(length)
502         {
503           case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
504           case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
505           case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
506           case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
507           case 8 : b+=k[1]; a+=k[0]; break;
508           case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
509           case 6 : b+=k[1]&0xffff; a+=k[0]; break;
510           case 5 : b+=k[1]&0xff; a+=k[0]; break;
511           case 4 : a+=k[0]; break;
512           case 3 : a+=k[0]&0xffffff; break;
513           case 2 : a+=k[0]&0xffff; break;
514           case 1 : a+=k[0]&0xff; break;
515           case 0 :         /* zero length strings require no mixing */
516             *pc=c; *pb=b;
517             return;
518         }
519 
520 #else /* make valgrind happy */
521 
522         k8 = (const uint8_t *)k;
523         switch(length)
524         {
525           case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
526           case 11: c+=((uint32_t)k8[10])<<16;  /* fall through */
527           case 10: c+=((uint32_t)k8[9])<<8;    /* fall through */
528           case 9 : c+=k8[8];                   /* fall through */
529           case 8 : b+=k[1]; a+=k[0]; break;
530           case 7 : b+=((uint32_t)k8[6])<<16;   /* fall through */
531           case 6 : b+=((uint32_t)k8[5])<<8;    /* fall through */
532           case 5 : b+=k8[4];                   /* fall through */
533           case 4 : a+=k[0]; break;
534           case 3 : a+=((uint32_t)k8[2])<<16;   /* fall through */
535           case 2 : a+=((uint32_t)k8[1])<<8;    /* fall through */
536           case 1 : a+=k8[0]; break;
537           case 0 :         /* zero length strings require no mixing */
538             *pc=c; *pb=b;
539             return;
540         }
541 
542 #endif /* !VALGRIND */
543 
544     } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
545         /* read 16-bit chunks */
546         const uint16_t *k = (const uint16_t *)key;
547         const uint8_t  *k8;
548 
549         /* all but last block: aligned reads and different mixing */
550         while (length > 12)
551         {
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);
555             mix(a,b,c);
556             length -= 12;
557             k += 6;
558         }
559 
560         /* handle the last (probably partial) block */
561         k8 = (const uint8_t *)k;
562         switch(length)
563         {
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);
567             break;
568           case 11: c+=((uint32_t)k8[10])<<16;     /* fall through */
569           case 10: c+=k[4];
570             b+=k[2]+(((uint32_t)k[3])<<16);
571             a+=k[0]+(((uint32_t)k[1])<<16);
572             break;
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);
576             break;
577           case 7 : b+=((uint32_t)k8[6])<<16;      /* fall through */
578           case 6 : b+=k[2];
579             a+=k[0]+(((uint32_t)k[1])<<16);
580             break;
581           case 5 : b+=k8[4];                      /* fall through */
582           case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
583             break;
584           case 3 : a+=((uint32_t)k8[2])<<16;      /* fall through */
585           case 2 : a+=k[0];
586             break;
587           case 1 : a+=k8[0];
588             break;
589           case 0 :         /* zero length strings require no mixing */
590             *pc=c; *pb=b;
591             return;
592         }
593 
594     } else {
595         /* need to read the key one byte at a time */
596         const uint8_t *k = (const uint8_t *)key;
597 
598         /* all but the last block: affect some 32 bits of (a,b,c) */
599         while (length > 12)
600         {
601             a += k[0];
602             a += ((uint32_t)k[1])<<8;
603             a += ((uint32_t)k[2])<<16;
604             a += ((uint32_t)k[3])<<24;
605             b += k[4];
606             b += ((uint32_t)k[5])<<8;
607             b += ((uint32_t)k[6])<<16;
608             b += ((uint32_t)k[7])<<24;
609             c += k[8];
610             c += ((uint32_t)k[9])<<8;
611             c += ((uint32_t)k[10])<<16;
612             c += ((uint32_t)k[11])<<24;
613             mix(a,b,c);
614             length -= 12;
615             k += 12;
616         }
617 
618         /* last block: affect all 32 bits of (c) */
619         switch(length)      /* all the case statements fall through */
620         {
621           case 12: c+=((uint32_t)k[11])<<24; /* fall through */
622           case 11: c+=((uint32_t)k[10])<<16; /* fall through */
623           case 10: c+=((uint32_t)k[9])<<8;   /* fall through */
624           case 9 : c+=k[8];                  /* fall through */
625           case 8 : b+=((uint32_t)k[7])<<24;  /* fall through */
626           case 7 : b+=((uint32_t)k[6])<<16;  /* fall through */
627           case 6 : b+=((uint32_t)k[5])<<8;   /* fall through */
628           case 5 : b+=k[4];                  /* fall through */
629           case 4 : a+=((uint32_t)k[3])<<24;  /* fall through */
630           case 3 : a+=((uint32_t)k[2])<<16;  /* fall through */
631           case 2 : a+=((uint32_t)k[1])<<8;   /* fall through */
632           case 1 : a+=k[0];
633             break;
634           case 0 :         /* zero length strings require no mixing */
635             *pc=c; *pb=b;
636             return;
637         }
638     }
639 
640     final(a,b,c);
641     *pc=c; *pb=b;
642 }
643 
644 
645 
646 /*
647  * hashbig():
648  * This is the same as hashword() on big-endian machines.  It is different
649  * from hashlittle() on all machines.  hashbig() takes advantage of
650  * big-endian byte ordering.
651  */
hashbig(const void * key,size_t length,uint32_t initval)652 uint32_t hashbig(const void *key, size_t length, uint32_t initval)
653 {
654     uint32_t a,b,c;
655     /* to cast key to (size_t) happily */
656     union { const void *ptr; size_t i; } u;
657 
658     /* Set up the internal state */
659     a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
660 
661     u.ptr = key;
662     if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
663         /* read 32-bit chunks */
664         const uint32_t *k = (const uint32_t *)key;
665 #ifdef VALGRIND
666         const uint8_t  *k8;
667 #endif
668 
669         /* all but last block: aligned reads and affect 32 bits of (a,b,c) */
670         while (length > 12)
671         {
672             a += k[0];
673             b += k[1];
674             c += k[2];
675             mix(a,b,c);
676             length -= 12;
677             k += 3;
678         }
679 
680         /* handle the last (probably partial) block */
681         /*
682          * "k[2]<<8" actually reads beyond the end of the string, but
683          * then shifts out the part it's not allowed to read.  Because the
684          * string is aligned, the illegal read is in the same word as the
685          * rest of the string.  Every machine with memory protection I've seen
686          * does it on word boundaries, so is OK with this.  But VALGRIND will
687          * still catch it and complain.  The masking trick does make the hash
688          * noticably faster for short strings (like English words).
689          */
690 #ifndef VALGRIND
691 
692         switch(length)
693         {
694           case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
695           case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break;
696           case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break;
697           case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break;
698           case 8 : b+=k[1]; a+=k[0]; break;
699           case 7 : b+=k[1]&0xffffff00; a+=k[0]; break;
700           case 6 : b+=k[1]&0xffff0000; a+=k[0]; break;
701           case 5 : b+=k[1]&0xff000000; a+=k[0]; break;
702           case 4 : a+=k[0]; break;
703           case 3 : a+=k[0]&0xffffff00; break;
704           case 2 : a+=k[0]&0xffff0000; break;
705           case 1 : a+=k[0]&0xff000000; break;
706           case 0 : return c; /* zero length strings require no mixing */
707         }
708 
709 #else  /* make valgrind happy */
710 
711         k8 = (const uint8_t *)k;
712         switch(length)      /* all the case statements fall through */
713         {
714           case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
715           case 11: c+=((uint32_t)k8[10])<<8;  /* fall through */
716           case 10: c+=((uint32_t)k8[9])<<16;  /* fall through */
717           case 9 : c+=((uint32_t)k8[8])<<24;  /* fall through */
718           case 8 : b+=k[1]; a+=k[0]; break;
719           case 7 : b+=((uint32_t)k8[6])<<8;   /* fall through */
720           case 6 : b+=((uint32_t)k8[5])<<16;  /* fall through */
721           case 5 : b+=((uint32_t)k8[4])<<24;  /* fall through */
722           case 4 : a+=k[0]; break;
723           case 3 : a+=((uint32_t)k8[2])<<8;   /* fall through */
724           case 2 : a+=((uint32_t)k8[1])<<16;  /* fall through */
725           case 1 : a+=((uint32_t)k8[0])<<24; break;
726           case 0 : return c;
727         }
728 
729 #endif /* !VALGRIND */
730 
731     } else {
732         /* need to read the key one byte at a time */
733         const uint8_t *k = (const uint8_t *)key;
734 
735         /* all but the last block: affect some 32 bits of (a,b,c) */
736         while (length > 12)
737         {
738             a += ((uint32_t)k[0])<<24;
739             a += ((uint32_t)k[1])<<16;
740             a += ((uint32_t)k[2])<<8;
741             a += ((uint32_t)k[3]);
742             b += ((uint32_t)k[4])<<24;
743             b += ((uint32_t)k[5])<<16;
744             b += ((uint32_t)k[6])<<8;
745             b += ((uint32_t)k[7]);
746             c += ((uint32_t)k[8])<<24;
747             c += ((uint32_t)k[9])<<16;
748             c += ((uint32_t)k[10])<<8;
749             c += ((uint32_t)k[11]);
750             mix(a,b,c);
751             length -= 12;
752             k += 12;
753         }
754 
755         /* last block: affect all 32 bits of (c) */
756         switch(length)      /* all the case statements fall through */
757         {
758           case 12: c+=k[11];                /* fall through */
759           case 11: c+=((uint32_t)k[10])<<8; /* fall through */
760           case 10: c+=((uint32_t)k[9])<<16; /* fall through */
761           case 9 : c+=((uint32_t)k[8])<<24; /* fall through */
762           case 8 : b+=k[7];                 /* fall through */
763           case 7 : b+=((uint32_t)k[6])<<8;  /* fall through */
764           case 6 : b+=((uint32_t)k[5])<<16; /* fall through */
765           case 5 : b+=((uint32_t)k[4])<<24; /* fall through */
766           case 4 : a+=k[3];                 /* fall through */
767           case 3 : a+=((uint32_t)k[2])<<8;  /* fall through */
768           case 2 : a+=((uint32_t)k[1])<<16; /* fall through */
769           case 1 : a+=((uint32_t)k[0])<<24;
770             break;
771           case 0 : return c;
772         }
773     }
774 
775     final(a,b,c);
776     return c;
777 }
778 
779 
hashbig2(const void * key,size_t length,uint32_t * pc,uint32_t * pb)780 void hashbig2(
781     const void *key,
782     size_t      length,
783     uint32_t   *pc,
784     uint32_t   *pb)
785 {
786     uint32_t a,b,c;
787     /* to cast key to (size_t) happily */
788     union { const void *ptr; size_t i; } u;
789 
790     /* Set up the internal state */
791     a = b = c = 0xdeadbeef + ((uint32_t)length) + *pc;
792     c += *pb;
793 
794     u.ptr = key;
795     if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
796         /* read 32-bit chunks */
797         const uint32_t *k = (const uint32_t *)key;
798 #ifdef VALGRIND
799         const uint8_t  *k8;
800 #endif
801 
802         /* all but last block: aligned reads and affect 32 bits of (a,b,c) */
803         while (length > 12)
804         {
805             a += k[0];
806             b += k[1];
807             c += k[2];
808             mix(a,b,c);
809             length -= 12;
810             k += 3;
811         }
812 
813         /* handle the last (probably partial) block */
814         /*
815          * "k[2]<<8" actually reads beyond the end of the string, but
816          * then shifts out the part it's not allowed to read.  Because the
817          * string is aligned, the illegal read is in the same word as the
818          * rest of the string.  Every machine with memory protection I've seen
819          * does it on word boundaries, so is OK with this.  But VALGRIND will
820          * still catch it and complain.  The masking trick does make the hash
821          * noticably faster for short strings (like English words).
822          */
823 #ifndef VALGRIND
824 
825         switch(length)
826         {
827           case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
828           case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break;
829           case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break;
830           case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break;
831           case 8 : b+=k[1]; a+=k[0]; break;
832           case 7 : b+=k[1]&0xffffff00; a+=k[0]; break;
833           case 6 : b+=k[1]&0xffff0000; a+=k[0]; break;
834           case 5 : b+=k[1]&0xff000000; a+=k[0]; break;
835           case 4 : a+=k[0]; break;
836           case 3 : a+=k[0]&0xffffff00; break;
837           case 2 : a+=k[0]&0xffff0000; break;
838           case 1 : a+=k[0]&0xff000000; break;
839           case 0 :         /* zero length strings require no mixing */
840             *pc=c; *pb=b;
841             return;
842         }
843 
844 #else  /* make valgrind happy */
845 
846         k8 = (const uint8_t *)k;
847         switch(length)      /* all the case statements fall through */
848         {
849           case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
850           case 11: c+=((uint32_t)k8[10])<<8;  /* fall through */
851           case 10: c+=((uint32_t)k8[9])<<16;  /* fall through */
852           case 9 : c+=((uint32_t)k8[8])<<24;  /* fall through */
853           case 8 : b+=k[1]; a+=k[0]; break;
854           case 7 : b+=((uint32_t)k8[6])<<8;   /* fall through */
855           case 6 : b+=((uint32_t)k8[5])<<16;  /* fall through */
856           case 5 : b+=((uint32_t)k8[4])<<24;  /* fall through */
857           case 4 : a+=k[0]; break;
858           case 3 : a+=((uint32_t)k8[2])<<8;   /* fall through */
859           case 2 : a+=((uint32_t)k8[1])<<16;  /* fall through */
860           case 1 : a+=((uint32_t)k8[0])<<24; break;
861           case 0 :
862             *pc=c; *pb=b;
863             return;
864         }
865 
866 #endif /* !VALGRIND */
867 
868     } else {
869         /* need to read the key one byte at a time */
870         const uint8_t *k = (const uint8_t *)key;
871 
872         /* all but the last block: affect some 32 bits of (a,b,c) */
873         while (length > 12)
874         {
875             a += ((uint32_t)k[0])<<24;
876             a += ((uint32_t)k[1])<<16;
877             a += ((uint32_t)k[2])<<8;
878             a += ((uint32_t)k[3]);
879             b += ((uint32_t)k[4])<<24;
880             b += ((uint32_t)k[5])<<16;
881             b += ((uint32_t)k[6])<<8;
882             b += ((uint32_t)k[7]);
883             c += ((uint32_t)k[8])<<24;
884             c += ((uint32_t)k[9])<<16;
885             c += ((uint32_t)k[10])<<8;
886             c += ((uint32_t)k[11]);
887             mix(a,b,c);
888             length -= 12;
889             k += 12;
890         }
891 
892         /* last block: affect all 32 bits of (c) */
893         switch(length)      /* all the case statements fall through */
894         {
895           case 12: c+=k[11];                /* fall through */
896           case 11: c+=((uint32_t)k[10])<<8; /* fall through */
897           case 10: c+=((uint32_t)k[9])<<16; /* fall through */
898           case 9 : c+=((uint32_t)k[8])<<24; /* fall through */
899           case 8 : b+=k[7];                 /* fall through */
900           case 7 : b+=((uint32_t)k[6])<<8;  /* fall through */
901           case 6 : b+=((uint32_t)k[5])<<16; /* fall through */
902           case 5 : b+=((uint32_t)k[4])<<24; /* fall through */
903           case 4 : a+=k[3];                 /* fall through */
904           case 3 : a+=((uint32_t)k[2])<<8;  /* fall through */
905           case 2 : a+=((uint32_t)k[1])<<16; /* fall through */
906           case 1 : a+=((uint32_t)k[0])<<24;
907             break;
908           case 0 :
909             *pc=c; *pb=b;
910             return;
911         }
912     }
913 
914     final(a,b,c);
915     *pc=c; *pb=b;
916 }
917 
918 
919 #if SELF_TEST
920 
921 /* used for timings */
driver1(void)922 static void driver1(void)
923 {
924     uint8_t buf[256];
925     uint32_t i;
926     uint32_t h=0;
927     time_t a,z;
928 
929     time(&a);
930     for (i=0; i<256; ++i) buf[i] = 'x';
931     for (i=0; i<1; ++i)
932     {
933         h = hashlittle(&buf[0],1,h);
934     }
935     time(&z);
936     if (z-a > 0)
937         printf("time %d %.8x\n", (int)z-a, h);
938 }
939 
940 
941 /* check that every input bit changes every output bit half the time */
942 #define HASHSTATE 1
943 #define HASHLEN   1
944 #define MAXPAIR 60
945 #define MAXLEN  70
driver2(void)946 static void driver2(void)
947 {
948     uint8_t qa[MAXLEN+1], qb[MAXLEN+2], *a = &qa[0], *b = &qb[1];
949     uint32_t c[HASHSTATE], d[HASHSTATE], i=0, j=0, k, l, m=0, z;
950     uint32_t e[HASHSTATE],f[HASHSTATE],g[HASHSTATE],h[HASHSTATE];
951     uint32_t x[HASHSTATE],y[HASHSTATE];
952     uint32_t hlen;
953 
954     printf("No more than %d trials should ever be needed \n",MAXPAIR/2);
955     for (hlen=0; hlen < MAXLEN; ++hlen)
956     {
957         z=0;
958         for (i=0; i<hlen; ++i)  /* for each input byte, */
959         {
960             for (j=0; j<8; ++j)   /* for each input bit, */
961             {
962                 for (m=1; m<8; ++m) /* for serveral possible initvals, */
963                 {
964                     for (l=0; l<HASHSTATE; ++l)
965                         e[l]=f[l]=g[l]=h[l]=x[l]=y[l]=~((uint32_t)0);
966 
967                     /* check that every output bit is affected by that
968                      * input bit */
969                     for (k=0; k<MAXPAIR; k+=2)
970                     {
971                         uint32_t finished=1;
972                         /* keys have one bit different */
973                         for (l=0; l<hlen+1; ++l) {a[l] = b[l] = (uint8_t)0;}
974                         /* have a and b be two keys differing in only one bit */
975                         a[i] ^= (k<<j);
976                         a[i] ^= (k>>(8-j));
977                         c[0] = hashlittle(a, hlen, m);
978                         b[i] ^= ((k+1)<<j);
979                         b[i] ^= ((k+1)>>(8-j));
980                         d[0] = hashlittle(b, hlen, m);
981                         /* check every bit is 1, 0, set, and not set
982                          * at least once */
983                         for (l=0; l<HASHSTATE; ++l)
984                         {
985                             e[l] &= (c[l]^d[l]);
986                             f[l] &= ~(c[l]^d[l]);
987                             g[l] &= c[l];
988                             h[l] &= ~c[l];
989                             x[l] &= d[l];
990                             y[l] &= ~d[l];
991                             if (e[l]|f[l]|g[l]|h[l]|x[l]|y[l]) finished=0;
992                         }
993                         if (finished) break;
994                     }
995                     if (k>z) z=k;
996                     if (k==MAXPAIR)
997                     {
998                         printf("Some bit didn't change: ");
999                         printf("%.8x %.8x %.8x %.8x %.8x %.8x  ",
1000                                e[0],f[0],g[0],h[0],x[0],y[0]);
1001                         printf("i %d j %d m %d len %d\n", i, j, m, hlen);
1002                     }
1003                     if (z==MAXPAIR) goto done;
1004                 }
1005             }
1006         }
1007       done:
1008         if (z < MAXPAIR)
1009         {
1010             printf("Mix success  %2d bytes  %2d initvals  ",i,m);
1011             printf("required  %d  trials\n", z/2);
1012         }
1013     }
1014     printf("\n");
1015 }
1016 
1017 /* Check for reading beyond the end of the buffer and alignment problems */
driver3(void)1018 static void driver3(void)
1019 {
1020 #define GOOD_MEN                                                        \
1021     "This is the time for all good men to come to the aid of their country..."
1022 
1023     uint8_t buf[MAXLEN+20], *b;
1024     uint32_t len;
1025     uint8_t q[] = GOOD_MEN;
1026     uint32_t h;
1027     uint8_t qq[] = "x" GOOD_MEN;
1028     uint32_t i;
1029     uint8_t qqq[] = "xx" GOOD_MEN;
1030     uint32_t j;
1031     uint8_t qqqq[] = "xxx" GOOD_MEN;
1032     uint32_t ref,x,y;
1033     uint8_t *p;
1034 
1035     printf("Endianness.  These lines should all be the same"
1036            " (for values filled in):\n");
1037     printf("%.8x                            "
1038            "%.8x                            "
1039            "%.8x\n",
1040            hashword((const uint32_t *)q, (sizeof(q)-1)/4, 13),
1041            hashword((const uint32_t *)q, (sizeof(q)-5)/4, 13),
1042            hashword((const uint32_t *)q, (sizeof(q)-9)/4, 13));
1043     p = q;
1044     printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
1045            hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
1046            hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
1047            hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
1048            hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
1049            hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
1050            hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
1051     p = &qq[1];
1052     printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
1053            hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
1054            hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
1055            hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
1056            hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
1057            hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
1058            hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
1059     p = &qqq[2];
1060     printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
1061            hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
1062            hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
1063            hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
1064            hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
1065            hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
1066            hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
1067     p = &qqqq[3];
1068     printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
1069            hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
1070            hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
1071            hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
1072            hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
1073            hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
1074            hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
1075     printf("\n");
1076 
1077     /* check that hashlittle2 and hashlittle produce the same results */
1078     i=47; j=0;
1079     hashlittle2(q, sizeof(q), &i, &j);
1080     if (hashlittle(q, sizeof(q), 47) != i)
1081         printf("hashlittle2 and hashlittle mismatch\n");
1082 
1083     /* check that hashword2 and hashword produce the same results */
1084     len = 0xdeadbeef;
1085     i=47, j=0;
1086     hashword2(&len, 1, &i, &j);
1087     if (hashword(&len, 1, 47) != i)
1088         printf("hashword2 and hashword mismatch %x %x\n",
1089                i, hashword(&len, 1, 47));
1090 
1091     /* check hashlittle doesn't read before or after the ends of the string */
1092     for (h=0, b=buf+1; h<8; ++h, ++b)
1093     {
1094         for (i=0; i<MAXLEN; ++i)
1095         {
1096             len = i;
1097             for (j=0; j<i; ++j) *(b+j)=0;
1098 
1099             /* these should all be equal */
1100             ref = hashlittle(b, len, (uint32_t)1);
1101             *(b+i)=(uint8_t)~0;
1102             *(b-1)=(uint8_t)~0;
1103             x = hashlittle(b, len, (uint32_t)1);
1104             y = hashlittle(b, len, (uint32_t)1);
1105             if ((ref != x) || (ref != y))
1106             {
1107                 printf("alignment error: %.8x %.8x %.8x %d %d\n",ref,x,y,
1108                        h, i);
1109             }
1110         }
1111     }
1112 }
1113 
1114 
1115 /* check for problems with nulls */
driver4(void)1116 static void driver4(void)
1117 {
1118     uint8_t buf[1];
1119     uint32_t h,i,state[HASHSTATE];
1120 
1121 
1122     buf[0] = ~0;
1123     for (i=0; i<HASHSTATE; ++i) state[i] = 1;
1124     printf("These should all be different\n");
1125     for (i=0, h=0; i<8; ++i)
1126     {
1127         h = hashlittle(buf, 0, h);
1128         printf("%2" PRIu32 "  0-byte strings, hash is  %.8x\n", i, h);
1129     }
1130 }
1131 
1132 
driver5(void)1133 static void driver5(void)
1134 {
1135     uint32_t b,c;
1136     b=0, c=0, hashlittle2("", 0, &c, &b);
1137     printf("hash is %.8lx %.8lx\n", c, b);   /* deadbeef deadbeef */
1138     b=0xdeadbeef, c=0, hashlittle2("", 0, &c, &b);
1139     printf("hash is %.8lx %.8lx\n", c, b);   /* bd5b7dde deadbeef */
1140     b=0xdeadbeef, c=0xdeadbeef, hashlittle2("", 0, &c, &b);
1141     printf("hash is %.8lx %.8lx\n", c, b);   /* 9c093ccd bd5b7dde */
1142     b=0, c=0, hashlittle2("Four score and seven years ago", 30, &c, &b);
1143     printf("hash is %.8lx %.8lx\n", c, b);   /* 17770551 ce7226e6 */
1144     b=1, c=0, hashlittle2("Four score and seven years ago", 30, &c, &b);
1145     printf("hash is %.8lx %.8lx\n", c, b);   /* e3607cae bd371de4 */
1146     b=0, c=1, hashlittle2("Four score and seven years ago", 30, &c, &b);
1147     printf("hash is %.8lx %.8lx\n", c, b);   /* cd628161 6cbea4b3 */
1148     c = hashlittle("Four score and seven years ago", 30, 0);
1149     printf("hash is %.8lx\n", c);   /* 17770551 */
1150     c = hashlittle("Four score and seven years ago", 30, 1);
1151     printf("hash is %.8lx\n", c);   /* cd628161 */
1152 }
1153 
1154 
main()1155 int main()
1156 {
1157     driver1();   /* test that the key is hashed: used for timings */
1158     driver2();   /* test that whole key is hashed thoroughly */
1159     driver3();   /* test that nothing but the key is hashed */
1160     driver4();   /* test hashing multiple buffers (all buffers are null) */
1161     driver5();   /* test the hash against known vectors */
1162     return 1;
1163 }
1164 
1165 #endif  /* SELF_TEST */
1166