Author Topic: FPU power  (Read 6457 times)

rrr314159

  • Member
  • *****
  • Posts: 1383
Re: FPU power
« Reply #15 on: October 06, 2015, 10:29:00 AM »
print real8 variables with, for instance "%.4g", there are many other format options available also. Both real4 and real10 are not so easy. If you don't care about the extra precision of real10 simplest would be to stick with real8, just convert all the 10's to 8's in dedndave's code
I am NaN ;)

mabdelouahab

  • Member
  • ***
  • Posts: 343
Re: FPU power
« Reply #16 on: October 06, 2015, 11:05:31 AM »
I Thank you rrr314159, It works  :t

rrr314159

  • Member
  • *****
  • Posts: 1383
Re: FPU power
« Reply #17 on: October 06, 2015, 11:16:35 AM »
You're welcome it's a privilege to help such a hard worker as yourself

@Siekmanski et al,

It occurred to me, fyl2x and f2xm1 are similar to fsin; they use a power series to compute the function. And fsin is incredibly slow on Intel, can be beat by factor of 100, as we found out in the Trigonometry thread in Laboratory. Checked Agner, turns out these are also very slow! (On Prescott particularly, probably applies to modern Intels). So should be easy to go much faster. Essentially that would entail going back to the way we did it in the good old days, roll-your-own power series; PITA, but worth it if you need speed. If I get around to it I'll start a Laboratory thread on the topic. Of course at my age that might take a year, anyone else is welcome to look into it
I am NaN ;)

dedndave

  • Member
  • *****
  • Posts: 8749
  • Still using Abacus 2.0
    • DednDave
Re: FPU power
« Reply #18 on: October 06, 2015, 11:27:38 AM »
the REAL10 proc can be easily modifed to REAL8
i use REAL10's because that's the "native" size for the FPU, internally

notice that, when you load REAL8's into the FPU, it converts them to REAL10's internally
hopefully, the result is in range when it saves the result to a REAL8 when done   :P

Code: [Select]
f8Exp PROTO :LPVOID,:LPVOID,:LPVOID
Code: [Select]
;***********************************************************************************************

        OPTION  PROLOGUE:None
        OPTION  EPILOGUE:None

f8Exp 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 real8 ptr [ecx]
    fld real8 ptr [edx]
    fyl2x
    fld     st
    frndint
    fsub    st(1),st
    fxch    st(1)
    f2xm1
    fld1
    fadd
    fscale
    fstp    st(1)
    fstp real8 ptr [eax]
    ret     12

f8Exp ENDP

        OPTION  PROLOGUE:PrologueDef
        OPTION  EPILOGUE:EpilogueDef

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

Siekmanski

  • Member
  • *****
  • Posts: 1144
Re: FPU power
« Reply #19 on: October 06, 2015, 08:28:12 PM »
It would be a nice challenge to come up with a faster power function.
Wish I had more time to give it a try.....

jj2007

  • Member
  • *****
  • Posts: 7737
  • Assembler is fun ;-)
    • MasmBasic
Re: FPU power
« Reply #20 on: October 06, 2015, 08:54:36 PM »
It would be a nice challenge to come up with a faster power function.

Testbed attached :biggrin:

Code: [Select]
Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz (SSE4)
24947   cycles for 100 * f8Exp (Dave)
12338   cycles for 100 * ExpXY (MasmBasic)
25097   cycles for 100 * pow (CRT)

24944   cycles for 100 * f8Exp (Dave)
12329   cycles for 100 * ExpXY (MasmBasic)
24983   cycles for 100 * pow (CRT)

24959   cycles for 100 * f8Exp (Dave)
12298   cycles for 100 * ExpXY (MasmBasic)
24983   cycles for 100 * pow (CRT)

24947   cycles for 100 * f8Exp (Dave)
12345   cycles for 100 * ExpXY (MasmBasic)
24990   cycles for 100 * pow (CRT)

24957   cycles for 100 * f8Exp (Dave)
12328   cycles for 100 * ExpXY (MasmBasic)
24972   cycles for 100 * pow (CRT)

1.41421356237310 for f8Exp (Dave, 2^0.5)
1.73205080756888 for ExpXY (MasmBasic, 3^0.5)
2.00000000000000 for pow (CRT, 4^0.5)

TWell

  • Member
  • ****
  • Posts: 748
Re: FPU power
« Reply #21 on: October 06, 2015, 09:36:01 PM »
Code: [Select]
AMD E-450 APU with Radeon(tm) HD Graphics (SSE4)

20886   cycles for 100 * f8Exp (Dave, 2^0.5)
18506   cycles for 100 * ExpXY (MasmBasic, 3^0.5)
30764   cycles for 100 * pow (CRT, 4^0.5)

20947   cycles for 100 * f8Exp (Dave, 2^0.5)
18486   cycles for 100 * ExpXY (MasmBasic, 3^0.5)
31182   cycles for 100 * pow (CRT, 4^0.5)

21028   cycles for 100 * f8Exp (Dave, 2^0.5)
18480   cycles for 100 * ExpXY (MasmBasic, 3^0.5)
30621   cycles for 100 * pow (CRT, 4^0.5)

21278   cycles for 100 * f8Exp (Dave, 2^0.5)
18486   cycles for 100 * ExpXY (MasmBasic, 3^0.5)
30769   cycles for 100 * pow (CRT, 4^0.5)

20891   cycles for 100 * f8Exp (Dave, 2^0.5)
18572   cycles for 100 * ExpXY (MasmBasic, 3^0.5)
30608   cycles for 100 * pow (CRT, 4^0.5)

1.41421356237310 for f8Exp (Dave, 2^0.5)
1.73205080756888 for ExpXY (MasmBasic, 3^0.5)
2.00000000000000 for pow (CRT, 4^0.5)

gelatine1

  • Member
  • **
  • Posts: 217
Re: FPU power
« Reply #22 on: October 06, 2015, 09:39:09 PM »
Code: [Select]
Intel(R) Core(TM) i5-2430M CPU @ 2.40GHz (SSE4)

25147   cycles for 100 * f8Exp (Dave, 2^0.5)
12421   cycles for 100 * ExpXY (MasmBasic, 3^0.5)
25152   cycles for 100 * pow (CRT, 4^0.5)

25197   cycles for 100 * f8Exp (Dave, 2^0.5)
12404   cycles for 100 * ExpXY (MasmBasic, 3^0.5)
25155   cycles for 100 * pow (CRT, 4^0.5)

25099   cycles for 100 * f8Exp (Dave, 2^0.5)
12393   cycles for 100 * ExpXY (MasmBasic, 3^0.5)
25162   cycles for 100 * pow (CRT, 4^0.5)

25092   cycles for 100 * f8Exp (Dave, 2^0.5)
12388   cycles for 100 * ExpXY (MasmBasic, 3^0.5)
25194   cycles for 100 * pow (CRT, 4^0.5)

25115   cycles for 100 * f8Exp (Dave, 2^0.5)
12417   cycles for 100 * ExpXY (MasmBasic, 3^0.5)
25116   cycles for 100 * pow (CRT, 4^0.5)

1.41421356237310 for f8Exp (Dave, 2^0.5)
1.73205080756888 for ExpXY (MasmBasic, 3^0.5)
2.00000000000000 for pow (CRT, 4^0.5)

Siekmanski

  • Member
  • *****
  • Posts: 1144
Re: FPU power
« Reply #23 on: October 06, 2015, 10:33:55 PM »
Code: [Select]
Intel(R) Core(TM) i7-4930K CPU @ 3.40GHz (SSE4)

25032   cycles for 100 * f8Exp (Dave, 2^0.5)
12318   cycles for 100 * ExpXY (MasmBasic, 3^0.5)
25215   cycles for 100 * pow (CRT, 4^0.5)

25034   cycles for 100 * f8Exp (Dave, 2^0.5)
12314   cycles for 100 * ExpXY (MasmBasic, 3^0.5)
25169   cycles for 100 * pow (CRT, 4^0.5)

25055   cycles for 100 * f8Exp (Dave, 2^0.5)
12332   cycles for 100 * ExpXY (MasmBasic, 3^0.5)
25148   cycles for 100 * pow (CRT, 4^0.5)

25066   cycles for 100 * f8Exp (Dave, 2^0.5)
12312   cycles for 100 * ExpXY (MasmBasic, 3^0.5)
25172   cycles for 100 * pow (CRT, 4^0.5)

25048   cycles for 100 * f8Exp (Dave, 2^0.5)
12302   cycles for 100 * ExpXY (MasmBasic, 3^0.5)
25132   cycles for 100 * pow (CRT, 4^0.5)

1.41421356237310 for f8Exp (Dave, 2^0.5)
1.73205080756888 for ExpXY (MasmBasic, 3^0.5)
2.00000000000000 for pow (CRT, 4^0.5)

--- ok ---

rrr314159

  • Member
  • *****
  • Posts: 1383
Re: FPU power
« Reply #24 on: October 07, 2015, 12:31:15 AM »
Thanks jj,

Obvious question, why is ExpXY faster? Aren't you (MasmBasic) using the same instructions: fyl2x and f2xm1; fprem or frndint; and fscale? If instead you're using a traditional algo (pre- fyl2x and f2xm1) that may explain it.

Another obvious one, why different test cases for the three "Units Under Test": square roots of 2, 3, and 4? Judging by the numbers it's not a problem. For some implementations 4^0.5 might go a lot faster, but obviously not with CRT pow. I guess we can suppose any inputs will go at the same speed.

Evidently ExpXY gets less advantage (tho still ahead) with AMD, which (judging by fsin) may have much better implementations of fyl2x and f2xm1. That would make sense if you're using a traditional algo which implements its own power series (for ln and exp)

Code: [Select]
AMD A6-6310 APU with AMD Radeon R4 Graphics     (SSE4)

18776   cycles for 100 * f8Exp (Dave, 2^0.5)
17133   cycles for 100 * ExpXY (MasmBasic, 3^0.5)
22233   cycles for 100 * pow (CRT, 4^0.5)

18549   cycles for 100 * f8Exp (Dave, 2^0.5)
17595   cycles for 100 * ExpXY (MasmBasic, 3^0.5)
22059   cycles for 100 * pow (CRT, 4^0.5)

18576   cycles for 100 * f8Exp (Dave, 2^0.5)
17082   cycles for 100 * ExpXY (MasmBasic, 3^0.5)
22169   cycles for 100 * pow (CRT, 4^0.5)

18787   cycles for 100 * f8Exp (Dave, 2^0.5)
17142   cycles for 100 * ExpXY (MasmBasic, 3^0.5)
22370   cycles for 100 * pow (CRT, 4^0.5)

18554   cycles for 100 * f8Exp (Dave, 2^0.5)
17172   cycles for 100 * ExpXY (MasmBasic, 3^0.5)
22242   cycles for 100 * pow (CRT, 4^0.5)

1.41421356237310 for f8Exp (Dave, 2^0.5)
1.73205080756888 for ExpXY (MasmBasic, 3^0.5)
2.00000000000000 for pow (CRT, 4^0.5)

--- ok ---

I am NaN ;)

jj2007

  • Member
  • *****
  • Posts: 7737
  • Assembler is fun ;-)
    • MasmBasic
Re: FPU power
« Reply #25 on: October 07, 2015, 12:36:38 AM »
I guess we can suppose any inputs will go at the same speed.

That's an interesting hypothesis (and it was mine, too), but unfortunately it is strongly at odds with reality 8)

rrr314159

  • Member
  • *****
  • Posts: 1383
Re: FPU power
« Reply #26 on: October 07, 2015, 12:45:57 AM »
So ... wouldn't it be better to test them with the same inputs? Not that I want to make extra work for you. Only 4^0.5 ought to go faster, and since it's used with the slowest algo, the results should be good anyway
I am NaN ;)

mabdelouahab

  • Member
  • ***
  • Posts: 343
Re: FPU power
« Reply #27 on: October 07, 2015, 12:49:27 AM »
Code: [Select]
Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz (SSE4)

28576   cycles for 100 * f8Exp (Dave, 2^0.5)
13867   cycles for 100 * ExpXY (MasmBasic, 3^0.5)
30481   cycles for 100 * pow (CRT, 4^0.5)

28425   cycles for 100 * f8Exp (Dave, 2^0.5)
13873   cycles for 100 * ExpXY (MasmBasic, 3^0.5)
27419   cycles for 100 * pow (CRT, 4^0.5)

28460   cycles for 100 * f8Exp (Dave, 2^0.5)
13872   cycles for 100 * ExpXY (MasmBasic, 3^0.5)
27390   cycles for 100 * pow (CRT, 4^0.5)

28419   cycles for 100 * f8Exp (Dave, 2^0.5)
13830   cycles for 100 * ExpXY (MasmBasic, 3^0.5)
27393   cycles for 100 * pow (CRT, 4^0.5)

28441   cycles for 100 * f8Exp (Dave, 2^0.5)
13849   cycles for 100 * ExpXY (MasmBasic, 3^0.5)
27396   cycles for 100 * pow (CRT, 4^0.5)

1.41421356237310 for f8Exp (Dave, 2^0.5)
1.73205080756888 for ExpXY (MasmBasic, 3^0.5)
2.00000000000000 for pow (CRT, 4^0.5)

--- ok ---

jj2007

  • Member
  • *****
  • Posts: 7737
  • Assembler is fun ;-)
    • MasmBasic
Re: FPU power
« Reply #28 on: October 07, 2015, 12:58:21 AM »
So ... wouldn't it be better to test them with the same inputs? Not that I want to make extra work for you. Only 4^0.5 ought to go faster, and since it's used with the slowest algo, the results should be good anyway

Here is one with almost identical values. The little difference is to verify that the algo actually changed the variable...

Code: [Select]
Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz (SSE4)

14648   cycles for 100 * f8Exp (Dave, 5.4321^0.5)
12378   cycles for 100 * ExpXY (MasmBasic, 5.4322^0.5)
24845   cycles for 100 * pow (CRT, 5.4323^0.5)

14645   cycles for 100 * f8Exp (Dave, 5.4321^0.5)
12376   cycles for 100 * ExpXY (MasmBasic, 5.4322^0.5)
24840   cycles for 100 * pow (CRT, 5.4323^0.5)

14656   cycles for 100 * f8Exp (Dave, 5.4321^0.5)
12378   cycles for 100 * ExpXY (MasmBasic, 5.4322^0.5)
24854   cycles for 100 * pow (CRT, 5.4323^0.5)

14656   cycles for 100 * f8Exp (Dave, 5.4321^0.5)
12373   cycles for 100 * ExpXY (MasmBasic, 5.4322^0.5)
24837   cycles for 100 * pow (CRT, 5.4323^0.5)

14642   cycles for 100 * f8Exp (Dave, 5.4321^0.5)
12374   cycles for 100 * ExpXY (MasmBasic, 5.4322^0.5)
24848   cycles for 100 * pow (CRT, 5.4323^0.5)

5.43210000000000  for f8Exp (Dave, 5.4321^0.5)^2
5.43220000000000  for ExpXY (MasmBasic, 5.4322^0.5)^2
5.43230000000000  for pow (CRT, 5.4323^0.5)^2

TWell

  • Member
  • ****
  • Posts: 748
Re: FPU power
« Reply #29 on: October 07, 2015, 02:54:17 AM »
Code: [Select]
AMD E-450 APU with Radeon(tm) HD Graphics (SSE4)

21041   cycles for 100 * f8Exp (Dave, 5.4321^0.5)
23269   cycles for 100 * ExpXY (MasmBasic, 5.4322^0.5)
32989   cycles for 100 * pow (CRT, 5.4323^0.5)

20927   cycles for 100 * f8Exp (Dave, 5.4321^0.5)
23154   cycles for 100 * ExpXY (MasmBasic, 5.4322^0.5)
33051   cycles for 100 * pow (CRT, 5.4323^0.5)

20892   cycles for 100 * f8Exp (Dave, 5.4321^0.5)
23173   cycles for 100 * ExpXY (MasmBasic, 5.4322^0.5)
33151   cycles for 100 * pow (CRT, 5.4323^0.5)

20886   cycles for 100 * f8Exp (Dave, 5.4321^0.5)
23342   cycles for 100 * ExpXY (MasmBasic, 5.4322^0.5)
33086   cycles for 100 * pow (CRT, 5.4323^0.5)

21158   cycles for 100 * f8Exp (Dave, 5.4321^0.5)
23167   cycles for 100 * ExpXY (MasmBasic, 5.4322^0.5)
33050   cycles for 100 * pow (CRT, 5.4323^0.5)

5.43210000000000  for f8Exp (Dave, 5.4321^0.5)^2
5.43220000000000  for ExpXY (MasmBasic, 5.4322^0.5)^2
5.43230000000000  for pow (CRT, 5.4323^0.5)^2

--- ok ---