Skip to content

Commit 29e01af

Browse files
committed
Fix iabs and add some more tests
1 parent ee69cd7 commit 29e01af

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/libcompiler_builtins/lib.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -469,16 +469,25 @@ pub mod reimpls {
469469
}
470470

471471
trait AbsExt: Sized {
472-
fn uabs(self) -> u128_;
472+
fn uabs(self) -> u128_ {
473+
self.iabs() as u128_
474+
}
473475
fn iabs(self) -> i128_;
474476
}
475477

478+
#[cfg(stage0)]
476479
impl AbsExt for i128_ {
477-
fn uabs(self) -> u128_ {
478-
self.iabs() as u128_
480+
fn iabs(self) -> i128_ {
481+
let s = self >> 63;
482+
((self ^ s).wrapping_sub(s))
479483
}
484+
}
485+
486+
#[cfg(not(stage0))]
487+
impl AbsExt for i128_ {
480488
fn iabs(self) -> i128_ {
481-
((self ^ self).wrapping_sub(self))
489+
let s = self >> 127;
490+
((self ^ s).wrapping_sub(s))
482491
}
483492
}
484493

src/test/run-pass/i128.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,9 @@ fn main() {
9191
format!("{:b}", j));
9292
assert_eq!("-147573952589676412928", format!("{:?}", j));
9393
// common traits
94-
x.clone();
94+
assert_eq!(x, b(x.clone()));
95+
// overflow checks
96+
assert_eq!((-z).checked_mul(-z), Some(0x734C_C2F2_A521));
97+
assert_eq!((z).checked_mul(z), Some(0x734C_C2F2_A521));
98+
assert_eq!((k).checked_mul(k), None);
9599
}

0 commit comments

Comments
 (0)