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:
parent
751f06dc3e
commit
585a0b464c
25
maths.cpp
25
maths.cpp
@ -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
|
||||
|
@ -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);
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
12
maths.ipp
12
maths.ipp
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user