Use std checked_div

This commit is contained in:
bjorn3 2019-07-25 14:55:55 +02:00
parent 718a5a2e3f
commit b46c8bfb7e

View File

@ -34,8 +34,8 @@ fn main() {
assert_eq!(0b0000000000000000000000000010000010000000000000000000000000000000_0000000000100000000000000000000000001000000000000100000000000000u128.leading_zeros(), 26); assert_eq!(0b0000000000000000000000000010000010000000000000000000000000000000_0000000000100000000000000000000000001000000000000100000000000000u128.leading_zeros(), 26);
assert_eq!(0b0000000000000000000000000010000000000000000000000000000000000000_0000000000000000000000000000000000001000000000000000000010000000u128.trailing_zeros(), 7); assert_eq!(0b0000000000000000000000000010000000000000000000000000000000000000_0000000000000000000000000000000000001000000000000000000010000000u128.trailing_zeros(), 7);
checked_div_i128(0i128, 2i128); 0i128.checked_div(2i128);
checked_div_u128(0u128, 2u128); 0u128.checked_div(2u128);
assert_eq!(1u128 + 2, 3); assert_eq!(1u128 + 2, 3);
assert_eq!(0b100010000000000000000000000000000u128 >> 10, 0b10001000000000000000000u128); assert_eq!(0b100010000000000000000000000000000u128 >> 10, 0b10001000000000000000000u128);
@ -61,18 +61,3 @@ fn map(a: Option<(u8, Box<Instruction>)>) -> Option<Box<Instruction>> {
Some((_, instr)) => Some(instr), Some((_, instr)) => Some(instr),
} }
} }
fn checked_div_i128(lhs: i128, rhs: i128) -> Option<i128> {
if rhs == 0 || (lhs == -170141183460469231731687303715884105728 && rhs == -1) {
None
} else {
Some(unsafe { intrinsics::unchecked_div(lhs, rhs) })
}
}
fn checked_div_u128(lhs: u128, rhs: u128) -> Option<u128> {
match rhs {
0 => None,
rhs => Some(unsafe { intrinsics::unchecked_div(lhs, rhs) })
}
}