Skip to content

Commit 4a69a34

Browse files
committed
Bigint updates
1 parent 29e0670 commit 4a69a34

File tree

2 files changed

+46
-61
lines changed

2 files changed

+46
-61
lines changed

src/int/big.rs

+34-46
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,17 @@ macro_rules! impl_common {
136136
// }
137137
// }
138138

139-
// impl ops::BitOr for $ty {
140-
// type Output = Self;
139+
impl ops::BitOr for $ty {
140+
type Output = Self;
141141

142-
// fn bitor(self, rhs: Self) -> Self::Output {
143-
// Self([
144-
// self.0[0] | rhs.0[0],
145-
// self.0[1] | rhs.0[1],
146-
// self.0[2] | rhs.0[2],
147-
// self.0[3] | rhs.0[3],
148-
// ])
149-
// }
150-
// }
142+
fn bitor(mut self, rhs: Self) -> Self::Output {
143+
self.0[0] |= rhs.0[0];
144+
self.0[1] |= rhs.0[1];
145+
self.0[2] |= rhs.0[2];
146+
self.0[3] |= rhs.0[3];
147+
self
148+
}
149+
}
151150

152151
// impl ops::BitOrAssign for $ty {
153152
// fn bitor_assign(&mut self, rhs: Self) {
@@ -181,19 +180,20 @@ macro_rules! impl_common {
181180
Self([!self.0[0], !self.0[1], !self.0[2], !self.0[3]])
182181
}
183182
}
183+
184+
impl ops::Shl<u32> for $ty {
185+
type Output = Self;
186+
187+
fn shl(self, rhs: u32) -> Self::Output {
188+
todo!()
189+
}
190+
}
184191
};
185192
}
186193

187194
impl_common!(i256);
188195
impl_common!(u256);
189196

190-
impl ops::Shl<u32> for u256 {
191-
type Output = Self;
192-
193-
fn shl(self, rhs: u32) -> Self::Output {
194-
todo!()
195-
}
196-
}
197197

198198
macro_rules! word {
199199
(1, $val:expr) => {
@@ -223,10 +223,6 @@ impl HInt for u128 {
223223
self.widen()
224224
}
225225

226-
fn widen_hi(self) -> Self::D {
227-
self.widen() << 128
228-
}
229-
230226
fn zero_widen_mul(self, rhs: Self) -> Self::D {
231227
let product11: u64 = word!(1, self) * word!(1, rhs);
232228
let product12: u64 = word!(1, self) * word!(2, rhs);
@@ -293,10 +289,6 @@ impl HInt for i128 {
293289
self.unsigned().zero_widen().as_signed()
294290
}
295291

296-
fn widen_hi(self) -> Self::D {
297-
todo!()
298-
}
299-
300292
fn zero_widen_mul(self, rhs: Self) -> Self::D {
301293
self.unsigned().zero_widen_mul(rhs.unsigned()).as_signed()
302294
}
@@ -323,38 +315,34 @@ impl DInt for u256 {
323315
type H = u128;
324316

325317
fn lo(self) -> Self::H {
326-
todo!()
318+
let mut tmp = [0u8; 16];
319+
tmp[..8].copy_from_slice(&self.0[0].to_le_bytes());
320+
tmp[8..].copy_from_slice(&self.0[1].to_le_bytes());
321+
u128::from_le_bytes(tmp)
327322
}
328323

329324
fn hi(self) -> Self::H {
330-
todo!()
331-
}
332-
333-
fn lo_hi(self) -> (Self::H, Self::H) {
334-
todo!()
335-
}
336-
337-
fn from_lo_hi(lo: Self::H, hi: Self::H) -> Self {
338-
todo!()
325+
let mut tmp = [0u8; 16];
326+
tmp[..8].copy_from_slice(&self.0[2].to_le_bytes());
327+
tmp[8..].copy_from_slice(&self.0[3].to_le_bytes());
328+
u128::from_le_bytes(tmp)
339329
}
340330
}
341331

342332
impl DInt for i256 {
343333
type H = i128;
344334

345335
fn lo(self) -> Self::H {
346-
todo!()
336+
let mut tmp = [0u8; 16];
337+
tmp[..8].copy_from_slice(&self.0[0].to_le_bytes());
338+
tmp[8..].copy_from_slice(&self.0[1].to_le_bytes());
339+
i128::from_le_bytes(tmp)
347340
}
348341

349342
fn hi(self) -> Self::H {
350-
todo!()
351-
}
352-
353-
fn lo_hi(self) -> (Self::H, Self::H) {
354-
todo!()
355-
}
356-
357-
fn from_lo_hi(lo: Self::H, hi: Self::H) -> Self {
358-
todo!()
343+
let mut tmp = [0u8; 16];
344+
tmp[..8].copy_from_slice(&self.0[2].to_le_bytes());
345+
tmp[8..].copy_from_slice(&self.0[3].to_le_bytes());
346+
i128::from_le_bytes(tmp)
359347
}
360348
}

src/int/mod.rs

+12-15
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ public_test_dep! {
1616
/// Minimal integer implementations needed on all integer types, including wide integers.
1717
pub(crate) trait MinInt: Copy
1818
+ core::fmt::Debug
19+
+ ops::BitOr<Output = Self>
1920
+ ops::Not<Output = Self>
21+
+ ops::Shl<u32, Output = Self>
2022
{
2123

2224
/// Type with the same width but other signedness
@@ -52,9 +54,7 @@ pub(crate) trait Int: MinInt
5254
+ ops::Add<Output = Self>
5355
+ ops::Sub<Output = Self>
5456
+ ops::Div<Output = Self>
55-
+ ops::Shl<u32, Output = Self>
5657
+ ops::Shr<u32, Output = Self>
57-
+ ops::BitOr<Output = Self>
5858
+ ops::BitXor<Output = Self>
5959
+ ops::BitAnd<Output = Self>
6060
{
@@ -279,16 +279,20 @@ public_test_dep! {
279279
/// primitives except for `u8`, because there is not a smaller primitive.
280280
pub(crate) trait DInt: MinInt {
281281
/// Integer that is half the bit width of the integer this trait is implemented for
282-
type H: HInt<D = Self> + Int;
282+
type H: HInt<D = Self>;
283283

284284
/// Returns the low half of `self`
285285
fn lo(self) -> Self::H;
286286
/// Returns the high half of `self`
287287
fn hi(self) -> Self::H;
288288
/// Returns the low and high halves of `self` as a tuple
289-
fn lo_hi(self) -> (Self::H, Self::H);
289+
fn lo_hi(self) -> (Self::H, Self::H) {
290+
(self.lo(), self.hi())
291+
}
290292
/// Constructs an integer using lower and higher half parts
291-
fn from_lo_hi(lo: Self::H, hi: Self::H) -> Self;
293+
fn from_lo_hi(lo: Self::H, hi: Self::H) -> Self {
294+
lo.zero_widen() | hi.widen_hi()
295+
}
292296
}
293297
}
294298

@@ -305,7 +309,9 @@ pub(crate) trait HInt: Int {
305309
/// around problems with associated type bounds (such as `Int<Othersign: DInt>`) being unstable
306310
fn zero_widen(self) -> Self::D;
307311
/// Widens the integer to have double bit width and shifts the integer into the higher bits
308-
fn widen_hi(self) -> Self::D;
312+
fn widen_hi(self) -> Self::D {
313+
self.widen() << <Self as MinInt>::BITS
314+
}
309315
/// Widening multiplication with zero widening. This cannot overflow.
310316
fn zero_widen_mul(self, rhs: Self) -> Self::D;
311317
/// Widening multiplication. This cannot overflow.
@@ -325,12 +331,6 @@ macro_rules! impl_d_int {
325331
fn hi(self) -> Self::H {
326332
(self >> <$X as MinInt>::BITS) as $X
327333
}
328-
fn lo_hi(self) -> (Self::H, Self::H) {
329-
(self.lo(), self.hi())
330-
}
331-
fn from_lo_hi(lo: Self::H, hi: Self::H) -> Self {
332-
lo.zero_widen() | hi.widen_hi()
333-
}
334334
}
335335
)*
336336
};
@@ -348,9 +348,6 @@ macro_rules! impl_h_int {
348348
fn zero_widen(self) -> Self::D {
349349
(self as $uH) as $X
350350
}
351-
fn widen_hi(self) -> Self::D {
352-
(self as $X) << <$H as MinInt>::BITS
353-
}
354351
fn zero_widen_mul(self, rhs: Self) -> Self::D {
355352
self.zero_widen().wrapping_mul(rhs.zero_widen())
356353
}

0 commit comments

Comments
 (0)