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
use super::BUint;
use crate::digit::{self, Digit};
use crate::nightly::const_fns;
use crate::ExpType;
use crate::{doc, BInt};
#[doc = doc::overflowing::impl_desc!()]
impl<const N: usize> BUint<N> {
#[doc = doc::overflowing::overflowing_add!(U)]
#[must_use = doc::must_use_op!()]
#[inline]
pub const fn overflowing_add(self, rhs: Self) -> (Self, bool) {
let mut out = Self::ZERO;
let mut carry = false;
let mut i = 0;
while i < N {
let result = digit::carrying_add(self.digits[i], rhs.digits[i], carry);
out.digits[i] = result.0;
carry = result.1;
i += 1;
}
(out, carry)
}
#[doc = doc::overflowing::overflowing_add_signed!(U)]
#[must_use = doc::must_use_op!()]
#[inline]
pub const fn overflowing_add_signed(self, rhs: BInt<N>) -> (Self, bool) {
let (sum, overflow) = self.overflowing_add(rhs.to_bits());
(sum, rhs.is_negative() != overflow)
}
#[doc = doc::overflowing::overflowing_sub!(U)]
#[must_use = doc::must_use_op!()]
#[inline]
pub const fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
let mut out = Self::ZERO;
let mut borrow = false;
let mut i = 0;
while i < N {
let result = digit::borrowing_sub(self.digits[i], rhs.digits[i], borrow);
out.digits[i] = result.0;
borrow = result.1;
i += 1;
}
(out, borrow)
}
#[inline]
const fn long_mul(self, rhs: Self) -> (Self, bool) {
let mut overflow = false;
let mut out = Self::ZERO;
let mut carry: Digit;
let mut i = 0;
while i < N {
carry = 0;
let mut j = 0;
while j < N {
let index = i + j;
if index < N {
let (prod, c) = super::carrying_mul(
self.digits[i],
rhs.digits[j],
carry,
out.digits[index],
);
out.digits[index] = prod;
carry = c;
} else if self.digits[i] != 0 && rhs.digits[j] != 0 {
overflow = true;
break;
}
j += 1;
}
if carry != 0 {
overflow = true;
}
i += 1;
}
(out, overflow)
}
#[doc = doc::overflowing::overflowing_mul!(U)]
#[must_use = doc::must_use_op!()]
#[inline]
pub const fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
self.long_mul(rhs)
}
const_fns! {
#[doc = doc::overflowing::overflowing_div!(U)]
#[must_use = doc::must_use_op!()]
#[inline]
pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
(self.wrapping_div(rhs), false)
}
#[doc = doc::overflowing::overflowing_div_euclid!(U)]
#[must_use = doc::must_use_op!()]
#[inline]
pub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
self.overflowing_div(rhs)
}
#[doc = doc::overflowing::overflowing_rem!(U)]
#[must_use = doc::must_use_op!()]
#[inline]
pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
(self.wrapping_rem(rhs), false)
}
#[doc = doc::overflowing::overflowing_rem_euclid!(U)]
#[must_use = doc::must_use_op!()]
#[inline]
pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
self.overflowing_rem(rhs)
}
#[doc = doc::overflowing::overflowing_neg!(U)]
#[must_use = doc::must_use_op!()]
#[inline]
pub const fn overflowing_neg(self) -> (Self, bool) {
let (a, b) = (!self).overflowing_add(Self::ONE);
(a, !b)
}
#[doc = doc::overflowing::overflowing_shl!(U)]
#[must_use = doc::must_use_op!()]
#[inline]
pub const fn overflowing_shl(self, rhs: ExpType) -> (Self, bool) {
unsafe {
if rhs >= Self::BITS {
(super::unchecked_shl(self, rhs & Self::BITS_MINUS_1), true)
} else {
(super::unchecked_shl(self, rhs), false)
}
}
}
#[doc = doc::overflowing::overflowing_shr!(U)]
#[must_use = doc::must_use_op!()]
#[inline]
pub const fn overflowing_shr(self, rhs: ExpType) -> (Self, bool) {
unsafe {
if rhs >= Self::BITS {
(super::unchecked_shr(self, rhs & Self::BITS_MINUS_1), true)
} else {
(super::unchecked_shr(self, rhs), false)
}
}
}
}
#[doc = doc::overflowing::overflowing_pow!(U)]
#[must_use = doc::must_use_op!()]
#[inline]
pub const fn overflowing_pow(mut self, mut pow: ExpType) -> (Self, bool) {
if pow == 0 {
return (Self::ONE, false);
}
let mut overflow = false;
let mut y = Self::ONE;
while pow > 1 {
if pow & 1 == 1 {
let (prod, o) = y.overflowing_mul(self);
overflow |= o;
y = prod;
}
let (prod, o) = self.overflowing_mul(self);
overflow |= o;
self = prod;
pow >>= 1;
}
let (prod, o) = self.overflowing_mul(y);
(prod, o || overflow)
}
}
#[cfg(test)]
mod tests {
use crate::test::{test_bignum, types::utest};
test_bignum! {
function: <utest>::overflowing_add(a: utest, b: utest)
}
test_bignum! {
function: <utest>::overflowing_sub(a: utest, b: utest)
}
test_bignum! {
function: <utest>::overflowing_mul(a: utest, b: utest)
}
test_bignum! {
function: <utest>::overflowing_div(a: utest, b: utest),
skip: b == 0
}
test_bignum! {
function: <utest>::overflowing_div_euclid(a: utest, b: utest),
skip: b == 0
}
test_bignum! {
function: <utest>::overflowing_rem(a: utest, b: utest),
skip: b == 0
}
test_bignum! {
function: <utest>::overflowing_rem_euclid(a: utest, b: utest),
skip: b == 0
}
test_bignum! {
function: <utest>::overflowing_neg(a: utest)
}
test_bignum! {
function: <utest>::overflowing_shl(a: utest, b: u16)
}
test_bignum! {
function: <utest>::overflowing_shr(a: utest, b: u16)
}
test_bignum! {
function: <utest>::overflowing_pow(a: utest, b: u16)
}
}