vkdb
A time series database engine in C++.
Loading...
Searching...
No Matches
murmur_hash_3.cpp
1//-----------------------------------------------------------------------------
2// MurmurHash3 was written by Austin Appleby, and is placed in the public
3// domain. The author hereby disclaims copyright to this source code.
4
5// Note - The x86 and x64 versions do _not_ produce the same results, as the
6// algorithms are optimized for their respective platforms. You can still
7// compile and run any of them on any platform, but your performance with the
8// non-native version will be less than optimal.
9
10//-----------------------------------------------------------------------------
11// Platform-specific functions and macros
12
13#include <cstdint>
14
15// Microsoft Visual Studio
16
17#if defined(_MSC_VER)
18
19#define FORCE_INLINE __forceinline
20
21#define ROTL32(x, y) _rotl(x, y)
22#define ROTL64(x, y) _rotl64(x, y)
23
24#define BIG_CONSTANT(x) (x)
25
26// Other compilers
27
28#else // defined(_MSC_VER)
29
30#define FORCE_INLINE inline __attribute__((always_inline))
31
32inline uint32_t rotl32(uint32_t x, int8_t r) {
33 return (x << r) | (x >> (32 - r));
34}
35
36inline uint64_t rotl64(uint64_t x, int8_t r) {
37 return (x << r) | (x >> (64 - r));
38}
39
40#define ROTL32(x, y) rotl32(x, y)
41#define ROTL64(x, y) rotl64(x, y)
42
43#define BIG_CONSTANT(x) (x##LLU)
44
45#endif // !defined(_MSC_VER)
46
47//-----------------------------------------------------------------------------
48// Block read - if your platform needs to do endian-swapping or can only
49// handle aligned reads, do the conversion here
50
51FORCE_INLINE uint32_t getblock32(const uint32_t *p, int i) {
52 return p[i];
53}
54
55FORCE_INLINE uint64_t getblock64(const uint64_t *p, int i) {
56 return p[i];
57}
58
59//-----------------------------------------------------------------------------
60// Finalization mix - force all bits of a hash block to avalanche
61
62FORCE_INLINE uint32_t fmix32(uint32_t h) {
63 h ^= h >> 16;
64 h *= 0x85ebca6b;
65 h ^= h >> 13;
66 h *= 0xc2b2ae35;
67 h ^= h >> 16;
68
69 return h;
70}
71
72//----------
73
74FORCE_INLINE uint64_t fmix64(uint64_t k) {
75 k ^= k >> 33;
76 k *= BIG_CONSTANT(0xff51afd7ed558ccd);
77 k ^= k >> 33;
78 k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53);
79 k ^= k >> 33;
80
81 return k;
82}
83
84//-----------------------------------------------------------------------------
85
86void MurmurHash3_x86_32(const void *key, int len,
87 uint32_t seed, void *out) {
88 const uint8_t *data = (const uint8_t *)key;
89 const int nblocks = len / 4;
90
91 uint32_t h1 = seed;
92
93 const uint32_t c1 = 0xcc9e2d51;
94 const uint32_t c2 = 0x1b873593;
95
96 //----------
97 // body
98
99 const uint32_t *blocks = (const uint32_t *)(data + nblocks * 4);
100
101 for (int i = -nblocks; i; i++) {
102 uint32_t k1 = getblock32(blocks, i);
103
104 k1 *= c1;
105 k1 = ROTL32(k1, 15);
106 k1 *= c2;
107
108 h1 ^= k1;
109 h1 = ROTL32(h1, 13);
110 h1 = h1 * 5 + 0xe6546b64;
111 }
112
113 //----------
114 // tail
115
116 const uint8_t *tail = (const uint8_t *)(data + nblocks * 4);
117
118 uint32_t k1 = 0;
119
120 switch (len & 3) {
121 case 3:
122 k1 ^= tail[2] << 16;
123 case 2:
124 k1 ^= tail[1] << 8;
125 case 1:
126 k1 ^= tail[0];
127 k1 *= c1;
128 k1 = ROTL32(k1, 15);
129 k1 *= c2;
130 h1 ^= k1;
131 };
132
133 //----------
134 // finalization
135
136 h1 ^= len;
137
138 h1 = fmix32(h1);
139
140 *(uint32_t *)out = h1;
141}
142
143//-----------------------------------------------------------------------------
144
145void MurmurHash3_x86_128(const void *key, const int len,
146 uint32_t seed, void *out) {
147 const uint8_t *data = (const uint8_t *)key;
148 const int nblocks = len / 16;
149
150 uint32_t h1 = seed;
151 uint32_t h2 = seed;
152 uint32_t h3 = seed;
153 uint32_t h4 = seed;
154
155 const uint32_t c1 = 0x239b961b;
156 const uint32_t c2 = 0xab0e9789;
157 const uint32_t c3 = 0x38b34ae5;
158 const uint32_t c4 = 0xa1e38b93;
159
160 //----------
161 // body
162
163 const uint32_t *blocks = (const uint32_t *)(data + nblocks * 16);
164
165 for (int i = -nblocks; i; i++) {
166 uint32_t k1 = getblock32(blocks, i * 4 + 0);
167 uint32_t k2 = getblock32(blocks, i * 4 + 1);
168 uint32_t k3 = getblock32(blocks, i * 4 + 2);
169 uint32_t k4 = getblock32(blocks, i * 4 + 3);
170
171 k1 *= c1;
172 k1 = ROTL32(k1, 15);
173 k1 *= c2;
174 h1 ^= k1;
175
176 h1 = ROTL32(h1, 19);
177 h1 += h2;
178 h1 = h1 * 5 + 0x561ccd1b;
179
180 k2 *= c2;
181 k2 = ROTL32(k2, 16);
182 k2 *= c3;
183 h2 ^= k2;
184
185 h2 = ROTL32(h2, 17);
186 h2 += h3;
187 h2 = h2 * 5 + 0x0bcaa747;
188
189 k3 *= c3;
190 k3 = ROTL32(k3, 17);
191 k3 *= c4;
192 h3 ^= k3;
193
194 h3 = ROTL32(h3, 15);
195 h3 += h4;
196 h3 = h3 * 5 + 0x96cd1c35;
197
198 k4 *= c4;
199 k4 = ROTL32(k4, 18);
200 k4 *= c1;
201 h4 ^= k4;
202
203 h4 = ROTL32(h4, 13);
204 h4 += h1;
205 h4 = h4 * 5 + 0x32ac3b17;
206 }
207
208 //----------
209 // tail
210
211 const uint8_t *tail = (const uint8_t *)(data + nblocks * 16);
212
213 uint32_t k1 = 0;
214 uint32_t k2 = 0;
215 uint32_t k3 = 0;
216 uint32_t k4 = 0;
217
218 switch (len & 15) {
219 case 15:
220 k4 ^= tail[14] << 16;
221 case 14:
222 k4 ^= tail[13] << 8;
223 case 13:
224 k4 ^= tail[12] << 0;
225 k4 *= c4;
226 k4 = ROTL32(k4, 18);
227 k4 *= c1;
228 h4 ^= k4;
229
230 case 12:
231 k3 ^= tail[11] << 24;
232 case 11:
233 k3 ^= tail[10] << 16;
234 case 10:
235 k3 ^= tail[9] << 8;
236 case 9:
237 k3 ^= tail[8] << 0;
238 k3 *= c3;
239 k3 = ROTL32(k3, 17);
240 k3 *= c4;
241 h3 ^= k3;
242
243 case 8:
244 k2 ^= tail[7] << 24;
245 case 7:
246 k2 ^= tail[6] << 16;
247 case 6:
248 k2 ^= tail[5] << 8;
249 case 5:
250 k2 ^= tail[4] << 0;
251 k2 *= c2;
252 k2 = ROTL32(k2, 16);
253 k2 *= c3;
254 h2 ^= k2;
255
256 case 4:
257 k1 ^= tail[3] << 24;
258 case 3:
259 k1 ^= tail[2] << 16;
260 case 2:
261 k1 ^= tail[1] << 8;
262 case 1:
263 k1 ^= tail[0] << 0;
264 k1 *= c1;
265 k1 = ROTL32(k1, 15);
266 k1 *= c2;
267 h1 ^= k1;
268 };
269
270 //----------
271 // finalization
272
273 h1 ^= len;
274 h2 ^= len;
275 h3 ^= len;
276 h4 ^= len;
277
278 h1 += h2;
279 h1 += h3;
280 h1 += h4;
281 h2 += h1;
282 h3 += h1;
283 h4 += h1;
284
285 h1 = fmix32(h1);
286 h2 = fmix32(h2);
287 h3 = fmix32(h3);
288 h4 = fmix32(h4);
289
290 h1 += h2;
291 h1 += h3;
292 h1 += h4;
293 h2 += h1;
294 h3 += h1;
295 h4 += h1;
296
297 ((uint32_t *)out)[0] = h1;
298 ((uint32_t *)out)[1] = h2;
299 ((uint32_t *)out)[2] = h3;
300 ((uint32_t *)out)[3] = h4;
301}
302
303//-----------------------------------------------------------------------------
304
305void MurmurHash3_x64_128(const void *key, const int len,
306 const uint32_t seed, void *out) {
307 const uint8_t *data = (const uint8_t *)key;
308 const int nblocks = len / 16;
309
310 uint64_t h1 = seed;
311 uint64_t h2 = seed;
312
313 const uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5);
314 const uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f);
315
316 //----------
317 // body
318
319 const uint64_t *blocks = (const uint64_t *)(data);
320
321 for (int i = 0; i < nblocks; i++) {
322 uint64_t k1 = getblock64(blocks, i * 2 + 0);
323 uint64_t k2 = getblock64(blocks, i * 2 + 1);
324
325 k1 *= c1;
326 k1 = ROTL64(k1, 31);
327 k1 *= c2;
328 h1 ^= k1;
329
330 h1 = ROTL64(h1, 27);
331 h1 += h2;
332 h1 = h1 * 5 + 0x52dce729;
333
334 k2 *= c2;
335 k2 = ROTL64(k2, 33);
336 k2 *= c1;
337 h2 ^= k2;
338
339 h2 = ROTL64(h2, 31);
340 h2 += h1;
341 h2 = h2 * 5 + 0x38495ab5;
342 }
343
344 //----------
345 // tail
346
347 const uint8_t *tail = (const uint8_t *)(data + nblocks * 16);
348
349 uint64_t k1 = 0;
350 uint64_t k2 = 0;
351
352 switch (len & 15) {
353 case 15:
354 k2 ^= ((uint64_t)tail[14]) << 48;
355 case 14:
356 k2 ^= ((uint64_t)tail[13]) << 40;
357 case 13:
358 k2 ^= ((uint64_t)tail[12]) << 32;
359 case 12:
360 k2 ^= ((uint64_t)tail[11]) << 24;
361 case 11:
362 k2 ^= ((uint64_t)tail[10]) << 16;
363 case 10:
364 k2 ^= ((uint64_t)tail[9]) << 8;
365 case 9:
366 k2 ^= ((uint64_t)tail[8]) << 0;
367 k2 *= c2;
368 k2 = ROTL64(k2, 33);
369 k2 *= c1;
370 h2 ^= k2;
371
372 case 8:
373 k1 ^= ((uint64_t)tail[7]) << 56;
374 case 7:
375 k1 ^= ((uint64_t)tail[6]) << 48;
376 case 6:
377 k1 ^= ((uint64_t)tail[5]) << 40;
378 case 5:
379 k1 ^= ((uint64_t)tail[4]) << 32;
380 case 4:
381 k1 ^= ((uint64_t)tail[3]) << 24;
382 case 3:
383 k1 ^= ((uint64_t)tail[2]) << 16;
384 case 2:
385 k1 ^= ((uint64_t)tail[1]) << 8;
386 case 1:
387 k1 ^= ((uint64_t)tail[0]) << 0;
388 k1 *= c1;
389 k1 = ROTL64(k1, 31);
390 k1 *= c2;
391 h1 ^= k1;
392 };
393
394 //----------
395 // finalization
396
397 h1 ^= len;
398 h2 ^= len;
399
400 h1 += h2;
401 h2 += h1;
402
403 h1 = fmix64(h1);
404 h2 = fmix64(h2);
405
406 h1 += h2;
407 h2 += h1;
408
409 ((uint64_t *)out)[0] = h1;
410 ((uint64_t *)out)[1] = h2;
411}
412
413//-----------------------------------------------------------------------------