diff --git a/src/int/big.rs b/src/int/big.rs index 785b6df6..0d793bc8 100644 --- a/src/int/big.rs +++ b/src/int/big.rs @@ -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 { @@ -27,6 +27,13 @@ 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 { @@ -34,13 +41,6 @@ impl i256 { } } -/// 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; @@ -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 { - ::fmt(self, f) - } - } // impl ops::Add for $ty { // type Output = Self; diff --git a/testcrate/tests/big.rs b/testcrate/tests/big.rs index 52d27ddb..93ddb5ed 100644 --- a/testcrate/tests/big.rs +++ b/testcrate/tests/big.rs @@ -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])); @@ -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()); } @@ -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()); }