src/core/math_func.hpp
branchnoai
changeset 11126 72d4c9314c72
parent 9869 6404afe43575
--- a/src/core/math_func.hpp	Mon Jun 30 21:31:23 2008 +0000
+++ b/src/core/math_func.hpp	Wed Jul 09 13:32:13 2008 +0000
@@ -27,7 +27,8 @@
  * @param b The second value
  * @return The greater value or a if equals
  */
-template<typename T> static inline T max(const T a, const T b)
+template <typename T>
+static FORCEINLINE T max(const T a, const T b)
 {
 	return (a >= b) ? a : b;
 }
@@ -42,7 +43,8 @@
  * @param b The second value
  * @return The smaller value or b if equals
  */
-template<typename T> static inline T min(const T a, const T b)
+template <typename T>
+static FORCEINLINE T min(const T a, const T b)
 {
 	return (a < b) ? a : b;
 }
@@ -56,7 +58,7 @@
  * @param b The second integer
  * @return The smaller value
  */
-static inline int min(const int a, const int b)
+static FORCEINLINE int min(const int a, const int b)
 {
 	return (a < b) ? a : b;
 }
@@ -70,7 +72,7 @@
  * @param b The second unsigned integer
  * @return The smaller value
  */
-static inline uint minu(const uint a, const uint b)
+static FORCEINLINE uint minu(const uint a, const uint b)
 {
 	return (a < b) ? a : b;
 }
@@ -82,7 +84,8 @@
  * @param a The value we want to unsign
  * @return The unsigned value
  */
-template <typename T> static inline T abs(const T a)
+template <typename T>
+static FORCEINLINE T abs(const T a)
 {
 	return (a < (T)0) ? -a : a;
 }
@@ -95,10 +98,31 @@
  * @param n The base of the number we are searching
  * @return The smallest multiple of n equal or greater than x
  */
-template<typename T> static inline T Align(const T x, uint n)
+template <typename T>
+static FORCEINLINE T Align(const T x, uint n)
 {
+	assert((n & (n - 1)) == 0 && n != 0);
 	n--;
-	return (T)((x + n) & ~(n));
+	return (T)((x + n) & ~((T)n));
+}
+
+/**
+ * Return the smallest multiple of n equal or greater than x
+ * Applies to pointers only
+ *
+ * @note n must be a power of 2
+ * @param x The min value
+ * @param n The base of the number we are searching
+ * @return The smallest multiple of n equal or greater than x
+ * @see Align()
+ */
+
+assert_compile(sizeof(size_t) == sizeof(void *));
+
+template <typename T>
+static FORCEINLINE T *AlignPtr(T *x, uint n)
+{
+	return (T *)Align((size_t)x, n);
 }
 
 /**
@@ -117,7 +141,7 @@
  * @returns A value between min and max which is closest to a.
  * @see ClampU(uint, uint, uint)
  */
-static inline int Clamp(const int a, const int min, const int max)
+static FORCEINLINE int Clamp(const int a, const int min, const int max)
 {
 	if (a <= min) return min;
 	if (a >= max) return max;
@@ -140,7 +164,7 @@
  * @returns A value between min and max which is closest to a.
  * @see Clamp(int, int, int)
  */
-static inline uint ClampU(const uint a, const uint min, const uint max)
+static FORCEINLINE uint ClampU(const uint a, const uint min, const uint max)
 {
 	if (a <= min) return min;
 	if (a >= max) return max;
@@ -161,7 +185,7 @@
  * @return The 64-bit value reduced to a 32-bit value
  * @see Clamp(int, int, int)
  */
-static inline int32 ClampToI32(const int64 a)
+static FORCEINLINE int32 ClampToI32(const int64 a)
 {
 	if (a <= INT32_MIN) return INT32_MIN;
 	if (a >= INT32_MAX) return INT32_MAX;
@@ -175,7 +199,7 @@
  * @return The 64-bit value reduced to a 16-bit value
  * @see ClampU(uint, uint, uint)
  */
-static inline uint16 ClampToU16(const uint64 a)
+static FORCEINLINE uint16 ClampToU16(const uint64 a)
 {
 	return (uint16)(a <= UINT16_MAX ? a : UINT16_MAX);
 }
@@ -187,7 +211,9 @@
  * @param b The second scalar
  * @return The absolute difference between the given scalars
  */
-template <typename T> static inline T Delta(const T a, const T b) {
+template <typename T>
+static FORCEINLINE T Delta(const T a, const T b)
+{
 	return (a < b) ? b - a : a - b;
 }
 
@@ -203,7 +229,8 @@
  * @param size The size of the interval
  * @return True if the value is in the interval, false else.
  */
-template<typename T> static inline bool IsInsideBS(const T x, const uint base, const uint size)
+template <typename T>
+static FORCEINLINE bool IsInsideBS(const T x, const uint base, const uint size)
 {
 	return (uint)(x - base) < size;
 }
@@ -218,7 +245,8 @@
  * @param max The maximum of the interval
  * @see IsInsideBS()
  */
-template<typename T> static inline bool IsInsideMM(const T x, const uint min, const uint max)
+template <typename T>
+static FORCEINLINE bool IsInsideMM(const T x, const uint min, const uint max)
 {
 	return (uint)(x - min) < (max - min);
 }
@@ -228,7 +256,8 @@
  * @param a variable to swap with b
  * @param b variable to swap with a
  */
-template<typename T> void Swap(T& a, T& b)
+template <typename T>
+static FORCEINLINE void Swap(T &a, T &b)
 {
 	T t = a;
 	a = b;