mirror of
https://github.com/bkaradzic/bx.git
synced 2026-02-17 20:52:37 +01:00
Added isPowerOf2 function.
This commit is contained in:
@@ -33,7 +33,7 @@ namespace bx
|
||||
|
||||
/// Template for avoiding MSVC: C4127: conditional expression is constant
|
||||
template<bool>
|
||||
bool isEnabled();
|
||||
constexpr bool isEnabled();
|
||||
|
||||
/// Exchange two values.
|
||||
template<typename Ty>
|
||||
@@ -44,27 +44,31 @@ namespace bx
|
||||
|
||||
/// Returns minimum of two values.
|
||||
template<typename Ty>
|
||||
Ty min(const Ty& _a, const Ty& _b);
|
||||
constexpr Ty min(const Ty& _a, const Ty& _b);
|
||||
|
||||
/// Returns maximum of two values.
|
||||
template<typename Ty>
|
||||
Ty max(const Ty& _a, const Ty& _b);
|
||||
constexpr 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);
|
||||
constexpr 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);
|
||||
constexpr 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);
|
||||
constexpr 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);
|
||||
constexpr Ty clamp(const Ty& _a, const Ty& _min, const Ty& _max);
|
||||
|
||||
/// Returns true if value is power of 2.
|
||||
template<typename Ty>
|
||||
constexpr bool isPowerOf2(Ty _a);
|
||||
|
||||
// http://cnicholson.net/2011/01/stupid-c-tricks-a-better-sizeof_array/
|
||||
template<typename T, size_t N>
|
||||
|
||||
@@ -10,18 +10,18 @@
|
||||
namespace bx
|
||||
{
|
||||
template<bool>
|
||||
inline bool isEnabled()
|
||||
inline constexpr bool isEnabled()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
template<>
|
||||
inline bool isEnabled<false>()
|
||||
inline constexpr bool isEnabled<false>()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool ignoreC4127(bool _x)
|
||||
inline constexpr bool ignoreC4127(bool _x)
|
||||
{
|
||||
return _x;
|
||||
}
|
||||
@@ -33,39 +33,45 @@ namespace bx
|
||||
}
|
||||
|
||||
template<typename Ty>
|
||||
inline Ty min(const Ty& _a, const Ty& _b)
|
||||
inline constexpr Ty min(const Ty& _a, const Ty& _b)
|
||||
{
|
||||
return _a < _b ? _a : _b;
|
||||
}
|
||||
|
||||
template<typename Ty>
|
||||
inline Ty max(const Ty& _a, const Ty& _b)
|
||||
inline constexpr Ty max(const Ty& _a, const Ty& _b)
|
||||
{
|
||||
return _a > _b ? _a : _b;
|
||||
}
|
||||
|
||||
template<typename Ty>
|
||||
inline Ty min(const Ty& _a, const Ty& _b, const Ty& _c)
|
||||
inline constexpr 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)
|
||||
inline constexpr 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)
|
||||
inline constexpr 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)
|
||||
inline constexpr Ty clamp(const Ty& _a, const Ty& _min, const Ty& _max)
|
||||
{
|
||||
return max(min(_a, _max), _min);
|
||||
}
|
||||
|
||||
template<typename Ty>
|
||||
inline constexpr bool isPowerOf2(Ty _a)
|
||||
{
|
||||
return _a && !(_a & (_a - 1) );
|
||||
}
|
||||
|
||||
} // namespace bx
|
||||
|
||||
Reference in New Issue
Block a user