OpenTTD
math_func.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #ifndef MATH_FUNC_HPP
11 #define MATH_FUNC_HPP
12 
23 template <typename T>
24 static inline T max(const T a, const T b)
25 {
26  return (a >= b) ? a : b;
27 }
28 
39 template <typename T>
40 static inline T min(const T a, const T b)
41 {
42  return (a < b) ? a : b;
43 }
44 
54 static inline int min(const int a, const int b)
55 {
56  return min<int>(a, b);
57 }
58 
68 static inline uint minu(const uint a, const uint b)
69 {
70  return min<uint>(a, b);
71 }
72 
80 template <typename T>
81 static inline T abs(const T a)
82 {
83  return (a < (T)0) ? -a : a;
84 }
85 
94 template <typename T>
95 static inline T Align(const T x, uint n)
96 {
97  assert((n & (n - 1)) == 0 && n != 0);
98  n--;
99  return (T)((x + n) & ~((T)n));
100 }
101 
112 template <typename T>
113 static inline T *AlignPtr(T *x, uint n)
114 {
115  assert_compile(sizeof(size_t) == sizeof(void *));
116  return reinterpret_cast<T *>(Align((size_t)x, n));
117 }
118 
136 template <typename T>
137 static inline T Clamp(const T a, const T min, const T max)
138 {
139  assert(min <= max);
140  if (a <= min) return min;
141  if (a >= max) return max;
142  return a;
143 }
144 
161 static inline int Clamp(const int a, const int min, const int max)
162 {
163  return Clamp<int>(a, min, max);
164 }
165 
182 static inline uint ClampU(const uint a, const uint min, const uint max)
183 {
184  return Clamp<uint>(a, min, max);
185 }
186 
201 static inline int32 ClampToI32(const int64 a)
202 {
203  return static_cast<int32>(Clamp<int64>(a, INT32_MIN, INT32_MAX));
204 }
205 
213 static inline uint16 ClampToU16(const uint64 a)
214 {
215  /* MSVC thinks, in its infinite wisdom, that int min(int, int) is a better
216  * match for min(uint64, uint) than uint64 min(uint64, uint64). As such we
217  * need to cast the UINT16_MAX to prevent MSVC from displaying its
218  * infinite loads of warnings. */
219  return static_cast<uint16>(min<uint64>(a, static_cast<uint64>(UINT16_MAX)));
220 }
221 
229 template <typename T>
230 static inline T Delta(const T a, const T b)
231 {
232  return (a < b) ? b - a : a - b;
233 }
234 
247 template <typename T>
248 static inline bool IsInsideBS(const T x, const size_t base, const size_t size)
249 {
250  return (size_t)(x - base) < size;
251 }
252 
263 template <typename T>
264 static inline bool IsInsideMM(const T x, const size_t min, const size_t max)
265 {
266  return (size_t)(x - min) < (max - min);
267 }
268 
274 template <typename T>
275 static inline void Swap(T &a, T &b)
276 {
277  T t = a;
278  a = b;
279  b = t;
280 }
281 
287 static inline uint ToPercent8(uint i)
288 {
289  assert(i < 256);
290  return i * 101 >> 8;
291 }
292 
298 static inline uint ToPercent16(uint i)
299 {
300  assert(i < 65536);
301  return i * 101 >> 16;
302 }
303 
304 int LeastCommonMultiple(int a, int b);
305 int GreatestCommonDivisor(int a, int b);
306 int DivideApprox(int a, int b);
307 
314 static inline uint CeilDiv(uint a, uint b)
315 {
316  return (a + b - 1) / b;
317 }
318 
325 static inline uint Ceil(uint a, uint b)
326 {
327  return CeilDiv(a, b) * b;
328 }
329 
336 static inline int RoundDivSU(int a, uint b)
337 {
338  if (a > 0) {
339  /* 0.5 is rounded to 1 */
340  return (a + static_cast<int>(b) / 2) / static_cast<int>(b);
341  } else {
342  /* -0.5 is rounded to 0 */
343  return (a - (static_cast<int>(b) - 1) / 2) / static_cast<int>(b);
344  }
345 }
346 
353 static inline int DivAwayFromZero(int a, uint b)
354 {
355  const int _b = static_cast<int>(b);
356  if (a > 0) {
357  return (a + _b - 1) / _b;
358  } else {
359  /* Note: Behaviour of negative numerator division is truncation toward zero. */
360  return (a - _b + 1) / _b;
361  }
362 }
363 
364 uint32 IntSqrt(uint32 num);
365 
366 #endif /* MATH_FUNC_HPP */
static void Swap(T &a, T &b)
Type safe swap operation.
Definition: math_func.hpp:275
static uint minu(const uint a, const uint b)
Returns the minimum of two unsigned integers.
Definition: math_func.hpp:68
int DivideApprox(int a, int b)
Deterministic approximate division.
Definition: math_func.cpp:57
static int DivAwayFromZero(int a, uint b)
Computes (a / b) rounded away from zero.
Definition: math_func.hpp:353
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:24
static int RoundDivSU(int a, uint b)
Computes round(a / b) for signed a and unsigned b.
Definition: math_func.hpp:336
static bool IsInsideBS(const T x, const size_t base, const size_t size)
Checks if a value is between a window started at some base point.
Definition: math_func.hpp:248
static uint ClampU(const uint a, const uint min, const uint max)
Clamp an unsigned integer between an interval.
Definition: math_func.hpp:182
static bool IsInsideMM(const T x, const size_t min, const size_t max)
Checks if a value is in an interval.
Definition: math_func.hpp:264
uint32 IntSqrt(uint32 num)
Compute the integer square root.
Definition: math_func.cpp:77
static T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
Definition: math_func.hpp:95
static uint CeilDiv(uint a, uint b)
Computes ceil(a / b) for non-negative a and b.
Definition: math_func.hpp:314
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:113
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:24
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:40
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:137
static int32 ClampToI32(const int64 a)
Reduce a signed 64-bit int to a signed 32-bit one.
Definition: math_func.hpp:201
static uint ToPercent8(uint i)
Converts a "fract" value 0..255 to "percent" value 0..100.
Definition: math_func.hpp:287
static T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition: math_func.hpp:81
static uint ToPercent16(uint i)
Converts a "fract" value 0..65535 to "percent" value 0..100.
Definition: math_func.hpp:298
static uint Ceil(uint a, uint b)
Computes ceil(a / b) * b for non-negative a and b.
Definition: math_func.hpp:325
static T Delta(const T a, const T b)
Returns the (absolute) difference between two (scalar) variables.
Definition: math_func.hpp:230
static uint16 ClampToU16(const uint64 a)
Reduce an unsigned 64-bit int to an unsigned 16-bit one.
Definition: math_func.hpp:213
int GreatestCommonDivisor(int a, int b)
Compute greatest common divisor (gcd) of a and b.
Definition: math_func.cpp:39