Don't recalculate constants at every call in isqrt.
(cherry picked from commit 27869b1a37
)
This commit is contained in:
parent
147b11abe0
commit
ee4e4d0773
|
@ -54,11 +54,14 @@ constexpr T ct_sqrt(T x)
|
||||||
return sqrt_helper<T>(x, 0, x / 2 + 1);
|
return sqrt_helper<T>(x, 0, x / 2 + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static constexpr T sqrt_max_typed = ct_sqrt( std::numeric_limits<T>::max() );
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T isqrt(T x)
|
T isqrt(T x)
|
||||||
{
|
{
|
||||||
T r = (T) std::sqrt((double) x);
|
T r = (T) std::sqrt((double) x);
|
||||||
T sqrt_max = ct_sqrt(std::numeric_limits<T>::max());
|
T sqrt_max = sqrt_max_typed<T>;
|
||||||
|
|
||||||
while (r < sqrt_max && r * r < x)
|
while (r < sqrt_max && r * r < x)
|
||||||
r++;
|
r++;
|
||||||
|
|
Loading…
Reference in New Issue