Jump to content
Sign in to follow this  
Tommi Prami

Delphi version of Fast inverse square root

Recommended Posts

https://en.wikipedia.org/wiki/Fast_inverse_square_root

 

There was code samples long time ago for Delphi. With different constants, to spread error to positive , negative  or +/-

 

Also pure pascal version so routine could be inlined. It vas pretty fast Used in odl company. No need for me right now, but would be still cool to have it around and put into the Wiki and so on, also Delphi version of the code.

 

-Tee-

Share this post


Link to post

Surprisingly difficult to find any Delphi implementation at all.

 

Googling "5F3759DF" "delphi" finally turned up something:

 

http://www.sql.ru/forum/actualutils.aspx?action=gotomsg&tid=190407&msg=20745512

function FastInvSqrt(const Value: Single): Single; inline;
var
  IntCst: Cardinal absolute result;
begin
  result := Value;
  IntCst := ($BE6EB50C - IntCst) shr 1;
  result := 0.5 * result * (3 - Value * sqr(result));
end; 

and

 

function FastInvSqrt(x: Single): Single;
var
  xhalf: Single;
  i: Integer;
begin
  xhalf := 0.5 * x;
  i := $5F3759DF - (PInteger(@x)^ shr 1);
  x := PSingle(@i)^;
  result := x * (1.5 - (xhalf * x * x));
end;

These are using different constants.

(And some more in that thread, unfortunately I don't read Russian, so I don't know what the result of the tests were.)

 

Nothing useful turned up for "5F375A86" "delphi" which is supposedly the optimal constant when using Newtons iteration.

 

And "fastinvsqrt" "delphi" found some more:

https://github.com/itchyny/fastinvsqrt/issues/1

(In Chinese, no idea what they are talking about, it is not part of the official list in that project.)

 

https://sourceforge.net/p/graphics32/mailman/graphics32-commits/?viewmonth=201109&page=0

 

 

 

Share this post


Link to post
5 hours ago, dummzeuch said:

>And some more in that thread, unfortunately I don't read Russian, so I don't know what the result of the tests were.)

That page test says that function with  $BE6EB50C constant (from GR32_Math.pas) is faster 

 

 

Edited by Boris Novgorodov
  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×