u256 to inline assembly opt
This commit is contained in:
parent
d4ad673d64
commit
dd8652dbf4
37
util/benches/bigint.rs
Normal file
37
util/benches/bigint.rs
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
||||||
|
// This file is part of Parity.
|
||||||
|
|
||||||
|
// Parity is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
|
||||||
|
// Parity is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
//! benchmarking for rlp
|
||||||
|
//! should be started with:
|
||||||
|
//! ```bash
|
||||||
|
//! multirust run nightly cargo bench
|
||||||
|
//! ```
|
||||||
|
|
||||||
|
#![feature(test)]
|
||||||
|
|
||||||
|
extern crate test;
|
||||||
|
extern crate ethcore_util;
|
||||||
|
|
||||||
|
use test::{Bencher, black_box};
|
||||||
|
use ethcore_util::uint::*;
|
||||||
|
|
||||||
|
#[bench]
|
||||||
|
fn u256_first_degree(b: &mut test::Bencher) {
|
||||||
|
b.iter(|| {
|
||||||
|
let n = black_box(10000);
|
||||||
|
(0..n).fold(U256::zero(), |old, new| { old.overflowing_add(U256::from(new)).0 })
|
||||||
|
});
|
||||||
|
}
|
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
#![warn(missing_docs)]
|
#![warn(missing_docs)]
|
||||||
#![cfg_attr(feature="dev", feature(plugin))]
|
#![cfg_attr(feature="dev", feature(plugin))]
|
||||||
|
#![cfg_attr(feature="dev", feature(asm))]
|
||||||
#![cfg_attr(feature="dev", plugin(clippy))]
|
#![cfg_attr(feature="dev", plugin(clippy))]
|
||||||
|
|
||||||
// Clippy settings
|
// Clippy settings
|
||||||
|
@ -51,6 +51,64 @@ macro_rules! impl_map_from {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! overflowing_add_regular {
|
||||||
|
($name:ident, $n_words:expr, $self_expr: expr, $other: expr) => ({
|
||||||
|
let $name(ref me) = $self_expr;
|
||||||
|
let $name(ref you) = $other;
|
||||||
|
let mut ret = [0u64; $n_words];
|
||||||
|
let mut carry = [0u64; $n_words];
|
||||||
|
let mut b_carry = false;
|
||||||
|
let mut overflow = false;
|
||||||
|
|
||||||
|
for i in 0..$n_words {
|
||||||
|
ret[i] = me[i].wrapping_add(you[i]);
|
||||||
|
|
||||||
|
if ret[i] < me[i] {
|
||||||
|
if i < $n_words - 1 {
|
||||||
|
carry[i + 1] = 1;
|
||||||
|
b_carry = true;
|
||||||
|
} else {
|
||||||
|
overflow = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if b_carry {
|
||||||
|
let ret = overflowing!($name(ret).overflowing_add($name(carry)), overflow);
|
||||||
|
(ret, overflow)
|
||||||
|
} else {
|
||||||
|
($name(ret), overflow)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! overflowing_add_u256_asm {
|
||||||
|
(U256, $n_words: expr, $self_expr: expr, $other: expr) => ({
|
||||||
|
let mut result: [u64; 4] = unsafe { mem::uninitialized() };
|
||||||
|
let self_t: &[u64; 4] = unsafe { &mem::transmute($self_expr) };
|
||||||
|
let other_t: &[u64; 4] = unsafe { &mem::transmute($other) };
|
||||||
|
|
||||||
|
let overflow: u8;
|
||||||
|
unsafe {
|
||||||
|
asm!("
|
||||||
|
xor %al, %al
|
||||||
|
adc $9, %r8
|
||||||
|
adc $10, %r9
|
||||||
|
adc $11, %r10
|
||||||
|
adc $12, %r11
|
||||||
|
adc $$0, %al"
|
||||||
|
: "={r8}"(result[0]), "={r9}"(result[1]), "={r10}"(result[2]), "={r11}"(result[3]), "={al}"(overflow)
|
||||||
|
: "{r8}"(self_t[0]), "{r9}"(self_t[1]), "{r10}"(self_t[2]), "{r11}"(self_t[3]), "m"(other_t[0]), "m"(other_t[1]), "m"(other_t[2]), "m"(other_t[3])
|
||||||
|
:
|
||||||
|
:
|
||||||
|
);
|
||||||
|
}
|
||||||
|
(U256(result), overflow != 0)
|
||||||
|
});
|
||||||
|
($name:ident, $n_words:expr, $self_expr: expr, $other: expr) => (
|
||||||
|
overflowing_add_regular!($name, $n_words, $self_expr, $other)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
macro_rules! overflowing {
|
macro_rules! overflowing {
|
||||||
($op: expr, $overflow: expr) => (
|
($op: expr, $overflow: expr) => (
|
||||||
{
|
{
|
||||||
@ -297,32 +355,14 @@ macro_rules! construct_uint {
|
|||||||
(res, overflow)
|
(res, overflow)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(all(feature = "dev", target_arch = "x86_64"))]
|
||||||
fn overflowing_add(self, other: $name) -> ($name, bool) {
|
fn overflowing_add(self, other: $name) -> ($name, bool) {
|
||||||
let $name(ref me) = self;
|
overflowing_add_u256_asm!($name, $n_words, self, other)
|
||||||
let $name(ref you) = other;
|
}
|
||||||
let mut ret = [0u64; $n_words];
|
|
||||||
let mut carry = [0u64; $n_words];
|
|
||||||
let mut b_carry = false;
|
|
||||||
let mut overflow = false;
|
|
||||||
|
|
||||||
for i in 0..$n_words {
|
#[cfg(not(all(feature = "dev", target_arch = "x86_64")))]
|
||||||
ret[i] = me[i].wrapping_add(you[i]);
|
fn overflowing_add(self, other: $name) -> ($name, bool) {
|
||||||
|
overflowing_add_regular!($name, $n_words, self, other)
|
||||||
if ret[i] < me[i] {
|
|
||||||
if i < $n_words - 1 {
|
|
||||||
carry[i + 1] = 1;
|
|
||||||
b_carry = true;
|
|
||||||
} else {
|
|
||||||
overflow = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if b_carry {
|
|
||||||
let ret = overflowing!($name(ret).overflowing_add($name(carry)), overflow);
|
|
||||||
(ret, overflow)
|
|
||||||
} else {
|
|
||||||
($name(ret), overflow)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn overflowing_sub(self, other: $name) -> ($name, bool) {
|
fn overflowing_sub(self, other: $name) -> ($name, bool) {
|
||||||
@ -1171,8 +1211,6 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
pub fn uint256_mul_overflow_panic() {
|
pub fn uint256_mul_overflow_panic() {
|
||||||
|
Loading…
Reference in New Issue
Block a user