pixon 0.8.0

SIMD-dispatched color conversion for the FFmpeg AVPixelFormat space: one-call decode of validated source frames to RGB / RGBA / Luma / HSV, fused resampling while converting, colorimetry-honest matrices and chroma siting, no_std-friendly.
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
//! AVX-512 VUYA / VUYX (packed YUV 4:4:4, 8-bit) kernels.
//!
//! ## Layout
//!
//! Four `u8` elements per pixel: `V(8) ‖ U(8) ‖ Y(8) ‖ A(8)`.
//! VUYA carries a real alpha channel in byte 3. VUYX treats byte 3 as
//! padding and forces output α to `0xFF`.
//!
//! ## Per-iter pipeline (64 px / iter)
//!
//! Four contiguous `_mm512_loadu_si512` loads fetch 256 bytes = 64
//! pixels of `V U Y A`. Each 512-bit register holds 16 pixels (4 in
//! each of its four 128-bit lanes). Per-128-bit-lane `_mm512_shuffle_epi8`
//! gathers each channel's 4 bytes from each lane into the lane's low
//! 32 bits (one `i32` lane); the upper 12 bytes of each lane are zeroed.
//!
//! After per-lane shuffle (e.g. for V on `raw_c0` covering pixels 0..15):
//! ```text
//!   v_c0 i32 lanes:
//!     [V0..V3, _, _, _, V4..V7, _, _, _, V8..V11, _, _, _, V12..V15, _, _, _]
//! ```
//! (lane 0 holds the 4-byte i32 packing `V0|V1|V2|V3`; lanes 1, 2, 3
//! are zero; lane 4 holds `V4|V5|V6|V7`; etc.) Two rounds of
//! `_mm512_permutex2var_epi32` (cross-vector i32 gather) then
//! consolidate the 16 valid i32 lanes scattered across the four
//! per-load partials into a single naturally-ordered 64-byte channel
//! vector:
//!
//! - Round 1: from `(v_c0, v_c1)` pick valid i32 lanes (0, 4, 8, 12) of
//!   each, producing 8 valid lanes covering V0..V31 in i32-lanes 0..7
//!   of the result. Same for `(v_c2, v_c3)` covering V32..V63.
//! - Round 2: combine the two half-results into a full 16-lane vector
//!   via `_mm512_permutex2var_epi32` with index `[0..7, 16..23]`.
//!
//! Net deinterleave: 4 loads + 16 shuffles + 12 permutes for all four
//! channels (each per-load shuffle is per-channel; channels are
//! independent and parallelizable). The cross-lane primitive used is
//! `vpermt2d` (i32) from AVX-512F — no AVX-512VBMI required (which
//! would be needed for `_mm512_permutex2var_epi8`).
//!
//! After deinterleave: zero-extend each 64-byte channel to two
//! `i16x32` halves via `_mm512_cvtepu8_epi16` on the low / high 256-bit
//! halves. The Q15 chroma + Y pipeline at BITS=8 is byte-identical to
//! the AVX-512 NV24 / packed YUV422 kernels — `chroma_i16x32`,
//! `scale_y`, `narrow_u8x64` from `mod.rs`.
//!
//! ## α handling
//!
//! When `ALPHA && ALPHA_SRC`, the A channel from the deinterleave is
//! passed straight through. When `ALPHA && !ALPHA_SRC`,
//! `_mm512_set1_epi8(-1)` (= 0xFF) is used. The `<false, false>`
//! monomorphization (RGB) drops the A channel entirely. The
//! `<false, true>` combination is rejected at monomorphization via
//! `const { assert! }`.
//!
//! ## Tail
//!
//! `width % 64` remaining pixels fall through to
//! `scalar::vuya_to_rgb_or_rgba_row`.

use super::*;
use crate::{KernelMatrix, row::scalar};

// ---- Static permute index tables ----------------------------------------
//
// VUYA layout per pixel: [V, U, Y, A] (4 u8 per pixel).
// 64 pixels occupy 256 bytes across 4 × __m512i loads:
//   raw_c0: bytes  0..63  → pixels  0..15
//   raw_c1: bytes 64..127 → pixels 16..31
//   raw_c2: bytes 128..191 → pixels 32..47
//   raw_c3: bytes 192..255 → pixels 48..63
//
// Within one __m512i, each of its 4 × 128-bit lanes holds 4 pixels' worth
// of VUYA (16 bytes). Per-128-bit-lane shuffle masks gather each channel's
// 4 bytes into the low 4 bytes of each lane (one `i32` lane in the low
// 32 bits of the lane); the upper 12 bytes of each lane are zeroed.
//
// After per-lane shuffle, the result holds the channel's bytes at i32
// lanes 0, 4, 8, 12 (one valid i32 per 128-bit lane). All other i32
// lanes are zero.
//
// Two rounds of `_mm512_permutex2var_epi32` then consolidate four
// such per-load partials into a single 16-lane (i32) channel vector
// holding 64 bytes in natural pixel order.

/// Round-1 index for `_mm512_permutex2var_epi32(a, idx, b)`: gather the
/// valid i32 lanes (0, 4, 8, 12) of `a` (covering 16 pixels of one
/// channel) and the valid i32 lanes (0, 4, 8, 12) of `b` (covering the
/// next 16 pixels) into the low 8 lanes of the result. Lanes 8..16 are
/// don't-care (use lane 0 as a safe index).
///
/// For `_mm512_permutex2var_epi32`:
///   `idx[i] < 16`  → output lane i = a[idx[i]]
///   `idx[i] >= 16` → output lane i = b[idx[i] - 16]
#[rustfmt::skip]
static GATHER_PAIR_IDX: [i32; 16] = [
  // From a (lanes 0..4 of output): 16 pixels of channel from one __m512i.
  0, 4, 8, 12,
  // From b (lanes 4..8 of output): 16 pixels from the next __m512i.
  16, 20, 24, 28,
  // Don't-care lanes 8..16: safe index 0.
  0, 0, 0, 0, 0, 0, 0, 0,
];

/// Round-2 index: combine two 8-i32-lane half-vectors into a full
/// 16-lane vector. Low 8 lanes from `a` (pixels 0..31), high 8 lanes
/// from `b` (pixels 32..63).
#[rustfmt::skip]
static COMBINE_IDX: [i32; 16] = [
  0, 1, 2, 3, 4, 5, 6, 7,
  16, 17, 18, 19, 20, 21, 22, 23,
];

// ---- Deinterleave helper ------------------------------------------------

/// Loads 64 VUYA quadruples (256 bytes = 64 pixels) from `ptr` and
/// unpacks them into four `__m512i` channel vectors holding 64 bytes
/// in natural pixel order (lane n = byte from pixel n):
/// - `v_vec`, `u_vec`, `y_vec`, `a_vec` — each 64 bytes, lane n = V/U/Y/A
///   from pixel n.
///
/// Strategy:
/// 1. 4 × `_mm512_loadu_si512` (one per 16-pixel group).
/// 2. Per-128-bit-lane `_mm512_shuffle_epi8` per channel, gathering each
///    channel's 4 bytes from each of the 4 × 128-bit lanes into the
///    low 4 bytes of each lane (one `i32` lane). Upper bytes zeroed.
/// 3. Two rounds of `_mm512_permutex2var_epi32` per channel: round 1
///    gathers the valid i32 lanes (0, 4, 8, 12) from each of two
///    consecutive `__m512i` partials into the low 8 lanes of an
///    intermediate; round 2 combines the two 8-lane halves into the
///    full 16-lane channel vector.
///
/// # Safety
///
/// `ptr` must point to at least 256 readable bytes (64 VUYA quadruples).
/// Caller's `target_feature` must include AVX-512F + AVX-512BW (BW
/// provides `_mm512_shuffle_epi8`; F provides `_mm512_permutex2var_epi32`).
/// Per-128-bit-lane gather mask for one channel at byte offset `OFF`
/// within each 4-byte pixel, broadcast across all four 128-bit lanes.
/// The const offset makes the mask a compile-time constant.
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
unsafe fn chan_mask_avx512<const OFF: usize>() -> __m512i {
  let o = OFF as i8;
  let lane = _mm_setr_epi8(
    o,
    o + 4,
    o + 8,
    o + 12,
    -1,
    -1,
    -1,
    -1,
    -1,
    -1,
    -1,
    -1,
    -1,
    -1,
    -1,
    -1,
  );
  _mm512_broadcast_i32x4(lane)
}

/// Offset-parameterized AVX-512 deinterleave of 64 four-byte pixels into
/// `(v_vec, u_vec, y_vec, a_vec)` (each `__m512i` of 64 natural-order
/// channel bytes). The four byte offsets select which source byte feeds
/// each channel, serving every channel re-ordering of the 4-byte family.
///
/// # Safety
///
/// `ptr` must point to at least 256 readable bytes (64 pixels). AVX-512F
/// + AVX-512BW must be available.
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
unsafe fn deinterleave_packed444_avx512<
  const V_OFF: usize,
  const U_OFF: usize,
  const Y_OFF: usize,
  const A_OFF: usize,
>(
  ptr: *const u8,
) -> (__m512i, __m512i, __m512i, __m512i) {
  // SAFETY: caller obligation — `ptr` has 256 bytes readable; AVX-512F
  // + AVX-512BW are available.
  unsafe {
    let raw_c0 = _mm512_loadu_si512(ptr.cast());
    let raw_c1 = _mm512_loadu_si512(ptr.add(64).cast());
    let raw_c2 = _mm512_loadu_si512(ptr.add(128).cast());
    let raw_c3 = _mm512_loadu_si512(ptr.add(192).cast());

    let v_mask = chan_mask_avx512::<V_OFF>();
    let u_mask = chan_mask_avx512::<U_OFF>();
    let y_mask = chan_mask_avx512::<Y_OFF>();
    let a_mask = chan_mask_avx512::<A_OFF>();

    // Apply the per-channel masks to each load. Each result holds
    // the channel's 16 valid bytes spread across i32 lanes 0, 4, 8, 12.
    //
    // For V (analogous for U/Y/A), v_c0 (covering pixels 0..15) holds:
    //   i32-lane 0:  V0V1V2V3   (4 bytes, low 32 of lane 0)
    //   i32-lane 1:  zero
    //   i32-lane 2:  zero
    //   i32-lane 3:  zero
    //   i32-lane 4:  V4V5V6V7
    //   i32-lane 5:  zero
    //   i32-lane 6:  zero
    //   i32-lane 7:  zero
    //   i32-lane 8:  V8V9V10V11
    //   ...
    //   i32-lane 12: V12V13V14V15
    //   i32-lanes 13, 14, 15: zero
    let v_c0 = _mm512_shuffle_epi8(raw_c0, v_mask);
    let v_c1 = _mm512_shuffle_epi8(raw_c1, v_mask);
    let v_c2 = _mm512_shuffle_epi8(raw_c2, v_mask);
    let v_c3 = _mm512_shuffle_epi8(raw_c3, v_mask);
    let u_c0 = _mm512_shuffle_epi8(raw_c0, u_mask);
    let u_c1 = _mm512_shuffle_epi8(raw_c1, u_mask);
    let u_c2 = _mm512_shuffle_epi8(raw_c2, u_mask);
    let u_c3 = _mm512_shuffle_epi8(raw_c3, u_mask);
    let y_c0 = _mm512_shuffle_epi8(raw_c0, y_mask);
    let y_c1 = _mm512_shuffle_epi8(raw_c1, y_mask);
    let y_c2 = _mm512_shuffle_epi8(raw_c2, y_mask);
    let y_c3 = _mm512_shuffle_epi8(raw_c3, y_mask);
    let a_c0 = _mm512_shuffle_epi8(raw_c0, a_mask);
    let a_c1 = _mm512_shuffle_epi8(raw_c1, a_mask);
    let a_c2 = _mm512_shuffle_epi8(raw_c2, a_mask);
    let a_c3 = _mm512_shuffle_epi8(raw_c3, a_mask);

    // Permute index tables.
    let pair_idx = _mm512_loadu_si512(GATHER_PAIR_IDX.as_ptr().cast());
    let comb_idx = _mm512_loadu_si512(COMBINE_IDX.as_ptr().cast());

    // Round 1: gather valid i32 lanes (0, 4, 8, 12) from each pair of
    // partials. Result has 8 valid lanes (0..8) covering 32 pixels'
    // worth of channel bytes; lanes 8..16 are don't-care.
    //
    // For V: v_01 covers pixels 0..31 (lanes 0..8 = V0..V31 packed
    // 4 bytes per i32-lane); v_23 covers pixels 32..63.
    let v_01 = _mm512_permutex2var_epi32(v_c0, pair_idx, v_c1);
    let v_23 = _mm512_permutex2var_epi32(v_c2, pair_idx, v_c3);
    let u_01 = _mm512_permutex2var_epi32(u_c0, pair_idx, u_c1);
    let u_23 = _mm512_permutex2var_epi32(u_c2, pair_idx, u_c3);
    let y_01 = _mm512_permutex2var_epi32(y_c0, pair_idx, y_c1);
    let y_23 = _mm512_permutex2var_epi32(y_c2, pair_idx, y_c3);
    let a_01 = _mm512_permutex2var_epi32(a_c0, pair_idx, a_c1);
    let a_23 = _mm512_permutex2var_epi32(a_c2, pair_idx, a_c3);

    // Round 2: combine two 8-lane half-vectors into a full 16-lane
    // (i32) channel vector. `COMBINE_IDX` picks lanes 0..8 from `a` and
    // lanes 16..24 (= `b` lanes 0..8) for the high half.
    //
    // For V: v_vec is a __m512i with 64 bytes in natural pixel order:
    // byte n = V from pixel n.
    let v_vec = _mm512_permutex2var_epi32(v_01, comb_idx, v_23);
    let u_vec = _mm512_permutex2var_epi32(u_01, comb_idx, u_23);
    let y_vec = _mm512_permutex2var_epi32(y_01, comb_idx, y_23);
    let a_vec = _mm512_permutex2var_epi32(a_01, comb_idx, a_23);

    (v_vec, u_vec, y_vec, a_vec)
  }
}

// ---- Shared RGB / RGBA kernel (64 px/iter) ------------------------------

/// AVX-512 VUYA / VUYX → packed u8 RGB or RGBA.
///
/// Byte-identical to `scalar::vuya_to_rgb_or_rgba_row::<ALPHA, ALPHA_SRC>`.
///
/// Block size: 64 pixels per SIMD iteration (four `_mm512_loadu_si512`
/// loads, 256 bytes total).
///
/// The three valid monomorphizations are:
/// - `<false, false>` — RGB (drops α)
/// - `<true, true>`  — RGBA, source α pass-through (VUYA)
/// - `<true, false>` — RGBA, force α = `0xFF` (VUYX)
///
/// `<false, true>` is rejected at monomorphization via `const { assert! }`.
///
/// # Safety
///
/// 1. **AVX-512F + AVX-512BW must be available.**
/// 2. `packed.len() >= width * 4`.
/// 3. `out.len() >= width * (if ALPHA { 4 } else { 3 })`.
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn packed444_to_rgb_or_rgba_row<
  const ALPHA: bool,
  const ALPHA_SRC: bool,
  const V_OFF: usize,
  const U_OFF: usize,
  const Y_OFF: usize,
  const A_OFF: usize,
>(
  packed: &[u8],
  out: &mut [u8],
  width: usize,
  matrix: KernelMatrix,
  full_range: bool,
) {
  // Source alpha requires RGBA output.
  const { assert!(!ALPHA_SRC || ALPHA) };
  debug_assert!(packed.len() >= width * 4, "packed row too short");
  let bpp: usize = if ALPHA { 4 } else { 3 };
  debug_assert!(out.len() >= width * bpp, "out row too short");

  let coeffs = scalar::Coefficients::for_matrix(matrix);
  let (y_off, y_scale, c_scale) = scalar::range_params_n::<8, 8>(full_range);
  let bias = scalar::chroma_bias::<8>();
  const RND: i32 = 1 << 14;

  // SAFETY: AVX-512F + AVX-512BW availability is the caller's obligation.
  unsafe {
    let rnd_v = _mm512_set1_epi32(RND);
    let y_off_v = _mm512_set1_epi16(y_off as i16);
    let y_scale_v = _mm512_set1_epi32(y_scale);
    let c_scale_v = _mm512_set1_epi32(c_scale);
    let bias_v = _mm512_set1_epi16(bias as i16);
    let cru = _mm512_set1_epi32(coeffs.r_u());
    let crv = _mm512_set1_epi32(coeffs.r_v());
    let cgu = _mm512_set1_epi32(coeffs.g_u());
    let cgv = _mm512_set1_epi32(coeffs.g_v());
    let cbu = _mm512_set1_epi32(coeffs.b_u());
    let cbv = _mm512_set1_epi32(coeffs.b_v());
    let pack_fixup = _mm512_setr_epi64(0, 2, 4, 6, 1, 3, 5, 7);
    // 0xFF for the forced-opaque path.
    let alpha_u8 = _mm512_set1_epi8(-1i8);

    let mut x = 0usize;
    while x + 64 <= width {
      // Deinterleave 64 quadruples → V, U, Y, A as u8x64 in natural pixel
      // order per the format's byte offsets.
      let (v_u8, u_u8, y_u8, a_u8) =
        deinterleave_packed444_avx512::<V_OFF, U_OFF, Y_OFF, A_OFF>(packed.as_ptr().add(x * 4));

      // Zero-extend each channel to two i16x32 halves (low 32 bytes →
      // pixels 0..31, high 32 bytes → pixels 32..63).
      let v_lo_i16 = _mm512_cvtepu8_epi16(_mm512_castsi512_si256(v_u8));
      let v_hi_i16 = _mm512_cvtepu8_epi16(_mm512_extracti64x4_epi64::<1>(v_u8));
      let u_lo_i16 = _mm512_cvtepu8_epi16(_mm512_castsi512_si256(u_u8));
      let u_hi_i16 = _mm512_cvtepu8_epi16(_mm512_extracti64x4_epi64::<1>(u_u8));
      let y_lo_i16 = _mm512_cvtepu8_epi16(_mm512_castsi512_si256(y_u8));
      let y_hi_i16 = _mm512_cvtepu8_epi16(_mm512_extracti64x4_epi64::<1>(y_u8));

      // Subtract chroma bias (128 for 8-bit).
      let u_lo_sub = _mm512_sub_epi16(u_lo_i16, bias_v);
      let u_hi_sub = _mm512_sub_epi16(u_hi_i16, bias_v);
      let v_lo_sub = _mm512_sub_epi16(v_lo_i16, bias_v);
      let v_hi_sub = _mm512_sub_epi16(v_hi_i16, bias_v);

      // Widen each i16x32 chroma half into two i32x16 halves for Q15
      // multiply.
      let u_lo_a = _mm512_cvtepi16_epi32(_mm512_castsi512_si256(u_lo_sub));
      let u_lo_b = _mm512_cvtepi16_epi32(_mm512_extracti64x4_epi64::<1>(u_lo_sub));
      let u_hi_a = _mm512_cvtepi16_epi32(_mm512_castsi512_si256(u_hi_sub));
      let u_hi_b = _mm512_cvtepi16_epi32(_mm512_extracti64x4_epi64::<1>(u_hi_sub));
      let v_lo_a = _mm512_cvtepi16_epi32(_mm512_castsi512_si256(v_lo_sub));
      let v_lo_b = _mm512_cvtepi16_epi32(_mm512_extracti64x4_epi64::<1>(v_lo_sub));
      let v_hi_a = _mm512_cvtepi16_epi32(_mm512_castsi512_si256(v_hi_sub));
      let v_hi_b = _mm512_cvtepi16_epi32(_mm512_extracti64x4_epi64::<1>(v_hi_sub));

      // u_d / v_d = (u * c_scale + RND) >> 15.
      let u_d_lo_a = q15_shift(_mm512_add_epi32(
        _mm512_mullo_epi32(u_lo_a, c_scale_v),
        rnd_v,
      ));
      let u_d_lo_b = q15_shift(_mm512_add_epi32(
        _mm512_mullo_epi32(u_lo_b, c_scale_v),
        rnd_v,
      ));
      let u_d_hi_a = q15_shift(_mm512_add_epi32(
        _mm512_mullo_epi32(u_hi_a, c_scale_v),
        rnd_v,
      ));
      let u_d_hi_b = q15_shift(_mm512_add_epi32(
        _mm512_mullo_epi32(u_hi_b, c_scale_v),
        rnd_v,
      ));
      let v_d_lo_a = q15_shift(_mm512_add_epi32(
        _mm512_mullo_epi32(v_lo_a, c_scale_v),
        rnd_v,
      ));
      let v_d_lo_b = q15_shift(_mm512_add_epi32(
        _mm512_mullo_epi32(v_lo_b, c_scale_v),
        rnd_v,
      ));
      let v_d_hi_a = q15_shift(_mm512_add_epi32(
        _mm512_mullo_epi32(v_hi_a, c_scale_v),
        rnd_v,
      ));
      let v_d_hi_b = q15_shift(_mm512_add_epi32(
        _mm512_mullo_epi32(v_hi_b, c_scale_v),
        rnd_v,
      ));

      // 64 chroma per channel: two `chroma_i16x32` calls per channel
      // (no chroma duplication at 4:4:4 — one chroma sample per Y pixel).
      let r_chroma_lo = chroma_i16x32(
        cru, crv, u_d_lo_a, v_d_lo_a, u_d_lo_b, v_d_lo_b, rnd_v, pack_fixup,
      );
      let r_chroma_hi = chroma_i16x32(
        cru, crv, u_d_hi_a, v_d_hi_a, u_d_hi_b, v_d_hi_b, rnd_v, pack_fixup,
      );
      let g_chroma_lo = chroma_i16x32(
        cgu, cgv, u_d_lo_a, v_d_lo_a, u_d_lo_b, v_d_lo_b, rnd_v, pack_fixup,
      );
      let g_chroma_hi = chroma_i16x32(
        cgu, cgv, u_d_hi_a, v_d_hi_a, u_d_hi_b, v_d_hi_b, rnd_v, pack_fixup,
      );
      let b_chroma_lo = chroma_i16x32(
        cbu, cbv, u_d_lo_a, v_d_lo_a, u_d_lo_b, v_d_lo_b, rnd_v, pack_fixup,
      );
      let b_chroma_hi = chroma_i16x32(
        cbu, cbv, u_d_hi_a, v_d_hi_a, u_d_hi_b, v_d_hi_b, rnd_v, pack_fixup,
      );

      // Y path: scale each i16x32 half independently.
      let y_scaled_lo = scale_y(y_lo_i16, y_off_v, y_scale_v, rnd_v, pack_fixup);
      let y_scaled_hi = scale_y(y_hi_i16, y_off_v, y_scale_v, rnd_v, pack_fixup);

      // Saturating i16 add Y + chroma per channel, then narrow to u8x64
      // with natural lane order via `narrow_u8x64`.
      let r_lo = _mm512_adds_epi16(y_scaled_lo, r_chroma_lo);
      let r_hi = _mm512_adds_epi16(y_scaled_hi, r_chroma_hi);
      let g_lo = _mm512_adds_epi16(y_scaled_lo, g_chroma_lo);
      let g_hi = _mm512_adds_epi16(y_scaled_hi, g_chroma_hi);
      let b_lo = _mm512_adds_epi16(y_scaled_lo, b_chroma_lo);
      let b_hi = _mm512_adds_epi16(y_scaled_hi, b_chroma_hi);

      let r_u8 = narrow_u8x64(r_lo, r_hi, pack_fixup);
      let g_u8 = narrow_u8x64(g_lo, g_hi, pack_fixup);
      let b_u8 = narrow_u8x64(b_lo, b_hi, pack_fixup);

      let out_ptr = out.as_mut_ptr().add(x * bpp);
      if ALPHA {
        let a_vec = if ALPHA_SRC { a_u8 } else { alpha_u8 };
        write_rgba_64(r_u8, g_u8, b_u8, a_vec, out_ptr);
      } else {
        write_rgb_64(r_u8, g_u8, b_u8, out_ptr);
      }

      x += 64;
    }

    // Scalar tail — remaining < 64 pixels.
    if x < width {
      scalar::packed444_to_rgb_or_rgba_row::<ALPHA, ALPHA_SRC, V_OFF, U_OFF, Y_OFF, A_OFF>(
        &packed[x * 4..],
        &mut out[x * bpp..],
        width - x,
        matrix,
        full_range,
      );
    }
  }
}

/// VUYA / VUYX byte order (`V=0,U=1,Y=2,A=3`) over the offset-generic
/// AVX-512 kernel.
///
/// # Safety
///
/// Same contract as [`packed444_to_rgb_or_rgba_row`].
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn vuya_to_rgb_or_rgba_row<const ALPHA: bool, const ALPHA_SRC: bool>(
  packed: &[u8],
  out: &mut [u8],
  width: usize,
  matrix: KernelMatrix,
  full_range: bool,
) {
  unsafe {
    packed444_to_rgb_or_rgba_row::<ALPHA, ALPHA_SRC, 0, 1, 2, 3>(
      packed, out, width, matrix, full_range,
    );
  }
}

// ---- Thin wrappers ------------------------------------------------------

/// AVX-512 VUYA / VUYX → packed **RGB** (3 bpp). Alpha byte in source is
/// discarded — RGB output has no alpha channel.
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn vuya_to_rgb_row(
  packed: &[u8],
  rgb_out: &mut [u8],
  width: usize,
  matrix: KernelMatrix,
  full_range: bool,
) {
  // SAFETY: AVX-512F + AVX-512BW availability is the caller's obligation.
  unsafe {
    vuya_to_rgb_or_rgba_row::<false, false>(packed, rgb_out, width, matrix, full_range);
  }
}

/// AVX-512 VUYA → packed **RGBA** (4 bpp). Source A byte is passed
/// through verbatim.
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn vuya_to_rgba_row(
  packed: &[u8],
  rgba_out: &mut [u8],
  width: usize,
  matrix: KernelMatrix,
  full_range: bool,
) {
  // SAFETY: AVX-512F + AVX-512BW availability is the caller's obligation.
  unsafe {
    vuya_to_rgb_or_rgba_row::<true, true>(packed, rgba_out, width, matrix, full_range);
  }
}

/// AVX-512 VUYX → packed **RGBA** (4 bpp). Source A byte is padding;
/// output α is forced to `0xFF` (opaque).
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn vuyx_to_rgba_row(
  packed: &[u8],
  rgba_out: &mut [u8],
  width: usize,
  matrix: KernelMatrix,
  full_range: bool,
) {
  // SAFETY: AVX-512F + AVX-512BW availability is the caller's obligation.
  unsafe {
    vuya_to_rgb_or_rgba_row::<true, false>(packed, rgba_out, width, matrix, full_range);
  }
}

// ---- Luma extraction (64 px/iter) ---------------------------------------

/// AVX-512 VUYA / VUYX → u8 luma. Y is the third byte (offset 2) of each
/// pixel quadruple.
///
/// Byte-identical to `scalar::vuya_to_luma_row`.
///
/// Block size: 64 pixels per SIMD iteration. Reuses the full 4-channel
/// deinterleave and discards V/U/A: keeping the same code path gives the
/// lane-order regression test the strongest possible coverage — any
/// deinterleave bug in the V/U/Y path manifests identically here.
///
/// # Safety
///
/// 1. **AVX-512F + AVX-512BW must be available.**
/// 2. `packed.len() >= width * 4`.
/// 3. `luma_out.len() >= width`.
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn packed444_to_luma_row<const Y_OFF: usize>(
  packed: &[u8],
  luma_out: &mut [u8],
  width: usize,
) {
  debug_assert!(packed.len() >= width * 4, "packed row too short");
  debug_assert!(luma_out.len() >= width, "luma row too short");

  // SAFETY: AVX-512F + AVX-512BW availability is the caller's obligation.
  unsafe {
    let mut x = 0usize;
    while x + 64 <= width {
      let (_v, _u, y_vec, _a) =
        deinterleave_packed444_avx512::<0, 1, Y_OFF, 3>(packed.as_ptr().add(x * 4));
      _mm512_storeu_si512(luma_out.as_mut_ptr().add(x).cast(), y_vec);
      x += 64;
    }

    // Scalar tail — remaining < 64 pixels.
    if x < width {
      scalar::packed444_to_luma_row::<Y_OFF>(&packed[x * 4..], &mut luma_out[x..], width - x);
    }
  }
}

/// VUYA / VUYX u8 luma (Y at offset 2) over [`packed444_to_luma_row`].
///
/// # Safety
///
/// Same contract as [`packed444_to_luma_row`].
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn vuya_to_luma_row(packed: &[u8], luma_out: &mut [u8], width: usize) {
  unsafe {
    packed444_to_luma_row::<2>(packed, luma_out, width);
  }
}

/// AVX-512 VUYA → u16 luma (zero-extended Y bytes). Y is the third byte
/// (offset 2) of each pixel quadruple. 64 pixels per SIMD iteration.
///
/// Strategy: deinterleave 64 VUYA quadruples to get a `__m512i` of 64 Y
/// bytes. `_mm512_cvtepu8_epi16` widens the low 32 bytes (low 256-bit lane)
/// to u16x32; same for the high 32 bytes. Two `_mm512_storeu_si512` writes
/// output 64 u16 values.
///
/// Byte-identical to `scalar::vuya_to_luma_u16_row`.
///
/// # Safety
///
/// 1. **AVX-512F + AVX-512BW must be available.**
/// 2. `packed.len() >= width * 4`.
/// 3. `out.len() >= width`.
#[cfg_attr(not(any(feature = "std", feature = "alloc")), allow(dead_code))]
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn packed444_to_luma_u16_row<const Y_OFF: usize>(
  packed: &[u8],
  out: &mut [u16],
  width: usize,
) {
  debug_assert!(packed.len() >= width * 4, "packed row too short");
  debug_assert!(out.len() >= width, "out too short");

  // SAFETY: AVX-512F + AVX-512BW availability is the caller's obligation.
  unsafe {
    let mut x = 0usize;
    while x + 64 <= width {
      // Deinterleave 64 quadruples; keep only Y (at `Y_OFF`), u8 × 64.
      let (_v, _u, y_u8, _a) =
        deinterleave_packed444_avx512::<0, 1, Y_OFF, 3>(packed.as_ptr().add(x * 4));

      // Widen low 32 Y bytes → u16x32 (pixels 0-31).
      let lo_u16 = _mm512_cvtepu8_epi16(_mm512_castsi512_si256(y_u8));
      // Widen high 32 Y bytes → u16x32 (pixels 32-63).
      let hi_u16 = _mm512_cvtepu8_epi16(_mm512_extracti64x4_epi64::<1>(y_u8));

      _mm512_storeu_si512(out.as_mut_ptr().add(x).cast(), lo_u16);
      _mm512_storeu_si512(out.as_mut_ptr().add(x + 32).cast(), hi_u16);
      x += 64;
    }

    // Scalar tail — remaining < 64 pixels.
    if x < width {
      scalar::packed444_to_luma_u16_row::<Y_OFF>(&packed[x * 4..], &mut out[x..], width - x);
    }
  }
}

/// VUYA u16 luma (Y at offset 2) over [`packed444_to_luma_u16_row`].
///
/// # Safety
///
/// Same contract as [`packed444_to_luma_u16_row`].
#[cfg_attr(not(any(feature = "std", feature = "alloc")), allow(dead_code))]
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn vuya_to_luma_u16_row(packed: &[u8], out: &mut [u16], width: usize) {
  unsafe {
    packed444_to_luma_u16_row::<2>(packed, out, width);
  }
}

/// AVX-512 VUYX → u16 luma (zero-extended Y bytes). Byte-identical to
/// [`vuya_to_luma_u16_row`] — Y is at byte offset 2 of each quadruple
/// regardless of α semantics; the X byte is discarded.
///
/// # Safety
///
/// 1. **AVX-512F + AVX-512BW must be available.**
/// 2. `packed.len() >= width * 4`.
/// 3. `out.len() >= width`.
#[allow(dead_code)]
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn vuyx_to_luma_u16_row(packed: &[u8], out: &mut [u16], width: usize) {
  // SAFETY: AVX-512F + AVX-512BW availability is the caller's obligation.
  unsafe {
    vuya_to_luma_u16_row(packed, out, width);
  }
}

// ---- VUYA → HSV (staged via a reused 8-bit RGB chunk) ----------------
//
// The SIMD twin of the scalar `vuya_to_hsv_row` kernel. Rather than
// re-derive an HSV-specific register pipeline, it fills a small fixed
// reused 8-bit RGB scratch (one `HSV_CHUNK`-pixel chunk at a time)
// using the EXISTING vuya_to_rgb_row kernel of this file — so the chunk
// filler IS the production 8-bit RGB kernel — then runs the SIMD
// `rgb_to_hsv_row` on the chunk. This makes the result byte-identical to
// `rgb_to_hsv_row(vuya_to_rgb_row(...))` within this SIMD tier with no source-width RGB allocation. The
// scalar tail of the underlying RGB kernel handles widths below the SIMD
// block, so no separate tail is needed here. The α byte (slot 3) is
// dropped by the RGB kernel — HSV is colour-only — so a single kernel
// serves both VUYA (real α) and VUYX (padding); `vuyx_to_hsv_row` is a
// thin re-export.
//
// The chunked driver is defined locally (mirroring the semi-planar
// high-bit `pn_hsv_via_rgb_chunks`) and gated `yuv-444-packed` with the
// rest of this file. Only `rgb_to_hsv_row` (ungated) is shared.

/// One reused 8-bit RGB chunk's worth of pixels staged before the HSV
/// pass.
const HSV_CHUNK: usize = 64;

/// Shared SIMD driver: walks `width` in `HSV_CHUNK`-pixel chunks, fills a
/// small reused stack RGB scratch via `fill_rgb` (the existing SIMD RGB
/// kernel for the format, passed the chunk `offset` and length `n`),
/// then runs the SIMD [`rgb_to_hsv_row`] on that chunk into the H/S/V
/// planes. Byte-identical to `rgb_to_hsv_row(vuya_to_rgb_row(...))` within this SIMD tier, with no
/// source-width RGB allocation.
///
/// `fill_rgb` receives `(offset, n, &mut rgb_chunk)` and must write
/// `n * 3` packed RGB bytes for the `n` pixels at `offset`.
///
/// # Safety
///
/// The SIMD feature must be available, and `fill_rgb` must uphold the
/// underlying RGB kernel's safety contract for each chunk. Each of
/// `h_out` / `s_out` / `v_out` must be `>= width`.
#[inline]
unsafe fn vuya_hsv_via_rgb_chunks(
  h_out: &mut [u8],
  s_out: &mut [u8],
  v_out: &mut [u8],
  width: usize,
  mut fill_rgb: impl FnMut(usize, usize, &mut [u8]),
) {
  let mut scratch = [0u8; HSV_CHUNK * 3];
  let mut offset = 0;
  while offset < width {
    let n = (width - offset).min(HSV_CHUNK);
    fill_rgb(offset, n, &mut scratch[..n * 3]);
    // SAFETY: the SIMD feature is verified by the wrapper's
    // `#[target_feature]`; the chunk and the output sub-slices are all
    // length `n`.
    unsafe {
      rgb_to_hsv_row(
        &scratch[..n * 3],
        &mut h_out[offset..offset + n],
        &mut s_out[offset..offset + n],
        &mut v_out[offset..offset + n],
        n,
      );
    }
    offset += n;
  }
}

/// SIMD: VUYA (packed 4:4:4, 8-bit) → planar HSV bytes (OpenCV
/// encoding), staged via the reused-8-bit-RGB-chunk pattern over the
/// SIMD [`vuya_to_rgb_row`] + [`rgb_to_hsv_row`]. Byte-identical to
/// `rgb_to_hsv_row(vuya_to_rgb_row(...))` within this SIMD tier. The α
/// byte is dropped (HSV is colour-only).
///
/// # Safety
///
/// 1. The SIMD feature must be available.
/// 2. `packed.len() >= width * 4`.
/// 3. `h_out.len()`, `s_out.len()`, `v_out.len()` `>= width`.
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn packed444_to_hsv_row<
  const V_OFF: usize,
  const U_OFF: usize,
  const Y_OFF: usize,
>(
  packed: &[u8],
  h_out: &mut [u8],
  s_out: &mut [u8],
  v_out: &mut [u8],
  width: usize,
  matrix: KernelMatrix,
  full_range: bool,
) {
  debug_assert!(packed.len() >= width * 4, "packed row too short");
  debug_assert!(h_out.len() >= width, "h_out row too short");
  debug_assert!(s_out.len() >= width, "s_out row too short");
  debug_assert!(v_out.len() >= width, "v_out row too short");

  // SAFETY: the feature is the caller's obligation; the chunk filler
  // forwards the per-chunk sub-slices to the offset-generic AVX-512 RGB
  // kernel (no alpha). `A_OFF` unused in the RGB path; reuse `Y_OFF`.
  unsafe {
    vuya_hsv_via_rgb_chunks(h_out, s_out, v_out, width, |offset, n, rgb| {
      packed444_to_rgb_or_rgba_row::<false, false, V_OFF, U_OFF, Y_OFF, Y_OFF>(
        &packed[offset * 4..],
        rgb,
        n,
        matrix,
        full_range,
      );
    });
  }
}

/// VUYA / VUYX HSV (`V=0,U=1,Y=2`) over [`packed444_to_hsv_row`].
///
/// # Safety
///
/// Same contract as [`packed444_to_hsv_row`].
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn vuya_to_hsv_row(
  packed: &[u8],
  h_out: &mut [u8],
  s_out: &mut [u8],
  v_out: &mut [u8],
  width: usize,
  matrix: KernelMatrix,
  full_range: bool,
) {
  unsafe {
    packed444_to_hsv_row::<0, 1, 2>(packed, h_out, s_out, v_out, width, matrix, full_range);
  }
}

// ---- AYUV / UYVA AVX-512 wrappers -------------------------------------

/// AVX-512 AYUV (`A=0,Y=1,U=2,V=3`) → packed RGB.
///
/// # Safety
///
/// Same contract as [`packed444_to_rgb_or_rgba_row`].
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn ayuv_to_rgb_row(
  packed: &[u8],
  rgb_out: &mut [u8],
  width: usize,
  matrix: KernelMatrix,
  full_range: bool,
) {
  unsafe {
    packed444_to_rgb_or_rgba_row::<false, false, 3, 2, 1, 0>(
      packed, rgb_out, width, matrix, full_range,
    );
  }
}

/// AVX-512 AYUV → packed RGBA (source α at offset 0).
///
/// # Safety
///
/// Same contract as [`packed444_to_rgb_or_rgba_row`].
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn ayuv_to_rgba_row(
  packed: &[u8],
  rgba_out: &mut [u8],
  width: usize,
  matrix: KernelMatrix,
  full_range: bool,
) {
  unsafe {
    packed444_to_rgb_or_rgba_row::<true, true, 3, 2, 1, 0>(
      packed, rgba_out, width, matrix, full_range,
    );
  }
}

/// AVX-512 AYUV → planar HSV bytes.
///
/// # Safety
///
/// Same contract as [`packed444_to_hsv_row`].
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn ayuv_to_hsv_row(
  packed: &[u8],
  h_out: &mut [u8],
  s_out: &mut [u8],
  v_out: &mut [u8],
  width: usize,
  matrix: KernelMatrix,
  full_range: bool,
) {
  unsafe {
    packed444_to_hsv_row::<3, 2, 1>(packed, h_out, s_out, v_out, width, matrix, full_range);
  }
}

/// AVX-512 AYUV → u8 luma (Y at offset 1).
///
/// # Safety
///
/// Same contract as [`packed444_to_luma_row`].
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn ayuv_to_luma_row(packed: &[u8], luma_out: &mut [u8], width: usize) {
  unsafe {
    packed444_to_luma_row::<1>(packed, luma_out, width);
  }
}

/// AVX-512 AYUV → u16 luma (Y at offset 1).
///
/// # Safety
///
/// Same contract as [`packed444_to_luma_u16_row`].
#[cfg_attr(not(any(feature = "std", feature = "alloc")), allow(dead_code))]
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn ayuv_to_luma_u16_row(packed: &[u8], out: &mut [u16], width: usize) {
  unsafe {
    packed444_to_luma_u16_row::<1>(packed, out, width);
  }
}

/// AVX-512 UYVA (`U=0,Y=1,V=2,A=3`) → packed RGB.
///
/// # Safety
///
/// Same contract as [`packed444_to_rgb_or_rgba_row`].
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn uyva_to_rgb_row(
  packed: &[u8],
  rgb_out: &mut [u8],
  width: usize,
  matrix: KernelMatrix,
  full_range: bool,
) {
  unsafe {
    packed444_to_rgb_or_rgba_row::<false, false, 2, 0, 1, 3>(
      packed, rgb_out, width, matrix, full_range,
    );
  }
}

/// AVX-512 UYVA → packed RGBA (source α at offset 3).
///
/// # Safety
///
/// Same contract as [`packed444_to_rgb_or_rgba_row`].
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn uyva_to_rgba_row(
  packed: &[u8],
  rgba_out: &mut [u8],
  width: usize,
  matrix: KernelMatrix,
  full_range: bool,
) {
  unsafe {
    packed444_to_rgb_or_rgba_row::<true, true, 2, 0, 1, 3>(
      packed, rgba_out, width, matrix, full_range,
    );
  }
}

/// AVX-512 UYVA → planar HSV bytes.
///
/// # Safety
///
/// Same contract as [`packed444_to_hsv_row`].
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn uyva_to_hsv_row(
  packed: &[u8],
  h_out: &mut [u8],
  s_out: &mut [u8],
  v_out: &mut [u8],
  width: usize,
  matrix: KernelMatrix,
  full_range: bool,
) {
  unsafe {
    packed444_to_hsv_row::<2, 0, 1>(packed, h_out, s_out, v_out, width, matrix, full_range);
  }
}

/// AVX-512 UYVA → u8 luma (Y at offset 1).
///
/// # Safety
///
/// Same contract as [`packed444_to_luma_row`].
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn uyva_to_luma_row(packed: &[u8], luma_out: &mut [u8], width: usize) {
  unsafe {
    packed444_to_luma_row::<1>(packed, luma_out, width);
  }
}

/// AVX-512 UYVA → u16 luma (Y at offset 1).
///
/// # Safety
///
/// Same contract as [`packed444_to_luma_u16_row`].
#[cfg_attr(not(any(feature = "std", feature = "alloc")), allow(dead_code))]
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn uyva_to_luma_u16_row(packed: &[u8], out: &mut [u16], width: usize) {
  unsafe {
    packed444_to_luma_u16_row::<1>(packed, out, width);
  }
}

// ---- VYU444 (V=0, Y=1, U=2; 3 bytes per pixel, no alpha) AVX-512 -------
//
// A 3-byte de-interleave does not tile onto 512-bit lanes, so — exactly
// like the AVX-512 RGB-input HSV / luma kernels, which reuse the 16-pixel
// SSE-width `x86_common::deinterleave_rgb_16` / `rgb_to_hsv_16_pixels`
// helpers — the AVX-512 VYU444 entry points dispatch to the SSE4.1 16-px
// 3-byte-de-interleave kernel. AVX-512BW is a strict superset of SSE4.1,
// so the call is sound; the de-interleave (the throughput floor for this
// layout) runs at its natural 128-bit width. Byte-identical to the scalar
// reference and to the NEON `vld3q_u8` tier.

/// AVX-512 VYU444 → packed RGB (via the SSE4.1 16-px 3-byte kernel).
///
/// # Safety
///
/// `packed.len() >= width * 3`; `rgb_out.len() >= width * 3`. AVX-512BW
/// (⊇ SSE4.1) must be available.
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn vyu444_to_rgb_row(
  packed: &[u8],
  rgb_out: &mut [u8],
  width: usize,
  matrix: KernelMatrix,
  full_range: bool,
) {
  unsafe {
    crate::row::arch::x86_sse41::vyu444_to_rgb_row(packed, rgb_out, width, matrix, full_range);
  }
}

/// AVX-512 VYU444 → packed RGBA (α forced `0xFF`; via the SSE4.1 kernel).
///
/// # Safety
///
/// `packed.len() >= width * 3`; `rgba_out.len() >= width * 4`. AVX-512BW
/// (⊇ SSE4.1) must be available.
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn vyu444_to_rgba_row(
  packed: &[u8],
  rgba_out: &mut [u8],
  width: usize,
  matrix: KernelMatrix,
  full_range: bool,
) {
  unsafe {
    crate::row::arch::x86_sse41::vyu444_to_rgba_row(packed, rgba_out, width, matrix, full_range);
  }
}

/// AVX-512 VYU444 → planar HSV bytes (via the SSE4.1 kernel).
///
/// # Safety
///
/// `packed.len() >= width * 3`; each H/S/V plane `>= width`. AVX-512BW
/// (⊇ SSE4.1) must be available.
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn vyu444_to_hsv_row(
  packed: &[u8],
  h_out: &mut [u8],
  s_out: &mut [u8],
  v_out: &mut [u8],
  width: usize,
  matrix: KernelMatrix,
  full_range: bool,
) {
  unsafe {
    crate::row::arch::x86_sse41::vyu444_to_hsv_row(
      packed, h_out, s_out, v_out, width, matrix, full_range,
    );
  }
}

/// AVX-512 VYU444 → u8 luma (Y at offset 1, 3-byte stride; via the SSE4.1
/// kernel).
///
/// # Safety
///
/// `packed.len() >= width * 3`; `luma_out.len() >= width`. AVX-512BW (⊇
/// SSE4.1) must be available.
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn vyu444_to_luma_row(packed: &[u8], luma_out: &mut [u8], width: usize) {
  unsafe {
    crate::row::arch::x86_sse41::vyu444_to_luma_row(packed, luma_out, width);
  }
}

/// AVX-512 VYU444 → u16 luma (Y at offset 1, 3-byte stride; via the SSE4.1
/// kernel).
///
/// # Safety
///
/// `packed.len() >= width * 3`; `out.len() >= width`. AVX-512BW (⊇ SSE4.1)
/// must be available.
#[cfg_attr(not(any(feature = "std", feature = "alloc")), allow(dead_code))]
#[inline]
#[target_feature(enable = "avx512f,avx512bw")]
pub(crate) unsafe fn vyu444_to_luma_u16_row(packed: &[u8], out: &mut [u16], width: usize) {
  unsafe {
    crate::row::arch::x86_sse41::vyu444_to_luma_u16_row(packed, out, width);
  }
}