c++ - GLM openGL 数学 fastSqrt 函数

标签 c++ opengl glm-math

这段代码

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

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

产生以下结果

-2.16802e-058

我做错了什么?

最佳答案

解决方案很简单,滚动你自己的 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);
}

关于c++ - GLM openGL 数学 fastSqrt 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19833674/

相关文章:

c++ - 当我们编译 C++ 时,我们得到了什么?二进制代码 ?汇编代码?

Windows下C++高精度时间测量

objective-c - 如何在Objective-C OpenGL中正确使用InitWindow?

c++ - 关注 2d 播放器 openGL

c++ - 如何从 glm::mat4 中读取值

c++ - 尝试 move 大型位集时 GCC4.6 出现段错误,这是编译器错误吗?

c++ - 将 int 分配给具有两个不同输出的 char

c++ - 如何使用 Windows 内存 dc (c++) 创建 OpenGL 上下文

c++ - gluUnProject 不工作

c++ - GLM 是否与 GLload 和 GCC 兼容