From acfe6d033eafe12d61ad91f496850b1bf58c6f98 Mon Sep 17 00:00:00 2001 From: Alexander Monakov Date: Tue, 7 Jan 2020 15:53:03 +0300 Subject: math: move i386 sqrt to C with inline asm --- src/math/i386/sqrt.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/math/i386/sqrt.c (limited to 'src/math/i386/sqrt.c') diff --git a/src/math/i386/sqrt.c b/src/math/i386/sqrt.c new file mode 100644 index 00000000..934fbcca --- /dev/null +++ b/src/math/i386/sqrt.c @@ -0,0 +1,15 @@ +#include "libm.h" + +double sqrt(double x) +{ + union ldshape ux; + unsigned fpsr; + __asm__ ("fsqrt; fnstsw %%ax": "=t"(ux.f), "=a"(fpsr) : "0"(x)); + if ((ux.i.m & 0x7ff) != 0x400) + return (double)ux.f; + /* Rounding to double would have encountered an exact halfway case. + Adjust mantissa downwards if fsqrt rounded up, else upwards. + (result of fsqrt could not have been exact) */ + ux.i.m ^= (fpsr & 0x200) + 0x300; + return (double)ux.f; +} -- cgit v1.2.3-70-g09d2