rpm
4.5
|
00001 /* -------------------------------------------------------------------- */ 00002 /* 00003 * lookup3.c, by Bob Jenkins, May 2006, Public Domain. 00004 * 00005 * These are functions for producing 32-bit hashes for hash table lookup. 00006 * jlu32w(), jlu32l(), jlu32lpair(), jlu32b(), _JLU3_MIX(), and _JLU3_FINAL() 00007 * are externally useful functions. Routines to test the hash are included 00008 * if SELF_TEST is defined. You can use this free for any purpose. It's in 00009 * the public domain. It has no warranty. 00010 * 00011 * You probably want to use jlu32l(). jlu32l() and jlu32b() 00012 * hash byte arrays. jlu32l() is is faster than jlu32b() on 00013 * little-endian machines. Intel and AMD are little-endian machines. 00014 * On second thought, you probably want jlu32lpair(), which is identical to 00015 * jlu32l() except it returns two 32-bit hashes for the price of one. 00016 * You could implement jlu32bpair() if you wanted but I haven't bothered here. 00017 * 00018 * If you want to find a hash of, say, exactly 7 integers, do 00019 * a = i1; b = i2; c = i3; 00020 * _JLU3_MIX(a,b,c); 00021 * a += i4; b += i5; c += i6; 00022 * _JLU3_MIX(a,b,c); 00023 * a += i7; 00024 * _JLU3_FINAL(a,b,c); 00025 * then use c as the hash value. If you have a variable size array of 00026 * 4-byte integers to hash, use jlu32w(). If you have a byte array (like 00027 * a character string), use jlu32l(). If you have several byte arrays, or 00028 * a mix of things, see the comments above jlu32l(). 00029 * 00030 * Why is this so big? I read 12 bytes at a time into 3 4-byte integers, 00031 * then mix those integers. This is fast (you can do a lot more thorough 00032 * mixing with 12*3 instructions on 3 integers than you can with 3 instructions 00033 * on 1 byte), but shoehorning those bytes into integers efficiently is messy. 00034 */ 00035 /* -------------------------------------------------------------------- */ 00036 00037 #include "system.h" 00038 #include "debug.h" 00039 00040 #if defined(_JLU3_SELFTEST) 00041 # define _JLU3_jlu32w 1 00042 # define _JLU3_jlu32l 1 00043 # define _JLU3_jlu32lpair 1 00044 # define _JLU3_jlu32b 1 00045 #endif 00046 00047 /*@-redef@*/ 00048 /*@unchecked@*/ 00049 static const union _dbswap { 00050 const uint32_t ui; 00051 const unsigned char uc[4]; 00052 } endian = { .ui = 0x11223344 }; 00053 # define HASH_LITTLE_ENDIAN (endian.uc[0] == 0x44) 00054 # define HASH_BIG_ENDIAN (endian.uc[0] == 0x11) 00055 /*@=redef@*/ 00056 00057 #ifndef ROTL32 00058 # define ROTL32(x, s) (((x) << (s)) | ((x) >> (32 - (s)))) 00059 #endif 00060 00061 /* NOTE: The _size parameter should be in bytes. */ 00062 #define _JLU3_INIT(_h, _size) (0xdeadbeef + ((uint32_t)(_size)) + (_h)) 00063 00064 /* -------------------------------------------------------------------- */ 00065 /* 00066 * _JLU3_MIX -- mix 3 32-bit values reversibly. 00067 * 00068 * This is reversible, so any information in (a,b,c) before _JLU3_MIX() is 00069 * still in (a,b,c) after _JLU3_MIX(). 00070 * 00071 * If four pairs of (a,b,c) inputs are run through _JLU3_MIX(), or through 00072 * _JLU3_MIX() in reverse, there are at least 32 bits of the output that 00073 * are sometimes the same for one pair and different for another pair. 00074 * This was tested for: 00075 * * pairs that differed by one bit, by two bits, in any combination 00076 * of top bits of (a,b,c), or in any combination of bottom bits of 00077 * (a,b,c). 00078 * * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed 00079 * the output delta to a Gray code (a^(a>>1)) so a string of 1's (as 00080 * is commonly produced by subtraction) look like a single 1-bit 00081 * difference. 00082 * * the base values were pseudorandom, all zero but one bit set, or 00083 * all zero plus a counter that starts at zero. 00084 * 00085 * Some k values for my "a-=c; a^=ROTL32(c,k); c+=b;" arrangement that 00086 * satisfy this are 00087 * 4 6 8 16 19 4 00088 * 9 15 3 18 27 15 00089 * 14 9 3 7 17 3 00090 * Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing 00091 * for "differ" defined as + with a one-bit base and a two-bit delta. I 00092 * used http://burtleburtle.net/bob/hash/avalanche.html to choose 00093 * the operations, constants, and arrangements of the variables. 00094 * 00095 * This does not achieve avalanche. There are input bits of (a,b,c) 00096 * that fail to affect some output bits of (a,b,c), especially of a. The 00097 * most thoroughly mixed value is c, but it doesn't really even achieve 00098 * avalanche in c. 00099 * 00100 * This allows some parallelism. Read-after-writes are good at doubling 00101 * the number of bits affected, so the goal of mixing pulls in the opposite 00102 * direction as the goal of parallelism. I did what I could. Rotates 00103 * seem to cost as much as shifts on every machine I could lay my hands 00104 * on, and rotates are much kinder to the top and bottom bits, so I used 00105 * rotates. 00106 */ 00107 /* -------------------------------------------------------------------- */ 00108 #define _JLU3_MIX(a,b,c) \ 00109 { \ 00110 a -= c; a ^= ROTL32(c, 4); c += b; \ 00111 b -= a; b ^= ROTL32(a, 6); a += c; \ 00112 c -= b; c ^= ROTL32(b, 8); b += a; \ 00113 a -= c; a ^= ROTL32(c,16); c += b; \ 00114 b -= a; b ^= ROTL32(a,19); a += c; \ 00115 c -= b; c ^= ROTL32(b, 4); b += a; \ 00116 } 00117 00118 /* -------------------------------------------------------------------- */ 00142 /* -------------------------------------------------------------------- */ 00143 #define _JLU3_FINAL(a,b,c) \ 00144 { \ 00145 c ^= b; c -= ROTL32(b,14); \ 00146 a ^= c; a -= ROTL32(c,11); \ 00147 b ^= a; b -= ROTL32(a,25); \ 00148 c ^= b; c -= ROTL32(b,16); \ 00149 a ^= c; a -= ROTL32(c,4); \ 00150 b ^= a; b -= ROTL32(a,14); \ 00151 c ^= b; c -= ROTL32(b,24); \ 00152 } 00153 00154 #if defined(_JLU3_jlu32w) 00155 uint32_t jlu32w(uint32_t h, /*@null@*/ const uint32_t *k, size_t size) 00156 /*@*/; 00157 /* -------------------------------------------------------------------- */ 00174 /* -------------------------------------------------------------------- */ 00175 uint32_t jlu32w(uint32_t h, const uint32_t *k, size_t size) 00176 { 00177 uint32_t a = _JLU3_INIT(h, (size * sizeof(*k))); 00178 uint32_t b = a; 00179 uint32_t c = a; 00180 00181 if (k == NULL) 00182 goto exit; 00183 00184 /*----------------------------------------------- handle most of the key */ 00185 while (size > 3) { 00186 a += k[0]; 00187 b += k[1]; 00188 c += k[2]; 00189 _JLU3_MIX(a,b,c); 00190 size -= 3; 00191 k += 3; 00192 } 00193 00194 /*----------------------------------------- handle the last 3 uint32_t's */ 00195 switch (size) { 00196 case 3 : c+=k[2]; 00197 case 2 : b+=k[1]; 00198 case 1 : a+=k[0]; 00199 _JLU3_FINAL(a,b,c); 00200 /*@fallthrough@*/ 00201 case 0: 00202 break; 00203 } 00204 /*---------------------------------------------------- report the result */ 00205 exit: 00206 return c; 00207 } 00208 #endif /* defined(_JLU3_jlu32w) */ 00209 00210 #if defined(_JLU3_jlu32l) 00211 uint32_t jlu32l(uint32_t h, const void *key, size_t size) 00212 /*@*/; 00213 /* -------------------------------------------------------------------- */ 00214 /* 00215 * jlu32l() -- hash a variable-length key into a 32-bit value 00216 * h : can be any 4-byte value 00217 * k : the key (the unaligned variable-length array of bytes) 00218 * size : the size of the key, counting by bytes 00219 * Returns a 32-bit value. Every bit of the key affects every bit of 00220 * the return value. Two keys differing by one or two bits will have 00221 * totally different hash values. 00222 * 00223 * The best hash table sizes are powers of 2. There is no need to do 00224 * mod a prime (mod is sooo slow!). If you need less than 32 bits, 00225 * use a bitmask. For example, if you need only 10 bits, do 00226 * h = (h & hashmask(10)); 00227 * In which case, the hash table should have hashsize(10) elements. 00228 * 00229 * If you are hashing n strings (uint8_t **)k, do it like this: 00230 * for (i=0, h=0; i<n; ++i) h = jlu32l(h, k[i], len[i]); 00231 * 00232 * By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this 00233 * code any way you wish, private, educational, or commercial. It's free. 00234 * 00235 * Use for hash table lookup, or anything where one collision in 2^^32 is 00236 * acceptable. Do NOT use for cryptographic purposes. 00237 * 00238 * @param h the previous hash, or an arbitrary value 00239 * @param *k the key, an array of uint8_t values 00240 * @param size the size of the key 00241 * @return the lookup3 hash 00242 */ 00243 /* -------------------------------------------------------------------- */ 00244 uint32_t jlu32l(uint32_t h, const void *key, size_t size) 00245 { 00246 union { const void *ptr; size_t i; } u; 00247 uint32_t a = _JLU3_INIT(h, size); 00248 uint32_t b = a; 00249 uint32_t c = a; 00250 00251 if (key == NULL) 00252 goto exit; 00253 00254 u.ptr = key; 00255 if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) { 00256 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */ 00257 #ifdef VALGRIND 00258 const uint8_t *k8; 00259 #endif 00260 00261 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */ 00262 while (size > 12) { 00263 a += k[0]; 00264 b += k[1]; 00265 c += k[2]; 00266 _JLU3_MIX(a,b,c); 00267 size -= 12; 00268 k += 3; 00269 } 00270 00271 /*------------------------- handle the last (probably partial) block */ 00272 /* 00273 * "k[2]&0xffffff" actually reads beyond the end of the string, but 00274 * then masks off the part it's not allowed to read. Because the 00275 * string is aligned, the masked-off tail is in the same word as the 00276 * rest of the string. Every machine with memory protection I've seen 00277 * does it on word boundaries, so is OK with this. But VALGRIND will 00278 * still catch it and complain. The masking trick does make the hash 00279 * noticably faster for short strings (like English words). 00280 */ 00281 #ifndef VALGRIND 00282 00283 switch (size) { 00284 case 12: c += k[2]; b+=k[1]; a+=k[0]; break; 00285 case 11: c += k[2]&0xffffff; b+=k[1]; a+=k[0]; break; 00286 case 10: c += k[2]&0xffff; b+=k[1]; a+=k[0]; break; 00287 case 9: c += k[2]&0xff; b+=k[1]; a+=k[0]; break; 00288 case 8: b += k[1]; a+=k[0]; break; 00289 case 7: b += k[1]&0xffffff; a+=k[0]; break; 00290 case 6: b += k[1]&0xffff; a+=k[0]; break; 00291 case 5: b += k[1]&0xff; a+=k[0]; break; 00292 case 4: a += k[0]; break; 00293 case 3: a += k[0]&0xffffff; break; 00294 case 2: a += k[0]&0xffff; break; 00295 case 1: a += k[0]&0xff; break; 00296 case 0: goto exit; 00297 } 00298 00299 #else /* make valgrind happy */ 00300 00301 k8 = (const uint8_t *)k; 00302 switch (size) { 00303 case 12: c += k[2]; b+=k[1]; a+=k[0] break; 00304 case 11: c += ((uint32_t)k8[10])<<16; /*@fallthrough@*/ 00305 case 10: c += ((uint32_t)k8[9])<<8; /*@fallthrough@*/ 00306 case 9: c += k8[8]; /*@fallthrough@*/ 00307 case 8: b += k[1]; a+=k[0]; break; 00308 case 7: b += ((uint32_t)k8[6])<<16; /*@fallthrough@*/ 00309 case 6: b += ((uint32_t)k8[5])<<8; /*@fallthrough@*/ 00310 case 5: b += k8[4]; /*@fallthrough@*/ 00311 case 4: a += k[0]; break; 00312 case 3: a += ((uint32_t)k8[2])<<16; /*@fallthrough@*/ 00313 case 2: a += ((uint32_t)k8[1])<<8; /*@fallthrough@*/ 00314 case 1: a += k8[0]; break; 00315 case 0: goto exit; 00316 } 00317 00318 #endif /* !valgrind */ 00319 00320 } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) { 00321 const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */ 00322 const uint8_t *k8; 00323 00324 /*----------- all but last block: aligned reads and different mixing */ 00325 while (size > 12) { 00326 a += k[0] + (((uint32_t)k[1])<<16); 00327 b += k[2] + (((uint32_t)k[3])<<16); 00328 c += k[4] + (((uint32_t)k[5])<<16); 00329 _JLU3_MIX(a,b,c); 00330 size -= 12; 00331 k += 6; 00332 } 00333 00334 /*------------------------- handle the last (probably partial) block */ 00335 k8 = (const uint8_t *)k; 00336 switch (size) { 00337 case 12: 00338 c += k[4]+(((uint32_t)k[5])<<16); 00339 b += k[2]+(((uint32_t)k[3])<<16); 00340 a += k[0]+(((uint32_t)k[1])<<16); 00341 break; 00342 case 11: 00343 c += ((uint32_t)k8[10])<<16; 00344 /*@fallthrough@*/ 00345 case 10: 00346 c += k[4]; 00347 b += k[2]+(((uint32_t)k[3])<<16); 00348 a += k[0]+(((uint32_t)k[1])<<16); 00349 break; 00350 case 9: 00351 c += k8[8]; 00352 /*@fallthrough@*/ 00353 case 8: 00354 b += k[2]+(((uint32_t)k[3])<<16); 00355 a += k[0]+(((uint32_t)k[1])<<16); 00356 break; 00357 case 7: 00358 b += ((uint32_t)k8[6])<<16; 00359 /*@fallthrough@*/ 00360 case 6: 00361 b += k[2]; 00362 a += k[0]+(((uint32_t)k[1])<<16); 00363 break; 00364 case 5: 00365 b += k8[4]; 00366 /*@fallthrough@*/ 00367 case 4: 00368 a += k[0]+(((uint32_t)k[1])<<16); 00369 break; 00370 case 3: 00371 a += ((uint32_t)k8[2])<<16; 00372 /*@fallthrough@*/ 00373 case 2: 00374 a += k[0]; 00375 break; 00376 case 1: 00377 a += k8[0]; 00378 break; 00379 case 0: 00380 goto exit; 00381 } 00382 00383 } else { /* need to read the key one byte at a time */ 00384 const uint8_t *k = (const uint8_t *)key; 00385 00386 /*----------- all but the last block: affect some 32 bits of (a,b,c) */ 00387 while (size > 12) { 00388 a += k[0]; 00389 a += ((uint32_t)k[1])<<8; 00390 a += ((uint32_t)k[2])<<16; 00391 a += ((uint32_t)k[3])<<24; 00392 b += k[4]; 00393 b += ((uint32_t)k[5])<<8; 00394 b += ((uint32_t)k[6])<<16; 00395 b += ((uint32_t)k[7])<<24; 00396 c += k[8]; 00397 c += ((uint32_t)k[9])<<8; 00398 c += ((uint32_t)k[10])<<16; 00399 c += ((uint32_t)k[11])<<24; 00400 _JLU3_MIX(a,b,c); 00401 size -= 12; 00402 k += 12; 00403 } 00404 00405 /*---------------------------- last block: affect all 32 bits of (c) */ 00406 switch (size) { 00407 case 12: c += ((uint32_t)k[11])<<24; /*@fallthrough@*/ 00408 case 11: c += ((uint32_t)k[10])<<16; /*@fallthrough@*/ 00409 case 10: c += ((uint32_t)k[9])<<8; /*@fallthrough@*/ 00410 case 9: c += k[8]; /*@fallthrough@*/ 00411 case 8: b += ((uint32_t)k[7])<<24; /*@fallthrough@*/ 00412 case 7: b += ((uint32_t)k[6])<<16; /*@fallthrough@*/ 00413 case 6: b += ((uint32_t)k[5])<<8; /*@fallthrough@*/ 00414 case 5: b += k[4]; /*@fallthrough@*/ 00415 case 4: a += ((uint32_t)k[3])<<24; /*@fallthrough@*/ 00416 case 3: a += ((uint32_t)k[2])<<16; /*@fallthrough@*/ 00417 case 2: a += ((uint32_t)k[1])<<8; /*@fallthrough@*/ 00418 case 1: a += k[0]; 00419 break; 00420 case 0: 00421 goto exit; 00422 } 00423 } 00424 00425 _JLU3_FINAL(a,b,c); 00426 00427 exit: 00428 return c; 00429 } 00430 #endif /* defined(_JLU3_jlu32l) */ 00431 00432 #if defined(_JLU3_jlu32lpair) 00433 void jlu32lpair(/*@null@*/ const void *key, size_t size, 00434 uint32_t *pc, uint32_t *pb) 00435 /*@modifies *pc, *pb@*/; 00452 void jlu32lpair(const void *key, size_t size, uint32_t *pc, uint32_t *pb) 00453 { 00454 union { const void *ptr; size_t i; } u; 00455 uint32_t a = _JLU3_INIT(*pc, size); 00456 uint32_t b = a; 00457 uint32_t c = a; 00458 00459 if (key == NULL) 00460 goto exit; 00461 00462 c += *pb; /* Add the secondary hash. */ 00463 00464 u.ptr = key; 00465 if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) { 00466 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */ 00467 #ifdef VALGRIND 00468 const uint8_t *k8; 00469 #endif 00470 00471 /*-- all but last block: aligned reads and affect 32 bits of (a,b,c) */ 00472 while (size > 12) { 00473 a += k[0]; 00474 b += k[1]; 00475 c += k[2]; 00476 _JLU3_MIX(a,b,c); 00477 size -= 12; 00478 k += 3; 00479 } 00480 /*------------------------- handle the last (probably partial) block */ 00481 /* 00482 * "k[2]&0xffffff" actually reads beyond the end of the string, but 00483 * then masks off the part it's not allowed to read. Because the 00484 * string is aligned, the masked-off tail is in the same word as the 00485 * rest of the string. Every machine with memory protection I've seen 00486 * does it on word boundaries, so is OK with this. But VALGRIND will 00487 * still catch it and complain. The masking trick does make the hash 00488 * noticably faster for short strings (like English words). 00489 */ 00490 #ifndef VALGRIND 00491 00492 switch (size) { 00493 case 12: c += k[2]; b+=k[1]; a+=k[0]; break; 00494 case 11: c += k[2]&0xffffff; b+=k[1]; a+=k[0]; break; 00495 case 10: c += k[2]&0xffff; b+=k[1]; a+=k[0]; break; 00496 case 9: c += k[2]&0xff; b+=k[1]; a+=k[0]; break; 00497 case 8: b += k[1]; a+=k[0]; break; 00498 case 7: b += k[1]&0xffffff; a+=k[0]; break; 00499 case 6: b += k[1]&0xffff; a+=k[0]; break; 00500 case 5: b += k[1]&0xff; a+=k[0]; break; 00501 case 4: a += k[0]; break; 00502 case 3: a += k[0]&0xffffff; break; 00503 case 2: a += k[0]&0xffff; break; 00504 case 1: a += k[0]&0xff; break; 00505 case 0: goto exit; 00506 } 00507 00508 #else /* make valgrind happy */ 00509 00510 k8 = (const uint8_t *)k; 00511 switch (size) { 00512 case 12: c += k[2]; b+=k[1]; a+=k[0]; break; 00513 case 11: c += ((uint32_t)k8[10])<<16; /*@fallthrough@*/ 00514 case 10: c += ((uint32_t)k8[9])<<8; /*@fallthrough@*/ 00515 case 9: c += k8[8]; /*@fallthrough@*/ 00516 case 8: b += k[1]; a+=k[0]; break; 00517 case 7: b += ((uint32_t)k8[6])<<16; /*@fallthrough@*/ 00518 case 6: b += ((uint32_t)k8[5])<<8; /*@fallthrough@*/ 00519 case 5: b += k8[4]; /*@fallthrough@*/ 00520 case 4: a += k[0]; break; 00521 case 3: a += ((uint32_t)k8[2])<<16; /*@fallthrough@*/ 00522 case 2: a += ((uint32_t)k8[1])<<8; /*@fallthrough@*/ 00523 case 1: a += k8[0]; break; 00524 case 0: goto exit; 00525 } 00526 00527 #endif /* !valgrind */ 00528 00529 } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) { 00530 const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */ 00531 const uint8_t *k8; 00532 00533 /*----------- all but last block: aligned reads and different mixing */ 00534 while (size > 12) { 00535 a += k[0] + (((uint32_t)k[1])<<16); 00536 b += k[2] + (((uint32_t)k[3])<<16); 00537 c += k[4] + (((uint32_t)k[5])<<16); 00538 _JLU3_MIX(a,b,c); 00539 size -= 12; 00540 k += 6; 00541 } 00542 00543 /*------------------------- handle the last (probably partial) block */ 00544 k8 = (const uint8_t *)k; 00545 switch (size) { 00546 case 12: 00547 c += k[4]+(((uint32_t)k[5])<<16); 00548 b += k[2]+(((uint32_t)k[3])<<16); 00549 a += k[0]+(((uint32_t)k[1])<<16); 00550 break; 00551 case 11: 00552 c += ((uint32_t)k8[10])<<16; 00553 /*@fallthrough@*/ 00554 case 10: 00555 c += k[4]; 00556 b += k[2]+(((uint32_t)k[3])<<16); 00557 a += k[0]+(((uint32_t)k[1])<<16); 00558 break; 00559 case 9: 00560 c += k8[8]; 00561 /*@fallthrough@*/ 00562 case 8: 00563 b += k[2]+(((uint32_t)k[3])<<16); 00564 a += k[0]+(((uint32_t)k[1])<<16); 00565 break; 00566 case 7: 00567 b += ((uint32_t)k8[6])<<16; 00568 /*@fallthrough@*/ 00569 case 6: 00570 b += k[2]; 00571 a += k[0]+(((uint32_t)k[1])<<16); 00572 break; 00573 case 5: 00574 b += k8[4]; 00575 /*@fallthrough@*/ 00576 case 4: 00577 a += k[0]+(((uint32_t)k[1])<<16); 00578 break; 00579 case 3: 00580 a += ((uint32_t)k8[2])<<16; 00581 /*@fallthrough@*/ 00582 case 2: 00583 a += k[0]; 00584 break; 00585 case 1: 00586 a += k8[0]; 00587 break; 00588 case 0: 00589 goto exit; 00590 } 00591 00592 } else { /* need to read the key one byte at a time */ 00593 const uint8_t *k = (const uint8_t *)key; 00594 00595 /*----------- all but the last block: affect some 32 bits of (a,b,c) */ 00596 while (size > 12) { 00597 a += k[0]; 00598 a += ((uint32_t)k[1])<<8; 00599 a += ((uint32_t)k[2])<<16; 00600 a += ((uint32_t)k[3])<<24; 00601 b += k[4]; 00602 b += ((uint32_t)k[5])<<8; 00603 b += ((uint32_t)k[6])<<16; 00604 b += ((uint32_t)k[7])<<24; 00605 c += k[8]; 00606 c += ((uint32_t)k[9])<<8; 00607 c += ((uint32_t)k[10])<<16; 00608 c += ((uint32_t)k[11])<<24; 00609 _JLU3_MIX(a,b,c); 00610 size -= 12; 00611 k += 12; 00612 } 00613 00614 /*---------------------------- last block: affect all 32 bits of (c) */ 00615 switch (size) { 00616 case 12: c += ((uint32_t)k[11])<<24; /*@fallthrough@*/ 00617 case 11: c += ((uint32_t)k[10])<<16; /*@fallthrough@*/ 00618 case 10: c += ((uint32_t)k[9])<<8; /*@fallthrough@*/ 00619 case 9: c += k[8]; /*@fallthrough@*/ 00620 case 8: b += ((uint32_t)k[7])<<24; /*@fallthrough@*/ 00621 case 7: b += ((uint32_t)k[6])<<16; /*@fallthrough@*/ 00622 case 6: b += ((uint32_t)k[5])<<8; /*@fallthrough@*/ 00623 case 5: b += k[4]; /*@fallthrough@*/ 00624 case 4: a += ((uint32_t)k[3])<<24; /*@fallthrough@*/ 00625 case 3: a += ((uint32_t)k[2])<<16; /*@fallthrough@*/ 00626 case 2: a += ((uint32_t)k[1])<<8; /*@fallthrough@*/ 00627 case 1: a += k[0]; /*@fallthrough@*/ 00628 break; 00629 case 0: 00630 goto exit; 00631 } 00632 } 00633 00634 _JLU3_FINAL(a,b,c); 00635 00636 exit: 00637 *pc = c; 00638 *pb = b; 00639 return; 00640 } 00641 #endif /* defined(_JLU3_jlu32lpair) */ 00642 00643 #if defined(_JLU3_jlu32b) 00644 uint32_t jlu32b(uint32_t h, /*@null@*/ const void *key, size_t size) 00645 /*@*/; 00646 /* 00647 * jlu32b(): 00648 * This is the same as jlu32w() on big-endian machines. It is different 00649 * from jlu32l() on all machines. jlu32b() takes advantage of 00650 * big-endian byte ordering. 00651 * 00652 * @param h the previous hash, or an arbitrary value 00653 * @param *k the key, an array of uint8_t values 00654 * @param size the size of the key 00655 * @return the lookup3 hash 00656 */ 00657 uint32_t jlu32b(uint32_t h, const void *key, size_t size) 00658 { 00659 union { const void *ptr; size_t i; } u; 00660 uint32_t a = _JLU3_INIT(h, size); 00661 uint32_t b = a; 00662 uint32_t c = a; 00663 00664 if (key == NULL) 00665 return h; 00666 00667 u.ptr = key; 00668 if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) { 00669 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */ 00670 #ifdef VALGRIND 00671 const uint8_t *k8; 00672 #endif 00673 00674 /*-- all but last block: aligned reads and affect 32 bits of (a,b,c) */ 00675 while (size > 12) { 00676 a += k[0]; 00677 b += k[1]; 00678 c += k[2]; 00679 _JLU3_MIX(a,b,c); 00680 size -= 12; 00681 k += 3; 00682 } 00683 00684 /*------------------------- handle the last (probably partial) block */ 00685 /* 00686 * "k[2]<<8" actually reads beyond the end of the string, but 00687 * then shifts out the part it's not allowed to read. Because the 00688 * string is aligned, the illegal read is in the same word as the 00689 * rest of the string. Every machine with memory protection I've seen 00690 * does it on word boundaries, so is OK with this. But VALGRIND will 00691 * still catch it and complain. The masking trick does make the hash 00692 * noticably faster for short strings (like English words). 00693 */ 00694 #ifndef VALGRIND 00695 00696 switch (size) { 00697 case 12: c += k[2]; b+=k[1]; a+=k[0]; break; 00698 case 11: c += k[2]&0xffffff00; b+=k[1]; a+=k[0]; break; 00699 case 10: c += k[2]&0xffff0000; b+=k[1]; a+=k[0]; break; 00700 case 9: c += k[2]&0xff000000; b+=k[1]; a+=k[0]; break; 00701 case 8: b += k[1]; a+=k[0]; break; 00702 case 7: b += k[1]&0xffffff00; a+=k[0]; break; 00703 case 6: b += k[1]&0xffff0000; a+=k[0]; break; 00704 case 5: b += k[1]&0xff000000; a+=k[0]; break; 00705 case 4: a += k[0]; break; 00706 case 3: a += k[0]&0xffffff00; break; 00707 case 2: a += k[0]&0xffff0000; break; 00708 case 1: a += k[0]&0xff000000; break; 00709 case 0: goto exit; 00710 } 00711 00712 #else /* make valgrind happy */ 00713 00714 k8 = (const uint8_t *)k; 00715 switch (size) { /* all the case statements fall through */ 00716 case 12: c += k[2]; b+=k[1]; a+=k[0]; break; 00717 case 11: c += ((uint32_t)k8[10])<<8; /*@fallthrough@*/ 00718 case 10: c += ((uint32_t)k8[9])<<16; /*@fallthrough@*/ 00719 case 9: c += ((uint32_t)k8[8])<<24; /*@fallthrough@*/ 00720 case 8: b += k[1]; a+=k[0]; break; 00721 case 7: b += ((uint32_t)k8[6])<<8; /*@fallthrough@*/ 00722 case 6: b += ((uint32_t)k8[5])<<16; /*@fallthrough@*/ 00723 case 5: b += ((uint32_t)k8[4])<<24; /*@fallthrough@*/ 00724 case 4: a += k[0]; break; 00725 case 3: a += ((uint32_t)k8[2])<<8; /*@fallthrough@*/ 00726 case 2: a += ((uint32_t)k8[1])<<16; /*@fallthrough@*/ 00727 case 1: a += ((uint32_t)k8[0])<<24; break; 00728 case 0: goto exit; 00729 } 00730 00731 #endif /* !VALGRIND */ 00732 00733 } else { /* need to read the key one byte at a time */ 00734 const uint8_t *k = (const uint8_t *)key; 00735 00736 /*----------- all but the last block: affect some 32 bits of (a,b,c) */ 00737 while (size > 12) { 00738 a += ((uint32_t)k[0])<<24; 00739 a += ((uint32_t)k[1])<<16; 00740 a += ((uint32_t)k[2])<<8; 00741 a += ((uint32_t)k[3]); 00742 b += ((uint32_t)k[4])<<24; 00743 b += ((uint32_t)k[5])<<16; 00744 b += ((uint32_t)k[6])<<8; 00745 b += ((uint32_t)k[7]); 00746 c += ((uint32_t)k[8])<<24; 00747 c += ((uint32_t)k[9])<<16; 00748 c += ((uint32_t)k[10])<<8; 00749 c += ((uint32_t)k[11]); 00750 _JLU3_MIX(a,b,c); 00751 size -= 12; 00752 k += 12; 00753 } 00754 00755 /*---------------------------- last block: affect all 32 bits of (c) */ 00756 switch (size) { /* all the case statements fall through */ 00757 case 12: c += k[11]; /*@fallthrough@*/ 00758 case 11: c += ((uint32_t)k[10])<<8; /*@fallthrough@*/ 00759 case 10: c += ((uint32_t)k[9])<<16; /*@fallthrough@*/ 00760 case 9: c += ((uint32_t)k[8])<<24; /*@fallthrough@*/ 00761 case 8: b += k[7]; /*@fallthrough@*/ 00762 case 7: b += ((uint32_t)k[6])<<8; /*@fallthrough@*/ 00763 case 6: b += ((uint32_t)k[5])<<16; /*@fallthrough@*/ 00764 case 5: b += ((uint32_t)k[4])<<24; /*@fallthrough@*/ 00765 case 4: a += k[3]; /*@fallthrough@*/ 00766 case 3: a += ((uint32_t)k[2])<<8; /*@fallthrough@*/ 00767 case 2: a += ((uint32_t)k[1])<<16; /*@fallthrough@*/ 00768 case 1: a += ((uint32_t)k[0])<<24; /*@fallthrough@*/ 00769 break; 00770 case 0: 00771 goto exit; 00772 } 00773 } 00774 00775 _JLU3_FINAL(a,b,c); 00776 00777 exit: 00778 return c; 00779 } 00780 #endif /* defined(_JLU3_jlu32b) */ 00781 00782 #if defined(_JLU3_SELFTEST) 00783 00784 /* used for timings */ 00785 static void driver1(void) 00786 /*@*/ 00787 { 00788 uint8_t buf[256]; 00789 uint32_t i; 00790 uint32_t h=0; 00791 time_t a,z; 00792 00793 time(&a); 00794 for (i=0; i<256; ++i) buf[i] = 'x'; 00795 for (i=0; i<1; ++i) { 00796 h = jlu32l(h, &buf[0], sizeof(buf[0])); 00797 } 00798 time(&z); 00799 if (z-a > 0) printf("time %d %.8x\n", (int)(z-a), h); 00800 } 00801 00802 /* check that every input bit changes every output bit half the time */ 00803 #define HASHSTATE 1 00804 #define HASHLEN 1 00805 #define MAXPAIR 60 00806 #define MAXLEN 70 00807 static void driver2(void) 00808 /*@*/ 00809 { 00810 uint8_t qa[MAXLEN+1], qb[MAXLEN+2], *a = &qa[0], *b = &qb[1]; 00811 uint32_t c[HASHSTATE], d[HASHSTATE], i=0, j=0, k, l, m=0, z; 00812 uint32_t e[HASHSTATE],f[HASHSTATE],g[HASHSTATE],h[HASHSTATE]; 00813 uint32_t x[HASHSTATE],y[HASHSTATE]; 00814 uint32_t hlen; 00815 00816 printf("No more than %d trials should ever be needed \n",MAXPAIR/2); 00817 for (hlen=0; hlen < MAXLEN; ++hlen) { 00818 z=0; 00819 for (i=0; i<hlen; ++i) { /*-------------- for each input byte, */ 00820 for (j=0; j<8; ++j) { /*--------------- for each input bit, */ 00821 for (m=1; m<8; ++m) { /*--- for serveral possible initvals, */ 00822 for (l=0; l<HASHSTATE; ++l) 00823 e[l]=f[l]=g[l]=h[l]=x[l]=y[l]=~((uint32_t)0); 00824 00825 /* check that every output bit is affected by that input bit */ 00826 for (k=0; k<MAXPAIR; k+=2) { 00827 uint32_t finished=1; 00828 /* keys have one bit different */ 00829 for (l=0; l<hlen+1; ++l) {a[l] = b[l] = (uint8_t)0;} 00830 /* have a and b be two keys differing in only one bit */ 00831 a[i] ^= (k<<j); 00832 a[i] ^= (k>>(8-j)); 00833 c[0] = jlu32l(m, a, hlen); 00834 b[i] ^= ((k+1)<<j); 00835 b[i] ^= ((k+1)>>(8-j)); 00836 d[0] = jlu32l(m, b, hlen); 00837 /* check every bit is 1, 0, set, and not set at least once */ 00838 for (l=0; l<HASHSTATE; ++l) { 00839 e[l] &= (c[l]^d[l]); 00840 f[l] &= ~(c[l]^d[l]); 00841 g[l] &= c[l]; 00842 h[l] &= ~c[l]; 00843 x[l] &= d[l]; 00844 y[l] &= ~d[l]; 00845 if (e[l]|f[l]|g[l]|h[l]|x[l]|y[l]) finished=0; 00846 } 00847 if (finished) break; 00848 } 00849 if (k>z) z=k; 00850 if (k == MAXPAIR) { 00851 printf("Some bit didn't change: "); 00852 printf("%.8x %.8x %.8x %.8x %.8x %.8x ", 00853 e[0],f[0],g[0],h[0],x[0],y[0]); 00854 printf("i %d j %d m %d len %d\n", i, j, m, hlen); 00855 } 00856 if (z == MAXPAIR) goto done; 00857 } 00858 } 00859 } 00860 done: 00861 if (z < MAXPAIR) { 00862 printf("Mix success %2d bytes %2d initvals ",i,m); 00863 printf("required %d trials\n", z/2); 00864 } 00865 } 00866 printf("\n"); 00867 } 00868 00869 /* Check for reading beyond the end of the buffer and alignment problems */ 00870 static void driver3(void) 00871 /*@*/ 00872 { 00873 uint8_t buf[MAXLEN+20], *b; 00874 uint32_t len; 00875 uint8_t q[] = "This is the time for all good men to come to the aid of their country..."; 00876 uint32_t h; 00877 uint8_t qq[] = "xThis is the time for all good men to come to the aid of their country..."; 00878 uint32_t i; 00879 uint8_t qqq[] = "xxThis is the time for all good men to come to the aid of their country..."; 00880 uint32_t j; 00881 uint8_t qqqq[] = "xxxThis is the time for all good men to come to the aid of their country..."; 00882 uint32_t ref,x,y; 00883 uint8_t *p; 00884 uint32_t m = 13; 00885 00886 printf("Endianness. These lines should all be the same (for values filled in):\n"); 00887 printf("%.8x %.8x %.8x\n", 00888 jlu32w(m, (const uint32_t *)q, (sizeof(q)-1)/4), 00889 jlu32w(m, (const uint32_t *)q, (sizeof(q)-5)/4), 00890 jlu32w(m, (const uint32_t *)q, (sizeof(q)-9)/4)); 00891 p = q; 00892 printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n", 00893 jlu32l(m, p, sizeof(q)-1), jlu32l(m, p, sizeof(q)-2), 00894 jlu32l(m, p, sizeof(q)-3), jlu32l(m, p, sizeof(q)-4), 00895 jlu32l(m, p, sizeof(q)-5), jlu32l(m, p, sizeof(q)-6), 00896 jlu32l(m, p, sizeof(q)-7), jlu32l(m, p, sizeof(q)-8), 00897 jlu32l(m, p, sizeof(q)-9), jlu32l(m, p, sizeof(q)-10), 00898 jlu32l(m, p, sizeof(q)-11), jlu32l(m, p, sizeof(q)-12)); 00899 p = &qq[1]; 00900 printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n", 00901 jlu32l(m, p, sizeof(q)-1), jlu32l(m, p, sizeof(q)-2), 00902 jlu32l(m, p, sizeof(q)-3), jlu32l(m, p, sizeof(q)-4), 00903 jlu32l(m, p, sizeof(q)-5), jlu32l(m, p, sizeof(q)-6), 00904 jlu32l(m, p, sizeof(q)-7), jlu32l(m, p, sizeof(q)-8), 00905 jlu32l(m, p, sizeof(q)-9), jlu32l(m, p, sizeof(q)-10), 00906 jlu32l(m, p, sizeof(q)-11), jlu32l(m, p, sizeof(q)-12)); 00907 p = &qqq[2]; 00908 printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n", 00909 jlu32l(m, p, sizeof(q)-1), jlu32l(m, p, sizeof(q)-2), 00910 jlu32l(m, p, sizeof(q)-3), jlu32l(m, p, sizeof(q)-4), 00911 jlu32l(m, p, sizeof(q)-5), jlu32l(m, p, sizeof(q)-6), 00912 jlu32l(m, p, sizeof(q)-7), jlu32l(m, p, sizeof(q)-8), 00913 jlu32l(m, p, sizeof(q)-9), jlu32l(m, p, sizeof(q)-10), 00914 jlu32l(m, p, sizeof(q)-11), jlu32l(m, p, sizeof(q)-12)); 00915 p = &qqqq[3]; 00916 printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n", 00917 jlu32l(m, p, sizeof(q)-1), jlu32l(m, p, sizeof(q)-2), 00918 jlu32l(m, p, sizeof(q)-3), jlu32l(m, p, sizeof(q)-4), 00919 jlu32l(m, p, sizeof(q)-5), jlu32l(m, p, sizeof(q)-6), 00920 jlu32l(m, p, sizeof(q)-7), jlu32l(m, p, sizeof(q)-8), 00921 jlu32l(m, p, sizeof(q)-9), jlu32l(m, p, sizeof(q)-10), 00922 jlu32l(m, p, sizeof(q)-11), jlu32l(m, p, sizeof(q)-12)); 00923 printf("\n"); 00924 for (h=0, b=buf+1; h<8; ++h, ++b) { 00925 for (i=0; i<MAXLEN; ++i) { 00926 len = i; 00927 for (j=0; j<i; ++j) 00928 *(b+j)=0; 00929 00930 /* these should all be equal */ 00931 m = 1; 00932 ref = jlu32l(m, b, len); 00933 *(b+i)=(uint8_t)~0; 00934 *(b-1)=(uint8_t)~0; 00935 x = jlu32l(m, b, len); 00936 y = jlu32l(m, b, len); 00937 if ((ref != x) || (ref != y)) 00938 printf("alignment error: %.8x %.8x %.8x %d %d\n",ref,x,y, h, i); 00939 } 00940 } 00941 } 00942 00943 /* check for problems with nulls */ 00944 static void driver4(void) 00945 /*@*/ 00946 { 00947 uint8_t buf[1]; 00948 uint32_t h; 00949 uint32_t i; 00950 uint32_t state[HASHSTATE]; 00951 00952 buf[0] = ~0; 00953 for (i=0; i<HASHSTATE; ++i) 00954 state[i] = 1; 00955 printf("These should all be different\n"); 00956 h = 0; 00957 for (i=0; i<8; ++i) { 00958 h = jlu32l(h, buf, 0); 00959 printf("%2ld 0-byte strings, hash is %.8x\n", (long)i, h); 00960 } 00961 } 00962 00963 00964 int main(int argc, char ** argv) 00965 { 00966 driver1(); /* test that the key is hashed: used for timings */ 00967 driver2(); /* test that whole key is hashed thoroughly */ 00968 driver3(); /* test that nothing but the key is hashed */ 00969 driver4(); /* test hashing multiple buffers (all buffers are null) */ 00970 return 1; 00971 } 00972 00973 #endif /* _JLU3_SELFTEST */