Added min/max with 3 values, and clamp.

This commit is contained in:
Branimir Karadžić
2017-11-29 20:29:06 -08:00
parent 9ac1acc9e9
commit 85b8a94111
2 changed files with 44 additions and 7 deletions

View File

@@ -35,24 +35,37 @@ namespace bx
template<bool>
bool isEnabled();
///
bool ignoreC4127(bool _x);
///
/// Exchange two values.
template<typename Ty>
void xchg(Ty& _a, Ty& _b);
///
/// Exchange memory.
void xchg(void* _a, void* _b, size_t _numBytes);
///
/// Returns minimum of two values.
template<typename Ty>
Ty min(const Ty& _a, const Ty& _b);
///
/// Returns maximum of two values.
template<typename Ty>
Ty max(const Ty& _a, const Ty& _b);
/// Returns minimum of three values.
template<typename Ty>
Ty min(const Ty& _a, const Ty& _b, const Ty& _c);
/// Returns maximum of three values.
template<typename Ty>
Ty max(const Ty& _a, const Ty& _b, const Ty& _c);
/// Returns middle of three values.
template<typename Ty>
Ty mid(const Ty& _a, const Ty& _b, const Ty& _c);
/// Returns clamped value between min/max.
template<typename Ty>
Ty clamp(const Ty& _a, const Ty& _min, const Ty& _max);
// http://cnicholson.net/2011/01/stupid-c-tricks-a-better-sizeof_array/
template<typename T, size_t N>
char (&COUNTOF_REQUIRES_ARRAY_ARGUMENT(const T(&)[N]) )[N];

View File

@@ -44,4 +44,28 @@ namespace bx
return _a > _b ? _a : _b;
}
template<typename Ty>
inline Ty min(const Ty& _a, const Ty& _b, const Ty& _c)
{
return min(min(_a, _b), _c);
}
template<typename Ty>
inline Ty max(const Ty& _a, const Ty& _b, const Ty& _c)
{
return max(max(_a, _b), _c);
}
template<typename Ty>
inline Ty mid(const Ty& _a, const Ty& _b, const Ty& _c)
{
return max(min(_a, _b), min(max(_a, _b), _c) );
}
template<typename Ty>
inline Ty clamp(const Ty& _a, const Ty& _min, const Ty& _max)
{
return max(min(_a, _max), _min);
}
} // namespace bx