use crate::dtype::Element;
pub trait WideAcc: Copy {
const ZERO: Self;
const ONE: Self;
fn from_elem<T: Element>(v: T) -> Self;
fn to_elem<T: Element>(self) -> T;
fn wide_add(self, other: Self) -> Self;
fn wide_mul(self, other: Self) -> Self;
}
impl WideAcc for f32 {
const ZERO: Self = 0.0;
const ONE: Self = 1.0;
#[inline]
fn from_elem<T: Element>(v: T) -> Self {
v.to_f32()
}
#[inline]
fn to_elem<T: Element>(self) -> T {
T::from_f32(self)
}
#[inline]
fn wide_add(self, other: Self) -> Self {
self + other
}
#[inline]
fn wide_mul(self, other: Self) -> Self {
self * other
}
}
impl WideAcc for i128 {
const ZERO: Self = 0;
const ONE: Self = 1;
#[inline]
fn from_elem<T: Element>(v: T) -> Self {
v.to_i128()
}
#[inline]
fn to_elem<T: Element>(self) -> T {
T::from_i128_saturating(self)
}
#[inline]
fn wide_add(self, other: Self) -> Self {
self.saturating_add(other)
}
#[inline]
fn wide_mul(self, other: Self) -> Self {
self.saturating_mul(other)
}
}
pub trait FloatAcc: Copy {
const ZERO: Self;
fn from_elem<T: Element>(v: T) -> Self;
fn to_elem<T: Element>(self) -> T;
fn acc_add(self, other: Self) -> Self;
fn acc_mul(self, other: Self) -> Self;
}
impl FloatAcc for f32 {
const ZERO: Self = <f32 as WideAcc>::ZERO;
#[inline]
fn from_elem<T: Element>(v: T) -> Self {
<f32 as WideAcc>::from_elem(v)
}
#[inline]
fn to_elem<T: Element>(self) -> T {
<f32 as WideAcc>::to_elem(self)
}
#[inline]
fn acc_add(self, other: Self) -> Self {
self.wide_add(other)
}
#[inline]
fn acc_mul(self, other: Self) -> Self {
self.wide_mul(other)
}
}
impl FloatAcc for f64 {
const ZERO: Self = 0.0;
#[inline]
fn from_elem<T: Element>(v: T) -> Self {
v.to_f64()
}
#[inline]
fn to_elem<T: Element>(self) -> T {
T::from_f64(self)
}
#[inline]
fn acc_add(self, other: Self) -> Self {
self + other
}
#[inline]
fn acc_mul(self, other: Self) -> Self {
self * other
}
}
#[inline]
pub fn int_mean_from_sum<T: Element>(sum: i128, count: usize) -> T {
T::from_i128_saturating(sum / count.max(1) as i128)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn i128_accumulator_holds_an_i32_sum_that_overflows_i32() {
let a = 2_000_000_000i32;
let sum = i128::ZERO
.wide_add(i128::from_elem(a))
.wide_add(i128::from_elem(a));
assert_eq!(sum, 4_000_000_000i128);
assert_eq!((sum / 2).to_elem::<i32>(), 2_000_000_000i32);
}
#[test]
fn narrowing_saturates_instead_of_wrapping() {
assert_eq!(4_000_000_000i128.to_elem::<i32>(), i32::MAX);
assert_eq!((-4_000_000_000i128).to_elem::<i32>(), i32::MIN);
assert_eq!((-1i128).to_elem::<u8>(), 0u8);
assert_eq!(300i128.to_elem::<u8>(), u8::MAX);
}
#[test]
fn int_mean_survives_a_sum_that_overflows_the_output_dtype() {
let sum = 2_000_000_000i128 + 2_000_000_000i128;
assert_eq!(int_mean_from_sum::<i32>(sum, 2), 2_000_000_000i32);
assert_eq!(int_mean_from_sum::<i32>(-sum, 2), -2_000_000_000i32);
}
#[test]
fn int_mean_truncates_toward_zero() {
assert_eq!(int_mean_from_sum::<i32>(7, 2), 3i32);
assert_eq!(int_mean_from_sum::<i32>(-7, 2), -3i32);
}
#[test]
fn i128_products_of_i64_operands_are_exact() {
let a = i128::from_elem(3_000_000_000i64);
let b = i128::from_elem(3_000_000_000i64);
assert_eq!(a.wide_mul(b), 9_000_000_000_000_000_000i128);
}
}