maths: use __builtin_signbit to handle +- zeros

This commit is contained in:
Danny Robson 2015-03-24 02:42:42 +11:00
parent cf9db48e0e
commit 93ed983a6f
2 changed files with 20 additions and 7 deletions

View File

@ -104,9 +104,9 @@ digits [[gnu::pure]] (const T& value);
//-----------------------------------------------------------------------------
template <typename T>
typename try_signed<T>::type
sign [[gnu::pure]] (T val);
constexpr int sign (int);
constexpr float sign (float);
constexpr double sign (double);
//-----------------------------------------------------------------------------

View File

@ -50,10 +50,23 @@ pow (T x, unsigned y)
///
/// 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};
constexpr int
sign (int v)
{
return __builtin_signbitl (v) ? -1 : 1;
}
constexpr float
sign (float v)
{
return __builtin_signbitf (v) ? -1.f : 1.f;
}
constexpr double
sign (double v)
{
return __builtin_signbit (v) ? -1. : 1.f;
}