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

前端 未结 7 2033
醉话见心
醉话见心 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:24

    I don't know of a standardized C API for this, but that does not mean you cannot use the fast inverse sqrt instructions, as long as you are willing to write platform dependent intrinsics.

    Let's take 64-bit x86 with AVX for example, where you can use _mm256_rsqrt_ps() to approximate the reciprocal of a square root. Or more specifically: 8 square-roots in a single go, using SIMD.

    #include 
    
    ...
    
    float inputs[8] = { ... } __attribute__ ((aligned (32)));
    __m256 input = _mm256_load_ps(inputs);
    __m256 invroot = _mm256_rsqrt_ps(input);
    
    

    Similarly, you can use the intrinsic vrsqrteq_f32 on ARM with NEON. In this case, the SIMD is 4-wide, so it will compute four inverse square roots in a single go.

    #include 
    
    ...
    
    float32x4_t sqrt_reciprocal = vrsqrteq_f32(x);
    

    Even if you need just one root value per batch, it is still faster than a full square root. Just set the input in all, or one lane of the SIMD register. That way, you will not have to go through your memory with a load operation. On x86 that is done via _mm256_set1_ps(x).

提交回复
热议问题