From c5840be1cb1a55d0daf1c208dbfd3c7bcdea6c7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Mon, 7 Mar 2016 18:36:17 +0100 Subject: [PATCH] Small improvements --- util/bigint/src/uint.rs | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/util/bigint/src/uint.rs b/util/bigint/src/uint.rs index 789fc744e..8fbaca532 100644 --- a/util/bigint/src/uint.rs +++ b/util/bigint/src/uint.rs @@ -379,11 +379,24 @@ macro_rules! uint_overflowing_mul_reg { ($name:ident, $n_words: expr, $self_expr: expr, $other: expr) => ({ let mut res = $name::from(0u64); let mut overflow = false; - for i in 0..(2 * $n_words) { - let v = overflowing!($self_expr.overflowing_mul_u32(($other >> (32 * i)).low_u32()), overflow); - let res2 = overflowing!(v.overflowing_shl(32 * i as u32), overflow); - res = overflowing!(res.overflowing_add(res2), overflow); + + let mut current = $other; + let mut current_shift = 0; + let mut current_u32; + let mut i = 0; + + while i < 2*$n_words { + current_u32 = current.low_u32(); + + let v = overflowing!($self_expr.overflowing_mul_u32(current_u32), overflow); + let v_shifted = overflowing!(v.overflowing_shl(current_shift), overflow); + res = overflowing!(res.overflowing_add(v_shifted), overflow); + + current = current >> 32; + current_shift += 32; + i += 1; } + (res, overflow) }) } @@ -973,6 +986,7 @@ macro_rules! construct_uint { let mut ret = [0u64; $n_words]; let word_shift = shift / 64; let bit_shift = shift % 64; + for i in word_shift..$n_words { // Shift ret[i - word_shift] += original[i] >> bit_shift; @@ -989,9 +1003,11 @@ macro_rules! construct_uint { fn cmp(&self, other: &$name) -> Ordering { let &$name(ref me) = self; let &$name(ref you) = other; - for i in 0..$n_words { - if me[$n_words - 1 - i] < you[$n_words - 1 - i] { return Ordering::Less; } - if me[$n_words - 1 - i] > you[$n_words - 1 - i] { return Ordering::Greater; } + let mut i = $n_words; + while i > 0 { + i -= 1; + if me[i] < you[i] { return Ordering::Less; } + if me[i] > you[i] { return Ordering::Greater; } } Ordering::Equal }