1

这段代码

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

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

产生以下结果

-2.16802e-058

我究竟做错了什么?

4

1 回答 1

1

解决方案很简单,自己动手fastSqrt,这行得通:

#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);
}
于 2013-11-07T11:07:23.223 回答