reweb3_num/buint/
wrapping.rs

1use crate::errors::option_expect;
2use crate::ExpType;
3use crate::{doc, errors};
4
5macro_rules! wrapping {
6    ($BUint: ident, $BInt: ident, $Digit: ident) => {
7        #[doc = doc::wrapping::impl_desc!()]
8        impl<const N: usize> $BUint<N> {
9            #[doc = doc::wrapping::wrapping_add!(U)]
10            #[must_use = doc::must_use_op!()]
11            #[inline]
12            pub const fn wrapping_add(self, rhs: Self) -> Self {
13                self.overflowing_add(rhs).0
14            }
15
16            #[doc = doc::wrapping::wrapping_add_signed!(U)]
17            #[must_use = doc::must_use_op!()]
18            #[inline]
19            pub const fn wrapping_add_signed(self, rhs: $BInt<N>) -> Self {
20                self.overflowing_add_signed(rhs).0
21            }
22
23            #[doc = doc::wrapping::wrapping_sub!(U)]
24            #[must_use = doc::must_use_op!()]
25            #[inline]
26            pub const fn wrapping_sub(self, rhs: Self) -> Self {
27                self.overflowing_sub(rhs).0
28            }
29
30            #[doc = doc::wrapping::wrapping_mul!(U)]
31            #[must_use = doc::must_use_op!()]
32            #[inline]
33            pub const fn wrapping_mul(self, rhs: Self) -> Self {
34                self.overflowing_mul(rhs).0
35            }
36
37            #[doc = doc::wrapping::wrapping_div!(U)]
38            #[must_use = doc::must_use_op!()]
39            #[inline]
40            pub const fn wrapping_div(self, rhs: Self) -> Self {
41                option_expect!(
42                    self.checked_div(rhs),
43                    errors::err_msg!(errors::div_by_zero_message!())
44                )
45            }
46
47            #[doc = doc::wrapping::wrapping_div_euclid!(U)]
48            #[must_use = doc::must_use_op!()]
49            #[inline]
50            pub const fn wrapping_div_euclid(self, rhs: Self) -> Self {
51                self.wrapping_div(rhs)
52            }
53
54            #[doc = doc::wrapping::wrapping_rem!(U)]
55            #[must_use = doc::must_use_op!()]
56            #[inline]
57            pub const fn wrapping_rem(self, rhs: Self) -> Self {
58                option_expect!(
59                    self.checked_rem(rhs),
60                    errors::err_msg!(errors::rem_by_zero_message!())
61                )
62            }
63
64            #[doc = doc::wrapping::wrapping_rem_euclid!(U)]
65            #[must_use = doc::must_use_op!()]
66            #[inline]
67            pub const fn wrapping_rem_euclid(self, rhs: Self) -> Self {
68                self.wrapping_rem(rhs)
69            }
70
71            #[doc = doc::wrapping::wrapping_neg!(U)]
72            #[must_use = doc::must_use_op!()]
73            #[inline]
74            pub const fn wrapping_neg(self) -> Self {
75                self.overflowing_neg().0
76            }
77
78            #[doc = doc::wrapping::wrapping_shl!(U)]
79            #[must_use = doc::must_use_op!()]
80            #[inline]
81            pub const fn wrapping_shl(self, rhs: ExpType) -> Self {
82                self.overflowing_shl(rhs).0
83            }
84
85            #[doc = doc::wrapping::wrapping_shr!(U)]
86            #[must_use = doc::must_use_op!()]
87            #[inline]
88            pub const fn wrapping_shr(self, rhs: ExpType) -> Self {
89                self.overflowing_shr(rhs).0
90            }
91
92            #[doc = doc::wrapping::wrapping_pow!(U)]
93            #[must_use = doc::must_use_op!()]
94            #[inline]
95            pub const fn wrapping_pow(mut self, mut pow: ExpType) -> Self {
96                // https://en.wikipedia.org/wiki/Exponentiation_by_squaring#Basic_method
97                if pow == 0 {
98                    return Self::ONE;
99                }
100                let mut y = Self::ONE;
101                while pow > 1 {
102                    if pow & 1 == 1 {
103                        y = self.wrapping_mul(y);
104                    }
105                    self = self.wrapping_mul(self);
106                    pow >>= 1;
107                }
108                self.wrapping_mul(y)
109            }
110
111            #[doc = doc::wrapping::wrapping_next_power_of_two!(U 256)]
112            #[must_use = doc::must_use_op!()]
113            #[inline]
114            pub const fn wrapping_next_power_of_two(self) -> Self {
115                match self.checked_next_power_of_two() {
116                    Some(int) => int,
117                    None => Self::ZERO,
118                }
119            }
120        }
121    };
122}
123
124crate::macro_impl!(wrapping);