Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #ifndef MATH_FUNC_HPP
00013 #define MATH_FUNC_HPP
00014
00015 #ifdef min
00016 #undef min
00017 #endif
00018
00019 #ifdef max
00020 #undef max
00021 #endif
00022
00023 #ifdef abs
00024 #undef abs
00025 #endif
00026
00037 template <typename T>
00038 static FORCEINLINE T max(const T a, const T b)
00039 {
00040 return (a >= b) ? a : b;
00041 }
00042
00053 template <typename T>
00054 static FORCEINLINE T min(const T a, const T b)
00055 {
00056 return (a < b) ? a : b;
00057 }
00058
00068 static FORCEINLINE int min(const int a, const int b)
00069 {
00070 return min<int>(a, b);
00071 }
00072
00082 static FORCEINLINE uint minu(const uint a, const uint b)
00083 {
00084 return min<uint>(a, b);
00085 }
00086
00094 template <typename T>
00095 static FORCEINLINE T abs(const T a)
00096 {
00097 return (a < (T)0) ? -a : a;
00098 }
00099
00108 template <typename T>
00109 static FORCEINLINE T Align(const T x, uint n)
00110 {
00111 assert((n & (n - 1)) == 0 && n != 0);
00112 n--;
00113 return (T)((x + n) & ~((T)n));
00114 }
00115
00126 template <typename T>
00127 static FORCEINLINE T *AlignPtr(T *x, uint n)
00128 {
00129 assert_compile(sizeof(size_t) == sizeof(void *));
00130 return (T *)Align((size_t)x, n);
00131 }
00132
00150 template <typename T>
00151 static FORCEINLINE T Clamp(const T a, const T min, const T max)
00152 {
00153 assert(min <= max);
00154 if (a <= min) return min;
00155 if (a >= max) return max;
00156 return a;
00157 }
00158
00175 static FORCEINLINE int Clamp(const int a, const int min, const int max)
00176 {
00177 return Clamp<int>(a, min, max);
00178 }
00179
00196 static FORCEINLINE uint ClampU(const uint a, const uint min, const uint max)
00197 {
00198 return Clamp<uint>(a, min, max);
00199 }
00200
00215 static FORCEINLINE int32 ClampToI32(const int64 a)
00216 {
00217 return (int32)Clamp<int64>(a, INT32_MIN, INT32_MAX);
00218 }
00219
00227 static FORCEINLINE uint16 ClampToU16(const uint64 a)
00228 {
00229
00230
00231
00232
00233 return (uint16)min<uint64>(a, (uint64)UINT16_MAX);
00234 }
00235
00243 template <typename T>
00244 static FORCEINLINE T Delta(const T a, const T b)
00245 {
00246 return (a < b) ? b - a : a - b;
00247 }
00248
00261 template <typename T>
00262 static FORCEINLINE bool IsInsideBS(const T x, const uint base, const uint size)
00263 {
00264 return (uint)(x - base) < size;
00265 }
00266
00277 template <typename T>
00278 static FORCEINLINE bool IsInsideMM(const T x, const uint min, const uint max)
00279 {
00280 return (uint)(x - min) < (max - min);
00281 }
00282
00288 template <typename T>
00289 static FORCEINLINE void Swap(T &a, T &b)
00290 {
00291 T t = a;
00292 a = b;
00293 b = t;
00294 }
00295
00301 static FORCEINLINE uint ToPercent8(uint i)
00302 {
00303 assert(i < 256);
00304 return i * 101 >> 8;
00305 }
00306
00312 static FORCEINLINE uint ToPercent16(uint i)
00313 {
00314 assert(i < 65536);
00315 return i * 101 >> 16;
00316 }
00317
00318 int LeastCommonMultiple(int a, int b);
00319 int GreatestCommonDivisor(int a, int b);
00320 int DivideApprox(int a, int b);
00321
00328 static FORCEINLINE uint CeilDiv(uint a, uint b)
00329 {
00330 return (a + b - 1) / b;
00331 }
00332
00339 static FORCEINLINE uint Ceil(uint a, uint b)
00340 {
00341 return CeilDiv(a, b) * b;
00342 }
00343
00350 static FORCEINLINE int RoundDivSU(int a, uint b)
00351 {
00352 if (a > 0) {
00353
00354 return (a + (int)b / 2) / (int)b;
00355 } else {
00356
00357 return (a - ((int)b - 1) / 2) / (int)b;
00358 }
00359 }
00360
00361 uint32 IntSqrt(uint32 num);
00362
00363 #endif