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