/root/doris/be/src/gutil/hash/city.cc
Line | Count | Source |
1 | | // Copyright 2010 Google Inc. All Rights Reserved. |
2 | | // Authors: gpike@google.com (Geoff Pike), jyrki@google.com (Jyrki Alakuijala) |
3 | | // |
4 | | // This file provides CityHash64() and related functions. |
5 | | // |
6 | | // The externally visible functions follow the naming conventions of |
7 | | // hash.h, where the size of the output is part of the name. For |
8 | | // example, CityHash64 returns a 64-bit hash. The internal helpers do |
9 | | // not have the return type in their name, but instead have names like |
10 | | // HashLenXX or HashLenXXtoYY, where XX and YY are input string lengths. |
11 | | // |
12 | | // Most of the constants and tricks here were copied from murmur.cc or |
13 | | // hash.h, or discovered by trial and error. It's probably possible to further |
14 | | // optimize the code here by writing a program that systematically explores |
15 | | // more of the space of possible hash functions, or by using SIMD instructions. |
16 | | |
17 | | #include "gutil/hash/city.h" |
18 | | |
19 | | #include <algorithm> |
20 | | |
21 | | using std::copy; |
22 | | using std::max; |
23 | | using std::min; |
24 | | using std::reverse; |
25 | | using std::sort; |
26 | | using std::swap; |
27 | | #include <utility> |
28 | | |
29 | | using std::make_pair; |
30 | | using std::pair; |
31 | | |
32 | | #include "common/logging.h" |
33 | | #include "gutil/endian.h" |
34 | | |
35 | | namespace util_hash { |
36 | | |
37 | | // Some primes between 2^63 and 2^64 for various uses. |
38 | | static const uint64_t k0 = 0xa5b85c5e198ed849ULL; |
39 | | static const uint64_t k1 = 0x8d58ac26afe12e47ULL; |
40 | | static const uint64_t k2 = 0xc47b6e9e3a970ed3ULL; |
41 | | static const uint64_t k3 = 0xc70f6907e782aa0bULL; |
42 | | |
43 | | // Bitwise right rotate. Normally this will compile to a single |
44 | | // instruction, especially if the shift is a manifest constant. |
45 | 2.34k | static uint64_t Rotate(uint64_t val, int shift) { |
46 | 2.34k | DCHECK_GE(shift, 0); |
47 | 2.34k | DCHECK_LE(shift, 63); |
48 | | // Avoid shifting by 64: doing so yields an undefined result. |
49 | 2.34k | return shift == 0 ? val : ((val >> shift) | (val << (64 - shift))); |
50 | 2.34k | } |
51 | | |
52 | | // Equivalent to Rotate(), but requires the second arg to be non-zero. |
53 | | // On x86-64, and probably others, it's possible for this to compile |
54 | | // to a single instruction if both args are already in registers. |
55 | 18 | static uint64_t RotateByAtLeast1(uint64_t val, int shift) { |
56 | 18 | DCHECK_GE(shift, 1); |
57 | 18 | DCHECK_LE(shift, 63); |
58 | 18 | return (val >> shift) | (val << (64 - shift)); |
59 | 18 | } |
60 | | |
61 | 38 | static uint64_t ShiftMix(uint64_t val) { |
62 | 38 | return val ^ (val >> 47); |
63 | 38 | } |
64 | | |
65 | 401k | uint64_t HashLen16(uint64_t u, uint64_t v) { |
66 | 401k | const uint64_t kMul = 0xc6a4a7935bd1e995ULL; |
67 | 401k | uint64_t a = (u ^ v) * kMul; |
68 | 401k | a ^= (a >> 47); |
69 | 401k | uint64_t b = (u ^ a) * kMul; |
70 | 401k | b ^= (b >> 47); |
71 | 401k | b *= kMul; |
72 | 401k | return b; |
73 | 401k | } |
74 | | |
75 | 128k | static uint64_t HashLen0to16(const char* s, size_t len) { |
76 | 128k | DCHECK_GE(len, 0); |
77 | 128k | DCHECK_LE(len, 16); |
78 | 128k | if (len > 8) { |
79 | 18 | uint64_t a = LittleEndian::Load64(s); |
80 | 18 | uint64_t b = LittleEndian::Load64(s + len - 8); |
81 | 18 | return HashLen16(a, RotateByAtLeast1(b + len, len)) ^ b; |
82 | 18 | } |
83 | 128k | if (len >= 4) { |
84 | 128k | uint64_t a = LittleEndian::Load32(s); |
85 | 128k | return HashLen16(len + (a << 3), LittleEndian::Load32(s + len - 4)); |
86 | 128k | } |
87 | 18 | if (len > 0) { |
88 | 18 | uint8_t a = s[0]; |
89 | 18 | uint8_t b = s[len >> 1]; |
90 | 18 | uint8_t c = s[len - 1]; |
91 | 18 | uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8); |
92 | 18 | uint32_t z = len + (static_cast<uint32_t>(c) << 2); |
93 | 18 | return ShiftMix(y * k2 ^ z * k3) * k2; |
94 | 18 | } |
95 | 0 | return k2; |
96 | 18 | } |
97 | | |
98 | | // This probably works well for 16-byte strings as well, but it may be overkill |
99 | | // in that case. |
100 | 2 | static uint64_t HashLen17to32(const char* s, size_t len) { |
101 | 2 | DCHECK_GE(len, 17); |
102 | 2 | DCHECK_LE(len, 32); |
103 | 2 | uint64_t a = LittleEndian::Load64(s) * k1; |
104 | 2 | uint64_t b = LittleEndian::Load64(s + 8); |
105 | 2 | uint64_t c = LittleEndian::Load64(s + len - 8) * k2; |
106 | 2 | uint64_t d = LittleEndian::Load64(s + len - 16) * k0; |
107 | 2 | return HashLen16(Rotate(a - b, 43) + Rotate(c, 30) + d, a + Rotate(b ^ k3, 20) - c + len); |
108 | 2 | } |
109 | | |
110 | | // Return a 16-byte hash for 48 bytes. Quick and dirty. |
111 | | // Callers do best to use "random-looking" values for a and b. |
112 | | // (For more, see the code review discussion of CL 18799087.) |
113 | | static pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(uint64_t w, uint64_t x, uint64_t y, |
114 | 684 | uint64_t z, uint64_t a, uint64_t b) { |
115 | 684 | a += w; |
116 | 684 | b = Rotate(b + a + z, 51); |
117 | 684 | uint64_t c = a; |
118 | 684 | a += x; |
119 | 684 | a += y; |
120 | 684 | b += Rotate(a, 23); |
121 | 684 | return make_pair(a + z, b + c); |
122 | 684 | } |
123 | | |
124 | | // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty. |
125 | 684 | static pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(const char* s, uint64_t a, uint64_t b) { |
126 | 684 | return WeakHashLen32WithSeeds(LittleEndian::Load64(s), LittleEndian::Load64(s + 8), |
127 | 684 | LittleEndian::Load64(s + 16), LittleEndian::Load64(s + 24), a, b); |
128 | 684 | } |
129 | | |
130 | | // Return an 8-byte hash for 33 to 64 bytes. |
131 | 0 | static uint64_t HashLen33to64(const char* s, size_t len) { |
132 | 0 | uint64_t z = LittleEndian::Load64(s + 24); |
133 | 0 | uint64_t a = LittleEndian::Load64(s) + (len + LittleEndian::Load64(s + len - 16)) * k0; |
134 | 0 | uint64_t b = Rotate(a + z, 52); |
135 | 0 | uint64_t c = Rotate(a, 37); |
136 | 0 | a += LittleEndian::Load64(s + 8); |
137 | 0 | c += Rotate(a, 7); |
138 | 0 | a += LittleEndian::Load64(s + 16); |
139 | 0 | uint64_t vf = a + z; |
140 | 0 | uint64_t vs = b + Rotate(a, 31) + c; |
141 | 0 | a = LittleEndian::Load64(s + 16) + LittleEndian::Load64(s + len - 32); |
142 | 0 | z += LittleEndian::Load64(s + len - 8); |
143 | 0 | b = Rotate(a + z, 52); |
144 | 0 | c = Rotate(a, 37); |
145 | 0 | a += LittleEndian::Load64(s + len - 24); |
146 | 0 | c += Rotate(a, 7); |
147 | 0 | a += LittleEndian::Load64(s + len - 16); |
148 | 0 | uint64_t wf = a + z; |
149 | 0 | uint64_t ws = b + Rotate(a, 31) + c; |
150 | 0 | uint64_t r = ShiftMix((vf + ws) * k2 + (wf + vs) * k0); |
151 | 0 | return ShiftMix(r * k0 + vs) * k2; |
152 | 0 | } |
153 | | |
154 | 128k | uint64_t CityHash64(const char* s, size_t len) { |
155 | 128k | if (len <= 32) { |
156 | 128k | if (len <= 16) { |
157 | 128k | return HashLen0to16(s, len); |
158 | 128k | } else { |
159 | 2 | return HashLen17to32(s, len); |
160 | 2 | } |
161 | 128k | } else if (len <= 64) { |
162 | 0 | return HashLen33to64(s, len); |
163 | 0 | } |
164 | | |
165 | | // For strings over 64 bytes we hash the end first, and then as we |
166 | | // loop we keep 56 bytes of state: v, w, x, y, and z. |
167 | 20 | uint64_t x = LittleEndian::Load64(s + len - 40); |
168 | 20 | uint64_t y = LittleEndian::Load64(s + len - 16) + LittleEndian::Load64(s + len - 56); |
169 | 20 | uint64_t z = |
170 | 20 | HashLen16(LittleEndian::Load64(s + len - 48) + len, LittleEndian::Load64(s + len - 24)); |
171 | 20 | pair<uint64_t, uint64_t> v = WeakHashLen32WithSeeds(s + len - 64, len, z); |
172 | 20 | pair<uint64_t, uint64_t> w = WeakHashLen32WithSeeds(s + len - 32, y + k1, x); |
173 | 20 | x = x * k1 + LittleEndian::Load64(s); |
174 | | |
175 | | // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks. |
176 | 20 | len = (len - 1) & ~static_cast<size_t>(63); |
177 | 20 | DCHECK_GT(len, 0); |
178 | 20 | DCHECK_EQ(len, len / 64 * 64); |
179 | 322 | do { |
180 | 322 | x = Rotate(x + y + v.first + LittleEndian::Load64(s + 8), 37) * k1; |
181 | 322 | y = Rotate(y + v.second + LittleEndian::Load64(s + 48), 42) * k1; |
182 | 322 | x ^= w.second; |
183 | 322 | y += v.first + LittleEndian::Load64(s + 40); |
184 | 322 | z = Rotate(z + w.first, 33) * k1; |
185 | 322 | v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first); |
186 | 322 | w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + LittleEndian::Load64(s + 16)); |
187 | 322 | std::swap(z, x); |
188 | 322 | s += 64; |
189 | 322 | len -= 64; |
190 | 322 | } while (len != 0); |
191 | 20 | return HashLen16(HashLen16(v.first, w.first) + ShiftMix(y) * k1 + z, |
192 | 20 | HashLen16(v.second, w.second) + x); |
193 | 128k | } |
194 | | |
195 | 128k | uint64_t CityHash64WithSeeds(const char* s, size_t len, uint64_t seed0, uint64_t seed1) { |
196 | 128k | return HashLen16(CityHash64(s, len) - seed0, seed1); |
197 | 128k | } |
198 | | |
199 | 128k | uint64_t CityHash64WithSeed(const char* s, size_t len, uint64_t seed) { |
200 | 128k | return CityHash64WithSeeds(s, len, k2, seed); |
201 | 128k | } |
202 | | } // namespace util_hash |