4 ms·
Aside from the alternative, there's not much room to optimize that function. 1/sqrt(x) has pretty good approximations (e.g. fast inverse sqrt, reciprocal sqrt i
by AlotOfReading 3y ago
Aside from the alternative, there's not much room to optimize that function. 1/sqrt(x) has pretty good approximations (e.g. fast inverse sqrt, reciprocal sqrt instructions) and 9x-6 is just FMA. A quick test with compiler explorer [1] shows 4 instructions, and you could probably do 3 with judicious instruction set choice and/or no regard for performance. Even a lookup table could be slower in practice, depending on what the rest of the program looks like.
[1] https://godbolt.org/z/86P86xTvx https://godbolt.org/z/86P86xTvx
- pclmulqdq 3y agoThose are four long instructions, and there's a chance that you could do better within your desired accuracy bounds by using one of the inverse square root instructions and a round of Newton-Raphson iteration.
- bee_rider 3y agoIt does have an FMA command that it can suggest, but it paired it with pow. double code(double x) { return pow(fma(x, 9.0, -6.0), -0.5); } It labeled this as having a slowdown.