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/>.
10 /** @file math_func.hpp Integer math functions */
28 * Returns the maximum of two values.
30 * This function returns the greater value of two given values.
31 * If they are equal the value of a is returned.
33 * @param a The first value
34 * @param b The second value
35 * @return The greater value or a if equals
38 static inline T
max(const T a
, const T b
)
40 return (a
>= b
) ? a
: b
;
44 * Returns the minimum of two values.
46 * This function returns the smaller value of two given values.
47 * If they are equal the value of b is returned.
49 * @param a The first value
50 * @param b The second value
51 * @return The smaller value or b if equals
54 static inline T
min(const T a
, const T b
)
56 return (a
< b
) ? a
: b
;
60 * Returns the minimum of two integer.
62 * This function returns the smaller value of two given integers.
64 * @param a The first integer
65 * @param b The second integer
66 * @return The smaller value
68 static inline int min(const int a
, const int b
)
70 return min
<int>(a
, b
);
74 * Returns the minimum of two unsigned integers.
76 * This function returns the smaller value of two given unsigned integers.
78 * @param a The first unsigned integer
79 * @param b The second unsigned integer
80 * @return The smaller value
82 static inline uint
minu(const uint a
, const uint b
)
84 return min
<uint
>(a
, b
);
88 * Returns the absolute value of (scalar) variable.
90 * @note assumes variable to be signed
91 * @param a The value we want to unsign
92 * @return The unsigned value
95 static inline T
abs(const T a
)
97 return (a
< (T
)0) ? -a
: a
;
101 * Return the smallest multiple of n equal or greater than x
103 * @note n must be a power of 2
104 * @param x The min value
105 * @param n The base of the number we are searching
106 * @return The smallest multiple of n equal or greater than x
108 template <typename T
>
109 static inline T
Align(const T x
, uint n
)
111 assert((n
& (n
- 1)) == 0 && n
!= 0);
113 return (T
)((x
+ n
) & ~((T
)n
));
117 * Return the smallest multiple of n equal or greater than x
118 * Applies to pointers only
120 * @note n must be a power of 2
121 * @param x The min value
122 * @param n The base of the number we are searching
123 * @return The smallest multiple of n equal or greater than x
126 template <typename T
>
127 static inline T
*AlignPtr(T
*x
, uint n
)
129 assert_compile(sizeof(size_t) == sizeof(void *));
130 return (T
*)Align((size_t)x
, n
);
134 * Clamp a value between an interval.
136 * This function returns a value which is between the given interval of
137 * min and max. If the given value is in this interval the value itself
138 * is returned otherwise the border of the interval is returned, according
139 * which side of the interval was 'left'.
141 * @note The min value must be less or equal of max or you get some
142 * unexpected results.
143 * @param a The value to clamp/truncate.
144 * @param min The minimum of the interval.
145 * @param max the maximum of the interval.
146 * @returns A value between min and max which is closest to a.
147 * @see ClampU(uint, uint, uint)
148 * @see Clamp(int, int, int)
150 template <typename T
>
151 static inline T
Clamp(const T a
, const T min
, const T max
)
154 if (a
<= min
) return min
;
155 if (a
>= max
) return max
;
160 * Clamp an integer between an interval.
162 * This function returns a value which is between the given interval of
163 * min and max. If the given value is in this interval the value itself
164 * is returned otherwise the border of the interval is returned, according
165 * which side of the interval was 'left'.
167 * @note The min value must be less or equal of max or you get some
168 * unexpected results.
169 * @param a The value to clamp/truncate.
170 * @param min The minimum of the interval.
171 * @param max the maximum of the interval.
172 * @returns A value between min and max which is closest to a.
173 * @see ClampU(uint, uint, uint)
175 static inline int Clamp(const int a
, const int min
, const int max
)
177 return Clamp
<int>(a
, min
, max
);
181 * Clamp an unsigned integer between an interval.
183 * This function returns a value which is between the given interval of
184 * min and max. If the given value is in this interval the value itself
185 * is returned otherwise the border of the interval is returned, according
186 * which side of the interval was 'left'.
188 * @note The min value must be less or equal of max or you get some
189 * unexpected results.
190 * @param a The value to clamp/truncate.
191 * @param min The minimum of the interval.
192 * @param max the maximum of the interval.
193 * @returns A value between min and max which is closest to a.
194 * @see Clamp(int, int, int)
196 static inline uint
ClampU(const uint a
, const uint min
, const uint max
)
198 return Clamp
<uint
>(a
, min
, max
);
202 * Reduce a signed 64-bit int to a signed 32-bit one
204 * This function clamps a 64-bit integer to a 32-bit integer.
205 * If the 64-bit value is smaller than the smallest 32-bit integer
206 * value 0x80000000 this value is returned (the left one bit is the sign bit).
207 * If the 64-bit value is greater than the greatest 32-bit integer value 0x7FFFFFFF
208 * this value is returned. In all other cases the 64-bit value 'fits' in a
209 * 32-bits integer field and so the value is casted to int32 and returned.
211 * @param a The 64-bit value to clamps
212 * @return The 64-bit value reduced to a 32-bit value
213 * @see Clamp(int, int, int)
215 static inline int32
ClampToI32(const int64 a
)
217 return (int32
)Clamp
<int64
>(a
, INT32_MIN
, INT32_MAX
);
221 * Reduce an unsigned 64-bit int to an unsigned 16-bit one
223 * @param a The 64-bit value to clamp
224 * @return The 64-bit value reduced to a 16-bit value
225 * @see ClampU(uint, uint, uint)
227 static inline uint16
ClampToU16(const uint64 a
)
229 /* MSVC thinks, in its infinite wisdom, that int min(int, int) is a better
230 * match for min(uint64, uint) than uint64 min(uint64, uint64). As such we
231 * need to cast the UINT16_MAX to prevent MSVC from displaying its
232 * infinite loads of warnings. */
233 return (uint16
)min
<uint64
>(a
, (uint64
)UINT16_MAX
);
237 * Returns the (absolute) difference between two (scalar) variables
239 * @param a The first scalar
240 * @param b The second scalar
241 * @return The absolute difference between the given scalars
243 template <typename T
>
244 static inline T
Delta(const T a
, const T b
)
246 return (a
< b
) ? b
- a
: a
- b
;
250 * Checks if a value is between a window started at some base point.
252 * This function checks if the value x is between the value of base
253 * and base+size. If x equals base this returns true. If x equals
254 * base+size this returns false.
256 * @param x The value to check
257 * @param base The base value of the interval
258 * @param size The size of the interval
259 * @return True if the value is in the interval, false else.
261 template <typename T
>
262 static inline bool IsInsideBS(const T x
, const uint base
, const uint size
)
264 return (uint
)(x
- base
) < size
;
268 * Checks if a value is in an interval.
270 * Returns true if a value is in the interval of [min, max).
272 * @param x The value to check
273 * @param min The minimum of the interval
274 * @param max The maximum of the interval
277 template <typename T
>
278 static inline bool IsInsideMM(const T x
, const uint min
, const uint max
)
280 return (uint
)(x
- min
) < (max
- min
);
284 * Type safe swap operation
285 * @param a variable to swap with b
286 * @param b variable to swap with a
288 template <typename T
>
289 static inline void Swap(T
&a
, T
&b
)
297 * Converts a "fract" value 0..255 to "percent" value 0..100
298 * @param i value to convert, range 0..255
299 * @return value in range 0..100
301 static inline uint
ToPercent8(uint i
)
308 * Converts a "fract" value 0..65535 to "percent" value 0..100
309 * @param i value to convert, range 0..65535
310 * @return value in range 0..100
312 static inline uint
ToPercent16(uint i
)
315 return i
* 101 >> 16;
318 int LeastCommonMultiple(int a
, int b
);
319 int GreatestCommonDivisor(int a
, int b
);
320 int DivideApprox(int a
, int b
);
323 * Computes ceil(a / b) for non-negative a and b.
325 * @param b Denominator
326 * @return Quotient, rounded up
328 static inline uint
CeilDiv(uint a
, uint b
)
330 return (a
+ b
- 1) / b
;
334 * Computes ceil(a / b) * b for non-negative a and b.
336 * @param b Denominator
337 * @return a rounded up to the nearest multiple of b.
339 static inline uint
Ceil(uint a
, uint b
)
341 return CeilDiv(a
, b
) * b
;
345 * Computes round(a / b) for signed a and unsigned b.
347 * @param b Denominator
348 * @return Quotient, rounded to nearest
350 static inline int RoundDivSU(int a
, uint b
)
353 /* 0.5 is rounded to 1 */
354 return (a
+ (int)b
/ 2) / (int)b
;
356 /* -0.5 is rounded to 0 */
357 return (a
- ((int)b
- 1) / 2) / (int)b
;
361 uint32
IntSqrt(uint32 num
);
363 #endif /* MATH_FUNC_HPP */