use core::arch::aarch64::{
vabsq_s16, vaddl_s16, vaddq_s16, vaddq_s32, vaddw_s16, vcombine_u16, vdupq_n_s16, vdupq_n_u16,
vdupq_n_u64, vget_high_s16, vget_low_s16, vgetq_lane_u64, vmaxq_s16, vminq_s16, vminq_u16,
vmovl_s16, vpadalq_u32, vpaddlq_u16, vqmovun_s32, vreinterpretq_s16_u16, vreinterpretq_u16_s16,
vrhaddq_s16, vrhaddq_s32, vshrq_n_s16, vshrq_n_s32, vsubq_s16, vzipq_s16, vzipq_s32,
};
use crate::dsp::neon::{vld1_s16, vld1q_s16, vld1q_u16, vst1q_s16, vst1q_u16};
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "the C conversions to uint16_t of a value in range"
)]
#[must_use]
fn clip_neon(v: i32, max: i32) -> u16 {
if v < 0 {
0
} else if v > max {
max as u16
} else {
v as u16
}
}
#[must_use]
fn length(len: i32) -> usize {
usize::try_from(len).unwrap_or(0)
}
#[expect(
clippy::cast_possible_truncation,
reason = "the C conversion of max_y to the int16_t lane"
)]
#[must_use]
fn lane(max_y: i32) -> i16 {
max_y as i16
}
#[target_feature(enable = "neon")]
#[expect(
clippy::many_single_char_names,
reason = "the short C names of the source"
)]
fn sharp_yuv_update_y_neon(
r#ref: &[u16],
src: &[u16],
dst: &mut [u16],
len: i32,
bit_depth: i32,
) -> u64 {
let max_y = (1 << bit_depth) - 1;
let zero = vdupq_n_s16(0);
let max = vdupq_n_s16(lane(max_y));
let mut sum = vdupq_n_u64(0);
let len = length(len);
let mut i = 0;
while i + 8 <= len {
let a = vreinterpretq_s16_u16(vld1q_u16(&r#ref[i..]));
let b = vreinterpretq_s16_u16(vld1q_u16(&src[i..]));
let c = vreinterpretq_s16_u16(vld1q_u16(&dst[i..]));
let d = vsubq_s16(a, b); let f = vaddq_s16(c, d); let h = vreinterpretq_u16_s16(vmaxq_s16(vminq_s16(f, max), zero));
let i_ = vabsq_s16(d); vst1q_u16(&mut dst[i..], h);
sum = vpadalq_u32(sum, vpaddlq_u16(vreinterpretq_u16_s16(i_)));
i += 8;
}
let mut diff = vgetq_lane_u64::<0>(sum).wrapping_add(vgetq_lane_u64::<1>(sum));
while i < len {
let diff_y = i32::from(r#ref[i]) - i32::from(src[i]);
let new_y = i32::from(dst[i]) + diff_y;
dst[i] = clip_neon(new_y, max_y);
diff = diff.wrapping_add(u64::from(diff_y.unsigned_abs()));
i += 1;
}
diff
}
pub(crate) fn sharp_yuv_update_y(
r#ref: &[u16],
src: &[u16],
dst: &mut [u16],
len: i32,
bit_depth: i32,
) -> u64 {
unsafe { sharp_yuv_update_y_neon(r#ref, src, dst, len, bit_depth) }
}
#[target_feature(enable = "neon")]
#[expect(
clippy::cast_possible_truncation,
reason = "the C assignment of an int to the int16_t element"
)]
#[expect(
clippy::many_single_char_names,
reason = "the short C names of the source"
)]
fn sharp_yuv_update_rgb_neon(r#ref: &[i16], src: &[i16], dst: &mut [i16], len: i32) {
let len = length(len);
let mut i = 0;
while i + 8 <= len {
let a = vld1q_s16(&r#ref[i..]);
let b = vld1q_s16(&src[i..]);
let c = vld1q_s16(&dst[i..]);
let d = vsubq_s16(a, b); let e = vaddq_s16(c, d); vst1q_s16(&mut dst[i..], e);
i += 8;
}
while i < len {
let diff_uv = i32::from(r#ref[i]) - i32::from(src[i]);
dst[i] = (i32::from(dst[i]) + diff_uv) as i16;
i += 1;
}
}
pub(crate) fn sharp_yuv_update_rgb(r#ref: &[i16], src: &[i16], dst: &mut [i16], len: i32) {
unsafe { sharp_yuv_update_rgb_neon(r#ref, src, dst, len) }
}
fn filter_row_tail(a: &[i16], b: &[i16], i: usize, best_y: &[u16], out: &mut [u16], max_y: i32) {
let a0b1 = i32::from(a[i]) + i32::from(b[i + 1]);
let a1b0 = i32::from(a[i + 1]) + i32::from(b[i]);
let a0a1b0b1 = a0b1 + a1b0 + 8;
let v0 = (8 * i32::from(a[i]) + 2 * a1b0 + a0a1b0b1) >> 4;
let v1 = (8 * i32::from(a[i + 1]) + 2 * a0b1 + a0a1b0b1) >> 4;
out[2 * i] = clip_neon(i32::from(best_y[2 * i]) + v0, max_y);
out[2 * i + 1] = clip_neon(i32::from(best_y[2 * i + 1]) + v1, max_y);
}
#[target_feature(enable = "neon")]
fn sharp_yuv_filter_row16_neon(
a: &[i16],
b: &[i16],
len: i32,
best_y: &[u16],
out: &mut [u16],
bit_depth: i32,
) {
let max_y = (1 << bit_depth) - 1;
let max = vdupq_n_s16(lane(max_y));
let zero = vdupq_n_s16(0);
let len = length(len);
let mut i = 0;
while i + 8 <= len {
let a0 = vld1q_s16(&a[i..]);
let a1 = vld1q_s16(&a[i + 1..]);
let b0 = vld1q_s16(&b[i..]);
let b1 = vld1q_s16(&b[i + 1..]);
let a0b1 = vaddq_s16(a0, b1);
let a1b0 = vaddq_s16(a1, b0);
let a0a1b0b1 = vaddq_s16(a0b1, a1b0); let a0b1_2 = vaddq_s16(a0b1, a0b1); let a1b0_2 = vaddq_s16(a1b0, a1b0); let c0 = vshrq_n_s16::<3>(vaddq_s16(a0b1_2, a0a1b0b1));
let c1 = vshrq_n_s16::<3>(vaddq_s16(a1b0_2, a0a1b0b1));
let e0 = vrhaddq_s16(c1, a0);
let e1 = vrhaddq_s16(c0, a1);
let f = vzipq_s16(e0, e1);
let g0 = vreinterpretq_s16_u16(vld1q_u16(&best_y[2 * i..]));
let g1 = vreinterpretq_s16_u16(vld1q_u16(&best_y[2 * i + 8..]));
let h0 = vaddq_s16(g0, f.0);
let h1 = vaddq_s16(g1, f.1);
let i0 = vmaxq_s16(vminq_s16(h0, max), zero);
let i1 = vmaxq_s16(vminq_s16(h1, max), zero);
vst1q_u16(&mut out[2 * i..], vreinterpretq_u16_s16(i0));
vst1q_u16(&mut out[2 * i + 8..], vreinterpretq_u16_s16(i1));
i += 8;
}
while i < len {
filter_row_tail(a, b, i, best_y, out, max_y);
i += 1;
}
}
#[target_feature(enable = "neon")]
#[expect(
clippy::cast_sign_loss,
clippy::cast_possible_truncation,
reason = "the C conversion of max_y to the uint16_t lane"
)]
#[expect(
clippy::many_single_char_names,
reason = "the short C names of the source"
)]
fn sharp_yuv_filter_row32_neon(
a: &[i16],
b: &[i16],
len: i32,
best_y: &[u16],
out: &mut [u16],
bit_depth: i32,
) {
let max_y = (1 << bit_depth) - 1;
let max = vdupq_n_u16(max_y as u16);
let len = length(len);
let mut i = 0;
while i + 4 <= len {
let a0 = vld1_s16(&a[i..]);
let a1 = vld1_s16(&a[i + 1..]);
let b0 = vld1_s16(&b[i..]);
let b1 = vld1_s16(&b[i + 1..]);
let a0b1 = vaddl_s16(a0, b1);
let a1b0 = vaddl_s16(a1, b0);
let a0a1b0b1 = vaddq_s32(a0b1, a1b0); let a0b1_2 = vaddq_s32(a0b1, a0b1); let a1b0_2 = vaddq_s32(a1b0, a1b0); let c0 = vshrq_n_s32::<3>(vaddq_s32(a0b1_2, a0a1b0b1));
let c1 = vshrq_n_s32::<3>(vaddq_s32(a1b0_2, a0a1b0b1));
let e0 = vrhaddq_s32(c1, vmovl_s16(a0));
let e1 = vrhaddq_s32(c0, vmovl_s16(a1));
let f = vzipq_s32(e0, e1);
let g = vreinterpretq_s16_u16(vld1q_u16(&best_y[2 * i..]));
let h0 = vaddw_s16(f.0, vget_low_s16(g));
let h1 = vaddw_s16(f.1, vget_high_s16(g));
let i_16 = vcombine_u16(vqmovun_s32(h0), vqmovun_s32(h1));
let i_clamped = vminq_u16(i_16, max);
vst1q_u16(&mut out[2 * i..], i_clamped);
i += 4;
}
while i < len {
filter_row_tail(a, b, i, best_y, out, max_y);
i += 1;
}
}
#[target_feature(enable = "neon")]
fn sharp_yuv_filter_row_neon(
a: &[i16],
b: &[i16],
len: i32,
best_y: &[u16],
out: &mut [u16],
bit_depth: i32,
) {
if bit_depth <= 10 {
sharp_yuv_filter_row16_neon(a, b, len, best_y, out, bit_depth);
} else {
sharp_yuv_filter_row32_neon(a, b, len, best_y, out, bit_depth);
}
}
pub(crate) fn sharp_yuv_filter_row(
a: &[i16],
b: &[i16],
len: i32,
best_y: &[u16],
out: &mut [u16],
bit_depth: i32,
) {
unsafe { sharp_yuv_filter_row_neon(a, b, len, best_y, out, bit_depth) }
}