diff --git a/src/Base/Tools.h b/src/Base/Tools.h index 182d804000..88982c0ae6 100644 --- a/src/Base/Tools.h +++ b/src/Base/Tools.h @@ -113,31 +113,54 @@ inline manipulator blanks(int n) // ---------------------------------------------------------------------------- template + requires std::is_arithmetic_v inline T clamp(T num, T lower, T upper) { - return std::max(std::min(upper, num), lower); + return std::clamp(num, lower, upper); } -template -inline T sgn(T t) +/// Returns -1, 0 or 1 depending on if the value is negative, zero or positive +/// As this function might be used in hot paths, it uses branchless implementation +template +constexpr std::enable_if_t && std::is_signed_v, T> sgn(T val) { - if (t == 0) { - return T(0); - } - - return (t > 0) ? T(1) : T(-1); + int oneIfPositive = int(0 < val); + int oneIfNegative = int(val < 0); + return T(oneIfPositive - oneIfNegative); // 0/1 - 0/1 = -1/0/1 } -template -inline T toRadians(T d) +/// Convert degrees to radians, allow deduction for floating point types +template +constexpr T toRadians(T degrees) { - return static_cast((d * std::numbers::pi) / 180.0); + constexpr auto degToRad = std::numbers::pi_v / T(180); + return degrees * degToRad; } -template -inline T toDegrees(T r) +/// Convert degrees to radians, allow **explicit-only** for any arithmetic type +template + requires(std::is_arithmetic_v && !std::floating_point) +constexpr T toRadians(std::type_identity_t degrees) { - return static_cast((r / std::numbers::pi) * 180.0); + using ResultT = std::conditional_t, double, T>; + return static_cast(toRadians(static_cast(degrees))); +} + +/// Convert radians to degrees, allow deduction for floating point types +template +constexpr T toDegrees(T radians) +{ + constexpr auto radToDeg = T(180) / std::numbers::pi_v; + return radians * radToDeg; +} + +/// Convert radians to degrees, allow **explicit-only** for any arithmetic type +template + requires(std::is_arithmetic_v && !std::floating_point) +constexpr T toDegrees(std::type_identity_t radians) +{ + using ResultT = std::conditional_t, double, T>; + return static_cast(toDegrees(static_cast(radians))); } inline float fromPercent(const long value) @@ -150,7 +173,7 @@ inline long toPercent(float value) return std::lround(100.0 * value); } -template +template inline T fmod(T numerator, T denominator) { T modulo = std::fmod(numerator, denominator);