It is currently Fri Apr 03, 2020 6:03 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: FPU how to cleanup register?
PostPosted: Sat Feb 15, 2020 1:59 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 638
I want to calculate timing differences but when i use my code without finit it works only a few times in succesion?

I suspect that my code has an issue.
What can i do/ clean up to make it work without finit?

Code:
Code:
proc Timing
     finit
     fld qword[rcx+0x20]
     fild qword[rcx+0x30]
     fild qword[rcx+0x38]
     fsub st0,st1
     fdiv st0,st2
     fstp qword[rcx+0x40];<- this pops st0
     ret
endp


Top
 Profile  
Reply with quote  
 Post subject: Re: FPU how to cleanup register?
PostPosted: Sat Feb 15, 2020 2:14 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3606
Location: Netherlands
You load three values onto the fpu stack and only remove one. That won't work.
Use fsubp and fdivp instead of fsub and fdiv.

_________________
macOS 10.15 Catalina, PB 5.71 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: FPU how to cleanup register?
PostPosted: Sat Feb 15, 2020 2:55 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 638
Thanks wilbert :)
I changed it and it works but im still not sure if it is correct?

Code:
Code:
proc Timing
     fild qword[rcx+0x38]
     fild qword[rcx+0x30]
     fsubp
     fld qword[rcx+0x20]
     fdivp
     fstp qword[rcx+0x40]
     ret
endp


Top
 Profile  
Reply with quote  
 Post subject: Re: FPU how to cleanup register?
PostPosted: Sat Feb 15, 2020 4:05 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3606
Location: Netherlands
Mijikai wrote:
I changed it and it works but im still not sure if it is correct?

The code looks fine to me.

_________________
macOS 10.15 Catalina, PB 5.71 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: FPU how to cleanup register?
PostPosted: Sat Feb 15, 2020 6:22 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 638
wilbert wrote:
Mijikai wrote:
I changed it and it works but im still not sure if it is correct?

The code looks fine to me.


Thank you :)
I also found no more issues while testing.
I hardly touch the fpu.


Top
 Profile  
Reply with quote  
 Post subject: Re: FPU how to cleanup register?
PostPosted: Sat Feb 15, 2020 7:14 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3606
Location: Netherlands
Mijikai wrote:
I hardly touch the fpu.

In most cases you can use SSE2 instructions if you don't like to use the x87 ones.

By the way, if the value you are using for division is a constant, it is a lot faster to calculate the reciprocal once and multiply by that.

_________________
macOS 10.15 Catalina, PB 5.71 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: FPU how to cleanup register?
PostPosted: Sat Feb 15, 2020 7:27 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 638
wilbert wrote:
Mijikai wrote:
I hardly touch the fpu.

In most cases you can use SSE2 instructions if you don't like to use the x87 ones.

By the way, if the value you are using for division is a constant, it is a lot faster to calculate the reciprocal once and multiply by that.


Sounds great but i have no idea what a reciprocal is :oops:
[rcx+0x20] in my case is static.

Is it just:
Code:
Constant =  (1 / Constant)
Result = Input * Constant


Top
 Profile  
Reply with quote  
 Post subject: Re: FPU how to cleanup register?
PostPosted: Sat Feb 15, 2020 7:58 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3606
Location: Netherlands
Mijikai wrote:
Is it just:
Code:
Constant =  (1 / Constant)
Result = Input * Constant

Yes, that is it :)
Multiply is a much faster cpu operation compared to division.

_________________
macOS 10.15 Catalina, PB 5.71 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: FPU how to cleanup register?
PostPosted: Sat Feb 15, 2020 8:23 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Apr 12, 2006 7:59 pm
Posts: 171
Location: Germany
With this you can check the FPU-Registers:

Code:
Procedure FPU()
  Global.q SaveRam, SaveRamAli, Ali       
  Global.d FST0, FST1, FST2, FST3, FST4, FST5, FST6, FST7
  Global Tag.b
  Protected Tag0$="   Free", Tag1$="   Free",  Tag2$="   Free",  Tag3$="   Free", Tag4$="   Free",  Tag5$="   Free",  Tag6$="   Free",  Tag7$="   Free"

  SaveRam = AllocateMemory(512+64)     ;512 Byte FXSAVE
  Ali = SaveRam % 64
  SaveRamAli = SaveRam + Ali

  !mov rsi,[v_SaveRamAli]
  !fxsave64 [rsi]

  !mov al,[rsi+4]            ;Tag
  !mov [v_Tag],al
  !test [v_Tag],1
 !jz @f
  Tag7$="   Busy"
 !@@:
  !test [v_Tag],2
 !jz @f
  Tag6$="   Busy"
 !@@:
  !test [v_Tag],4
 !jz @f
  Tag5$="   Busy"
 !@@:
  !test [v_Tag],8
 !jz @f
  Tag4$="   Busy"
 !@@:
  !test [v_Tag],10h
 !jz @f
  Tag3$="   Busy"
 !@@:
  !test [v_Tag],20h
 !jz @f
  Tag2$="   Busy"
 !@@:
  !test [v_Tag],40h
 !jz @f
  Tag1$="   Busy"
 !@@:
  !test [v_Tag],80h
 !jz @f
  Tag0$="   Busy"
 !@@:

  !mov rsi,[v_SaveRamAli]
  !fld tword[rsi+32]
  !fstp qword[v_FST0]
  !fld tword[rsi+48]
  !fstp qword[v_FST1]
  !fld tword[rsi+64]
  !fstp qword[v_FST2]
  !fld tword[rsi+80]
  !fstp qword[v_FST3]
  !fld tword[rsi+96]
  !fstp qword[v_FST4]
  !fld tword[rsi+112]
  !fstp qword[v_FST5]
  !fld tword[rsi+128]
  !fstp qword[v_FST6]
  !fld tword[rsi+144]
  !fstp qword[v_FST7]

  Reg$ = "ST0 = " + StrD(FST0, 20) + Tag0$ + #LFCR$
  Reg$ + "ST1 = " + StrD(FST1, 20) + Tag1$ + #LFCR$
  Reg$ + "ST2 = " + StrD(FST2, 20) + Tag2$ + #LFCR$
  Reg$ + "ST3 = " + StrD(FST3, 20) + Tag3$ + #LFCR$
  Reg$ + "ST4 = " + StrD(FST4, 20) + Tag4$ + #LFCR$
  Reg$ + "ST5 = " + StrD(FST5, 20) + Tag5$ + #LFCR$
  Reg$ + "ST6 = " + StrD(FST6, 20) + Tag6$ + #LFCR$
  Reg$ + "ST7 = " + StrD(FST7, 20) + Tag7$

  FreeMemory(SaveRam)

  MessageRequester("FPU-Register", Reg$)
EndProcedure

;Test
Data1.d = 20.0
Data2.q = 30
Data3.q = 40
Result.d

;FPU()
;!finit
FPU()
!fild qword[v_Data3]
FPU()
!fild qword[v_Data2]
FPU()
!fsubp
FPU()
!fdiv qword[v_Data1]
FPU()
!fstp qword[v_Result]
FPU()
Debug Result


Top
 Profile  
Reply with quote  
 Post subject: Re: FPU how to cleanup register?
PostPosted: Sat Feb 15, 2020 8:40 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 638
Thanks wilbert & Helle :)
That is really helpful Helle 8)
now i can easily see my mistakes XD

I created a small timer library in fasm to calculate FPS, frametime and deltatime.
I would post the source for everyone if i get some help in optimizing/ making it better.
Its not that big (~400 lines) and it might become a nice tutorial for anyone that is interested in asm :)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye