-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathLeopard2Backend.cpp
More file actions
4116 lines (3941 loc) · 160 KB
/
Copy pathLeopard2Backend.cpp
File metadata and controls
4116 lines (3941 loc) · 160 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 <atomic>
#include <cstring>
#include <mutex>
#include <vector>
namespace leopard { namespace backend {
static bool RangesOverlap(
const void* first, const void* second, uint64_t byte_count)
{
if (byte_count == 0)
return false;
if (!first || !second)
return false;
const uintptr_t a = reinterpret_cast<uintptr_t>(first);
const uintptr_t b = reinterpret_cast<uintptr_t>(second);
return a <= b ? b - a < byte_count : a - b < byte_count;
}
bool IsWeightedIFFTButterfly4AliasingValid(
const void* const inputs[4],
const void* const outputs[4],
uint8_t live_mask,
uint64_t byte_count)
{
if (byte_count == 0)
return true;
for (unsigned output = 0; output < 4; ++output)
{
if (!outputs[output])
return false;
for (unsigned other = output + 1; other < 4; ++other)
if (RangesOverlap(outputs[output], outputs[other], byte_count))
return false;
}
bool wholly_disjoint = true;
bool exact_in_place = true;
for (unsigned input = 0; input < 4; ++input)
{
if ((live_mask & (1U << input)) == 0)
continue;
if (!inputs[input])
return false;
if (inputs[input] != outputs[input])
exact_in_place = false;
for (unsigned output = 0; output < 4; ++output)
if (RangesOverlap(inputs[input], outputs[output], byte_count))
wholly_disjoint = false;
}
return wholly_disjoint || exact_in_place;
}
static const Ops* SelectedOps = NULL;
static const Ops* QualifiedOps[LEO2_BACKEND_GFNI + 1] = {};
enum QualificationState
{
QualificationUnattempted,
QualificationPassed,
QualificationFailed
};
static QualificationState QualificationStates[LEO2_BACKEND_GFNI + 1] = {};
static QualificationStatus QualificationFailures[LEO2_BACKEND_GFNI + 1] = {};
static InitializeArgs SavedInitializeArgs = { NULL, NULL, NULL };
static uint32_t QualifiableBackendMask = 0;
static bool SelfTestPassed = false;
static QualificationStatus StartupFailure = QualificationAvailable;
static FF8K65R65B64PackedKernel QualifiedAVX512GFNIT128 = NULL;
static FF8K65R65Multiples64PackedKernel
QualifiedAVX512GFNIT128Multiples64 = NULL;
static FF8K16R16B64PackedKernel QualifiedAVX512GFNIT16 = NULL;
#ifdef LEO2_ENABLE_TEST_HOOKS
static std::atomic<unsigned> TestFault(TestSetupFaultNone);
static std::atomic<unsigned> TestFaultConsumptions(0);
static TestSetupFault AllocationFaultFor(
leo2_backend backend,
bool ff16)
{
switch (backend)
{
case LEO2_BACKEND_SCALAR:
return ff16 ? TestSetupFaultScalarFF16Allocation
: TestSetupFaultScalarFF8Allocation;
case LEO2_BACKEND_SSSE3:
return ff16 ? TestSetupFaultSSSE3FF16Allocation
: TestSetupFaultSSSE3FF8Allocation;
case LEO2_BACKEND_AVX2:
return ff16 ? TestSetupFaultAVX2FF16Allocation
: TestSetupFaultAVX2FF8Allocation;
case LEO2_BACKEND_AVX512:
return ff16 ? TestSetupFaultAVX512FF16Allocation
: TestSetupFaultAVX512FF8Allocation;
case LEO2_BACKEND_GFNI:
return ff16 ? TestSetupFaultGFNIFF16Allocation
: TestSetupFaultGFNIFF8Allocation;
default:
return TestSetupFaultNone;
}
}
static TestSetupFault KATFaultFor(leo2_backend backend)
{
switch (backend)
{
case LEO2_BACKEND_SCALAR: return TestSetupFaultScalarKAT;
case LEO2_BACKEND_SSSE3: return TestSetupFaultSSSE3KAT;
case LEO2_BACKEND_AVX2: return TestSetupFaultAVX2KAT;
case LEO2_BACKEND_AVX512: return TestSetupFaultAVX512KAT;
case LEO2_BACKEND_GFNI: return TestSetupFaultGFNIKAT;
default: return TestSetupFaultNone;
}
}
static bool ConsumeTestFault(TestSetupFault expected)
{
if (expected == TestSetupFaultNone)
return false;
unsigned value = static_cast<unsigned>(expected);
if (!TestFault.compare_exchange_strong(value,
static_cast<unsigned>(TestSetupFaultNone),
std::memory_order_acq_rel, std::memory_order_acquire))
return false;
TestFaultConsumptions.fetch_add(1, std::memory_order_relaxed);
return true;
}
#endif
#ifdef LEO_HAS_FF8
static bool TestFF8(const Ops& ops, FF8MultiplyLog reference)
{
static const size_t kBytes = 521;
uint8_t source[kBytes + 2];
uint8_t output[kBytes + 2];
uint8_t expected[kBytes + 2];
for (size_t i = 0; i < kBytes + 2; ++i)
source[i] = static_cast<uint8_t>((i * 73U + 19U) & 255U);
for (unsigned log = 0; log < 256; ++log)
{
std::memset(output, 0xa5, sizeof(output));
std::memset(expected, 0xa5, sizeof(expected));
for (size_t i = 0; i < kBytes; ++i)
expected[i + 1] = reference(
source[i + 1], static_cast<uint8_t>(log));
ops.ff8_multiply(
output + 1, source + 1, static_cast<uint16_t>(log), kBytes);
if (std::memcmp(output, expected, sizeof(output)) != 0)
return false;
for (size_t i = 0; i < sizeof(output); ++i)
output[i] = expected[i] =
static_cast<uint8_t>((i * 29U + log) & 255U);
for (size_t i = 0; i < kBytes; ++i)
expected[i + 1] ^= reference(
source[i + 1], static_cast<uint8_t>(log));
ops.ff8_multiply_add(
output + 1, source + 1, static_cast<uint16_t>(log), kBytes);
if (std::memcmp(output, expected, sizeof(output)) != 0)
return false;
}
return true;
}
static bool TestFF8MultiplyAddOutputs(
const Ops& ops,
FF8MultiplyLog reference)
{
if (!ops.ff8_multiply_add_outputs)
return true;
static const uint64_t byte_counts[] = {
0, 1, 7, 31, 32, 33, 63, 64, 65, 257
};
static const uint16_t log_sets[][8] = {
{ 0, 1, 17, 29, 63, 127, 254, 193 },
{ 0, 1, 17, UINT16_MAX, 63, 127, 254, UINT16_MAX },
{ UINT16_MAX, 1, UINT16_MAX, 29,
UINT16_MAX, 127, UINT16_MAX, 193 },
{ 0, UINT16_MAX, 17, 29, 63, UINT16_MAX, 254, 193 }
};
uint8_t source[260];
uint8_t original_source[260];
uint8_t outputs[8][260];
uint8_t expected[8][260];
void* output_pointers[8];
for (size_t count_i = 0;
count_i < sizeof(byte_counts) / sizeof(byte_counts[0]); ++count_i)
{
for (size_t i = 0; i < sizeof(source); ++i)
source[i] = original_source[i] = static_cast<uint8_t>(
i * 73U + count_i * 19U + 11U);
for (size_t log_set = 0;
log_set < sizeof(log_sets) / sizeof(log_sets[0]); ++log_set)
{
const uint16_t* logs = log_sets[log_set];
const uint64_t bytes = byte_counts[count_i];
for (unsigned output_count = 2;
output_count <= 8; ++output_count)
{
for (unsigned output = 0; output < 8; ++output)
{
output_pointers[output] = outputs[output] + 1;
for (size_t i = 0; i < sizeof(outputs[output]); ++i)
{
outputs[output][i] = expected[output][i] =
static_cast<uint8_t>(
i * (29U + output * 6U) + count_i +
log_set * 7U + output * 13U + output_count);
}
}
for (unsigned output = 0; output < output_count; ++output)
{
if (logs[output] == UINT16_MAX)
continue;
for (uint64_t i = 0; i < bytes; ++i)
{
expected[output][i + 1] ^= reference(source[i + 1],
static_cast<uint8_t>(logs[output]));
}
}
ops.ff8_multiply_add_outputs(
output_pointers, source + 1, logs, output_count, bytes);
if (std::memcmp(outputs, expected, sizeof(outputs)) != 0 ||
std::memcmp(source, original_source, sizeof(source)) != 0)
return false;
}
}
}
return true;
}
static bool TestFF8MultiplyAdd2Sources2Outputs(
const Ops& ops,
FF8MultiplyLog reference)
{
if (!ops.ff8_multiply_add_2_sources_2_outputs)
return true;
static const uint64_t byte_counts[] = {
0, 1, 7, 31, 32, 33, 63, 64, 65, 257
};
static const uint16_t logs0[2] = { 0, 193 };
static const uint16_t logs1[2] = { 1, 254 };
uint8_t source0[260];
uint8_t source1[260];
uint8_t original0[260];
uint8_t original1[260];
uint8_t outputs[2][260];
uint8_t expected[2][260];
void* output_pointers[2] = { outputs[0] + 1, outputs[1] + 1 };
for (size_t count_i = 0;
count_i < sizeof(byte_counts) / sizeof(byte_counts[0]); ++count_i)
{
for (size_t i = 0; i < sizeof(source0); ++i)
{
source0[i] = original0[i] = static_cast<uint8_t>(
i * 73U + count_i * 19U + 11U);
source1[i] = original1[i] = static_cast<uint8_t>(
i * 37U + count_i * 23U + 7U);
}
for (unsigned alias_sources = 0; alias_sources < 2; ++alias_sources)
{
const uint8_t* second_source =
alias_sources ? source0 : source1;
for (unsigned output = 0; output < 2; ++output)
{
for (size_t i = 0; i < sizeof(outputs[output]); ++i)
{
outputs[output][i] = expected[output][i] =
static_cast<uint8_t>(i * (29U + output * 6U) +
count_i + alias_sources * 7U + output * 13U);
}
}
const uint64_t bytes = byte_counts[count_i];
for (unsigned output = 0; output < 2; ++output)
{
for (uint64_t i = 0; i < bytes; ++i)
{
expected[output][i + 1] ^=
reference(source0[i + 1],
static_cast<uint8_t>(logs0[output])) ^
reference(second_source[i + 1],
static_cast<uint8_t>(logs1[output]));
}
}
ops.ff8_multiply_add_2_sources_2_outputs(
output_pointers, source0 + 1, second_source + 1,
logs0, logs1, bytes);
if (std::memcmp(outputs, expected, sizeof(outputs)) != 0 ||
std::memcmp(source0, original0, sizeof(source0)) != 0 ||
std::memcmp(source1, original1, sizeof(source1)) != 0)
return false;
}
}
return true;
}
#if LEO2_EXPERIMENT_GENERAL_ONE_LOSS_DIRECT
static bool TestFF8LinearCombination2(
FF8LinearCombination2 callback,
FF8MultiplyLog reference)
{
if (!callback || !reference)
return false;
static const uint64_t byte_counts[] = {
0, 1, 2, 3, 7, 8, 15, 16, 17,
31, 32, 33, 63, 64, 65, 257
};
static const uint16_t logs[] = {
UINT16_MAX, 0, 1, 17, 193, 254, 255
};
uint8_t source0[260];
uint8_t source1[260];
uint8_t original0[260];
uint8_t original1[260];
uint8_t output[260];
uint8_t expected[260];
for (size_t count_i = 0;
count_i < sizeof(byte_counts) / sizeof(byte_counts[0]); ++count_i)
{
for (size_t i = 0; i < sizeof(source0); ++i)
{
source0[i] = original0[i] = static_cast<uint8_t>(
i * 73U + count_i * 19U + 11U);
source1[i] = original1[i] = static_cast<uint8_t>(
i * 37U + count_i * 23U + 7U);
}
for (unsigned alias_sources = 0; alias_sources < 2; ++alias_sources)
{
const uint8_t* second_source =
alias_sources ? source0 : source1;
for (size_t log0_i = 0;
log0_i < sizeof(logs) / sizeof(logs[0]); ++log0_i)
{
for (size_t log1_i = 0;
log1_i < sizeof(logs) / sizeof(logs[0]); ++log1_i)
{
const uint16_t log0 = logs[log0_i];
const uint16_t log1 = logs[log1_i];
const uint64_t bytes = byte_counts[count_i];
for (unsigned add = 0; add < 2; ++add)
{
for (size_t i = 0; i < sizeof(output); ++i)
{
output[i] = expected[i] = static_cast<uint8_t>(
i * 29U + count_i * 13U + log0_i * 7U +
log1_i * 3U + alias_sources + add);
}
for (uint64_t i = 0; i < bytes; ++i)
{
uint8_t value = 0;
if (log0 != UINT16_MAX)
{
value ^= reference(source0[i + 1],
static_cast<uint8_t>(log0));
}
if (log1 != UINT16_MAX)
{
value ^= reference(second_source[i + 1],
static_cast<uint8_t>(log1));
}
if (add)
expected[i + 1] ^= value;
else
expected[i + 1] = value;
}
callback(output + 1,
log0 == UINT16_MAX ? NULL : source0 + 1,
log1 == UINT16_MAX ? NULL : second_source + 1,
log0, log1, add != 0, bytes);
if (std::memcmp(output, expected, sizeof(output)) != 0 ||
std::memcmp(source0, original0,
sizeof(source0)) != 0 ||
std::memcmp(source1, original1,
sizeof(source1)) != 0)
return false;
}
}
}
}
}
// Exhaust every pair of live Leopard GF8 logarithms independently of the
// vector-tail/identity cases above.
for (unsigned log0 = 0; log0 < 255; ++log0)
{
for (unsigned log1 = 0; log1 < 255; ++log1)
{
source0[1] = static_cast<uint8_t>(
log0 * 17U + log1 * 3U + 1U);
source1[1] = static_cast<uint8_t>(
log0 * 5U + log1 * 29U + 7U);
const uint8_t combined = static_cast<uint8_t>(
reference(source0[1], static_cast<uint8_t>(log0)) ^
reference(source1[1], static_cast<uint8_t>(log1)));
output[1] = 0xa5;
callback(output + 1, source0 + 1, source1 + 1,
static_cast<uint16_t>(log0),
static_cast<uint16_t>(log1), false, 1);
if (output[1] != combined)
return false;
output[1] = 0xa5;
callback(output + 1, source0 + 1, source1 + 1,
static_cast<uint16_t>(log0),
static_cast<uint16_t>(log1), true, 1);
if (output[1] != static_cast<uint8_t>(0xa5 ^ combined))
return false;
}
}
callback(NULL, NULL, NULL, UINT16_MAX, UINT16_MAX, false, 0);
return true;
}
static bool TestFF8LinearCombination4Tiny(
FF8LinearCombination4Tiny callback,
FF8MultiplyLog reference)
{
if (!callback || !reference)
return false;
static const uint16_t selected_logs[] = { 0, 1, 17, 193, 254, 255 };
uint8_t source_storage[4][68];
uint8_t original_storage[4][68];
uint8_t output[68];
uint8_t expected[68];
const void* sources[4];
uint16_t logs[4];
for (uint64_t bytes = 0; bytes <= 63; ++bytes)
{
for (unsigned alias_mode = 0; alias_mode < 3; ++alias_mode)
{
for (unsigned source = 0; source < 4; ++source)
{
for (size_t i = 0; i < sizeof(source_storage[source]); ++i)
{
source_storage[source][i] =
original_storage[source][i] =
static_cast<uint8_t>(i * (23U + source * 10U) +
bytes * 7U + source * 31U);
}
const unsigned aliased_source = alias_mode == 1 ? 0 :
alias_mode == 2 ? (source & 1U) * 2U : source;
sources[source] = source_storage[aliased_source] + 1;
logs[source] = selected_logs[
(bytes + source + alias_mode) %
(sizeof(selected_logs) / sizeof(selected_logs[0]))];
}
for (unsigned add = 0; add < 2; ++add)
{
for (size_t i = 0; i < sizeof(output); ++i)
{
output[i] = expected[i] = static_cast<uint8_t>(
i * 43U + bytes * 5U + alias_mode * 11U + add);
}
for (uint64_t i = 0; i < bytes; ++i)
{
uint8_t value = 0;
for (unsigned source = 0; source < 4; ++source)
{
value ^= reference(
static_cast<const uint8_t*>(
sources[source])[i],
static_cast<uint8_t>(logs[source]));
}
if (add)
expected[i + 1] ^= value;
else
expected[i + 1] = value;
}
callback(output + 1, sources, logs, add != 0, bytes);
if (std::memcmp(output, expected, sizeof(output)) != 0)
return false;
for (unsigned source = 0; source < 4; ++source)
{
if (std::memcmp(source_storage[source],
original_storage[source],
sizeof(source_storage[source])) != 0)
return false;
}
}
}
}
/*
Exhaust every multiplier and input byte in every lane. The other
lanes are zero, which binds all 65,536 GF8 products without an
intractable four-dimensional Cartesian test.
*/
for (unsigned active_source = 0; active_source < 4; ++active_source)
{
for (unsigned log = 0; log < 256; ++log)
{
for (unsigned first_value = 0; first_value < 256;
first_value += 63)
{
const unsigned count = std::min(63U, 256U - first_value);
for (unsigned source = 0; source < 4; ++source)
{
sources[source] = source_storage[source] + 1;
logs[source] = source == active_source
? static_cast<uint16_t>(log) : 0;
for (unsigned i = 0; i < count; ++i)
{
source_storage[source][i + 1] =
source == active_source
? static_cast<uint8_t>(first_value + i) : 0;
}
}
std::memset(output, 0xa5, sizeof(output));
callback(output + 1, sources, logs, false, count);
for (unsigned i = 0; i < count; ++i)
{
if (output[i + 1] != reference(
static_cast<uint8_t>(first_value + i),
static_cast<uint8_t>(log)))
return false;
}
std::memset(output, 0xa5, sizeof(output));
callback(output + 1, sources, logs, true, count);
for (unsigned i = 0; i < count; ++i)
{
if (output[i + 1] != static_cast<uint8_t>(0xa5 ^
reference(static_cast<uint8_t>(first_value + i),
static_cast<uint8_t>(log))))
return false;
}
}
}
}
callback(NULL, NULL, NULL, false, 0);
return true;
}
#endif
template<bool Inverse>
static void ReferenceFF8Butterfly2(
uint8_t* x,
uint8_t* y,
uint8_t log,
uint64_t byte_count,
FF8MultiplyLog reference)
{
for (uint64_t i = 0; i < byte_count; ++i)
{
if (Inverse)
{
y[i] ^= x[i];
x[i] ^= reference(y[i], log);
}
else
{
x[i] ^= reference(y[i], log);
y[i] ^= x[i];
}
}
}
static bool TestFF8Butterflies(const Ops& ops, FF8MultiplyLog reference)
{
static const uint64_t byte_counts[] = {
0, 1, 3, 7, 15, 16, 17, 31, 32, 33,
63, 64, 65, 127, 128, 129, 257, 521
};
uint8_t x[524];
uint8_t y[524];
uint8_t expected_x[524];
uint8_t expected_y[524];
uint8_t output_x[524];
uint8_t output_y[524];
uint8_t expected_output_x[524];
uint8_t expected_output_y[524];
for (unsigned log = 0; log < 256; ++log)
{
for (size_t count_i = 0;
count_i < sizeof(byte_counts) / sizeof(byte_counts[0]);
++count_i)
{
const uint64_t bytes = byte_counts[count_i];
for (size_t i = 0; i < sizeof(x); ++i)
{
x[i] = expected_x[i] = static_cast<uint8_t>(
i * 73U + log * 11U + count_i);
y[i] = expected_y[i] = static_cast<uint8_t>(
i * 29U + log * 17U + count_i * 3U);
}
ReferenceFF8Butterfly2<true>(expected_x + 1, expected_y + 1,
static_cast<uint8_t>(log), bytes, reference);
ops.ff8_ifft_butterfly2(
x + 1, y + 1, static_cast<uint16_t>(log), bytes);
if (std::memcmp(x, expected_x, sizeof(x)) != 0 ||
std::memcmp(y, expected_y, sizeof(y)) != 0)
return false;
for (size_t i = 0; i < sizeof(x); ++i)
{
x[i] = expected_x[i] = static_cast<uint8_t>(
i * 61U + log * 7U + count_i);
y[i] = expected_y[i] = static_cast<uint8_t>(
i * 43U + log * 13U + count_i * 5U);
}
ReferenceFF8Butterfly2<false>(expected_x + 1, expected_y + 1,
static_cast<uint8_t>(log), bytes, reference);
ops.ff8_fft_butterfly2(
x + 1, y + 1, static_cast<uint16_t>(log), bytes);
if (std::memcmp(x, expected_x, sizeof(x)) != 0 ||
std::memcmp(y, expected_y, sizeof(y)) != 0)
return false;
for (size_t i = 0; i < sizeof(x); ++i)
{
x[i] = static_cast<uint8_t>(
i * 37U + log * 19U + count_i);
y[i] = static_cast<uint8_t>(
i * 101U + log * 5U + count_i * 7U);
output_x[i] = expected_output_x[i] = static_cast<uint8_t>(
i * 23U + log + count_i * 11U);
output_y[i] = expected_output_y[i] = static_cast<uint8_t>(
i * 47U + log * 3U + count_i * 13U);
expected_x[i] = x[i];
expected_y[i] = y[i];
}
ReferenceFF8Butterfly2<true>(expected_x + 1, expected_y + 1,
static_cast<uint8_t>(log), bytes, reference);
for (uint64_t i = 0; i < bytes; ++i)
{
expected_output_x[i + 1] ^= expected_x[i + 1];
expected_output_y[i + 1] ^= expected_y[i + 1];
}
ops.ff8_ifft_butterfly2_xor(
x + 1, y + 1, output_x + 1, output_y + 1,
static_cast<uint16_t>(log), bytes);
if (std::memcmp(output_x, expected_output_x,
sizeof(output_x)) != 0 ||
std::memcmp(output_y, expected_output_y,
sizeof(output_y)) != 0)
return false;
// Accumulating butterflies must not consume or modify their input.
for (size_t i = 0; i < sizeof(x); ++i)
{
const uint8_t original_x = static_cast<uint8_t>(
i * 37U + log * 19U + count_i);
const uint8_t original_y = static_cast<uint8_t>(
i * 101U + log * 5U + count_i * 7U);
if (x[i] != original_x || y[i] != original_y)
return false;
}
for (size_t i = 0; i < sizeof(x); ++i)
{
x[i] = expected_x[i] = static_cast<uint8_t>(
i * 53U + log * 23U + count_i * 17U);
y[i] = expected_y[i] = static_cast<uint8_t>(
i * 79U + log * 31U + count_i * 19U);
output_x[i] = expected_output_x[i] = 0xa5;
output_y[i] = expected_output_y[i] = 0x5a;
}
std::memcpy(expected_output_x + 1, x + 1, bytes);
std::memcpy(expected_output_y + 1, y + 1, bytes);
if (log == 255)
{
for (uint64_t i = 0; i < bytes; ++i)
expected_output_y[i + 1] ^= expected_output_x[i + 1];
}
else
{
ReferenceFF8Butterfly2<false>(
expected_output_x + 1, expected_output_y + 1,
static_cast<uint8_t>(log), bytes, reference);
}
ops.ff8_fft_butterfly2_out(
x + 1, y + 1, output_x + 1, output_y + 1,
static_cast<uint16_t>(log), bytes);
if (std::memcmp(output_x, expected_output_x,
sizeof(output_x)) != 0 ||
std::memcmp(output_y, expected_output_y,
sizeof(output_y)) != 0 ||
std::memcmp(x, expected_x, sizeof(x)) != 0 ||
std::memcmp(y, expected_y, sizeof(y)) != 0)
return false;
}
}
return true;
}
template<bool Inverse>
static void ReferenceFF8Butterfly4(
uint8_t* value0,
uint8_t* value1,
uint8_t* value2,
uint8_t* value3,
uint16_t log01,
uint16_t log23,
uint16_t log02,
uint64_t byte_count,
FF8MultiplyLog reference)
{
static const uint16_t kZeroSkew = 255;
if (Inverse)
{
if (log01 == kZeroSkew)
for (uint64_t i = 0; i < byte_count; ++i)
value1[i] ^= value0[i];
else
ReferenceFF8Butterfly2<true>(value0, value1,
static_cast<uint8_t>(log01), byte_count, reference);
if (log23 == kZeroSkew)
for (uint64_t i = 0; i < byte_count; ++i)
value3[i] ^= value2[i];
else
ReferenceFF8Butterfly2<true>(value2, value3,
static_cast<uint8_t>(log23), byte_count, reference);
if (log02 == kZeroSkew)
for (uint64_t i = 0; i < byte_count; ++i)
{
value2[i] ^= value0[i];
value3[i] ^= value1[i];
}
else
{
ReferenceFF8Butterfly2<true>(value0, value2,
static_cast<uint8_t>(log02), byte_count, reference);
ReferenceFF8Butterfly2<true>(value1, value3,
static_cast<uint8_t>(log02), byte_count, reference);
}
}
else
{
if (log02 == kZeroSkew)
for (uint64_t i = 0; i < byte_count; ++i)
{
value2[i] ^= value0[i];
value3[i] ^= value1[i];
}
else
{
ReferenceFF8Butterfly2<false>(value0, value2,
static_cast<uint8_t>(log02), byte_count, reference);
ReferenceFF8Butterfly2<false>(value1, value3,
static_cast<uint8_t>(log02), byte_count, reference);
}
if (log01 == kZeroSkew)
for (uint64_t i = 0; i < byte_count; ++i)
value1[i] ^= value0[i];
else
ReferenceFF8Butterfly2<false>(value0, value1,
static_cast<uint8_t>(log01), byte_count, reference);
if (log23 == kZeroSkew)
for (uint64_t i = 0; i < byte_count; ++i)
value3[i] ^= value2[i];
else
ReferenceFF8Butterfly2<false>(value2, value3,
static_cast<uint8_t>(log23), byte_count, reference);
}
}
static bool TestFF8Butterflies4(const Ops& ops, FF8MultiplyLog reference)
{
static const uint16_t log_sets[][3] = {
{ 255, 255, 255 },
{ 0, 0, 0 },
{ 1, 2, 3 },
{ 254, 253, 252 },
{ 255, 0, 1 },
{ 2, 255, 3 },
{ 4, 5, 255 },
{ 255, 255, 7 },
{ 255, 11, 255 }
};
static const uint64_t byte_counts[] = {
0, 1, 3, 7, 15, 16, 17, 31, 32, 33,
63, 64, 65, 127, 128, 129, 257, 521,
1023, 1024, 1025
};
uint8_t values[4][1028];
uint8_t expected[4][1028];
uint8_t inputs[4][1028];
uint8_t original_inputs[4][1028];
uint8_t outputs[4][1028];
for (size_t set_i = 0;
set_i < sizeof(log_sets) / sizeof(log_sets[0]); ++set_i)
for (size_t count_i = 0;
count_i < sizeof(byte_counts) / sizeof(byte_counts[0]);
++count_i)
{
const uint64_t bytes = byte_counts[count_i];
for (unsigned lane = 0; lane < 4; ++lane)
for (size_t i = 0; i < sizeof(values[lane]); ++i)
values[lane][i] = expected[lane][i] =
static_cast<uint8_t>(i * (29U + lane * 12U) +
set_i * 31U + count_i * 7U + lane);
ReferenceFF8Butterfly4<true>(
expected[0] + 1, expected[1] + 1,
expected[2] + 1, expected[3] + 1,
log_sets[set_i][0], log_sets[set_i][1],
log_sets[set_i][2], bytes, reference);
ops.ff8_ifft_butterfly4(
values[0] + 1, values[1] + 1,
values[2] + 1, values[3] + 1,
log_sets[set_i][0], log_sets[set_i][1],
log_sets[set_i][2], bytes);
if (std::memcmp(values, expected, sizeof(values)) != 0)
return false;
for (unsigned lane = 0; lane < 4; ++lane)
for (size_t i = 0; i < sizeof(values[lane]); ++i)
values[lane][i] = expected[lane][i] =
static_cast<uint8_t>(i * (43U + lane * 10U) +
set_i * 17U + count_i * 11U + lane * 3U);
ReferenceFF8Butterfly4<false>(
expected[0] + 1, expected[1] + 1,
expected[2] + 1, expected[3] + 1,
log_sets[set_i][0], log_sets[set_i][1],
log_sets[set_i][2], bytes, reference);
ops.ff8_fft_butterfly4(
values[0] + 1, values[1] + 1,
values[2] + 1, values[3] + 1,
log_sets[set_i][0], log_sets[set_i][1],
log_sets[set_i][2], bytes);
if (std::memcmp(values, expected, sizeof(values)) != 0)
return false;
for (unsigned lane = 0; lane < 4; ++lane)
for (size_t i = 0; i < sizeof(inputs[lane]); ++i)
{
inputs[lane][i] = original_inputs[lane][i] =
static_cast<uint8_t>(i * (59U + lane * 14U) +
set_i * 37U + count_i * 23U + lane * 5U);
outputs[lane][i] = expected[lane][i] =
static_cast<uint8_t>(0x91U + lane * 13U);
}
for (unsigned lane = 0; lane < 4; ++lane)
std::memcpy(expected[lane] + 1, inputs[lane] + 1, bytes);
ReferenceFF8Butterfly4<false>(
expected[0] + 1, expected[1] + 1,
expected[2] + 1, expected[3] + 1,
log_sets[set_i][0], log_sets[set_i][1],
log_sets[set_i][2], bytes, reference);
ops.ff8_fft_butterfly4_out(
inputs[0] + 1, inputs[1] + 1,
inputs[2] + 1, inputs[3] + 1,
outputs[0] + 1, outputs[1] + 1,
outputs[2] + 1, outputs[3] + 1,
log_sets[set_i][0], log_sets[set_i][1],
log_sets[set_i][2], bytes);
if (std::memcmp(outputs, expected, sizeof(outputs)) != 0 ||
std::memcmp(inputs, original_inputs, sizeof(inputs)) != 0)
return false;
for (unsigned lane = 0; lane < 4; ++lane)
{
std::memset(outputs[lane], 0x6dU + lane,
sizeof(outputs[lane]));
std::memcpy(expected[lane], outputs[lane],
sizeof(outputs[lane]));
std::memcpy(expected[lane] + 1, inputs[lane] + 1, bytes);
}
ReferenceFF8Butterfly4<true>(
expected[0] + 1, expected[1] + 1,
expected[2] + 1, expected[3] + 1,
log_sets[set_i][0], log_sets[set_i][1],
log_sets[set_i][2], bytes, reference);
ops.ff8_ifft_butterfly4_out(
inputs[0] + 1, inputs[1] + 1,
inputs[2] + 1, inputs[3] + 1,
outputs[0] + 1, outputs[1] + 1,
outputs[2] + 1, outputs[3] + 1,
log_sets[set_i][0], log_sets[set_i][1],
log_sets[set_i][2], bytes);
if (std::memcmp(outputs, expected, sizeof(outputs)) != 0 ||
std::memcmp(inputs, original_inputs, sizeof(inputs)) != 0)
return false;
}
return true;
}
static bool TestFF8WeightedIFFTButterfly4(
const Ops& ops, FF8MultiplyLog reference)
{
static const uint16_t weight_sets[][4] = {
{ 0, 0, 0, 0 },
{ 0, 255, 17, 129 },
{ 254, 1, 255, 0 }
};
static const uint16_t skew_sets[][3] = {
{ 255, 0, 7 },
{ 3, 255, 255 },
{ 1, 2, 3 }
};
static const uint64_t byte_counts[] = { 0, 1, 15, 16, 17, 31, 32, 37 };
uint8_t inputs[4][40];
uint8_t original_inputs[4][40];
uint8_t outputs[4][40];
uint8_t expected[4][40];
uint8_t in_place[4][40];
for (size_t weight_i = 0;
weight_i < sizeof(weight_sets) / sizeof(weight_sets[0]); ++weight_i)
for (size_t skew_i = 0;
skew_i < sizeof(skew_sets) / sizeof(skew_sets[0]); ++skew_i)
for (size_t count_i = 0;
count_i < sizeof(byte_counts) / sizeof(byte_counts[0]);
++count_i)
for (unsigned mask = 0; mask < 16; ++mask)
{
const uint64_t bytes = byte_counts[count_i];
for (unsigned lane = 0; lane < 4; ++lane)
{
for (size_t i = 0; i < sizeof(inputs[lane]); ++i)
{
inputs[lane][i] = static_cast<uint8_t>(
i * (31U + lane * 14U) +
weight_i * 19U + skew_i * 23U +
count_i * 29U + mask * 7U + lane);
outputs[lane][i] = expected[lane][i] =
static_cast<uint8_t>(0xa1U + lane * 11U);
}
std::memcpy(original_inputs[lane], inputs[lane],
sizeof(inputs[lane]));
std::memcpy(in_place[lane], inputs[lane],
sizeof(inputs[lane]));
if ((mask & (1U << lane)) == 0)
std::memset(expected[lane] + 1, 0, bytes);
else
{
std::memcpy(expected[lane] + 1,
inputs[lane] + 1, bytes);
const uint16_t weight = weight_sets[weight_i][lane];
if (weight != 0 && weight != 255)
for (uint64_t i = 0; i < bytes; ++i)
expected[lane][i + 1] = reference(
expected[lane][i + 1],
static_cast<uint8_t>(weight));
}
}
ReferenceFF8Butterfly4<true>(
expected[0] + 1, expected[1] + 1,
expected[2] + 1, expected[3] + 1,
skew_sets[skew_i][0], skew_sets[skew_i][1],
skew_sets[skew_i][2], bytes, reference);
ops.ff8_weighted_ifft_butterfly4(
(mask & 1U) ? inputs[0] + 1 : NULL,
(mask & 2U) ? inputs[1] + 1 : NULL,
(mask & 4U) ? inputs[2] + 1 : NULL,
(mask & 8U) ? inputs[3] + 1 : NULL,
outputs[0] + 1, outputs[1] + 1,
outputs[2] + 1, outputs[3] + 1,
weight_sets[weight_i][0], weight_sets[weight_i][1],
weight_sets[weight_i][2], weight_sets[weight_i][3],
static_cast<uint8_t>(mask),
skew_sets[skew_i][0], skew_sets[skew_i][1],
skew_sets[skew_i][2], bytes);
if (std::memcmp(outputs, expected, sizeof(outputs)) != 0 ||
std::memcmp(inputs, original_inputs,
sizeof(inputs)) != 0)
return false;
ops.ff8_weighted_ifft_butterfly4(
in_place[0] + 1, in_place[1] + 1,
in_place[2] + 1, in_place[3] + 1,
in_place[0] + 1, in_place[1] + 1,
in_place[2] + 1, in_place[3] + 1,
weight_sets[weight_i][0], weight_sets[weight_i][1],
weight_sets[weight_i][2], weight_sets[weight_i][3],
static_cast<uint8_t>(mask),
skew_sets[skew_i][0], skew_sets[skew_i][1],
skew_sets[skew_i][2], bytes);
for (unsigned lane = 0; lane < 4; ++lane)
{
if (std::memcmp(in_place[lane] + 1,
expected[lane] + 1, bytes) != 0 ||
in_place[lane][0] != inputs[lane][0] ||
std::memcmp(in_place[lane] + bytes + 1,
inputs[lane] + bytes + 1,
sizeof(in_place[lane]) -
static_cast<size_t>(bytes) - 1) != 0)
return false;
}
}
return true;
}
static bool TestFF8ButterflyRanges(const Ops& ops)
{
static const unsigned kDistance = 3;
static const unsigned kLaneCount = kDistance * 4;
static const uint16_t log_sets[][3] = {
{ 255, 0, 7 }, { 1, 2, 3 }
};
// The leaf KAT already exercises the 1,025-byte cutoff tail. Keep this
// range-specific test below common 64-KiB caller-stack budgets while still
// spanning multiple AVX2 vectors and a byte tail.