Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 4 additions & 8 deletions uint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,23 @@ homepage = "http://parity.io"
repository = "https://github.com/paritytech/primitives"
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.

btw, repository needs an update?

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", optional = true }
rustc-hex = "2.0"
quickcheck = { version = "0.6", optional = true }

[dev-dependencies]
crunchy = "0.1.5"
quickcheck = "0.6"

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

[[example]]
Expand Down
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
Copy Markdown
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
Copy Markdown
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.

9 changes: 6 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,16 @@ 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))]
#[macro_use]
extern crate alloc;

mod uint;
pub use uint::*;
Loading