This commit is contained in:
Nikolay Volf 2016-02-25 03:09:33 +03:00
parent 370d9015b4
commit da69ea51fe
2 changed files with 26 additions and 13 deletions

View File

@ -33,7 +33,15 @@ use ethcore_util::uint::*;
fn u256_add(b: &mut Bencher) {
b.iter(|| {
let n = black_box(10000);
(0..n).fold(U256::zero(), |old, new| { old.overflowing_add(U256::from(new)).0 })
(0..n).fold(U256::from(1234599u64), |old, new| { old.overflowing_add(U256::from(new)).0 })
});
}
#[bench]
fn u256_uber_add(b: &mut Bencher) {
b.iter(|| {
let n = black_box(10000);
(0..n).fold(U256::from(1234599u64), |old, new| { old.uber_add(U256::from(new)).0 })
});
}
@ -41,7 +49,7 @@ fn u256_add(b: &mut Bencher) {
fn u256_sub(b: &mut Bencher) {
b.iter(|| {
let n = black_box(10000);
(0..n).fold(U256::zero(), |old, new| { old.overflowing_sub(U256::from(new)).0 })
(0..n).fold(U256::from(::std::u64::MAX), |old, new| { old.overflowing_sub(U256::from(new)).0 })
});
}

View File

@ -97,17 +97,19 @@ macro_rules! uint_overflowing_add {
let other_t: &[u64; 4] = unsafe { &mem::transmute($other) };
let overflow: u8;
unsafe {
asm!("
adc $9, $0
adc $10, $1
adc $11, $2
adc $12, $3
setc %al"
: "=r"(result[0]), "=r"(result[1]), "=r"(result[2]), "=r"(result[3]), "={al}"(overflow)
: "0"(self_t[0]), "1"(self_t[1]), "2"(self_t[2]), "3"(self_t[3]), "mr"(other_t[0]), "mr"(other_t[1]), "mr"(other_t[2]), "mr"(other_t[3])
:
:
unsafe {
asm!("
adc $9, %r8
adc $10, %r9
adc $11, %r10
adc $12, %r11
setc %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)
@ -522,14 +524,17 @@ macro_rules! construct_uint {
}
/// Optimized instructions
#[inline(always)]
fn overflowing_add(self, other: $name) -> ($name, bool) {
uint_overflowing_add!($name, $n_words, self, other)
}
#[inline(always)]
fn overflowing_sub(self, other: $name) -> ($name, bool) {
uint_overflowing_sub!($name, $n_words, self, other)
}
#[inline(always)]
fn overflowing_mul(self, other: $name) -> ($name, bool) {
uint_overflowing_mul!($name, $n_words, self, other)
}