Fix iabs and add some more tests
This commit is contained in:
parent
ee69cd7925
commit
29e01af6a6
2 changed files with 20 additions and 7 deletions
|
@ -469,16 +469,25 @@ pub mod reimpls {
|
||||||
}
|
}
|
||||||
|
|
||||||
trait AbsExt: Sized {
|
trait AbsExt: Sized {
|
||||||
fn uabs(self) -> u128_;
|
|
||||||
fn iabs(self) -> i128_;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AbsExt for i128_ {
|
|
||||||
fn uabs(self) -> u128_ {
|
fn uabs(self) -> u128_ {
|
||||||
self.iabs() as u128_
|
self.iabs() as u128_
|
||||||
}
|
}
|
||||||
|
fn iabs(self) -> i128_;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(stage0)]
|
||||||
|
impl AbsExt for i128_ {
|
||||||
fn iabs(self) -> 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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -91,5 +91,9 @@ fn main() {
|
||||||
format!("{:b}", j));
|
format!("{:b}", j));
|
||||||
assert_eq!("-147573952589676412928", format!("{:?}", j));
|
assert_eq!("-147573952589676412928", format!("{:?}", j));
|
||||||
// common traits
|
// 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);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue