Search code examples
c++openglglm-math

GLM openGL Mathematics fastSqrt function


This code

#include "glm/glm.hpp"
#include "glm/gtx/fast_square_root.hpp"

double temp = 4.0;
temp = glm::fastSqrt(temp);

produces the following result

-2.16802e-058

What am I doing wrong?


Solution

  • The solution is simple, roll your own fastSqrt, that works:

    #if defined(__SSE__)
    template <typename T>
    inline T fastSqrt(T const x)
    {
      float r;
    
      _mm_store_ss(&r, _mm_rsqrt_ss(_mm_set_ss(x)));
    
      return x * r * (T(1.5) - T(.5) * x * r * r);
    }
    #elif defined(__ARM_NEON__)
    template <typename T>
    inline T fastSqrt(T const x)
    {
      auto const r(vrsqrte_f32(float32x2_t{float32_t(x)}));
    
      return x * r[0] * (T(1.5) - T(.5) * x * r[0] * r[0]);
    }
    #else
    template <typename T>
    inline T fastSqrt(T const x)
    {
      constexpr ::std::int32_t const SQRT_MAGIC_F(0x5f3759df);
    
      float r(x);
    
      reinterpret_cast<::std::int32_t&>(r) = SQRT_MAGIC_F -
        (reinterpret_cast<::std::int32_t const&>(r) >> 1);
    
      return x * r * (1.5f - .5f * x * r * r);
    }