-
Notifications
You must be signed in to change notification settings - Fork 23
/
LeopardFF16.cpp
1799 lines (1473 loc) · 56 KB
/
LeopardFF16.cpp
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.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of Leopard-RS nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "LeopardFF16.h"
#ifdef LEO_HAS_FF16
#include <string.h>
#ifdef _MSC_VER
#pragma warning(disable: 4752) // found Intel(R) Advanced Vector Extensions; consider using /arch:AVX
#endif
namespace leopard { namespace ff16 {
//------------------------------------------------------------------------------
// Datatypes and Constants
// Basis used for generating logarithm tables
static const ffe_t kCantorBasis[kBits] = {
0x0001, 0xACCA, 0x3C0E, 0x163E,
0xC582, 0xED2E, 0x914C, 0x4012,
0x6C98, 0x10D8, 0x6A72, 0xB900,
0xFDB8, 0xFB34, 0xFF38, 0x991E
};
// Using the Cantor basis here enables us to avoid a lot of extra calculations
// when applying the formal derivative in decoding.
//------------------------------------------------------------------------------
// Field Operations
// z = x + y (mod kModulus)
static inline ffe_t AddMod(const ffe_t a, const ffe_t b)
{
const unsigned sum = (unsigned)a + b;
// Partial reduction step, allowing for kModulus to be returned
return static_cast<ffe_t>(sum + (sum >> kBits));
}
// z = x - y (mod kModulus)
static inline ffe_t SubMod(const ffe_t a, const ffe_t b)
{
const unsigned dif = (unsigned)a - b;
// Partial reduction step, allowing for kModulus to be returned
return static_cast<ffe_t>(dif + (dif >> kBits));
}
//------------------------------------------------------------------------------
// Fast Walsh-Hadamard Transform (FWHT) (mod kModulus)
// {a, b} = {a + b, a - b} (Mod Q)
static LEO_FORCE_INLINE void FWHT_2(ffe_t& LEO_RESTRICT a, ffe_t& LEO_RESTRICT b)
{
const ffe_t sum = AddMod(a, b);
const ffe_t dif = SubMod(a, b);
a = sum;
b = dif;
}
static LEO_FORCE_INLINE void FWHT_4(ffe_t* data, unsigned s)
{
const unsigned s2 = s << 1;
ffe_t t0 = data[0];
ffe_t t1 = data[s];
ffe_t t2 = data[s2];
ffe_t t3 = data[s2 + s];
FWHT_2(t0, t1);
FWHT_2(t2, t3);
FWHT_2(t0, t2);
FWHT_2(t1, t3);
data[0] = t0;
data[s] = t1;
data[s2] = t2;
data[s2 + s] = t3;
}
// Decimation in time (DIT) Fast Walsh-Hadamard Transform
// Unrolls pairs of layers to perform cross-layer operations in registers
// m_truncated: Number of elements that are non-zero at the front of data
static void FWHT(ffe_t* data, const unsigned m, const unsigned m_truncated)
{
// Decimation in time: Unroll 2 layers at a time
unsigned dist = 1, dist4 = 4;
for (; dist4 <= m; dist = dist4, dist4 <<= 2)
{
// For each set of dist*4 elements:
#pragma omp parallel for
for (int r = 0; r < (int)m_truncated; r += dist4)
{
// For each set of dist elements:
const int i_end = r + dist;
for (int i = r; i < i_end; ++i)
FWHT_4(data + i, dist);
}
}
// If there is one layer left:
if (dist < m)
#pragma omp parallel for
for (int i = 0; i < (int)dist; ++i)
FWHT_2(data[i], data[i + dist]);
}
//------------------------------------------------------------------------------
// Logarithm Tables
static ffe_t LogLUT[kOrder];
static ffe_t ExpLUT[kOrder];
// Returns a * Log(b)
static ffe_t MultiplyLog(ffe_t a, ffe_t log_b)
{
/*
Note that this operation is not a normal multiplication in a finite
field because the right operand is already a logarithm. This is done
because it moves K table lookups from the Decode() method into the
initialization step that is less performance critical. The LogWalsh[]
table below contains precalculated logarithms so it is easier to do
all the other multiplies in that form as well.
*/
if (a == 0)
return 0;
return ExpLUT[AddMod(LogLUT[a], log_b)];
}
// Initialize LogLUT[], ExpLUT[]
static void InitializeLogarithmTables()
{
// LFSR table generation:
unsigned state = 1;
for (unsigned i = 0; i < kModulus; ++i)
{
ExpLUT[state] = static_cast<ffe_t>(i);
state <<= 1;
if (state >= kOrder)
state ^= kPolynomial;
}
ExpLUT[0] = kModulus;
// Conversion to Cantor basis:
LogLUT[0] = 0;
for (unsigned i = 0; i < kBits; ++i)
{
const ffe_t basis = kCantorBasis[i];
const unsigned width = static_cast<unsigned>(1UL << i);
for (unsigned j = 0; j < width; ++j)
LogLUT[j + width] = LogLUT[j] ^ basis;
}
for (unsigned i = 0; i < kOrder; ++i)
LogLUT[i] = ExpLUT[LogLUT[i]];
for (unsigned i = 0; i < kOrder; ++i)
ExpLUT[LogLUT[i]] = i;
ExpLUT[kModulus] = ExpLUT[0];
}
//------------------------------------------------------------------------------
// Multiplies
/*
The multiplication algorithm used follows the approach outlined in {4}.
Specifically section 7 outlines the algorithm used here for 16-bit fields.
The ALTMAP memory layout is used since there is no need to convert in/out.
*/
struct Multiply128LUT_t
{
LEO_M128 Lo[4];
LEO_M128 Hi[4];
};
static const Multiply128LUT_t* Multiply128LUT = nullptr;
#define LEO_MUL_TABLES_128(table, log_m) \
const LEO_M128 T0_lo_##table = _mm_loadu_si128(&Multiply128LUT[log_m].Lo[0]); \
const LEO_M128 T1_lo_##table = _mm_loadu_si128(&Multiply128LUT[log_m].Lo[1]); \
const LEO_M128 T2_lo_##table = _mm_loadu_si128(&Multiply128LUT[log_m].Lo[2]); \
const LEO_M128 T3_lo_##table = _mm_loadu_si128(&Multiply128LUT[log_m].Lo[3]); \
const LEO_M128 T0_hi_##table = _mm_loadu_si128(&Multiply128LUT[log_m].Hi[0]); \
const LEO_M128 T1_hi_##table = _mm_loadu_si128(&Multiply128LUT[log_m].Hi[1]); \
const LEO_M128 T2_hi_##table = _mm_loadu_si128(&Multiply128LUT[log_m].Hi[2]); \
const LEO_M128 T3_hi_##table = _mm_loadu_si128(&Multiply128LUT[log_m].Hi[3]);
// 128-bit {prod_lo, prod_hi} = {value_lo, value_hi} * log_m
#define LEO_MUL_128(value_lo, value_hi, table) { \
LEO_M128 data_1 = _mm_srli_epi64(value_lo, 4); \
LEO_M128 data_0 = _mm_and_si128(value_lo, clr_mask); \
data_1 = _mm_and_si128(data_1, clr_mask); \
prod_lo = _mm_shuffle_epi8(T0_lo_##table, data_0); \
prod_hi = _mm_shuffle_epi8(T0_hi_##table, data_0); \
prod_lo = _mm_xor_si128(prod_lo, _mm_shuffle_epi8(T1_lo_##table, data_1)); \
prod_hi = _mm_xor_si128(prod_hi, _mm_shuffle_epi8(T1_hi_##table, data_1)); \
data_0 = _mm_and_si128(value_hi, clr_mask); \
data_1 = _mm_srli_epi64(value_hi, 4); \
data_1 = _mm_and_si128(data_1, clr_mask); \
prod_lo = _mm_xor_si128(prod_lo, _mm_shuffle_epi8(T2_lo_##table, data_0)); \
prod_hi = _mm_xor_si128(prod_hi, _mm_shuffle_epi8(T2_hi_##table, data_0)); \
prod_lo = _mm_xor_si128(prod_lo, _mm_shuffle_epi8(T3_lo_##table, data_1)); \
prod_hi = _mm_xor_si128(prod_hi, _mm_shuffle_epi8(T3_hi_##table, data_1)); }
// {x_lo, x_hi} ^= {y_lo, y_hi} * log_m
#define LEO_MULADD_128(x_lo, x_hi, y_lo, y_hi, table) { \
LEO_M128 prod_lo, prod_hi; \
LEO_MUL_128(y_lo, y_hi, table); \
x_lo = _mm_xor_si128(x_lo, prod_lo); \
x_hi = _mm_xor_si128(x_hi, prod_hi); }
#if defined(LEO_TRY_AVX2)
struct Multiply256LUT_t
{
LEO_M256 Lo[4];
LEO_M256 Hi[4];
};
static const Multiply256LUT_t* Multiply256LUT = nullptr;
#define LEO_MUL_TABLES_256(table, log_m) \
const LEO_M256 T0_lo_##table = _mm256_loadu_si256(&Multiply256LUT[log_m].Lo[0]); \
const LEO_M256 T1_lo_##table = _mm256_loadu_si256(&Multiply256LUT[log_m].Lo[1]); \
const LEO_M256 T2_lo_##table = _mm256_loadu_si256(&Multiply256LUT[log_m].Lo[2]); \
const LEO_M256 T3_lo_##table = _mm256_loadu_si256(&Multiply256LUT[log_m].Lo[3]); \
const LEO_M256 T0_hi_##table = _mm256_loadu_si256(&Multiply256LUT[log_m].Hi[0]); \
const LEO_M256 T1_hi_##table = _mm256_loadu_si256(&Multiply256LUT[log_m].Hi[1]); \
const LEO_M256 T2_hi_##table = _mm256_loadu_si256(&Multiply256LUT[log_m].Hi[2]); \
const LEO_M256 T3_hi_##table = _mm256_loadu_si256(&Multiply256LUT[log_m].Hi[3]);
// 256-bit {prod_lo, prod_hi} = {value_lo, value_hi} * log_m
#define LEO_MUL_256(value_lo, value_hi, table) { \
LEO_M256 data_1 = _mm256_srli_epi64(value_lo, 4); \
LEO_M256 data_0 = _mm256_and_si256(value_lo, clr_mask); \
data_1 = _mm256_and_si256(data_1, clr_mask); \
prod_lo = _mm256_shuffle_epi8(T0_lo_##table, data_0); \
prod_hi = _mm256_shuffle_epi8(T0_hi_##table, data_0); \
prod_lo = _mm256_xor_si256(prod_lo, _mm256_shuffle_epi8(T1_lo_##table, data_1)); \
prod_hi = _mm256_xor_si256(prod_hi, _mm256_shuffle_epi8(T1_hi_##table, data_1)); \
data_0 = _mm256_and_si256(value_hi, clr_mask); \
data_1 = _mm256_srli_epi64(value_hi, 4); \
data_1 = _mm256_and_si256(data_1, clr_mask); \
prod_lo = _mm256_xor_si256(prod_lo, _mm256_shuffle_epi8(T2_lo_##table, data_0)); \
prod_hi = _mm256_xor_si256(prod_hi, _mm256_shuffle_epi8(T2_hi_##table, data_0)); \
prod_lo = _mm256_xor_si256(prod_lo, _mm256_shuffle_epi8(T3_lo_##table, data_1)); \
prod_hi = _mm256_xor_si256(prod_hi, _mm256_shuffle_epi8(T3_hi_##table, data_1)); }
// {x_lo, x_hi} ^= {y_lo, y_hi} * log_m
#define LEO_MULADD_256(x_lo, x_hi, y_lo, y_hi, table) { \
LEO_M256 prod_lo, prod_hi; \
LEO_MUL_256(y_lo, y_hi, table); \
x_lo = _mm256_xor_si256(x_lo, prod_lo); \
x_hi = _mm256_xor_si256(x_hi, prod_hi); }
#endif // LEO_TRY_AVX2
// Stores the partial products of x * y at offset x + y * 65536
// Repeated accesses from the same y value are faster
struct Product16Table
{
ffe_t LUT[4 * 16];
};
static const Product16Table* Multiply16LUT = nullptr;
// Reference version of muladd: x[] ^= y[] * log_m
static LEO_FORCE_INLINE void RefMulAdd(
void* LEO_RESTRICT x,
const void* LEO_RESTRICT y,
ffe_t log_m,
uint64_t bytes)
{
const ffe_t* LEO_RESTRICT lut = Multiply16LUT[log_m].LUT;
const uint8_t * LEO_RESTRICT y1 = reinterpret_cast<const uint8_t *>(y);
uint8_t * LEO_RESTRICT x1 = reinterpret_cast<uint8_t *>(x);
do
{
for (unsigned i = 0; i < 32; ++i)
{
const unsigned lo = y1[i];
const unsigned hi = y1[i + 32];
const ffe_t prod = \
lut[(lo & 15)] ^ \
lut[(lo >> 4) + 16] ^ \
lut[(hi & 15) + 32] ^ \
lut[(hi >> 4) + 48];
x1[i] ^= (uint8_t)prod;
x1[i + 32] ^= (uint8_t)(prod >> 8);
}
x1 += 64, y1 += 64;
bytes -= 64;
} while (bytes > 0);
}
// Reference version of mul: x[] = y[] * log_m
static LEO_FORCE_INLINE void RefMul(
void* LEO_RESTRICT x,
const void* LEO_RESTRICT y,
ffe_t log_m,
uint64_t bytes)
{
const ffe_t* LEO_RESTRICT lut = Multiply16LUT[log_m].LUT;
const uint8_t * LEO_RESTRICT y1 = reinterpret_cast<const uint8_t *>(y);
uint8_t * LEO_RESTRICT x1 = reinterpret_cast<uint8_t *>(x);
do
{
for (unsigned i = 0; i < 32; ++i)
{
const unsigned lo = y1[i];
const unsigned hi = y1[i + 32];
const ffe_t prod = \
lut[(lo & 15)] ^ \
lut[(lo >> 4) + 16] ^ \
lut[(hi & 15) + 32] ^ \
lut[(hi >> 4) + 48];
x1[i] = (uint8_t)prod;
x1[i + 32] = (uint8_t)(prod >> 8);
}
x1 += 64, y1 += 64;
bytes -= 64;
} while (bytes > 0);
}
static void InitializeMultiplyTables()
{
// If we cannot use the PSHUFB instruction, generate Multiply8LUT:
if (!CpuHasSSSE3)
{
Multiply16LUT = new Product16Table[65536];
// For each log_m multiplicand:
#pragma omp parallel for
for (int log_m = 0; log_m < (int)kOrder; ++log_m)
{
const Product16Table& lut = Multiply16LUT[log_m];
for (unsigned nibble = 0, shift = 0; nibble < 4; ++nibble, shift += 4)
{
ffe_t* nibble_lut = (ffe_t*)&lut.LUT[nibble * 16];
for (unsigned x_nibble = 0; x_nibble < 16; ++x_nibble)
{
const ffe_t prod = MultiplyLog(x_nibble << shift, static_cast<ffe_t>(log_m));
nibble_lut[x_nibble] = prod;
}
}
}
return;
}
#if defined(LEO_TRY_AVX2)
if (CpuHasAVX2)
Multiply256LUT = reinterpret_cast<const Multiply256LUT_t*>(SIMDSafeAllocate(sizeof(Multiply256LUT_t) * kOrder));
else
#endif // LEO_TRY_AVX2
Multiply128LUT = reinterpret_cast<const Multiply128LUT_t*>(SIMDSafeAllocate(sizeof(Multiply128LUT_t) * kOrder));
// For each value we could multiply by:
#pragma omp parallel for
for (int log_m = 0; log_m < (int)kOrder; ++log_m)
{
// For each 4 bits of the finite field width in bits:
for (unsigned i = 0, shift = 0; i < 4; ++i, shift += 4)
{
// Construct 16 entry LUT for PSHUFB
uint8_t prod_lo[16], prod_hi[16];
for (ffe_t x = 0; x < 16; ++x)
{
const ffe_t prod = MultiplyLog(x << shift, static_cast<ffe_t>(log_m));
prod_lo[x] = static_cast<uint8_t>(prod);
prod_hi[x] = static_cast<uint8_t>(prod >> 8);
}
const LEO_M128 value_lo = _mm_loadu_si128((LEO_M128*)prod_lo);
const LEO_M128 value_hi = _mm_loadu_si128((LEO_M128*)prod_hi);
// Store in 128-bit wide table
#if defined(LEO_TRY_AVX2)
if (!CpuHasAVX2)
#endif // LEO_TRY_AVX2
{
_mm_storeu_si128((LEO_M128*)&Multiply128LUT[log_m].Lo[i], value_lo);
_mm_storeu_si128((LEO_M128*)&Multiply128LUT[log_m].Hi[i], value_hi);
}
// Store in 256-bit wide table
#if defined(LEO_TRY_AVX2)
if (CpuHasAVX2)
{
_mm256_storeu_si256((LEO_M256*)&Multiply256LUT[log_m].Lo[i],
_mm256_broadcastsi128_si256(value_lo));
_mm256_storeu_si256((LEO_M256*)&Multiply256LUT[log_m].Hi[i],
_mm256_broadcastsi128_si256(value_hi));
}
#endif // LEO_TRY_AVX2
}
}
}
static void mul_mem(
void * LEO_RESTRICT x, const void * LEO_RESTRICT y,
ffe_t log_m, uint64_t bytes)
{
#if defined(LEO_TRY_AVX2)
if (CpuHasAVX2)
{
LEO_MUL_TABLES_256(0, log_m);
const LEO_M256 clr_mask = _mm256_set1_epi8(0x0f);
LEO_M256 * LEO_RESTRICT x32 = reinterpret_cast<LEO_M256 *>(x);
const LEO_M256 * LEO_RESTRICT y32 = reinterpret_cast<const LEO_M256 *>(y);
do
{
#define LEO_MUL_256_LS(x_ptr, y_ptr) { \
const LEO_M256 data_lo = _mm256_loadu_si256(y_ptr); \
const LEO_M256 data_hi = _mm256_loadu_si256(y_ptr + 1); \
LEO_M256 prod_lo, prod_hi; \
LEO_MUL_256(data_lo, data_hi, 0); \
_mm256_storeu_si256(x_ptr, prod_lo); \
_mm256_storeu_si256(x_ptr + 1, prod_hi); }
LEO_MUL_256_LS(x32, y32);
y32 += 2, x32 += 2;
bytes -= 64;
} while (bytes > 0);
return;
}
#endif // LEO_TRY_AVX2
if (CpuHasSSSE3)
{
LEO_MUL_TABLES_128(0, log_m);
const LEO_M128 clr_mask = _mm_set1_epi8(0x0f);
LEO_M128 * LEO_RESTRICT x16 = reinterpret_cast<LEO_M128 *>(x);
const LEO_M128 * LEO_RESTRICT y16 = reinterpret_cast<const LEO_M128 *>(y);
do
{
#define LEO_MUL_128_LS(x_ptr, y_ptr) { \
const LEO_M128 data_lo = _mm_loadu_si128(y_ptr); \
const LEO_M128 data_hi = _mm_loadu_si128(y_ptr + 2); \
LEO_M128 prod_lo, prod_hi; \
LEO_MUL_128(data_lo, data_hi, 0); \
_mm_storeu_si128(x_ptr, prod_lo); \
_mm_storeu_si128(x_ptr + 2, prod_hi); }
LEO_MUL_128_LS(x16 + 1, y16 + 1);
LEO_MUL_128_LS(x16, y16);
x16 += 4, y16 += 4;
bytes -= 64;
} while (bytes > 0);
return;
}
RefMul(x, y, log_m, bytes);
}
//------------------------------------------------------------------------------
// FFT
// Twisted factors used in FFT
static ffe_t FFTSkew[kModulus];
// Factors used in the evaluation of the error locator polynomial
static ffe_t LogWalsh[kOrder];
static void FFTInitialize()
{
ffe_t temp[kBits - 1];
// Generate FFT skew vector {1}:
for (unsigned i = 1; i < kBits; ++i)
temp[i - 1] = static_cast<ffe_t>(1UL << i);
for (unsigned m = 0; m < (kBits - 1); ++m)
{
const unsigned step = 1UL << (m + 1);
FFTSkew[(1UL << m) - 1] = 0;
for (unsigned i = m; i < (kBits - 1); ++i)
{
const unsigned s = (1UL << (i + 1));
for (unsigned j = (1UL << m) - 1; j < s; j += step)
FFTSkew[j + s] = FFTSkew[j] ^ temp[i];
}
temp[m] = kModulus - LogLUT[MultiplyLog(temp[m], LogLUT[temp[m] ^ 1])];
for (unsigned i = m + 1; i < (kBits - 1); ++i)
{
const ffe_t sum = AddMod(LogLUT[temp[i] ^ 1], temp[m]);
temp[i] = MultiplyLog(temp[i], sum);
}
}
for (unsigned i = 0; i < kModulus; ++i)
FFTSkew[i] = LogLUT[FFTSkew[i]];
// Precalculate FWHT(Log[i]):
for (unsigned i = 0; i < kOrder; ++i)
LogWalsh[i] = LogLUT[i];
LogWalsh[0] = 0;
FWHT(LogWalsh, kOrder, kOrder);
}
/*
Decimation in time IFFT:
The decimation in time IFFT algorithm allows us to unroll 2 layers at a time,
performing calculations on local registers and faster cache memory.
Each ^___^ below indicates a butterfly between the associated indices.
The ifft_butterfly(x, y) operation:
y[] ^= x[]
if (log_m != kModulus)
x[] ^= exp(log(y[]) + log_m)
Layer 0:
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ ^_^
Layer 1:
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
^___^ ^___^ ^___^ ^___^
^___^ ^___^ ^___^ ^___^
Layer 2:
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
^_______^ ^_______^
^_______^ ^_______^
^_______^ ^_______^
^_______^ ^_______^
Layer 3:
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
^_______________^
^_______________^
^_______________^
^_______________^
^_______________^
^_______________^
^_______________^
^_______________^
DIT layer 0-1 operations, grouped 4 at a time:
{0-1, 2-3, 0-2, 1-3},
{4-5, 6-7, 4-6, 5-7},
DIT layer 1-2 operations, grouped 4 at a time:
{0-2, 4-6, 0-4, 2-6},
{1-3, 5-7, 1-5, 3-7},
DIT layer 2-3 operations, grouped 4 at a time:
{0-4, 0'-4', 0-0', 4-4'},
{1-5, 1'-5', 1-1', 5-5'},
*/
// 2-way butterfly
static void IFFT_DIT2(
void * LEO_RESTRICT x, void * LEO_RESTRICT y,
ffe_t log_m, uint64_t bytes)
{
#if defined(LEO_TRY_AVX2)
if (CpuHasAVX2)
{
LEO_MUL_TABLES_256(0, log_m);
const LEO_M256 clr_mask = _mm256_set1_epi8(0x0f);
LEO_M256 * LEO_RESTRICT x32 = reinterpret_cast<LEO_M256 *>(x);
LEO_M256 * LEO_RESTRICT y32 = reinterpret_cast<LEO_M256 *>(y);
do
{
#define LEO_IFFTB_256(x_ptr, y_ptr) { \
LEO_M256 x_lo = _mm256_loadu_si256(x_ptr); \
LEO_M256 x_hi = _mm256_loadu_si256(x_ptr + 1); \
LEO_M256 y_lo = _mm256_loadu_si256(y_ptr); \
LEO_M256 y_hi = _mm256_loadu_si256(y_ptr + 1); \
y_lo = _mm256_xor_si256(y_lo, x_lo); \
y_hi = _mm256_xor_si256(y_hi, x_hi); \
_mm256_storeu_si256(y_ptr, y_lo); \
_mm256_storeu_si256(y_ptr + 1, y_hi); \
LEO_MULADD_256(x_lo, x_hi, y_lo, y_hi, 0); \
_mm256_storeu_si256(x_ptr, x_lo); \
_mm256_storeu_si256(x_ptr + 1, x_hi); }
LEO_IFFTB_256(x32, y32);
y32 += 2, x32 += 2;
bytes -= 64;
} while (bytes > 0);
return;
}
#endif // LEO_TRY_AVX2
if (CpuHasSSSE3)
{
LEO_MUL_TABLES_128(0, log_m);
const LEO_M128 clr_mask = _mm_set1_epi8(0x0f);
LEO_M128 * LEO_RESTRICT x16 = reinterpret_cast<LEO_M128 *>(x);
LEO_M128 * LEO_RESTRICT y16 = reinterpret_cast<LEO_M128 *>(y);
do
{
#define LEO_IFFTB_128(x_ptr, y_ptr) { \
LEO_M128 x_lo = _mm_loadu_si128(x_ptr); \
LEO_M128 x_hi = _mm_loadu_si128(x_ptr + 2); \
LEO_M128 y_lo = _mm_loadu_si128(y_ptr); \
LEO_M128 y_hi = _mm_loadu_si128(y_ptr + 2); \
y_lo = _mm_xor_si128(y_lo, x_lo); \
y_hi = _mm_xor_si128(y_hi, x_hi); \
_mm_storeu_si128(y_ptr, y_lo); \
_mm_storeu_si128(y_ptr + 2, y_hi); \
LEO_MULADD_128(x_lo, x_hi, y_lo, y_hi, 0); \
_mm_storeu_si128(x_ptr, x_lo); \
_mm_storeu_si128(x_ptr + 2, x_hi); }
LEO_IFFTB_128(x16 + 1, y16 + 1);
LEO_IFFTB_128(x16, y16);
x16 += 4, y16 += 4;
bytes -= 64;
} while (bytes > 0);
return;
}
// Reference version:
xor_mem(y, x, bytes);
RefMulAdd(x, y, log_m, bytes);
}
// 4-way butterfly
static void IFFT_DIT4(
uint64_t bytes,
void** work,
unsigned dist,
const ffe_t log_m01,
const ffe_t log_m23,
const ffe_t log_m02)
{
#ifdef LEO_INTERLEAVE_BUTTERFLY4_OPT
#if defined(LEO_TRY_AVX2)
if (CpuHasAVX2)
{
LEO_MUL_TABLES_256(01, log_m01);
LEO_MUL_TABLES_256(23, log_m23);
LEO_MUL_TABLES_256(02, log_m02);
const LEO_M256 clr_mask = _mm256_set1_epi8(0x0f);
LEO_M256 * LEO_RESTRICT work0 = reinterpret_cast<LEO_M256 *>(work[0]);
LEO_M256 * LEO_RESTRICT work1 = reinterpret_cast<LEO_M256 *>(work[dist]);
LEO_M256 * LEO_RESTRICT work2 = reinterpret_cast<LEO_M256 *>(work[dist * 2]);
LEO_M256 * LEO_RESTRICT work3 = reinterpret_cast<LEO_M256 *>(work[dist * 3]);
do
{
LEO_M256 work_reg_lo_0 = _mm256_loadu_si256(work0);
LEO_M256 work_reg_hi_0 = _mm256_loadu_si256(work0 + 1);
LEO_M256 work_reg_lo_1 = _mm256_loadu_si256(work1);
LEO_M256 work_reg_hi_1 = _mm256_loadu_si256(work1 + 1);
// First layer:
work_reg_lo_1 = _mm256_xor_si256(work_reg_lo_0, work_reg_lo_1);
work_reg_hi_1 = _mm256_xor_si256(work_reg_hi_0, work_reg_hi_1);
if (log_m01 != kModulus)
LEO_MULADD_256(work_reg_lo_0, work_reg_hi_0, work_reg_lo_1, work_reg_hi_1, 01);
LEO_M256 work_reg_lo_2 = _mm256_loadu_si256(work2);
LEO_M256 work_reg_hi_2 = _mm256_loadu_si256(work2 + 1);
LEO_M256 work_reg_lo_3 = _mm256_loadu_si256(work3);
LEO_M256 work_reg_hi_3 = _mm256_loadu_si256(work3 + 1);
work_reg_lo_3 = _mm256_xor_si256(work_reg_lo_2, work_reg_lo_3);
work_reg_hi_3 = _mm256_xor_si256(work_reg_hi_2, work_reg_hi_3);
if (log_m23 != kModulus)
LEO_MULADD_256(work_reg_lo_2, work_reg_hi_2, work_reg_lo_3, work_reg_hi_3, 23);
// Second layer:
work_reg_lo_2 = _mm256_xor_si256(work_reg_lo_0, work_reg_lo_2);
work_reg_hi_2 = _mm256_xor_si256(work_reg_hi_0, work_reg_hi_2);
work_reg_lo_3 = _mm256_xor_si256(work_reg_lo_1, work_reg_lo_3);
work_reg_hi_3 = _mm256_xor_si256(work_reg_hi_1, work_reg_hi_3);
if (log_m02 != kModulus)
{
LEO_MULADD_256(work_reg_lo_0, work_reg_hi_0, work_reg_lo_2, work_reg_hi_2, 02);
LEO_MULADD_256(work_reg_lo_1, work_reg_hi_1, work_reg_lo_3, work_reg_hi_3, 02);
}
_mm256_storeu_si256(work0, work_reg_lo_0);
_mm256_storeu_si256(work0 + 1, work_reg_hi_0);
_mm256_storeu_si256(work1, work_reg_lo_1);
_mm256_storeu_si256(work1 + 1, work_reg_hi_1);
_mm256_storeu_si256(work2, work_reg_lo_2);
_mm256_storeu_si256(work2 + 1, work_reg_hi_2);
_mm256_storeu_si256(work3, work_reg_lo_3);
_mm256_storeu_si256(work3 + 1, work_reg_hi_3);
work0 += 2, work1 += 2, work2 += 2, work3 += 2;
bytes -= 64;
} while (bytes > 0);
return;
}
#endif // LEO_TRY_AVX2
if (CpuHasSSSE3)
{
LEO_MUL_TABLES_128(01, log_m01);
LEO_MUL_TABLES_128(23, log_m23);
LEO_MUL_TABLES_128(02, log_m02);
const LEO_M128 clr_mask = _mm_set1_epi8(0x0f);
LEO_M128 * LEO_RESTRICT work0 = reinterpret_cast<LEO_M128 *>(work[0]);
LEO_M128 * LEO_RESTRICT work1 = reinterpret_cast<LEO_M128 *>(work[dist]);
LEO_M128 * LEO_RESTRICT work2 = reinterpret_cast<LEO_M128 *>(work[dist * 2]);
LEO_M128 * LEO_RESTRICT work3 = reinterpret_cast<LEO_M128 *>(work[dist * 3]);
do
{
for (unsigned i = 0; i < 2; ++i)
{
LEO_M128 work_reg_lo_0 = _mm_loadu_si128(work0);
LEO_M128 work_reg_hi_0 = _mm_loadu_si128(work0 + 2);
LEO_M128 work_reg_lo_1 = _mm_loadu_si128(work1);
LEO_M128 work_reg_hi_1 = _mm_loadu_si128(work1 + 2);
// First layer:
work_reg_lo_1 = _mm_xor_si128(work_reg_lo_0, work_reg_lo_1);
work_reg_hi_1 = _mm_xor_si128(work_reg_hi_0, work_reg_hi_1);
if (log_m01 != kModulus)
LEO_MULADD_128(work_reg_lo_0, work_reg_hi_0, work_reg_lo_1, work_reg_hi_1, 01);
LEO_M128 work_reg_lo_2 = _mm_loadu_si128(work2);
LEO_M128 work_reg_hi_2 = _mm_loadu_si128(work2 + 2);
LEO_M128 work_reg_lo_3 = _mm_loadu_si128(work3);
LEO_M128 work_reg_hi_3 = _mm_loadu_si128(work3 + 2);
work_reg_lo_3 = _mm_xor_si128(work_reg_lo_2, work_reg_lo_3);
work_reg_hi_3 = _mm_xor_si128(work_reg_hi_2, work_reg_hi_3);
if (log_m23 != kModulus)
LEO_MULADD_128(work_reg_lo_2, work_reg_hi_2, work_reg_lo_3, work_reg_hi_3, 23);
// Second layer:
work_reg_lo_2 = _mm_xor_si128(work_reg_lo_0, work_reg_lo_2);
work_reg_hi_2 = _mm_xor_si128(work_reg_hi_0, work_reg_hi_2);
work_reg_lo_3 = _mm_xor_si128(work_reg_lo_1, work_reg_lo_3);
work_reg_hi_3 = _mm_xor_si128(work_reg_hi_1, work_reg_hi_3);
if (log_m02 != kModulus)
{
LEO_MULADD_128(work_reg_lo_0, work_reg_hi_0, work_reg_lo_2, work_reg_hi_2, 02);
LEO_MULADD_128(work_reg_lo_1, work_reg_hi_1, work_reg_lo_3, work_reg_hi_3, 02);
}
_mm_storeu_si128(work0, work_reg_lo_0);
_mm_storeu_si128(work0 + 2, work_reg_hi_0);
_mm_storeu_si128(work1, work_reg_lo_1);
_mm_storeu_si128(work1 + 2, work_reg_hi_1);
_mm_storeu_si128(work2, work_reg_lo_2);
_mm_storeu_si128(work2 + 2, work_reg_hi_2);
_mm_storeu_si128(work3, work_reg_lo_3);
_mm_storeu_si128(work3 + 2, work_reg_hi_3);
work0++, work1++, work2++, work3++;
}
work0 += 2, work1 += 2, work2 += 2, work3 += 2;
bytes -= 64;
} while (bytes > 0);
return;
}
#endif // LEO_INTERLEAVE_BUTTERFLY4_OPT
// First layer:
if (log_m01 == kModulus)
xor_mem(work[dist], work[0], bytes);
else
IFFT_DIT2(work[0], work[dist], log_m01, bytes);
if (log_m23 == kModulus)
xor_mem(work[dist * 3], work[dist * 2], bytes);
else
IFFT_DIT2(work[dist * 2], work[dist * 3], log_m23, bytes);
// Second layer:
if (log_m02 == kModulus)
{
xor_mem(work[dist * 2], work[0], bytes);
xor_mem(work[dist * 3], work[dist], bytes);
}
else
{
IFFT_DIT2(work[0], work[dist * 2], log_m02, bytes);
IFFT_DIT2(work[dist], work[dist * 3], log_m02, bytes);
}
}
// Unrolled IFFT for encoder
static void IFFT_DIT_Encoder(
const uint64_t bytes,
const void* const* data,
const unsigned m_truncated,
void** work,
void** xor_result,
const unsigned m,
const ffe_t* skewLUT)
{
// I tried rolling the memcpy/memset into the first layer of the FFT and
// found that it only yields a 4% performance improvement, which is not
// worth the extra complexity.
#pragma omp parallel for
for (int i = 0; i < (int)m_truncated; ++i)
memcpy(work[i], data[i], bytes);
#pragma omp parallel for
for (int i = m_truncated; i < (int)m; ++i)
memset(work[i], 0, bytes);
// I tried splitting up the first few layers into L3-cache sized blocks but
// found that it only provides about 5% performance boost, which is not
// worth the extra complexity.
// Decimation in time: Unroll 2 layers at a time
unsigned dist = 1, dist4 = 4;
for (; dist4 <= m; dist = dist4, dist4 <<= 2)
{
// For each set of dist*4 elements:
#pragma omp parallel for
for (int r = 0; r < (int)m_truncated; r += dist4)
{
const unsigned i_end = r + dist;
const ffe_t log_m01 = skewLUT[i_end];
const ffe_t log_m02 = skewLUT[i_end + dist];
const ffe_t log_m23 = skewLUT[i_end + dist * 2];
// For each set of dist elements:
for (int i = r; i < (int)i_end; ++i)
{
IFFT_DIT4(
bytes,
work + i,
dist,
log_m01,
log_m23,
log_m02);
}
}
// I tried alternating sweeps left->right and right->left to reduce cache misses.
// It provides about 1% performance boost when done for both FFT and IFFT, so it
// does not seem to be worth the extra complexity.
}
// If there is one layer left:
if (dist < m)
{
// Assuming that dist = m / 2
LEO_DEBUG_ASSERT(dist * 2 == m);
const ffe_t log_m = skewLUT[dist];
if (log_m == kModulus)
VectorXOR_Threads(bytes, dist, work + dist, work);
else
{
#pragma omp parallel for
for (int i = 0; i < (int)dist; ++i)
{
IFFT_DIT2(
work[i],
work[i + dist],
log_m,
bytes);
}
}
}
// I tried unrolling this but it does not provide more than 5% performance
// improvement for 16-bit finite fields, so it's not worth the complexity.
if (xor_result)
VectorXOR_Threads(bytes, m, xor_result, work);
}
// Basic no-frills version for decoder
static void IFFT_DIT_Decoder(
const uint64_t bytes,
const unsigned m_truncated,
void** work,
const unsigned m,
const ffe_t* skewLUT)
{
// Decimation in time: Unroll 2 layers at a time
unsigned dist = 1, dist4 = 4;
for (; dist4 <= m; dist = dist4, dist4 <<= 2)
{
// For each set of dist*4 elements:
#pragma omp parallel for
for (int r = 0; r < (int)m_truncated; r += dist4)
{
const unsigned i_end = r + dist;
const ffe_t log_m01 = skewLUT[i_end];
const ffe_t log_m02 = skewLUT[i_end + dist];
const ffe_t log_m23 = skewLUT[i_end + dist * 2];
// For each set of dist elements:
for (int i = r; i < (int)i_end; ++i)
{
IFFT_DIT4(
bytes,
work + i,
dist,
log_m01,
log_m23,
log_m02);
}
}