### Author Topic: FPU power  (Read 9410 times)

#### mabdelouahab

• Member
• Posts: 364
##### FPU power
« on: October 06, 2015, 02:19:01 AM »
I trying to find a FPU power function (x^y)

#### gelatine1

• Member
• Posts: 217
##### Re: FPU power
« Reply #1 on: October 06, 2015, 02:39:05 AM »
That is because x^y = 2^(ylog_2(x)) (possible math mistake there...). I am not sure if theres no better solution.

#### dedndave

• Member
• Posts: 8790
• Still using Abacus 2.0
##### Re: FPU power
« Reply #2 on: October 06, 2015, 03:19:21 AM »
this is Ray's code, put into PROC form for REAL10's

Code: [Select]
`f10Exp PROTO :LPVOID,:LPVOID,:LPVOID`
Code: [Select]
`;***********************************************************************************************        OPTION  PROLOGUE:None        OPTION  EPILOGUE:Nonef10Exp 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     12f10Exp ENDP        OPTION  PROLOGUE:PrologueDef        OPTION  EPILOGUE:EpilogueDef;***********************************************************************************************`

#### rrr314159

• Member
• Posts: 1382
##### Re: FPU power
« Reply #3 on: October 06, 2015, 04:44:08 AM »
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

• Member
• Posts: 8522
• Assembler is fun ;-)
##### Re: FPU power
« Reply #4 on: October 06, 2015, 04:48:20 AM »
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

• Member
• Posts: 1382
##### Re: FPU power
« Reply #5 on: October 06, 2015, 05:58:39 AM »
@jj2007,

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

p.s. if you spell the word "kitch*n" with the e, this forum software substitutes " your link does not work IDIOT ". I, and others, have noted this strange interpolation b4
I am NaN

#### jj2007

• Member
• Posts: 8522
• Assembler is fun ;-)
##### Re: FPU power
« Reply #6 on: October 06, 2015, 06:05:29 AM »
Did you know that the famous cuisine problem is a feature, not a bug?

#### Siekmanski

• Member
• Posts: 1553
##### Re: FPU power
« Reply #7 on: October 06, 2015, 06:17:31 AM »
Another one,

Code: [Select]
` 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 their strategy.

#### rrr314159

• Member
• Posts: 1382
##### Re: FPU power
« Reply #8 on: October 06, 2015, 06:43:46 AM »
Quote from: jj2007
Did you know that the famous cuisine problem is a feature, not a bug?

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

@siekmanski,

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

• Member
• Posts: 1553
##### Re: FPU power
« Reply #9 on: October 06, 2015, 07:13:00 AM »
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 their strategy.

#### rrr314159

• Member
• Posts: 1382
##### Re: FPU power
« Reply #10 on: October 06, 2015, 07:16:51 AM »
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

• Member
• Posts: 1553
##### Re: FPU power
« Reply #11 on: October 06, 2015, 07:21:19 AM »
Yeah, it's very difficult to write one "fastest routine" for all cpu's.
Creative coders use backward thinking techniques as their strategy.

#### mabdelouahab

• Member
• Posts: 364
##### Re: FPU power
« Reply #12 on: October 06, 2015, 07:55:10 AM »
Men ...  , All these instructions, I thought it was just a single instruction like " EXP", I'm looking for a shorthand

#### dedndave

• Member
• Posts: 8790
• Still using Abacus 2.0
##### Re: FPU power
« Reply #13 on: October 06, 2015, 08:16:13 AM »
the "shorthand" you are looking for is in reply #2

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

Code: [Select]
`        .DATAr10Base REAL10 2.0r10Exp  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

• Member
• Posts: 364
##### Re: FPU power
« Reply #14 on: October 06, 2015, 10:00:41 AM »
Ok Dave Thank you,
I've been using REAL8   , or should I use the conversion of REAL10 to REAL8?, Also How to print real8 by printf