1// Copyright 2018 The Abseil Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// This file provides CityHash64() and related functions.
16//
17// It's probably possible to create even faster hash functions by
18// writing a program that systematically explores some of the space of
19// possible hash functions, by using SIMD instructions, or by
20// compromising on hash quality.
21
22#include "absl/hash/internal/city.h"
23
24#include <string.h> // for memcpy and memset
25#include <algorithm>
26
27#include "absl/base/config.h"
28#include "absl/base/internal/endian.h"
29#include "absl/base/internal/unaligned_access.h"
30#include "absl/base/optimization.h"
31
32namespace absl {
33namespace hash_internal {
34
35#ifdef ABSL_IS_BIG_ENDIAN
36#define uint32_in_expected_order(x) (absl::gbswap_32(x))
37#define uint64_in_expected_order(x) (absl::gbswap_64(x))
38#else
39#define uint32_in_expected_order(x) (x)
40#define uint64_in_expected_order(x) (x)
41#endif
42
43static uint64_t Fetch64(const char *p) {
44 return uint64_in_expected_order(ABSL_INTERNAL_UNALIGNED_LOAD64(p));
45}
46
47static uint32_t Fetch32(const char *p) {
48 return uint32_in_expected_order(ABSL_INTERNAL_UNALIGNED_LOAD32(p));
49}
50
51// Some primes between 2^63 and 2^64 for various uses.
52static const uint64_t k0 = 0xc3a5c85c97cb3127ULL;
53static const uint64_t k1 = 0xb492b66fbe98f273ULL;
54static const uint64_t k2 = 0x9ae16a3b2f90404fULL;
55
56// Magic numbers for 32-bit hashing. Copied from Murmur3.
57static const uint32_t c1 = 0xcc9e2d51;
58static const uint32_t c2 = 0x1b873593;
59
60// A 32-bit to 32-bit integer hash copied from Murmur3.
61static uint32_t fmix(uint32_t h) {
62 h ^= h >> 16;
63 h *= 0x85ebca6b;
64 h ^= h >> 13;
65 h *= 0xc2b2ae35;
66 h ^= h >> 16;
67 return h;
68}
69
70static uint32_t Rotate32(uint32_t val, int shift) {
71 // Avoid shifting by 32: doing so yields an undefined result.
72 return shift == 0 ? val : ((val >> shift) | (val << (32 - shift)));
73}
74
75#undef PERMUTE3
76#define PERMUTE3(a, b, c) \
77 do { \
78 std::swap(a, b); \
79 std::swap(a, c); \
80 } while (0)
81
82static uint32_t Mur(uint32_t a, uint32_t h) {
83 // Helper from Murmur3 for combining two 32-bit values.
84 a *= c1;
85 a = Rotate32(a, 17);
86 a *= c2;
87 h ^= a;
88 h = Rotate32(h, 19);
89 return h * 5 + 0xe6546b64;
90}
91
92static uint32_t Hash32Len13to24(const char *s, size_t len) {
93 uint32_t a = Fetch32(s - 4 + (len >> 1));
94 uint32_t b = Fetch32(s + 4);
95 uint32_t c = Fetch32(s + len - 8);
96 uint32_t d = Fetch32(s + (len >> 1));
97 uint32_t e = Fetch32(s);
98 uint32_t f = Fetch32(s + len - 4);
99 uint32_t h = len;
100
101 return fmix(Mur(f, Mur(e, Mur(d, Mur(c, Mur(b, Mur(a, h)))))));
102}
103
104static uint32_t Hash32Len0to4(const char *s, size_t len) {
105 uint32_t b = 0;
106 uint32_t c = 9;
107 for (size_t i = 0; i < len; i++) {
108 signed char v = s[i];
109 b = b * c1 + v;
110 c ^= b;
111 }
112 return fmix(Mur(b, Mur(len, c)));
113}
114
115static uint32_t Hash32Len5to12(const char *s, size_t len) {
116 uint32_t a = len, b = len * 5, c = 9, d = b;
117 a += Fetch32(s);
118 b += Fetch32(s + len - 4);
119 c += Fetch32(s + ((len >> 1) & 4));
120 return fmix(Mur(c, Mur(b, Mur(a, d))));
121}
122
123uint32_t CityHash32(const char *s, size_t len) {
124 if (len <= 24) {
125 return len <= 12
126 ? (len <= 4 ? Hash32Len0to4(s, len) : Hash32Len5to12(s, len))
127 : Hash32Len13to24(s, len);
128 }
129
130 // len > 24
131 uint32_t h = len, g = c1 * len, f = g;
132
133 uint32_t a0 = Rotate32(Fetch32(s + len - 4) * c1, 17) * c2;
134 uint32_t a1 = Rotate32(Fetch32(s + len - 8) * c1, 17) * c2;
135 uint32_t a2 = Rotate32(Fetch32(s + len - 16) * c1, 17) * c2;
136 uint32_t a3 = Rotate32(Fetch32(s + len - 12) * c1, 17) * c2;
137 uint32_t a4 = Rotate32(Fetch32(s + len - 20) * c1, 17) * c2;
138 h ^= a0;
139 h = Rotate32(h, 19);
140 h = h * 5 + 0xe6546b64;
141 h ^= a2;
142 h = Rotate32(h, 19);
143 h = h * 5 + 0xe6546b64;
144 g ^= a1;
145 g = Rotate32(g, 19);
146 g = g * 5 + 0xe6546b64;
147 g ^= a3;
148 g = Rotate32(g, 19);
149 g = g * 5 + 0xe6546b64;
150 f += a4;
151 f = Rotate32(f, 19);
152 f = f * 5 + 0xe6546b64;
153 size_t iters = (len - 1) / 20;
154 do {
155 uint32_t b0 = Rotate32(Fetch32(s) * c1, 17) * c2;
156 uint32_t b1 = Fetch32(s + 4);
157 uint32_t b2 = Rotate32(Fetch32(s + 8) * c1, 17) * c2;
158 uint32_t b3 = Rotate32(Fetch32(s + 12) * c1, 17) * c2;
159 uint32_t b4 = Fetch32(s + 16);
160 h ^= b0;
161 h = Rotate32(h, 18);
162 h = h * 5 + 0xe6546b64;
163 f += b1;
164 f = Rotate32(f, 19);
165 f = f * c1;
166 g += b2;
167 g = Rotate32(g, 18);
168 g = g * 5 + 0xe6546b64;
169 h ^= b3 + b1;
170 h = Rotate32(h, 19);
171 h = h * 5 + 0xe6546b64;
172 g ^= b4;
173 g = absl::gbswap_32(g) * 5;
174 h += b4 * 5;
175 h = absl::gbswap_32(h);
176 f += b0;
177 PERMUTE3(f, h, g);
178 s += 20;
179 } while (--iters != 0);
180 g = Rotate32(g, 11) * c1;
181 g = Rotate32(g, 17) * c1;
182 f = Rotate32(f, 11) * c1;
183 f = Rotate32(f, 17) * c1;
184 h = Rotate32(h + g, 19);
185 h = h * 5 + 0xe6546b64;
186 h = Rotate32(h, 17) * c1;
187 h = Rotate32(h + f, 19);
188 h = h * 5 + 0xe6546b64;
189 h = Rotate32(h, 17) * c1;
190 return h;
191}
192
193// Bitwise right rotate. Normally this will compile to a single
194// instruction, especially if the shift is a manifest constant.
195static uint64_t Rotate(uint64_t val, int shift) {
196 // Avoid shifting by 64: doing so yields an undefined result.
197 return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
198}
199
200static uint64_t ShiftMix(uint64_t val) { return val ^ (val >> 47); }
201
202static uint64_t HashLen16(uint64_t u, uint64_t v) {
203 return Hash128to64(uint128(u, v));
204}
205
206static uint64_t HashLen16(uint64_t u, uint64_t v, uint64_t mul) {
207 // Murmur-inspired hashing.
208 uint64_t a = (u ^ v) * mul;
209 a ^= (a >> 47);
210 uint64_t b = (v ^ a) * mul;
211 b ^= (b >> 47);
212 b *= mul;
213 return b;
214}
215
216static uint64_t HashLen0to16(const char *s, size_t len) {
217 if (len >= 8) {
218 uint64_t mul = k2 + len * 2;
219 uint64_t a = Fetch64(s) + k2;
220 uint64_t b = Fetch64(s + len - 8);
221 uint64_t c = Rotate(b, 37) * mul + a;
222 uint64_t d = (Rotate(a, 25) + b) * mul;
223 return HashLen16(c, d, mul);
224 }
225 if (len >= 4) {
226 uint64_t mul = k2 + len * 2;
227 uint64_t a = Fetch32(s);
228 return HashLen16(len + (a << 3), Fetch32(s + len - 4), mul);
229 }
230 if (len > 0) {
231 uint8_t a = s[0];
232 uint8_t b = s[len >> 1];
233 uint8_t c = s[len - 1];
234 uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8);
235 uint32_t z = len + (static_cast<uint32_t>(c) << 2);
236 return ShiftMix(y * k2 ^ z * k0) * k2;
237 }
238 return k2;
239}
240
241// This probably works well for 16-byte strings as well, but it may be overkill
242// in that case.
243static uint64_t HashLen17to32(const char *s, size_t len) {
244 uint64_t mul = k2 + len * 2;
245 uint64_t a = Fetch64(s) * k1;
246 uint64_t b = Fetch64(s + 8);
247 uint64_t c = Fetch64(s + len - 8) * mul;
248 uint64_t d = Fetch64(s + len - 16) * k2;
249 return HashLen16(Rotate(a + b, 43) + Rotate(c, 30) + d,
250 a + Rotate(b + k2, 18) + c, mul);
251}
252
253// Return a 16-byte hash for 48 bytes. Quick and dirty.
254// Callers do best to use "random-looking" values for a and b.
255static std::pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(uint64_t w, uint64_t x,
256 uint64_t y, uint64_t z,
257 uint64_t a, uint64_t b) {
258 a += w;
259 b = Rotate(b + a + z, 21);
260 uint64_t c = a;
261 a += x;
262 a += y;
263 b += Rotate(a, 44);
264 return std::make_pair(a + z, b + c);
265}
266
267// Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
268static std::pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(const char *s, uint64_t a,
269 uint64_t b) {
270 return WeakHashLen32WithSeeds(Fetch64(s), Fetch64(s + 8), Fetch64(s + 16),
271 Fetch64(s + 24), a, b);
272}
273
274// Return an 8-byte hash for 33 to 64 bytes.
275static uint64_t HashLen33to64(const char *s, size_t len) {
276 uint64_t mul = k2 + len * 2;
277 uint64_t a = Fetch64(s) * k2;
278 uint64_t b = Fetch64(s + 8);
279 uint64_t c = Fetch64(s + len - 24);
280 uint64_t d = Fetch64(s + len - 32);
281 uint64_t e = Fetch64(s + 16) * k2;
282 uint64_t f = Fetch64(s + 24) * 9;
283 uint64_t g = Fetch64(s + len - 8);
284 uint64_t h = Fetch64(s + len - 16) * mul;
285 uint64_t u = Rotate(a + g, 43) + (Rotate(b, 30) + c) * 9;
286 uint64_t v = ((a + g) ^ d) + f + 1;
287 uint64_t w = absl::gbswap_64((u + v) * mul) + h;
288 uint64_t x = Rotate(e + f, 42) + c;
289 uint64_t y = (absl::gbswap_64((v + w) * mul) + g) * mul;
290 uint64_t z = e + f + c;
291 a = absl::gbswap_64((x + z) * mul + y) + b;
292 b = ShiftMix((z + a) * mul + d + h) * mul;
293 return b + x;
294}
295
296uint64_t CityHash64(const char *s, size_t len) {
297 if (len <= 32) {
298 if (len <= 16) {
299 return HashLen0to16(s, len);
300 } else {
301 return HashLen17to32(s, len);
302 }
303 } else if (len <= 64) {
304 return HashLen33to64(s, len);
305 }
306
307 // For strings over 64 bytes we hash the end first, and then as we
308 // loop we keep 56 bytes of state: v, w, x, y, and z.
309 uint64_t x = Fetch64(s + len - 40);
310 uint64_t y = Fetch64(s + len - 16) + Fetch64(s + len - 56);
311 uint64_t z = HashLen16(Fetch64(s + len - 48) + len, Fetch64(s + len - 24));
312 std::pair<uint64_t, uint64_t> v = WeakHashLen32WithSeeds(s + len - 64, len, z);
313 std::pair<uint64_t, uint64_t> w = WeakHashLen32WithSeeds(s + len - 32, y + k1, x);
314 x = x * k1 + Fetch64(s);
315
316 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
317 len = (len - 1) & ~static_cast<size_t>(63);
318 do {
319 x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1;
320 y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
321 x ^= w.second;
322 y += v.first + Fetch64(s + 40);
323 z = Rotate(z + w.first, 33) * k1;
324 v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
325 w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
326 std::swap(z, x);
327 s += 64;
328 len -= 64;
329 } while (len != 0);
330 return HashLen16(HashLen16(v.first, w.first) + ShiftMix(y) * k1 + z,
331 HashLen16(v.second, w.second) + x);
332}
333
334uint64_t CityHash64WithSeed(const char *s, size_t len, uint64_t seed) {
335 return CityHash64WithSeeds(s, len, k2, seed);
336}
337
338uint64_t CityHash64WithSeeds(const char *s, size_t len, uint64_t seed0,
339 uint64_t seed1) {
340 return HashLen16(CityHash64(s, len) - seed0, seed1);
341}
342
343} // namespace hash_internal
344} // namespace absl
345