Coverage Report

Created: 2026-06-03 14:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/hash/murmur_hash3.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
//-----------------------------------------------------------------------------
19
// MurmurHash3 was written by Austin Appleby, and is placed in the public
20
// domain. The author hereby disclaims copyright to this source code.
21
22
// Note - The x86 and x64 versions do _not_ produce the same results, as the
23
// algorithms are optimized for their respective platforms. You can still
24
// compile and run any of them on any platform, but your performance with the
25
// non-native version will be less than optimal.
26
27
#include "util/hash/murmur_hash3.h"
28
29
#if defined(_MSC_VER)
30
#include <stdlib.h>
31
#endif
32
33
#include <glog/logging.h>
34
35
#include "util/unaligned.h"
36
37
namespace doris {
38
39
#if defined(_MSC_VER)
40
41
#define FORCE_INLINE __forceinline
42
43
#define ROTL32(x, y) _rotl(x, y)
44
#define ROTL64(x, y) _rotl64(x, y)
45
46
#define BIG_CONSTANT(x) (x)
47
48
// Other compilers
49
50
#else // defined(_MSC_VER)
51
52
#define FORCE_INLINE inline __attribute__((always_inline))
53
54
53
FORCE_INLINE uint32_t rotl32(uint32_t x, int8_t r) {
55
53
    return (x << r) | (x >> (32 - r));
56
53
}
57
58
2.07M
FORCE_INLINE uint64_t rotl64(uint64_t x, int8_t r) {
59
2.07M
    return (x << r) | (x >> (64 - r));
60
2.07M
}
61
62
53
#define ROTL32(x, y) rotl32(x, y)
63
2.07M
#define ROTL64(x, y) rotl64(x, y)
64
65
2.91M
#define BIG_CONSTANT(x) (x##LLU)
66
67
#endif // !defined(_MSC_VER)
68
69
//-----------------------------------------------------------------------------
70
// Block read - if your platform needs to do endian-swapping or can only
71
// handle aligned reads, do the conversion here
72
73
21
FORCE_INLINE uint32_t getblock32(const uint32_t* p, int i) {
74
21
    return unaligned_load<uint32_t>(&p[i]);
75
21
}
76
77
912k
FORCE_INLINE uint64_t getblock64(const uint64_t* p, size_t i) {
78
912k
    return unaligned_load<uint64_t>(&p[i]);
79
912k
}
80
81
//-----------------------------------------------------------------------------
82
// Finalization mix - force all bits of a hash block to avalanche
83
84
20
FORCE_INLINE uint32_t fmix32(uint32_t h) {
85
20
    h ^= h >> 16;
86
20
    h *= 0x85ebca6b;
87
20
    h ^= h >> 13;
88
20
    h *= 0xc2b2ae35;
89
20
    h ^= h >> 16;
90
91
20
    return h;
92
20
}
93
94
//----------
95
96
728k
FORCE_INLINE uint64_t fmix64(uint64_t k) {
97
728k
    k ^= k >> 33;
98
728k
    k *= BIG_CONSTANT(0xff51afd7ed558ccd);
99
728k
    k ^= k >> 33;
100
728k
    k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53);
101
728k
    k ^= k >> 33;
102
103
728k
    return k;
104
728k
}
105
106
//-----------------------------------------------------------------------------
107
108
20
void murmur_hash3_x86_32(const void* key, int64_t len, uint32_t seed, void* out) {
109
20
    const uint8_t* data = (const uint8_t*)key;
110
20
    const int nblocks = (int)len / 4;
111
112
20
    uint32_t h1 = seed;
113
114
20
    const uint32_t c1 = 0xcc9e2d51;
115
20
    const uint32_t c2 = 0x1b873593;
116
117
    //----------
118
    // body
119
120
20
    const uint32_t* blocks = (const uint32_t*)(data + nblocks * 4);
121
122
41
    for (int i = -nblocks; i; i++) {
123
21
        uint32_t k1 = getblock32(blocks, i);
124
125
21
        k1 *= c1;
126
21
        k1 = ROTL32(k1, 15);
127
21
        k1 *= c2;
128
129
21
        h1 ^= k1;
130
21
        h1 = ROTL32(h1, 13);
131
21
        h1 = h1 * 5 + 0xe6546b64;
132
21
    }
133
134
    //----------
135
    // tail
136
137
20
    const uint8_t* tail = (const uint8_t*)(data + nblocks * 4);
138
139
20
    uint32_t k1 = 0;
140
141
20
    switch (len & 3) {
142
1
    case 3:
143
1
        k1 ^= tail[2] << 16;
144
1
        [[fallthrough]];
145
2
    case 2:
146
2
        k1 ^= tail[1] << 8;
147
2
        [[fallthrough]];
148
11
    case 1:
149
11
        k1 ^= tail[0];
150
11
        k1 *= c1;
151
11
        k1 = ROTL32(k1, 15);
152
11
        k1 *= c2;
153
11
        h1 ^= k1;
154
20
    };
155
156
    //----------
157
    // finalization
158
159
20
    h1 ^= len;
160
161
20
    h1 = fmix32(h1);
162
163
20
    *(uint32_t*)out = h1;
164
20
}
165
166
//-----------------------------------------------------------------------------
167
168
0
void murmur_hash3_x86_128(const void* key, const int len, uint32_t seed, void* out) {
169
0
    const uint8_t* data = (const uint8_t*)key;
170
0
    const int nblocks = len / 16;
171
172
0
    uint32_t h1 = seed;
173
0
    uint32_t h2 = seed;
174
0
    uint32_t h3 = seed;
175
0
    uint32_t h4 = seed;
176
177
0
    const uint32_t c1 = 0x239b961b;
178
0
    const uint32_t c2 = 0xab0e9789;
179
0
    const uint32_t c3 = 0x38b34ae5;
180
0
    const uint32_t c4 = 0xa1e38b93;
181
182
    //----------
183
    // body
184
185
0
    const uint32_t* blocks = (const uint32_t*)(data + nblocks * 16);
186
187
0
    for (int i = -nblocks; i; i++) {
188
0
        uint32_t k1 = getblock32(blocks, i * 4 + 0);
189
0
        uint32_t k2 = getblock32(blocks, i * 4 + 1);
190
0
        uint32_t k3 = getblock32(blocks, i * 4 + 2);
191
0
        uint32_t k4 = getblock32(blocks, i * 4 + 3);
192
193
0
        k1 *= c1;
194
0
        k1 = ROTL32(k1, 15);
195
0
        k1 *= c2;
196
0
        h1 ^= k1;
197
198
0
        h1 = ROTL32(h1, 19);
199
0
        h1 += h2;
200
0
        h1 = h1 * 5 + 0x561ccd1b;
201
202
0
        k2 *= c2;
203
0
        k2 = ROTL32(k2, 16);
204
0
        k2 *= c3;
205
0
        h2 ^= k2;
206
207
0
        h2 = ROTL32(h2, 17);
208
0
        h2 += h3;
209
0
        h2 = h2 * 5 + 0x0bcaa747;
210
211
0
        k3 *= c3;
212
0
        k3 = ROTL32(k3, 17);
213
0
        k3 *= c4;
214
0
        h3 ^= k3;
215
216
0
        h3 = ROTL32(h3, 15);
217
0
        h3 += h4;
218
0
        h3 = h3 * 5 + 0x96cd1c35;
219
220
0
        k4 *= c4;
221
0
        k4 = ROTL32(k4, 18);
222
0
        k4 *= c1;
223
0
        h4 ^= k4;
224
225
0
        h4 = ROTL32(h4, 13);
226
0
        h4 += h1;
227
0
        h4 = h4 * 5 + 0x32ac3b17;
228
0
    }
229
230
    //----------
231
    // tail
232
233
0
    const uint8_t* tail = (const uint8_t*)(data + nblocks * 16);
234
235
0
    uint32_t k1 = 0;
236
0
    uint32_t k2 = 0;
237
0
    uint32_t k3 = 0;
238
0
    uint32_t k4 = 0;
239
240
0
    switch (len & 15) {
241
0
    case 15:
242
0
        k4 ^= tail[14] << 16;
243
0
        [[fallthrough]];
244
0
    case 14:
245
0
        k4 ^= tail[13] << 8;
246
0
        [[fallthrough]];
247
0
    case 13:
248
0
        k4 ^= tail[12] << 0;
249
0
        k4 *= c4;
250
0
        k4 = ROTL32(k4, 18);
251
0
        k4 *= c1;
252
0
        h4 ^= k4;
253
0
        [[fallthrough]];
254
0
    case 12:
255
0
        k3 ^= tail[11] << 24;
256
0
        [[fallthrough]];
257
0
    case 11:
258
0
        k3 ^= tail[10] << 16;
259
0
        [[fallthrough]];
260
0
    case 10:
261
0
        k3 ^= tail[9] << 8;
262
0
        [[fallthrough]];
263
0
    case 9:
264
0
        k3 ^= tail[8] << 0;
265
0
        k3 *= c3;
266
0
        k3 = ROTL32(k3, 17);
267
0
        k3 *= c4;
268
0
        h3 ^= k3;
269
0
        [[fallthrough]];
270
0
    case 8:
271
0
        k2 ^= tail[7] << 24;
272
0
        [[fallthrough]];
273
0
    case 7:
274
0
        k2 ^= tail[6] << 16;
275
0
        [[fallthrough]];
276
0
    case 6:
277
0
        k2 ^= tail[5] << 8;
278
0
        [[fallthrough]];
279
0
    case 5:
280
0
        k2 ^= tail[4] << 0;
281
0
        k2 *= c2;
282
0
        k2 = ROTL32(k2, 16);
283
0
        k2 *= c3;
284
0
        h2 ^= k2;
285
0
        [[fallthrough]];
286
0
    case 4:
287
0
        k1 ^= tail[3] << 24;
288
0
        [[fallthrough]];
289
0
    case 3:
290
0
        k1 ^= tail[2] << 16;
291
0
        [[fallthrough]];
292
0
    case 2:
293
0
        k1 ^= tail[1] << 8;
294
0
        [[fallthrough]];
295
0
    case 1:
296
0
        k1 ^= tail[0] << 0;
297
0
        k1 *= c1;
298
0
        k1 = ROTL32(k1, 15);
299
0
        k1 *= c2;
300
0
        h1 ^= k1;
301
0
    };
302
303
    //----------
304
    // finalization
305
306
0
    h1 ^= len;
307
0
    h2 ^= len;
308
0
    h3 ^= len;
309
0
    h4 ^= len;
310
311
0
    h1 += h2;
312
0
    h1 += h3;
313
0
    h1 += h4;
314
0
    h2 += h1;
315
0
    h3 += h1;
316
0
    h4 += h1;
317
318
0
    h1 = fmix32(h1);
319
0
    h2 = fmix32(h2);
320
0
    h3 = fmix32(h3);
321
0
    h4 = fmix32(h4);
322
323
0
    h1 += h2;
324
0
    h1 += h3;
325
0
    h1 += h4;
326
0
    h2 += h1;
327
0
    h3 += h1;
328
0
    h4 += h1;
329
330
0
    ((uint32_t*)out)[0] = h1;
331
0
    ((uint32_t*)out)[1] = h2;
332
0
    ((uint32_t*)out)[2] = h3;
333
0
    ((uint32_t*)out)[3] = h4;
334
0
}
335
336
//-----------------------------------------------------------------------------
337
338
// Helper function that implements the core MurmurHash3 128-bit hashing algorithm
339
114
void murmur_hash3_x64_process(const void* key, const size_t len, uint64_t& h1, uint64_t& h2) {
340
114
    const uint8_t* data = (const uint8_t*)key;
341
114
    const size_t nblocks = len / 16;
342
343
114
    const uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5);
344
114
    const uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f);
345
346
    //----------
347
    // body
348
349
114
    const uint64_t* blocks = (const uint64_t*)(data);
350
351
115
    for (size_t i = 0; i < nblocks; i++) {
352
1
        uint64_t k1 = getblock64(blocks, i * 2 + 0);
353
1
        uint64_t k2 = getblock64(blocks, i * 2 + 1);
354
355
1
        k1 *= c1;
356
1
        k1 = ROTL64(k1, 31);
357
1
        k1 *= c2;
358
1
        h1 ^= k1;
359
360
1
        h1 = ROTL64(h1, 27);
361
1
        h1 += h2;
362
1
        h1 = h1 * 5 + 0x52dce729;
363
364
1
        k2 *= c2;
365
1
        k2 = ROTL64(k2, 33);
366
1
        k2 *= c1;
367
1
        h2 ^= k2;
368
369
1
        h2 = ROTL64(h2, 31);
370
1
        h2 += h1;
371
1
        h2 = h2 * 5 + 0x38495ab5;
372
1
    }
373
374
    //----------
375
    // tail
376
377
114
    const uint8_t* tail = (const uint8_t*)(data + nblocks * 16);
378
379
114
    uint64_t k1 = 0;
380
114
    uint64_t k2 = 0;
381
382
114
    switch (len & 15) {
383
0
    case 15:
384
0
        k2 ^= ((uint64_t)tail[14]) << 48;
385
0
        [[fallthrough]];
386
0
    case 14:
387
0
        k2 ^= ((uint64_t)tail[13]) << 40;
388
0
        [[fallthrough]];
389
0
    case 13:
390
0
        k2 ^= ((uint64_t)tail[12]) << 32;
391
0
        [[fallthrough]];
392
1
    case 12:
393
1
        k2 ^= ((uint64_t)tail[11]) << 24;
394
1
        [[fallthrough]];
395
13
    case 11:
396
13
        k2 ^= ((uint64_t)tail[10]) << 16;
397
13
        [[fallthrough]];
398
13
    case 10:
399
13
        k2 ^= ((uint64_t)tail[9]) << 8;
400
13
        [[fallthrough]];
401
13
    case 9:
402
13
        k2 ^= ((uint64_t)tail[8]) << 0;
403
13
        k2 *= c2;
404
13
        k2 = ROTL64(k2, 33);
405
13
        k2 *= c1;
406
13
        h2 ^= k2;
407
13
        [[fallthrough]];
408
13
    case 8:
409
13
        k1 ^= ((uint64_t)tail[7]) << 56;
410
13
        [[fallthrough]];
411
13
    case 7:
412
13
        k1 ^= ((uint64_t)tail[6]) << 48;
413
13
        [[fallthrough]];
414
13
    case 6:
415
13
        k1 ^= ((uint64_t)tail[5]) << 40;
416
13
        [[fallthrough]];
417
82
    case 5:
418
82
        k1 ^= ((uint64_t)tail[4]) << 32;
419
82
        [[fallthrough]];
420
82
    case 4:
421
82
        k1 ^= ((uint64_t)tail[3]) << 24;
422
82
        [[fallthrough]];
423
82
    case 3:
424
82
        k1 ^= ((uint64_t)tail[2]) << 16;
425
82
        [[fallthrough]];
426
82
    case 2:
427
82
        k1 ^= ((uint64_t)tail[1]) << 8;
428
82
        [[fallthrough]];
429
100
    case 1:
430
100
        k1 ^= ((uint64_t)tail[0]) << 0;
431
100
        k1 *= c1;
432
100
        k1 = ROTL64(k1, 31);
433
100
        k1 *= c2;
434
100
        h1 ^= k1;
435
114
    };
436
437
    //----------
438
    // finalization
439
440
114
    h1 ^= len;
441
114
    h2 ^= len;
442
443
114
    h1 += h2;
444
114
    h2 += h1;
445
446
114
    h1 = fmix64(h1);
447
114
    h2 = fmix64(h2);
448
449
114
    h1 += h2;
450
114
    h2 += h1;
451
114
}
452
453
//-----------------------------------------------------------------------------
454
455
// The origin function `murmur_hash3_x64_128` is copied from: https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp
456
// And Doris modified it into function `murmur_hash3_x64_process`
457
// For this reason, this function is still retained even though it has no calls.
458
47
void murmur_hash3_x64_128(const void* key, const size_t len, const uint32_t seed, void* out) {
459
47
    uint64_t h1 = seed;
460
47
    uint64_t h2 = seed;
461
47
    murmur_hash3_x64_process(key, len, h1, h2);
462
47
    ((uint64_t*)out)[0] = h1;
463
47
    ((uint64_t*)out)[1] = h2;
464
47
}
465
466
//-----------------------------------------------------------------------------
467
468
// MurmurHash3 x64 64-bit variant using shared 128-bit processing function
469
// This implementation reuses the murmur_hash3_x64_process function and only outputs the first hash value
470
// Used for function mmh3_64_v2
471
void murmur_hash3_x64_64_shared(const void* key, const int64_t len, const uint64_t seed,
472
4
                                void* out) {
473
4
    DCHECK_GE(len, 0);
474
4
    uint64_t h1 = seed;
475
4
    uint64_t h2 = seed;
476
4
    murmur_hash3_x64_process(key, static_cast<size_t>(len), h1, h2);
477
4
    ((uint64_t*)out)[0] = h1;
478
4
}
479
480
//-----------------------------------------------------------------------------
481
482
// MurmurHash3 x64 64-bit variant with optimized standalone implementation
483
// This implementation is specifically optimized for 64-bit output
484
// Used for function mmh3_64
485
727k
void murmur_hash3_x64_64(const void* key, const int64_t len, const uint64_t seed, void* out) {
486
727k
    const uint8_t* data = (const uint8_t*)key;
487
727k
    const int nblocks = (int)len / 8;
488
727k
    uint64_t h1 = seed;
489
490
727k
    const uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5);
491
727k
    const uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f);
492
493
    //----------
494
    // body
495
496
727k
    const uint64_t* blocks = (const uint64_t*)(data);
497
498
1.63M
    for (int i = 0; i < nblocks; i++) {
499
912k
        uint64_t k1 = getblock64(blocks, i);
500
501
912k
        k1 *= c1;
502
912k
        k1 = ROTL64(k1, 31);
503
912k
        k1 *= c2;
504
912k
        h1 ^= k1;
505
506
912k
        h1 = ROTL64(h1, 27);
507
912k
        h1 = h1 * 5 + 0x52dce729;
508
912k
    }
509
510
    //----------
511
    // tail
512
513
727k
    const uint8_t* tail = (const uint8_t*)(data + nblocks * 8);
514
727k
    uint64_t k1 = 0;
515
516
727k
    switch (len & 7) {
517
9.97k
    case 7:
518
9.97k
        k1 ^= ((uint64_t)tail[6]) << 48;
519
9.97k
        [[fallthrough]];
520
11.0k
    case 6:
521
11.0k
        k1 ^= ((uint64_t)tail[5]) << 40;
522
11.0k
        [[fallthrough]];
523
11.4k
    case 5:
524
11.4k
        k1 ^= ((uint64_t)tail[4]) << 32;
525
11.4k
        [[fallthrough]];
526
90.1k
    case 4:
527
90.1k
        k1 ^= ((uint64_t)tail[3]) << 24;
528
90.1k
        [[fallthrough]];
529
102k
    case 3:
530
102k
        k1 ^= ((uint64_t)tail[2]) << 16;
531
102k
        [[fallthrough]];
532
239k
    case 2:
533
239k
        k1 ^= ((uint64_t)tail[1]) << 8;
534
239k
        [[fallthrough]];
535
245k
    case 1:
536
245k
        k1 ^= ((uint64_t)tail[0]) << 0;
537
245k
        k1 *= c1;
538
245k
        k1 = ROTL64(k1, 31);
539
245k
        k1 *= c2;
540
245k
        h1 ^= k1;
541
727k
    };
542
543
    //----------
544
    // finalization
545
546
727k
    h1 ^= len;
547
727k
    h1 = fmix64(h1);
548
549
727k
    ((uint64_t*)out)[0] = h1;
550
727k
}
551
552
} // namespace doris