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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
macro_rules! checked_ilog {
    ($method: ident $(, $base: ident: $ty: ty)?) => {
        #[doc = doc::checked::$method!(I)]
        #[must_use = doc::must_use_op!()]
        #[inline]
        pub const fn $method(self $(, $base: $ty)?) -> Option<ExpType> {
            if self.is_negative() {
                None
            } else {
                self.bits.$method($($base)?)
            }
        }
    }
}

use crate::doc;
use crate::int::checked::tuple_to_option;
use crate::ExpType;

macro_rules! checked {
    ($BUint: ident, $BInt: ident, $Digit: ident) => {
        #[doc = doc::checked::impl_desc!()]
        impl<const N: usize> $BInt<N> {
            #[doc = doc::checked::checked_add!(I)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_add(self, rhs: Self) -> Option<Self> {
                tuple_to_option(self.overflowing_add(rhs))
            }

            #[doc = doc::checked::checked_add_unsigned!(I)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_add_unsigned(self, rhs: $BUint<N>) -> Option<Self> {
                tuple_to_option(self.overflowing_add_unsigned(rhs))
            }

            #[doc = doc::checked::checked_sub!(I)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
                tuple_to_option(self.overflowing_sub(rhs))
            }

            #[doc = doc::checked::checked_sub_unsigned!(I)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_sub_unsigned(self, rhs: $BUint<N>) -> Option<Self> {
                tuple_to_option(self.overflowing_sub_unsigned(rhs))
            }

            #[doc = doc::checked::checked_mul!(I)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
                tuple_to_option(self.overflowing_mul(rhs))
            }

            #[doc = doc::checked::checked_div!(I)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_div(self, rhs: Self) -> Option<Self> {
                if rhs.is_zero() {
                    None
                } else {
                    tuple_to_option(self.overflowing_div(rhs))
                }
            }

            #[doc = doc::checked::checked_div_euclid!(I)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
                if rhs.is_zero() {
                    None
                } else {
                    tuple_to_option(self.overflowing_div_euclid(rhs))
                }
            }

            #[doc = doc::checked::checked_rem!(I)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_rem(self, rhs: Self) -> Option<Self> {
                if rhs.is_zero() {
                    None
                } else {
                    tuple_to_option(self.overflowing_rem(rhs))
                }
            }

            #[doc = doc::checked::checked_rem_euclid!(I)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
                if rhs.is_zero() {
                    None
                } else {
                    tuple_to_option(self.overflowing_rem_euclid(rhs))
                }
            }

            #[doc = doc::checked::checked_neg!(I)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_neg(self) -> Option<Self> {
                tuple_to_option(self.overflowing_neg())
            }

            #[doc = doc::checked::checked_shl!(I)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_shl(self, rhs: ExpType) -> Option<Self> {
                tuple_to_option(self.overflowing_shl(rhs))
            }

            #[doc = doc::checked::checked_shr!(I)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_shr(self, rhs: ExpType) -> Option<Self> {
                tuple_to_option(self.overflowing_shr(rhs))
            }

            #[doc = doc::checked::checked_abs!(I)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_abs(self) -> Option<Self> {
                tuple_to_option(self.overflowing_abs())
            }

            #[doc = doc::checked::checked_pow!(I)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_pow(self, pow: ExpType) -> Option<Self> {
                match self.unsigned_abs().checked_pow(pow) {
                    Some(u) => {
                        let out = Self::from_bits(u);
                        let neg = self.is_negative();
                        if !neg || pow & 1 == 0 {
                            if out.is_negative() {
                                None
                            } else {
                                Some(out)
                            }
                        } else {
                            let out = out.wrapping_neg();
                            if !out.is_negative() {
                                None
                            } else {
                                Some(out)
                            }
                        }
                    }
                    None => None,
                }
            }

            #[doc = doc::checked::checked_next_multiple_of!(I)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
                if rhs.is_zero() {
                    return None;
                }
                let rem = self.wrapping_rem_euclid(rhs);
                if rem.is_zero() {
                    return Some(self);
                }
                if rem.is_negative() == rhs.is_negative() {
                    self.checked_add(rhs.wrapping_sub(rem))
                } else {
                    self.checked_sub(rem)
                }
            }

            #[doc = doc::checked::checked_ilog!(I)]
            #[must_use = doc::must_use_op!()]
            #[inline]
            pub const fn checked_ilog(self, base: Self) -> Option<ExpType> {
                if base.is_negative() || self.is_negative() {
                    None
                } else {
                    self.to_bits().checked_ilog(base.to_bits())
                }
            }

            checked_ilog!(checked_ilog2);
            checked_ilog!(checked_ilog10);
        }

        #[cfg(test)]
        paste::paste! {
            mod [<$Digit _digit_tests>] {
                use crate::test::types::big_types::$Digit::*;
                use crate::test::{test_bignum, types::*};

                test_bignum! {
                    function: <itest>::checked_add(a: itest, b: itest),
                    cases: [
                        (itest::MAX, -1i8)
                    ]
                }
                test_bignum! {
                    function: <itest>::checked_add_unsigned(a: itest, b: utest)
                }
                test_bignum! {
                    function: <itest>::checked_sub(a: itest, b: itest),
                    cases: [
                        (itest::MIN, -1i8)
                    ]
                }
                test_bignum! {
                    function: <itest>::checked_sub_unsigned(a: itest, b: utest)
                }
                test_bignum! {
                    function: <itest>::checked_mul(a: itest, b: itest),
                    cases: [
                        (itest::MIN, -1i8)
                    ]
                }
                test_bignum! {
                    function: <itest>::checked_div(a: itest, b: itest),
                    cases: [
                        (23098403i32 as itest, 0i8),
                        (itest::MIN, -1i8),
                        (8388600i32 as itest, 68201i32 as itest) // tests the unlikely condition in the division algorithm at step D5
                    ]
                }
                test_bignum! {
                    function: <itest>::checked_div_euclid(a: itest, b: itest),
                    cases: [
                        (itest::MIN, -1i8)
                    ]
                }
                test_bignum! {
                    function: <itest>::checked_rem(a: itest, b: itest),
                    cases: [
                        (itest::MIN, -1i8)
                    ]
                }
                test_bignum! {
                    function: <itest>::checked_rem_euclid(a: itest, b: itest),
                    skip: b <= u8::MAX as itest,
                    cases: [
                        (itest::MIN, -1i8)
                    ]
                }
                test_bignum! {
                    function: <itest>::checked_neg(a: itest),
                    cases: [
                        (itest::MIN)
                    ]
                }
                test_bignum! {
                    function: <itest>::checked_shl(a: itest, b: u16)
                }
                test_bignum! {
                    function: <itest>::checked_shr(a: itest, b: u16)
                }
                test_bignum! {
                    function: <itest>::checked_pow(a: itest, b: u16)
                }
            }
        }
    };
}

crate::macro_impl!(checked);