bnum/bint/
wrapping.rs

1use crate::{doc, ExpType};
2
3macro_rules! wrapping {
4    ($BUint: ident, $BInt: ident, $Digit: ident) => {
5        #[doc = doc::wrapping::impl_desc!()]
6        impl<const N: usize> $BInt<N> {
7            #[doc = doc::wrapping::wrapping_add!(I)]
8            #[must_use = doc::must_use_op!()]
9            #[inline]
10            pub const fn wrapping_add(self, rhs: Self) -> Self {
11                Self::from_bits(self.bits.wrapping_add(rhs.bits))
12            }
13
14            #[doc = doc::wrapping::wrapping_add_unsigned!(I)]
15            #[must_use = doc::must_use_op!()]
16            #[inline]
17            pub const fn wrapping_add_unsigned(self, rhs: $BUint<N>) -> Self {
18                self.overflowing_add_unsigned(rhs).0
19            }
20
21            #[doc = doc::wrapping::wrapping_sub!(I)]
22            #[must_use = doc::must_use_op!()]
23            #[inline]
24            pub const fn wrapping_sub(self, rhs: Self) -> Self {
25                Self::from_bits(self.bits.wrapping_sub(rhs.bits))
26            }
27
28            #[doc = doc::wrapping::wrapping_sub_unsigned!(I)]
29            #[must_use = doc::must_use_op!()]
30            #[inline]
31            pub const fn wrapping_sub_unsigned(self, rhs: $BUint<N>) -> Self {
32                self.overflowing_sub_unsigned(rhs).0
33            }
34
35            #[doc = doc::wrapping::wrapping_mul!(I)]
36            #[must_use = doc::must_use_op!()]
37            #[inline]
38            pub const fn wrapping_mul(self, rhs: Self) -> Self {
39                Self::from_bits(self.bits.wrapping_mul(rhs.bits))
40            }
41
42            #[doc = doc::wrapping::wrapping_div!(I)]
43            #[must_use = doc::must_use_op!()]
44            #[inline]
45            pub const fn wrapping_div(self, rhs: Self) -> Self {
46                self.overflowing_div(rhs).0
47            }
48
49            #[doc = doc::wrapping::wrapping_div_euclid!(I)]
50            #[must_use = doc::must_use_op!()]
51            #[inline]
52            pub const fn wrapping_div_euclid(self, rhs: Self) -> Self {
53                self.overflowing_div_euclid(rhs).0
54            }
55
56            #[doc = doc::wrapping::wrapping_rem!(I)]
57            #[must_use = doc::must_use_op!()]
58            #[inline]
59            pub const fn wrapping_rem(self, rhs: Self) -> Self {
60                self.overflowing_rem(rhs).0
61            }
62
63            #[doc = doc::wrapping::wrapping_rem_euclid!(I)]
64            #[must_use = doc::must_use_op!()]
65            #[inline]
66            pub const fn wrapping_rem_euclid(self, rhs: Self) -> Self {
67                self.overflowing_rem_euclid(rhs).0
68            }
69
70            #[doc = doc::wrapping::wrapping_neg!(I)]
71            #[must_use = doc::must_use_op!()]
72            #[inline]
73            pub const fn wrapping_neg(self) -> Self {
74                self.overflowing_neg().0
75            }
76
77            #[doc = doc::wrapping::wrapping_shl!(I)]
78            #[must_use = doc::must_use_op!()]
79            #[inline]
80            pub const fn wrapping_shl(self, rhs: ExpType) -> Self {
81                self.overflowing_shl(rhs).0
82            }
83
84            #[doc = doc::wrapping::wrapping_shr!(I)]
85            #[must_use = doc::must_use_op!()]
86            #[inline]
87            pub const fn wrapping_shr(self, rhs: ExpType) -> Self {
88                self.overflowing_shr(rhs).0
89            }
90
91            #[doc = doc::wrapping::wrapping_abs!(I)]
92            #[must_use = doc::must_use_op!()]
93            #[inline]
94            pub const fn wrapping_abs(self) -> Self {
95                self.overflowing_abs().0
96            }
97
98            #[doc = doc::wrapping::wrapping_pow!(I)]
99            #[must_use = doc::must_use_op!()]
100            #[inline]
101            pub const fn wrapping_pow(self, pow: ExpType) -> Self {
102                // as wrapping_mul for signed and unsigned is the same
103                Self::from_bits(self.bits.wrapping_pow(pow))
104            }
105        }
106    };
107}
108
109#[cfg(test)]
110crate::test::all_digit_tests! {
111    use crate::test::{test_bignum, types::{itest, utest}};
112
113    test_bignum! {
114        function: <itest>::wrapping_add(a: itest, b: itest)
115    }
116    test_bignum! {
117        function: <itest>::wrapping_add_unsigned(a: itest, b: utest)
118    }
119    test_bignum! {
120        function: <itest>::wrapping_sub(a: itest, b: itest)
121    }
122    test_bignum! {
123        function: <itest>::wrapping_sub_unsigned(a: itest, b: utest)
124    }
125    test_bignum! {
126        function: <itest>::wrapping_mul(a: itest, b: itest)
127    }
128    test_bignum! {
129        function: <itest>::wrapping_div(a: itest, b: itest),
130        skip: b == 0
131    }
132    test_bignum! {
133        function: <itest>::wrapping_div_euclid(a: itest, b: itest),
134        skip: b == 0
135    }
136    test_bignum! {
137        function: <itest>::wrapping_rem(a: itest, b: itest),
138        skip: b == 0,
139        cases: [
140            (itest::MIN, -1i8),
141            (185892231884832768i64 as itest, 92946115942416385i64 as itest)
142        ]
143    }
144    test_bignum! {
145        function: <itest>::wrapping_rem_euclid(a: itest, b: itest),
146        skip: b == 0
147    }
148    test_bignum! {
149        function: <itest>::wrapping_neg(a: itest),
150        cases: [
151            (itest::MIN)
152        ]
153    }
154    test_bignum! {
155        function: <itest>::wrapping_shl(a: itest, b: u16)
156    }
157    test_bignum! {
158        function: <itest>::wrapping_shr(a: itest, b: u16)
159    }
160    test_bignum! {
161        function: <itest>::wrapping_abs(a: itest)
162    }
163    test_bignum! {
164        function: <itest>::wrapping_pow(a: itest, b: u16)
165    }
166}
167
168crate::macro_impl!(wrapping);