-
Notifications
You must be signed in to change notification settings - Fork 8
/
umash_long.inc
1058 lines (909 loc) · 29.7 KB
/
umash_long.inc
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
/* -*- mode: c; -*- vim: set ft=c: */
/*
* UMASH is distributed under the MIT license.
*
* SPDX-License-Identifier: MIT
*
* Copyright 2020-2022 Backtrace I/O, Inc.
* Copyright 2022 Paul Khuong
* Copyright 2022 Dougall Johnson
*
* 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.
*/
/**
* More wasteful routine for longer inputs (that can absorb I$ misses
* and fixed setup work).
*/
#if UMASH_LONG_INPUTS
/*
* Minimum byte size before switching to `umash_multiple_blocks`.
*
* Leaving this variable undefined disable calls to
* `umash_multiple_blocks.
*/
#define UMASH_MULTIPLE_BLOCKS_THRESHOLD 1024
#endif
typedef uint64_t umash_multiple_blocks_fn(uint64_t initial,
const uint64_t multipliers[static 2], const uint64_t *oh_ptr, uint64_t seed,
const void *blocks, size_t n_blocks);
typedef struct umash_fp umash_fprint_multiple_blocks_fn(struct umash_fp initial,
const uint64_t multipliers[static 2][2], const uint64_t *oh, uint64_t seed,
const void *data, size_t n_blocks);
/**
* Updates a 64-bit UMASH state for `n_blocks` 256-byte blocks in
* `data`.
*/
TEST_DEF umash_multiple_blocks_fn umash_multiple_blocks_generic;
/**
* Updates a 128-bit UMASH fingerprint state for `n_blocks` 256-byte
* blocks in `data`.
*/
TEST_DEF umash_fprint_multiple_blocks_fn umash_fprint_multiple_blocks_generic;
/**
* Runtime dispatch logic. When dynamic dispatch is enabled,
* `umash_multiple_blocks` just forwards the call to
* `umash_multiple_blocks_impl`, a function pointer. That pointer is
* initialised with a function that updates
* `umash_multiple_blocks_impl` with an implementation appropriate for
* the current CPU and tail-calls to that chosen implementation.
*
* When dynamic dispatch is disabled, `umash_multiple_blocks` just
* always forwards to `umash_multiple_blocks_generic`.
*/
#if defined(__x86_64__) && UMASH_DYNAMIC_DISPATCH
#include <stdatomic.h>
static umash_multiple_blocks_fn umash_multiple_blocks_initial,
umash_multiple_blocks_vpclmulqdq;
static umash_fprint_multiple_blocks_fn umash_fprint_multiple_blocks_initial,
umash_fprint_multiple_blocks_vpclmulqdq;
static umash_multiple_blocks_fn *_Atomic umash_multiple_blocks_impl =
umash_multiple_blocks_initial;
static umash_fprint_multiple_blocks_fn *_Atomic umash_fprint_multiple_blocks_impl =
umash_fprint_multiple_blocks_initial;
static COLD FN void
umash_long_pick(void)
{
umash_multiple_blocks_fn *umash;
umash_fprint_multiple_blocks_fn *fprint;
bool has_vpclmulqdq = false;
{
const uint32_t extended_features_level = 7;
const uint32_t vpclmulqdq_bit = 1UL << 10;
uint32_t eax, ebx, ecx, edx;
eax = ebx = ecx = edx = 0;
__asm__("cpuid" : "+a"(eax), "+b"(ebx), "+c"(ecx), "+d"(edx));
if (eax >= extended_features_level) {
eax = extended_features_level;
ebx = ecx = edx = 0;
__asm__("cpuid" : "+a"(eax), "+b"(ebx), "+c"(ecx), "+d"(edx));
has_vpclmulqdq = (ecx & vpclmulqdq_bit) != 0;
}
/* Confirm OS support for AVX. */
if (has_vpclmulqdq) {
uint64_t feature_mask;
/* 1: XSAVE SSE; 2: AVX enabled. */
uint64_t avx_mask = (1UL << 1) | (1UL << 2);
uint64_t hi, lo;
__asm__("xgetbv" : "=a"(lo), "=d"(hi) : "c"(0));
feature_mask = (hi << 32) | lo;
/* If the OS doesn't save AVX registers, stick to SSE. */
if ((feature_mask & avx_mask) != avx_mask)
has_vpclmulqdq = false;
}
}
if (has_vpclmulqdq) {
umash = umash_multiple_blocks_vpclmulqdq;
fprint = umash_fprint_multiple_blocks_vpclmulqdq;
} else {
umash = umash_multiple_blocks_generic;
fprint = umash_fprint_multiple_blocks_generic;
}
atomic_store_explicit(&umash_multiple_blocks_impl, umash, memory_order_relaxed);
atomic_store_explicit(
&umash_fprint_multiple_blocks_impl, fprint, memory_order_relaxed);
return;
}
static COLD FN uint64_t
umash_multiple_blocks_initial(uint64_t initial, const uint64_t multipliers[static 2],
const uint64_t *oh_ptr, uint64_t seed, const void *blocks, size_t n_blocks)
{
umash_multiple_blocks_fn *impl;
umash_long_pick();
impl = atomic_load_explicit(&umash_multiple_blocks_impl, memory_order_relaxed);
return impl(initial, multipliers, oh_ptr, seed, blocks, n_blocks);
}
TEST_DEF
#ifndef UMASH_TEST_ONLY
inline /* Can't have an extern inline refer to static data. */
#endif
uint64_t
umash_multiple_blocks(uint64_t initial, const uint64_t multipliers[static 2],
const uint64_t *oh_ptr, uint64_t seed, const void *blocks, size_t n_blocks)
{
/*
* We should use a relaxed load here, but compilers have
* trouble fusing that with the call. Directly calling the
* function pointer gets us a one-instruction memory indirect
* CALL on x86-64, versus a load, and a roundtrip through the
* stack if we use a discrete atomic load (on gcc 8).
*/
return umash_multiple_blocks_impl(
initial, multipliers, oh_ptr, seed, blocks, n_blocks);
}
static COLD FN struct umash_fp
umash_fprint_multiple_blocks_initial(struct umash_fp initial,
const uint64_t multipliers[static 2][2], const uint64_t *oh, uint64_t seed,
const void *data, size_t n_blocks)
{
umash_fprint_multiple_blocks_fn *impl;
umash_long_pick();
impl = atomic_load_explicit(
&umash_fprint_multiple_blocks_impl, memory_order_relaxed);
return impl(initial, multipliers, oh, seed, data, n_blocks);
}
TEST_DEF
#ifndef UMASH_TEST_ONLY
inline /* Can't have an extern inline refer to static data. */
#endif
struct umash_fp
umash_fprint_multiple_blocks(struct umash_fp initial,
const uint64_t multipliers[static 2][2], const uint64_t *oh, uint64_t seed,
const void *data, size_t n_blocks)
{
/*
* We should use a relaxed load here, but compilers have
* trouble fusing that with the call. Directly calling the
* function pointer gets us a one-instruction memory indirect
* CALL on x86-64, versus a load, and a roundtrip through the
* stack if we use a discrete atomic load (on gcc 8).
*/
return umash_fprint_multiple_blocks_impl(
initial, multipliers, oh, seed, data, n_blocks);
}
#else
TEST_DEF inline uint64_t
umash_multiple_blocks(uint64_t initial, const uint64_t multipliers[static 2],
const uint64_t *oh_ptr, uint64_t seed, const void *blocks, size_t n_blocks)
{
return umash_multiple_blocks_generic(
initial, multipliers, oh_ptr, seed, blocks, n_blocks);
}
TEST_DEF inline struct umash_fp
umash_fprint_multiple_blocks(struct umash_fp initial,
const uint64_t multipliers[static 2][2], const uint64_t *oh, uint64_t seed,
const void *data, size_t n_blocks)
{
return umash_fprint_multiple_blocks_generic(
initial, multipliers, oh, seed, data, n_blocks);
}
#endif
#define SPLIT_ACCUMULATOR_MAX_FIXUP 3
/**
* A split accumulator represents a value (mod 2**64 - 8) as the
* evaluated sum `base + 8 * fixup`, where `fixup <=
* SPLIT_ACCUMULATOR_MAX_FIXUP`
*/
#ifndef UMASH_TEST_ONLY /* Also defined in `umash_test_only.h` */
struct split_accumulator {
uint64_t base;
uint64_t fixup;
};
#endif
/**
* Reduces the split accumulator's value to a value < 2**64 - 8.
*/
TEST_DEF inline uint64_t
split_accumulator_eval(struct split_accumulator acc)
{
return add_mod_slow(acc.base, 8 * acc.fixup);
}
/**
* Returns the updated version of `acc` after a double-pumped Horner
* update: acc = m0 * (acc + h0) + m1 * h1 (mod 2**64 - 8).
*/
TEST_DEF inline struct split_accumulator
split_accumulator_update(const struct split_accumulator acc, const uint64_t m0,
const uint64_t m1, uint64_t h0, const uint64_t h1)
{
uint64_t partial;
uint64_t lo0, hi0, lo1, hi1;
uint64_t hi;
unsigned long long sum;
int8_t fixup;
mul128(m1, h1, &hi1, &lo1);
/* partial \eqv (acc.base + h0 + 8 * acc.fixup) mod 2**64 - 8 */
if (UNLIKELY(h0 > -8ULL * (SPLIT_ACCUMULATOR_MAX_FIXUP + 1))) {
h0 = add_mod_slow(h0, 8 * acc.fixup);
} else {
/*
* h0 is a hash value, so it's unlikely to be
* extremely high. In the common case, this addition
* doesn't overflows.
*/
h0 += 8 * acc.fixup;
}
partial = add_mod_fast(acc.base, h0);
mul128(partial, m0, &hi0, &lo0);
#if defined(__x86_64__) && UMASH_INLINE_ASM
{
uint8_t cf0, cf1;
__asm__("addq %3, %0"
: "=r"(sum), "=@ccc"(cf0)
: "%0"(lo0), "r"(lo1)
: "cc");
hi = hi0 + hi1;
__asm__("shlq $3, %0" : "+r"(hi), "=@ccc"(cf1) : : "cc");
__asm__("addq %3, %0\n\t"
"adcb %5, %1"
: "=r"(sum), "=r"(fixup)
: "%0"(sum), "r"(hi), "%1"(cf0), "r"(cf1)
: "cc");
}
#else
fixup = __builtin_uaddll_overflow(lo0, lo1, &sum);
assert(hi0 < (1UL << 61));
assert(hi1 < (1UL << 61));
/* hi0 and hi1 < 2**61, so this addition never overflows. */
hi = hi0 + hi1;
fixup += (hi & (1ULL << 61)) != 0;
hi *= 8;
fixup += __builtin_uaddll_overflow(sum, hi, &sum);
#endif
return (struct split_accumulator) {
.base = sum,
/* Avoid sign extension: we know `fixup` is non-negative. */
.fixup = (uint8_t)fixup,
};
}
#ifndef UMASH_LONG_PH_PARAMS_IN_REGS
/*
* Cache hash parameters in SIMD registers if there's room. Neon on
* aarch64 has 32x128-bit, and AVX-512 VL extends SSE/AVX to 32
* registers (if AVX-512 VL is available, the compiler may silently
* use the additional registers, so we might as well optimise for
* their presence). Our 15x 128-bit PH parameters fit in 32 registers.
*
* Platform-specific routines make their own decision.
*/
#if defined(__aarch64__) || defined(__ARM_ARCH_ISA_A64) || defined(__AVX512VL__)
#define UMASH_LONG_PH_PARAMS_IN_REGS 1
#else
#define UMASH_LONG_PH_PARAMS_IN_REGS 0
#endif
#endif
TEST_DEF HOT uint64_t
umash_multiple_blocks_generic(uint64_t initial, const uint64_t multipliers[static 2],
const uint64_t *oh_ptr, uint64_t seed, const void *blocks, size_t n_blocks)
{
const uint64_t m0 = multipliers[0];
const uint64_t m1 = multipliers[1];
const uint64_t kx = oh_ptr[UMASH_OH_PARAM_COUNT - 2];
const uint64_t ky = oh_ptr[UMASH_OH_PARAM_COUNT - 1];
struct split_accumulator ret = { .base = initial };
#if UMASH_LONG_PH_PARAMS_IN_REGS
#define K(N) \
v128 k##N; \
memcpy(&k##N, &oh_ptr[N], sizeof(k##N))
K(0);
K(2);
K(4);
K(6);
K(8);
K(10);
K(12);
K(14);
K(16);
K(18);
K(20);
K(22);
K(24);
K(26);
K(28);
#undef K
#define GET_PARAM(V, I) ((V) = k##I)
#else
#define GET_PARAM(V, I) memcpy(&(V), &oh_ptr[I], sizeof((V)))
#endif
assert(n_blocks > 0);
do {
const void *data = blocks;
struct umash_oh oh;
v128 acc = V128_ZERO;
blocks = (const char *)blocks + BLOCK_SIZE;
/*
* FORCE() makes sure the compiler computes the value
* of `acc` at that program points. Forcing a full
* computation prevents the compiler from evaluating
* the inner loop's xor-reduction tree widely: the
* bottleneck is in the carryless multiplications.
*/
#if UMASH_INLINE_ASM
#define FORCE() __asm__("" : "+x"(acc))
#else
#define FORCE() ((void)0)
#endif
#define PH(I) \
do { \
v128 x, k; \
\
memcpy(&x, data, sizeof(x)); \
data = (const char *)data + sizeof(x); \
\
GET_PARAM(k, I); \
x ^= k; \
acc ^= v128_clmul_cross(x); \
} while (0)
#if defined(__ARM_FEATURE_CRYPTO)
/*
* Specialised PH2 for the first iteration (overwrite the accumulator
* instead of `xor`ing into it). We use inline assembly to issue
* instructions in pairs that work well on Apple ARMs.
*/
#if !UMASH_INLINE_ASM
#define PH2_START_BODY \
do { \
v128 result; \
result = vreinterpretq_u64_p128( \
vmull_p64(vgetq_lane_u64(x1, 0), vgetq_lane_u64(swapped, 0))); \
\
acc = vreinterpretq_u64_p128(vmull_high_p64( \
vreinterpretq_p64_u64(x2), vreinterpretq_p64_u64(swapped))); \
acc ^= result; \
} while (0)
#elif defined(__clang__)
#define PH2_START_BODY \
do { \
v128 tmp; \
__asm__("pmull.1q %[tmp], %[swapped], %[x1] \n\t" \
"pmull2.1q %[acc], %[swapped], %[x2] \n\t" \
"eor.16b %[acc], %[acc], %[tmp] \n\t" \
: [acc] "=w"(acc), [tmp]"=&w"(tmp) \
: [swapped] "w"(swapped), [x1] "w"(x1), [x2] "w"(x2)); \
} while (0)
#else
/* Assume GCC syntax if !clang. */
#define PH2_START_BODY \
do { \
v128 tmp; \
__asm__("pmull %[tmp].1q, %[swapped].1d, %[x1].1d \n\t" \
"pmull2 %[acc].1q, %[swapped].2d, %[x2].2d \n\t" \
"eor %[acc].16b, %[acc].16b, %[tmp].16b \n\t" \
: [acc] "=w"(acc), [tmp]"=&w"(tmp) \
: [swapped] "w"(swapped), [x1] "w"(x1), [x2] "w"(x2)); \
} while (0)
#endif
#define PH2_START(I, J) \
do { \
v128 x1, x2; \
v128 k; \
\
memcpy(&x1, data, sizeof(x1)); \
data = (const char *)data + sizeof(x1); \
\
memcpy(&x2, data, sizeof(x2)); \
data = (const char *)data + sizeof(x2); \
\
GET_PARAM(k, I); \
x1 ^= k; \
GET_PARAM(k, J); \
x2 ^= k; \
\
v128 swapped = vextq_u64(x1, x2, 1); \
PH2_START_BODY; \
} while (0)
/*
* Handle pairs of PH with a single `vextq` to transpose two 128-bit
* registers at once. This lets us use `pmull` on the low and high
* lanes for the first and second PH.
*/
#if !UMASH_INLINE_ASM
#define PH2_BODY \
do { \
acc ^= vreinterpretq_u64_p128( \
vmull_p64(vgetq_lane_u64(x1, 0), vgetq_lane_u64(swapped, 0))); \
acc ^= vreinterpretq_u64_p128(vmull_high_p64( \
vreinterpretq_p64_u64(x2), vreinterpretq_p64_u64(swapped))); \
} while (0)
#elif defined(__clang__)
#define PH2_BODY \
do { \
v128 tmp; \
__asm__("pmull.1q %[tmp], %[swapped], %[x1] \n\t" \
"eor.16b %[tmp], %[tmp], %[acc] \n\t" \
"pmull2.1q %[acc], %[swapped], %[x2] \n\t" \
"eor.16b %[acc], %[acc], %[tmp] \n\t" \
: [acc] "+w"(acc), [tmp] "=&w"(tmp) \
: [swapped] "w"(swapped), [x1] "w"(x1), [x2] "w"(x2)); \
} while (0)
#else
#define PH2_BODY \
do { \
v128 tmp; \
__asm__("pmull %[tmp].1q, %[swapped].1d, %[x1].1d \n\t" \
"eor %[tmp].16b, %[tmp].16b, %[acc].16b \n\t" \
"pmull2 %[acc].1q, %[swapped].2d, %[x2].2d \n\t" \
"eor %[acc].16b, %[acc].16b, %[tmp].16b \n\t" \
: [acc] "+w"(acc), [tmp]"=&w"(tmp) \
: [swapped] "w"(swapped), [x1] "w"(x1), [x2] "w"(x2)); \
} while (0)
#endif
#define PH2(I, J) \
do { \
v128 x1, x2; \
v128 k; \
\
memcpy(&x1, data, sizeof(x1)); \
data = (const char *)data + sizeof(x1); \
\
memcpy(&x2, data, sizeof(x2)); \
data = (const char *)data + sizeof(x2); \
\
GET_PARAM(k, I); \
x1 ^= k; \
GET_PARAM(k, J); \
x2 ^= k; \
\
v128 swapped = vextq_u64(x1, x2, 1); \
PH2_BODY; \
} while (0)
#else
#define PH2(I, J) \
do { \
PH(I); \
PH(J); \
} while (0)
#define PH2_START PH2
#endif
PH2_START(0, 2);
FORCE();
PH2(4, 6);
FORCE();
PH2(8, 10);
FORCE();
PH2(12, 14);
FORCE();
PH2(16, 18);
FORCE();
PH2(20, 22);
FORCE();
PH2(24, 26);
FORCE();
PH(28);
#ifdef PH2_START_BODY
#undef PH2_START_BODY
#undef PH2_BODY
#endif
#undef PH2_START
#undef PH2
#undef PH
#undef FORCE
#undef GET_PARAM
memcpy(&oh, &acc, sizeof(oh));
/* Final ENH chunk. */
{
uint64_t x, y, enh_hi, enh_lo;
memcpy(&x, data, sizeof(x));
data = (const char *)data + sizeof(x);
memcpy(&y, data, sizeof(y));
data = (const char *)data + sizeof(y);
x += kx;
y += ky;
mul128(x, y, &enh_hi, &enh_lo);
enh_hi += seed;
oh.bits[0] ^= enh_lo;
oh.bits[1] ^= enh_hi ^ enh_lo;
}
ret = split_accumulator_update(ret, m0, m1, oh.bits[0], oh.bits[1]);
} while (--n_blocks);
return split_accumulator_eval(ret);
}
TEST_DEF HOT struct umash_fp
umash_fprint_multiple_blocks_generic(struct umash_fp initial,
const uint64_t multipliers[static 2][2], const uint64_t *oh, uint64_t seed,
const void *data, size_t n_blocks)
{
const v128 lrc_init =
v128_create(oh[UMASH_OH_PARAM_COUNT], oh[UMASH_OH_PARAM_COUNT + 1]);
const uint64_t m00 = multipliers[0][0];
const uint64_t m01 = multipliers[0][1];
const uint64_t m10 = multipliers[1][0];
const uint64_t m11 = multipliers[1][1];
struct split_accumulator acc0 = { .base = initial.hash[0] };
struct split_accumulator acc1 = { .base = initial.hash[1] };
#if UMASH_LONG_PH_PARAMS_IN_REGS
#define K(N) \
v128 k##N; \
memcpy(&k##N, &oh[N], sizeof(k##N))
K(0);
K(2);
K(4);
K(6);
K(8);
K(10);
K(12);
K(14);
K(16);
K(18);
K(20);
K(22);
K(24);
K(26);
K(28);
K(30);
#undef K
#define GET_PARAM(V, I) ((V) = k##I)
#else
#define GET_PARAM(V, I) memcpy(&(V), &oh[I], sizeof((V)))
#endif
do {
struct umash_oh compressed[2];
v128 acc = V128_ZERO; /* Base umash */
v128 acc_shifted = V128_ZERO; /* Accumulates shifted values */
v128 lrc = lrc_init;
const void *block = data;
data = (const char *)data + BLOCK_SIZE;
#if UMASH_INLINE_ASM
#define FORCE() __asm__("" : "+x"(acc), "+x"(acc_shifted), "+x"(lrc))
#else
#define FORCE() ((void)0)
#endif
#define TWIST(I) \
do { \
v128 x, k; \
\
memcpy(&x, block, sizeof(x)); \
block = (const char *)block + sizeof(x); \
\
GET_PARAM(k, I); \
\
x ^= k; \
lrc ^= x; \
\
x = v128_clmul_cross(x); \
\
acc ^= x; \
\
if (I == 28) \
break; \
\
acc_shifted ^= x; \
acc_shifted = v128_shift(acc_shifted); \
} while (0)
TWIST(0);
FORCE();
TWIST(2);
FORCE();
TWIST(4);
FORCE();
TWIST(6);
FORCE();
TWIST(8);
FORCE();
TWIST(10);
FORCE();
TWIST(12);
FORCE();
TWIST(14);
FORCE();
TWIST(16);
FORCE();
TWIST(18);
FORCE();
TWIST(20);
FORCE();
TWIST(22);
FORCE();
TWIST(24);
FORCE();
TWIST(26);
FORCE();
TWIST(28);
FORCE();
#undef TWIST
#undef FORCE
{
v128 x, k;
memcpy(&x, block, sizeof(x));
GET_PARAM(k, 30);
lrc ^= x ^ k;
}
#undef GET_PARAM
acc_shifted ^= acc;
acc_shifted = v128_shift(acc_shifted);
acc_shifted ^= v128_clmul_cross(lrc);
memcpy(&compressed[0], &acc, sizeof(compressed[0]));
memcpy(&compressed[1], &acc_shifted, sizeof(compressed[1]));
{
uint64_t x, y, kx, ky, enh_hi, enh_lo;
memcpy(&x, block, sizeof(x));
block = (const char *)block + sizeof(x);
memcpy(&y, block, sizeof(y));
kx = x + oh[30];
ky = y + oh[31];
mul128(kx, ky, &enh_hi, &enh_lo);
enh_hi += seed;
enh_hi ^= enh_lo;
compressed[0].bits[0] ^= enh_lo;
compressed[0].bits[1] ^= enh_hi;
compressed[1].bits[0] ^= enh_lo;
compressed[1].bits[1] ^= enh_hi;
}
acc0 = split_accumulator_update(
acc0, m00, m01, compressed[0].bits[0], compressed[0].bits[1]);
acc1 = split_accumulator_update(
acc1, m10, m11, compressed[1].bits[0], compressed[1].bits[1]);
} while (--n_blocks);
return (struct umash_fp) {
.hash = {
split_accumulator_eval(acc0),
split_accumulator_eval(acc1),
},
};
}
#if defined(__x86_64__) && UMASH_DYNAMIC_DISPATCH
/**
* Updates a 64-bit UMASH state for `n_blocks` 256-byte blocks in data.
*/
TEST_DEF HOT __attribute__((__target__("avx2,vpclmulqdq"))) uint64_t
umash_multiple_blocks_vpclmulqdq(uint64_t initial, const uint64_t multipliers[static 2],
const uint64_t *oh_ptr, uint64_t seed, const void *blocks, size_t n_blocks)
{
const uint64_t m0 = multipliers[0];
const uint64_t m1 = multipliers[1];
__m256i k0, k4, k8, k12, k16, k20, k24;
v128 k28;
const uint64_t kx = oh_ptr[UMASH_OH_PARAM_COUNT - 2];
const uint64_t ky = oh_ptr[UMASH_OH_PARAM_COUNT - 1];
struct split_accumulator ret = { .base = initial };
assert(n_blocks > 0);
#if UMASH_INLINE_ASM
/*
* This type represents unaligned AVX2-sized regions of
* memory.
*/
struct b256 {
char bytes[32];
};
#define LOADU(DST, PTR) \
__asm__("vmovdqu %1, %0" : "=x"(DST) : "m"(*(const struct b256 *)PTR))
#else
#define LOADU(DST, PTR) ((DST) = _mm256_loadu_si256((void *)(PTR)))
#endif
LOADU(k0, &oh_ptr[0]);
LOADU(k4, &oh_ptr[4]);
LOADU(k8, &oh_ptr[8]);
LOADU(k12, &oh_ptr[12]);
LOADU(k16, &oh_ptr[16]);
LOADU(k20, &oh_ptr[20]);
LOADU(k24, &oh_ptr[24]);
memcpy(&k28, &oh_ptr[28], sizeof(k28));
do {
const void *data = blocks;
struct umash_oh oh;
__m256i acc2 = _mm256_setzero_si256();
blocks = (const char *)blocks + BLOCK_SIZE;
#define PH(I) \
do { \
__m256i x; \
\
LOADU(x, data); \
data = (const char *)data + sizeof(x); \
\
x = _mm256_xor_si256(x, k##I); \
x = _mm256_clmulepi64_epi128(x, x, 1); \
acc2 = _mm256_xor_si256(acc2, x); \
} while (0)
PH(0);
PH(4);
PH(8);
PH(12);
PH(16);
PH(20);
PH(24);
#undef PH
#undef LOADU
{
v128 x;
memcpy(&x, data, sizeof(x));
data = (const char *)data + sizeof(x);
x ^= k28;
x = v128_clmul_cross(x);
x ^= _mm256_extracti128_si256(acc2, 0) ^
_mm256_extracti128_si256(acc2, 1);
memcpy(&oh, &x, sizeof(oh));
}
/* Final ENH chunk. */
{
uint64_t x, y, enh_hi, enh_lo;
memcpy(&x, data, sizeof(x));
data = (const char *)data + sizeof(x);
memcpy(&y, data, sizeof(y));
data = (const char *)data + sizeof(y);
x += kx;
y += ky;
mul128(x, y, &enh_hi, &enh_lo);
enh_hi += seed;
oh.bits[0] ^= enh_lo;
oh.bits[1] ^= enh_hi ^ enh_lo;
}
ret = split_accumulator_update(ret, m0, m1, oh.bits[0], oh.bits[1]);
} while (--n_blocks);
return split_accumulator_eval(ret);
}
TEST_DEF HOT __attribute__((__target__("avx2,vpclmulqdq"))) struct umash_fp
umash_fprint_multiple_blocks_vpclmulqdq(struct umash_fp initial,
const uint64_t multipliers[static 2][2], const uint64_t *oh, uint64_t seed,
const void *data, size_t n_blocks)
{
__m256i k0, k4, k8, k12, k16, k20, k24, k28;
__m256i lrc_init = { 0 };
const uint64_t m00 = multipliers[0][0];
const uint64_t m01 = multipliers[0][1];
const uint64_t m10 = multipliers[1][0];
const uint64_t m11 = multipliers[1][1];
struct split_accumulator acc0 = { .base = initial.hash[0] };
struct split_accumulator acc1 = { .base = initial.hash[1] };
#if UMASH_INLINE_ASM
/*
* This type represents unaligned AVX2-sized regions of
* memory.
*/
struct b256 {
char bytes[32];
};
#define LOADU(DST, PTR) \
__asm__("vmovdqu %1, %0" : "=x"(DST) : "m"(*(const struct b256 *)PTR))
#else
#define LOADU(DST, PTR) ((DST) = _mm256_loadu_si256((void *)(PTR)))
#endif
LOADU(k0, &oh[0]);
LOADU(k4, &oh[4]);
LOADU(k8, &oh[8]);
LOADU(k12, &oh[12]);
LOADU(k16, &oh[16]);
LOADU(k20, &oh[20]);
LOADU(k24, &oh[24]);
LOADU(k28, &oh[28]);
{
__m128i lrc_key;
memcpy(&lrc_key, &oh[UMASH_OH_PARAM_COUNT], sizeof(lrc_key));
lrc_init = _mm256_insertf128_si256(lrc_init, lrc_key, 0);
}
/*
* A UMASH fingerprint combines a regular (primary) 64-bit
* UMASH hash value with a secondary hash value that mostly
* reuses the primary's OH-mixed values.
*
* The key differences are:
*
* 1. the secondary hash generates an additional chunk by
* xoring together all the input chunks, themselves xor-ed
* with the corresponding random bits in the OH key.
* 2. the first 14 PH-mixed values are shifted by different
* values (the first one by 15, second by 14, etc.)
* 3. the first 15 PH-mixed values are also shifted by 1
* 4. the values obtained by 14 and 15 are xored together
* before xoring in the new chunk's PH-mixed value and
* the ENH value.
*
* This AVX2 implementation derives the additional chunk in a
* 256-bit AVX register, and only xors its two halves together
* before CLMUL.
*
* Similarly, the PH-mixed values that are only shifted by 1
* are xored in an AVX register, and the two halves are only
* xored together before shifting the 128-bit result by 1.
*
* As for the 14 PH values with distinct shifts, they too are
* first accumulated in an AVX register, where each 128-bit
* half is shifted by 2. When merging the result in a 128-bit
* result, the low half is shifted left (by 1) once more
* before xoring the halves together.
*/
do {
struct umash_oh compressed[2];
__m256i acc2 = { 0 }; /* Base umash */
/*
* Accumulates shifted values in odd/even pairs, so
* each iteration must shift by two, until the final
* merge.
*/
__m256i acc_shifted2 = { 0 };
__m256i lrc2 = lrc_init;
v128 acc, acc_shifted, lrc;
const void *block = data;
data = (const char *)data + BLOCK_SIZE;
#define TWIST(I) \
do { \
__m256i x; \
\
LOADU(x, block); \
block = (const char *)block + sizeof(x); \
\
x ^= k##I; \
lrc2 ^= x; \
x = _mm256_clmulepi64_epi128(x, x, 1); \
\
acc2 ^= x; \
x = _mm256_slli_epi64(x, (24 - I) / 2); \
acc_shifted2 ^= x; \
} while (0)
TWIST(0);
TWIST(4);
TWIST(8);
TWIST(12);
TWIST(16);
TWIST(20);
TWIST(24);
#undef TWIST
{
__m256i x;
v128 x_lo;
LOADU(x, block);
block = (const char *)block + sizeof(x_lo);
x ^= k28;
lrc2 ^= x;
lrc = _mm256_extracti128_si256(lrc2, 0) ^
_mm256_extracti128_si256(lrc2, 1);
acc_shifted =