diff --git a/rtengine/rt_math.h b/rtengine/rt_math.h index a7cbbf301..e1c01a94f 100644 --- a/rtengine/rt_math.h +++ b/rtengine/rt_math.h @@ -27,71 +27,68 @@ constexpr float RT_PI_F_2 = RT_PI_2; constexpr float RT_INFINITY_F = std::numeric_limits::infinity(); constexpr float RT_NAN_F = std::numeric_limits::quiet_NaN(); -template -inline _Tp SQR (_Tp x) +template +constexpr T SQR(T x) { -// return std::pow(x,2); Slower than: return x * x; } -template -inline const _Tp& min(const _Tp& a, const _Tp& b) +template +constexpr const T& min(const T& a) { - return std::min(a, b); + return a; } -template -inline const _Tp& max(const _Tp& a, const _Tp& b) +template +constexpr const T& min(const T& a, const T& b) { - return std::max(a, b); + return a < b ? a : b; } - -template -inline const _Tp& LIM(const _Tp& a, const _Tp& b, const _Tp& c) +template +constexpr const T& min(const T& a, const T& b, const ARGS&... args) { - return std::max(b, std::min(a, c)); + return min(min(a, b), min(args...)); } -template -inline _Tp LIM01(const _Tp& a) +template +constexpr const T& max(const T& a) { - return std::max(_Tp(0), std::min(a, _Tp(1))); + return a; } -template -inline _Tp CLIP(const _Tp& a) +template +constexpr const T& max(const T& a, const T& b) { - return LIM(a, static_cast<_Tp>(0), static_cast<_Tp>(MAXVAL)); + return a < b ? b : a; } - -template -inline const _Tp& min(const _Tp& a, const _Tp& b, const _Tp& c) +template +constexpr const T& max(const T& a, const T& b, const ARGS&... args) { - return std::min(c, std::min(a, b)); + return max(max(a, b), max(args...)); } -template -inline const _Tp& max(const _Tp& a, const _Tp& b, const _Tp& c) +template +constexpr const T& LIM(const T& a, const T& b, const T& c) { - return std::max(c, std::max(a, b)); + return max(b, min(a, c)); } -template -inline const _Tp& min(const _Tp& a, const _Tp& b, const _Tp& c, const _Tp& d) +template +constexpr T LIM01(const T& a) { - return std::min(d, std::min(c, std::min(a, b))); + return max(T(0), min(a, T(1))); } -template -inline const _Tp& max(const _Tp& a, const _Tp& b, const _Tp& c, const _Tp& d) +template +constexpr T CLIP(const T& a) { - return std::max(d, std::max(c, std::max(a, b))); + return LIM(a, static_cast(0), static_cast(MAXVAL)); } -template -inline _Tp intp(_Tp a, _Tp b, _Tp c) +template +constexpr T intp(T a, T b, T c) { // calculate a * b + (1 - a) * c // following is valid: @@ -115,13 +112,13 @@ inline T norm2(const T& x, const T& y) template< typename T > inline T norminf(const T& x, const T& y) { - return std::max(std::abs(x), std::abs(y)); + return max(std::abs(x), std::abs(y)); } -inline int float2uint16range(float d) // clips input to [0;65535] and rounds +constexpr int float2uint16range(float d) { - d = CLIP(d); // clip to [0;65535] - return d + 0.5f; + // clips input to [0;65535] and rounds + return CLIP(d) + 0.5f; } constexpr std::uint8_t uint16ToUint8Rounded(std::uint16_t i)