Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port over relevant changes from bigint #25

Merged
merged 16 commits into from
Aug 14, 2018
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 .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ matrix:
script:
- cargo build
- cargo test --all --exclude uint
- cd uint/ && cargo test --features=std,impl_quickcheck_arbitrary && cd ..
- cd uint/ && cargo test --features=std,impl_quickcheck_arbitrary --release && cd ..
- cd hashdb/ && cargo test --no-default-features && cd ..
- cd plain_hasher/ && cargo test --no-default-features && cd ..
16 changes: 6 additions & 10 deletions uint/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
[package]
description = "Large fixed-size integers arithmetics"
homepage = "http://parity.io"
repository = "https://github.com/paritytech/primitives"
repository = "https://github.com/paritytech/parity-common"
license = "MIT/Apache-2.0"
name = "uint"
version = "0.2.2"
version = "0.3.0"
authors = ["Parity Technologies <admin@parity.io>"]
build = "build.rs"

[build-dependencies]
rustc_version = "0.2"

[dependencies]
byteorder = { version = "1", default-features = false }
heapsize = { version = "0.4.2", optional = true }
rustc-hex = { version = "1.0", optional = true }
rustc-hex = { version = "2.0", default-features = false }
quickcheck = { version = "0.6", optional = true }
crunchy = "0.1"

[dev-dependencies]
crunchy = "0.1.5"
quickcheck = "0.6"
rustc-hex = "2.0"

[features]
std = ["rustc-hex", "byteorder/std"]
std = ["byteorder/std", "rustc-hex/std"]
heapsizeof = ["heapsize"]
use_asm = []
impl_quickcheck_arbitrary = ["quickcheck"]

[[example]]
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.
The functions here are designed to be fast. There are optional `x86_64`
implementations for even more speed, hidden behind the `x64_arithmetic`
feature flag.
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);`.

Run tests with `cargo test --features=std,impl_quickcheck_arbitrary`.
230 changes: 230 additions & 0 deletions uint/benches/bigint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
// Copyright 2015-2017 Parity Technologies
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! benchmarking for bigint
//! should be started with:
//! ```bash
//! rustup run nightly cargo bench
//! ```

#![feature(test)]

extern crate core;
extern crate test;
#[macro_use]
extern crate crunchy;
#[macro_use]
extern crate uint;
Copy link
Contributor

Choose a reason for hiding this comment

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

Add newline before macro invocation?

construct_uint!(U128, 2);
construct_uint!(U256, 4);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use the exported types here? Apparently, creating the types here influences the benchmark results.

construct_uint!(U512, 8);

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(|| {
let n = black_box(10000);
let zero = black_box(U256::zero());
(0..n).fold(zero, |old, new| {
old.overflowing_add(U256::from(black_box(new))).0
})
});
}

#[bench]
fn u256_sub(b: &mut Bencher) {
b.iter(|| {
let n = black_box(10000);
let max = black_box(U256::max_value());
(0..n).fold(max, |old, new| {
old.overflowing_sub(U256::from(black_box(new))).0
})
});
}

#[bench]
fn u512_sub(b: &mut Bencher) {
b.iter(|| {
let n = black_box(10000);
let max = black_box(U512::max_value());
(0..n).fold(max, |old, new| {
let new = black_box(new);
let p = new % 2;
old.overflowing_sub(U512([p, p, p, p, p, p, p, new])).0
})
});
}

#[bench]
fn u512_add(b: &mut Bencher) {
b.iter(|| {
let n = black_box(10000);
let zero = black_box(U512::zero());
(0..n).fold(zero, |old, new| {
let new = black_box(new);
old.overflowing_add(U512([new, new, new, new, new, new, new, new]))
.0
})
});
}

#[bench]
fn u512_mul(b: &mut Bencher) {
b.iter(|| {
(1..10000).fold(black_box(U512::one()), |old, new| {
old.overflowing_mul(U512::from(black_box(new | 1))).0
})
});
}

#[bench]
fn u512_mul_small(b: &mut Bencher) {
b.iter(|| {
(1..153).fold(black_box(U512::one()), |old, _| {
old.overflowing_mul(U512::from(black_box(10))).0
})
});
}

#[bench]
fn u256_mul(b: &mut Bencher) {
b.iter(|| {
(1..10000).fold(black_box(U256::one()), |old, new| {
old.overflowing_mul(U256::from(black_box(new | 1))).0
})
});
}

#[bench]
fn u256_mul_small(b: &mut Bencher) {
b.iter(|| {
(1..77).fold(black_box(U256::one()), |old, _| {
old.overflowing_mul(U256::from(black_box(10))).0
})
});
}

#[bench]
fn u256_full_mul(b: &mut Bencher) {
b.iter(|| {
let n = black_box(10000);
let one = black_box(U256::one());
(1..n).map(|n| n | 1).fold(one, |old, new| {
let new = black_box(new);
let U512(ref u512words) = old.full_mul(U256([new, new, new, new]));
U256([u512words[0], u512words[2], u512words[2], u512words[3]])
})
});
}


#[bench]
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
})
});
}

#[bench]
fn u256_from_le(b: &mut Bencher) {
b.iter(|| {
let raw = black_box(
[
1u8,
2,
3,
5,
7,
11,
13,
17,
19,
23,
29,
31,
37,
41,
43,
47,
53,
59,
61,
67,
71,
73,
79,
83,
89,
97,
101,
103,
107,
109,
113,
127,
],
);
let _ = U256::from_little_endian(&raw[..]);
});
}

#[bench]
fn u256_from_be(b: &mut Bencher) {
b.iter(|| {
let raw = black_box(
[
1u8,
2,
3,
5,
7,
11,
13,
17,
19,
23,
29,
31,
37,
41,
43,
47,
53,
59,
61,
67,
71,
73,
79,
83,
89,
97,
101,
103,
107,
109,
113,
127,
],
);
let _ = U256::from_big_endian(&raw[..]);
});
}
19 changes: 0 additions & 19 deletions uint/build.rs

This file was deleted.

11 changes: 8 additions & 3 deletions uint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

//! Efficient large, fixed-size big integers and hashes.

#![cfg_attr(asm_available, feature(asm))]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature="std"), no_std)]
#![cfg_attr(all(not(feature="std"), test), feature(alloc))]

#[doc(hidden)]
pub extern crate byteorder;
Expand All @@ -22,13 +22,18 @@ pub extern crate heapsize;
#[doc(hidden)]
pub extern crate core;

#[cfg(feature = "std")]
#[doc(hidden)]
pub extern crate rustc_hex;

#[cfg(feature="impl_quickcheck_arbitrary")]
#[doc(hidden)]
pub extern crate quickcheck;

#[cfg(all(not(feature = "std"), test))]
extern crate alloc;

#[macro_use]
extern crate crunchy;

mod uint;
pub use uint::*;
Loading