Merge pull request #3720 from Beep6581/variadic_min_max

Variadic template version of `rtengine::(min|max)()`
This commit is contained in:
Floessie
2017-03-02 07:40:34 +01:00
committed by GitHub

View File

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