forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmurmur_hash.hh
153 lines (120 loc) · 4 KB
/
murmur_hash.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
*
* Modified by ScyllaDB
* Copyright (C) 2015-present ScyllaDB
*/
/*
* SPDX-License-Identifier: (LicenseRef-ScyllaDB-Source-Available-1.0 and Apache-2.0)
*/
#pragma once
#include <cstdint>
#include <array>
#include "bytes_fwd.hh"
/**
* This is a very fast, non-cryptographic hash suitable for general hash-based
* lookup. See http://murmurhash.googlepages.com/ (Murmur Hash 2) and
* https://code.google.com/p/smhasher/wiki/MurmurHash3.
*
* This code is not based on the original Murmur Hash C code, but rather
* a translation of Cassandra's Java version back to C.
**/
namespace utils {
namespace murmur_hash {
uint32_t hash32(bytes_view data, int32_t seed);
uint64_t hash2_64(bytes_view key, uint64_t seed);
template<typename InputIterator>
inline
uint64_t read_block(InputIterator& in) {
typename std::iterator_traits<InputIterator>::value_type tmp[8];
for (int i = 0; i < 8; ++i) {
tmp[i] = *in;
++in;
}
return ((uint64_t) tmp[0] & 0xff) + (((uint64_t) tmp[1] & 0xff) << 8) +
(((uint64_t) tmp[2] & 0xff) << 16) + (((uint64_t) tmp[3] & 0xff) << 24) +
(((uint64_t) tmp[4] & 0xff) << 32) + (((uint64_t) tmp[5] & 0xff) << 40) +
(((uint64_t) tmp[6] & 0xff) << 48) + (((uint64_t) tmp[7] & 0xff) << 56);
}
inline
uint64_t fmix(uint64_t k) {
k ^= (uint64_t)k >> 33;
k *= 0xff51afd7ed558ccdL;
k ^= (uint64_t)k >> 33;
k *= 0xc4ceb9fe1a85ec53L;
k ^= (uint64_t)k >> 33;
return k;
}
template <typename InputIterator>
void hash3_x64_128(InputIterator in, uint32_t length, uint64_t seed, std::array<uint64_t, 2>& result) {
const uint32_t nblocks = length >> 4; // Process as 128-bit blocks.
uint64_t h1 = seed;
uint64_t h2 = seed;
uint64_t c1 = 0x87c37b91114253d5L;
uint64_t c2 = 0x4cf5ad432745937fL;
//----------
// body
for(uint32_t i = 0; i < nblocks; i++)
{
uint64_t k1 = read_block(in);
uint64_t k2 = read_block(in);
k1 *= c1; k1 = std::rotl(k1,31); k1 *= c2; h1 ^= k1;
h1 = std::rotl(h1,27); h1 += h2; h1 = h1*5+0x52dce729;
k2 *= c2; k2 = std::rotl(k2,33); k2 *= c1; h2 ^= k2;
h2 = std::rotl(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;
}
//----------
// tail
uint64_t k1 = 0;
uint64_t k2 = 0;
typename std::iterator_traits<InputIterator>::value_type tmp[15];
std::copy_n(in, length & 15, tmp);
switch (length & 15)
{
case 15: k2 ^= ((uint64_t) tmp[14]) << 48;
[[fallthrough]];
case 14: k2 ^= ((uint64_t) tmp[13]) << 40;
[[fallthrough]];
case 13: k2 ^= ((uint64_t) tmp[12]) << 32;
[[fallthrough]];
case 12: k2 ^= ((uint64_t) tmp[11]) << 24;
[[fallthrough]];
case 11: k2 ^= ((uint64_t) tmp[10]) << 16;
[[fallthrough]];
case 10: k2 ^= ((uint64_t) tmp[9]) << 8;
[[fallthrough]];
case 9: k2 ^= ((uint64_t) tmp[8]) << 0;
k2 *= c2; k2 = std::rotl(k2,33); k2 *= c1; h2 ^= k2;
[[fallthrough]];
case 8: k1 ^= ((uint64_t) tmp[7]) << 56;
[[fallthrough]];
case 7: k1 ^= ((uint64_t) tmp[6]) << 48;
[[fallthrough]];
case 6: k1 ^= ((uint64_t) tmp[5]) << 40;
[[fallthrough]];
case 5: k1 ^= ((uint64_t) tmp[4]) << 32;
[[fallthrough]];
case 4: k1 ^= ((uint64_t) tmp[3]) << 24;
[[fallthrough]];
case 3: k1 ^= ((uint64_t) tmp[2]) << 16;
[[fallthrough]];
case 2: k1 ^= ((uint64_t) tmp[1]) << 8;
[[fallthrough]];
case 1: k1 ^= ((uint64_t) tmp[0]);
k1 *= c1; k1 = std::rotl(k1,31); k1 *= c2; h1 ^= k1;
};
//----------
// finalization
h1 ^= length;
h2 ^= length;
h1 += h2;
h2 += h1;
h1 = fmix(h1);
h2 = fmix(h2);
h1 += h2;
h2 += h1;
result[0] = h1;
result[1] = h2;
}
void hash3_x64_128(bytes_view key, uint64_t seed, std::array<uint64_t, 2>& result);
} // namespace murmur_hash
} // namespace utils