Added isPowerOf2 function.

This commit is contained in:
Branimir Karadžić
2018-01-23 18:20:38 -08:00
parent 13ebfdc422
commit 181670afbb
2 changed files with 26 additions and 16 deletions

View File

@@ -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>

View File

@@ -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