* hashlittle2: return 2 32-bit hash values joined into an int64. * * This is identical to hashlittle(), except it returns two 32-bit hash * values instead of just one. This is good enough for hash table * lookup with 2^^64 buckets, or if you want a second hash if you're not * happy with the first, or if you want a probably-unique 64-bit ID for * the key. *pc is better mixed than *pb, so us
| 512 | * a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)". |
| 513 | */ |
| 514 | int64 hashlittle2(const void *key, size_t length) |
| 515 | { |
| 516 | uint32_t a,b,c; /* internal state */ |
| 517 | union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */ |
| 518 | |
| 519 | /* Set up the internal state */ |
| 520 | a = b = c = 0xdeadbeef + ((uint32_t)length); |
| 521 | |
| 522 | u.ptr = key; |
| 523 | if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) { |
| 524 | const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */ |
| 525 | const uint8_t *k8; |
| 526 | |
| 527 | /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */ |
| 528 | while (length > 12) |
| 529 | { |
| 530 | a += k[0]; |
| 531 | b += k[1]; |
| 532 | c += k[2]; |
| 533 | mix(a,b,c); |
| 534 | length -= 12; |
| 535 | k += 3; |
| 536 | } |
| 537 | |
| 538 | /*----------------------------- handle the last (probably partial) block */ |
| 539 | k8 = (const uint8_t *)k; |
| 540 | switch(length) |
| 541 | { |
| 542 | case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; |
| 543 | case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ |
| 544 | case 10: c+=((uint32_t)k8[9])<<8; /* fall through */ |
| 545 | case 9 : c+=k8[8]; /* fall through */ |
| 546 | case 8 : b+=k[1]; a+=k[0]; break; |
| 547 | case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ |
| 548 | case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */ |
| 549 | case 5 : b+=k8[4]; /* fall through */ |
| 550 | case 4 : a+=k[0]; break; |
| 551 | case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ |
| 552 | case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */ |
| 553 | case 1 : a+=k8[0]; break; |
| 554 | case 0 : return NON_ZERO_64(b, c); |
| 555 | } |
| 556 | } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) { |
| 557 | const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */ |
| 558 | const uint8_t *k8; |
| 559 | |
| 560 | /*--------------- all but last block: aligned reads and different mixing */ |
| 561 | while (length > 12) |
| 562 | { |
| 563 | a += k[0] + (((uint32_t)k[1])<<16); |
| 564 | b += k[2] + (((uint32_t)k[3])<<16); |
| 565 | c += k[4] + (((uint32_t)k[5])<<16); |
| 566 | mix(a,b,c); |
| 567 | length -= 12; |
| 568 | k += 6; |
| 569 | } |
| 570 | |
| 571 | /*----------------------------- handle the last (probably partial) block */ |