#[cfg(target_arch = "x86")]
use core::arch::x86 as arch;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64 as arch;
use arch::{
__m128i, _mm_add_epi16, _mm_add_epi32, _mm_and_si128, _mm_cvtsi128_si32, _mm_extract_epi8,
_mm_hadd_epi32, _mm_madd_epi16, _mm_mulhi_epi16, _mm_mullo_epi32, _mm_set_epi8, _mm_set_epi32,
_mm_set1_epi32, _mm_setr_epi8, _mm_shuffle_epi8, _mm_srli_epi32, _mm_srli_si128, _mm_sub_epi8,
_mm_sub_epi16,
};
use crate::dsp::common_sse2::{loadu_si128, storeu_si128};
use crate::dsp::lossless_enc::{
vp8l_collect_color_blue_transforms_c, vp8l_collect_color_red_transforms_c,
vp8l_subtract_green_from_blue_and_red_c,
};
use crate::dsp::lossless_enc_sse2::{cst_5b, mk_cst_16};
use crate::dsp::lossless_sse2::idx;
#[target_feature(enable = "sse4.1")]
#[inline]
fn horizontal_sum_sse41(cost: __m128i) -> u32 {
let cost = _mm_add_epi32(cost, _mm_srli_si128::<8>(cost));
let cost = _mm_add_epi32(cost, _mm_srli_si128::<4>(cost));
_mm_cvtsi128_si32(cost).cast_unsigned()
}
#[target_feature(enable = "sse4.1")]
pub(crate) fn extra_cost_sse41(a: &[u32], length: i32) -> u32 {
let mut cost = _mm_set_epi32(
2_u32.wrapping_mul(a[7]).cast_signed(),
2_u32.wrapping_mul(a[6]).cast_signed(),
a[5].cast_signed(),
a[4].cast_signed(),
);
debug_assert_eq!(length % 8, 0);
let mut i: i32 = 8;
while i + 8 <= length {
let j = (i - 2) >> 1;
let a0 = loadu_si128(&a[idx(i)..]);
let a1 = loadu_si128(&a[idx(i + 4)..]);
let w = _mm_set_epi32(j + 3, j + 2, j + 1, j);
let a2 = _mm_hadd_epi32(a0, a1);
let mul = _mm_mullo_epi32(a2, w);
cost = _mm_add_epi32(mul, cost);
i += 8;
}
horizontal_sum_sse41(cost)
}
#[target_feature(enable = "sse4.1")]
pub(crate) fn subtract_green_from_blue_and_red_sse41(argb_data: &mut [u32], num_pixels: i32) {
let k_cst_shuffle = _mm_set_epi8(-1, 13, -1, 13, -1, 9, -1, 9, -1, 5, -1, 5, -1, 1, -1, 1);
let mut i: i32 = 0;
while i + 4 <= num_pixels {
let input = loadu_si128(&argb_data[idx(i)..]);
let in_0g0g = _mm_shuffle_epi8(input, k_cst_shuffle);
let out = _mm_sub_epi8(input, in_0g0g);
storeu_si128(&mut argb_data[idx(i)..], out);
i += 4;
}
if i != num_pixels {
vp8l_subtract_green_from_blue_and_red_c(&mut argb_data[idx(i)..], num_pixels - i);
}
}
#[inline]
fn row_start(y: i32, stride: i32) -> usize {
usize::try_from(i64::from(y) * i64::from(stride)).expect("a tile row lies in its buffer")
}
#[inline]
fn count(histo: &mut [u32], value: i32) {
let index = idx(value);
histo[index] = histo[index].wrapping_add(1);
}
#[target_feature(enable = "sse4.1")]
#[expect(non_snake_case, reason = "the C register names A1 to E")]
pub(crate) fn collect_color_blue_transforms_sse41(
argb: &[u32],
stride: i32,
tile_width: i32,
tile_height: i32,
green_to_blue: i32,
red_to_blue: i32,
histo: &mut [u32],
) {
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "the C (uint16_t) cast in CST_5b"
)]
let mult = mk_cst_16(cst_5b(red_to_blue as u8) + 256, cst_5b(green_to_blue as u8));
let perm = _mm_setr_epi8(-1, 1, -1, 2, -1, 5, -1, 6, -1, 9, -1, 10, -1, 13, -1, 14);
if tile_width >= 4 {
for y in 0..tile_height {
let src = row_start(y, stride);
let A1 = loadu_si128(&argb[src..]);
let B1 = _mm_shuffle_epi8(A1, perm);
let C1 = _mm_mulhi_epi16(B1, mult);
let D1 = _mm_sub_epi16(A1, C1);
let mut E = _mm_add_epi16(_mm_srli_epi32::<16>(D1), D1);
let mut x: i32 = 4;
while x + 4 <= tile_width {
let A2 = loadu_si128(&argb[src + idx(x)..]);
count(histo, _mm_extract_epi8::<0>(E));
let B2 = _mm_shuffle_epi8(A2, perm);
count(histo, _mm_extract_epi8::<4>(E));
let C2 = _mm_mulhi_epi16(B2, mult);
count(histo, _mm_extract_epi8::<8>(E));
let D2 = _mm_sub_epi16(A2, C2);
count(histo, _mm_extract_epi8::<12>(E));
E = _mm_add_epi16(_mm_srli_epi32::<16>(D2), D2);
x += 4;
}
count(histo, _mm_extract_epi8::<0>(E));
count(histo, _mm_extract_epi8::<4>(E));
count(histo, _mm_extract_epi8::<8>(E));
count(histo, _mm_extract_epi8::<12>(E));
}
}
let left_over = tile_width & 3;
if left_over > 0 {
vp8l_collect_color_blue_transforms_c(
&argb[idx(tile_width - left_over)..],
stride,
left_over,
tile_height,
green_to_blue,
red_to_blue,
histo,
);
}
}
#[target_feature(enable = "sse4.1")]
#[expect(non_snake_case, reason = "the C register names A1 to D")]
pub(crate) fn collect_color_red_transforms_sse41(
argb: &[u32],
stride: i32,
tile_width: i32,
tile_height: i32,
green_to_red: i32,
histo: &mut [u32],
) {
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "the C (uint16_t) cast in CST_5b"
)]
let mult = mk_cst_16(0, cst_5b(green_to_red as u8));
let mask_g = _mm_set1_epi32(0x0000_ff00);
if tile_width >= 4 {
for y in 0..tile_height {
let src = row_start(y, stride);
let A1 = loadu_si128(&argb[src..]);
let B1 = _mm_and_si128(A1, mask_g);
let C1 = _mm_madd_epi16(B1, mult);
let mut D = _mm_sub_epi16(A1, C1);
let mut x: i32 = 4;
while x + 4 <= tile_width {
let A2 = loadu_si128(&argb[src + idx(x)..]);
count(histo, _mm_extract_epi8::<2>(D));
let B2 = _mm_and_si128(A2, mask_g);
count(histo, _mm_extract_epi8::<6>(D));
let C2 = _mm_madd_epi16(B2, mult);
count(histo, _mm_extract_epi8::<10>(D));
count(histo, _mm_extract_epi8::<14>(D));
D = _mm_sub_epi16(A2, C2);
x += 4;
}
count(histo, _mm_extract_epi8::<2>(D));
count(histo, _mm_extract_epi8::<6>(D));
count(histo, _mm_extract_epi8::<10>(D));
count(histo, _mm_extract_epi8::<14>(D));
}
}
let left_over = tile_width & 3;
if left_over > 0 {
vp8l_collect_color_red_transforms_c(
&argb[idx(tile_width - left_over)..],
stride,
left_over,
tile_height,
green_to_red,
histo,
);
}
}
pub(crate) fn extra_cost(a: &[u32], length: i32) -> u32 {
unsafe { extra_cost_sse41(a, length) }
}
pub(crate) fn subtract_green_from_blue_and_red(argb_data: &mut [u32], num_pixels: i32) {
unsafe { subtract_green_from_blue_and_red_sse41(argb_data, num_pixels) }
}
pub(crate) fn collect_color_blue_transforms(
argb: &[u32],
stride: i32,
tile_width: i32,
tile_height: i32,
green_to_blue: i32,
red_to_blue: i32,
histo: &mut [u32],
) {
unsafe {
collect_color_blue_transforms_sse41(
argb,
stride,
tile_width,
tile_height,
green_to_blue,
red_to_blue,
histo,
);
}
}
pub(crate) fn collect_color_red_transforms(
argb: &[u32],
stride: i32,
tile_width: i32,
tile_height: i32,
green_to_red: i32,
histo: &mut [u32],
) {
unsafe {
collect_color_red_transforms_sse41(
argb,
stride,
tile_width,
tile_height,
green_to_red,
histo,
);
}
}