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