diff --git a/uint/Cargo.toml b/uint/Cargo.toml index 190841e..a3c37c4 100644 --- a/uint/Cargo.toml +++ b/uint/Cargo.toml @@ -4,7 +4,7 @@ homepage = "http://parity.io" repository = "https://github.com/paritytech/parity-common" license = "MIT/Apache-2.0" name = "uint" -version = "0.3.0" +version = "0.4.0" authors = ["Parity Technologies "] [dependencies] diff --git a/uint/README.md b/uint/README.md index 9ba5885..20679a2 100644 --- a/uint/README.md +++ b/uint/README.md @@ -1,8 +1,8 @@ # Big unsigned integer types -Implementation of a various large-but-fixed sized unsigned integer types. +Macros to implement large-but-fixed sized unsigned integer types. The functions here are designed to be fast. -The crate exports two commonly used types: `U256` and `U512`. Other sizes can be constructed with `construct_uint!(NAME, SIZE_IN_WORDS)`, e.g. `construct_uint!(U128, 2);`. +The crate builds and exports two commonly used types: `U256` and `U512`. Other sizes can be constructed with `construct_uint!(NAME, SIZE_IN_WORDS)`, e.g. `construct_uint!(U128, 2);`. -Run tests with `cargo test --features=std,impl_quickcheck_arbitrary`. \ No newline at end of file +Run tests with `cargo test --features=std,impl_quickcheck_arbitrary --release`. \ No newline at end of file diff --git a/uint/benches/bigint.rs b/uint/benches/bigint.rs index be8490b..123638e 100644 --- a/uint/benches/bigint.rs +++ b/uint/benches/bigint.rs @@ -16,25 +16,12 @@ extern crate core; extern crate test; -#[macro_use] -extern crate crunchy; -#[macro_use] extern crate uint; -construct_uint!(U128, 2); -construct_uint!(U256, 4); -construct_uint!(U512, 8); + +use uint::{U256, U512}; use test::{Bencher, black_box}; -impl U256 { - /// Multiplies two 256-bit integers to produce full 512-bit integer - /// No overflow possible - #[inline(always)] - pub fn full_mul(self, other: U256) -> U512 { - U512(uint_full_mul_reg!(U256, 4, self, other)) - } -} - #[bench] fn u256_add(b: &mut Bencher) { b.iter(|| { @@ -134,11 +121,13 @@ fn u256_full_mul(b: &mut Bencher) { #[bench] +// NOTE: uses native `u128` and does not measure this crates performance, +// but might be interesting as a comparison. fn u128_mul(b: &mut Bencher) { b.iter(|| { let n = black_box(10000); - (1..n).fold(U128([12345u64, 0u64]), |old, new| { - old.overflowing_mul(U128::from(new | 1)).0 + (1..n).fold(12345u128, |old, new| { + old.overflowing_mul(u128::from(new | 1u32)).0 }) }); } diff --git a/uint/src/uint.rs b/uint/src/uint.rs index dc2db06..a68f51c 100644 --- a/uint/src/uint.rs +++ b/uint/src/uint.rs @@ -1376,4 +1376,14 @@ macro_rules! impl_quickcheck_arbitrary_for_uint { } construct_uint!(U256, 4); -construct_uint!(U512, 8); \ No newline at end of file +construct_uint!(U512, 8); + +#[doc(hidden)] +impl U256 { + /// Multiplies two 256-bit integers to produce full 512-bit integer + /// No overflow possible + #[inline(always)] + pub fn full_mul(self, other: U256) -> U512 { + U512(uint_full_mul_reg!(U256, 4, self, other)) + } +} diff --git a/uint/tests/uint_tests.rs b/uint/tests/uint_tests.rs index fdf1b71..328e320 100644 --- a/uint/tests/uint_tests.rs +++ b/uint/tests/uint_tests.rs @@ -12,8 +12,6 @@ use core::u64::MAX; use core::str::FromStr; use uint::{U256, U512, FromDecStrErr}; -construct_uint!(U128, 2); - #[test] fn uint256_checked_ops() { let z = U256::from(0); @@ -360,14 +358,6 @@ fn should_format_and_debug_correctly() { test(0x1000, "1000", "4096"); } -#[test] -pub fn display_u128() { - let expected = "340282366920938463463374607431768211455"; - let value = U128::MAX; - assert_eq!(format!("{}", value), expected); - assert_eq!(format!("{:?}", value), expected); -} - #[test] pub fn display_u256() { let expected = "115792089237316195423570985008687907853269984665640564039457584007913129639935"; @@ -419,42 +409,6 @@ fn uint256_overflowing_mul() { ); } -#[test] -fn uint128_add() { - assert_eq!( - U128::from_str("fffffffffffffffff").unwrap() + U128::from_str("fffffffffffffffff").unwrap(), - U128::from_str("1ffffffffffffffffe").unwrap() - ); -} - -#[test] -fn uint128_add_overflow() { - assert_eq!( - U128::from_str("ffffffffffffffffffffffffffffffff").unwrap() - .overflowing_add( - U128::from_str("ffffffffffffffffffffffffffffffff").unwrap() - ), - (U128::from_str("fffffffffffffffffffffffffffffffe").unwrap(), true) - ); -} - -#[test] -#[should_panic] -#[cfg(debug_assertions)] -#[allow(unused_must_use)] -fn uint128_add_overflow_panic() { - U128::from_str("ffffffffffffffffffffffffffffffff").unwrap() - + - U128::from_str("ffffffffffffffffffffffffffffffff").unwrap(); -} - -#[test] -fn uint128_mul() { - assert_eq!( - U128::from_str("fffffffff").unwrap() * U128::from_str("fffffffff").unwrap(), - U128::from_str("ffffffffe000000001").unwrap()); -} - #[test] fn uint512_mul() { assert_eq!( @@ -1196,13 +1150,11 @@ pub mod laws { } construct_uint!(U64, 1); - construct_uint!(U128, 2); construct_uint!(U256, 4); construct_uint!(U512, 8); construct_uint!(U1024, 16); uint_laws!(u64, U64); - uint_laws!(u128, U128); uint_laws!(u256, U256); uint_laws!(u512, U512); uint_laws!(u1024, U1024);