Skip to content

Commit d5f42d4

Browse files
committed
Implement 128bit checked add and sub
1 parent eb0a731 commit d5f42d4

File tree

3 files changed

+17
-37
lines changed

3 files changed

+17
-37
lines changed

example/mini_core.rs

-35
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,10 @@ unsafe impl Copy for u8 {}
4444
unsafe impl Copy for u16 {}
4545
unsafe impl Copy for u32 {}
4646
unsafe impl Copy for u64 {}
47-
unsafe impl Copy for u128 {}
4847
unsafe impl Copy for usize {}
4948
unsafe impl Copy for i8 {}
5049
unsafe impl Copy for i16 {}
5150
unsafe impl Copy for i32 {}
52-
unsafe impl Copy for i128 {}
5351
unsafe impl Copy for isize {}
5452
unsafe impl Copy for char {}
5553
unsafe impl<'a, T: ?Sized> Copy for &'a T {}
@@ -138,22 +136,6 @@ impl Add for usize {
138136
}
139137
}
140138

141-
impl Add for u128 {
142-
type Output = Self;
143-
144-
fn add(self, rhs: Self) -> Self {
145-
self + rhs
146-
}
147-
}
148-
149-
impl Add for i128 {
150-
type Output = Self;
151-
152-
fn add(self, rhs: Self) -> Self {
153-
self + rhs
154-
}
155-
}
156-
157139
#[lang = "sub"]
158140
pub trait Sub<RHS = Self> {
159141
type Output;
@@ -254,15 +236,6 @@ impl PartialEq for i32 {
254236
}
255237
}
256238

257-
impl PartialEq for i128 {
258-
fn eq(&self, other: &i128) -> bool {
259-
(*self) == (*other)
260-
}
261-
fn ne(&self, other: &i128) -> bool {
262-
(*self) != (*other)
263-
}
264-
}
265-
266239
impl PartialEq for isize {
267240
fn eq(&self, other: &isize) -> bool {
268241
(*self) == (*other)
@@ -297,14 +270,6 @@ pub trait Neg {
297270
fn neg(self) -> Self::Output;
298271
}
299272

300-
impl Neg for i128 {
301-
type Output = i128;
302-
303-
fn neg(self) -> i128 {
304-
-self
305-
}
306-
}
307-
308273
impl Neg for isize {
309274
type Output = isize;
310275

example/std_example.rs

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ fn main() {
2323
checked_div_u128(0u128, 2u128);
2424
assert_eq!(1u128 + 2, 3);
2525

26+
// overflow panic
27+
// 0xFEDCBA987654321123456789ABCDEFu128 + 0xFEDCBA987654321123456789ABCDEFu128;
28+
2629
println!("{}", 0b100010000000000000000000000000000u128 >> 10);
2730
println!("{}", 0xFEDCBA987654321123456789ABCDEFu128 >> 64);
2831
println!("{} >> 64 == {}", 0xFEDCBA987654321123456789ABCDEFu128 as i128, 0xFEDCBA987654321123456789ABCDEFu128 as i128 >> 64);

src/codegen_i128.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,20 @@ pub fn maybe_codegen<'a, 'tcx>(
2323
assert!(!checked);
2424
return None;
2525
}
26-
BinOp::Add | BinOp::Sub => {
27-
return None; // FIXME implement checked versions
26+
BinOp::Add | BinOp::Sub if !checked => return None,
27+
BinOp::Add => {
28+
return Some(if is_signed {
29+
fx.easy_call("__rust_i128_addo", &[lhs, rhs], out_ty)
30+
} else {
31+
fx.easy_call("__rust_u128_addo", &[lhs, rhs], out_ty)
32+
})
33+
}
34+
BinOp::Sub => {
35+
return Some(if is_signed {
36+
fx.easy_call("__rust_i128_subo", &[lhs, rhs], out_ty)
37+
} else {
38+
fx.easy_call("__rust_u128_subo", &[lhs, rhs], out_ty)
39+
})
2840
}
2941
BinOp::Offset => unreachable!("offset should only be used on pointers, not 128bit ints"),
3042
BinOp::Mul => {

0 commit comments

Comments
 (0)