29 #include "pedigree/kernel/utilities/smhasher/MurmurHash3.h" 38 #define FORCE_INLINE __forceinline 42 #define ROTL32(x,y) _rotl(x,y) 43 #define ROTL64(x,y) _rotl64(x,y) 45 #define BIG_CONSTANT(x) (x) 49 #else // defined(_MSC_VER) 51 #define FORCE_INLINE inline __attribute__((always_inline)) 53 inline uint32_t rotl32 ( uint32_t x, int8_t r )
55 return (x << r) | (x >> (32 - r));
58 inline uint64_t rotl64 ( uint64_t x, int8_t r )
60 return (x << r) | (x >> (64 - r));
63 #define ROTL32(x,y) rotl32(x,y) 64 #define ROTL64(x,y) rotl64(x,y) 66 #define BIG_CONSTANT(x) (x##LLU) 68 #endif // !defined(_MSC_VER) 74 FORCE_INLINE uint32_t getblock32 (
const uint32_t * p,
int i )
79 FORCE_INLINE uint64_t getblock64 (
const uint64_t * p,
int i )
87 FORCE_INLINE uint32_t fmix32 ( uint32_t h )
100 FORCE_INLINE uint64_t fmix64 ( uint64_t k )
103 k *= BIG_CONSTANT(0xff51afd7ed558ccd);
105 k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53);
113 void MurmurHash3_x86_32 (
const void * key,
int len,
114 uint32_t seed,
void * out )
116 const uint8_t * data = (
const uint8_t*)key;
117 const int nblocks = len / 4;
121 const uint32_t c1 = 0xcc9e2d51;
122 const uint32_t c2 = 0x1b873593;
127 const uint32_t * blocks = (
const uint32_t *)(data + nblocks*4);
129 for(
int i = -nblocks; i; i++)
131 uint32_t k1 = getblock32(blocks,i);
139 h1 = h1*5+0xe6546b64;
145 const uint8_t * tail = (
const uint8_t*)(data + nblocks*4);
151 case 3: k1 ^= tail[2] << 16;
152 case 2: k1 ^= tail[1] << 8;
153 case 1: k1 ^= tail[0];
154 k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
164 *(uint32_t*)out = h1;
169 void MurmurHash3_x86_128 (
const void * key,
const int len,
170 uint32_t seed,
void * out )
172 const uint8_t * data = (
const uint8_t*)key;
173 const int nblocks = len / 16;
180 const uint32_t c1 = 0x239b961b;
181 const uint32_t c2 = 0xab0e9789;
182 const uint32_t c3 = 0x38b34ae5;
183 const uint32_t c4 = 0xa1e38b93;
188 const uint32_t * blocks = (
const uint32_t *)(data + nblocks*16);
190 for(
int i = -nblocks; i; i++)
192 uint32_t k1 = getblock32(blocks,i*4+0);
193 uint32_t k2 = getblock32(blocks,i*4+1);
194 uint32_t k3 = getblock32(blocks,i*4+2);
195 uint32_t k4 = getblock32(blocks,i*4+3);
197 k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
199 h1 = ROTL32(h1,19); h1 += h2; h1 = h1*5+0x561ccd1b;
201 k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
203 h2 = ROTL32(h2,17); h2 += h3; h2 = h2*5+0x0bcaa747;
205 k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
207 h3 = ROTL32(h3,15); h3 += h4; h3 = h3*5+0x96cd1c35;
209 k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
211 h4 = ROTL32(h4,13); h4 += h1; h4 = h4*5+0x32ac3b17;
217 const uint8_t * tail = (
const uint8_t*)(data + nblocks*16);
226 case 15: k4 ^= tail[14] << 16;
227 case 14: k4 ^= tail[13] << 8;
228 case 13: k4 ^= tail[12] << 0;
229 k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
231 case 12: k3 ^= tail[11] << 24;
232 case 11: k3 ^= tail[10] << 16;
233 case 10: k3 ^= tail[ 9] << 8;
234 case 9: k3 ^= tail[ 8] << 0;
235 k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
237 case 8: k2 ^= tail[ 7] << 24;
238 case 7: k2 ^= tail[ 6] << 16;
239 case 6: k2 ^= tail[ 5] << 8;
240 case 5: k2 ^= tail[ 4] << 0;
241 k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
243 case 4: k1 ^= tail[ 3] << 24;
244 case 3: k1 ^= tail[ 2] << 16;
245 case 2: k1 ^= tail[ 1] << 8;
246 case 1: k1 ^= tail[ 0] << 0;
247 k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
253 h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len;
255 h1 += h2; h1 += h3; h1 += h4;
256 h2 += h1; h3 += h1; h4 += h1;
263 h1 += h2; h1 += h3; h1 += h4;
264 h2 += h1; h3 += h1; h4 += h1;
266 ((uint32_t*)out)[0] = h1;
267 ((uint32_t*)out)[1] = h2;
268 ((uint32_t*)out)[2] = h3;
269 ((uint32_t*)out)[3] = h4;
274 void MurmurHash3_x64_128 (
const void * key,
const int len,
275 const uint32_t seed,
void * out )
277 const uint8_t * data = (
const uint8_t*)key;
278 const int nblocks = len / 16;
283 const uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5);
284 const uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f);
289 const uint64_t * blocks = (
const uint64_t *)(data);
291 for(
int i = 0; i < nblocks; i++)
293 uint64_t k1 = getblock64(blocks,i*2+0);
294 uint64_t k2 = getblock64(blocks,i*2+1);
296 k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
298 h1 = ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729;
300 k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
302 h2 = ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;
308 const uint8_t * tail = (
const uint8_t*)(data + nblocks*16);
315 case 15: k2 ^= ((uint64_t)tail[14]) << 48;
316 case 14: k2 ^= ((uint64_t)tail[13]) << 40;
317 case 13: k2 ^= ((uint64_t)tail[12]) << 32;
318 case 12: k2 ^= ((uint64_t)tail[11]) << 24;
319 case 11: k2 ^= ((uint64_t)tail[10]) << 16;
320 case 10: k2 ^= ((uint64_t)tail[ 9]) << 8;
321 case 9: k2 ^= ((uint64_t)tail[ 8]) << 0;
322 k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
324 case 8: k1 ^= ((uint64_t)tail[ 7]) << 56;
325 case 7: k1 ^= ((uint64_t)tail[ 6]) << 48;
326 case 6: k1 ^= ((uint64_t)tail[ 5]) << 40;
327 case 5: k1 ^= ((uint64_t)tail[ 4]) << 32;
328 case 4: k1 ^= ((uint64_t)tail[ 3]) << 24;
329 case 3: k1 ^= ((uint64_t)tail[ 2]) << 16;
330 case 2: k1 ^= ((uint64_t)tail[ 1]) << 8;
331 case 1: k1 ^= ((uint64_t)tail[ 0]) << 0;
332 k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
338 h1 ^= len; h2 ^= len;
349 ((uint64_t*)out)[0] = h1;
350 ((uint64_t*)out)[1] = h2;