Skip to content

Commit

Permalink
Fix iabs and add some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
est31 committed Dec 31, 2016
1 parent ee69cd7 commit 29e01af
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
17 changes: 13 additions & 4 deletions src/libcompiler_builtins/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,16 +469,25 @@ pub mod reimpls {
}

trait AbsExt: Sized {
fn uabs(self) -> u128_;
fn uabs(self) -> u128_ {
self.iabs() as u128_
}
fn iabs(self) -> i128_;
}

#[cfg(stage0)]
impl AbsExt for i128_ {
fn uabs(self) -> u128_ {
self.iabs() as u128_
fn iabs(self) -> i128_ {
let s = self >> 63;
((self ^ s).wrapping_sub(s))
}
}

#[cfg(not(stage0))]
impl AbsExt for i128_ {
fn iabs(self) -> i128_ {
((self ^ self).wrapping_sub(self))
let s = self >> 127;
((self ^ s).wrapping_sub(s))
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/test/run-pass/i128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,9 @@ fn main() {
format!("{:b}", j));
assert_eq!("-147573952589676412928", format!("{:?}", j));
// common traits
x.clone();
assert_eq!(x, b(x.clone()));
// overflow checks
assert_eq!((-z).checked_mul(-z), Some(0x734C_C2F2_A521));
assert_eq!((z).checked_mul(z), Some(0x734C_C2F2_A521));
assert_eq!((k).checked_mul(k), None);
}

0 comments on commit 29e01af

Please sign in to comment.