diff --git a/AK/Math.h b/AK/Math.h index 625dff549c..20c1cd23a0 100644 --- a/AK/Math.h +++ b/AK/Math.h @@ -106,6 +106,40 @@ constexpr T sqrt(T x) #endif } +template +constexpr T rsqrt(T x) +{ + return (T)1. / sqrt(x); +} + +#ifdef __SSE__ +template<> +constexpr float sqrt(float x) +{ + if (is_constant_evaluated()) + return __builtin_sqrtf(x); + + float res; + asm("sqrtss %1, %0" + : "=x"(res) + : "x"(x)); + return res; +} + +template<> +constexpr float rsqrt(float x) +{ + if (is_constant_evaluated()) + return 1.f / __builtin_sqrtf(x); + + float res; + asm("rsqrtss %1, %0" + : "=x"(res) + : "x"(x)); + return res; +} +#endif + template constexpr T cbrt(T x) {