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
|
//! Integers used for wide operations, larger than `u128`.
#[cfg(test)]
mod tests;
use core::ops;
use super::{DInt, HInt, Int, MinInt};
const U128_LO_MASK: u128 = u64::MAX as u128;
/// A 256-bit unsigned integer represented as two 128-bit native-endian limbs.
#[allow(non_camel_case_types)]
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
pub struct u256 {
pub lo: u128,
pub hi: u128,
}
impl u256 {
#[cfg(any(test, feature = "unstable-public-internals"))]
pub const MAX: Self = Self {
lo: u128::MAX,
hi: u128::MAX,
};
/// Reinterpret as a signed integer
pub fn signed(self) -> i256 {
i256 {
lo: self.lo,
hi: self.hi,
}
}
}
/// A 256-bit signed integer represented as two 128-bit native-endian limbs.
#[allow(non_camel_case_types)]
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
pub struct i256 {
pub lo: u128,
pub hi: u128,
}
impl i256 {
/// Reinterpret as an unsigned integer
#[cfg(any(test, feature = "unstable-public-internals"))]
pub fn unsigned(self) -> u256 {
u256 {
lo: self.lo,
hi: self.hi,
}
}
}
impl MinInt for u256 {
type OtherSign = i256;
type Unsigned = u256;
const SIGNED: bool = false;
const BITS: u32 = 256;
const ZERO: Self = Self { lo: 0, hi: 0 };
const ONE: Self = Self { lo: 1, hi: 0 };
const MIN: Self = Self { lo: 0, hi: 0 };
const MAX: Self = Self {
lo: u128::MAX,
hi: u128::MAX,
};
}
impl MinInt for i256 {
type OtherSign = u256;
type Unsigned = u256;
const SIGNED: bool = false;
const BITS: u32 = 256;
const ZERO: Self = Self { lo: 0, hi: 0 };
const ONE: Self = Self { lo: 1, hi: 0 };
const MIN: Self = Self {
lo: 0,
hi: 1 << 127,
};
const MAX: Self = Self {
lo: u128::MAX,
hi: u128::MAX >> 1,
};
}
macro_rules! impl_common {
($ty:ty) => {
impl ops::BitOr for $ty {
type Output = Self;
fn bitor(mut self, rhs: Self) -> Self::Output {
self.lo |= rhs.lo;
self.hi |= rhs.hi;
self
}
}
impl ops::Not for $ty {
type Output = Self;
fn not(mut self) -> Self::Output {
self.lo = !self.lo;
self.hi = !self.hi;
self
}
}
impl ops::Shl<u32> for $ty {
type Output = Self;
fn shl(self, _rhs: u32) -> Self::Output {
unimplemented!("only used to meet trait bounds")
}
}
};
}
impl_common!(i256);
impl_common!(u256);
impl ops::Add<Self> for u256 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
let (lo, carry) = self.lo.overflowing_add(rhs.lo);
let hi = self.hi.wrapping_add(carry as u128).wrapping_add(rhs.hi);
Self { lo, hi }
}
}
impl ops::Shr<u32> for u256 {
type Output = Self;
fn shr(mut self, rhs: u32) -> Self::Output {
debug_assert!(rhs < Self::BITS, "attempted to shift right with overflow");
if rhs >= Self::BITS {
return Self::ZERO;
}
if rhs == 0 {
return self;
}
if rhs < 128 {
self.lo >>= rhs;
self.lo |= self.hi << (128 - rhs);
} else {
self.lo = self.hi >> (rhs - 128);
}
if rhs < 128 {
self.hi >>= rhs;
} else {
self.hi = 0;
}
self
}
}
impl HInt for u128 {
type D = u256;
fn widen(self) -> Self::D {
u256 { lo: self, hi: 0 }
}
fn zero_widen(self) -> Self::D {
self.widen()
}
fn zero_widen_mul(self, rhs: Self) -> Self::D {
let l0 = self & U128_LO_MASK;
let l1 = rhs & U128_LO_MASK;
let h0 = self >> 64;
let h1 = rhs >> 64;
let p_ll: u128 = l0.overflowing_mul(l1).0;
let p_lh: u128 = l0.overflowing_mul(h1).0;
let p_hl: u128 = h0.overflowing_mul(l1).0;
let p_hh: u128 = h0.overflowing_mul(h1).0;
let s0 = p_hl + (p_ll >> 64);
let s1 = (p_ll & U128_LO_MASK) + (s0 << 64);
let s2 = p_lh + (s1 >> 64);
let lo = (p_ll & U128_LO_MASK) + (s2 << 64);
let hi = p_hh + (s0 >> 64) + (s2 >> 64);
u256 { lo, hi }
}
fn widen_mul(self, rhs: Self) -> Self::D {
self.zero_widen_mul(rhs)
}
fn widen_hi(self) -> Self::D {
self.widen() << <Self as MinInt>::BITS
}
}
impl HInt for i128 {
type D = i256;
fn widen(self) -> Self::D {
let mut ret = self.unsigned().zero_widen().signed();
if self.is_negative() {
ret.hi = u128::MAX;
}
ret
}
fn zero_widen(self) -> Self::D {
self.unsigned().zero_widen().signed()
}
fn zero_widen_mul(self, rhs: Self) -> Self::D {
self.unsigned().zero_widen_mul(rhs.unsigned()).signed()
}
fn widen_mul(self, _rhs: Self) -> Self::D {
unimplemented!("signed i128 widening multiply is not used")
}
fn widen_hi(self) -> Self::D {
self.widen() << <Self as MinInt>::BITS
}
}
impl DInt for u256 {
type H = u128;
fn lo(self) -> Self::H {
self.lo
}
fn hi(self) -> Self::H {
self.hi
}
}
impl DInt for i256 {
type H = i128;
fn lo(self) -> Self::H {
self.lo as i128
}
fn hi(self) -> Self::H {
self.hi as i128
}
}
|