Skip to content

Commit c471c5a

Browse files
committed
Refactor addsub.rs
1 parent f521427 commit c471c5a

File tree

1 file changed

+14
-43
lines changed

1 file changed

+14
-43
lines changed

src/int/addsub.rs

+14-43
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,9 @@ trait Addo: AddSub
3939
where
4040
<Self as Int>::UnsignedInt: UAddSub,
4141
{
42-
fn addo(self, other: Self, overflow: &mut i32) -> Self {
43-
*overflow = 0;
44-
let result = AddSub::add(self, other);
45-
if other >= Self::ZERO {
46-
if result < self {
47-
*overflow = 1;
48-
}
49-
} else {
50-
if result >= self {
51-
*overflow = 1;
52-
}
53-
}
54-
result
42+
fn addo(self, other: Self) -> (Self, bool) {
43+
let sum = AddSub::add(self, other);
44+
(sum, (other < Self::ZERO) != (sum < self))
5545
}
5646
}
5747

@@ -62,19 +52,9 @@ trait Subo: AddSub
6252
where
6353
<Self as Int>::UnsignedInt: UAddSub,
6454
{
65-
fn subo(self, other: Self, overflow: &mut i32) -> Self {
66-
*overflow = 0;
67-
let result = AddSub::sub(self, other);
68-
if other >= Self::ZERO {
69-
if result > self {
70-
*overflow = 1;
71-
}
72-
} else {
73-
if result <= self {
74-
*overflow = 1;
75-
}
76-
}
77-
result
55+
fn subo(self, other: Self) -> (Self, bool) {
56+
let sum = AddSub::sub(self, other);
57+
(sum, (other < Self::ZERO) != (self < sum))
7858
}
7959
}
8060

@@ -83,43 +63,34 @@ impl Subo for u128 {}
8363

8464
intrinsics! {
8565
pub extern "C" fn __rust_i128_add(a: i128, b: i128) -> i128 {
86-
__rust_u128_add(a as _, b as _) as _
66+
AddSub::add(a,b)
8767
}
8868

8969
pub extern "C" fn __rust_i128_addo(a: i128, b: i128) -> (i128, bool) {
90-
let mut oflow = 0;
91-
let r = a.addo(b, &mut oflow);
92-
(r, oflow != 0)
70+
a.addo(b)
9371
}
9472

9573
pub extern "C" fn __rust_u128_add(a: u128, b: u128) -> u128 {
96-
a.add(b)
74+
AddSub::add(a,b)
9775
}
9876

9977
pub extern "C" fn __rust_u128_addo(a: u128, b: u128) -> (u128, bool) {
100-
let mut oflow = 0;
101-
let r = a.addo(b, &mut oflow);
102-
(r, oflow != 0)
78+
a.addo(b)
10379
}
10480

105-
10681
pub extern "C" fn __rust_i128_sub(a: i128, b: i128) -> i128 {
107-
__rust_u128_sub(a as _, b as _) as _
82+
AddSub::sub(a,b)
10883
}
10984

11085
pub extern "C" fn __rust_i128_subo(a: i128, b: i128) -> (i128, bool) {
111-
let mut oflow = 0;
112-
let r = a.subo(b, &mut oflow);
113-
(r, oflow != 0)
86+
a.subo(b)
11487
}
11588

11689
pub extern "C" fn __rust_u128_sub(a: u128, b: u128) -> u128 {
117-
a.sub(b)
90+
AddSub::sub(a,b)
11891
}
11992

12093
pub extern "C" fn __rust_u128_subo(a: u128, b: u128) -> (u128, bool) {
121-
let mut oflow = 0;
122-
let r = a.subo(b, &mut oflow);
123-
(r, oflow != 0)
94+
a.subo(b)
12495
}
12596
}

0 commit comments

Comments
 (0)