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 = "sse4.1")]
unsafe fn unpack_y2xx_8px_sse41(
ptr: *const u16,
shr_count: __m128i,
) -> (__m128i, __m128i, __m128i) {
unsafe {
let lo = _mm_loadu_si128(ptr.cast()); let hi = _mm_loadu_si128(ptr.add(8).cast());
let y_idx = _mm_setr_epi8(0, 1, 4, 5, 8, 9, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1);
let y_lo = _mm_shuffle_epi8(lo, y_idx); let y_hi = _mm_shuffle_epi8(hi, y_idx); let y_vec_raw = _mm_unpacklo_epi64(y_lo, y_hi);
let c_idx = _mm_setr_epi8(2, 3, 6, 7, 10, 11, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1);
let c_lo = _mm_shuffle_epi8(lo, c_idx); let c_hi = _mm_shuffle_epi8(hi, c_idx); let chroma_raw = _mm_unpacklo_epi64(c_lo, c_hi);
let y_vec = _mm_srl_epi16(y_vec_raw, shr_count);
let chroma = _mm_srl_epi16(chroma_raw, shr_count);
let u_idx = _mm_setr_epi8(0, 1, 4, 5, 8, 9, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1);
let v_idx = _mm_setr_epi8(2, 3, 6, 7, 10, 11, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1);
let u_vec = _mm_shuffle_epi8(chroma, u_idx);
let v_vec = _mm_shuffle_epi8(chroma, v_idx);
(y_vec, u_vec, v_vec)
}
}
#[inline]
#[target_feature(enable = "sse4.1")]
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 = _mm_set1_epi32(RND);
let y_off_v = _mm_set1_epi16(y_off as i16);
let y_scale_v = _mm_set1_epi32(y_scale);
let c_scale_v = _mm_set1_epi32(c_scale);
let bias_v = _mm_set1_epi16(bias as i16);
let shr_count = _mm_cvtsi32_si128((16 - BITS) as i32);
let cru = _mm_set1_epi32(coeffs.r_u());
let crv = _mm_set1_epi32(coeffs.r_v());
let cgu = _mm_set1_epi32(coeffs.g_u());
let cgv = _mm_set1_epi32(coeffs.g_v());
let cbu = _mm_set1_epi32(coeffs.b_u());
let cbv = _mm_set1_epi32(coeffs.b_v());
while x + 8 <= width {
let (y_vec, u_vec, v_vec) = unpack_y2xx_8px_sse41(packed.as_ptr().add(x * 2), shr_count);
let y_i16 = y_vec;
let u_i16 = _mm_sub_epi16(u_vec, bias_v);
let v_i16 = _mm_sub_epi16(v_vec, bias_v);
let u_lo_i32 = _mm_cvtepi16_epi32(u_i16);
let u_hi_i32 = _mm_cvtepi16_epi32(_mm_srli_si128::<8>(u_i16));
let v_lo_i32 = _mm_cvtepi16_epi32(v_i16);
let v_hi_i32 = _mm_cvtepi16_epi32(_mm_srli_si128::<8>(v_i16));
let u_d_lo = q15_shift(_mm_add_epi32(_mm_mullo_epi32(u_lo_i32, c_scale_v), rnd_v));
let u_d_hi = q15_shift(_mm_add_epi32(_mm_mullo_epi32(u_hi_i32, c_scale_v), rnd_v));
let v_d_lo = q15_shift(_mm_add_epi32(_mm_mullo_epi32(v_lo_i32, c_scale_v), rnd_v));
let v_d_hi = q15_shift(_mm_add_epi32(_mm_mullo_epi32(v_hi_i32, c_scale_v), rnd_v));
let r_chroma = chroma_i16x8(cru, crv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v);
let g_chroma = chroma_i16x8(cgu, cgv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v);
let b_chroma = chroma_i16x8(cbu, cbv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v);
let r_dup = _mm_unpacklo_epi16(r_chroma, r_chroma);
let g_dup = _mm_unpacklo_epi16(g_chroma, g_chroma);
let b_dup = _mm_unpacklo_epi16(b_chroma, b_chroma);
let y_scaled = scale_y(y_i16, y_off_v, y_scale_v, rnd_v);
let zero = _mm_setzero_si128();
let r_u8 = _mm_packus_epi16(_mm_adds_epi16(y_scaled, r_dup), zero);
let g_u8 = _mm_packus_epi16(_mm_adds_epi16(y_scaled, g_dup), zero);
let b_u8 = _mm_packus_epi16(_mm_adds_epi16(y_scaled, b_dup), zero);
let mut r_tmp = [0u8; 16];
let mut g_tmp = [0u8; 16];
let mut b_tmp = [0u8; 16];
_mm_storeu_si128(r_tmp.as_mut_ptr().cast(), r_u8);
_mm_storeu_si128(g_tmp.as_mut_ptr().cast(), g_u8);
_mm_storeu_si128(b_tmp.as_mut_ptr().cast(), b_u8);
if ALPHA {
let dst = &mut out[x * 4..x * 4 + 8 * 4];
for i in 0..8 {
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 + 8 * 3];
for i in 0..8 {
dst[i * 3] = r_tmp[i];
dst[i * 3 + 1] = g_tmp[i];
dst[i * 3 + 2] = b_tmp[i];
}
}
x += 8;
}
}
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 = "sse4.1")]
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 = _mm_set1_epi32(RND);
let y_off_v = _mm_set1_epi16(y_off as i16);
let y_scale_v = _mm_set1_epi32(y_scale);
let c_scale_v = _mm_set1_epi32(c_scale);
let bias_v = _mm_set1_epi16(bias as i16);
let shr_count = _mm_cvtsi32_si128((16 - BITS) as i32);
let max_v = _mm_set1_epi16(out_max);
let zero_v = _mm_set1_epi16(0);
let cru = _mm_set1_epi32(coeffs.r_u());
let crv = _mm_set1_epi32(coeffs.r_v());
let cgu = _mm_set1_epi32(coeffs.g_u());
let cgv = _mm_set1_epi32(coeffs.g_v());
let cbu = _mm_set1_epi32(coeffs.b_u());
let cbv = _mm_set1_epi32(coeffs.b_v());
while x + 8 <= width {
let (y_vec, u_vec, v_vec) = unpack_y2xx_8px_sse41(packed.as_ptr().add(x * 2), shr_count);
let y_i16 = y_vec;
let u_i16 = _mm_sub_epi16(u_vec, bias_v);
let v_i16 = _mm_sub_epi16(v_vec, bias_v);
let u_lo_i32 = _mm_cvtepi16_epi32(u_i16);
let u_hi_i32 = _mm_cvtepi16_epi32(_mm_srli_si128::<8>(u_i16));
let v_lo_i32 = _mm_cvtepi16_epi32(v_i16);
let v_hi_i32 = _mm_cvtepi16_epi32(_mm_srli_si128::<8>(v_i16));
let u_d_lo = q15_shift(_mm_add_epi32(_mm_mullo_epi32(u_lo_i32, c_scale_v), rnd_v));
let u_d_hi = q15_shift(_mm_add_epi32(_mm_mullo_epi32(u_hi_i32, c_scale_v), rnd_v));
let v_d_lo = q15_shift(_mm_add_epi32(_mm_mullo_epi32(v_lo_i32, c_scale_v), rnd_v));
let v_d_hi = q15_shift(_mm_add_epi32(_mm_mullo_epi32(v_hi_i32, c_scale_v), rnd_v));
let r_chroma = chroma_i16x8(cru, crv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v);
let g_chroma = chroma_i16x8(cgu, cgv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v);
let b_chroma = chroma_i16x8(cbu, cbv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v);
let r_dup = _mm_unpacklo_epi16(r_chroma, r_chroma);
let g_dup = _mm_unpacklo_epi16(g_chroma, g_chroma);
let b_dup = _mm_unpacklo_epi16(b_chroma, b_chroma);
let y_scaled = scale_y(y_i16, y_off_v, y_scale_v, rnd_v);
let r = clamp_u16_max(_mm_adds_epi16(y_scaled, r_dup), zero_v, max_v);
let g = clamp_u16_max(_mm_adds_epi16(y_scaled, g_dup), zero_v, max_v);
let b = clamp_u16_max(_mm_adds_epi16(y_scaled, b_dup), zero_v, max_v);
if ALPHA {
let alpha = _mm_set1_epi16(out_max);
write_rgba_u16_8(r, g, b, alpha, out.as_mut_ptr().add(x * 4));
} else {
write_rgb_u16_8(r, g, b, out.as_mut_ptr().add(x * 3));
}
x += 8;
}
}
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 = "sse4.1")]
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 y_idx = _mm_setr_epi8(0, 1, 4, 5, 8, 9, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1);
while x + 8 <= width {
let lo = _mm_loadu_si128(packed.as_ptr().add(x * 2).cast());
let hi = _mm_loadu_si128(packed.as_ptr().add(x * 2 + 8).cast());
let y_lo = _mm_shuffle_epi8(lo, y_idx); let y_hi = _mm_shuffle_epi8(hi, y_idx); let y_vec = _mm_unpacklo_epi64(y_lo, y_hi);
let y_shr = _mm_srli_epi16::<8>(y_vec);
let y_u8 = _mm_packus_epi16(y_shr, _mm_setzero_si128());
let mut tmp = [0u8; 16];
_mm_storeu_si128(tmp.as_mut_ptr().cast(), y_u8);
luma_out[x..x + 8].copy_from_slice(&tmp[..8]);
x += 8;
}
}
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 = "sse4.1")]
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 y_idx = _mm_setr_epi8(0, 1, 4, 5, 8, 9, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1);
while x + 8 <= width {
let lo = _mm_loadu_si128(packed.as_ptr().add(x * 2).cast());
let hi = _mm_loadu_si128(packed.as_ptr().add(x * 2 + 8).cast());
let y_lo = _mm_shuffle_epi8(lo, y_idx);
let y_hi = _mm_shuffle_epi8(hi, y_idx);
let y_vec = _mm_unpacklo_epi64(y_lo, y_hi);
let y_low = _mm_srl_epi16(y_vec, shr_count);
_mm_storeu_si128(luma_out.as_mut_ptr().add(x).cast(), y_low);
x += 8;
}
}
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);
}
}
}