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