Interesting, but I think the function is not implemented correctly (tested 0.17-July30 only).
Code: Select all
#include "windows.bi"
#include "counter.bas"
Function InvSqrt ( x As Double ) As Double
Dim As Double xhalf = .5 * x
Dim As Integer i = cast(Integer, x)
i = &h5F3759DF - (i Shr 1)
x = cast(Double, i)
x = x * (1.5 - xhalf * x * x)
Return x
End Function
Function FpuInvSqrt ( x As Double ) As Double
return 1.0 / sqr(x)
End Function
dim as double d, r
print InvSqrt( 1 )
print FpuInvSqrt( 1 )
print InvSqrt( .0000123 )
print FpuInvSqrt( .0000123 )
print InvSqrt( 123 )
print FpuInvSqrt( 123 )
d = 123.456
sleep 3000 ' Give lazy writes time to complete
counter_begin
r = InvSqrt( d )
counter_end
print "InvSqrt ";counter_cycles;" cycles"
counter_begin
r = FpuInvSqrt( d )
counter_end
print "FpuInvSqrt ";counter_cycles;" cycles"
sleep
Code: Select all
-2.038273385915891e+027
1
-2.507076264676306e+022
285.1329742561005
-2.507075977474315e+029
9.016696346674323e-002
InvSqrt 217 cycles
FpuInvSqrt 271 cycles
It’s faster on my P3, but fixing it could speed it up or slow it down.
Counter.bas is
here.
Edit: My timings are meaningless. After the first call to InvSqrt the cycle count goes up for both procedures, about 70 cycles for FpuInvSqrt, and about 535 cycles for InvSqrt! I at first thought the increase might be due to an exception, but a delay between the calls and/or an finit does not change the timings. I can't see how the problem could be my timing macros, but that doesn't mean it isn't. There is a potential problem with the FPU executing in parallel with the timing code, but inserting FWAITs does not correct the problem, and I can't see how this could extend the cycle count.