use alloc::vec::Vec;
use core::cmp::Ordering;
use core::ops::{Add, Mul};
#[cfg(feature = "float")]
use super::NonNegF64;
use super::{Cost, Decimal};
mod signed;
pub use signed::BigInt;
pub trait ExactNumber: Clone + Ord + Add<Output = Self> + Mul<Output = Self> {
fn zero() -> Self;
fn one() -> Self;
#[must_use]
fn abs_diff(&self, other: &Self) -> Self;
}
pub trait ExactArith: Cost {
type Exact: ExactNumber;
#[must_use]
fn exact(self) -> Self::Exact;
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
pub struct BigUint(Vec<u32>);
impl BigUint {
#[must_use]
pub fn limbs(&self) -> &[u32] {
&self.0
}
fn trimmed(mut self) -> Self {
while self.0.last() == Some(&0) {
self.0.pop();
}
self
}
fn minus(&self, other: &Self) -> Self {
let mut out = self.0.clone();
let mut borrow = 0_i64;
for (i, slot) in out.iter_mut().enumerate() {
let sub = i64::from(other.0.get(i).copied().unwrap_or(0)) + borrow;
let mut v = i64::from(*slot) - sub;
borrow = 0;
if v < 0 {
v += 1 << 32;
borrow = 1;
}
*slot = u32::try_from(v).expect("a limb after borrowing");
}
Self(out).trimmed()
}
}
impl From<u128> for BigUint {
fn from(mut value: u128) -> Self {
let mut limbs = Vec::new();
while value > 0 {
limbs.push(u32::try_from(value & u128::from(u32::MAX)).expect("masked"));
value >>= 32;
}
Self(limbs)
}
}
impl Add for BigUint {
type Output = Self;
fn add(self, other: Self) -> Self {
let n = self.0.len().max(other.0.len());
let mut out = Vec::with_capacity(n + 1);
let mut carry = 0_u64;
for i in 0..n {
let s = u64::from(self.0.get(i).copied().unwrap_or(0))
+ u64::from(other.0.get(i).copied().unwrap_or(0))
+ carry;
out.push(u32::try_from(s & u64::from(u32::MAX)).expect("masked"));
carry = s >> 32;
}
out.push(u32::try_from(carry).expect("a carry bit"));
Self(out).trimmed()
}
}
impl Mul for BigUint {
type Output = Self;
fn mul(self, other: Self) -> Self {
let mut out = alloc::vec![0_u32; self.0.len() + other.0.len()];
for (i, &a) in self.0.iter().enumerate() {
let mut carry = 0_u64;
for (j, &b) in other.0.iter().enumerate() {
let t = u64::from(a) * u64::from(b) + u64::from(out[i + j]) + carry;
out[i + j] = u32::try_from(t & u64::from(u32::MAX)).expect("masked");
carry = t >> 32;
}
out[i + other.0.len()] = u32::try_from(carry).expect("below 2^32");
}
Self(out).trimmed()
}
}
impl PartialOrd for BigUint {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for BigUint {
fn cmp(&self, other: &Self) -> Ordering {
self.0
.len()
.cmp(&other.0.len())
.then_with(|| self.0.iter().rev().cmp(other.0.iter().rev()))
}
}
impl ExactNumber for BigUint {
fn zero() -> Self {
Self(Vec::new())
}
fn one() -> Self {
Self(alloc::vec![1])
}
fn abs_diff(&self, other: &Self) -> Self {
if self >= other {
self.minus(other)
} else {
other.minus(self)
}
}
}
macro_rules! exact_unsigned {
($($ty:ty),*) => {
$(
impl ExactArith for $ty {
type Exact = BigUint;
fn exact(self) -> BigUint {
BigUint::from(u128::from(self))
}
}
)*
};
}
exact_unsigned!(u8, u16, u32, u64, u128);
impl ExactArith for usize {
type Exact = BigUint;
fn exact(self) -> BigUint {
BigUint::from(u128::from(u64::try_from(self).expect("usize fits in u64")))
}
}
impl<const K: u32> ExactArith for Decimal<K> {
type Exact = BigUint;
fn exact(self) -> BigUint {
BigUint::from(u128::from(self.raw()))
}
}
#[cfg(feature = "float")]
impl ExactNumber for NonNegF64 {
fn zero() -> Self {
Self::new(0.0).expect("zero is a valid cost")
}
fn one() -> Self {
Self::new(1.0).expect("one is a valid cost")
}
fn abs_diff(&self, other: &Self) -> Self {
crate::WideMul::abs_diff(*self, *other)
}
}
#[cfg(feature = "float")]
impl ExactArith for NonNegF64 {
type Exact = Self;
fn exact(self) -> Self {
self
}
}
#[cfg(test)]
mod tests {
use proptest::prelude::*;
use super::{BigUint, ExactArith, ExactNumber};
fn value(b: &BigUint) -> u128 {
b.limbs()
.iter()
.rev()
.fold(0, |v, &l| (v << 32) | u128::from(l))
}
#[test]
fn zero() {
assert_eq!(BigUint::zero().limbs(), &[] as &[u32]);
assert_eq!(BigUint::zero() + 5_u64.exact(), 5_u64.exact());
assert_eq!(BigUint::zero() * 5_u64.exact(), BigUint::zero());
assert_eq!(BigUint::one() * 5_u64.exact(), 5_u64.exact());
}
proptest! {
#[test]
fn matches_u128(a: u64, b: u64, c: u32) {
let (x, y, z) = (a.exact(), b.exact(), c.exact());
prop_assert_eq!(value(&(x.clone() + y.clone())), u128::from(a) + u128::from(b));
prop_assert_eq!(value(&(x.clone() * y.clone())), u128::from(a) * u128::from(b));
prop_assert_eq!(value(&x.abs_diff(&y)), u128::from(a.abs_diff(b)));
prop_assert_eq!(x.cmp(&y), a.cmp(&b));
prop_assert_eq!((x.clone() * y.clone()) * z.clone(), x * (y * z));
}
}
}