/src/mozilla-central/other-licenses/nsis/Contrib/CityHash/cityhash/city.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2011 Google, Inc. |
2 | | // |
3 | | // Permission is hereby granted, free of charge, to any person obtaining a copy |
4 | | // of this software and associated documentation files (the "Software"), to deal |
5 | | // in the Software without restriction, including without limitation the rights |
6 | | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
7 | | // copies of the Software, and to permit persons to whom the Software is |
8 | | // furnished to do so, subject to the following conditions: |
9 | | // |
10 | | // The above copyright notice and this permission notice shall be included in |
11 | | // all copies or substantial portions of the Software. |
12 | | // |
13 | | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
14 | | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
15 | | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
16 | | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
17 | | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
18 | | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
19 | | // THE SOFTWARE. |
20 | | // |
21 | | // CityHash Version 1, by Geoff Pike and Jyrki Alakuijala |
22 | | // |
23 | | // This file provides CityHash64() and related functions. |
24 | | // |
25 | | // It's probably possible to create even faster hash functions by |
26 | | // writing a program that systematically explores some of the space of |
27 | | // possible hash functions, by using SIMD instructions, or by |
28 | | // compromising on hash quality. |
29 | | |
30 | | #include "city.h" |
31 | | |
32 | | #include <algorithm> |
33 | | |
34 | | using namespace std; |
35 | | |
36 | 0 | #define UNALIGNED_LOAD64(p) (*(const uint64*)(p)) |
37 | 0 | #define UNALIGNED_LOAD32(p) (*(const uint32*)(p)) |
38 | | |
39 | | #if !defined(LIKELY) |
40 | | #if defined(__GNUC__) |
41 | 0 | #define LIKELY(x) (__builtin_expect(!!(x), 1)) |
42 | | #else |
43 | | #define LIKELY(x) (x) |
44 | | #endif |
45 | | #endif |
46 | | |
47 | | // Some primes between 2^63 and 2^64 for various uses. |
48 | | static const uint64 k0 = 0xc3a5c85c97cb3127; |
49 | | static const uint64 k1 = 0xb492b66fbe98f273; |
50 | | static const uint64 k2 = 0x9ae16a3b2f90404f; |
51 | | static const uint64 k3 = 0xc949d7c7509e6557; |
52 | | |
53 | | // Bitwise right rotate. Normally this will compile to a single |
54 | | // instruction, especially if the shift is a manifest constant. |
55 | 0 | static uint64 Rotate(uint64 val, int shift) { |
56 | 0 | // Avoid shifting by 64: doing so yields an undefined result. |
57 | 0 | return shift == 0 ? val : ((val >> shift) | (val << (64 - shift))); |
58 | 0 | } |
59 | | |
60 | | // Equivalent to Rotate(), but requires the second arg to be non-zero. |
61 | | // On x86-64, and probably others, it's possible for this to compile |
62 | | // to a single instruction if both args are already in registers. |
63 | 0 | static uint64 RotateByAtLeast1(uint64 val, int shift) { |
64 | 0 | return (val >> shift) | (val << (64 - shift)); |
65 | 0 | } |
66 | | |
67 | 0 | static uint64 ShiftMix(uint64 val) { |
68 | 0 | return val ^ (val >> 47); |
69 | 0 | } |
70 | | |
71 | 0 | static uint64 HashLen16(uint64 u, uint64 v) { |
72 | 0 | return Hash128to64(uint128(u, v)); |
73 | 0 | } |
74 | | |
75 | 0 | static uint64 HashLen0to16(const char *s, size_t len) { |
76 | 0 | if (len > 8) { |
77 | 0 | uint64 a = UNALIGNED_LOAD64(s); |
78 | 0 | uint64 b = UNALIGNED_LOAD64(s + len - 8); |
79 | 0 | return HashLen16(a, RotateByAtLeast1(b + len, len)) ^ b; |
80 | 0 | } |
81 | 0 | if (len >= 4) { |
82 | 0 | uint64 a = UNALIGNED_LOAD32(s); |
83 | 0 | return HashLen16(len + (a << 3), UNALIGNED_LOAD32(s + len - 4)); |
84 | 0 | } |
85 | 0 | if (len > 0) { |
86 | 0 | uint8 a = s[0]; |
87 | 0 | uint8 b = s[len >> 1]; |
88 | 0 | uint8 c = s[len - 1]; |
89 | 0 | uint32 y = static_cast<uint32>(a) + (static_cast<uint32>(b) << 8); |
90 | 0 | uint32 z = len + (static_cast<uint32>(c) << 2); |
91 | 0 | return ShiftMix(y * k2 ^ z * k3) * k2; |
92 | 0 | } |
93 | 0 | return k2; |
94 | 0 | } |
95 | | |
96 | | // This probably works well for 16-byte strings as well, but it may be overkill |
97 | | // in that case. |
98 | 0 | static uint64 HashLen17to32(const char *s, size_t len) { |
99 | 0 | uint64 a = UNALIGNED_LOAD64(s) * k1; |
100 | 0 | uint64 b = UNALIGNED_LOAD64(s + 8); |
101 | 0 | uint64 c = UNALIGNED_LOAD64(s + len - 8) * k2; |
102 | 0 | uint64 d = UNALIGNED_LOAD64(s + len - 16) * k0; |
103 | 0 | return HashLen16(Rotate(a - b, 43) + Rotate(c, 30) + d, |
104 | 0 | a + Rotate(b ^ k3, 20) - c + len); |
105 | 0 | } |
106 | | |
107 | | // Return a 16-byte hash for 48 bytes. Quick and dirty. |
108 | | // Callers do best to use "random-looking" values for a and b. |
109 | | static pair<uint64, uint64> WeakHashLen32WithSeeds( |
110 | 0 | uint64 w, uint64 x, uint64 y, uint64 z, uint64 a, uint64 b) { |
111 | 0 | a += w; |
112 | 0 | b = Rotate(b + a + z, 21); |
113 | 0 | uint64 c = a; |
114 | 0 | a += x; |
115 | 0 | a += y; |
116 | 0 | b += Rotate(a, 44); |
117 | 0 | return make_pair(a + z, b + c); |
118 | 0 | } |
119 | | |
120 | | // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty. |
121 | | static pair<uint64, uint64> WeakHashLen32WithSeeds( |
122 | 0 | const char* s, uint64 a, uint64 b) { |
123 | 0 | return WeakHashLen32WithSeeds(UNALIGNED_LOAD64(s), |
124 | 0 | UNALIGNED_LOAD64(s + 8), |
125 | 0 | UNALIGNED_LOAD64(s + 16), |
126 | 0 | UNALIGNED_LOAD64(s + 24), |
127 | 0 | a, |
128 | 0 | b); |
129 | 0 | } |
130 | | |
131 | | // Return an 8-byte hash for 33 to 64 bytes. |
132 | 0 | static uint64 HashLen33to64(const char *s, size_t len) { |
133 | 0 | uint64 z = UNALIGNED_LOAD64(s + 24); |
134 | 0 | uint64 a = UNALIGNED_LOAD64(s) + (len + UNALIGNED_LOAD64(s + len - 16)) * k0; |
135 | 0 | uint64 b = Rotate(a + z, 52); |
136 | 0 | uint64 c = Rotate(a, 37); |
137 | 0 | a += UNALIGNED_LOAD64(s + 8); |
138 | 0 | c += Rotate(a, 7); |
139 | 0 | a += UNALIGNED_LOAD64(s + 16); |
140 | 0 | uint64 vf = a + z; |
141 | 0 | uint64 vs = b + Rotate(a, 31) + c; |
142 | 0 | a = UNALIGNED_LOAD64(s + 16) + UNALIGNED_LOAD64(s + len - 32); |
143 | 0 | z = UNALIGNED_LOAD64(s + len - 8); |
144 | 0 | b = Rotate(a + z, 52); |
145 | 0 | c = Rotate(a, 37); |
146 | 0 | a += UNALIGNED_LOAD64(s + len - 24); |
147 | 0 | c += Rotate(a, 7); |
148 | 0 | a += UNALIGNED_LOAD64(s + len - 16); |
149 | 0 | uint64 wf = a + z; |
150 | 0 | uint64 ws = b + Rotate(a, 31) + c; |
151 | 0 | uint64 r = ShiftMix((vf + ws) * k2 + (wf + vs) * k0); |
152 | 0 | return ShiftMix(r * k0 + vs) * k2; |
153 | 0 | } |
154 | | |
155 | 0 | uint64 CityHash64(const char *s, size_t len) { |
156 | 0 | if (len <= 32) { |
157 | 0 | if (len <= 16) { |
158 | 0 | return HashLen0to16(s, len); |
159 | 0 | } else { |
160 | 0 | return HashLen17to32(s, len); |
161 | 0 | } |
162 | 0 | } else if (len <= 64) { |
163 | 0 | return HashLen33to64(s, len); |
164 | 0 | } |
165 | 0 | |
166 | 0 | // For strings over 64 bytes we hash the end first, and then as we |
167 | 0 | // loop we keep 56 bytes of state: v, w, x, y, and z. |
168 | 0 | uint64 x = UNALIGNED_LOAD64(s); |
169 | 0 | uint64 y = UNALIGNED_LOAD64(s + len - 16) ^ k1; |
170 | 0 | uint64 z = UNALIGNED_LOAD64(s + len - 56) ^ k0; |
171 | 0 | pair<uint64, uint64> v = WeakHashLen32WithSeeds(s + len - 64, len, y); |
172 | 0 | pair<uint64, uint64> w = WeakHashLen32WithSeeds(s + len - 32, len * k1, k0); |
173 | 0 | z += ShiftMix(v.second) * k1; |
174 | 0 | x = Rotate(z + x, 39) * k1; |
175 | 0 | y = Rotate(y, 33) * k1; |
176 | 0 |
|
177 | 0 | // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks. |
178 | 0 | len = (len - 1) & ~static_cast<size_t>(63); |
179 | 0 | do { |
180 | 0 | x = Rotate(x + y + v.first + UNALIGNED_LOAD64(s + 16), 37) * k1; |
181 | 0 | y = Rotate(y + v.second + UNALIGNED_LOAD64(s + 48), 42) * k1; |
182 | 0 | x ^= w.second; |
183 | 0 | y ^= v.first; |
184 | 0 | z = Rotate(z ^ w.first, 33); |
185 | 0 | v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first); |
186 | 0 | w = WeakHashLen32WithSeeds(s + 32, z + w.second, y); |
187 | 0 | std::swap(z, x); |
188 | 0 | s += 64; |
189 | 0 | len -= 64; |
190 | 0 | } while (len != 0); |
191 | 0 | return HashLen16(HashLen16(v.first, w.first) + ShiftMix(y) * k1 + z, |
192 | 0 | HashLen16(v.second, w.second) + x); |
193 | 0 | } |
194 | | |
195 | 0 | uint64 CityHash64WithSeed(const char *s, size_t len, uint64 seed) { |
196 | 0 | return CityHash64WithSeeds(s, len, k2, seed); |
197 | 0 | } |
198 | | |
199 | | uint64 CityHash64WithSeeds(const char *s, size_t len, |
200 | 0 | uint64 seed0, uint64 seed1) { |
201 | 0 | return HashLen16(CityHash64(s, len) - seed0, seed1); |
202 | 0 | } |
203 | | |
204 | | // A subroutine for CityHash128(). Returns a decent 128-bit hash for strings |
205 | | // of any length representable in an int. Based on City and Murmur. |
206 | 0 | static uint128 CityMurmur(const char *s, size_t len, uint128 seed) { |
207 | 0 | uint64 a = Uint128Low64(seed); |
208 | 0 | uint64 b = Uint128High64(seed); |
209 | 0 | uint64 c = 0; |
210 | 0 | uint64 d = 0; |
211 | 0 | int l = len - 16; |
212 | 0 | if (l <= 0) { // len <= 16 |
213 | 0 | c = b * k1 + HashLen0to16(s, len); |
214 | 0 | d = Rotate(a + (len >= 8 ? UNALIGNED_LOAD64(s) : c), 32); |
215 | 0 | } else { // len > 16 |
216 | 0 | c = HashLen16(UNALIGNED_LOAD64(s + len - 8) + k1, a); |
217 | 0 | d = HashLen16(b + len, c + UNALIGNED_LOAD64(s + len - 16)); |
218 | 0 | a += d; |
219 | 0 | do { |
220 | 0 | a ^= ShiftMix(UNALIGNED_LOAD64(s) * k1) * k1; |
221 | 0 | a *= k1; |
222 | 0 | b ^= a; |
223 | 0 | c ^= ShiftMix(UNALIGNED_LOAD64(s + 8) * k1) * k1; |
224 | 0 | c *= k1; |
225 | 0 | d ^= c; |
226 | 0 | s += 16; |
227 | 0 | l -= 16; |
228 | 0 | } while (l > 0); |
229 | 0 | } |
230 | 0 | a = HashLen16(a, c); |
231 | 0 | b = HashLen16(d, b); |
232 | 0 | return uint128(a ^ b, HashLen16(b, a)); |
233 | 0 | } |
234 | | |
235 | 0 | uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed) { |
236 | 0 | if (len < 128) { |
237 | 0 | return CityMurmur(s, len, seed); |
238 | 0 | } |
239 | 0 | |
240 | 0 | // We expect len >= 128 to be the common case. Keep 56 bytes of state: |
241 | 0 | // v, w, x, y, and z. |
242 | 0 | pair<uint64, uint64> v, w; |
243 | 0 | uint64 x = Uint128Low64(seed); |
244 | 0 | uint64 y = Uint128High64(seed); |
245 | 0 | uint64 z = len * k1; |
246 | 0 | v.first = Rotate(y ^ k1, 49) * k1 + UNALIGNED_LOAD64(s); |
247 | 0 | v.second = Rotate(v.first, 42) * k1 + UNALIGNED_LOAD64(s + 8); |
248 | 0 | w.first = Rotate(y + z, 35) * k1 + x; |
249 | 0 | w.second = Rotate(x + UNALIGNED_LOAD64(s + 88), 53) * k1; |
250 | 0 |
|
251 | 0 | // This is the same inner loop as CityHash64(), manually unrolled. |
252 | 0 | do { |
253 | 0 | x = Rotate(x + y + v.first + UNALIGNED_LOAD64(s + 16), 37) * k1; |
254 | 0 | y = Rotate(y + v.second + UNALIGNED_LOAD64(s + 48), 42) * k1; |
255 | 0 | x ^= w.second; |
256 | 0 | y ^= v.first; |
257 | 0 | z = Rotate(z ^ w.first, 33); |
258 | 0 | v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first); |
259 | 0 | w = WeakHashLen32WithSeeds(s + 32, z + w.second, y); |
260 | 0 | std::swap(z, x); |
261 | 0 | s += 64; |
262 | 0 | x = Rotate(x + y + v.first + UNALIGNED_LOAD64(s + 16), 37) * k1; |
263 | 0 | y = Rotate(y + v.second + UNALIGNED_LOAD64(s + 48), 42) * k1; |
264 | 0 | x ^= w.second; |
265 | 0 | y ^= v.first; |
266 | 0 | z = Rotate(z ^ w.first, 33); |
267 | 0 | v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first); |
268 | 0 | w = WeakHashLen32WithSeeds(s + 32, z + w.second, y); |
269 | 0 | std::swap(z, x); |
270 | 0 | s += 64; |
271 | 0 | len -= 128; |
272 | 0 | } while (LIKELY(len >= 128)); |
273 | 0 | y += Rotate(w.first, 37) * k0 + z; |
274 | 0 | x += Rotate(v.first + z, 49) * k0; |
275 | 0 | // If 0 < len < 128, hash up to 4 chunks of 32 bytes each from the end of s. |
276 | 0 | for (size_t tail_done = 0; tail_done < len; ) { |
277 | 0 | tail_done += 32; |
278 | 0 | y = Rotate(y - x, 42) * k0 + v.second; |
279 | 0 | w.first += UNALIGNED_LOAD64(s + len - tail_done + 16); |
280 | 0 | x = Rotate(x, 49) * k0 + w.first; |
281 | 0 | w.first += v.first; |
282 | 0 | v = WeakHashLen32WithSeeds(s + len - tail_done, v.first, v.second); |
283 | 0 | } |
284 | 0 | // At this point our 48 bytes of state should contain more than |
285 | 0 | // enough information for a strong 128-bit hash. We use two |
286 | 0 | // different 48-byte-to-8-byte hashes to get a 16-byte final result. |
287 | 0 | x = HashLen16(x, v.first); |
288 | 0 | y = HashLen16(y, w.first); |
289 | 0 | return uint128(HashLen16(x + v.second, w.second) + y, |
290 | 0 | HashLen16(x + w.second, y + v.second)); |
291 | 0 | } |
292 | | |
293 | 0 | uint128 CityHash128(const char *s, size_t len) { |
294 | 0 | if (len >= 16) { |
295 | 0 | return CityHash128WithSeed(s + 16, |
296 | 0 | len - 16, |
297 | 0 | uint128(UNALIGNED_LOAD64(s) ^ k3, |
298 | 0 | UNALIGNED_LOAD64(s + 8))); |
299 | 0 | } else if (len >= 8) { |
300 | 0 | return CityHash128WithSeed(NULL, |
301 | 0 | 0, |
302 | 0 | uint128(UNALIGNED_LOAD64(s) ^ (len * k0), |
303 | 0 | UNALIGNED_LOAD64(s + len - 8) ^ k1)); |
304 | 0 | } else { |
305 | 0 | return CityHash128WithSeed(s, len, uint128(k0, k1)); |
306 | 0 | } |
307 | 0 | } |