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
|
#include "common.h"
namespace {
#include "farmhashuo.cc"
}
namespace farmhashxo {
#undef Fetch
#define Fetch Fetch64
#undef Rotate
#define Rotate Rotate64
STATIC_INLINE uint64_t H32(const char *s, size_t len, uint64_t mul,
uint64_t seed0 = 0, uint64_t seed1 = 0) {
uint64_t a = Fetch(s) * k1;
uint64_t b = Fetch(s + 8);
uint64_t c = Fetch(s + len - 8) * mul;
uint64_t d = Fetch(s + len - 16) * k2;
uint64_t u = Rotate(a + b, 43) + Rotate(c, 30) + d + seed0;
uint64_t v = a + Rotate(b + k2, 18) + c + seed1;
a = farmhashna::ShiftMix((u ^ v) * mul);
b = farmhashna::ShiftMix((v ^ a) * mul);
return b;
}
// Return an 8-byte hash for 33 to 64 bytes.
STATIC_INLINE uint64_t HashLen33to64(const char *s, size_t len) {
uint64_t mul0 = k2 - 30;
uint64_t mul1 = k2 - 30 + 2 * len;
uint64_t h0 = H32(s, 32, mul0);
uint64_t h1 = H32(s + len - 32, 32, mul1);
return ((h1 * mul1) + h0) * mul1;
}
// Return an 8-byte hash for 65 to 96 bytes.
STATIC_INLINE uint64_t HashLen65to96(const char *s, size_t len) {
uint64_t mul0 = k2 - 114;
uint64_t mul1 = k2 - 114 + 2 * len;
uint64_t h0 = H32(s, 32, mul0);
uint64_t h1 = H32(s + 32, 32, mul1);
uint64_t h2 = H32(s + len - 32, 32, mul1, h0, h1);
return (h2 * 9 + (h0 >> 17) + (h1 >> 21)) * mul1;
}
uint64_t Hash64(const char *s, size_t len) {
if (len <= 32) {
if (len <= 16) {
return farmhashna::HashLen0to16(s, len);
} else {
return farmhashna::HashLen17to32(s, len);
}
} else if (len <= 64) {
return HashLen33to64(s, len);
} else if (len <= 96) {
return HashLen65to96(s, len);
} else if (len <= 256) {
return farmhashna::Hash64(s, len);
} else {
return farmhashuo::Hash64(s, len);
}
}
uint64_t Hash64WithSeeds(const char *s, size_t len, uint64_t seed0, uint64_t seed1) {
return farmhashuo::Hash64WithSeeds(s, len, seed0, seed1);
}
uint64_t Hash64WithSeed(const char *s, size_t len, uint64_t seed) {
return farmhashuo::Hash64WithSeed(s, len, seed);
}
} // namespace farmhashxo
|