xref: /openbsd-src/usr.sbin/nsd/lookup3.c (revision bc6311d77cd5d30ac9d52b9dbc59ea6cf4b9c7d3)
12c1ae072Ssthen /*
2dd5b221eSsthen   February 2013(Wouter) patch defines for BSD endianness, from Brad Smith.
32c1ae072Ssthen   January 2012(Wouter) added randomised initial value, fallout from 28c3.
42c1ae072Ssthen   March 2007(Wouter) adapted from lookup3.c original, add config.h include.
52c1ae072Ssthen      added #ifdef VALGRIND to remove 298,384,660 'unused variable k8' warnings.
62c1ae072Ssthen      added include of lookup3.h to check definitions match declarations.
72c1ae072Ssthen      removed include of stdint - config.h takes care of platform independence.
8ee5153b7Sflorian      added fallthrough comments for new gcc warning suppression.
92c1ae072Ssthen   url http://burtleburtle.net/bob/hash/index.html.
102c1ae072Ssthen */
112c1ae072Ssthen /*
122c1ae072Ssthen -------------------------------------------------------------------------------
132c1ae072Ssthen lookup3.c, by Bob Jenkins, May 2006, Public Domain.
142c1ae072Ssthen 
152c1ae072Ssthen These are functions for producing 32-bit hashes for hash table lookup.
162c1ae072Ssthen hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
172c1ae072Ssthen are externally useful functions.  Routines to test the hash are included
182c1ae072Ssthen if SELF_TEST is defined.  You can use this free for any purpose.  It's in
192c1ae072Ssthen the public domain.  It has no warranty.
202c1ae072Ssthen 
212c1ae072Ssthen You probably want to use hashlittle().  hashlittle() and hashbig()
222c1ae072Ssthen hash byte arrays.  hashlittle() is is faster than hashbig() on
232c1ae072Ssthen little-endian machines.  Intel and AMD are little-endian machines.
242c1ae072Ssthen On second thought, you probably want hashlittle2(), which is identical to
252c1ae072Ssthen hashlittle() except it returns two 32-bit hashes for the price of one.
262c1ae072Ssthen You could implement hashbig2() if you wanted but I haven't bothered here.
272c1ae072Ssthen 
282c1ae072Ssthen If you want to find a hash of, say, exactly 7 integers, do
292c1ae072Ssthen   a = i1;  b = i2;  c = i3;
302c1ae072Ssthen   mix(a,b,c);
312c1ae072Ssthen   a += i4; b += i5; c += i6;
322c1ae072Ssthen   mix(a,b,c);
332c1ae072Ssthen   a += i7;
342c1ae072Ssthen   final(a,b,c);
352c1ae072Ssthen then use c as the hash value.  If you have a variable length array of
362c1ae072Ssthen 4-byte integers to hash, use hashword().  If you have a byte array (like
372c1ae072Ssthen a character string), use hashlittle().  If you have several byte arrays, or
382c1ae072Ssthen a mix of things, see the comments above hashlittle().
392c1ae072Ssthen 
402c1ae072Ssthen Why is this so big?  I read 12 bytes at a time into 3 4-byte integers,
412c1ae072Ssthen then mix those integers.  This is fast (you can do a lot more thorough
422c1ae072Ssthen mixing with 12*3 instructions on 3 integers than you can with 3 instructions
432c1ae072Ssthen on 1 byte), but shoehorning those bytes into integers efficiently is messy.
442c1ae072Ssthen -------------------------------------------------------------------------------
452c1ae072Ssthen */
462c1ae072Ssthen /*#define SELF_TEST 1*/
472c1ae072Ssthen 
482c1ae072Ssthen #include "config.h"
492c1ae072Ssthen #include "lookup3.h"
502c1ae072Ssthen #include <stdio.h>      /* defines printf for tests */
512c1ae072Ssthen #include <time.h>       /* defines time_t for timings in the test */
522c1ae072Ssthen /*#include <stdint.h>     defines uint32_t etc  (from config.h) */
532c1ae072Ssthen #include <sys/param.h>  /* attempt to define endianness */
54dd5b221eSsthen #ifdef HAVE_SYS_TYPES_H
55dd5b221eSsthen # include <sys/types.h> /* attempt to define endianness (solaris) */
56dd5b221eSsthen #endif
57533110e2Sbrad #if defined(linux) || defined(__OpenBSD__)
58533110e2Sbrad #  ifdef HAVE_ENDIAN_H
592c1ae072Ssthen #    include <endian.h>    /* attempt to define endianness */
60533110e2Sbrad #  else
61533110e2Sbrad #    include <machine/endian.h> /* on older OpenBSD */
62533110e2Sbrad #  endif
632c1ae072Ssthen #endif
64034e7b7cSbrad #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
65034e7b7cSbrad #include <sys/endian.h> /* attempt to define endianness */
66034e7b7cSbrad #endif
672c1ae072Ssthen 
682c1ae072Ssthen /* random initial value */
692c1ae072Ssthen static uint32_t raninit = 0xdeadbeef;
702c1ae072Ssthen 
712c1ae072Ssthen void
hash_set_raninit(uint32_t v)722c1ae072Ssthen hash_set_raninit(uint32_t v)
732c1ae072Ssthen {
742c1ae072Ssthen 	raninit = v;
752c1ae072Ssthen }
762c1ae072Ssthen 
772c1ae072Ssthen /*
782c1ae072Ssthen  * My best guess at if you are big-endian or little-endian.  This may
792c1ae072Ssthen  * need adjustment.
802c1ae072Ssthen  */
812c1ae072Ssthen #if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
822c1ae072Ssthen      __BYTE_ORDER == __LITTLE_ENDIAN) || \
832c1ae072Ssthen     (defined(i386) || defined(__i386__) || defined(__i486__) || \
84dd5b221eSsthen      defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL) || defined(__x86))
852c1ae072Ssthen # define HASH_LITTLE_ENDIAN 1
862c1ae072Ssthen # define HASH_BIG_ENDIAN 0
872c1ae072Ssthen #elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
882c1ae072Ssthen        __BYTE_ORDER == __BIG_ENDIAN) || \
89dd5b221eSsthen       (defined(sparc) || defined(__sparc) || defined(__sparc__) || defined(POWERPC) || defined(mc68000) || defined(sel))
902c1ae072Ssthen # define HASH_LITTLE_ENDIAN 0
912c1ae072Ssthen # define HASH_BIG_ENDIAN 1
92dd5b221eSsthen #elif defined(_MACHINE_ENDIAN_H_)
93dd5b221eSsthen /* test for machine_endian_h protects failure if some are empty strings */
94dd5b221eSsthen # if defined(_BYTE_ORDER) && defined(_BIG_ENDIAN) && _BYTE_ORDER == _BIG_ENDIAN
95dd5b221eSsthen #  define HASH_LITTLE_ENDIAN 0
96dd5b221eSsthen #  define HASH_BIG_ENDIAN 1
97dd5b221eSsthen # endif
98dd5b221eSsthen # if defined(_BYTE_ORDER) && defined(_LITTLE_ENDIAN) && _BYTE_ORDER == _LITTLE_ENDIAN
99dd5b221eSsthen #  define HASH_LITTLE_ENDIAN 1
100dd5b221eSsthen #  define HASH_BIG_ENDIAN 0
101dd5b221eSsthen # endif /* _MACHINE_ENDIAN_H_ */
1022c1ae072Ssthen #else
1032c1ae072Ssthen # define HASH_LITTLE_ENDIAN 0
1042c1ae072Ssthen # define HASH_BIG_ENDIAN 0
1052c1ae072Ssthen #endif
1062c1ae072Ssthen 
1072c1ae072Ssthen #define hashsize(n) ((uint32_t)1<<(n))
1082c1ae072Ssthen #define hashmask(n) (hashsize(n)-1)
1092c1ae072Ssthen #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
1102c1ae072Ssthen 
1112c1ae072Ssthen /*
1122c1ae072Ssthen -------------------------------------------------------------------------------
1132c1ae072Ssthen mix -- mix 3 32-bit values reversibly.
1142c1ae072Ssthen 
1152c1ae072Ssthen This is reversible, so any information in (a,b,c) before mix() is
1162c1ae072Ssthen still in (a,b,c) after mix().
1172c1ae072Ssthen 
1182c1ae072Ssthen If four pairs of (a,b,c) inputs are run through mix(), or through
1192c1ae072Ssthen mix() in reverse, there are at least 32 bits of the output that
1202c1ae072Ssthen are sometimes the same for one pair and different for another pair.
1212c1ae072Ssthen This was tested for:
1222c1ae072Ssthen * pairs that differed by one bit, by two bits, in any combination
1232c1ae072Ssthen   of top bits of (a,b,c), or in any combination of bottom bits of
1242c1ae072Ssthen   (a,b,c).
1252c1ae072Ssthen * "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
1262c1ae072Ssthen   the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
1272c1ae072Ssthen   is commonly produced by subtraction) look like a single 1-bit
1282c1ae072Ssthen   difference.
1292c1ae072Ssthen * the base values were pseudorandom, all zero but one bit set, or
1302c1ae072Ssthen   all zero plus a counter that starts at zero.
1312c1ae072Ssthen 
1322c1ae072Ssthen Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
1332c1ae072Ssthen satisfy this are
1342c1ae072Ssthen     4  6  8 16 19  4
1352c1ae072Ssthen     9 15  3 18 27 15
1362c1ae072Ssthen    14  9  3  7 17  3
1372c1ae072Ssthen Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
1382c1ae072Ssthen for "differ" defined as + with a one-bit base and a two-bit delta.  I
1392c1ae072Ssthen used http://burtleburtle.net/bob/hash/avalanche.html to choose
1402c1ae072Ssthen the operations, constants, and arrangements of the variables.
1412c1ae072Ssthen 
1422c1ae072Ssthen This does not achieve avalanche.  There are input bits of (a,b,c)
1432c1ae072Ssthen that fail to affect some output bits of (a,b,c), especially of a.  The
1442c1ae072Ssthen most thoroughly mixed value is c, but it doesn't really even achieve
1452c1ae072Ssthen avalanche in c.
1462c1ae072Ssthen 
1472c1ae072Ssthen This allows some parallelism.  Read-after-writes are good at doubling
1482c1ae072Ssthen the number of bits affected, so the goal of mixing pulls in the opposite
1492c1ae072Ssthen direction as the goal of parallelism.  I did what I could.  Rotates
1502c1ae072Ssthen seem to cost as much as shifts on every machine I could lay my hands
1512c1ae072Ssthen on, and rotates are much kinder to the top and bottom bits, so I used
1522c1ae072Ssthen rotates.
1532c1ae072Ssthen -------------------------------------------------------------------------------
1542c1ae072Ssthen */
1552c1ae072Ssthen #define mix(a,b,c) \
1562c1ae072Ssthen { \
1572c1ae072Ssthen   a -= c;  a ^= rot(c, 4);  c += b; \
1582c1ae072Ssthen   b -= a;  b ^= rot(a, 6);  a += c; \
1592c1ae072Ssthen   c -= b;  c ^= rot(b, 8);  b += a; \
1602c1ae072Ssthen   a -= c;  a ^= rot(c,16);  c += b; \
1612c1ae072Ssthen   b -= a;  b ^= rot(a,19);  a += c; \
1622c1ae072Ssthen   c -= b;  c ^= rot(b, 4);  b += a; \
1632c1ae072Ssthen }
1642c1ae072Ssthen 
1652c1ae072Ssthen /*
1662c1ae072Ssthen -------------------------------------------------------------------------------
1672c1ae072Ssthen final -- final mixing of 3 32-bit values (a,b,c) into c
1682c1ae072Ssthen 
1692c1ae072Ssthen Pairs of (a,b,c) values differing in only a few bits will usually
1702c1ae072Ssthen produce values of c that look totally different.  This was tested for
1712c1ae072Ssthen * pairs that differed by one bit, by two bits, in any combination
1722c1ae072Ssthen   of top bits of (a,b,c), or in any combination of bottom bits of
1732c1ae072Ssthen   (a,b,c).
1742c1ae072Ssthen * "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
1752c1ae072Ssthen   the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
1762c1ae072Ssthen   is commonly produced by subtraction) look like a single 1-bit
1772c1ae072Ssthen   difference.
1782c1ae072Ssthen * the base values were pseudorandom, all zero but one bit set, or
1792c1ae072Ssthen   all zero plus a counter that starts at zero.
1802c1ae072Ssthen 
1812c1ae072Ssthen These constants passed:
1822c1ae072Ssthen  14 11 25 16 4 14 24
1832c1ae072Ssthen  12 14 25 16 4 14 24
1842c1ae072Ssthen and these came close:
1852c1ae072Ssthen   4  8 15 26 3 22 24
1862c1ae072Ssthen  10  8 15 26 3 22 24
1872c1ae072Ssthen  11  8 15 26 3 22 24
1882c1ae072Ssthen -------------------------------------------------------------------------------
1892c1ae072Ssthen */
1902c1ae072Ssthen #define final(a,b,c) \
1912c1ae072Ssthen { \
1922c1ae072Ssthen   c ^= b; c -= rot(b,14); \
1932c1ae072Ssthen   a ^= c; a -= rot(c,11); \
1942c1ae072Ssthen   b ^= a; b -= rot(a,25); \
1952c1ae072Ssthen   c ^= b; c -= rot(b,16); \
1962c1ae072Ssthen   a ^= c; a -= rot(c,4);  \
1972c1ae072Ssthen   b ^= a; b -= rot(a,14); \
1982c1ae072Ssthen   c ^= b; c -= rot(b,24); \
1992c1ae072Ssthen }
2002c1ae072Ssthen 
2012c1ae072Ssthen /*
2022c1ae072Ssthen --------------------------------------------------------------------
2032c1ae072Ssthen  This works on all machines.  To be useful, it requires
2042c1ae072Ssthen  -- that the key be an array of uint32_t's, and
2052c1ae072Ssthen  -- that the length be the number of uint32_t's in the key
2062c1ae072Ssthen 
2072c1ae072Ssthen  The function hashword() is identical to hashlittle() on little-endian
2082c1ae072Ssthen  machines, and identical to hashbig() on big-endian machines,
2092c1ae072Ssthen  except that the length has to be measured in uint32_ts rather than in
2102c1ae072Ssthen  bytes.  hashlittle() is more complicated than hashword() only because
2112c1ae072Ssthen  hashlittle() has to dance around fitting the key bytes into registers.
2122c1ae072Ssthen --------------------------------------------------------------------
2132c1ae072Ssthen */
hashword(const uint32_t * k,size_t length,uint32_t initval)2142c1ae072Ssthen uint32_t hashword(
2152c1ae072Ssthen const uint32_t *k,                   /* the key, an array of uint32_t values */
2162c1ae072Ssthen size_t          length,               /* the length of the key, in uint32_ts */
2172c1ae072Ssthen uint32_t        initval)         /* the previous hash, or an arbitrary value */
2182c1ae072Ssthen {
2192c1ae072Ssthen   uint32_t a,b,c;
2202c1ae072Ssthen 
2212c1ae072Ssthen   /* Set up the internal state */
2222c1ae072Ssthen   a = b = c = raninit + (((uint32_t)length)<<2) + initval;
2232c1ae072Ssthen 
2242c1ae072Ssthen   /*------------------------------------------------- handle most of the key */
2252c1ae072Ssthen   while (length > 3)
2262c1ae072Ssthen   {
2272c1ae072Ssthen     a += k[0];
2282c1ae072Ssthen     b += k[1];
2292c1ae072Ssthen     c += k[2];
2302c1ae072Ssthen     mix(a,b,c);
2312c1ae072Ssthen     length -= 3;
2322c1ae072Ssthen     k += 3;
2332c1ae072Ssthen   }
2342c1ae072Ssthen 
2352c1ae072Ssthen   /*------------------------------------------- handle the last 3 uint32_t's */
2362c1ae072Ssthen   switch(length)                     /* all the case statements fall through */
2372c1ae072Ssthen   {
2382c1ae072Ssthen   case 3 : c+=k[2];
239ee5153b7Sflorian   	/* fallthrough */
2402c1ae072Ssthen   case 2 : b+=k[1];
241ee5153b7Sflorian   	/* fallthrough */
2422c1ae072Ssthen   case 1 : a+=k[0];
2432c1ae072Ssthen     final(a,b,c);
2442c1ae072Ssthen   case 0:     /* case 0: nothing left to add */
2452c1ae072Ssthen     break;
2462c1ae072Ssthen   }
2472c1ae072Ssthen   /*------------------------------------------------------ report the result */
2482c1ae072Ssthen   return c;
2492c1ae072Ssthen }
2502c1ae072Ssthen 
2512c1ae072Ssthen 
2522c1ae072Ssthen #ifdef SELF_TEST
2532c1ae072Ssthen 
2542c1ae072Ssthen /*
2552c1ae072Ssthen --------------------------------------------------------------------
2562c1ae072Ssthen hashword2() -- same as hashword(), but take two seeds and return two
2572c1ae072Ssthen 32-bit values.  pc and pb must both be nonnull, and *pc and *pb must
2582c1ae072Ssthen both be initialized with seeds.  If you pass in (*pb)==0, the output
2592c1ae072Ssthen (*pc) will be the same as the return value from hashword().
2602c1ae072Ssthen --------------------------------------------------------------------
2612c1ae072Ssthen */
hashword2(const uint32_t * k,size_t length,uint32_t * pc,uint32_t * pb)2622c1ae072Ssthen void hashword2 (
2632c1ae072Ssthen const uint32_t *k,                   /* the key, an array of uint32_t values */
2642c1ae072Ssthen size_t          length,               /* the length of the key, in uint32_ts */
2652c1ae072Ssthen uint32_t       *pc,                      /* IN: seed OUT: primary hash value */
2662c1ae072Ssthen uint32_t       *pb)               /* IN: more seed OUT: secondary hash value */
2672c1ae072Ssthen {
2682c1ae072Ssthen   uint32_t a,b,c;
2692c1ae072Ssthen 
2702c1ae072Ssthen   /* Set up the internal state */
2712c1ae072Ssthen   a = b = c = raninit + ((uint32_t)(length<<2)) + *pc;
2722c1ae072Ssthen   c += *pb;
2732c1ae072Ssthen 
2742c1ae072Ssthen   /*------------------------------------------------- handle most of the key */
2752c1ae072Ssthen   while (length > 3)
2762c1ae072Ssthen   {
2772c1ae072Ssthen     a += k[0];
2782c1ae072Ssthen     b += k[1];
2792c1ae072Ssthen     c += k[2];
2802c1ae072Ssthen     mix(a,b,c);
2812c1ae072Ssthen     length -= 3;
2822c1ae072Ssthen     k += 3;
2832c1ae072Ssthen   }
2842c1ae072Ssthen 
2852c1ae072Ssthen   /*------------------------------------------- handle the last 3 uint32_t's */
2862c1ae072Ssthen   switch(length)                     /* all the case statements fall through */
2872c1ae072Ssthen   {
2882c1ae072Ssthen   case 3 : c+=k[2];
2892c1ae072Ssthen   case 2 : b+=k[1];
2902c1ae072Ssthen   case 1 : a+=k[0];
2912c1ae072Ssthen     final(a,b,c);
2922c1ae072Ssthen   case 0:     /* case 0: nothing left to add */
2932c1ae072Ssthen     break;
2942c1ae072Ssthen   }
2952c1ae072Ssthen   /*------------------------------------------------------ report the result */
2962c1ae072Ssthen   *pc=c; *pb=b;
2972c1ae072Ssthen }
2982c1ae072Ssthen 
2992c1ae072Ssthen #endif /* SELF_TEST */
3002c1ae072Ssthen 
3012c1ae072Ssthen /*
3022c1ae072Ssthen -------------------------------------------------------------------------------
3032c1ae072Ssthen hashlittle() -- hash a variable-length key into a 32-bit value
3042c1ae072Ssthen   k       : the key (the unaligned variable-length array of bytes)
3052c1ae072Ssthen   length  : the length of the key, counting by bytes
3062c1ae072Ssthen   initval : can be any 4-byte value
3072c1ae072Ssthen Returns a 32-bit value.  Every bit of the key affects every bit of
3082c1ae072Ssthen the return value.  Two keys differing by one or two bits will have
3092c1ae072Ssthen totally different hash values.
3102c1ae072Ssthen 
3112c1ae072Ssthen The best hash table sizes are powers of 2.  There is no need to do
3122c1ae072Ssthen mod a prime (mod is sooo slow!).  If you need less than 32 bits,
3132c1ae072Ssthen use a bitmask.  For example, if you need only 10 bits, do
3142c1ae072Ssthen   h = (h & hashmask(10));
3152c1ae072Ssthen In which case, the hash table should have hashsize(10) elements.
3162c1ae072Ssthen 
3172c1ae072Ssthen If you are hashing n strings (uint8_t **)k, do it like this:
3182c1ae072Ssthen   for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
3192c1ae072Ssthen 
3202c1ae072Ssthen By Bob Jenkins, 2006.  bob_jenkins@burtleburtle.net.  You may use this
3212c1ae072Ssthen code any way you wish, private, educational, or commercial.  It's free.
3222c1ae072Ssthen 
3232c1ae072Ssthen Use for hash table lookup, or anything where one collision in 2^^32 is
3242c1ae072Ssthen acceptable.  Do NOT use for cryptographic purposes.
3252c1ae072Ssthen -------------------------------------------------------------------------------
3262c1ae072Ssthen */
3272c1ae072Ssthen 
hashlittle(const void * key,size_t length,uint32_t initval)3282c1ae072Ssthen uint32_t hashlittle( const void *key, size_t length, uint32_t initval)
3292c1ae072Ssthen {
3302c1ae072Ssthen   uint32_t a,b,c;                                          /* internal state */
3312c1ae072Ssthen   union { const void *ptr; size_t i; } u;     /* needed for Mac Powerbook G4 */
3322c1ae072Ssthen 
3332c1ae072Ssthen   /* Set up the internal state */
3342c1ae072Ssthen   a = b = c = raninit + ((uint32_t)length) + initval;
3352c1ae072Ssthen 
3362c1ae072Ssthen   u.ptr = key;
3372c1ae072Ssthen   if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
3382c1ae072Ssthen     const uint32_t *k = (const uint32_t *)key;         /* read 32-bit chunks */
3392c1ae072Ssthen #ifdef VALGRIND
3402c1ae072Ssthen     const uint8_t  *k8;
3412c1ae072Ssthen #endif
3422c1ae072Ssthen 
3432c1ae072Ssthen     /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
3442c1ae072Ssthen     while (length > 12)
3452c1ae072Ssthen     {
3462c1ae072Ssthen       a += k[0];
3472c1ae072Ssthen       b += k[1];
3482c1ae072Ssthen       c += k[2];
3492c1ae072Ssthen       mix(a,b,c);
3502c1ae072Ssthen       length -= 12;
3512c1ae072Ssthen       k += 3;
3522c1ae072Ssthen     }
3532c1ae072Ssthen 
3542c1ae072Ssthen     /*----------------------------- handle the last (probably partial) block */
3552c1ae072Ssthen     /*
3562c1ae072Ssthen      * "k[2]&0xffffff" actually reads beyond the end of the string, but
3572c1ae072Ssthen      * then masks off the part it's not allowed to read.  Because the
3582c1ae072Ssthen      * string is aligned, the masked-off tail is in the same word as the
3592c1ae072Ssthen      * rest of the string.  Every machine with memory protection I've seen
3602c1ae072Ssthen      * does it on word boundaries, so is OK with this.  But VALGRIND will
3612c1ae072Ssthen      * still catch it and complain.  The masking trick does make the hash
3622fd875a4Ssthen      * noticeably faster for short strings (like English words).
3632c1ae072Ssthen      */
3642c1ae072Ssthen #ifndef VALGRIND
3652c1ae072Ssthen 
3662c1ae072Ssthen     switch(length)
3672c1ae072Ssthen     {
3682c1ae072Ssthen     case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
3692c1ae072Ssthen     case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
3702c1ae072Ssthen     case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
3712c1ae072Ssthen     case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
3722c1ae072Ssthen     case 8 : b+=k[1]; a+=k[0]; break;
3732c1ae072Ssthen     case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
3742c1ae072Ssthen     case 6 : b+=k[1]&0xffff; a+=k[0]; break;
3752c1ae072Ssthen     case 5 : b+=k[1]&0xff; a+=k[0]; break;
3762c1ae072Ssthen     case 4 : a+=k[0]; break;
3772c1ae072Ssthen     case 3 : a+=k[0]&0xffffff; break;
3782c1ae072Ssthen     case 2 : a+=k[0]&0xffff; break;
3792c1ae072Ssthen     case 1 : a+=k[0]&0xff; break;
3802c1ae072Ssthen     case 0 : return c;              /* zero length strings require no mixing */
3812c1ae072Ssthen     }
3822c1ae072Ssthen 
3832c1ae072Ssthen #else /* make valgrind happy */
3842c1ae072Ssthen 
3852c1ae072Ssthen     k8 = (const uint8_t *)k;
3862c1ae072Ssthen     switch(length)
3872c1ae072Ssthen     {
3882c1ae072Ssthen     case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
3892c1ae072Ssthen     case 11: c+=((uint32_t)k8[10])<<16;  /* fall through */
3902c1ae072Ssthen     case 10: c+=((uint32_t)k8[9])<<8;    /* fall through */
3912c1ae072Ssthen     case 9 : c+=k8[8];                   /* fall through */
3922c1ae072Ssthen     case 8 : b+=k[1]; a+=k[0]; break;
3932c1ae072Ssthen     case 7 : b+=((uint32_t)k8[6])<<16;   /* fall through */
3942c1ae072Ssthen     case 6 : b+=((uint32_t)k8[5])<<8;    /* fall through */
3952c1ae072Ssthen     case 5 : b+=k8[4];                   /* fall through */
3962c1ae072Ssthen     case 4 : a+=k[0]; break;
3972c1ae072Ssthen     case 3 : a+=((uint32_t)k8[2])<<16;   /* fall through */
3982c1ae072Ssthen     case 2 : a+=((uint32_t)k8[1])<<8;    /* fall through */
3992c1ae072Ssthen     case 1 : a+=k8[0]; break;
4002c1ae072Ssthen     case 0 : return c;
4012c1ae072Ssthen     }
4022c1ae072Ssthen 
4032c1ae072Ssthen #endif /* !valgrind */
4042c1ae072Ssthen 
4052c1ae072Ssthen   } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
4062c1ae072Ssthen     const uint16_t *k = (const uint16_t *)key;         /* read 16-bit chunks */
4072c1ae072Ssthen     const uint8_t  *k8;
4082c1ae072Ssthen 
4092c1ae072Ssthen     /*--------------- all but last block: aligned reads and different mixing */
4102c1ae072Ssthen     while (length > 12)
4112c1ae072Ssthen     {
4122c1ae072Ssthen       a += k[0] + (((uint32_t)k[1])<<16);
4132c1ae072Ssthen       b += k[2] + (((uint32_t)k[3])<<16);
4142c1ae072Ssthen       c += k[4] + (((uint32_t)k[5])<<16);
4152c1ae072Ssthen       mix(a,b,c);
4162c1ae072Ssthen       length -= 12;
4172c1ae072Ssthen       k += 6;
4182c1ae072Ssthen     }
4192c1ae072Ssthen 
4202c1ae072Ssthen     /*----------------------------- handle the last (probably partial) block */
4212c1ae072Ssthen     k8 = (const uint8_t *)k;
4222c1ae072Ssthen     switch(length)
4232c1ae072Ssthen     {
4242c1ae072Ssthen     case 12: c+=k[4]+(((uint32_t)k[5])<<16);
4252c1ae072Ssthen              b+=k[2]+(((uint32_t)k[3])<<16);
4262c1ae072Ssthen              a+=k[0]+(((uint32_t)k[1])<<16);
4272c1ae072Ssthen              break;
4282c1ae072Ssthen     case 11: c+=((uint32_t)k8[10])<<16;     /* fall through */
4292c1ae072Ssthen     case 10: c+=k[4];
4302c1ae072Ssthen              b+=k[2]+(((uint32_t)k[3])<<16);
4312c1ae072Ssthen              a+=k[0]+(((uint32_t)k[1])<<16);
4322c1ae072Ssthen              break;
4332c1ae072Ssthen     case 9 : c+=k8[8];                      /* fall through */
4342c1ae072Ssthen     case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
4352c1ae072Ssthen              a+=k[0]+(((uint32_t)k[1])<<16);
4362c1ae072Ssthen              break;
4372c1ae072Ssthen     case 7 : b+=((uint32_t)k8[6])<<16;      /* fall through */
4382c1ae072Ssthen     case 6 : b+=k[2];
4392c1ae072Ssthen              a+=k[0]+(((uint32_t)k[1])<<16);
4402c1ae072Ssthen              break;
4412c1ae072Ssthen     case 5 : b+=k8[4];                      /* fall through */
4422c1ae072Ssthen     case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
4432c1ae072Ssthen              break;
4442c1ae072Ssthen     case 3 : a+=((uint32_t)k8[2])<<16;      /* fall through */
4452c1ae072Ssthen     case 2 : a+=k[0];
4462c1ae072Ssthen              break;
4472c1ae072Ssthen     case 1 : a+=k8[0];
4482c1ae072Ssthen              break;
4492c1ae072Ssthen     case 0 : return c;                     /* zero length requires no mixing */
4502c1ae072Ssthen     }
4512c1ae072Ssthen 
4522c1ae072Ssthen   } else {                        /* need to read the key one byte at a time */
4532c1ae072Ssthen     const uint8_t *k = (const uint8_t *)key;
4542c1ae072Ssthen 
4552c1ae072Ssthen     /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
4562c1ae072Ssthen     while (length > 12)
4572c1ae072Ssthen     {
4582c1ae072Ssthen       a += k[0];
4592c1ae072Ssthen       a += ((uint32_t)k[1])<<8;
4602c1ae072Ssthen       a += ((uint32_t)k[2])<<16;
4612c1ae072Ssthen       a += ((uint32_t)k[3])<<24;
4622c1ae072Ssthen       b += k[4];
4632c1ae072Ssthen       b += ((uint32_t)k[5])<<8;
4642c1ae072Ssthen       b += ((uint32_t)k[6])<<16;
4652c1ae072Ssthen       b += ((uint32_t)k[7])<<24;
4662c1ae072Ssthen       c += k[8];
4672c1ae072Ssthen       c += ((uint32_t)k[9])<<8;
4682c1ae072Ssthen       c += ((uint32_t)k[10])<<16;
4692c1ae072Ssthen       c += ((uint32_t)k[11])<<24;
4702c1ae072Ssthen       mix(a,b,c);
4712c1ae072Ssthen       length -= 12;
4722c1ae072Ssthen       k += 12;
4732c1ae072Ssthen     }
4742c1ae072Ssthen 
4752c1ae072Ssthen     /*-------------------------------- last block: affect all 32 bits of (c) */
4762c1ae072Ssthen     switch(length)                   /* all the case statements fall through */
4772c1ae072Ssthen     {
4782c1ae072Ssthen     case 12: c+=((uint32_t)k[11])<<24;
479ee5153b7Sflorian   	/* fallthrough */
4802c1ae072Ssthen     case 11: c+=((uint32_t)k[10])<<16;
481ee5153b7Sflorian   	/* fallthrough */
4822c1ae072Ssthen     case 10: c+=((uint32_t)k[9])<<8;
483ee5153b7Sflorian   	/* fallthrough */
4842c1ae072Ssthen     case 9 : c+=k[8];
485ee5153b7Sflorian   	/* fallthrough */
4862c1ae072Ssthen     case 8 : b+=((uint32_t)k[7])<<24;
487ee5153b7Sflorian   	/* fallthrough */
4882c1ae072Ssthen     case 7 : b+=((uint32_t)k[6])<<16;
489ee5153b7Sflorian   	/* fallthrough */
4902c1ae072Ssthen     case 6 : b+=((uint32_t)k[5])<<8;
491ee5153b7Sflorian   	/* fallthrough */
4922c1ae072Ssthen     case 5 : b+=k[4];
493ee5153b7Sflorian   	/* fallthrough */
4942c1ae072Ssthen     case 4 : a+=((uint32_t)k[3])<<24;
495ee5153b7Sflorian   	/* fallthrough */
4962c1ae072Ssthen     case 3 : a+=((uint32_t)k[2])<<16;
497ee5153b7Sflorian   	/* fallthrough */
4982c1ae072Ssthen     case 2 : a+=((uint32_t)k[1])<<8;
499ee5153b7Sflorian   	/* fallthrough */
5002c1ae072Ssthen     case 1 : a+=k[0];
5012c1ae072Ssthen              break;
5022c1ae072Ssthen     case 0 : return c;
5032c1ae072Ssthen     }
5042c1ae072Ssthen   }
5052c1ae072Ssthen 
5062c1ae072Ssthen   final(a,b,c);
5072c1ae072Ssthen   return c;
5082c1ae072Ssthen }
5092c1ae072Ssthen 
5102c1ae072Ssthen #ifdef SELF_TEST
5112c1ae072Ssthen 
5122c1ae072Ssthen /*
5132c1ae072Ssthen  * hashlittle2: return 2 32-bit hash values
5142c1ae072Ssthen  *
5152c1ae072Ssthen  * This is identical to hashlittle(), except it returns two 32-bit hash
5162c1ae072Ssthen  * values instead of just one.  This is good enough for hash table
5172c1ae072Ssthen  * lookup with 2^^64 buckets, or if you want a second hash if you're not
5182c1ae072Ssthen  * happy with the first, or if you want a probably-unique 64-bit ID for
5192c1ae072Ssthen  * the key.  *pc is better mixed than *pb, so use *pc first.  If you want
5202c1ae072Ssthen  * a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)".
5212c1ae072Ssthen  */
hashlittle2(const void * key,size_t length,uint32_t * pc,uint32_t * pb)5222c1ae072Ssthen void hashlittle2(
5232c1ae072Ssthen   const void *key,       /* the key to hash */
5242c1ae072Ssthen   size_t      length,    /* length of the key */
5252c1ae072Ssthen   uint32_t   *pc,        /* IN: primary initval, OUT: primary hash */
5262c1ae072Ssthen   uint32_t   *pb)        /* IN: secondary initval, OUT: secondary hash */
5272c1ae072Ssthen {
5282c1ae072Ssthen   uint32_t a,b,c;                                          /* internal state */
5292c1ae072Ssthen   union { const void *ptr; size_t i; } u;     /* needed for Mac Powerbook G4 */
5302c1ae072Ssthen 
5312c1ae072Ssthen   /* Set up the internal state */
5322c1ae072Ssthen   a = b = c = raninit + ((uint32_t)length) + *pc;
5332c1ae072Ssthen   c += *pb;
5342c1ae072Ssthen 
5352c1ae072Ssthen   u.ptr = key;
5362c1ae072Ssthen   if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
5372c1ae072Ssthen     const uint32_t *k = (const uint32_t *)key;         /* read 32-bit chunks */
5382c1ae072Ssthen #ifdef VALGRIND
5392c1ae072Ssthen     const uint8_t  *k8;
5402c1ae072Ssthen #endif
5412c1ae072Ssthen 
5422c1ae072Ssthen     /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
5432c1ae072Ssthen     while (length > 12)
5442c1ae072Ssthen     {
5452c1ae072Ssthen       a += k[0];
5462c1ae072Ssthen       b += k[1];
5472c1ae072Ssthen       c += k[2];
5482c1ae072Ssthen       mix(a,b,c);
5492c1ae072Ssthen       length -= 12;
5502c1ae072Ssthen       k += 3;
5512c1ae072Ssthen     }
5522c1ae072Ssthen 
5532c1ae072Ssthen     /*----------------------------- handle the last (probably partial) block */
5542c1ae072Ssthen     /*
5552c1ae072Ssthen      * "k[2]&0xffffff" actually reads beyond the end of the string, but
5562c1ae072Ssthen      * then masks off the part it's not allowed to read.  Because the
5572c1ae072Ssthen      * string is aligned, the masked-off tail is in the same word as the
5582c1ae072Ssthen      * rest of the string.  Every machine with memory protection I've seen
5592c1ae072Ssthen      * does it on word boundaries, so is OK with this.  But VALGRIND will
5602c1ae072Ssthen      * still catch it and complain.  The masking trick does make the hash
5612fd875a4Ssthen      * noticeably faster for short strings (like English words).
5622c1ae072Ssthen      */
5632c1ae072Ssthen #ifndef VALGRIND
5642c1ae072Ssthen 
5652c1ae072Ssthen     switch(length)
5662c1ae072Ssthen     {
5672c1ae072Ssthen     case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
5682c1ae072Ssthen     case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
5692c1ae072Ssthen     case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
5702c1ae072Ssthen     case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
5712c1ae072Ssthen     case 8 : b+=k[1]; a+=k[0]; break;
5722c1ae072Ssthen     case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
5732c1ae072Ssthen     case 6 : b+=k[1]&0xffff; a+=k[0]; break;
5742c1ae072Ssthen     case 5 : b+=k[1]&0xff; a+=k[0]; break;
5752c1ae072Ssthen     case 4 : a+=k[0]; break;
5762c1ae072Ssthen     case 3 : a+=k[0]&0xffffff; break;
5772c1ae072Ssthen     case 2 : a+=k[0]&0xffff; break;
5782c1ae072Ssthen     case 1 : a+=k[0]&0xff; break;
5792c1ae072Ssthen     case 0 : *pc=c; *pb=b; return;  /* zero length strings require no mixing */
5802c1ae072Ssthen     }
5812c1ae072Ssthen 
5822c1ae072Ssthen #else /* make valgrind happy */
5832c1ae072Ssthen 
5842c1ae072Ssthen     k8 = (const uint8_t *)k;
5852c1ae072Ssthen     switch(length)
5862c1ae072Ssthen     {
5872c1ae072Ssthen     case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
5882c1ae072Ssthen     case 11: c+=((uint32_t)k8[10])<<16;  /* fall through */
5892c1ae072Ssthen     case 10: c+=((uint32_t)k8[9])<<8;    /* fall through */
5902c1ae072Ssthen     case 9 : c+=k8[8];                   /* fall through */
5912c1ae072Ssthen     case 8 : b+=k[1]; a+=k[0]; break;
5922c1ae072Ssthen     case 7 : b+=((uint32_t)k8[6])<<16;   /* fall through */
5932c1ae072Ssthen     case 6 : b+=((uint32_t)k8[5])<<8;    /* fall through */
5942c1ae072Ssthen     case 5 : b+=k8[4];                   /* fall through */
5952c1ae072Ssthen     case 4 : a+=k[0]; break;
5962c1ae072Ssthen     case 3 : a+=((uint32_t)k8[2])<<16;   /* fall through */
5972c1ae072Ssthen     case 2 : a+=((uint32_t)k8[1])<<8;    /* fall through */
5982c1ae072Ssthen     case 1 : a+=k8[0]; break;
5992c1ae072Ssthen     case 0 : *pc=c; *pb=b; return;  /* zero length strings require no mixing */
6002c1ae072Ssthen     }
6012c1ae072Ssthen 
6022c1ae072Ssthen #endif /* !valgrind */
6032c1ae072Ssthen 
6042c1ae072Ssthen   } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
6052c1ae072Ssthen     const uint16_t *k = (const uint16_t *)key;         /* read 16-bit chunks */
6062c1ae072Ssthen     const uint8_t  *k8;
6072c1ae072Ssthen 
6082c1ae072Ssthen     /*--------------- all but last block: aligned reads and different mixing */
6092c1ae072Ssthen     while (length > 12)
6102c1ae072Ssthen     {
6112c1ae072Ssthen       a += k[0] + (((uint32_t)k[1])<<16);
6122c1ae072Ssthen       b += k[2] + (((uint32_t)k[3])<<16);
6132c1ae072Ssthen       c += k[4] + (((uint32_t)k[5])<<16);
6142c1ae072Ssthen       mix(a,b,c);
6152c1ae072Ssthen       length -= 12;
6162c1ae072Ssthen       k += 6;
6172c1ae072Ssthen     }
6182c1ae072Ssthen 
6192c1ae072Ssthen     /*----------------------------- handle the last (probably partial) block */
6202c1ae072Ssthen     k8 = (const uint8_t *)k;
6212c1ae072Ssthen     switch(length)
6222c1ae072Ssthen     {
6232c1ae072Ssthen     case 12: c+=k[4]+(((uint32_t)k[5])<<16);
6242c1ae072Ssthen              b+=k[2]+(((uint32_t)k[3])<<16);
6252c1ae072Ssthen              a+=k[0]+(((uint32_t)k[1])<<16);
6262c1ae072Ssthen              break;
6272c1ae072Ssthen     case 11: c+=((uint32_t)k8[10])<<16;     /* fall through */
6282c1ae072Ssthen     case 10: c+=k[4];
6292c1ae072Ssthen              b+=k[2]+(((uint32_t)k[3])<<16);
6302c1ae072Ssthen              a+=k[0]+(((uint32_t)k[1])<<16);
6312c1ae072Ssthen              break;
6322c1ae072Ssthen     case 9 : c+=k8[8];                      /* fall through */
6332c1ae072Ssthen     case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
6342c1ae072Ssthen              a+=k[0]+(((uint32_t)k[1])<<16);
6352c1ae072Ssthen              break;
6362c1ae072Ssthen     case 7 : b+=((uint32_t)k8[6])<<16;      /* fall through */
6372c1ae072Ssthen     case 6 : b+=k[2];
6382c1ae072Ssthen              a+=k[0]+(((uint32_t)k[1])<<16);
6392c1ae072Ssthen              break;
6402c1ae072Ssthen     case 5 : b+=k8[4];                      /* fall through */
6412c1ae072Ssthen     case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
6422c1ae072Ssthen              break;
6432c1ae072Ssthen     case 3 : a+=((uint32_t)k8[2])<<16;      /* fall through */
6442c1ae072Ssthen     case 2 : a+=k[0];
6452c1ae072Ssthen              break;
6462c1ae072Ssthen     case 1 : a+=k8[0];
6472c1ae072Ssthen              break;
6482c1ae072Ssthen     case 0 : *pc=c; *pb=b; return;  /* zero length strings require no mixing */
6492c1ae072Ssthen     }
6502c1ae072Ssthen 
6512c1ae072Ssthen   } else {                        /* need to read the key one byte at a time */
6522c1ae072Ssthen     const uint8_t *k = (const uint8_t *)key;
6532c1ae072Ssthen 
6542c1ae072Ssthen     /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
6552c1ae072Ssthen     while (length > 12)
6562c1ae072Ssthen     {
6572c1ae072Ssthen       a += k[0];
6582c1ae072Ssthen       a += ((uint32_t)k[1])<<8;
6592c1ae072Ssthen       a += ((uint32_t)k[2])<<16;
6602c1ae072Ssthen       a += ((uint32_t)k[3])<<24;
6612c1ae072Ssthen       b += k[4];
6622c1ae072Ssthen       b += ((uint32_t)k[5])<<8;
6632c1ae072Ssthen       b += ((uint32_t)k[6])<<16;
6642c1ae072Ssthen       b += ((uint32_t)k[7])<<24;
6652c1ae072Ssthen       c += k[8];
6662c1ae072Ssthen       c += ((uint32_t)k[9])<<8;
6672c1ae072Ssthen       c += ((uint32_t)k[10])<<16;
6682c1ae072Ssthen       c += ((uint32_t)k[11])<<24;
6692c1ae072Ssthen       mix(a,b,c);
6702c1ae072Ssthen       length -= 12;
6712c1ae072Ssthen       k += 12;
6722c1ae072Ssthen     }
6732c1ae072Ssthen 
6742c1ae072Ssthen     /*-------------------------------- last block: affect all 32 bits of (c) */
6752c1ae072Ssthen     switch(length)                   /* all the case statements fall through */
6762c1ae072Ssthen     {
6772c1ae072Ssthen     case 12: c+=((uint32_t)k[11])<<24;
6782c1ae072Ssthen     case 11: c+=((uint32_t)k[10])<<16;
6792c1ae072Ssthen     case 10: c+=((uint32_t)k[9])<<8;
6802c1ae072Ssthen     case 9 : c+=k[8];
6812c1ae072Ssthen     case 8 : b+=((uint32_t)k[7])<<24;
6822c1ae072Ssthen     case 7 : b+=((uint32_t)k[6])<<16;
6832c1ae072Ssthen     case 6 : b+=((uint32_t)k[5])<<8;
6842c1ae072Ssthen     case 5 : b+=k[4];
6852c1ae072Ssthen     case 4 : a+=((uint32_t)k[3])<<24;
6862c1ae072Ssthen     case 3 : a+=((uint32_t)k[2])<<16;
6872c1ae072Ssthen     case 2 : a+=((uint32_t)k[1])<<8;
6882c1ae072Ssthen     case 1 : a+=k[0];
6892c1ae072Ssthen              break;
6902c1ae072Ssthen     case 0 : *pc=c; *pb=b; return;  /* zero length strings require no mixing */
6912c1ae072Ssthen     }
6922c1ae072Ssthen   }
6932c1ae072Ssthen 
6942c1ae072Ssthen   final(a,b,c);
6952c1ae072Ssthen   *pc=c; *pb=b;
6962c1ae072Ssthen }
6972c1ae072Ssthen 
6982c1ae072Ssthen #endif /* SELF_TEST */
6992c1ae072Ssthen 
7002c1ae072Ssthen #if 0	/* currently not used */
7012c1ae072Ssthen 
7022c1ae072Ssthen /*
7032c1ae072Ssthen  * hashbig():
7042c1ae072Ssthen  * This is the same as hashword() on big-endian machines.  It is different
7052c1ae072Ssthen  * from hashlittle() on all machines.  hashbig() takes advantage of
7062c1ae072Ssthen  * big-endian byte ordering.
7072c1ae072Ssthen  */
7082c1ae072Ssthen uint32_t hashbig( const void *key, size_t length, uint32_t initval)
7092c1ae072Ssthen {
7102c1ae072Ssthen   uint32_t a,b,c;
7112c1ae072Ssthen   union { const void *ptr; size_t i; } u; /* to cast key to (size_t) happily */
7122c1ae072Ssthen 
7132c1ae072Ssthen   /* Set up the internal state */
7142c1ae072Ssthen   a = b = c = raninit + ((uint32_t)length) + initval;
7152c1ae072Ssthen 
7162c1ae072Ssthen   u.ptr = key;
7172c1ae072Ssthen   if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
7182c1ae072Ssthen     const uint32_t *k = (const uint32_t *)key;         /* read 32-bit chunks */
7192c1ae072Ssthen #ifdef VALGRIND
7202c1ae072Ssthen     const uint8_t  *k8;
7212c1ae072Ssthen #endif
7222c1ae072Ssthen 
7232c1ae072Ssthen     /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
7242c1ae072Ssthen     while (length > 12)
7252c1ae072Ssthen     {
7262c1ae072Ssthen       a += k[0];
7272c1ae072Ssthen       b += k[1];
7282c1ae072Ssthen       c += k[2];
7292c1ae072Ssthen       mix(a,b,c);
7302c1ae072Ssthen       length -= 12;
7312c1ae072Ssthen       k += 3;
7322c1ae072Ssthen     }
7332c1ae072Ssthen 
7342c1ae072Ssthen     /*----------------------------- handle the last (probably partial) block */
7352c1ae072Ssthen     /*
7362c1ae072Ssthen      * "k[2]<<8" actually reads beyond the end of the string, but
7372c1ae072Ssthen      * then shifts out the part it's not allowed to read.  Because the
7382c1ae072Ssthen      * string is aligned, the illegal read is in the same word as the
7392c1ae072Ssthen      * rest of the string.  Every machine with memory protection I've seen
7402c1ae072Ssthen      * does it on word boundaries, so is OK with this.  But VALGRIND will
7412c1ae072Ssthen      * still catch it and complain.  The masking trick does make the hash
7422fd875a4Ssthen      * noticeably faster for short strings (like English words).
7432c1ae072Ssthen      */
7442c1ae072Ssthen #ifndef VALGRIND
7452c1ae072Ssthen 
7462c1ae072Ssthen     switch(length)
7472c1ae072Ssthen     {
7482c1ae072Ssthen     case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
7492c1ae072Ssthen     case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break;
7502c1ae072Ssthen     case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break;
7512c1ae072Ssthen     case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break;
7522c1ae072Ssthen     case 8 : b+=k[1]; a+=k[0]; break;
7532c1ae072Ssthen     case 7 : b+=k[1]&0xffffff00; a+=k[0]; break;
7542c1ae072Ssthen     case 6 : b+=k[1]&0xffff0000; a+=k[0]; break;
7552c1ae072Ssthen     case 5 : b+=k[1]&0xff000000; a+=k[0]; break;
7562c1ae072Ssthen     case 4 : a+=k[0]; break;
7572c1ae072Ssthen     case 3 : a+=k[0]&0xffffff00; break;
7582c1ae072Ssthen     case 2 : a+=k[0]&0xffff0000; break;
7592c1ae072Ssthen     case 1 : a+=k[0]&0xff000000; break;
7602c1ae072Ssthen     case 0 : return c;              /* zero length strings require no mixing */
7612c1ae072Ssthen     }
7622c1ae072Ssthen 
7632c1ae072Ssthen #else  /* make valgrind happy */
7642c1ae072Ssthen 
7652c1ae072Ssthen     k8 = (const uint8_t *)k;
7662c1ae072Ssthen     switch(length)                   /* all the case statements fall through */
7672c1ae072Ssthen     {
7682c1ae072Ssthen     case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
7692c1ae072Ssthen     case 11: c+=((uint32_t)k8[10])<<8;  /* fall through */
7702c1ae072Ssthen     case 10: c+=((uint32_t)k8[9])<<16;  /* fall through */
7712c1ae072Ssthen     case 9 : c+=((uint32_t)k8[8])<<24;  /* fall through */
7722c1ae072Ssthen     case 8 : b+=k[1]; a+=k[0]; break;
7732c1ae072Ssthen     case 7 : b+=((uint32_t)k8[6])<<8;   /* fall through */
7742c1ae072Ssthen     case 6 : b+=((uint32_t)k8[5])<<16;  /* fall through */
7752c1ae072Ssthen     case 5 : b+=((uint32_t)k8[4])<<24;  /* fall through */
7762c1ae072Ssthen     case 4 : a+=k[0]; break;
7772c1ae072Ssthen     case 3 : a+=((uint32_t)k8[2])<<8;   /* fall through */
7782c1ae072Ssthen     case 2 : a+=((uint32_t)k8[1])<<16;  /* fall through */
7792c1ae072Ssthen     case 1 : a+=((uint32_t)k8[0])<<24; break;
7802c1ae072Ssthen     case 0 : return c;
7812c1ae072Ssthen     }
7822c1ae072Ssthen 
7832c1ae072Ssthen #endif /* !VALGRIND */
7842c1ae072Ssthen 
7852c1ae072Ssthen   } else {                        /* need to read the key one byte at a time */
7862c1ae072Ssthen     const uint8_t *k = (const uint8_t *)key;
7872c1ae072Ssthen 
7882c1ae072Ssthen     /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
7892c1ae072Ssthen     while (length > 12)
7902c1ae072Ssthen     {
7912c1ae072Ssthen       a += ((uint32_t)k[0])<<24;
7922c1ae072Ssthen       a += ((uint32_t)k[1])<<16;
7932c1ae072Ssthen       a += ((uint32_t)k[2])<<8;
7942c1ae072Ssthen       a += ((uint32_t)k[3]);
7952c1ae072Ssthen       b += ((uint32_t)k[4])<<24;
7962c1ae072Ssthen       b += ((uint32_t)k[5])<<16;
7972c1ae072Ssthen       b += ((uint32_t)k[6])<<8;
7982c1ae072Ssthen       b += ((uint32_t)k[7]);
7992c1ae072Ssthen       c += ((uint32_t)k[8])<<24;
8002c1ae072Ssthen       c += ((uint32_t)k[9])<<16;
8012c1ae072Ssthen       c += ((uint32_t)k[10])<<8;
8022c1ae072Ssthen       c += ((uint32_t)k[11]);
8032c1ae072Ssthen       mix(a,b,c);
8042c1ae072Ssthen       length -= 12;
8052c1ae072Ssthen       k += 12;
8062c1ae072Ssthen     }
8072c1ae072Ssthen 
8082c1ae072Ssthen     /*-------------------------------- last block: affect all 32 bits of (c) */
8092c1ae072Ssthen     switch(length)                   /* all the case statements fall through */
8102c1ae072Ssthen     {
8112c1ae072Ssthen     case 12: c+=k[11];
8122c1ae072Ssthen     case 11: c+=((uint32_t)k[10])<<8;
8132c1ae072Ssthen     case 10: c+=((uint32_t)k[9])<<16;
8142c1ae072Ssthen     case 9 : c+=((uint32_t)k[8])<<24;
8152c1ae072Ssthen     case 8 : b+=k[7];
8162c1ae072Ssthen     case 7 : b+=((uint32_t)k[6])<<8;
8172c1ae072Ssthen     case 6 : b+=((uint32_t)k[5])<<16;
8182c1ae072Ssthen     case 5 : b+=((uint32_t)k[4])<<24;
8192c1ae072Ssthen     case 4 : a+=k[3];
8202c1ae072Ssthen     case 3 : a+=((uint32_t)k[2])<<8;
8212c1ae072Ssthen     case 2 : a+=((uint32_t)k[1])<<16;
8222c1ae072Ssthen     case 1 : a+=((uint32_t)k[0])<<24;
8232c1ae072Ssthen              break;
8242c1ae072Ssthen     case 0 : return c;
8252c1ae072Ssthen     }
8262c1ae072Ssthen   }
8272c1ae072Ssthen 
8282c1ae072Ssthen   final(a,b,c);
8292c1ae072Ssthen   return c;
8302c1ae072Ssthen }
8312c1ae072Ssthen 
8322c1ae072Ssthen #endif /* 0 == currently not used */
8332c1ae072Ssthen 
8342c1ae072Ssthen #ifdef SELF_TEST
8352c1ae072Ssthen 
8362c1ae072Ssthen /* used for timings */
driver1()8372c1ae072Ssthen void driver1()
8382c1ae072Ssthen {
8392c1ae072Ssthen   uint8_t buf[256];
8402c1ae072Ssthen   uint32_t i;
8412c1ae072Ssthen   uint32_t h=0;
8422c1ae072Ssthen   time_t a,z;
8432c1ae072Ssthen 
8442c1ae072Ssthen   time(&a);
8452c1ae072Ssthen   for (i=0; i<256; ++i) buf[i] = 'x';
8462c1ae072Ssthen   for (i=0; i<1; ++i)
8472c1ae072Ssthen   {
8482c1ae072Ssthen     h = hashlittle(&buf[0],1,h);
8492c1ae072Ssthen   }
8502c1ae072Ssthen   time(&z);
851e3932aeeSsthen   if (z-a > 0) printf("time %lld %.8x\n", (long long) z-a, h);
8522c1ae072Ssthen }
8532c1ae072Ssthen 
8542c1ae072Ssthen /* check that every input bit changes every output bit half the time */
8552c1ae072Ssthen #define HASHSTATE 1
8562c1ae072Ssthen #define HASHLEN   1
8572c1ae072Ssthen #define MAXPAIR 60
8582c1ae072Ssthen #define MAXLEN  70
driver2()8592c1ae072Ssthen void driver2()
8602c1ae072Ssthen {
8612c1ae072Ssthen   uint8_t qa[MAXLEN+1], qb[MAXLEN+2], *a = &qa[0], *b = &qb[1];
8622c1ae072Ssthen   uint32_t c[HASHSTATE], d[HASHSTATE], i=0, j=0, k, l, m=0, z;
8632c1ae072Ssthen   uint32_t e[HASHSTATE],f[HASHSTATE],g[HASHSTATE],h[HASHSTATE];
8642c1ae072Ssthen   uint32_t x[HASHSTATE],y[HASHSTATE];
8652c1ae072Ssthen   uint32_t hlen;
8662c1ae072Ssthen 
8672c1ae072Ssthen   printf("No more than %d trials should ever be needed \n",MAXPAIR/2);
8682c1ae072Ssthen   for (hlen=0; hlen < MAXLEN; ++hlen)
8692c1ae072Ssthen   {
8702c1ae072Ssthen     z=0;
8712c1ae072Ssthen     for (i=0; i<hlen; ++i)  /*----------------------- for each input byte, */
8722c1ae072Ssthen     {
8732c1ae072Ssthen       for (j=0; j<8; ++j)   /*------------------------ for each input bit, */
8742c1ae072Ssthen       {
875*bc6311d7Sflorian 	for (m=1; m<8; ++m) /*------------ for several possible initvals, */
8762c1ae072Ssthen 	{
8772c1ae072Ssthen 	  for (l=0; l<HASHSTATE; ++l)
8782c1ae072Ssthen 	    e[l]=f[l]=g[l]=h[l]=x[l]=y[l]=~((uint32_t)0);
8792c1ae072Ssthen 
8802c1ae072Ssthen       	  /*---- check that every output bit is affected by that input bit */
8812c1ae072Ssthen 	  for (k=0; k<MAXPAIR; k+=2)
8822c1ae072Ssthen 	  {
8832c1ae072Ssthen 	    uint32_t finished=1;
8842c1ae072Ssthen 	    /* keys have one bit different */
8852c1ae072Ssthen 	    for (l=0; l<hlen+1; ++l) {a[l] = b[l] = (uint8_t)0;}
8862c1ae072Ssthen 	    /* have a and b be two keys differing in only one bit */
8872c1ae072Ssthen 	    a[i] ^= (k<<j);
8882c1ae072Ssthen 	    a[i] ^= (k>>(8-j));
8892c1ae072Ssthen 	     c[0] = hashlittle(a, hlen, m);
8902c1ae072Ssthen 	    b[i] ^= ((k+1)<<j);
8912c1ae072Ssthen 	    b[i] ^= ((k+1)>>(8-j));
8922c1ae072Ssthen 	     d[0] = hashlittle(b, hlen, m);
8932c1ae072Ssthen 	    /* check every bit is 1, 0, set, and not set at least once */
8942c1ae072Ssthen 	    for (l=0; l<HASHSTATE; ++l)
8952c1ae072Ssthen 	    {
8962c1ae072Ssthen 	      e[l] &= (c[l]^d[l]);
8972c1ae072Ssthen 	      f[l] &= ~(c[l]^d[l]);
8982c1ae072Ssthen 	      g[l] &= c[l];
8992c1ae072Ssthen 	      h[l] &= ~c[l];
9002c1ae072Ssthen 	      x[l] &= d[l];
9012c1ae072Ssthen 	      y[l] &= ~d[l];
9022c1ae072Ssthen 	      if (e[l]|f[l]|g[l]|h[l]|x[l]|y[l]) finished=0;
9032c1ae072Ssthen 	    }
9042c1ae072Ssthen 	    if (finished) break;
9052c1ae072Ssthen 	  }
9062c1ae072Ssthen 	  if (k>z) z=k;
9072c1ae072Ssthen 	  if (k==MAXPAIR)
9082c1ae072Ssthen 	  {
9092c1ae072Ssthen 	     printf("Some bit didn't change: ");
9102c1ae072Ssthen 	     printf("%.8x %.8x %.8x %.8x %.8x %.8x  ",
9112c1ae072Ssthen 	            e[0],f[0],g[0],h[0],x[0],y[0]);
9122c1ae072Ssthen 	     printf("i %d j %d m %d len %d\n", i, j, m, hlen);
9132c1ae072Ssthen 	  }
9142c1ae072Ssthen 	  if (z==MAXPAIR) goto done;
9152c1ae072Ssthen 	}
9162c1ae072Ssthen       }
9172c1ae072Ssthen     }
9182c1ae072Ssthen    done:
9192c1ae072Ssthen     if (z < MAXPAIR)
9202c1ae072Ssthen     {
9212c1ae072Ssthen       printf("Mix success  %2d bytes  %2d initvals  ",i,m);
9222c1ae072Ssthen       printf("required  %d  trials\n", z/2);
9232c1ae072Ssthen     }
9242c1ae072Ssthen   }
9252c1ae072Ssthen   printf("\n");
9262c1ae072Ssthen }
9272c1ae072Ssthen 
9282c1ae072Ssthen /* Check for reading beyond the end of the buffer and alignment problems */
driver3()9292c1ae072Ssthen void driver3()
9302c1ae072Ssthen {
9312c1ae072Ssthen   uint8_t buf[MAXLEN+20], *b;
9322c1ae072Ssthen   uint32_t len;
9332c1ae072Ssthen   uint8_t q[] = "This is the time for all good men to come to the aid of their country...";
9342c1ae072Ssthen   uint32_t h;
9352c1ae072Ssthen   uint8_t qq[] = "xThis is the time for all good men to come to the aid of their country...";
9362c1ae072Ssthen   uint32_t i;
9372c1ae072Ssthen   uint8_t qqq[] = "xxThis is the time for all good men to come to the aid of their country...";
9382c1ae072Ssthen   uint32_t j;
9392c1ae072Ssthen   uint8_t qqqq[] = "xxxThis is the time for all good men to come to the aid of their country...";
9402c1ae072Ssthen   uint32_t ref,x,y;
9412c1ae072Ssthen   uint8_t *p;
9422c1ae072Ssthen 
9432c1ae072Ssthen   printf("Endianness.  These lines should all be the same (for values filled in):\n");
9442c1ae072Ssthen   printf("%.8x                            %.8x                            %.8x\n",
9452c1ae072Ssthen          hashword((const uint32_t *)q, (sizeof(q)-1)/4, 13),
9462c1ae072Ssthen          hashword((const uint32_t *)q, (sizeof(q)-5)/4, 13),
9472c1ae072Ssthen          hashword((const uint32_t *)q, (sizeof(q)-9)/4, 13));
9482c1ae072Ssthen   p = q;
9492c1ae072Ssthen   printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
9502c1ae072Ssthen          hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
9512c1ae072Ssthen          hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
9522c1ae072Ssthen          hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
9532c1ae072Ssthen          hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
9542c1ae072Ssthen          hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
9552c1ae072Ssthen          hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
9562c1ae072Ssthen   p = &qq[1];
9572c1ae072Ssthen   printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
9582c1ae072Ssthen          hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
9592c1ae072Ssthen          hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
9602c1ae072Ssthen          hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
9612c1ae072Ssthen          hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
9622c1ae072Ssthen          hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
9632c1ae072Ssthen          hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
9642c1ae072Ssthen   p = &qqq[2];
9652c1ae072Ssthen   printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
9662c1ae072Ssthen          hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
9672c1ae072Ssthen          hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
9682c1ae072Ssthen          hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
9692c1ae072Ssthen          hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
9702c1ae072Ssthen          hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
9712c1ae072Ssthen          hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
9722c1ae072Ssthen   p = &qqqq[3];
9732c1ae072Ssthen   printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
9742c1ae072Ssthen          hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
9752c1ae072Ssthen          hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
9762c1ae072Ssthen          hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
9772c1ae072Ssthen          hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
9782c1ae072Ssthen          hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
9792c1ae072Ssthen          hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
9802c1ae072Ssthen   printf("\n");
9812c1ae072Ssthen 
9822c1ae072Ssthen   /* check that hashlittle2 and hashlittle produce the same results */
9832c1ae072Ssthen   i=47; j=0;
9842c1ae072Ssthen   hashlittle2(q, sizeof(q), &i, &j);
9852c1ae072Ssthen   if (hashlittle(q, sizeof(q), 47) != i)
9862c1ae072Ssthen     printf("hashlittle2 and hashlittle mismatch\n");
9872c1ae072Ssthen 
9882c1ae072Ssthen   /* check that hashword2 and hashword produce the same results */
9892c1ae072Ssthen   len = raninit;
9902c1ae072Ssthen   i=47, j=0;
9912c1ae072Ssthen   hashword2(&len, 1, &i, &j);
9922c1ae072Ssthen   if (hashword(&len, 1, 47) != i)
9932c1ae072Ssthen     printf("hashword2 and hashword mismatch %x %x\n",
9942c1ae072Ssthen 	   i, hashword(&len, 1, 47));
9952c1ae072Ssthen 
9962c1ae072Ssthen   /* check hashlittle doesn't read before or after the ends of the string */
9972c1ae072Ssthen   for (h=0, b=buf+1; h<8; ++h, ++b)
9982c1ae072Ssthen   {
9992c1ae072Ssthen     for (i=0; i<MAXLEN; ++i)
10002c1ae072Ssthen     {
10012c1ae072Ssthen       len = i;
10022c1ae072Ssthen       for (j=0; j<i; ++j) *(b+j)=0;
10032c1ae072Ssthen 
10042c1ae072Ssthen       /* these should all be equal */
10052c1ae072Ssthen       ref = hashlittle(b, len, (uint32_t)1);
10062c1ae072Ssthen       *(b+i)=(uint8_t)~0;
10072c1ae072Ssthen       *(b-1)=(uint8_t)~0;
10082c1ae072Ssthen       x = hashlittle(b, len, (uint32_t)1);
10092c1ae072Ssthen       y = hashlittle(b, len, (uint32_t)1);
10102c1ae072Ssthen       if ((ref != x) || (ref != y))
10112c1ae072Ssthen       {
10122c1ae072Ssthen 	printf("alignment error: %.8x %.8x %.8x %d %d\n",ref,x,y,
10132c1ae072Ssthen                h, i);
10142c1ae072Ssthen       }
10152c1ae072Ssthen     }
10162c1ae072Ssthen   }
10172c1ae072Ssthen }
10182c1ae072Ssthen 
10192c1ae072Ssthen /* check for problems with nulls */
driver4()10202c1ae072Ssthen  void driver4()
10212c1ae072Ssthen {
10222c1ae072Ssthen   uint8_t buf[1];
10232c1ae072Ssthen   uint32_t h,i,state[HASHSTATE];
10242c1ae072Ssthen 
10252c1ae072Ssthen 
10262c1ae072Ssthen   buf[0] = ~0;
10272c1ae072Ssthen   for (i=0; i<HASHSTATE; ++i) state[i] = 1;
10282c1ae072Ssthen   printf("These should all be different\n");
10292c1ae072Ssthen   for (i=0, h=0; i<8; ++i)
10302c1ae072Ssthen   {
10312c1ae072Ssthen     h = hashlittle(buf, 0, h);
10322c1ae072Ssthen     printf("%2ld  0-byte strings, hash is  %.8x\n", i, h);
10332c1ae072Ssthen   }
10342c1ae072Ssthen }
10352c1ae072Ssthen 
10362c1ae072Ssthen 
main()10372c1ae072Ssthen int main()
10382c1ae072Ssthen {
10392c1ae072Ssthen   driver1();   /* test that the key is hashed: used for timings */
10402c1ae072Ssthen   driver2();   /* test that whole key is hashed thoroughly */
10412c1ae072Ssthen   driver3();   /* test that nothing but the key is hashed */
10422c1ae072Ssthen   driver4();   /* test hashing multiple buffers (all buffers are null) */
10432c1ae072Ssthen   return 1;
10442c1ae072Ssthen }
10452c1ae072Ssthen 
10462c1ae072Ssthen #endif  /* SELF_TEST */
1047