-
Notifications
You must be signed in to change notification settings - Fork 214
/
big.rs
362 lines (302 loc) · 10.8 KB
/
big.rs
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//! Integers used for wide operations, larger than `u128`.
#![allow(unused)]
use crate::int::{DInt, HInt, Int, MinInt};
use core::{fmt, ops};
const WORD_LO_MASK: u64 = 0x00000000ffffffff;
const WORD_HI_MASK: u64 = 0xffffffff00000000;
const WORD_FULL_MASK: u64 = 0xffffffffffffffff;
const U128_LO_MASK: u128 = u64::MAX as u128;
const U128_HI_MASK: u128 = (u64::MAX as u128) << 64;
/// A 256-bit unsigned integer represented as 4 64-bit limbs.
///
/// Each limb is a native-endian number, but the array is little-limb-endian.
#[allow(non_camel_case_types)]
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
pub struct u256(pub [u64; 4]);
impl u256 {
pub const MAX: Self = Self([u64::MAX, u64::MAX, u64::MAX, u64::MAX]);
/// Reinterpret as a signed integer
pub fn signed(self) -> i256 {
i256(self.0)
}
}
/// A 256-bit signed integer represented as 4 64-bit limbs.
///
/// Each limb is a native-endian number, but the array is little-limb-endian.
#[allow(non_camel_case_types)]
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
pub struct i256(pub [u64; 4]);
impl i256 {
/// Reinterpret as an unsigned integer
pub fn unsigned(self) -> u256 {
u256(self.0)
}
}
impl MinInt for u256 {
type OtherSign = i256;
type UnsignedInt = u256;
const SIGNED: bool = false;
const BITS: u32 = 256;
const ZERO: Self = Self([0u64; 4]);
const ONE: Self = Self([1, 0, 0, 0]);
const MIN: Self = Self([0u64; 4]);
const MAX: Self = Self([u64::MAX; 4]);
}
impl MinInt for i256 {
type OtherSign = u256;
type UnsignedInt = u256;
const SIGNED: bool = false;
const BITS: u32 = 256;
const ZERO: Self = Self([0u64; 4]);
const ONE: Self = Self([1, 0, 0, 0]);
const MIN: Self = Self([0, 0, 0, 1 << 63]);
const MAX: Self = Self([u64::MAX, u64::MAX, u64::MAX, u64::MAX << 1]);
}
// impl Int for i256 {
// fn is_zero(self) -> bool {
// self == Self::ZERO
// }
// fn wrapping_neg(self) -> Self {
// Self::ZERO.wrapping_sub(self)
// }
// fn wrapping_add(self, other: Self) -> Self {
// self.overflowing_add(other).0
// }
//
// fn overflowing_add(self, other: Self) -> (Self, bool) {
// let x0 = (u128::from(self.0[0])).wrapping_add(u128::from(other.0[0]));
// let v0 = x0 as u64;
// let c0 = x0 >> 64;
// let x1 = (u128::from(self.0[1]))
// .wrapping_add(u128::from(other.0[1]))
// .wrapping_add(c0);
// let v1 = x1 as u64;
// let c1 = x1 >> 64;
// let x2 = (u128::from(self.0[2]))
// .wrapping_add(u128::from(other.0[2]))
// .wrapping_add(c1);
// let v2 = x2 as u64;
// let c2 = x2 >> 64;
// let x3 = (u128::from(self.0[3]))
// .wrapping_add(u128::from(other.0[3]))
// .wrapping_add(c2);
// let v3 = x3 as u64;
// let c3 = x3 >> 64;
// (Self([v0, v1, v2, v3]), c3 > 0)
// }
// }
macro_rules! impl_common {
($ty:ty) => {
// impl ops::Add for $ty {
// type Output = Self;
// fn add(self, rhs: Self) -> Self::Output {
// let (val, wrapped) = self.overflowing_add(rhs);
// debug_assert!(!wrapped, "attempted to add with overflow");
// val
// }
// }
// impl ops::AddAssign for $ty {
// fn add_assign(&mut self, rhs: Self) {
// *self = *self + rhs
// }
// }
// impl ops::BitAnd for $ty {
// type Output = Self;
// fn bitand(self, rhs: Self) -> Self::Output {
// Self([
// self.0[0] & rhs.0[0],
// self.0[1] & rhs.0[1],
// self.0[2] & rhs.0[2],
// self.0[3] & rhs.0[3],
// ])
// }
// }
// impl ops::BitAndAssign for $ty {
// fn bitand_assign(&mut self, rhs: Self) {
// *self = *self & rhs
// }
// }
impl ops::BitOr for $ty {
type Output = Self;
fn bitor(mut self, rhs: Self) -> Self::Output {
self.0[0] |= rhs.0[0];
self.0[1] |= rhs.0[1];
self.0[2] |= rhs.0[2];
self.0[3] |= rhs.0[3];
self
}
}
// impl ops::BitOrAssign for $ty {
// fn bitor_assign(&mut self, rhs: Self) {
// *self = *self | rhs
// }
// }
// impl ops::BitXor for $ty {
// type Output = Self;
// fn bitxor(self, rhs: Self) -> Self::Output {
// Self([
// self.0[0] ^ rhs.0[0],
// self.0[1] ^ rhs.0[1],
// self.0[2] ^ rhs.0[2],
// self.0[3] ^ rhs.0[3],
// ])
// }
// }
// impl ops::BitXorAssign for $ty {
// fn bitxor_assign(&mut self, rhs: Self) {
// *self = *self ^ rhs
// }
// }
impl ops::Not for $ty {
type Output = Self;
fn not(self) -> Self::Output {
Self([!self.0[0], !self.0[1], !self.0[2], !self.0[3]])
}
}
impl ops::Shl<u32> for $ty {
type Output = Self;
fn shl(self, rhs: u32) -> Self::Output {
todo!()
}
}
};
}
impl_common!(i256);
impl_common!(u256);
macro_rules! word {
(1, $val:expr) => {
(($val >> (32 * 3)) & Self::from(WORD_LO_MASK)) as u64
};
(2, $val:expr) => {
(($val >> (32 * 2)) & Self::from(WORD_LO_MASK)) as u64
};
(3, $val:expr) => {
(($val >> (32 * 1)) & Self::from(WORD_LO_MASK)) as u64
};
(4, $val:expr) => {
(($val >> (32 * 0)) & Self::from(WORD_LO_MASK)) as u64
};
}
impl HInt for u128 {
type D = u256;
fn widen(self) -> Self::D {
let w0 = self & u128::from(u64::MAX);
let w1 = (self >> u64::BITS) & u128::from(u64::MAX);
u256([w0 as u64, w1 as u64, 0, 0])
}
fn zero_widen(self) -> Self::D {
self.widen()
}
fn zero_widen_mul(self, rhs: Self) -> Self::D {
let product11: u64 = word!(1, self) * word!(1, rhs);
let product12: u64 = word!(1, self) * word!(2, rhs);
let product13: u64 = word!(1, self) * word!(3, rhs);
let product14: u64 = word!(1, self) * word!(4, rhs);
let product21: u64 = word!(2, self) * word!(1, rhs);
let product22: u64 = word!(2, self) * word!(2, rhs);
let product23: u64 = word!(2, self) * word!(3, rhs);
let product24: u64 = word!(2, self) * word!(4, rhs);
let product31: u64 = word!(3, self) * word!(1, rhs);
let product32: u64 = word!(3, self) * word!(2, rhs);
let product33: u64 = word!(3, self) * word!(3, rhs);
let product34: u64 = word!(3, self) * word!(4, rhs);
let product41: u64 = word!(4, self) * word!(1, rhs);
let product42: u64 = word!(4, self) * word!(2, rhs);
let product43: u64 = word!(4, self) * word!(3, rhs);
let product44: u64 = word!(4, self) * word!(4, rhs);
let sum0: u128 = u128::from(product44);
let sum1: u128 = u128::from(product34) + u128::from(product43);
let sum2: u128 = u128::from(product24) + u128::from(product33) + u128::from(product42);
let sum3: u128 = u128::from(product14)
+ u128::from(product23)
+ u128::from(product32)
+ u128::from(product41);
let sum4: u128 = u128::from(product13) + u128::from(product22) + u128::from(product31);
let sum5: u128 = u128::from(product12) + u128::from(product21);
let sum6: u128 = u128::from(product11);
let r0: u128 =
(sum0 & u128::from(WORD_FULL_MASK)) + ((sum1 & u128::from(WORD_LO_MASK)) << 32);
let r1: u128 = (sum0 >> 64)
+ ((sum1 >> 32) & u128::from(WORD_FULL_MASK))
+ (sum2 & u128::from(WORD_FULL_MASK))
+ ((sum3 << 32) & u128::from(WORD_HI_MASK));
let lo = r0.wrapping_add(r1 << 64);
let hi = (r1 >> 64)
+ (sum1 >> 96)
+ (sum2 >> 64)
+ (sum3 >> 32)
+ sum4
+ (sum5 << 32)
+ (sum6 << 64);
u256([
(lo & U128_LO_MASK) as u64,
((lo >> 64) & U128_LO_MASK) as u64,
(hi & U128_LO_MASK) as u64,
((hi >> 64) & U128_LO_MASK) as u64,
])
}
fn widen_mul(self, rhs: Self) -> Self::D {
self.zero_widen_mul(rhs)
}
}
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.0[2] = u64::MAX;
ret.0[3] = u64::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 {
let mut res = self.zero_widen_mul(rhs);
if self.is_negative() ^ rhs.is_negative() {
for word in res.0.iter_mut().rev() {
let zeroes = word.leading_zeros();
let leading = u64::MAX << (64 - zeroes);
*word |= leading;
if zeroes != 64 {
break;
}
}
}
res
}
}
impl DInt for u256 {
type H = u128;
fn lo(self) -> Self::H {
let mut tmp = [0u8; 16];
tmp[..8].copy_from_slice(&self.0[0].to_le_bytes());
tmp[8..].copy_from_slice(&self.0[1].to_le_bytes());
u128::from_le_bytes(tmp)
}
fn hi(self) -> Self::H {
let mut tmp = [0u8; 16];
tmp[..8].copy_from_slice(&self.0[2].to_le_bytes());
tmp[8..].copy_from_slice(&self.0[3].to_le_bytes());
u128::from_le_bytes(tmp)
}
}
impl DInt for i256 {
type H = i128;
fn lo(self) -> Self::H {
let mut tmp = [0u8; 16];
tmp[..8].copy_from_slice(&self.0[0].to_le_bytes());
tmp[8..].copy_from_slice(&self.0[1].to_le_bytes());
i128::from_le_bytes(tmp)
}
fn hi(self) -> Self::H {
let mut tmp = [0u8; 16];
tmp[..8].copy_from_slice(&self.0[2].to_le_bytes());
tmp[8..].copy_from_slice(&self.0[3].to_le_bytes());
i128::from_le_bytes(tmp)
}
}