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
48
static const union _dbswap {
50
const unsigned char uc[4];
51
} endian = { .ui = 0x11223344 };
52
# define HASH_LITTLE_ENDIAN (endian.uc[0] == (unsigned char) 0x44)
53
# define HASH_BIG_ENDIAN (endian.uc[0] == (unsigned char) 0x11)
57
# define ROTL32(x, s) (((x) << (s)) | ((x) >> (32 - (s))))
60
/* NOTE: The _size parameter should be in bytes. */
61
#define _JLU3_INIT(_h, _size) (0xdeadbeef + ((uint32_t)(_size)) + (_h))
63
/* -------------------------------------------------------------------- */
65
* _JLU3_MIX -- mix 3 32-bit values reversibly.
67
* This is reversible, so any information in (a,b,c) before _JLU3_MIX() is
68
* still in (a,b,c) after _JLU3_MIX().
70
* If four pairs of (a,b,c) inputs are run through _JLU3_MIX(), or through
71
* _JLU3_MIX() in reverse, there are at least 32 bits of the output that
72
* are sometimes the same for one pair and different for another pair.
73
* This was tested for:
74
* * pairs that differed by one bit, by two bits, in any combination
75
* of top bits of (a,b,c), or in any combination of bottom bits of
77
* * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
78
* the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
79
* is commonly produced by subtraction) look like a single 1-bit
81
* * the base values were pseudorandom, all zero but one bit set, or
82
* all zero plus a counter that starts at zero.
84
* Some k values for my "a-=c; a^=ROTL32(c,k); c+=b;" arrangement that
89
* Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
90
* for "differ" defined as + with a one-bit base and a two-bit delta. I
91
* used http://burtleburtle.net/bob/hash/avalanche.html to choose
92
* the operations, constants, and arrangements of the variables.
94
* This does not achieve avalanche. There are input bits of (a,b,c)
95
* that fail to affect some output bits of (a,b,c), especially of a. The
96
* most thoroughly mixed value is c, but it doesn't really even achieve
99
* This allows some parallelism. Read-after-writes are good at doubling
100
* the number of bits affected, so the goal of mixing pulls in the opposite
101
* direction as the goal of parallelism. I did what I could. Rotates
102
* seem to cost as much as shifts on every machine I could lay my hands
103
* on, and rotates are much kinder to the top and bottom bits, so I used
106
/* -------------------------------------------------------------------- */
107
#define _JLU3_MIX(a,b,c) \
109
a -= c; a ^= ROTL32(c, 4); c += b; \
110
b -= a; b ^= ROTL32(a, 6); a += c; \
111
c -= b; c ^= ROTL32(b, 8); b += a; \
112
a -= c; a ^= ROTL32(c,16); c += b; \
113
b -= a; b ^= ROTL32(a,19); a += c; \
114
c -= b; c ^= ROTL32(b, 4); b += a; \
117
/* -------------------------------------------------------------------- */
119
* _JLU3_FINAL -- final mixing of 3 32-bit values (a,b,c) into c
121
* Pairs of (a,b,c) values differing in only a few bits will usually
122
* produce values of c that look totally different. This was tested for
123
* * pairs that differed by one bit, by two bits, in any combination
124
* of top bits of (a,b,c), or in any combination of bottom bits of
126
* * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
127
* the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
128
* is commonly produced by subtraction) look like a single 1-bit
130
* * the base values were pseudorandom, all zero but one bit set, or
131
* all zero plus a counter that starts at zero.
133
* These constants passed:
134
* 14 11 25 16 4 14 24
135
* 12 14 25 16 4 14 24
136
* and these came close:
141
/* -------------------------------------------------------------------- */
142
#define _JLU3_FINAL(a,b,c) \
144
c ^= b; c -= ROTL32(b,14); \
145
a ^= c; a -= ROTL32(c,11); \
146
b ^= a; b -= ROTL32(a,25); \
147
c ^= b; c -= ROTL32(b,16); \
148
a ^= c; a -= ROTL32(c,4); \
149
b ^= a; b -= ROTL32(a,14); \
150
c ^= b; c -= ROTL32(b,24); \
153
#if defined(_JLU3_jlu32w)
154
uint32_t jlu32w(uint32_t h, /*@null@*/ const uint32_t *k, size_t size)
156
/* -------------------------------------------------------------------- */
158
* This works on all machines. To be useful, it requires
159
* -- that the key be an array of uint32_t's, and
160
* -- that the size be the number of uint32_t's in the key
162
* The function jlu32w() is identical to jlu32l() on little-endian
163
* machines, and identical to jlu32b() on big-endian machines,
164
* except that the size has to be measured in uint32_ts rather than in
165
* bytes. jlu32l() is more complicated than jlu32w() only because
166
* jlu32l() has to dance around fitting the key bytes into registers.
168
* @param h the previous hash, or an arbitrary value
169
* @param *k the key, an array of uint32_t values
170
* @param size the size of the key, in uint32_ts
171
* @return the lookup3 hash
173
/* -------------------------------------------------------------------- */
174
uint32_t jlu32w(uint32_t h, const uint32_t *k, size_t size)
176
uint32_t a = _JLU3_INIT(h, (size * sizeof(*k)));
183
/*----------------------------------------------- handle most of the key */
193
/*----------------------------------------- handle the last 3 uint32_t's */
203
/*---------------------------------------------------- report the result */
207
#endif /* defined(_JLU3_jlu32w) */
209
#if defined(_JLU3_jlu32l)
210
uint32_t jlu32l(uint32_t h, const void *key, size_t size)
212
/* -------------------------------------------------------------------- */
214
* jlu32l() -- hash a variable-length key into a 32-bit value
215
* h : can be any 4-byte value
216
* k : the key (the unaligned variable-length array of bytes)
217
* size : the size of the key, counting by bytes
218
* Returns a 32-bit value. Every bit of the key affects every bit of
219
* the return value. Two keys differing by one or two bits will have
220
* totally different hash values.
222
* The best hash table sizes are powers of 2. There is no need to do
223
* mod a prime (mod is sooo slow!). If you need less than 32 bits,
224
* use a bitmask. For example, if you need only 10 bits, do
225
* h = (h & hashmask(10));
226
* In which case, the hash table should have hashsize(10) elements.
228
* If you are hashing n strings (uint8_t **)k, do it like this:
229
* for (i=0, h=0; i<n; ++i) h = jlu32l(h, k[i], len[i]);
231
* By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
232
* code any way you wish, private, educational, or commercial. It's free.
234
* Use for hash table lookup, or anything where one collision in 2^^32 is
235
* acceptable. Do NOT use for cryptographic purposes.
237
* @param h the previous hash, or an arbitrary value
238
* @param *k the key, an array of uint8_t values
239
* @param size the size of the key
240
* @return the lookup3 hash
242
/* -------------------------------------------------------------------- */
243
uint32_t jlu32l(uint32_t h, const void *key, size_t size)
245
union { const void *ptr; size_t i; } u;
246
uint32_t a = _JLU3_INIT(h, size);
254
if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
255
const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
260
/*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
270
/*------------------------- handle the last (probably partial) block */
272
* "k[2]&0xffffff" actually reads beyond the end of the string, but
273
* then masks off the part it's not allowed to read. Because the
274
* string is aligned, the masked-off tail is in the same word as the
275
* rest of the string. Every machine with memory protection I've seen
276
* does it on word boundaries, so is OK with this. But VALGRIND will
277
* still catch it and complain. The masking trick does make the hash
278
* noticably faster for short strings (like English words).
283
case 12: c += k[2]; b+=k[1]; a+=k[0]; break;
284
case 11: c += k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
285
case 10: c += k[2]&0xffff; b+=k[1]; a+=k[0]; break;
286
case 9: c += k[2]&0xff; b+=k[1]; a+=k[0]; break;
287
case 8: b += k[1]; a+=k[0]; break;
288
case 7: b += k[1]&0xffffff; a+=k[0]; break;
289
case 6: b += k[1]&0xffff; a+=k[0]; break;
290
case 5: b += k[1]&0xff; a+=k[0]; break;
291
case 4: a += k[0]; break;
292
case 3: a += k[0]&0xffffff; break;
293
case 2: a += k[0]&0xffff; break;
294
case 1: a += k[0]&0xff; break;
298
#else /* make valgrind happy */
300
k8 = (const uint8_t *)k;
302
case 12: c += k[2]; b+=k[1]; a+=k[0] break;
303
case 11: c += ((uint32_t)k8[10])<<16; /*@fallthrough@*/
304
case 10: c += ((uint32_t)k8[9])<<8; /*@fallthrough@*/
305
case 9: c += k8[8]; /*@fallthrough@*/
306
case 8: b += k[1]; a+=k[0]; break;
307
case 7: b += ((uint32_t)k8[6])<<16; /*@fallthrough@*/
308
case 6: b += ((uint32_t)k8[5])<<8; /*@fallthrough@*/
309
case 5: b += k8[4]; /*@fallthrough@*/
310
case 4: a += k[0]; break;
311
case 3: a += ((uint32_t)k8[2])<<16; /*@fallthrough@*/
312
case 2: a += ((uint32_t)k8[1])<<8; /*@fallthrough@*/
313
case 1: a += k8[0]; break;
317
#endif /* !valgrind */
319
} else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
320
const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
323
/*----------- all but last block: aligned reads and different mixing */
325
a += k[0] + (((uint32_t)k[1])<<16);
326
b += k[2] + (((uint32_t)k[3])<<16);
327
c += k[4] + (((uint32_t)k[5])<<16);
333
/*------------------------- handle the last (probably partial) block */
334
k8 = (const uint8_t *)k;
337
c += k[4]+(((uint32_t)k[5])<<16);
338
b += k[2]+(((uint32_t)k[3])<<16);
339
a += k[0]+(((uint32_t)k[1])<<16);
342
c += ((uint32_t)k8[10])<<16;
346
b += k[2]+(((uint32_t)k[3])<<16);
347
a += k[0]+(((uint32_t)k[1])<<16);
350
c += (uint32_t)k8[8];
353
b += k[2]+(((uint32_t)k[3])<<16);
354
a += k[0]+(((uint32_t)k[1])<<16);
357
b += ((uint32_t)k8[6])<<16;
361
a += k[0]+(((uint32_t)k[1])<<16);
364
b += (uint32_t)k8[4];
367
a += k[0]+(((uint32_t)k[1])<<16);
370
a += ((uint32_t)k8[2])<<16;
376
a += (uint32_t)k8[0];
382
} else { /* need to read the key one byte at a time */
383
const uint8_t *k = (const uint8_t *)key;
385
/*----------- all but the last block: affect some 32 bits of (a,b,c) */
388
a += ((uint32_t)k[1])<<8;
389
a += ((uint32_t)k[2])<<16;
390
a += ((uint32_t)k[3])<<24;
392
b += ((uint32_t)k[5])<<8;
393
b += ((uint32_t)k[6])<<16;
394
b += ((uint32_t)k[7])<<24;
396
c += ((uint32_t)k[9])<<8;
397
c += ((uint32_t)k[10])<<16;
398
c += ((uint32_t)k[11])<<24;
404
/*---------------------------- last block: affect all 32 bits of (c) */
406
case 12: c += ((uint32_t)k[11])<<24; /*@fallthrough@*/
407
case 11: c += ((uint32_t)k[10])<<16; /*@fallthrough@*/
408
case 10: c += ((uint32_t)k[9])<<8; /*@fallthrough@*/
409
case 9: c += (uint32_t)k[8]; /*@fallthrough@*/
410
case 8: b += ((uint32_t)k[7])<<24; /*@fallthrough@*/
411
case 7: b += ((uint32_t)k[6])<<16; /*@fallthrough@*/
412
case 6: b += ((uint32_t)k[5])<<8; /*@fallthrough@*/
413
case 5: b += (uint32_t)k[4]; /*@fallthrough@*/
414
case 4: a += ((uint32_t)k[3])<<24; /*@fallthrough@*/
415
case 3: a += ((uint32_t)k[2])<<16; /*@fallthrough@*/
416
case 2: a += ((uint32_t)k[1])<<8; /*@fallthrough@*/
417
case 1: a += (uint32_t)k[0];
429
#endif /* defined(_JLU3_jlu32l) */
431
#if defined(_JLU3_jlu32lpair)
433
* jlu32lpair: return 2 32-bit hash values.
435
* This is identical to jlu32l(), except it returns two 32-bit hash
436
* values instead of just one. This is good enough for hash table
437
* lookup with 2^^64 buckets, or if you want a second hash if you're not
438
* happy with the first, or if you want a probably-unique 64-bit ID for
439
* the key. *pc is better mixed than *pb, so use *pc first. If you want
440
* a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)".
442
* @param h the previous hash, or an arbitrary value
443
* @param *key the key, an array of uint8_t values
444
* @param size the size of the key in bytes
445
* @retval *pc, IN: primary initval, OUT: primary hash
446
* *retval *pb IN: secondary initval, OUT: secondary hash
448
void jlu32lpair(const void *key, size_t size, uint32_t *pc, uint32_t *pb)
450
union { const void *ptr; size_t i; } u;
451
uint32_t a = _JLU3_INIT(*pc, size);
458
c += *pb; /* Add the secondary hash. */
461
if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
462
const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
467
/*-- all but last block: aligned reads and affect 32 bits of (a,b,c) */
468
while (size > (size_t)12) {
476
/*------------------------- handle the last (probably partial) block */
478
* "k[2]&0xffffff" actually reads beyond the end of the string, but
479
* then masks off the part it's not allowed to read. Because the
480
* string is aligned, the masked-off tail is in the same word as the
481
* rest of the string. Every machine with memory protection I've seen
482
* does it on word boundaries, so is OK with this. But VALGRIND will
483
* still catch it and complain. The masking trick does make the hash
484
* noticably faster for short strings (like English words).
489
case 12: c += k[2]; b+=k[1]; a+=k[0]; break;
490
case 11: c += k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
491
case 10: c += k[2]&0xffff; b+=k[1]; a+=k[0]; break;
492
case 9: c += k[2]&0xff; b+=k[1]; a+=k[0]; break;
493
case 8: b += k[1]; a+=k[0]; break;
494
case 7: b += k[1]&0xffffff; a+=k[0]; break;
495
case 6: b += k[1]&0xffff; a+=k[0]; break;
496
case 5: b += k[1]&0xff; a+=k[0]; break;
497
case 4: a += k[0]; break;
498
case 3: a += k[0]&0xffffff; break;
499
case 2: a += k[0]&0xffff; break;
500
case 1: a += k[0]&0xff; break;
504
#else /* make valgrind happy */
506
k8 = (const uint8_t *)k;
508
case 12: c += k[2]; b+=k[1]; a+=k[0]; break;
509
case 11: c += ((uint32_t)k8[10])<<16; /*@fallthrough@*/
510
case 10: c += ((uint32_t)k8[9])<<8; /*@fallthrough@*/
511
case 9: c += k8[8]; /*@fallthrough@*/
512
case 8: b += k[1]; a+=k[0]; break;
513
case 7: b += ((uint32_t)k8[6])<<16; /*@fallthrough@*/
514
case 6: b += ((uint32_t)k8[5])<<8; /*@fallthrough@*/
515
case 5: b += k8[4]; /*@fallthrough@*/
516
case 4: a += k[0]; break;
517
case 3: a += ((uint32_t)k8[2])<<16; /*@fallthrough@*/
518
case 2: a += ((uint32_t)k8[1])<<8; /*@fallthrough@*/
519
case 1: a += k8[0]; break;
523
#endif /* !valgrind */
525
} else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
526
const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
529
/*----------- all but last block: aligned reads and different mixing */
530
while (size > (size_t)12) {
531
a += k[0] + (((uint32_t)k[1])<<16);
532
b += k[2] + (((uint32_t)k[3])<<16);
533
c += k[4] + (((uint32_t)k[5])<<16);
539
/*------------------------- handle the last (probably partial) block */
540
k8 = (const uint8_t *)k;
543
c += k[4]+(((uint32_t)k[5])<<16);
544
b += k[2]+(((uint32_t)k[3])<<16);
545
a += k[0]+(((uint32_t)k[1])<<16);
548
c += ((uint32_t)k8[10])<<16;
552
b += k[2]+(((uint32_t)k[3])<<16);
553
a += k[0]+(((uint32_t)k[1])<<16);
559
b += k[2]+(((uint32_t)k[3])<<16);
560
a += k[0]+(((uint32_t)k[1])<<16);
563
b += ((uint32_t)k8[6])<<16;
567
a += k[0]+(((uint32_t)k[1])<<16);
573
a += k[0]+(((uint32_t)k[1])<<16);
576
a += ((uint32_t)k8[2])<<16;
588
} else { /* need to read the key one byte at a time */
589
const uint8_t *k = (const uint8_t *)key;
591
/*----------- all but the last block: affect some 32 bits of (a,b,c) */
592
while (size > (size_t)12) {
594
a += ((uint32_t)k[1])<<8;
595
a += ((uint32_t)k[2])<<16;
596
a += ((uint32_t)k[3])<<24;
598
b += ((uint32_t)k[5])<<8;
599
b += ((uint32_t)k[6])<<16;
600
b += ((uint32_t)k[7])<<24;
602
c += ((uint32_t)k[9])<<8;
603
c += ((uint32_t)k[10])<<16;
604
c += ((uint32_t)k[11])<<24;
610
/*---------------------------- last block: affect all 32 bits of (c) */
612
case 12: c += ((uint32_t)k[11])<<24; /*@fallthrough@*/
613
case 11: c += ((uint32_t)k[10])<<16; /*@fallthrough@*/
614
case 10: c += ((uint32_t)k[9])<<8; /*@fallthrough@*/
615
case 9: c += k[8]; /*@fallthrough@*/
616
case 8: b += ((uint32_t)k[7])<<24; /*@fallthrough@*/
617
case 7: b += ((uint32_t)k[6])<<16; /*@fallthrough@*/
618
case 6: b += ((uint32_t)k[5])<<8; /*@fallthrough@*/
619
case 5: b += k[4]; /*@fallthrough@*/
620
case 4: a += ((uint32_t)k[3])<<24; /*@fallthrough@*/
621
case 3: a += ((uint32_t)k[2])<<16; /*@fallthrough@*/
622
case 2: a += ((uint32_t)k[1])<<8; /*@fallthrough@*/
637
#endif /* defined(_JLU3_jlu32lpair) */
639
#if defined(_JLU3_jlu32b)
640
uint32_t jlu32b(uint32_t h, /*@null@*/ const void *key, size_t size)
644
* This is the same as jlu32w() on big-endian machines. It is different
645
* from jlu32l() on all machines. jlu32b() takes advantage of
646
* big-endian byte ordering.
648
* @param h the previous hash, or an arbitrary value
649
* @param *k the key, an array of uint8_t values
650
* @param size the size of the key
651
* @return the lookup3 hash
653
uint32_t jlu32b(uint32_t h, const void *key, size_t size)
655
union { const void *ptr; size_t i; } u;
656
uint32_t a = _JLU3_INIT(h, size);
664
if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
665
const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
670
/*-- all but last block: aligned reads and affect 32 bits of (a,b,c) */
680
/*------------------------- handle the last (probably partial) block */
682
* "k[2]<<8" actually reads beyond the end of the string, but
683
* then shifts out the part it's not allowed to read. Because the
684
* string is aligned, the illegal read is in the same word as the
685
* rest of the string. Every machine with memory protection I've seen
686
* does it on word boundaries, so is OK with this. But VALGRIND will
687
* still catch it and complain. The masking trick does make the hash
688
* noticably faster for short strings (like English words).
693
case 12: c += k[2]; b+=k[1]; a+=k[0]; break;
694
case 11: c += k[2]&0xffffff00; b+=k[1]; a+=k[0]; break;
695
case 10: c += k[2]&0xffff0000; b+=k[1]; a+=k[0]; break;
696
case 9: c += k[2]&0xff000000; b+=k[1]; a+=k[0]; break;
697
case 8: b += k[1]; a+=k[0]; break;
698
case 7: b += k[1]&0xffffff00; a+=k[0]; break;
699
case 6: b += k[1]&0xffff0000; a+=k[0]; break;
700
case 5: b += k[1]&0xff000000; a+=k[0]; break;
701
case 4: a += k[0]; break;
702
case 3: a += k[0]&0xffffff00; break;
703
case 2: a += k[0]&0xffff0000; break;
704
case 1: a += k[0]&0xff000000; break;
708
#else /* make valgrind happy */
710
k8 = (const uint8_t *)k;
711
switch (size) { /* all the case statements fall through */
712
case 12: c += k[2]; b+=k[1]; a+=k[0]; break;
713
case 11: c += ((uint32_t)k8[10])<<8; /*@fallthrough@*/
714
case 10: c += ((uint32_t)k8[9])<<16; /*@fallthrough@*/
715
case 9: c += ((uint32_t)k8[8])<<24; /*@fallthrough@*/
716
case 8: b += k[1]; a+=k[0]; break;
717
case 7: b += ((uint32_t)k8[6])<<8; /*@fallthrough@*/
718
case 6: b += ((uint32_t)k8[5])<<16; /*@fallthrough@*/
719
case 5: b += ((uint32_t)k8[4])<<24; /*@fallthrough@*/
720
case 4: a += k[0]; break;
721
case 3: a += ((uint32_t)k8[2])<<8; /*@fallthrough@*/
722
case 2: a += ((uint32_t)k8[1])<<16; /*@fallthrough@*/
723
case 1: a += ((uint32_t)k8[0])<<24; break;
727
#endif /* !VALGRIND */
729
} else { /* need to read the key one byte at a time */
730
const uint8_t *k = (const uint8_t *)key;
732
/*----------- all but the last block: affect some 32 bits of (a,b,c) */
734
a += ((uint32_t)k[0])<<24;
735
a += ((uint32_t)k[1])<<16;
736
a += ((uint32_t)k[2])<<8;
737
a += ((uint32_t)k[3]);
738
b += ((uint32_t)k[4])<<24;
739
b += ((uint32_t)k[5])<<16;
740
b += ((uint32_t)k[6])<<8;
741
b += ((uint32_t)k[7]);
742
c += ((uint32_t)k[8])<<24;
743
c += ((uint32_t)k[9])<<16;
744
c += ((uint32_t)k[10])<<8;
745
c += ((uint32_t)k[11]);
751
/*---------------------------- last block: affect all 32 bits of (c) */
752
switch (size) { /* all the case statements fall through */
753
case 12: c += k[11]; /*@fallthrough@*/
754
case 11: c += ((uint32_t)k[10])<<8; /*@fallthrough@*/
755
case 10: c += ((uint32_t)k[9])<<16; /*@fallthrough@*/
756
case 9: c += ((uint32_t)k[8])<<24; /*@fallthrough@*/
757
case 8: b += k[7]; /*@fallthrough@*/
758
case 7: b += ((uint32_t)k[6])<<8; /*@fallthrough@*/
759
case 6: b += ((uint32_t)k[5])<<16; /*@fallthrough@*/
760
case 5: b += ((uint32_t)k[4])<<24; /*@fallthrough@*/
761
case 4: a += k[3]; /*@fallthrough@*/
762
case 3: a += ((uint32_t)k[2])<<8; /*@fallthrough@*/
763
case 2: a += ((uint32_t)k[1])<<16; /*@fallthrough@*/
764
case 1: a += ((uint32_t)k[0])<<24; /*@fallthrough@*/
776
#endif /* defined(_JLU3_jlu32b) */
778
#if defined(_JLU3_SELFTEST)
780
/* used for timings */
781
static void driver1(void)
790
for (i=0; i<256; ++i) buf[i] = 'x';
791
for (i=0; i<1; ++i) {
792
h = jlu32l(h, &buf[0], sizeof(buf[0]));
795
if (z-a > 0) printf("time %d %.8x\n", (int)(z-a), h);
798
/* check that every input bit changes every output bit half the time */
803
static void driver2(void)
806
uint8_t qa[MAXLEN+1], qb[MAXLEN+2], *a = &qa[0], *b = &qb[1];
807
uint32_t c[HASHSTATE], d[HASHSTATE], i=0, j=0, k, l, m=0, z;
808
uint32_t e[HASHSTATE],f[HASHSTATE],g[HASHSTATE],h[HASHSTATE];
809
uint32_t x[HASHSTATE],y[HASHSTATE];
812
printf("No more than %d trials should ever be needed \n",MAXPAIR/2);
813
for (hlen=0; hlen < MAXLEN; ++hlen) {
815
for (i=0; i<hlen; ++i) { /*-------------- for each input byte, */
816
for (j=0; j<8; ++j) { /*--------------- for each input bit, */
817
for (m=1; m<8; ++m) { /*--- for serveral possible initvals, */
818
for (l=0; l<HASHSTATE; ++l)
819
e[l]=f[l]=g[l]=h[l]=x[l]=y[l]=~((uint32_t)0);
821
/* check that every output bit is affected by that input bit */
822
for (k=0; k<MAXPAIR; k+=2) {
824
/* keys have one bit different */
825
for (l=0; l<hlen+1; ++l) {a[l] = b[l] = (uint8_t)0;}
826
/* have a and b be two keys differing in only one bit */
829
c[0] = jlu32l(m, a, hlen);
831
b[i] ^= ((k+1)>>(8-j));
832
d[0] = jlu32l(m, b, hlen);
833
/* check every bit is 1, 0, set, and not set at least once */
834
for (l=0; l<HASHSTATE; ++l) {
836
f[l] &= ~(c[l]^d[l]);
841
if (e[l]|f[l]|g[l]|h[l]|x[l]|y[l]) finished=0;
847
printf("Some bit didn't change: ");
848
printf("%.8x %.8x %.8x %.8x %.8x %.8x ",
849
e[0],f[0],g[0],h[0],x[0],y[0]);
850
printf("i %d j %d m %d len %d\n", i, j, m, hlen);
852
if (z == MAXPAIR) goto done;
858
printf("Mix success %2d bytes %2d initvals ",i,m);
859
printf("required %d trials\n", z/2);
865
/* Check for reading beyond the end of the buffer and alignment problems */
866
static void driver3(void)
869
uint8_t buf[MAXLEN+20], *b;
871
uint8_t q[] = "This is the time for all good men to come to the aid of their country...";
873
uint8_t qq[] = "xThis is the time for all good men to come to the aid of their country...";
875
uint8_t qqq[] = "xxThis is the time for all good men to come to the aid of their country...";
877
uint8_t qqqq[] = "xxxThis is the time for all good men to come to the aid of their country...";
882
printf("Endianness. These lines should all be the same (for values filled in):\n");
883
printf("%.8x %.8x %.8x\n",
884
jlu32w(m, (const uint32_t *)q, (sizeof(q)-1)/4),
885
jlu32w(m, (const uint32_t *)q, (sizeof(q)-5)/4),
886
jlu32w(m, (const uint32_t *)q, (sizeof(q)-9)/4));
888
printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
889
jlu32l(m, p, sizeof(q)-1), jlu32l(m, p, sizeof(q)-2),
890
jlu32l(m, p, sizeof(q)-3), jlu32l(m, p, sizeof(q)-4),
891
jlu32l(m, p, sizeof(q)-5), jlu32l(m, p, sizeof(q)-6),
892
jlu32l(m, p, sizeof(q)-7), jlu32l(m, p, sizeof(q)-8),
893
jlu32l(m, p, sizeof(q)-9), jlu32l(m, p, sizeof(q)-10),
894
jlu32l(m, p, sizeof(q)-11), jlu32l(m, p, sizeof(q)-12));
896
printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
897
jlu32l(m, p, sizeof(q)-1), jlu32l(m, p, sizeof(q)-2),
898
jlu32l(m, p, sizeof(q)-3), jlu32l(m, p, sizeof(q)-4),
899
jlu32l(m, p, sizeof(q)-5), jlu32l(m, p, sizeof(q)-6),
900
jlu32l(m, p, sizeof(q)-7), jlu32l(m, p, sizeof(q)-8),
901
jlu32l(m, p, sizeof(q)-9), jlu32l(m, p, sizeof(q)-10),
902
jlu32l(m, p, sizeof(q)-11), jlu32l(m, p, sizeof(q)-12));
904
printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
905
jlu32l(m, p, sizeof(q)-1), jlu32l(m, p, sizeof(q)-2),
906
jlu32l(m, p, sizeof(q)-3), jlu32l(m, p, sizeof(q)-4),
907
jlu32l(m, p, sizeof(q)-5), jlu32l(m, p, sizeof(q)-6),
908
jlu32l(m, p, sizeof(q)-7), jlu32l(m, p, sizeof(q)-8),
909
jlu32l(m, p, sizeof(q)-9), jlu32l(m, p, sizeof(q)-10),
910
jlu32l(m, p, sizeof(q)-11), jlu32l(m, p, sizeof(q)-12));
912
printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
913
jlu32l(m, p, sizeof(q)-1), jlu32l(m, p, sizeof(q)-2),
914
jlu32l(m, p, sizeof(q)-3), jlu32l(m, p, sizeof(q)-4),
915
jlu32l(m, p, sizeof(q)-5), jlu32l(m, p, sizeof(q)-6),
916
jlu32l(m, p, sizeof(q)-7), jlu32l(m, p, sizeof(q)-8),
917
jlu32l(m, p, sizeof(q)-9), jlu32l(m, p, sizeof(q)-10),
918
jlu32l(m, p, sizeof(q)-11), jlu32l(m, p, sizeof(q)-12));
920
for (h=0, b=buf+1; h<8; ++h, ++b) {
921
for (i=0; i<MAXLEN; ++i) {
926
/* these should all be equal */
928
ref = jlu32l(m, b, len);
931
x = jlu32l(m, b, len);
932
y = jlu32l(m, b, len);
933
if ((ref != x) || (ref != y))
934
printf("alignment error: %.8x %.8x %.8x %d %d\n",ref,x,y, h, i);
939
/* check for problems with nulls */
940
static void driver4(void)
946
uint32_t state[HASHSTATE];
949
for (i=0; i<HASHSTATE; ++i)
951
printf("These should all be different\n");
953
for (i=0; i<8; ++i) {
954
h = jlu32l(h, buf, 0);
955
printf("%2ld 0-byte strings, hash is %.8x\n", (long)i, h);
960
int main(int argc, char ** argv)
962
driver1(); /* test that the key is hashed: used for timings */
963
driver2(); /* test that whole key is hashed thoroughly */
964
driver3(); /* test that nothing but the key is hashed */
965
driver4(); /* test hashing multiple buffers (all buffers are null) */
969
#endif /* _JLU3_SELFTEST */