From 6838815f9f72a80532cf54490576769a0bcd1004 Mon Sep 17 00:00:00 2001 From: Christophe Riccio Date: Sun, 15 Sep 2013 14:39:59 +0200 Subject: [PATCH] Factorized fastInversesqrt code --- glm/core/func_exponential.hpp | 2 +- glm/core/func_exponential.inl | 47 ++++++++++++++--------------------- glm/gtx/fast_square_root.inl | 10 +------- 3 files changed, 21 insertions(+), 38 deletions(-) diff --git a/glm/core/func_exponential.hpp b/glm/core/func_exponential.hpp index dc76fcbb..e85200df 100644 --- a/glm/core/func_exponential.hpp +++ b/glm/core/func_exponential.hpp @@ -104,7 +104,7 @@ namespace glm /// @see GLSL 4.20.8 specification, section 8.2 Exponential Functions template GLM_FUNC_DECL genType sqrt(genType const & x); - + /// Returns the reciprocal of the positive square root of x. /// /// @param x inversesqrt function is defined for input values of x defined in the range [0, inf+) in the limit of the type precision. diff --git a/glm/core/func_exponential.inl b/glm/core/func_exponential.inl index f3f62e59..6ebdf913 100644 --- a/glm/core/func_exponential.inl +++ b/glm/core/func_exponential.inl @@ -153,52 +153,43 @@ namespace _detail VECTORIZE_VEC(inversesqrt) + namespace detail + { + template + genType fastInversesqrt(genType const & v) + { + genType tmp(v); + genType xhalf(tmp * genType(0.5f)); + genUType i = *reinterpret_cast(const_cast(&v)); + i = genUType(0x5f375a86) - (i >> genUType(1)); + tmp = *reinterpret_cast(&i); + tmp = tmp * (genType(1.5f) - xhalf * tmp * tmp); + return tmp; + } + } + template <> GLM_FUNC_QUALIFIER lowp_vec1 inversesqrt(lowp_vec1 const & v) { - float tmp(v.x); - float xhalf(0.5f * tmp); - uint i = *reinterpret_cast(const_cast(&v)); - i = 0x5f375a86 - (i >> 1); - tmp = *reinterpret_cast(i); - tmp = tmp * (1.5f - xhalf * tmp * tmp); - return lowp_vec1(tmp); + return detail::fastInversesqrt(v); } template <> GLM_FUNC_QUALIFIER lowp_vec2 inversesqrt(lowp_vec2 const & v) { - lowp_vec2 tmp(v); - lowp_vec2 xhalf(0.5f * tmp); - lowp_uvec2 i = *reinterpret_cast(const_cast(&v)); - i = lowp_uvec2(0x5f375a86) - (i >> lowp_uvec2(1)); - tmp = *reinterpret_cast(&i); - tmp = tmp * (1.5f - xhalf * tmp * tmp); - return tmp; + return detail::fastInversesqrt(v); } template <> GLM_FUNC_QUALIFIER lowp_vec3 inversesqrt(lowp_vec3 const & v) { - lowp_vec3 tmp(v); - lowp_vec3 xhalf(0.5f * tmp); - lowp_uvec3 i = *reinterpret_cast(const_cast(&v)); - i = lowp_uvec3(0x5f375a86) - (i >> lowp_uvec3(1)); - tmp = *reinterpret_cast(&i); - tmp = tmp * (1.5f - xhalf * tmp * tmp); - return tmp; + return detail::fastInversesqrt(v); } template <> GLM_FUNC_QUALIFIER lowp_vec4 inversesqrt(lowp_vec4 const & v) { - lowp_vec4 tmp(v); - lowp_vec4 xhalf(0.5f * tmp); - lowp_uvec4 i = *reinterpret_cast(const_cast(&v)); - i = lowp_uvec4(0x5f375a86) - (i >> lowp_uvec4(1)); - tmp = *reinterpret_cast(&i); - tmp = tmp * (1.5f - xhalf * tmp * tmp); - return tmp; + return detail::fastInversesqrt(v); } }//namespace glm diff --git a/glm/gtx/fast_square_root.inl b/glm/gtx/fast_square_root.inl index aef9ad0d..f514dd57 100644 --- a/glm/gtx/fast_square_root.inl +++ b/glm/gtx/fast_square_root.inl @@ -30,15 +30,7 @@ namespace glm genType const & x ) { - genType tmp = x; - float xhalf = 0.5f * float(tmp); - uint i = *(uint*)&x; - i = 0x5f375a86 - (i >> 1); - //x = *(float*)&i; - //x = *((float*)(char*)&i); - tmp = detail::uif32(i).f; - tmp = tmp * (1.5f - xhalf * tmp * tmp); - return genType(tmp); + return detail::fastInversesqrt(x); } VECTORIZE_VEC(fastInverseSqrt)