Browse Source

Math: make sign() branchless and constexpr

pull/651/head
Stanislaw Halik 2 years ago
parent
commit
4ed3ad59e9
  1. 7
      src/Magnum/Math/Functions.h

7
src/Magnum/Math/Functions.h

@ -399,10 +399,9 @@ template<std::size_t size, class T> inline Vector<size, T> clamp(const Vector<si
Returns `1` if @p x > 0, `0` if @p x = 0 and `-1` if @p x < 0. Returns `1` if @p x > 0, `0` if @p x = 0 and `-1` if @p x < 0.
*/ */
template<class T> inline typename std::enable_if<IsScalar<T>::value, UnderlyingTypeOf<T>>::type sign(T scalar) { template<class T> constexpr inline typename std::enable_if<IsScalar<T>::value, UnderlyingTypeOf<T>>::type sign(T x) {
if(scalar > T(0)) return UnderlyingTypeOf<T>(1); using U = UnderlyingTypeOf<T>;
if(scalar < T(0)) return UnderlyingTypeOf<T>(-1); return U(U(bool(x > T(0))) - U(bool(x < T(0))));
return UnderlyingTypeOf<T>(0);
} }
/** @overload */ /** @overload */

Loading…
Cancel
Save