I am using Embarcadero C++ Builder 12 and I am having a giant problem with my power function in Assembly, its called vpow(base, exp)
. It works perfectly with integers, but I need it to work with doubles. My target is a common PC processor x86-64. Here is my function:
double vpow(double base, int exp) {
double result = 0.0;
__asm {
finit
fld base
fild exp
fyl2x
fld st(0)
frndint
fsub st(1), st(0)
fxch st(1)
f2xm1
fld1
fadd
fscale
fstp result
}
return result;
}
What am I doing wrong? I need it working for floating point bases. Like 2,5^3= 15,625. The exp, for now, will be integer. It's 64 bits assembly and it needs to run inline in the C++ Builder IDE.
I tried
double vpow(double base, int exp) {
double result = 0.0;
__asm {
finit
fld base
fild exp
fyl2x
fld st(0)
frndint
fsub st(1), st(0)
fxch st(1)
f2xm1
fld1
fadd
fscale
fstp result
}
return result;
}
I just need it to work for doubles too. It works for integers already!
fld
loads a floating-point value.fild
should be used to load an integer. But on x86_64 usually SSE operations are used instead of the FPU.exp
is an integer so you should load it withfild
. Running this line by line in a debugger would easily show you which line isn't giving correct results.