|
@@ -1,25 +1,16 @@
|
|
|
-use int::Int;
|
|
|
-use int::LargeInt;
|
|
|
+use int::{DInt, Int};
|
|
|
|
|
|
-trait UAddSub: LargeInt {
|
|
|
+trait UAddSub: DInt {
|
|
|
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
|
|
|
- };
|
|
|
- Self::from_parts(low, high.wrapping_add(carry))
|
|
|
+ let (lo, carry) = self.lo().overflowing_add(other.lo());
|
|
|
+ let hi = self.hi().wrapping_add(other.hi());
|
|
|
+ let carry = if carry { Self::H::ONE } else { Self::H::ZERO };
|
|
|
+ Self::from_lo_hi(lo, hi.wrapping_add(carry))
|
|
|
}
|
|
|
fn uadd_one(self) -> Self {
|
|
|
- let (low, carry) = self.low().overflowing_add(Self::LowHalf::ONE);
|
|
|
- let carry = if carry {
|
|
|
- Self::HighHalf::ONE
|
|
|
- } else {
|
|
|
- Self::HighHalf::ZERO
|
|
|
- };
|
|
|
- Self::from_parts(low, self.high().wrapping_add(carry))
|
|
|
+ let (lo, carry) = self.lo().overflowing_add(Self::H::ONE);
|
|
|
+ let carry = if carry { Self::H::ONE } else { Self::H::ZERO };
|
|
|
+ Self::from_lo_hi(lo, self.hi().wrapping_add(carry))
|
|
|
}
|
|
|
fn usub(self, other: Self) -> Self {
|
|
|
let uneg = (!other).uadd_one();
|
|
@@ -48,19 +39,9 @@ trait Addo: AddSub
|
|
|
where
|
|
|
<Self as Int>::UnsignedInt: UAddSub,
|
|
|
{
|
|
|
- fn addo(self, other: Self, overflow: &mut i32) -> Self {
|
|
|
- *overflow = 0;
|
|
|
- let result = AddSub::add(self, other);
|
|
|
- if other >= Self::ZERO {
|
|
|
- if result < self {
|
|
|
- *overflow = 1;
|
|
|
- }
|
|
|
- } else {
|
|
|
- if result >= self {
|
|
|
- *overflow = 1;
|
|
|
- }
|
|
|
- }
|
|
|
- result
|
|
|
+ fn addo(self, other: Self) -> (Self, bool) {
|
|
|
+ let sum = AddSub::add(self, other);
|
|
|
+ (sum, (other < Self::ZERO) != (sum < self))
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -71,19 +52,9 @@ trait Subo: AddSub
|
|
|
where
|
|
|
<Self as Int>::UnsignedInt: UAddSub,
|
|
|
{
|
|
|
- fn subo(self, other: Self, overflow: &mut i32) -> Self {
|
|
|
- *overflow = 0;
|
|
|
- let result = AddSub::sub(self, other);
|
|
|
- if other >= Self::ZERO {
|
|
|
- if result > self {
|
|
|
- *overflow = 1;
|
|
|
- }
|
|
|
- } else {
|
|
|
- if result <= self {
|
|
|
- *overflow = 1;
|
|
|
- }
|
|
|
- }
|
|
|
- result
|
|
|
+ fn subo(self, other: Self) -> (Self, bool) {
|
|
|
+ let sum = AddSub::sub(self, other);
|
|
|
+ (sum, (other < Self::ZERO) != (self < sum))
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -92,43 +63,34 @@ impl Subo for u128 {}
|
|
|
|
|
|
intrinsics! {
|
|
|
pub extern "C" fn __rust_i128_add(a: i128, b: i128) -> i128 {
|
|
|
- __rust_u128_add(a as _, b as _) as _
|
|
|
+ AddSub::add(a,b)
|
|
|
}
|
|
|
|
|
|
pub extern "C" fn __rust_i128_addo(a: i128, b: i128) -> (i128, bool) {
|
|
|
- let mut oflow = 0;
|
|
|
- let r = a.addo(b, &mut oflow);
|
|
|
- (r, oflow != 0)
|
|
|
+ a.addo(b)
|
|
|
}
|
|
|
|
|
|
pub extern "C" fn __rust_u128_add(a: u128, b: u128) -> u128 {
|
|
|
- a.add(b)
|
|
|
+ AddSub::add(a,b)
|
|
|
}
|
|
|
|
|
|
pub extern "C" fn __rust_u128_addo(a: u128, b: u128) -> (u128, bool) {
|
|
|
- let mut oflow = 0;
|
|
|
- let r = a.addo(b, &mut oflow);
|
|
|
- (r, oflow != 0)
|
|
|
+ a.addo(b)
|
|
|
}
|
|
|
|
|
|
-
|
|
|
pub extern "C" fn __rust_i128_sub(a: i128, b: i128) -> i128 {
|
|
|
- __rust_u128_sub(a as _, b as _) as _
|
|
|
+ AddSub::sub(a,b)
|
|
|
}
|
|
|
|
|
|
pub extern "C" fn __rust_i128_subo(a: i128, b: i128) -> (i128, bool) {
|
|
|
- let mut oflow = 0;
|
|
|
- let r = a.subo(b, &mut oflow);
|
|
|
- (r, oflow != 0)
|
|
|
+ a.subo(b)
|
|
|
}
|
|
|
|
|
|
pub extern "C" fn __rust_u128_sub(a: u128, b: u128) -> u128 {
|
|
|
- a.sub(b)
|
|
|
+ AddSub::sub(a,b)
|
|
|
}
|
|
|
|
|
|
pub extern "C" fn __rust_u128_subo(a: u128, b: u128) -> (u128, bool) {
|
|
|
- let mut oflow = 0;
|
|
|
- let r = a.subo(b, &mut oflow);
|
|
|
- (r, oflow != 0)
|
|
|
+ a.subo(b)
|
|
|
}
|
|
|
}
|