提交 87026f68 编写于 作者: A Alexander Monakov 提交者: Rich Felker

math: move x86_64 fabs, fabsf to C with inline asm

上级 33338ebc
#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;
}
.global fabs
.type fabs,@function
fabs:
xor %eax,%eax
dec %rax
shr %rax
movq %rax,%xmm1
andpd %xmm1,%xmm0
ret
#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;
}
.global fabsf
.type fabsf,@function
fabsf:
mov $0x7fffffff,%eax
movq %rax,%xmm1
andps %xmm1,%xmm0
ret
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册