diff --git a/base/third_party/cityhash/city.cc b/base/third_party/cityhash/city.cc index 41cd5ee16993..22e3b4981ff5 100644 --- a/base/third_party/cityhash/city.cc +++ b/base/third_party/cityhash/city.cc @@ -27,25 +27,13 @@ // possible hash functions, by using SIMD instructions, or by // compromising on hash quality. -#include "config.h" -#include +#include "city.h" #include #include // for memcpy and memset -using namespace std; - -static uint64 UNALIGNED_LOAD64(const char *p) { - uint64 result; - memcpy(&result, p, sizeof(result)); - return result; -} - -static uint32 UNALIGNED_LOAD32(const char *p) { - uint32 result; - memcpy(&result, p, sizeof(result)); - return result; -} +using std::make_pair; +using std::pair; #ifdef _MSC_VER @@ -89,10 +77,19 @@ static uint32 UNALIGNED_LOAD32(const char *p) { #else -#include +// XXX(cavalcanti): building 'native_client' fails with this header. +//#include + +// Falling back to compiler builtins instead. +#define bswap_32(x) __builtin_bswap32(x) +#define bswap_64(x) __builtin_bswap64(x) #endif +namespace base { +namespace internal { +namespace cityhash_v111 { + #ifdef WORDS_BIGENDIAN #define uint32_in_expected_order(x) (bswap_32(x)) #define uint64_in_expected_order(x) (bswap_64(x)) @@ -109,6 +106,18 @@ static uint32 UNALIGNED_LOAD32(const char *p) { #endif #endif +static uint64 UNALIGNED_LOAD64(const char *p) { + uint64 result; + memcpy(&result, p, sizeof(result)); + return result; +} + +static uint32 UNALIGNED_LOAD32(const char *p) { + uint32 result; + memcpy(&result, p, sizeof(result)); + return result; +} + static uint64 Fetch64(const char *p) { return uint64_in_expected_order(UNALIGNED_LOAD64(p)); } @@ -217,11 +226,11 @@ uint32 CityHash32(const char *s, size_t len) { f = f * 5 + 0xe6546b64; size_t iters = (len - 1) / 20; do { - uint32 a0 = Rotate32(Fetch32(s) * c1, 17) * c2; - uint32 a1 = Fetch32(s + 4); - uint32 a2 = Rotate32(Fetch32(s + 8) * c1, 17) * c2; - uint32 a3 = Rotate32(Fetch32(s + 12) * c1, 17) * c2; - uint32 a4 = Fetch32(s + 16); + a0 = Rotate32(Fetch32(s) * c1, 17) * c2; + a1 = Fetch32(s + 4); + a2 = Rotate32(Fetch32(s + 8) * c1, 17) * c2; + a3 = Rotate32(Fetch32(s + 12) * c1, 17) * c2; + a4 = Fetch32(s + 16); h ^= a0; h = Rotate32(h, 18); h = h * 5 + 0xe6546b64; @@ -512,135 +521,7 @@ uint128 CityHash128(const char *s, size_t len) { CityHash128WithSeed(s, len, uint128(k0, k1)); } -#ifdef __SSE4_2__ -#include -#include - -// Requires len >= 240. -static void CityHashCrc256Long(const char *s, size_t len, - uint32 seed, uint64 *result) { - uint64 a = Fetch64(s + 56) + k0; - uint64 b = Fetch64(s + 96) + k0; - uint64 c = result[0] = HashLen16(b, len); - uint64 d = result[1] = Fetch64(s + 120) * k0 + len; - uint64 e = Fetch64(s + 184) + seed; - uint64 f = 0; - uint64 g = 0; - uint64 h = c + d; - uint64 x = seed; - uint64 y = 0; - uint64 z = 0; - - // 240 bytes of input per iter. - size_t iters = len / 240; - len -= iters * 240; - do { -#undef CHUNK -#define CHUNK(r) \ - PERMUTE3(x, z, y); \ - b += Fetch64(s); \ - c += Fetch64(s + 8); \ - d += Fetch64(s + 16); \ - e += Fetch64(s + 24); \ - f += Fetch64(s + 32); \ - a += b; \ - h += f; \ - b += c; \ - f += d; \ - g += e; \ - e += z; \ - g += x; \ - z = _mm_crc32_u64(z, b + g); \ - y = _mm_crc32_u64(y, e + h); \ - x = _mm_crc32_u64(x, f + a); \ - e = Rotate(e, r); \ - c += e; \ - s += 40 - - CHUNK(0); PERMUTE3(a, h, c); - CHUNK(33); PERMUTE3(a, h, f); - CHUNK(0); PERMUTE3(b, h, f); - CHUNK(42); PERMUTE3(b, h, d); - CHUNK(0); PERMUTE3(b, h, e); - CHUNK(33); PERMUTE3(a, h, e); - } while (--iters > 0); - - while (len >= 40) { - CHUNK(29); - e ^= Rotate(a, 20); - h += Rotate(b, 30); - g ^= Rotate(c, 40); - f += Rotate(d, 34); - PERMUTE3(c, h, g); - len -= 40; - } - if (len > 0) { - s = s + len - 40; - CHUNK(33); - e ^= Rotate(a, 43); - h += Rotate(b, 42); - g ^= Rotate(c, 41); - f += Rotate(d, 40); - } - result[0] ^= h; - result[1] ^= g; - g += h; - a = HashLen16(a, g + z); - x += y << 32; - b += x; - c = HashLen16(c, z) + h; - d = HashLen16(d, e + result[0]); - g += e; - h += HashLen16(x, f); - e = HashLen16(a, d) + g; - z = HashLen16(b, c) + a; - y = HashLen16(g, h) + c; - result[0] = e + z + y + x; - a = ShiftMix((a + y) * k0) * k0 + b; - result[1] += a + result[0]; - a = ShiftMix(a * k0) * k0 + c; - result[2] = a + result[1]; - a = ShiftMix((a + e) * k0) * k0; - result[3] = a + result[2]; -} - -// Requires len < 240. -static void CityHashCrc256Short(const char *s, size_t len, uint64 *result) { - char buf[240]; - memcpy(buf, s, len); - memset(buf + len, 0, 240 - len); - CityHashCrc256Long(buf, 240, ~static_cast(len), result); -} +} // namespace cityhash_v111 +} // namespace internal +} // namespace base -void CityHashCrc256(const char *s, size_t len, uint64 *result) { - if (LIKELY(len >= 240)) { - CityHashCrc256Long(s, len, 0, result); - } else { - CityHashCrc256Short(s, len, result); - } -} - -uint128 CityHashCrc128WithSeed(const char *s, size_t len, uint128 seed) { - if (len <= 900) { - return CityHash128WithSeed(s, len, seed); - } else { - uint64 result[4]; - CityHashCrc256(s, len, result); - uint64 u = Uint128High64(seed) + result[0]; - uint64 v = Uint128Low64(seed) + result[1]; - return uint128(HashLen16(u, v + result[2]), - HashLen16(Rotate(v, 32), u * k0 + result[3])); - } -} - -uint128 CityHashCrc128(const char *s, size_t len) { - if (len <= 900) { - return CityHash128(s, len); - } else { - uint64 result[4]; - CityHashCrc256(s, len, result); - return uint128(result[2], result[3]); - } -} - -#endif diff --git a/base/third_party/cityhash/city.h b/base/third_party/cityhash/city.h index 94499ce419c7..e4672f6d44da 100644 --- a/base/third_party/cityhash/city.h +++ b/base/third_party/cityhash/city.h @@ -59,13 +59,20 @@ // of a+b is easily derived from the hashes of a and b. This property // doesn't hold for any hash functions in this file. -#ifndef CITY_HASH_H_ -#define CITY_HASH_H_ +#ifndef BASE_THIRD_PARTY_CITYHASH_CITY_H_ +#define BASE_THIRD_PARTY_CITYHASH_CITY_H_ #include // for size_t. #include #include +// XXX(cavalcantii): Declaring it inside of the 'base' namespace allows to +// handle linker symbol clash error with deprecated CityHash from +// third_party/smhasher in a few unit tests. +namespace base { +namespace internal { +namespace cityhash_v111 { + typedef uint8_t uint8; typedef uint32_t uint32; typedef uint64_t uint64; @@ -109,4 +116,8 @@ inline uint64 Hash128to64(const uint128& x) { return b; } +} // namespace cityhash_v111 +} // namespace internal +} // namespace base + #endif // CITY_HASH_H_