Skip to content

Commit 3cc6355

Browse files
committed
Fix core calls
1 parent 53203b0 commit 3cc6355

File tree

2 files changed

+27
-32
lines changed

2 files changed

+27
-32
lines changed

src/int/big.rs

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const U128_HI_MASK: u128 = (u64::MAX as u128) << 64;
1515
///
1616
/// Each limb is a native-endian number, but the array is little-limb-endian.
1717
#[allow(non_camel_case_types)]
18-
#[derive(Clone, Copy, PartialEq, PartialOrd)]
18+
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
1919
pub struct u256(pub [u64; 4]);
2020

2121
impl u256 {
@@ -27,20 +27,20 @@ impl u256 {
2727
}
2828
}
2929

30+
/// A 256-bit signed integer represented as 4 64-bit limbs.
31+
///
32+
/// Each limb is a native-endian number, but the array is little-limb-endian.
33+
#[allow(non_camel_case_types)]
34+
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
35+
pub struct i256(pub [u64; 4]);
36+
3037
impl i256 {
3138
/// Reinterpret as an unsigned integer
3239
pub fn unsigned(self) -> u256 {
3340
u256(self.0)
3441
}
3542
}
3643

37-
/// A 256-bit signed integer represented as 4 64-bit limbs.
38-
///
39-
/// Each limb is a native-endian number, but the array is little-limb-endian.
40-
#[allow(non_camel_case_types)]
41-
#[derive(Clone, Copy, PartialEq, PartialOrd)]
42-
pub struct i256(pub [u64; 4]);
43-
4444
impl MinInt for u256 {
4545
type OtherSign = i256;
4646

@@ -109,21 +109,6 @@ impl MinInt for i256 {
109109

110110
macro_rules! impl_common {
111111
($ty:ty) => {
112-
impl fmt::LowerHex for $ty {
113-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
114-
write!(
115-
f,
116-
"0x{:016x}{:016x}{:016x}{:016x}",
117-
self.0[3], self.0[2], self.0[1], self.0[0]
118-
)
119-
}
120-
}
121-
122-
impl fmt::Debug for $ty {
123-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
124-
<Self as fmt::LowerHex>::fmt(self, f)
125-
}
126-
}
127112
// impl ops::Add for $ty {
128113
// type Output = Self;
129114

testcrate/tests/big.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ use compiler_builtins::int::{i256, u256, HInt, Int, MinInt};
22

33
const LOHI_SPLIT: u128 = 0xaaaaaaaaaaaaaaaaffffffffffffffff;
44

5+
/// Print a `u256` as hex since we can't add format implementations
6+
fn hexu(v: u256) -> String {
7+
format!("0x{:016x}{:016x}{:016x}{:016x}", v.0[3], v.0[2], v.0[1], v.0[0])
8+
}
9+
10+
fn hexi(v: i256) -> String{
11+
hexu(v.unsigned())
12+
}
13+
514
#[test]
615
fn widen_u128() {
716
assert_eq!(u128::MAX.widen(), u256([u64::MAX, u64::MAX, 0, 0]));
@@ -33,17 +42,17 @@ fn widen_mul_u128() {
3342
];
3443

3544
let mut errors = Vec::new();
36-
for (i, (a, b, exp)) in tests.iter().enumerate() {
37-
let res = a.widen_mul(*b);
38-
let res_z = a.zero_widen_mul(*b);
45+
for (i, (a, b, exp)) in tests.iter().copied().enumerate() {
46+
let res = a.widen_mul(b);
47+
let res_z = a.zero_widen_mul(b);
3948
assert_eq!(res, res_z);
40-
if res != *exp {
49+
if res != exp {
4150
errors.push((i, a, b, exp, res));
4251
}
4352
}
4453

4554
for (i, a, b, exp, res) in &errors {
46-
eprintln!("FAILURE ({i}): {a:#034x} * {b:#034x} = {exp:x} got {res:x}");
55+
eprintln!("FAILURE ({i}): {a:#034x} * {b:#034x} = {} got {}", hexu(*exp), hexu(*res));
4756
}
4857
assert!(errors.is_empty());
4958
}
@@ -72,15 +81,16 @@ fn widen_mul_i128() {
7281
];
7382

7483
let mut errors = Vec::new();
75-
for (i, (a, b, exp)) in tests.iter().enumerate() {
76-
let res = a.widen_mul(*b);
77-
if res != *exp {
84+
for (i, (a, b, exp)) in tests.iter().copied().enumerate() {
85+
let res = a.widen_mul(b);
86+
// TODO check zero widen mul
87+
if res != exp {
7888
errors.push((i, a, b, exp, res));
7989
}
8090
}
8191

8292
for (i, a, b, exp, res) in &errors {
83-
eprintln!("FAILURE ({i}): {a:#034x} * {b:#034x} = {exp:x} got {res:x}");
93+
eprintln!("FAILURE ({i}): {a:#034x} * {b:#034x} = {} got {}", hexi(*exp), hexi(*res));
8494
}
8595
assert!(errors.is_empty());
8696
}

0 commit comments

Comments
 (0)