Skip to content

Commit ba75010

Browse files
committed
Another attempt at making MIPS happy
(I really don't understand how arithmetic makes it segfault...)
1 parent bcc41a9 commit ba75010

File tree

5 files changed

+136
-136
lines changed

5 files changed

+136
-136
lines changed

build.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,15 @@ mod tests {
129129
Divsf3,
130130
Divdf3,
131131

132-
// int/add.rs
132+
// int/addsub.rs
133133
AddU128,
134134
AddI128,
135135
AddoU128,
136136
AddoI128,
137+
SubU128,
138+
SubI128,
139+
SuboU128,
140+
SuboI128,
137141

138142
// int/mul.rs
139143
Muldi3,
@@ -160,12 +164,6 @@ mod tests {
160164
Lshrdi3,
161165
Lshrti3,
162166

163-
// int/sub.rs
164-
SubU128,
165-
SubI128,
166-
SuboU128,
167-
SuboI128,
168-
169167
// int/udiv.rs
170168
Udivdi3,
171169
Udivmoddi4,
@@ -390,7 +388,7 @@ fn addsf3() {
390388

391389
fn prologue() -> &'static str {
392390
"
393-
use compiler_builtins::int::add::rust_u128_add;
391+
use compiler_builtins::int::addsub::rust_u128_add;
394392
395393
static TEST_CASES: &[((u128, u128), u128)] = &[
396394
"
@@ -448,7 +446,7 @@ fn u128_add() {
448446

449447
fn prologue() -> &'static str {
450448
"
451-
use compiler_builtins::int::add::rust_i128_add;
449+
use compiler_builtins::int::addsub::rust_i128_add;
452450
453451
static TEST_CASES: &[((i128, i128), i128)] = &[
454452
"
@@ -508,7 +506,7 @@ fn i128_add() {
508506

509507
fn prologue() -> &'static str {
510508
"
511-
use compiler_builtins::int::add::rust_u128_addo;
509+
use compiler_builtins::int::addsub::rust_u128_addo;
512510
513511
static TEST_CASES: &[((u128, u128), (u128, bool))] = &[
514512
"
@@ -568,7 +566,7 @@ fn u128_addo() {
568566

569567
fn prologue() -> &'static str {
570568
"
571-
use compiler_builtins::int::add::rust_i128_addo;
569+
use compiler_builtins::int::addsub::rust_i128_addo;
572570
573571
static TEST_CASES: &[((i128, i128), (i128, bool))] = &[
574572
"
@@ -3514,7 +3512,7 @@ fn subsf3() {
35143512

35153513
fn prologue() -> &'static str {
35163514
"
3517-
use compiler_builtins::int::sub::rust_u128_sub;
3515+
use compiler_builtins::int::addsub::rust_u128_sub;
35183516
35193517
static TEST_CASES: &[((u128, u128), u128)] = &[
35203518
"
@@ -3572,7 +3570,7 @@ fn u128_sub() {
35723570

35733571
fn prologue() -> &'static str {
35743572
"
3575-
use compiler_builtins::int::sub::rust_i128_sub;
3573+
use compiler_builtins::int::addsub::rust_i128_sub;
35763574
35773575
static TEST_CASES: &[((i128, i128), i128)] = &[
35783576
"
@@ -3632,7 +3630,7 @@ fn i128_sub() {
36323630

36333631
fn prologue() -> &'static str {
36343632
"
3635-
use compiler_builtins::int::sub::rust_u128_subo;
3633+
use compiler_builtins::int::addsub::rust_u128_subo;
36363634
36373635
static TEST_CASES: &[((u128, u128), (u128, bool))] = &[
36383636
"
@@ -3692,7 +3690,7 @@ fn u128_subo() {
36923690

36933691
fn prologue() -> &'static str {
36943692
"
3695-
use compiler_builtins::int::sub::rust_i128_subo;
3693+
use compiler_builtins::int::addsub::rust_i128_subo;
36963694
36973695
static TEST_CASES: &[((i128, i128), (i128, bool))] = &[
36983696
"

src/int/add.rs

-67
This file was deleted.

src/int/addsub.rs

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
}

src/int/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ macro_rules! os_ty {
1212
}
1313
}
1414

15-
pub mod add;
15+
pub mod addsub;
1616
pub mod mul;
1717
pub mod sdiv;
1818
pub mod shift;
19-
pub mod sub;
2019
pub mod udiv;
2120

2221
/// Trait for some basic operations on integers

src/int/sub.rs

-52
This file was deleted.

0 commit comments

Comments
 (0)