use core::arch::aarch64::{
int32x4_t, uint32x4_t, vcombine_u16, vcombine_u32, vdupq_n_s32, vdupq_n_u32, vget_high_u32,
vget_low_u32, vmlal_n_u32, vmovn_u32, vmull_n_u32, vqdmulhq_s32, vqmovn_u16, vqrdmulhq_s32,
vqsubq_u32, vreinterpretq_s32_u32, vreinterpretq_u32_s32, vrshrn_n_u64,
};
use crate::dsp::neon::{vld1q_u32, vst1_u8, vst1q_u32};
use crate::utils::rescaler_utils::{
WEBP_RESCALER_ONE, WEBP_RESCALER_RFIX, WebPRescaler, rescaler_t, webp_rescaler_frac,
webp_rescaler_output_done,
};
const ROUNDER: u64 = WEBP_RESCALER_ONE >> 1;
#[must_use]
fn mult_fix_c(x: u32, y: u32) -> u64 {
(u64::from(x) * u64::from(y) + ROUNDER) >> WEBP_RESCALER_RFIX
}
#[must_use]
fn mult_fix_floor_c(x: u32, y: u32) -> u64 {
(u64::from(x) * u64::from(y)) >> WEBP_RESCALER_RFIX
}
#[expect(
clippy::cast_possible_truncation,
reason = "the C conversion keeps the low 32 bits"
)]
#[must_use]
fn low32(value: u64) -> u32 {
value as u32
}
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "the C conversions to int and uint8_t keep the low bits"
)]
#[must_use]
fn clip_to_byte(value: u64) -> u8 {
let v = value as i32;
if v > 255 { 255 } else { v as u8 }
}
#[expect(
clippy::cast_sign_loss,
reason = "C converts a signed value to uint32_t by wrapping it"
)]
#[must_use]
fn as_u32(value: i32) -> u32 {
value as u32
}
#[must_use]
fn row_length(wrk: &WebPRescaler) -> usize {
usize::try_from(wrk.dst_width * wrk.num_channels).expect("the row length is positive")
}
#[inline]
#[must_use]
fn load_32x4(src: &[rescaler_t]) -> uint32x4_t {
vld1q_u32(src)
}
#[inline]
#[must_use]
fn load_32x8(src: &[rescaler_t]) -> (uint32x4_t, uint32x4_t) {
(load_32x4(&src[0..]), load_32x4(&src[4..]))
}
#[inline]
fn store_32x8(src0: uint32x4_t, src1: uint32x4_t, dst: &mut [rescaler_t]) {
vst1q_u32(&mut dst[0..], src0);
vst1q_u32(&mut dst[4..], src1);
}
#[target_feature(enable = "neon")]
#[must_use]
#[expect(
clippy::cast_possible_wrap,
reason = "the C conversion (int32_t) of the halved scale keeps its bits"
)]
fn make_half_cst(c: u32) -> int32x4_t {
vdupq_n_s32((c >> 1) as i32)
}
#[target_feature(enable = "neon")]
#[must_use]
fn mult_fix(a: uint32x4_t, b: int32x4_t) -> uint32x4_t {
vreinterpretq_u32_s32(vqrdmulhq_s32(vreinterpretq_s32_u32(a), b))
}
#[target_feature(enable = "neon")]
#[must_use]
fn mult_fix_floor(a: uint32x4_t, b: int32x4_t) -> uint32x4_t {
vreinterpretq_u32_s32(vqdmulhq_s32(vreinterpretq_s32_u32(a), b))
}
#[target_feature(enable = "neon")]
#[must_use]
fn interpolate_neon(frow: &[rescaler_t], irow: &[rescaler_t], a: u32, b: u32) -> uint32x4_t {
let a0 = load_32x4(frow);
let b0 = load_32x4(irow);
let c0 = vmull_n_u32(vget_low_u32(a0), a);
let c1 = vmull_n_u32(vget_high_u32(a0), a);
let d0 = vmlal_n_u32(c0, vget_low_u32(b0), b);
let d1 = vmlal_n_u32(c1, vget_high_u32(b0), b);
vcombine_u32(vrshrn_n_u64::<32>(d0), vrshrn_n_u64::<32>(d1))
}
#[target_feature(enable = "neon")]
fn rescaler_export_row_expand_neon(wrk: &mut WebPRescaler, dst: &mut [u8]) {
let dst = &mut dst[wrk.dst_pos..];
let irow = &*wrk.irow;
let x_out_max = row_length(wrk);
let max_span = x_out_max & !7;
let frow = &*wrk.frow;
let fy_scale = wrk.fy_scale;
let fy_scale_half = make_half_cst(fy_scale);
debug_assert!(!webp_rescaler_output_done(wrk));
debug_assert!(wrk.y_accum <= 0);
debug_assert!(wrk.y_expand);
debug_assert!(wrk.y_sub != 0);
let mut x_out = 0;
if wrk.y_accum == 0 {
while x_out < max_span {
let a0 = load_32x4(&frow[x_out..]);
let a1 = load_32x4(&frow[x_out + 4..]);
let b0 = mult_fix(a0, fy_scale_half);
let b1 = mult_fix(a1, fy_scale_half);
let c0 = vmovn_u32(b0);
let c1 = vmovn_u32(b1);
let d = vqmovn_u16(vcombine_u16(c0, c1));
vst1_u8(&mut dst[x_out..], d);
x_out += 8;
}
while x_out < x_out_max {
let j = frow[x_out];
dst[x_out] = clip_to_byte(mult_fix_c(j, fy_scale));
x_out += 1;
}
} else {
let b = webp_rescaler_frac(-wrk.y_accum, wrk.y_sub);
let a = low32(WEBP_RESCALER_ONE - u64::from(b));
while x_out < max_span {
let c0 = interpolate_neon(&frow[x_out..], &irow[x_out..], a, b);
let c1 = interpolate_neon(&frow[x_out + 4..], &irow[x_out + 4..], a, b);
let d0 = mult_fix(c0, fy_scale_half);
let d1 = mult_fix(c1, fy_scale_half);
let e0 = vmovn_u32(d0);
let e1 = vmovn_u32(d1);
let f = vqmovn_u16(vcombine_u16(e0, e1));
vst1_u8(&mut dst[x_out..], f);
x_out += 8;
}
while x_out < x_out_max {
let i = u64::from(a)
.wrapping_mul(u64::from(frow[x_out]))
.wrapping_add(u64::from(b).wrapping_mul(u64::from(irow[x_out])));
let j = low32(i.wrapping_add(ROUNDER) >> WEBP_RESCALER_RFIX);
dst[x_out] = clip_to_byte(mult_fix_c(j, fy_scale));
x_out += 1;
}
}
}
pub fn rescaler_export_row_expand(wrk: &mut WebPRescaler, dst: &mut [u8]) {
unsafe { rescaler_export_row_expand_neon(wrk, dst) }
}
#[target_feature(enable = "neon")]
fn rescaler_export_row_shrink_neon(wrk: &mut WebPRescaler, dst: &mut [u8]) {
let dst = &mut dst[wrk.dst_pos..];
let x_out_max = row_length(wrk);
let max_span = x_out_max & !7;
let yscale = wrk.fy_scale.wrapping_mul(as_u32(-wrk.y_accum));
let fxy_scale = wrk.fxy_scale;
let zero = vdupq_n_u32(0);
let yscale_half = make_half_cst(yscale);
let fxy_scale_half = make_half_cst(fxy_scale);
debug_assert!(!webp_rescaler_output_done(wrk));
debug_assert!(wrk.y_accum <= 0);
debug_assert!(!wrk.y_expand);
let irow = &mut *wrk.irow;
let frow = &*wrk.frow;
let mut x_out = 0;
if yscale != 0 {
while x_out < max_span {
let (in0, in1) = load_32x8(&frow[x_out..]);
let (in2, in3) = load_32x8(&irow[x_out..]);
let a0 = mult_fix_floor(in0, yscale_half);
let a1 = mult_fix_floor(in1, yscale_half);
let b0 = vqsubq_u32(in2, a0);
let b1 = vqsubq_u32(in3, a1);
let c0 = mult_fix(b0, fxy_scale_half);
let c1 = mult_fix(b1, fxy_scale_half);
let d0 = vmovn_u32(c0);
let d1 = vmovn_u32(c1);
let e = vqmovn_u16(vcombine_u16(d0, d1));
vst1_u8(&mut dst[x_out..], e);
store_32x8(a0, a1, &mut irow[x_out..]);
x_out += 8;
}
while x_out < x_out_max {
let frac = low32(mult_fix_floor_c(frow[x_out], yscale));
dst[x_out] = clip_to_byte(mult_fix_c(irow[x_out].wrapping_sub(frac), fxy_scale));
irow[x_out] = frac; x_out += 1;
}
} else {
while x_out < max_span {
let (in0, in1) = load_32x8(&irow[x_out..]);
let a0 = mult_fix(in0, fxy_scale_half);
let a1 = mult_fix(in1, fxy_scale_half);
let b0 = vmovn_u32(a0);
let b1 = vmovn_u32(a1);
let c = vqmovn_u16(vcombine_u16(b0, b1));
vst1_u8(&mut dst[x_out..], c);
store_32x8(zero, zero, &mut irow[x_out..]);
x_out += 8;
}
while x_out < x_out_max {
dst[x_out] = clip_to_byte(mult_fix_c(irow[x_out], fxy_scale));
irow[x_out] = 0;
x_out += 1;
}
}
}
pub fn rescaler_export_row_shrink(wrk: &mut WebPRescaler, dst: &mut [u8]) {
unsafe { rescaler_export_row_shrink_neon(wrk, dst) }
}