use super::Decimal;
#[cfg(feature = "float")]
use super::NonNegF64;
pub trait WideMul: Copy {
type Wide: Copy + Ord;
#[must_use]
fn wide_mul(self, other: Self) -> Self::Wide;
#[must_use]
fn abs_diff(self, other: Self) -> Self;
}
macro_rules! widening {
($($ty:ty => $wide:ty),*) => {
$(
impl WideMul for $ty {
type Wide = $wide;
fn wide_mul(self, other: Self) -> $wide {
<$wide>::from(self) * <$wide>::from(other)
}
fn abs_diff(self, other: Self) -> Self {
<$ty>::abs_diff(self, other)
}
}
)*
};
}
widening!(u8 => u16, u16 => u32, u32 => u64, u64 => u128);
impl WideMul for usize {
type Wide = u128;
fn wide_mul(self, other: Self) -> u128 {
let widen = |x: usize| u128::from(u64::try_from(x).expect("usize fits in u64"));
widen(self) * widen(other)
}
fn abs_diff(self, other: Self) -> Self {
usize::abs_diff(self, other)
}
}
impl WideMul for u128 {
type Wide = (u128, u128);
fn wide_mul(self, other: Self) -> (u128, u128) {
mul_u128(self, other)
}
fn abs_diff(self, other: Self) -> Self {
u128::abs_diff(self, other)
}
}
fn mul_u128(a: u128, b: u128) -> (u128, u128) {
const LOW: u128 = u64::MAX as u128;
let (a_hi, a_lo) = (a >> 64, a & LOW);
let (b_hi, b_lo) = (b >> 64, b & LOW);
let lo_lo = a_lo * b_lo;
let hi_lo = a_hi * b_lo;
let lo_hi = a_lo * b_hi;
let hi_hi = a_hi * b_hi;
let middle = (lo_lo >> 64) + (hi_lo & LOW) + (lo_hi & LOW);
let low = (middle << 64) | (lo_lo & LOW);
let high = hi_hi + (hi_lo >> 64) + (lo_hi >> 64) + (middle >> 64);
(high, low)
}
impl<const K: u32> WideMul for Decimal<K> {
type Wide = u128;
fn wide_mul(self, other: Self) -> u128 {
self.raw().wide_mul(other.raw())
}
fn abs_diff(self, other: Self) -> Self {
Self::from_raw(self.raw().abs_diff(other.raw()))
}
}
#[cfg(feature = "float")]
impl WideMul for NonNegF64 {
type Wide = Self;
fn wide_mul(self, other: Self) -> Self {
if self.get() == 0.0 || other.get() == 0.0 {
return Self::new(0.0).expect("zero is a valid cost");
}
Self::new(self.get() * other.get()).expect("a product of non-negative non-NaN values")
}
fn abs_diff(self, other: Self) -> Self {
if self == other {
return Self::new(0.0).expect("zero is a valid cost");
}
Self::new((self.get() - other.get()).abs()).expect("a distance of unequal values")
}
}
#[cfg(test)]
mod tests {
use super::{Decimal, WideMul, mul_u128};
use proptest::prelude::*;
#[test]
fn extremes_are_exact() {
assert_eq!(u8::MAX.wide_mul(u8::MAX), 65_025);
assert_eq!(
u64::MAX.wide_mul(u64::MAX),
u128::from(u64::MAX) * u128::from(u64::MAX)
);
assert_eq!(u128::MAX.wide_mul(u128::MAX), (u128::MAX - 1, 1));
assert_eq!(u128::MAX.wide_mul(0), (0, 0));
assert_eq!(usize::MAX.wide_mul(2), 2 * u128::from(u64::MAX));
}
#[test]
fn abs_diff_is_exact() {
assert_eq!(WideMul::abs_diff(3_u8, 250), 247);
assert_eq!(WideMul::abs_diff(u128::MAX, 0), u128::MAX);
let (a, b) = (Decimal::<1>::from_raw(5), Decimal::<1>::from_raw(12));
assert_eq!(WideMul::abs_diff(a, b), Decimal::from_raw(7));
}
#[test]
fn decimal_multiplies_raw_values() {
let a = Decimal::<2>::from_raw(150);
let b = Decimal::<2>::from_raw(u64::MAX);
assert_eq!(a.wide_mul(b), 150 * u128::from(u64::MAX));
}
#[cfg(feature = "float")]
#[test]
fn float_products() {
use crate::NonNegF64;
let inf = NonNegF64::new(f64::INFINITY).unwrap();
let zero = NonNegF64::new(0.0).unwrap();
let half = NonNegF64::new(0.5).unwrap();
assert_eq!(inf.wide_mul(zero), zero);
assert_eq!(half.wide_mul(half).get(), 0.25);
assert_eq!(inf.wide_mul(half), inf);
assert_eq!(WideMul::abs_diff(inf, inf), zero);
assert_eq!(WideMul::abs_diff(half, inf), inf);
}
proptest! {
#[test]
fn u128_matches_limb_reference(a: u128, b: u128) {
let (high, low) = mul_u128(a, b);
prop_assert_eq!(low, a.wrapping_mul(b));
if let Some(exact) = a.checked_mul(b) {
prop_assert_eq!((high, low), (0, exact));
}
let (h2, l2) = mul_u128(a >> 1, b);
let doubled = ((h2 << 1) | (l2 >> 127), l2 << 1);
let odd = if a & 1 == 1 { b } else { 0 };
let (sum_low, carry) = doubled.1.overflowing_add(odd);
prop_assert_eq!((high, low), (doubled.0 + u128::from(carry), sum_low));
}
#[test]
fn widening_matches_limb_product(a: u64, b: u64, c: u32, d: u32, e: u16, f: u16) {
prop_assert_eq!(mul_u128(a.into(), b.into()), (0, a.wide_mul(b)));
prop_assert_eq!(mul_u128(c.into(), d.into()), (0, c.wide_mul(d).into()));
prop_assert_eq!(mul_u128(e.into(), f.into()), (0, e.wide_mul(f).into()));
}
}
}