parmesan-par2 0.6.0

Fast, standalone PAR2 (Reed-Solomon) creation, verification and repair tool
Documentation
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
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
use super::*;

impl RecoveryEncoder {
    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "avx2")]
    pub(super) unsafe fn flush_avx2(&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_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")]
    pub(super) unsafe fn flush_avx2_work(
        buffers: &mut [Vec<u16>],
        queued: &[Vec<u8>],
        start_index: usize,
        logbases: &[u32],
        exponent_start: u32,
        gf: &Gf16,
    ) {
        let mask_f = _mm256_set1_epi8(0x0F);
        let mask_even = _mm256_set1_epi16(0x00FF);

        let n_rec = buffers.len();
        let n_queued = queued.len();

        // Pre-build all SIMD coefficient tables in a single parallel pass — one Vec
        // entry per (recovery_block × input_slice) pair, laid out as [rec * n_queued + q].
        //
        // Building tables outside the chunk loop means they are computed once per flush
        // rather than once per (flush × chunk). The chunk loop below can then reference
        // pre-built tables without any GF-table lookups in the hot path.
        //
        // __m256i is Send+Sync (it is [i64; 4] under the hood) so storing it in a Vec
        // that is shared across rayon tasks is safe.
        let all_tables: Vec<Avx2Table> = (0..n_rec * n_queued)
            .into_par_iter()
            .map(|flat| unsafe {
                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);

                let mut tl_l = [0u8; 16];
                let mut tl_h = [0u8; 16];
                let mut th_l = [0u8; 16];
                let mut th_h = [0u8; 16];
                let mut hl_l = [0u8; 16];
                let mut hl_h = [0u8; 16];
                let mut hh_l = [0u8; 16];
                let mut hh_h = [0u8; 16];

                for val in 0..16usize {
                    let r0 = gf.mul(val as u16, coeff);
                    tl_l[val] = (r0 & 0xFF) as u8;
                    th_l[val] = (r0 >> 8) as u8;
                    let r1 = gf.mul((val as u16) << 4, coeff);
                    tl_h[val] = (r1 & 0xFF) as u8;
                    th_h[val] = (r1 >> 8) as u8;
                    let r2 = gf.mul((val as u16) << 8, coeff);
                    hl_l[val] = (r2 & 0xFF) as u8;
                    hh_l[val] = (r2 >> 8) as u8;
                    let r3 = gf.mul((val as u16) << 12, coeff);
                    hl_h[val] = (r3 & 0xFF) as u8;
                    hh_h[val] = (r3 >> 8) as u8;
                }

                let v_tl_l =
                    _mm256_broadcastsi128_si256(_mm_loadu_si128(tl_l.as_ptr() as *const __m128i));
                let v_tl_h =
                    _mm256_broadcastsi128_si256(_mm_loadu_si128(tl_h.as_ptr() as *const __m128i));
                let v_th_l =
                    _mm256_broadcastsi128_si256(_mm_loadu_si128(th_l.as_ptr() as *const __m128i));
                let v_th_h =
                    _mm256_broadcastsi128_si256(_mm_loadu_si128(th_h.as_ptr() as *const __m128i));
                let v_hl_l =
                    _mm256_broadcastsi128_si256(_mm_loadu_si128(hl_l.as_ptr() as *const __m128i));
                let v_hl_h =
                    _mm256_broadcastsi128_si256(_mm_loadu_si128(hl_h.as_ptr() as *const __m128i));
                let v_hh_l =
                    _mm256_broadcastsi128_si256(_mm_loadu_si128(hh_l.as_ptr() as *const __m128i));
                let v_hh_h =
                    _mm256_broadcastsi128_si256(_mm_loadu_si128(hh_h.as_ptr() as *const __m128i));

                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);
                }

                (
                    v_tl_l, v_tl_h, v_th_l, v_th_h, v_hl_l, v_hl_h, v_hh_l, v_hh_h, table_low,
                    table_high,
                )
            })
            .collect();

        // 2D parallel loop: outer dimension = recovery block pairs (91 tasks for 183
        // blocks), inner dimension = 32 KiB chunks of each recovery buffer (960 chunks
        // for a 30 MiB slice). Total rayon tasks = 91 × 960 = ~87 K, saturating all
        // available cores instead of the previous 91-task ceiling.
        //
        // Each rayon task handles a group of consecutive recovery blocks (4× unrolling
        // over the recovery dimension). One input load + one nibble decomposition serves
        // all blocks in the group, halving the load and AND/SRL overhead per byte processed.
        let chunk_size = 16384usize; // 32 KiB recovery buffer chunk (see avx2_gfni A/B notes)

        buffers
            .par_chunks_mut(4)
            .enumerate()
            .for_each(|(group_idx, buf_group)| {
                let i = group_idx * 4;
                match buf_group {
                    [buf_a, buf_b, buf_c, buf_d] => {
                        // 4× unrolled: four recovery blocks share one input load.
                        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 (
                                            v_tl_l_a,
                                            v_tl_h_a,
                                            v_th_l_a,
                                            v_th_h_a,
                                            v_hl_l_a,
                                            v_hl_h_a,
                                            v_hh_l_a,
                                            v_hh_h_a,
                                            ref tlow_a,
                                            ref thigh_a,
                                        ) = all_tables[base_a + q_idx];
                                        let (
                                            v_tl_l_b,
                                            v_tl_h_b,
                                            v_th_l_b,
                                            v_th_h_b,
                                            v_hl_l_b,
                                            v_hl_h_b,
                                            v_hh_l_b,
                                            v_hh_h_b,
                                            ref tlow_b,
                                            ref thigh_b,
                                        ) = all_tables[base_b + q_idx];
                                        let (
                                            v_tl_l_c,
                                            v_tl_h_c,
                                            v_th_l_c,
                                            v_th_h_c,
                                            v_hl_l_c,
                                            v_hl_h_c,
                                            v_hh_l_c,
                                            v_hh_h_c,
                                            ref tlow_c,
                                            ref thigh_c,
                                        ) = all_tables[base_c + q_idx];
                                        let (
                                            v_tl_l_d,
                                            v_tl_h_d,
                                            v_th_l_d,
                                            v_th_h_d,
                                            v_hl_l_d,
                                            v_hl_h_d,
                                            v_hh_l_d,
                                            v_hh_h_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 n0_2 = _mm256_and_si256(input, mask_f);
                                            let n1_3 = _mm256_and_si256(
                                                _mm256_srli_epi16(input, 4),
                                                mask_f,
                                            );

                                            // Block A
                                            let rle_a = _mm256_xor_si256(
                                                _mm256_shuffle_epi8(v_tl_l_a, n0_2),
                                                _mm256_shuffle_epi8(v_tl_h_a, n1_3),
                                            );
                                            let rhe_a = _mm256_xor_si256(
                                                _mm256_shuffle_epi8(v_th_l_a, n0_2),
                                                _mm256_shuffle_epi8(v_th_h_a, n1_3),
                                            );
                                            let rlo_a = _mm256_xor_si256(
                                                _mm256_shuffle_epi8(v_hl_l_a, n0_2),
                                                _mm256_shuffle_epi8(v_hl_h_a, n1_3),
                                            );
                                            let rho_a = _mm256_xor_si256(
                                                _mm256_shuffle_epi8(v_hh_l_a, n0_2),
                                                _mm256_shuffle_epi8(v_hh_h_a, n1_3),
                                            );
                                            let out_a = _mm256_or_si256(
                                                _mm256_and_si256(
                                                    _mm256_xor_si256(
                                                        rle_a,
                                                        _mm256_srli_epi16(rlo_a, 8),
                                                    ),
                                                    mask_even,
                                                ),
                                                _mm256_slli_epi16(
                                                    _mm256_xor_si256(
                                                        rhe_a,
                                                        _mm256_srli_epi16(rho_a, 8),
                                                    ),
                                                    8,
                                                ),
                                            );
                                            _mm256_storeu_si256(
                                                ptr_a,
                                                _mm256_xor_si256(_mm256_loadu_si256(ptr_a), out_a),
                                            );

                                            // Block B
                                            let rle_b = _mm256_xor_si256(
                                                _mm256_shuffle_epi8(v_tl_l_b, n0_2),
                                                _mm256_shuffle_epi8(v_tl_h_b, n1_3),
                                            );
                                            let rhe_b = _mm256_xor_si256(
                                                _mm256_shuffle_epi8(v_th_l_b, n0_2),
                                                _mm256_shuffle_epi8(v_th_h_b, n1_3),
                                            );
                                            let rlo_b = _mm256_xor_si256(
                                                _mm256_shuffle_epi8(v_hl_l_b, n0_2),
                                                _mm256_shuffle_epi8(v_hl_h_b, n1_3),
                                            );
                                            let rho_b = _mm256_xor_si256(
                                                _mm256_shuffle_epi8(v_hh_l_b, n0_2),
                                                _mm256_shuffle_epi8(v_hh_h_b, n1_3),
                                            );
                                            let out_b = _mm256_or_si256(
                                                _mm256_and_si256(
                                                    _mm256_xor_si256(
                                                        rle_b,
                                                        _mm256_srli_epi16(rlo_b, 8),
                                                    ),
                                                    mask_even,
                                                ),
                                                _mm256_slli_epi16(
                                                    _mm256_xor_si256(
                                                        rhe_b,
                                                        _mm256_srli_epi16(rho_b, 8),
                                                    ),
                                                    8,
                                                ),
                                            );
                                            _mm256_storeu_si256(
                                                ptr_b,
                                                _mm256_xor_si256(_mm256_loadu_si256(ptr_b), out_b),
                                            );

                                            // Block C
                                            let rle_c = _mm256_xor_si256(
                                                _mm256_shuffle_epi8(v_tl_l_c, n0_2),
                                                _mm256_shuffle_epi8(v_tl_h_c, n1_3),
                                            );
                                            let rhe_c = _mm256_xor_si256(
                                                _mm256_shuffle_epi8(v_th_l_c, n0_2),
                                                _mm256_shuffle_epi8(v_th_h_c, n1_3),
                                            );
                                            let rlo_c = _mm256_xor_si256(
                                                _mm256_shuffle_epi8(v_hl_l_c, n0_2),
                                                _mm256_shuffle_epi8(v_hl_h_c, n1_3),
                                            );
                                            let rho_c = _mm256_xor_si256(
                                                _mm256_shuffle_epi8(v_hh_l_c, n0_2),
                                                _mm256_shuffle_epi8(v_hh_h_c, n1_3),
                                            );
                                            let out_c = _mm256_or_si256(
                                                _mm256_and_si256(
                                                    _mm256_xor_si256(
                                                        rle_c,
                                                        _mm256_srli_epi16(rlo_c, 8),
                                                    ),
                                                    mask_even,
                                                ),
                                                _mm256_slli_epi16(
                                                    _mm256_xor_si256(
                                                        rhe_c,
                                                        _mm256_srli_epi16(rho_c, 8),
                                                    ),
                                                    8,
                                                ),
                                            );
                                            _mm256_storeu_si256(
                                                ptr_c,
                                                _mm256_xor_si256(_mm256_loadu_si256(ptr_c), out_c),
                                            );

                                            // Block D
                                            let rle_d = _mm256_xor_si256(
                                                _mm256_shuffle_epi8(v_tl_l_d, n0_2),
                                                _mm256_shuffle_epi8(v_tl_h_d, n1_3),
                                            );
                                            let rhe_d = _mm256_xor_si256(
                                                _mm256_shuffle_epi8(v_th_l_d, n0_2),
                                                _mm256_shuffle_epi8(v_th_h_d, n1_3),
                                            );
                                            let rlo_d = _mm256_xor_si256(
                                                _mm256_shuffle_epi8(v_hl_l_d, n0_2),
                                                _mm256_shuffle_epi8(v_hl_h_d, n1_3),
                                            );
                                            let rho_d = _mm256_xor_si256(
                                                _mm256_shuffle_epi8(v_hh_l_d, n0_2),
                                                _mm256_shuffle_epi8(v_hh_h_d, n1_3),
                                            );
                                            let out_d = _mm256_or_si256(
                                                _mm256_and_si256(
                                                    _mm256_xor_si256(
                                                        rle_d,
                                                        _mm256_srli_epi16(rlo_d, 8),
                                                    ),
                                                    mask_even,
                                                ),
                                                _mm256_slli_epi16(
                                                    _mm256_xor_si256(
                                                        rhe_d,
                                                        _mm256_srli_epi16(rho_d, 8),
                                                    ),
                                                    8,
                                                ),
                                            );
                                            _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] => {
                        // Fallback for 2 blocks (remains 2× unrolled).
                        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 (
                                        v_tl_l_a,
                                        v_tl_h_a,
                                        v_th_l_a,
                                        v_th_h_a,
                                        v_hl_l_a,
                                        v_hl_h_a,
                                        v_hh_l_a,
                                        v_hh_h_a,
                                        ref tlow_a,
                                        ref thigh_a,
                                    ) = all_tables[base_a + q_idx];
                                    let (
                                        v_tl_l_b,
                                        v_tl_h_b,
                                        v_th_l_b,
                                        v_th_h_b,
                                        v_hl_l_b,
                                        v_hl_h_b,
                                        v_hh_l_b,
                                        v_hh_h_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 n0_2 = _mm256_and_si256(input, mask_f);
                                        let n1_3 =
                                            _mm256_and_si256(_mm256_srli_epi16(input, 4), mask_f);

                                        let rle_a = _mm256_xor_si256(
                                            _mm256_shuffle_epi8(v_tl_l_a, n0_2),
                                            _mm256_shuffle_epi8(v_tl_h_a, n1_3),
                                        );
                                        let rhe_a = _mm256_xor_si256(
                                            _mm256_shuffle_epi8(v_th_l_a, n0_2),
                                            _mm256_shuffle_epi8(v_th_h_a, n1_3),
                                        );
                                        let rlo_a = _mm256_xor_si256(
                                            _mm256_shuffle_epi8(v_hl_l_a, n0_2),
                                            _mm256_shuffle_epi8(v_hl_h_a, n1_3),
                                        );
                                        let rho_a = _mm256_xor_si256(
                                            _mm256_shuffle_epi8(v_hh_l_a, n0_2),
                                            _mm256_shuffle_epi8(v_hh_h_a, n1_3),
                                        );
                                        let out_a = _mm256_or_si256(
                                            _mm256_and_si256(
                                                _mm256_xor_si256(
                                                    rle_a,
                                                    _mm256_srli_epi16(rlo_a, 8),
                                                ),
                                                mask_even,
                                            ),
                                            _mm256_slli_epi16(
                                                _mm256_xor_si256(
                                                    rhe_a,
                                                    _mm256_srli_epi16(rho_a, 8),
                                                ),
                                                8,
                                            ),
                                        );
                                        _mm256_storeu_si256(
                                            ptr_a,
                                            _mm256_xor_si256(_mm256_loadu_si256(ptr_a), out_a),
                                        );

                                        let rle_b = _mm256_xor_si256(
                                            _mm256_shuffle_epi8(v_tl_l_b, n0_2),
                                            _mm256_shuffle_epi8(v_tl_h_b, n1_3),
                                        );
                                        let rhe_b = _mm256_xor_si256(
                                            _mm256_shuffle_epi8(v_th_l_b, n0_2),
                                            _mm256_shuffle_epi8(v_th_h_b, n1_3),
                                        );
                                        let rlo_b = _mm256_xor_si256(
                                            _mm256_shuffle_epi8(v_hl_l_b, n0_2),
                                            _mm256_shuffle_epi8(v_hl_h_b, n1_3),
                                        );
                                        let rho_b = _mm256_xor_si256(
                                            _mm256_shuffle_epi8(v_hh_l_b, n0_2),
                                            _mm256_shuffle_epi8(v_hh_h_b, n1_3),
                                        );
                                        let out_b = _mm256_or_si256(
                                            _mm256_and_si256(
                                                _mm256_xor_si256(
                                                    rle_b,
                                                    _mm256_srli_epi16(rlo_b, 8),
                                                ),
                                                mask_even,
                                            ),
                                            _mm256_slli_epi16(
                                                _mm256_xor_si256(
                                                    rhe_b,
                                                    _mm256_srli_epi16(rho_b, 8),
                                                ),
                                                8,
                                            ),
                                        );
                                        _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 => {
                        // Fallback for remaining 1 or 3 blocks (scalar for SIMD simplicity here).
                        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 (
                                            v_tl_l,
                                            v_tl_h,
                                            v_th_l,
                                            v_th_h,
                                            v_hl_l,
                                            v_hl_h,
                                            v_hh_l,
                                            v_hh_h,
                                            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.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 n0_2 = _mm256_and_si256(input, mask_f);
                                            let n1_3 = _mm256_and_si256(
                                                _mm256_srli_epi16(input, 4),
                                                mask_f,
                                            );
                                            let res_lo_even = _mm256_xor_si256(
                                                _mm256_shuffle_epi8(v_tl_l, n0_2),
                                                _mm256_shuffle_epi8(v_tl_h, n1_3),
                                            );
                                            let res_hi_even = _mm256_xor_si256(
                                                _mm256_shuffle_epi8(v_th_l, n0_2),
                                                _mm256_shuffle_epi8(v_th_h, n1_3),
                                            );
                                            let res_lo_odd = _mm256_xor_si256(
                                                _mm256_shuffle_epi8(v_hl_l, n0_2),
                                                _mm256_shuffle_epi8(v_hl_h, n1_3),
                                            );
                                            let res_hi_odd = _mm256_xor_si256(
                                                _mm256_shuffle_epi8(v_hh_l, n0_2),
                                                _mm256_shuffle_epi8(v_hh_h, n1_3),
                                            );
                                            let out = _mm256_or_si256(
                                                _mm256_and_si256(
                                                    _mm256_xor_si256(
                                                        res_lo_even,
                                                        _mm256_srli_epi16(res_lo_odd, 8),
                                                    ),
                                                    mask_even,
                                                ),
                                                _mm256_slli_epi16(
                                                    _mm256_xor_si256(
                                                        res_hi_even,
                                                        _mm256_srli_epi16(res_hi_odd, 8),
                                                    ),
                                                    8,
                                                ),
                                            );
                                            _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 ^= table_low[lo] ^ table_high[hi];
                                                pw = pw.add(1);
                                                p_in = p_in.add(2);
                                            }
                                        }
                                    }
                                },
                            );
                        }
                    }
                }
            });
    }

    /// AVX-512 BW nibble-shuffle on Normal `u16` buffers (parpar Shuffle AVX-512).
    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "avx512f,avx512bw")]
    pub(super) unsafe fn flush_avx512_shuffle(&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_avx512_shuffle_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 = "avx512f,avx512bw")]
    pub(super) unsafe fn flush_avx512_shuffle_work(
        buffers: &mut [Vec<u16>],
        queued: &[Vec<u8>],
        start_index: usize,
        logbases: &[u32],
        exponent_start: u32,
        gf: &Gf16,
    ) {
        let mask_f = _mm512_set1_epi8(0x0F_u8 as i8);
        let mask_even = _mm512_set1_epi16(0x00FF_u16 as i16);
        let n_rec = buffers.len();
        let n_queued = queued.len();
        if n_queued == 0 {
            return;
        }
        let all_tables: Vec<Avx512ShuffleTable> = (0..n_rec * n_queued)
            .into_par_iter()
            .map(|flat| unsafe {
                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);
                let mut tl_l = [0u8; 16];
                let mut tl_h = [0u8; 16];
                let mut th_l = [0u8; 16];
                let mut th_h = [0u8; 16];
                let mut hl_l = [0u8; 16];
                let mut hl_h = [0u8; 16];
                let mut hh_l = [0u8; 16];
                let mut hh_h = [0u8; 16];
                for val in 0..16usize {
                    let r0 = gf.mul(val as u16, coeff);
                    tl_l[val] = (r0 & 0xFF) as u8;
                    th_l[val] = (r0 >> 8) as u8;
                    let r1 = gf.mul((val as u16) << 4, coeff);
                    tl_h[val] = (r1 & 0xFF) as u8;
                    th_h[val] = (r1 >> 8) as u8;
                    let r2 = gf.mul((val as u16) << 8, coeff);
                    hl_l[val] = (r2 & 0xFF) as u8;
                    hh_l[val] = (r2 >> 8) as u8;
                    let r3 = gf.mul((val as u16) << 12, coeff);
                    hl_h[val] = (r3 & 0xFF) as u8;
                    hh_h[val] = (r3 >> 8) as u8;
                }
                let bcast =
                    |t: &[u8; 16]| _mm512_broadcast_i32x4(_mm_loadu_si128(t.as_ptr().cast()));
                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);
                }
                (
                    bcast(&tl_l),
                    bcast(&tl_h),
                    bcast(&th_l),
                    bcast(&th_h),
                    bcast(&hl_l),
                    bcast(&hl_h),
                    bcast(&hh_l),
                    bcast(&hh_h),
                    table_low,
                    table_high,
                )
            })
            .collect();

        buffers.par_iter_mut().enumerate().for_each(|(rec, buf)| {
            let base = rec * n_queued;
            let byte_len = buf.len() * 2;
            let blocks = byte_len / 64;
            let remainder = byte_len % 64;
            for q_idx in 0..n_queued {
                let (tl_l, tl_h, th_l, th_h, hl_l, hl_h, hh_l, hh_h, ref tlow, ref thigh) =
                    all_tables[base + q_idx];
                let slice = &queued[q_idx][..byte_len];
                unsafe {
                    let mut pin = slice.as_ptr().cast::<__m512i>();
                    let mut pout = buf.as_mut_ptr().cast::<__m512i>();
                    for _ in 0..blocks {
                        let input = _mm512_loadu_si512(pin);
                        let n0 = _mm512_and_si512(input, mask_f);
                        let n1 = _mm512_and_si512(_mm512_srli_epi16(input, 4), mask_f);
                        let rle = _mm512_xor_si512(
                            _mm512_shuffle_epi8(tl_l, n0),
                            _mm512_shuffle_epi8(tl_h, n1),
                        );
                        let rhe = _mm512_xor_si512(
                            _mm512_shuffle_epi8(th_l, n0),
                            _mm512_shuffle_epi8(th_h, n1),
                        );
                        let rlo = _mm512_xor_si512(
                            _mm512_shuffle_epi8(hl_l, n0),
                            _mm512_shuffle_epi8(hl_h, n1),
                        );
                        let rho = _mm512_xor_si512(
                            _mm512_shuffle_epi8(hh_l, n0),
                            _mm512_shuffle_epi8(hh_h, n1),
                        );
                        let out = _mm512_or_si512(
                            _mm512_and_si512(
                                _mm512_xor_si512(rle, _mm512_srli_epi16(rlo, 8)),
                                mask_even,
                            ),
                            _mm512_slli_epi16(_mm512_xor_si512(rhe, _mm512_srli_epi16(rho, 8)), 8),
                        );
                        _mm512_storeu_si512(pout, _mm512_xor_si512(_mm512_loadu_si512(pout), out));
                        pin = pin.add(1);
                        pout = pout.add(1);
                    }
                    if remainder > 0 {
                        let ow = blocks * 32;
                        let mut pw = buf[ow..].as_mut_ptr();
                        let mut p_in = slice[blocks * 64..].as_ptr();
                        let end = p_in.add(remainder);
                        while p_in < 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);
                        }
                    }
                }
            }
        });
    }

    /// AVX2 Shuffle2x flush: accumulates queued slices into Shuffle2x recovery buffers.
    ///
    /// Input slices are in normal u16 layout. Recovery buffers are in Shuffle2x layout
    /// (lo-bytes in lane 0, hi-bytes in lane 1 of each 32-byte chunk). Uses 4 PSHUFB
    /// per recovery block per 32-byte input chunk instead of the 8 used by the plain
    /// AVX2 nibble-shuffle path, achieving ~33% fewer instructions per block.
    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "avx2")]
    pub(super) unsafe fn flush_avx2_shuffle2x(&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()
        };

        let RecoveryBufferSet::Shuffle2x(ref mut bufs) = self.buffers else {
            unreachable!("flush_avx2_shuffle2x called on non-Shuffle2x encoder");
        };

        unsafe {
            Self::flush_avx2_shuffle2x_work(
                bufs,
                &queued,
                start_index,
                &self.logbases,
                self.exponent_start,
                &self.gf,
            );
        }

        self.pending_checksums.extend(new_cs);
        self.recycle_queue(queued);
    }

    /// Shuffle2x madd of one prepared 32-byte vector into a recovery buffer.
    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "avx2")]
    pub(super) unsafe fn s2x_contrib(
        s: __m256i,
        mask_f: __m256i,
        tna: __m256i,
        tnb: __m256i,
        tsa: __m256i,
        tsb: __m256i,
    ) -> __m256i {
        let sw = _mm256_permute2x128_si256(s, s, 0x01);
        let lo_nib_s = _mm256_and_si256(s, mask_f);
        let hi_nib_s = _mm256_and_si256(_mm256_srli_epi16(s, 4), mask_f);
        let lo_nib_sw = _mm256_and_si256(sw, mask_f);
        let hi_nib_sw = _mm256_and_si256(_mm256_srli_epi16(sw, 4), mask_f);
        let norm = _mm256_xor_si256(
            _mm256_shuffle_epi8(tna, lo_nib_s),
            _mm256_shuffle_epi8(tnb, hi_nib_s),
        );
        let swap = _mm256_xor_si256(
            _mm256_shuffle_epi8(tsa, lo_nib_sw),
            _mm256_shuffle_epi8(tsb, hi_nib_sw),
        );
        _mm256_xor_si256(norm, swap)
    }

    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "avx2")]
    pub(super) unsafe fn flush_avx2_shuffle2x_work(
        buffers: &mut [Vec<u8>],
        queued: &[Vec<u8>],
        start_index: usize,
        logbases: &[u32],
        exponent_start: u32,
        gf: &Gf16,
    ) {
        let mask_f = _mm256_set1_epi8(0x0F_u8 as i8);

        let n_rec = buffers.len();
        let n_queued = queued.len();

        // Pre-build 4-register Shuffle2x coefficient tables in parallel.
        // For coefficient c, 4-bit nibble index n (0..15):
        //   loNk[n] = (gf.mul(n << 4k, c) & 0xFF) as u8
        //   hiNk[n] = (gf.mul(n << 4k, c) >> 8) as u8
        // Table layout (each __m256i packs two 128-bit sub-tables into its two lanes):
        //   tNormA: lane0 = loN0, lane1 = hiN2
        //   tNormB: lane0 = loN1, lane1 = hiN3
        //   tSwapA: lane0 = loN2, lane1 = hiN0
        //   tSwapB: lane0 = loN3, lane1 = hiN1
        // Bounds check: ensure start_index + q_idx is within logbases
        if start_index + n_queued > logbases.len() {
            panic!(
                "PAR2 slice index overflow: start_index({}) + n_queued({}) > logbases.len({})",
                start_index,
                n_queued,
                logbases.len()
            );
        }

        let all_tables: Vec<Avx2Shuffle2xTable> = (0..n_rec * n_queued)
            .into_par_iter()
            .map(|flat| unsafe {
                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);

                let mut lo_n0 = [0u8; 16];
                let mut lo_n1 = [0u8; 16];
                let mut lo_n2 = [0u8; 16];
                let mut lo_n3 = [0u8; 16];
                let mut hi_n0 = [0u8; 16];
                let mut hi_n1 = [0u8; 16];
                let mut hi_n2 = [0u8; 16];
                let mut hi_n3 = [0u8; 16];

                for n in 0..16usize {
                    let r0 = gf.mul(n as u16, coeff);
                    lo_n0[n] = (r0 & 0xFF) as u8;
                    hi_n0[n] = (r0 >> 8) as u8;
                    let r1 = gf.mul((n as u16) << 4, coeff);
                    lo_n1[n] = (r1 & 0xFF) as u8;
                    hi_n1[n] = (r1 >> 8) as u8;
                    let r2 = gf.mul((n as u16) << 8, coeff);
                    lo_n2[n] = (r2 & 0xFF) as u8;
                    hi_n2[n] = (r2 >> 8) as u8;
                    let r3 = gf.mul((n as u16) << 12, coeff);
                    lo_n3[n] = (r3 & 0xFF) as u8;
                    hi_n3[n] = (r3 >> 8) as u8;
                }

                let t_norm_a = _mm256_set_m128i(
                    _mm_loadu_si128(hi_n2.as_ptr() as *const __m128i),
                    _mm_loadu_si128(lo_n0.as_ptr() as *const __m128i),
                );
                let t_norm_b = _mm256_set_m128i(
                    _mm_loadu_si128(hi_n3.as_ptr() as *const __m128i),
                    _mm_loadu_si128(lo_n1.as_ptr() as *const __m128i),
                );
                let t_swap_a = _mm256_set_m128i(
                    _mm_loadu_si128(hi_n0.as_ptr() as *const __m128i),
                    _mm_loadu_si128(lo_n2.as_ptr() as *const __m128i),
                );
                let t_swap_b = _mm256_set_m128i(
                    _mm_loadu_si128(hi_n1.as_ptr() as *const __m128i),
                    _mm_loadu_si128(lo_n3.as_ptr() as *const __m128i),
                );

                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);
                }

                (
                    t_norm_a, t_norm_b, t_swap_a, t_swap_b, table_low, table_high,
                )
            })
            .collect();

        // Convert each input once (Normal → Shuffle2x). The inner loop used to
        // vpshufb+vpermq every 32-byte block for every recovery group.
        let prepared: Vec<Vec<u8>> = queued
            .par_iter()
            .map(|s| {
                let mut out = vec![0u8; s.len()];
                crate::shuffle2x::to_shuffle2x(s, &mut out);
                out
            })
            .collect();

        // 8 KiB tiles — ParPar's Shuffle2x idealChunkSize. 32 KiB was oversized
        // for L2 when several recovery buffers share the same input stream.
        let chunk_size_bytes = 8192usize;

        buffers
            .par_chunks_mut(4)
            .enumerate()
            .for_each(|(group_idx, buf_group)| {
                let i = group_idx * 4;
                match buf_group {
                    [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_bytes)
                            .zip(buf_b.par_chunks_mut(chunk_size_bytes))
                            .zip(buf_c.par_chunks_mut(chunk_size_bytes))
                            .zip(buf_d.par_chunks_mut(chunk_size_bytes))
                            .enumerate()
                            .for_each(
                                |(chunk_idx, (((chunk_a, chunk_b), chunk_c), chunk_d))| unsafe {
                                    let byte_offset = chunk_idx * chunk_size_bytes;
                                    let byte_len = chunk_a.len();
                                    let blocks_32 = byte_len / 32;

                                    let mut q_idx = 0usize;
                                    while q_idx < n_queued {
                                        let pair = q_idx + 1 < n_queued;
                                        let (tna_a, tnb_a, tsa_a, tsb_a, _, _) =
                                            all_tables[base_a + q_idx];
                                        let (tna_b, tnb_b, tsa_b, tsb_b, _, _) =
                                            all_tables[base_b + q_idx];
                                        let (tna_c, tnb_c, tsa_c, tsb_c, _, _) =
                                            all_tables[base_c + q_idx];
                                        let (tna_d, tnb_d, tsa_d, tsb_d, _, _) =
                                            all_tables[base_d + q_idx];

                                        let chunk0 =
                                            &prepared[q_idx][byte_offset..byte_offset + byte_len];
                                        let mut ptr0 = chunk0.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 = ptr0.add(blocks_32);

                                        if pair {
                                            let (tna_a2, tnb_a2, tsa_a2, tsb_a2, _, _) =
                                                all_tables[base_a + q_idx + 1];
                                            let (tna_b2, tnb_b2, tsa_b2, tsb_b2, _, _) =
                                                all_tables[base_b + q_idx + 1];
                                            let (tna_c2, tnb_c2, tsa_c2, tsb_c2, _, _) =
                                                all_tables[base_c + q_idx + 1];
                                            let (tna_d2, tnb_d2, tsa_d2, tsb_d2, _, _) =
                                                all_tables[base_d + q_idx + 1];
                                            let chunk1 = &prepared[q_idx + 1]
                                                [byte_offset..byte_offset + byte_len];
                                            let mut ptr1 = chunk1.as_ptr() as *const __m256i;
                                            while ptr0 < end {
                                                _mm_prefetch(ptr0.add(4) as *const i8, _MM_HINT_T0);
                                                let s0 = _mm256_loadu_si256(ptr0);
                                                let s1 = _mm256_loadu_si256(ptr1);
                                                let c0 = RecoveryEncoder::s2x_contrib(
                                                    s0, mask_f, tna_a, tnb_a, tsa_a, tsb_a,
                                                );
                                                let c1 = RecoveryEncoder::s2x_contrib(
                                                    s1, mask_f, tna_a2, tnb_a2, tsa_a2, tsb_a2,
                                                );
                                                _mm256_storeu_si256(
                                                    ptr_a,
                                                    _mm256_xor_si256(
                                                        _mm256_loadu_si256(ptr_a),
                                                        _mm256_xor_si256(c0, c1),
                                                    ),
                                                );
                                                let c0 = RecoveryEncoder::s2x_contrib(
                                                    s0, mask_f, tna_b, tnb_b, tsa_b, tsb_b,
                                                );
                                                let c1 = RecoveryEncoder::s2x_contrib(
                                                    s1, mask_f, tna_b2, tnb_b2, tsa_b2, tsb_b2,
                                                );
                                                _mm256_storeu_si256(
                                                    ptr_b,
                                                    _mm256_xor_si256(
                                                        _mm256_loadu_si256(ptr_b),
                                                        _mm256_xor_si256(c0, c1),
                                                    ),
                                                );
                                                let c0 = RecoveryEncoder::s2x_contrib(
                                                    s0, mask_f, tna_c, tnb_c, tsa_c, tsb_c,
                                                );
                                                let c1 = RecoveryEncoder::s2x_contrib(
                                                    s1, mask_f, tna_c2, tnb_c2, tsa_c2, tsb_c2,
                                                );
                                                _mm256_storeu_si256(
                                                    ptr_c,
                                                    _mm256_xor_si256(
                                                        _mm256_loadu_si256(ptr_c),
                                                        _mm256_xor_si256(c0, c1),
                                                    ),
                                                );
                                                let c0 = RecoveryEncoder::s2x_contrib(
                                                    s0, mask_f, tna_d, tnb_d, tsa_d, tsb_d,
                                                );
                                                let c1 = RecoveryEncoder::s2x_contrib(
                                                    s1, mask_f, tna_d2, tnb_d2, tsa_d2, tsb_d2,
                                                );
                                                _mm256_storeu_si256(
                                                    ptr_d,
                                                    _mm256_xor_si256(
                                                        _mm256_loadu_si256(ptr_d),
                                                        _mm256_xor_si256(c0, c1),
                                                    ),
                                                );
                                                ptr0 = ptr0.add(1);
                                                ptr1 = ptr1.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);
                                            }
                                            q_idx += 2;
                                        } else {
                                            while ptr0 < end {
                                                _mm_prefetch(ptr0.add(4) as *const i8, _MM_HINT_T0);
                                                let s0 = _mm256_loadu_si256(ptr0);
                                                let c0 = RecoveryEncoder::s2x_contrib(
                                                    s0, mask_f, tna_a, tnb_a, tsa_a, tsb_a,
                                                );
                                                _mm256_storeu_si256(
                                                    ptr_a,
                                                    _mm256_xor_si256(_mm256_loadu_si256(ptr_a), c0),
                                                );
                                                let c0 = RecoveryEncoder::s2x_contrib(
                                                    s0, mask_f, tna_b, tnb_b, tsa_b, tsb_b,
                                                );
                                                _mm256_storeu_si256(
                                                    ptr_b,
                                                    _mm256_xor_si256(_mm256_loadu_si256(ptr_b), c0),
                                                );
                                                let c0 = RecoveryEncoder::s2x_contrib(
                                                    s0, mask_f, tna_c, tnb_c, tsa_c, tsb_c,
                                                );
                                                _mm256_storeu_si256(
                                                    ptr_c,
                                                    _mm256_xor_si256(_mm256_loadu_si256(ptr_c), c0),
                                                );
                                                let c0 = RecoveryEncoder::s2x_contrib(
                                                    s0, mask_f, tna_d, tnb_d, tsa_d, tsb_d,
                                                );
                                                _mm256_storeu_si256(
                                                    ptr_d,
                                                    _mm256_xor_si256(_mm256_loadu_si256(ptr_d), c0),
                                                );
                                                ptr0 = ptr0.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);
                                            }
                                            q_idx += 1;
                                        }
                                    }
                                },
                            );
                    }
                    [buf_a, buf_b] => {
                        let base_a = i * n_queued;
                        let base_b = (i + 1) * n_queued;
                        buf_a
                            .par_chunks_mut(chunk_size_bytes)
                            .zip(buf_b.par_chunks_mut(chunk_size_bytes))
                            .enumerate()
                            .for_each(|(chunk_idx, (chunk_a, chunk_b))| unsafe {
                                let byte_offset = chunk_idx * chunk_size_bytes;
                                let byte_len = chunk_a.len();
                                let blocks_32 = byte_len / 32;

                                let mut q_idx = 0usize;
                                while q_idx < n_queued {
                                    let (tna_a, tnb_a, tsa_a, tsb_a, _, _) =
                                        all_tables[base_a + q_idx];
                                    let (tna_b, tnb_b, tsa_b, tsb_b, _, _) =
                                        all_tables[base_b + q_idx];
                                    let slice_chunk =
                                        &prepared[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 s = _mm256_loadu_si256(ptr_in);
                                        let c = RecoveryEncoder::s2x_contrib(
                                            s, mask_f, tna_a, tnb_a, tsa_a, tsb_a,
                                        );
                                        _mm256_storeu_si256(
                                            ptr_a,
                                            _mm256_xor_si256(_mm256_loadu_si256(ptr_a), c),
                                        );
                                        let c = RecoveryEncoder::s2x_contrib(
                                            s, mask_f, tna_b, tnb_b, tsa_b, tsb_b,
                                        );
                                        _mm256_storeu_si256(
                                            ptr_b,
                                            _mm256_xor_si256(_mm256_loadu_si256(ptr_b), c),
                                        );
                                        ptr_in = ptr_in.add(1);
                                        ptr_a = ptr_a.add(1);
                                        ptr_b = ptr_b.add(1);
                                    }
                                    q_idx += 1;
                                }
                            });
                    }
                    rest => {
                        for (j, buf) in rest.iter_mut().enumerate() {
                            let base = (i + j) * n_queued;
                            buf.par_chunks_mut(chunk_size_bytes).enumerate().for_each(
                                |(chunk_idx, chunk)| unsafe {
                                    let byte_offset = chunk_idx * chunk_size_bytes;
                                    let byte_len = chunk.len();
                                    let blocks_32 = byte_len / 32;

                                    for q_idx in 0..n_queued {
                                        let (tna, tnb, tsa, tsb, _, _) = all_tables[base + q_idx];
                                        let slice_chunk =
                                            &prepared[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 s = _mm256_loadu_si256(ptr_in);
                                            let c = RecoveryEncoder::s2x_contrib(
                                                s, mask_f, tna, tnb, tsa, tsb,
                                            );
                                            _mm256_storeu_si256(
                                                ptr_buf,
                                                _mm256_xor_si256(_mm256_loadu_si256(ptr_buf), c),
                                            );
                                            ptr_in = ptr_in.add(1);
                                            ptr_buf = ptr_buf.add(1);
                                        }
                                    }
                                },
                            );
                        }
                    }
                }
            });
    }

    /// Affine2x + GFNI: two `gf2p8affine` + 64-bit swap, fused `srcCount=6`,
    /// 4 KiB tiles. Inputs are prepared once (Normal → Affine2x).
    /// ALTMAP XOR bit-dependency kernel (Phase 27e).
    ///
    /// Transposes each queued raw slice into ALTMAP layout, then applies the
    /// pre-built dep-matrix table via `vpxor` — one 256-bit vector per
    /// (output-plane, vec-index) pair.  4-way unroll over recovery blocks.
    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "avx2")]
    pub(super) unsafe fn flush_avx2_altmap(&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()
        };

        let slice_words = self.slice_words;
        let altmap_slices: Vec<Vec<u8>> = queued
            .par_iter()
            .map(|s| {
                let mut am = vec![0u8; super::altmap::altmap_size(slice_words)];
                // SAFETY: slice_size is always even; s is exactly slice_size bytes.
                let words =
                    unsafe { std::slice::from_raw_parts(s.as_ptr().cast::<u16>(), slice_words) };
                super::altmap::to_altmap(words, &mut am);
                am
            })
            .collect();

        let dep_tables = self
            .dep_tables
            .as_deref()
            .expect("dep_tables must be built for ALTMAP path");

        let buffers = match &mut self.buffers {
            RecoveryBufferSet::Altmap(b) => b.as_mut_slice(),
            _ => panic!("flush_avx2_altmap called on non-ALTMAP encoder"),
        };

        unsafe {
            Self::flush_avx2_altmap_work(
                buffers,
                &altmap_slices,
                start_index,
                &self.logbases,
                self.exponent_start,
                dep_tables,
                &self.gf,
            );
        }

        self.pending_checksums.extend(new_cs);
        self.recycle_queue(queued);
    }

    /// Static worker for [`flush_avx2_altmap`].
    ///
    /// `buffers`: one `Vec<u8>` per recovery block (ALTMAP layout).
    /// `queued`:  one `Vec<u8>` per input slice (already in ALTMAP layout).
    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "avx2")]
    #[allow(clippy::needless_range_loop)]
    pub(super) unsafe fn flush_avx2_altmap_work(
        buffers: &mut [Vec<u8>],
        queued: &[Vec<u8>],
        start_index: usize,
        logbases: &[u32],
        exponent_start: u32,
        dep_tables: &[[u16; 16]; 65536],
        gf: &Gf16,
    ) {
        use std::arch::x86_64::*;

        let n_rec = buffers.len();
        if n_rec == 0 || queued.is_empty() {
            return;
        }

        // plane_bytes = slice_words / 8.  ALTMAP invariant: buf.len() == plane_bytes * 16.
        let plane_bytes = buffers[0].len() / 16;
        let n_vec = plane_bytes / 32; // full 256-bit vectors per plane section

        // Process 4 recovery blocks at a time.
        buffers
            .par_chunks_mut(4)
            .enumerate()
            .for_each(|(chunk_idx, rec_chunk)| {
                let rec_base = chunk_idx * 4;
                let chunk_len = rec_chunk.len(); // 1..=4

                for (q, slice_am) in queued.iter().enumerate() {
                    let slice_index = start_index + q;
                    let logbase = logbases[slice_index] as u64;

                    // Coefficient = antilog[(logbase * exponent) % ORDER].
                    let mut coeffs = [0u16; 4];
                    for r in 0..chunk_len {
                        let exponent = exponent_start + (rec_base + r) as u32;
                        let log_coeff =
                            ((logbase * exponent as u64) % super::gf16::ORDER as u64) as u32;
                        coeffs[r] = gf.exp(log_coeff);
                    }

                    // Decode each active coefficient's dependency matrix once per
                    // (recovery-chunk, input-slice) pair — reused across every `vi`
                    // and every tail byte below. See `decode_plane_deps`.
                    let deps: [Option<([PlaneOutDeps; 16], usize)>; 4] = std::array::from_fn(|r| {
                        if r >= chunk_len || coeffs[r] == 0 {
                            None
                        } else {
                            Some(decode_plane_deps(&dep_tables[coeffs[r] as usize]))
                        }
                    });

                    // AVX2 path: one 256-bit vector per plane per vec-index.
                    for vi in 0..n_vec {
                        // Load 16 input planes at this vector position.
                        let mut in_planes = [_mm256_setzero_si256(); 16];
                        for p in 0..16usize {
                            let off = p * plane_bytes + vi * 32;
                            // SAFETY: bounds guaranteed by ALTMAP layout invariant.
                            in_planes[p] =
                                unsafe { _mm256_loadu_si256(slice_am.as_ptr().add(off).cast()) };
                        }

                        for r in 0..chunk_len {
                            let Some((ref plane_deps, count)) = deps[r] else {
                                continue;
                            };
                            for pd in &plane_deps[..count] {
                                let mut acc = _mm256_setzero_si256();
                                for &plane_in in &pd.plane_ins[..pd.n_ins as usize] {
                                    acc = _mm256_xor_si256(acc, in_planes[plane_in as usize]);
                                }
                                let off = pd.plane_out as usize * plane_bytes + vi * 32;
                                // SAFETY: off + 32 <= plane_bytes * 16 == buf.len().
                                let ptr = rec_chunk[r].as_mut_ptr().add(off).cast::<__m256i>();
                                let prev = unsafe { _mm256_loadu_si256(ptr) };
                                unsafe {
                                    _mm256_storeu_si256(ptr, _mm256_xor_si256(prev, acc));
                                }
                            }
                        }
                    }

                    // Scalar tail for remainder bytes within each plane.
                    let tail_start = n_vec * 32;
                    if tail_start < plane_bytes {
                        for r in 0..chunk_len {
                            let Some((ref plane_deps, count)) = deps[r] else {
                                continue;
                            };
                            for pd in &plane_deps[..count] {
                                for byte_off in tail_start..plane_bytes {
                                    let mut acc: u8 = 0;
                                    for &plane_in in &pd.plane_ins[..pd.n_ins as usize] {
                                        acc ^= slice_am[plane_in as usize * plane_bytes + byte_off];
                                    }
                                    rec_chunk[r][pd.plane_out as usize * plane_bytes + byte_off] ^=
                                        acc;
                                }
                            }
                        }
                    }
                }
            });
    }
}