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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
use super::*;
impl RecoveryEncoder {
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2,gfni")]
pub(super) unsafe fn flush_avx2_gfni(&mut self) {
let start_index = self.next_index;
let queued = std::mem::take(&mut self.queued_slices);
self.next_index += queued.len();
let new_cs: Vec<SliceChecksum> = if self.compute_checksums {
queued.par_iter().map(|s| slice_checksum(s)).collect()
} else {
Vec::new()
};
unsafe {
Self::flush_avx2_gfni_work(
self.buffers.as_normal_mut(),
&queued,
start_index,
&self.logbases,
self.exponent_start,
&self.gf,
);
}
self.pending_checksums.extend(new_cs);
self.recycle_queue(queued);
}
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2,gfni")]
#[allow(dead_code)]
pub(super) unsafe fn flush_avx2_gfni_work(
buffers: &mut [Vec<u16>],
queued: &[Vec<u8>],
start_index: usize,
logbases: &[u32],
exponent_start: u32,
gf: &Gf16,
) {
use std::arch::x86_64::*;
let deint_mask = _mm256_broadcastsi128_si256(_mm_setr_epi8(
0, 2, 4, 6, 8, 10, 12, 14, // lo bytes of 8 words → positions 0..7
1, 3, 5, 7, 9, 11, 13, 15, // hi bytes of 8 words → positions 8..15
));
let n_rec = buffers.len();
let n_queued = queued.len();
let all_tables: Vec<Avx2GfniTable> = (0..n_rec * n_queued)
.into_par_iter()
.map(|flat| {
let i = flat / n_queued;
let q_idx = flat % n_queued;
let exponent = exponent_start + i as u32;
let logbase = logbases[start_index + q_idx] as u64;
let log_coeff = ((logbase * exponent as u64) % ORDER as u64) as u32;
let coeff = gf.exp(log_coeff);
// gf2p8affineqb uses byte (7-row) of the u64 matrix operand for
// output bit `row` — so store each row at position (7-row)*8.
let mut m_ll = 0u64; // lo input byte → lo output byte
let mut m_lh = 0u64; // hi input byte → lo output byte
let mut m_hl = 0u64; // lo input byte → hi output byte
let mut m_hh = 0u64; // hi input byte → hi output byte
for row in 0..8usize {
let mut row_ll = 0u8;
let mut row_lh = 0u8;
let mut row_hl = 0u8;
let mut row_hh = 0u8;
for j in 0..8usize {
let r_lo = gf.mul(1u16 << j, coeff);
if (r_lo >> row) & 1 == 1 {
row_ll |= 1 << j;
}
if (r_lo >> (row + 8)) & 1 == 1 {
row_hl |= 1 << j;
}
let r_hi = gf.mul(1u16 << (j + 8), coeff);
if (r_hi >> row) & 1 == 1 {
row_lh |= 1 << j;
}
if (r_hi >> (row + 8)) & 1 == 1 {
row_hh |= 1 << j;
}
}
let shift = (7 - row) * 8;
m_ll |= (row_ll as u64) << shift;
m_lh |= (row_lh as u64) << shift;
m_hl |= (row_hl as u64) << shift;
m_hh |= (row_hh as u64) << shift;
}
let mat_lo = _mm256_set_epi64x(m_lh as i64, m_ll as i64, m_lh as i64, m_ll as i64);
let mat_hi = _mm256_set_epi64x(m_hh as i64, m_hl as i64, m_hh as i64, m_hl as i64);
let mut table_low = [0u16; 256];
let mut table_high = [0u16; 256];
for b in 0..=255usize {
table_low[b] = gf.mul(b as u16, coeff);
table_high[b] = gf.mul((b as u16) << 8, coeff);
}
(mat_lo, mat_hi, table_low, table_high)
})
.collect();
// 32 KiB recovery buffer chunk: chunk × group fits L1/L2 and amortizes
// the rayon task overhead. 8 × 32 KiB = 256 KiB stays in L2 on most
// modern CPUs; if L2 is smaller (older Skylake clients) the hardware
// prefetcher compensates adequately.
let chunk_size = 16384usize;
buffers
.par_chunks_mut(8)
.enumerate()
.for_each(|(group_idx, buf_group)| {
let i = group_idx * 8;
match buf_group {
// 8-way unroll: one input load + one deinterleave feeds 8 recovery blocks,
// halving the loadu/shuffle overhead compared with the 4-way arm.
[buf_a, buf_b, buf_c, buf_d, buf_e, buf_f, buf_g, buf_h] => {
let base_a = i * n_queued;
let base_b = (i + 1) * n_queued;
let base_c = (i + 2) * n_queued;
let base_d = (i + 3) * n_queued;
let base_e = (i + 4) * n_queued;
let base_f = (i + 5) * n_queued;
let base_g = (i + 6) * n_queued;
let base_h = (i + 7) * n_queued;
buf_a
.par_chunks_mut(chunk_size)
.zip(buf_b.par_chunks_mut(chunk_size))
.zip(buf_c.par_chunks_mut(chunk_size))
.zip(buf_d.par_chunks_mut(chunk_size))
.zip(buf_e.par_chunks_mut(chunk_size))
.zip(buf_f.par_chunks_mut(chunk_size))
.zip(buf_g.par_chunks_mut(chunk_size))
.zip(buf_h.par_chunks_mut(chunk_size))
.enumerate()
.for_each(
|(
chunk_idx,
(
(
(
((((chunk_a, chunk_b), chunk_c), chunk_d), chunk_e),
chunk_f,
),
chunk_g,
),
chunk_h,
),
)| unsafe {
let byte_offset = chunk_idx * chunk_size * 2;
let byte_len = chunk_a.len() * 2;
let blocks_32 = byte_len / 32;
let remainder = byte_len % 32;
for q_idx in 0..n_queued {
let (mat_lo_a, mat_hi_a, ref tlow_a, ref thigh_a) =
all_tables[base_a + q_idx];
let (mat_lo_b, mat_hi_b, ref tlow_b, ref thigh_b) =
all_tables[base_b + q_idx];
let (mat_lo_c, mat_hi_c, ref tlow_c, ref thigh_c) =
all_tables[base_c + q_idx];
let (mat_lo_d, mat_hi_d, ref tlow_d, ref thigh_d) =
all_tables[base_d + q_idx];
let (mat_lo_e, mat_hi_e, ref tlow_e, ref thigh_e) =
all_tables[base_e + q_idx];
let (mat_lo_f, mat_hi_f, ref tlow_f, ref thigh_f) =
all_tables[base_f + q_idx];
let (mat_lo_g, mat_hi_g, ref tlow_g, ref thigh_g) =
all_tables[base_g + q_idx];
let (mat_lo_h, mat_hi_h, ref tlow_h, ref thigh_h) =
all_tables[base_h + q_idx];
let slice_chunk =
&queued[q_idx][byte_offset..byte_offset + byte_len];
let mut ptr_in = slice_chunk.as_ptr() as *const __m256i;
let mut ptr_a = chunk_a.as_mut_ptr() as *mut __m256i;
let mut ptr_b = chunk_b.as_mut_ptr() as *mut __m256i;
let mut ptr_c = chunk_c.as_mut_ptr() as *mut __m256i;
let mut ptr_d = chunk_d.as_mut_ptr() as *mut __m256i;
let mut ptr_e = chunk_e.as_mut_ptr() as *mut __m256i;
let mut ptr_f = chunk_f.as_mut_ptr() as *mut __m256i;
let mut ptr_g = chunk_g.as_mut_ptr() as *mut __m256i;
let mut ptr_h = chunk_h.as_mut_ptr() as *mut __m256i;
let end = ptr_in.add(blocks_32);
while ptr_in < end {
_mm_prefetch(ptr_in.add(4) as *const i8, _MM_HINT_T0);
let input = _mm256_loadu_si256(ptr_in);
let separated = _mm256_shuffle_epi8(input, deint_mask);
macro_rules! gfni_block {
($mat_lo:expr, $mat_hi:expr, $ptr:expr) => {{
let vlo = _mm256_gf2p8affine_epi64_epi8(
separated, $mat_lo, 0,
);
let vhi = _mm256_gf2p8affine_epi64_epi8(
separated, $mat_hi, 0,
);
let out = _mm256_unpacklo_epi8(
_mm256_xor_si256(
vlo,
_mm256_bsrli_epi128::<8>(vlo),
),
_mm256_xor_si256(
vhi,
_mm256_bsrli_epi128::<8>(vhi),
),
);
_mm256_storeu_si256(
$ptr,
_mm256_xor_si256(
_mm256_loadu_si256($ptr),
out,
),
);
}};
}
gfni_block!(mat_lo_a, mat_hi_a, ptr_a);
gfni_block!(mat_lo_b, mat_hi_b, ptr_b);
gfni_block!(mat_lo_c, mat_hi_c, ptr_c);
gfni_block!(mat_lo_d, mat_hi_d, ptr_d);
gfni_block!(mat_lo_e, mat_hi_e, ptr_e);
gfni_block!(mat_lo_f, mat_hi_f, ptr_f);
gfni_block!(mat_lo_g, mat_hi_g, ptr_g);
gfni_block!(mat_lo_h, mat_hi_h, ptr_h);
ptr_in = ptr_in.add(1);
ptr_a = ptr_a.add(1);
ptr_b = ptr_b.add(1);
ptr_c = ptr_c.add(1);
ptr_d = ptr_d.add(1);
ptr_e = ptr_e.add(1);
ptr_f = ptr_f.add(1);
ptr_g = ptr_g.add(1);
ptr_h = ptr_h.add(1);
}
if remainder > 0 {
let ow = blocks_32 * 16;
let mut pw_a = chunk_a[ow..].as_mut_ptr();
let mut pw_b = chunk_b[ow..].as_mut_ptr();
let mut pw_c = chunk_c[ow..].as_mut_ptr();
let mut pw_d = chunk_d[ow..].as_mut_ptr();
let mut pw_e = chunk_e[ow..].as_mut_ptr();
let mut pw_f = chunk_f[ow..].as_mut_ptr();
let mut pw_g = chunk_g[ow..].as_mut_ptr();
let mut pw_h = chunk_h[ow..].as_mut_ptr();
let mut p_in = slice_chunk[blocks_32 * 32..].as_ptr();
let tail_end = p_in.add(remainder);
while p_in < tail_end {
let lo = *p_in as usize;
let hi = *p_in.add(1) as usize;
*pw_a ^= tlow_a[lo] ^ thigh_a[hi];
*pw_b ^= tlow_b[lo] ^ thigh_b[hi];
*pw_c ^= tlow_c[lo] ^ thigh_c[hi];
*pw_d ^= tlow_d[lo] ^ thigh_d[hi];
*pw_e ^= tlow_e[lo] ^ thigh_e[hi];
*pw_f ^= tlow_f[lo] ^ thigh_f[hi];
*pw_g ^= tlow_g[lo] ^ thigh_g[hi];
*pw_h ^= tlow_h[lo] ^ thigh_h[hi];
pw_a = pw_a.add(1);
pw_b = pw_b.add(1);
pw_c = pw_c.add(1);
pw_d = pw_d.add(1);
pw_e = pw_e.add(1);
pw_f = pw_f.add(1);
pw_g = pw_g.add(1);
pw_h = pw_h.add(1);
p_in = p_in.add(2);
}
}
}
},
);
}
[buf_a, buf_b, buf_c, buf_d] => {
let base_a = i * n_queued;
let base_b = (i + 1) * n_queued;
let base_c = (i + 2) * n_queued;
let base_d = (i + 3) * n_queued;
buf_a
.par_chunks_mut(chunk_size)
.zip(buf_b.par_chunks_mut(chunk_size))
.zip(buf_c.par_chunks_mut(chunk_size))
.zip(buf_d.par_chunks_mut(chunk_size))
.enumerate()
.for_each(
|(chunk_idx, (((chunk_a, chunk_b), chunk_c), chunk_d))| unsafe {
let byte_offset = chunk_idx * chunk_size * 2;
let byte_len = chunk_a.len() * 2;
let blocks_32 = byte_len / 32;
let remainder = byte_len % 32;
for q_idx in 0..n_queued {
let (mat_lo_a, mat_hi_a, ref tlow_a, ref thigh_a) =
all_tables[base_a + q_idx];
let (mat_lo_b, mat_hi_b, ref tlow_b, ref thigh_b) =
all_tables[base_b + q_idx];
let (mat_lo_c, mat_hi_c, ref tlow_c, ref thigh_c) =
all_tables[base_c + q_idx];
let (mat_lo_d, mat_hi_d, ref tlow_d, ref thigh_d) =
all_tables[base_d + q_idx];
let slice_chunk =
&queued[q_idx][byte_offset..byte_offset + byte_len];
let mut ptr_in = slice_chunk.as_ptr() as *const __m256i;
let mut ptr_a = chunk_a.as_mut_ptr() as *mut __m256i;
let mut ptr_b = chunk_b.as_mut_ptr() as *mut __m256i;
let mut ptr_c = chunk_c.as_mut_ptr() as *mut __m256i;
let mut ptr_d = chunk_d.as_mut_ptr() as *mut __m256i;
let end = ptr_in.add(blocks_32);
while ptr_in < end {
_mm_prefetch(ptr_in.add(4) as *const i8, _MM_HINT_T0);
let input = _mm256_loadu_si256(ptr_in);
let separated = _mm256_shuffle_epi8(input, deint_mask);
// Block A
let vlo_a = _mm256_gf2p8affine_epi64_epi8(
separated, mat_lo_a, 0,
);
let new_lo_a = _mm256_xor_si256(
vlo_a,
_mm256_bsrli_epi128::<8>(vlo_a),
);
let vhi_a = _mm256_gf2p8affine_epi64_epi8(
separated, mat_hi_a, 0,
);
let new_hi_a = _mm256_xor_si256(
vhi_a,
_mm256_bsrli_epi128::<8>(vhi_a),
);
let out_a = _mm256_unpacklo_epi8(new_lo_a, new_hi_a);
_mm256_storeu_si256(
ptr_a,
_mm256_xor_si256(_mm256_loadu_si256(ptr_a), out_a),
);
// Block B
let vlo_b = _mm256_gf2p8affine_epi64_epi8(
separated, mat_lo_b, 0,
);
let new_lo_b = _mm256_xor_si256(
vlo_b,
_mm256_bsrli_epi128::<8>(vlo_b),
);
let vhi_b = _mm256_gf2p8affine_epi64_epi8(
separated, mat_hi_b, 0,
);
let new_hi_b = _mm256_xor_si256(
vhi_b,
_mm256_bsrli_epi128::<8>(vhi_b),
);
let out_b = _mm256_unpacklo_epi8(new_lo_b, new_hi_b);
_mm256_storeu_si256(
ptr_b,
_mm256_xor_si256(_mm256_loadu_si256(ptr_b), out_b),
);
// Block C
let vlo_c = _mm256_gf2p8affine_epi64_epi8(
separated, mat_lo_c, 0,
);
let new_lo_c = _mm256_xor_si256(
vlo_c,
_mm256_bsrli_epi128::<8>(vlo_c),
);
let vhi_c = _mm256_gf2p8affine_epi64_epi8(
separated, mat_hi_c, 0,
);
let new_hi_c = _mm256_xor_si256(
vhi_c,
_mm256_bsrli_epi128::<8>(vhi_c),
);
let out_c = _mm256_unpacklo_epi8(new_lo_c, new_hi_c);
_mm256_storeu_si256(
ptr_c,
_mm256_xor_si256(_mm256_loadu_si256(ptr_c), out_c),
);
// Block D
let vlo_d = _mm256_gf2p8affine_epi64_epi8(
separated, mat_lo_d, 0,
);
let new_lo_d = _mm256_xor_si256(
vlo_d,
_mm256_bsrli_epi128::<8>(vlo_d),
);
let vhi_d = _mm256_gf2p8affine_epi64_epi8(
separated, mat_hi_d, 0,
);
let new_hi_d = _mm256_xor_si256(
vhi_d,
_mm256_bsrli_epi128::<8>(vhi_d),
);
let out_d = _mm256_unpacklo_epi8(new_lo_d, new_hi_d);
_mm256_storeu_si256(
ptr_d,
_mm256_xor_si256(_mm256_loadu_si256(ptr_d), out_d),
);
ptr_in = ptr_in.add(1);
ptr_a = ptr_a.add(1);
ptr_b = ptr_b.add(1);
ptr_c = ptr_c.add(1);
ptr_d = ptr_d.add(1);
}
if remainder > 0 {
let ow = blocks_32 * 16;
let mut pw_a = chunk_a[ow..].as_mut_ptr();
let mut pw_b = chunk_b[ow..].as_mut_ptr();
let mut pw_c = chunk_c[ow..].as_mut_ptr();
let mut pw_d = chunk_d[ow..].as_mut_ptr();
let mut p_in = slice_chunk[blocks_32 * 32..].as_ptr();
let tail_end = p_in.add(remainder);
while p_in < tail_end {
let lo = *p_in as usize;
let hi = *p_in.add(1) as usize;
*pw_a ^= tlow_a[lo] ^ thigh_a[hi];
*pw_b ^= tlow_b[lo] ^ thigh_b[hi];
*pw_c ^= tlow_c[lo] ^ thigh_c[hi];
*pw_d ^= tlow_d[lo] ^ thigh_d[hi];
pw_a = pw_a.add(1);
pw_b = pw_b.add(1);
pw_c = pw_c.add(1);
pw_d = pw_d.add(1);
p_in = p_in.add(2);
}
}
}
},
);
}
[buf_a, buf_b] => {
let base_a = i * n_queued;
let base_b = (i + 1) * n_queued;
buf_a
.par_chunks_mut(chunk_size)
.zip(buf_b.par_chunks_mut(chunk_size))
.enumerate()
.for_each(|(chunk_idx, (chunk_a, chunk_b))| unsafe {
let byte_offset = chunk_idx * chunk_size * 2;
let byte_len = chunk_a.len() * 2;
let blocks_32 = byte_len / 32;
let remainder = byte_len % 32;
for q_idx in 0..n_queued {
let (mat_lo_a, mat_hi_a, ref tlow_a, ref thigh_a) =
all_tables[base_a + q_idx];
let (mat_lo_b, mat_hi_b, ref tlow_b, ref thigh_b) =
all_tables[base_b + q_idx];
let slice_chunk =
&queued[q_idx][byte_offset..byte_offset + byte_len];
let mut ptr_in = slice_chunk.as_ptr() as *const __m256i;
let mut ptr_a = chunk_a.as_mut_ptr() as *mut __m256i;
let mut ptr_b = chunk_b.as_mut_ptr() as *mut __m256i;
let end = ptr_in.add(blocks_32);
while ptr_in < end {
_mm_prefetch(ptr_in.add(4) as *const i8, _MM_HINT_T0);
let input = _mm256_loadu_si256(ptr_in);
let separated = _mm256_shuffle_epi8(input, deint_mask);
let vlo_a =
_mm256_gf2p8affine_epi64_epi8(separated, mat_lo_a, 0);
let new_lo_a = _mm256_xor_si256(
vlo_a,
_mm256_bsrli_epi128::<8>(vlo_a),
);
let vhi_a =
_mm256_gf2p8affine_epi64_epi8(separated, mat_hi_a, 0);
let new_hi_a = _mm256_xor_si256(
vhi_a,
_mm256_bsrli_epi128::<8>(vhi_a),
);
let out_a = _mm256_unpacklo_epi8(new_lo_a, new_hi_a);
_mm256_storeu_si256(
ptr_a,
_mm256_xor_si256(_mm256_loadu_si256(ptr_a), out_a),
);
let vlo_b =
_mm256_gf2p8affine_epi64_epi8(separated, mat_lo_b, 0);
let new_lo_b = _mm256_xor_si256(
vlo_b,
_mm256_bsrli_epi128::<8>(vlo_b),
);
let vhi_b =
_mm256_gf2p8affine_epi64_epi8(separated, mat_hi_b, 0);
let new_hi_b = _mm256_xor_si256(
vhi_b,
_mm256_bsrli_epi128::<8>(vhi_b),
);
let out_b = _mm256_unpacklo_epi8(new_lo_b, new_hi_b);
_mm256_storeu_si256(
ptr_b,
_mm256_xor_si256(_mm256_loadu_si256(ptr_b), out_b),
);
ptr_in = ptr_in.add(1);
ptr_a = ptr_a.add(1);
ptr_b = ptr_b.add(1);
}
if remainder > 0 {
let ow = blocks_32 * 16;
let mut pw_a = chunk_a[ow..].as_mut_ptr();
let mut pw_b = chunk_b[ow..].as_mut_ptr();
let mut p_in = slice_chunk[blocks_32 * 32..].as_ptr();
let tail_end = p_in.add(remainder);
while p_in < tail_end {
let lo = *p_in as usize;
let hi = *p_in.add(1) as usize;
*pw_a ^= tlow_a[lo] ^ thigh_a[hi];
*pw_b ^= tlow_b[lo] ^ thigh_b[hi];
pw_a = pw_a.add(1);
pw_b = pw_b.add(1);
p_in = p_in.add(2);
}
}
}
});
}
rest => {
for (j, buf) in rest.iter_mut().enumerate() {
let base = (i + j) * n_queued;
buf.par_chunks_mut(chunk_size).enumerate().for_each(
|(chunk_idx, chunk)| unsafe {
let byte_offset = chunk_idx * chunk_size * 2;
let byte_len = chunk.len() * 2;
let blocks_32 = byte_len / 32;
let remainder = byte_len % 32;
for q_idx in 0..n_queued {
let (mat_lo, mat_hi, ref tlow, ref thigh) =
all_tables[base + q_idx];
let slice_chunk =
&queued[q_idx][byte_offset..byte_offset + byte_len];
let mut ptr_buf = chunk.as_mut_ptr() as *mut __m256i;
let mut ptr_in = slice_chunk.as_ptr() as *const __m256i;
let end = ptr_in.add(blocks_32);
while ptr_in < end {
_mm_prefetch(ptr_in.add(4) as *const i8, _MM_HINT_T0);
let input = _mm256_loadu_si256(ptr_in);
let separated = _mm256_shuffle_epi8(input, deint_mask);
let vlo =
_mm256_gf2p8affine_epi64_epi8(separated, mat_lo, 0);
let vhi =
_mm256_gf2p8affine_epi64_epi8(separated, mat_hi, 0);
let out = _mm256_unpacklo_epi8(
_mm256_xor_si256(
vlo,
_mm256_bsrli_epi128::<8>(vlo),
),
_mm256_xor_si256(
vhi,
_mm256_bsrli_epi128::<8>(vhi),
),
);
_mm256_storeu_si256(
ptr_buf,
_mm256_xor_si256(_mm256_loadu_si256(ptr_buf), out),
);
ptr_in = ptr_in.add(1);
ptr_buf = ptr_buf.add(1);
}
if remainder > 0 {
let ow = blocks_32 * 16;
let mut pw = chunk[ow..].as_mut_ptr();
let mut p_in = slice_chunk[blocks_32 * 32..].as_ptr();
let tail_end = p_in.add(remainder);
while p_in < tail_end {
let lo = *p_in as usize;
let hi = *p_in.add(1) as usize;
*pw ^= tlow[lo] ^ thigh[hi];
pw = pw.add(1);
p_in = p_in.add(2);
}
}
}
},
);
}
}
}
});
}
/// GF(2^16) multiply-by-coefficient using AVX-512BW + GFNI instructions.
///
/// The `vgf2p8affineqb` instruction applies an 8×8 GF(2) matrix to each input
/// byte in a single cycle. Any GF(2^16) multiply-by-constant is a GF(2)-linear
/// map on 16 bits, which decomposes into four 8×8 matrices (one per pair of
/// input/output byte halves). Processing 512-bit vectors yields 32 GF(2^16)
/// words per loop iteration — roughly twice the AVX2 throughput.
///
/// Inner-loop layout (per 512-bit iteration):
/// 1. De-interleave bytes within each 128-bit lane so lo bytes occupy the
/// low qword and hi bytes the high qword.
/// 2. Apply two GFNI affine transforms (mat_lo, mat_hi) — each call covers
/// both the M_ll/M_lh or M_hl/M_hh matrices simultaneously by placing
/// different matrices in the two qwords of each lane.
/// 3. Fold the two qword results within each lane (bsrli + xor) to produce
/// the combined lo and hi result bytes.
/// 4. Re-interleave with `vunpcklbw` and XOR into the recovery buffer.
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx512f,avx512bw,gfni")]
pub(super) unsafe fn flush_avx512_gfni(&mut self) {
let start_index = self.next_index;
let queued = std::mem::take(&mut self.queued_slices);
self.next_index += queued.len();
let new_cs: Vec<SliceChecksum> = if self.compute_checksums {
let buffers = self.buffers.as_normal_mut();
let logbases = &self.logbases;
let exponent_start = self.exponent_start;
let gf = &self.gf;
let ((), cs) = rayon::join(
|| unsafe {
Self::flush_avx512_gfni_work(
buffers,
&queued,
start_index,
logbases,
exponent_start,
gf,
)
},
|| queued.par_iter().map(|s| slice_checksum(s)).collect(),
);
cs
} else {
unsafe {
Self::flush_avx512_gfni_work(
self.buffers.as_normal_mut(),
&queued,
start_index,
&self.logbases,
self.exponent_start,
&self.gf,
);
}
Vec::new()
};
self.pending_checksums.extend(new_cs);
self.recycle_queue(queued);
}
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx512f,avx512bw,gfni")]
pub(super) unsafe fn flush_avx512_gfni_work(
buffers: &mut [Vec<u16>],
queued: &[Vec<u8>],
start_index: usize,
logbases: &[u32],
exponent_start: u32,
gf: &Gf16,
) {
use std::arch::x86_64::*;
// Broadcast the de-interleave shuffle to all four 128-bit lanes:
// within each lane, move lo bytes (even positions 0,2,…,14) to the low
// qword (positions 0..7) and hi bytes (odd positions 1,3,…,15) to the
// high qword (positions 8..15). This lets us apply different GFNI
// matrices to lo vs hi bytes in a single vgf2p8affineqb call.
let deint_mask = _mm512_broadcast_i32x4(_mm_setr_epi8(
0, 2, 4, 6, 8, 10, 12, 14, // lo bytes of 8 words → positions 0..7
1, 3, 5, 7, 9, 11, 13, 15, // hi bytes of 8 words → positions 8..15
));
let n_rec = buffers.len();
let n_queued = queued.len();
// Pre-build all coefficient tables in a single parallel pass.
// Layout: all_tables[rec * n_queued + q_idx].
let all_tables: Vec<Avx512GfniTable> = (0..n_rec * n_queued)
.into_par_iter()
.map(|flat| {
let i = flat / n_queued;
let q_idx = flat % n_queued;
let exponent = exponent_start + i as u32;
let logbase = logbases[start_index + q_idx] as u64;
let log_coeff = ((logbase * exponent as u64) % ORDER as u64) as u32;
let coeff = gf.exp(log_coeff);
// Decompose GF(2^16) multiply-by-coeff into four 8×8 GF(2) matrices.
// For each 16-bit word w = (lo_byte, hi_byte):
// result_lo = M_ll * lo ^ M_lh * hi
// result_hi = M_hl * lo ^ M_hh * hi
//
// gf2p8affineqb uses byte (7-row) of the u64 matrix operand for
// output bit `row` — so store each row at position (7-row)*8.
let mut m_ll = 0u64; // lo input byte → lo output byte
let mut m_lh = 0u64; // hi input byte → lo output byte
let mut m_hl = 0u64; // lo input byte → hi output byte
let mut m_hh = 0u64; // hi input byte → hi output byte
for row in 0..8usize {
let mut row_ll = 0u8;
let mut row_lh = 0u8;
let mut row_hl = 0u8;
let mut row_hh = 0u8;
for j in 0..8usize {
let r_lo = gf.mul(1u16 << j, coeff);
if (r_lo >> row) & 1 == 1 {
row_ll |= 1 << j;
}
if (r_lo >> (row + 8)) & 1 == 1 {
row_hl |= 1 << j;
}
let r_hi = gf.mul(1u16 << (j + 8), coeff);
if (r_hi >> row) & 1 == 1 {
row_lh |= 1 << j;
}
if (r_hi >> (row + 8)) & 1 == 1 {
row_hh |= 1 << j;
}
}
let shift = (7 - row) * 8;
m_ll |= (row_ll as u64) << shift;
m_lh |= (row_lh as u64) << shift;
m_hl |= (row_hl as u64) << shift;
m_hh |= (row_hh as u64) << shift;
}
// Each 128-bit lane has two qwords: the low qword handles lo bytes
// (positions 0..7 after de-interleave) and the high qword handles hi
// bytes (positions 8..15). Alternating the two matrices in adjacent
// qwords lets one vgf2p8affineqb cover both contributions at once.
// _mm512_set_epi64 takes arguments from high (e7) to low (e0).
let mat_lo = _mm512_set_epi64(
m_lh as i64,
m_ll as i64, // lane 3: hi→lo, lo→lo
m_lh as i64,
m_ll as i64, // lane 2
m_lh as i64,
m_ll as i64, // lane 1
m_lh as i64,
m_ll as i64, // lane 0
);
let mat_hi = _mm512_set_epi64(
m_hh as i64,
m_hl as i64, // lane 3: hi→hi, lo→hi
m_hh as i64,
m_hl as i64, // lane 2
m_hh as i64,
m_hl as i64, // lane 1
m_hh as i64,
m_hl as i64, // lane 0
);
let mut table_low = [0u16; 256];
let mut table_high = [0u16; 256];
for b in 0..=255usize {
table_low[b] = gf.mul(b as u16, coeff);
table_high[b] = gf.mul((b as u16) << 8, coeff);
}
(mat_lo, mat_hi, table_low, table_high)
})
.collect();
// 2D parallel loop: outer = recovery block groups, inner = 32 KiB chunks of
// each recovery buffer. 4 × 32 KiB = 128 KiB fits comfortably in L2 on all
// current AVX-512 CPUs (512 KB L2 on Ice Lake/Sapphire Rapids).
let chunk_size = 16384usize;
buffers
.par_chunks_mut(4)
.enumerate()
.for_each(|(group_idx, buf_group)| {
let i = group_idx * 4;
match buf_group {
// 4-way: one input load + deinterleave feeds 4 recovery blocks.
[buf_a, buf_b, buf_c, buf_d] => {
let base_a = i * n_queued;
let base_b = (i + 1) * n_queued;
let base_c = (i + 2) * n_queued;
let base_d = (i + 3) * n_queued;
buf_a
.par_chunks_mut(chunk_size)
.zip(buf_b.par_chunks_mut(chunk_size))
.zip(buf_c.par_chunks_mut(chunk_size))
.zip(buf_d.par_chunks_mut(chunk_size))
.enumerate()
.for_each(
|(chunk_idx, (((chunk_a, chunk_b), chunk_c), chunk_d))| unsafe {
let byte_offset = chunk_idx * chunk_size * 2;
let byte_len = chunk_a.len() * 2;
let blocks_64 = byte_len / 64;
let remainder = byte_len % 64;
for q_idx in 0..n_queued {
let (mat_lo_a, mat_hi_a, ref tlow_a, ref thigh_a) =
all_tables[base_a + q_idx];
let (mat_lo_b, mat_hi_b, ref tlow_b, ref thigh_b) =
all_tables[base_b + q_idx];
let (mat_lo_c, mat_hi_c, ref tlow_c, ref thigh_c) =
all_tables[base_c + q_idx];
let (mat_lo_d, mat_hi_d, ref tlow_d, ref thigh_d) =
all_tables[base_d + q_idx];
let slice_chunk =
&queued[q_idx][byte_offset..byte_offset + byte_len];
let mut ptr_in = slice_chunk.as_ptr() as *const __m512i;
let mut ptr_a = chunk_a.as_mut_ptr() as *mut __m512i;
let mut ptr_b = chunk_b.as_mut_ptr() as *mut __m512i;
let mut ptr_c = chunk_c.as_mut_ptr() as *mut __m512i;
let mut ptr_d = chunk_d.as_mut_ptr() as *mut __m512i;
let end = ptr_in.add(blocks_64);
while ptr_in < end {
let input = _mm512_loadu_si512(ptr_in.cast());
let separated = _mm512_shuffle_epi8(input, deint_mask);
macro_rules! gfni512_block {
($mat_lo:expr, $mat_hi:expr, $ptr:expr) => {{
let vlo = _mm512_gf2p8affine_epi64_epi8(
separated, $mat_lo, 0,
);
let vhi = _mm512_gf2p8affine_epi64_epi8(
separated, $mat_hi, 0,
);
let out = _mm512_unpacklo_epi8(
_mm512_xor_si512(
vlo,
_mm512_bsrli_epi128::<8>(vlo),
),
_mm512_xor_si512(
vhi,
_mm512_bsrli_epi128::<8>(vhi),
),
);
_mm512_storeu_si512(
($ptr as *mut __m512i).cast(),
_mm512_xor_si512(
_mm512_loadu_si512(
($ptr as *const __m512i).cast(),
),
out,
),
);
}};
}
gfni512_block!(mat_lo_a, mat_hi_a, ptr_a);
gfni512_block!(mat_lo_b, mat_hi_b, ptr_b);
gfni512_block!(mat_lo_c, mat_hi_c, ptr_c);
gfni512_block!(mat_lo_d, mat_hi_d, ptr_d);
ptr_in = ptr_in.add(1);
ptr_a = ptr_a.add(1);
ptr_b = ptr_b.add(1);
ptr_c = ptr_c.add(1);
ptr_d = ptr_d.add(1);
}
if remainder > 0 {
let ow = blocks_64 * 32;
let mut pw_a = chunk_a[ow..].as_mut_ptr();
let mut pw_b = chunk_b[ow..].as_mut_ptr();
let mut pw_c = chunk_c[ow..].as_mut_ptr();
let mut pw_d = chunk_d[ow..].as_mut_ptr();
let mut p_in = slice_chunk[blocks_64 * 64..].as_ptr();
let tail_end = p_in.add(remainder);
while p_in < tail_end {
let lo = *p_in as usize;
let hi = *p_in.add(1) as usize;
*pw_a ^= tlow_a[lo] ^ thigh_a[hi];
*pw_b ^= tlow_b[lo] ^ thigh_b[hi];
*pw_c ^= tlow_c[lo] ^ thigh_c[hi];
*pw_d ^= tlow_d[lo] ^ thigh_d[hi];
pw_a = pw_a.add(1);
pw_b = pw_b.add(1);
pw_c = pw_c.add(1);
pw_d = pw_d.add(1);
p_in = p_in.add(2);
}
}
}
},
);
}
[buf_a, buf_b] => {
let base_a = i * n_queued;
let base_b = (i + 1) * n_queued;
buf_a
.par_chunks_mut(chunk_size)
.zip(buf_b.par_chunks_mut(chunk_size))
.enumerate()
.for_each(|(chunk_idx, (chunk_a, chunk_b))| unsafe {
let byte_offset = chunk_idx * chunk_size * 2;
let byte_len = chunk_a.len() * 2;
let blocks_64 = byte_len / 64;
let remainder = byte_len % 64;
for q_idx in 0..n_queued {
let (mat_lo_a, mat_hi_a, ref tlow_a, ref thigh_a) =
all_tables[base_a + q_idx];
let (mat_lo_b, mat_hi_b, ref tlow_b, ref thigh_b) =
all_tables[base_b + q_idx];
let slice_chunk =
&queued[q_idx][byte_offset..byte_offset + byte_len];
let mut ptr_in = slice_chunk.as_ptr() as *const __m512i;
let mut ptr_a = chunk_a.as_mut_ptr() as *mut __m512i;
let mut ptr_b = chunk_b.as_mut_ptr() as *mut __m512i;
let end = ptr_in.add(blocks_64);
while ptr_in < end {
let input = _mm512_loadu_si512(ptr_in.cast());
let separated = _mm512_shuffle_epi8(input, deint_mask);
// Block A
let vlo_a =
_mm512_gf2p8affine_epi64_epi8(separated, mat_lo_a, 0);
let new_lo_a = _mm512_xor_si512(
vlo_a,
_mm512_bsrli_epi128::<8>(vlo_a),
);
let vhi_a =
_mm512_gf2p8affine_epi64_epi8(separated, mat_hi_a, 0);
let new_hi_a = _mm512_xor_si512(
vhi_a,
_mm512_bsrli_epi128::<8>(vhi_a),
);
let out_a = _mm512_unpacklo_epi8(new_lo_a, new_hi_a);
let prev_a = _mm512_loadu_si512(ptr_a.cast());
_mm512_storeu_si512(
ptr_a.cast(),
_mm512_xor_si512(prev_a, out_a),
);
// Block B — reuses `separated`
let vlo_b =
_mm512_gf2p8affine_epi64_epi8(separated, mat_lo_b, 0);
let new_lo_b = _mm512_xor_si512(
vlo_b,
_mm512_bsrli_epi128::<8>(vlo_b),
);
let vhi_b =
_mm512_gf2p8affine_epi64_epi8(separated, mat_hi_b, 0);
let new_hi_b = _mm512_xor_si512(
vhi_b,
_mm512_bsrli_epi128::<8>(vhi_b),
);
let out_b = _mm512_unpacklo_epi8(new_lo_b, new_hi_b);
let prev_b = _mm512_loadu_si512(ptr_b.cast());
_mm512_storeu_si512(
ptr_b.cast(),
_mm512_xor_si512(prev_b, out_b),
);
ptr_in = ptr_in.add(1);
ptr_a = ptr_a.add(1);
ptr_b = ptr_b.add(1);
}
if remainder > 0 {
let ow = blocks_64 * 32;
let mut pw_a = chunk_a[ow..].as_mut_ptr();
let mut pw_b = chunk_b[ow..].as_mut_ptr();
let mut p_in = slice_chunk[blocks_64 * 64..].as_ptr();
let tail_end = p_in.add(remainder);
while p_in < tail_end {
let lo = *p_in as usize;
let hi = *p_in.add(1) as usize;
*pw_a ^= tlow_a[lo] ^ thigh_a[hi];
*pw_b ^= tlow_b[lo] ^ thigh_b[hi];
pw_a = pw_a.add(1);
pw_b = pw_b.add(1);
p_in = p_in.add(2);
}
}
}
});
}
// Fallback for 1 or 3 remaining buffers (2 is handled
// above). Previously only `[buf_a]` (exactly 1) was
// matched here, with everything else — crucially, a
// remainder of *3*, which is exactly what
// `par_chunks_mut(4)` leaves whenever `recovery_count %
// 4 == 3` — silently falling through to an empty `_ =>
// {}` arm. Those recovery blocks were never written to
// at all, left as zero-initialized `buffers` entries
// instead of real recovery data: this was the root
// cause of issue #51's "silent data corruption" (the
// decoder faithfully reconstructing from all-zero
// recovery blocks). See the issue for the investigation
// history — this is what every local repro attempt
// couldn't catch, since it only reproduces on hardware
// with AVX-512+GFNI, which none of those attempts had.
rest => {
for (j, buf_a) in rest.iter_mut().enumerate() {
let base = (i + j) * n_queued;
buf_a.par_chunks_mut(chunk_size).enumerate().for_each(
|(chunk_idx, chunk_a)| unsafe {
let byte_offset = chunk_idx * chunk_size * 2;
let byte_len = chunk_a.len() * 2;
let blocks_64 = byte_len / 64;
let remainder = byte_len % 64;
for q_idx in 0..n_queued {
let (mat_lo, mat_hi, ref table_low, ref table_high) =
all_tables[base + q_idx];
let slice_chunk =
&queued[q_idx][byte_offset..byte_offset + byte_len];
let mut ptr_buf = chunk_a.as_mut_ptr() as *mut __m512i;
let mut ptr_in = slice_chunk.as_ptr() as *const __m512i;
let end = ptr_in.add(blocks_64);
while ptr_in < end {
let input = _mm512_loadu_si512(ptr_in.cast());
let separated = _mm512_shuffle_epi8(input, deint_mask);
let v_lo =
_mm512_gf2p8affine_epi64_epi8(separated, mat_lo, 0);
let new_lo = _mm512_xor_si512(
v_lo,
_mm512_bsrli_epi128::<8>(v_lo),
);
let v_hi =
_mm512_gf2p8affine_epi64_epi8(separated, mat_hi, 0);
let new_hi = _mm512_xor_si512(
v_hi,
_mm512_bsrli_epi128::<8>(v_hi),
);
let out = _mm512_unpacklo_epi8(new_lo, new_hi);
let prev = _mm512_loadu_si512(ptr_buf.cast());
_mm512_storeu_si512(
ptr_buf.cast(),
_mm512_xor_si512(prev, out),
);
ptr_in = ptr_in.add(1);
ptr_buf = ptr_buf.add(1);
}
if remainder > 0 {
let ow = blocks_64 * 32;
let mut pw = chunk_a[ow..].as_mut_ptr();
let mut p_in = slice_chunk[blocks_64 * 64..].as_ptr();
let tail_end = p_in.add(remainder);
while p_in < tail_end {
let lo = *p_in as usize;
let hi = *p_in.add(1) as usize;
*pw ^= table_low[lo] ^ table_high[hi];
pw = pw.add(1);
p_in = p_in.add(2);
}
}
}
},
);
}
}
}
});
}
}