1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use std::intrinsics::assume;
use std::ops::{Add, AddAssign, Mul};

use num_traits::Zero;

use crate::integer::big::{NonZeroUbig, Ubig};
use crate::integer::big::ops::building_blocks::add_assign_slice;
use crate::integer::big::ops::non_zero::{add_assign, mul_non_zero};

pub(crate) mod building_blocks;
pub mod non_zero;
pub mod div;
pub mod normalize;


impl<const S: usize> Add for Ubig<S> {
    type Output = Self;

    #[must_use]
    #[inline]
    fn add(self, rhs: Self) -> Self::Output {
        let (mut left, right) = if rhs.0.len() > self.0.len() {
            (rhs, self)
        } else {
            (self, rhs)
        };

        if right.is_zero() {
            return left;
        }

        unsafe {
            // SAFETY: Last value can only become zero with overflow, which is accounted for
            let overflow = add_assign_slice(left.inner_mut(), &right);
            if overflow {
                left.inner_mut().push(1);
            }
        }

        left
    }
}


impl<const S: usize> Add for NonZeroUbig<S> {
    type Output = Self;

    #[must_use]
    #[inline]
    fn add(self, rhs: Self) -> Self::Output {
        let (mut left, right) = if rhs.0.len() > self.0.len() {
            (rhs, self)
        } else {
            (self, rhs)
        };

        left += &right;

        left
    }
}


impl<const S: usize> AddAssign<&Self> for Ubig<S> {
    #[inline]
    fn add_assign(&mut self, rhs: &Ubig<S>) {
        add_assign(&mut self.0, &rhs.0);
    }
}

impl<const S: usize> AddAssign<&Self> for NonZeroUbig<S> {
    #[inline]
    fn add_assign(&mut self, rhs: &Self) {
        unsafe {
            // SAFETY: Is non zero so not empty
            assume(!self.0.is_empty());
            assume(!rhs.0.is_empty());
        }
        add_assign(&mut self.0, &rhs.0);
    }
}

impl<const S: usize> Mul for Ubig<S> {
    type Output = Self;

    fn mul(mut self, rhs: Self) -> Self::Output {
        if !self.is_zero() && !rhs.is_zero() {
            unsafe {
                // SAFETY: Are not empty so not zero
                Self(mul_non_zero(&self.0, &rhs.0))
            }
        } else {
            self.set_zero();
            self
        }
    }
}

impl<const S: usize> Mul for NonZeroUbig<S> {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self::Output {
        unsafe {
            // SAFETY: Are not empty so not zero
            Self(mul_non_zero(&self.0, &rhs.0))
        }
    }
}

#[cfg(test)]
mod test {
    use std::str::FromStr;

    use num_traits::One;
    use smallvec::smallvec;

    use crate::{NonZeroUbig, Ubig};

    #[test]
    fn test_add() {
        assert_eq!(Ubig::<8>::from(0) + Ubig::<8>::from(0), Ubig::<8>::from(0));
        assert_eq!(Ubig::<8>::from(0) + Ubig::<8>::from(1), Ubig::<8>::from(1));
        assert_eq!(Ubig::<8>::from(1) + Ubig::<8>::from(0), Ubig::<8>::from(1));
        assert_eq!(Ubig::<8>::from(8) + Ubig::<8>::from(9), Ubig::<8>::from(17));
        assert_eq!(
            Ubig::<8>::from(usize::MAX) + Ubig::<8>::from(1),
            unsafe { Ubig::<8>::from_inner_unchecked(smallvec![0, 1]) },
        );
    }

    #[test]
    fn test_add_non_zero() {
        assert_eq!(
            NonZeroUbig::<8>::from_str("8").unwrap() + NonZeroUbig::<8>::from_str("9").unwrap(),
            NonZeroUbig::<8>::from_str("17").unwrap(),
        );
        assert_eq!(
            NonZeroUbig::<8>::from_str("18446744073709551615").unwrap() + NonZeroUbig::<8>::one(),
            NonZeroUbig::<8>::from_str("18446744073709551616").unwrap(),
        );
    }

    #[test]
    fn test_mul() {
        assert_eq!(Ubig::<8>::from(0) * Ubig::<8>::from(0), Ubig::<8>::from(0));
        assert_eq!(Ubig::<8>::from(0) * Ubig::<8>::from(1), Ubig::<8>::from(0));
        assert_eq!(Ubig::<8>::from(1) * Ubig::<8>::from(0), Ubig::<8>::from(0));
        assert_eq!(Ubig::<8>::from(8) * Ubig::<8>::from(9), Ubig::<8>::from(72));
        assert_eq!(
            Ubig::<8>::from(usize::MAX) * Ubig::<8>::from(1),
            Ubig::<8>::from(usize::MAX),
        );
    }

    #[test]
    fn test_mul_non_zero() {
        assert_eq!(
            NonZeroUbig::<8>::from_str("8").unwrap() * NonZeroUbig::<8>::from_str("9").unwrap(),
            NonZeroUbig::<8>::from_str("72").unwrap(),
        );
        assert_eq!(
            NonZeroUbig::<8>::from_str("18446744073709551615").unwrap() * NonZeroUbig::<8>::one(),
            NonZeroUbig::<8>::from_str("18446744073709551615").unwrap(),
        );
    }
}