use core::arch::wasm32::*;
use super::*;
use crate::{ColorMatrix, row::scalar};
const HOST_NATIVE_BE: bool = cfg!(target_endian = "big");
#[inline]
#[target_feature(enable = "simd128")]
unsafe fn unpack_y2xx_8px_wasm(ptr: *const u16, shr_count: u32) -> (v128, v128, v128) {
unsafe {
let lo = v128_load(ptr.cast()); let hi = v128_load(ptr.add(8).cast());
let y_idx = i8x16(0, 1, 4, 5, 8, 9, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1);
let y_lo = u8x16_swizzle(lo, y_idx); let y_hi = u8x16_swizzle(hi, y_idx); let y_vec_raw =
i8x16_shuffle::<0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23>(y_lo, y_hi);
let c_idx = i8x16(2, 3, 6, 7, 10, 11, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1);
let c_lo = u8x16_swizzle(lo, c_idx); let c_hi = u8x16_swizzle(hi, c_idx); let chroma_raw =
i8x16_shuffle::<0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23>(c_lo, c_hi);
let y_vec = u16x8_shr(y_vec_raw, shr_count);
let chroma = u16x8_shr(chroma_raw, shr_count);
let u_idx = i8x16(0, 1, 4, 5, 8, 9, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1);
let v_idx = i8x16(2, 3, 6, 7, 10, 11, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1);
let u_vec = u8x16_swizzle(chroma, u_idx);
let v_vec = u8x16_swizzle(chroma, v_idx);
(y_vec, u_vec, v_vec)
}
}
#[inline]
#[target_feature(enable = "simd128")]
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 = i32x4_splat(RND);
let y_off_v = i16x8_splat(y_off as i16);
let y_scale_v = i32x4_splat(y_scale);
let c_scale_v = i32x4_splat(c_scale);
let bias_v = i16x8_splat(bias as i16);
let shr_count: u32 = 16 - BITS;
let cru = i32x4_splat(coeffs.r_u());
let crv = i32x4_splat(coeffs.r_v());
let cgu = i32x4_splat(coeffs.g_u());
let cgv = i32x4_splat(coeffs.g_v());
let cbu = i32x4_splat(coeffs.b_u());
let cbv = i32x4_splat(coeffs.b_v());
while x + 8 <= width {
let (y_vec, u_vec, v_vec) = unpack_y2xx_8px_wasm(packed.as_ptr().add(x * 2), shr_count);
let y_i16 = y_vec;
let u_i16 = i16x8_sub(u_vec, bias_v);
let v_i16 = i16x8_sub(v_vec, bias_v);
let u_lo_i32 = i32x4_extend_low_i16x8(u_i16);
let u_hi_i32 = i32x4_extend_high_i16x8(u_i16);
let v_lo_i32 = i32x4_extend_low_i16x8(v_i16);
let v_hi_i32 = i32x4_extend_high_i16x8(v_i16);
let u_d_lo = q15_shift(i32x4_add(i32x4_mul(u_lo_i32, c_scale_v), rnd_v));
let u_d_hi = q15_shift(i32x4_add(i32x4_mul(u_hi_i32, c_scale_v), rnd_v));
let v_d_lo = q15_shift(i32x4_add(i32x4_mul(v_lo_i32, c_scale_v), rnd_v));
let v_d_hi = q15_shift(i32x4_add(i32x4_mul(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 = dup_lo(r_chroma);
let g_dup = dup_lo(g_chroma);
let b_dup = dup_lo(b_chroma);
let y_scaled = scale_y(y_i16, y_off_v, y_scale_v, rnd_v);
let r_sum = i16x8_add_sat(y_scaled, r_dup);
let g_sum = i16x8_add_sat(y_scaled, g_dup);
let b_sum = i16x8_add_sat(y_scaled, b_dup);
let r_u8 = u8x16_narrow_i16x8(r_sum, r_sum);
let g_u8 = u8x16_narrow_i16x8(g_sum, g_sum);
let b_u8 = u8x16_narrow_i16x8(b_sum, b_sum);
let mut r_tmp = [0u8; 16];
let mut g_tmp = [0u8; 16];
let mut b_tmp = [0u8; 16];
v128_store(r_tmp.as_mut_ptr().cast(), r_u8);
v128_store(g_tmp.as_mut_ptr().cast(), g_u8);
v128_store(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 = "simd128")]
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 = i32x4_splat(RND);
let y_off_v = i16x8_splat(y_off as i16);
let y_scale_v = i32x4_splat(y_scale);
let c_scale_v = i32x4_splat(c_scale);
let bias_v = i16x8_splat(bias as i16);
let shr_count: u32 = 16 - BITS;
let max_v = i16x8_splat(out_max);
let zero_v = i16x8_splat(0);
let cru = i32x4_splat(coeffs.r_u());
let crv = i32x4_splat(coeffs.r_v());
let cgu = i32x4_splat(coeffs.g_u());
let cgv = i32x4_splat(coeffs.g_v());
let cbu = i32x4_splat(coeffs.b_u());
let cbv = i32x4_splat(coeffs.b_v());
while x + 8 <= width {
let (y_vec, u_vec, v_vec) = unpack_y2xx_8px_wasm(packed.as_ptr().add(x * 2), shr_count);
let y_i16 = y_vec;
let u_i16 = i16x8_sub(u_vec, bias_v);
let v_i16 = i16x8_sub(v_vec, bias_v);
let u_lo_i32 = i32x4_extend_low_i16x8(u_i16);
let u_hi_i32 = i32x4_extend_high_i16x8(u_i16);
let v_lo_i32 = i32x4_extend_low_i16x8(v_i16);
let v_hi_i32 = i32x4_extend_high_i16x8(v_i16);
let u_d_lo = q15_shift(i32x4_add(i32x4_mul(u_lo_i32, c_scale_v), rnd_v));
let u_d_hi = q15_shift(i32x4_add(i32x4_mul(u_hi_i32, c_scale_v), rnd_v));
let v_d_lo = q15_shift(i32x4_add(i32x4_mul(v_lo_i32, c_scale_v), rnd_v));
let v_d_hi = q15_shift(i32x4_add(i32x4_mul(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 = dup_lo(r_chroma);
let g_dup = dup_lo(g_chroma);
let b_dup = dup_lo(b_chroma);
let y_scaled = scale_y(y_i16, y_off_v, y_scale_v, rnd_v);
let r = clamp_u16_max_wasm(i16x8_add_sat(y_scaled, r_dup), zero_v, max_v);
let g = clamp_u16_max_wasm(i16x8_add_sat(y_scaled, g_dup), zero_v, max_v);
let b = clamp_u16_max_wasm(i16x8_add_sat(y_scaled, b_dup), zero_v, max_v);
if ALPHA {
let alpha = i16x8_splat(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 = "simd128")]
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 = i8x16(0, 1, 4, 5, 8, 9, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1);
while x + 8 <= width {
let lo = v128_load(packed.as_ptr().add(x * 2).cast());
let hi = v128_load(packed.as_ptr().add(x * 2 + 8).cast());
let y_lo = u8x16_swizzle(lo, y_idx); let y_hi = u8x16_swizzle(hi, y_idx); let y_vec =
i8x16_shuffle::<0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23>(y_lo, y_hi);
let y_shr = u16x8_shr(y_vec, 8);
let y_u8 = u8x16_narrow_i16x8(y_shr, y_shr);
let mut tmp = [0u8; 16];
v128_store(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 = "simd128")]
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: u32 = 16 - BITS;
let y_idx = i8x16(0, 1, 4, 5, 8, 9, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1);
while x + 8 <= width {
let lo = v128_load(packed.as_ptr().add(x * 2).cast());
let hi = v128_load(packed.as_ptr().add(x * 2 + 8).cast());
let y_lo = u8x16_swizzle(lo, y_idx);
let y_hi = u8x16_swizzle(hi, y_idx);
let y_vec =
i8x16_shuffle::<0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23>(y_lo, y_hi);
let y_low = u16x8_shr(y_vec, shr_count);
v128_store(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);
}
}
}