#![allow(dead_code)]
use core::arch::wasm32::*;
use super::*;
const HOST_NATIVE_BE: bool = cfg!(target_endian = "big");
#[inline(always)]
unsafe fn byteswap32_if_be<const BE: bool>(v: v128) -> v128 {
if BE != HOST_NATIVE_BE {
u8x16_swizzle(
v,
i8x16(3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12),
)
} else {
v
}
}
#[inline(always)]
unsafe fn deinterleave_rgb96_4px(v0: v128, v1: v128, v2: v128) -> (v128, v128, v128) {
let r_v0 = i8x16(0, 1, 2, 3, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1);
let r_v1 = i8x16(-1, -1, -1, -1, -1, -1, -1, -1, 8, 9, 10, 11, -1, -1, -1, -1);
let r_v2 = i8x16(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 4, 5, 6, 7);
let r = v128_or(
v128_or(u8x16_swizzle(v0, r_v0), u8x16_swizzle(v1, r_v1)),
u8x16_swizzle(v2, r_v2),
);
let g_v0 = i8x16(4, 5, 6, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let g_v1 = i8x16(-1, -1, -1, -1, 0, 1, 2, 3, 12, 13, 14, 15, -1, -1, -1, -1);
let g_v2 = i8x16(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 8, 9, 10, 11);
let g = v128_or(
v128_or(u8x16_swizzle(v0, g_v0), u8x16_swizzle(v1, g_v1)),
u8x16_swizzle(v2, g_v2),
);
let b_v0 = i8x16(8, 9, 10, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let b_v1 = i8x16(-1, -1, -1, -1, 4, 5, 6, 7, -1, -1, -1, -1, -1, -1, -1, -1);
let b_v2 = i8x16(-1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 12, 13, 14, 15);
let b = v128_or(
v128_or(u8x16_swizzle(v0, b_v0), u8x16_swizzle(v1, b_v1)),
u8x16_swizzle(v2, b_v2),
);
(r, g, b)
}
#[inline(always)]
unsafe fn load_deint_rgb96_4px<const BE: bool>(ptr: *const u32) -> (v128, v128, v128) {
unsafe {
let v0 = byteswap32_if_be::<BE>(v128_load(ptr.cast()));
let v1 = byteswap32_if_be::<BE>(v128_load(ptr.add(4).cast()));
let v2 = byteswap32_if_be::<BE>(v128_load(ptr.add(8).cast()));
deinterleave_rgb96_4px(v0, v1, v2)
}
}
#[inline(always)]
unsafe fn narrow_pair_u32_to_u16x8(lo: v128, hi: v128) -> v128 {
u16x8_narrow_i32x4(u32x4_shr(lo, 16), u32x4_shr(hi, 16))
}
#[inline(always)]
unsafe fn narrow_pair_u32_to_u8x8(lo: v128, hi: v128) -> v128 {
let u16 = u16x8_narrow_i32x4(u32x4_shr(lo, 24), u32x4_shr(hi, 24));
u8x16_narrow_i16x8(u16, u16x8_splat(0))
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn wasm_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 + 8 <= width {
let p = rgb96.as_ptr().add(x * 3);
let (r0, g0, b0) = load_deint_rgb96_4px::<BE>(p);
let (r1, g1, b1) = load_deint_rgb96_4px::<BE>(p.add(12));
let r = narrow_pair_u32_to_u8x8(r0, r1);
let g = narrow_pair_u32_to_u8x8(g0, g1);
let b = narrow_pair_u32_to_u8x8(b0, b1);
let mut tmp = [0u8; 48];
write_rgb_16(r, g, b, tmp.as_mut_ptr());
core::ptr::copy_nonoverlapping(tmp.as_ptr(), rgb_out.as_mut_ptr().add(x * 3), 24);
x += 8;
}
if x < width {
scalar::rgb96_to_rgb_row::<BE>(&rgb96[x * 3..], &mut rgb_out[x * 3..], width - x);
}
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn wasm_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 opaque = u8x16_splat(0xFF);
let mut x = 0usize;
while x + 8 <= width {
let p = rgb96.as_ptr().add(x * 3);
let (r0, g0, b0) = load_deint_rgb96_4px::<BE>(p);
let (r1, g1, b1) = load_deint_rgb96_4px::<BE>(p.add(12));
let r = narrow_pair_u32_to_u8x8(r0, r1);
let g = narrow_pair_u32_to_u8x8(g0, g1);
let b = narrow_pair_u32_to_u8x8(b0, b1);
let mut tmp = [0u8; 64];
write_rgba_16(r, g, b, opaque, tmp.as_mut_ptr());
core::ptr::copy_nonoverlapping(tmp.as_ptr(), rgba_out.as_mut_ptr().add(x * 4), 32);
x += 8;
}
if x < width {
scalar::rgb96_to_rgba_row::<BE>(&rgb96[x * 3..], &mut rgba_out[x * 4..], width - x);
}
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn wasm_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 + 8 <= width {
let p = rgb96.as_ptr().add(x * 3);
let (r0, g0, b0) = load_deint_rgb96_4px::<BE>(p);
let (r1, g1, b1) = load_deint_rgb96_4px::<BE>(p.add(12));
let r = narrow_pair_u32_to_u16x8(r0, r1);
let g = narrow_pair_u32_to_u16x8(g0, g1);
let b = narrow_pair_u32_to_u16x8(b0, b1);
write_rgb_u16_8(r, g, b, 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);
}
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn wasm_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 opaque = u16x8_splat(0xFFFF);
let mut x = 0usize;
while x + 8 <= width {
let p = rgb96.as_ptr().add(x * 3);
let (r0, g0, b0) = load_deint_rgb96_4px::<BE>(p);
let (r1, g1, b1) = load_deint_rgb96_4px::<BE>(p.add(12));
let r = narrow_pair_u32_to_u16x8(r0, r1);
let g = narrow_pair_u32_to_u16x8(g0, g1);
let b = narrow_pair_u32_to_u16x8(b0, b1);
write_rgba_u16_8(r, g, b, opaque, 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);
}
}
}
#[inline(always)]
unsafe fn deinterleave_rgba128_4px(
v0: v128,
v1: v128,
v2: v128,
v3: v128,
) -> (v128, v128, v128, v128) {
let t0 = i32x4_shuffle::<0, 4, 1, 5>(v0, v1); let t1 = i32x4_shuffle::<2, 6, 3, 7>(v0, v1); let t2 = i32x4_shuffle::<0, 4, 1, 5>(v2, v3); let t3 = i32x4_shuffle::<2, 6, 3, 7>(v2, v3); let r = i32x4_shuffle::<0, 1, 4, 5>(t0, t2); let g = i32x4_shuffle::<2, 3, 6, 7>(t0, t2); let b = i32x4_shuffle::<0, 1, 4, 5>(t1, t3); let a = i32x4_shuffle::<2, 3, 6, 7>(t1, t3); (r, g, b, a)
}
#[inline(always)]
unsafe fn load_deint_rgba128_4px<const BE: bool>(ptr: *const u32) -> (v128, v128, v128, v128) {
unsafe {
let v0 = byteswap32_if_be::<BE>(v128_load(ptr.cast()));
let v1 = byteswap32_if_be::<BE>(v128_load(ptr.add(4).cast()));
let v2 = byteswap32_if_be::<BE>(v128_load(ptr.add(8).cast()));
let v3 = byteswap32_if_be::<BE>(v128_load(ptr.add(12).cast()));
deinterleave_rgba128_4px(v0, v1, v2, v3)
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn wasm_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 + 8 <= width {
let p = rgba128.as_ptr().add(x * 4);
let (r0, g0, b0, _) = load_deint_rgba128_4px::<BE>(p);
let (r1, g1, b1, _) = load_deint_rgba128_4px::<BE>(p.add(16));
let r = narrow_pair_u32_to_u8x8(r0, r1);
let g = narrow_pair_u32_to_u8x8(g0, g1);
let b = narrow_pair_u32_to_u8x8(b0, b1);
let mut tmp = [0u8; 48];
write_rgb_16(r, g, b, tmp.as_mut_ptr());
core::ptr::copy_nonoverlapping(tmp.as_ptr(), rgb_out.as_mut_ptr().add(x * 3), 24);
x += 8;
}
if x < width {
scalar::rgba128_to_rgb_row::<BE>(&rgba128[x * 4..], &mut rgb_out[x * 3..], width - x);
}
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn wasm_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 + 8 <= width {
let p = rgba128.as_ptr().add(x * 4);
let (r0, g0, b0, a0) = load_deint_rgba128_4px::<BE>(p);
let (r1, g1, b1, a1) = load_deint_rgba128_4px::<BE>(p.add(16));
let r = narrow_pair_u32_to_u8x8(r0, r1);
let g = narrow_pair_u32_to_u8x8(g0, g1);
let b = narrow_pair_u32_to_u8x8(b0, b1);
let a = narrow_pair_u32_to_u8x8(a0, a1);
let mut tmp = [0u8; 64];
write_rgba_16(r, g, b, a, tmp.as_mut_ptr());
core::ptr::copy_nonoverlapping(tmp.as_ptr(), rgba_out.as_mut_ptr().add(x * 4), 32);
x += 8;
}
if x < width {
scalar::rgba128_to_rgba_row::<BE>(&rgba128[x * 4..], &mut rgba_out[x * 4..], width - x);
}
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn wasm_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 + 8 <= width {
let p = rgba128.as_ptr().add(x * 4);
let (r0, g0, b0, _) = load_deint_rgba128_4px::<BE>(p);
let (r1, g1, b1, _) = load_deint_rgba128_4px::<BE>(p.add(16));
let r = narrow_pair_u32_to_u16x8(r0, r1);
let g = narrow_pair_u32_to_u16x8(g0, g1);
let b = narrow_pair_u32_to_u16x8(b0, b1);
write_rgb_u16_8(r, g, b, 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);
}
}
}
#[inline]
#[target_feature(enable = "simd128")]
pub(crate) unsafe fn wasm_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 + 8 <= width {
let p = rgba128.as_ptr().add(x * 4);
let (r0, g0, b0, a0) = load_deint_rgba128_4px::<BE>(p);
let (r1, g1, b1, a1) = load_deint_rgba128_4px::<BE>(p.add(16));
let r = narrow_pair_u32_to_u16x8(r0, r1);
let g = narrow_pair_u32_to_u16x8(g0, g1);
let b = narrow_pair_u32_to_u16x8(b0, b1);
let a = narrow_pair_u32_to_u16x8(a0, a1);
write_rgba_u16_8(r, g, b, a, 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);
}
}
}