|
| 1 | +use int::LargeInt; |
| 2 | +use int::Int; |
| 3 | + |
| 4 | +trait UAddSub: LargeInt { |
| 5 | + fn uadd(self, other: Self) -> Self { |
| 6 | + let (low, carry) = self.low().overflowing_add(other.low()); |
| 7 | + let high = self.high().wrapping_add(other.high()); |
| 8 | + let carry = if carry { Self::HighHalf::ONE } else { Self::HighHalf::ZERO }; |
| 9 | + Self::from_parts(low, high.wrapping_add(carry)) |
| 10 | + } |
| 11 | + fn uadd_one(self) -> Self { |
| 12 | + let (low, carry) = self.low().overflowing_add(Self::LowHalf::ONE); |
| 13 | + let carry = if carry { Self::HighHalf::ONE } else { Self::HighHalf::ZERO }; |
| 14 | + Self::from_parts(low, self.high().wrapping_add(carry)) |
| 15 | + } |
| 16 | + fn usub(self, other: Self) -> Self { |
| 17 | + let uneg = (!other).uadd_one(); |
| 18 | + self.uadd(uneg) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +impl UAddSub for u128 {} |
| 23 | + |
| 24 | +trait AddSub: Int |
| 25 | + where <Self as Int>::UnsignedInt: UAddSub |
| 26 | +{ |
| 27 | + fn add(self, other: Self) -> Self { |
| 28 | + Self::from_unsigned(self.unsigned().uadd(other.unsigned())) |
| 29 | + } |
| 30 | + fn sub(self, other: Self) -> Self { |
| 31 | + Self::from_unsigned(self.unsigned().usub(other.unsigned())) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +impl AddSub for u128 {} |
| 36 | +impl AddSub for i128 {} |
| 37 | + |
| 38 | +trait Addo: AddSub |
| 39 | + where <Self as Int>::UnsignedInt: UAddSub |
| 40 | +{ |
| 41 | + fn addo(self, other: Self, overflow: &mut i32) -> Self { |
| 42 | + *overflow = 0; |
| 43 | + let result = AddSub::add(self, other); |
| 44 | + if other >= Self::ZERO { |
| 45 | + if result < self { |
| 46 | + *overflow = 1; |
| 47 | + } |
| 48 | + } else { |
| 49 | + if result >= self { |
| 50 | + *overflow = 1; |
| 51 | + } |
| 52 | + } |
| 53 | + result |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl Addo for i128 {} |
| 58 | +impl Addo for u128 {} |
| 59 | + |
| 60 | +trait Subo: AddSub |
| 61 | + where <Self as Int>::UnsignedInt: UAddSub |
| 62 | +{ |
| 63 | + fn subo(self, other: Self, overflow: &mut i32) -> Self { |
| 64 | + *overflow = 0; |
| 65 | + let result = AddSub::sub(self, other); |
| 66 | + if other >= Self::ZERO { |
| 67 | + if result > self { |
| 68 | + *overflow = 1; |
| 69 | + } |
| 70 | + } else { |
| 71 | + if result <= self { |
| 72 | + *overflow = 1; |
| 73 | + } |
| 74 | + } |
| 75 | + result |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl Subo for i128 {} |
| 80 | +impl Subo for u128 {} |
| 81 | + |
| 82 | +#[cfg_attr(not(stage0), lang = "i128_add")] |
| 83 | +pub fn rust_i128_add(a: i128, b: i128) -> i128 { |
| 84 | + rust_u128_add(a as _, b as _) as _ |
| 85 | +} |
| 86 | +#[cfg_attr(not(stage0), lang = "i128_addo")] |
| 87 | +pub fn rust_i128_addo(a: i128, b: i128) -> (i128, bool) { |
| 88 | + let mut oflow = 0; |
| 89 | + let r = a.addo(b, &mut oflow); |
| 90 | + (r, oflow != 0) |
| 91 | +} |
| 92 | +#[cfg_attr(not(stage0), lang = "u128_add")] |
| 93 | +pub fn rust_u128_add(a: u128, b: u128) -> u128 { |
| 94 | + a.add(b) |
| 95 | +} |
| 96 | +#[cfg_attr(not(stage0), lang = "u128_addo")] |
| 97 | +pub fn rust_u128_addo(a: u128, b: u128) -> (u128, bool) { |
| 98 | + let mut oflow = 0; |
| 99 | + let r = a.addo(b, &mut oflow); |
| 100 | + (r, oflow != 0) |
| 101 | +} |
| 102 | + |
| 103 | +#[cfg_attr(not(stage0), lang = "i128_sub")] |
| 104 | +pub fn rust_i128_sub(a: i128, b: i128) -> i128 { |
| 105 | + rust_u128_sub(a as _, b as _) as _ |
| 106 | +} |
| 107 | +#[cfg_attr(not(stage0), lang = "i128_subo")] |
| 108 | +pub fn rust_i128_subo(a: i128, b: i128) -> (i128, bool) { |
| 109 | + let mut oflow = 0; |
| 110 | + let r = a.subo(b, &mut oflow); |
| 111 | + (r, oflow != 0) |
| 112 | +} |
| 113 | +#[cfg_attr(not(stage0), lang = "u128_sub")] |
| 114 | +pub fn rust_u128_sub(a: u128, b: u128) -> u128 { |
| 115 | + a.sub(b) |
| 116 | +} |
| 117 | +#[cfg_attr(not(stage0), lang = "u128_subo")] |
| 118 | +pub fn rust_u128_subo(a: u128, b: u128) -> (u128, bool) { |
| 119 | + let mut oflow = 0; |
| 120 | + let r = a.subo(b, &mut oflow); |
| 121 | + (r, oflow != 0) |
| 122 | +} |
0 commit comments