1 // Copyright (c) 2011 Google, Inc.
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:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
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
21 // CityHash, by Geoff Pike and Jyrki Alakuijala
23 // This file provides CityHash64() and related functions.
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.
34 #include <string.h> // for memcpy and memset
38 static uint64 UNALIGNED_LOAD64(const char *p) {
40 memcpy(&result, p, sizeof(result));
44 static uint32 UNALIGNED_LOAD32(const char *p) {
46 memcpy(&result, p, sizeof(result));
53 #define bswap_32(x) _byteswap_ulong(x)
54 #define bswap_64(x) _byteswap_uint64(x)
56 #elif defined(__APPLE__)
58 // Mac OS X / Darwin features
59 #include <libkern/OSByteOrder.h>
60 #define bswap_32(x) OSSwapInt32(x)
61 #define bswap_64(x) OSSwapInt64(x)
63 #elif defined(__NetBSD__)
65 #include <sys/types.h>
66 #include <machine/bswap.h>
67 #if defined(__BSWAP_RENAME) && !defined(__bswap_32)
68 #define bswap_32(x) bswap32(x)
69 #define bswap_64(x) bswap64(x)
78 #ifdef WORDS_BIGENDIAN
79 #define uint32_in_expected_order(x) (bswap_32(x))
80 #define uint64_in_expected_order(x) (bswap_64(x))
82 #define uint32_in_expected_order(x) (x)
83 #define uint64_in_expected_order(x) (x)
87 #if HAVE_BUILTIN_EXPECT
88 #define LIKELY(x) (__builtin_expect(!!(x), 1))
94 static uint64 Fetch64(const char *p) {
95 return uint64_in_expected_order(UNALIGNED_LOAD64(p));
98 static uint32 Fetch32(const char *p) {
99 return uint32_in_expected_order(UNALIGNED_LOAD32(p));
102 // Some primes between 2^63 and 2^64 for various uses.
103 static const uint64 k0 = 0xc3a5c85c97cb3127ULL;
104 static const uint64 k1 = 0xb492b66fbe98f273ULL;
105 static const uint64 k2 = 0x9ae16a3b2f90404fULL;
107 // Magic numbers for 32-bit hashing. Copied from Murmur3.
108 static const uint32_t c1 = 0xcc9e2d51;
109 static const uint32_t c2 = 0x1b873593;
111 // A 32-bit to 32-bit integer hash copied from Murmur3.
112 static uint32 fmix(uint32 h)
122 static uint32 Rotate32(uint32 val, int shift) {
123 // Avoid shifting by 32: doing so yields an undefined result.
124 return shift == 0 ? val : ((val >> shift) | (val << (32 - shift)));
128 #define PERMUTE3(a, b, c) do { std::swap(a, b); std::swap(a, c); } while (0)
130 static uint32 Mur(uint32 a, uint32 h) {
131 // Helper from Murmur3 for combining two 32-bit values.
137 return h * 5 + 0xe6546b64;
140 static uint32 Hash32Len13to24(const char *s, size_t len) {
141 uint32 a = Fetch32(s - 4 + (len >> 1));
142 uint32 b = Fetch32(s + 4);
143 uint32 c = Fetch32(s + len - 8);
144 uint32 d = Fetch32(s + (len >> 1));
145 uint32 e = Fetch32(s);
146 uint32 f = Fetch32(s + len - 4);
149 return fmix(Mur(f, Mur(e, Mur(d, Mur(c, Mur(b, Mur(a, h)))))));
152 static uint32 Hash32Len0to4(const char *s, size_t len) {
155 for (int i = 0; i < len; i++) {
156 signed char v = s[i];
160 return fmix(Mur(b, Mur(len, c)));
163 static uint32 Hash32Len5to12(const char *s, size_t len) {
164 uint32 a = len, b = len * 5, c = 9, d = b;
166 b += Fetch32(s + len - 4);
167 c += Fetch32(s + ((len >> 1) & 4));
168 return fmix(Mur(c, Mur(b, Mur(a, d))));
171 uint32 CityHash32(const char *s, size_t len) {
174 (len <= 4 ? Hash32Len0to4(s, len) : Hash32Len5to12(s, len)) :
175 Hash32Len13to24(s, len);
179 uint32 h = len, g = c1 * len, f = g;
180 uint32 a0 = Rotate32(Fetch32(s + len - 4) * c1, 17) * c2;
181 uint32 a1 = Rotate32(Fetch32(s + len - 8) * c1, 17) * c2;
182 uint32 a2 = Rotate32(Fetch32(s + len - 16) * c1, 17) * c2;
183 uint32 a3 = Rotate32(Fetch32(s + len - 12) * c1, 17) * c2;
184 uint32 a4 = Rotate32(Fetch32(s + len - 20) * c1, 17) * c2;
187 h = h * 5 + 0xe6546b64;
190 h = h * 5 + 0xe6546b64;
193 g = g * 5 + 0xe6546b64;
196 g = g * 5 + 0xe6546b64;
199 f = f * 5 + 0xe6546b64;
200 size_t iters = (len - 1) / 20;
202 uint32 a0 = Rotate32(Fetch32(s) * c1, 17) * c2;
203 uint32 a1 = Fetch32(s + 4);
204 uint32 a2 = Rotate32(Fetch32(s + 8) * c1, 17) * c2;
205 uint32 a3 = Rotate32(Fetch32(s + 12) * c1, 17) * c2;
206 uint32 a4 = Fetch32(s + 16);
209 h = h * 5 + 0xe6546b64;
215 g = g * 5 + 0xe6546b64;
218 h = h * 5 + 0xe6546b64;
226 } while (--iters != 0);
227 g = Rotate32(g, 11) * c1;
228 g = Rotate32(g, 17) * c1;
229 f = Rotate32(f, 11) * c1;
230 f = Rotate32(f, 17) * c1;
231 h = Rotate32(h + g, 19);
232 h = h * 5 + 0xe6546b64;
233 h = Rotate32(h, 17) * c1;
234 h = Rotate32(h + f, 19);
235 h = h * 5 + 0xe6546b64;
236 h = Rotate32(h, 17) * c1;
240 // Bitwise right rotate. Normally this will compile to a single
241 // instruction, especially if the shift is a manifest constant.
242 static uint64 Rotate(uint64 val, int shift) {
243 // Avoid shifting by 64: doing so yields an undefined result.
244 return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
247 static uint64 ShiftMix(uint64 val) {
248 return val ^ (val >> 47);
251 static uint64 HashLen16(uint64 u, uint64 v) {
252 return Hash128to64(uint128(u, v));
255 static uint64 HashLen16(uint64 u, uint64 v, uint64 mul) {
256 // Murmur-inspired hashing.
257 uint64 a = (u ^ v) * mul;
259 uint64 b = (v ^ a) * mul;
265 static uint64 HashLen0to16(const char *s, size_t len) {
267 uint64 mul = k2 + len * 2;
268 uint64 a = Fetch64(s) + k2;
269 uint64 b = Fetch64(s + len - 8);
270 uint64 c = Rotate(b, 37) * mul + a;
271 uint64 d = (Rotate(a, 25) + b) * mul;
272 return HashLen16(c, d, mul);
275 uint64 mul = k2 + len * 2;
276 uint64 a = Fetch32(s);
277 return HashLen16(len + (a << 3), Fetch32(s + len - 4), mul);
281 uint8 b = s[len >> 1];
282 uint8 c = s[len - 1];
283 uint32 y = static_cast<uint32>(a) + (static_cast<uint32>(b) << 8);
284 uint32 z = len + (static_cast<uint32>(c) << 2);
285 return ShiftMix(y * k2 ^ z * k0) * k2;
290 // This probably works well for 16-byte strings as well, but it may be overkill
292 static uint64 HashLen17to32(const char *s, size_t len) {
293 uint64 mul = k2 + len * 2;
294 uint64 a = Fetch64(s) * k1;
295 uint64 b = Fetch64(s + 8);
296 uint64 c = Fetch64(s + len - 8) * mul;
297 uint64 d = Fetch64(s + len - 16) * k2;
298 return HashLen16(Rotate(a + b, 43) + Rotate(c, 30) + d,
299 a + Rotate(b + k2, 18) + c, mul);
302 // Return a 16-byte hash for 48 bytes. Quick and dirty.
303 // Callers do best to use "random-looking" values for a and b.
304 static pair<uint64, uint64> WeakHashLen32WithSeeds(
305 uint64 w, uint64 x, uint64 y, uint64 z, uint64 a, uint64 b) {
307 b = Rotate(b + a + z, 21);
312 return make_pair(a + z, b + c);
315 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
316 static pair<uint64, uint64> WeakHashLen32WithSeeds(
317 const char* s, uint64 a, uint64 b) {
318 return WeakHashLen32WithSeeds(Fetch64(s),
326 // Return an 8-byte hash for 33 to 64 bytes.
327 static uint64 HashLen33to64(const char *s, size_t len) {
328 uint64 mul = k2 + len * 2;
329 uint64 a = Fetch64(s) * k2;
330 uint64 b = Fetch64(s + 8);
331 uint64 c = Fetch64(s + len - 24);
332 uint64 d = Fetch64(s + len - 32);
333 uint64 e = Fetch64(s + 16) * k2;
334 uint64 f = Fetch64(s + 24) * 9;
335 uint64 g = Fetch64(s + len - 8);
336 uint64 h = Fetch64(s + len - 16) * mul;
337 uint64 u = Rotate(a + g, 43) + (Rotate(b, 30) + c) * 9;
338 uint64 v = ((a + g) ^ d) + f + 1;
339 uint64 w = bswap_64((u + v) * mul) + h;
340 uint64 x = Rotate(e + f, 42) + c;
341 uint64 y = (bswap_64((v + w) * mul) + g) * mul;
342 uint64 z = e + f + c;
343 a = bswap_64((x + z) * mul + y) + b;
344 b = ShiftMix((z + a) * mul + d + h) * mul;
348 uint64 CityHash64(const char *s, size_t len) {
351 return HashLen0to16(s, len);
353 return HashLen17to32(s, len);
355 } else if (len <= 64) {
356 return HashLen33to64(s, len);
359 // For strings over 64 bytes we hash the end first, and then as we
360 // loop we keep 56 bytes of state: v, w, x, y, and z.
361 uint64 x = Fetch64(s + len - 40);
362 uint64 y = Fetch64(s + len - 16) + Fetch64(s + len - 56);
363 uint64 z = HashLen16(Fetch64(s + len - 48) + len, Fetch64(s + len - 24));
364 pair<uint64, uint64> v = WeakHashLen32WithSeeds(s + len - 64, len, z);
365 pair<uint64, uint64> w = WeakHashLen32WithSeeds(s + len - 32, y + k1, x);
366 x = x * k1 + Fetch64(s);
368 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
369 len = (len - 1) & ~static_cast<size_t>(63);
371 x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1;
372 y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
374 y += v.first + Fetch64(s + 40);
375 z = Rotate(z + w.first, 33) * k1;
376 v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
377 w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
382 return HashLen16(HashLen16(v.first, w.first) + ShiftMix(y) * k1 + z,
383 HashLen16(v.second, w.second) + x);
386 uint64 CityHash64WithSeed(const char *s, size_t len, uint64 seed) {
387 return CityHash64WithSeeds(s, len, k2, seed);
390 uint64 CityHash64WithSeeds(const char *s, size_t len,
391 uint64 seed0, uint64 seed1) {
392 return HashLen16(CityHash64(s, len) - seed0, seed1);
395 // A subroutine for CityHash128(). Returns a decent 128-bit hash for strings
396 // of any length representable in signed long. Based on City and Murmur.
397 static uint128 CityMurmur(const char *s, size_t len, uint128 seed) {
398 uint64 a = Uint128Low64(seed);
399 uint64 b = Uint128High64(seed);
402 signed long l = len - 16;
403 if (l <= 0) { // len <= 16
404 a = ShiftMix(a * k1) * k1;
405 c = b * k1 + HashLen0to16(s, len);
406 d = ShiftMix(a + (len >= 8 ? Fetch64(s) : c));
408 c = HashLen16(Fetch64(s + len - 8) + k1, a);
409 d = HashLen16(b + len, c + Fetch64(s + len - 16));
412 a ^= ShiftMix(Fetch64(s) * k1) * k1;
415 c ^= ShiftMix(Fetch64(s + 8) * k1) * k1;
424 return uint128(a ^ b, HashLen16(b, a));
427 uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed) {
429 return CityMurmur(s, len, seed);
432 // We expect len >= 128 to be the common case. Keep 56 bytes of state:
433 // v, w, x, y, and z.
434 pair<uint64, uint64> v, w;
435 uint64 x = Uint128Low64(seed);
436 uint64 y = Uint128High64(seed);
438 v.first = Rotate(y ^ k1, 49) * k1 + Fetch64(s);
439 v.second = Rotate(v.first, 42) * k1 + Fetch64(s + 8);
440 w.first = Rotate(y + z, 35) * k1 + x;
441 w.second = Rotate(x + Fetch64(s + 88), 53) * k1;
443 // This is the same inner loop as CityHash64(), manually unrolled.
445 x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1;
446 y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
448 y += v.first + Fetch64(s + 40);
449 z = Rotate(z + w.first, 33) * k1;
450 v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
451 w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
454 x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1;
455 y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
457 y += v.first + Fetch64(s + 40);
458 z = Rotate(z + w.first, 33) * k1;
459 v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
460 w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
464 } while (LIKELY(len >= 128));
465 x += Rotate(v.first + z, 49) * k0;
466 y = y * k0 + Rotate(w.second, 37);
467 z = z * k0 + Rotate(w.first, 27);
470 // If 0 < len < 128, hash up to 4 chunks of 32 bytes each from the end of s.
471 for (size_t tail_done = 0; tail_done < len; ) {
473 y = Rotate(x + y, 42) * k0 + v.second;
474 w.first += Fetch64(s + len - tail_done + 16);
475 x = x * k0 + w.first;
476 z += w.second + Fetch64(s + len - tail_done);
478 v = WeakHashLen32WithSeeds(s + len - tail_done, v.first + z, v.second);
481 // At this point our 56 bytes of state should contain more than
482 // enough information for a strong 128-bit hash. We use two
483 // different 56-byte-to-8-byte hashes to get a 16-byte final result.
484 x = HashLen16(x, v.first);
485 y = HashLen16(y + z, w.first);
486 return uint128(HashLen16(x + v.second, w.second) + y,
487 HashLen16(x + w.second, y + v.second));
490 uint128 CityHash128(const char *s, size_t len) {
492 CityHash128WithSeed(s + 16, len - 16,
493 uint128(Fetch64(s), Fetch64(s + 8) + k0)) :
494 CityHash128WithSeed(s, len, uint128(k0, k1));
499 #include <nmmintrin.h>
501 // Requires len >= 240.
502 static void CityHashCrc256Long(const char *s, size_t len,
503 uint32 seed, uint64 *result) {
504 uint64 a = Fetch64(s + 56) + k0;
505 uint64 b = Fetch64(s + 96) + k0;
506 uint64 c = result[0] = HashLen16(b, len);
507 uint64 d = result[1] = Fetch64(s + 120) * k0 + len;
508 uint64 e = Fetch64(s + 184) + seed;
516 // 240 bytes of input per iter.
517 size_t iters = len / 240;
524 c += Fetch64(s + 8); \
525 d += Fetch64(s + 16); \
526 e += Fetch64(s + 24); \
527 f += Fetch64(s + 32); \
535 z = _mm_crc32_u64(z, b + g); \
536 y = _mm_crc32_u64(y, e + h); \
537 x = _mm_crc32_u64(x, f + a); \
542 CHUNK(0); PERMUTE3(a, h, c);
543 CHUNK(33); PERMUTE3(a, h, f);
544 CHUNK(0); PERMUTE3(b, h, f);
545 CHUNK(42); PERMUTE3(b, h, d);
546 CHUNK(0); PERMUTE3(b, h, e);
547 CHUNK(33); PERMUTE3(a, h, e);
548 } while (--iters > 0);
570 a = HashLen16(a, g + z);
573 c = HashLen16(c, z) + h;
574 d = HashLen16(d, e + result[0]);
576 h += HashLen16(x, f);
577 e = HashLen16(a, d) + g;
578 z = HashLen16(b, c) + a;
579 y = HashLen16(g, h) + c;
580 result[0] = e + z + y + x;
581 a = ShiftMix((a + y) * k0) * k0 + b;
582 result[1] += a + result[0];
583 a = ShiftMix(a * k0) * k0 + c;
584 result[2] = a + result[1];
585 a = ShiftMix((a + e) * k0) * k0;
586 result[3] = a + result[2];
589 // Requires len < 240.
590 static void CityHashCrc256Short(const char *s, size_t len, uint64 *result) {
593 memset(buf + len, 0, 240 - len);
594 CityHashCrc256Long(buf, 240, ~static_cast<uint32>(len), result);
597 void CityHashCrc256(const char *s, size_t len, uint64 *result) {
598 if (LIKELY(len >= 240)) {
599 CityHashCrc256Long(s, len, 0, result);
601 CityHashCrc256Short(s, len, result);
605 uint128 CityHashCrc128WithSeed(const char *s, size_t len, uint128 seed) {
607 return CityHash128WithSeed(s, len, seed);
610 CityHashCrc256(s, len, result);
611 uint64 u = Uint128High64(seed) + result[0];
612 uint64 v = Uint128Low64(seed) + result[1];
613 return uint128(HashLen16(u, v + result[2]),
614 HashLen16(Rotate(v, 32), u * k0 + result[3]));
618 uint128 CityHashCrc128(const char *s, size_t len) {
620 return CityHash128(s, len);
623 CityHashCrc256(s, len, result);
624 return uint128(result[2], result[3]);