Repository navigation
Expand file tree
/
Copy pathLeopard2BackendAVX512GFNIT128.cpp
More file actions
677 lines (629 loc) · 24.3 KB
/
Copy pathLeopard2BackendAVX512GFNIT128.cpp
File metadata and controls
677 lines (629 loc) · 24.3 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
/*
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 "Leopard2Backend.h"
#include "LeopardCommon.h"
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <immintrin.h>
#if !defined(LEO2_HAVE_AVX512_GFNI_T128)
#error "This translation unit requires LEO2_HAVE_AVX512_GFNI_T128"
#endif
#if !defined(LEO_HAS_FF8)
#error "The AVX-512/GFNI T=128 kernel requires GF8"
#endif
namespace leopard { namespace backend {
namespace {
static const unsigned kOriginalCount = 65;
static const unsigned kRecoveryCount = 65;
static const unsigned kSide = 128;
static const unsigned kShardBytes = 64;
static const unsigned kLargerTileVectors = 4;
static const size_t kStateBytes =
static_cast<size_t>(kSide) * kShardBytes;
static const unsigned kLargerKATBytes = 320;
static const uint8_t kZeroSkewLog = 255;
alignas(64) static uint64_t MultiplierMatrixByLog[256];
alignas(64) static uint8_t SkewLogStorage[256];
static bool BaseKernelQualified = false;
#if defined(__GNUC__) || defined(__clang__)
#define LEO2_T128_INLINE inline __attribute__((always_inline))
#if defined(__GNUC__) && !defined(__clang__) && defined(__ELF__)
#define LEO2_T128_KERNEL __attribute__((noinline, noipa, aligned(64), \
section(".text.leo2_avx512_gfni_t128")))
#elif defined(__ELF__)
#define LEO2_T128_KERNEL __attribute__((noinline, aligned(64), \
section(".text.leo2_avx512_gfni_t128")))
#else
#define LEO2_T128_KERNEL __attribute__((noinline, aligned(64)))
#endif
#else
#error "The AVX-512/GFNI T=128 object currently requires GCC or Clang"
#endif
static LEO2_T128_INLINE __m512i LoadRow(
const uint8_t* state, unsigned row)
{
return _mm512_load_si512(reinterpret_cast<const __m512i*>(
state + static_cast<size_t>(row) * kShardBytes));
}
static LEO2_T128_INLINE void StoreRow(
uint8_t* state, unsigned row, __m512i value)
{
_mm512_store_si512(reinterpret_cast<__m512i*>(
state + static_cast<size_t>(row) * kShardBytes), value);
}
static LEO2_T128_INLINE __m512i Multiply(
__m512i value, uint64_t matrix)
{
return _mm512_gf2p8affine_epi64_epi8(
value, _mm512_set1_epi64(static_cast<long long>(matrix)), 0);
}
static LEO2_T128_INLINE void Inverse4(
uint8_t* state, unsigned row, unsigned distance,
uint64_t matrix01, uint64_t matrix23, uint64_t matrix02)
{
__m512i x0 = LoadRow(state, row);
__m512i x1 = LoadRow(state, row + distance);
__m512i x2 = LoadRow(state, row + distance * 2U);
__m512i x3 = LoadRow(state, row + distance * 3U);
x1 = _mm512_xor_si512(x1, x0);
if (matrix01 != 0)
x0 = _mm512_xor_si512(x0, Multiply(x1, matrix01));
x3 = _mm512_xor_si512(x3, x2);
if (matrix23 != 0)
x2 = _mm512_xor_si512(x2, Multiply(x3, matrix23));
x2 = _mm512_xor_si512(x2, x0);
x3 = _mm512_xor_si512(x3, x1);
if (matrix02 != 0)
{
x0 = _mm512_xor_si512(x0, Multiply(x2, matrix02));
x1 = _mm512_xor_si512(x1, Multiply(x3, matrix02));
}
StoreRow(state, row, x0);
StoreRow(state, row + distance, x1);
StoreRow(state, row + distance * 2U, x2);
StoreRow(state, row + distance * 3U, x3);
}
static LEO2_T128_INLINE void Forward4(
uint8_t* state, unsigned row, unsigned distance,
uint64_t matrix01, uint64_t matrix23, uint64_t matrix02)
{
__m512i x0 = LoadRow(state, row);
__m512i x1 = LoadRow(state, row + distance);
__m512i x2 = LoadRow(state, row + distance * 2U);
__m512i x3 = LoadRow(state, row + distance * 3U);
if (matrix02 != 0)
{
x0 = _mm512_xor_si512(x0, Multiply(x2, matrix02));
x1 = _mm512_xor_si512(x1, Multiply(x3, matrix02));
}
x2 = _mm512_xor_si512(x2, x0);
x3 = _mm512_xor_si512(x3, x1);
if (matrix01 != 0)
x0 = _mm512_xor_si512(x0, Multiply(x1, matrix01));
x1 = _mm512_xor_si512(x1, x0);
if (matrix23 != 0)
x2 = _mm512_xor_si512(x2, Multiply(x3, matrix23));
x3 = _mm512_xor_si512(x3, x2);
StoreRow(state, row, x0);
StoreRow(state, row + distance, x1);
StoreRow(state, row + distance * 2U, x2);
StoreRow(state, row + distance * 3U, x3);
}
static LEO2_T128_INLINE void Inverse2(
uint8_t* state, unsigned row, unsigned distance, uint64_t matrix)
{
__m512i x = LoadRow(state, row);
__m512i y = LoadRow(state, row + distance);
y = _mm512_xor_si512(y, x);
if (matrix != 0)
x = _mm512_xor_si512(x, Multiply(y, matrix));
StoreRow(state, row, x);
StoreRow(state, row + distance, y);
}
static LEO2_T128_INLINE void Forward2(
uint8_t* state, unsigned row, unsigned distance, uint64_t matrix)
{
__m512i x = LoadRow(state, row);
__m512i y = LoadRow(state, row + distance);
if (matrix != 0)
x = _mm512_xor_si512(x, Multiply(y, matrix));
y = _mm512_xor_si512(y, x);
StoreRow(state, row, x);
StoreRow(state, row + distance, y);
}
static void LEO2_T128_KERNEL EncodeK65R65B64(
const void* input_pointer,
void* output_pointer,
void* state_pointer)
{
const uint8_t* const input = static_cast<const uint8_t*>(input_pointer);
uint8_t* const output = static_cast<uint8_t*>(output_pointer);
uint8_t* const state = static_cast<uint8_t*>(state_pointer);
for (unsigned row = 0; row < kOriginalCount; ++row)
{
StoreRow(state, row, _mm512_loadu_si512(
input + static_cast<size_t>(row) * kShardBytes));
}
const __m512i zero = _mm512_setzero_si512();
for (unsigned row = kOriginalCount; row < kSide; ++row)
StoreRow(state, row, zero);
for (unsigned distance = 1; distance <= 16; distance *= 4)
{
const unsigned width = distance * 4U;
for (unsigned row = 0; row < kOriginalCount; row += width)
{
const uint8_t* const skew = SkewLogStorage + 128U + row;
const uint64_t matrix01 =
MultiplierMatrixByLog[skew[distance]];
const uint64_t matrix23 =
MultiplierMatrixByLog[skew[distance * 3U]];
const uint64_t matrix02 =
MultiplierMatrixByLog[skew[distance * 2U]];
for (unsigned lane = 0; lane < distance; ++lane)
{
Inverse4(state, row + lane, distance,
matrix01, matrix23, matrix02);
}
}
}
const uint64_t inverse_final =
MultiplierMatrixByLog[SkewLogStorage[128U + 64U]];
for (unsigned row = 0; row < 64; ++row)
Inverse2(state, row, 64, inverse_final);
for (unsigned distance = 32; distance >= 2; distance /= 4)
{
const unsigned width = distance * 4U;
for (unsigned row = 0; row < kRecoveryCount; row += width)
{
const uint8_t* const skew = SkewLogStorage + row;
const uint64_t matrix01 =
MultiplierMatrixByLog[skew[distance]];
const uint64_t matrix23 =
MultiplierMatrixByLog[skew[distance * 3U]];
const uint64_t matrix02 =
MultiplierMatrixByLog[skew[distance * 2U]];
for (unsigned lane = 0; lane < distance; ++lane)
{
Forward4(state, row + lane, distance,
matrix01, matrix23, matrix02);
}
}
}
for (unsigned row = 0; row < kRecoveryCount; row += 2)
{
Forward2(state, row, 1,
MultiplierMatrixByLog[SkewLogStorage[row + 1U]]);
}
for (unsigned row = 0; row < kRecoveryCount; ++row)
{
_mm512_storeu_si512(
output + static_cast<size_t>(row) * kShardBytes,
LoadRow(state, row));
}
}
/*
Keep this body separate from the already-qualified B=64 entry. Payload
columns are independent GF(256) transforms. Group up to four adjacent
vectors so every skew/matrix lookup is reused across a 256-byte row
microtile while each transform retains its own aligned 8 KiB state.
*/
static void LEO2_T128_KERNEL EncodeK65R65Multiples64(
const void* input_pointer,
void* output_pointer,
void* state_pointer,
size_t shard_bytes)
{
LEO_DEBUG_ASSERT(shard_bytes > kShardBytes);
LEO_DEBUG_ASSERT((shard_bytes & (kShardBytes - 1U)) == 0);
if (shard_bytes <= kShardBytes ||
(shard_bytes & (kShardBytes - 1U)) != 0)
return;
const uint8_t* const input = static_cast<const uint8_t*>(input_pointer);
uint8_t* const output = static_cast<uint8_t*>(output_pointer);
uint8_t* const state = static_cast<uint8_t*>(state_pointer);
for (size_t offset = 0; offset < shard_bytes; )
{
const size_t remaining_vectors =
(shard_bytes - offset) / kShardBytes;
const unsigned vector_count = static_cast<unsigned>(
remaining_vectors < kLargerTileVectors
? remaining_vectors : kLargerTileVectors);
for (unsigned row = 0; row < kOriginalCount; ++row)
{
for (unsigned vector = 0; vector < vector_count; ++vector)
{
uint8_t* const vector_state =
state + static_cast<size_t>(vector) * kStateBytes;
StoreRow(vector_state, row, _mm512_loadu_si512(
input + static_cast<size_t>(row) * shard_bytes + offset +
static_cast<size_t>(vector) * kShardBytes));
}
}
const __m512i zero = _mm512_setzero_si512();
for (unsigned vector = 0; vector < vector_count; ++vector)
{
uint8_t* const vector_state =
state + static_cast<size_t>(vector) * kStateBytes;
for (unsigned row = kOriginalCount; row < kSide; ++row)
StoreRow(vector_state, row, zero);
}
for (unsigned distance = 1; distance <= 16; distance *= 4)
{
const unsigned width = distance * 4U;
for (unsigned row = 0; row < kOriginalCount; row += width)
{
const uint8_t* const skew = SkewLogStorage + 128U + row;
const uint64_t matrix01 =
MultiplierMatrixByLog[skew[distance]];
const uint64_t matrix23 =
MultiplierMatrixByLog[skew[distance * 3U]];
const uint64_t matrix02 =
MultiplierMatrixByLog[skew[distance * 2U]];
for (unsigned lane = 0; lane < distance; ++lane)
{
for (unsigned vector = 0; vector < vector_count; ++vector)
{
Inverse4(
state + static_cast<size_t>(vector) * kStateBytes,
row + lane, distance,
matrix01, matrix23, matrix02);
}
}
}
}
const uint64_t inverse_final =
MultiplierMatrixByLog[SkewLogStorage[128U + 64U]];
for (unsigned row = 0; row < 64; ++row)
{
for (unsigned vector = 0; vector < vector_count; ++vector)
{
Inverse2(
state + static_cast<size_t>(vector) * kStateBytes,
row, 64, inverse_final);
}
}
for (unsigned distance = 32; distance >= 2; distance /= 4)
{
const unsigned width = distance * 4U;
for (unsigned row = 0; row < kRecoveryCount; row += width)
{
const uint8_t* const skew = SkewLogStorage + row;
const uint64_t matrix01 =
MultiplierMatrixByLog[skew[distance]];
const uint64_t matrix23 =
MultiplierMatrixByLog[skew[distance * 3U]];
const uint64_t matrix02 =
MultiplierMatrixByLog[skew[distance * 2U]];
for (unsigned lane = 0; lane < distance; ++lane)
{
for (unsigned vector = 0; vector < vector_count; ++vector)
{
Forward4(
state + static_cast<size_t>(vector) * kStateBytes,
row + lane, distance,
matrix01, matrix23, matrix02);
}
}
}
}
for (unsigned row = 0; row < kRecoveryCount; row += 2)
{
const uint64_t matrix =
MultiplierMatrixByLog[SkewLogStorage[row + 1U]];
for (unsigned vector = 0; vector < vector_count; ++vector)
{
Forward2(
state + static_cast<size_t>(vector) * kStateBytes,
row, 1, matrix);
}
}
for (unsigned row = 0; row < kRecoveryCount; ++row)
{
for (unsigned vector = 0; vector < vector_count; ++vector)
{
const uint8_t* const vector_state =
state + static_cast<size_t>(vector) * kStateBytes;
_mm512_storeu_si512(
output + static_cast<size_t>(row) * shard_bytes + offset +
static_cast<size_t>(vector) * kShardBytes,
LoadRow(vector_state, row));
}
}
offset += static_cast<size_t>(vector_count) * kShardBytes;
}
}
static uint8_t ApplyMatrix(uint8_t value, uint64_t matrix)
{
static const uint8_t kNibbleParity[16] = {
0, 1, 1, 0, 1, 0, 0, 1,
1, 0, 0, 1, 0, 1, 1, 0
};
uint8_t result = 0;
for (unsigned output_bit = 0; output_bit < 8; ++output_bit)
{
const uint8_t row = static_cast<uint8_t>(
matrix >> (8U * (7U - output_bit)));
const uint8_t masked = static_cast<uint8_t>(row & value);
const unsigned parity = static_cast<unsigned>(
kNibbleParity[masked & 15U] ^ kNibbleParity[masked >> 4]);
result |= static_cast<uint8_t>(parity << output_bit);
}
return result;
}
static bool BuildMatrices(FF8MultiplyLog multiply_log)
{
if (!multiply_log)
return false;
for (unsigned log = 0; log < 256; ++log)
{
if (log == kZeroSkewLog)
{
MultiplierMatrixByLog[log] = 0;
continue;
}
uint64_t matrix = 0;
for (unsigned output_bit = 0; output_bit < 8; ++output_bit)
{
uint8_t row = 0;
for (unsigned input_bit = 0; input_bit < 8; ++input_bit)
{
const uint8_t product = multiply_log(
static_cast<uint8_t>(1U << input_bit),
static_cast<uint8_t>(log));
if ((product & (1U << output_bit)) != 0)
row |= static_cast<uint8_t>(1U << input_bit);
}
matrix |= static_cast<uint64_t>(row) <<
(8U * (7U - output_bit));
}
MultiplierMatrixByLog[log] = matrix;
for (unsigned value = 0; value < 256; ++value)
{
if (ApplyMatrix(static_cast<uint8_t>(value), matrix) !=
multiply_log(static_cast<uint8_t>(value),
static_cast<uint8_t>(log)))
return false;
}
}
return true;
}
static void ScalarTransform(
FF8MultiplyLog multiply_log,
const uint8_t* input,
uint8_t* output,
uint8_t state[128][64])
{
std::memcpy(state, input, kOriginalCount * kShardBytes);
std::memset(state[kOriginalCount], 0,
(kSide - kOriginalCount) * kShardBytes);
for (unsigned level = 0; level < 7; ++level)
{
const unsigned distance = 1U << level;
const unsigned width = distance * 2U;
for (unsigned row = 0; row < kSide; row += width)
{
const uint8_t log =
SkewLogStorage[128U + row + distance];
for (unsigned lane = 0; lane < distance; ++lane)
{
uint8_t* const x = state[row + lane];
uint8_t* const y = state[row + lane + distance];
for (unsigned column = 0; column < kShardBytes; ++column)
{
y[column] ^= x[column];
if (log != kZeroSkewLog)
x[column] ^= multiply_log(y[column], log);
}
}
}
}
for (unsigned level = 7; level-- > 0; )
{
const unsigned distance = 1U << level;
const unsigned width = distance * 2U;
for (unsigned row = 0; row < kSide; row += width)
{
const uint8_t log = SkewLogStorage[row + distance];
for (unsigned lane = 0; lane < distance; ++lane)
{
uint8_t* const x = state[row + lane];
uint8_t* const y = state[row + lane + distance];
for (unsigned column = 0; column < kShardBytes; ++column)
{
if (log != kZeroSkewLog)
x[column] ^= multiply_log(y[column], log);
y[column] ^= x[column];
}
}
}
}
std::memcpy(output, state, kRecoveryCount * kShardBytes);
}
static void ScalarTransformMultiples64(
FF8MultiplyLog multiply_log,
const uint8_t* input,
uint8_t* output,
uint8_t state[kSide][kLargerKATBytes],
size_t shard_bytes)
{
for (unsigned row = 0; row < kOriginalCount; ++row)
{
std::memcpy(state[row],
input + static_cast<size_t>(row) * shard_bytes, shard_bytes);
}
for (unsigned row = kOriginalCount; row < kSide; ++row)
std::memset(state[row], 0, shard_bytes);
for (unsigned level = 0; level < 7; ++level)
{
const unsigned distance = 1U << level;
const unsigned width = distance * 2U;
for (unsigned row = 0; row < kSide; row += width)
{
const uint8_t log =
SkewLogStorage[128U + row + distance];
for (unsigned lane = 0; lane < distance; ++lane)
{
uint8_t* const x = state[row + lane];
uint8_t* const y = state[row + lane + distance];
for (size_t column = 0; column < shard_bytes; ++column)
{
y[column] ^= x[column];
if (log != kZeroSkewLog)
x[column] ^= multiply_log(y[column], log);
}
}
}
}
for (unsigned level = 7; level-- > 0; )
{
const unsigned distance = 1U << level;
const unsigned width = distance * 2U;
for (unsigned row = 0; row < kSide; row += width)
{
const uint8_t log = SkewLogStorage[row + distance];
for (unsigned lane = 0; lane < distance; ++lane)
{
uint8_t* const x = state[row + lane];
uint8_t* const y = state[row + lane + distance];
for (size_t column = 0; column < shard_bytes; ++column)
{
if (log != kZeroSkewLog)
x[column] ^= multiply_log(y[column], log);
y[column] ^= x[column];
}
}
}
}
for (unsigned row = 0; row < kRecoveryCount; ++row)
{
std::memcpy(output + static_cast<size_t>(row) * shard_bytes,
state[row], shard_bytes);
}
}
static bool KnownAnswerTest(FF8MultiplyLog multiply_log)
{
alignas(64) static uint8_t input[kOriginalCount * kShardBytes];
alignas(64) static uint8_t expected[kRecoveryCount * kShardBytes];
alignas(64) static uint8_t actual[kRecoveryCount * kShardBytes];
alignas(64) static uint8_t scalar_state[kSide][kShardBytes];
alignas(64) static uint8_t vector_state[kSide * kShardBytes];
for (unsigned pattern = 0; pattern < 2; ++pattern)
{
uint64_t random = UINT64_C(0x6b36357236356236) + pattern;
for (size_t i = 0; i < sizeof(input); ++i)
{
random += UINT64_C(0x9e3779b97f4a7c15);
uint64_t value = random;
value = (value ^ (value >> 30)) *
UINT64_C(0xbf58476d1ce4e5b9);
value = (value ^ (value >> 27)) *
UINT64_C(0x94d049bb133111eb);
value ^= value >> 31;
input[i] = pattern == 0
? static_cast<uint8_t>(value)
: static_cast<uint8_t>(i * 29U + (i >> 6) * 17U + 1U);
}
ScalarTransform(multiply_log, input, expected, scalar_state);
EncodeK65R65B64(input, actual, vector_state);
if (std::memcmp(expected, actual, sizeof(expected)) != 0)
return false;
}
return true;
}
static bool KnownAnswerTestMultiples64(FF8MultiplyLog multiply_log)
{
alignas(64) static uint8_t
input[kOriginalCount * kLargerKATBytes];
alignas(64) static uint8_t
expected[kRecoveryCount * kLargerKATBytes];
alignas(64) static uint8_t
actual[kRecoveryCount * kLargerKATBytes];
alignas(64) static uint8_t
scalar_state[kSide][kLargerKATBytes];
alignas(64) static uint8_t
vector_state[kLargerTileVectors * kSide * kShardBytes];
static const size_t kTestBytes[] = { 128, kLargerKATBytes };
for (size_t byte_index = 0;
byte_index < sizeof(kTestBytes) / sizeof(kTestBytes[0]);
++byte_index)
{
const size_t shard_bytes = kTestBytes[byte_index];
for (unsigned pattern = 0; pattern < 2; ++pattern)
{
uint64_t random = UINT64_C(0x6c61726765723634) +
pattern + shard_bytes;
const size_t input_bytes = kOriginalCount * shard_bytes;
for (size_t i = 0; i < input_bytes; ++i)
{
random += UINT64_C(0x9e3779b97f4a7c15);
uint64_t value = random;
value = (value ^ (value >> 30)) *
UINT64_C(0xbf58476d1ce4e5b9);
value = (value ^ (value >> 27)) *
UINT64_C(0x94d049bb133111eb);
value ^= value >> 31;
input[i] = pattern == 0
? static_cast<uint8_t>(value)
: static_cast<uint8_t>(
i * 29U + (i / shard_bytes) * 17U + 1U);
}
ScalarTransformMultiples64(
multiply_log, input, expected, scalar_state, shard_bytes);
EncodeK65R65Multiples64(
input, actual, vector_state, shard_bytes);
if (std::memcmp(expected, actual,
kRecoveryCount * shard_bytes) != 0)
return false;
}
}
return true;
}
} // namespace
FF8K65R65B64PackedKernel InitializeAVX512GFNIT128(
FF8MultiplyLog multiply_log,
const uint8_t* skew_log_storage)
{
BaseKernelQualified = false;
if (!multiply_log || !skew_log_storage)
return NULL;
std::memcpy(SkewLogStorage, skew_log_storage,
sizeof(SkewLogStorage));
if (!BuildMatrices(multiply_log) || !KnownAnswerTest(multiply_log))
return NULL;
BaseKernelQualified = true;
return &EncodeK65R65B64;
}
FF8K65R65Multiples64PackedKernel InitializeAVX512GFNIT128Multiples64(
FF8MultiplyLog multiply_log)
{
if (!BaseKernelQualified || !multiply_log ||
!KnownAnswerTestMultiples64(multiply_log))
return NULL;
return &EncodeK65R65Multiples64;
}
#undef LEO2_T128_KERNEL
#undef LEO2_T128_INLINE
}} // namespace leopard::backend