From 29e01af6a68817a12c1fc5fa04c483d2200c3cbb Mon Sep 17 00:00:00 2001 From: est31 Date: Sat, 31 Dec 2016 18:16:55 +0100 Subject: [PATCH] Fix iabs and add some more tests --- src/libcompiler_builtins/lib.rs | 17 +++++++++++++---- src/test/run-pass/i128.rs | 6 +++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/libcompiler_builtins/lib.rs b/src/libcompiler_builtins/lib.rs index 1bb84c9fc0256..1bc9b66000124 100644 --- a/src/libcompiler_builtins/lib.rs +++ b/src/libcompiler_builtins/lib.rs @@ -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)) } } diff --git a/src/test/run-pass/i128.rs b/src/test/run-pass/i128.rs index 71d3e6491cbad..b7aeb21229ccb 100644 --- a/src/test/run-pass/i128.rs +++ b/src/test/run-pass/i128.rs @@ -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); }