News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

FPU power

Started by mabdelouahab, October 06, 2015, 02:19:01 AM

Previous topic - Next topic

mabdelouahab

I trying to find a FPU power function (x^y)

gelatine1

I believe FYL2X,FYL2XP1  and F2XM1 could help you out. http://www.ray.masmcode.com/tutorial/appen1.htm
That is because x^y = 2^(ylog_2(x)) (possible math mistake there...). I am not sure if theres no better solution.

dedndave

this is Ray's code, put into PROC form for REAL10's

f10Exp PROTO :LPVOID,:LPVOID,:LPVOID

;***********************************************************************************************

        OPTION  PROLOGUE:None
        OPTION  EPILOGUE:None

f10Exp PROC lpResult:LPVOID,lpBase:LPVOID,lpExponent:LPVOID

;0.0 raised to any exponent returns an indefinate qNaN
;any non-zero base raised to 0.0 returns 1.0

    mov     ecx,[esp+12]        ;ECX = lpExponent
    mov     edx,[esp+8]         ;EDX = lpBase
    mov     eax,[esp+4]         ;EAX = lpResult
    fld real10 ptr [ecx]
    fld real10 ptr [edx]
    fyl2x
    fld     st
    frndint
    fsub    st(1),st
    fxch    st(1)
    f2xm1
    fld1
    fadd
    fscale
    fstp    st(1)
    fstp real10 ptr [eax]
    ret     12

f10Exp ENDP

        OPTION  PROLOGUE:PrologueDef
        OPTION  EPILOGUE:EpilogueDef

;***********************************************************************************************

rrr314159

Cool ... Ray's original is in \masm32\fpulib\FpuXexpY.asm, with other good stuff. Your version is useful modification, easier to understand without his extensive inputs checking
I am NaN ;)

jj2007

include \masm32\MasmBasic\MasmBasic.inc      ; download
  Init
  PrintLine "exact:", Tb$, "1.41421356237309504880..."      ; Wolfram Alpha
  Inkey Str$("2^0.5=\t%Jf", ExpXY(2, 0.5))      ; more
EndOfCode

Output:
exact:  1.41421356237309504880...
2^0.5=  1.414213562373095049

rrr314159

@jj2007,

should have known MasmBasic has exp function! All it needs is a "kitch*n sink" function :P

p.s. if you spell the word "kitch*n" with the e, this forum software substitutes "kitchen". I, and others, have noted this strange interpolation b4
I am NaN ;)

jj2007

Did you know that the famous cuisine problem is a feature, not a bug?
:P

Siekmanski

Another one,

fld FLT4(0.5)
fld FLT4(2.0)
fyl2x
fld1
fld st(1)
fprem
f2xm1
faddp
fscale
fstp result ; result =  1.414213562373
fstp st(0)

Creative coders use backward thinking techniques as a strategy.

rrr314159

Quote from: jj2007Did you know that the famous cuisine problem is a feature, not a bug?

- No I didn't - and still don't :eusa_snooty: Some bored web-forum-software programmer's idea of a joke apparently :biggrin: But it's nice to get it straight finally, hope no other words are affected

@siekmanski,

*** Nit Pick Alert ***

fprem is slow, perhaps that's why Raymond uses frndint and fsub to get it. OTOH probably not, since speed is not a major concern for him in this library (as he states somewhere). fscale is also slow (per Agner Fog), he recommends shifting the float's exponent bits directly; but in this case the algo would have to be changed since that must be done to a float in memory. Possibly it's best to multiply by the appropriate power of 2; but actually best course would be to ignore this Nit entirely - unless speed is very important
I am NaN ;)

Siekmanski

Yes on most cpu's frndint and fsub is faster ( just checked Agner Fog ) On AMD fprem is a little bit faster than  frndint.
Creative coders use backward thinking techniques as a strategy.

rrr314159

thx that's useful to know, 2 bad AMD is so different speed-wise; another important example is fsin. Fast routines really should have different versions for Intel and AMD. Almost makes u want to use a (good) C++ optimizing compiler
I am NaN ;)

Siekmanski

Yeah, it's very difficult to write one "fastest routine" for all cpu's.
Creative coders use backward thinking techniques as a strategy.

mabdelouahab

Men ...  :icon_eek: , All these instructions, I thought it was just a single instruction like " EXP", I'm looking for a shorthand

dedndave

the "shorthand" you are looking for is in reply #2   :biggrin:

that is to put it in a PROC, then use it to do the work

        .DATA

r10Base REAL10 2.0
r10Exp  REAL10 3.0

        .DATA?

r10Result REAL10 ?

        .CODE

        INVOKE  f10Exp,offset r10Result,offset r10Base,offset r10Exp


when you're done, r10Result will be 8.0

mabdelouahab

 Ok Dave Thank you, :biggrin:
I've been using REAL8   , or should I use the conversion of REAL10 to REAL8?, Also How to print real8 by printf