Branimir Karadžić 8 лет назад
Родитель
Сommit
181670afbb
2 измененных файлов с 26 добавлено и 16 удалено
  1. 11 7
      include/bx/bx.h
  2. 15 9
      include/bx/inline/bx.inl

+ 11 - 7
include/bx/bx.h

@@ -33,7 +33,7 @@ namespace bx
 
 
 	/// Template for avoiding MSVC: C4127: conditional expression is constant
 	/// Template for avoiding MSVC: C4127: conditional expression is constant
 	template<bool>
 	template<bool>
-	bool isEnabled();
+	constexpr bool isEnabled();
 
 
 	/// Exchange two values.
 	/// Exchange two values.
 	template<typename Ty>
 	template<typename Ty>
@@ -44,27 +44,31 @@ namespace bx
 
 
 	/// Returns minimum of two values.
 	/// Returns minimum of two values.
 	template<typename Ty>
 	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.
 	/// Returns maximum of two values.
 	template<typename Ty>
 	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.
 	/// Returns minimum of three values.
 	template<typename Ty>
 	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.
 	/// Returns maximum of three values.
 	template<typename Ty>
 	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.
 	/// Returns middle of three values.
 	template<typename Ty>
 	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.
 	/// Returns clamped value between min/max.
 	template<typename Ty>
 	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/
 	// http://cnicholson.net/2011/01/stupid-c-tricks-a-better-sizeof_array/
 	template<typename T, size_t N>
 	template<typename T, size_t N>

+ 15 - 9
include/bx/inline/bx.inl

@@ -10,18 +10,18 @@
 namespace bx
 namespace bx
 {
 {
 	template<bool>
 	template<bool>
-	inline bool isEnabled()
+	inline constexpr bool isEnabled()
 	{
 	{
 		return true;
 		return true;
 	}
 	}
 
 
 	template<>
 	template<>
-	inline bool isEnabled<false>()
+	inline constexpr bool isEnabled<false>()
 	{
 	{
 		return false;
 		return false;
 	}
 	}
 
 
-	inline bool ignoreC4127(bool _x)
+	inline constexpr bool ignoreC4127(bool _x)
 	{
 	{
 		return _x;
 		return _x;
 	}
 	}
@@ -33,39 +33,45 @@ namespace bx
 	}
 	}
 
 
 	template<typename Ty>
 	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;
 		return _a < _b ? _a : _b;
 	}
 	}
 
 
 	template<typename Ty>
 	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;
 		return _a > _b ? _a : _b;
 	}
 	}
 
 
 	template<typename Ty>
 	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);
 		return min(min(_a, _b), _c);
 	}
 	}
 
 
 	template<typename Ty>
 	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);
 		return max(max(_a, _b), _c);
 	}
 	}
 
 
 	template<typename Ty>
 	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) );
 		return max(min(_a, _b), min(max(_a, _b), _c) );
 	}
 	}
 
 
 	template<typename Ty>
 	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);
 		return max(min(_a, _max), _min);
 	}
 	}
 
 
+	template<typename Ty>
+	inline constexpr bool isPowerOf2(Ty _a)
+	{
+		return _a && !(_a & (_a - 1) );
+	}
+
 } // namespace bx
 } // namespace bx