# math_func.hpp

Go to the documentation of this file.

    00001 /* $Id: math_func.hpp 12867 2008-04-24 12:15:24Z rubidium $ */
    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> static inline T max(const T a, const T b)
    00031 {
    00032   return (a >= b) ? a : b;
    00033 }
    00034
    00045 template<typename T> static inline T min(const T a, const T b)
    00046 {
    00047   return (a < b) ? a : b;
    00048 }
    00049
    00059 static inline int min(const int a, const int b)
    00060 {
    00061   return (a < b) ? a : b;
    00062 }
    00063
    00073 static inline uint minu(const uint a, const uint b)
    00074 {
    00075   return (a < b) ? a : b;
    00076 }
    00077
    00085 template <typename T> static inline T abs(const T a)
    00086 {
    00087   return (a < (T)0) ? -a : a;
    00088 }
    00089
    00098 template<typename T> static inline T Align(const T x, uint n)
    00099 {
    00100   n--;
    00101   return (T)((x + n) & ~(n));
    00102 }
    00103
    00120 static inline int Clamp(const int a, const int min, const int max)
    00121 {
    00122   if (a <= min) return min;
    00123   if (a >= max) return max;
    00124   return a;
    00125 }
    00126
    00143 static inline uint ClampU(const uint a, const uint min, const uint max)
    00144 {
    00145   if (a <= min) return min;
    00146   if (a >= max) return max;
    00147   return a;
    00148 }
    00149
    00164 static inline int32 ClampToI32(const int64 a)
    00165 {
    00166   if (a <= (int32)0x80000000) return 0x80000000;
    00167   if (a >= (int32)0x7FFFFFFF) return 0x7FFFFFFF;
    00168   return (int32)a;
    00169 }
    00170
    00178 static inline uint16 ClampToU16(const uint64 a)
    00179 {
    00180   return (uint16)(a <= 0xFFFFU ? a : 0xFFFFU);
    00181 }
    00182
    00190 template <typename T> static inline T Delta(const T a, const T b) {
    00191   return (a < b) ? b - a : a - b;
    00192 }
    00193
    00206 template<typename T> static inline bool IsInsideBS(const T x, const uint base, const uint size)
    00207 {
    00208   return (uint)(x - base) < size;
    00209 }
    00210
    00221 template<typename T> static inline bool IsInsideMM(const T x, const uint min, const uint max)
    00222 {
    00223   return (uint)(x - min) < (max - min);
    00224 }
    00225
    00231 template<typename T> void Swap(T& a, T& b)
    00232 {
    00233   T t = a;
    00234   a = b;
    00235   b = t;
    00236 }
    00237
    00238 #endif /* MATH_FUNC_HPP */