From 76865694ce705b945655b685e05ef21ade44e8e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Mon, 7 Mar 2016 19:03:29 +0100 Subject: [PATCH] Subtraction optimization --- util/bigint/src/uint.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/util/bigint/src/uint.rs b/util/bigint/src/uint.rs index 8fbaca532..0e8d1e7b7 100644 --- a/util/bigint/src/uint.rs +++ b/util/bigint/src/uint.rs @@ -166,9 +166,22 @@ macro_rules! uint_overflowing_add { #[cfg(not(all(asm_available, target_arch="x86_64")))] macro_rules! uint_overflowing_sub { ($name:ident, $n_words: expr, $self_expr: expr, $other: expr) => ({ - let res = overflowing!((!$other).overflowing_add(From::from(1u64))); - let res = overflowing!($self_expr.overflowing_add(res)); - (res, $self_expr < $other) + let $name(ref me) = $self_expr; + let $name(ref you) = $other; + + let mut ret = [0u64; $n_words]; + let mut carry = 0u64; + + for i in 0..$n_words { + let (res1, overflow1) = me[i].overflowing_sub(you[i]); + let (res2, overflow2) = res1.overflowing_sub(carry); + + ret[i] = res2; + carry = overflow1 as u64 + overflow2 as u64; + } + + ($name(ret), carry > 0) + }) }