| 1 |
|
| #ifndef MURMUR3_H |
| 2 |
|
| #define MURMUR3_H |
| 3 |
|
| |
| 4 |
|
| //----------------------------------------------------------------------------- |
| 5 |
|
| // MurmurHash3 was written by Austin Appleby, and is placed in the public |
| 6 |
|
| // domain. The author hereby disclaims copyright to this source code. |
| 7 |
|
| |
| 8 |
|
| // Note - The x86 and x64 versions do _not_ produce the same results, as the |
| 9 |
|
| // algorithms are optimized for their respective platforms. You can still |
| 10 |
|
| // compile and run any of them on any platform, but your performance with the |
| 11 |
|
| // non-native version will be less than optimal. |
| 12 |
|
| |
| 13 |
|
| //----------------------------------------------------------------------------- |
| 14 |
|
| // Platform-specific functions and macros |
| 15 |
|
| |
| 16 |
|
| // Microsoft Visual Studio |
| 17 |
|
| |
| 18 |
|
| #if defined(_MSC_VER) |
| 19 |
|
| |
| 20 |
|
| #define FORCE_INLINE __forceinline |
| 21 |
|
| typedef unsigned char uint8_t; |
| 22 |
|
| typedef unsigned long uint32_t; |
| 23 |
|
| typedef unsigned __int64 uint64_t; |
| 24 |
|
| |
| 25 |
|
| // Other compilers |
| 26 |
|
| |
| 27 |
|
| #else // defined(_MSC_VER) |
| 28 |
|
| #include <stdint.h> |
| 29 |
|
| |
| 30 |
|
| #endif // !defined(_MSC_VER) |
| 31 |
|
| #define FORCE_INLINE inline __attribute__((always_inline)) |
| 32 |
|
| |
| 33 |
|
| inline uint32_t rotl32(uint32_t x, int8_t r) { |
| 34 |
|
| return (x << r) | (x >> (32 - r)); |
| 35 |
|
| } |
| 36 |
|
| |
| 37 |
|
| #define BIG_CONSTANT(x) (x##LLU) |
| 38 |
|
| |
| 39 |
|
| /* NO-OP for little-endian platforms */ |
| 40 |
|
| #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) |
| 41 |
|
| # if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 42 |
|
| # define BYTESWAP(x) (x) |
| 43 |
|
| # endif |
| 44 |
|
| /* if __BYTE_ORDER__ is not predefined (like FreeBSD), use arch */ |
| 45 |
|
| #elif defined(__i386) || defined(__x86_64) \ |
| 46 |
|
| || defined(__alpha) || defined(__vax) |
| 47 |
|
| |
| 48 |
|
| # define BYTESWAP(x) (x) |
| 49 |
|
| /* use __builtin_bswap32 if available */ |
| 50 |
|
| #elif defined(__GNUC__) || defined(__clang__) |
| 51 |
|
| #ifdef __has_builtin |
| 52 |
|
| #if __has_builtin(__builtin_bswap32) |
| 53 |
|
| #define BYTESWAP(x) __builtin_bswap32(x) |
| 54 |
|
| #endif // __has_builtin(__builtin_bswap32) |
| 55 |
|
| #endif // __has_builtin |
| 56 |
|
| #endif // defined(__GNUC__) || defined(__clang__) |
| 57 |
|
| /* last resort (big-endian w/o __builtin_bswap) */ |
| 58 |
|
| #ifndef BYTESWAP |
| 59 |
|
| # define BYTESWAP(x) ((((x)&0xFF)<<24) \ |
| 60 |
|
| |(((x)>>24)&0xFF) \ |
| 61 |
|
| |(((x)&0x0000FF00)<<8) \ |
| 62 |
|
| |(((x)&0x00FF0000)>>8) ) |
| 63 |
|
| #endif |
| 64 |
|
| |
| 65 |
|
| //----------------------------------------------------------------------------- |
| 66 |
|
| // Block read - if your platform needs to do endian-swapping or can only |
| 67 |
|
| // handle aligned reads, do the conversion here |
| 68 |
|
| #define getblock(p, i) BYTESWAP(p[i]) |
| 69 |
|
| |
| 70 |
|
| //----------------------------------------------------------------------------- |
| 71 |
|
| // Finalization mix - force all bits of a hash block to avalanche |
| 72 |
|
| |
| 73 |
|
| FORCE_INLINE uint32_t fmix32(uint32_t h) { |
| 74 |
0 |
| h ^= h >> 16; |
| 75 |
0 |
| h *= 0x85ebca6b; |
| 76 |
0 |
| h ^= h >> 13; |
| 77 |
0 |
| h *= 0xc2b2ae35; |
| 78 |
0 |
| h ^= h >> 16; |
| 79 |
|
| |
| 80 |
0 |
| return h; |
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
|
| //----------------------------------------------------------------------------- |
| 84 |
|
| |
| 85 |
|
| #ifdef __cplusplus |
| 86 |
|
| extern "C" |
| 87 |
|
| #else |
| 88 |
|
| extern |
| 89 |
|
| #endif |
| 90 |
0 |
| void MurmurHash3_32(const void * key, int len, uint32_t seed, void * out) { |
| 91 |
0 |
| const uint8_t * data = (const uint8_t*) key; |
| 92 |
0 |
| const int nblocks = len / 4; |
| 93 |
|
| int i; |
| 94 |
|
| |
| 95 |
0 |
| uint32_t h1 = seed; |
| 96 |
|
| |
| 97 |
0 |
| uint32_t c1 = 0xcc9e2d51; |
| 98 |
0 |
| uint32_t c2 = 0x1b873593; |
| 99 |
|
| |
| 100 |
|
| //---------- |
| 101 |
|
| // body |
| 102 |
|
| |
| 103 |
0 |
| const uint32_t * blocks = (const uint32_t *) (data + nblocks * 4); |
| 104 |
|
| |
| 105 |
0 |
▶ | for (i = -nblocks; i; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = -nblocks; i; i++) { |
0 |
0 |
0 |
|
| 106 |
0 |
| uint32_t k1 = getblock(blocks,i); |
| 107 |
|
| |
| 108 |
0 |
| k1 *= c1; |
| 109 |
0 |
| k1 = rotl32(k1,15); |
| 110 |
0 |
| k1 *= c2; |
| 111 |
|
| |
| 112 |
0 |
| h1 ^= k1; |
| 113 |
0 |
| h1 = rotl32(h1,13); |
| 114 |
0 |
| h1 = h1 * 5 + 0xe6546b64; |
| 115 |
|
| } |
| 116 |
|
| |
| 117 |
|
| //---------- |
| 118 |
|
| // tail |
| 119 |
|
| { |
| 120 |
0 |
| const uint8_t * tail = (const uint8_t*) (data + nblocks * 4); |
| 121 |
|
| |
| 122 |
0 |
| uint32_t k1 = 0; |
| 123 |
|
| |
| 124 |
0 |
| switch (len & 3) { |
| 125 |
0 |
| case 3: |
| 126 |
0 |
| k1 ^= tail[2] << 16; |
| 127 |
0 |
| case 2: |
| 128 |
0 |
| k1 ^= tail[1] << 8; |
| 129 |
0 |
| case 1: |
| 130 |
0 |
| k1 ^= tail[0]; |
| 131 |
0 |
| k1 *= c1; |
| 132 |
0 |
| k1 = rotl32(k1,15); |
| 133 |
0 |
| k1 *= c2; |
| 134 |
0 |
| h1 ^= k1; |
| 135 |
|
| }; |
| 136 |
|
| } |
| 137 |
|
| |
| 138 |
|
| //---------- |
| 139 |
|
| // finalization |
| 140 |
|
| |
| 141 |
0 |
| h1 ^= len; |
| 142 |
|
| |
| 143 |
0 |
| h1 = fmix32(h1); |
| 144 |
|
| |
| 145 |
0 |
| *(uint32_t*) out = h1; |
| 146 |
0 |
| } |
| 147 |
|
| |
| 148 |
|
| #endif |