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