News for hackers. Stuff that matters.
Re:Improving the fast inverse square root (2010) by simonbyrne on Monday July 09, 2018 @12:19AM (#17487945)
It is worth noting that with AVX-512, Intel has introduced a native inverse sqrt approximation (VRSQRT14).
Re:Improving the fast inverse square root (2010) by mmozeiko on Monday July 09, 2018 @12:54AM (#17488048)
Inverse sqrt approximation is available since SSE1 with rsqrtss & rsqrtps instructions.
Re:Improving the fast inverse square root (2010) by slavik81 on Monday July 09, 2018 @02:55AM (#17488418)
Which is nice because SSE1 and SSE2 are mandatory parts of x86_64. If you're a 64bit application for desktop, you can use rsqrtss without any checks or fallbacks.

Unfortunately, it doesn't tend to get used automatically in languages like C. The result of rsqrtss is slightly different from 1/sqrtf(x) as two seperate operations, so it cannot be applied as an optimization.

If the rules for floating point optimization are loosened by passing -ffast-math to GCC, the compiler will use it. That being said, -ffast-math is a shotgun that affects a lot of things. If you need signed zeros, Infs, NaNs or denormals that flag may break your program.

Re:Improving the fast inverse square root (2010) by MaxBarraclough on Monday July 09, 2018 @05:01AM (#17488868)
> -ffast-math is a shotgun that affects a lot of things

Interesting point. GCC and MSVC both seem to have (incompatible) intrinsic functions, for what that's worth.

https://gcc.gnu.org/onlinedocs/gcc-4.8.5/gcc/X86-Built-in-Fu...

https://docs.microsoft.com/en-us/previous-versions/visualstu...

Re:Improving the fast inverse square root (2010) by mmozeiko on Monday July 09, 2018 @12:50PM (#17491585)
That is not quite true. You don't need to use GCC __builtin functions for this. GCC supports SSE1 intrinsics like _mm_rsqrt_ss exactly same as MSVC - it is declared in xmmintrin.h header. Just include it and _mm_rsqrt_ss/ps will be available for you in gcc and msvc.
Re:Improving the fast inverse square root (2010) by edynoid on Monday July 09, 2018 @07:55AM (#17489553)
I find it quite fortunate, that they don't use it automatically. Introducing a 1e-3 relative error is quite a deal breaker for some. Not for games sure, but for science that is mostly unacceptable.
Re:Improving the fast inverse square root (2010) by meta_AU on Monday July 09, 2018 @11:13AM (#17490806)
From memory, GCC does one NewtonRaphson iteration on the approximate result so the error is much lower (closer to e-9 from memory again). They don't use the approximation directly in fast-math mode.
Re:Improving the fast inverse square root (2010) by convolvatron on Monday July 09, 2018 @01:11PM (#17491755)
this is wildly off topic, but can anyone from either the scientific or the graphics community comment on the practical impact of losing denorms? i certainly understand that it softens the impact of underflow, but does anyone care?
Re:Improving the fast inverse square root (2010) by vardump on Monday July 09, 2018 @01:40AM (#17488204)
Indeed.

Both reciprocal (inverse) square root SSE SIMD instructions were available in Intel Pentium III, released in 1999.

Re:Improving the fast inverse square root (2010) by simonbyrne on Wednesday July 11, 2018 @01:21AM (#17504621)
Ah, I should have done more googling. I guess the AVX-512 ones are marginally more accurate?
Re:Improving the fast inverse square root (2010) by zbjornson on Monday July 09, 2018 @02:02AM (#17488274)
VRSQRT28 too, which has max 2^-28 rel error.

https://software.intel.com/en-us/articles/reference-implemen...

Re:Improving the fast inverse square root (2010) by piyush_soni on Monday July 09, 2018 @02:27AM (#17488345)
Thanks, I came here to ask the similar question about native optimizations on this 'hack'. Apologies for my lack of knowledge, but I'm a little confused on which one to use out of all these variants while compiling C++ code on a 64 bit platform for the standard 'float' type inverse square root? Are there varying levels of compromise between speed and accuracy among all these methods? Thanks ...
Re:Improving the fast inverse square root (2010) by stephencanon on Monday July 09, 2018 @12:26PM (#17491403)
For a generic 64b platform, use RSQRTSS/RSQRTPS, since it's the only one that will exist. The others are specific to rather new hardware.

My recollection is that it's accurate to 11.5 bits, so after one refinement step you have nearly full precision (an error bound of a couple ULP). Check Intel's docs for more details.

Re:Improving the fast inverse square root (2010) by piyush_soni on Tuesday July 10, 2018 @12:09AM (#17495384)
Thanks!
Re:Improving the fast inverse square root (2010) by stephencanon on Monday July 09, 2018 @09:43AM (#17490074)
Note that VRSQRT28 is in AVX-512ER, which is Xeon Phi only.
Re:Improving the fast inverse square root (2010) by robin_reala on Monday July 09, 2018 @12:47AM (#17488031)
How does that perform in comparison?
Re:Improving the fast inverse square root (2010) by brandmeyer on Monday July 09, 2018 @01:01AM (#17488072)
rsqrt{p,s}s has guaranteed relative error <= 1.5 * 2^-12, or about 3.6e-4. According to Agner Fog, it typically executes in one cycle. I would assume that the AVX512 versions are similar.