Skip to content

Commit

Permalink
Merge pull request #13 from paritytech/td-lower-hex
Browse files Browse the repository at this point in the history
Lower hex and couple of cleanups
  • Loading branch information
debris authored Feb 4, 2018
2 parents 4c01801 + 7e874f4 commit 0378495
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 58 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
root = true
[*]
indent_style=tab
indent_size=tab
tab_width=4
end_of_line=lf
charset=utf-8
trim_trailing_whitespace=true
max_line_length=120
insert_final_newline=true

[.travis.yml]
indent_style=space
indent_size=2
tab_width=8
end_of_line=lf
7 changes: 7 additions & 0 deletions fixed-hash/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ macro_rules! construct_hash {
}
}

impl ::core::fmt::LowerHex for $from {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Debug::fmt(self, f)
}
}

impl Copy for $from {}
#[cfg_attr(feature="dev", allow(expl_impl_clone_on_copy))]
impl Clone for $from {
Expand Down Expand Up @@ -337,6 +343,7 @@ macro_rules! impl_std_for_hash {
($from: ident, $size: tt) => {
impl $from {
/// Get a hex representation.
#[deprecated(note="Use LowerHex or Debug formatting instead.")]
pub fn hex(&self) -> String {
format!("{:?}", self)
}
Expand Down
43 changes: 28 additions & 15 deletions tests/src/uint_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ fn uint256_arithmetic_test() {
let sub = overflowing!(incr.overflowing_sub(init));
assert_eq!(sub, U256([0x9F30411021524112u64, 0x0001BD5B7DDFBD5A, 0, 0]));
// Multiplication
let mult = sub.mul_u32(300);
let mult = sub * 300u32;
assert_eq!(mult, U256([0x8C8C3EE70C644118u64, 0x0209E7378231E632, 0, 0]));
// Division
assert_eq!(U256::from(105u8) / U256::from(5u8), U256::from(21u8));
Expand Down Expand Up @@ -221,6 +221,15 @@ fn uint256_exp10() {

#[test]
fn uint256_mul32() {
assert_eq!(U256::from(0u64) * 2u32, U256::from(0u64));
assert_eq!(U256::from(1u64) * 2u32, U256::from(2u64));
assert_eq!(U256::from(10u64) * 2u32, U256::from(20u64));
assert_eq!(U256::from(10u64) * 5u32, U256::from(50u64));
assert_eq!(U256::from(1000u64) * 50u32, U256::from(50000u64));
}

#[test]
fn uint256_mul32_old() {
assert_eq!(U256::from(0u64).mul_u32(2), U256::from(0u64));
assert_eq!(U256::from(1u64).mul_u32(2), U256::from(2u64));
assert_eq!(U256::from(10u64).mul_u32(2), U256::from(20u64));
Expand All @@ -244,23 +253,27 @@ fn uint256_pow_overflow_panic() {
}

#[test]
fn should_format_hex_correctly() {
assert_eq!(&U256::from(0).to_hex(), &"0");
assert_eq!(&U256::from(0x1).to_hex(), &"1");
assert_eq!(&U256::from(0xf).to_hex(), &"f");
assert_eq!(&U256::from(0x10).to_hex(), &"10");
assert_eq!(&U256::from(0xff).to_hex(), &"ff");
assert_eq!(&U256::from(0x100).to_hex(), &"100");
assert_eq!(&U256::from(0xfff).to_hex(), &"fff");
assert_eq!(&U256::from(0x1000).to_hex(), &"1000");
fn should_format_and_debug_correctly() {
let test = |x: usize, hex: &'static str, dbg: &'static str| {
assert_eq!(format!("{:?}", U256::from(x)), dbg);
assert_eq!(format!("{:x}", U256::from(x)), hex);
};

test(0x1, "1", "1");
test(0xf, "f", "15");
test(0x10, "10", "16");
test(0xff, "ff", "255");
test(0x100, "100", "256");
test(0xfff, "fff", "4095");
test(0x1000, "1000", "4096");
}

#[test]
fn uint256_overflowing_pow() {
// assert_eq!(
// U256::from(2).overflowing_pow(U256::from(0xff)),
// (U256::from_str("8000000000000000000000000000000000000000000000000000000000000000").unwrap(), false)
// );
assert_eq!(
U256::from(2).overflowing_pow(U256::from(0xff)),
(U256::from_str("8000000000000000000000000000000000000000000000000000000000000000").unwrap(), false)
);
assert_eq!(
U256::from(2).overflowing_pow(U256::from(0x100)),
(U256::zero(), true)
Expand Down Expand Up @@ -871,7 +884,7 @@ fn u256_multi_muls2() {
#[test]
fn example() {
let mut val: U256 = 1023.into();
for _ in 0..200 { val = val * 2.into() }
for _ in 0..200 { val = val * U256::from(2) }
assert_eq!(&format!("{}", val), "1643897619276947051879427220465009342380213662639797070513307648");
}

Expand Down
93 changes: 50 additions & 43 deletions uint/src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ macro_rules! uint_overflowing_add {
"
: "=r"(result[0]), "=r"(result[1]), "=r"(result[2]), "=r"(result[3]), "={al}"(overflow)
: "0"(self_t[0]), "1"(self_t[1]), "2"(self_t[2]), "3"(self_t[3]),
"mr"(other_t[0]), "mr"(other_t[1]), "mr"(other_t[2]), "mr"(other_t[3])
"mr"(other_t[0]), "mr"(other_t[1]), "mr"(other_t[2]), "mr"(other_t[3])
:
:
);
Expand Down Expand Up @@ -129,16 +129,16 @@ macro_rules! uint_overflowing_add {
": "=r"(result[0]), "=r"(result[1]), "=r"(result[2]), "=r"(result[3]),

"={al}"(overflow) /* $0 - $4 */
"={al}"(overflow) /* $0 - $4 */

: "{rdi}"(&result[4] as *const u64) /* $5 */
"{rsi}"(&other_t[4] as *const u64) /* $6 */
"0"(self_t[0]), "1"(self_t[1]), "2"(self_t[2]), "3"(self_t[3]),
"m"(self_t[4]), "m"(self_t[5]), "m"(self_t[6]), "m"(self_t[7]),
/* $7 - $14 */
: "{rdi}"(&result[4] as *const u64) /* $5 */
"{rsi}"(&other_t[4] as *const u64) /* $6 */
"0"(self_t[0]), "1"(self_t[1]), "2"(self_t[2]), "3"(self_t[3]),
"m"(self_t[4]), "m"(self_t[5]), "m"(self_t[6]), "m"(self_t[7]),
/* $7 - $14 */

"mr"(other_t[0]), "mr"(other_t[1]), "mr"(other_t[2]), "mr"(other_t[3]),
"m"(other_t[4]), "m"(other_t[5]), "m"(other_t[6]), "m"(other_t[7]) /* $15 - $22 */
"mr"(other_t[0]), "mr"(other_t[1]), "mr"(other_t[2]), "mr"(other_t[3]),
"m"(other_t[4]), "m"(other_t[5]), "m"(other_t[6]), "m"(other_t[7]) /* $15 - $22 */
: "rdi", "rsi"
:
);
Expand Down Expand Up @@ -274,16 +274,16 @@ macro_rules! uint_overflowing_sub {
"
: "=r"(result[0]), "=r"(result[1]), "=r"(result[2]), "=r"(result[3]),

"={al}"(overflow) /* $0 - $4 */
"={al}"(overflow) /* $0 - $4 */

: "{rdi}"(&result[4] as *const u64) /* $5 */
"{rsi}"(&self_t[4] as *const u64) /* $6 */
"0"(self_t[0]), "1"(self_t[1]), "2"(self_t[2]), "3"(self_t[3]),
"m"(self_t[4]), "m"(self_t[5]), "m"(self_t[6]), "m"(self_t[7]),
/* $7 - $14 */
"0"(self_t[0]), "1"(self_t[1]), "2"(self_t[2]), "3"(self_t[3]),
"m"(self_t[4]), "m"(self_t[5]), "m"(self_t[6]), "m"(self_t[7]),
/* $7 - $14 */

"m"(other_t[0]), "m"(other_t[1]), "m"(other_t[2]), "m"(other_t[3]),
"m"(other_t[4]), "m"(other_t[5]), "m"(other_t[6]), "m"(other_t[7]) /* $15 - $22 */
"m"(other_t[0]), "m"(other_t[1]), "m"(other_t[2]), "m"(other_t[3]),
"m"(other_t[4]), "m"(other_t[5]), "m"(other_t[6]), "m"(other_t[7]) /* $15 - $22 */
: "rdi", "rsi"
:
);
Expand Down Expand Up @@ -399,12 +399,12 @@ macro_rules! uint_overflowing_mul {
2:
"
: /* $0 */ "={r8}"(result[0]), /* $1 */ "={r9}"(result[1]), /* $2 */ "={r10}"(result[2]),
/* $3 */ "={r11}"(result[3]), /* $4 */ "={rcx}"(overflow)
/* $3 */ "={r11}"(result[3]), /* $4 */ "={rcx}"(overflow)

: /* $5 */ "m"(self_t[0]), /* $6 */ "m"(self_t[1]), /* $7 */ "m"(self_t[2]),
/* $8 */ "m"(self_t[3]), /* $9 */ "m"(other_t[0]), /* $10 */ "m"(other_t[1]),
/* $11 */ "m"(other_t[2]), /* $12 */ "m"(other_t[3])
: "rax", "rdx"
/* $8 */ "m"(self_t[3]), /* $9 */ "m"(other_t[0]), /* $10 */ "m"(other_t[1]),
/* $11 */ "m"(other_t[2]), /* $12 */ "m"(other_t[3])
: "rax", "rdx"
:

);
Expand Down Expand Up @@ -717,7 +717,7 @@ macro_rules! construct_uint {
pub fn exp10(n: usize) -> Self {
match n {
0 => Self::from(1u64),
_ => Self::exp10(n - 1).mul_u32(10)
_ => Self::exp10(n - 1) * 10u32
}
}

Expand Down Expand Up @@ -913,11 +913,10 @@ macro_rules! construct_uint {
}

/// Multiplication by u32
pub fn mul_u32(self, other: u32) -> Self {
let (ret, overflow) = self.overflowing_mul_u32(other);
panic_on_overflow!(overflow);
ret
}
#[deprecated(note = "Use Mul<u32> instead.")]
pub fn mul_u32(self, other: u32) -> Self {
self * other
}

/// Overflowing multiplication by u32
#[allow(dead_code)] // not used when multiplied with inline assembly
Expand All @@ -936,6 +935,8 @@ macro_rules! construct_uint {
($name(ret), carry > 0)
}

impl_std_for_uint_internals!($name, $n_words);

/// Converts from big endian representation bytes in memory
/// Can also be used as (&slice).into(), as it is default `From`
/// slice implementation for U256
Expand Down Expand Up @@ -969,8 +970,6 @@ macro_rules! construct_uint {

$name(ret)
}

impl_std_for_uint_internals!($name, $n_words);
}

impl Default for $name {
Expand Down Expand Up @@ -1036,6 +1035,16 @@ macro_rules! construct_uint {
}
}

impl ::core::ops::Mul<u32> for $name {
type Output = $name;

fn mul(self, other: u32) -> $name {
let (ret, overflow) = self.overflowing_mul_u32(other);
panic_on_overflow!(overflow);
ret
}
}

impl ::core::ops::Mul<$name> for $name {
type Output = $name;

Expand Down Expand Up @@ -1229,18 +1238,9 @@ macro_rules! construct_uint {
macro_rules! impl_std_for_uint_internals {
($name: ident, $n_words: tt) => {
/// Convert to hex string.
#[inline]
#[deprecated(note = "Use LowerHex instead.")]
pub fn to_hex(&self) -> String {
use core::cmp;
use $crate::rustc_hex::ToHex;;

if self.is_zero() { return "0".to_owned(); } // special case.
let mut bytes = [0u8; 8 * $n_words];
self.to_big_endian(&mut bytes);
let bp7 = self.bits() + 7;
let len = cmp::max(bp7 / 8, 1);
let bytes_hex = bytes[bytes.len() - len..].to_hex();
(&bytes_hex[1 - bp7 % 8 / 4..]).to_owned()
format!("{:x}", self)
}
}
}
Expand Down Expand Up @@ -1294,14 +1294,21 @@ macro_rules! impl_std_for_uint {
impl ::core::fmt::LowerHex for $name {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
let &$name(ref data) = self;
try!(write!(f, "0x"));
// special case.
if self.is_zero() {
return write!(f, "0");
}

let mut latch = false;
for ch in data.iter().rev() {
for x in 0..16 {
let nibble = (ch & (15u64 << ((15 - x) * 4) as u64)) >> (((15 - x) * 4) as u64);
if !latch { latch = nibble != 0 }
if !latch {
latch = nibble != 0;
}

if latch {
try!(write!(f, "{:x}", nibble));
write!(f, "{:x}", nibble)?;
}
}
}
Expand All @@ -1320,14 +1327,14 @@ macro_rules! impl_std_for_uint {
#[cfg(not(feature="std"))]
#[macro_export]
#[doc(hidden)]
macro_rules! impl_std_for_uint_internals {
macro_rules! impl_std_for_uint {
($name: ident, $n_words: tt) => {}
}

#[cfg(not(feature="std"))]
#[macro_export]
#[doc(hidden)]
macro_rules! impl_std_for_uint {
macro_rules! impl_std_for_uint_internals {
($name: ident, $n_words: tt) => {}
}

Expand Down

0 comments on commit 0378495

Please sign in to comment.