Fast inverse sqr was faster so long ago.
I tested it in x86 asm;
Function _SQRT_(_F_ As _REAL_) As _REAL_
Dim As _REAL_ X2 = Any, Y = Any, ThreeHalves = Any
ThreeHalves = 1.5
X2 = 0.5
Asm
mov eax, [ebp + 8]
shr eax, 1
mov ebx, &H5F3759DF
sub ebx, eax
mov [Y], ebx
fld DWORD PTR [X2]
fmul DWORD PTR [ebp + 8]
fstp DWORD PTR [X2]
fld1
fld DWORD PTR [Y]
fld DWORD PTR [ThreeHalves]
fld DWORD PTR [Y]
fmul ST(0)
fmul DWORD PTR[X2]
fsubp ST(1), ST(0)
fmulp ST(1), ST(0)
fdivp ST(1), ST(0)
fstp DWORD PTR [ebp - 4]
End Asm
End Function
versus;
Asm
fld DWORD PTR [A]
fsqrt
fstp DWORD PTR [B]
End Asm
Ofcourse it gets worse in AGK;
// Project: Inverse Sqrt
// Created: 2021-11-19
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "Inverse Sqrt" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
Global Mem As Integer
Global _WEIRDO_ As Integer
Mem = Creatememblock(4)
_WEIRDO_ = 0x5F3759DF
Local q As Float, s As Float
Local A As Float, B As Float
Local l As Integer
Local Time0 As Integer, Time1 As Integer
q = Random()
s = Random()
q = q/s
Time0 = Timer() * 1000
For l = 0 To 999999
s = Sqrt(q)
Next
Time0 = Abs(Time0 - Timer() * 1000)
Time1 = Timer() * 1000
For l = 0 To 999999
s = _SQRT_(q)
Next
Time1 = Abs(Time1 - Timer() * 1000)
q = 0
do
If GetRawKeyState(65)
If l
q = Random()
s = Random()
q = q/s
A = Sqrt(q)
B = _SQRT_(q)
EndIf
l = 0
Else
l = 1
EndIf
Print("AGK Sqrt took...: " + Str(Time0) + " miliseconds...")
Print("_SQRT_ took.....: " + Str(Time1) + " miliseconds...")
Print("")
Print("The number........: " + Str(q))
Print("Square root.......: " + Str(A))
Print("_SQRT_............: " + Str(B))
Sync()
loop
Deletememblock(Mem)
Function _SQRT_(_F_ As Float)
Local h As Float
SetMemblockFloat(Mem, 0, _F_)
j = GetMemblockInt(Mem, 0)
j = _WEIRDO_ - (j >> 1)
SetMemblockInt(Mem, 0, j)
h = GetMemblockFloat(Mem, 0)
h = h * (1.5 - (_F_ * h * h * 0.5))
h = 1/h
EndFunction h
maybe you should try making your distance comparisions in 'squared_length's so you will get rid of sqrt?