Fix iabs and add some more tests

This commit is contained in:
est31 2016-12-31 18:16:55 +01:00
parent ee69cd7925
commit 29e01af6a6
2 changed files with 20 additions and 7 deletions

View File

@ -469,16 +469,25 @@ pub mod reimpls {
}
trait AbsExt: Sized {
fn uabs(self) -> u128_;
fn iabs(self) -> i128_;
}
impl AbsExt for i128_ {
fn uabs(self) -> u128_ {
self.iabs() as u128_
}
fn iabs(self) -> i128_;
}
#[cfg(stage0)]
impl AbsExt for i128_ {
fn iabs(self) -> i128_ {
((self ^ self).wrapping_sub(self))
let s = self >> 63;
((self ^ s).wrapping_sub(s))
}
}
#[cfg(not(stage0))]
impl AbsExt for i128_ {
fn iabs(self) -> i128_ {
let s = self >> 127;
((self ^ s).wrapping_sub(s))
}
}

View File

@ -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);
}