diff --git a/util/src/uint.rs b/util/src/uint.rs index 6793376a0..8e9172a04 100644 --- a/util/src/uint.rs +++ b/util/src/uint.rs @@ -165,7 +165,7 @@ macro_rules! uint_overflowing_mul { let self_t: &[u64; 4] = unsafe { &mem::transmute($self_expr) }; let other_t: &[u64; 4] = unsafe { &mem::transmute($other) }; - let overflow: u8; + let overflow: u64; unsafe { asm!(" mov $5, %rax @@ -222,25 +222,25 @@ macro_rules! uint_overflowing_mul { adc $$0, %rdx or %rdx, %rcx - cmpq $$0, %rcx + cmpq $$0, %rcx jne 2f mov $8, %rax cmpq $$0, %rax - sete %cl + setne %cl mov $7, %rax cmpq $$0, %rax - sete %dl + setne %dl or %dl, %cl mov $3, %rax cmpq $$0, %rax - sete %dl + setne %dl mov $2, %rax cmpq $$0, %rax - sete %bl + setne %bl or %bl, %dl and %dl, %cl @@ -253,7 +253,7 @@ macro_rules! uint_overflowing_mul { : /* $5 */ "m"(self_t[0]), /* $6 */ "m"(self_t[1]), /* $7 */ "m"(self_t[2]), /* $8 */ "m"(self_t[3]), /* $9 */ "m"(other_t[0]), /* $10 */ "m"(other_t[1]), /* $11 */ "m"(other_t[2]), /* $12 */ "m"(other_t[3]) - : "rax", "rdx" + : "rax", "rdx", "rbx" : ); @@ -740,23 +740,8 @@ macro_rules! construct_uint { type Output = $name; fn add(self, other: $name) -> $name { - let $name(ref me) = self; - let $name(ref you) = other; - let mut ret = [0u64; $n_words]; - let mut carry = [0u64; $n_words]; - let mut b_carry = false; - for i in 0..$n_words { - if i < $n_words - 1 { - ret[i] = me[i].wrapping_add(you[i]); - if ret[i] < me[i] { - carry[i + 1] = 1; - b_carry = true; - } - } else { - ret[i] = me[i] + you[i]; - } - } - if b_carry { $name(ret) + $name(carry) } else { $name(ret) } + let (result, _) = self.overflowing_add(other); + result } } @@ -765,8 +750,7 @@ macro_rules! construct_uint { #[inline] fn sub(self, other: $name) -> $name { - let (result, overflow) = self.overflowing_sub(other); - panic_on_overflow!(overflow); + let (result, _) = self.overflowing_sub(other); result } } @@ -775,15 +759,9 @@ macro_rules! construct_uint { type Output = $name; fn mul(self, other: $name) -> $name { - let mut res = $name::from(0u64); - // TODO: be more efficient about this - for i in 0..(2 * $n_words) { - let v = self.mul_u32((other >> (32 * i)).low_u32()); - let (r, overflow) = v.overflowing_shl(32 * i as u32); - panic_on_overflow!(overflow); - res = res + r; - } - res + let (result, overflow) = self.overflowing_mul(other); + panic_on_overflow!(overflow); + result } }