isqrt: cite source and rename variables to match original C code
This commit is contained in:
parent
c97ab23141
commit
51463175a4
@ -1998,21 +1998,26 @@ macro_rules! uint_impl {
|
|||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut x = self;
|
// The algorithm is based on the one presented in
|
||||||
let mut c = 0;
|
// <https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Binary_numeral_system_(base_2)>
|
||||||
let mut d = 1 << (self.ilog2() & !1);
|
// which cites as source the following C code:
|
||||||
|
// <https://web.archive.org/web/20120306040058/http://medialab.freaknet.org/martin/src/sqrt/sqrt.c>.
|
||||||
|
|
||||||
while d != 0 {
|
let mut op = self;
|
||||||
if x >= c + d {
|
let mut res = 0;
|
||||||
x -= c + d;
|
let mut one = 1 << (self.ilog2() & !1);
|
||||||
c = (c >> 1) + d;
|
|
||||||
|
while one != 0 {
|
||||||
|
if op >= res + one {
|
||||||
|
op -= res + one;
|
||||||
|
res = (res >> 1) + one;
|
||||||
} else {
|
} else {
|
||||||
c >>= 1;
|
res >>= 1;
|
||||||
}
|
}
|
||||||
d >>= 2;
|
one >>= 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
c
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Performs Euclidean division.
|
/// Performs Euclidean division.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user