-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathLeopard2BackendAVX2.cpp
More file actions
9366 lines (8962 loc) · 341 KB
/
Copy pathLeopard2BackendAVX2.cpp
File metadata and controls
9366 lines (8962 loc) · 341 KB
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) 2017 Christopher A. Taylor. All rights reserved.
See Leopard2Backend.h for the full BSD license notice.
*/
#include "Leopard2Backend.h"
#include "LeopardCommon.h"
#include <algorithm>
#include <cstring>
#include <immintrin.h>
#include <memory>
#include <new>
namespace leopard { namespace backend {
#if defined(LEO2_AVX512_VARIANT)
# if defined(LEO2_GFNI_VARIANT)
# define LEO2_AVX_BACKEND_NAME "avx512vl-gfni"
# else
# define LEO2_AVX_BACKEND_NAME "avx512vl"
# endif
# define LEO2_AVX_BACKEND_KIND LEO2_BACKEND_AVX512
# define LEO2_AVX_INITIALIZER InitializeAVX512
# define LEO2_AVX_TABLE_STATE TestGetAVX512TableState
#elif defined(LEO2_GFNI_MEMBER)
// Production GFNI member: its own backend identity, qualified by CPUID
// GFNI+AVX2, selected only by explicit request. LEO2_GFNI_VARIANT without
// LEO2_GFNI_MEMBER remains the in-place evaluation configuration that reuses
// the AVX2 identity for A/B experiments.
# define LEO2_AVX_BACKEND_KIND LEO2_BACKEND_GFNI
# define LEO2_AVX_BACKEND_NAME "avx2-gfni"
# define LEO2_AVX_INITIALIZER InitializeGFNI
# define LEO2_AVX_TABLE_STATE TestGetGFNITableState
#else
# define LEO2_AVX_BACKEND_KIND LEO2_BACKEND_AVX2
# define LEO2_AVX_BACKEND_NAME "avx2"
# define LEO2_AVX_INITIALIZER InitializeAVX2
# define LEO2_AVX_TABLE_STATE TestGetAVX2TableState
#endif
// GFNI variant contract.
//
// A fixed multiplication by one field element is a GF(2)-linear map on the
// symbol bits, so each byte-wide component is an 8-by-8 bit matrix and
// VGF2P8AFFINEQB evaluates it in one instruction. The measured operand order
// on this family is operand_bit = 8 * (7 - output_bit) + input_bit, which is
// the order the exhaustive experiment under experiments/leopard2/gfni_affine
// validated against all 65,536 GF8 products.
//
// GF8 deliberately reuses the nibble-table storage shape: each 16-byte row
// instead holds one affine matrix duplicated, and a 128-bit broadcast fills
// every 64-bit lane with that matrix. GF16 uses four packed 64-bit matrices
// per multiplier and broadcasts them at use sites, reducing its table from
// 8 MiB to 2 MiB. Scalar tails evaluate these same affine operands directly.
#if defined(LEO2_GFNI_VARIANT)
static LEO_FORCE_INLINE uint64_t GFNIAffineMatrixBit(
unsigned output_bit, unsigned input_bit)
{
return static_cast<uint64_t>(1) << (8 * (7 - output_bit) + input_bit);
}
#ifdef LEO_HAS_FF8
static void GFNIStoreMatrix(uint8_t row[16], uint64_t matrix)
{
for (unsigned byte = 0; byte < 8; ++byte)
{
const uint8_t value = static_cast<uint8_t>(matrix >> (8 * byte));
row[byte] = value;
row[byte + 8] = value;
}
}
#endif
static LEO_FORCE_INLINE uint8_t GFNIParity(uint8_t value)
{
value = static_cast<uint8_t>(value ^ (value >> 4));
value = static_cast<uint8_t>(value ^ (value >> 2));
return static_cast<uint8_t>((value ^ (value >> 1)) & 1U);
}
// Scalar evaluation of one stored affine matrix. Sub-vector tails use this
// instead of a second set of nibble tables, so the variant needs no extra
// storage beyond the affine operands themselves. Operand byte 7 - output_bit
// holds that output bit's input mask, matching GFNIAffineMatrixBit.
static LEO_FORCE_INLINE uint8_t GFNIApplyMatrix(
const uint8_t row[16], uint8_t value)
{
uint8_t result = 0;
for (unsigned output_bit = 0; output_bit < 8; ++output_bit)
result = static_cast<uint8_t>(result |
(GFNIParity(static_cast<uint8_t>(row[7 - output_bit] & value))
<< output_bit));
return result;
}
// Packed-form twins for the GF16 affine tables, which store one 8-byte matrix
// per block instead of a duplicated 16-byte row. Byte b of the integer is
// row byte b of the stored form above, so `set1_epi64x` fills the four 64-bit
// lanes exactly as the 16-byte broadcast did and VGF2P8AFFINEQB sees
// identical operands.
static LEO_FORCE_INLINE uint8_t GFNIApplyMatrix64(
uint64_t matrix, uint8_t value)
{
uint8_t result = 0;
for (unsigned output_bit = 0; output_bit < 8; ++output_bit)
result = static_cast<uint8_t>(result |
(GFNIParity(static_cast<uint8_t>(
static_cast<uint8_t>(matrix >> (8 * (7 - output_bit))) &
value)) << output_bit));
return result;
}
static LEO_FORCE_INLINE __m256i BroadcastAffine(uint64_t matrix)
{
return _mm256_set1_epi64x(static_cast<long long>(matrix));
}
#endif
#ifdef LEO_HAS_FF8
struct FF8NibbleTable
{
uint8_t low[16];
uint8_t high[16];
};
static const FF8NibbleTable* FF8Tables = NULL;
#endif
#ifdef LEO_HAS_FF16
#if defined(LEO2_GFNI_VARIANT)
// Packed affine storage: the four 8x8 GF(2) matrix blocks of one fixed GF16
// multiplication, 32 bytes per logarithm (2 MiB total) instead of the nibble
// shape's 128 bytes (8 MiB). Vector kernels broadcast each 64-bit matrix
// with vpbroadcastq, which fills the four 64-bit lanes exactly as the former
// 16-byte duplicated-row broadcast did, so VGF2P8AFFINEQB sees identical
// operands. GFNI doc production requirement 2.
struct FF16AffineTable
{
uint64_t block[4];
};
static const FF16AffineTable* FF16Tables = NULL;
typedef FF16AffineTable FF16Table;
#else
struct FF16NibbleTable
{
uint8_t low[4][16];
uint8_t high[4][16];
};
static const FF16NibbleTable* FF16Tables = NULL;
typedef FF16NibbleTable FF16Table;
#endif
#endif
#ifdef LEO_HAS_FF8
static uint8_t FF8Product(uint16_t log, uint8_t value)
{
const FF8NibbleTable& table = FF8Tables[log];
#if defined(LEO2_GFNI_VARIANT)
return GFNIApplyMatrix(table.low, value);
#else
return static_cast<uint8_t>(
table.low[value & 15U] ^ table.high[value >> 4]);
#endif
}
#endif
#if defined(LEO_HAS_FF8) && !defined(LEO2_AVX512_VARIANT) && \
!defined(LEO2_GFNI_VARIANT)
static LEO_FORCE_INLINE __m256i AVX2AddMod255(
__m256i a, __m256i b)
{
const __m256i mask = _mm256_set1_epi16(255);
__m256i sum = _mm256_add_epi16(a, b);
sum = _mm256_add_epi16(sum, _mm256_srli_epi16(sum, 8));
return _mm256_and_si256(sum, mask);
}
static LEO_FORCE_INLINE __m256i AVX2SubMod255(
__m256i a, __m256i b)
{
const __m256i modulus = _mm256_set1_epi16(255);
const __m256i borrow = _mm256_cmpgt_epi16(b, a);
return _mm256_add_epi16(
_mm256_sub_epi16(a, b), _mm256_and_si256(borrow, modulus));
}
static LEO_FORCE_INLINE void AVX2WalshPair(
__m256i a, __m256i b, __m256i& sum, __m256i& difference)
{
sum = AVX2AddMod255(a, b);
difference = AVX2SubMod255(a, b);
}
template<int Distance>
static LEO_FORCE_INLINE __m256i AVX2Walsh16Stage(__m256i value)
{
__m256i swapped;
if (Distance == 1)
{
swapped = _mm256_shufflelo_epi16(value, 0xb1);
swapped = _mm256_shufflehi_epi16(swapped, 0xb1);
}
else if (Distance == 2)
{
swapped = _mm256_shufflelo_epi16(value, 0x4e);
swapped = _mm256_shufflehi_epi16(swapped, 0x4e);
}
else if (Distance == 4)
swapped = _mm256_shuffle_epi32(value, 0x4e);
else
swapped = _mm256_permute2x128_si256(value, value, 0x01);
__m256i sum, difference;
AVX2WalshPair(value, swapped, sum, difference);
if (Distance == 1)
{
difference = _mm256_shufflelo_epi16(difference, 0xb1);
difference = _mm256_shufflehi_epi16(difference, 0xb1);
return _mm256_blend_epi16(sum, difference, 0xaa);
}
if (Distance == 2)
{
difference = _mm256_shufflelo_epi16(difference, 0x4e);
difference = _mm256_shufflehi_epi16(difference, 0x4e);
return _mm256_blend_epi16(sum, difference, 0xcc);
}
if (Distance == 4)
{
difference = _mm256_shuffle_epi32(difference, 0x4e);
return _mm256_blend_epi16(sum, difference, 0xf0);
}
difference = _mm256_permute2x128_si256(
difference, difference, 0x01);
return _mm256_blend_epi32(sum, difference, 0xf0);
}
static LEO_FORCE_INLINE __m256i AVX2Walsh16(__m256i value)
{
value = AVX2Walsh16Stage<1>(value);
value = AVX2Walsh16Stage<2>(value);
value = AVX2Walsh16Stage<4>(value);
return AVX2Walsh16Stage<8>(value);
}
static LEO_FORCE_INLINE __m256i AVX2PackMod255(
__m256i low, __m256i high)
{
// vpackuswb operates independently in each 128-bit lane. Restore the
// original contiguous element order after packing.
return _mm256_permute4x64_epi64(
_mm256_packus_epi16(low, high), 0xd8);
}
static LEO_FORCE_INLINE void AVX2ExpandBytes(
__m256i bytes, __m256i& low, __m256i& high)
{
low = _mm256_cvtepu8_epi16(_mm256_castsi256_si128(bytes));
high = _mm256_cvtepu8_epi16(_mm256_extracti128_si256(bytes, 1));
}
static void AVX2WalshTransform(uint8_t* data, uint32_t n)
{
for (uint32_t offset = 0; offset < n; offset += 32)
{
const __m256i packed = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(data + offset));
__m256i low, high;
AVX2ExpandBytes(packed, low, high);
low = AVX2Walsh16(low);
high = AVX2Walsh16(high);
AVX2WalshPair(low, high, low, high);
_mm256_storeu_si256(reinterpret_cast<__m256i*>(data + offset),
AVX2PackMod255(low, high));
}
for (uint32_t distance = 32; distance < n; distance <<= 1)
{
const uint32_t group_size = distance << 1;
for (uint32_t group = 0; group < n; group += group_size)
{
for (uint32_t offset = 0; offset < distance; offset += 32)
{
uint8_t* const a_pointer = data + group + offset;
uint8_t* const b_pointer = a_pointer + distance;
__m256i a_low, a_high, b_low, b_high;
AVX2ExpandBytes(_mm256_loadu_si256(
reinterpret_cast<const __m256i*>(a_pointer)),
a_low, a_high);
AVX2ExpandBytes(_mm256_loadu_si256(
reinterpret_cast<const __m256i*>(b_pointer)),
b_low, b_high);
__m256i sum_low, sum_high, difference_low, difference_high;
AVX2WalshPair(
a_low, b_low, sum_low, difference_low);
AVX2WalshPair(
a_high, b_high, sum_high, difference_high);
_mm256_storeu_si256(
reinterpret_cast<__m256i*>(a_pointer),
AVX2PackMod255(sum_low, sum_high));
_mm256_storeu_si256(
reinterpret_cast<__m256i*>(b_pointer),
AVX2PackMod255(difference_low, difference_high));
}
}
}
}
static LEO_FORCE_INLINE __m256i AVX2MultiplyMod255(
__m256i a, __m256i b)
{
const __m256i mask = _mm256_set1_epi16(255);
const __m256i cutoff = _mm256_set1_epi16(254);
const __m256i product = _mm256_mullo_epi16(a, b);
__m256i folded = _mm256_add_epi16(
_mm256_and_si256(product, mask), _mm256_srli_epi16(product, 8));
const __m256i reduce = _mm256_cmpgt_epi16(folded, cutoff);
folded = _mm256_sub_epi16(folded, _mm256_and_si256(reduce, mask));
return folded;
}
static void AVX2FF8WalshLocator(
const uint8_t* erasures,
const uint8_t* transformed_kernel,
uint8_t* locator_logs,
uint32_t n)
{
const __m256i zero = _mm256_setzero_si256();
const __m256i one = _mm256_set1_epi8(1);
for (uint32_t offset = 0; offset < n; offset += 32)
{
__m256i erased = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(erasures + offset));
erased = _mm256_andnot_si256(
_mm256_cmpeq_epi8(erased, zero), one);
_mm256_storeu_si256(
reinterpret_cast<__m256i*>(locator_logs + offset), erased);
}
AVX2WalshTransform(locator_logs, n);
for (uint32_t offset = 0; offset < n; offset += 32)
{
__m256i value_low, value_high, kernel_low, kernel_high;
AVX2ExpandBytes(_mm256_loadu_si256(
reinterpret_cast<const __m256i*>(locator_logs + offset)),
value_low, value_high);
AVX2ExpandBytes(_mm256_loadu_si256(
reinterpret_cast<const __m256i*>(transformed_kernel + offset)),
kernel_low, kernel_high);
value_low = AVX2MultiplyMod255(value_low, kernel_low);
value_high = AVX2MultiplyMod255(value_high, kernel_high);
_mm256_storeu_si256(
reinterpret_cast<__m256i*>(locator_logs + offset),
AVX2PackMod255(value_low, value_high));
}
AVX2WalshTransform(locator_logs, n);
}
#endif
#if defined(LEO_HAS_FF16) && !defined(LEO2_AVX512_VARIANT) && \
!defined(LEO2_GFNI_VARIANT)
// The GF16 locator uses the same XOR-convolution as GF8, but its coefficients
// are represented modulo 65535. Keep the unsigned comparisons explicit: the
// field-log range occupies the complete uint16_t domain, so signed compares
// are incorrect for values with bit 15 set.
static LEO_FORCE_INLINE __m256i AVX2UnsignedLess16(
__m256i a, __m256i b)
{
const __m256i sign = _mm256_set1_epi16(static_cast<short>(0x8000));
return _mm256_cmpgt_epi16(
_mm256_xor_si256(b, sign), _mm256_xor_si256(a, sign));
}
static LEO_FORCE_INLINE __m256i AVX2AddMod65535(
__m256i a, __m256i b)
{
const __m256i modulus = _mm256_set1_epi16(-1);
const __m256i one = _mm256_set1_epi16(1);
__m256i sum = _mm256_add_epi16(a, b);
const __m256i carry = AVX2UnsignedLess16(sum, a);
sum = _mm256_add_epi16(sum, _mm256_and_si256(carry, one));
const __m256i exact_modulus = _mm256_cmpeq_epi16(sum, modulus);
return _mm256_sub_epi16(
sum, _mm256_and_si256(exact_modulus, modulus));
}
static LEO_FORCE_INLINE __m256i AVX2SubMod65535(
__m256i a, __m256i b)
{
const __m256i modulus = _mm256_set1_epi16(-1);
const __m256i borrow = AVX2UnsignedLess16(a, b);
return _mm256_add_epi16(
_mm256_sub_epi16(a, b), _mm256_and_si256(borrow, modulus));
}
template<int Distance>
static LEO_FORCE_INLINE __m256i AVX2Walsh16Mod65535Stage(__m256i value)
{
__m256i swapped;
if (Distance == 1)
{
swapped = _mm256_shufflelo_epi16(value, 0xb1);
swapped = _mm256_shufflehi_epi16(swapped, 0xb1);
}
else if (Distance == 2)
{
swapped = _mm256_shufflelo_epi16(value, 0x4e);
swapped = _mm256_shufflehi_epi16(swapped, 0x4e);
}
else if (Distance == 4)
swapped = _mm256_shuffle_epi32(value, 0x4e);
else
swapped = _mm256_permute2x128_si256(value, value, 0x01);
__m256i sum = AVX2AddMod65535(value, swapped);
__m256i difference = AVX2SubMod65535(value, swapped);
if (Distance == 1)
{
difference = _mm256_shufflelo_epi16(difference, 0xb1);
difference = _mm256_shufflehi_epi16(difference, 0xb1);
return _mm256_blend_epi16(sum, difference, 0xaa);
}
if (Distance == 2)
{
difference = _mm256_shufflelo_epi16(difference, 0x4e);
difference = _mm256_shufflehi_epi16(difference, 0x4e);
return _mm256_blend_epi16(sum, difference, 0xcc);
}
if (Distance == 4)
{
difference = _mm256_shuffle_epi32(difference, 0x4e);
return _mm256_blend_epi16(sum, difference, 0xf0);
}
difference = _mm256_permute2x128_si256(difference, difference, 0x01);
return _mm256_blend_epi32(sum, difference, 0xf0);
}
static LEO_FORCE_INLINE __m256i AVX2Walsh16Mod65535(__m256i value)
{
value = AVX2Walsh16Mod65535Stage<1>(value);
value = AVX2Walsh16Mod65535Stage<2>(value);
value = AVX2Walsh16Mod65535Stage<4>(value);
return AVX2Walsh16Mod65535Stage<8>(value);
}
static LEO_FORCE_INLINE __m256i AVX2ReduceMod65535(__m256i product)
{
const __m256i mask = _mm256_set1_epi32(65535);
__m256i folded = _mm256_add_epi32(
_mm256_and_si256(product, mask), _mm256_srli_epi32(product, 16));
folded = _mm256_add_epi32(
_mm256_and_si256(folded, mask), _mm256_srli_epi32(folded, 16));
const __m256i exact_modulus = _mm256_cmpeq_epi32(folded, mask);
return _mm256_sub_epi32(
folded, _mm256_and_si256(exact_modulus, mask));
}
static LEO_FORCE_INLINE __m256i AVX2MultiplyMod65535(
__m256i a, __m256i b)
{
const __m256i a_low = _mm256_cvtepu16_epi32(
_mm256_castsi256_si128(a));
const __m256i a_high = _mm256_cvtepu16_epi32(
_mm256_extracti128_si256(a, 1));
const __m256i b_low = _mm256_cvtepu16_epi32(
_mm256_castsi256_si128(b));
const __m256i b_high = _mm256_cvtepu16_epi32(
_mm256_extracti128_si256(b, 1));
const __m256i product_low = AVX2ReduceMod65535(
_mm256_mullo_epi32(a_low, b_low));
const __m256i product_high = AVX2ReduceMod65535(
_mm256_mullo_epi32(a_high, b_high));
return _mm256_permute4x64_epi64(
_mm256_packus_epi32(product_low, product_high), 0xd8);
}
static void AVX2FF16WalshTransform(uint16_t* data, uint32_t n)
{
for (uint32_t offset = 0; offset < n; offset += 16)
{
__m256i value = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(data + offset));
_mm256_storeu_si256(reinterpret_cast<__m256i*>(data + offset),
AVX2Walsh16Mod65535(value));
}
for (uint32_t distance = 16; distance < n; distance <<= 1)
{
const uint32_t group_size = distance << 1;
for (uint32_t group = 0; group < n; group += group_size)
{
for (uint32_t offset = 0; offset < distance; offset += 16)
{
uint16_t* const a_pointer = data + group + offset;
uint16_t* const b_pointer = a_pointer + distance;
const __m256i a = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(a_pointer));
const __m256i b = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(b_pointer));
_mm256_storeu_si256(reinterpret_cast<__m256i*>(a_pointer),
AVX2AddMod65535(a, b));
_mm256_storeu_si256(reinterpret_cast<__m256i*>(b_pointer),
AVX2SubMod65535(a, b));
}
}
}
}
static void AVX2FF16WalshLocator(
const uint8_t* erasures,
const uint16_t* transformed_kernel,
uint16_t* locator_logs,
uint32_t n)
{
for (uint32_t offset = 0; offset < n; offset += 16)
{
const __m128i bytes = _mm_loadu_si128(
reinterpret_cast<const __m128i*>(erasures + offset));
const __m256i values = _mm256_cvtepu8_epi16(bytes);
const __m256i zero = _mm256_setzero_si256();
const __m256i one = _mm256_set1_epi16(1);
const __m256i active = _mm256_andnot_si256(
_mm256_cmpeq_epi16(values, zero), one);
_mm256_storeu_si256(reinterpret_cast<__m256i*>(locator_logs + offset),
active);
}
AVX2FF16WalshTransform(locator_logs, n);
for (uint32_t offset = 0; offset < n; offset += 16)
{
const __m256i values = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(locator_logs + offset));
const __m256i kernel = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(transformed_kernel + offset));
_mm256_storeu_si256(reinterpret_cast<__m256i*>(locator_logs + offset),
AVX2MultiplyMod65535(values, kernel));
}
AVX2FF16WalshTransform(locator_logs, n);
}
#endif
#if defined(LEO_HAS_FF8) || !defined(LEO2_GFNI_VARIANT)
static __m256i BroadcastTable(const uint8_t table[16])
{
return _mm256_broadcastsi128_si256(_mm_loadu_si128(
reinterpret_cast<const __m128i*>(table)));
}
#endif
#ifdef LEO_HAS_FF8
#if defined(LEO2_AVX512_VARIANT)
# define LEO2_AVX2_OPERATION_INLINE
#else
# define LEO2_AVX2_OPERATION_INLINE LEO_FORCE_INLINE
#endif
template<bool Add>
static LEO2_AVX2_OPERATION_INLINE void AVX2FF8Operation(
void* destination,
const void* source,
uint16_t multiplier_log,
uint64_t byte_count)
{
uint8_t* output = static_cast<uint8_t*>(destination);
const uint8_t* input = static_cast<const uint8_t*>(source);
const FF8NibbleTable& table = FF8Tables[multiplier_log];
const __m256i low_table = BroadcastTable(table.low);
#if !defined(LEO2_GFNI_VARIANT)
const __m256i high_table = BroadcastTable(table.high);
const __m256i nibble_mask = _mm256_set1_epi8(15);
#endif
#if defined(LEO2_GFNI_VARIANT)
// The affine form leaves only one arithmetic operation per vector, so the
// three loop-bookkeeping instructions would otherwise be a large share of
// the issue slots. Two independent vectors per iteration also give the
// three-cycle affine latency somewhere to hide. This matches Leopard1's
// 64-byte mul_mem/muladd_mem stride.
while (byte_count >= 64)
{
const __m256i data0 = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(input));
const __m256i data1 = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(input + 32));
__m256i product0 = _mm256_gf2p8affine_epi64_epi8(data0, low_table, 0);
__m256i product1 = _mm256_gf2p8affine_epi64_epi8(data1, low_table, 0);
if (Add)
{
product0 = _mm256_xor_si256(product0, _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(output)));
product1 = _mm256_xor_si256(product1, _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(output + 32)));
}
_mm256_storeu_si256(reinterpret_cast<__m256i*>(output), product0);
_mm256_storeu_si256(reinterpret_cast<__m256i*>(output + 32), product1);
input += 64;
output += 64;
byte_count -= 64;
}
#endif
while (byte_count >= 32)
{
const __m256i data = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(input));
#if defined(LEO2_GFNI_VARIANT)
__m256i product = _mm256_gf2p8affine_epi64_epi8(data, low_table, 0);
#else
const __m256i low = _mm256_shuffle_epi8(
low_table, _mm256_and_si256(data, nibble_mask));
const __m256i high = _mm256_shuffle_epi8(high_table,
_mm256_and_si256(_mm256_srli_epi64(data, 4), nibble_mask));
__m256i product = _mm256_xor_si256(low, high);
#endif
if (Add)
product = _mm256_xor_si256(product, _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(output)));
_mm256_storeu_si256(reinterpret_cast<__m256i*>(output), product);
input += 32;
output += 32;
byte_count -= 32;
}
while (byte_count-- != 0)
{
const uint8_t product = FF8Product(multiplier_log, *input++);
if (Add)
*output++ ^= product;
else
*output++ = product;
}
}
#undef LEO2_AVX2_OPERATION_INLINE
static void AVX2FF8Multiply(
void* destination, const void* source,
uint16_t multiplier_log, uint64_t byte_count)
{
AVX2FF8Operation<false>(destination, source, multiplier_log, byte_count);
}
#if defined(LEO2_AVX512_VARIANT)
# define LEO2_AVX2_MULADD_NOINLINE
#elif defined(_MSC_VER)
# define LEO2_AVX2_MULADD_NOINLINE __declspec(noinline)
#elif defined(__GNUC__) || defined(__clang__)
# define LEO2_AVX2_MULADD_NOINLINE __attribute__((noinline))
#else
# define LEO2_AVX2_MULADD_NOINLINE
#endif
static LEO2_AVX2_MULADD_NOINLINE void AVX2FF8MultiplyAdd(
void* destination, const void* source,
uint16_t multiplier_log, uint64_t byte_count)
{
AVX2FF8Operation<true>(destination, source, multiplier_log, byte_count);
}
#undef LEO2_AVX2_MULADD_NOINLINE
static __m256i AVX2FF8ProductVector(
__m256i data,
__m256i low_table,
__m256i high_table)
{
#if defined(LEO2_GFNI_VARIANT)
// low_table carries the broadcast affine matrix; high_table is unused.
(void)high_table;
return _mm256_gf2p8affine_epi64_epi8(data, low_table, 0);
#else
const __m256i nibble_mask = _mm256_set1_epi8(15);
const __m256i low = _mm256_shuffle_epi8(
low_table, _mm256_and_si256(data, nibble_mask));
const __m256i high = _mm256_shuffle_epi8(high_table,
_mm256_and_si256(_mm256_srli_epi64(data, 4), nibble_mask));
return _mm256_xor_si256(low, high);
#endif
}
static __m256i AVX2FF8ApplyWeight(__m256i data, uint16_t weight_log)
{
static const uint16_t kModulus = 255;
if (weight_log == 0 || weight_log == kModulus)
return data;
const FF8NibbleTable& table = FF8Tables[weight_log];
return AVX2FF8ProductVector(
data, BroadcastTable(table.low), BroadcastTable(table.high));
}
template<bool Inverse>
static void AVX2FF8Butterfly2(
void* x_pointer,
void* y_pointer,
uint16_t multiplier_log,
uint64_t byte_count)
{
uint8_t* x = static_cast<uint8_t*>(x_pointer);
uint8_t* y = static_cast<uint8_t*>(y_pointer);
const FF8NibbleTable& table = FF8Tables[multiplier_log];
const __m256i low_table = BroadcastTable(table.low);
const __m256i high_table = BroadcastTable(table.high);
#if !defined(LEO2_AVX512_VARIANT)
// Two independent vectors hide shuffle latency for small shards on the
// explicitly qualified AVX2 backend. Keep the measured 4 KiB cutoff: an
// unconditional unroll regressed T8 at 64 KiB, while the mature
// single-vector loop remains neutral above it. The separately qualified
// AVX-512VL variant retains its measured schedule.
if (byte_count <= 4096)
{
while (byte_count >= 64)
{
__m256i x_value0 = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(x));
__m256i y_value0 = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(y));
__m256i x_value1 = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(x + 32));
__m256i y_value1 = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(y + 32));
if (Inverse)
{
y_value0 = _mm256_xor_si256(y_value0, x_value0);
y_value1 = _mm256_xor_si256(y_value1, x_value1);
x_value0 = _mm256_xor_si256(x_value0,
AVX2FF8ProductVector(y_value0, low_table, high_table));
x_value1 = _mm256_xor_si256(x_value1,
AVX2FF8ProductVector(y_value1, low_table, high_table));
}
else
{
x_value0 = _mm256_xor_si256(x_value0,
AVX2FF8ProductVector(y_value0, low_table, high_table));
x_value1 = _mm256_xor_si256(x_value1,
AVX2FF8ProductVector(y_value1, low_table, high_table));
y_value0 = _mm256_xor_si256(y_value0, x_value0);
y_value1 = _mm256_xor_si256(y_value1, x_value1);
}
_mm256_storeu_si256(reinterpret_cast<__m256i*>(x), x_value0);
_mm256_storeu_si256(reinterpret_cast<__m256i*>(y), y_value0);
_mm256_storeu_si256(
reinterpret_cast<__m256i*>(x + 32), x_value1);
_mm256_storeu_si256(
reinterpret_cast<__m256i*>(y + 32), y_value1);
x += 64;
y += 64;
byte_count -= 64;
}
}
#endif
while (byte_count >= 32)
{
__m256i x_value = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(x));
__m256i y_value = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(y));
if (Inverse)
{
y_value = _mm256_xor_si256(y_value, x_value);
x_value = _mm256_xor_si256(x_value,
AVX2FF8ProductVector(y_value, low_table, high_table));
}
else
{
x_value = _mm256_xor_si256(x_value,
AVX2FF8ProductVector(y_value, low_table, high_table));
y_value = _mm256_xor_si256(y_value, x_value);
}
_mm256_storeu_si256(reinterpret_cast<__m256i*>(x), x_value);
_mm256_storeu_si256(reinterpret_cast<__m256i*>(y), y_value);
x += 32;
y += 32;
byte_count -= 32;
}
while (byte_count-- != 0)
{
if (Inverse)
{
*y ^= *x;
*x ^= FF8Product(multiplier_log, *y);
}
else
{
*x ^= FF8Product(multiplier_log, *y);
*y ^= *x;
}
++x;
++y;
}
}
static void AVX2FF8IFFTButterfly2(
void* x, void* y, uint16_t multiplier_log, uint64_t byte_count)
{
AVX2FF8Butterfly2<true>(x, y, multiplier_log, byte_count);
}
static void AVX2FF8FFTButterfly2(
void* x, void* y, uint16_t multiplier_log, uint64_t byte_count)
{
AVX2FF8Butterfly2<false>(x, y, multiplier_log, byte_count);
}
static void AVX2FF8FFTButterfly2Out(
const void* x_input_pointer,
const void* y_input_pointer,
void* x_output_pointer,
void* y_output_pointer,
uint16_t multiplier_log,
uint64_t byte_count)
{
static const uint16_t kZeroSkew = 255;
const uint8_t* x_input = static_cast<const uint8_t*>(x_input_pointer);
const uint8_t* y_input = static_cast<const uint8_t*>(y_input_pointer);
uint8_t* x_output = static_cast<uint8_t*>(x_output_pointer);
uint8_t* y_output = static_cast<uint8_t*>(y_output_pointer);
__m256i low_table = _mm256_setzero_si256();
__m256i high_table = _mm256_setzero_si256();
if (multiplier_log != kZeroSkew)
{
low_table = BroadcastTable(FF8Tables[multiplier_log].low);
high_table = BroadcastTable(FF8Tables[multiplier_log].high);
}
while (byte_count >= 32)
{
__m256i x_value = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(x_input));
__m256i y_value = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(y_input));
if (multiplier_log != kZeroSkew)
x_value = _mm256_xor_si256(x_value,
AVX2FF8ProductVector(y_value, low_table, high_table));
y_value = _mm256_xor_si256(y_value, x_value);
_mm256_storeu_si256(reinterpret_cast<__m256i*>(x_output), x_value);
_mm256_storeu_si256(reinterpret_cast<__m256i*>(y_output), y_value);
x_input += 32;
y_input += 32;
x_output += 32;
y_output += 32;
byte_count -= 32;
}
while (byte_count-- != 0)
{
uint8_t x_value = *x_input++;
uint8_t y_value = *y_input++;
if (multiplier_log != kZeroSkew)
x_value ^= FF8Product(multiplier_log, y_value);
y_value ^= x_value;
*x_output++ = x_value;
*y_output++ = y_value;
}
}
#if !defined(LEO2_AVX512_VARIANT)
static void AVX2FF8IFFTButterfly2Out(
const void* x_input_pointer,
const void* y_input_pointer,
void* x_output_pointer,
void* y_output_pointer,
uint16_t multiplier_log,
uint64_t byte_count)
{
static const uint16_t kZeroSkew = 255;
const uint8_t* x_input = static_cast<const uint8_t*>(x_input_pointer);
const uint8_t* y_input = static_cast<const uint8_t*>(y_input_pointer);
uint8_t* x_output = static_cast<uint8_t*>(x_output_pointer);
uint8_t* y_output = static_cast<uint8_t*>(y_output_pointer);
__m256i low_table = _mm256_setzero_si256();
__m256i high_table = _mm256_setzero_si256();
if (multiplier_log != kZeroSkew)
{
low_table = BroadcastTable(FF8Tables[multiplier_log].low);
high_table = BroadcastTable(FF8Tables[multiplier_log].high);
}
while (byte_count >= 32)
{
const __m256i x_original = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(x_input));
const __m256i y_value = _mm256_xor_si256(x_original,
_mm256_loadu_si256(reinterpret_cast<const __m256i*>(y_input)));
__m256i x_value = x_original;
if (multiplier_log != kZeroSkew)
x_value = _mm256_xor_si256(x_value,
AVX2FF8ProductVector(y_value, low_table, high_table));
_mm256_storeu_si256(reinterpret_cast<__m256i*>(x_output), x_value);
_mm256_storeu_si256(reinterpret_cast<__m256i*>(y_output), y_value);
x_input += 32;
y_input += 32;
x_output += 32;
y_output += 32;
byte_count -= 32;
}
while (byte_count-- != 0)
{
const uint8_t y_value = static_cast<uint8_t>(*y_input ^ *x_input);
uint8_t x_value = *x_input;
if (multiplier_log != kZeroSkew)
x_value ^= FF8Product(multiplier_log, y_value);
*x_output++ = x_value;
*y_output++ = y_value;
++x_input;
++y_input;
}
}
#endif
static void AVX2FF8IFFTButterfly2Xor(
const void* x_input_pointer,
const void* y_input_pointer,
void* x_output_pointer,
void* y_output_pointer,
uint16_t multiplier_log,
uint64_t byte_count)
{
const uint8_t* x_input = static_cast<const uint8_t*>(x_input_pointer);
const uint8_t* y_input = static_cast<const uint8_t*>(y_input_pointer);
uint8_t* x_output = static_cast<uint8_t*>(x_output_pointer);
uint8_t* y_output = static_cast<uint8_t*>(y_output_pointer);
const FF8NibbleTable& table = FF8Tables[multiplier_log];
const __m256i low_table = BroadcastTable(table.low);
const __m256i high_table = BroadcastTable(table.high);
while (byte_count >= 32)
{
const __m256i x_original = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(x_input));
const __m256i y_value = _mm256_xor_si256(x_original,
_mm256_loadu_si256(reinterpret_cast<const __m256i*>(y_input)));
const __m256i x_value = _mm256_xor_si256(x_original,
AVX2FF8ProductVector(y_value, low_table, high_table));
const __m256i x_result = _mm256_xor_si256(x_value,
_mm256_loadu_si256(reinterpret_cast<const __m256i*>(x_output)));
const __m256i y_result = _mm256_xor_si256(y_value,
_mm256_loadu_si256(reinterpret_cast<const __m256i*>(y_output)));
_mm256_storeu_si256(reinterpret_cast<__m256i*>(x_output), x_result);
_mm256_storeu_si256(reinterpret_cast<__m256i*>(y_output), y_result);
x_input += 32;
y_input += 32;
x_output += 32;
y_output += 32;
byte_count -= 32;
}
while (byte_count-- != 0)
{
const uint8_t y_value = static_cast<uint8_t>(*y_input ^ *x_input);
const uint8_t x_value = static_cast<uint8_t>(
*x_input ^ FF8Product(multiplier_log, y_value));
*x_output ^= x_value;
*y_output ^= y_value;
++x_input;
++y_input;
++x_output;
++y_output;
}
}
#endif // LEO_HAS_FF8
#ifdef LEO_HAS_FF16
static uint16_t FF16Product(uint16_t log, uint16_t value)
{
const FF16Table& table = FF16Tables[log];
#if defined(LEO2_GFNI_VARIANT)
const uint8_t input_low = static_cast<uint8_t>(value);
const uint8_t input_high = static_cast<uint8_t>(value >> 8);
const uint8_t product_low = static_cast<uint8_t>(
GFNIApplyMatrix64(table.block[0], input_low) ^
GFNIApplyMatrix64(table.block[1], input_high));
const uint8_t product_high = static_cast<uint8_t>(
GFNIApplyMatrix64(table.block[2], input_low) ^
GFNIApplyMatrix64(table.block[3], input_high));
return static_cast<uint16_t>(
product_low | (static_cast<unsigned>(product_high) << 8));
#else
const unsigned n0 = value & 15U;
const unsigned n1 = (value >> 4) & 15U;
const unsigned n2 = (value >> 8) & 15U;
const unsigned n3 = value >> 12;
const uint8_t low = static_cast<uint8_t>(
table.low[0][n0] ^ table.low[1][n1] ^
table.low[2][n2] ^ table.low[3][n3]);
const uint8_t high = static_cast<uint8_t>(
table.high[0][n0] ^ table.high[1][n1] ^
table.high[2][n2] ^ table.high[3][n3]);
return static_cast<uint16_t>(low | (static_cast<unsigned>(high) << 8));
#endif
}
static void AVX2FF16ProductVectors(
__m256i low_data,
__m256i high_data,
const __m256i low_tables[4],
const __m256i high_tables[4],
__m256i& product_low,
__m256i& product_high)
{
#if defined(LEO2_GFNI_VARIANT)
// low_tables carries the four broadcast affine blocks of the 16-by-16 bit
// multiplication matrix, ordered (low<-low, low<-high, high<-low,
// high<-high). high_tables is unused.
(void)high_tables;
product_low = _mm256_xor_si256(
_mm256_gf2p8affine_epi64_epi8(low_data, low_tables[0], 0),
_mm256_gf2p8affine_epi64_epi8(high_data, low_tables[1], 0));
product_high = _mm256_xor_si256(