-
Notifications
You must be signed in to change notification settings - Fork 8
/
base_hash.hpp
228 lines (182 loc) · 6.52 KB
/
base_hash.hpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#pragma once
#include <cstdint>
#include <tuple>
#include <algorithm>
#include <cstring>
#include "common.hpp"
namespace emphf {
inline uint64_t unaligned_load64(uint8_t const* from)
{
uint64_t tmp;
memcpy(reinterpret_cast<char*>(&tmp), from, 8);
// XXX(ot): reverse bytes in big-endian architectures
return tmp;
}
struct jenkins64_hasher {
typedef uint64_t seed_t;
typedef uint64_t hash_t;
typedef std::tuple<hash_t, hash_t, hash_t> hash_triple_t;
jenkins64_hasher()
{}
jenkins64_hasher(uint64_t seed)
: m_seed(seed)
{}
template <typename Rng>
static jenkins64_hasher generate(Rng& rng)
{
return jenkins64_hasher(rng());
}
// Adapted from http://www.burtleburtle.net/bob/c/lookup8.c
hash_triple_t operator()(byte_range_t s) const
{
using std::get;
hash_triple_t h(m_seed, m_seed, 0x9e3779b97f4a7c13ULL);
size_t len = (size_t)(s.second - s.first);
uint8_t const* cur = s.first;
uint8_t const* end = s.second;
while (end - cur >= 24) {
get<0>(h) += unaligned_load64(cur);
cur += 8;
get<1>(h) += unaligned_load64(cur);
cur += 8;
get<2>(h) += unaligned_load64(cur);
cur += 8;
mix(h);
}
get<2>(h) += len;
switch (end - cur) {
case 23: get<2>(h) += (uint64_t(cur[22]) << 56);
case 22: get<2>(h) += (uint64_t(cur[21]) << 48);
case 21: get<2>(h) += (uint64_t(cur[20]) << 40);
case 20: get<2>(h) += (uint64_t(cur[19]) << 32);
case 19: get<2>(h) += (uint64_t(cur[18]) << 24);
case 18: get<2>(h) += (uint64_t(cur[17]) << 16);
case 17: get<2>(h) += (uint64_t(cur[16]) << 8);
// the first byte of c is reserved for the length
case 16: get<1>(h) += (uint64_t(cur[15]) << 56);
case 15: get<1>(h) += (uint64_t(cur[14]) << 48);
case 14: get<1>(h) += (uint64_t(cur[13]) << 40);
case 13: get<1>(h) += (uint64_t(cur[12]) << 32);
case 12: get<1>(h) += (uint64_t(cur[11]) << 24);
case 11: get<1>(h) += (uint64_t(cur[10]) << 16);
case 10: get<1>(h) += (uint64_t(cur[ 9]) << 8);
case 9: get<1>(h) += (uint64_t(cur[ 8]));
case 8: get<0>(h) += (uint64_t(cur[ 7]) << 56);
case 7: get<0>(h) += (uint64_t(cur[ 6]) << 48);
case 6: get<0>(h) += (uint64_t(cur[ 5]) << 40);
case 5: get<0>(h) += (uint64_t(cur[ 4]) << 32);
case 4: get<0>(h) += (uint64_t(cur[ 3]) << 24);
case 3: get<0>(h) += (uint64_t(cur[ 2]) << 16);
case 2: get<0>(h) += (uint64_t(cur[ 1]) << 8);
case 1: get<0>(h) += (uint64_t(cur[ 0]));
case 0: break; // nothing to add
default: assert(false);
}
mix(h);
return h;
}
// rehash a hash triple
hash_triple_t operator()(hash_triple_t h) const
{
std::get<0>(h) += m_seed;
std::get<1>(h) += m_seed;
std::get<2>(h) += 0x9e3779b97f4a7c13ULL;
mix(h);
return h;
}
void swap(jenkins64_hasher& other)
{
std::swap(m_seed, other.m_seed);
}
void save(std::ostream& os) const
{
os.write(reinterpret_cast<char const*>(&m_seed), sizeof(m_seed));
}
void load(std::istream& is)
{
is.read(reinterpret_cast<char*>(&m_seed), sizeof(m_seed));
}
seed_t seed() const
{
return m_seed;
}
protected:
static void mix(hash_triple_t& h)
{
uint64_t& a = std::get<0>(h);
uint64_t& b = std::get<1>(h);
uint64_t& c = std::get<2>(h);
a -= b; a -= c; a ^= (c >> 43);
b -= c; b -= a; b ^= (a << 9);
c -= a; c -= b; c ^= (b >> 8);
a -= b; a -= c; a ^= (c >> 38);
b -= c; b -= a; b ^= (a << 23);
c -= a; c -= b; c ^= (b >> 5);
a -= b; a -= c; a ^= (c >> 35);
b -= c; b -= a; b ^= (a << 49);
c -= a; c -= b; c ^= (b >> 11);
a -= b; a -= c; a ^= (c >> 12);
b -= c; b -= a; b ^= (a << 18);
c -= a; c -= b; c ^= (b >> 22);
}
seed_t m_seed;
};
// This is basically a wrapper to jenkins64_hasher that uses a
// 32-bit seed and returns 32-bit hashes by truncation
struct jenkins32_hasher {
typedef uint32_t seed_t;
typedef uint32_t hash_t;
typedef std::tuple<hash_t, hash_t, hash_t> hash_triple_t;
jenkins32_hasher()
{}
jenkins32_hasher(uint32_t seed)
: m_seed(seed)
{}
template <typename Rng>
static jenkins32_hasher generate(Rng& rng)
{
return jenkins32_hasher((uint32_t)rng());
}
hash_triple_t operator()(byte_range_t s) const
{
using std::get;
auto h64 = jenkins64_hasher(seed64())(s);
return hash_triple_t((uint32_t)get<0>(h64),
(uint32_t)get<1>(h64),
(uint32_t)get<2>(h64));
}
hash_triple_t operator()(hash_triple_t h) const
{
using std::get;
auto h64 = jenkins64_hasher::hash_triple_t(get<0>(h),
get<1>(h),
get<2>(h));
h64 = jenkins64_hasher(seed64())(h64);
return hash_triple_t((uint32_t)get<0>(h64),
(uint32_t)get<1>(h64),
(uint32_t)get<2>(h64));
}
void swap(jenkins32_hasher& other)
{
std::swap(m_seed, other.m_seed);
}
void save(std::ostream& os) const
{
os.write(reinterpret_cast<char const*>(&m_seed), sizeof(m_seed));
}
void load(std::istream& is)
{
is.read(reinterpret_cast<char*>(&m_seed), sizeof(m_seed));
}
seed_t seed() const
{
return m_seed;
}
protected:
uint64_t seed64() const
{
return (uint64_t(m_seed) << 32) | m_seed;
}
seed_t m_seed;
};
}