Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion uint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <admin@parity.io>"]

[dependencies]
Expand Down
6 changes: 3 additions & 3 deletions uint/README.md
Original file line number Diff line number Diff line change
@@ -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`.
Run tests with `cargo test --features=std,impl_quickcheck_arbitrary --release`.
23 changes: 6 additions & 17 deletions uint/benches/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,11 @@

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 test::{Bencher, black_box};
use uint::{U256, U512};

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))
}
}
use test::{Bencher, black_box};

#[bench]
fn u256_add(b: &mut Bencher) {
Expand Down Expand Up @@ -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
})
});
}
Expand Down
12 changes: 11 additions & 1 deletion uint/src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1376,4 +1376,14 @@ macro_rules! impl_quickcheck_arbitrary_for_uint {
}

construct_uint!(U256, 4);
construct_uint!(U512, 8);
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))
}
}
48 changes: 0 additions & 48 deletions uint/tests/uint_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -1196,13 +1150,11 @@ pub mod laws {
}

construct_uint!(U64, 1);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure we also don't need U64 :D

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);
Expand Down