# math_func.hpp

Go to the documentation of this file.

    00001 /* $Id: math_func.hpp 13639 2008-06-26 15:46:19Z smatz $ */
    00002
    00005 #ifndef MATH_FUNC_HPP
    00006 #define MATH_FUNC_HPP
    00007
    00008 #ifdef min
    00009 #undef min
    00010 #endif
    00011
    00012 #ifdef max
    00013 #undef max
    00014 #endif
    00015
    00016 #ifdef abs
    00017 #undef abs
    00018 #endif
    00019
    00030 template <typename T>
    00031 static FORCEINLINE T max(const T a, const T b)
    00032 {
    00033   return (a >= b) ? a : b;
    00034 }
    00035
    00046 template <typename T>
    00047 static FORCEINLINE T min(const T a, const T b)
    00048 {
    00049   return (a < b) ? a : b;
    00050 }
    00051
    00061 static FORCEINLINE int min(const int a, const int b)
    00062 {
    00063   return (a < b) ? a : b;
    00064 }
    00065
    00075 static FORCEINLINE uint minu(const uint a, const uint b)
    00076 {
    00077   return (a < b) ? a : b;
    00078 }
    00079
    00087 template <typename T>
    00088 static FORCEINLINE T abs(const T a)
    00089 {
    00090   return (a < (T)0) ? -a : a;
    00091 }
    00092
    00101 template <typename T>
    00102 static FORCEINLINE T Align(const T x, uint n)
    00103 {
    00104   assert((n & (n - 1)) == 0 && n != 0);
    00105   n--;
    00106   return (T)((x + n) & ~((T)n));
    00107 }
    00108
    00120 assert_compile(sizeof(size_t) == sizeof(void *));
    00121
    00122 template <typename T>
    00123 static FORCEINLINE T *AlignPtr(T *x, uint n)
    00124 {
    00125   return (T *)Align((size_t)x, n);
    00126 }
    00127
    00144 static FORCEINLINE int Clamp(const int a, const int min, const int max)
    00145 {
    00146   if (a <= min) return min;
    00147   if (a >= max) return max;
    00148   return a;
    00149 }
    00150
    00167 static FORCEINLINE uint ClampU(const uint a, const uint min, const uint max)
    00168 {
    00169   if (a <= min) return min;
    00170   if (a >= max) return max;
    00171   return a;
    00172 }
    00173
    00188 static FORCEINLINE int32 ClampToI32(const int64 a)
    00189 {
    00190   if (a <= INT32_MIN) return INT32_MIN;
    00191   if (a >= INT32_MAX) return INT32_MAX;
    00192   return (int32)a;
    00193 }
    00194
    00202 static FORCEINLINE uint16 ClampToU16(const uint64 a)
    00203 {
    00204   return (uint16)(a <= UINT16_MAX ? a : UINT16_MAX);
    00205 }
    00206
    00214 template <typename T>
    00215 static FORCEINLINE T Delta(const T a, const T b)
    00216 {
    00217   return (a < b) ? b - a : a - b;
    00218 }
    00219
    00232 template <typename T>
    00233 static FORCEINLINE bool IsInsideBS(const T x, const uint base, const uint size)
    00234 {
    00235   return (uint)(x - base) < size;
    00236 }
    00237
    00248 template <typename T>
    00249 static FORCEINLINE bool IsInsideMM(const T x, const uint min, const uint max)
    00250 {
    00251   return (uint)(x - min) < (max - min);
    00252 }
    00253
    00259 template <typename T>
    00260 static FORCEINLINE void Swap(T &a, T &b)
    00261 {
    00262   T t = a;
    00263   a = b;
    00264   b = t;
    00265 }
    00266
    00267 #endif /* MATH_FUNC_HPP */