use core::arch::x86_64::*;
use super::*;
use crate::{ColorMatrix, row::scalar};
const HOST_NATIVE_BE: bool = cfg!(target_endian = "big");
#[inline]
#[target_feature(enable = "avx2")]
unsafe fn unpack_y2xx_16px_avx2(
ptr: *const u16,
shr_count: __m128i,
) -> (__m256i, __m256i, __m256i) {
unsafe {
let v0 = _mm256_loadu_si256(ptr.cast());
let v1 = _mm256_loadu_si256(ptr.add(16).cast());
let split_idx = _mm256_setr_epi8(
0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15, 0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15, );
let v0s = _mm256_shuffle_epi8(v0, split_idx);
let v1s = _mm256_shuffle_epi8(v1, split_idx);
let v0p = _mm256_permute4x64_epi64::<0xD8>(v0s);
let v1p = _mm256_permute4x64_epi64::<0xD8>(v1s);
let y_raw = _mm256_permute2x128_si256::<0x20>(v0p, v1p); let chroma_raw = _mm256_permute2x128_si256::<0x31>(v0p, v1p);
let y_vec = _mm256_srl_epi16(y_raw, shr_count);
let chroma = _mm256_srl_epi16(chroma_raw, shr_count);
let u_idx = _mm256_setr_epi8(
0, 1, 4, 5, 8, 9, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 4, 5, 8, 9, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1, );
let v_idx = _mm256_setr_epi8(
2, 3, 6, 7, 10, 11, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, 2, 3, 6, 7, 10, 11, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, );
let u_per_lane = _mm256_shuffle_epi8(chroma, u_idx);
let v_per_lane = _mm256_shuffle_epi8(chroma, v_idx);
let u_vec = _mm256_permute4x64_epi64::<0x88>(u_per_lane);
let v_vec = _mm256_permute4x64_epi64::<0x88>(v_per_lane);
(y_vec, u_vec, v_vec)
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn y2xx_n_to_rgb_or_rgba_row<
const BITS: u32,
const ALPHA: bool,
const BE: bool,
>(
packed: &[u16],
out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
const {
assert!(
BITS == 10 || BITS == 12,
"y2xx_n_to_rgb_or_rgba_row requires BITS in {{10, 12}}"
);
}
debug_assert!(width.is_multiple_of(2), "Y2xx requires even width");
debug_assert!(packed.len() >= width * 2);
let bpp: usize = if ALPHA { 4 } else { 3 };
debug_assert!(out.len() >= width * bpp);
let coeffs = scalar::Coefficients::for_matrix(matrix);
let (y_off, y_scale, c_scale) = scalar::range_params_n::<BITS, 8>(full_range);
let bias = scalar::chroma_bias::<BITS>();
const RND: i32 = 1 << 14;
unsafe {
let mut x = 0usize;
if BE == HOST_NATIVE_BE {
let rnd_v = _mm256_set1_epi32(RND);
let y_off_v = _mm256_set1_epi16(y_off as i16);
let y_scale_v = _mm256_set1_epi32(y_scale);
let c_scale_v = _mm256_set1_epi32(c_scale);
let bias_v = _mm256_set1_epi16(bias as i16);
let shr_count = _mm_cvtsi32_si128((16 - BITS) as i32);
let cru = _mm256_set1_epi32(coeffs.r_u());
let crv = _mm256_set1_epi32(coeffs.r_v());
let cgu = _mm256_set1_epi32(coeffs.g_u());
let cgv = _mm256_set1_epi32(coeffs.g_v());
let cbu = _mm256_set1_epi32(coeffs.b_u());
let cbv = _mm256_set1_epi32(coeffs.b_v());
while x + 16 <= width {
let (y_vec, u_vec, v_vec) = unpack_y2xx_16px_avx2(packed.as_ptr().add(x * 2), shr_count);
let y_i16 = y_vec;
let u_i16 = _mm256_sub_epi16(u_vec, bias_v);
let v_i16 = _mm256_sub_epi16(v_vec, bias_v);
let u_lo_i32 = _mm256_cvtepi16_epi32(_mm256_castsi256_si128(u_i16));
let u_hi_i32 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256::<1>(u_i16));
let v_lo_i32 = _mm256_cvtepi16_epi32(_mm256_castsi256_si128(v_i16));
let v_hi_i32 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256::<1>(v_i16));
let u_d_lo = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(u_lo_i32, c_scale_v),
rnd_v,
));
let u_d_hi = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(u_hi_i32, c_scale_v),
rnd_v,
));
let v_d_lo = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(v_lo_i32, c_scale_v),
rnd_v,
));
let v_d_hi = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(v_hi_i32, c_scale_v),
rnd_v,
));
let r_chroma = chroma_i16x16(cru, crv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v);
let g_chroma = chroma_i16x16(cgu, cgv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v);
let b_chroma = chroma_i16x16(cbu, cbv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v);
let (r_dup_lo, _r_dup_hi) = chroma_dup(r_chroma);
let (g_dup_lo, _g_dup_hi) = chroma_dup(g_chroma);
let (b_dup_lo, _b_dup_hi) = chroma_dup(b_chroma);
let y_scaled = scale_y(y_i16, y_off_v, y_scale_v, rnd_v);
let zero = _mm256_setzero_si256();
let r_u8 = narrow_u8x32(_mm256_adds_epi16(y_scaled, r_dup_lo), zero);
let g_u8 = narrow_u8x32(_mm256_adds_epi16(y_scaled, g_dup_lo), zero);
let b_u8 = narrow_u8x32(_mm256_adds_epi16(y_scaled, b_dup_lo), zero);
let mut r_tmp = [0u8; 32];
let mut g_tmp = [0u8; 32];
let mut b_tmp = [0u8; 32];
_mm256_storeu_si256(r_tmp.as_mut_ptr().cast(), r_u8);
_mm256_storeu_si256(g_tmp.as_mut_ptr().cast(), g_u8);
_mm256_storeu_si256(b_tmp.as_mut_ptr().cast(), b_u8);
if ALPHA {
let dst = &mut out[x * 4..x * 4 + 16 * 4];
for i in 0..16 {
dst[i * 4] = r_tmp[i];
dst[i * 4 + 1] = g_tmp[i];
dst[i * 4 + 2] = b_tmp[i];
dst[i * 4 + 3] = 0xFF;
}
} else {
let dst = &mut out[x * 3..x * 3 + 16 * 3];
for i in 0..16 {
dst[i * 3] = r_tmp[i];
dst[i * 3 + 1] = g_tmp[i];
dst[i * 3 + 2] = b_tmp[i];
}
}
x += 16;
}
}
if x < width {
let tail_packed = &packed[x * 2..width * 2];
let tail_out = &mut out[x * bpp..width * bpp];
let tail_w = width - x;
scalar::y2xx_n_to_rgb_or_rgba_row::<BITS, ALPHA, BE>(
tail_packed,
tail_out,
tail_w,
matrix,
full_range,
);
}
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn y2xx_n_to_rgb_u16_or_rgba_u16_row<
const BITS: u32,
const ALPHA: bool,
const BE: bool,
>(
packed: &[u16],
out: &mut [u16],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
const {
assert!(
BITS == 10 || BITS == 12,
"y2xx_n_to_rgb_u16_or_rgba_u16_row requires BITS in {{10, 12}}"
);
}
debug_assert!(width.is_multiple_of(2), "Y2xx requires even width");
debug_assert!(packed.len() >= width * 2);
let bpp: usize = if ALPHA { 4 } else { 3 };
debug_assert!(out.len() >= width * bpp);
let coeffs = scalar::Coefficients::for_matrix(matrix);
let (y_off, y_scale, c_scale) = scalar::range_params_n::<BITS, BITS>(full_range);
let bias = scalar::chroma_bias::<BITS>();
const RND: i32 = 1 << 14;
let out_max: i16 = ((1i32 << BITS) - 1) as i16;
unsafe {
let mut x = 0usize;
if BE == HOST_NATIVE_BE {
let rnd_v = _mm256_set1_epi32(RND);
let y_off_v = _mm256_set1_epi16(y_off as i16);
let y_scale_v = _mm256_set1_epi32(y_scale);
let c_scale_v = _mm256_set1_epi32(c_scale);
let bias_v = _mm256_set1_epi16(bias as i16);
let shr_count = _mm_cvtsi32_si128((16 - BITS) as i32);
let max_v = _mm256_set1_epi16(out_max);
let zero_v = _mm256_set1_epi16(0);
let cru = _mm256_set1_epi32(coeffs.r_u());
let crv = _mm256_set1_epi32(coeffs.r_v());
let cgu = _mm256_set1_epi32(coeffs.g_u());
let cgv = _mm256_set1_epi32(coeffs.g_v());
let cbu = _mm256_set1_epi32(coeffs.b_u());
let cbv = _mm256_set1_epi32(coeffs.b_v());
while x + 16 <= width {
let (y_vec, u_vec, v_vec) = unpack_y2xx_16px_avx2(packed.as_ptr().add(x * 2), shr_count);
let y_i16 = y_vec;
let u_i16 = _mm256_sub_epi16(u_vec, bias_v);
let v_i16 = _mm256_sub_epi16(v_vec, bias_v);
let u_lo_i32 = _mm256_cvtepi16_epi32(_mm256_castsi256_si128(u_i16));
let u_hi_i32 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256::<1>(u_i16));
let v_lo_i32 = _mm256_cvtepi16_epi32(_mm256_castsi256_si128(v_i16));
let v_hi_i32 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256::<1>(v_i16));
let u_d_lo = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(u_lo_i32, c_scale_v),
rnd_v,
));
let u_d_hi = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(u_hi_i32, c_scale_v),
rnd_v,
));
let v_d_lo = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(v_lo_i32, c_scale_v),
rnd_v,
));
let v_d_hi = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(v_hi_i32, c_scale_v),
rnd_v,
));
let r_chroma = chroma_i16x16(cru, crv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v);
let g_chroma = chroma_i16x16(cgu, cgv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v);
let b_chroma = chroma_i16x16(cbu, cbv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v);
let (r_dup_lo, _r_dup_hi) = chroma_dup(r_chroma);
let (g_dup_lo, _g_dup_hi) = chroma_dup(g_chroma);
let (b_dup_lo, _b_dup_hi) = chroma_dup(b_chroma);
let y_scaled = scale_y(y_i16, y_off_v, y_scale_v, rnd_v);
let r = clamp_u16_max_x16(_mm256_adds_epi16(y_scaled, r_dup_lo), zero_v, max_v);
let g = clamp_u16_max_x16(_mm256_adds_epi16(y_scaled, g_dup_lo), zero_v, max_v);
let b = clamp_u16_max_x16(_mm256_adds_epi16(y_scaled, b_dup_lo), zero_v, max_v);
if ALPHA {
let alpha_u16 = _mm_set1_epi16(out_max);
let dst = out.as_mut_ptr().add(x * 4);
write_rgba_u16_8(
_mm256_castsi256_si128(r),
_mm256_castsi256_si128(g),
_mm256_castsi256_si128(b),
alpha_u16,
dst,
);
write_rgba_u16_8(
_mm256_extracti128_si256::<1>(r),
_mm256_extracti128_si256::<1>(g),
_mm256_extracti128_si256::<1>(b),
alpha_u16,
dst.add(32),
);
} else {
let dst = out.as_mut_ptr().add(x * 3);
write_rgb_u16_8(
_mm256_castsi256_si128(r),
_mm256_castsi256_si128(g),
_mm256_castsi256_si128(b),
dst,
);
write_rgb_u16_8(
_mm256_extracti128_si256::<1>(r),
_mm256_extracti128_si256::<1>(g),
_mm256_extracti128_si256::<1>(b),
dst.add(24),
);
}
x += 16;
}
}
if x < width {
let tail_packed = &packed[x * 2..width * 2];
let tail_out = &mut out[x * bpp..width * bpp];
let tail_w = width - x;
scalar::y2xx_n_to_rgb_u16_or_rgba_u16_row::<BITS, ALPHA, BE>(
tail_packed,
tail_out,
tail_w,
matrix,
full_range,
);
}
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn y2xx_n_to_luma_row<const BITS: u32, const BE: bool>(
packed: &[u16],
luma_out: &mut [u8],
width: usize,
) {
const {
assert!(
BITS == 10 || BITS == 12,
"y2xx_n_to_luma_row requires BITS in {{10, 12}}"
);
}
debug_assert!(width.is_multiple_of(2), "Y2xx requires even width");
debug_assert!(packed.len() >= width * 2);
debug_assert!(luma_out.len() >= width);
unsafe {
let mut x = 0usize;
if BE == HOST_NATIVE_BE {
let split_idx = _mm256_setr_epi8(
0, 1, 4, 5, 8, 9, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 4, 5, 8, 9, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1, );
while x + 16 <= width {
let v0 = _mm256_loadu_si256(packed.as_ptr().add(x * 2).cast());
let v1 = _mm256_loadu_si256(packed.as_ptr().add(x * 2 + 16).cast());
let v0s = _mm256_shuffle_epi8(v0, split_idx);
let v1s = _mm256_shuffle_epi8(v1, split_idx);
let v0p = _mm256_permute4x64_epi64::<0x88>(v0s);
let v1p = _mm256_permute4x64_epi64::<0x88>(v1s);
let y_vec = _mm256_permute2x128_si256::<0x20>(v0p, v1p);
let y_shr = _mm256_srli_epi16::<8>(y_vec);
let y_u8 = narrow_u8x32(y_shr, _mm256_setzero_si256());
let mut tmp = [0u8; 32];
_mm256_storeu_si256(tmp.as_mut_ptr().cast(), y_u8);
luma_out[x..x + 16].copy_from_slice(&tmp[..16]);
x += 16;
}
}
if x < width {
let tail_packed = &packed[x * 2..width * 2];
let tail_out = &mut luma_out[x..width];
let tail_w = width - x;
scalar::y2xx_n_to_luma_row::<BITS, BE>(tail_packed, tail_out, tail_w);
}
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn y2xx_n_to_luma_u16_row<const BITS: u32, const BE: bool>(
packed: &[u16],
luma_out: &mut [u16],
width: usize,
) {
const {
assert!(
BITS == 10 || BITS == 12,
"y2xx_n_to_luma_u16_row requires BITS in {{10, 12}}"
);
}
debug_assert!(width.is_multiple_of(2), "Y2xx requires even width");
debug_assert!(packed.len() >= width * 2);
debug_assert!(luma_out.len() >= width);
unsafe {
let mut x = 0usize;
if BE == HOST_NATIVE_BE {
let shr_count = _mm_cvtsi32_si128((16 - BITS) as i32);
let split_idx = _mm256_setr_epi8(
0, 1, 4, 5, 8, 9, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 4, 5, 8, 9, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1, );
while x + 16 <= width {
let v0 = _mm256_loadu_si256(packed.as_ptr().add(x * 2).cast());
let v1 = _mm256_loadu_si256(packed.as_ptr().add(x * 2 + 16).cast());
let v0s = _mm256_shuffle_epi8(v0, split_idx);
let v1s = _mm256_shuffle_epi8(v1, split_idx);
let v0p = _mm256_permute4x64_epi64::<0x88>(v0s);
let v1p = _mm256_permute4x64_epi64::<0x88>(v1s);
let y_vec = _mm256_permute2x128_si256::<0x20>(v0p, v1p);
let y_low = _mm256_srl_epi16(y_vec, shr_count);
_mm256_storeu_si256(luma_out.as_mut_ptr().add(x).cast(), y_low);
x += 16;
}
}
if x < width {
let tail_packed = &packed[x * 2..width * 2];
let tail_out = &mut luma_out[x..width];
let tail_w = width - x;
scalar::y2xx_n_to_luma_u16_row::<BITS, BE>(tail_packed, tail_out, tail_w);
}
}
}