1 /* -------------------------------------------------------------------- */
3 * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
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.
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.
18 * If you want to find a hash of, say, exactly 7 integers, do
19 * a = i1; b = i2; c = i3;
21 * a += i4; b += i5; c += i6;
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().
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.
35 /* -------------------------------------------------------------------- */
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
46 static const union _dbswap
{
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)
54 # define ROTL32(x, s) (((x) << (s)) | ((x) >> (32 - (s))))
57 /* NOTE: The _size parameter should be in bytes. */
58 #define _JLU3_INIT(_h, _size) (0xdeadbeef + ((uint32_t)(_size)) + (_h))
60 /* -------------------------------------------------------------------- */
62 * _JLU3_MIX -- mix 3 32-bit values reversibly.
64 * This is reversible, so any information in (a,b,c) before _JLU3_MIX() is
65 * still in (a,b,c) after _JLU3_MIX().
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
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
78 * * the base values were pseudorandom, all zero but one bit set, or
79 * all zero plus a counter that starts at zero.
81 * Some k values for my "a-=c; a^=ROTL32(c,k); c+=b;" arrangement that
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.
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
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
103 /* -------------------------------------------------------------------- */
104 #define _JLU3_MIX(a,b,c) \
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; \
114 /* -------------------------------------------------------------------- */
116 * _JLU3_FINAL -- final mixing of 3 32-bit values (a,b,c) into c
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
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
127 * * the base values were pseudorandom, all zero but one bit set, or
128 * all zero plus a counter that starts at zero.
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:
138 /* -------------------------------------------------------------------- */
139 #define _JLU3_FINAL(a,b,c) \
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); \
150 #if defined(_JLU3_jlu32w)
151 uint32_t jlu32w(uint32_t h
, const uint32_t *k
, size_t size
);
152 /* -------------------------------------------------------------------- */
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
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.
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
169 /* -------------------------------------------------------------------- */
170 uint32_t jlu32w(uint32_t h
, const uint32_t *k
, size_t size
)
172 uint32_t a
= _JLU3_INIT(h
, (size
* sizeof(*k
)));
179 /*----------------------------------------------- handle most of the key */
189 /*----------------------------------------- handle the last 3 uint32_t's */
199 /*---------------------------------------------------- report the result */
203 #endif /* defined(_JLU3_jlu32w) */
205 #if defined(_JLU3_jlu32l)
206 uint32_t jlu32l(uint32_t h
, const void *key
, size_t size
);
207 /* -------------------------------------------------------------------- */
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.
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.
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]);
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.
229 * Use for hash table lookup, or anything where one collision in 2^^32 is
230 * acceptable. Do NOT use for cryptographic purposes.
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
237 /* -------------------------------------------------------------------- */
238 uint32_t jlu32l(uint32_t h
, const void *key
, size_t size
)
240 union { const void *ptr
; size_t i
; } u
;
241 uint32_t a
= _JLU3_INIT(h
, size
);
249 if (HASH_LITTLE_ENDIAN
&& ((u
.i
& 0x3) == 0)) {
250 const uint32_t *k
= (const uint32_t *)key
; /* read 32-bit chunks */
255 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
265 /*------------------------- handle the last (probably partial) block */
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).
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;
293 #else /* make valgrind happy */
295 k8
= (const uint8_t *)k
;
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;
312 #endif /* !valgrind */
314 } else if (HASH_LITTLE_ENDIAN
&& ((u
.i
& 0x1) == 0)) {
315 const uint16_t *k
= (const uint16_t *)key
; /* read 16-bit chunks */
318 /*----------- all but last block: aligned reads and different mixing */
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);
328 /*------------------------- handle the last (probably partial) block */
329 k8
= (const uint8_t *)k
;
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);
337 c
+= ((uint32_t)k8
[10])<<16;
341 b
+= k
[2]+(((uint32_t)k
[3])<<16);
342 a
+= k
[0]+(((uint32_t)k
[1])<<16);
345 c
+= (uint32_t)k8
[8];
348 b
+= k
[2]+(((uint32_t)k
[3])<<16);
349 a
+= k
[0]+(((uint32_t)k
[1])<<16);
352 b
+= ((uint32_t)k8
[6])<<16;
356 a
+= k
[0]+(((uint32_t)k
[1])<<16);
359 b
+= (uint32_t)k8
[4];
362 a
+= k
[0]+(((uint32_t)k
[1])<<16);
365 a
+= ((uint32_t)k8
[2])<<16;
371 a
+= (uint32_t)k8
[0];
377 } else { /* need to read the key one byte at a time */
378 const uint8_t *k
= (const uint8_t *)key
;
380 /*----------- all but the last block: affect some 32 bits of (a,b,c) */
383 a
+= ((uint32_t)k
[1])<<8;
384 a
+= ((uint32_t)k
[2])<<16;
385 a
+= ((uint32_t)k
[3])<<24;
387 b
+= ((uint32_t)k
[5])<<8;
388 b
+= ((uint32_t)k
[6])<<16;
389 b
+= ((uint32_t)k
[7])<<24;
391 c
+= ((uint32_t)k
[9])<<8;
392 c
+= ((uint32_t)k
[10])<<16;
393 c
+= ((uint32_t)k
[11])<<24;
399 /*---------------------------- last block: affect all 32 bits of (c) */
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];
424 #endif /* defined(_JLU3_jlu32l) */
426 #if defined(_JLU3_jlu32lpair)
428 * jlu32lpair: return 2 32-bit hash values.
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)".
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
443 void jlu32lpair(const void *key
, size_t size
, uint32_t *pc
, uint32_t *pb
)
445 union { const void *ptr
; size_t i
; } u
;
446 uint32_t a
= _JLU3_INIT(*pc
, size
);
453 c
+= *pb
; /* Add the secondary hash. */
456 if (HASH_LITTLE_ENDIAN
&& ((u
.i
& 0x3) == 0)) {
457 const uint32_t *k
= (const uint32_t *)key
; /* read 32-bit chunks */
462 /*-- all but last block: aligned reads and affect 32 bits of (a,b,c) */
463 while (size
> (size_t)12) {
471 /*------------------------- handle the last (probably partial) block */
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).
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;
499 #else /* make valgrind happy */
501 k8
= (const uint8_t *)k
;
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;
518 #endif /* !valgrind */
520 } else if (HASH_LITTLE_ENDIAN
&& ((u
.i
& 0x1) == 0)) {
521 const uint16_t *k
= (const uint16_t *)key
; /* read 16-bit chunks */
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);
534 /*------------------------- handle the last (probably partial) block */
535 k8
= (const uint8_t *)k
;
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);
543 c
+= ((uint32_t)k8
[10])<<16;
547 b
+= k
[2]+(((uint32_t)k
[3])<<16);
548 a
+= k
[0]+(((uint32_t)k
[1])<<16);
554 b
+= k
[2]+(((uint32_t)k
[3])<<16);
555 a
+= k
[0]+(((uint32_t)k
[1])<<16);
558 b
+= ((uint32_t)k8
[6])<<16;
562 a
+= k
[0]+(((uint32_t)k
[1])<<16);
568 a
+= k
[0]+(((uint32_t)k
[1])<<16);
571 a
+= ((uint32_t)k8
[2])<<16;
583 } else { /* need to read the key one byte at a time */
584 const uint8_t *k
= (const uint8_t *)key
;
586 /*----------- all but the last block: affect some 32 bits of (a,b,c) */
587 while (size
> (size_t)12) {
589 a
+= ((uint32_t)k
[1])<<8;
590 a
+= ((uint32_t)k
[2])<<16;
591 a
+= ((uint32_t)k
[3])<<24;
593 b
+= ((uint32_t)k
[5])<<8;
594 b
+= ((uint32_t)k
[6])<<16;
595 b
+= ((uint32_t)k
[7])<<24;
597 c
+= ((uint32_t)k
[9])<<8;
598 c
+= ((uint32_t)k
[10])<<16;
599 c
+= ((uint32_t)k
[11])<<24;
605 /*---------------------------- last block: affect all 32 bits of (c) */
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 */
632 #endif /* defined(_JLU3_jlu32lpair) */
634 #if defined(_JLU3_jlu32b)
635 uint32_t jlu32b(uint32_t h
, const void *key
, size_t size
);
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.
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
647 uint32_t jlu32b(uint32_t h
, const void *key
, size_t size
)
649 union { const void *ptr
; size_t i
; } u
;
650 uint32_t a
= _JLU3_INIT(h
, size
);
658 if (HASH_BIG_ENDIAN
&& ((u
.i
& 0x3) == 0)) {
659 const uint32_t *k
= (const uint32_t *)key
; /* read 32-bit chunks */
664 /*-- all but last block: aligned reads and affect 32 bits of (a,b,c) */
674 /*------------------------- handle the last (probably partial) block */
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).
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;
702 #else /* make valgrind happy */
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;
721 #endif /* !VALGRIND */
723 } else { /* need to read the key one byte at a time */
724 const uint8_t *k
= (const uint8_t *)key
;
726 /*----------- all but the last block: affect some 32 bits of (a,b,c) */
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]);
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 */
770 #endif /* defined(_JLU3_jlu32b) */
772 #if defined(_JLU3_SELFTEST)
774 /* used for timings */
775 static void driver1(void)
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]));
788 if (z
-a
> 0) printf("time %d %.8x\n", (int)(z
-a
), h
);
791 /* check that every input bit changes every output bit half the time */
796 static void driver2(void)
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
];
804 printf("No more than %d trials should ever be needed \n",MAXPAIR
/2);
805 for (hlen
=0; hlen
< MAXLEN
; ++hlen
) {
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);
813 /* check that every output bit is affected by that input bit */
814 for (k
=0; k
<MAXPAIR
; k
+=2) {
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 */
821 c
[0] = jlu32l(m
, a
, hlen
);
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
) {
828 f
[l
] &= ~(c
[l
]^d
[l
]);
833 if (e
[l
]|f
[l
]|g
[l
]|h
[l
]|x
[l
]|y
[l
]) finished
=0;
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
);
844 if (z
== MAXPAIR
) goto done
;
850 printf("Mix success %2u bytes %2u initvals ",i
,m
);
851 printf("required %u trials\n", z
/2);
857 /* Check for reading beyond the end of the buffer and alignment problems */
858 static void driver3(void)
860 uint8_t buf
[MAXLEN
+20], *b
;
862 uint8_t q
[] = "This is the time for all good men to come to the aid of their country...";
864 uint8_t qq
[] = "xThis is the time for all good men to come to the aid of their country...";
866 uint8_t qqq
[] = "xxThis is the time for all good men to come to the aid of their country...";
868 uint8_t qqqq
[] = "xxxThis is the time for all good men to come to the aid of their country...";
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));
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));
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));
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));
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));
911 for (h
=0, b
=buf
+1; h
<8; ++h
, ++b
) {
912 for (i
=0; i
<MAXLEN
; ++i
) {
917 /* these should all be equal */
919 ref
= jlu32l(m
, b
, len
);
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
);
930 /* check for problems with nulls */
931 static void driver4(void)
936 uint32_t state
[HASHSTATE
];
939 for (i
=0; i
<HASHSTATE
; ++i
)
941 printf("These should all be different\n");
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
);
950 int main(int argc
, char ** argv
)
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) */
959 #endif /* _JLU3_SELFTEST */