use crate::domain::scalar::Scalar;
use eunomia::{Bf16, FloatElement, NumericElement, UnitScalar, F16};
pub trait RealScalar: Scalar + FloatElement + UnitScalar + core::ops::Neg<Output = Self> {
#[inline]
fn abs_sum_slice(s: &[Self]) -> Self {
s.iter().fold(<Self as NumericElement>::ZERO, |acc, &x| {
acc.add(<Self as NumericElement>::abs(x))
})
}
#[inline]
fn abs_max_slice(s: &[Self]) -> Self {
s.iter().fold(<Self as NumericElement>::ZERO, |acc, &x| {
let magnitude = <Self as NumericElement>::abs(x);
if magnitude > acc {
magnitude
} else {
acc
}
})
}
}
macro_rules! impl_real_simd {
($t:ty) => {
impl RealScalar for $t {
#[inline]
fn abs_sum_slice(s: &[Self]) -> Self {
use crate::domain::strategy::{SimdOperations, SimdStrategy};
if let Some(res) = <SimdStrategy as SimdOperations<Self>>::abs_sum_slice(s) {
res
} else {
s.iter().fold(<Self as NumericElement>::ZERO, |acc, &x| {
acc + <Self as NumericElement>::abs(x)
})
}
}
#[inline]
fn abs_max_slice(s: &[Self]) -> Self {
use crate::domain::strategy::{SimdOperations, SimdStrategy};
if let Some(res) = <SimdStrategy as SimdOperations<Self>>::abs_max_slice(s) {
res
} else {
s.iter().fold(<Self as NumericElement>::ZERO, |acc, &x| {
let magnitude = <Self as NumericElement>::abs(x);
if magnitude > acc {
magnitude
} else {
acc
}
})
}
}
}
};
}
impl_real_simd!(f32);
impl_real_simd!(f64);
impl RealScalar for F16 {}
impl RealScalar for Bf16 {}