# 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 inline T max(const T a, const T b)
    00039 {
    00040   return (a >= b) ? a : b;
    00041 }
    00042
    00053 template <typename T>
    00054 static inline T min(const T a, const T b)
    00055 {
    00056   return (a < b) ? a : b;
    00057 }
    00058
    00068 static inline int min(const int a, const int b)
    00069 {
    00070   return min<int>(a, b);
    00071 }
    00072
    00082 static inline uint minu(const uint a, const uint b)
    00083 {
    00084   return min<uint>(a, b);
    00085 }
    00086
    00094 template <typename T>
    00095 static inline T abs(const T a)
    00096 {
    00097   return (a < (T)0) ? -a : a;
    00098 }
    00099
    00108 template <typename T>
    00109 static inline 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 inline 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 inline 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 inline int Clamp(const int a, const int min, const int max)
    00176 {
    00177   return Clamp<int>(a, min, max);
    00178 }
    00179
    00196 static inline uint ClampU(const uint a, const uint min, const uint max)
    00197 {
    00198   return Clamp<uint>(a, min, max);
    00199 }
    00200
    00215 static inline int32 ClampToI32(const int64 a)
    00216 {
    00217   return (int32)Clamp<int64>(a, INT32_MIN, INT32_MAX);
    00218 }
    00219
    00227 static inline uint16 ClampToU16(const uint64 a)
    00228 {
    00229   /* MSVC thinks, in its 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 its
    00232    * infinite loads of warnings. */
    00233   return (uint16)min<uint64>(a, (uint64)UINT16_MAX);
    00234 }
    00235
    00243 template <typename T>
    00244 static inline 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 inline 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 inline 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 inline void Swap(T &a, T &b)
    00290 {
    00291   T t = a;
    00292   a = b;
    00293   b = t;
    00294 }
    00295
    00301 static inline uint ToPercent8(uint i)
    00302 {
    00303   assert(i < 256);
    00304   return i * 101 >> 8;
    00305 }
    00306
    00312 static inline 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 inline uint CeilDiv(uint a, uint b)
    00329 {
    00330   return (a + b - 1) / b;
    00331 }
    00332
    00339 static inline uint Ceil(uint a, uint b)
    00340 {
    00341   return CeilDiv(a, b) * b;
    00342 }
    00343
    00350 static inline int RoundDivSU(int a, uint b)
    00351 {
    00352   if (a > 0) {
    00353     /* 0.5 is rounded to 1 */
    00354     return (a + (int)b / 2) / (int)b;
    00355   } else {
    00356     /* -0.5 is rounded to 0 */
    00357     return (a - ((int)b - 1) / 2) / (int)b;
    00358   }
    00359 }
    00360
    00361 uint32 IntSqrt(uint32 num);
    00362
    00363 #endif /* MATH_FUNC_HPP */