Selaa lähdekoodia

Tweak addo & subo to try and fix MIPS

Scott McMurray 7 vuotta sitten
vanhempi
commit
665f268872
2 muutettua tiedostoa jossa 20 lisäystä ja 7 poistoa
  1. 17 4
      src/int/add.rs
  2. 3 3
      src/int/sub.rs

+ 17 - 4
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 UAdd for u128 {}
+
+trait Add: Int
+    where <Self as Int>::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: Int {
+trait Addo: Add
+    where <Self as Int>::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;

+ 3 - 3
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;