Added vectorized implementation of fast inversesqrt

master
Christophe Riccio ago%!(EXTRA string=12 years)
parent 9b57315681
commit 6ed8c3dbb3
  1. 79
      glm/detail/func_exponential.inl
  2. 4
      glm/detail/func_geometric.hpp
  3. 13
      glm/gtx/fast_square_root.inl

@ -153,68 +153,53 @@ namespace detail
VECTORIZE_VEC(sqrt) VECTORIZE_VEC(sqrt)
template <typename genType>
GLM_FUNC_QUALIFIER genType inversesqrt
(
genType const & x
)
{
GLM_STATIC_ASSERT(
std::numeric_limits<genType>::is_iec559,
"'inversesqrt' only accept floating-point inputs");
assert(x > genType(0));
return genType(1) / std::sqrt(x);
}
VECTORIZE_VEC(inversesqrt)
namespace detail namespace detail
{ {
template <typename genType, typename genUType> template <template <class, precision> class vecType, typename T, precision P>
genType fastInversesqrt(genType const & v) struct compute_inversesqrt
{ {
genType tmp(v); static vecType<T, P> call(vecType<T, P> const & x)
genType xhalf(tmp * genType(0.5f)); {
genUType i = *reinterpret_cast<genUType*>(const_cast<genType*>(&v)); return static_cast<T>(1) / sqrt(x);
i = genUType(0x5f375a86) - (i >> genUType(1));
tmp = *reinterpret_cast<genType*>(&i);
tmp = tmp * (genType(1.5f) - xhalf * tmp * tmp);
return tmp;
}
} }
};
template <> template <template <class, precision> class vecType>
GLM_FUNC_QUALIFIER lowp_vec1 inversesqrt(lowp_vec1 const & v) struct compute_inversesqrt<vecType, float, lowp>
{ {
assert(glm::all(glm::greaterThan(v, lowp_vec1(0)))); static vecType<float, lowp> call(vecType<float, lowp> const & x)
{
return detail::fastInversesqrt<lowp_vec1, uint>(v); vecType<float, lowp> tmp(x);
vecType<float, lowp> xhalf(tmp * 0.5f);
vecType<uint, lowp> i = *reinterpret_cast<vecType<uint, lowp>*>(const_cast<vecType<float, lowp>*>(&x));
i = vecType<uint, lowp>(0x5f375a86) - (i >> vecType<uint, lowp>(1));
tmp = *reinterpret_cast<vecType<float, lowp>*>(&i);
tmp = tmp * (1.5f - xhalf * tmp * tmp);
return tmp;
} }
};
}//namespace detail
template <> // inversesqrt
GLM_FUNC_QUALIFIER lowp_vec2 inversesqrt(lowp_vec2 const & v) GLM_FUNC_QUALIFIER float inversesqrt(float const & x)
{ {
assert(glm::all(glm::greaterThan(v, lowp_vec2(0)))); return 1.0f / sqrt(x);
return detail::fastInversesqrt<lowp_vec2, lowp_uvec2>(v);
} }
template <> GLM_FUNC_QUALIFIER double inversesqrt(double const & x)
GLM_FUNC_QUALIFIER lowp_vec3 inversesqrt(lowp_vec3 const & v)
{ {
assert(glm::all(glm::greaterThan(v, lowp_vec3(0)))); return 1.0 / sqrt(x);
return detail::fastInversesqrt<lowp_vec3, lowp_uvec3>(v);
} }
template <> template <template <class, precision> class vecType, typename T, precision P>
GLM_FUNC_QUALIFIER lowp_vec4 inversesqrt(lowp_vec4 const & v) GLM_FUNC_QUALIFIER vecType<T, P> inversesqrt
(
vecType<T, P> const & x
)
{ {
assert(glm::all(glm::greaterThan(v, lowp_vec4(0)))); GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'inversesqrt' only accept floating-point inputs");
return detail::compute_inversesqrt<vecType, T, P>::call(x);
return detail::fastInversesqrt<lowp_vec4, lowp_uvec4>(v);
} }
VECTORIZE_VEC(inversesqrt)
}//namespace glm }//namespace glm

@ -74,7 +74,7 @@ namespace glm
GLM_FUNC_DECL T dot( GLM_FUNC_DECL T dot(
vecType<T, P> const & x, vecType<T, P> const & x,
vecType<T, P> const & y); vecType<T, P> const & y);
/*
/// Returns the dot product of x and y, i.e., result = x * y. /// Returns the dot product of x and y, i.e., result = x * y.
/// ///
/// @tparam genType Floating-point vector types. /// @tparam genType Floating-point vector types.
@ -85,7 +85,7 @@ namespace glm
GLM_FUNC_DECL genType dot( GLM_FUNC_DECL genType dot(
genType const & x, genType const & x,
genType const & y); genType const & y);
*/
/// Returns the cross product of x and y. /// Returns the cross product of x and y.
/// ///
/// @tparam valType Floating-point scalar types. /// @tparam valType Floating-point scalar types.

@ -24,13 +24,18 @@ namespace glm
VECTORIZE_VEC(fastSqrt) VECTORIZE_VEC(fastSqrt)
// fastInversesqrt // fastInversesqrt
template <typename genType> GLM_FUNC_QUALIFIER float fastInverseSqrt(float x)
GLM_FUNC_QUALIFIER genType fastInverseSqrt {
return detail::compute_inversesqrt<detail::tvec1, float, lowp>::call(x).x;
}
template <template <class, precision> class vecType, typename T, precision P>
GLM_FUNC_QUALIFIER vecType<T, P> fastInverseSqrt
( (
genType const & x vecType<T, P> const & x
) )
{ {
return detail::fastInversesqrt<genType, uint>(x); return detail::compute_inversesqrt<vecType, T, P>::call(x);
} }
VECTORIZE_VEC(fastInverseSqrt) VECTORIZE_VEC(fastInverseSqrt)

Loading…
Cancel
Save