Fast Single-Precision-Float-Calculations

Share your advanced PureBasic knowledge/code with the community.
Helle
Enthusiast
Enthusiast
Posts: 178
Joined: Wed Apr 12, 2006 7:59 pm
Location: Germany
Contact:

Fast Single-Precision-Float-Calculations

Post by Helle »

Code updated for 5.20+

If for extensive calculations the FPU-single-precision is sufficient, you can test this:

Code: Select all

;- Test for set the FPU-Control-Word to single-precision-float 
;- "Helle" Klaus Helbing, 04.01.2007, PB v4.02  

Global CWO.w     
Global CWN.w
Global A.f = 12345
Global B.f = 67890
Global C.f

Procedure SetSingleFloat()
  !fstcw [v_CWO]               ;save Control-Word
  !mov ax,[v_CWO]
  !and ax,1111110011111111b    ;set the 2 precisions-bits to zero = single-precision-float
  !mov [v_CWN],ax
  !fldcw [v_CWN]               ;write-back changed Control-Word
EndProcedure 

Procedure RestoreCW()
  !fldcw [v_CWO]               ;write-back original Control-Word
EndProcedure 

;-------- test with original-settings
Time1 = ElapsedMilliseconds()
For i = 0 To 99999999
  C = A * B                   ;any calculations
  C = Sqr(C / A + B)          
Next 
Time = ElapsedMilliseconds() - Time1 
MessageRequester("Test Original Settings", Str(Time) + " ms   " + StrF(C))

;-------- test with set to single-precision
SetSingleFloat()
Time1 = ElapsedMilliseconds()
For i = 0 To 99999999
  C = A * B                   ;any calculations
  C = Sqr(C / A + B)
Next 
Time = ElapsedMilliseconds() - Time1 
RestoreCW()
MessageRequester("Test Set to Single-Precision", Str(Time) + " ms   " + StrF(C))
Gruss
Helle