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
use crate::int::*;
impl Int {
pub fn is_power_of_two(self) -> bool {
let ext = self.ext();
if let Some(uint) = ext.to_biguint() {
uint.count_ones() == 1
} else { false }
}
pub fn next_power_of_two(self) -> Int {
let Some(mut n) = self.ext().to_biguint() else {
return Int::ONE;
};
while n.count_ones() != 1 {
n = n + 1u32;
}
Self::wrap(ExtInt::from(n))
}
pub fn abs(self) -> Int {
if self < 0 {
self * -1i32
} else {
self
}
}
pub fn checked_div(self, other: Int) -> Option<Int> {
if other == 0 { return None; }
Some(self / other)
}
pub fn pow(self, other: Int) -> Int {
fn ext_pow(x: &ExtInt, other: &ExtInt) -> ExtInt {
use num_traits::{Zero, One};
assert!(x != &ExtInt::zero());
if other == &ExtInt::zero() {
ExtInt::one()
} else if other == &ExtInt::one() {
x.clone()
} else if other % 2 == ExtInt::zero() {
let other = other >> 1;
let a = ext_pow(x, &other);
&a * &a
} else {
let other = (other-1) >> 1;
let a = ext_pow(x, &other);
&a * &a * x
}
}
Self::wrap(ext_pow(&self.ext(), &other.ext()))
}
pub fn trailing_zeros(self) -> Option<Int> {
self.ext()
.trailing_zeros()
.map(|x| x.into())
}
pub fn div_ceil(self, other: impl Into<Int>) -> Int {
use num_integer::Integer;
Self::wrap(self.ext().div_ceil(&other.into().ext()))
}
pub fn modulo(self, signed: Signedness, size: Size) -> Int {
if size.is_zero() {
panic!("Int::modulo received invalid size zero!");
}
let m = Int::from(2).pow(size.bits());
let n = self % m;
match signed {
Unsigned if n < 0 => n + m,
Signed if n >= m/2 => n - m,
Signed if n < -m/2 => n + m,
_ => n,
}
}
pub fn in_bounds(self, signed: Signedness, size: Size) -> bool {
self == self.modulo(signed, size)
}
#[doc(hidden)]
pub fn try_to_usize(self) -> Option<usize> {
self.ext().to_usize()
}
#[doc(hidden)]
pub fn try_to_u8(self) -> Option<u8> {
self.ext().to_u8()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn in_bounds_helper(int: Int, signed: Signedness, size: Size) -> bool {
let m = Int::from(2).pow(size.bits());
let range = match signed {
Signed => -m/2..m/2,
Unsigned => Int::ZERO..m,
};
range.contains(&int)
}
fn test_modulo_helper(x: Int, signed: Signedness, size: Size) {
let out = x.modulo(signed, size);
assert!(in_bounds_helper(out, signed, size));
let delta = (out - x).abs();
assert_eq!(delta % size.bits(), 0);
}
#[test]
fn test_modulo() {
for s in [Signed, Unsigned] {
for bits in [16, 32, 64] {
let size = Size::from_bits_const(bits).unwrap();
let m = Int::from(2).pow(Int::from(bits));
for base in [-m*2, -m, Int::ZERO, m, m*2] {
for offset1 in [-m/2, Int::ZERO, m/2] {
for offset2 in [-3, -2, -1, 0, 1, 2, 3] {
let x = base + offset1 + offset2;
test_modulo_helper(x, s, size);
}
}
}
}
}
}
}