Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 1 | // Copyright (c) 2011 Google, Inc. |
| 2 | // |
| 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: |
| 9 | // |
| 10 | // The above copyright notice and this permission notice shall be included in |
| 11 | // all copies or substantial portions of the Software. |
| 12 | // |
| 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 |
| 19 | // THE SOFTWARE. |
| 20 | // |
| 21 | // CityHash, by Geoff Pike and Jyrki Alakuijala |
| 22 | // |
| 23 | // This file provides CityHash64() and related functions. |
| 24 | // |
| 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. |
| 29 | |
| 30 | //#include "config.h" |
| 31 | #include "city-hash.hpp" |
| 32 | #include <algorithm> |
| 33 | #include <string.h> // for memcpy and memset |
| 34 | |
| 35 | using namespace std; |
| 36 | |
| 37 | |
| 38 | static uint64 UNALIGNED_LOAD64(const char *p) { |
| 39 | uint64 result; |
| 40 | memcpy(&result, p, sizeof(result)); |
| 41 | return result; |
| 42 | } |
| 43 | |
| 44 | static uint32 UNALIGNED_LOAD32(const char *p) { |
| 45 | uint32 result; |
| 46 | memcpy(&result, p, sizeof(result)); |
| 47 | return result; |
| 48 | } |
| 49 | |
| 50 | #ifdef _MSC_VER |
| 51 | |
| 52 | #include <stdlib.h> |
| 53 | #define bswap_32(x) _byteswap_ulong(x) |
| 54 | #define bswap_64(x) _byteswap_uint64(x) |
| 55 | |
| 56 | #elif defined(__APPLE__) |
| 57 | |
| 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) |
| 62 | |
| 63 | #elif defined(__NetBSD__) |
| 64 | |
| 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) |
| 70 | #endif |
| 71 | |
| 72 | #else |
| 73 | |
| 74 | #include <byteswap.h> |
| 75 | |
| 76 | #endif |
| 77 | |
| 78 | #ifdef WORDS_BIGENDIAN |
| 79 | #define uint32_in_expected_order(x) (bswap_32(x)) |
| 80 | #define uint64_in_expected_order(x) (bswap_64(x)) |
| 81 | #else |
| 82 | #define uint32_in_expected_order(x) (x) |
| 83 | #define uint64_in_expected_order(x) (x) |
| 84 | #endif |
| 85 | |
| 86 | #if !defined(LIKELY) |
| 87 | #if HAVE_BUILTIN_EXPECT |
| 88 | #define LIKELY(x) (__builtin_expect(!!(x), 1)) |
| 89 | #else |
| 90 | #define LIKELY(x) (x) |
| 91 | #endif |
| 92 | #endif |
| 93 | |
| 94 | static uint64 Fetch64(const char *p) { |
| 95 | return uint64_in_expected_order(UNALIGNED_LOAD64(p)); |
| 96 | } |
| 97 | |
| 98 | static uint32 Fetch32(const char *p) { |
| 99 | return uint32_in_expected_order(UNALIGNED_LOAD32(p)); |
| 100 | } |
| 101 | |
| 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; |
| 106 | |
| 107 | // Magic numbers for 32-bit hashing. Copied from Murmur3. |
| 108 | static const uint32_t c1 = 0xcc9e2d51; |
| 109 | static const uint32_t c2 = 0x1b873593; |
| 110 | |
| 111 | // A 32-bit to 32-bit integer hash copied from Murmur3. |
| 112 | static uint32 fmix(uint32 h) |
| 113 | { |
| 114 | h ^= h >> 16; |
| 115 | h *= 0x85ebca6b; |
| 116 | h ^= h >> 13; |
| 117 | h *= 0xc2b2ae35; |
| 118 | h ^= h >> 16; |
| 119 | return h; |
| 120 | } |
| 121 | |
| 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))); |
| 125 | } |
| 126 | |
| 127 | #undef PERMUTE3 |
| 128 | #define PERMUTE3(a, b, c) do { std::swap(a, b); std::swap(a, c); } while (0) |
| 129 | |
| 130 | static uint32 Mur(uint32 a, uint32 h) { |
| 131 | // Helper from Murmur3 for combining two 32-bit values. |
| 132 | a *= c1; |
| 133 | a = Rotate32(a, 17); |
| 134 | a *= c2; |
| 135 | h ^= a; |
| 136 | h = Rotate32(h, 19); |
| 137 | return h * 5 + 0xe6546b64; |
| 138 | } |
| 139 | |
| 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); |
| 147 | uint32 h = len; |
| 148 | |
| 149 | return fmix(Mur(f, Mur(e, Mur(d, Mur(c, Mur(b, Mur(a, h))))))); |
| 150 | } |
| 151 | |
| 152 | static uint32 Hash32Len0to4(const char *s, size_t len) { |
| 153 | uint32 b = 0; |
| 154 | uint32 c = 9; |
| 155 | for (size_t i = 0; i < len; i++) { |
| 156 | signed char v = s[i]; |
| 157 | b = b * c1 + v; |
| 158 | c ^= b; |
| 159 | } |
| 160 | return fmix(Mur(b, Mur(len, c))); |
| 161 | } |
| 162 | |
| 163 | static uint32 Hash32Len5to12(const char *s, size_t len) { |
| 164 | uint32 a = len, b = len * 5, c = 9, d = b; |
| 165 | a += Fetch32(s); |
| 166 | b += Fetch32(s + len - 4); |
| 167 | c += Fetch32(s + ((len >> 1) & 4)); |
| 168 | return fmix(Mur(c, Mur(b, Mur(a, d)))); |
| 169 | } |
| 170 | |
| 171 | uint32 CityHash32(const char *s, size_t len) { |
| 172 | if (len <= 24) { |
| 173 | return len <= 12 ? |
| 174 | (len <= 4 ? Hash32Len0to4(s, len) : Hash32Len5to12(s, len)) : |
| 175 | Hash32Len13to24(s, len); |
| 176 | } |
| 177 | |
| 178 | // len > 24 |
| 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; |
| 185 | h ^= a0; |
| 186 | h = Rotate32(h, 19); |
| 187 | h = h * 5 + 0xe6546b64; |
| 188 | h ^= a2; |
| 189 | h = Rotate32(h, 19); |
| 190 | h = h * 5 + 0xe6546b64; |
| 191 | g ^= a1; |
| 192 | g = Rotate32(g, 19); |
| 193 | g = g * 5 + 0xe6546b64; |
| 194 | g ^= a3; |
| 195 | g = Rotate32(g, 19); |
| 196 | g = g * 5 + 0xe6546b64; |
| 197 | f += a4; |
| 198 | f = Rotate32(f, 19); |
| 199 | f = f * 5 + 0xe6546b64; |
| 200 | size_t iters = (len - 1) / 20; |
| 201 | do { |
| 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); |
| 207 | h ^= a0; |
| 208 | h = Rotate32(h, 18); |
| 209 | h = h * 5 + 0xe6546b64; |
| 210 | f += a1; |
| 211 | f = Rotate32(f, 19); |
| 212 | f = f * c1; |
| 213 | g += a2; |
| 214 | g = Rotate32(g, 18); |
| 215 | g = g * 5 + 0xe6546b64; |
| 216 | h ^= a3 + a1; |
| 217 | h = Rotate32(h, 19); |
| 218 | h = h * 5 + 0xe6546b64; |
| 219 | g ^= a4; |
| 220 | g = bswap_32(g) * 5; |
| 221 | h += a4 * 5; |
| 222 | h = bswap_32(h); |
| 223 | f += a0; |
| 224 | PERMUTE3(f, h, g); |
| 225 | s += 20; |
| 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; |
| 237 | return h; |
| 238 | } |
| 239 | |
| 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))); |
| 245 | } |
| 246 | |
| 247 | static uint64 ShiftMix(uint64 val) { |
| 248 | return val ^ (val >> 47); |
| 249 | } |
| 250 | |
| 251 | static uint64 HashLen16(uint64 u, uint64 v) { |
| 252 | return Hash128to64(uint128(u, v)); |
| 253 | } |
| 254 | |
| 255 | static uint64 HashLen16(uint64 u, uint64 v, uint64 mul) { |
| 256 | // Murmur-inspired hashing. |
| 257 | uint64 a = (u ^ v) * mul; |
| 258 | a ^= (a >> 47); |
| 259 | uint64 b = (v ^ a) * mul; |
| 260 | b ^= (b >> 47); |
| 261 | b *= mul; |
| 262 | return b; |
| 263 | } |
| 264 | |
| 265 | static uint64 HashLen0to16(const char *s, size_t len) { |
| 266 | if (len >= 8) { |
| 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); |
| 273 | } |
| 274 | if (len >= 4) { |
| 275 | uint64 mul = k2 + len * 2; |
| 276 | uint64 a = Fetch32(s); |
| 277 | return HashLen16(len + (a << 3), Fetch32(s + len - 4), mul); |
| 278 | } |
| 279 | if (len > 0) { |
| 280 | uint8 a = s[0]; |
| 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; |
| 286 | } |
| 287 | return k2; |
| 288 | } |
| 289 | |
| 290 | // This probably works well for 16-byte strings as well, but it may be overkill |
| 291 | // in that case. |
| 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); |
| 300 | } |
| 301 | |
| 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) { |
| 306 | a += w; |
| 307 | b = Rotate(b + a + z, 21); |
| 308 | uint64 c = a; |
| 309 | a += x; |
| 310 | a += y; |
| 311 | b += Rotate(a, 44); |
| 312 | return make_pair(a + z, b + c); |
| 313 | } |
| 314 | |
| 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), |
| 319 | Fetch64(s + 8), |
| 320 | Fetch64(s + 16), |
| 321 | Fetch64(s + 24), |
| 322 | a, |
| 323 | b); |
| 324 | } |
| 325 | |
| 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; |
| 345 | return b + x; |
| 346 | } |
| 347 | |
| 348 | uint64 CityHash64(const char *s, size_t len) { |
| 349 | if (len <= 32) { |
| 350 | if (len <= 16) { |
| 351 | return HashLen0to16(s, len); |
| 352 | } else { |
| 353 | return HashLen17to32(s, len); |
| 354 | } |
| 355 | } else if (len <= 64) { |
| 356 | return HashLen33to64(s, len); |
| 357 | } |
| 358 | |
| 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); |
| 367 | |
| 368 | // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks. |
| 369 | len = (len - 1) & ~static_cast<size_t>(63); |
| 370 | do { |
| 371 | x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1; |
| 372 | y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1; |
| 373 | x ^= w.second; |
| 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)); |
| 378 | std::swap(z, x); |
| 379 | s += 64; |
| 380 | len -= 64; |
| 381 | } while (len != 0); |
| 382 | return HashLen16(HashLen16(v.first, w.first) + ShiftMix(y) * k1 + z, |
| 383 | HashLen16(v.second, w.second) + x); |
| 384 | } |
| 385 | |
| 386 | uint64 CityHash64WithSeed(const char *s, size_t len, uint64 seed) { |
| 387 | return CityHash64WithSeeds(s, len, k2, seed); |
| 388 | } |
| 389 | |
| 390 | uint64 CityHash64WithSeeds(const char *s, size_t len, |
| 391 | uint64 seed0, uint64 seed1) { |
| 392 | return HashLen16(CityHash64(s, len) - seed0, seed1); |
| 393 | } |
| 394 | |
| 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); |
| 400 | uint64 c = 0; |
| 401 | uint64 d = 0; |
| 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)); |
| 407 | } else { // len > 16 |
| 408 | c = HashLen16(Fetch64(s + len - 8) + k1, a); |
| 409 | d = HashLen16(b + len, c + Fetch64(s + len - 16)); |
| 410 | a += d; |
| 411 | do { |
| 412 | a ^= ShiftMix(Fetch64(s) * k1) * k1; |
| 413 | a *= k1; |
| 414 | b ^= a; |
| 415 | c ^= ShiftMix(Fetch64(s + 8) * k1) * k1; |
| 416 | c *= k1; |
| 417 | d ^= c; |
| 418 | s += 16; |
| 419 | l -= 16; |
| 420 | } while (l > 0); |
| 421 | } |
| 422 | a = HashLen16(a, c); |
| 423 | b = HashLen16(d, b); |
| 424 | return uint128(a ^ b, HashLen16(b, a)); |
| 425 | } |
| 426 | |
| 427 | uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed) { |
| 428 | if (len < 128) { |
| 429 | return CityMurmur(s, len, seed); |
| 430 | } |
| 431 | |
| 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); |
| 437 | uint64 z = len * k1; |
| 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; |
| 442 | |
| 443 | // This is the same inner loop as CityHash64(), manually unrolled. |
| 444 | do { |
| 445 | x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1; |
| 446 | y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1; |
| 447 | x ^= w.second; |
| 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)); |
| 452 | std::swap(z, x); |
| 453 | s += 64; |
| 454 | x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1; |
| 455 | y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1; |
| 456 | x ^= w.second; |
| 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)); |
| 461 | std::swap(z, x); |
| 462 | s += 64; |
| 463 | len -= 128; |
| 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); |
| 468 | w.first *= 9; |
| 469 | v.first *= k0; |
| 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; ) { |
| 472 | tail_done += 32; |
| 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); |
| 477 | w.second += v.first; |
| 478 | v = WeakHashLen32WithSeeds(s + len - tail_done, v.first + z, v.second); |
| 479 | v.first *= k0; |
| 480 | } |
| 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)); |
| 488 | } |
| 489 | |
| 490 | uint128 CityHash128(const char *s, size_t len) { |
| 491 | return len >= 16 ? |
| 492 | CityHash128WithSeed(s + 16, len - 16, |
| 493 | uint128(Fetch64(s), Fetch64(s + 8) + k0)) : |
| 494 | CityHash128WithSeed(s, len, uint128(k0, k1)); |
| 495 | } |
| 496 | |
| 497 | #ifdef __SSE4_2__ |
| 498 | #include <citycrc.h> |
| 499 | #include <nmmintrin.h> |
| 500 | |
| 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; |
| 509 | uint64 f = 0; |
| 510 | uint64 g = 0; |
| 511 | uint64 h = c + d; |
| 512 | uint64 x = seed; |
| 513 | uint64 y = 0; |
| 514 | uint64 z = 0; |
| 515 | |
| 516 | // 240 bytes of input per iter. |
| 517 | size_t iters = len / 240; |
| 518 | len -= iters * 240; |
| 519 | do { |
| 520 | #undef CHUNK |
| 521 | #define CHUNK(r) \ |
| 522 | PERMUTE3(x, z, y); \ |
| 523 | b += Fetch64(s); \ |
| 524 | c += Fetch64(s + 8); \ |
| 525 | d += Fetch64(s + 16); \ |
| 526 | e += Fetch64(s + 24); \ |
| 527 | f += Fetch64(s + 32); \ |
| 528 | a += b; \ |
| 529 | h += f; \ |
| 530 | b += c; \ |
| 531 | f += d; \ |
| 532 | g += e; \ |
| 533 | e += z; \ |
| 534 | g += x; \ |
| 535 | z = _mm_crc32_u64(z, b + g); \ |
| 536 | y = _mm_crc32_u64(y, e + h); \ |
| 537 | x = _mm_crc32_u64(x, f + a); \ |
| 538 | e = Rotate(e, r); \ |
| 539 | c += e; \ |
| 540 | s += 40 |
| 541 | |
| 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); |
| 549 | |
| 550 | while (len >= 40) { |
| 551 | CHUNK(29); |
| 552 | e ^= Rotate(a, 20); |
| 553 | h += Rotate(b, 30); |
| 554 | g ^= Rotate(c, 40); |
| 555 | f += Rotate(d, 34); |
| 556 | PERMUTE3(c, h, g); |
| 557 | len -= 40; |
| 558 | } |
| 559 | if (len > 0) { |
| 560 | s = s + len - 40; |
| 561 | CHUNK(33); |
| 562 | e ^= Rotate(a, 43); |
| 563 | h += Rotate(b, 42); |
| 564 | g ^= Rotate(c, 41); |
| 565 | f += Rotate(d, 40); |
| 566 | } |
| 567 | result[0] ^= h; |
| 568 | result[1] ^= g; |
| 569 | g += h; |
| 570 | a = HashLen16(a, g + z); |
| 571 | x += y << 32; |
| 572 | b += x; |
| 573 | c = HashLen16(c, z) + h; |
| 574 | d = HashLen16(d, e + result[0]); |
| 575 | g += e; |
| 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]; |
| 587 | } |
| 588 | |
| 589 | // Requires len < 240. |
| 590 | static void CityHashCrc256Short(const char *s, size_t len, uint64 *result) { |
| 591 | char buf[240]; |
| 592 | memcpy(buf, s, len); |
| 593 | memset(buf + len, 0, 240 - len); |
| 594 | CityHashCrc256Long(buf, 240, ~static_cast<uint32>(len), result); |
| 595 | } |
| 596 | |
| 597 | void CityHashCrc256(const char *s, size_t len, uint64 *result) { |
| 598 | if (LIKELY(len >= 240)) { |
| 599 | CityHashCrc256Long(s, len, 0, result); |
| 600 | } else { |
| 601 | CityHashCrc256Short(s, len, result); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | uint128 CityHashCrc128WithSeed(const char *s, size_t len, uint128 seed) { |
| 606 | if (len <= 900) { |
| 607 | return CityHash128WithSeed(s, len, seed); |
| 608 | } else { |
| 609 | uint64 result[4]; |
| 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])); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | uint128 CityHashCrc128(const char *s, size_t len) { |
| 619 | if (len <= 900) { |
| 620 | return CityHash128(s, len); |
| 621 | } else { |
| 622 | uint64 result[4]; |
| 623 | CityHashCrc256(s, len, result); |
| 624 | return uint128(result[2], result[3]); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | #endif |