colconv 0.1.0

SIMD-dispatched color-conversion kernels covering the FFmpeg AVPixelFormat space, with a Sink-based API so consumers pick which derived outputs (RGB / Luma / HSV / custom) they want without paying for the ones they don't.
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
//! NEON kernels for 16-bit packed RGB/BGR/RGBA/BGRA sources (Tier 8 finish).
//!
// Kernels are wired into the dispatcher in the SIMD dispatch task; suppress
// dead_code until then.
#![allow(dead_code)]
//!
//! ## Format layouts
//!
//! | Format | Elements per pixel | Channel order in memory |
//! |--------|--------------------|------------------------|
//! | Rgb48  | 3 u16              | R, G, B                |
//! | Bgr48  | 3 u16              | B, G, R                |
//! | Rgba64 | 4 u16              | R, G, B, A             |
//! | Bgra64 | 4 u16              | B, G, R, A             |
//!
//! ## Per-format SIMD strategy (8 pixels per SIMD iteration)
//!
//! - **Rgb48 / Bgr48:** `vld3q_u16(src_ptr)` → `uint16x8x3_t(ch0, ch1, ch2)`
//!   deinterleaves 8 pixels (24 u16 elements). For Bgr48, the channel fields
//!   are reordered on store (`.0` ↔ `.2`) so output is always R-first.
//! - **Rgba64 / Bgra64:** `vld4q_u16(src_ptr)` → `uint16x8x4_t(ch0, ch1, ch2, ch3)`.
//!   For Bgra64, `ch0` = B and `ch2` = R (swapped on store).
//!
//! ## Big-endian support
//!
//! Every public kernel accepts `<const BE: bool>`. Each per-channel
//! `uint16x8_t` vector produced by `vld3q_u16`/`vld4q_u16` is conditionally
//! byte-swapped via the canonical [`super::bswap_u16x8_if_be`] helper before
//! any channel math. The gate is `BE != HOST_NATIVE_BE`, so the swap fires
//! only when the wire endian differs from the host's native byte order — on
//! LE hosts (all current AArch64 hardware) reading LE data the helper is a
//! no-op and emits zero extra instructions; on BE hosts (e.g. `aarch64_be`)
//! reading LE data the swap fires to recover host-native u16 lanes for the
//! arithmetic that follows.
//!
//! ## Depth conversion
//!
//! - **u16 → u8:** `vshrn_n_u16::<8>(v)` — high-byte extraction, matching
//!   `scalar`'s `(v >> 8) as u8`.
//! - **u16 → u16:** native passthrough (`vst3q_u16` / `vst4q_u16` directly).
//!
//! ## Scalar tail
//!
//! All kernels handle `width % 8` remaining pixels via the scalar reference.

use core::arch::aarch64::*;

use super::bswap_u16x8_if_be;
use crate::row::scalar;

// Rgb48 (R, G, B — 3 u16 elements per pixel).
/// NEON Rgb48 → packed u8 RGB. 8 pixels per SIMD iteration.
///
/// `vld3q_u16` deinterleaves into `(R, G, B)` u16x8; `vshrn_n_u16::<8>`
/// narrows each channel; `vst3_u8` interleaves back.
///
/// When `BE = true` each channel vector is byte-swapped before narrowing.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `rgb48.len() >= width * 3`.
/// 3. `rgb_out.len() >= width * 3`.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn neon_rgb48_to_rgb_row<const BE: bool>(
  rgb48: &[u16],
  rgb_out: &mut [u8],
  width: usize,
) {
  debug_assert!(rgb48.len() >= width * 3, "rgb48 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 8 <= width {
      let px: uint16x8x3_t = vld3q_u16(rgb48.as_ptr().add(x * 3));
      let r8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.0));
      let g8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.1));
      let b8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.2));
      vst3_u8(rgb_out.as_mut_ptr().add(x * 3), uint8x8x3_t(r8, g8, b8));
      x += 8;
    }
    if x < width {
      scalar::rgb48_to_rgb_row::<BE>(&rgb48[x * 3..], &mut rgb_out[x * 3..], width - x);
    }
  }
}

/// NEON Rgb48 → packed u8 RGBA. 8 pixels per SIMD iteration. Alpha forced to 0xFF.
///
/// When `BE = true` each channel vector is byte-swapped before narrowing.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `rgb48.len() >= width * 3`.
/// 3. `rgba_out.len() >= width * 4`.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn neon_rgb48_to_rgba_row<const BE: bool>(
  rgb48: &[u16],
  rgba_out: &mut [u8],
  width: usize,
) {
  debug_assert!(rgb48.len() >= width * 3, "rgb48 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");

  unsafe {
    let alpha = vdup_n_u8(0xFF);
    let mut x = 0usize;
    while x + 8 <= width {
      let px: uint16x8x3_t = vld3q_u16(rgb48.as_ptr().add(x * 3));
      let r8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.0));
      let g8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.1));
      let b8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.2));
      vst4_u8(
        rgba_out.as_mut_ptr().add(x * 4),
        uint8x8x4_t(r8, g8, b8, alpha),
      );
      x += 8;
    }
    if x < width {
      scalar::rgb48_to_rgba_row::<BE>(&rgb48[x * 3..], &mut rgba_out[x * 4..], width - x);
    }
  }
}

/// NEON Rgb48 → native-depth u16 RGB. 8 pixels per SIMD iteration.
///
/// `vld3q_u16` deinterleaves, `vst3q_u16` reinterleaves.
/// When `BE = true` each channel is byte-swapped to host-native order before storing.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `rgb48.len() >= width * 3`.
/// 3. `rgb_out.len() >= width * 3`.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn neon_rgb48_to_rgb_u16_row<const BE: bool>(
  rgb48: &[u16],
  rgb_out: &mut [u16],
  width: usize,
) {
  debug_assert!(rgb48.len() >= width * 3, "rgb48 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 8 <= width {
      let px: uint16x8x3_t = vld3q_u16(rgb48.as_ptr().add(x * 3));
      vst3q_u16(
        rgb_out.as_mut_ptr().add(x * 3),
        uint16x8x3_t(
          bswap_u16x8_if_be::<BE>(px.0),
          bswap_u16x8_if_be::<BE>(px.1),
          bswap_u16x8_if_be::<BE>(px.2),
        ),
      );
      x += 8;
    }
    if x < width {
      scalar::rgb48_to_rgb_u16_row::<BE>(&rgb48[x * 3..], &mut rgb_out[x * 3..], width - x);
    }
  }
}

/// NEON Rgb48 → native-depth u16 RGBA. 8 pixels per SIMD iteration. Alpha forced to 0xFFFF.
///
/// When `BE = true` each channel is byte-swapped to host-native order before storing.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `rgb48.len() >= width * 3`.
/// 3. `rgba_out.len() >= width * 4`.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn neon_rgb48_to_rgba_u16_row<const BE: bool>(
  rgb48: &[u16],
  rgba_out: &mut [u16],
  width: usize,
) {
  debug_assert!(rgb48.len() >= width * 3, "rgb48 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");

  unsafe {
    let alpha = vdupq_n_u16(0xFFFF);
    let mut x = 0usize;
    while x + 8 <= width {
      let px: uint16x8x3_t = vld3q_u16(rgb48.as_ptr().add(x * 3));
      vst4q_u16(
        rgba_out.as_mut_ptr().add(x * 4),
        uint16x8x4_t(
          bswap_u16x8_if_be::<BE>(px.0),
          bswap_u16x8_if_be::<BE>(px.1),
          bswap_u16x8_if_be::<BE>(px.2),
          alpha,
        ),
      );
      x += 8;
    }
    if x < width {
      scalar::rgb48_to_rgba_u16_row::<BE>(&rgb48[x * 3..], &mut rgba_out[x * 4..], width - x);
    }
  }
}

// Bgr48 (B, G, R — 3 u16 elements per pixel).
/// NEON Bgr48 → packed u8 RGB. 8 pixels per SIMD iteration.
///
/// `vld3q_u16` deinterleaves into `(B, G, R)` u16x8; channels are swapped
/// (`px.2` = R, `px.0` = B) in the `vst3_u8` call to produce R-first output.
/// When `BE = true` each channel is byte-swapped before narrowing.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `bgr48.len() >= width * 3`.
/// 3. `rgb_out.len() >= width * 3`.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn neon_bgr48_to_rgb_row<const BE: bool>(
  bgr48: &[u16],
  rgb_out: &mut [u8],
  width: usize,
) {
  debug_assert!(bgr48.len() >= width * 3, "bgr48 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 8 <= width {
      // px.0 = B, px.1 = G, px.2 = R (source BGR order)
      let px: uint16x8x3_t = vld3q_u16(bgr48.as_ptr().add(x * 3));
      let r8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.2)); // R (was at position 2)
      let g8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.1)); // G (unchanged)
      let b8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.0)); // B (was at position 0)
      vst3_u8(rgb_out.as_mut_ptr().add(x * 3), uint8x8x3_t(r8, g8, b8));
      x += 8;
    }
    if x < width {
      scalar::bgr48_to_rgb_row::<BE>(&bgr48[x * 3..], &mut rgb_out[x * 3..], width - x);
    }
  }
}

/// NEON Bgr48 → packed u8 RGBA. 8 pixels per SIMD iteration.
/// B↔R swap on output; alpha forced to 0xFF.
/// When `BE = true` each channel is byte-swapped before narrowing.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `bgr48.len() >= width * 3`.
/// 3. `rgba_out.len() >= width * 4`.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn neon_bgr48_to_rgba_row<const BE: bool>(
  bgr48: &[u16],
  rgba_out: &mut [u8],
  width: usize,
) {
  debug_assert!(bgr48.len() >= width * 3, "bgr48 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");

  unsafe {
    let alpha = vdup_n_u8(0xFF);
    let mut x = 0usize;
    while x + 8 <= width {
      let px: uint16x8x3_t = vld3q_u16(bgr48.as_ptr().add(x * 3));
      let r8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.2));
      let g8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.1));
      let b8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.0));
      vst4_u8(
        rgba_out.as_mut_ptr().add(x * 4),
        uint8x8x4_t(r8, g8, b8, alpha),
      );
      x += 8;
    }
    if x < width {
      scalar::bgr48_to_rgba_row::<BE>(&bgr48[x * 3..], &mut rgba_out[x * 4..], width - x);
    }
  }
}

/// NEON Bgr48 → native-depth u16 RGB. 8 pixels per SIMD iteration.
/// B↔R swap: `px.2` → position 0 (R), `px.0` → position 2 (B).
/// When `BE = true` each channel is byte-swapped to host-native order.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `bgr48.len() >= width * 3`.
/// 3. `rgb_out.len() >= width * 3`.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn neon_bgr48_to_rgb_u16_row<const BE: bool>(
  bgr48: &[u16],
  rgb_out: &mut [u16],
  width: usize,
) {
  debug_assert!(bgr48.len() >= width * 3, "bgr48 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 8 <= width {
      let px: uint16x8x3_t = vld3q_u16(bgr48.as_ptr().add(x * 3));
      // Swap B↔R: store (R=px.2, G=px.1, B=px.0)
      vst3q_u16(
        rgb_out.as_mut_ptr().add(x * 3),
        uint16x8x3_t(
          bswap_u16x8_if_be::<BE>(px.2),
          bswap_u16x8_if_be::<BE>(px.1),
          bswap_u16x8_if_be::<BE>(px.0),
        ),
      );
      x += 8;
    }
    if x < width {
      scalar::bgr48_to_rgb_u16_row::<BE>(&bgr48[x * 3..], &mut rgb_out[x * 3..], width - x);
    }
  }
}

/// NEON Bgr48 → native-depth u16 RGBA. 8 pixels per SIMD iteration.
/// B↔R swap; alpha forced to 0xFFFF.
/// When `BE = true` each channel is byte-swapped to host-native order.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `bgr48.len() >= width * 3`.
/// 3. `rgba_out.len() >= width * 4`.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn neon_bgr48_to_rgba_u16_row<const BE: bool>(
  bgr48: &[u16],
  rgba_out: &mut [u16],
  width: usize,
) {
  debug_assert!(bgr48.len() >= width * 3, "bgr48 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");

  unsafe {
    let alpha = vdupq_n_u16(0xFFFF);
    let mut x = 0usize;
    while x + 8 <= width {
      let px: uint16x8x3_t = vld3q_u16(bgr48.as_ptr().add(x * 3));
      // Store (R=px.2, G=px.1, B=px.0, A=0xFFFF)
      vst4q_u16(
        rgba_out.as_mut_ptr().add(x * 4),
        uint16x8x4_t(
          bswap_u16x8_if_be::<BE>(px.2),
          bswap_u16x8_if_be::<BE>(px.1),
          bswap_u16x8_if_be::<BE>(px.0),
          alpha,
        ),
      );
      x += 8;
    }
    if x < width {
      scalar::bgr48_to_rgba_u16_row::<BE>(&bgr48[x * 3..], &mut rgba_out[x * 4..], width - x);
    }
  }
}

// Rgba64 (R, G, B, A — 4 u16 elements per pixel).
/// NEON Rgba64 → packed u8 RGB. 8 pixels per SIMD iteration. Alpha discarded.
///
/// `vld4q_u16` deinterleaves into `(R, G, B, A)` u16x8; R/G/B narrowed;
/// `vst3_u8` writes only 3 channels.
/// When `BE = true` each channel is byte-swapped before narrowing.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `rgba64.len() >= width * 4`.
/// 3. `rgb_out.len() >= width * 3`.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn neon_rgba64_to_rgb_row<const BE: bool>(
  rgba64: &[u16],
  rgb_out: &mut [u8],
  width: usize,
) {
  debug_assert!(rgba64.len() >= width * 4, "rgba64 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 8 <= width {
      let px: uint16x8x4_t = vld4q_u16(rgba64.as_ptr().add(x * 4));
      let r8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.0));
      let g8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.1));
      let b8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.2));
      // Alpha (px.3) discarded.
      vst3_u8(rgb_out.as_mut_ptr().add(x * 3), uint8x8x3_t(r8, g8, b8));
      x += 8;
    }
    if x < width {
      scalar::rgba64_to_rgb_row::<BE>(&rgba64[x * 4..], &mut rgb_out[x * 3..], width - x);
    }
  }
}

/// NEON Rgba64 → packed u8 RGBA. 8 pixels per SIMD iteration. Source alpha passes through.
///
/// All 4 channels narrowed via `vshrn_n_u16::<8>`.
/// When `BE = true` each channel is byte-swapped before narrowing.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `rgba64.len() >= width * 4`.
/// 3. `rgba_out.len() >= width * 4`.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn neon_rgba64_to_rgba_row<const BE: bool>(
  rgba64: &[u16],
  rgba_out: &mut [u8],
  width: usize,
) {
  debug_assert!(rgba64.len() >= width * 4, "rgba64 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 8 <= width {
      let px: uint16x8x4_t = vld4q_u16(rgba64.as_ptr().add(x * 4));
      let r8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.0));
      let g8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.1));
      let b8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.2));
      let a8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.3)); // source alpha depth-converted
      vst4_u8(
        rgba_out.as_mut_ptr().add(x * 4),
        uint8x8x4_t(r8, g8, b8, a8),
      );
      x += 8;
    }
    if x < width {
      scalar::rgba64_to_rgba_row::<BE>(&rgba64[x * 4..], &mut rgba_out[x * 4..], width - x);
    }
  }
}

/// NEON Rgba64 → native-depth u16 RGB. 8 pixels per SIMD iteration. Alpha discarded.
///
/// `vld4q_u16` deinterleaves; `vst3q_u16` writes R, G, B channels only.
/// When `BE = true` each channel is byte-swapped to host-native order.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `rgba64.len() >= width * 4`.
/// 3. `rgb_out.len() >= width * 3`.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn neon_rgba64_to_rgb_u16_row<const BE: bool>(
  rgba64: &[u16],
  rgb_out: &mut [u16],
  width: usize,
) {
  debug_assert!(rgba64.len() >= width * 4, "rgba64 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 8 <= width {
      let px: uint16x8x4_t = vld4q_u16(rgba64.as_ptr().add(x * 4));
      // Alpha (px.3) discarded.
      vst3q_u16(
        rgb_out.as_mut_ptr().add(x * 3),
        uint16x8x3_t(
          bswap_u16x8_if_be::<BE>(px.0),
          bswap_u16x8_if_be::<BE>(px.1),
          bswap_u16x8_if_be::<BE>(px.2),
        ),
      );
      x += 8;
    }
    if x < width {
      scalar::rgba64_to_rgb_u16_row::<BE>(&rgba64[x * 4..], &mut rgb_out[x * 3..], width - x);
    }
  }
}

/// NEON Rgba64 → native-depth u16 RGBA. 8 pixels per SIMD iteration.
///
/// `vld4q_u16` deinterleaves; `vst4q_u16` reinterleaves — source alpha preserved.
/// When `BE = true` each channel is byte-swapped to host-native order.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `rgba64.len() >= width * 4`.
/// 3. `rgba_out.len() >= width * 4`.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn neon_rgba64_to_rgba_u16_row<const BE: bool>(
  rgba64: &[u16],
  rgba_out: &mut [u16],
  width: usize,
) {
  debug_assert!(rgba64.len() >= width * 4, "rgba64 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 8 <= width {
      let px: uint16x8x4_t = vld4q_u16(rgba64.as_ptr().add(x * 4));
      vst4q_u16(
        rgba_out.as_mut_ptr().add(x * 4),
        uint16x8x4_t(
          bswap_u16x8_if_be::<BE>(px.0),
          bswap_u16x8_if_be::<BE>(px.1),
          bswap_u16x8_if_be::<BE>(px.2),
          bswap_u16x8_if_be::<BE>(px.3),
        ),
      );
      x += 8;
    }
    if x < width {
      scalar::rgba64_to_rgba_u16_row::<BE>(&rgba64[x * 4..], &mut rgba_out[x * 4..], width - x);
    }
  }
}

// Bgra64 (B, G, R, A — 4 u16 elements per pixel).
/// NEON Bgra64 → packed u8 RGB. 8 pixels per SIMD iteration.
/// B↔R swap; alpha discarded.
///
/// `vld4q_u16` gives `(B, G, R, A)` → store `(R=px.2, G=px.1, B=px.0)`.
/// When `BE = true` each channel is byte-swapped before narrowing.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `bgra64.len() >= width * 4`.
/// 3. `rgb_out.len() >= width * 3`.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn neon_bgra64_to_rgb_row<const BE: bool>(
  bgra64: &[u16],
  rgb_out: &mut [u8],
  width: usize,
) {
  debug_assert!(bgra64.len() >= width * 4, "bgra64 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 8 <= width {
      // px.0 = B, px.1 = G, px.2 = R, px.3 = A
      let px: uint16x8x4_t = vld4q_u16(bgra64.as_ptr().add(x * 4));
      let r8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.2)); // R (from position 2)
      let g8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.1)); // G (unchanged)
      let b8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.0)); // B (from position 0)
      // Alpha (px.3) discarded.
      vst3_u8(rgb_out.as_mut_ptr().add(x * 3), uint8x8x3_t(r8, g8, b8));
      x += 8;
    }
    if x < width {
      scalar::bgra64_to_rgb_row::<BE>(&bgra64[x * 4..], &mut rgb_out[x * 3..], width - x);
    }
  }
}

/// NEON Bgra64 → packed u8 RGBA. 8 pixels per SIMD iteration.
/// B↔R swap; source alpha passes through (narrowed via `>> 8`).
/// When `BE = true` each channel is byte-swapped before narrowing.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `bgra64.len() >= width * 4`.
/// 3. `rgba_out.len() >= width * 4`.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn neon_bgra64_to_rgba_row<const BE: bool>(
  bgra64: &[u16],
  rgba_out: &mut [u8],
  width: usize,
) {
  debug_assert!(bgra64.len() >= width * 4, "bgra64 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 8 <= width {
      let px: uint16x8x4_t = vld4q_u16(bgra64.as_ptr().add(x * 4));
      let r8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.2));
      let g8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.1));
      let b8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.0));
      let a8 = vshrn_n_u16::<8>(bswap_u16x8_if_be::<BE>(px.3)); // source alpha depth-converted
      vst4_u8(
        rgba_out.as_mut_ptr().add(x * 4),
        uint8x8x4_t(r8, g8, b8, a8),
      );
      x += 8;
    }
    if x < width {
      scalar::bgra64_to_rgba_row::<BE>(&bgra64[x * 4..], &mut rgba_out[x * 4..], width - x);
    }
  }
}

/// NEON Bgra64 → native-depth u16 RGB. 8 pixels per SIMD iteration.
/// B↔R swap; alpha discarded. `vld4q_u16` → `vst3q_u16(R, G, B)`.
/// When `BE = true` each channel is byte-swapped to host-native order.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `bgra64.len() >= width * 4`.
/// 3. `rgb_out.len() >= width * 3`.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn neon_bgra64_to_rgb_u16_row<const BE: bool>(
  bgra64: &[u16],
  rgb_out: &mut [u16],
  width: usize,
) {
  debug_assert!(bgra64.len() >= width * 4, "bgra64 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 8 <= width {
      let px: uint16x8x4_t = vld4q_u16(bgra64.as_ptr().add(x * 4));
      // Swap B↔R, drop alpha: store (R=px.2, G=px.1, B=px.0)
      vst3q_u16(
        rgb_out.as_mut_ptr().add(x * 3),
        uint16x8x3_t(
          bswap_u16x8_if_be::<BE>(px.2),
          bswap_u16x8_if_be::<BE>(px.1),
          bswap_u16x8_if_be::<BE>(px.0),
        ),
      );
      x += 8;
    }
    if x < width {
      scalar::bgra64_to_rgb_u16_row::<BE>(&bgra64[x * 4..], &mut rgb_out[x * 3..], width - x);
    }
  }
}

/// NEON Bgra64 → native-depth u16 RGBA. 8 pixels per SIMD iteration.
/// B↔R swap; source alpha preserved at position 3.
///
/// `vld4q_u16` gives `(B, G, R, A)` → `vst4q_u16(R=px.2, G=px.1, B=px.0, A=px.3)`.
/// When `BE = true` each channel is byte-swapped to host-native order.
///
/// # Safety
///
/// 1. NEON must be available.
/// 2. `bgra64.len() >= width * 4`.
/// 3. `rgba_out.len() >= width * 4`.
#[inline]
#[target_feature(enable = "neon")]
pub(crate) unsafe fn neon_bgra64_to_rgba_u16_row<const BE: bool>(
  bgra64: &[u16],
  rgba_out: &mut [u16],
  width: usize,
) {
  debug_assert!(bgra64.len() >= width * 4, "bgra64 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 8 <= width {
      let px: uint16x8x4_t = vld4q_u16(bgra64.as_ptr().add(x * 4));
      // Swap B↔R, preserve A: store (R=px.2, G=px.1, B=px.0, A=px.3)
      vst4q_u16(
        rgba_out.as_mut_ptr().add(x * 4),
        uint16x8x4_t(
          bswap_u16x8_if_be::<BE>(px.2),
          bswap_u16x8_if_be::<BE>(px.1),
          bswap_u16x8_if_be::<BE>(px.0),
          bswap_u16x8_if_be::<BE>(px.3),
        ),
      );
      x += 8;
    }
    if x < width {
      scalar::bgra64_to_rgba_u16_row::<BE>(&bgra64[x * 4..], &mut rgba_out[x * 4..], width - x);
    }
  }
}