39#if defined(__SSE4_2__) && defined(__x86_64__)
57 const uint64_t kMul = 0x9ddfea08eb382d69ULL;
66static uint64_t UNALIGNED_LOAD64(
const char *
p) {
68 memcpy(&result,
p,
sizeof(result));
72static uint32_t UNALIGNED_LOAD32(
const char *
p) {
74 memcpy(&result,
p,
sizeof(result));
81#define bswap_32(x) _byteswap_ulong(x)
82#define bswap_64(x) _byteswap_uint64(x)
84#elif defined(__APPLE__)
87#include <libkern/OSByteOrder.h>
88#define bswap_32(x) OSSwapInt32(x)
89#define bswap_64(x) OSSwapInt64(x)
91#elif defined(__sun) || defined(sun)
93#include <sys/byteorder.h>
94#define bswap_32(x) BSWAP_32(x)
95#define bswap_64(x) BSWAP_64(x)
97#elif defined(__FreeBSD__)
99#include <sys/endian.h>
100#define bswap_32(x) bswap32(x)
101#define bswap_64(x) bswap64(x)
103#elif defined(__OpenBSD__)
105#include <sys/types.h>
106#define bswap_32(x) swap32(x)
107#define bswap_64(x) swap64(x)
109#elif defined(__NetBSD__)
111#include <sys/types.h>
112#include <machine/bswap.h>
113#if defined(__BSWAP_RENAME) && !defined(__bswap_32)
114#define bswap_32(x) bswap32(x)
115#define bswap_64(x) bswap64(x)
124#ifdef WORDS_BIGENDIAN
125#define uint32_in_expected_order(x) (bswap_32(x))
126#define uint64_in_expected_order(x) (bswap_64(x))
128#define uint32_in_expected_order(x) (x)
129#define uint64_in_expected_order(x) (x)
133#if HAVE_BUILTIN_EXPECT
134#define LIKELY(x) (__builtin_expect(!!(x), 1))
149static const uint64_t k0 = 0xc3a5c85c97cb3127ULL;
150static const uint64_t k1 = 0xb492b66fbe98f273ULL;
151static const uint64_t k2 = 0x9ae16a3b2f90404fULL;
154static const uint32_t c1 = 0xcc9e2d51;
155static const uint32_t c2 = 0x1b873593;
170 return shift == 0 ? val : ((val >> shift) | (val << (32 - shift)));
174#define PERMUTE3(a, b, c) do { std::swap(a, b); std::swap(a, c); } while (0)
183 return h * 5 + 0xe6546b64;
186static uint32_t Hash32Len13to24(
const char *
s,
size_t len) {
195 return fmix(Mur(
f, Mur(e, Mur(d, Mur(c, Mur(b, Mur(
a, h)))))));
198static uint32_t Hash32Len0to4(
const char *
s,
size_t len) {
201 for (
size_t i = 0; i <
len; i++) {
202 signed char v =
s[i];
206 return fmix(Mur(b, Mur(
len, c)));
209static uint32_t Hash32Len5to12(
const char *
s,
size_t len) {
212 b += Fetch32(
s +
len - 4);
213 c += Fetch32(
s + ((
len >> 1) & 4));
214 return fmix(Mur(c, Mur(b, Mur(
a, d))));
220 (
len <= 4 ? Hash32Len0to4(
s,
len) : Hash32Len5to12(
s,
len)) :
221 Hash32Len13to24(
s,
len);
226 uint32_t a0 = Rotate32(Fetch32(
s +
len - 4) * c1, 17) * c2;
227 uint32_t a1 = Rotate32(Fetch32(
s +
len - 8) * c1, 17) * c2;
228 uint32_t a2 = Rotate32(Fetch32(
s +
len - 16) * c1, 17) * c2;
229 uint32_t a3 = Rotate32(Fetch32(
s +
len - 12) * c1, 17) * c2;
230 uint32_t a4 = Rotate32(Fetch32(
s +
len - 20) * c1, 17) * c2;
233 h = h * 5 + 0xe6546b64;
236 h = h * 5 + 0xe6546b64;
239 g = g * 5 + 0xe6546b64;
242 g = g * 5 + 0xe6546b64;
245 f =
f * 5 + 0xe6546b64;
246 size_t iters = (
len - 1) / 20;
248 uint32_t a0 = Rotate32(Fetch32(
s) * c1, 17) * c2;
250 uint32_t a2 = Rotate32(Fetch32(
s + 8) * c1, 17) * c2;
251 uint32_t a3 = Rotate32(Fetch32(
s + 12) * c1, 17) * c2;
255 h = h * 5 + 0xe6546b64;
261 g = g * 5 + 0xe6546b64;
264 h = h * 5 + 0xe6546b64;
272 }
while (--iters != 0);
273 g = Rotate32(g, 11) * c1;
274 g = Rotate32(g, 17) * c1;
275 f = Rotate32(
f, 11) * c1;
276 f = Rotate32(
f, 17) * c1;
277 h = Rotate32(h + g, 19);
278 h = h * 5 + 0xe6546b64;
279 h = Rotate32(h, 17) * c1;
280 h = Rotate32(h +
f, 19);
281 h = h * 5 + 0xe6546b64;
282 h = Rotate32(h, 17) * c1;
290 return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
294 return val ^ (val >> 47);
311static uint64_t HashLen0to16(
const char *
s,
size_t len) {
318 return HashLen16(c, d,
mul);
323 return HashLen16(
len + (
a << 3), Fetch32(
s +
len - 4),
mul);
331 return ShiftMix(
y * k2 ^ z * k0) * k2;
338static uint64_t HashLen17to32(
const char *
s,
size_t len) {
344 return HashLen16(Rotate(
a + b, 43) + Rotate(c, 30) + d,
345 a + Rotate(b + k2, 18) + c,
mul);
350static pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(
353 b = Rotate(b +
a + z, 21);
358 return make_pair(
a + z, b + c);
362static pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(
364 return WeakHashLen32WithSeeds(Fetch64(
s),
373static uint64_t HashLen33to64(
const char *
s,
size_t len) {
383 uint64_t u = Rotate(
a + g, 43) + (Rotate(b, 30) + c) * 9;
389 a = bswap_64((x + z) *
mul +
y) + b;
390 b = ShiftMix((z +
a) *
mul + d + h) *
mul;
397 return HashLen0to16(
s,
len);
399 return HashLen17to32(
s,
len);
401 }
else if (
len <= 64) {
402 return HashLen33to64(
s,
len);
410 pair<uint64_t, uint64_t> v = WeakHashLen32WithSeeds(
s +
len - 64,
len, z);
411 pair<uint64_t, uint64_t> w = WeakHashLen32WithSeeds(
s +
len - 32,
y + k1, x);
412 x = x * k1 + Fetch64(
s);
415 len = (
len - 1) & ~
static_cast<size_t>(63);
417 x = Rotate(x +
y + v.first + Fetch64(
s + 8), 37) * k1;
418 y = Rotate(
y + v.second + Fetch64(
s + 48), 42) * k1;
420 y += v.first + Fetch64(
s + 40);
421 z = Rotate(z + w.first, 33) * k1;
422 v = WeakHashLen32WithSeeds(
s, v.second * k1, x + w.first);
423 w = WeakHashLen32WithSeeds(
s + 32, z + w.second,
y + Fetch64(
s + 16));
428 return HashLen16(HashLen16(v.first, w.first) + ShiftMix(
y) * k1 + z,
429 HashLen16(v.second, w.second) + x);
443static uint128 CityMurmur(
const char *
s,
size_t len, uint128 seed) {
448 signed long l =
len - 16;
450 a = ShiftMix(
a * k1) * k1;
451 c = b * k1 + HashLen0to16(
s,
len);
452 d = ShiftMix(
a + (
len >= 8 ? Fetch64(
s) : c));
454 c = HashLen16(Fetch64(
s +
len - 8) + k1,
a);
455 d = HashLen16(b +
len, c + Fetch64(
s +
len - 16));
458 a ^= ShiftMix(Fetch64(
s) * k1) * k1;
461 c ^= ShiftMix(Fetch64(
s + 8) * k1) * k1;
470 return uint128(
a ^ b, HashLen16(b,
a));
475 return CityMurmur(
s,
len, seed);
480 pair<uint64_t, uint64_t> v, w;
484 v.first = Rotate(
y ^ k1, 49) * k1 + Fetch64(
s);
485 v.second = Rotate(v.first, 42) * k1 + Fetch64(
s + 8);
486 w.first = Rotate(
y + z, 35) * k1 + x;
487 w.second = Rotate(x + Fetch64(
s + 88), 53) * k1;
491 x = Rotate(x +
y + v.first + Fetch64(
s + 8), 37) * k1;
492 y = Rotate(
y + v.second + Fetch64(
s + 48), 42) * k1;
494 y += v.first + Fetch64(
s + 40);
495 z = Rotate(z + w.first, 33) * k1;
496 v = WeakHashLen32WithSeeds(
s, v.second * k1, x + w.first);
497 w = WeakHashLen32WithSeeds(
s + 32, z + w.second,
y + Fetch64(
s + 16));
500 x = Rotate(x +
y + v.first + Fetch64(
s + 8), 37) * k1;
501 y = Rotate(
y + v.second + Fetch64(
s + 48), 42) * k1;
503 y += v.first + Fetch64(
s + 40);
504 z = Rotate(z + w.first, 33) * k1;
505 v = WeakHashLen32WithSeeds(
s, v.second * k1, x + w.first);
506 w = WeakHashLen32WithSeeds(
s + 32, z + w.second,
y + Fetch64(
s + 16));
511 x += Rotate(v.first + z, 49) * k0;
512 y =
y * k0 + Rotate(w.second, 37);
513 z = z * k0 + Rotate(w.first, 27);
517 for (
size_t tail_done = 0; tail_done <
len; ) {
519 y = Rotate(x +
y, 42) * k0 + v.second;
520 w.first += Fetch64(
s +
len - tail_done + 16);
521 x = x * k0 + w.first;
522 z += w.second + Fetch64(
s +
len - tail_done);
524 v = WeakHashLen32WithSeeds(
s +
len - tail_done, v.first + z, v.second);
530 x = HashLen16(x, v.first);
531 y = HashLen16(
y + z, w.first);
532 return uint128(HashLen16(x + v.second, w.second) +
y,
533 HashLen16(x + w.second,
y + v.second));
539 uint128(Fetch64(
s), Fetch64(
s + 8) + k0)) :
548static void CityHashCrc256Long(
const char *
s,
size_t len,
563 size_t iters =
len / 240;
570 c += Fetch64(s + 8); \
571 d += Fetch64(s + 16); \
572 e += Fetch64(s + 24); \
573 f += Fetch64(s + 32); \
581 z = _mm_crc32_u64(z, b + g); \
582 y = _mm_crc32_u64(y, e + h); \
583 x = _mm_crc32_u64(x, f + a); \
594 }
while (--iters > 0);
616 a = HashLen16(
a, g + z);
619 c = HashLen16(c, z) + h;
620 d = HashLen16(d, e + result[0]);
622 h += HashLen16(x,
f);
623 e = HashLen16(
a, d) + g;
624 z = HashLen16(b, c) +
a;
625 y = HashLen16(g, h) + c;
626 result[0] = e + z +
y + x;
627 a = ShiftMix((
a +
y) * k0) * k0 + b;
628 result[1] +=
a + result[0];
629 a = ShiftMix(
a * k0) * k0 + c;
630 result[2] =
a + result[1];
631 a = ShiftMix((
a + e) * k0) * k0;
632 result[3] =
a + result[2];
636static void CityHashCrc256Short(
const char *
s,
size_t len,
uint64_t *result) {
640 CityHashCrc256Long(
buf, 240, ~
static_cast<uint32_t>(
len), result);
645 CityHashCrc256Long(
s,
len, 0, result);
647 CityHashCrc256Short(
s,
len, result);
666 return uint128(HashLen16(u, v + result[2]),
667 HashLen16(Rotate(v, 32), u * k0 + result[3]));
677 return uint128(result[2], result[3]);
uint64_t _mm_crc32_u64(uint64_t a, uint64_t b)
#define PERMUTE3(a, b, c)
#define uint32_in_expected_order(x)
#define uint64_in_expected_order(x)
an implementation of 128 bit unsigned integer
uint64_t high_bits() const
uint64_t low_bits() const
uint32_t city_hash32(const char *buf, size_t len)
uint128 city_hash128(const char *s, size_t len)
uint64_t CityHash64WithSeeds(const char *s, size_t len, uint64_t seed0, uint64_t seed1)
uint64_t Uint128High64(const uint128 &x)
uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed)
uint64_t Uint128Low64(const uint128 &x)
uint64_t city_hash64(const char *buf, size_t len)
void CityHashCrc256(const char *s, size_t len, uint64_t *result)
uint64_t CityHash64WithSeed(const char *s, size_t len, uint64_t seed)
uint128 city_hash_crc_128(const char *s, size_t len)
uint128 CityHashCrc128WithSeed(const char *s, size_t len, uint128 seed)
array< uint64_t, 4 > city_hash_crc_256(const char *s, size_t len)
uint64_t Hash128to64(const uint128 &x)
void swap(picojson::value &x, picojson::value &y)
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
unsigned __int64 uint64_t
void mul(const Operand &op)
memset(pInfo->slotDescription, ' ', 64)
memcpy((char *) pInfo->slotDescription, s, l)