1

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!

20
  • Since assembly languages are specific to a processor, what processor are you targeting? Commented Aug 31 at 0:13
  • x86-64, friend. Commented Aug 31 at 0:14
  • 1
    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.
    – interjay
    Commented Aug 31 at 0:21
  • 1
    @TedLyngmo I am writing the entire cmath in Assembly to a Master degree project. And also to practice a lot because I will need to know assembly. Commented Aug 31 at 0:27
  • 1
    exp is an integer so you should load it with fild. Running this line by line in a debugger would easily show you which line isn't giving correct results.
    – interjay
    Commented Aug 31 at 0:36

1 Answer 1

8

The fix is simple. Read the specification of FYL2X or use a debugger. It is a bit odd to be using x87 instructions today on x64 - they are much slower than AVX2 (although still default code generation on some compilers).

FYL2X computes ST(1)*log2(ST(0)) you have loaded the arguments onto the stack in the wrong order! FINIT also removed since it isn't at all helpful.

If it appeared to work OK for integer arguments it can only be because of inadequate testing.

#include <stdio.h>

double vpow(double base, int exp) {
double result = 0.0;
__asm {
       fild exp   // note order of arguments swapped here
       fld base
   
       fyl2x
       fld st(0)
       frndint
       fsub st(1), st(0)
       fxch st(1)
       f2xm1
       fld1
       fadd
       fscale
       fstp result
    }
    return result;
}


int main()
{
   printf("%f\n", vpow(2.5, 2));
   for (int i=0; i<6; i++)
   printf("%18.9g\n", vpow(1.1, i));
}

Is a sufficient test case to validate that it works for doubles. Output is

6.250000
             1
           1.1
          1.21
         1.331
        1.4641
       1.61051
1
  • Thank you a lot, friend! It worked like a charm! Commented Aug 31 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.