From 181670afbb7c05af1748a29dbaf5803c470fe364 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Tue, 23 Jan 2018 18:20:38 -0800 Subject: [PATCH] Added isPowerOf2 function. --- include/bx/bx.h | 18 +++++++++++------- include/bx/inline/bx.inl | 24 +++++++++++++++--------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/include/bx/bx.h b/include/bx/bx.h index 26ab38d..a819eca 100644 --- a/include/bx/bx.h +++ b/include/bx/bx.h @@ -33,7 +33,7 @@ namespace bx /// Template for avoiding MSVC: C4127: conditional expression is constant template - bool isEnabled(); + constexpr bool isEnabled(); /// Exchange two values. template @@ -44,27 +44,31 @@ namespace bx /// Returns minimum of two values. template - Ty min(const Ty& _a, const Ty& _b); + constexpr Ty min(const Ty& _a, const Ty& _b); /// Returns maximum of two values. template - Ty max(const Ty& _a, const Ty& _b); + constexpr Ty max(const Ty& _a, const Ty& _b); /// Returns minimum of three values. template - 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 - 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 - 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 - 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 + constexpr bool isPowerOf2(Ty _a); // http://cnicholson.net/2011/01/stupid-c-tricks-a-better-sizeof_array/ template diff --git a/include/bx/inline/bx.inl b/include/bx/inline/bx.inl index 6324001..cdd154d 100644 --- a/include/bx/inline/bx.inl +++ b/include/bx/inline/bx.inl @@ -10,18 +10,18 @@ namespace bx { template - inline bool isEnabled() + inline constexpr bool isEnabled() { return true; } template<> - inline bool isEnabled() + inline constexpr bool isEnabled() { return false; } - inline bool ignoreC4127(bool _x) + inline constexpr bool ignoreC4127(bool _x) { return _x; } @@ -33,39 +33,45 @@ namespace bx } template - 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 - 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 - 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 - 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 - 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 - 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 + inline constexpr bool isPowerOf2(Ty _a) + { + return _a && !(_a & (_a - 1) ); + } + } // namespace bx