From 4ed3ad59e96d3aee9c5251a3dfee32a68753cfd5 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 28 Oct 2024 15:24:18 +0100 Subject: [PATCH] Math: make sign() branchless and constexpr --- src/Magnum/Math/Functions.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Magnum/Math/Functions.h b/src/Magnum/Math/Functions.h index f358cd51c..2cd3c4594 100644 --- a/src/Magnum/Math/Functions.h +++ b/src/Magnum/Math/Functions.h @@ -399,10 +399,9 @@ template inline Vector clamp(const Vector 0, `0` if @p x = 0 and `-1` if @p x < 0. */ -template inline typename std::enable_if::value, UnderlyingTypeOf>::type sign(T scalar) { - if(scalar > T(0)) return UnderlyingTypeOf(1); - if(scalar < T(0)) return UnderlyingTypeOf(-1); - return UnderlyingTypeOf(0); +template constexpr inline typename std::enable_if::value, UnderlyingTypeOf>::type sign(T x) { + using U = UnderlyingTypeOf; + return U(U(bool(x > T(0))) - U(bool(x < T(0)))); } /** @overload */