Author Topic: FPU power  (Read 6528 times)

mabdelouahab

  • Member
  • ***
  • Posts: 347
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 »
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

  • Member
  • *****
  • Posts: 8752
  • Still using Abacus 2.0
    • DednDave
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: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

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

  • Member
  • *****
  • Posts: 1383
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

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: 7765
  • Assembler is fun ;-)
    • MasmBasic
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?
 :P

Siekmanski

  • Member
  • *****
  • Posts: 1145
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)

rrr314159

  • Member
  • *****
  • Posts: 1383
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 :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

  • Member
  • *****
  • Posts: 1145
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.

rrr314159

  • Member
  • *****
  • Posts: 1383
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: 1145
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.

mabdelouahab

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

dedndave

  • Member
  • *****
  • Posts: 8752
  • Still using Abacus 2.0
    • DednDave
Re: FPU power
« Reply #13 on: October 06, 2015, 08:16:13 AM »
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

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

  • Member
  • ***
  • Posts: 347
Re: FPU power
« Reply #14 on: October 06, 2015, 10:00:41 AM »
 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