use alloc::vec::Vec;
use core::cmp::Ordering;
use core::ops::{Add, AddAssign, Mul, MulAssign};
#[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.clone();
out.subtract(other, false);
out
}
fn subtract(&mut self, other: &Self, reversed: bool) {
if self.0.len() < other.0.len() {
self.0.resize(other.0.len(), 0);
}
let mut borrow = 0_i64;
for (i, slot) in self.0.iter_mut().enumerate() {
let theirs = i64::from(other.0.get(i).copied().unwrap_or(0));
let ours = i64::from(*slot);
let (minuend, subtrahend) = if reversed {
(theirs, ours)
} else {
(ours, theirs)
};
let mut v = minuend - subtrahend - borrow;
borrow = 0;
if v < 0 {
v += 1 << 32;
borrow = 1;
}
*slot = u32::try_from(v).expect("a limb after borrowing");
}
debug_assert_eq!(borrow, 0, "the subtrahend exceeds the minuend");
self.trim();
}
fn trim(&mut self) {
while self.0.last() == Some(&0) {
self.0.pop();
}
}
fn product(a: &[u32], b: &[u32]) -> Self {
let mut out = alloc::vec![0_u32; a.len() + b.len()];
for (i, &x) in a.iter().enumerate() {
let mut carry = 0_u64;
for (j, &y) in b.iter().enumerate() {
let t = u64::from(x) * u64::from(y) + u64::from(out[i + j]) + carry;
out[i + j] = u32::try_from(t & u64::from(u32::MAX)).expect("masked");
carry = t >> 32;
}
out[i + b.len()] = u32::try_from(carry).expect("below 2^32");
}
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 AddAssign<&BigUint> for BigUint {
fn add_assign(&mut self, other: &BigUint) {
if self.0.len() < other.0.len() {
self.0.resize(other.0.len(), 0);
}
let mut carry = 0_u64;
for (i, slot) in self.0.iter_mut().enumerate() {
let s = u64::from(*slot) + u64::from(other.0.get(i).copied().unwrap_or(0)) + carry;
*slot = u32::try_from(s & u64::from(u32::MAX)).expect("masked");
carry = s >> 32;
if carry == 0 && i + 1 >= other.0.len() {
return;
}
}
if carry > 0 {
self.0.push(u32::try_from(carry).expect("a carry bit"));
}
}
}
impl AddAssign for BigUint {
fn add_assign(&mut self, other: Self) {
*self += &other;
}
}
impl Add for BigUint {
type Output = Self;
fn add(mut self, other: Self) -> Self {
self += &other;
self
}
}
impl Add<&BigUint> for BigUint {
type Output = Self;
fn add(mut self, other: &BigUint) -> Self {
self += other;
self
}
}
impl Add for &BigUint {
type Output = BigUint;
fn add(self, other: Self) -> BigUint {
let (long, short) = if self.0.len() >= other.0.len() {
(self, other)
} else {
(other, self)
};
long.clone() + short
}
}
impl MulAssign<&BigUint> for BigUint {
fn mul_assign(&mut self, other: &BigUint) {
*self = BigUint::product(&self.0, &other.0);
}
}
impl MulAssign for BigUint {
fn mul_assign(&mut self, other: Self) {
*self *= &other;
}
}
impl Mul for BigUint {
type Output = Self;
fn mul(self, other: Self) -> Self {
Self::product(&self.0, &other.0)
}
}
impl Mul<&BigUint> for BigUint {
type Output = Self;
fn mul(self, other: &BigUint) -> Self {
Self::product(&self.0, &other.0)
}
}
impl Mul for &BigUint {
type Output = BigUint;
fn mul(self, other: Self) -> BigUint {
BigUint::product(&self.0, &other.0)
}
}
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));
}
#[test]
fn by_reference_matches_owned(
a in proptest::collection::vec(any::<u32>(), 0..8),
b in proptest::collection::vec(any::<u32>(), 0..8),
) {
let (x, y) = (BigUint(a).trimmed(), BigUint(b).trimmed());
let sum = x.clone() + y.clone();
let product = x.clone() * y.clone();
prop_assert_eq!(&x + &y, sum.clone());
prop_assert_eq!(x.clone() + &y, sum.clone());
prop_assert_eq!(&x * &y, product.clone());
prop_assert_eq!(x.clone() * &y, product.clone());
let mut z = x.clone();
z += &y;
prop_assert_eq!(&z, &sum);
z = x.clone();
z += y.clone();
prop_assert_eq!(&z, &sum);
z = x.clone();
z *= &y;
prop_assert_eq!(&z, &product);
z = x.clone();
z *= y.clone();
prop_assert_eq!(&z, &product);
prop_assert_ne!(sum.limbs().last(), Some(&0));
prop_assert_eq!(sum.abs_diff(&y), x.clone());
prop_assert_eq!(y.abs_diff(&sum), x);
}
}
}