#[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_and_si128, _mm_andnot_si128, _mm_cmpeq_epi8, _mm_cmpeq_epi32,
_mm_maskmoveu_si128, _mm_movemask_epi8, _mm_mulhi_epu16, _mm_mullo_epi16, _mm_or_si128,
_mm_packs_epi32, _mm_packus_epi16, _mm_set_epi16, _mm_set_epi32, _mm_set1_epi8, _mm_set1_epi16,
_mm_set1_epi32, _mm_setzero_si128, _mm_sfence, _mm_shufflehi_epi16, _mm_shufflelo_epi16,
_mm_srai_epi32, _mm_srli_epi16, _mm_srli_epi32, _mm_unpackhi_epi8, _mm_unpackhi_epi16,
_mm_unpacklo_epi8, _mm_unpacklo_epi16,
};
use crate::dsp::alpha_processing::{webp_mult_argb_row_c, webp_mult_row_c};
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")
}
fn stride(value: i32) -> usize {
usize::try_from(value).expect("the stride is not negative")
}
#[expect(
clippy::cast_possible_truncation,
reason = "a C conversion to uint8_t keeps the low eight bits"
)]
fn low_byte(value: u32) -> u8 {
value as u8
}
const fn mm_shuffle(z: i32, y: i32, x: i32, w: i32) -> i32 {
(z << 6) | (y << 4) | (x << 2) | w
}
#[target_feature(enable = "sse2")]
#[inline]
fn maskmoveu_si128(value: __m128i, mask: __m128i, dst: &mut [u8]) {
assert!(
dst.len() >= 16,
"a 16-byte masked store past the end of a slice"
);
unsafe { _mm_maskmoveu_si128(value, mask, dst.as_mut_ptr().cast()) }
}
#[target_feature(enable = "sse2")]
#[expect(
clippy::int_plus_one,
reason = "the C loop bounds i + 16 <= width - 1 and i + 8 <= width - 1"
)]
fn dispatch_alpha_sse2(
alpha: &[u8],
alpha_stride: i32,
width: i32,
height: i32,
dst: &mut [u8],
dst_stride: i32,
) -> bool {
let mut alpha_and: u32 = 0xff;
let zero = _mm_setzero_si128();
let alpha_mask = _mm_set1_epi32(0xff); let all_0xff = _mm_set1_epi8(0xff_u8.cast_signed());
let mut all_alphas16 = all_0xff;
let mut all_alphas8 = all_0xff;
let alpha_stride = stride(alpha_stride);
let dst_stride = stride(dst_stride);
for j in 0..idx(height.max(0)) {
let alpha = &alpha[j * alpha_stride..];
let dst = &mut dst[j * dst_stride..];
let mut ptr = 0;
let mut i = 0;
while i + 16 <= width - 1 {
let a0 = loadu_si128(&alpha[idx(i)..idx(i) + 16]);
let a1_lo = _mm_unpacklo_epi8(a0, zero);
let a1_hi = _mm_unpackhi_epi8(a0, zero);
let a2_lo_lo = _mm_unpacklo_epi16(a1_lo, zero);
let a2_lo_hi = _mm_unpackhi_epi16(a1_lo, zero);
let a2_hi_lo = _mm_unpacklo_epi16(a1_hi, zero);
let a2_hi_hi = _mm_unpackhi_epi16(a1_hi, zero);
maskmoveu_si128(a2_lo_lo, alpha_mask, &mut dst[ptr..ptr + 16]);
maskmoveu_si128(a2_lo_hi, alpha_mask, &mut dst[ptr + 16..ptr + 32]);
maskmoveu_si128(a2_hi_lo, alpha_mask, &mut dst[ptr + 32..ptr + 48]);
maskmoveu_si128(a2_hi_hi, alpha_mask, &mut dst[ptr + 48..ptr + 64]);
all_alphas16 = _mm_and_si128(all_alphas16, a0);
ptr += 64;
i += 16;
}
if i + 8 <= width - 1 {
let a0 = loadl_epi64(&alpha[idx(i)..idx(i) + 8]);
let a1 = _mm_unpacklo_epi8(a0, zero);
let a2_lo = _mm_unpacklo_epi16(a1, zero);
let a2_hi = _mm_unpackhi_epi16(a1, zero);
maskmoveu_si128(a2_lo, alpha_mask, &mut dst[ptr..ptr + 16]);
maskmoveu_si128(a2_hi, alpha_mask, &mut dst[ptr + 16..ptr + 32]);
all_alphas8 = _mm_and_si128(all_alphas8, a0);
i += 8;
}
while i < width {
let alpha_value = u32::from(alpha[idx(i)]);
dst[4 * idx(i)] = low_byte(alpha_value);
alpha_and &= alpha_value;
i += 1;
}
}
_mm_sfence();
alpha_and &= (_mm_movemask_epi8(_mm_cmpeq_epi8(all_alphas8, all_0xff)) & 0xff).cast_unsigned();
alpha_and != 0xff || _mm_movemask_epi8(_mm_cmpeq_epi8(all_alphas16, all_0xff)) != 0xffff
}
pub(crate) fn dispatch_alpha(
alpha: &[u8],
alpha_stride: i32,
width: i32,
height: i32,
dst: &mut [u8],
dst_stride: i32,
) -> bool {
unsafe { dispatch_alpha_sse2(alpha, alpha_stride, width, height, dst, dst_stride) }
}
#[target_feature(enable = "sse2")]
fn dispatch_alpha_to_green_sse2(
alpha: &[u8],
alpha_stride: i32,
width: i32,
height: i32,
dst: &mut [u32],
dst_stride: i32,
) {
let zero = _mm_setzero_si128();
let limit = width & !15;
let alpha_stride = stride(alpha_stride);
let dst_stride = stride(dst_stride);
for j in 0..idx(height.max(0)) {
let alpha = &alpha[j * alpha_stride..];
let dst = &mut dst[j * dst_stride..];
let mut i = 0;
while i < limit {
let k = idx(i);
let a0 = loadu_si128(&alpha[k..k + 16]);
let a1 = _mm_unpacklo_epi8(zero, a0); let b1 = _mm_unpackhi_epi8(zero, a0);
let a2_lo = _mm_unpacklo_epi16(a1, zero);
let b2_lo = _mm_unpacklo_epi16(b1, zero);
let a2_hi = _mm_unpackhi_epi16(a1, zero);
let b2_hi = _mm_unpackhi_epi16(b1, zero);
storeu_si128(&mut dst[k..k + 4], a2_lo);
storeu_si128(&mut dst[k + 4..k + 8], a2_hi);
storeu_si128(&mut dst[k + 8..k + 12], b2_lo);
storeu_si128(&mut dst[k + 12..k + 16], b2_hi);
i += 16;
}
while i < width {
dst[idx(i)] = u32::from(alpha[idx(i)]) << 8;
i += 1;
}
}
}
pub(crate) fn dispatch_alpha_to_green(
alpha: &[u8],
alpha_stride: i32,
width: i32,
height: i32,
dst: &mut [u32],
dst_stride: i32,
) {
unsafe { dispatch_alpha_to_green_sse2(alpha, alpha_stride, width, height, dst, dst_stride) }
}
#[target_feature(enable = "sse2")]
fn extract_alpha_sse2(
argb: &[u8],
argb_stride: i32,
width: i32,
height: i32,
alpha: &mut [u8],
alpha_stride: i32,
) -> bool {
let mut alpha_and: u32 = 0xff;
let a_mask = _mm_set1_epi32(0xff); let all_0xff = _mm_set_epi32(0, 0, !0, !0);
let mut all_alphas = all_0xff;
let argb_stride = stride(argb_stride);
let alpha_stride = stride(alpha_stride);
let limit = (width - 1) & !7;
for j in 0..idx(height.max(0)) {
let argb = &argb[j * argb_stride..];
let alpha = &mut alpha[j * alpha_stride..];
let mut src = 0;
let mut i = 0;
while i < limit {
let a0 = loadu_si128(&argb[src..src + 16]);
let a1 = loadu_si128(&argb[src + 16..src + 32]);
let b0 = _mm_and_si128(a0, a_mask);
let b1 = _mm_and_si128(a1, a_mask);
let c0 = _mm_packs_epi32(b0, b1);
let d0 = _mm_packus_epi16(c0, c0);
storel_epi64(&mut alpha[idx(i)..idx(i) + 8], d0);
all_alphas = _mm_and_si128(all_alphas, d0);
src += 32;
i += 8;
}
while i < width {
let alpha_value = u32::from(argb[4 * idx(i)]);
alpha[idx(i)] = low_byte(alpha_value);
alpha_and &= alpha_value;
i += 1;
}
}
alpha_and &= _mm_movemask_epi8(_mm_cmpeq_epi8(all_alphas, all_0xff)).cast_unsigned();
alpha_and == 0xff
}
pub(crate) fn extract_alpha(
argb: &[u8],
argb_stride: i32,
width: i32,
height: i32,
alpha: &mut [u8],
alpha_stride: i32,
) -> bool {
unsafe { extract_alpha_sse2(argb, argb_stride, width, height, alpha, alpha_stride) }
}
#[target_feature(enable = "sse2")]
fn extract_green_sse2(argb: &[u32], alpha: &mut [u8], size: i32) {
let mask = _mm_set1_epi32(0xff);
let mut src = 0;
let mut i = 0;
while i + 16 <= size {
let a0 = loadu_si128(&argb[src..src + 4]);
let a1 = loadu_si128(&argb[src + 4..src + 8]);
let a2 = loadu_si128(&argb[src + 8..src + 12]);
let a3 = loadu_si128(&argb[src + 12..src + 16]);
let b0 = _mm_srli_epi32::<8>(a0);
let b1 = _mm_srli_epi32::<8>(a1);
let b2 = _mm_srli_epi32::<8>(a2);
let b3 = _mm_srli_epi32::<8>(a3);
let c0 = _mm_and_si128(b0, mask);
let c1 = _mm_and_si128(b1, mask);
let c2 = _mm_and_si128(b2, mask);
let c3 = _mm_and_si128(b3, mask);
let d0 = _mm_packs_epi32(c0, c1);
let d1 = _mm_packs_epi32(c2, c3);
let e = _mm_packus_epi16(d0, d1);
storeu_si128(&mut alpha[idx(i)..idx(i) + 16], e);
i += 16;
src += 16;
}
if i + 8 <= size {
let a0 = loadu_si128(&argb[src..src + 4]);
let a1 = loadu_si128(&argb[src + 4..src + 8]);
let b0 = _mm_srli_epi32::<8>(a0);
let b1 = _mm_srli_epi32::<8>(a1);
let c0 = _mm_and_si128(b0, mask);
let c1 = _mm_and_si128(b1, mask);
let d = _mm_packs_epi32(c0, c1);
let e = _mm_packus_epi16(d, d);
storel_epi64(&mut alpha[idx(i)..idx(i) + 8], e);
i += 8;
}
while i < size {
alpha[idx(i)] = low_byte(argb[idx(i)] >> 8);
i += 1;
}
}
pub(crate) fn extract_green(argb: &[u32], alpha: &mut [u8], size: i32) {
unsafe { extract_green_sse2(argb, alpha, size) }
}
fn multiplier(a: u32) -> u32 {
a * 0x8081
}
fn premultiply(x: u8, m: u32) -> u8 {
low_byte((u32::from(x) * m) >> 23)
}
#[target_feature(enable = "sse2")]
#[inline]
fn apply_alpha<const SHUFFLE: i32>(
rgbx: &mut [u8],
zero: __m128i,
k_mask: __m128i,
k_mult: __m128i,
) {
let argb0 = loadu_si128(rgbx);
let argb1_lo = _mm_unpacklo_epi8(argb0, zero);
let argb1_hi = _mm_unpackhi_epi8(argb0, zero);
let alpha0_lo = _mm_or_si128(argb1_lo, k_mask);
let alpha0_hi = _mm_or_si128(argb1_hi, k_mask);
let alpha1_lo = _mm_shufflelo_epi16::<SHUFFLE>(alpha0_lo);
let alpha1_hi = _mm_shufflelo_epi16::<SHUFFLE>(alpha0_hi);
let alpha2_lo = _mm_shufflehi_epi16::<SHUFFLE>(alpha1_lo);
let alpha2_hi = _mm_shufflehi_epi16::<SHUFFLE>(alpha1_hi);
let a0_lo = _mm_mullo_epi16(alpha2_lo, argb1_lo);
let a0_hi = _mm_mullo_epi16(alpha2_hi, argb1_hi);
let a1_lo = _mm_mulhi_epu16(a0_lo, k_mult);
let a1_hi = _mm_mulhi_epu16(a0_hi, k_mult);
let a2_lo = _mm_srli_epi16::<7>(a1_lo);
let a2_hi = _mm_srli_epi16::<7>(a1_hi);
let a3 = _mm_packus_epi16(a2_lo, a2_hi);
storeu_si128(rgbx, a3);
}
#[target_feature(enable = "sse2")]
#[expect(clippy::many_single_char_names, reason = "the C names w, h, i and a")]
fn apply_alpha_multiply_sse2(rgba: &mut [u8], alpha_first: bool, w: i32, h: i32, stride_: i32) {
let zero = _mm_setzero_si128();
let k_mult = _mm_set1_epi16(0x8081_u16.cast_signed());
let k_mask = _mm_set_epi16(0, 0xff, 0xff, 0, 0, 0xff, 0xff, 0);
let k_span = 4;
let row_stride = stride(stride_);
let mut h = h;
let mut row = 0;
while h > 0 {
h -= 1;
let rgba = &mut rgba[row..];
let mut i = 0;
if alpha_first {
while i + k_span <= w {
apply_alpha::<{ mm_shuffle(0, 0, 0, 1) }>(
&mut rgba[4 * idx(i)..4 * idx(i) + 16],
zero,
k_mask,
k_mult,
);
i += k_span;
}
} else {
while i + k_span <= w {
apply_alpha::<{ mm_shuffle(2, 3, 3, 3) }>(
&mut rgba[4 * idx(i)..4 * idx(i) + 16],
zero,
k_mask,
k_mult,
);
i += k_span;
}
}
while i < w {
let rgb = usize::from(alpha_first);
let alpha = if alpha_first { 0 } else { 3 };
let a = u32::from(rgba[alpha + 4 * idx(i)]);
if a != 0xff {
let mult = multiplier(a);
let p = rgb + 4 * idx(i);
rgba[p] = premultiply(rgba[p], mult);
rgba[p + 1] = premultiply(rgba[p + 1], mult);
rgba[p + 2] = premultiply(rgba[p + 2], mult);
}
i += 1;
}
if h > 0 {
row += row_stride;
}
}
}
pub(crate) fn apply_alpha_multiply(
rgba: &mut [u8],
alpha_first: bool,
w: i32,
h: i32,
stride: i32,
) {
unsafe { apply_alpha_multiply_sse2(rgba, alpha_first, w, h, stride) }
}
#[target_feature(enable = "sse2")]
fn has_alpha8b_sse2(src: &[u8], length: i32) -> bool {
let all_0xff = _mm_set1_epi8(0xff_u8.cast_signed());
let mut i = 0;
while i + 16 <= length {
let v = loadu_si128(&src[idx(i)..idx(i) + 16]);
let bits = _mm_cmpeq_epi8(v, all_0xff);
let mask = _mm_movemask_epi8(bits);
if mask != 0xffff {
return true;
}
i += 16;
}
while i < length {
if src[idx(i)] != 0xff {
return true;
}
i += 1;
}
false
}
pub(crate) fn has_alpha8b(src: &[u8], length: i32) -> bool {
unsafe { has_alpha8b_sse2(src, length) }
}
#[target_feature(enable = "sse2")]
fn has_alpha32b_sse2(src: &[u8], length: i32) -> bool {
let alpha_mask = _mm_set1_epi32(0xff);
let all_0xff = _mm_set1_epi8(0xff_u8.cast_signed());
let mut i = 0;
let length = length * 4 - 3; while i + 64 <= length {
let k = idx(i);
let a0 = loadu_si128(&src[k..k + 16]);
let a1 = loadu_si128(&src[k + 16..k + 32]);
let a2 = loadu_si128(&src[k + 32..k + 48]);
let a3 = loadu_si128(&src[k + 48..k + 64]);
let b0 = _mm_and_si128(a0, alpha_mask);
let b1 = _mm_and_si128(a1, alpha_mask);
let b2 = _mm_and_si128(a2, alpha_mask);
let b3 = _mm_and_si128(a3, alpha_mask);
let c0 = _mm_packs_epi32(b0, b1);
let c1 = _mm_packs_epi32(b2, b3);
let d = _mm_packus_epi16(c0, c1);
let bits = _mm_cmpeq_epi8(d, all_0xff);
let mask = _mm_movemask_epi8(bits);
if mask != 0xffff {
return true;
}
i += 64;
}
while i + 32 <= length {
let k = idx(i);
let a0 = loadu_si128(&src[k..k + 16]);
let a1 = loadu_si128(&src[k + 16..k + 32]);
let b0 = _mm_and_si128(a0, alpha_mask);
let b1 = _mm_and_si128(a1, alpha_mask);
let c = _mm_packs_epi32(b0, b1);
let d = _mm_packus_epi16(c, c);
let bits = _mm_cmpeq_epi8(d, all_0xff);
let mask = _mm_movemask_epi8(bits);
if mask != 0xffff {
return true;
}
i += 32;
}
while i <= length {
if src[idx(i)] != 0xff {
return true;
}
i += 4;
}
false
}
pub(crate) fn has_alpha32b(src: &[u8], length: i32) -> bool {
unsafe { has_alpha32b_sse2(src, length) }
}
#[target_feature(enable = "sse2")]
fn alpha_replace_sse2(src: &mut [u32], length: i32, color: u32) {
let m_color = _mm_set1_epi32(color.cast_signed());
let zero = _mm_setzero_si128();
let mut i = 0;
while i + 8 <= length {
let k = idx(i);
let a0 = loadu_si128(&src[k..k + 4]);
let a1 = loadu_si128(&src[k + 4..k + 8]);
let b0 = _mm_srai_epi32::<24>(a0);
let b1 = _mm_srai_epi32::<24>(a1);
let c0 = _mm_cmpeq_epi32(b0, zero);
let c1 = _mm_cmpeq_epi32(b1, zero);
let d0 = _mm_and_si128(c0, m_color);
let d1 = _mm_and_si128(c1, m_color);
let e0 = _mm_andnot_si128(c0, a0);
let e1 = _mm_andnot_si128(c1, a1);
storeu_si128(&mut src[k..k + 4], _mm_or_si128(d0, e0));
storeu_si128(&mut src[k + 4..k + 8], _mm_or_si128(d1, e1));
i += 8;
}
while i < length {
if (src[idx(i)] >> 24) == 0 {
src[idx(i)] = color;
}
i += 1;
}
}
pub(crate) fn alpha_replace(src: &mut [u32], length: i32, color: u32) {
unsafe { alpha_replace_sse2(src, length, color) }
}
#[target_feature(enable = "sse2")]
fn mult_argb_row_sse2(ptr: &mut [u32], width: i32, inverse: bool) {
let mut x = 0;
if !inverse {
let k_span = 2;
let zero = _mm_setzero_si128();
let k128 = _mm_set1_epi16(128);
let k_mult = _mm_set1_epi16(0x0101);
let k_mask = _mm_set_epi16(0, 0xff, 0, 0, 0, 0xff, 0, 0);
while x + k_span <= width {
let a0 = loadl_epi64(&ptr[idx(x)..idx(x) + 2]);
let a1 = _mm_unpacklo_epi8(a0, zero);
let a2 = _mm_or_si128(a1, k_mask);
let a3 = _mm_shufflelo_epi16::<{ mm_shuffle(2, 3, 3, 3) }>(a2);
let a4 = _mm_shufflehi_epi16::<{ mm_shuffle(2, 3, 3, 3) }>(a3);
let a5 = _mm_mullo_epi16(a4, a1);
let a6 = _mm_add_epi16(a5, k128);
let a7 = _mm_mulhi_epu16(a6, k_mult);
let a10 = _mm_packus_epi16(a7, zero);
storel_epi64(&mut ptr[idx(x)..idx(x) + 2], a10);
x += k_span;
}
}
let width = width - x;
if width > 0 {
webp_mult_argb_row_c(&mut ptr[idx(x)..], width, inverse);
}
}
pub(crate) fn mult_argb_row(ptr: &mut [u32], width: i32, inverse: bool) {
unsafe { mult_argb_row_sse2(ptr, width, inverse) }
}
#[target_feature(enable = "sse2")]
fn mult_row_sse2(ptr: &mut [u8], alpha: &[u8], width: i32, inverse: bool) {
let mut x = 0;
if !inverse {
let zero = _mm_setzero_si128();
let k128 = _mm_set1_epi16(128);
let k_mult = _mm_set1_epi16(0x0101);
while x + 8 <= width {
let k = idx(x);
let v0 = loadl_epi64(&ptr[k..k + 8]);
let a0 = loadl_epi64(&alpha[k..k + 8]);
let v1 = _mm_unpacklo_epi8(v0, zero);
let a1 = _mm_unpacklo_epi8(a0, zero);
let v2 = _mm_mullo_epi16(v1, a1);
let v3 = _mm_add_epi16(v2, k128);
let v4 = _mm_mulhi_epu16(v3, k_mult);
let v5 = _mm_packus_epi16(v4, zero);
storel_epi64(&mut ptr[k..k + 8], v5);
x += 8;
}
}
let width = width - x;
if width > 0 {
webp_mult_row_c(&mut ptr[idx(x)..], &alpha[idx(x)..], width, inverse);
}
}
pub(crate) fn mult_row(ptr: &mut [u8], alpha: &[u8], width: i32, inverse: bool) {
unsafe { mult_row_sse2(ptr, alpha, width, inverse) }
}