math: move x86_64 fabs, fabsf to C with inline asm
authorAlexander Monakov <amonakov@ispras.ru>
Sun, 5 Jan 2020 15:47:02 +0000 (18:47 +0300)
committerRich Felker <dalias@aerifal.cx>
Tue, 24 Mar 2020 20:27:06 +0000 (16:27 -0400)
src/math/x86_64/fabs.c [new file with mode: 0644]
src/math/x86_64/fabs.s [deleted file]
src/math/x86_64/fabsf.c [new file with mode: 0644]
src/math/x86_64/fabsf.s [deleted file]

diff --git a/src/math/x86_64/fabs.c b/src/math/x86_64/fabs.c
new file mode 100644 (file)
index 0000000..1656247
--- /dev/null
@@ -0,0 +1,10 @@
+#include <math.h>
+
+double fabs(double x)
+{
+       double t;
+       __asm__ ("pcmpeqd %0, %0" : "=x"(t));          // t = ~0
+       __asm__ ("psrlq   $1, %0" : "+x"(t));          // t >>= 1
+       __asm__ ("andps   %1, %0" : "+x"(x) : "x"(t)); // x &= t
+       return x;
+}
diff --git a/src/math/x86_64/fabs.s b/src/math/x86_64/fabs.s
deleted file mode 100644 (file)
index 5715005..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-.global fabs
-.type fabs,@function
-fabs:
-       xor %eax,%eax
-       dec %rax
-       shr %rax
-       movq %rax,%xmm1
-       andpd %xmm1,%xmm0
-       ret
diff --git a/src/math/x86_64/fabsf.c b/src/math/x86_64/fabsf.c
new file mode 100644 (file)
index 0000000..36ea748
--- /dev/null
@@ -0,0 +1,10 @@
+#include <math.h>
+
+float fabsf(float x)
+{
+       float t;
+       __asm__ ("pcmpeqd %0, %0" : "=x"(t));          // t = ~0
+       __asm__ ("psrld   $1, %0" : "+x"(t));          // t >>= 1
+       __asm__ ("andps   %1, %0" : "+x"(x) : "x"(t)); // x &= t
+       return x;
+}
diff --git a/src/math/x86_64/fabsf.s b/src/math/x86_64/fabsf.s
deleted file mode 100644 (file)
index 501a1f1..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-.global fabsf
-.type fabsf,@function
-fabsf:
-       mov $0x7fffffff,%eax
-       movq %rax,%xmm1
-       andps %xmm1,%xmm0
-       ret