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
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
//! NEON kernels for the Tier 9 packed-float-RGB (`Rgbf32`) source.
//!
//! Each kernel processes 4 `f32` lanes per iteration via NEON's
//! `float32x4_t` registers. The conversions are componentwise:
//!
//! ```text
//!   clamped = max(min(v, 1.0), 0.0)
//!   scaled  = clamped * out_max         // round-to-nearest (vcvtnq_*)
//!   out_int = saturating_cast(scaled)   // already in-range after clamp
//! ```
//!
//! Operating in `f32` lanes means the loop is **lane-grouped**, not
//! pixel-grouped: the 4-lane vector covers e.g. `[R0, G0, B0, R1]`.
//! That is fine for the integer-output kernels (we narrow the lane
//! vector to 4 bytes / 4 u16 elements with `vst*` straight into the
//! `R, G, B, R, …` packed output) and trivially fine for the lossless
//! `f32` pass-through (just `vst1q_f32`).
//!
//! For `<const BE: bool>` kernels, each 4-lane f32 load is replaced by
//! an endian-aware u32x4 load (via `load_endian_u32x4::<BE>`) followed
//! by a `vreinterpretq_f32_u32` cast. For LE (BE=false) this is a
//! pure load; for BE it adds a `vrev32q_u8` byte-swap.

#[cfg_attr(miri, allow(unused_imports))]
use core::arch::aarch64::*;

use super::{endian::load_endian_u32x4, miri_compat::*, scalar};

/// Load 4 `f32` lanes from `ptr` in endian-aware fashion.
/// `BE = false` → host-native load (identical to `vld1q_f32`).
/// `BE = true`  → load as u32 with byte-swap, then reinterpret as f32.
///
/// # Safety
///
/// * NEON must be available.
/// * `ptr` must be valid for 16 bytes.
#[inline(always)]
unsafe fn load_f32x4<const BE: bool>(ptr: *const f32) -> float32x4_t {
  unsafe {
    let u = load_endian_u32x4::<BE>(ptr as *const u8);
    vreinterpretq_f32_u32(u)
  }
}

/// f32 RGB → u8 RGB. Clamp `[0, 1]` x 255, saturating round-to-nearest
/// cast.
///
/// When `BE = true` the input `f32` values are big-endian encoded.
///
/// # Safety
///
/// 1. NEON must be available (`is_aarch64_feature_detected!("neon")`).
/// 2. `rgb_in.len() >= 3 * width`; `rgb_out.len() >= 3 * width`.
/// 3. `rgb_in` / `rgb_out` must not alias.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn rgbf32_to_rgb_row<const BE: bool>(
  rgb_in: &[f32],
  rgb_out: &mut [u8],
  width: usize,
) {
  debug_assert!(rgb_in.len() >= width * 3, "rgbf32 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");

  unsafe {
    let zero = vdupq_n_f32(0.0);
    let one = vdupq_n_f32(1.0);
    let scale = vdupq_n_f32(255.0);

    // Iterate in **lane-multiples-of-3 = pixel-aligned** chunks. We
    // process 12 f32 lanes (= 4 pixels = 12 output bytes) per iter so
    // every chunk lands on a pixel boundary.
    let total_lanes = width * 3;
    let mut lane = 0usize;
    while lane + 12 <= total_lanes {
      let v0 = load_f32x4::<BE>(rgb_in.as_ptr().add(lane));
      let v1 = load_f32x4::<BE>(rgb_in.as_ptr().add(lane + 4));
      let v2 = load_f32x4::<BE>(rgb_in.as_ptr().add(lane + 8));

      let s0 = vmulq_f32(vminq_f32_compat(vmaxq_f32_compat(v0, zero), one), scale);
      let s1 = vmulq_f32(vminq_f32_compat(vmaxq_f32_compat(v1, zero), one), scale);
      let s2 = vmulq_f32(vminq_f32_compat(vmaxq_f32_compat(v2, zero), one), scale);

      let u0 = vqmovn_u32_compat(vcvtnq_u32_f32_compat(s0));
      let u1 = vqmovn_u32_compat(vcvtnq_u32_f32_compat(s1));
      let u2 = vqmovn_u32_compat(vcvtnq_u32_f32_compat(s2));

      // Narrow each u16x4 to u8x4 via vqmovn_u16(vcombine_u16(x, x))
      // and emit 12 bytes via three 4-byte stores.
      let b0 = vqmovn_u16_compat(vcombine_u16(u0, u0));
      let b1 = vqmovn_u16_compat(vcombine_u16(u1, u1));
      let b2 = vqmovn_u16_compat(vcombine_u16(u2, u2));

      let mut tmp = [0u8; 8];
      vst1_u8(tmp.as_mut_ptr(), b0);
      rgb_out
        .get_unchecked_mut(lane..lane + 4)
        .copy_from_slice(&tmp[..4]);
      vst1_u8(tmp.as_mut_ptr(), b1);
      rgb_out
        .get_unchecked_mut(lane + 4..lane + 8)
        .copy_from_slice(&tmp[..4]);
      vst1_u8(tmp.as_mut_ptr(), b2);
      rgb_out
        .get_unchecked_mut(lane + 8..lane + 12)
        .copy_from_slice(&tmp[..4]);

      lane += 12;
    }

    // Scalar tail handles the leftover 0–3 pixels.
    let pix_done = lane / 3;
    let tail_pix = width - pix_done;
    if tail_pix > 0 {
      scalar::rgbf32_to_rgb_row::<BE>(
        &rgb_in[pix_done * 3..width * 3],
        &mut rgb_out[pix_done * 3..width * 3],
        tail_pix,
      );
    }
  }
}

/// f32 RGB → u8 RGBA (alpha forced to `0xFF`).
///
/// When `BE = true` the input `f32` values are big-endian encoded.
///
/// # Safety
///
/// Same as [`rgbf32_to_rgb_row`] but `rgba_out.len() >= 4 * width`.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn rgbf32_to_rgba_row<const BE: bool>(
  rgb_in: &[f32],
  rgba_out: &mut [u8],
  width: usize,
) {
  debug_assert!(rgb_in.len() >= width * 3, "rgbf32 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");

  unsafe {
    let zero = vdupq_n_f32(0.0);
    let one = vdupq_n_f32(1.0);
    let scale = vdupq_n_f32(255.0);
    let alpha = vdupq_n_u8(0xFF);

    // Process 4 pixels per iteration — 12 input lanes → 16 output bytes
    // (R0 G0 B0 A0 R1 G1 B1 A1 …) using `vst4q_u8` with the 4 channels
    // gathered into separate u8x16 vectors.
    //
    // For a 4-pixel iteration we need 4 R lanes, 4 G lanes, 4 B lanes
    // — gathered with strided `vld3q_f32` then converted lanewise.
    let mut x = 0usize;
    while x + 16 <= width {
      let mut r_bytes = [0u8; 16];
      let mut g_bytes = [0u8; 16];
      let mut b_bytes = [0u8; 16];
      // Inner loop covers 16 pixels (4 NEON 4-pixel sub-blocks). We
      // fall back to per-pixel scalar conversion of the loaded lanes
      // — the f32→u8 cast itself is the cost, not the gather.
      for sub in 0..4 {
        let base = (x + sub * 4) * 3;
        // Fast path: on-disk encoding (`BE`) matches host-native, so the raw
        // `vld3q_f32` reads host-native bytes that already carry the right f32
        // values. Slow path (`BE != HOST_NATIVE_BE`): each f32 must be byte-
        // swapped before deinterleave; load via the endian-aware helper into
        // a contiguous buffer, then unstride into per-channel vectors.
        let (r_v, g_v, b_v) = if BE == HOST_NATIVE_BE {
          let v_rgb = vld3q_f32(rgb_in.as_ptr().add(base));
          (v_rgb.0, v_rgb.1, v_rgb.2)
        } else {
          let raw0 = load_f32x4::<BE>(rgb_in.as_ptr().add(base));
          let raw1 = load_f32x4::<BE>(rgb_in.as_ptr().add(base + 4));
          let raw2 = load_f32x4::<BE>(rgb_in.as_ptr().add(base + 8));
          // Manual deinterleave: contiguous host-native f32 layout is
          // [R0,G0,B0,R1, G1,B1,R2,G2, B2,R3,G3,B3] across raw{0,1,2}.
          let mut r_arr = [0.0f32; 4];
          let mut g_arr = [0.0f32; 4];
          let mut b_arr = [0.0f32; 4];
          vst1q_f32(r_arr.as_mut_ptr(), raw0);
          vst1q_f32(g_arr.as_mut_ptr(), raw1);
          vst1q_f32(b_arr.as_mut_ptr(), raw2);
          let r_deint = [r_arr[0], r_arr[3], g_arr[2], b_arr[1]];
          let g_deint = [r_arr[1], g_arr[0], g_arr[3], b_arr[2]];
          let b_deint = [r_arr[2], g_arr[1], b_arr[0], b_arr[3]];
          (
            vld1q_f32(r_deint.as_ptr()),
            vld1q_f32(g_deint.as_ptr()),
            vld1q_f32(b_deint.as_ptr()),
          )
        };

        let r_clamped = vmulq_f32(vminq_f32_compat(vmaxq_f32_compat(r_v, zero), one), scale);
        let g_clamped = vmulq_f32(vminq_f32_compat(vmaxq_f32_compat(g_v, zero), one), scale);
        let b_clamped = vmulq_f32(vminq_f32_compat(vmaxq_f32_compat(b_v, zero), one), scale);

        let r_u32 = vcvtnq_u32_f32_compat(r_clamped);
        let g_u32 = vcvtnq_u32_f32_compat(g_clamped);
        let b_u32 = vcvtnq_u32_f32_compat(b_clamped);

        // Narrow u32x4 → u8x4, store directly into the per-channel
        // staging arrays. Each sub block contributes 4 bytes.
        let r_u16 = vqmovn_u32_compat(r_u32);
        let g_u16 = vqmovn_u32_compat(g_u32);
        let b_u16 = vqmovn_u32_compat(b_u32);
        // vqmovn produces a 4-element u16 vector; combine with itself
        // to make 8, narrow with vqmovn_u16, then write 4 bytes from
        // the low half.
        let r_u8 = vqmovn_u16_compat(vcombine_u16(r_u16, r_u16));
        let g_u8 = vqmovn_u16_compat(vcombine_u16(g_u16, g_u16));
        let b_u8 = vqmovn_u16_compat(vcombine_u16(b_u16, b_u16));
        let mut tmp = [0u8; 8];
        vst1_u8(tmp.as_mut_ptr(), r_u8);
        r_bytes[sub * 4..sub * 4 + 4].copy_from_slice(&tmp[..4]);
        vst1_u8(tmp.as_mut_ptr(), g_u8);
        g_bytes[sub * 4..sub * 4 + 4].copy_from_slice(&tmp[..4]);
        vst1_u8(tmp.as_mut_ptr(), b_u8);
        b_bytes[sub * 4..sub * 4 + 4].copy_from_slice(&tmp[..4]);
      }
      let r = vld1q_u8(r_bytes.as_ptr());
      let g = vld1q_u8(g_bytes.as_ptr());
      let b = vld1q_u8(b_bytes.as_ptr());
      let rgba = uint8x16x4_t(r, g, b, alpha);
      vst4q_u8(rgba_out.as_mut_ptr().add(x * 4), rgba);
      x += 16;
    }

    if x < width {
      scalar::rgbf32_to_rgba_row::<BE>(
        &rgb_in[x * 3..width * 3],
        &mut rgba_out[x * 4..width * 4],
        width - x,
      );
    }
  }
}

/// f32 RGB → u16 RGB. Clamp `[0, 1]` x 65535, saturating cast.
///
/// When `BE = true` the input `f32` values are big-endian encoded.
///
/// # Safety
///
/// Same as [`rgbf32_to_rgb_row`] but `rgb_out` is `&mut [u16]` with
/// `len() >= 3 * width` u16 elements.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn rgbf32_to_rgb_u16_row<const BE: bool>(
  rgb_in: &[f32],
  rgb_out: &mut [u16],
  width: usize,
) {
  debug_assert!(rgb_in.len() >= width * 3, "rgbf32 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_u16_out row too short");

  unsafe {
    let zero = vdupq_n_f32(0.0);
    let one = vdupq_n_f32(1.0);
    let scale = vdupq_n_f32(65535.0);

    // Process pixel-aligned chunks of 4 pixels = 12 lanes per iter.
    let total_lanes = width * 3;
    let mut lane = 0usize;
    while lane + 12 <= total_lanes {
      let v0 = load_f32x4::<BE>(rgb_in.as_ptr().add(lane));
      let v1 = load_f32x4::<BE>(rgb_in.as_ptr().add(lane + 4));
      let v2 = load_f32x4::<BE>(rgb_in.as_ptr().add(lane + 8));

      let s0 = vmulq_f32(vminq_f32_compat(vmaxq_f32_compat(v0, zero), one), scale);
      let s1 = vmulq_f32(vminq_f32_compat(vmaxq_f32_compat(v1, zero), one), scale);
      let s2 = vmulq_f32(vminq_f32_compat(vmaxq_f32_compat(v2, zero), one), scale);
      let u0 = vqmovn_u32_compat(vcvtnq_u32_f32_compat(s0));
      let u1 = vqmovn_u32_compat(vcvtnq_u32_f32_compat(s1));
      let u2 = vqmovn_u32_compat(vcvtnq_u32_f32_compat(s2));

      vst1_u16(rgb_out.as_mut_ptr().add(lane), u0);
      vst1_u16(rgb_out.as_mut_ptr().add(lane + 4), u1);
      vst1_u16(rgb_out.as_mut_ptr().add(lane + 8), u2);
      lane += 12;
    }
    let pix_done = lane / 3;
    let tail_pix = width - pix_done;
    if tail_pix > 0 {
      scalar::rgbf32_to_rgb_u16_row::<BE>(
        &rgb_in[pix_done * 3..width * 3],
        &mut rgb_out[pix_done * 3..width * 3],
        tail_pix,
      );
    }
  }
}

/// f32 RGB → u16 RGBA (alpha forced to `0xFFFF`).
///
/// When `BE = true` the input `f32` values are big-endian encoded.
///
/// # Safety
///
/// Same as [`rgbf32_to_rgb_u16_row`] but the output is `&mut [u16]`
/// with `len() >= 4 * width` u16 elements.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn rgbf32_to_rgba_u16_row<const BE: bool>(
  rgb_in: &[f32],
  rgba_out: &mut [u16],
  width: usize,
) {
  debug_assert!(rgb_in.len() >= width * 3, "rgbf32 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_u16_out row too short");

  unsafe {
    let zero = vdupq_n_f32(0.0);
    let one = vdupq_n_f32(1.0);
    let scale = vdupq_n_f32(65535.0);
    let alpha_v = vdupq_n_u16(0xFFFF);

    let mut x = 0usize;
    while x + 8 <= width {
      // 8 pixels = 24 input lanes processed in three vld3q_f32 blocks
      // (each loads 4 pixels). We split into two 4-pixel sub-blocks.
      //
      // For each 4-pixel sub-block: deinterleave R, G, B → three
      // f32x4 vectors, clamp+scale+convert, then narrow each 4-element
      // u32 vector to 4 u16. Build R/G/B/A as u16x8 vectors and
      // interleave with `vst4q_u16`.
      let mut r_h = [0u16; 8];
      let mut g_h = [0u16; 8];
      let mut b_h = [0u16; 8];
      for sub in 0..2 {
        let base = (x + sub * 4) * 3;
        // Fast path: `BE == HOST_NATIVE_BE` means on-disk encoding matches the
        // host's native byte order, so `vld3q_f32` (which always reads host-
        // native bytes) decodes the encoded f32s correctly. Slow path: the
        // encoded bytes are foreign — load each f32 through the endian-aware
        // helper (which byte-swaps when `BE != HOST_NATIVE_BE`) into a
        // contiguous buffer, then deinterleave into per-channel vectors.
        let (r_v, g_v, b_v) = if BE == HOST_NATIVE_BE {
          let v_rgb = vld3q_f32(rgb_in.as_ptr().add(base));
          (v_rgb.0, v_rgb.1, v_rgb.2)
        } else {
          let raw0 = load_f32x4::<BE>(rgb_in.as_ptr().add(base));
          let raw1 = load_f32x4::<BE>(rgb_in.as_ptr().add(base + 4));
          let raw2 = load_f32x4::<BE>(rgb_in.as_ptr().add(base + 8));
          let mut r_arr = [0.0f32; 4];
          let mut g_arr = [0.0f32; 4];
          let mut b_arr = [0.0f32; 4];
          vst1q_f32(r_arr.as_mut_ptr(), raw0);
          vst1q_f32(g_arr.as_mut_ptr(), raw1);
          vst1q_f32(b_arr.as_mut_ptr(), raw2);
          let r_deint = [r_arr[0], r_arr[3], g_arr[2], b_arr[1]];
          let g_deint = [r_arr[1], g_arr[0], g_arr[3], b_arr[2]];
          let b_deint = [r_arr[2], g_arr[1], b_arr[0], b_arr[3]];
          (
            vld1q_f32(r_deint.as_ptr()),
            vld1q_f32(g_deint.as_ptr()),
            vld1q_f32(b_deint.as_ptr()),
          )
        };

        let r_s = vmulq_f32(vminq_f32_compat(vmaxq_f32_compat(r_v, zero), one), scale);
        let g_s = vmulq_f32(vminq_f32_compat(vmaxq_f32_compat(g_v, zero), one), scale);
        let b_s = vmulq_f32(vminq_f32_compat(vmaxq_f32_compat(b_v, zero), one), scale);
        let r_u = vqmovn_u32_compat(vcvtnq_u32_f32_compat(r_s));
        let g_u = vqmovn_u32_compat(vcvtnq_u32_f32_compat(g_s));
        let b_u = vqmovn_u32_compat(vcvtnq_u32_f32_compat(b_s));
        vst1_u16(r_h.as_mut_ptr().add(sub * 4), r_u);
        vst1_u16(g_h.as_mut_ptr().add(sub * 4), g_u);
        vst1_u16(b_h.as_mut_ptr().add(sub * 4), b_u);
      }
      let r = vld1q_u16(r_h.as_ptr());
      let g = vld1q_u16(g_h.as_ptr());
      let b = vld1q_u16(b_h.as_ptr());
      let rgba = uint16x8x4_t(r, g, b, alpha_v);
      vst4q_u16(rgba_out.as_mut_ptr().add(x * 4), rgba);
      x += 8;
    }

    if x < width {
      scalar::rgbf32_to_rgba_u16_row::<BE>(
        &rgb_in[x * 3..width * 3],
        &mut rgba_out[x * 4..width * 4],
        width - x,
      );
    }
  }
}

/// f32 RGB → f32 RGB lossless pass-through.
///
/// When `BE = true` the input values are byte-swapped to host-native
/// before being written (big-endian input → host-native output).
///
/// # Safety
///
/// Same as [`rgbf32_to_rgb_row`] but `rgb_out` is `&mut [f32]` with
/// `len() >= 3 * width` f32 elements.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn rgbf32_to_rgb_f32_row<const BE: bool>(
  rgb_in: &[f32],
  rgb_out: &mut [f32],
  width: usize,
) {
  debug_assert!(rgb_in.len() >= width * 3, "rgbf32 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_f32_out row too short");

  unsafe {
    let total = width * 3;
    let mut i = 0usize;
    // Fast path: when the requested encoding (BE) matches the host's native
    // endian, the bytes can be copied verbatim — `vld1q_f32` reads host-native
    // bytes which is exactly what we need to emit. Otherwise we must decode
    // through `load_f32x4::<BE>` (which byte-swaps when BE != host-native) so
    // the stored host-native f32 round-trips back to the same value.
    if BE == HOST_NATIVE_BE {
      while i + 4 <= total {
        let v = vld1q_f32(rgb_in.as_ptr().add(i));
        vst1q_f32(rgb_out.as_mut_ptr().add(i), v);
        i += 4;
      }
      while i < total {
        *rgb_out.get_unchecked_mut(i) = *rgb_in.get_unchecked(i);
        i += 1;
      }
    } else {
      // Encoding doesn't match host: decode each lane to host-native.
      while i + 4 <= total {
        let v = load_f32x4::<BE>(rgb_in.as_ptr().add(i));
        vst1q_f32(rgb_out.as_mut_ptr().add(i), v);
        i += 4;
      }
      while i < total {
        let bits = (*rgb_in.get_unchecked(i)).to_bits();
        let host_bits = if BE {
          u32::from_be(bits)
        } else {
          u32::from_le(bits)
        };
        *rgb_out.get_unchecked_mut(i) = f32::from_bits(host_bits);
        i += 1;
      }
    }
  }
}

// ---- Tier 9 — Rgbf16 NEON entry points ------------------------------------
//
// Strategy: widen a chunk of `f16` lanes to `f32` into a stack buffer, then
// delegate to the existing NEON Rgbf32 downstream kernels. The chunk size is
// 4 pixels (= 12 f16 values) which matches the Rgbf32 loop granularity.
//
// `vcvt_f32_f16` widens 4 x f16 to 4 x f32 in a single FCVT instruction.
//
// For BE: we load the u16 bits via `load_endian_u16x4::<BE>` (loads 4 u16 with
// byte-swap for BE) into a `uint16x4_t`, then reinterpret as `float16x4_t`
// before widening with `vcvt_f32_f16`.  `load_endian_u16x4` reads exactly
// 8 bytes regardless of `BE`, matching the 4 x f16 region the kernel owns
// (a 16-byte load via `load_endian_u16x8` would tail-overread).

use super::endian::load_endian_u16x4;

/// `BE` value that makes the f32 row loaders treat their input as host-native
/// (a no-op byte-swap). Used by f16→f32 widen-then-convert paths whose stack
/// buffer is already host-native after `vcvt_f32_f16`. On a LE target, host-
/// native == LE so `BE = false`; on a BE target, host-native == BE so
/// `BE = true`. Without this routing the downstream `rgbf32_to_*::<false>`
/// would byte-swap an already-decoded host-native f32 buffer on BE hosts.
///
/// Also used by the `rgbf32_to_rgb_f32_row` pass-through fast path: the raw
/// `vld1q_f32`/`vst1q_f32` copy is byte-correct only when the source encoding
/// (`BE`) matches the host's native endian, so the kernel falls through to
/// the endian-aware `load_f32x4::<BE>` slow path otherwise.
///
/// Same gate applies to the `rgbf32_to_rgba_row` / `rgbf32_to_rgba_u16_row`
/// `vld3q_f32` deinterleave fast path: `vld3q_f32` reads host-native bytes,
/// so it's only correct when the on-disk encoding matches host-native.
/// Otherwise the kernel falls through to the endian-aware `load_f32x4::<BE>`
/// path with a manual deinterleave.
const HOST_NATIVE_BE: bool = cfg!(target_endian = "big");

/// Widen 4 half-precision floats (`f16x4`, i.e. 8 bytes starting at `ptr`)
/// to 4 single-precision floats into `out[0..4]`.
///
/// For `BE = true` the f16 values are stored big-endian (bytes swapped);
/// the byte-swap is applied before the widening conversion. The loader reads
/// exactly 8 bytes regardless of `BE` so the caller's `ptr` only needs 8
/// readable bytes (a 16-byte load via `load_endian_u16x8` would tail-overread
/// the 4 x f16 region the kernel actually owns).
///
/// # Safety
///
/// * NEON must be available.
/// * `ptr` must be valid for 4 x u16 reads (8 bytes).
/// * `out` must be valid for 4 x f32 writes.
#[inline(always)]
unsafe fn widen_f16x4<const BE: bool>(ptr: *const half::f16, out: *mut f32) {
  unsafe {
    // 8-byte load (4 x u16), byte-swapped per-lane when BE = true so the
    // resulting `uint16x4_t` carries host-native f16 bit patterns ready for
    // `vcvt_f32_f16`.
    let u16x4 = load_endian_u16x4::<BE>(ptr as *const u8);
    let f16x4 = vreinterpret_f16_u16(u16x4);
    let f32x4 = vcvt_f32_f16(f16x4);
    vst1q_f32(out, f32x4);
  }
}

/// Widen `n` half-precision floats (at most 4) from `src` to `f32` in `dst`.
/// `n` must be in `[0, 4]` — `n == 0` is a no-op (the caller passes
/// `total_lanes - lane`, which is `0` when `total_lanes` is a multiple of 4
/// and the SIMD loop consumed the whole row).
///
/// For `BE = true` the source f16 bits are decoded from big-endian to
/// host-native before widening; for `BE = false` they are read as host-
/// native (identical to a plain LE load on every shipping target). This
/// matches the SIMD body's `widen_f16x4::<BE>` semantics so partial-pixel
/// tail bytes round-trip identically to the full-vector path.
#[inline(always)]
unsafe fn widen_f16_tail<const BE: bool>(src: &[half::f16], dst: &mut [f32], n: usize) {
  for i in 0..n {
    unsafe {
      let raw = src.get_unchecked(i).to_bits();
      let host_bits = if BE {
        u16::from_be(raw)
      } else {
        u16::from_le(raw)
      };
      *dst.get_unchecked_mut(i) = half::f16::from_bits(host_bits).to_f32();
    }
  }
}

/// f16 RGB → u8 RGB.
///
/// When `BE = true` the input `half::f16` values are big-endian encoded.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `rgb_in.len() >= 3 * width`; `rgb_out.len() >= 3 * width`.
/// 3. `rgb_in` / `rgb_out` must not alias.
#[inline]
#[target_feature(enable = "neon,fp16")]
pub(crate) unsafe fn rgbf16_to_rgb_row<const BE: bool>(
  rgb_in: &[half::f16],
  rgb_out: &mut [u8],
  width: usize,
) {
  debug_assert!(rgb_in.len() >= width * 3, "rgbf16 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");

  // Process 4 pixels (12 f16 lanes = 12 f32 lanes) per iteration.
  let total_lanes = width * 3;
  let mut lane = 0usize;
  while lane + 12 <= total_lanes {
    let mut buf = [0.0f32; 12];
    unsafe {
      widen_f16x4::<BE>(rgb_in.as_ptr().add(lane), buf.as_mut_ptr());
      widen_f16x4::<BE>(rgb_in.as_ptr().add(lane + 4), buf.as_mut_ptr().add(4));
      widen_f16x4::<BE>(rgb_in.as_ptr().add(lane + 8), buf.as_mut_ptr().add(8));
      // Buffer is host-native f32 after vcvt_f32_f16; route through the f32
      // kernel with HOST_NATIVE_BE so its loaders perform a no-op swap.
      rgbf32_to_rgb_row::<HOST_NATIVE_BE>(&buf, rgb_out.get_unchecked_mut(lane..lane + 12), 4);
    }
    lane += 12;
  }
  let pix_done = lane / 3;
  if pix_done < width {
    scalar::rgbf16_to_rgb_row::<BE>(
      &rgb_in[pix_done * 3..width * 3],
      &mut rgb_out[pix_done * 3..width * 3],
      width - pix_done,
    );
  }
}

/// f16 RGB → u8 RGBA (alpha forced to `0xFF`).
///
/// When `BE = true` the input `half::f16` values are big-endian encoded.
///
/// # Safety
///
/// Same as [`rgbf16_to_rgb_row`] but `rgba_out.len() >= 4 * width`.
#[inline]
#[target_feature(enable = "neon,fp16")]
pub(crate) unsafe fn rgbf16_to_rgba_row<const BE: bool>(
  rgb_in: &[half::f16],
  rgba_out: &mut [u8],
  width: usize,
) {
  debug_assert!(rgb_in.len() >= width * 3, "rgbf16 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");

  let total_lanes = width * 3;
  let mut lane = 0usize;
  let mut pix = 0usize;
  while lane + 12 <= total_lanes {
    let mut buf = [0.0f32; 12];
    unsafe {
      widen_f16x4::<BE>(rgb_in.as_ptr().add(lane), buf.as_mut_ptr());
      widen_f16x4::<BE>(rgb_in.as_ptr().add(lane + 4), buf.as_mut_ptr().add(4));
      widen_f16x4::<BE>(rgb_in.as_ptr().add(lane + 8), buf.as_mut_ptr().add(8));
      // Buffer is host-native f32; route via HOST_NATIVE_BE (see widen_f16x4).
      rgbf32_to_rgba_row::<HOST_NATIVE_BE>(
        &buf,
        rgba_out.get_unchecked_mut(pix * 4..pix * 4 + 16),
        4,
      );
    }
    lane += 12;
    pix += 4;
  }
  if pix < width {
    scalar::rgbf16_to_rgba_row::<BE>(
      &rgb_in[pix * 3..width * 3],
      &mut rgba_out[pix * 4..width * 4],
      width - pix,
    );
  }
}

/// f16 RGB → u16 RGB.
///
/// When `BE = true` the input `half::f16` values are big-endian encoded.
///
/// # Safety
///
/// Same as [`rgbf16_to_rgb_row`] but `rgb_out` is `&mut [u16]` with
/// `len() >= 3 * width` u16 elements.
#[inline]
#[target_feature(enable = "neon,fp16")]
pub(crate) unsafe fn rgbf16_to_rgb_u16_row<const BE: bool>(
  rgb_in: &[half::f16],
  rgb_out: &mut [u16],
  width: usize,
) {
  debug_assert!(rgb_in.len() >= width * 3, "rgbf16 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_u16_out row too short");

  let total_lanes = width * 3;
  let mut lane = 0usize;
  while lane + 12 <= total_lanes {
    let mut buf = [0.0f32; 12];
    unsafe {
      widen_f16x4::<BE>(rgb_in.as_ptr().add(lane), buf.as_mut_ptr());
      widen_f16x4::<BE>(rgb_in.as_ptr().add(lane + 4), buf.as_mut_ptr().add(4));
      widen_f16x4::<BE>(rgb_in.as_ptr().add(lane + 8), buf.as_mut_ptr().add(8));
      // Buffer is host-native f32; route via HOST_NATIVE_BE (see widen_f16x4).
      rgbf32_to_rgb_u16_row::<HOST_NATIVE_BE>(&buf, rgb_out.get_unchecked_mut(lane..lane + 12), 4);
    }
    lane += 12;
  }
  let pix_done = lane / 3;
  if pix_done < width {
    scalar::rgbf16_to_rgb_u16_row::<BE>(
      &rgb_in[pix_done * 3..width * 3],
      &mut rgb_out[pix_done * 3..width * 3],
      width - pix_done,
    );
  }
}

/// f16 RGB → u16 RGBA (alpha forced to `0xFFFF`).
///
/// When `BE = true` the input `half::f16` values are big-endian encoded.
///
/// # Safety
///
/// Same as [`rgbf16_to_rgb_u16_row`] but the output is `&mut [u16]` with
/// `len() >= 4 * width` u16 elements.
#[inline]
#[target_feature(enable = "neon,fp16")]
pub(crate) unsafe fn rgbf16_to_rgba_u16_row<const BE: bool>(
  rgb_in: &[half::f16],
  rgba_out: &mut [u16],
  width: usize,
) {
  debug_assert!(rgb_in.len() >= width * 3, "rgbf16 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_u16_out row too short");

  let total_lanes = width * 3;
  let mut lane = 0usize;
  let mut pix = 0usize;
  while lane + 12 <= total_lanes {
    let mut buf = [0.0f32; 12];
    unsafe {
      widen_f16x4::<BE>(rgb_in.as_ptr().add(lane), buf.as_mut_ptr());
      widen_f16x4::<BE>(rgb_in.as_ptr().add(lane + 4), buf.as_mut_ptr().add(4));
      widen_f16x4::<BE>(rgb_in.as_ptr().add(lane + 8), buf.as_mut_ptr().add(8));
      // Buffer is host-native f32; route via HOST_NATIVE_BE (see widen_f16x4).
      rgbf32_to_rgba_u16_row::<HOST_NATIVE_BE>(
        &buf,
        rgba_out.get_unchecked_mut(pix * 4..pix * 4 + 16),
        4,
      );
    }
    lane += 12;
    pix += 4;
  }
  if pix < width {
    scalar::rgbf16_to_rgba_u16_row::<BE>(
      &rgb_in[pix * 3..width * 3],
      &mut rgba_out[pix * 4..width * 4],
      width - pix,
    );
  }
}

/// f16 RGB → f32 RGB (lossless widen).
///
/// When `BE = true` the input `half::f16` values are big-endian encoded.
///
/// # Safety
///
/// Same as [`rgbf16_to_rgb_row`] but `rgb_out` is `&mut [f32]` with
/// `len() >= 3 * width` f32 elements.
#[inline]
#[target_feature(enable = "neon,fp16")]
pub(crate) unsafe fn rgbf16_to_rgb_f32_row<const BE: bool>(
  rgb_in: &[half::f16],
  rgb_out: &mut [f32],
  width: usize,
) {
  debug_assert!(rgb_in.len() >= width * 3, "rgbf16 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_f32_out row too short");

  let total_lanes = width * 3;
  let mut lane = 0usize;
  while lane + 4 <= total_lanes {
    unsafe {
      widen_f16x4::<BE>(rgb_in.as_ptr().add(lane), rgb_out.as_mut_ptr().add(lane));
    }
    lane += 4;
  }
  // Scalar tail for the last 0-3 lanes (partial pixel at most).
  unsafe {
    widen_f16_tail::<BE>(
      rgb_in.get_unchecked(lane..),
      rgb_out.get_unchecked_mut(lane..),
      total_lanes - lane,
    );
  }
}

/// f16 RGB → f16 RGB lossless pass-through.
///
/// When `BE = true` the input values are byte-swapped to host-native order
/// on output.
///
/// # Safety
///
/// Same as [`rgbf16_to_rgb_row`] but `rgb_out` is `&mut [half::f16]` with
/// `len() >= 3 * width` f16 elements.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn rgbf16_to_rgb_f16_row<const BE: bool>(
  rgb_in: &[half::f16],
  rgb_out: &mut [half::f16],
  width: usize,
) {
  debug_assert!(rgb_in.len() >= width * 3, "rgbf16 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_f16_out row too short");

  // Bit-exact copy / byte-swap: delegate to scalar.
  scalar::rgbf16_to_rgb_f16_row::<BE>(rgb_in, rgb_out, width);
}

// ---- Tier 9 — Rgbaf32 NEON entry points (4-channel, real alpha) -----------
//
// Thin delegates over the `rgbf32_*` kernels above. The conversions are
// per-element, so:
//   - **real-alpha** kernels feed the flat `R, G, B, A` array through the
//     elementwise RGB sibling in 12-lane chunks (12 = LCM(3,4), so each
//     chunk is a whole number of both RGB-lane and RGBA-pixel groups) and
//     finish the 0–2 pixel tail with the scalar reference.
//   - **drop-alpha** kernels gather the `R, G, B` lanes (skipping α) into a
//     small stack buffer of raw (wire-encoded) `f32`, then run the RGB
//     sibling (which BE-decodes), scalar tail for the remainder.

/// Packed RGBA f32 → packed RGB u8 (drop α).
///
/// # Safety
/// NEON available; `rgba_in.len() >= 4*width`, `rgb_out.len() >= 3*width`,
/// no aliasing.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn rgbaf32_to_rgb_row<const BE: bool>(
  rgba_in: &[f32],
  rgb_out: &mut [u8],
  width: usize,
) {
  debug_assert!(rgba_in.len() >= width * 4, "rgbaf32 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");
  const CHUNK: usize = 4;
  let mut p = 0usize;
  let mut buf = [0.0f32; CHUNK * 3];
  while p + CHUNK <= width {
    for k in 0..CHUNK {
      let s = (p + k) * 4;
      buf[k * 3] = rgba_in[s];
      buf[k * 3 + 1] = rgba_in[s + 1];
      buf[k * 3 + 2] = rgba_in[s + 2];
    }
    unsafe {
      rgbf32_to_rgb_row::<BE>(
        &buf,
        rgb_out.get_unchecked_mut(p * 3..(p + CHUNK) * 3),
        CHUNK,
      );
    }
    p += CHUNK;
  }
  if p < width {
    scalar::rgbaf32_to_rgb_row::<BE>(
      &rgba_in[p * 4..width * 4],
      &mut rgb_out[p * 3..width * 3],
      width - p,
    );
  }
}

/// Packed RGBA f32 → packed RGBA u8 (real α).
///
/// # Safety
/// NEON available; `rgba_in.len() >= 4*width`, `rgba_out.len() >= 4*width`,
/// no aliasing.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn rgbaf32_to_rgba_row<const BE: bool>(
  rgba_in: &[f32],
  rgba_out: &mut [u8],
  width: usize,
) {
  debug_assert!(rgba_in.len() >= width * 4, "rgbaf32 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");
  let total = width * 4;
  let mut lane = 0usize;
  while lane + 12 <= total {
    unsafe {
      rgbf32_to_rgb_row::<BE>(
        rgba_in.get_unchecked(lane..lane + 12),
        rgba_out.get_unchecked_mut(lane..lane + 12),
        4,
      );
    }
    lane += 12;
  }
  let pix_done = lane / 4;
  if pix_done < width {
    scalar::rgbaf32_to_rgba_row::<BE>(
      &rgba_in[pix_done * 4..total],
      &mut rgba_out[pix_done * 4..total],
      width - pix_done,
    );
  }
}

/// Packed RGBA f32 → packed RGB u16 (drop α).
///
/// # Safety
/// NEON available; `rgba_in.len() >= 4*width`, `rgb_out.len() >= 3*width`.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn rgbaf32_to_rgb_u16_row<const BE: bool>(
  rgba_in: &[f32],
  rgb_out: &mut [u16],
  width: usize,
) {
  debug_assert!(rgba_in.len() >= width * 4, "rgbaf32 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");
  const CHUNK: usize = 4;
  let mut p = 0usize;
  let mut buf = [0.0f32; CHUNK * 3];
  while p + CHUNK <= width {
    for k in 0..CHUNK {
      let s = (p + k) * 4;
      buf[k * 3] = rgba_in[s];
      buf[k * 3 + 1] = rgba_in[s + 1];
      buf[k * 3 + 2] = rgba_in[s + 2];
    }
    unsafe {
      rgbf32_to_rgb_u16_row::<BE>(
        &buf,
        rgb_out.get_unchecked_mut(p * 3..(p + CHUNK) * 3),
        CHUNK,
      );
    }
    p += CHUNK;
  }
  if p < width {
    scalar::rgbaf32_to_rgb_u16_row::<BE>(
      &rgba_in[p * 4..width * 4],
      &mut rgb_out[p * 3..width * 3],
      width - p,
    );
  }
}

/// Packed RGBA f32 → packed RGBA u16 (real α).
///
/// # Safety
/// NEON available; `rgba_in.len() >= 4*width`, `rgba_out.len() >= 4*width`.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn rgbaf32_to_rgba_u16_row<const BE: bool>(
  rgba_in: &[f32],
  rgba_out: &mut [u16],
  width: usize,
) {
  debug_assert!(rgba_in.len() >= width * 4, "rgbaf32 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");
  let total = width * 4;
  let mut lane = 0usize;
  while lane + 12 <= total {
    unsafe {
      rgbf32_to_rgb_u16_row::<BE>(
        rgba_in.get_unchecked(lane..lane + 12),
        rgba_out.get_unchecked_mut(lane..lane + 12),
        4,
      );
    }
    lane += 12;
  }
  let pix_done = lane / 4;
  if pix_done < width {
    scalar::rgbaf32_to_rgba_u16_row::<BE>(
      &rgba_in[pix_done * 4..total],
      &mut rgba_out[pix_done * 4..total],
      width - pix_done,
    );
  }
}

/// Packed RGBA f32 → packed RGB f32 (drop α, lossless).
///
/// # Safety
/// NEON available; `rgba_in.len() >= 4*width`, `rgb_out.len() >= 3*width`.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn rgbaf32_to_rgb_f32_row<const BE: bool>(
  rgba_in: &[f32],
  rgb_out: &mut [f32],
  width: usize,
) {
  debug_assert!(rgba_in.len() >= width * 4, "rgbaf32 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_f32_out row too short");
  const CHUNK: usize = 4;
  let mut p = 0usize;
  let mut buf = [0.0f32; CHUNK * 3];
  while p + CHUNK <= width {
    for k in 0..CHUNK {
      let s = (p + k) * 4;
      buf[k * 3] = rgba_in[s];
      buf[k * 3 + 1] = rgba_in[s + 1];
      buf[k * 3 + 2] = rgba_in[s + 2];
    }
    unsafe {
      rgbf32_to_rgb_f32_row::<BE>(
        &buf,
        rgb_out.get_unchecked_mut(p * 3..(p + CHUNK) * 3),
        CHUNK,
      );
    }
    p += CHUNK;
  }
  if p < width {
    scalar::rgbaf32_to_rgb_f32_row::<BE>(
      &rgba_in[p * 4..width * 4],
      &mut rgb_out[p * 3..width * 3],
      width - p,
    );
  }
}

/// Packed RGBA f32 → packed RGBA f32 (lossless 4-channel pass-through).
///
/// # Safety
/// NEON available; `rgba_in.len() >= 4*width`, `rgba_out.len() >= 4*width`.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn rgbaf32_to_rgba_f32_row<const BE: bool>(
  rgba_in: &[f32],
  rgba_out: &mut [f32],
  width: usize,
) {
  debug_assert!(rgba_in.len() >= width * 4, "rgbaf32 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_f32_out row too short");
  // Pure elementwise byte-order normalize — feed the flat 4*width array
  // through the f32 pass-through sibling in 12-lane chunks, scalar tail.
  let total = width * 4;
  let mut lane = 0usize;
  while lane + 12 <= total {
    unsafe {
      rgbf32_to_rgb_f32_row::<BE>(
        rgba_in.get_unchecked(lane..lane + 12),
        rgba_out.get_unchecked_mut(lane..lane + 12),
        4,
      );
    }
    lane += 12;
  }
  let pix_done = lane / 4;
  if pix_done < width {
    scalar::rgbaf32_to_rgba_f32_row::<BE>(
      &rgba_in[pix_done * 4..total],
      &mut rgba_out[pix_done * 4..total],
      width - pix_done,
    );
  }
}

// ---- Tier 9 — Rgbaf16 NEON entry points (4-channel, real alpha) -----------
//
// Same delegate strategy as Rgbaf32 but over the `rgbf16_*` widen-then-
// convert siblings (so each carries the `fp16` target feature the sibling
// needs). The lossless `_f16` pass-through delegates straight to scalar
// (no f16 hardware needed for a bit copy), matching the `rgbf16_to_rgb_f16_row`
// sibling.

/// Packed RGBA f16 → packed RGB u8 (drop α).
///
/// # Safety
/// NEON+fp16 available; `rgba_in.len() >= 4*width`, `rgb_out.len() >= 3*width`.
#[inline]
#[target_feature(enable = "neon,fp16")]
pub(crate) unsafe fn rgbaf16_to_rgb_row<const BE: bool>(
  rgba_in: &[half::f16],
  rgb_out: &mut [u8],
  width: usize,
) {
  debug_assert!(rgba_in.len() >= width * 4, "rgbaf16 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");
  const CHUNK: usize = 4;
  let mut p = 0usize;
  let mut buf = [half::f16::ZERO; CHUNK * 3];
  while p + CHUNK <= width {
    for k in 0..CHUNK {
      let s = (p + k) * 4;
      buf[k * 3] = rgba_in[s];
      buf[k * 3 + 1] = rgba_in[s + 1];
      buf[k * 3 + 2] = rgba_in[s + 2];
    }
    unsafe {
      rgbf16_to_rgb_row::<BE>(
        &buf,
        rgb_out.get_unchecked_mut(p * 3..(p + CHUNK) * 3),
        CHUNK,
      );
    }
    p += CHUNK;
  }
  if p < width {
    scalar::rgbaf16_to_rgb_row::<BE>(
      &rgba_in[p * 4..width * 4],
      &mut rgb_out[p * 3..width * 3],
      width - p,
    );
  }
}

/// Packed RGBA f16 → packed RGBA u8 (real α).
///
/// # Safety
/// NEON+fp16 available; `rgba_in.len() >= 4*width`, `rgba_out.len() >= 4*width`.
#[inline]
#[target_feature(enable = "neon,fp16")]
pub(crate) unsafe fn rgbaf16_to_rgba_row<const BE: bool>(
  rgba_in: &[half::f16],
  rgba_out: &mut [u8],
  width: usize,
) {
  debug_assert!(rgba_in.len() >= width * 4, "rgbaf16 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");
  let total = width * 4;
  let mut lane = 0usize;
  while lane + 12 <= total {
    unsafe {
      rgbf16_to_rgb_row::<BE>(
        rgba_in.get_unchecked(lane..lane + 12),
        rgba_out.get_unchecked_mut(lane..lane + 12),
        4,
      );
    }
    lane += 12;
  }
  let pix_done = lane / 4;
  if pix_done < width {
    scalar::rgbaf16_to_rgba_row::<BE>(
      &rgba_in[pix_done * 4..total],
      &mut rgba_out[pix_done * 4..total],
      width - pix_done,
    );
  }
}

/// Packed RGBA f16 → packed RGB u16 (drop α).
///
/// # Safety
/// NEON+fp16 available; `rgba_in.len() >= 4*width`, `rgb_out.len() >= 3*width`.
#[inline]
#[target_feature(enable = "neon,fp16")]
pub(crate) unsafe fn rgbaf16_to_rgb_u16_row<const BE: bool>(
  rgba_in: &[half::f16],
  rgb_out: &mut [u16],
  width: usize,
) {
  debug_assert!(rgba_in.len() >= width * 4, "rgbaf16 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");
  const CHUNK: usize = 4;
  let mut p = 0usize;
  let mut buf = [half::f16::ZERO; CHUNK * 3];
  while p + CHUNK <= width {
    for k in 0..CHUNK {
      let s = (p + k) * 4;
      buf[k * 3] = rgba_in[s];
      buf[k * 3 + 1] = rgba_in[s + 1];
      buf[k * 3 + 2] = rgba_in[s + 2];
    }
    unsafe {
      rgbf16_to_rgb_u16_row::<BE>(
        &buf,
        rgb_out.get_unchecked_mut(p * 3..(p + CHUNK) * 3),
        CHUNK,
      );
    }
    p += CHUNK;
  }
  if p < width {
    scalar::rgbaf16_to_rgb_u16_row::<BE>(
      &rgba_in[p * 4..width * 4],
      &mut rgb_out[p * 3..width * 3],
      width - p,
    );
  }
}

/// Packed RGBA f16 → packed RGBA u16 (real α).
///
/// # Safety
/// NEON+fp16 available; `rgba_in.len() >= 4*width`, `rgba_out.len() >= 4*width`.
#[inline]
#[target_feature(enable = "neon,fp16")]
pub(crate) unsafe fn rgbaf16_to_rgba_u16_row<const BE: bool>(
  rgba_in: &[half::f16],
  rgba_out: &mut [u16],
  width: usize,
) {
  debug_assert!(rgba_in.len() >= width * 4, "rgbaf16 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");
  let total = width * 4;
  let mut lane = 0usize;
  while lane + 12 <= total {
    unsafe {
      rgbf16_to_rgb_u16_row::<BE>(
        rgba_in.get_unchecked(lane..lane + 12),
        rgba_out.get_unchecked_mut(lane..lane + 12),
        4,
      );
    }
    lane += 12;
  }
  let pix_done = lane / 4;
  if pix_done < width {
    scalar::rgbaf16_to_rgba_u16_row::<BE>(
      &rgba_in[pix_done * 4..total],
      &mut rgba_out[pix_done * 4..total],
      width - pix_done,
    );
  }
}

/// Packed RGBA f16 → packed RGB f32 (drop α, widen).
///
/// # Safety
/// NEON+fp16 available; `rgba_in.len() >= 4*width`, `rgb_out.len() >= 3*width`.
#[inline]
#[target_feature(enable = "neon,fp16")]
pub(crate) unsafe fn rgbaf16_to_rgb_f32_row<const BE: bool>(
  rgba_in: &[half::f16],
  rgb_out: &mut [f32],
  width: usize,
) {
  debug_assert!(rgba_in.len() >= width * 4, "rgbaf16 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_f32_out row too short");
  const CHUNK: usize = 4;
  let mut p = 0usize;
  let mut buf = [half::f16::ZERO; CHUNK * 3];
  while p + CHUNK <= width {
    for k in 0..CHUNK {
      let s = (p + k) * 4;
      buf[k * 3] = rgba_in[s];
      buf[k * 3 + 1] = rgba_in[s + 1];
      buf[k * 3 + 2] = rgba_in[s + 2];
    }
    unsafe {
      rgbf16_to_rgb_f32_row::<BE>(
        &buf,
        rgb_out.get_unchecked_mut(p * 3..(p + CHUNK) * 3),
        CHUNK,
      );
    }
    p += CHUNK;
  }
  if p < width {
    scalar::rgbaf16_to_rgb_f32_row::<BE>(
      &rgba_in[p * 4..width * 4],
      &mut rgb_out[p * 3..width * 3],
      width - p,
    );
  }
}

/// Packed RGBA f16 → packed RGBA f32 (widen, 4-channel).
///
/// # Safety
/// NEON+fp16 available; `rgba_in.len() >= 4*width`, `rgba_out.len() >= 4*width`.
#[inline]
#[target_feature(enable = "neon,fp16")]
pub(crate) unsafe fn rgbaf16_to_rgba_f32_row<const BE: bool>(
  rgba_in: &[half::f16],
  rgba_out: &mut [f32],
  width: usize,
) {
  debug_assert!(rgba_in.len() >= width * 4, "rgbaf16 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_f32_out row too short");
  // Elementwise widen — feed the flat 4*width array through the f16→f32
  // widen sibling in 12-lane chunks, scalar tail.
  let total = width * 4;
  let mut lane = 0usize;
  while lane + 12 <= total {
    unsafe {
      rgbf16_to_rgb_f32_row::<BE>(
        rgba_in.get_unchecked(lane..lane + 12),
        rgba_out.get_unchecked_mut(lane..lane + 12),
        4,
      );
    }
    lane += 12;
  }
  let pix_done = lane / 4;
  if pix_done < width {
    scalar::rgbaf16_to_rgba_f32_row::<BE>(
      &rgba_in[pix_done * 4..total],
      &mut rgba_out[pix_done * 4..total],
      width - pix_done,
    );
  }
}

/// Packed RGBA f16 → packed RGB f16 (drop α, lossless). Delegates to scalar
/// (a bit copy / byte-swap, no f16 hardware needed — matches the
/// `rgbf16_to_rgb_f16_row` sibling).
///
/// # Safety
/// NEON available; `rgba_in.len() >= 4*width`, `rgb_out.len() >= 3*width`.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn rgbaf16_to_rgb_f16_row<const BE: bool>(
  rgba_in: &[half::f16],
  rgb_out: &mut [half::f16],
  width: usize,
) {
  scalar::rgbaf16_to_rgb_f16_row::<BE>(rgba_in, rgb_out, width);
}

/// Packed RGBA f16 → packed RGBA f16 (lossless 4-channel). Delegates to
/// scalar.
///
/// # Safety
/// NEON available; `rgba_in.len() >= 4*width`, `rgba_out.len() >= 4*width`.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn rgbaf16_to_rgba_f16_row<const BE: bool>(
  rgba_in: &[half::f16],
  rgba_out: &mut [half::f16],
  width: usize,
) {
  scalar::rgbaf16_to_rgba_f16_row::<BE>(rgba_in, rgba_out, width);
}