OpenTTD
math_func.hpp
Go to the documentation of this file.
1 /* $Id: math_func.hpp 26651 2014-06-17 19:01:45Z rubidium $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #ifndef MATH_FUNC_HPP
13 #define MATH_FUNC_HPP
14 
25 template <typename T>
26 static inline T max(const T a, const T b)
27 {
28  return (a >= b) ? a : b;
29 }
30 
41 template <typename T>
42 static inline T min(const T a, const T b)
43 {
44  return (a < b) ? a : b;
45 }
46 
56 static inline int min(const int a, const int b)
57 {
58  return min<int>(a, b);
59 }
60 
70 static inline uint minu(const uint a, const uint b)
71 {
72  return min<uint>(a, b);
73 }
74 
82 template <typename T>
83 static inline T abs(const T a)
84 {
85  return (a < (T)0) ? -a : a;
86 }
87 
96 template <typename T>
97 static inline T Align(const T x, uint n)
98 {
99  assert((n & (n - 1)) == 0 && n != 0);
100  n--;
101  return (T)((x + n) & ~((T)n));
102 }
103 
114 template <typename T>
115 static inline T *AlignPtr(T *x, uint n)
116 {
117  assert_compile(sizeof(size_t) == sizeof(void *));
118  return (T *)Align((size_t)x, n);
119 }
120 
138 template <typename T>
139 static inline T Clamp(const T a, const T min, const T max)
140 {
141  assert(min <= max);
142  if (a <= min) return min;
143  if (a >= max) return max;
144  return a;
145 }
146 
163 static inline int Clamp(const int a, const int min, const int max)
164 {
165  return Clamp<int>(a, min, max);
166 }
167 
184 static inline uint ClampU(const uint a, const uint min, const uint max)
185 {
186  return Clamp<uint>(a, min, max);
187 }
188 
203 static inline int32 ClampToI32(const int64 a)
204 {
205  return (int32)Clamp<int64>(a, INT32_MIN, INT32_MAX);
206 }
207 
215 static inline uint16 ClampToU16(const uint64 a)
216 {
217  /* MSVC thinks, in its infinite wisdom, that int min(int, int) is a better
218  * match for min(uint64, uint) than uint64 min(uint64, uint64). As such we
219  * need to cast the UINT16_MAX to prevent MSVC from displaying its
220  * infinite loads of warnings. */
221  return (uint16)min<uint64>(a, (uint64)UINT16_MAX);
222 }
223 
231 template <typename T>
232 static inline T Delta(const T a, const T b)
233 {
234  return (a < b) ? b - a : a - b;
235 }
236 
249 template <typename T>
250 static inline bool IsInsideBS(const T x, const uint base, const uint size)
251 {
252  return (uint)(x - base) < size;
253 }
254 
265 template <typename T>
266 static inline bool IsInsideMM(const T x, const uint min, const uint max)
267 {
268  return (uint)(x - min) < (max - min);
269 }
270 
276 template <typename T>
277 static inline void Swap(T &a, T &b)
278 {
279  T t = a;
280  a = b;
281  b = t;
282 }
283 
289 static inline uint ToPercent8(uint i)
290 {
291  assert(i < 256);
292  return i * 101 >> 8;
293 }
294 
300 static inline uint ToPercent16(uint i)
301 {
302  assert(i < 65536);
303  return i * 101 >> 16;
304 }
305 
306 int LeastCommonMultiple(int a, int b);
307 int GreatestCommonDivisor(int a, int b);
308 int DivideApprox(int a, int b);
309 
316 static inline uint CeilDiv(uint a, uint b)
317 {
318  return (a + b - 1) / b;
319 }
320 
327 static inline uint Ceil(uint a, uint b)
328 {
329  return CeilDiv(a, b) * b;
330 }
331 
338 static inline int RoundDivSU(int a, uint b)
339 {
340  if (a > 0) {
341  /* 0.5 is rounded to 1 */
342  return (a + (int)b / 2) / (int)b;
343  } else {
344  /* -0.5 is rounded to 0 */
345  return (a - ((int)b - 1) / 2) / (int)b;
346  }
347 }
348 
349 uint32 IntSqrt(uint32 num);
350 
351 #endif /* MATH_FUNC_HPP */
static void Swap(T &a, T &b)
Type safe swap operation.
Definition: math_func.hpp:277
static uint minu(const uint a, const uint b)
Returns the minimum of two unsigned integers.
Definition: math_func.hpp:70
static bool IsInsideMM(const T x, const uint min, const uint max)
Checks if a value is in an interval.
Definition: math_func.hpp:266
int DivideApprox(int a, int b)
Deterministic approximate division.
Definition: math_func.cpp:59
static bool IsInsideBS(const T x, const uint base, const uint size)
Checks if a value is between a window started at some base point.
Definition: math_func.hpp:250
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
static int RoundDivSU(int a, uint b)
Computes round(a / b) for signed a and unsigned b.
Definition: math_func.hpp:338
static uint ClampU(const uint a, const uint min, const uint max)
Clamp an unsigned integer between an interval.
Definition: math_func.hpp:184
uint32 IntSqrt(uint32 num)
Compute the integer square root.
Definition: math_func.cpp:79
static T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
Definition: math_func.hpp:97
static uint CeilDiv(uint a, uint b)
Computes ceil(a / b) for non-negative a and b.
Definition: math_func.hpp:316
static T * AlignPtr(T *x, uint n)
Return the smallest multiple of n equal or greater than x Applies to pointers only.
Definition: math_func.hpp:115
int LeastCommonMultiple(int a, int b)
Compute least common multiple (lcm) of arguments a and b, the smallest integer value that is a multip...
Definition: math_func.cpp:26
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:139
static int32 ClampToI32(const int64 a)
Reduce a signed 64-bit int to a signed 32-bit one.
Definition: math_func.hpp:203
static uint ToPercent8(uint i)
Converts a "fract" value 0..255 to "percent" value 0..100.
Definition: math_func.hpp:289
static T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition: math_func.hpp:83
static uint ToPercent16(uint i)
Converts a "fract" value 0..65535 to "percent" value 0..100.
Definition: math_func.hpp:300
static uint Ceil(uint a, uint b)
Computes ceil(a / b) * b for non-negative a and b.
Definition: math_func.hpp:327
static T Delta(const T a, const T b)
Returns the (absolute) difference between two (scalar) variables.
Definition: math_func.hpp:232
static uint16 ClampToU16(const uint64 a)
Reduce an unsigned 64-bit int to an unsigned 16-bit one.
Definition: math_func.hpp:215
int GreatestCommonDivisor(int a, int b)
Compute greatest common divisor (gcd) of a and b.
Definition: math_func.cpp:41