summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorHendiadyoin1 <leon.a@serenityos.org>2022-04-01 13:46:15 +0200
committerAndreas Kling <kling@serenityos.org>2022-04-02 18:37:38 +0200
commitb1db1e4e4f90e4574888230b699d2191607d33b6 (patch)
tree8e03065716dc1ed667e26b6354c012d505a0ba56 /AK
parent50c4ba07563c32dc023a9805e5e643a089ebcca0 (diff)
downloadserenity-b1db1e4e4f90e4574888230b699d2191607d33b6.zip
AK: Add rsqrt and a SSE specific implementation for sqrt
Diffstat (limited to 'AK')
-rw-r--r--AK/Math.h34
1 files changed, 34 insertions, 0 deletions
diff --git a/AK/Math.h b/AK/Math.h
index 625dff549c..20c1cd23a0 100644
--- a/AK/Math.h
+++ b/AK/Math.h
@@ -107,6 +107,40 @@ constexpr T sqrt(T x)
}
template<FloatingPoint T>
+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<FloatingPoint T>
constexpr T cbrt(T x)
{
CONSTEXPR_STATE(cbrt, x);