19 #include "MurmurHash3.hpp"
27 #define FORCE_INLINE __forceinline
31 #define ROTL32(x,y) _rotl(x,y)
32 #define ROTL64(x,y) _rotl64(x,y)
34 #define BIG_CONSTANT(x) (x)
38 #else // not defined(_MSC_VER)
42 inline uint32_t rotl32 ( uint32_t x, int8_t r )
44 return (x << r) | (x >> (32 - r));
47 inline uint64_t rotl64 ( uint64_t x, int8_t r )
49 return (x << r) | (x >> (64 - r));
54 #define ROTL32(x,y) rotl32(x,y)
55 #define ROTL64(x,y) rotl64(x,y)
57 #define BIG_CONSTANT(x) (x##LLU)
59 #endif // !defined(_MSC_VER)
65 #define GETBLOCK(lhs, p, i ) \
74 #define FMIX_32( h ) \
91 t_k *= BIG_CONSTANT(0xff51afd7ed558ccd);\
93 t_k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53);\
103 void MurmurHash3_x86_32 (
const void * key,
int len,
104 uint32_t seed,
void * out )
106 const uint8_t * data = (
const uint8_t*)key;
107 const int nblocks = len / 4;
111 const uint32_t c1 = 0xcc9e2d51;
112 const uint32_t c2 = 0x1b873593;
117 const uint32_t * blocks = (
const uint32_t *)(data + nblocks*4);
119 for(
int i = -nblocks; i; i++)
122 GETBLOCK(k1, blocks,i);
130 h1 = h1*5+0xe6546b64;
136 const uint8_t * tail = (
const uint8_t*)(data + nblocks*4);
142 case 3: k1 ^= tail[2] << 16;
143 case 2: k1 ^= tail[1] << 8;
144 case 1: k1 ^= tail[0];
145 k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
155 *(uint32_t*)out = h1;
160 void MurmurHash3_x86_128 (
const void * key,
const int len,
161 uint32_t seed,
void * out )
163 const uint8_t * data = (
const uint8_t*)key;
164 const int nblocks = len / 16;
171 const uint32_t c1 = 0x239b961b;
172 const uint32_t c2 = 0xab0e9789;
173 const uint32_t c3 = 0x38b34ae5;
174 const uint32_t c4 = 0xa1e38b93;
179 const uint32_t * blocks = (
const uint32_t *)(data + nblocks*16);
181 for(
int i = -nblocks; i; i++)
183 uint32_t k1, k2, k3, k4;
184 GETBLOCK(k1, blocks,i*4+0);
185 GETBLOCK(k2, blocks,i*4+1);
186 GETBLOCK(k3, blocks,i*4+2);
187 GETBLOCK(k4, blocks,i*4+3);
189 k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
191 h1 = ROTL32(h1,19); h1 += h2; h1 = h1*5+0x561ccd1b;
193 k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
195 h2 = ROTL32(h2,17); h2 += h3; h2 = h2*5+0x0bcaa747;
197 k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
199 h3 = ROTL32(h3,15); h3 += h4; h3 = h3*5+0x96cd1c35;
201 k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
203 h4 = ROTL32(h4,13); h4 += h1; h4 = h4*5+0x32ac3b17;
209 const uint8_t * tail = (
const uint8_t*)(data + nblocks*16);
218 case 15: k4 ^= tail[14] << 16;
219 case 14: k4 ^= tail[13] << 8;
220 case 13: k4 ^= tail[12] << 0;
221 k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
223 case 12: k3 ^= tail[11] << 24;
224 case 11: k3 ^= tail[10] << 16;
225 case 10: k3 ^= tail[ 9] << 8;
226 case 9: k3 ^= tail[ 8] << 0;
227 k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
229 case 8: k2 ^= tail[ 7] << 24;
230 case 7: k2 ^= tail[ 6] << 16;
231 case 6: k2 ^= tail[ 5] << 8;
232 case 5: k2 ^= tail[ 4] << 0;
233 k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
235 case 4: k1 ^= tail[ 3] << 24;
236 case 3: k1 ^= tail[ 2] << 16;
237 case 2: k1 ^= tail[ 1] << 8;
238 case 1: k1 ^= tail[ 0] << 0;
239 k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
245 h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len;
247 h1 += h2; h1 += h3; h1 += h4;
248 h2 += h1; h3 += h1; h4 += h1;
255 h1 += h2; h1 += h3; h1 += h4;
256 h2 += h1; h3 += h1; h4 += h1;
258 ((uint32_t*)out)[0] = h1;
259 ((uint32_t*)out)[1] = h2;
260 ((uint32_t*)out)[2] = h3;
261 ((uint32_t*)out)[3] = h4;
266 void MurmurHash3_x64_128 (
const void * key,
const int len,
267 const uint32_t seed,
void * out )
269 const uint8_t * data = (
const uint8_t*)key;
270 const int nblocks = len / 16;
275 const uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5);
276 const uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f);
281 const uint64_t * blocks = (
const uint64_t *)(data);
283 for(
int i = 0; i < nblocks; i++)
286 GETBLOCK(k1, blocks,i*2+0);
287 GETBLOCK(k2, blocks,i*2+1);
289 k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
291 h1 = ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729;
293 k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
295 h2 = ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;
301 const uint8_t * tail = (
const uint8_t*)(data + nblocks*16);
308 case 15: k2 ^= uint64_t(tail[14]) << 48;
309 case 14: k2 ^= uint64_t(tail[13]) << 40;
310 case 13: k2 ^= uint64_t(tail[12]) << 32;
311 case 12: k2 ^= uint64_t(tail[11]) << 24;
312 case 11: k2 ^= uint64_t(tail[10]) << 16;
313 case 10: k2 ^= uint64_t(tail[ 9]) << 8;
314 case 9: k2 ^= uint64_t(tail[ 8]) << 0;
315 k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
317 case 8: k1 ^= uint64_t(tail[ 7]) << 56;
318 case 7: k1 ^= uint64_t(tail[ 6]) << 48;
319 case 6: k1 ^= uint64_t(tail[ 5]) << 40;
320 case 5: k1 ^= uint64_t(tail[ 4]) << 32;
321 case 4: k1 ^= uint64_t(tail[ 3]) << 24;
322 case 3: k1 ^= uint64_t(tail[ 2]) << 16;
323 case 2: k1 ^= uint64_t(tail[ 1]) << 8;
324 case 1: k1 ^= uint64_t(tail[ 0]) << 0;
325 k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
331 h1 ^= len; h2 ^= len;
342 ((uint64_t*)out)[0] = h1;
343 ((uint64_t*)out)[1] = h2;