| 33 | static inline uint64 ByteAs64(char c) { return static_cast<uint64>(c) & 0xff; } |
| 34 | |
| 35 | uint32 Hash32(const char* data, size_t n, uint32 seed) { |
| 36 | // 'm' and 'r' are mixing constants generated offline. |
| 37 | // They're not really 'magic', they just happen to work well. |
| 38 | |
| 39 | const uint32 m = 0x5bd1e995; |
| 40 | const int r = 24; |
| 41 | |
| 42 | // Initialize the hash to a 'random' value |
| 43 | uint32 h = seed ^ n; |
| 44 | |
| 45 | // Mix 4 bytes at a time into the hash |
| 46 | while (n >= 4) { |
| 47 | uint32 k = core::DecodeFixed32(data); |
| 48 | |
| 49 | k *= m; |
| 50 | k ^= k >> r; |
| 51 | k *= m; |
| 52 | |
| 53 | h *= m; |
| 54 | h ^= k; |
| 55 | |
| 56 | data += 4; |
| 57 | n -= 4; |
| 58 | } |
| 59 | |
| 60 | // Handle the last few bytes of the input array |
| 61 | |
| 62 | switch (n) { |
| 63 | case 3: |
| 64 | h ^= ByteAs32(data[2]) << 16; |
| 65 | TF_FALLTHROUGH_INTENDED; |
| 66 | case 2: |
| 67 | h ^= ByteAs32(data[1]) << 8; |
| 68 | TF_FALLTHROUGH_INTENDED; |
| 69 | case 1: |
| 70 | h ^= ByteAs32(data[0]); |
| 71 | h *= m; |
| 72 | } |
| 73 | |
| 74 | // Do a few final mixes of the hash to ensure the last few |
| 75 | // bytes are well-incorporated. |
| 76 | |
| 77 | h ^= h >> 13; |
| 78 | h *= m; |
| 79 | h ^= h >> 15; |
| 80 | |
| 81 | return h; |
| 82 | } |
| 83 | |
| 84 | uint64 SDBMHash64(const char *key, size_t len) { |
| 85 | register uint64 hash_value = 0; |