From 665f268872cdaf5a8cd85aab695ad121c49df887 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Sat, 25 Nov 2017 05:26:24 -0800 Subject: [PATCH] Tweak addo & subo to try and fix MIPS --- src/int/add.rs | 23 ++++++++++++++++++----- src/int/sub.rs | 6 +++--- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/int/add.rs b/src/int/add.rs index 639d14a..4253b64 100644 --- a/src/int/add.rs +++ b/src/int/add.rs @@ -1,8 +1,8 @@ use int::LargeInt; use int::Int; -trait Add: LargeInt { - fn add(self, other: Self) -> Self { +trait UAdd: LargeInt { + fn uadd(self, other: Self) -> Self { let (low, carry) = self.low().overflowing_add(other.low()); let high = self.high().wrapping_add(other.high()); let carry = if carry { Self::HighHalf::ONE } else { Self::HighHalf::ZERO }; @@ -10,12 +10,25 @@ trait Add: LargeInt { } } -impl Add for u128 {} +impl UAdd for u128 {} -trait Addo: Int { +trait Add: Int + where ::UnsignedInt: UAdd +{ + fn add(self, other: Self) -> Self { + Self::from_unsigned(self.unsigned().uadd(other.unsigned())) + } +} + +impl Add for u128 {} +impl Add for i128 {} + +trait Addo: Add + where ::UnsignedInt: UAdd +{ fn addo(self, other: Self, overflow: &mut i32) -> Self { *overflow = 0; - let result = self.wrapping_add(other); + let result = Add::add(self, other); if other >= Self::ZERO { if result < self { *overflow = 1; diff --git a/src/int/sub.rs b/src/int/sub.rs index 4d3dfeb..ba29c10 100644 --- a/src/int/sub.rs +++ b/src/int/sub.rs @@ -1,5 +1,4 @@ use int::LargeInt; -use int::Int; trait Sub: LargeInt { fn sub(self, other: Self) -> Self { @@ -8,12 +7,13 @@ trait Sub: LargeInt { } } +impl Sub for i128 {} impl Sub for u128 {} -trait Subo: Int { +trait Subo: Sub { fn subo(self, other: Self, overflow: &mut i32) -> Self { *overflow = 0; - let result = self.wrapping_sub(other); + let result = Sub::sub(self, other); if other >= Self::ZERO { if result > self { *overflow = 1;