s3: smbd: Move check_fsp_open() and check_fsp() to smb1_reply.c
[Samba.git] / third_party / popt / lookup3.c
blobeb4e5ce6bdfcd169d1d3485fad70ae43b47808b3
1 /* -------------------------------------------------------------------- */
2 /*
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;
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().
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 /* -------------------------------------------------------------------- */
37 #include <stdint.h>
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
46 /*@-redef@*/
47 /*@unchecked@*/
48 static const union _dbswap {
49 const uint32_t ui;
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)
54 /*@=redef@*/
56 #ifndef ROTL32
57 # define ROTL32(x, s) (((x) << (s)) | ((x) >> (32 - (s))))
58 #endif
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
76 * (a,b,c).
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
80 * difference.
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
85 * satisfy this are
86 * 4 6 8 16 19 4
87 * 9 15 3 18 27 15
88 * 14 9 3 7 17 3
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
97 * avalanche in c.
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
104 * rotates.
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
125 * (a,b,c).
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
129 * difference.
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:
137 * 4 8 15 26 3 22 24
138 * 10 8 15 26 3 22 24
139 * 11 8 15 26 3 22 24
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)
155 /*@*/;
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)));
177 uint32_t b = a;
178 uint32_t c = a;
180 if (k == NULL)
181 goto exit;
183 /*----------------------------------------------- handle most of the key */
184 while (size > 3) {
185 a += k[0];
186 b += k[1];
187 c += k[2];
188 _JLU3_MIX(a,b,c);
189 size -= 3;
190 k += 3;
193 /*----------------------------------------- handle the last 3 uint32_t's */
194 switch (size) {
195 case 3 : c+=k[2];
196 case 2 : b+=k[1];
197 case 1 : a+=k[0];
198 _JLU3_FINAL(a,b,c);
199 /*@fallthrough@*/
200 case 0:
201 break;
203 /*---------------------------------------------------- report the result */
204 exit:
205 return c;
207 #endif /* defined(_JLU3_jlu32w) */
209 #if defined(_JLU3_jlu32l)
210 uint32_t jlu32l(uint32_t h, const void *key, size_t size)
211 /*@*/;
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);
247 uint32_t b = a;
248 uint32_t c = a;
250 if (key == NULL)
251 goto exit;
253 u.ptr = key;
254 if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
255 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
256 #ifdef VALGRIND
257 const uint8_t *k8;
258 #endif
260 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
261 while (size > 12) {
262 a += k[0];
263 b += k[1];
264 c += k[2];
265 _JLU3_MIX(a,b,c);
266 size -= 12;
267 k += 3;
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).
280 #ifndef VALGRIND
282 switch (size) {
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;
295 case 0: goto exit;
298 #else /* make valgrind happy */
300 k8 = (const uint8_t *)k;
301 switch (size) {
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;
314 case 0: goto exit;
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 */
321 const uint8_t *k8;
323 /*----------- all but last block: aligned reads and different mixing */
324 while (size > 12) {
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);
328 _JLU3_MIX(a,b,c);
329 size -= 12;
330 k += 6;
333 /*------------------------- handle the last (probably partial) block */
334 k8 = (const uint8_t *)k;
335 switch (size) {
336 case 12:
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);
340 break;
341 case 11:
342 c += ((uint32_t)k8[10])<<16;
343 /*@fallthrough@*/
344 case 10:
345 c += (uint32_t)k[4];
346 b += k[2]+(((uint32_t)k[3])<<16);
347 a += k[0]+(((uint32_t)k[1])<<16);
348 break;
349 case 9:
350 c += (uint32_t)k8[8];
351 /*@fallthrough@*/
352 case 8:
353 b += k[2]+(((uint32_t)k[3])<<16);
354 a += k[0]+(((uint32_t)k[1])<<16);
355 break;
356 case 7:
357 b += ((uint32_t)k8[6])<<16;
358 /*@fallthrough@*/
359 case 6:
360 b += (uint32_t)k[2];
361 a += k[0]+(((uint32_t)k[1])<<16);
362 break;
363 case 5:
364 b += (uint32_t)k8[4];
365 /*@fallthrough@*/
366 case 4:
367 a += k[0]+(((uint32_t)k[1])<<16);
368 break;
369 case 3:
370 a += ((uint32_t)k8[2])<<16;
371 /*@fallthrough@*/
372 case 2:
373 a += (uint32_t)k[0];
374 break;
375 case 1:
376 a += (uint32_t)k8[0];
377 break;
378 case 0:
379 goto exit;
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) */
386 while (size > 12) {
387 a += (uint32_t)k[0];
388 a += ((uint32_t)k[1])<<8;
389 a += ((uint32_t)k[2])<<16;
390 a += ((uint32_t)k[3])<<24;
391 b += (uint32_t)k[4];
392 b += ((uint32_t)k[5])<<8;
393 b += ((uint32_t)k[6])<<16;
394 b += ((uint32_t)k[7])<<24;
395 c += (uint32_t)k[8];
396 c += ((uint32_t)k[9])<<8;
397 c += ((uint32_t)k[10])<<16;
398 c += ((uint32_t)k[11])<<24;
399 _JLU3_MIX(a,b,c);
400 size -= 12;
401 k += 12;
404 /*---------------------------- last block: affect all 32 bits of (c) */
405 switch (size) {
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];
418 break;
419 case 0:
420 goto exit;
424 _JLU3_FINAL(a,b,c);
426 exit:
427 return c;
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);
452 uint32_t b = a;
453 uint32_t c = a;
455 if (key == NULL)
456 goto exit;
458 c += *pb; /* Add the secondary hash. */
460 u.ptr = key;
461 if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
462 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
463 #ifdef VALGRIND
464 const uint8_t *k8;
465 #endif
467 /*-- all but last block: aligned reads and affect 32 bits of (a,b,c) */
468 while (size > (size_t)12) {
469 a += k[0];
470 b += k[1];
471 c += k[2];
472 _JLU3_MIX(a,b,c);
473 size -= 12;
474 k += 3;
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).
486 #ifndef VALGRIND
488 switch (size) {
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;
501 case 0: goto exit;
504 #else /* make valgrind happy */
506 k8 = (const uint8_t *)k;
507 switch (size) {
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;
520 case 0: goto exit;
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 */
527 const uint8_t *k8;
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);
534 _JLU3_MIX(a,b,c);
535 size -= 12;
536 k += 6;
539 /*------------------------- handle the last (probably partial) block */
540 k8 = (const uint8_t *)k;
541 switch (size) {
542 case 12:
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);
546 break;
547 case 11:
548 c += ((uint32_t)k8[10])<<16;
549 /*@fallthrough@*/
550 case 10:
551 c += k[4];
552 b += k[2]+(((uint32_t)k[3])<<16);
553 a += k[0]+(((uint32_t)k[1])<<16);
554 break;
555 case 9:
556 c += k8[8];
557 /*@fallthrough@*/
558 case 8:
559 b += k[2]+(((uint32_t)k[3])<<16);
560 a += k[0]+(((uint32_t)k[1])<<16);
561 break;
562 case 7:
563 b += ((uint32_t)k8[6])<<16;
564 /*@fallthrough@*/
565 case 6:
566 b += k[2];
567 a += k[0]+(((uint32_t)k[1])<<16);
568 break;
569 case 5:
570 b += k8[4];
571 /*@fallthrough@*/
572 case 4:
573 a += k[0]+(((uint32_t)k[1])<<16);
574 break;
575 case 3:
576 a += ((uint32_t)k8[2])<<16;
577 /*@fallthrough@*/
578 case 2:
579 a += k[0];
580 break;
581 case 1:
582 a += k8[0];
583 break;
584 case 0:
585 goto exit;
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) {
593 a += k[0];
594 a += ((uint32_t)k[1])<<8;
595 a += ((uint32_t)k[2])<<16;
596 a += ((uint32_t)k[3])<<24;
597 b += k[4];
598 b += ((uint32_t)k[5])<<8;
599 b += ((uint32_t)k[6])<<16;
600 b += ((uint32_t)k[7])<<24;
601 c += k[8];
602 c += ((uint32_t)k[9])<<8;
603 c += ((uint32_t)k[10])<<16;
604 c += ((uint32_t)k[11])<<24;
605 _JLU3_MIX(a,b,c);
606 size -= 12;
607 k += 12;
610 /*---------------------------- last block: affect all 32 bits of (c) */
611 switch (size) {
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@*/
623 case 1: a += k[0];
624 break;
625 case 0:
626 goto exit;
630 _JLU3_FINAL(a,b,c);
632 exit:
633 *pc = c;
634 *pb = b;
635 return;
637 #endif /* defined(_JLU3_jlu32lpair) */
639 #if defined(_JLU3_jlu32b)
640 uint32_t jlu32b(uint32_t h, /*@null@*/ const void *key, size_t size)
641 /*@*/;
643 * jlu32b():
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);
657 uint32_t b = a;
658 uint32_t c = a;
660 if (key == NULL)
661 return h;
663 u.ptr = key;
664 if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
665 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
666 #ifdef VALGRIND
667 const uint8_t *k8;
668 #endif
670 /*-- all but last block: aligned reads and affect 32 bits of (a,b,c) */
671 while (size > 12) {
672 a += k[0];
673 b += k[1];
674 c += k[2];
675 _JLU3_MIX(a,b,c);
676 size -= 12;
677 k += 3;
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).
690 #ifndef VALGRIND
692 switch (size) {
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;
705 case 0: goto exit;
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;
724 case 0: goto exit;
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) */
733 while (size > 12) {
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]);
746 _JLU3_MIX(a,b,c);
747 size -= 12;
748 k += 12;
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@*/
765 break;
766 case 0:
767 goto exit;
771 _JLU3_FINAL(a,b,c);
773 exit:
774 return c;
776 #endif /* defined(_JLU3_jlu32b) */
778 #if defined(_JLU3_SELFTEST)
780 /* used for timings */
781 static void driver1(void)
782 /*@*/
784 uint8_t buf[256];
785 uint32_t i;
786 uint32_t h=0;
787 time_t a,z;
789 time(&a);
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]));
794 time(&z);
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 */
799 #define HASHSTATE 1
800 #define HASHLEN 1
801 #define MAXPAIR 60
802 #define MAXLEN 70
803 static void driver2(void)
804 /*@*/
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];
810 uint32_t hlen;
812 printf("No more than %d trials should ever be needed \n",MAXPAIR/2);
813 for (hlen=0; hlen < MAXLEN; ++hlen) {
814 z=0;
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) {
823 uint32_t finished=1;
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 */
827 a[i] ^= (k<<j);
828 a[i] ^= (k>>(8-j));
829 c[0] = jlu32l(m, a, hlen);
830 b[i] ^= ((k+1)<<j);
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) {
835 e[l] &= (c[l]^d[l]);
836 f[l] &= ~(c[l]^d[l]);
837 g[l] &= c[l];
838 h[l] &= ~c[l];
839 x[l] &= d[l];
840 y[l] &= ~d[l];
841 if (e[l]|f[l]|g[l]|h[l]|x[l]|y[l]) finished=0;
843 if (finished) break;
845 if (k>z) z=k;
846 if (k == MAXPAIR) {
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;
856 done:
857 if (z < MAXPAIR) {
858 printf("Mix success %2d bytes %2d initvals ",i,m);
859 printf("required %d trials\n", z/2);
862 printf("\n");
865 /* Check for reading beyond the end of the buffer and alignment problems */
866 static void driver3(void)
867 /*@*/
869 uint8_t buf[MAXLEN+20], *b;
870 uint32_t len;
871 uint8_t q[] = "This is the time for all good men to come to the aid of their country...";
872 uint32_t h;
873 uint8_t qq[] = "xThis is the time for all good men to come to the aid of their country...";
874 uint32_t i;
875 uint8_t qqq[] = "xxThis is the time for all good men to come to the aid of their country...";
876 uint32_t j;
877 uint8_t qqqq[] = "xxxThis is the time for all good men to come to the aid of their country...";
878 uint32_t ref,x,y;
879 uint8_t *p;
880 uint32_t m = 13;
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));
887 p = q;
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));
895 p = &qq[1];
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));
903 p = &qqq[2];
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));
911 p = &qqqq[3];
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));
919 printf("\n");
920 for (h=0, b=buf+1; h<8; ++h, ++b) {
921 for (i=0; i<MAXLEN; ++i) {
922 len = i;
923 for (j=0; j<i; ++j)
924 *(b+j)=0;
926 /* these should all be equal */
927 m = 1;
928 ref = jlu32l(m, b, len);
929 *(b+i)=(uint8_t)~0;
930 *(b-1)=(uint8_t)~0;
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)
941 /*@*/
943 uint8_t buf[1];
944 uint32_t h;
945 uint32_t i;
946 uint32_t state[HASHSTATE];
948 buf[0] = ~0;
949 for (i=0; i<HASHSTATE; ++i)
950 state[i] = 1;
951 printf("These should all be different\n");
952 h = 0;
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) */
966 return 1;
969 #endif /* _JLU3_SELFTEST */