Made it panic on gcd = min_val in debug mode

The additional call to .abs() causes a panic on the min value. There are
no other negative values it can encounter.
Does nothing in release mode
This commit is contained in:
Emerentius 2015-09-11 20:22:57 +02:00
parent 82a6ab2f30
commit 59089d9d5c
1 changed files with 10 additions and 7 deletions

View File

@ -225,14 +225,17 @@ macro_rules! impl_integer_for_isize {
// find common factors of 2 // find common factors of 2
let shift = (m | n).trailing_zeros(); let shift = (m | n).trailing_zeros();
// If one number is the minimum value, it cannot be represented as a // The algorithm needs positive numbers, but the minimum value
// positive number. It's also a power of two, so the gcd can // can't be represented as a positive one.
// trivially be calculated in that case by bitshifting // It's also a power of two, so the gcd can be
// calculated by bitshifting in that case
// The result is always positive in two's complement, unless // Assuming two's complement, the number created by the shift
// n and m are the minimum value, then it's negative // is positive for all numbers except gcd = abs(min value)
// no other way to represent that number // The call to .abs() causes a panic in debug mode
if m == <$T>::min_value() || n == <$T>::min_value() { return 1 << shift } if m == <$T>::min_value() || n == <$T>::min_value() {
return (1 << shift).abs()
}
// guaranteed to be positive now, rest like unsigned algorithm // guaranteed to be positive now, rest like unsigned algorithm
m = m.abs(); m = m.abs();