Live data from Hacker News

Fast Inverse Square Root

timmmm.github.io

51–60 of 100 posts

Re: Fast Inverse Square Root

#51
post #2

This is an explanation of the fast inverse square root that I hope is easy to understand. I also managed to improve on it a little bit.

Computers do not have _real_ number systems, only _rational_ number systems and rational approximations of real numbers.

There are plenty of libraries for computable numbers [1], which sit between the rationals and reals

[1] https://en.wikipedia.org/wiki/Computable_number

Re: Fast Inverse Square Root

#52

Earlier quoted context omitted.

Computers do not have _real_ number systems, only _rational_ number systems and rational approximations of real numbers.

There are plenty of libraries for computable numbers [1], which sit between the rationals and reals [1] https://en.wikipedia.org/wiki/Computable_number

...which (naturally) can't determine if some number x is equal to y or not; that alone makes using constructive real numbers challenging and the usage is virtually non-existent. I believe the most-known software using them is the Android calculator since 6.0 and Hans Boehm (of the gc fame) documented various issues encountered [1].

[1] https://dl.acm.org/doi/fullHtml/10.1145/2911981

Re: Fast Inverse Square Root

#53
post #2

This is an explanation of the fast inverse square root that I hope is easy to understand. I also managed to improve on it a little bit.

You mention the wiki page is badly written, and I have the same feeling whenever I read about anything mathematics related on it. But... it's written by volunteers, so I'll take what I can get. Since you obviously have the talent for explaining things, do you think the wiki page could be edited and improved for clarity?

Re: Fast Inverse Square Root

#54
A little self-promotion: I've written a blog post to answer the question that this one ends with (how to optimize divisions by constant integers): https://rubenvannieuwpoort.nl/posts/division-by-constant-uns...

https://ridiculousfish.com/blog/posts/labor-of-division-epis... and https://ridiculousfish.com/blog/posts/labor-of-division-epis... also explain this (and are probably better written than my blogpost).

Re: Fast Inverse Square Root

#55
post #31

Earlier quoted context omitted.

In C++, type punning through pointers and unions is undefined behavior. Even `reinterpret_cast ` isn’t allowed because of aliasing (IIRC). The only “defined” way to do type punning is a memcpy. A compiler targeting something like x86 would optimize out the memcpy. For more information, see the C++20 final draft[0§7.6.1.9] [0]: https://isocpp.org/files/papers/N4860.pdf

There's a Torvalds rant about this specific UB and it's how they do it in the Linux kernel.

Linux is written in C. In C99 and later type punning trough unions is well defined operation. No issues there.

It’s C++ that’s lacking this feature. Not C.

Re: Fast Inverse Square Root

#56
post #2

This is an explanation of the fast inverse square root that I hope is easy to understand. I also managed to improve on it a little bit.

This is a great post! The setup could be clearer: I found it a little difficult to follow (during “a real number x … this value which I will denote f … treat the 31-bit value as an unsigned integer u”) what each of the named variables was intended to refer to, and formatting the 31-bit number as 32 bits threw me off too. Nothing insurmountable but I could feel my brain stumbling before I got to the clever part.

Re: Fast Inverse Square Root

#58
> [Fixed point arithmetic] has some niche uses but isn't commonly used

It is very common in the embedded world and in hardware. I routinely program a CPU which has no FPU, so any arithmetic will have to be done in fixed point.

It's perfectly possible to get work done with fixed point arith, it just requires a bit more thinking through so you don't run out of bits. Usually, I write unit tests where I compile my code for host architecture and compare the fixed point result to host's floating point. Then I can set a precise error margin for my XP approximation.

Re: Fast Inverse Square Root

#59

A little self-promotion: I've written a blog post to answer the question that this one ends with (how to optimize divisions by constant integers): https://rubenvannieuwpoort.nl/posts/division-by-constant-uns... https://ridiculousfish.com/blog/posts/labor-of-division-epis... and https://ridiculousfish.com/blog/posts/labor-of-division-epis... also explain this (and are probably better written than my blogpost).

Off-topic comment, I really like the clutter-free look of your site, with the latex to html generator.

For anyone curious like I was:

https://github.com/rubenvannieuwpoort/static-site-generator

Re: Fast Inverse Square Root

#60
post #41

What was the speedup from this? If only 1% of cpu time was spent on slow square root, and this sped it up by 100%, it would be barely worth it.

It was first found in quake 3, which I believed used software rendering. Given that inverse square root is heavily used for lighting functions, and it was all done in CPU, it's reasonable to believe that this provided a non-negligible speedup overall.

iirc Quake 3 required a 3D accelerator and didn't support software rendering - but this was before graphics cards supported hardware T&L so you're right those lighting calcs would be performed on the CPU
Post reply on HN