-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.cpp
More file actions
419 lines (357 loc) · 9.27 KB
/
hash.cpp
File metadata and controls
419 lines (357 loc) · 9.27 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include "hash.h"
#include "util.h"
namespace hard_core {
namespace {
/* Fast tolower() alike function that does not care about locale
* but just returns a-z instead of A-Z. */
int siptlw(int c) {
if (c >= 'A' && c <= 'Z') {
return c + ('a' - 'A');
} else {
return c;
}
}
/* Test of the CPU is Little Endian and supports not aligned accesses.
* Two interesting conditions to speedup the function that happen to be
* in most of x86 servers. */
#if defined(__X86_64__) || defined(__x86_64__) || defined(__i386__) || \
defined(__aarch64__) || defined(__arm64__)
#define UNALIGNED_LE_CPU
#endif
#define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))
#define U32TO8_LE(p, v) \
(p)[0] = (uint8_t)((v)); \
(p)[1] = (uint8_t)((v) >> 8); \
(p)[2] = (uint8_t)((v) >> 16); \
(p)[3] = (uint8_t)((v) >> 24);
#define U64TO8_LE(p, v) \
U32TO8_LE((p), (uint32_t)((v))); \
U32TO8_LE((p) + 4, (uint32_t)((v) >> 32));
#ifdef UNALIGNED_LE_CPU
#define U8TO64_LE(p) (*((uint64_t *)(p)))
#else
#define U8TO64_LE(p) \
(((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) | \
((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) | \
((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) | \
((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56))
#endif
#define U8TO64_LE_NOCASE(p) \
(((uint64_t)(siptlw((p)[0]))) | ((uint64_t)(siptlw((p)[1])) << 8) | \
((uint64_t)(siptlw((p)[2])) << 16) | ((uint64_t)(siptlw((p)[3])) << 24) | \
((uint64_t)(siptlw((p)[4])) << 32) | ((uint64_t)(siptlw((p)[5])) << 40) | \
((uint64_t)(siptlw((p)[6])) << 48) | ((uint64_t)(siptlw((p)[7])) << 56))
#define SIPROUND \
do { \
v0 += v1; \
v1 = ROTL(v1, 13); \
v1 ^= v0; \
v0 = ROTL(v0, 32); \
v2 += v3; \
v3 = ROTL(v3, 16); \
v3 ^= v2; \
v0 += v3; \
v3 = ROTL(v3, 21); \
v3 ^= v0; \
v2 += v1; \
v1 = ROTL(v1, 17); \
v1 ^= v2; \
v2 = ROTL(v2, 32); \
} while (0)
} // namespace
uint32_t HashFunc::SimMurMurHash(const char *data, uint32_t len) {
uint32_t seed = 0xbc9f1d34;
// Similar to murmur hash
const uint32_t m = 0xc6a4a793;
const uint32_t r = 24;
const char *limit = data + len;
uint32_t h = seed ^ (len * m);
// Pick up four bytes at a time
while (data + 4 <= limit) {
uint32_t w = DecodeFixed32(data);
data += 4;
h += w;
h *= m;
h ^= (h >> 16);
}
switch (limit - data) {
case 3:
h += static_cast<uint8_t>(data[2]) << 16;
case 2:
h += static_cast<uint8_t>(data[1]) << 8;
case 1:
h += static_cast<uint8_t>(data[0]);
h *= m;
h ^= (h >> r);
break;
}
return h;
}
uint32_t HashFunc::MurMurHash2(const char *data, uint32_t len) {
uint32_t seed = 5381;
/* 'm' and 'r' are mixing constants generated offline.
They're not really 'magic', they just happen to work well. */
const uint32_t m = 0x5bd1e995;
const int32_t r = 24;
/* Initialize the hash to a 'random' value */
uint32_t h = seed ^ len;
while (len >= 4) {
uint32_t k = *(uint32_t *)data;
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
data += 4;
len -= 4;
}
/* Handle the last few bytes of the input array */
switch (len) {
case 3:
h ^= data[2] << 16;
case 2:
h ^= data[1] << 8;
case 1:
h ^= data[0];
h *= m;
};
/* Do a few final mixes of the hash to ensure the last few
* bytes are well-incorporated. */
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h;
}
uint32_t HashFunc::DJB(const char *data, uint32_t len) {
uint32_t seed = 5381;
uint32_t h = seed;
while (len--) h = ((h << 5) + h) + (tolower(*data++)); /* hash * 33 + c */
return h;
}
uint32_t HashFunc::SipHash(const uint8_t *in, uint32_t inlen,
const uint8_t *k) {
#ifndef UNALIGNED_LE_CPU
uint64_t hash;
uint8_t *out = (uint8_t *)&hash;
#endif
uint64_t v0 = 0x736f6d6570736575ULL;
uint64_t v1 = 0x646f72616e646f6dULL;
uint64_t v2 = 0x6c7967656e657261ULL;
uint64_t v3 = 0x7465646279746573ULL;
uint64_t k0 = U8TO64_LE(k);
uint64_t k1 = U8TO64_LE(k + 8);
uint64_t m;
const uint8_t *end = in + inlen - (inlen % sizeof(uint64_t));
const int left = inlen & 7;
uint64_t b = ((uint64_t)inlen) << 56;
v3 ^= k1;
v2 ^= k0;
v1 ^= k1;
v0 ^= k0;
for (; in != end; in += 8) {
m = U8TO64_LE(in);
v3 ^= m;
SIPROUND;
v0 ^= m;
}
switch (left) {
case 7:
b |= ((uint64_t)in[6]) << 48; /* fall-thru */
case 6:
b |= ((uint64_t)in[5]) << 40; /* fall-thru */
case 5:
b |= ((uint64_t)in[4]) << 32; /* fall-thru */
case 4:
b |= ((uint64_t)in[3]) << 24; /* fall-thru */
case 3:
b |= ((uint64_t)in[2]) << 16; /* fall-thru */
case 2:
b |= ((uint64_t)in[1]) << 8; /* fall-thru */
case 1:
b |= ((uint64_t)in[0]);
break;
case 0:
break;
}
v3 ^= b;
SIPROUND;
v0 ^= b;
v2 ^= 0xff;
SIPROUND;
SIPROUND;
b = v0 ^ v1 ^ v2 ^ v3;
#ifndef UNALIGNED_LE_CPU
U64TO8_LE(out, b);
return hash;
#else
return b;
#endif
}
uint32_t HashFunc::SipHashNoCase(const uint8_t *in, uint32_t inlen,
const uint8_t *k) {
#ifndef UNALIGNED_LE_CPU
uint64_t hash;
uint8_t *out = (uint8_t *)&hash;
#endif
uint64_t v0 = 0x736f6d6570736575ULL;
uint64_t v1 = 0x646f72616e646f6dULL;
uint64_t v2 = 0x6c7967656e657261ULL;
uint64_t v3 = 0x7465646279746573ULL;
uint64_t k0 = U8TO64_LE(k);
uint64_t k1 = U8TO64_LE(k + 8);
uint64_t m;
const uint8_t *end = in + inlen - (inlen % sizeof(uint64_t));
const int left = inlen & 7;
uint64_t b = ((uint64_t)inlen) << 56;
v3 ^= k1;
v2 ^= k0;
v1 ^= k1;
v0 ^= k0;
for (; in != end; in += 8) {
m = U8TO64_LE_NOCASE(in);
v3 ^= m;
SIPROUND;
v0 ^= m;
}
switch (left) {
case 7:
b |= ((uint64_t)siptlw(in[6])) << 48; /* fall-thru */
case 6:
b |= ((uint64_t)siptlw(in[5])) << 40; /* fall-thru */
case 5:
b |= ((uint64_t)siptlw(in[4])) << 32; /* fall-thru */
case 4:
b |= ((uint64_t)siptlw(in[3])) << 24; /* fall-thru */
case 3:
b |= ((uint64_t)siptlw(in[2])) << 16; /* fall-thru */
case 2:
b |= ((uint64_t)siptlw(in[1])) << 8; /* fall-thru */
case 1:
b |= ((uint64_t)siptlw(in[0]));
break;
case 0:
break;
}
v3 ^= b;
SIPROUND;
v0 ^= b;
v2 ^= 0xff;
SIPROUND;
SIPROUND;
b = v0 ^ v1 ^ v2 ^ v3;
#ifndef UNALIGNED_LE_CPU
U64TO8_LE(out, b);
return hash;
#else
return b;
#endif
}
uint32_t HashFunc::PJW(const char *data, uint32_t len) {
uint32_t h = 0, g = 0;
while (len--) {
h = (h << 4) + *data++;
if ((g = (h & 0xF0000000))) {
h = h ^ (g >> 24);
h = h ^ g;
}
}
return h;
}
uint32_t HashFunc::CalcNrHash(const char *data, uint32_t len) {
uint32_t h = 1, g = 4;
while (len--) {
h ^= (((h & 63) + g) * ((uint)*data++)) + (h << 8);
g += 3;
}
return h;
}
uint32_t HashFunc::SDBMHash(const char *data, uint32_t len) {
uint32_t h = 0;
while (len--) {
h = (*data++) + (h << 6) + (h << 16) - h;
}
return h;
}
uint32_t HashFunc::RSHash(const char *data, uint32_t len) {
static constexpr uint32_t b = 378551;
uint32_t a = 63689;
uint32_t hash = 0;
while (len--) {
hash = hash * a + (*data++);
a *= b;
}
return (hash & 0x7FFFFFFF);
}
// JS Hash Function
uint32_t HashFunc::JSHash(const char *data, uint32_t len) {
uint32_t hash = 1315423911;
while (len--) {
hash ^= ((hash << 5) + (*data++) + (hash >> 2));
}
return (hash & 0x7FFFFFFF);
}
// JS Hash Function
uint32_t HashFunc::ELFHash(const char *data, uint32_t len) {
uint32_t hash = 0, x = 0;
while (len--) {
hash = (hash << 4) + (*data++);
if ((x = hash & 0xF0000000L) != 0) {
hash ^= (x >> 24);
hash &= ~x;
}
}
return (hash & 0x7FFFFFFF);
}
// BKDR Hash Function
uint32_t HashFunc::BKDRHash(const char *data, uint32_t len) {
uint32_t seed = 131; // 31 131 1313 13131 131313 etc..
uint32_t hash = 0;
while (len--) {
hash = hash * seed + (*data++);
}
return (hash & 0x7FFFFFFF);
}
// BKDR Hash Function
uint32_t HashFunc::APHash(const char *data, uint32_t len) {
uint32_t hash = 0;
for (uint32_t idx = 0; idx < len; idx++) {
if ((idx & 1) == 0) {
hash ^= ((hash << 7) ^ (*data++) ^ (hash >> 3));
} else {
hash ^= (~((hash << 11) ^ (*data++) ^ (hash >> 5)));
}
}
return (hash & 0x7FFFFFFF);
}
// BKDR Hash Function
uint32_t HashFunc::DJB2Hash(const char *data, uint32_t len) {
uint32_t seed = 5381;
uint32_t h = seed;
while (len--) {
h = h * 33 ^ (*data++); /* hash * 33 + c */
}
return h;
}
uint32_t HashFunc::FNVHash(const char *data, uint32_t len) {
uint32_t hash = 2166136261;
while (len--) {
hash *= 16777619;
hash ^= (*data++);
}
return hash;
}
uint32_t HashFunc::DEKHash(const char *data, uint32_t len) {
uint32_t hash = 1315423911;
while (len--) {
hash = ((hash << 5) ^ (hash >> 27)) ^ (*data++);
}
return hash;
}
uint32_t HashFunc::BPHash(const char *data, uint32_t len) {
uint32_t hash = len;
while (len--) {
hash = (hash << 7) ^ (*data++);
}
return hash & 0x7FFFFFFF;
}
} // namespace hard_core