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.
Fast Inverse Square Root
41–50 of 100 posts
Re: Fast Inverse Square Root
#42This 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.
Re: Fast Inverse Square Root
#43Here's another article (from 2012) describing the same calculation: http://h14s.p5r.org/2012/09/0x5f3759df.html
Re: Fast Inverse Square Root
#44What 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.
Re: Fast Inverse Square Root
#45Earlier 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
I really wish C and C++ had an explicit type punning operator. They are systems programming languages and this is very often needed.
Re: Fast Inverse Square Root
#46What 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.
I don't know how heavily it was used in Quake but I'd bet quite a lot. Carmack didn't mess around optimizing things for no reason, he was (and is) pretty focused on spending time (coder and CPU) where it will do most good. Also, a "mere" 1% speedup seems trivial for most general coding but squeezing 1% out of optimized game code is like blood from a stone. Add a bunch of similar tricks together and you can see 10-20%…
Re: Fast Inverse Square Root
#47What 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.
Over 1,000%.
x87 fsqrt took ~70 cycles on the Pentium MMX x87, x87 fdiv was ~39 cycles - so doing inverse square root on x87 was ~109 cycles. The fast inverse square root routine was in the vicinity of 10 cycles.
In Quake, each vertex needed to have its normal vector calculated, which resulted in... I dunno, 100,000 inverse square root calculations per second, assuming you're pushing 100,000 polygons per second. Assuming a 100MHz CPU, the x87 inverse square root calculations would consume 11% of the CPU cycles, the fast inverse square root routine would consume 1% of the CPU cycles.
Re: Fast Inverse Square Root
#48This 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.
Re: Fast Inverse Square Root
#49Re: Fast Inverse Square Root
#50Earlier 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
I really wish C and C++ had an explicit type punning operator. They are systems programming languages and this is very often needed.