use crate::math::FloatExt;
use fearless_simd::{
Bytes, Simd, SimdBase, SimdFloat, f32x16, u8x16, u8x32, u16x16, u16x32, u32x16,
};
use peniko::kurbo::Affine;
#[cfg(not(feature = "std"))]
use peniko::kurbo::common::FloatFuncs as _;
#[inline(always)]
pub fn f32_to_u8<S: Simd>(val: f32x16<S>) -> u8x16<S> {
let simd = val.simd;
let converted = val.to_int::<u32x16<S>>().to_bytes();
let (x8_1, x8_2) = simd.split_u8x64(converted);
let (p1, p2) = simd.split_u8x32(x8_1);
let (p3, p4) = simd.split_u8x32(x8_2);
let uzp1 = simd.unzip_low_u8x16(p1, p2);
let uzp2 = simd.unzip_low_u8x16(p3, p4);
simd.unzip_low_u8x16(uzp1, uzp2)
}
pub trait Div255Ext {
fn div_255(self) -> Self;
}
impl<S: Simd> Div255Ext for u16x32<S> {
#[inline(always)]
fn div_255(self) -> Self {
let p1 = Self::splat(self.simd, 255);
let p2 = self + p1;
p2 >> 8
}
}
impl<S: Simd> Div255Ext for u16x16<S> {
#[inline(always)]
fn div_255(self) -> Self {
let p1 = Self::splat(self.simd, 255);
let p2 = self + p1;
p2 >> 8
}
}
#[inline(always)]
pub fn normalized_mul_u8x32<S: Simd>(a: u8x32<S>, b: u8x32<S>) -> u16x32<S> {
(S::widen_u8x32(a.simd, a) * S::widen_u8x32(b.simd, b)).div_255()
}
#[inline(always)]
pub fn normalized_mul_u8x16<S: Simd>(a: u8x16<S>, b: u8x16<S>) -> u16x16<S> {
(S::widen_u8x16(a.simd, a) * S::widen_u8x16(b.simd, b)).div_255()
}
#[inline]
pub fn is_integer_translation(transform: &Affine) -> bool {
let [a, b, c, d, e, f] = transform.as_coeffs();
(a - 1.0).is_nearly_zero()
&& b.is_nearly_zero()
&& c.is_nearly_zero()
&& (d - 1.0).is_nearly_zero()
&& (e - e.round()).is_nearly_zero()
&& (f - f.round()).is_nearly_zero()
}
#[inline]
pub fn is_axis_aligned(transform: &Affine) -> bool {
let [_, b, c, ..] = transform.as_coeffs();
b.is_nearly_zero() && c.is_nearly_zero()
}
#[inline]
pub fn extract_scales(transform: &Affine) -> (f32, f32) {
let [a, b, c, d, _, _] = transform.as_coeffs();
let a = a as f32;
let b = b as f32;
let c = c as f32;
let d = d as f32;
let a2 = a * a;
let b2 = b * b;
let c2 = c * c;
let d2 = d * d;
let s1 = a2 + b2 + c2 + d2;
let s2 = ((a2 - b2 + c2 - d2).powi(2) + 4.0 * (a * b + c * d).powi(2)).sqrt();
let scale_x = (0.5 * (s1 + s2)).sqrt();
let scale_y = (0.5 * (s1 - s2)).sqrt();
(scale_x.max(1e-6), scale_y.max(1e-6))
}