1

Is there any way to compute powers of floating point numbers purely in x86/x87 assembly? I'm not talking about computing integral powers of floating point numbers, but something like x^y where both x and y are floating point numbers.

I have a solution for integral powers (NASM syntax):

compute_integral_powers:
    fldl [esp+4]              ;load base twice
    fld  st0
    movl ecx, [esp+12]        ;load exponent
.loop:
    decl ecx                  ;decrement exponent
    cmpl ecx, 0               ;are we done yet?
    jz   .done
    fmul st1                  ;multiply by original base
    jmp  .loop                ;keep going
.done:
    fstp st1                  ;store st0 in st1 and pop (for return value)
    ret

Is there something similar to compute powers using floating point exponents, using only x86/x87 assembly instructions?

owacoder
  • 4,815
  • 20
  • 47
  • 1
    If you compile a C implementation of the standard function `pow` to assembly then you'll have a floating-point implementation using only x86/x87 assembly instructions. Starting from a typical implementation with argument reduction followed by polynomial approximation, you can expect a couple hundred instructions for each (less if we are talking of a function for a precision narrower than the maximum available to the FPU). – Pascal Cuoq Aug 29 '15 at 20:04
  • Unless you are looking for the last drop of performance there is no reason to write this function in assembly. If `call pow` is an acceptable implementation with x86/87 instructions, you should definitely use that. – Pascal Cuoq Aug 29 '15 at 20:15
  • 1
    If you only care about x87 (and not about the SSE instructions), then look into the FYL2X and F2XM1 x87 instructions. – Mark Dickinson Aug 29 '15 at 20:16
  • See also this related question and answer: http://stackoverflow.com/a/4638502/270986 – Mark Dickinson Aug 29 '15 at 20:19
  • 3
    Related: http://stackoverflow.com/questions/4638473/how-to-powreal-real-in-x86 – Ross Ridge Aug 29 '15 at 20:19
  • @PascalCuoq - I'm writing this for a runtime library, and thus have no access to `call pow`. – owacoder Aug 30 '15 at 01:56

0 Answers0