CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

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