reweb3_num/buint/
ops.rs

1use crate::digit;
2use crate::nightly::impl_const;
3use crate::ExpType;
4use core::ops::{
5    Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
6    Mul, MulAssign, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
7};
8
9macro_rules! ops {
10    ($BUint: ident, $BInt: ident, $Digit: ident) => {
11        impl_const! {
12            impl<const N: usize> const Add<$Digit> for $BUint<N> {
13                type Output = Self;
14
15                #[inline]
16                fn add(self, rhs: $Digit) -> Self {
17                    let mut out = self;
18                    let result = digit::$Digit::carrying_add(out.digits[0], rhs, false);
19                    out.digits[0] = result.0;
20                    let mut carry = result.1;
21                    let mut i = 1;
22                    while i < N && carry {
23                        let result = out.digits[i].overflowing_add(1);
24                        out.digits[i] = result.0;
25                        carry = result.1;
26                        i += 1;
27                    }
28                    out
29                }
30            }
31        }
32
33        impl_const! {
34            impl<const N: usize> const BitAnd for $BUint<N> {
35                type Output = Self;
36
37                #[inline]
38                fn bitand(self, rhs: Self) -> Self {
39                    Self::bitand(self, rhs)
40                }
41            }
42        }
43
44        impl_const! {
45            impl<const N: usize> const BitOr for $BUint<N> {
46                type Output = Self;
47
48                #[inline]
49                fn bitor(self, rhs: Self) -> Self {
50                    Self::bitor(self, rhs)
51                }
52            }
53        }
54
55        impl_const! {
56            impl<const N: usize> const BitXor for $BUint<N> {
57                type Output = Self;
58
59                #[inline]
60                fn bitxor(self, rhs: Self) -> Self {
61                    Self::bitxor(self, rhs)
62                }
63            }
64        }
65
66        impl_const! {
67            impl<const N: usize> const Div for $BUint<N> {
68                type Output = Self;
69
70                #[inline]
71                fn div(self, rhs: Self) -> Self {
72                    Self::div(self, rhs)
73                }
74            }
75        }
76
77        impl_const! {
78            impl<const N: usize> const Div<$Digit> for $BUint<N> {
79                type Output = Self;
80
81                #[inline]
82                fn div(self, rhs: $Digit) -> Self {
83                    self.div_rem_digit(rhs).0
84                }
85            }
86        }
87
88        impl_const! {
89            impl<const N: usize> const Not for $BUint<N> {
90                type Output = Self;
91
92                #[inline]
93                fn not(self) -> Self {
94                    Self::not(self)
95                }
96            }
97        }
98
99        impl_const! {
100            impl<const N: usize> const Rem for $BUint<N> {
101                type Output = Self;
102
103                #[inline]
104                fn rem(self, rhs: Self) -> Self {
105                    Self::rem(self, rhs)
106                }
107            }
108        }
109
110        impl_const! {
111            impl<const N: usize> const Rem<$Digit> for $BUint<N> {
112                type Output = $Digit;
113
114                #[inline]
115                fn rem(self, rhs: $Digit) -> $Digit {
116                    self.div_rem_digit(rhs).1
117                }
118            }
119        }
120
121        crate::int::ops::impls!($BUint, $BUint, $BInt);
122    };
123}
124
125crate::macro_impl!(ops);