xref: /openbsd-src/usr.sbin/unbound/util/storage/lookup3.c (revision 98bc733b08604094f4138174a0ee0bb9faaca4bd)
1933707f3Ssthen /*
2550cf4a9Ssthen   May 2019(Wouter) patch to enable the valgrind clean implementation all the
3550cf4a9Ssthen      time.  This enables better security audit and checks, which is better
4550cf4a9Ssthen      than the speedup.  Git issue #30.  Renamed the define ARRAY_CLEAN_ACCESS.
5229e174cSsthen   February 2013(Wouter) patch defines for BSD endianness, from Brad Smith.
6933707f3Ssthen   January 2012(Wouter) added randomised initial value, fallout from 28c3.
7933707f3Ssthen   March 2007(Wouter) adapted from lookup3.c original, add config.h include.
8933707f3Ssthen      added #ifdef VALGRIND to remove 298,384,660 'unused variable k8' warnings.
9933707f3Ssthen      added include of lookup3.h to check definitions match declarations.
10933707f3Ssthen      removed include of stdint - config.h takes care of platform independence.
117191de28Ssthen      added fallthrough comments for new gcc warning suppression.
12933707f3Ssthen   url http://burtleburtle.net/bob/hash/index.html.
13933707f3Ssthen */
14933707f3Ssthen /*
15933707f3Ssthen -------------------------------------------------------------------------------
16933707f3Ssthen lookup3.c, by Bob Jenkins, May 2006, Public Domain.
17933707f3Ssthen 
18933707f3Ssthen These are functions for producing 32-bit hashes for hash table lookup.
19933707f3Ssthen hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
20933707f3Ssthen are externally useful functions.  Routines to test the hash are included
21933707f3Ssthen if SELF_TEST is defined.  You can use this free for any purpose.  It's in
22933707f3Ssthen the public domain.  It has no warranty.
23933707f3Ssthen 
24933707f3Ssthen You probably want to use hashlittle().  hashlittle() and hashbig()
25933707f3Ssthen hash byte arrays.  hashlittle() is is faster than hashbig() on
26933707f3Ssthen little-endian machines.  Intel and AMD are little-endian machines.
27933707f3Ssthen On second thought, you probably want hashlittle2(), which is identical to
28933707f3Ssthen hashlittle() except it returns two 32-bit hashes for the price of one.
29933707f3Ssthen You could implement hashbig2() if you wanted but I haven't bothered here.
30933707f3Ssthen 
31933707f3Ssthen If you want to find a hash of, say, exactly 7 integers, do
32933707f3Ssthen   a = i1;  b = i2;  c = i3;
33933707f3Ssthen   mix(a,b,c);
34933707f3Ssthen   a += i4; b += i5; c += i6;
35933707f3Ssthen   mix(a,b,c);
36933707f3Ssthen   a += i7;
37933707f3Ssthen   final(a,b,c);
38933707f3Ssthen then use c as the hash value.  If you have a variable length array of
39933707f3Ssthen 4-byte integers to hash, use hashword().  If you have a byte array (like
40933707f3Ssthen a character string), use hashlittle().  If you have several byte arrays, or
41933707f3Ssthen a mix of things, see the comments above hashlittle().
42933707f3Ssthen 
43933707f3Ssthen Why is this so big?  I read 12 bytes at a time into 3 4-byte integers,
44933707f3Ssthen then mix those integers.  This is fast (you can do a lot more thorough
45933707f3Ssthen mixing with 12*3 instructions on 3 integers than you can with 3 instructions
46933707f3Ssthen on 1 byte), but shoehorning those bytes into integers efficiently is messy.
47933707f3Ssthen -------------------------------------------------------------------------------
48933707f3Ssthen */
49933707f3Ssthen /*#define SELF_TEST 1*/
50550cf4a9Ssthen #define ARRAY_CLEAN_ACCESS 1
51933707f3Ssthen 
52933707f3Ssthen #include "config.h"
53933707f3Ssthen #include "util/storage/lookup3.h"
54933707f3Ssthen #include <stdio.h>      /* defines printf for tests */
55933707f3Ssthen #include <time.h>       /* defines time_t for timings in the test */
56191f22c6Ssthen 
57191f22c6Ssthen /*
58191f22c6Ssthen  * If our build system provides endianness info, signalled by
59191f22c6Ssthen  * HAVE_TARGET_ENDIANNESS and the presence or absence of TARGET_IS_BIG_ENDIAN,
60191f22c6Ssthen  * use that. Otherwise try to work out the endianness.
61191f22c6Ssthen  */
62191f22c6Ssthen #if defined(HAVE_TARGET_ENDIANNESS)
63191f22c6Ssthen # if defined(TARGET_IS_BIG_ENDIAN)
64191f22c6Ssthen #  define HASH_LITTLE_ENDIAN 0
65191f22c6Ssthen #  define HASH_BIG_ENDIAN 1
66191f22c6Ssthen # else
67191f22c6Ssthen #  define HASH_LITTLE_ENDIAN 1
68191f22c6Ssthen #  define HASH_BIG_ENDIAN 0
69191f22c6Ssthen # endif
70191f22c6Ssthen #else
71933707f3Ssthen # include <sys/param.h>  /* attempt to define endianness */
72229e174cSsthen # ifdef HAVE_SYS_TYPES_H
73229e174cSsthen #  include <sys/types.h> /* attempt to define endianness (solaris) */
74229e174cSsthen # endif
7598f3ca02Sbrad # if defined(linux) || defined(__OpenBSD__)
7698f3ca02Sbrad #  ifdef HAVE_ENDIAN_H
77933707f3Ssthen #    include <endian.h>    /* attempt to define endianness */
7898f3ca02Sbrad #  else
7998f3ca02Sbrad #    include <machine/endian.h> /* on older OpenBSD */
8098f3ca02Sbrad #  endif
81933707f3Ssthen # endif
82229e174cSsthen # if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
83229e174cSsthen #  include <sys/endian.h> /* attempt to define endianness */
84229e174cSsthen # endif
85933707f3Ssthen   /*
86933707f3Ssthen    * My best guess at if you are big-endian or little-endian.  This may
87933707f3Ssthen    * need adjustment.
88933707f3Ssthen    */
89933707f3Ssthen # if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
90933707f3Ssthen       __BYTE_ORDER == __LITTLE_ENDIAN) || \
91933707f3Ssthen      (defined(i386) || defined(__i386__) || defined(__i486__) || \
922bdc0ed1Ssthen       defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL) || defined(__x86) || defined(__loongarch__))
93933707f3Ssthen #  define HASH_LITTLE_ENDIAN 1
94933707f3Ssthen #  define HASH_BIG_ENDIAN 0
95933707f3Ssthen # elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
96933707f3Ssthen         __BYTE_ORDER == __BIG_ENDIAN) || \
97229e174cSsthen        (defined(sparc) || defined(__sparc) || defined(__sparc__) || defined(POWERPC) || defined(mc68000) || defined(sel))
98933707f3Ssthen #  define HASH_LITTLE_ENDIAN 0
99933707f3Ssthen #  define HASH_BIG_ENDIAN 1
100229e174cSsthen # elif defined(_MACHINE_ENDIAN_H_)
101229e174cSsthen   /* test for machine_endian_h protects failure if some are empty strings */
102229e174cSsthen #  if defined(_BYTE_ORDER) && defined(_BIG_ENDIAN) && _BYTE_ORDER == _BIG_ENDIAN
103229e174cSsthen #   define HASH_LITTLE_ENDIAN 0
104229e174cSsthen #   define HASH_BIG_ENDIAN 1
105229e174cSsthen #  endif
106229e174cSsthen #  if defined(_BYTE_ORDER) && defined(_LITTLE_ENDIAN) && _BYTE_ORDER == _LITTLE_ENDIAN
107229e174cSsthen #   define HASH_LITTLE_ENDIAN 1
108229e174cSsthen #   define HASH_BIG_ENDIAN 0
109229e174cSsthen #  endif /* _MACHINE_ENDIAN_H_ */
110933707f3Ssthen # else
111933707f3Ssthen #  define HASH_LITTLE_ENDIAN 0
112933707f3Ssthen #  define HASH_BIG_ENDIAN 0
113933707f3Ssthen # endif
114191f22c6Ssthen #endif /* defined(HAVE_TARGET_ENDIANNESS) */
115933707f3Ssthen 
116933707f3Ssthen #define hashsize(n) ((uint32_t)1<<(n))
117933707f3Ssthen #define hashmask(n) (hashsize(n)-1)
118933707f3Ssthen #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
119933707f3Ssthen 
120191f22c6Ssthen /* random initial value */
121191f22c6Ssthen static uint32_t raninit = (uint32_t)0xdeadbeef;
122191f22c6Ssthen 
123191f22c6Ssthen void
124191f22c6Ssthen hash_set_raninit(uint32_t v)
125191f22c6Ssthen {
126191f22c6Ssthen 	raninit = v;
127191f22c6Ssthen }
128191f22c6Ssthen 
129933707f3Ssthen /*
130933707f3Ssthen -------------------------------------------------------------------------------
131933707f3Ssthen mix -- mix 3 32-bit values reversibly.
132933707f3Ssthen 
133933707f3Ssthen This is reversible, so any information in (a,b,c) before mix() is
134933707f3Ssthen still in (a,b,c) after mix().
135933707f3Ssthen 
136933707f3Ssthen If four pairs of (a,b,c) inputs are run through mix(), or through
137933707f3Ssthen mix() in reverse, there are at least 32 bits of the output that
138933707f3Ssthen are sometimes the same for one pair and different for another pair.
139933707f3Ssthen This was tested for:
140933707f3Ssthen * pairs that differed by one bit, by two bits, in any combination
141933707f3Ssthen   of top bits of (a,b,c), or in any combination of bottom bits of
142933707f3Ssthen   (a,b,c).
143933707f3Ssthen * "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
144933707f3Ssthen   the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
145933707f3Ssthen   is commonly produced by subtraction) look like a single 1-bit
146933707f3Ssthen   difference.
147933707f3Ssthen * the base values were pseudorandom, all zero but one bit set, or
148933707f3Ssthen   all zero plus a counter that starts at zero.
149933707f3Ssthen 
150933707f3Ssthen Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
151933707f3Ssthen satisfy this are
152933707f3Ssthen     4  6  8 16 19  4
153933707f3Ssthen     9 15  3 18 27 15
154933707f3Ssthen    14  9  3  7 17  3
155933707f3Ssthen Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
156933707f3Ssthen for "differ" defined as + with a one-bit base and a two-bit delta.  I
157933707f3Ssthen used http://burtleburtle.net/bob/hash/avalanche.html to choose
158933707f3Ssthen the operations, constants, and arrangements of the variables.
159933707f3Ssthen 
160933707f3Ssthen This does not achieve avalanche.  There are input bits of (a,b,c)
161933707f3Ssthen that fail to affect some output bits of (a,b,c), especially of a.  The
162933707f3Ssthen most thoroughly mixed value is c, but it doesn't really even achieve
163933707f3Ssthen avalanche in c.
164933707f3Ssthen 
165933707f3Ssthen This allows some parallelism.  Read-after-writes are good at doubling
166933707f3Ssthen the number of bits affected, so the goal of mixing pulls in the opposite
167933707f3Ssthen direction as the goal of parallelism.  I did what I could.  Rotates
168933707f3Ssthen seem to cost as much as shifts on every machine I could lay my hands
169933707f3Ssthen on, and rotates are much kinder to the top and bottom bits, so I used
170933707f3Ssthen rotates.
171933707f3Ssthen -------------------------------------------------------------------------------
172933707f3Ssthen */
173933707f3Ssthen #define mix(a,b,c) \
174933707f3Ssthen { \
175933707f3Ssthen   a -= c;  a ^= rot(c, 4);  c += b; \
176933707f3Ssthen   b -= a;  b ^= rot(a, 6);  a += c; \
177933707f3Ssthen   c -= b;  c ^= rot(b, 8);  b += a; \
178933707f3Ssthen   a -= c;  a ^= rot(c,16);  c += b; \
179933707f3Ssthen   b -= a;  b ^= rot(a,19);  a += c; \
180933707f3Ssthen   c -= b;  c ^= rot(b, 4);  b += a; \
181933707f3Ssthen }
182933707f3Ssthen 
183933707f3Ssthen /*
184933707f3Ssthen -------------------------------------------------------------------------------
185933707f3Ssthen final -- final mixing of 3 32-bit values (a,b,c) into c
186933707f3Ssthen 
187933707f3Ssthen Pairs of (a,b,c) values differing in only a few bits will usually
188933707f3Ssthen produce values of c that look totally different.  This was tested for
189933707f3Ssthen * pairs that differed by one bit, by two bits, in any combination
190933707f3Ssthen   of top bits of (a,b,c), or in any combination of bottom bits of
191933707f3Ssthen   (a,b,c).
192933707f3Ssthen * "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
193933707f3Ssthen   the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
194933707f3Ssthen   is commonly produced by subtraction) look like a single 1-bit
195933707f3Ssthen   difference.
196933707f3Ssthen * the base values were pseudorandom, all zero but one bit set, or
197933707f3Ssthen   all zero plus a counter that starts at zero.
198933707f3Ssthen 
199933707f3Ssthen These constants passed:
200933707f3Ssthen  14 11 25 16 4 14 24
201933707f3Ssthen  12 14 25 16 4 14 24
202933707f3Ssthen and these came close:
203933707f3Ssthen   4  8 15 26 3 22 24
204933707f3Ssthen  10  8 15 26 3 22 24
205933707f3Ssthen  11  8 15 26 3 22 24
206933707f3Ssthen -------------------------------------------------------------------------------
207933707f3Ssthen */
208933707f3Ssthen #define final(a,b,c) \
209933707f3Ssthen { \
210933707f3Ssthen   c ^= b; c -= rot(b,14); \
211933707f3Ssthen   a ^= c; a -= rot(c,11); \
212933707f3Ssthen   b ^= a; b -= rot(a,25); \
213933707f3Ssthen   c ^= b; c -= rot(b,16); \
214933707f3Ssthen   a ^= c; a -= rot(c,4);  \
215933707f3Ssthen   b ^= a; b -= rot(a,14); \
216933707f3Ssthen   c ^= b; c -= rot(b,24); \
217933707f3Ssthen }
218933707f3Ssthen 
219933707f3Ssthen /*
220933707f3Ssthen --------------------------------------------------------------------
221933707f3Ssthen  This works on all machines.  To be useful, it requires
222933707f3Ssthen  -- that the key be an array of uint32_t's, and
223933707f3Ssthen  -- that the length be the number of uint32_t's in the key
224933707f3Ssthen 
225933707f3Ssthen  The function hashword() is identical to hashlittle() on little-endian
226933707f3Ssthen  machines, and identical to hashbig() on big-endian machines,
227933707f3Ssthen  except that the length has to be measured in uint32_ts rather than in
228933707f3Ssthen  bytes.  hashlittle() is more complicated than hashword() only because
229933707f3Ssthen  hashlittle() has to dance around fitting the key bytes into registers.
230933707f3Ssthen --------------------------------------------------------------------
231933707f3Ssthen */
232933707f3Ssthen uint32_t hashword(
233933707f3Ssthen const uint32_t *k,                   /* the key, an array of uint32_t values */
234933707f3Ssthen size_t          length,               /* the length of the key, in uint32_ts */
235933707f3Ssthen uint32_t        initval)         /* the previous hash, or an arbitrary value */
236933707f3Ssthen {
237933707f3Ssthen   uint32_t a,b,c;
238933707f3Ssthen 
239933707f3Ssthen   /* Set up the internal state */
240933707f3Ssthen   a = b = c = raninit + (((uint32_t)length)<<2) + initval;
241933707f3Ssthen 
242933707f3Ssthen   /*------------------------------------------------- handle most of the key */
243933707f3Ssthen   while (length > 3)
244933707f3Ssthen   {
245933707f3Ssthen     a += k[0];
246933707f3Ssthen     b += k[1];
247933707f3Ssthen     c += k[2];
248933707f3Ssthen     mix(a,b,c);
249933707f3Ssthen     length -= 3;
250933707f3Ssthen     k += 3;
251933707f3Ssthen   }
252933707f3Ssthen 
253933707f3Ssthen   /*------------------------------------------- handle the last 3 uint32_t's */
254933707f3Ssthen   switch(length)                     /* all the case statements fall through */
255933707f3Ssthen   {
256933707f3Ssthen   case 3 : c+=k[2];
257*98bc733bSsthen 	ATTR_FALLTHROUGH
2587191de28Ssthen   	/* fallthrough */
259933707f3Ssthen   case 2 : b+=k[1];
260*98bc733bSsthen 	ATTR_FALLTHROUGH
2617191de28Ssthen   	/* fallthrough */
262933707f3Ssthen   case 1 : a+=k[0];
263933707f3Ssthen     final(a,b,c);
264*98bc733bSsthen 	ATTR_FALLTHROUGH
265*98bc733bSsthen 	/* fallthrough */
266933707f3Ssthen   case 0:     /* case 0: nothing left to add */
267933707f3Ssthen     break;
268933707f3Ssthen   }
269933707f3Ssthen   /*------------------------------------------------------ report the result */
270933707f3Ssthen   return c;
271933707f3Ssthen }
272933707f3Ssthen 
273933707f3Ssthen 
274933707f3Ssthen #ifdef SELF_TEST
275933707f3Ssthen 
276933707f3Ssthen /*
277933707f3Ssthen --------------------------------------------------------------------
278933707f3Ssthen hashword2() -- same as hashword(), but take two seeds and return two
279933707f3Ssthen 32-bit values.  pc and pb must both be nonnull, and *pc and *pb must
280933707f3Ssthen both be initialized with seeds.  If you pass in (*pb)==0, the output
281933707f3Ssthen (*pc) will be the same as the return value from hashword().
282933707f3Ssthen --------------------------------------------------------------------
283933707f3Ssthen */
284933707f3Ssthen void hashword2 (
285933707f3Ssthen const uint32_t *k,                   /* the key, an array of uint32_t values */
286933707f3Ssthen size_t          length,               /* the length of the key, in uint32_ts */
287933707f3Ssthen uint32_t       *pc,                      /* IN: seed OUT: primary hash value */
288933707f3Ssthen uint32_t       *pb)               /* IN: more seed OUT: secondary hash value */
289933707f3Ssthen {
290933707f3Ssthen   uint32_t a,b,c;
291933707f3Ssthen 
292933707f3Ssthen   /* Set up the internal state */
293933707f3Ssthen   a = b = c = raninit + ((uint32_t)(length<<2)) + *pc;
294933707f3Ssthen   c += *pb;
295933707f3Ssthen 
296933707f3Ssthen   /*------------------------------------------------- handle most of the key */
297933707f3Ssthen   while (length > 3)
298933707f3Ssthen   {
299933707f3Ssthen     a += k[0];
300933707f3Ssthen     b += k[1];
301933707f3Ssthen     c += k[2];
302933707f3Ssthen     mix(a,b,c);
303933707f3Ssthen     length -= 3;
304933707f3Ssthen     k += 3;
305933707f3Ssthen   }
306933707f3Ssthen 
307933707f3Ssthen   /*------------------------------------------- handle the last 3 uint32_t's */
308933707f3Ssthen   switch(length)                     /* all the case statements fall through */
309933707f3Ssthen   {
310933707f3Ssthen   case 3 : c+=k[2];
311*98bc733bSsthen 	ATTR_FALLTHROUGH
312*98bc733bSsthen 	/* fallthrough */
313933707f3Ssthen   case 2 : b+=k[1];
314*98bc733bSsthen 	ATTR_FALLTHROUGH
315*98bc733bSsthen 	/* fallthrough */
316933707f3Ssthen   case 1 : a+=k[0];
317933707f3Ssthen     final(a,b,c);
318*98bc733bSsthen 	ATTR_FALLTHROUGH
319*98bc733bSsthen 	/* fallthrough */
320933707f3Ssthen   case 0:     /* case 0: nothing left to add */
321933707f3Ssthen     break;
322933707f3Ssthen   }
323933707f3Ssthen   /*------------------------------------------------------ report the result */
324933707f3Ssthen   *pc=c; *pb=b;
325933707f3Ssthen }
326933707f3Ssthen 
327933707f3Ssthen #endif /* SELF_TEST */
328933707f3Ssthen 
329933707f3Ssthen /*
330933707f3Ssthen -------------------------------------------------------------------------------
331933707f3Ssthen hashlittle() -- hash a variable-length key into a 32-bit value
332933707f3Ssthen   k       : the key (the unaligned variable-length array of bytes)
333933707f3Ssthen   length  : the length of the key, counting by bytes
334933707f3Ssthen   initval : can be any 4-byte value
335933707f3Ssthen Returns a 32-bit value.  Every bit of the key affects every bit of
336933707f3Ssthen the return value.  Two keys differing by one or two bits will have
337933707f3Ssthen totally different hash values.
338933707f3Ssthen 
339933707f3Ssthen The best hash table sizes are powers of 2.  There is no need to do
340933707f3Ssthen mod a prime (mod is sooo slow!).  If you need less than 32 bits,
341933707f3Ssthen use a bitmask.  For example, if you need only 10 bits, do
342933707f3Ssthen   h = (h & hashmask(10));
343933707f3Ssthen In which case, the hash table should have hashsize(10) elements.
344933707f3Ssthen 
345933707f3Ssthen If you are hashing n strings (uint8_t **)k, do it like this:
346933707f3Ssthen   for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
347933707f3Ssthen 
348933707f3Ssthen By Bob Jenkins, 2006.  bob_jenkins@burtleburtle.net.  You may use this
349933707f3Ssthen code any way you wish, private, educational, or commercial.  It's free.
350933707f3Ssthen 
351933707f3Ssthen Use for hash table lookup, or anything where one collision in 2^^32 is
352933707f3Ssthen acceptable.  Do NOT use for cryptographic purposes.
353933707f3Ssthen -------------------------------------------------------------------------------
354933707f3Ssthen */
355933707f3Ssthen 
356933707f3Ssthen uint32_t hashlittle( const void *key, size_t length, uint32_t initval)
357933707f3Ssthen {
358933707f3Ssthen   uint32_t a,b,c;                                          /* internal state */
359933707f3Ssthen   union { const void *ptr; size_t i; } u;     /* needed for Mac Powerbook G4 */
360933707f3Ssthen 
361933707f3Ssthen   /* Set up the internal state */
362933707f3Ssthen   a = b = c = raninit + ((uint32_t)length) + initval;
363933707f3Ssthen 
364933707f3Ssthen   u.ptr = key;
365933707f3Ssthen   if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
366933707f3Ssthen     const uint32_t *k = (const uint32_t *)key;         /* read 32-bit chunks */
367550cf4a9Ssthen #ifdef ARRAY_CLEAN_ACCESS
368933707f3Ssthen     const uint8_t  *k8;
369933707f3Ssthen #endif
370933707f3Ssthen 
371933707f3Ssthen     /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
372933707f3Ssthen     while (length > 12)
373933707f3Ssthen     {
374933707f3Ssthen       a += k[0];
375933707f3Ssthen       b += k[1];
376933707f3Ssthen       c += k[2];
377933707f3Ssthen       mix(a,b,c);
378933707f3Ssthen       length -= 12;
379933707f3Ssthen       k += 3;
380933707f3Ssthen     }
381933707f3Ssthen 
382933707f3Ssthen     /*----------------------------- handle the last (probably partial) block */
383933707f3Ssthen     /*
384933707f3Ssthen      * "k[2]&0xffffff" actually reads beyond the end of the string, but
385933707f3Ssthen      * then masks off the part it's not allowed to read.  Because the
386933707f3Ssthen      * string is aligned, the masked-off tail is in the same word as the
387933707f3Ssthen      * rest of the string.  Every machine with memory protection I've seen
388933707f3Ssthen      * does it on word boundaries, so is OK with this.  But VALGRIND will
389933707f3Ssthen      * still catch it and complain.  The masking trick does make the hash
3904bfc71b0Ssthen      * noticeably faster for short strings (like English words).
391933707f3Ssthen      */
392550cf4a9Ssthen #ifndef ARRAY_CLEAN_ACCESS
393933707f3Ssthen 
394933707f3Ssthen     switch(length)
395933707f3Ssthen     {
396933707f3Ssthen     case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
397933707f3Ssthen     case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
398933707f3Ssthen     case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
399933707f3Ssthen     case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
400933707f3Ssthen     case 8 : b+=k[1]; a+=k[0]; break;
401933707f3Ssthen     case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
402933707f3Ssthen     case 6 : b+=k[1]&0xffff; a+=k[0]; break;
403933707f3Ssthen     case 5 : b+=k[1]&0xff; a+=k[0]; break;
404933707f3Ssthen     case 4 : a+=k[0]; break;
405933707f3Ssthen     case 3 : a+=k[0]&0xffffff; break;
406933707f3Ssthen     case 2 : a+=k[0]&0xffff; break;
407933707f3Ssthen     case 1 : a+=k[0]&0xff; break;
408933707f3Ssthen     case 0 : return c;              /* zero length strings require no mixing */
409933707f3Ssthen     }
410933707f3Ssthen 
411933707f3Ssthen #else /* make valgrind happy */
412933707f3Ssthen 
413933707f3Ssthen     k8 = (const uint8_t *)k;
414933707f3Ssthen     switch(length)
415933707f3Ssthen     {
416933707f3Ssthen     case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
417*98bc733bSsthen     case 11: c+=((uint32_t)k8[10])<<16;
418*98bc733bSsthen 	ATTR_FALLTHROUGH
419*98bc733bSsthen 	/* fallthrough */
420*98bc733bSsthen     case 10: c+=((uint32_t)k8[9])<<8;
421*98bc733bSsthen 	ATTR_FALLTHROUGH
422*98bc733bSsthen 	/* fallthrough */
423*98bc733bSsthen     case 9 : c+=k8[8];
424*98bc733bSsthen 	ATTR_FALLTHROUGH
425*98bc733bSsthen 	/* fallthrough */
426933707f3Ssthen     case 8 : b+=k[1]; a+=k[0]; break;
427*98bc733bSsthen     case 7 : b+=((uint32_t)k8[6])<<16;
428*98bc733bSsthen 	ATTR_FALLTHROUGH
429*98bc733bSsthen 	/* fallthrough */
430*98bc733bSsthen     case 6 : b+=((uint32_t)k8[5])<<8;
431*98bc733bSsthen 	ATTR_FALLTHROUGH
432*98bc733bSsthen 	/* fallthrough */
433*98bc733bSsthen     case 5 : b+=k8[4];
434*98bc733bSsthen 	ATTR_FALLTHROUGH
435*98bc733bSsthen 	/* fallthrough */
436933707f3Ssthen     case 4 : a+=k[0]; break;
437*98bc733bSsthen     case 3 : a+=((uint32_t)k8[2])<<16;
438*98bc733bSsthen 	ATTR_FALLTHROUGH
439*98bc733bSsthen 	/* fallthrough */
440*98bc733bSsthen     case 2 : a+=((uint32_t)k8[1])<<8;
441*98bc733bSsthen 	ATTR_FALLTHROUGH
442*98bc733bSsthen 	/* fallthrough */
443933707f3Ssthen     case 1 : a+=k8[0]; break;
444933707f3Ssthen     case 0 : return c;
445933707f3Ssthen     }
446933707f3Ssthen 
447933707f3Ssthen #endif /* !valgrind */
448933707f3Ssthen 
449933707f3Ssthen   } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
450933707f3Ssthen     const uint16_t *k = (const uint16_t *)key;         /* read 16-bit chunks */
451933707f3Ssthen     const uint8_t  *k8;
452933707f3Ssthen 
453933707f3Ssthen     /*--------------- all but last block: aligned reads and different mixing */
454933707f3Ssthen     while (length > 12)
455933707f3Ssthen     {
456933707f3Ssthen       a += k[0] + (((uint32_t)k[1])<<16);
457933707f3Ssthen       b += k[2] + (((uint32_t)k[3])<<16);
458933707f3Ssthen       c += k[4] + (((uint32_t)k[5])<<16);
459933707f3Ssthen       mix(a,b,c);
460933707f3Ssthen       length -= 12;
461933707f3Ssthen       k += 6;
462933707f3Ssthen     }
463933707f3Ssthen 
464933707f3Ssthen     /*----------------------------- handle the last (probably partial) block */
465933707f3Ssthen     k8 = (const uint8_t *)k;
466933707f3Ssthen     switch(length)
467933707f3Ssthen     {
468933707f3Ssthen     case 12: c+=k[4]+(((uint32_t)k[5])<<16);
469933707f3Ssthen              b+=k[2]+(((uint32_t)k[3])<<16);
470933707f3Ssthen              a+=k[0]+(((uint32_t)k[1])<<16);
471933707f3Ssthen              break;
472*98bc733bSsthen     case 11: c+=((uint32_t)k8[10])<<16;
473*98bc733bSsthen 	     ATTR_FALLTHROUGH
474*98bc733bSsthen 	     /* fallthrough */
475933707f3Ssthen     case 10: c+=k[4];
476933707f3Ssthen              b+=k[2]+(((uint32_t)k[3])<<16);
477933707f3Ssthen              a+=k[0]+(((uint32_t)k[1])<<16);
478933707f3Ssthen              break;
479*98bc733bSsthen     case 9 : c+=k8[8];
480*98bc733bSsthen 	     ATTR_FALLTHROUGH
481*98bc733bSsthen 	     /* fallthrough */
482933707f3Ssthen     case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
483933707f3Ssthen              a+=k[0]+(((uint32_t)k[1])<<16);
484933707f3Ssthen              break;
485*98bc733bSsthen     case 7 : b+=((uint32_t)k8[6])<<16;
486*98bc733bSsthen 	     ATTR_FALLTHROUGH
487*98bc733bSsthen 	     /* fallthrough */
488933707f3Ssthen     case 6 : b+=k[2];
489933707f3Ssthen              a+=k[0]+(((uint32_t)k[1])<<16);
490933707f3Ssthen              break;
491*98bc733bSsthen     case 5 : b+=k8[4];
492*98bc733bSsthen 	     ATTR_FALLTHROUGH
493*98bc733bSsthen 	     /* fallthrough */
494933707f3Ssthen     case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
495933707f3Ssthen              break;
496*98bc733bSsthen     case 3 : a+=((uint32_t)k8[2])<<16;
497*98bc733bSsthen 	     ATTR_FALLTHROUGH
498*98bc733bSsthen 	     /* fallthrough */
499933707f3Ssthen     case 2 : a+=k[0];
500933707f3Ssthen              break;
501933707f3Ssthen     case 1 : a+=k8[0];
502933707f3Ssthen              break;
503933707f3Ssthen     case 0 : return c;                     /* zero length requires no mixing */
504933707f3Ssthen     }
505933707f3Ssthen 
506933707f3Ssthen   } else {                        /* need to read the key one byte at a time */
507933707f3Ssthen     const uint8_t *k = (const uint8_t *)key;
508933707f3Ssthen 
509933707f3Ssthen     /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
510933707f3Ssthen     while (length > 12)
511933707f3Ssthen     {
512933707f3Ssthen       a += k[0];
513933707f3Ssthen       a += ((uint32_t)k[1])<<8;
514933707f3Ssthen       a += ((uint32_t)k[2])<<16;
515933707f3Ssthen       a += ((uint32_t)k[3])<<24;
516933707f3Ssthen       b += k[4];
517933707f3Ssthen       b += ((uint32_t)k[5])<<8;
518933707f3Ssthen       b += ((uint32_t)k[6])<<16;
519933707f3Ssthen       b += ((uint32_t)k[7])<<24;
520933707f3Ssthen       c += k[8];
521933707f3Ssthen       c += ((uint32_t)k[9])<<8;
522933707f3Ssthen       c += ((uint32_t)k[10])<<16;
523933707f3Ssthen       c += ((uint32_t)k[11])<<24;
524933707f3Ssthen       mix(a,b,c);
525933707f3Ssthen       length -= 12;
526933707f3Ssthen       k += 12;
527933707f3Ssthen     }
528933707f3Ssthen 
529933707f3Ssthen     /*-------------------------------- last block: affect all 32 bits of (c) */
530933707f3Ssthen     switch(length)                   /* all the case statements fall through */
531933707f3Ssthen     {
532933707f3Ssthen     case 12: c+=((uint32_t)k[11])<<24;
533*98bc733bSsthen 	ATTR_FALLTHROUGH
5347191de28Ssthen   	/* fallthrough */
535933707f3Ssthen     case 11: c+=((uint32_t)k[10])<<16;
536*98bc733bSsthen 	ATTR_FALLTHROUGH
5377191de28Ssthen   	/* fallthrough */
538933707f3Ssthen     case 10: c+=((uint32_t)k[9])<<8;
539*98bc733bSsthen 	ATTR_FALLTHROUGH
5407191de28Ssthen   	/* fallthrough */
541933707f3Ssthen     case 9 : c+=k[8];
542*98bc733bSsthen 	ATTR_FALLTHROUGH
5437191de28Ssthen   	/* fallthrough */
544933707f3Ssthen     case 8 : b+=((uint32_t)k[7])<<24;
545*98bc733bSsthen 	ATTR_FALLTHROUGH
5467191de28Ssthen   	/* fallthrough */
547933707f3Ssthen     case 7 : b+=((uint32_t)k[6])<<16;
548*98bc733bSsthen 	ATTR_FALLTHROUGH
5497191de28Ssthen   	/* fallthrough */
550933707f3Ssthen     case 6 : b+=((uint32_t)k[5])<<8;
551*98bc733bSsthen 	ATTR_FALLTHROUGH
5527191de28Ssthen   	/* fallthrough */
553933707f3Ssthen     case 5 : b+=k[4];
554*98bc733bSsthen 	ATTR_FALLTHROUGH
5557191de28Ssthen   	/* fallthrough */
556933707f3Ssthen     case 4 : a+=((uint32_t)k[3])<<24;
557*98bc733bSsthen 	ATTR_FALLTHROUGH
5587191de28Ssthen   	/* fallthrough */
559933707f3Ssthen     case 3 : a+=((uint32_t)k[2])<<16;
560*98bc733bSsthen 	ATTR_FALLTHROUGH
5617191de28Ssthen   	/* fallthrough */
562933707f3Ssthen     case 2 : a+=((uint32_t)k[1])<<8;
563*98bc733bSsthen 	ATTR_FALLTHROUGH
5647191de28Ssthen   	/* fallthrough */
565933707f3Ssthen     case 1 : a+=k[0];
566933707f3Ssthen              break;
567933707f3Ssthen     case 0 : return c;
568933707f3Ssthen     }
569933707f3Ssthen   }
570933707f3Ssthen 
571933707f3Ssthen   final(a,b,c);
572933707f3Ssthen   return c;
573933707f3Ssthen }
574933707f3Ssthen 
575933707f3Ssthen #ifdef SELF_TEST
576933707f3Ssthen 
577933707f3Ssthen /*
578933707f3Ssthen  * hashlittle2: return 2 32-bit hash values
579933707f3Ssthen  *
580933707f3Ssthen  * This is identical to hashlittle(), except it returns two 32-bit hash
581933707f3Ssthen  * values instead of just one.  This is good enough for hash table
582933707f3Ssthen  * lookup with 2^^64 buckets, or if you want a second hash if you're not
583933707f3Ssthen  * happy with the first, or if you want a probably-unique 64-bit ID for
584933707f3Ssthen  * the key.  *pc is better mixed than *pb, so use *pc first.  If you want
585933707f3Ssthen  * a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)".
586933707f3Ssthen  */
587933707f3Ssthen void hashlittle2(
588933707f3Ssthen   const void *key,       /* the key to hash */
589933707f3Ssthen   size_t      length,    /* length of the key */
590933707f3Ssthen   uint32_t   *pc,        /* IN: primary initval, OUT: primary hash */
591933707f3Ssthen   uint32_t   *pb)        /* IN: secondary initval, OUT: secondary hash */
592933707f3Ssthen {
593933707f3Ssthen   uint32_t a,b,c;                                          /* internal state */
594933707f3Ssthen   union { const void *ptr; size_t i; } u;     /* needed for Mac Powerbook G4 */
595933707f3Ssthen 
596933707f3Ssthen   /* Set up the internal state */
597933707f3Ssthen   a = b = c = raninit + ((uint32_t)length) + *pc;
598933707f3Ssthen   c += *pb;
599933707f3Ssthen 
600933707f3Ssthen   u.ptr = key;
601933707f3Ssthen   if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
602933707f3Ssthen     const uint32_t *k = (const uint32_t *)key;         /* read 32-bit chunks */
603933707f3Ssthen #ifdef VALGRIND
604933707f3Ssthen     const uint8_t  *k8;
605933707f3Ssthen #endif
606933707f3Ssthen 
607933707f3Ssthen     /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
608933707f3Ssthen     while (length > 12)
609933707f3Ssthen     {
610933707f3Ssthen       a += k[0];
611933707f3Ssthen       b += k[1];
612933707f3Ssthen       c += k[2];
613933707f3Ssthen       mix(a,b,c);
614933707f3Ssthen       length -= 12;
615933707f3Ssthen       k += 3;
616933707f3Ssthen     }
617933707f3Ssthen 
618933707f3Ssthen     /*----------------------------- handle the last (probably partial) block */
619933707f3Ssthen     /*
620933707f3Ssthen      * "k[2]&0xffffff" actually reads beyond the end of the string, but
621933707f3Ssthen      * then masks off the part it's not allowed to read.  Because the
622933707f3Ssthen      * string is aligned, the masked-off tail is in the same word as the
623933707f3Ssthen      * rest of the string.  Every machine with memory protection I've seen
624933707f3Ssthen      * does it on word boundaries, so is OK with this.  But VALGRIND will
625933707f3Ssthen      * still catch it and complain.  The masking trick does make the hash
6264bfc71b0Ssthen      * noticeably faster for short strings (like English words).
627933707f3Ssthen      */
628933707f3Ssthen #ifndef VALGRIND
629933707f3Ssthen 
630933707f3Ssthen     switch(length)
631933707f3Ssthen     {
632933707f3Ssthen     case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
633933707f3Ssthen     case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
634933707f3Ssthen     case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
635933707f3Ssthen     case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
636933707f3Ssthen     case 8 : b+=k[1]; a+=k[0]; break;
637933707f3Ssthen     case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
638933707f3Ssthen     case 6 : b+=k[1]&0xffff; a+=k[0]; break;
639933707f3Ssthen     case 5 : b+=k[1]&0xff; a+=k[0]; break;
640933707f3Ssthen     case 4 : a+=k[0]; break;
641933707f3Ssthen     case 3 : a+=k[0]&0xffffff; break;
642933707f3Ssthen     case 2 : a+=k[0]&0xffff; break;
643933707f3Ssthen     case 1 : a+=k[0]&0xff; break;
644933707f3Ssthen     case 0 : *pc=c; *pb=b; return;  /* zero length strings require no mixing */
645933707f3Ssthen     }
646933707f3Ssthen 
647933707f3Ssthen #else /* make valgrind happy */
648933707f3Ssthen 
649933707f3Ssthen     k8 = (const uint8_t *)k;
650933707f3Ssthen     switch(length)
651933707f3Ssthen     {
652933707f3Ssthen     case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
653*98bc733bSsthen     case 11: c+=((uint32_t)k8[10])<<16;
654*98bc733bSsthen 	     ATTR_FALLTHROUGH
655*98bc733bSsthen 	     /* fallthrough */
656*98bc733bSsthen     case 10: c+=((uint32_t)k8[9])<<8;
657*98bc733bSsthen 	     ATTR_FALLTHROUGH
658*98bc733bSsthen 	     /* fallthrough */
659*98bc733bSsthen     case 9 : c+=k8[8];
660*98bc733bSsthen 	     ATTR_FALLTHROUGH
661*98bc733bSsthen 	     /* fallthrough */
662933707f3Ssthen     case 8 : b+=k[1]; a+=k[0]; break;
663*98bc733bSsthen     case 7 : b+=((uint32_t)k8[6])<<16;
664*98bc733bSsthen 	     ATTR_FALLTHROUGH
665*98bc733bSsthen 	     /* fallthrough */
666*98bc733bSsthen     case 6 : b+=((uint32_t)k8[5])<<8;
667*98bc733bSsthen 	     ATTR_FALLTHROUGH
668*98bc733bSsthen 	     /* fallthrough */
669*98bc733bSsthen     case 5 : b+=k8[4];
670*98bc733bSsthen 	     ATTR_FALLTHROUGH
671*98bc733bSsthen 	     /* fallthrough */
672933707f3Ssthen     case 4 : a+=k[0]; break;
673*98bc733bSsthen     case 3 : a+=((uint32_t)k8[2])<<16;
674*98bc733bSsthen 	     ATTR_FALLTHROUGH
675*98bc733bSsthen 	     /* fallthrough */
676*98bc733bSsthen     case 2 : a+=((uint32_t)k8[1])<<8;
677*98bc733bSsthen 	     ATTR_FALLTHROUGH
678*98bc733bSsthen 	     /* fallthrough */
679933707f3Ssthen     case 1 : a+=k8[0]; break;
680933707f3Ssthen     case 0 : *pc=c; *pb=b; return;  /* zero length strings require no mixing */
681933707f3Ssthen     }
682933707f3Ssthen 
683933707f3Ssthen #endif /* !valgrind */
684933707f3Ssthen 
685933707f3Ssthen   } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
686933707f3Ssthen     const uint16_t *k = (const uint16_t *)key;         /* read 16-bit chunks */
687933707f3Ssthen     const uint8_t  *k8;
688933707f3Ssthen 
689933707f3Ssthen     /*--------------- all but last block: aligned reads and different mixing */
690933707f3Ssthen     while (length > 12)
691933707f3Ssthen     {
692933707f3Ssthen       a += k[0] + (((uint32_t)k[1])<<16);
693933707f3Ssthen       b += k[2] + (((uint32_t)k[3])<<16);
694933707f3Ssthen       c += k[4] + (((uint32_t)k[5])<<16);
695933707f3Ssthen       mix(a,b,c);
696933707f3Ssthen       length -= 12;
697933707f3Ssthen       k += 6;
698933707f3Ssthen     }
699933707f3Ssthen 
700933707f3Ssthen     /*----------------------------- handle the last (probably partial) block */
701933707f3Ssthen     k8 = (const uint8_t *)k;
702933707f3Ssthen     switch(length)
703933707f3Ssthen     {
704933707f3Ssthen     case 12: c+=k[4]+(((uint32_t)k[5])<<16);
705933707f3Ssthen              b+=k[2]+(((uint32_t)k[3])<<16);
706933707f3Ssthen              a+=k[0]+(((uint32_t)k[1])<<16);
707933707f3Ssthen              break;
708*98bc733bSsthen     case 11: c+=((uint32_t)k8[10])<<16;
709*98bc733bSsthen 	     ATTR_FALLTHROUGH
710*98bc733bSsthen 	     /* fallthrough */
711933707f3Ssthen     case 10: c+=k[4];
712933707f3Ssthen              b+=k[2]+(((uint32_t)k[3])<<16);
713933707f3Ssthen              a+=k[0]+(((uint32_t)k[1])<<16);
714933707f3Ssthen              break;
715*98bc733bSsthen     case 9 : c+=k8[8];
716*98bc733bSsthen 	     ATTR_FALLTHROUGH
717*98bc733bSsthen 	     /* fallthrough */
718933707f3Ssthen     case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
719933707f3Ssthen              a+=k[0]+(((uint32_t)k[1])<<16);
720933707f3Ssthen              break;
721*98bc733bSsthen     case 7 : b+=((uint32_t)k8[6])<<16;
722*98bc733bSsthen 	     ATTR_FALLTHROUGH
723*98bc733bSsthen 	     /* fallthrough */
724933707f3Ssthen     case 6 : b+=k[2];
725933707f3Ssthen              a+=k[0]+(((uint32_t)k[1])<<16);
726933707f3Ssthen              break;
727*98bc733bSsthen     case 5 : b+=k8[4];
728*98bc733bSsthen 	     ATTR_FALLTHROUGH
729*98bc733bSsthen 	     /* fallthrough */
730933707f3Ssthen     case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
731933707f3Ssthen              break;
732*98bc733bSsthen     case 3 : a+=((uint32_t)k8[2])<<16;
733*98bc733bSsthen 	     ATTR_FALLTHROUGH
734*98bc733bSsthen 	     /* fallthrough */
735933707f3Ssthen     case 2 : a+=k[0];
736933707f3Ssthen              break;
737933707f3Ssthen     case 1 : a+=k8[0];
738933707f3Ssthen              break;
739933707f3Ssthen     case 0 : *pc=c; *pb=b; return;  /* zero length strings require no mixing */
740933707f3Ssthen     }
741933707f3Ssthen 
742933707f3Ssthen   } else {                        /* need to read the key one byte at a time */
743933707f3Ssthen     const uint8_t *k = (const uint8_t *)key;
744933707f3Ssthen 
745933707f3Ssthen     /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
746933707f3Ssthen     while (length > 12)
747933707f3Ssthen     {
748933707f3Ssthen       a += k[0];
749933707f3Ssthen       a += ((uint32_t)k[1])<<8;
750933707f3Ssthen       a += ((uint32_t)k[2])<<16;
751933707f3Ssthen       a += ((uint32_t)k[3])<<24;
752933707f3Ssthen       b += k[4];
753933707f3Ssthen       b += ((uint32_t)k[5])<<8;
754933707f3Ssthen       b += ((uint32_t)k[6])<<16;
755933707f3Ssthen       b += ((uint32_t)k[7])<<24;
756933707f3Ssthen       c += k[8];
757933707f3Ssthen       c += ((uint32_t)k[9])<<8;
758933707f3Ssthen       c += ((uint32_t)k[10])<<16;
759933707f3Ssthen       c += ((uint32_t)k[11])<<24;
760933707f3Ssthen       mix(a,b,c);
761933707f3Ssthen       length -= 12;
762933707f3Ssthen       k += 12;
763933707f3Ssthen     }
764933707f3Ssthen 
765933707f3Ssthen     /*-------------------------------- last block: affect all 32 bits of (c) */
766933707f3Ssthen     switch(length)                   /* all the case statements fall through */
767933707f3Ssthen     {
768933707f3Ssthen     case 12: c+=((uint32_t)k[11])<<24;
769*98bc733bSsthen 	     ATTR_FALLTHROUGH
770*98bc733bSsthen 	     /* fallthrough */
771933707f3Ssthen     case 11: c+=((uint32_t)k[10])<<16;
772*98bc733bSsthen 	     ATTR_FALLTHROUGH
773*98bc733bSsthen 	     /* fallthrough */
774933707f3Ssthen     case 10: c+=((uint32_t)k[9])<<8;
775*98bc733bSsthen 	     ATTR_FALLTHROUGH
776*98bc733bSsthen 	     /* fallthrough */
777933707f3Ssthen     case 9 : c+=k[8];
778*98bc733bSsthen 	     ATTR_FALLTHROUGH
779*98bc733bSsthen 	     /* fallthrough */
780933707f3Ssthen     case 8 : b+=((uint32_t)k[7])<<24;
781*98bc733bSsthen 	     ATTR_FALLTHROUGH
782*98bc733bSsthen 	     /* fallthrough */
783933707f3Ssthen     case 7 : b+=((uint32_t)k[6])<<16;
784*98bc733bSsthen 	     ATTR_FALLTHROUGH
785*98bc733bSsthen 	     /* fallthrough */
786933707f3Ssthen     case 6 : b+=((uint32_t)k[5])<<8;
787*98bc733bSsthen 	     ATTR_FALLTHROUGH
788*98bc733bSsthen 	     /* fallthrough */
789933707f3Ssthen     case 5 : b+=k[4];
790*98bc733bSsthen 	     ATTR_FALLTHROUGH
791*98bc733bSsthen 	     /* fallthrough */
792933707f3Ssthen     case 4 : a+=((uint32_t)k[3])<<24;
793*98bc733bSsthen 	     ATTR_FALLTHROUGH
794*98bc733bSsthen 	     /* fallthrough */
795933707f3Ssthen     case 3 : a+=((uint32_t)k[2])<<16;
796*98bc733bSsthen 	     ATTR_FALLTHROUGH
797*98bc733bSsthen 	     /* fallthrough */
798933707f3Ssthen     case 2 : a+=((uint32_t)k[1])<<8;
799*98bc733bSsthen 	     ATTR_FALLTHROUGH
800*98bc733bSsthen 	     /* fallthrough */
801933707f3Ssthen     case 1 : a+=k[0];
802933707f3Ssthen              break;
803933707f3Ssthen     case 0 : *pc=c; *pb=b; return;  /* zero length strings require no mixing */
804933707f3Ssthen     }
805933707f3Ssthen   }
806933707f3Ssthen 
807933707f3Ssthen   final(a,b,c);
808933707f3Ssthen   *pc=c; *pb=b;
809933707f3Ssthen }
810933707f3Ssthen 
811933707f3Ssthen #endif /* SELF_TEST */
812933707f3Ssthen 
813933707f3Ssthen #if 0	/* currently not used */
814933707f3Ssthen 
815933707f3Ssthen /*
816933707f3Ssthen  * hashbig():
817933707f3Ssthen  * This is the same as hashword() on big-endian machines.  It is different
818933707f3Ssthen  * from hashlittle() on all machines.  hashbig() takes advantage of
819933707f3Ssthen  * big-endian byte ordering.
820933707f3Ssthen  */
821933707f3Ssthen uint32_t hashbig( const void *key, size_t length, uint32_t initval)
822933707f3Ssthen {
823933707f3Ssthen   uint32_t a,b,c;
824933707f3Ssthen   union { const void *ptr; size_t i; } u; /* to cast key to (size_t) happily */
825933707f3Ssthen 
826933707f3Ssthen   /* Set up the internal state */
827933707f3Ssthen   a = b = c = raninit + ((uint32_t)length) + initval;
828933707f3Ssthen 
829933707f3Ssthen   u.ptr = key;
830933707f3Ssthen   if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
831933707f3Ssthen     const uint32_t *k = (const uint32_t *)key;         /* read 32-bit chunks */
832933707f3Ssthen #ifdef VALGRIND
833933707f3Ssthen     const uint8_t  *k8;
834933707f3Ssthen #endif
835933707f3Ssthen 
836933707f3Ssthen     /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
837933707f3Ssthen     while (length > 12)
838933707f3Ssthen     {
839933707f3Ssthen       a += k[0];
840933707f3Ssthen       b += k[1];
841933707f3Ssthen       c += k[2];
842933707f3Ssthen       mix(a,b,c);
843933707f3Ssthen       length -= 12;
844933707f3Ssthen       k += 3;
845933707f3Ssthen     }
846933707f3Ssthen 
847933707f3Ssthen     /*----------------------------- handle the last (probably partial) block */
848933707f3Ssthen     /*
849933707f3Ssthen      * "k[2]<<8" actually reads beyond the end of the string, but
850933707f3Ssthen      * then shifts out the part it's not allowed to read.  Because the
851933707f3Ssthen      * string is aligned, the illegal read is in the same word as the
852933707f3Ssthen      * rest of the string.  Every machine with memory protection I've seen
853933707f3Ssthen      * does it on word boundaries, so is OK with this.  But VALGRIND will
854933707f3Ssthen      * still catch it and complain.  The masking trick does make the hash
8554bfc71b0Ssthen      * noticeably faster for short strings (like English words).
856933707f3Ssthen      */
857933707f3Ssthen #ifndef VALGRIND
858933707f3Ssthen 
859933707f3Ssthen     switch(length)
860933707f3Ssthen     {
861933707f3Ssthen     case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
862933707f3Ssthen     case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break;
863933707f3Ssthen     case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break;
864933707f3Ssthen     case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break;
865933707f3Ssthen     case 8 : b+=k[1]; a+=k[0]; break;
866933707f3Ssthen     case 7 : b+=k[1]&0xffffff00; a+=k[0]; break;
867933707f3Ssthen     case 6 : b+=k[1]&0xffff0000; a+=k[0]; break;
868933707f3Ssthen     case 5 : b+=k[1]&0xff000000; a+=k[0]; break;
869933707f3Ssthen     case 4 : a+=k[0]; break;
870933707f3Ssthen     case 3 : a+=k[0]&0xffffff00; break;
871933707f3Ssthen     case 2 : a+=k[0]&0xffff0000; break;
872933707f3Ssthen     case 1 : a+=k[0]&0xff000000; break;
873933707f3Ssthen     case 0 : return c;              /* zero length strings require no mixing */
874933707f3Ssthen     }
875933707f3Ssthen 
876933707f3Ssthen #else  /* make valgrind happy */
877933707f3Ssthen 
878933707f3Ssthen     k8 = (const uint8_t *)k;
879933707f3Ssthen     switch(length)                   /* all the case statements fall through */
880933707f3Ssthen     {
881933707f3Ssthen     case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
882*98bc733bSsthen     case 11: c+=((uint32_t)k8[10])<<8;
883*98bc733bSsthen 	     ATTR_FALLTHROUGH
884*98bc733bSsthen 	     /* fallthrough */
885*98bc733bSsthen     case 10: c+=((uint32_t)k8[9])<<16;
886*98bc733bSsthen 	     ATTR_FALLTHROUGH
887*98bc733bSsthen 	     /* fallthrough */
888*98bc733bSsthen     case 9 : c+=((uint32_t)k8[8])<<24;
889*98bc733bSsthen 	     ATTR_FALLTHROUGH
890*98bc733bSsthen 	     /* fallthrough */
891933707f3Ssthen     case 8 : b+=k[1]; a+=k[0]; break;
892*98bc733bSsthen     case 7 : b+=((uint32_t)k8[6])<<8;
893*98bc733bSsthen 	     ATTR_FALLTHROUGH
894*98bc733bSsthen 	     /* fallthrough */
895*98bc733bSsthen     case 6 : b+=((uint32_t)k8[5])<<16;
896*98bc733bSsthen 	     ATTR_FALLTHROUGH
897*98bc733bSsthen 	     /* fallthrough */
898*98bc733bSsthen     case 5 : b+=((uint32_t)k8[4])<<24;
899*98bc733bSsthen 	     ATTR_FALLTHROUGH
900*98bc733bSsthen 	     /* fallthrough */
901933707f3Ssthen     case 4 : a+=k[0]; break;
902*98bc733bSsthen     case 3 : a+=((uint32_t)k8[2])<<8;
903*98bc733bSsthen 	     ATTR_FALLTHROUGH
904*98bc733bSsthen 	     /* fallthrough */
905*98bc733bSsthen     case 2 : a+=((uint32_t)k8[1])<<16;
906*98bc733bSsthen 	     ATTR_FALLTHROUGH
907*98bc733bSsthen 	     /* fallthrough */
908933707f3Ssthen     case 1 : a+=((uint32_t)k8[0])<<24; break;
909933707f3Ssthen     case 0 : return c;
910933707f3Ssthen     }
911933707f3Ssthen 
912933707f3Ssthen #endif /* !VALGRIND */
913933707f3Ssthen 
914933707f3Ssthen   } else {                        /* need to read the key one byte at a time */
915933707f3Ssthen     const uint8_t *k = (const uint8_t *)key;
916933707f3Ssthen 
917933707f3Ssthen     /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
918933707f3Ssthen     while (length > 12)
919933707f3Ssthen     {
920933707f3Ssthen       a += ((uint32_t)k[0])<<24;
921933707f3Ssthen       a += ((uint32_t)k[1])<<16;
922933707f3Ssthen       a += ((uint32_t)k[2])<<8;
923933707f3Ssthen       a += ((uint32_t)k[3]);
924933707f3Ssthen       b += ((uint32_t)k[4])<<24;
925933707f3Ssthen       b += ((uint32_t)k[5])<<16;
926933707f3Ssthen       b += ((uint32_t)k[6])<<8;
927933707f3Ssthen       b += ((uint32_t)k[7]);
928933707f3Ssthen       c += ((uint32_t)k[8])<<24;
929933707f3Ssthen       c += ((uint32_t)k[9])<<16;
930933707f3Ssthen       c += ((uint32_t)k[10])<<8;
931933707f3Ssthen       c += ((uint32_t)k[11]);
932933707f3Ssthen       mix(a,b,c);
933933707f3Ssthen       length -= 12;
934933707f3Ssthen       k += 12;
935933707f3Ssthen     }
936933707f3Ssthen 
937933707f3Ssthen     /*-------------------------------- last block: affect all 32 bits of (c) */
938933707f3Ssthen     switch(length)                   /* all the case statements fall through */
939933707f3Ssthen     {
940933707f3Ssthen     case 12: c+=k[11];
941*98bc733bSsthen 	     ATTR_FALLTHROUGH
942*98bc733bSsthen 	     /* fallthrough */
943933707f3Ssthen     case 11: c+=((uint32_t)k[10])<<8;
944*98bc733bSsthen 	     ATTR_FALLTHROUGH
945*98bc733bSsthen 	     /* fallthrough */
946933707f3Ssthen     case 10: c+=((uint32_t)k[9])<<16;
947*98bc733bSsthen 	     ATTR_FALLTHROUGH
948*98bc733bSsthen 	     /* fallthrough */
949933707f3Ssthen     case 9 : c+=((uint32_t)k[8])<<24;
950*98bc733bSsthen 	     ATTR_FALLTHROUGH
951*98bc733bSsthen 	     /* fallthrough */
952933707f3Ssthen     case 8 : b+=k[7];
953*98bc733bSsthen 	     ATTR_FALLTHROUGH
954*98bc733bSsthen 	     /* fallthrough */
955933707f3Ssthen     case 7 : b+=((uint32_t)k[6])<<8;
956*98bc733bSsthen 	     ATTR_FALLTHROUGH
957*98bc733bSsthen 	     /* fallthrough */
958933707f3Ssthen     case 6 : b+=((uint32_t)k[5])<<16;
959*98bc733bSsthen 	     ATTR_FALLTHROUGH
960*98bc733bSsthen 	     /* fallthrough */
961933707f3Ssthen     case 5 : b+=((uint32_t)k[4])<<24;
962*98bc733bSsthen 	     ATTR_FALLTHROUGH
963*98bc733bSsthen 	     /* fallthrough */
964933707f3Ssthen     case 4 : a+=k[3];
965*98bc733bSsthen 	     ATTR_FALLTHROUGH
966*98bc733bSsthen 	     /* fallthrough */
967933707f3Ssthen     case 3 : a+=((uint32_t)k[2])<<8;
968*98bc733bSsthen 	     ATTR_FALLTHROUGH
969*98bc733bSsthen 	     /* fallthrough */
970933707f3Ssthen     case 2 : a+=((uint32_t)k[1])<<16;
971*98bc733bSsthen 	     ATTR_FALLTHROUGH
972*98bc733bSsthen 	     /* fallthrough */
973933707f3Ssthen     case 1 : a+=((uint32_t)k[0])<<24;
974933707f3Ssthen              break;
975933707f3Ssthen     case 0 : return c;
976933707f3Ssthen     }
977933707f3Ssthen   }
978933707f3Ssthen 
979933707f3Ssthen   final(a,b,c);
980933707f3Ssthen   return c;
981933707f3Ssthen }
982933707f3Ssthen 
983933707f3Ssthen #endif /* 0 == currently not used */
984933707f3Ssthen 
985933707f3Ssthen #ifdef SELF_TEST
986933707f3Ssthen 
987933707f3Ssthen /* used for timings */
98877079be7Ssthen void driver1(void)
989933707f3Ssthen {
990933707f3Ssthen   uint8_t buf[256];
991933707f3Ssthen   uint32_t i;
992933707f3Ssthen   uint32_t h=0;
993933707f3Ssthen   time_t a,z;
994933707f3Ssthen 
995933707f3Ssthen   time(&a);
996933707f3Ssthen   for (i=0; i<256; ++i) buf[i] = 'x';
997933707f3Ssthen   for (i=0; i<1; ++i)
998933707f3Ssthen   {
999933707f3Ssthen     h = hashlittle(&buf[0],1,h);
1000933707f3Ssthen   }
1001933707f3Ssthen   time(&z);
1002933707f3Ssthen   if (z-a > 0) printf("time %d %.8x\n", z-a, h);
1003933707f3Ssthen }
1004933707f3Ssthen 
1005933707f3Ssthen /* check that every input bit changes every output bit half the time */
1006933707f3Ssthen #define HASHSTATE 1
1007933707f3Ssthen #define HASHLEN   1
1008933707f3Ssthen #define MAXPAIR 60
1009933707f3Ssthen #define MAXLEN  70
101077079be7Ssthen void driver2(void)
1011933707f3Ssthen {
1012933707f3Ssthen   uint8_t qa[MAXLEN+1], qb[MAXLEN+2], *a = &qa[0], *b = &qb[1];
1013933707f3Ssthen   uint32_t c[HASHSTATE], d[HASHSTATE], i=0, j=0, k, l, m=0, z;
1014933707f3Ssthen   uint32_t e[HASHSTATE],f[HASHSTATE],g[HASHSTATE],h[HASHSTATE];
1015933707f3Ssthen   uint32_t x[HASHSTATE],y[HASHSTATE];
1016933707f3Ssthen   uint32_t hlen;
1017933707f3Ssthen 
1018933707f3Ssthen   printf("No more than %d trials should ever be needed \n",MAXPAIR/2);
1019933707f3Ssthen   for (hlen=0; hlen < MAXLEN; ++hlen)
1020933707f3Ssthen   {
1021933707f3Ssthen     z=0;
1022933707f3Ssthen     for (i=0; i<hlen; ++i)  /*----------------------- for each input byte, */
1023933707f3Ssthen     {
1024933707f3Ssthen       for (j=0; j<8; ++j)   /*------------------------ for each input bit, */
1025933707f3Ssthen       {
10264bfc71b0Ssthen 	for (m=1; m<8; ++m) /*------------ for several possible initvals, */
1027933707f3Ssthen 	{
1028933707f3Ssthen 	  for (l=0; l<HASHSTATE; ++l)
1029933707f3Ssthen 	    e[l]=f[l]=g[l]=h[l]=x[l]=y[l]=~((uint32_t)0);
1030933707f3Ssthen 
1031933707f3Ssthen       	  /*---- check that every output bit is affected by that input bit */
1032933707f3Ssthen 	  for (k=0; k<MAXPAIR; k+=2)
1033933707f3Ssthen 	  {
1034933707f3Ssthen 	    uint32_t finished=1;
1035933707f3Ssthen 	    /* keys have one bit different */
1036933707f3Ssthen 	    for (l=0; l<hlen+1; ++l) {a[l] = b[l] = (uint8_t)0;}
1037933707f3Ssthen 	    /* have a and b be two keys differing in only one bit */
1038933707f3Ssthen 	    a[i] ^= (k<<j);
1039933707f3Ssthen 	    a[i] ^= (k>>(8-j));
1040933707f3Ssthen 	     c[0] = hashlittle(a, hlen, m);
1041933707f3Ssthen 	    b[i] ^= ((k+1)<<j);
1042933707f3Ssthen 	    b[i] ^= ((k+1)>>(8-j));
1043933707f3Ssthen 	     d[0] = hashlittle(b, hlen, m);
1044933707f3Ssthen 	    /* check every bit is 1, 0, set, and not set at least once */
1045933707f3Ssthen 	    for (l=0; l<HASHSTATE; ++l)
1046933707f3Ssthen 	    {
1047933707f3Ssthen 	      e[l] &= (c[l]^d[l]);
1048933707f3Ssthen 	      f[l] &= ~(c[l]^d[l]);
1049933707f3Ssthen 	      g[l] &= c[l];
1050933707f3Ssthen 	      h[l] &= ~c[l];
1051933707f3Ssthen 	      x[l] &= d[l];
1052933707f3Ssthen 	      y[l] &= ~d[l];
1053933707f3Ssthen 	      if (e[l]|f[l]|g[l]|h[l]|x[l]|y[l]) finished=0;
1054933707f3Ssthen 	    }
1055933707f3Ssthen 	    if (finished) break;
1056933707f3Ssthen 	  }
1057933707f3Ssthen 	  if (k>z) z=k;
1058933707f3Ssthen 	  if (k==MAXPAIR)
1059933707f3Ssthen 	  {
1060933707f3Ssthen 	     printf("Some bit didn't change: ");
1061933707f3Ssthen 	     printf("%.8x %.8x %.8x %.8x %.8x %.8x  ",
1062933707f3Ssthen 	            e[0],f[0],g[0],h[0],x[0],y[0]);
1063933707f3Ssthen 	     printf("i %d j %d m %d len %d\n", i, j, m, hlen);
1064933707f3Ssthen 	  }
1065933707f3Ssthen 	  if (z==MAXPAIR) goto done;
1066933707f3Ssthen 	}
1067933707f3Ssthen       }
1068933707f3Ssthen     }
1069933707f3Ssthen    done:
1070933707f3Ssthen     if (z < MAXPAIR)
1071933707f3Ssthen     {
1072933707f3Ssthen       printf("Mix success  %2d bytes  %2d initvals  ",i,m);
1073933707f3Ssthen       printf("required  %d  trials\n", z/2);
1074933707f3Ssthen     }
1075933707f3Ssthen   }
1076933707f3Ssthen   printf("\n");
1077933707f3Ssthen }
1078933707f3Ssthen 
1079933707f3Ssthen /* Check for reading beyond the end of the buffer and alignment problems */
108077079be7Ssthen void driver3(void)
1081933707f3Ssthen {
1082933707f3Ssthen   uint8_t buf[MAXLEN+20], *b;
1083933707f3Ssthen   uint32_t len;
1084933707f3Ssthen   uint8_t q[] = "This is the time for all good men to come to the aid of their country...";
1085933707f3Ssthen   uint32_t h;
1086933707f3Ssthen   uint8_t qq[] = "xThis is the time for all good men to come to the aid of their country...";
1087933707f3Ssthen   uint32_t i;
1088933707f3Ssthen   uint8_t qqq[] = "xxThis is the time for all good men to come to the aid of their country...";
1089933707f3Ssthen   uint32_t j;
1090933707f3Ssthen   uint8_t qqqq[] = "xxxThis is the time for all good men to come to the aid of their country...";
1091933707f3Ssthen   uint32_t ref,x,y;
1092933707f3Ssthen   uint8_t *p;
1093933707f3Ssthen 
1094933707f3Ssthen   printf("Endianness.  These lines should all be the same (for values filled in):\n");
1095933707f3Ssthen   printf("%.8x                            %.8x                            %.8x\n",
1096933707f3Ssthen          hashword((const uint32_t *)q, (sizeof(q)-1)/4, 13),
1097933707f3Ssthen          hashword((const uint32_t *)q, (sizeof(q)-5)/4, 13),
1098933707f3Ssthen          hashword((const uint32_t *)q, (sizeof(q)-9)/4, 13));
1099933707f3Ssthen   p = q;
1100933707f3Ssthen   printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
1101933707f3Ssthen          hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
1102933707f3Ssthen          hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
1103933707f3Ssthen          hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
1104933707f3Ssthen          hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
1105933707f3Ssthen          hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
1106933707f3Ssthen          hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
1107933707f3Ssthen   p = &qq[1];
1108933707f3Ssthen   printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
1109933707f3Ssthen          hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
1110933707f3Ssthen          hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
1111933707f3Ssthen          hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
1112933707f3Ssthen          hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
1113933707f3Ssthen          hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
1114933707f3Ssthen          hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
1115933707f3Ssthen   p = &qqq[2];
1116933707f3Ssthen   printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
1117933707f3Ssthen          hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
1118933707f3Ssthen          hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
1119933707f3Ssthen          hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
1120933707f3Ssthen          hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
1121933707f3Ssthen          hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
1122933707f3Ssthen          hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
1123933707f3Ssthen   p = &qqqq[3];
1124933707f3Ssthen   printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
1125933707f3Ssthen          hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
1126933707f3Ssthen          hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
1127933707f3Ssthen          hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
1128933707f3Ssthen          hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
1129933707f3Ssthen          hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
1130933707f3Ssthen          hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
1131933707f3Ssthen   printf("\n");
1132933707f3Ssthen 
1133933707f3Ssthen   /* check that hashlittle2 and hashlittle produce the same results */
1134933707f3Ssthen   i=47; j=0;
1135933707f3Ssthen   hashlittle2(q, sizeof(q), &i, &j);
1136933707f3Ssthen   if (hashlittle(q, sizeof(q), 47) != i)
1137933707f3Ssthen     printf("hashlittle2 and hashlittle mismatch\n");
1138933707f3Ssthen 
1139933707f3Ssthen   /* check that hashword2 and hashword produce the same results */
1140933707f3Ssthen   len = raninit;
1141933707f3Ssthen   i=47, j=0;
1142933707f3Ssthen   hashword2(&len, 1, &i, &j);
1143933707f3Ssthen   if (hashword(&len, 1, 47) != i)
1144933707f3Ssthen     printf("hashword2 and hashword mismatch %x %x\n",
1145933707f3Ssthen 	   i, hashword(&len, 1, 47));
1146933707f3Ssthen 
1147933707f3Ssthen   /* check hashlittle doesn't read before or after the ends of the string */
1148933707f3Ssthen   for (h=0, b=buf+1; h<8; ++h, ++b)
1149933707f3Ssthen   {
1150933707f3Ssthen     for (i=0; i<MAXLEN; ++i)
1151933707f3Ssthen     {
1152933707f3Ssthen       len = i;
1153933707f3Ssthen       for (j=0; j<i; ++j) *(b+j)=0;
1154933707f3Ssthen 
1155933707f3Ssthen       /* these should all be equal */
1156933707f3Ssthen       ref = hashlittle(b, len, (uint32_t)1);
1157933707f3Ssthen       *(b+i)=(uint8_t)~0;
1158933707f3Ssthen       *(b-1)=(uint8_t)~0;
1159933707f3Ssthen       x = hashlittle(b, len, (uint32_t)1);
1160933707f3Ssthen       y = hashlittle(b, len, (uint32_t)1);
1161933707f3Ssthen       if ((ref != x) || (ref != y))
1162933707f3Ssthen       {
1163933707f3Ssthen 	printf("alignment error: %.8x %.8x %.8x %d %d\n",ref,x,y,
1164933707f3Ssthen                h, i);
1165933707f3Ssthen       }
1166933707f3Ssthen     }
1167933707f3Ssthen   }
1168933707f3Ssthen }
1169933707f3Ssthen 
1170933707f3Ssthen /* check for problems with nulls */
117177079be7Ssthen  void driver4(void)
1172933707f3Ssthen {
1173933707f3Ssthen   uint8_t buf[1];
1174933707f3Ssthen   uint32_t h,i,state[HASHSTATE];
1175933707f3Ssthen 
1176933707f3Ssthen 
1177933707f3Ssthen   buf[0] = ~0;
1178933707f3Ssthen   for (i=0; i<HASHSTATE; ++i) state[i] = 1;
1179933707f3Ssthen   printf("These should all be different\n");
1180933707f3Ssthen   for (i=0, h=0; i<8; ++i)
1181933707f3Ssthen   {
1182933707f3Ssthen     h = hashlittle(buf, 0, h);
1183933707f3Ssthen     printf("%2ld  0-byte strings, hash is  %.8x\n", i, h);
1184933707f3Ssthen   }
1185933707f3Ssthen }
1186933707f3Ssthen 
1187933707f3Ssthen 
118877079be7Ssthen int main(void)
1189933707f3Ssthen {
1190933707f3Ssthen   driver1();   /* test that the key is hashed: used for timings */
1191933707f3Ssthen   driver2();   /* test that whole key is hashed thoroughly */
1192933707f3Ssthen   driver3();   /* test that nothing but the key is hashed */
1193933707f3Ssthen   driver4();   /* test hashing multiple buffers (all buffers are null) */
1194933707f3Ssthen   return 1;
1195933707f3Ssthen }
1196933707f3Ssthen 
1197933707f3Ssthen #endif  /* SELF_TEST */
1198