Don't recalculate constants at every call in isqrt.

(cherry picked from commit 27869b1a37)
This commit is contained in:
Alex 2022-07-14 18:42:48 +03:00 committed by Seth Hillbrand
parent 147b11abe0
commit ee4e4d0773
1 changed files with 4 additions and 1 deletions

View File

@ -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++;