diff --git a/library/core/src/num/uint_macros.rs b/library/core/src/num/uint_macros.rs index 5de672d5d7e..371d6e180c2 100644 --- a/library/core/src/num/uint_macros.rs +++ b/library/core/src/num/uint_macros.rs @@ -1998,21 +1998,26 @@ macro_rules! uint_impl { return self; } - let mut x = self; - let mut c = 0; - let mut d = 1 << (self.ilog2() & !1); + // The algorithm is based on the one presented in + // + // which cites as source the following C code: + // . - while d != 0 { - if x >= c + d { - x -= c + d; - c = (c >> 1) + d; + let mut op = self; + let mut res = 0; + let mut one = 1 << (self.ilog2() & !1); + + while one != 0 { + if op >= res + one { + op -= res + one; + res = (res >> 1) + one; } else { - c >>= 1; + res >>= 1; } - d >>= 2; + one >>= 2; } - c + res } /// Performs Euclidean division.