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
8 /** @file math_func.hpp Integer math functions */
9
10 #ifndef MATH_FUNC_HPP
11 #define MATH_FUNC_HPP
12
13 /**
14 * Returns the absolute value of (scalar) variable.
15 *
16 * @note assumes variable to be signed
17 * @param a The value we want to unsign
18 * @return The unsigned value
19 */
20 template <typename T>
abs(const T a)21 static inline T abs(const T a)
22 {
23 return (a < (T)0) ? -a : a;
24 }
25
26 /**
27 * Return the smallest multiple of n equal or greater than x
28 *
29 * @note n must be a power of 2
30 * @param x The min value
31 * @param n The base of the number we are searching
32 * @return The smallest multiple of n equal or greater than x
33 */
34 template <typename T>
Align(const T x,uint n)35 static inline T Align(const T x, uint n)
36 {
37 assert((n & (n - 1)) == 0 && n != 0);
38 n--;
39 return (T)((x + n) & ~((T)n));
40 }
41
42 /**
43 * Return the smallest multiple of n equal or greater than x
44 * Applies to pointers only
45 *
46 * @note n must be a power of 2
47 * @param x The min value
48 * @param n The base of the number we are searching
49 * @return The smallest multiple of n equal or greater than x
50 * @see Align()
51 */
52 template <typename T>
AlignPtr(T * x,uint n)53 static inline T *AlignPtr(T *x, uint n)
54 {
55 static_assert(sizeof(size_t) == sizeof(void *));
56 return reinterpret_cast<T *>(Align((size_t)x, n));
57 }
58
59 /**
60 * Clamp a value between an interval.
61 *
62 * This function returns a value which is between the given interval of
63 * min and max. If the given value is in this interval the value itself
64 * is returned otherwise the border of the interval is returned, according
65 * which side of the interval was 'left'.
66 *
67 * @note The min value must be less or equal of max or you get some
68 * unexpected results.
69 * @param a The value to clamp/truncate.
70 * @param min The minimum of the interval.
71 * @param max the maximum of the interval.
72 * @returns A value between min and max which is closest to a.
73 * @see ClampU(uint, uint, uint)
74 * @see Clamp(int, int, int)
75 */
76 template <typename T>
Clamp(const T a,const T min,const T max)77 static inline T Clamp(const T a, const T min, const T max)
78 {
79 assert(min <= max);
80 if (a <= min) return min;
81 if (a >= max) return max;
82 return a;
83 }
84
85 /**
86 * Clamp an integer between an interval.
87 *
88 * This function returns a value which is between the given interval of
89 * min and max. If the given value is in this interval the value itself
90 * is returned otherwise the border of the interval is returned, according
91 * which side of the interval was 'left'.
92 *
93 * @note The min value must be less or equal of max or you get some
94 * unexpected results.
95 * @param a The value to clamp/truncate.
96 * @param min The minimum of the interval.
97 * @param max the maximum of the interval.
98 * @returns A value between min and max which is closest to a.
99 * @see ClampU(uint, uint, uint)
100 */
Clamp(const int a,const int min,const int max)101 static inline int Clamp(const int a, const int min, const int max)
102 {
103 return Clamp<int>(a, min, max);
104 }
105
106 /**
107 * Clamp an unsigned integer between an interval.
108 *
109 * This function returns a value which is between the given interval of
110 * min and max. If the given value is in this interval the value itself
111 * is returned otherwise the border of the interval is returned, according
112 * which side of the interval was 'left'.
113 *
114 * @note The min value must be less or equal of max or you get some
115 * unexpected results.
116 * @param a The value to clamp/truncate.
117 * @param min The minimum of the interval.
118 * @param max the maximum of the interval.
119 * @returns A value between min and max which is closest to a.
120 * @see Clamp(int, int, int)
121 */
ClampU(const uint a,const uint min,const uint max)122 static inline uint ClampU(const uint a, const uint min, const uint max)
123 {
124 return Clamp<uint>(a, min, max);
125 }
126
127 /**
128 * Reduce a signed 64-bit int to a signed 32-bit one
129 *
130 * This function clamps a 64-bit integer to a 32-bit integer.
131 * If the 64-bit value is smaller than the smallest 32-bit integer
132 * value 0x80000000 this value is returned (the left one bit is the sign bit).
133 * If the 64-bit value is greater than the greatest 32-bit integer value 0x7FFFFFFF
134 * this value is returned. In all other cases the 64-bit value 'fits' in a
135 * 32-bits integer field and so the value is casted to int32 and returned.
136 *
137 * @param a The 64-bit value to clamps
138 * @return The 64-bit value reduced to a 32-bit value
139 * @see Clamp(int, int, int)
140 */
ClampToI32(const int64 a)141 static inline int32 ClampToI32(const int64 a)
142 {
143 return static_cast<int32>(Clamp<int64>(a, INT32_MIN, INT32_MAX));
144 }
145
146 /**
147 * Reduce an unsigned 64-bit int to an unsigned 16-bit one
148 *
149 * @param a The 64-bit value to clamp
150 * @return The 64-bit value reduced to a 16-bit value
151 * @see ClampU(uint, uint, uint)
152 */
ClampToU16(const uint64 a)153 static inline uint16 ClampToU16(const uint64 a)
154 {
155 /* MSVC thinks, in its infinite wisdom, that int min(int, int) is a better
156 * match for min(uint64, uint) than uint64 min(uint64, uint64). As such we
157 * need to cast the UINT16_MAX to prevent MSVC from displaying its
158 * infinite loads of warnings. */
159 return static_cast<uint16>(std::min(a, static_cast<uint64>(UINT16_MAX)));
160 }
161
162 /**
163 * Returns the (absolute) difference between two (scalar) variables
164 *
165 * @param a The first scalar
166 * @param b The second scalar
167 * @return The absolute difference between the given scalars
168 */
169 template <typename T>
Delta(const T a,const T b)170 static inline T Delta(const T a, const T b)
171 {
172 return (a < b) ? b - a : a - b;
173 }
174
175 /**
176 * Checks if a value is between a window started at some base point.
177 *
178 * This function checks if the value x is between the value of base
179 * and base+size. If x equals base this returns true. If x equals
180 * base+size this returns false.
181 *
182 * @param x The value to check
183 * @param base The base value of the interval
184 * @param size The size of the interval
185 * @return True if the value is in the interval, false else.
186 */
187 template <typename T>
IsInsideBS(const T x,const size_t base,const size_t size)188 static inline bool IsInsideBS(const T x, const size_t base, const size_t size)
189 {
190 return (size_t)(x - base) < size;
191 }
192
193 /**
194 * Checks if a value is in an interval.
195 *
196 * Returns true if a value is in the interval of [min, max).
197 *
198 * @param x The value to check
199 * @param min The minimum of the interval
200 * @param max The maximum of the interval
201 * @see IsInsideBS()
202 */
203 template <typename T>
IsInsideMM(const T x,const size_t min,const size_t max)204 static inline bool IsInsideMM(const T x, const size_t min, const size_t max)
205 {
206 return (size_t)(x - min) < (max - min);
207 }
208
209 /**
210 * Type safe swap operation
211 * @param a variable to swap with b
212 * @param b variable to swap with a
213 */
214 template <typename T>
Swap(T & a,T & b)215 static inline void Swap(T &a, T &b)
216 {
217 T t = a;
218 a = b;
219 b = t;
220 }
221
222 /**
223 * Converts a "fract" value 0..255 to "percent" value 0..100
224 * @param i value to convert, range 0..255
225 * @return value in range 0..100
226 */
ToPercent8(uint i)227 static inline uint ToPercent8(uint i)
228 {
229 assert(i < 256);
230 return i * 101 >> 8;
231 }
232
233 /**
234 * Converts a "fract" value 0..65535 to "percent" value 0..100
235 * @param i value to convert, range 0..65535
236 * @return value in range 0..100
237 */
ToPercent16(uint i)238 static inline uint ToPercent16(uint i)
239 {
240 assert(i < 65536);
241 return i * 101 >> 16;
242 }
243
244 int LeastCommonMultiple(int a, int b);
245 int GreatestCommonDivisor(int a, int b);
246 int DivideApprox(int a, int b);
247
248 /**
249 * Computes ceil(a / b) for non-negative a and b.
250 * @param a Numerator
251 * @param b Denominator
252 * @return Quotient, rounded up
253 */
CeilDiv(uint a,uint b)254 static inline uint CeilDiv(uint a, uint b)
255 {
256 return (a + b - 1) / b;
257 }
258
259 /**
260 * Computes ceil(a / b) * b for non-negative a and b.
261 * @param a Numerator
262 * @param b Denominator
263 * @return a rounded up to the nearest multiple of b.
264 */
Ceil(uint a,uint b)265 static inline uint Ceil(uint a, uint b)
266 {
267 return CeilDiv(a, b) * b;
268 }
269
270 /**
271 * Computes round(a / b) for signed a and unsigned b.
272 * @param a Numerator
273 * @param b Denominator
274 * @return Quotient, rounded to nearest
275 */
RoundDivSU(int a,uint b)276 static inline int RoundDivSU(int a, uint b)
277 {
278 if (a > 0) {
279 /* 0.5 is rounded to 1 */
280 return (a + static_cast<int>(b) / 2) / static_cast<int>(b);
281 } else {
282 /* -0.5 is rounded to 0 */
283 return (a - (static_cast<int>(b) - 1) / 2) / static_cast<int>(b);
284 }
285 }
286
287 /**
288 * Computes (a / b) rounded away from zero.
289 * @param a Numerator
290 * @param b Denominator
291 * @return Quotient, rounded away from zero
292 */
DivAwayFromZero(int a,uint b)293 static inline int DivAwayFromZero(int a, uint b)
294 {
295 const int _b = static_cast<int>(b);
296 if (a > 0) {
297 return (a + _b - 1) / _b;
298 } else {
299 /* Note: Behaviour of negative numerator division is truncation toward zero. */
300 return (a - _b + 1) / _b;
301 }
302 }
303
304 uint32 IntSqrt(uint32 num);
305
306 #endif /* MATH_FUNC_HPP */
307