6 ms·
If your computer was built after 1999, it probably supports the SSE instruction set. It contains the _mm_rsqrt_ps instruction, which is faster and will give you
by johndough 2y ago
If your computer was built after 1999, it probably supports the SSE instruction set. It contains the _mm_rsqrt_ps instruction, which is faster and will give you four reciprocal square roots at once: https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm_rsqrt_ps&ig_expand=5642&ssetechs=SSE https://www.intel.com/content/www/us/en/docs/intrinsics-guid...
That being said, the techniques discussed here are not totally irrelevant (yet). There still exists some hardware with fast instructions for float/int conversion, but lacking rsqrt, sqrt, pow, log instructions, which can all be approximated with this nice trick.
- bnprks 2y agoAmusingly, (to me at least) there's also an SSE instruction for non-reciprocal square roots but it's so much slower than reciprocal square root that calculating sqrt(x) as x * 1/sqrt(x) is faster assuming you can tolerate the somewhat reduced precision.
- mabster 2y agoI wouldn't be surprised if _mm_rsqrt_ps is actually implemented using the same bit level trick. Same as Carmack's, we did a single step of Newton's method and it was definitely good enough.
- Findecanor 2y agoI dunno about Intel and AMD, but ARM and RISC-V use lookup tables for rsqrt. Unlike AMD and Intel, those tables are precisely defined in their respective specs.
- pclmulqdq 2y agoIntel provides bit-accurate code. In older chips it used a faithful bipartite ROM: https://web.archive.org/web/20120124193536id_/http://www.acsel-lab.com/arithmetic/papers/ARITH12/ARITH12_Das%20Sarma.pdf https://web.archive.org/web/20120124193536id_/http://www.acs...
- mabster 2y agoI don't recall the coprocessor having either reciprocal or reciprocal square root? I didn't do much Intel until later in my career though, so I might be missing something though. Both _mm_rcp_ps (rcpps) and _mm_rsqrt_ps (rsqrtps) are only good for about half the bits.
- kragen 2y agothe vast, vast majority of computers do not support the sse instruction set or indeed any of the i386 and amd64 instruction set at all, and the fraction of computers that do support them (other than through emulation) is continually shrinking gpu instruction sets, arm, risc-v, avr, pic, 8051, fpga... of course, in many cases, these do have a built-in approximate reciprocal square root operation, but probably implemented with this algorithm
- colejohnson66 2y agoIn terms of CPUs where you would use an inverse square root (mainly games or scientific), x86 still reigns supreme.
- kragen 2y agogpus reign supreme for games and scientific, and none of them are x86; larrabee failed. and you'd be surprised at the 3-d kinematics and control systems that people do with microcontrollers
- khangaroo 2y agoThe SSE float reciprocal instructions have slightly different results between Intel and AMD, which can be a source of headaches for those expecting deterministic results between PCs. (see https://robert.ocallahan.org/2021/09/rr-trace-portability-diverging-behavior.html https://robert.ocallahan.org/2021/09/rr-trace-portability-di...)
- kragen 2y agothe vast, vast majority of computers do not support the sse instruction set or indeed any of the i386 and amd64 instruction set at all, and the fraction of computers that do support them, other than through emulation, is continually shrinking gpu instruction sets, arm, risc-v, avr, pic, 8051, fpga... of course, in many cases, these do have a built-in approximate reciprocal square root operation, but probably implemented with this algorithm