Added multiple 'sign' implementations for integers, unit tests and performancetests.

This commit is contained in:
Christophe Riccio
2014-10-29 00:18:41 +01:00
parent 378074ee66
commit dced21e745
5 changed files with 248 additions and 19 deletions

View File

@@ -43,11 +43,12 @@ namespace detail
template <typename genFIType>
struct compute_abs<genFIType, true>
{
GLM_FUNC_QUALIFIER static genFIType call(genFIType const & x)
GLM_FUNC_QUALIFIER static genFIType call(genFIType x)
{
GLM_STATIC_ASSERT(
std::numeric_limits<genFIType>::is_iec559 || std::numeric_limits<genFIType>::is_signed,
"'abs' only accept floating-point and integer scalar or vector inputs");
return x >= genFIType(0) ? x : -x;
// TODO, perf comp with: *(((int *) &x) + 1) &= 0x7fffffff;
}
@@ -56,7 +57,7 @@ namespace detail
template <typename genFIType>
struct compute_abs<genFIType, false>
{
GLM_FUNC_QUALIFIER static genFIType call(genFIType const & x)
GLM_FUNC_QUALIFIER static genFIType call(genFIType x)
{
GLM_STATIC_ASSERT(
!std::numeric_limits<genFIType>::is_signed && std::numeric_limits<genFIType>::is_integer,
@@ -130,6 +131,13 @@ namespace detail
}//namespace detail
// abs
template <>
GLM_FUNC_QUALIFIER int32 abs(int32 x)
{
int32 const y = x >> 31;
return (x ^ y) - y;
}
template <typename genFIType>
GLM_FUNC_QUALIFIER genFIType abs(genFIType x)
{

View File

@@ -57,14 +57,14 @@ namespace glm
/// Build a mask of 'count' bits
///
/// @see gtc_bitfield
template <typename genType>
GLM_FUNC_DECL genType mask(genType Bits);
template <typename genIUType>
GLM_FUNC_DECL genIUType mask(genIUType Bits);
/// Build a mask of 'count' bits
///
/// @see gtc_bitfield
template <typename T, precision P, template <typename, precision> class vecType>
GLM_FUNC_DECL vecType<T, P> mask(vecType<T, P> const & v);
template <typename T, precision P, template <typename, precision> class vecIUType>
GLM_FUNC_DECL vecIUType<T, P> mask(vecIUType<T, P> const & v);
/// Rotate all bits to the right. All the bits dropped in the right side are inserted back on the left side.
///

View File

@@ -245,16 +245,16 @@ namespace detail
}
}//namespace detail
template <typename genType>
GLM_FUNC_QUALIFIER genType mask(genType Bits)
template <typename genIUType>
GLM_FUNC_QUALIFIER genIUType mask(genIUType Bits)
{
GLM_STATIC_ASSERT(std::numeric_limits<genIType>::is_integer, "'mask' accepts only integer values");
GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'mask' accepts only integer values");
return ~((~static_cast<genType>(0)) << Bits);
return ~((~static_cast<genIUType>(0)) << Bits);
}
template <typename T, precision P, template <typename, precision> class vecType>
GLM_FUNC_QUALIFIER vecType<T, P> mask(vecType<T, P> const & v)
template <typename T, precision P, template <typename, precision> class vecIUType>
GLM_FUNC_QUALIFIER vecIUType<T, P> mask(vecIUType<T, P> const & v)
{
GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'mask' accepts only integer values");