Merge pull request #271 from plasmacel/master

fast branchless sign function #271
master
Christophe Riccio ago%!(EXTRA string=11 years)
commit fe20b216a0
  1. 13
      glm/detail/func_common.inl

@ -151,22 +151,15 @@ namespace detail
} }
// sign // sign
//Try something like based on x >> 31 to get the sign bit // fast and works for any type
template <typename genFIType> template <typename genFIType>
GLM_FUNC_QUALIFIER genFIType sign(genFIType x) GLM_FUNC_QUALIFIER genFIType sign(genFIType x)
{ {
GLM_STATIC_ASSERT( GLM_STATIC_ASSERT(
std::numeric_limits<genFIType>::is_iec559 || std::numeric_limits<genFIType>::is_iec559 ||
(std::numeric_limits<genFIType>::is_signed && std::numeric_limits<genFIType>::is_integer), "'sign' only accept signed inputs"); (std::numeric_limits<genFIType>::is_signed && std::numeric_limits<genFIType>::is_integer), "'sign' only accept signed inputs");
genFIType result; return genFIType(genFIType(0) < x) - (x < genFIType(0));
if(x > genFIType(0))
result = genFIType(1);
else if(x < genFIType(0))
result = genFIType(-1);
else
result = genFIType(0);
return result;
} }
template <typename T, precision P, template <typename, precision> class vecType> template <typename T, precision P, template <typename, precision> class vecType>

Loading…
Cancel
Save