maths: make sign query inline

code which needs this function tends to require a fairly simple
implementation inline for the optimiser to reach more successfully. we
tended to generate function calls to this which slowed this inner loops.
This commit is contained in:
Danny Robson 2015-03-20 01:32:56 +11:00
parent 751f06dc3e
commit 585a0b464c
3 changed files with 15 additions and 26 deletions

View File

@ -152,31 +152,6 @@ template uint32_t round_pow2 (uint32_t);
template uint64_t round_pow2 (uint64_t);
//-----------------------------------------------------------------------------
template <typename T>
int
sign (T val) {
return val >= 0 ? 1 : -1;
}
template <>
int
sign (float val) {
return static_cast<int> (copysign (1.0f, val));
}
template <>
int
sign (double val) {
return static_cast<int> (copysign (1.0, val));
}
template int sign (int);
//-----------------------------------------------------------------------------
// Simple instantiations. Some functions aren't used internally to the library
// so it's easier to instantiate early and check for broken code at library

View File

@ -21,6 +21,7 @@
#define __MATHS_HPP
#include "debug.hpp"
#include "types/traits.hpp"
#include <cstdint>
#include <type_traits>
@ -104,7 +105,8 @@ digits [[gnu::pure]] (const T& value);
//-----------------------------------------------------------------------------
template <typename T>
int sign [[gnu::pure]] (T val);
typename try_signed<T>::type
sign [[gnu::pure]] (T val);
//-----------------------------------------------------------------------------

View File

@ -45,6 +45,18 @@ pow (T x, unsigned y)
}
///----------------------------------------------------------------------------
/// Return a unit type with a sign that matches the provided value
///
/// This really needs to be inline for performance as it's used in a few inner
/// loops where the possibility of a function call is a deal breaker.
template <typename T>
typename try_signed<T>::type
sign (T val) {
return val >= 0 ? T{1} : T{-1};
}
//-----------------------------------------------------------------------------
template <>
struct constants<float>