Originally Posted by
rempas
Yeah but why call another function and make it run slower? Isn't my example working?
If you are refering to fabs() it's not a function call, since the only thing this function does is to zero the msb. floor() can be avoided, since we are only truncating a positive value:
Code:
#include <math.h>
unsigned int intdigits(double x)
{
x = log10(fabs(x));
return x >= 0 ? x + 1 : 1;
}
In x86-64 code this will be transtaled to:
Code:
bits 64
default rel
section .text
extern log10
global intdigits
align 16
intdigits:
sub rsp,8
vandpd xmm0,xmm0,[.LC0]
call log10
vcomisd xmm0,[.LC1]
jb .L6
vaddsd xmm0,xmm0,[.LC2]
add rsp,8
vcvttsd2si rax,xmm0
ret
align 4
.L6:
mov eax,1
add rsp,8
ret
section .rodata
align 16
.LC0:
dq ~(1 << 63)
dq 0
align 8
.LC1:
dq 0.0
.LC2:
dq 1.0
Notice: Only one branch and if log10(fabs(x)) < 0.