-------------------------------------------------------------------- */ * jlu32l() -- hash a variable-length key into a 32-bit value * h : can be any 4-byte value * k : the key (the unaligned variable-length array of bytes) * size : the size of the key, counting by bytes * Returns a 32-bit value. Every bit of the key affects every bit of * the return value. Two keys dif
| 236 | */ |
| 237 | /* -------------------------------------------------------------------- */ |
| 238 | uint32_t jlu32l(uint32_t h, const void *key, size_t size) |
| 239 | { |
| 240 | union { const void *ptr; size_t i; } u; |
| 241 | uint32_t a = _JLU3_INIT(h, size); |
| 242 | uint32_t b = a; |
| 243 | uint32_t c = a; |
| 244 | |
| 245 | if (key == NULL) |
| 246 | goto exit; |
| 247 | |
| 248 | u.ptr = key; |
| 249 | if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) { |
| 250 | const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */ |
| 251 | #ifdef VALGRIND |
| 252 | const uint8_t *k8; |
| 253 | #endif |
| 254 | |
| 255 | /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */ |
| 256 | while (size > 12) { |
| 257 | a += k[0]; |
| 258 | b += k[1]; |
| 259 | c += k[2]; |
| 260 | _JLU3_MIX(a,b,c); |
| 261 | size -= 12; |
| 262 | k += 3; |
| 263 | } |
| 264 | |
| 265 | /*------------------------- handle the last (probably partial) block */ |
| 266 | /* |
| 267 | * "k[2]&0xffffff" actually reads beyond the end of the string, but |
| 268 | * then masks off the part it's not allowed to read. Because the |
| 269 | * string is aligned, the masked-off tail is in the same word as the |
| 270 | * rest of the string. Every machine with memory protection I've seen |
| 271 | * does it on word boundaries, so is OK with this. But VALGRIND will |
| 272 | * still catch it and complain. The masking trick does make the hash |
| 273 | * noticeably faster for short strings (like English words). |
| 274 | */ |
| 275 | #ifndef VALGRIND |
| 276 | |
| 277 | switch (size) { |
| 278 | case 12: c += k[2]; b+=k[1]; a+=k[0]; break; |
| 279 | case 11: c += k[2]&0xffffff; b+=k[1]; a+=k[0]; break; |
| 280 | case 10: c += k[2]&0xffff; b+=k[1]; a+=k[0]; break; |
| 281 | case 9: c += k[2]&0xff; b+=k[1]; a+=k[0]; break; |
| 282 | case 8: b += k[1]; a+=k[0]; break; |
| 283 | case 7: b += k[1]&0xffffff; a+=k[0]; break; |
| 284 | case 6: b += k[1]&0xffff; a+=k[0]; break; |
| 285 | case 5: b += k[1]&0xff; a+=k[0]; break; |
| 286 | case 4: a += k[0]; break; |
| 287 | case 3: a += k[0]&0xffffff; break; |
| 288 | case 2: a += k[0]&0xffff; break; |
| 289 | case 1: a += k[0]&0xff; break; |
| 290 | case 0: goto exit; |
| 291 | } |
| 292 | |
| 293 | #else /* make valgrind happy */ |
| 294 | |
| 295 | k8 = (const uint8_t *)k; |