use core::arch::x86_64::*;
use super::*;
use crate::{ColorMatrix, row::scalar};
#[inline]
#[target_feature(enable = "avx2")]
unsafe fn deinterleave_vuya_avx2(ptr: *const u8) -> (__m256i, __m256i, __m256i, __m256i) {
unsafe {
let raw_c0 = _mm256_loadu_si256(ptr.cast());
let raw_c1 = _mm256_loadu_si256(ptr.add(32).cast());
let raw_c2 = _mm256_loadu_si256(ptr.add(64).cast());
let raw_c3 = _mm256_loadu_si256(ptr.add(96).cast());
let raw0 = _mm256_permute2x128_si256::<0x20>(raw_c0, raw_c2);
let raw1 = _mm256_permute2x128_si256::<0x31>(raw_c0, raw_c2);
let raw2 = _mm256_permute2x128_si256::<0x20>(raw_c1, raw_c3);
let raw3 = _mm256_permute2x128_si256::<0x31>(raw_c1, raw_c3);
let v_mask = _mm256_setr_epi8(
0, 4, 8, 12, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 4, 8, 12, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, );
let u_mask = _mm256_setr_epi8(
1, 5, 9, 13, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 5, 9, 13, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, );
let y_mask = _mm256_setr_epi8(
2, 6, 10, 14, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 6, 10, 14, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, );
let a_mask = _mm256_setr_epi8(
3, 7, 11, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 7, 11, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, );
let v0 = _mm256_shuffle_epi8(raw0, v_mask);
let v1 = _mm256_shuffle_epi8(raw1, v_mask);
let v2 = _mm256_shuffle_epi8(raw2, v_mask);
let v3 = _mm256_shuffle_epi8(raw3, v_mask);
let u0 = _mm256_shuffle_epi8(raw0, u_mask);
let u1 = _mm256_shuffle_epi8(raw1, u_mask);
let u2 = _mm256_shuffle_epi8(raw2, u_mask);
let u3 = _mm256_shuffle_epi8(raw3, u_mask);
let y0 = _mm256_shuffle_epi8(raw0, y_mask);
let y1 = _mm256_shuffle_epi8(raw1, y_mask);
let y2 = _mm256_shuffle_epi8(raw2, y_mask);
let y3 = _mm256_shuffle_epi8(raw3, y_mask);
let a0 = _mm256_shuffle_epi8(raw0, a_mask);
let a1 = _mm256_shuffle_epi8(raw1, a_mask);
let a2 = _mm256_shuffle_epi8(raw2, a_mask);
let a3 = _mm256_shuffle_epi8(raw3, a_mask);
let v_01 = _mm256_unpacklo_epi32(v0, v1);
let v_23 = _mm256_unpacklo_epi32(v2, v3);
let u_01 = _mm256_unpacklo_epi32(u0, u1);
let u_23 = _mm256_unpacklo_epi32(u2, u3);
let y_01 = _mm256_unpacklo_epi32(y0, y1);
let y_23 = _mm256_unpacklo_epi32(y2, y3);
let a_01 = _mm256_unpacklo_epi32(a0, a1);
let a_23 = _mm256_unpacklo_epi32(a2, a3);
let v_vec = _mm256_unpacklo_epi64(v_01, v_23);
let u_vec = _mm256_unpacklo_epi64(u_01, u_23);
let y_vec = _mm256_unpacklo_epi64(y_01, y_23);
let a_vec = _mm256_unpacklo_epi64(a_01, a_23);
(v_vec, u_vec, y_vec, a_vec)
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn vuya_to_rgb_or_rgba_row<const ALPHA: bool, const ALPHA_SRC: bool>(
packed: &[u8],
out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
const { assert!(!ALPHA_SRC || ALPHA) };
debug_assert!(packed.len() >= width * 4, "packed row too short");
let bpp: usize = if ALPHA { 4 } else { 3 };
debug_assert!(out.len() >= width * bpp, "out row too short");
let coeffs = scalar::Coefficients::for_matrix(matrix);
let (y_off, y_scale, c_scale) = scalar::range_params_n::<8, 8>(full_range);
let bias = scalar::chroma_bias::<8>();
const RND: i32 = 1 << 14;
unsafe {
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 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());
let alpha_u8 = _mm256_set1_epi8(-1i8);
let mut x = 0usize;
while x + 32 <= width {
let (v_u8, u_u8, y_u8, a_u8) = deinterleave_vuya_avx2(packed.as_ptr().add(x * 4));
let v_lo_i16 = _mm256_cvtepu8_epi16(_mm256_castsi256_si128(v_u8));
let v_hi_i16 = _mm256_cvtepu8_epi16(_mm256_extracti128_si256::<1>(v_u8));
let u_lo_i16 = _mm256_cvtepu8_epi16(_mm256_castsi256_si128(u_u8));
let u_hi_i16 = _mm256_cvtepu8_epi16(_mm256_extracti128_si256::<1>(u_u8));
let y_lo_i16 = _mm256_cvtepu8_epi16(_mm256_castsi256_si128(y_u8));
let y_hi_i16 = _mm256_cvtepu8_epi16(_mm256_extracti128_si256::<1>(y_u8));
let u_lo_sub = _mm256_sub_epi16(u_lo_i16, bias_v);
let u_hi_sub = _mm256_sub_epi16(u_hi_i16, bias_v);
let v_lo_sub = _mm256_sub_epi16(v_lo_i16, bias_v);
let v_hi_sub = _mm256_sub_epi16(v_hi_i16, bias_v);
let u_lo_a = _mm256_cvtepi16_epi32(_mm256_castsi256_si128(u_lo_sub));
let u_lo_b = _mm256_cvtepi16_epi32(_mm256_extracti128_si256::<1>(u_lo_sub));
let u_hi_a = _mm256_cvtepi16_epi32(_mm256_castsi256_si128(u_hi_sub));
let u_hi_b = _mm256_cvtepi16_epi32(_mm256_extracti128_si256::<1>(u_hi_sub));
let v_lo_a = _mm256_cvtepi16_epi32(_mm256_castsi256_si128(v_lo_sub));
let v_lo_b = _mm256_cvtepi16_epi32(_mm256_extracti128_si256::<1>(v_lo_sub));
let v_hi_a = _mm256_cvtepi16_epi32(_mm256_castsi256_si128(v_hi_sub));
let v_hi_b = _mm256_cvtepi16_epi32(_mm256_extracti128_si256::<1>(v_hi_sub));
let u_d_lo_a = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(u_lo_a, c_scale_v),
rnd_v,
));
let u_d_lo_b = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(u_lo_b, c_scale_v),
rnd_v,
));
let u_d_hi_a = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(u_hi_a, c_scale_v),
rnd_v,
));
let u_d_hi_b = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(u_hi_b, c_scale_v),
rnd_v,
));
let v_d_lo_a = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(v_lo_a, c_scale_v),
rnd_v,
));
let v_d_lo_b = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(v_lo_b, c_scale_v),
rnd_v,
));
let v_d_hi_a = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(v_hi_a, c_scale_v),
rnd_v,
));
let v_d_hi_b = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(v_hi_b, c_scale_v),
rnd_v,
));
let r_chroma_lo = chroma_i16x16(cru, crv, u_d_lo_a, v_d_lo_a, u_d_lo_b, v_d_lo_b, rnd_v);
let r_chroma_hi = chroma_i16x16(cru, crv, u_d_hi_a, v_d_hi_a, u_d_hi_b, v_d_hi_b, rnd_v);
let g_chroma_lo = chroma_i16x16(cgu, cgv, u_d_lo_a, v_d_lo_a, u_d_lo_b, v_d_lo_b, rnd_v);
let g_chroma_hi = chroma_i16x16(cgu, cgv, u_d_hi_a, v_d_hi_a, u_d_hi_b, v_d_hi_b, rnd_v);
let b_chroma_lo = chroma_i16x16(cbu, cbv, u_d_lo_a, v_d_lo_a, u_d_lo_b, v_d_lo_b, rnd_v);
let b_chroma_hi = chroma_i16x16(cbu, cbv, u_d_hi_a, v_d_hi_a, u_d_hi_b, v_d_hi_b, rnd_v);
let y_scaled_lo = scale_y(y_lo_i16, y_off_v, y_scale_v, rnd_v);
let y_scaled_hi = scale_y(y_hi_i16, y_off_v, y_scale_v, rnd_v);
let r_lo = _mm256_adds_epi16(y_scaled_lo, r_chroma_lo);
let r_hi = _mm256_adds_epi16(y_scaled_hi, r_chroma_hi);
let g_lo = _mm256_adds_epi16(y_scaled_lo, g_chroma_lo);
let g_hi = _mm256_adds_epi16(y_scaled_hi, g_chroma_hi);
let b_lo = _mm256_adds_epi16(y_scaled_lo, b_chroma_lo);
let b_hi = _mm256_adds_epi16(y_scaled_hi, b_chroma_hi);
let r_u8 = narrow_u8x32(r_lo, r_hi);
let g_u8 = narrow_u8x32(g_lo, g_hi);
let b_u8 = narrow_u8x32(b_lo, b_hi);
let out_ptr = out.as_mut_ptr().add(x * bpp);
if ALPHA {
let a_vec = if ALPHA_SRC { a_u8 } else { alpha_u8 };
write_rgba_32(r_u8, g_u8, b_u8, a_vec, out_ptr);
} else {
write_rgb_32(r_u8, g_u8, b_u8, out_ptr);
}
x += 32;
}
if x < width {
scalar::vuya_to_rgb_or_rgba_row::<ALPHA, ALPHA_SRC>(
&packed[x * 4..],
&mut out[x * bpp..],
width - x,
matrix,
full_range,
);
}
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn vuya_to_rgb_row(
packed: &[u8],
rgb_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
vuya_to_rgb_or_rgba_row::<false, false>(packed, rgb_out, width, matrix, full_range);
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn vuya_to_rgba_row(
packed: &[u8],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
vuya_to_rgb_or_rgba_row::<true, true>(packed, rgba_out, width, matrix, full_range);
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn vuyx_to_rgba_row(
packed: &[u8],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
vuya_to_rgb_or_rgba_row::<true, false>(packed, rgba_out, width, matrix, full_range);
}
}
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn vuya_to_luma_row(packed: &[u8], luma_out: &mut [u8], width: usize) {
debug_assert!(packed.len() >= width * 4, "packed row too short");
debug_assert!(luma_out.len() >= width, "luma row too short");
unsafe {
let mut x = 0usize;
while x + 32 <= width {
let (_v, _u, y_vec, _a) = deinterleave_vuya_avx2(packed.as_ptr().add(x * 4));
_mm256_storeu_si256(luma_out.as_mut_ptr().add(x).cast(), y_vec);
x += 32;
}
if x < width {
scalar::vuya_to_luma_row(&packed[x * 4..], &mut luma_out[x..], width - x);
}
}
}
#[cfg_attr(not(any(feature = "std", feature = "alloc")), allow(dead_code))]
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn vuya_to_luma_u16_row(packed: &[u8], out: &mut [u16], width: usize) {
debug_assert!(packed.len() >= width * 4, "packed row too short");
debug_assert!(out.len() >= width, "out too short");
unsafe {
let mut x = 0usize;
while x + 32 <= width {
let (_v, _u, y_u8, _a) = deinterleave_vuya_avx2(packed.as_ptr().add(x * 4));
let lo_u16 = _mm256_cvtepu8_epi16(_mm256_castsi256_si128(y_u8));
let hi_u16 = _mm256_cvtepu8_epi16(_mm256_extracti128_si256::<1>(y_u8));
_mm256_storeu_si256(out.as_mut_ptr().add(x).cast(), lo_u16);
_mm256_storeu_si256(out.as_mut_ptr().add(x + 16).cast(), hi_u16);
x += 32;
}
if x < width {
scalar::vuya_to_luma_u16_row(&packed[x * 4..], &mut out[x..], width - x);
}
}
}
#[allow(dead_code)]
#[inline]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn vuyx_to_luma_u16_row(packed: &[u8], out: &mut [u16], width: usize) {
unsafe {
vuya_to_luma_u16_row(packed, out, width);
}
}