Skip to content

Commit

Permalink
Fix core calls
Browse files Browse the repository at this point in the history
  • Loading branch information
tgross35 committed Apr 17, 2024
1 parent 53203b0 commit 3cc6355
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
31 changes: 8 additions & 23 deletions src/int/big.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const U128_HI_MASK: u128 = (u64::MAX as u128) << 64;
///
/// Each limb is a native-endian number, but the array is little-limb-endian.
#[allow(non_camel_case_types)]
#[derive(Clone, Copy, PartialEq, PartialOrd)]
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
pub struct u256(pub [u64; 4]);

impl u256 {
Expand All @@ -27,20 +27,20 @@ impl u256 {
}
}

/// 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)
}
}

/// 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, PartialEq, PartialOrd)]
pub struct i256(pub [u64; 4]);

impl MinInt for u256 {
type OtherSign = i256;

Expand Down Expand Up @@ -109,21 +109,6 @@ impl MinInt for i256 {

macro_rules! impl_common {
($ty:ty) => {
impl fmt::LowerHex for $ty {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"0x{:016x}{:016x}{:016x}{:016x}",
self.0[3], self.0[2], self.0[1], self.0[0]
)
}
}

impl fmt::Debug for $ty {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
<Self as fmt::LowerHex>::fmt(self, f)
}
}
// impl ops::Add for $ty {
// type Output = Self;

Expand Down
28 changes: 19 additions & 9 deletions testcrate/tests/big.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ use compiler_builtins::int::{i256, u256, HInt, Int, MinInt};

const LOHI_SPLIT: u128 = 0xaaaaaaaaaaaaaaaaffffffffffffffff;

/// Print a `u256` as hex since we can't add format implementations
fn hexu(v: u256) -> String {
format!("0x{:016x}{:016x}{:016x}{:016x}", v.0[3], v.0[2], v.0[1], v.0[0])
}

fn hexi(v: i256) -> String{
hexu(v.unsigned())
}

#[test]
fn widen_u128() {
assert_eq!(u128::MAX.widen(), u256([u64::MAX, u64::MAX, 0, 0]));
Expand Down Expand Up @@ -33,17 +42,17 @@ fn widen_mul_u128() {
];

let mut errors = Vec::new();
for (i, (a, b, exp)) in tests.iter().enumerate() {
let res = a.widen_mul(*b);
let res_z = a.zero_widen_mul(*b);
for (i, (a, b, exp)) in tests.iter().copied().enumerate() {
let res = a.widen_mul(b);
let res_z = a.zero_widen_mul(b);
assert_eq!(res, res_z);
if res != *exp {
if res != exp {
errors.push((i, a, b, exp, res));
}
}

for (i, a, b, exp, res) in &errors {
eprintln!("FAILURE ({i}): {a:#034x} * {b:#034x} = {exp:x} got {res:x}");
eprintln!("FAILURE ({i}): {a:#034x} * {b:#034x} = {} got {}", hexu(*exp), hexu(*res));
}
assert!(errors.is_empty());
}
Expand Down Expand Up @@ -72,15 +81,16 @@ fn widen_mul_i128() {
];

let mut errors = Vec::new();
for (i, (a, b, exp)) in tests.iter().enumerate() {
let res = a.widen_mul(*b);
if res != *exp {
for (i, (a, b, exp)) in tests.iter().copied().enumerate() {
let res = a.widen_mul(b);
// TODO check zero widen mul
if res != exp {
errors.push((i, a, b, exp, res));
}
}

for (i, a, b, exp, res) in &errors {
eprintln!("FAILURE ({i}): {a:#034x} * {b:#034x} = {exp:x} got {res:x}");
eprintln!("FAILURE ({i}): {a:#034x} * {b:#034x} = {} got {}", hexi(*exp), hexi(*res));
}
assert!(errors.is_empty());
}

0 comments on commit 3cc6355

Please sign in to comment.