Factorized fastInversesqrt code

This commit is contained in:
Christophe Riccio 2013-09-15 14:39:59 +02:00
parent eeacdf37a0
commit 6838815f9f
3 changed files with 21 additions and 38 deletions

View File

@ -153,52 +153,43 @@ namespace _detail
VECTORIZE_VEC(inversesqrt) VECTORIZE_VEC(inversesqrt)
namespace detail
{
template <typename genType, typename genUType>
genType fastInversesqrt(genType const & v)
{
genType tmp(v);
genType xhalf(tmp * genType(0.5f));
genUType i = *reinterpret_cast<genUType*>(const_cast<genType*>(&v));
i = genUType(0x5f375a86) - (i >> genUType(1));
tmp = *reinterpret_cast<genType*>(&i);
tmp = tmp * (genType(1.5f) - xhalf * tmp * tmp);
return tmp;
}
}
template <> template <>
GLM_FUNC_QUALIFIER lowp_vec1 inversesqrt(lowp_vec1 const & v) GLM_FUNC_QUALIFIER lowp_vec1 inversesqrt(lowp_vec1 const & v)
{ {
float tmp(v.x); return detail::fastInversesqrt<lowp_vec1, uint>(v);
float xhalf(0.5f * tmp);
uint i = *reinterpret_cast<uint*>(const_cast<lowp_vec1*>(&v));
i = 0x5f375a86 - (i >> 1);
tmp = *reinterpret_cast<float*>(i);
tmp = tmp * (1.5f - xhalf * tmp * tmp);
return lowp_vec1(tmp);
} }
template <> template <>
GLM_FUNC_QUALIFIER lowp_vec2 inversesqrt(lowp_vec2 const & v) GLM_FUNC_QUALIFIER lowp_vec2 inversesqrt(lowp_vec2 const & v)
{ {
lowp_vec2 tmp(v); return detail::fastInversesqrt<lowp_vec2, lowp_uvec2>(v);
lowp_vec2 xhalf(0.5f * tmp);
lowp_uvec2 i = *reinterpret_cast<lowp_uvec2*>(const_cast<lowp_vec2*>(&v));
i = lowp_uvec2(0x5f375a86) - (i >> lowp_uvec2(1));
tmp = *reinterpret_cast<lowp_vec2*>(&i);
tmp = tmp * (1.5f - xhalf * tmp * tmp);
return tmp;
} }
template <> template <>
GLM_FUNC_QUALIFIER lowp_vec3 inversesqrt(lowp_vec3 const & v) GLM_FUNC_QUALIFIER lowp_vec3 inversesqrt(lowp_vec3 const & v)
{ {
lowp_vec3 tmp(v); return detail::fastInversesqrt<lowp_vec3, lowp_uvec3>(v);
lowp_vec3 xhalf(0.5f * tmp);
lowp_uvec3 i = *reinterpret_cast<lowp_uvec3*>(const_cast<lowp_vec3*>(&v));
i = lowp_uvec3(0x5f375a86) - (i >> lowp_uvec3(1));
tmp = *reinterpret_cast<lowp_vec3*>(&i);
tmp = tmp * (1.5f - xhalf * tmp * tmp);
return tmp;
} }
template <> template <>
GLM_FUNC_QUALIFIER lowp_vec4 inversesqrt(lowp_vec4 const & v) GLM_FUNC_QUALIFIER lowp_vec4 inversesqrt(lowp_vec4 const & v)
{ {
lowp_vec4 tmp(v); return detail::fastInversesqrt<lowp_vec4, lowp_uvec4>(v);
lowp_vec4 xhalf(0.5f * tmp);
lowp_uvec4 i = *reinterpret_cast<lowp_uvec4*>(const_cast<lowp_vec4*>(&v));
i = lowp_uvec4(0x5f375a86) - (i >> lowp_uvec4(1));
tmp = *reinterpret_cast<lowp_vec4*>(&i);
tmp = tmp * (1.5f - xhalf * tmp * tmp);
return tmp;
} }
}//namespace glm }//namespace glm

View File

@ -30,15 +30,7 @@ namespace glm
genType const & x genType const & x
) )
{ {
genType tmp = x; return detail::fastInversesqrt<genType, uint>(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);
} }
VECTORIZE_VEC(fastInverseSqrt) VECTORIZE_VEC(fastInverseSqrt)