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.h>
32
33#include <algorithm>
34#include <string.h> // for memcpy and memset
35
36using namespace std;
37
38static uint64 UNALIGNED_LOAD64(const char *p) {
39 uint64 result;
40 memcpy(&result, p, sizeof(result));
41 return result;
42}
43
44static 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(__sun) || defined(sun)
64
65#include <sys/byteorder.h>
66#define bswap_32(x) BSWAP_32(x)
67#define bswap_64(x) BSWAP_64(x)
68
69#elif defined(__FreeBSD__)
70
71#include <sys/endian.h>
72#define bswap_32(x) bswap32(x)
73#define bswap_64(x) bswap64(x)
74
75#elif defined(__OpenBSD__)
76
77#include <sys/types.h>
78#define bswap_32(x) swap32(x)
79#define bswap_64(x) swap64(x)
80
81#elif defined(__NetBSD__)
82
83#include <sys/types.h>
84#include <machine/bswap.h>
85#if defined(__BSWAP_RENAME) && !defined(__bswap_32)
86#define bswap_32(x) bswap32(x)
87#define bswap_64(x) bswap64(x)
88#endif
89
90#else
91
92#include <byteswap.h>
93
94#endif
95
96#ifdef WORDS_BIGENDIAN
97#define uint32_in_expected_order(x) (bswap_32(x))
98#define uint64_in_expected_order(x) (bswap_64(x))
99#else
100#define uint32_in_expected_order(x) (x)
101#define uint64_in_expected_order(x) (x)
102#endif
103
104#if !defined(LIKELY)
105#if HAVE_BUILTIN_EXPECT
106#define LIKELY(x) (__builtin_expect(!!(x), 1))
107#else
108#define LIKELY(x) (x)
109#endif
110#endif
111
112static uint64 Fetch64(const char *p) {
113 return uint64_in_expected_order(UNALIGNED_LOAD64(p));
114}
115
116static uint32 Fetch32(const char *p) {
117 return uint32_in_expected_order(UNALIGNED_LOAD32(p));
118}
119
120// Some primes between 2^63 and 2^64 for various uses.
121static const uint64 k0 = 0xc3a5c85c97cb3127ULL;
122static const uint64 k1 = 0xb492b66fbe98f273ULL;
123static const uint64 k2 = 0x9ae16a3b2f90404fULL;
124
125// Magic numbers for 32-bit hashing. Copied from Murmur3.
126static const uint32 c1 = 0xcc9e2d51;
127static const uint32 c2 = 0x1b873593;
128
129// A 32-bit to 32-bit integer hash copied from Murmur3.
130static uint32 fmix(uint32 h)
131{
132 h ^= h >> 16;
133 h *= 0x85ebca6b;
134 h ^= h >> 13;
135 h *= 0xc2b2ae35;
136 h ^= h >> 16;
137 return h;
138}
139
140static uint32 Rotate32(uint32 val, int shift) {
141 // Avoid shifting by 32: doing so yields an undefined result.
142 return shift == 0 ? val : ((val >> shift) | (val << (32 - shift)));
143}
144
145#undef PERMUTE3
146#define PERMUTE3(a, b, c) do { std::swap(a, b); std::swap(a, c); } while (0)
147
148static uint32 Mur(uint32 a, uint32 h) {
149 // Helper from Murmur3 for combining two 32-bit values.
150 a *= c1;
151 a = Rotate32(a, 17);
152 a *= c2;
153 h ^= a;
154 h = Rotate32(h, 19);
155 return h * 5 + 0xe6546b64;
156}
157
158static uint32 Hash32Len13to24(const char *s, size_t len) {
159 uint32 a = Fetch32(s - 4 + (len >> 1));
160 uint32 b = Fetch32(s + 4);
161 uint32 c = Fetch32(s + len - 8);
162 uint32 d = Fetch32(s + (len >> 1));
163 uint32 e = Fetch32(s);
164 uint32 f = Fetch32(s + len - 4);
165 uint32 h = len;
166
167 return fmix(Mur(f, Mur(e, Mur(d, Mur(c, Mur(b, Mur(a, h)))))));
168}
169
170static uint32 Hash32Len0to4(const char *s, size_t len) {
171 uint32 b = 0;
172 uint32 c = 9;
173 for (size_t i = 0; i < len; i++) {
174 signed char v = s[i];
175 b = b * c1 + v;
176 c ^= b;
177 }
178 return fmix(Mur(b, Mur(len, c)));
179}
180
181static uint32 Hash32Len5to12(const char *s, size_t len) {
182 uint32 a = len, b = len * 5, c = 9, d = b;
183 a += Fetch32(s);
184 b += Fetch32(s + len - 4);
185 c += Fetch32(s + ((len >> 1) & 4));
186 return fmix(Mur(c, Mur(b, Mur(a, d))));
187}
188
189uint32 CityHash32(const char *s, size_t len) {
190 if (len <= 24) {
191 return len <= 12 ?
192 (len <= 4 ? Hash32Len0to4(s, len) : Hash32Len5to12(s, len)) :
193 Hash32Len13to24(s, len);
194 }
195
196 // len > 24
197 uint32 h = len, g = c1 * len, f = g;
198 uint32 a0 = Rotate32(Fetch32(s + len - 4) * c1, 17) * c2;
199 uint32 a1 = Rotate32(Fetch32(s + len - 8) * c1, 17) * c2;
200 uint32 a2 = Rotate32(Fetch32(s + len - 16) * c1, 17) * c2;
201 uint32 a3 = Rotate32(Fetch32(s + len - 12) * c1, 17) * c2;
202 uint32 a4 = Rotate32(Fetch32(s + len - 20) * c1, 17) * c2;
203 h ^= a0;
204 h = Rotate32(h, 19);
205 h = h * 5 + 0xe6546b64;
206 h ^= a2;
207 h = Rotate32(h, 19);
208 h = h * 5 + 0xe6546b64;
209 g ^= a1;
210 g = Rotate32(g, 19);
211 g = g * 5 + 0xe6546b64;
212 g ^= a3;
213 g = Rotate32(g, 19);
214 g = g * 5 + 0xe6546b64;
215 f += a4;
216 f = Rotate32(f, 19);
217 f = f * 5 + 0xe6546b64;
218 size_t iters = (len - 1) / 20;
219 do {
220 uint32 a0 = Rotate32(Fetch32(s) * c1, 17) * c2;
221 uint32 a1 = Fetch32(s + 4);
222 uint32 a2 = Rotate32(Fetch32(s + 8) * c1, 17) * c2;
223 uint32 a3 = Rotate32(Fetch32(s + 12) * c1, 17) * c2;
224 uint32 a4 = Fetch32(s + 16);
225 h ^= a0;
226 h = Rotate32(h, 18);
227 h = h * 5 + 0xe6546b64;
228 f += a1;
229 f = Rotate32(f, 19);
230 f = f * c1;
231 g += a2;
232 g = Rotate32(g, 18);
233 g = g * 5 + 0xe6546b64;
234 h ^= a3 + a1;
235 h = Rotate32(h, 19);
236 h = h * 5 + 0xe6546b64;
237 g ^= a4;
238 g = bswap_32(g) * 5;
239 h += a4 * 5;
240 h = bswap_32(h);
241 f += a0;
242 PERMUTE3(f, h, g);
243 s += 20;
244 } while (--iters != 0);
245 g = Rotate32(g, 11) * c1;
246 g = Rotate32(g, 17) * c1;
247 f = Rotate32(f, 11) * c1;
248 f = Rotate32(f, 17) * c1;
249 h = Rotate32(h + g, 19);
250 h = h * 5 + 0xe6546b64;
251 h = Rotate32(h, 17) * c1;
252 h = Rotate32(h + f, 19);
253 h = h * 5 + 0xe6546b64;
254 h = Rotate32(h, 17) * c1;
255 return h;
256}
257
258// Bitwise right rotate. Normally this will compile to a single
259// instruction, especially if the shift is a manifest constant.
260static uint64 Rotate(uint64 val, int shift) {
261 // Avoid shifting by 64: doing so yields an undefined result.
262 return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
263}
264
265static uint64 ShiftMix(uint64 val) {
266 return val ^ (val >> 47);
267}
268
269static uint64 HashLen16(uint64 u, uint64 v) {
270 return Hash128to64(uint128(u, v));
271}
272
273static uint64 HashLen16(uint64 u, uint64 v, uint64 mul) {
274 // Murmur-inspired hashing.
275 uint64 a = (u ^ v) * mul;
276 a ^= (a >> 47);
277 uint64 b = (v ^ a) * mul;
278 b ^= (b >> 47);
279 b *= mul;
280 return b;
281}
282
283static uint64 HashLen0to16(const char *s, size_t len) {
284 if (len >= 8) {
285 uint64 mul = k2 + len * 2;
286 uint64 a = Fetch64(s) + k2;
287 uint64 b = Fetch64(s + len - 8);
288 uint64 c = Rotate(b, 37) * mul + a;
289 uint64 d = (Rotate(a, 25) + b) * mul;
290 return HashLen16(c, d, mul);
291 }
292 if (len >= 4) {
293 uint64 mul = k2 + len * 2;
294 uint64 a = Fetch32(s);
295 return HashLen16(len + (a << 3), Fetch32(s + len - 4), mul);
296 }
297 if (len > 0) {
298 uint8 a = s[0];
299 uint8 b = s[len >> 1];
300 uint8 c = s[len - 1];
301 uint32 y = static_cast<uint32>(a) + (static_cast<uint32>(b) << 8);
302 uint32 z = len + (static_cast<uint32>(c) << 2);
303 return ShiftMix(y * k2 ^ z * k0) * k2;
304 }
305 return k2;
306}
307
308// This probably works well for 16-byte strings as well, but it may be overkill
309// in that case.
310static uint64 HashLen17to32(const char *s, size_t len) {
311 uint64 mul = k2 + len * 2;
312 uint64 a = Fetch64(s) * k1;
313 uint64 b = Fetch64(s + 8);
314 uint64 c = Fetch64(s + len - 8) * mul;
315 uint64 d = Fetch64(s + len - 16) * k2;
316 return HashLen16(Rotate(a + b, 43) + Rotate(c, 30) + d,
317 a + Rotate(b + k2, 18) + c, mul);
318}
319
320// Return a 16-byte hash for 48 bytes. Quick and dirty.
321// Callers do best to use "random-looking" values for a and b.
322static pair<uint64, uint64> WeakHashLen32WithSeeds(
323 uint64 w, uint64 x, uint64 y, uint64 z, uint64 a, uint64 b) {
324 a += w;
325 b = Rotate(b + a + z, 21);
326 uint64 c = a;
327 a += x;
328 a += y;
329 b += Rotate(a, 44);
330 return make_pair(a + z, b + c);
331}
332
333// Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
334static pair<uint64, uint64> WeakHashLen32WithSeeds(
335 const char* s, uint64 a, uint64 b) {
336 return WeakHashLen32WithSeeds(Fetch64(s),
337 Fetch64(s + 8),
338 Fetch64(s + 16),
339 Fetch64(s + 24),
340 a,
341 b);
342}
343
344// Return an 8-byte hash for 33 to 64 bytes.
345static uint64 HashLen33to64(const char *s, size_t len) {
346 uint64 mul = k2 + len * 2;
347 uint64 a = Fetch64(s) * k2;
348 uint64 b = Fetch64(s + 8);
349 uint64 c = Fetch64(s + len - 24);
350 uint64 d = Fetch64(s + len - 32);
351 uint64 e = Fetch64(s + 16) * k2;
352 uint64 f = Fetch64(s + 24) * 9;
353 uint64 g = Fetch64(s + len - 8);
354 uint64 h = Fetch64(s + len - 16) * mul;
355 uint64 u = Rotate(a + g, 43) + (Rotate(b, 30) + c) * 9;
356 uint64 v = ((a + g) ^ d) + f + 1;
357 uint64 w = bswap_64((u + v) * mul) + h;
358 uint64 x = Rotate(e + f, 42) + c;
359 uint64 y = (bswap_64((v + w) * mul) + g) * mul;
360 uint64 z = e + f + c;
361 a = bswap_64((x + z) * mul + y) + b;
362 b = ShiftMix((z + a) * mul + d + h) * mul;
363 return b + x;
364}
365
366uint64 CityHash64(const char *s, size_t len) {
367 if (len <= 32) {
368 if (len <= 16) {
369 return HashLen0to16(s, len);
370 } else {
371 return HashLen17to32(s, len);
372 }
373 } else if (len <= 64) {
374 return HashLen33to64(s, len);
375 }
376
377 // For strings over 64 bytes we hash the end first, and then as we
378 // loop we keep 56 bytes of state: v, w, x, y, and z.
379 uint64 x = Fetch64(s + len - 40);
380 uint64 y = Fetch64(s + len - 16) + Fetch64(s + len - 56);
381 uint64 z = HashLen16(Fetch64(s + len - 48) + len, Fetch64(s + len - 24));
382 pair<uint64, uint64> v = WeakHashLen32WithSeeds(s + len - 64, len, z);
383 pair<uint64, uint64> w = WeakHashLen32WithSeeds(s + len - 32, y + k1, x);
384 x = x * k1 + Fetch64(s);
385
386 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
387 len = (len - 1) & ~static_cast<size_t>(63);
388 do {
389 x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1;
390 y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
391 x ^= w.second;
392 y += v.first + Fetch64(s + 40);
393 z = Rotate(z + w.first, 33) * k1;
394 v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
395 w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
396 std::swap(z, x);
397 s += 64;
398 len -= 64;
399 } while (len != 0);
400 return HashLen16(HashLen16(v.first, w.first) + ShiftMix(y) * k1 + z,
401 HashLen16(v.second, w.second) + x);
402}
403
404uint64 CityHash64WithSeed(const char *s, size_t len, uint64 seed) {
405 return CityHash64WithSeeds(s, len, k2, seed);
406}
407
408uint64 CityHash64WithSeeds(const char *s, size_t len,
409 uint64 seed0, uint64 seed1) {
410 return HashLen16(CityHash64(s, len) - seed0, seed1);
411}
412
413// A subroutine for CityHash128(). Returns a decent 128-bit hash for strings
414// of any length representable in signed long. Based on City and Murmur.
415static uint128 CityMurmur(const char *s, size_t len, uint128 seed) {
416 uint64 a = Uint128Low64(seed);
417 uint64 b = Uint128High64(seed);
418 uint64 c = 0;
419 uint64 d = 0;
420 signed long l = len - 16;
421 if (l <= 0) { // len <= 16
422 a = ShiftMix(a * k1) * k1;
423 c = b * k1 + HashLen0to16(s, len);
424 d = ShiftMix(a + (len >= 8 ? Fetch64(s) : c));
425 } else { // len > 16
426 c = HashLen16(Fetch64(s + len - 8) + k1, a);
427 d = HashLen16(b + len, c + Fetch64(s + len - 16));
428 a += d;
429 do {
430 a ^= ShiftMix(Fetch64(s) * k1) * k1;
431 a *= k1;
432 b ^= a;
433 c ^= ShiftMix(Fetch64(s + 8) * k1) * k1;
434 c *= k1;
435 d ^= c;
436 s += 16;
437 l -= 16;
438 } while (l > 0);
439 }
440 a = HashLen16(a, c);
441 b = HashLen16(d, b);
442 return uint128(a ^ b, HashLen16(b, a));
443}
444
445uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed) {
446 if (len < 128) {
447 return CityMurmur(s, len, seed);
448 }
449
450 // We expect len >= 128 to be the common case. Keep 56 bytes of state:
451 // v, w, x, y, and z.
452 pair<uint64, uint64> v, w;
453 uint64 x = Uint128Low64(seed);
454 uint64 y = Uint128High64(seed);
455 uint64 z = len * k1;
456 v.first = Rotate(y ^ k1, 49) * k1 + Fetch64(s);
457 v.second = Rotate(v.first, 42) * k1 + Fetch64(s + 8);
458 w.first = Rotate(y + z, 35) * k1 + x;
459 w.second = Rotate(x + Fetch64(s + 88), 53) * k1;
460
461 // This is the same inner loop as CityHash64(), manually unrolled.
462 do {
463 x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1;
464 y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
465 x ^= w.second;
466 y += v.first + Fetch64(s + 40);
467 z = Rotate(z + w.first, 33) * k1;
468 v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
469 w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
470 std::swap(z, x);
471 s += 64;
472 x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1;
473 y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
474 x ^= w.second;
475 y += v.first + Fetch64(s + 40);
476 z = Rotate(z + w.first, 33) * k1;
477 v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
478 w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
479 std::swap(z, x);
480 s += 64;
481 len -= 128;
482 } while (LIKELY(len >= 128));
483 x += Rotate(v.first + z, 49) * k0;
484 y = y * k0 + Rotate(w.second, 37);
485 z = z * k0 + Rotate(w.first, 27);
486 w.first *= 9;
487 v.first *= k0;
488 // If 0 < len < 128, hash up to 4 chunks of 32 bytes each from the end of s.
489 for (size_t tail_done = 0; tail_done < len; ) {
490 tail_done += 32;
491 y = Rotate(x + y, 42) * k0 + v.second;
492 w.first += Fetch64(s + len - tail_done + 16);
493 x = x * k0 + w.first;
494 z += w.second + Fetch64(s + len - tail_done);
495 w.second += v.first;
496 v = WeakHashLen32WithSeeds(s + len - tail_done, v.first + z, v.second);
497 v.first *= k0;
498 }
499 // At this point our 56 bytes of state should contain more than
500 // enough information for a strong 128-bit hash. We use two
501 // different 56-byte-to-8-byte hashes to get a 16-byte final result.
502 x = HashLen16(x, v.first);
503 y = HashLen16(y + z, w.first);
504 return uint128(HashLen16(x + v.second, w.second) + y,
505 HashLen16(x + w.second, y + v.second));
506}
507
508uint128 CityHash128(const char *s, size_t len) {
509 return len >= 16 ?
510 CityHash128WithSeed(s + 16, len - 16,
511 uint128(Fetch64(s), Fetch64(s + 8) + k0)) :
512 CityHash128WithSeed(s, len, uint128(k0, k1));
513}
514
515#ifdef __SSE4_2__
516#include <citycrc.h>
517#include <nmmintrin.h>
518
519// Requires len >= 240.
520static void CityHashCrc256Long(const char *s, size_t len,
521 uint32 seed, uint64 *result) {
522 uint64 a = Fetch64(s + 56) + k0;
523 uint64 b = Fetch64(s + 96) + k0;
524 uint64 c = result[0] = HashLen16(b, len);
525 uint64 d = result[1] = Fetch64(s + 120) * k0 + len;
526 uint64 e = Fetch64(s + 184) + seed;
527 uint64 f = 0;
528 uint64 g = 0;
529 uint64 h = c + d;
530 uint64 x = seed;
531 uint64 y = 0;
532 uint64 z = 0;
533
534 // 240 bytes of input per iter.
535 size_t iters = len / 240;
536 len -= iters * 240;
537 do {
538#undef CHUNK
539#define CHUNK(r) \
540 PERMUTE3(x, z, y); \
541 b += Fetch64(s); \
542 c += Fetch64(s + 8); \
543 d += Fetch64(s + 16); \
544 e += Fetch64(s + 24); \
545 f += Fetch64(s + 32); \
546 a += b; \
547 h += f; \
548 b += c; \
549 f += d; \
550 g += e; \
551 e += z; \
552 g += x; \
553 z = _mm_crc32_u64(z, b + g); \
554 y = _mm_crc32_u64(y, e + h); \
555 x = _mm_crc32_u64(x, f + a); \
556 e = Rotate(e, r); \
557 c += e; \
558 s += 40
559
560 CHUNK(0); PERMUTE3(a, h, c);
561 CHUNK(33); PERMUTE3(a, h, f);
562 CHUNK(0); PERMUTE3(b, h, f);
563 CHUNK(42); PERMUTE3(b, h, d);
564 CHUNK(0); PERMUTE3(b, h, e);
565 CHUNK(33); PERMUTE3(a, h, e);
566 } while (--iters > 0);
567
568 while (len >= 40) {
569 CHUNK(29);
570 e ^= Rotate(a, 20);
571 h += Rotate(b, 30);
572 g ^= Rotate(c, 40);
573 f += Rotate(d, 34);
574 PERMUTE3(c, h, g);
575 len -= 40;
576 }
577 if (len > 0) {
578 s = s + len - 40;
579 CHUNK(33);
580 e ^= Rotate(a, 43);
581 h += Rotate(b, 42);
582 g ^= Rotate(c, 41);
583 f += Rotate(d, 40);
584 }
585 result[0] ^= h;
586 result[1] ^= g;
587 g += h;
588 a = HashLen16(a, g + z);
589 x += y << 32;
590 b += x;
591 c = HashLen16(c, z) + h;
592 d = HashLen16(d, e + result[0]);
593 g += e;
594 h += HashLen16(x, f);
595 e = HashLen16(a, d) + g;
596 z = HashLen16(b, c) + a;
597 y = HashLen16(g, h) + c;
598 result[0] = e + z + y + x;
599 a = ShiftMix((a + y) * k0) * k0 + b;
600 result[1] += a + result[0];
601 a = ShiftMix(a * k0) * k0 + c;
602 result[2] = a + result[1];
603 a = ShiftMix((a + e) * k0) * k0;
604 result[3] = a + result[2];
605}
606
607// Requires len < 240.
608static void CityHashCrc256Short(const char *s, size_t len, uint64 *result) {
609 char buf[240];
610 memcpy(buf, s, len);
611 memset(buf + len, 0, 240 - len);
612 CityHashCrc256Long(buf, 240, ~static_cast<uint32>(len), result);
613}
614
615void CityHashCrc256(const char *s, size_t len, uint64 *result) {
616 if (LIKELY(len >= 240)) {
617 CityHashCrc256Long(s, len, 0, result);
618 } else {
619 CityHashCrc256Short(s, len, result);
620 }
621}
622
623uint128 CityHashCrc128WithSeed(const char *s, size_t len, uint128 seed) {
624 if (len <= 900) {
625 return CityHash128WithSeed(s, len, seed);
626 } else {
627 uint64 result[4];
628 CityHashCrc256(s, len, result);
629 uint64 u = Uint128High64(seed) + result[0];
630 uint64 v = Uint128Low64(seed) + result[1];
631 return uint128(HashLen16(u, v + result[2]),
632 HashLen16(Rotate(v, 32), u * k0 + result[3]));
633 }
634}
635
636uint128 CityHashCrc128(const char *s, size_t len) {
637 if (len <= 900) {
638 return CityHash128(s, len);
639 } else {
640 uint64 result[4];
641 CityHashCrc256(s, len, result);
642 return uint128(result[2], result[3]);
643 }
644}
645
646#endif
647