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
//! AVX2 kernels for 32-bit packed RGB / RGBA sources (Rgb96 / Rgba128).
//!
//! ## Format layouts
//!
//! | Format  | Elements per pixel | Channel order in memory |
//! |---------|--------------------|------------------------|
//! | Rgb96   | 3 u32              | R, G, B                |
//! | Rgba128 | 4 u32              | R, G, B, A             |
//!
//! ## Per-format SIMD strategy (16 pixels per outer iteration)
//!
//! The stride-3 u32 deinterleave does not tile cleanly across the two
//! 128-bit lanes of a `__m256i`, so — exactly like the 16-bit AVX2 sibling's
//! 3-channel path — each 16-pixel outer iteration is processed as **two**
//! 8-pixel SSE4.1-style halves (`_mm_shuffle_epi8` gather + `_mm_srli_epi32`
//! narrow + `_mm_packus_epi32` / `_mm_packus_epi16` pack), reusing the shared
//! [`super::write_rgb_16`] / [`super::write_rgba_16`] / [`super::write_rgb_u16_8`]
//! / [`super::write_rgba_u16_8`] writers. The outer loop still advances 16
//! pixels per iteration.
//!
//! ## Depth conversion
//!
//! - **u32 → u8:**  `_mm_srli_epi32::<24>` then two-stage saturating narrow
//!   (`>> 24`, matching scalar `(v >> 24) as u8`).
//! - **u32 → u16:** `_mm_srli_epi32::<16>` then `_mm_packus_epi32` (`>> 16`,
//!   matching scalar `(v >> 16) as u16`).
//!
//! ## Scalar tail
//!
//! All kernels handle the `width % 8` remaining pixels via the scalar reference
//! (an 8-pixel cleanup pass follows the 16-pixel main loop).
// Kernels are wired into the dispatcher in the SIMD dispatch task; suppress
// dead_code until then.
#![allow(dead_code)]

use super::*;

// ---- endian byte-swap helper ------------------------------------------------

/// Compile-time host endianness. `true` on BE targets, `false` on LE.
const HOST_NATIVE_BE: bool = cfg!(target_endian = "big");

/// Conditionally byte-swap every u32 lane in `v` into host-native order.
/// The gate is `BE != HOST_NATIVE_BE` — the swap fires only when the wire
/// endian differs from the host's native byte order.
#[inline(always)]
unsafe fn byteswap32_if_be<const BE: bool>(v: __m128i) -> __m128i {
  if BE != HOST_NATIVE_BE {
    const MASK: __m128i =
      unsafe { core::mem::transmute([3u8, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12]) };
    unsafe { _mm_shuffle_epi8(v, MASK) }
  } else {
    v
  }
}

/// Deinterleave 4 pixels of stride-3 u32 (Rgb96) into `(R, G, B)` `u32x4`
/// channel lane vectors. See the SSE4.1 sibling for the mask derivation.
///
/// # Safety
///
/// Caller must have verified AVX2 availability (SSSE3 superset).
#[inline(always)]
unsafe fn deinterleave_rgb96_4px(
  v0: __m128i,
  v1: __m128i,
  v2: __m128i,
) -> (__m128i, __m128i, __m128i) {
  unsafe {
    let r_v0 = _mm_setr_epi8(0, 1, 2, 3, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1);
    let r_v1 = _mm_setr_epi8(-1, -1, -1, -1, -1, -1, -1, -1, 8, 9, 10, 11, -1, -1, -1, -1);
    let r_v2 = _mm_setr_epi8(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 4, 5, 6, 7);
    let r = _mm_or_si128(
      _mm_or_si128(_mm_shuffle_epi8(v0, r_v0), _mm_shuffle_epi8(v1, r_v1)),
      _mm_shuffle_epi8(v2, r_v2),
    );

    let g_v0 = _mm_setr_epi8(4, 5, 6, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
    let g_v1 = _mm_setr_epi8(-1, -1, -1, -1, 0, 1, 2, 3, 12, 13, 14, 15, -1, -1, -1, -1);
    let g_v2 = _mm_setr_epi8(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 8, 9, 10, 11);
    let g = _mm_or_si128(
      _mm_or_si128(_mm_shuffle_epi8(v0, g_v0), _mm_shuffle_epi8(v1, g_v1)),
      _mm_shuffle_epi8(v2, g_v2),
    );

    let b_v0 = _mm_setr_epi8(8, 9, 10, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
    let b_v1 = _mm_setr_epi8(-1, -1, -1, -1, 4, 5, 6, 7, -1, -1, -1, -1, -1, -1, -1, -1);
    let b_v2 = _mm_setr_epi8(-1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 12, 13, 14, 15);
    let b = _mm_or_si128(
      _mm_or_si128(_mm_shuffle_epi8(v0, b_v0), _mm_shuffle_epi8(v1, b_v1)),
      _mm_shuffle_epi8(v2, b_v2),
    );

    (r, g, b)
  }
}

/// Loads, byte-swaps, and deinterleaves 4 pixels of Rgb96.
///
/// # Safety
///
/// `ptr` must point to at least 12 readable u32; AVX2 must be available.
#[inline(always)]
unsafe fn load_deint_rgb96_4px<const BE: bool>(ptr: *const u32) -> (__m128i, __m128i, __m128i) {
  unsafe {
    let v0 = byteswap32_if_be::<BE>(_mm_loadu_si128(ptr.cast()));
    let v1 = byteswap32_if_be::<BE>(_mm_loadu_si128(ptr.add(4).cast()));
    let v2 = byteswap32_if_be::<BE>(_mm_loadu_si128(ptr.add(8).cast()));
    deinterleave_rgb96_4px(v0, v1, v2)
  }
}

/// Narrows four `u32x4` lane vectors (`>> 24` applied) into one `u8x16`.
#[inline(always)]
unsafe fn pack_u32x4_quad_to_u8x16(v0: __m128i, v1: __m128i, v2: __m128i, v3: __m128i) -> __m128i {
  unsafe { _mm_packus_epi16(_mm_packus_epi32(v0, v1), _mm_packus_epi32(v2, v3)) }
}

// ---- 8-pixel building blocks (shared by the 16-px main + 8-px cleanup) ------

/// Emits 8 Rgb96 pixels of packed u8 RGB at `out_ptr` (24 bytes).
#[inline(always)]
unsafe fn block_rgb96_to_rgb_8px<const BE: bool>(src_ptr: *const u32, out_ptr: *mut u8) {
  unsafe {
    let zero = _mm_setzero_si128();
    let (r0, g0, b0) = load_deint_rgb96_4px::<BE>(src_ptr);
    let (r1, g1, b1) = load_deint_rgb96_4px::<BE>(src_ptr.add(12));
    let r = pack_u32x4_quad_to_u8x16(
      _mm_srli_epi32::<24>(r0),
      _mm_srli_epi32::<24>(r1),
      zero,
      zero,
    );
    let g = pack_u32x4_quad_to_u8x16(
      _mm_srli_epi32::<24>(g0),
      _mm_srli_epi32::<24>(g1),
      zero,
      zero,
    );
    let b = pack_u32x4_quad_to_u8x16(
      _mm_srli_epi32::<24>(b0),
      _mm_srli_epi32::<24>(b1),
      zero,
      zero,
    );
    let mut tmp = [0u8; 48];
    write_rgb_16(r, g, b, tmp.as_mut_ptr());
    core::ptr::copy_nonoverlapping(tmp.as_ptr(), out_ptr, 24);
  }
}

/// Emits 8 Rgb96 pixels of packed u8 RGBA at `out_ptr` (32 bytes), alpha 0xFF.
#[inline(always)]
unsafe fn block_rgb96_to_rgba_8px<const BE: bool>(src_ptr: *const u32, out_ptr: *mut u8) {
  unsafe {
    let zero = _mm_setzero_si128();
    let opaque = _mm_set1_epi8(-1i8);
    let (r0, g0, b0) = load_deint_rgb96_4px::<BE>(src_ptr);
    let (r1, g1, b1) = load_deint_rgb96_4px::<BE>(src_ptr.add(12));
    let r = pack_u32x4_quad_to_u8x16(
      _mm_srli_epi32::<24>(r0),
      _mm_srli_epi32::<24>(r1),
      zero,
      zero,
    );
    let g = pack_u32x4_quad_to_u8x16(
      _mm_srli_epi32::<24>(g0),
      _mm_srli_epi32::<24>(g1),
      zero,
      zero,
    );
    let b = pack_u32x4_quad_to_u8x16(
      _mm_srli_epi32::<24>(b0),
      _mm_srli_epi32::<24>(b1),
      zero,
      zero,
    );
    let mut tmp = [0u8; 64];
    write_rgba_16(r, g, b, opaque, tmp.as_mut_ptr());
    core::ptr::copy_nonoverlapping(tmp.as_ptr(), out_ptr, 32);
  }
}

/// Emits 8 Rgb96 pixels of native u16 RGB at `out_ptr` (48 bytes).
#[inline(always)]
unsafe fn block_rgb96_to_rgb_u16_8px<const BE: bool>(src_ptr: *const u32, out_ptr: *mut u16) {
  unsafe {
    let (r0, g0, b0) = load_deint_rgb96_4px::<BE>(src_ptr);
    let (r1, g1, b1) = load_deint_rgb96_4px::<BE>(src_ptr.add(12));
    let r = _mm_packus_epi32(_mm_srli_epi32::<16>(r0), _mm_srli_epi32::<16>(r1));
    let g = _mm_packus_epi32(_mm_srli_epi32::<16>(g0), _mm_srli_epi32::<16>(g1));
    let b = _mm_packus_epi32(_mm_srli_epi32::<16>(b0), _mm_srli_epi32::<16>(b1));
    write_rgb_u16_8(r, g, b, out_ptr);
  }
}

/// Emits 8 Rgb96 pixels of native u16 RGBA at `out_ptr` (64 bytes), alpha 0xFFFF.
#[inline(always)]
unsafe fn block_rgb96_to_rgba_u16_8px<const BE: bool>(src_ptr: *const u32, out_ptr: *mut u16) {
  unsafe {
    let opaque = _mm_set1_epi16(0xFFFFu16 as i16);
    let (r0, g0, b0) = load_deint_rgb96_4px::<BE>(src_ptr);
    let (r1, g1, b1) = load_deint_rgb96_4px::<BE>(src_ptr.add(12));
    let r = _mm_packus_epi32(_mm_srli_epi32::<16>(r0), _mm_srli_epi32::<16>(r1));
    let g = _mm_packus_epi32(_mm_srli_epi32::<16>(g0), _mm_srli_epi32::<16>(g1));
    let b = _mm_packus_epi32(_mm_srli_epi32::<16>(b0), _mm_srli_epi32::<16>(b1));
    write_rgba_u16_8(r, g, b, opaque, out_ptr);
  }
}

// Rgb96 (R, G, B — 3 u32 elements per pixel).

/// AVX2 Rgb96 → packed u8 RGB. 16 pixels per outer iteration (2 x 8-px halves).
///
/// # Safety
///
/// 1. AVX2 must be available (caller obligation).
/// 2. `rgb96.len() >= width * 3`.
/// 3. `rgb_out.len() >= width * 3`.
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn avx2_rgb96_to_rgb_row<const BE: bool>(
  rgb96: &[u32],
  rgb_out: &mut [u8],
  width: usize,
) {
  debug_assert!(rgb96.len() >= width * 3, "rgb96 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 16 <= width {
      block_rgb96_to_rgb_8px::<BE>(rgb96.as_ptr().add(x * 3), rgb_out.as_mut_ptr().add(x * 3));
      block_rgb96_to_rgb_8px::<BE>(
        rgb96.as_ptr().add((x + 8) * 3),
        rgb_out.as_mut_ptr().add((x + 8) * 3),
      );
      x += 16;
    }
    while x + 8 <= width {
      block_rgb96_to_rgb_8px::<BE>(rgb96.as_ptr().add(x * 3), rgb_out.as_mut_ptr().add(x * 3));
      x += 8;
    }
    if x < width {
      scalar::rgb96_to_rgb_row::<BE>(&rgb96[x * 3..], &mut rgb_out[x * 3..], width - x);
    }
  }
}

/// AVX2 Rgb96 → packed u8 RGBA. 16 pixels per outer iteration. Alpha forced to 0xFF.
///
/// # Safety
///
/// 1. AVX2 must be available.
/// 2. `rgb96.len() >= width * 3`.
/// 3. `rgba_out.len() >= width * 4`.
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn avx2_rgb96_to_rgba_row<const BE: bool>(
  rgb96: &[u32],
  rgba_out: &mut [u8],
  width: usize,
) {
  debug_assert!(rgb96.len() >= width * 3, "rgb96 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 16 <= width {
      block_rgb96_to_rgba_8px::<BE>(rgb96.as_ptr().add(x * 3), rgba_out.as_mut_ptr().add(x * 4));
      block_rgb96_to_rgba_8px::<BE>(
        rgb96.as_ptr().add((x + 8) * 3),
        rgba_out.as_mut_ptr().add((x + 8) * 4),
      );
      x += 16;
    }
    while x + 8 <= width {
      block_rgb96_to_rgba_8px::<BE>(rgb96.as_ptr().add(x * 3), rgba_out.as_mut_ptr().add(x * 4));
      x += 8;
    }
    if x < width {
      scalar::rgb96_to_rgba_row::<BE>(&rgb96[x * 3..], &mut rgba_out[x * 4..], width - x);
    }
  }
}

/// AVX2 Rgb96 → native-depth u16 RGB. 16 pixels per outer iteration.
///
/// # Safety
///
/// 1. AVX2 must be available.
/// 2. `rgb96.len() >= width * 3`.
/// 3. `rgb_out.len() >= width * 3`.
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn avx2_rgb96_to_rgb_u16_row<const BE: bool>(
  rgb96: &[u32],
  rgb_out: &mut [u16],
  width: usize,
) {
  debug_assert!(rgb96.len() >= width * 3, "rgb96 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 16 <= width {
      block_rgb96_to_rgb_u16_8px::<BE>(rgb96.as_ptr().add(x * 3), rgb_out.as_mut_ptr().add(x * 3));
      block_rgb96_to_rgb_u16_8px::<BE>(
        rgb96.as_ptr().add((x + 8) * 3),
        rgb_out.as_mut_ptr().add((x + 8) * 3),
      );
      x += 16;
    }
    while x + 8 <= width {
      block_rgb96_to_rgb_u16_8px::<BE>(rgb96.as_ptr().add(x * 3), rgb_out.as_mut_ptr().add(x * 3));
      x += 8;
    }
    if x < width {
      scalar::rgb96_to_rgb_u16_row::<BE>(&rgb96[x * 3..], &mut rgb_out[x * 3..], width - x);
    }
  }
}

/// AVX2 Rgb96 → native-depth u16 RGBA. 16 pixels per outer iteration. Alpha 0xFFFF.
///
/// # Safety
///
/// 1. AVX2 must be available.
/// 2. `rgb96.len() >= width * 3`.
/// 3. `rgba_out.len() >= width * 4`.
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn avx2_rgb96_to_rgba_u16_row<const BE: bool>(
  rgb96: &[u32],
  rgba_out: &mut [u16],
  width: usize,
) {
  debug_assert!(rgb96.len() >= width * 3, "rgb96 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");

  unsafe {
    let mut x = 0usize;
    while x + 16 <= width {
      block_rgb96_to_rgba_u16_8px::<BE>(
        rgb96.as_ptr().add(x * 3),
        rgba_out.as_mut_ptr().add(x * 4),
      );
      block_rgb96_to_rgba_u16_8px::<BE>(
        rgb96.as_ptr().add((x + 8) * 3),
        rgba_out.as_mut_ptr().add((x + 8) * 4),
      );
      x += 16;
    }
    while x + 8 <= width {
      block_rgb96_to_rgba_u16_8px::<BE>(
        rgb96.as_ptr().add(x * 3),
        rgba_out.as_mut_ptr().add(x * 4),
      );
      x += 8;
    }
    if x < width {
      scalar::rgb96_to_rgba_u16_row::<BE>(&rgb96[x * 3..], &mut rgba_out[x * 4..], width - x);
    }
  }
}

// ---- Rgba128 deinterleave (4 u32 per pixel, 4 loads per 4 px) ---------------

/// Deinterleave 4 pixels of stride-4 u32 (Rgba128) into `(R, G, B, A)` `u32x4`
/// lane vectors via the SSE2 `unpacklo/hi` 4x4 transpose ladder.
///
/// # Safety
///
/// Caller must have verified AVX2 availability.
#[inline(always)]
unsafe fn deinterleave_rgba128_4px(
  v0: __m128i,
  v1: __m128i,
  v2: __m128i,
  v3: __m128i,
) -> (__m128i, __m128i, __m128i, __m128i) {
  unsafe {
    let t0 = _mm_unpacklo_epi32(v0, v1);
    let t1 = _mm_unpackhi_epi32(v0, v1);
    let t2 = _mm_unpacklo_epi32(v2, v3);
    let t3 = _mm_unpackhi_epi32(v2, v3);
    let r = _mm_unpacklo_epi64(t0, t2);
    let g = _mm_unpackhi_epi64(t0, t2);
    let b = _mm_unpacklo_epi64(t1, t3);
    let a = _mm_unpackhi_epi64(t1, t3);
    (r, g, b, a)
  }
}

/// Loads, byte-swaps, and deinterleaves 4 pixels of Rgba128.
///
/// # Safety
///
/// `ptr` must point to at least 16 readable u32; AVX2 must be available.
#[inline(always)]
unsafe fn load_deint_rgba128_4px<const BE: bool>(
  ptr: *const u32,
) -> (__m128i, __m128i, __m128i, __m128i) {
  unsafe {
    let v0 = byteswap32_if_be::<BE>(_mm_loadu_si128(ptr.cast()));
    let v1 = byteswap32_if_be::<BE>(_mm_loadu_si128(ptr.add(4).cast()));
    let v2 = byteswap32_if_be::<BE>(_mm_loadu_si128(ptr.add(8).cast()));
    let v3 = byteswap32_if_be::<BE>(_mm_loadu_si128(ptr.add(12).cast()));
    deinterleave_rgba128_4px(v0, v1, v2, v3)
  }
}

/// Emits 8 Rgba128 pixels of packed u8 RGB at `out_ptr` (24 bytes), alpha dropped.
#[inline(always)]
unsafe fn block_rgba128_to_rgb_8px<const BE: bool>(src_ptr: *const u32, out_ptr: *mut u8) {
  unsafe {
    let zero = _mm_setzero_si128();
    let (r0, g0, b0, _) = load_deint_rgba128_4px::<BE>(src_ptr);
    let (r1, g1, b1, _) = load_deint_rgba128_4px::<BE>(src_ptr.add(16));
    let r = pack_u32x4_quad_to_u8x16(
      _mm_srli_epi32::<24>(r0),
      _mm_srli_epi32::<24>(r1),
      zero,
      zero,
    );
    let g = pack_u32x4_quad_to_u8x16(
      _mm_srli_epi32::<24>(g0),
      _mm_srli_epi32::<24>(g1),
      zero,
      zero,
    );
    let b = pack_u32x4_quad_to_u8x16(
      _mm_srli_epi32::<24>(b0),
      _mm_srli_epi32::<24>(b1),
      zero,
      zero,
    );
    let mut tmp = [0u8; 48];
    write_rgb_16(r, g, b, tmp.as_mut_ptr());
    core::ptr::copy_nonoverlapping(tmp.as_ptr(), out_ptr, 24);
  }
}

/// Emits 8 Rgba128 pixels of packed u8 RGBA at `out_ptr` (32 bytes), alpha passed through.
#[inline(always)]
unsafe fn block_rgba128_to_rgba_8px<const BE: bool>(src_ptr: *const u32, out_ptr: *mut u8) {
  unsafe {
    let zero = _mm_setzero_si128();
    let (r0, g0, b0, a0) = load_deint_rgba128_4px::<BE>(src_ptr);
    let (r1, g1, b1, a1) = load_deint_rgba128_4px::<BE>(src_ptr.add(16));
    let r = pack_u32x4_quad_to_u8x16(
      _mm_srli_epi32::<24>(r0),
      _mm_srli_epi32::<24>(r1),
      zero,
      zero,
    );
    let g = pack_u32x4_quad_to_u8x16(
      _mm_srli_epi32::<24>(g0),
      _mm_srli_epi32::<24>(g1),
      zero,
      zero,
    );
    let b = pack_u32x4_quad_to_u8x16(
      _mm_srli_epi32::<24>(b0),
      _mm_srli_epi32::<24>(b1),
      zero,
      zero,
    );
    let a = pack_u32x4_quad_to_u8x16(
      _mm_srli_epi32::<24>(a0),
      _mm_srli_epi32::<24>(a1),
      zero,
      zero,
    );
    let mut tmp = [0u8; 64];
    write_rgba_16(r, g, b, a, tmp.as_mut_ptr());
    core::ptr::copy_nonoverlapping(tmp.as_ptr(), out_ptr, 32);
  }
}

/// Emits 8 Rgba128 pixels of native u16 RGB at `out_ptr` (48 bytes), alpha dropped.
#[inline(always)]
unsafe fn block_rgba128_to_rgb_u16_8px<const BE: bool>(src_ptr: *const u32, out_ptr: *mut u16) {
  unsafe {
    let (r0, g0, b0, _) = load_deint_rgba128_4px::<BE>(src_ptr);
    let (r1, g1, b1, _) = load_deint_rgba128_4px::<BE>(src_ptr.add(16));
    let r = _mm_packus_epi32(_mm_srli_epi32::<16>(r0), _mm_srli_epi32::<16>(r1));
    let g = _mm_packus_epi32(_mm_srli_epi32::<16>(g0), _mm_srli_epi32::<16>(g1));
    let b = _mm_packus_epi32(_mm_srli_epi32::<16>(b0), _mm_srli_epi32::<16>(b1));
    write_rgb_u16_8(r, g, b, out_ptr);
  }
}

/// Emits 8 Rgba128 pixels of native u16 RGBA at `out_ptr` (64 bytes), alpha passed through.
#[inline(always)]
unsafe fn block_rgba128_to_rgba_u16_8px<const BE: bool>(src_ptr: *const u32, out_ptr: *mut u16) {
  unsafe {
    let (r0, g0, b0, a0) = load_deint_rgba128_4px::<BE>(src_ptr);
    let (r1, g1, b1, a1) = load_deint_rgba128_4px::<BE>(src_ptr.add(16));
    let r = _mm_packus_epi32(_mm_srli_epi32::<16>(r0), _mm_srli_epi32::<16>(r1));
    let g = _mm_packus_epi32(_mm_srli_epi32::<16>(g0), _mm_srli_epi32::<16>(g1));
    let b = _mm_packus_epi32(_mm_srli_epi32::<16>(b0), _mm_srli_epi32::<16>(b1));
    let a = _mm_packus_epi32(_mm_srli_epi32::<16>(a0), _mm_srli_epi32::<16>(a1));
    write_rgba_u16_8(r, g, b, a, out_ptr);
  }
}

// Rgba128 (R, G, B, A — 4 u32 elements per pixel).

/// AVX2 Rgba128 → packed u8 RGB. 16 pixels per outer iteration. Alpha discarded.
///
/// # Safety
///
/// 1. AVX2 must be available.
/// 2. `rgba128.len() >= width * 4`.
/// 3. `rgb_out.len() >= width * 3`.
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn avx2_rgba128_to_rgb_row<const BE: bool>(
  rgba128: &[u32],
  rgb_out: &mut [u8],
  width: usize,
) {
  debug_assert!(rgba128.len() >= width * 4, "rgba128 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");
  unsafe {
    let mut x = 0usize;
    while x + 16 <= width {
      block_rgba128_to_rgb_8px::<BE>(rgba128.as_ptr().add(x * 4), rgb_out.as_mut_ptr().add(x * 3));
      block_rgba128_to_rgb_8px::<BE>(
        rgba128.as_ptr().add((x + 8) * 4),
        rgb_out.as_mut_ptr().add((x + 8) * 3),
      );
      x += 16;
    }
    while x + 8 <= width {
      block_rgba128_to_rgb_8px::<BE>(rgba128.as_ptr().add(x * 4), rgb_out.as_mut_ptr().add(x * 3));
      x += 8;
    }
    if x < width {
      scalar::rgba128_to_rgb_row::<BE>(&rgba128[x * 4..], &mut rgb_out[x * 3..], width - x);
    }
  }
}

/// AVX2 Rgba128 → packed u8 RGBA. 16 pixels per outer iteration. Alpha passed through.
///
/// # Safety
///
/// 1. AVX2 must be available.
/// 2. `rgba128.len() >= width * 4`.
/// 3. `rgba_out.len() >= width * 4`.
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn avx2_rgba128_to_rgba_row<const BE: bool>(
  rgba128: &[u32],
  rgba_out: &mut [u8],
  width: usize,
) {
  debug_assert!(rgba128.len() >= width * 4, "rgba128 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");
  unsafe {
    let mut x = 0usize;
    while x + 16 <= width {
      block_rgba128_to_rgba_8px::<BE>(
        rgba128.as_ptr().add(x * 4),
        rgba_out.as_mut_ptr().add(x * 4),
      );
      block_rgba128_to_rgba_8px::<BE>(
        rgba128.as_ptr().add((x + 8) * 4),
        rgba_out.as_mut_ptr().add((x + 8) * 4),
      );
      x += 16;
    }
    while x + 8 <= width {
      block_rgba128_to_rgba_8px::<BE>(
        rgba128.as_ptr().add(x * 4),
        rgba_out.as_mut_ptr().add(x * 4),
      );
      x += 8;
    }
    if x < width {
      scalar::rgba128_to_rgba_row::<BE>(&rgba128[x * 4..], &mut rgba_out[x * 4..], width - x);
    }
  }
}

/// AVX2 Rgba128 → native-depth u16 RGB. 16 pixels per outer iteration. Alpha discarded.
///
/// # Safety
///
/// 1. AVX2 must be available.
/// 2. `rgba128.len() >= width * 4`.
/// 3. `rgb_out.len() >= width * 3`.
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn avx2_rgba128_to_rgb_u16_row<const BE: bool>(
  rgba128: &[u32],
  rgb_out: &mut [u16],
  width: usize,
) {
  debug_assert!(rgba128.len() >= width * 4, "rgba128 row too short");
  debug_assert!(rgb_out.len() >= width * 3, "rgb_out row too short");
  unsafe {
    let mut x = 0usize;
    while x + 16 <= width {
      block_rgba128_to_rgb_u16_8px::<BE>(
        rgba128.as_ptr().add(x * 4),
        rgb_out.as_mut_ptr().add(x * 3),
      );
      block_rgba128_to_rgb_u16_8px::<BE>(
        rgba128.as_ptr().add((x + 8) * 4),
        rgb_out.as_mut_ptr().add((x + 8) * 3),
      );
      x += 16;
    }
    while x + 8 <= width {
      block_rgba128_to_rgb_u16_8px::<BE>(
        rgba128.as_ptr().add(x * 4),
        rgb_out.as_mut_ptr().add(x * 3),
      );
      x += 8;
    }
    if x < width {
      scalar::rgba128_to_rgb_u16_row::<BE>(&rgba128[x * 4..], &mut rgb_out[x * 3..], width - x);
    }
  }
}

/// AVX2 Rgba128 → native-depth u16 RGBA. 16 pixels per outer iteration. Alpha passed through.
///
/// # Safety
///
/// 1. AVX2 must be available.
/// 2. `rgba128.len() >= width * 4`.
/// 3. `rgba_out.len() >= width * 4`.
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn avx2_rgba128_to_rgba_u16_row<const BE: bool>(
  rgba128: &[u32],
  rgba_out: &mut [u16],
  width: usize,
) {
  debug_assert!(rgba128.len() >= width * 4, "rgba128 row too short");
  debug_assert!(rgba_out.len() >= width * 4, "rgba_out row too short");
  unsafe {
    let mut x = 0usize;
    while x + 16 <= width {
      block_rgba128_to_rgba_u16_8px::<BE>(
        rgba128.as_ptr().add(x * 4),
        rgba_out.as_mut_ptr().add(x * 4),
      );
      block_rgba128_to_rgba_u16_8px::<BE>(
        rgba128.as_ptr().add((x + 8) * 4),
        rgba_out.as_mut_ptr().add((x + 8) * 4),
      );
      x += 16;
    }
    while x + 8 <= width {
      block_rgba128_to_rgba_u16_8px::<BE>(
        rgba128.as_ptr().add(x * 4),
        rgba_out.as_mut_ptr().add(x * 4),
      );
      x += 8;
    }
    if x < width {
      scalar::rgba128_to_rgba_u16_row::<BE>(&rgba128[x * 4..], &mut rgba_out[x * 4..], width - x);
    }
  }
}