forked from rurban/smhasher
-
Notifications
You must be signed in to change notification settings - Fork 10
/
farmhash-c.c
1667 lines (1481 loc) · 49 KB
/
farmhash-c.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2014 Google, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// FarmHash, by Geoff Pike
#include "farmhash-c.h"
#include <string.h>
#include <assert.h>
// PLATFORM-SPECIFIC CONFIGURATION
#if defined (__x86_64) || defined (__x86_64__)
#define x86_64 1
#else
#define x86_64 0
#endif
#if defined(__i386__) || defined(__i386) || defined(__X86__)
#define x86 1
#else
#define x86 x86_64
#endif
#if defined(__SSSE3__) || defined(__SSE4_1__) || defined(__SSE4_2__)
#include <tmmintrin.h>
#define CAN_USE_SSSE3 1 // Now we can use _mm_hsub_epi16 and so on.
#else
#define CAN_USE_SSSE3 0
#endif
#if defined(__SSE4_1__) || defined(__SSE4_2__)
#include <immintrin.h>
#define CAN_USE_SSE41 1 // Now we can use _mm_insert_epi64 and so on.
#else
#define CAN_USE_SSE41 0
#endif
#if defined(__SSE4_2__)
#include <nmmintrin.h>
#define CAN_USE_SSE42 1 // Now we can use _mm_crc32_u{32,16,8}. And on 64-bit platforms, _mm_crc32_u64.
#else
#define CAN_USE_SSE42 0
#endif
#if defined(__AES__)
#include <wmmintrin.h>
#define CAN_USE_AESNI 1 // Now we can use _mm_aesimc_si128 and so on.
#else
#define CAN_USE_AESNI 0
#endif
#if defined(__AVX__)
#include <immintrin.h>
#define CAN_USE_AVX 1
#else
#define CAN_USE_AVX 0
#endif
#ifdef __GNUC__
#define likely(x) (__builtin_expect(!!(x), 1))
#else
#define likely(x) (x)
#endif
#ifdef LITTLE_ENDIAN
#define uint32_t_in_expected_order(x) (x)
#define uint64_t_in_expected_order(x) (x)
#else
#define uint32_t_in_expected_order(x) (bswap32(x))
#define uint64_t_in_expected_order(x) (bswap64(x))
#endif
#define PERMUTE3(a, b, c) \
do { \
swap32(a, b); \
swap32(a, c); \
} while (0)
static inline uint32_t bswap32(const uint32_t x) {
uint32_t y = x;
size_t i;
for (i = 0; i < sizeof(uint32_t) >> 1; i++) {
uint32_t d = sizeof(uint32_t) - i - 1;
uint32_t mh = ((uint32_t)0xff) << (d << 3);
uint32_t ml = ((uint32_t)0xff) << (i << 3);
uint32_t h = x & mh;
uint32_t l = x & ml;
uint64_t t = (l << ((d - i) << 3)) | (h >> ((d - i) << 3));
y = t | (y & ~(mh | ml));
}
return y;
}
static inline uint64_t bswap64(const uint64_t x) {
uint64_t y = x;
size_t i;
for (i = 0; i < sizeof(uint64_t) >> 1; i++) {
uint64_t d = sizeof(uint64_t) - i - 1;
uint64_t mh = ((uint64_t)0xff) << (d << 3);
uint64_t ml = ((uint64_t)0xff) << (i << 3);
uint64_t h = x & mh;
uint64_t l = x & ml;
uint64_t t = (l << ((d - i) << 3)) | (h >> ((d - i) << 3));
y = t | (y & ~(mh | ml));
}
return y;
}
static inline uint64_t fetch64(const char* p) {
uint64_t result;
memcpy(&result, p, sizeof(result));
return uint64_t_in_expected_order(result);
}
static inline uint32_t fetch32(const char* p) {
uint32_t result;
memcpy(&result, p, sizeof(result));
return uint32_t_in_expected_order(result);
}
#if CAN_USE_SSSE3 || CAN_USE_SSE41 || CAN_USE_SSE42 || CAN_USE_AESNI || CAN_USE_AVX
static inline __m128i fetch128(const char* s) {
return _mm_loadu_si128((const __m128i*) s);
}
#endif
static inline void swap32(uint32_t* a, uint32_t* b) {
uint32_t t;
t = *a;
*a = *b;
*b = t;
}
static inline void swap64(uint64_t* a, uint64_t* b) {
uint64_t t;
t = *a;
*a = *b;
*b = t;
}
#if CAN_USE_SSSE3 || CAN_USE_SSE41 || CAN_USE_SSE42 || CAN_USE_AESNI || CAN_USE_AVX
static inline void swap128(__m128i* a, __m128i* b) {
__m128i t;
t = *a;
*a = *b;
*b = t;
}
#endif
static inline uint32_t ror32(uint32_t val, size_t shift) {
// Avoid shifting by 32: doing so yields an undefined result.
return shift == 0 ? val : (val >> shift) | (val << (32 - shift));
}
static inline uint64_t ror64(uint64_t val, size_t shift) {
// Avoid shifting by 64: doing so yields an undefined result.
return shift == 0 ? val : (val >> shift) | (val << (64 - shift));
}
// Helpers for data-parallel operations (1x 128 bits or 2x64 or 4x32 or 8x16).
#if CAN_USE_SSSE3 || CAN_USE_SSE41 || CAN_USE_SSE42 || CAN_USE_AESNI || CAN_USE_AVX
static inline __m128i add64x2(__m128i x, __m128i y) { return _mm_add_epi64(x, y); }
static inline __m128i add32x4(__m128i x, __m128i y) { return _mm_add_epi32(x, y); }
static inline __m128i xor128(__m128i x, __m128i y) { return _mm_xor_si128(x, y); }
static inline __m128i or128(__m128i x, __m128i y) { return _mm_or_si128(x, y); }
static inline __m128i mul32x4_5(__m128i x) { return add32x4(x, _mm_slli_epi32(x, 2)); }
static inline __m128i rol32x4(__m128i x, int c) {
return or128(_mm_slli_epi32(x, c),
_mm_srli_epi32(x, 32 - c));
}
static inline __m128i rol32x4_17(__m128i x) { return rol32x4(x, 17); }
static inline __m128i rol32x4_19(__m128i x) { return rol32x4(x, 19); }
static inline __m128i shuf32x4_0_3_2_1(__m128i x) {
return _mm_shuffle_epi32(x, (0 << 6) + (3 << 4) + (2 << 2) + (1 << 0));
}
#endif
#if CAN_USE_SSSE3
static inline __m128i shuf8x16(__m128i x, __m128i y) { return _mm_shuffle_epi8(y, x); }
#endif
#if CAN_USE_SSE41
static inline __m128i mul32x4(__m128i x, __m128i y) { return _mm_mullo_epi32(x, y); }
static inline __m128i murk(__m128i a, __m128i b, __m128i c, __m128i d, __m128i e) {
return add32x4(e,
mul32x4_5(
rol32x4_19(
xor128(
mul32x4(d,
rol32x4_17(
mul32x4(c, a))),
(b)))));
}
#endif
// Building blocks for hash functions
// Some primes between 2^63 and 2^64 for various uses.
static const uint64_t k0 = 0xc3a5c85c97cb3127ULL;
static const uint64_t k1 = 0xb492b66fbe98f273ULL;
static const uint64_t k2 = 0x9ae16a3b2f90404fULL;
// Magic numbers for 32-bit hashing. Copied from Murmur3.
static const uint32_t c1 = 0xcc9e2d51;
static const uint32_t c2 = 0x1b873593;
// A 32-bit to 32-bit integer hash copied from Murmur3.
static inline uint32_t fmix(uint32_t h) {
h ^= h >> 16;
h *= 0x85ebca6b;
h ^= h >> 13;
h *= 0xc2b2ae35;
h ^= h >> 16;
return h;
}
static inline uint64_t smix(uint64_t val) {
return val ^ (val >> 47);
}
static inline uint32_t mur(uint32_t a, uint32_t h) {
// Helper from Murmur3 for combining two 32-bit values.
a *= c1;
a = ror32(a, 17);
a *= c2;
h ^= a;
h = ror32(h, 19);
return h * 5 + 0xe6546b64;
}
static inline uint32_t debug_tweak32(uint32_t x) {
#ifndef NDEBUG
x = ~bswap32(x * c1);
#endif
return x;
}
static inline uint64_t debug_tweak64(uint64_t x) {
#ifndef NDEBUG
x = ~bswap64(x * k1);
#endif
return x;
}
uint128_c_t debug_tweak128(uint128_c_t x) {
#ifndef NDEBUG
uint64_t y = debug_tweak64(uint128_c_t_low64(x));
uint64_t z = debug_tweak64(uint128_c_t_high64(x));
y += z;
z += y;
x = make_uint128_c_t(y, z * k1);
#endif
return x;
}
static inline uint64_t farmhash_len_16(uint64_t u, uint64_t v) {
return farmhash128_to_64(make_uint128_c_t(u, v));
}
static inline uint64_t farmhash_len_16_mul(uint64_t u, uint64_t v, uint64_t mul) {
// Murmur-inspired hashing.
uint64_t a = (u ^ v) * mul;
a ^= (a >> 47);
uint64_t b = (v ^ a) * mul;
b ^= (b >> 47);
b *= mul;
return b;
}
// farmhash na
static inline uint64_t farmhash_na_len_0_to_16(const char *s, size_t len) {
if (len >= 8) {
uint64_t mul = k2 + len * 2;
uint64_t a = fetch64(s) + k2;
uint64_t b = fetch64(s + len - 8);
uint64_t c = ror64(b, 37) * mul + a;
uint64_t d = (ror64(a, 25) + b) * mul;
return farmhash_len_16_mul(c, d, mul);
}
if (len >= 4) {
uint64_t mul = k2 + len * 2;
uint64_t a = fetch32(s);
return farmhash_len_16_mul(len + (a << 3), fetch32(s + len - 4), mul);
}
if (len > 0) {
uint8_t a = s[0];
uint8_t b = s[len >> 1];
uint8_t c = s[len - 1];
uint32_t y = (uint32_t) a + ((uint32_t) b << 8);
uint32_t z = len + ((uint32_t) c << 2);
return smix(y * k2 ^ z * k0) * k2;
}
return k2;
}
// This probably works well for 16-byte strings as well, but it may be overkill
// in that case.
static inline uint64_t farmhash_na_len_17_to_32(const char *s, size_t len) {
uint64_t mul = k2 + len * 2;
uint64_t a = fetch64(s) * k1;
uint64_t b = fetch64(s + 8);
uint64_t c = fetch64(s + len - 8) * mul;
uint64_t d = fetch64(s + len - 16) * k2;
return farmhash_len_16_mul(ror64(a + b, 43) + ror64(c, 30) + d,
a + ror64(b + k2, 18) + c, mul);
}
// Return a 16-byte hash for 48 bytes. Quick and dirty.
// Callers do best to use "random-looking" values for a and b.
static inline uint128_c_t weak_farmhash_na_len_32_with_seeds_vals(
uint64_t w, uint64_t x, uint64_t y, uint64_t z, uint64_t a, uint64_t b) {
a += w;
b = ror64(b + a + z, 21);
uint64_t c = a;
a += x;
a += y;
b += ror64(a, 44);
return make_uint128_c_t(a + z, b + c);
}
// Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
static inline uint128_c_t weak_farmhash_na_len_32_with_seeds(
const char* s, uint64_t a, uint64_t b) {
return weak_farmhash_na_len_32_with_seeds_vals(fetch64(s),
fetch64(s + 8),
fetch64(s + 16),
fetch64(s + 24),
a,
b);
}
// Return an 8-byte hash for 33 to 64 bytes.
static inline uint64_t farmhash_na_len_33_to_64(const char *s, size_t len) {
uint64_t mul = k2 + len * 2;
uint64_t a = fetch64(s) * k2;
uint64_t b = fetch64(s + 8);
uint64_t c = fetch64(s + len - 8) * mul;
uint64_t d = fetch64(s + len - 16) * k2;
uint64_t y = ror64(a + b, 43) + ror64(c, 30) + d;
uint64_t z = farmhash_len_16_mul(y, a + ror64(b + k2, 18) + c, mul);
uint64_t e = fetch64(s + 16) * mul;
uint64_t f = fetch64(s + 24);
uint64_t g = (y + fetch64(s + len - 32)) * mul;
uint64_t h = (z + fetch64(s + len - 24)) * mul;
return farmhash_len_16_mul(ror64(e + f, 43) + ror64(g, 30) + h,
e + ror64(f + a, 18) + g, mul);
}
uint64_t farmhash64_na(const char *s, size_t len) {
const uint64_t seed = 81;
if (len <= 32) {
if (len <= 16) {
return farmhash_na_len_0_to_16(s, len);
} else {
return farmhash_na_len_17_to_32(s, len);
}
} else if (len <= 64) {
return farmhash_na_len_33_to_64(s, len);
}
// For strings over 64 bytes we loop. Internal state consists of
// 56 bytes: v, w, x, y, and z.
uint64_t x = seed;
uint64_t y = seed * k1 + 113;
uint64_t z = smix(y * k2 + 113) * k2;
uint128_c_t v = make_uint128_c_t(0, 0);
uint128_c_t w = make_uint128_c_t(0, 0);
x = x * k2 + fetch64(s);
// Set end so that after the loop we have 1 to 64 bytes left to process.
const char* end = s + ((len - 1) / 64) * 64;
const char* last64 = end + ((len - 1) & 63) - 63;
assert(s + len - 64 == last64);
do {
x = ror64(x + y + v.a + fetch64(s + 8), 37) * k1;
y = ror64(y + v.b + fetch64(s + 48), 42) * k1;
x ^= w.b;
y += v.a + fetch64(s + 40);
z = ror64(z + w.a, 33) * k1;
v = weak_farmhash_na_len_32_with_seeds(s, v.b * k1, x + w.a);
w = weak_farmhash_na_len_32_with_seeds(s + 32, z + w.b, y + fetch64(s + 16));
swap64(&z, &x);
s += 64;
} while (s != end);
uint64_t mul = k1 + ((z & 0xff) << 1);
// Make s point to the last 64 bytes of input.
s = last64;
w.a += ((len - 1) & 63);
v.a += w.a;
w.a += v.a;
x = ror64(x + y + v.a + fetch64(s + 8), 37) * mul;
y = ror64(y + v.b + fetch64(s + 48), 42) * mul;
x ^= w.b * 9;
y += v.a * 9 + fetch64(s + 40);
z = ror64(z + w.a, 33) * mul;
v = weak_farmhash_na_len_32_with_seeds(s, v.b * mul, x + w.a);
w = weak_farmhash_na_len_32_with_seeds(s + 32, z + w.b, y + fetch64(s + 16));
swap64(&z, &x);
return farmhash_len_16_mul(farmhash_len_16_mul(v.a, w.a, mul) + smix(y) * k0 + z,
farmhash_len_16_mul(v.b, w.b, mul) + x,
mul);
}
uint64_t farmhash64_na_with_seeds(const char *s, size_t len, uint64_t seed0, uint64_t seed1) {
return farmhash_len_16(farmhash64_na(s, len) - seed0, seed1);
}
uint64_t farmhash64_na_with_seed(const char *s, size_t len, uint64_t seed) {
return farmhash64_na_with_seeds(s, len, k2, seed);
}
// farmhash uo
static inline uint64_t farmhash_uo_h(uint64_t x, uint64_t y, uint64_t mul, int r) {
uint64_t a = (x ^ y) * mul;
a ^= (a >> 47);
uint64_t b = (y ^ a) * mul;
return ror64(b, r) * mul;
}
uint64_t farmhash64_uo_with_seeds(const char *s, size_t len,
uint64_t seed0, uint64_t seed1) {
if (len <= 64) {
return farmhash64_na_with_seeds(s, len, seed0, seed1);
}
// For strings over 64 bytes we loop. Internal state consists of
// 64 bytes: u, v, w, x, y, and z.
uint64_t x = seed0;
uint64_t y = seed1 * k2 + 113;
uint64_t z = smix(y * k2) * k2;
uint128_c_t v = make_uint128_c_t(seed0, seed1);
uint128_c_t w = make_uint128_c_t(0, 0);
uint64_t u = x - z;
x *= k2;
uint64_t mul = k2 + (u & 0x82);
// Set end so that after the loop we have 1 to 64 bytes left to process.
const char* end = s + ((len - 1) / 64) * 64;
const char* last64 = end + ((len - 1) & 63) - 63;
assert(s + len - 64 == last64);
do {
uint64_t a0 = fetch64(s);
uint64_t a1 = fetch64(s + 8);
uint64_t a2 = fetch64(s + 16);
uint64_t a3 = fetch64(s + 24);
uint64_t a4 = fetch64(s + 32);
uint64_t a5 = fetch64(s + 40);
uint64_t a6 = fetch64(s + 48);
uint64_t a7 = fetch64(s + 56);
x += a0 + a1;
y += a2;
z += a3;
v.a += a4;
v.b += a5 + a1;
w.a += a6;
w.b += a7;
x = ror64(x, 26);
x *= 9;
y = ror64(y, 29);
z *= mul;
v.a = ror64(v.a, 33);
v.b = ror64(v.b, 30);
w.a ^= x;
w.a *= 9;
z = ror64(z, 32);
z += w.b;
w.b += z;
z *= 9;
swap64(&u, &y);
z += a0 + a6;
v.a += a2;
v.b += a3;
w.a += a4;
w.b += a5 + a6;
x += a1;
y += a7;
y += v.a;
v.a += x - y;
v.b += w.a;
w.a += v.b;
w.b += x - y;
x += w.b;
w.b = ror64(w.b, 34);
swap64(&u, &z);
s += 64;
} while (s != end);
// Make s point to the last 64 bytes of input.
s = last64;
u *= 9;
v.b = ror64(v.b, 28);
v.a = ror64(v.a, 20);
w.a += ((len - 1) & 63);
u += y;
y += u;
x = ror64(y - x + v.a + fetch64(s + 8), 37) * mul;
y = ror64(y ^ v.b ^ fetch64(s + 48), 42) * mul;
x ^= w.b * 9;
y += v.a + fetch64(s + 40);
z = ror64(z + w.a, 33) * mul;
v = weak_farmhash_na_len_32_with_seeds(s, v.b * mul, x + w.a);
w = weak_farmhash_na_len_32_with_seeds(s + 32, z + w.b, y + fetch64(s + 16));
return farmhash_uo_h(farmhash_len_16_mul(v.a + x, w.a ^ y, mul) + z - u,
farmhash_uo_h(v.b + y, w.b + z, k2, 30) ^ x,
k2,
31);
}
uint64_t farmhash64_uo_with_seed(const char *s, size_t len, uint64_t seed) {
return len <= 64 ? farmhash64_na_with_seed(s, len, seed) :
farmhash64_uo_with_seeds(s, len, 0, seed);
}
uint64_t farmhash64_uo(const char *s, size_t len) {
return len <= 64 ? farmhash64_na(s, len) :
farmhash64_uo_with_seeds(s, len, 81, 0);
}
// farmhash xo
static inline uint64_t farmhash_xo_h32(const char *s, size_t len, uint64_t mul,
uint64_t seed0, uint64_t seed1) {
uint64_t a = fetch64(s) * k1;
uint64_t b = fetch64(s + 8);
uint64_t c = fetch64(s + len - 8) * mul;
uint64_t d = fetch64(s + len - 16) * k2;
uint64_t u = ror64(a + b, 43) + ror64(c, 30) + d + seed0;
uint64_t v = a + ror64(b + k2, 18) + c + seed1;
a = smix((u ^ v) * mul);
b = smix((v ^ a) * mul);
return b;
}
// Return an 8-byte hash for 33 to 64 bytes.
static inline uint64_t farmhash_xo_len_33_to_64(const char *s, size_t len) {
uint64_t mul0 = k2 - 30;
uint64_t mul1 = k2 - 30 + 2 * len;
uint64_t h0 = farmhash_xo_h32(s, 32, mul0, 0, 0);
uint64_t h1 = farmhash_xo_h32(s + len - 32, 32, mul1, 0, 0);
return ((h1 * mul1) + h0) * mul1;
}
// Return an 8-byte hash for 65 to 96 bytes.
static inline uint64_t farmhash_xo_len_65_to_96(const char *s, size_t len) {
uint64_t mul0 = k2 - 114;
uint64_t mul1 = k2 - 114 + 2 * len;
uint64_t h0 = farmhash_xo_h32(s, 32, mul0, 0, 0);
uint64_t h1 = farmhash_xo_h32(s + 32, 32, mul1, 0, 0);
uint64_t h2 = farmhash_xo_h32(s + len - 32, 32, mul1, h0, h1);
return (h2 * 9 + (h0 >> 17) + (h1 >> 21)) * mul1;
}
uint64_t farmhash64_xo(const char *s, size_t len) {
if (len <= 32) {
if (len <= 16) {
return farmhash_na_len_0_to_16(s, len);
} else {
return farmhash_na_len_17_to_32(s, len);
}
} else if (len <= 64) {
return farmhash_xo_len_33_to_64(s, len);
} else if (len <= 96) {
return farmhash_xo_len_65_to_96(s, len);
} else if (len <= 256) {
return farmhash64_na(s, len);
} else {
return farmhash64_uo(s, len);
}
}
uint64_t farmhash64_xo_with_seeds(const char *s, size_t len, uint64_t seed0, uint64_t seed1) {
return farmhash64_uo_with_seeds(s, len, seed0, seed1);
}
uint64_t farmhash64_xo_with_seed(const char *s, size_t len, uint64_t seed) {
return farmhash64_uo_with_seed(s, len, seed);
}
// farmhash te
#if x86_64 && CAN_USE_SSE41 && CAN_USE_SSSE3
// Requires n >= 256. Requires SSE4.1. Should be slightly faster if the
// compiler uses AVX instructions (e.g., use the -mavx flag with GCC).
static inline uint64_t farmhash64_te_long(const char* s, size_t n,
uint64_t seed0, uint64_t seed1) {
const __m128i k_shuf =
_mm_set_epi8(4, 11, 10, 5, 8, 15, 6, 9, 12, 2, 14, 13, 0, 7, 3, 1);
const __m128i k_mult =
_mm_set_epi8(0xbd, 0xd6, 0x33, 0x39, 0x45, 0x54, 0xfa, 0x03,
0x34, 0x3e, 0x33, 0xed, 0xcc, 0x9e, 0x2d, 0x51);
uint64_t seed2 = (seed0 + 113) * (seed1 + 9);
uint64_t seed3 = (ror64(seed0, 23) + 27) * (ror64(seed1, 30) + 111);
__m128i d0 = _mm_cvtsi64_si128(seed0);
__m128i d1 = _mm_cvtsi64_si128(seed1);
__m128i d2 = shuf8x16(k_shuf, d0);
__m128i d3 = shuf8x16(k_shuf, d1);
__m128i d4 = xor128(d0, d1);
__m128i d5 = xor128(d1, d2);
__m128i d6 = xor128(d2, d4);
__m128i d7 = _mm_set1_epi32(seed2 >> 32);
__m128i d8 = mul32x4(k_mult, d2);
__m128i d9 = _mm_set1_epi32(seed3 >> 32);
__m128i d10 = _mm_set1_epi32(seed3);
__m128i d11 = add64x2(d2, _mm_set1_epi32(seed2));
const char* end = s + (n & ~((size_t) 255));
do {
__m128i z;
z = fetch128(s);
d0 = add64x2(d0, z);
d1 = shuf8x16(k_shuf, d1);
d2 = xor128(d2, d0);
d4 = xor128(d4, z);
d4 = xor128(d4, d1);
swap128(&d0, &d6);
z = fetch128(s + 16);
d5 = add64x2(d5, z);
d6 = shuf8x16(k_shuf, d6);
d8 = shuf8x16(k_shuf, d8);
d7 = xor128(d7, d5);
d0 = xor128(d0, z);
d0 = xor128(d0, d6);
swap128(&d5, &d11);
z = fetch128(s + 32);
d1 = add64x2(d1, z);
d2 = shuf8x16(k_shuf, d2);
d4 = shuf8x16(k_shuf, d4);
d5 = xor128(d5, z);
d5 = xor128(d5, d2);
swap128(&d10, &d4);
z = fetch128(s + 48);
d6 = add64x2(d6, z);
d7 = shuf8x16(k_shuf, d7);
d0 = shuf8x16(k_shuf, d0);
d8 = xor128(d8, d6);
d1 = xor128(d1, z);
d1 = add64x2(d1, d7);
z = fetch128(s + 64);
d2 = add64x2(d2, z);
d5 = shuf8x16(k_shuf, d5);
d4 = add64x2(d4, d2);
d6 = xor128(d6, z);
d6 = xor128(d6, d11);
swap128(&d8, &d2);
z = fetch128(s + 80);
d7 = xor128(d7, z);
d8 = shuf8x16(k_shuf, d8);
d1 = shuf8x16(k_shuf, d1);
d0 = add64x2(d0, d7);
d2 = add64x2(d2, z);
d2 = add64x2(d2, d8);
swap128(&d1, &d7);
z = fetch128(s + 96);
d4 = shuf8x16(k_shuf, d4);
d6 = shuf8x16(k_shuf, d6);
d8 = mul32x4(k_mult, d8);
d5 = xor128(d5, d11);
d7 = xor128(d7, z);
d7 = add64x2(d7, d4);
swap128(&d6, &d0);
z = fetch128(s + 112);
d8 = add64x2(d8, z);
d0 = shuf8x16(k_shuf, d0);
d2 = shuf8x16(k_shuf, d2);
d1 = xor128(d1, d8);
d10 = xor128(d10, z);
d10 = xor128(d10, d0);
swap128(&d11, &d5);
z = fetch128(s + 128);
d4 = add64x2(d4, z);
d5 = shuf8x16(k_shuf, d5);
d7 = shuf8x16(k_shuf, d7);
d6 = add64x2(d6, d4);
d8 = xor128(d8, z);
d8 = xor128(d8, d5);
swap128(&d4, &d10);
z = fetch128(s + 144);
d0 = add64x2(d0, z);
d1 = shuf8x16(k_shuf, d1);
d2 = add64x2(d2, d0);
d4 = xor128(d4, z);
d4 = xor128(d4, d1);
z = fetch128(s + 160);
d5 = add64x2(d5, z);
d6 = shuf8x16(k_shuf, d6);
d8 = shuf8x16(k_shuf, d8);
d7 = xor128(d7, d5);
d0 = xor128(d0, z);
d0 = xor128(d0, d6);
swap128(&d2, &d8);
z = fetch128(s + 176);
d1 = add64x2(d1, z);
d2 = shuf8x16(k_shuf, d2);
d4 = shuf8x16(k_shuf, d4);
d5 = mul32x4(k_mult, d5);
d5 = xor128(d5, z);
d5 = xor128(d5, d2);
swap128(&d7, &d1);
z = fetch128(s + 192);
d6 = add64x2(d6, z);
d7 = shuf8x16(k_shuf, d7);
d0 = shuf8x16(k_shuf, d0);
d8 = add64x2(d8, d6);
d1 = xor128(d1, z);
d1 = xor128(d1, d7);
swap128(&d0, &d6);
z = fetch128(s + 208);
d2 = add64x2(d2, z);
d5 = shuf8x16(k_shuf, d5);
d4 = xor128(d4, d2);
d6 = xor128(d6, z);
d6 = xor128(d6, d9);
swap128(&d5, &d11);
z = fetch128(s + 224);
d7 = add64x2(d7, z);
d8 = shuf8x16(k_shuf, d8);
d1 = shuf8x16(k_shuf, d1);
d0 = xor128(d0, d7);
d2 = xor128(d2, z);
d2 = xor128(d2, d8);
swap128(&d10, &d4);
z = fetch128(s + 240);
d3 = add64x2(d3, z);
d4 = shuf8x16(k_shuf, d4);
d6 = shuf8x16(k_shuf, d6);
d7 = mul32x4(k_mult, d7);
d5 = add64x2(d5, d3);
d7 = xor128(d7, z);
d7 = xor128(d7, d4);
swap128(&d3, &d9);
s += 256;
} while (s != end);
d6 = add64x2(mul32x4(k_mult, d6), _mm_cvtsi64_si128(n));
if (n % 256 != 0) {
d7 = add64x2(_mm_shuffle_epi32(d8, (0 << 6) + (3 << 4) + (2 << 2) + (1 << 0)), d7);
d8 = add64x2(mul32x4(k_mult, d8), _mm_cvtsi64_si128(farmhash64_xo(s, n % 256)));
}
__m128i t[8];
d0 = mul32x4(k_mult, shuf8x16(k_shuf, mul32x4(k_mult, d0)));
d3 = mul32x4(k_mult, shuf8x16(k_shuf, mul32x4(k_mult, d3)));
d9 = mul32x4(k_mult, shuf8x16(k_shuf, mul32x4(k_mult, d9)));
d1 = mul32x4(k_mult, shuf8x16(k_shuf, mul32x4(k_mult, d1)));
d0 = add64x2(d11, d0);
d3 = xor128(d7, d3);
d9 = add64x2(d8, d9);
d1 = add64x2(d10, d1);
d4 = add64x2(d3, d4);
d5 = add64x2(d9, d5);
d6 = xor128(d1, d6);
d2 = add64x2(d0, d2);
t[0] = d0;
t[1] = d3;
t[2] = d9;
t[3] = d1;
t[4] = d4;
t[5] = d5;
t[6] = d6;
t[7] = d2;
return farmhash64_xo((const char*) t, sizeof(t));
}
uint64_t farmhash64_te(const char *s, size_t len) {
// Empirically, farmhash xo seems faster until length 512.
return len >= 512 ? farmhash64_te_long(s, len, k2, k1) : farmhash64_xo(s, len);
}
uint64_t farmhash64_te_with_seed(const char *s, size_t len, uint64_t seed) {
return len >= 512 ? farmhash64_te_long(s, len, k1, seed) :
farmhash64_xo_with_seed(s, len, seed);
}
uint64_t farmhash64_te_with_seeds(const char *s, size_t len, uint64_t seed0, uint64_t seed1) {
return len >= 512 ? farmhash64_te_long(s, len, seed0, seed1) :
farmhash64_xo_with_seeds(s, len, seed0, seed1);
}
#endif
// farmhash nt
#if x86_64 && CAN_USE_SSE41 && CAN_USE_SSSE3
uint32_t farmhash32_nt(const char *s, size_t len) {
return (uint32_t) farmhash64_te(s, len);
}
uint32_t farmhash32_nt_with_seed(const char *s, size_t len, uint32_t seed) {
return (uint32_t) farmhash64_te_with_seed(s, len, seed);
}
#endif
// farmhash mk
static inline uint32_t farmhash32_mk_len_13_to_24(const char *s, size_t len, uint32_t seed) {
uint32_t a = fetch32(s - 4 + (len >> 1));
uint32_t b = fetch32(s + 4);
uint32_t c = fetch32(s + len - 8);
uint32_t d = fetch32(s + (len >> 1));
uint32_t e = fetch32(s);
uint32_t f = fetch32(s + len - 4);
uint32_t h = d * c1 + len + seed;
a = ror32(a, 12) + f;
h = mur(c, h) + a;
a = ror32(a, 3) + c;
h = mur(e, h) + a;
a = ror32(a + f, 12) + d;
h = mur(b ^ seed, h) + a;
return fmix(h);
}
static inline uint32_t farmhash32_mk_len_0_to_4(const char *s, size_t len, uint32_t seed) {
uint32_t b = seed;
uint32_t c = 9;
size_t i;
for (i = 0; i < len; i++) {
signed char v = s[i];
b = b * c1 + v;
c ^= b;
}
return fmix(mur(b, mur(len, c)));
}
static inline uint32_t farmhash32_mk_len_5_to_12(const char *s, size_t len, uint32_t seed) {
uint32_t a = len, b = len * 5, c = 9, d = b + seed;
a += fetch32(s);
b += fetch32(s + len - 4);
c += fetch32(s + ((len >> 1) & 4));
return fmix(seed ^ mur(c, mur(b, mur(a, d))));
}
uint32_t farmhash32_mk(const char *s, size_t len) {
if (len <= 24) {
return len <= 12 ?
(len <= 4 ? farmhash32_mk_len_0_to_4(s, len, 0) : farmhash32_mk_len_5_to_12(s, len, 0)) :
farmhash32_mk_len_13_to_24(s, len, 0);
}
// len > 24
uint32_t h = len, g = c1 * len, f = g;
uint32_t a0 = ror32(fetch32(s + len - 4) * c1, 17) * c2;
uint32_t a1 = ror32(fetch32(s + len - 8) * c1, 17) * c2;
uint32_t a2 = ror32(fetch32(s + len - 16) * c1, 17) * c2;
uint32_t a3 = ror32(fetch32(s + len - 12) * c1, 17) * c2;
uint32_t a4 = ror32(fetch32(s + len - 20) * c1, 17) * c2;
h ^= a0;
h = ror32(h, 19);
h = h * 5 + 0xe6546b64;
h ^= a2;
h = ror32(h, 19);
h = h * 5 + 0xe6546b64;
g ^= a1;
g = ror32(g, 19);
g = g * 5 + 0xe6546b64;
g ^= a3;
g = ror32(g, 19);
g = g * 5 + 0xe6546b64;
f += a4;
f = ror32(f, 19) + 113;
size_t iters = (len - 1) / 20;
do {
uint32_t a = fetch32(s);
uint32_t b = fetch32(s + 4);
uint32_t c = fetch32(s + 8);
uint32_t d = fetch32(s + 12);
uint32_t e = fetch32(s + 16);
h += a;
g += b;
f += c;
h = mur(d, h) + e;
g = mur(c, g) + a;
f = mur(b + e * c1, f) + d;
f += g;
g += f;
s += 20;
} while (--iters != 0);
g = ror32(g, 11) * c1;
g = ror32(g, 17) * c1;
f = ror32(f, 11) * c1;
f = ror32(f, 17) * c1;
h = ror32(h + g, 19);
h = h * 5 + 0xe6546b64;
h = ror32(h, 17) * c1;
h = ror32(h + f, 19);
h = h * 5 + 0xe6546b64;
h = ror32(h, 17) * c1;
return h;
}
uint32_t farmhash32_mk_with_seed(const char *s, size_t len, uint32_t seed) {
if (len <= 24) {
if (len >= 13) return farmhash32_mk_len_13_to_24(s, len, seed * c1);
else if (len >= 5) return farmhash32_mk_len_5_to_12(s, len, seed);
else return farmhash32_mk_len_0_to_4(s, len, seed);
}
uint32_t h = farmhash32_mk_len_13_to_24(s, 24, seed ^ len);
return mur(farmhash32_mk(s + 24, len - 24) + seed, h);
}
// farmhash su
#if CAN_USE_AESNI && CAN_USE_SSE42
uint32_t farmhash32_su(const char *s, size_t len) {
const uint32_t seed = 81;
if (len <= 24) {
return len <= 12 ?
(len <= 4 ?
farmhash32_mk_len_0_to_4(s, len, 0) :
farmhash32_mk_len_5_to_12(s, len, 0)) :
farmhash32_mk_len_13_to_24(s, len, 0);
}
if (len < 40) {
uint32_t a = len, b = seed * c2, c = a + b;
a += fetch32(s + len - 4);
b += fetch32(s + len - 20);
c += fetch32(s + len - 16);
uint32_t d = a;
a = ror32(a, 21);
a = mur(a, mur(b, _mm_crc32_u32(c, d)));
a += fetch32(s + len - 12);
b += fetch32(s + len - 8);
d += a;
a += d;
b = mur(b, d) * c2;
a = _mm_crc32_u32(a, b + c);
return farmhash32_mk_len_13_to_24(s, (len + 1) / 2, a) + b;
}
const __m128i cc1 = _mm_set1_epi32(c1);
const __m128i cc2 = _mm_set1_epi32(c2);
__m128i h = _mm_set1_epi32(seed);
__m128i g = _mm_set1_epi32(c1 * seed);
__m128i f = g;
__m128i k = _mm_set1_epi32(0xe6546b64);
__m128i q;