# math_func.hpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    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   /* MSVC thinks, in it's infinite wisdom, that int min(int, int) is a better
    00230    * match for min(uint64, uint) than uint64 min(uint64, uint64). As such we
    00231    * need to cast the UINT16_MAX to prevent MSVC from displaying it's
    00232    * infinite with loads of warnings. */
    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
    00321 #endif /* MATH_FUNC_HPP */