Skip to content

Commit

Permalink
improve sqrt performance for large numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
kthohr committed Sep 18, 2022
1 parent ece0770 commit 70b1267
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion include/gcem_incl/gcem_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
#endif

#ifndef GCEM_VERSION_PATCH
#define GCEM_VERSION_PATCH 0
#define GCEM_VERSION_PATCH 1
#endif

//
Expand Down
27 changes: 22 additions & 5 deletions include/gcem_incl/sqrt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,26 @@ noexcept
template<typename T>
constexpr
T
sqrt_check(const T x, const T m_val)
sqrt_simplify(const T x, const T m_val)
noexcept
{
return( x > T(1e+08) ? \
sqrt_simplify(x / T(1e+08), T(10000) * m_val) :
x > T(1e+06) ? \
sqrt_simplify(x / T(1e+06), T(1000) * m_val) :
x > T(10000) ? \
sqrt_simplify(x / T(10000), T(100) * m_val) :
x > T(100) ? \
sqrt_simplify(x / T(100), T(10) * m_val) :
x > T(4) ? \
sqrt_simplify(x / T(4), T(2) * m_val) :
m_val * sqrt_recur(x, x / T(2), 0) );
}

template<typename T>
constexpr
T
sqrt_check(const T x)
noexcept
{
return( is_nan(x) ? \
Expand All @@ -63,9 +82,7 @@ noexcept
GCLIM<T>::min() > abs(T(1) - x) ? \
x :
// else
x > T(4) ? \
sqrt_check(x/T(4), T(2)*m_val) :
m_val * sqrt_recur(x, x/T(2), 0) );
sqrt_simplify(x, T(1)) );
}

}
Expand All @@ -84,7 +101,7 @@ return_t<T>
sqrt(const T x)
noexcept
{
return internal::sqrt_check( static_cast<return_t<T>>(x), return_t<T>(1) );
return internal::sqrt_check( static_cast<return_t<T>>(x) );
}

#endif

0 comments on commit 70b1267

Please sign in to comment.