#[cfg(target_arch = "x86")]
use core::arch::x86 as arch;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64 as arch;
use arch::{
_mm_add_epi8, _mm_add_epi16, _mm_and_si128, _mm_or_si128, _mm_packus_epi16, _mm_set_epi32,
_mm_setzero_si128, _mm_slli_si128, _mm_srli_epi64, _mm_srli_si128, _mm_sub_epi8, _mm_sub_epi16,
_mm_unpacklo_epi8,
};
use crate::dsp::common_sse2::{loadl_epi64, loadu_si128, storel_epi64, storeu_si128};
fn idx(i: i32) -> usize {
usize::try_from(i).expect("a non-negative index")
}
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "a C conversion to uint8_t keeps the low eight bits"
)]
fn low_byte(value: i32) -> u8 {
value as u8
}
fn dcheck(width: i32, height: i32, stride: i32) {
debug_assert!(width > 0);
debug_assert!(height > 0);
debug_assert!(stride >= width);
}
#[target_feature(enable = "sse2")]
fn predict_line_top_sse2(src: &[u8], pred: &[u8], dst: &mut [u8], length: i32) {
let max_pos = length & !31;
debug_assert!(length >= 0);
let mut i = 0;
while i < max_pos {
let k = idx(i);
let a0 = loadu_si128(&src[k..k + 16]);
let a1 = loadu_si128(&src[k + 16..k + 32]);
let b0 = loadu_si128(&pred[k..k + 16]);
let b1 = loadu_si128(&pred[k + 16..k + 32]);
let c0 = _mm_sub_epi8(a0, b0);
let c1 = _mm_sub_epi8(a1, b1);
storeu_si128(&mut dst[k..k + 16], c0);
storeu_si128(&mut dst[k + 16..k + 32], c1);
i += 32;
}
while i < length {
let k = idx(i);
dst[k] = src[k].wrapping_sub(pred[k]);
i += 1;
}
}
#[target_feature(enable = "sse2")]
fn predict_line_left_sse2(src: &[u8], src_pos: usize, dst: &mut [u8], length: i32) {
debug_assert!(length >= 0);
let length = usize::try_from(length).unwrap_or(0);
let src = &src[src_pos - 1..][..=length];
let dst = &mut dst[..length];
let max_pos = length & !31;
let mut i = 0;
while i < max_pos {
let k = 1 + i;
let a0 = loadu_si128(&src[k..k + 16]);
let b0 = loadu_si128(&src[k - 1..k + 15]);
let a1 = loadu_si128(&src[k + 16..k + 32]);
let b1 = loadu_si128(&src[k + 15..k + 31]);
let c0 = _mm_sub_epi8(a0, b0);
let c1 = _mm_sub_epi8(a1, b1);
storeu_si128(&mut dst[i..i + 16], c0);
storeu_si128(&mut dst[i + 16..i + 32], c1);
i += 32;
}
while i < length {
let k = 1 + i;
dst[i] = src[k].wrapping_sub(src[k - 1]);
i += 1;
}
}
#[target_feature(enable = "sse2")]
#[inline]
fn do_horizontal_filter_sse2(input: &[u8], width: i32, height: i32, stride: i32, out: &mut [u8]) {
dcheck(width, height, stride);
let stride = idx(stride);
out[0] = input[0];
predict_line_left_sse2(input, 1, &mut out[1..], width - 1);
for row in 1..idx(height) {
let cur = row * stride;
out[cur] = input[cur].wrapping_sub(input[cur - stride]);
predict_line_left_sse2(input, cur + 1, &mut out[cur + 1..], width - 1);
}
}
#[target_feature(enable = "sse2")]
#[inline]
fn do_vertical_filter_sse2(input: &[u8], width: i32, height: i32, stride: i32, out: &mut [u8]) {
dcheck(width, height, stride);
let stride = idx(stride);
out[0] = input[0];
predict_line_left_sse2(input, 1, &mut out[1..], width - 1);
for row in 1..idx(height) {
let cur = row * stride;
predict_line_top_sse2(
&input[cur..],
&input[cur - stride..],
&mut out[cur..],
width,
);
}
}
fn gradient_predictor_sse2(a: u8, b: u8, c: u8) -> i32 {
let g = i32::from(a) + i32::from(b) - i32::from(c);
if (g & !0xff) == 0 {
g
} else if g < 0 {
0
} else {
255
} }
#[target_feature(enable = "sse2")]
#[expect(clippy::many_single_char_names, reason = "the C register names")]
fn gradient_predict_direct_sse2(
row: &[u8],
row_pos: usize,
top: &[u8],
top_pos: usize,
out: &mut [u8],
length: i32,
) {
let max_pos = length & !7;
let zero = _mm_setzero_si128();
let mut i = 0;
while i < max_pos {
let r = row_pos + idx(i);
let t = top_pos + idx(i);
let a0 = loadl_epi64(&row[r - 1..r + 7]);
let b0 = loadl_epi64(&top[t..t + 8]);
let c0 = loadl_epi64(&top[t - 1..t + 7]);
let d = loadl_epi64(&row[r..r + 8]);
let a1 = _mm_unpacklo_epi8(a0, zero);
let b1 = _mm_unpacklo_epi8(b0, zero);
let c1 = _mm_unpacklo_epi8(c0, zero);
let e = _mm_add_epi16(a1, b1);
let f = _mm_sub_epi16(e, c1);
let g = _mm_packus_epi16(f, zero);
let h = _mm_sub_epi8(d, g);
let o = idx(i);
storel_epi64(&mut out[o..o + 8], h);
i += 8;
}
while i < length {
let r = row_pos + idx(i);
let t = top_pos + idx(i);
let delta = gradient_predictor_sse2(row[r - 1], top[t], top[t - 1]);
out[idx(i)] = low_byte(i32::from(row[r]) - delta);
i += 1;
}
}
#[target_feature(enable = "sse2")]
#[inline]
fn do_gradient_filter_sse2(input: &[u8], width: i32, height: i32, stride: i32, out: &mut [u8]) {
dcheck(width, height, stride);
let stride = idx(stride);
out[0] = input[0];
predict_line_left_sse2(input, 1, &mut out[1..], width - 1);
for row in 1..idx(height) {
let cur = row * stride;
out[cur] = low_byte(i32::from(input[cur]) - i32::from(input[cur - stride]));
gradient_predict_direct_sse2(
input,
cur + 1,
input,
cur + 1 - stride,
&mut out[cur + 1..],
width - 1,
);
}
}
#[target_feature(enable = "sse2")]
fn horizontal_filter_sse2(
data: &[u8],
width: i32,
height: i32,
stride: i32,
filtered_data: &mut [u8],
) {
do_horizontal_filter_sse2(data, width, height, stride, filtered_data);
}
pub(crate) fn horizontal_filter(
data: &[u8],
width: i32,
height: i32,
stride: i32,
filtered_data: &mut [u8],
) {
unsafe { horizontal_filter_sse2(data, width, height, stride, filtered_data) }
}
#[target_feature(enable = "sse2")]
fn vertical_filter_sse2(
data: &[u8],
width: i32,
height: i32,
stride: i32,
filtered_data: &mut [u8],
) {
do_vertical_filter_sse2(data, width, height, stride, filtered_data);
}
pub(crate) fn vertical_filter(
data: &[u8],
width: i32,
height: i32,
stride: i32,
filtered_data: &mut [u8],
) {
unsafe { vertical_filter_sse2(data, width, height, stride, filtered_data) }
}
#[target_feature(enable = "sse2")]
fn gradient_filter_sse2(
data: &[u8],
width: i32,
height: i32,
stride: i32,
filtered_data: &mut [u8],
) {
do_gradient_filter_sse2(data, width, height, stride, filtered_data);
}
pub(crate) fn gradient_filter(
data: &[u8],
width: i32,
height: i32,
stride: i32,
filtered_data: &mut [u8],
) {
unsafe { gradient_filter_sse2(data, width, height, stride, filtered_data) }
}
#[target_feature(enable = "sse2")]
fn horizontal_unfilter_sse2(prev: Option<&[u8]>, input: &[u8], out: &mut [u8], width: i32) {
out[0] = input[0].wrapping_add(prev.map_or(0, |prev| prev[0]));
if width <= 1 {
return;
}
let mut last = _mm_set_epi32(0, 0, 0, i32::from(out[0]));
let mut i = 1;
while i + 8 <= width {
let k = idx(i);
let a0 = loadl_epi64(&input[k..k + 8]);
let a1 = _mm_add_epi8(a0, last);
let a2 = _mm_slli_si128::<1>(a1);
let a3 = _mm_add_epi8(a1, a2);
let a4 = _mm_slli_si128::<2>(a3);
let a5 = _mm_add_epi8(a3, a4);
let a6 = _mm_slli_si128::<4>(a5);
let a7 = _mm_add_epi8(a5, a6);
storel_epi64(&mut out[k..k + 8], a7);
last = _mm_srli_epi64::<56>(a7);
i += 8;
}
while i < width {
let k = idx(i);
out[k] = input[k].wrapping_add(out[k - 1]);
i += 1;
}
}
pub(crate) fn horizontal_unfilter(prev: Option<&[u8]>, input: &[u8], out: &mut [u8], width: i32) {
unsafe { horizontal_unfilter_sse2(prev, input, out, width) }
}
#[target_feature(enable = "sse2")]
fn vertical_unfilter_sse2(prev: Option<&[u8]>, input: &[u8], out: &mut [u8], width: i32) {
match prev {
None => horizontal_unfilter_sse2(None, input, out, width),
Some(prev) => {
debug_assert!(width >= 0);
let width = usize::try_from(width).unwrap_or(0);
let (input, prev, out) = (&input[..width], &prev[..width], &mut out[..width]);
let max_pos = width & !31;
let mut i = 0;
while i < max_pos {
let k = i;
let a0 = loadu_si128(&input[k..k + 16]);
let a1 = loadu_si128(&input[k + 16..k + 32]);
let b0 = loadu_si128(&prev[k..k + 16]);
let b1 = loadu_si128(&prev[k + 16..k + 32]);
let c0 = _mm_add_epi8(a0, b0);
let c1 = _mm_add_epi8(a1, b1);
storeu_si128(&mut out[k..k + 16], c0);
storeu_si128(&mut out[k + 16..k + 32], c1);
i += 32;
}
while i < width {
let k = i;
out[k] = input[k].wrapping_add(prev[k]);
i += 1;
}
}
}
}
pub(crate) fn vertical_unfilter(prev: Option<&[u8]>, input: &[u8], out: &mut [u8], width: i32) {
unsafe { vertical_unfilter_sse2(prev, input, out, width) }
}
#[target_feature(enable = "sse2")]
#[expect(clippy::many_single_char_names, reason = "the C register names")]
fn gradient_predict_inverse_sse2(
input: &[u8],
top: &[u8],
top_pos: usize,
row: &mut [u8],
row_pos: usize,
length: i32,
) {
if length > 0 {
let length = idx(length);
let input = &input[..length];
let top = &top[top_pos - 1..][..=length];
let row = &mut row[row_pos - 1..][..=length];
let (top_pos, row_pos) = (1, 1);
let max_pos = length & !7;
let zero = _mm_setzero_si128();
let mut a = _mm_set_epi32(0, 0, 0, i32::from(row[row_pos - 1])); let mut i = 0;
while i < max_pos {
let p = i;
let t = top_pos + p;
let tmp0 = loadl_epi64(&top[t..t + 8]);
let tmp1 = loadl_epi64(&top[t - 1..t + 7]);
let b = _mm_unpacklo_epi8(tmp0, zero);
let c = _mm_unpacklo_epi8(tmp1, zero);
let d = loadl_epi64(&input[p..p + 8]); let e = _mm_sub_epi16(b, c); let mut out = zero; let mut mask_hi = _mm_set_epi32(0, 0, 0, 0xff);
let mut k = 8;
loop {
let tmp3 = _mm_add_epi16(a, e); let tmp4 = _mm_packus_epi16(tmp3, zero); let tmp5 = _mm_add_epi8(tmp4, d); a = _mm_and_si128(tmp5, mask_hi); out = _mm_or_si128(out, a); k -= 1;
if k == 0 {
break;
}
a = _mm_slli_si128::<1>(a); mask_hi = _mm_slli_si128::<1>(mask_hi); a = _mm_unpacklo_epi8(a, zero); }
a = _mm_srli_si128::<7>(a); let r = row_pos + p;
storel_epi64(&mut row[r..r + 8], out);
i += 8;
}
while i < length {
let p = i;
let t = top_pos + p;
let r = row_pos + p;
let delta = gradient_predictor_sse2(row[r - 1], top[t], top[t - 1]);
row[r] = low_byte(i32::from(input[p]) + delta);
i += 1;
}
}
}
#[target_feature(enable = "sse2")]
fn gradient_unfilter_sse2(prev: Option<&[u8]>, input: &[u8], out: &mut [u8], width: i32) {
match prev {
None => horizontal_unfilter_sse2(None, input, out, width),
Some(prev) => {
out[0] = input[0].wrapping_add(prev[0]); gradient_predict_inverse_sse2(&input[1..], prev, 1, out, 1, width - 1);
}
}
}
pub(crate) fn gradient_unfilter(prev: Option<&[u8]>, input: &[u8], out: &mut [u8], width: i32) {
unsafe { gradient_unfilter_sse2(prev, input, out, width) }
}