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.
32 #if CDS_BUILD_BITS == 64
35 #include <string.h> // for memcpy and memset
39 static uint64 UNALIGNED_LOAD64(const char *p) {
41 memcpy(&result, p, sizeof(result));
45 static uint32 UNALIGNED_LOAD32(const char *p) {
47 memcpy(&result, p, sizeof(result));
54 #define bswap_32(x) _byteswap_ulong(x)
55 #define bswap_64(x) _byteswap_uint64(x)
57 #elif defined(__APPLE__)
59 // Mac OS X / Darwin features
60 #include <libkern/OSByteOrder.h>
61 #define bswap_32(x) OSSwapInt32(x)
62 #define bswap_64(x) OSSwapInt64(x)
64 #elif defined(__NetBSD__)
66 #include <sys/types.h>
67 #include <machine/bswap.h>
68 #if defined(__BSWAP_RENAME) && !defined(__bswap_32)
69 #define bswap_32(x) bswap32(x)
70 #define bswap_64(x) bswap64(x)
75 #include <cds_test/ext_byteswap.h>
79 #ifdef WORDS_BIGENDIAN
80 #define uint32_in_expected_order(x) (bswap_32(x))
81 #define uint64_in_expected_order(x) (bswap_64(x))
83 #define uint32_in_expected_order(x) (x)
84 #define uint64_in_expected_order(x) (x)
88 #if HAVE_BUILTIN_EXPECT
89 #define LIKELY(x) (__builtin_expect(!!(x), 1))
95 static uint64 Fetch64(const char *p) {
96 return uint64_in_expected_order(UNALIGNED_LOAD64(p));
99 static uint32 Fetch32(const char *p) {
100 return uint32_in_expected_order(UNALIGNED_LOAD32(p));
103 // Some primes between 2^63 and 2^64 for various uses.
104 static const uint64 k0 = 0xc3a5c85c97cb3127ULL;
105 static const uint64 k1 = 0xb492b66fbe98f273ULL;
106 static const uint64 k2 = 0x9ae16a3b2f90404fULL;
108 // Magic numbers for 32-bit hashing. Copied from Murmur3.
109 static const uint32_t c1 = 0xcc9e2d51;
110 static const uint32_t c2 = 0x1b873593;
112 // A 32-bit to 32-bit integer hash copied from Murmur3.
113 static uint32 fmix(uint32 h)
123 static uint32 Rotate32(uint32 val, int shift) {
124 // Avoid shifting by 32: doing so yields an undefined result.
125 return shift == 0 ? val : ((val >> shift) | (val << (32 - shift)));
129 #define PERMUTE3(a, b, c) do { std::swap(a, b); std::swap(a, c); } while (0)
131 static uint32 Mur(uint32 a, uint32 h) {
132 // Helper from Murmur3 for combining two 32-bit values.
138 return h * 5 + 0xe6546b64;
141 static uint32 Hash32Len13to24(const char *s, size_t len) {
142 uint32 a = Fetch32(s - 4 + (len >> 1));
143 uint32 b = Fetch32(s + 4);
144 uint32 c = Fetch32(s + len - 8);
145 uint32 d = Fetch32(s + (len >> 1));
146 uint32 e = Fetch32(s);
147 uint32 f = Fetch32(s + len - 4);
148 uint32 h = static_cast<uint32>( len );
150 return fmix(Mur(f, Mur(e, Mur(d, Mur(c, Mur(b, Mur(a, h)))))));
153 static uint32 Hash32Len0to4(const char *s, size_t len) {
156 for (size_t i = 0; i < len; i++) {
157 signed char v = s[i];
161 return fmix(Mur(b, Mur(static_cast<uint32>( len ), c)));
164 static uint32 Hash32Len5to12(const char *s, size_t len) {
165 uint32 a = static_cast<uint32>( len ), b = static_cast<uint32>( len ) * 5, c = 9, d = b;
167 b += Fetch32(s + len - 4);
168 c += Fetch32(s + ((len >> 1) & 4));
169 return fmix(Mur(c, Mur(b, Mur(a, d))));
172 uint32 CityHash32(const char *s, size_t len) {
175 (len <= 4 ? Hash32Len0to4(s, len) : Hash32Len5to12(s, len)) :
176 Hash32Len13to24(s, len);
180 uint32 h = static_cast<uint32>( len ), g = static_cast<uint32>( c1 * len ), f = g;
181 uint32 a0 = Rotate32(Fetch32(s + len - 4) * c1, 17) * c2;
182 uint32 a1 = Rotate32(Fetch32(s + len - 8) * c1, 17) * c2;
183 uint32 a2 = Rotate32(Fetch32(s + len - 16) * c1, 17) * c2;
184 uint32 a3 = Rotate32(Fetch32(s + len - 12) * c1, 17) * c2;
185 uint32 a4 = Rotate32(Fetch32(s + len - 20) * c1, 17) * c2;
188 h = h * 5 + 0xe6546b64;
191 h = h * 5 + 0xe6546b64;
194 g = g * 5 + 0xe6546b64;
197 g = g * 5 + 0xe6546b64;
200 f = f * 5 + 0xe6546b64;
201 size_t iters = (len - 1) / 20;
203 a0 = Rotate32(Fetch32(s) * c1, 17) * c2;
205 a2 = Rotate32(Fetch32(s + 8) * c1, 17) * c2;
206 a3 = Rotate32(Fetch32(s + 12) * c1, 17) * c2;
207 a4 = Fetch32(s + 16);
210 h = h * 5 + 0xe6546b64;
216 g = g * 5 + 0xe6546b64;
219 h = h * 5 + 0xe6546b64;
227 } while (--iters != 0);
228 g = Rotate32(g, 11) * c1;
229 g = Rotate32(g, 17) * c1;
230 f = Rotate32(f, 11) * c1;
231 f = Rotate32(f, 17) * c1;
232 h = Rotate32(h + g, 19);
233 h = h * 5 + 0xe6546b64;
234 h = Rotate32(h, 17) * c1;
235 h = Rotate32(h + f, 19);
236 h = h * 5 + 0xe6546b64;
237 h = Rotate32(h, 17) * c1;
241 // Bitwise right rotate. Normally this will compile to a single
242 // instruction, especially if the shift is a manifest constant.
243 static uint64 Rotate(uint64 val, int shift) {
244 // Avoid shifting by 64: doing so yields an undefined result.
245 return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
248 static uint64 ShiftMix(uint64 val) {
249 return val ^ (val >> 47);
252 static uint64 HashLen16(uint64 u, uint64 v) {
253 return Hash128to64(uint128(u, v));
256 static uint64 HashLen16(uint64 u, uint64 v, uint64 mul) {
257 // Murmur-inspired hashing.
258 uint64 a = (u ^ v) * mul;
260 uint64 b = (v ^ a) * mul;
266 static uint64 HashLen0to16(const char *s, size_t len) {
268 uint64 mul = k2 + len * 2;
269 uint64 a = Fetch64(s) + k2;
270 uint64 b = Fetch64(s + len - 8);
271 uint64 c = Rotate(b, 37) * mul + a;
272 uint64 d = (Rotate(a, 25) + b) * mul;
273 return HashLen16(c, d, mul);
276 uint64 mul = k2 + len * 2;
277 uint64 a = Fetch32(s);
278 return HashLen16(len + (a << 3), Fetch32(s + len - 4), mul);
282 uint8 b = s[len >> 1];
283 uint8 c = s[len - 1];
284 uint32 y = static_cast<uint32>(a) + (static_cast<uint32>(b) << 8);
285 uint32 z = static_cast<uint32>( len + (static_cast<uint32>(c) << 2));
286 return ShiftMix(y * k2 ^ z * k0) * k2;
291 // This probably works well for 16-byte strings as well, but it may be overkill
293 static uint64 HashLen17to32(const char *s, size_t len) {
294 uint64 mul = k2 + len * 2;
295 uint64 a = Fetch64(s) * k1;
296 uint64 b = Fetch64(s + 8);
297 uint64 c = Fetch64(s + len - 8) * mul;
298 uint64 d = Fetch64(s + len - 16) * k2;
299 return HashLen16(Rotate(a + b, 43) + Rotate(c, 30) + d,
300 a + Rotate(b + k2, 18) + c, mul);
303 // Return a 16-byte hash for 48 bytes. Quick and dirty.
304 // Callers do best to use "random-looking" values for a and b.
305 static pair<uint64, uint64> WeakHashLen32WithSeeds(
306 uint64 w, uint64 x, uint64 y, uint64 z, uint64 a, uint64 b) {
308 b = Rotate(b + a + z, 21);
313 return make_pair(a + z, b + c);
316 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
317 static pair<uint64, uint64> WeakHashLen32WithSeeds(
318 const char* s, uint64 a, uint64 b) {
319 return WeakHashLen32WithSeeds(Fetch64(s),
327 // Return an 8-byte hash for 33 to 64 bytes.
328 static uint64 HashLen33to64(const char *s, size_t len) {
329 uint64 mul = k2 + len * 2;
330 uint64 a = Fetch64(s) * k2;
331 uint64 b = Fetch64(s + 8);
332 uint64 c = Fetch64(s + len - 24);
333 uint64 d = Fetch64(s + len - 32);
334 uint64 e = Fetch64(s + 16) * k2;
335 uint64 f = Fetch64(s + 24) * 9;
336 uint64 g = Fetch64(s + len - 8);
337 uint64 h = Fetch64(s + len - 16) * mul;
338 uint64 u = Rotate(a + g, 43) + (Rotate(b, 30) + c) * 9;
339 uint64 v = ((a + g) ^ d) + f + 1;
340 uint64 w = bswap_64((u + v) * mul) + h;
341 uint64 x = Rotate(e + f, 42) + c;
342 uint64 y = (bswap_64((v + w) * mul) + g) * mul;
343 uint64 z = e + f + c;
344 a = bswap_64((x + z) * mul + y) + b;
345 b = ShiftMix((z + a) * mul + d + h) * mul;
349 uint64 CityHash64(const char *s, size_t len) {
352 return HashLen0to16(s, len);
354 return HashLen17to32(s, len);
356 } else if (len <= 64) {
357 return HashLen33to64(s, len);
360 // For strings over 64 bytes we hash the end first, and then as we
361 // loop we keep 56 bytes of state: v, w, x, y, and z.
362 uint64 x = Fetch64(s + len - 40);
363 uint64 y = Fetch64(s + len - 16) + Fetch64(s + len - 56);
364 uint64 z = HashLen16(Fetch64(s + len - 48) + len, Fetch64(s + len - 24));
365 pair<uint64, uint64> v = WeakHashLen32WithSeeds(s + len - 64, len, z);
366 pair<uint64, uint64> w = WeakHashLen32WithSeeds(s + len - 32, y + k1, x);
367 x = x * k1 + Fetch64(s);
369 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
370 len = (len - 1) & ~static_cast<size_t>(63);
372 x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1;
373 y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
375 y += v.first + Fetch64(s + 40);
376 z = Rotate(z + w.first, 33) * k1;
377 v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
378 w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
383 return HashLen16(HashLen16(v.first, w.first) + ShiftMix(y) * k1 + z,
384 HashLen16(v.second, w.second) + x);
387 uint64 CityHash64WithSeed(const char *s, size_t len, uint64 seed) {
388 return CityHash64WithSeeds(s, len, k2, seed);
391 uint64 CityHash64WithSeeds(const char *s, size_t len,
392 uint64 seed0, uint64 seed1) {
393 return HashLen16(CityHash64(s, len) - seed0, seed1);
396 // A subroutine for CityHash128(). Returns a decent 128-bit hash for strings
397 // of any length representable in signed long. Based on City and Murmur.
398 static uint128 CityMurmur(const char *s, size_t len, uint128 seed) {
399 uint64 a = Uint128Low64(seed);
400 uint64 b = Uint128High64(seed);
403 signed long l = len - 16;
404 if (l <= 0) { // len <= 16
405 a = ShiftMix(a * k1) * k1;
406 c = b * k1 + HashLen0to16(s, len);
407 d = ShiftMix(a + (len >= 8 ? Fetch64(s) : c));
409 c = HashLen16(Fetch64(s + len - 8) + k1, a);
410 d = HashLen16(b + len, c + Fetch64(s + len - 16));
413 a ^= ShiftMix(Fetch64(s) * k1) * k1;
416 c ^= ShiftMix(Fetch64(s + 8) * k1) * k1;
425 return uint128(a ^ b, HashLen16(b, a));
428 uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed) {
430 return CityMurmur(s, len, seed);
433 // We expect len >= 128 to be the common case. Keep 56 bytes of state:
434 // v, w, x, y, and z.
435 pair<uint64, uint64> v, w;
436 uint64 x = Uint128Low64(seed);
437 uint64 y = Uint128High64(seed);
439 v.first = Rotate(y ^ k1, 49) * k1 + Fetch64(s);
440 v.second = Rotate(v.first, 42) * k1 + Fetch64(s + 8);
441 w.first = Rotate(y + z, 35) * k1 + x;
442 w.second = Rotate(x + Fetch64(s + 88), 53) * k1;
444 // This is the same inner loop as CityHash64(), manually unrolled.
446 x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1;
447 y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
449 y += v.first + Fetch64(s + 40);
450 z = Rotate(z + w.first, 33) * k1;
451 v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
452 w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
455 x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1;
456 y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
458 y += v.first + Fetch64(s + 40);
459 z = Rotate(z + w.first, 33) * k1;
460 v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
461 w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
465 } while (LIKELY(len >= 128));
466 x += Rotate(v.first + z, 49) * k0;
467 y = y * k0 + Rotate(w.second, 37);
468 z = z * k0 + Rotate(w.first, 27);
471 // If 0 < len < 128, hash up to 4 chunks of 32 bytes each from the end of s.
472 for (size_t tail_done = 0; tail_done < len; ) {
474 y = Rotate(x + y, 42) * k0 + v.second;
475 w.first += Fetch64(s + len - tail_done + 16);
476 x = x * k0 + w.first;
477 z += w.second + Fetch64(s + len - tail_done);
479 v = WeakHashLen32WithSeeds(s + len - tail_done, v.first + z, v.second);
482 // At this point our 56 bytes of state should contain more than
483 // enough information for a strong 128-bit hash. We use two
484 // different 56-byte-to-8-byte hashes to get a 16-byte final result.
485 x = HashLen16(x, v.first);
486 y = HashLen16(y + z, w.first);
487 return uint128(HashLen16(x + v.second, w.second) + y,
488 HashLen16(x + w.second, y + v.second));
491 uint128 CityHash128(const char *s, size_t len) {
493 CityHash128WithSeed(s + 16, len - 16,
494 uint128(Fetch64(s), Fetch64(s + 8) + k0)) :
495 CityHash128WithSeed(s, len, uint128(k0, k1));
500 #include <nmmintrin.h>
502 // Requires len >= 240.
503 static void CityHashCrc256Long(const char *s, size_t len,
504 uint32 seed, uint64 *result) {
505 uint64 a = Fetch64(s + 56) + k0;
506 uint64 b = Fetch64(s + 96) + k0;
507 uint64 c = result[0] = HashLen16(b, len);
508 uint64 d = result[1] = Fetch64(s + 120) * k0 + len;
509 uint64 e = Fetch64(s + 184) + seed;
517 // 240 bytes of input per iter.
518 size_t iters = len / 240;
525 c += Fetch64(s + 8); \
526 d += Fetch64(s + 16); \
527 e += Fetch64(s + 24); \
528 f += Fetch64(s + 32); \
536 z = _mm_crc32_u64(z, b + g); \
537 y = _mm_crc32_u64(y, e + h); \
538 x = _mm_crc32_u64(x, f + a); \
543 CHUNK(0); PERMUTE3(a, h, c);
544 CHUNK(33); PERMUTE3(a, h, f);
545 CHUNK(0); PERMUTE3(b, h, f);
546 CHUNK(42); PERMUTE3(b, h, d);
547 CHUNK(0); PERMUTE3(b, h, e);
548 CHUNK(33); PERMUTE3(a, h, e);
549 } while (--iters > 0);
562 // cppcheck-suppress uselessAssignmentPtrArg
572 a = HashLen16(a, g + z);
575 c = HashLen16(c, z) + h;
576 d = HashLen16(d, e + result[0]);
578 h += HashLen16(x, f);
579 e = HashLen16(a, d) + g;
580 z = HashLen16(b, c) + a;
581 y = HashLen16(g, h) + c;
582 result[0] = e + z + y + x;
583 a = ShiftMix((a + y) * k0) * k0 + b;
584 result[1] += a + result[0];
585 a = ShiftMix(a * k0) * k0 + c;
586 result[2] = a + result[1];
587 a = ShiftMix((a + e) * k0) * k0;
588 result[3] = a + result[2];
591 // Requires len < 240.
592 static void CityHashCrc256Short(const char *s, size_t len, uint64 *result) {
595 memset(buf + len, 0, 240 - len);
596 CityHashCrc256Long(buf, 240, ~static_cast<uint32>(len), result);
599 void CityHashCrc256(const char *s, size_t len, uint64 *result) {
600 if (LIKELY(len >= 240)) {
601 CityHashCrc256Long(s, len, 0, result);
603 CityHashCrc256Short(s, len, result);
607 uint128 CityHashCrc128WithSeed(const char *s, size_t len, uint128 seed) {
609 return CityHash128WithSeed(s, len, seed);
612 CityHashCrc256(s, len, result);
613 uint64 u = Uint128High64(seed) + result[0];
614 uint64 v = Uint128Low64(seed) + result[1];
615 return uint128(HashLen16(u, v + result[2]),
616 HashLen16(Rotate(v, 32), u * k0 + result[3]));
620 uint128 CityHashCrc128(const char *s, size_t len) {
622 return CityHash128(s, len);
625 CityHashCrc256(s, len, result);
626 return uint128(result[2], result[3]);
631 #endif // #if CDS_BUILD_BITS == 64