Is there a fast C or C++ standard library function for double precision inverse square root?

前端 未结 7 2032
醉话见心
醉话见心 2021-01-18 01:26

I find myself typing

double foo=1.0/sqrt(...);

a lot, and I\'ve heard that modern processors have built-in inverse square root opcodes.

7条回答
  •  礼貌的吻别
    2021-01-18 02:17

    If your not afraid of using your own functions, try the following:

    template 
    T invsqrt(T x)
    {
        return 1.0 / std::sqrt(x);
    }
    

    It should be just as fast as the orginal 1.0 / std::sqrt(x) in any modernly optimized compiler. Also, it can be used with doubles or floats.

提交回复
热议问题