2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2007-12-17 01:35:45 +00:00
|
|
|
/** @file math_func.hpp Integer math functions */
|
2007-11-22 18:01:51 +00:00
|
|
|
|
|
|
|
#ifndef MATH_FUNC_HPP
|
|
|
|
#define MATH_FUNC_HPP
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the absolute value of (scalar) variable.
|
|
|
|
*
|
|
|
|
* @note assumes variable to be signed
|
|
|
|
* @param a The value we want to unsign
|
|
|
|
* @return The unsigned value
|
|
|
|
*/
|
2008-06-22 15:21:51 +00:00
|
|
|
template <typename T>
|
2011-12-20 17:57:56 +00:00
|
|
|
static inline T abs(const T a)
|
2007-11-22 18:01:51 +00:00
|
|
|
{
|
|
|
|
return (a < (T)0) ? -a : a;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the smallest multiple of n equal or greater than x
|
|
|
|
*
|
|
|
|
* @note n must be a power of 2
|
|
|
|
* @param x The min value
|
|
|
|
* @param n The base of the number we are searching
|
|
|
|
* @return The smallest multiple of n equal or greater than x
|
|
|
|
*/
|
2008-06-22 15:21:51 +00:00
|
|
|
template <typename T>
|
2011-12-20 17:57:56 +00:00
|
|
|
static inline T Align(const T x, uint n)
|
2007-11-22 18:01:51 +00:00
|
|
|
{
|
2008-06-26 15:46:19 +00:00
|
|
|
assert((n & (n - 1)) == 0 && n != 0);
|
2007-11-22 18:01:51 +00:00
|
|
|
n--;
|
2008-06-26 15:46:19 +00:00
|
|
|
return (T)((x + n) & ~((T)n));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the smallest multiple of n equal or greater than x
|
|
|
|
* Applies to pointers only
|
|
|
|
*
|
|
|
|
* @note n must be a power of 2
|
|
|
|
* @param x The min value
|
|
|
|
* @param n The base of the number we are searching
|
|
|
|
* @return The smallest multiple of n equal or greater than x
|
|
|
|
* @see Align()
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2011-12-20 17:57:56 +00:00
|
|
|
static inline T *AlignPtr(T *x, uint n)
|
2008-06-26 15:46:19 +00:00
|
|
|
{
|
2020-12-27 10:44:22 +00:00
|
|
|
static_assert(sizeof(size_t) == sizeof(void *));
|
2019-08-06 11:19:19 +00:00
|
|
|
return reinterpret_cast<T *>(Align((size_t)x, n));
|
2007-11-22 18:01:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-12-09 00:41:18 +00:00
|
|
|
* Clamp a value between an interval.
|
2007-11-22 18:01:51 +00:00
|
|
|
*
|
|
|
|
* This function returns a value which is between the given interval of
|
|
|
|
* min and max. If the given value is in this interval the value itself
|
|
|
|
* is returned otherwise the border of the interval is returned, according
|
|
|
|
* which side of the interval was 'left'.
|
|
|
|
*
|
|
|
|
* @note The min value must be less or equal of max or you get some
|
|
|
|
* unexpected results.
|
|
|
|
* @param a The value to clamp/truncate.
|
|
|
|
* @param min The minimum of the interval.
|
|
|
|
* @param max the maximum of the interval.
|
|
|
|
* @returns A value between min and max which is closest to a.
|
|
|
|
* @see ClampU(uint, uint, uint)
|
2009-12-09 00:41:18 +00:00
|
|
|
* @see Clamp(int, int, int)
|
2007-11-22 18:01:51 +00:00
|
|
|
*/
|
2009-12-09 00:41:18 +00:00
|
|
|
template <typename T>
|
2011-12-20 17:57:56 +00:00
|
|
|
static inline T Clamp(const T a, const T min, const T max)
|
2007-11-22 18:01:51 +00:00
|
|
|
{
|
2009-12-15 01:38:55 +00:00
|
|
|
assert(min <= max);
|
2007-11-22 18:01:51 +00:00
|
|
|
if (a <= min) return min;
|
|
|
|
if (a >= max) return max;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2009-12-09 00:41:18 +00:00
|
|
|
/**
|
|
|
|
* Clamp an integer between an interval.
|
|
|
|
*
|
|
|
|
* This function returns a value which is between the given interval of
|
|
|
|
* min and max. If the given value is in this interval the value itself
|
|
|
|
* is returned otherwise the border of the interval is returned, according
|
|
|
|
* which side of the interval was 'left'.
|
|
|
|
*
|
|
|
|
* @note The min value must be less or equal of max or you get some
|
|
|
|
* unexpected results.
|
|
|
|
* @param a The value to clamp/truncate.
|
|
|
|
* @param min The minimum of the interval.
|
|
|
|
* @param max the maximum of the interval.
|
|
|
|
* @returns A value between min and max which is closest to a.
|
|
|
|
* @see ClampU(uint, uint, uint)
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
static inline int Clamp(const int a, const int min, const int max)
|
2009-12-09 00:41:18 +00:00
|
|
|
{
|
|
|
|
return Clamp<int>(a, min, max);
|
|
|
|
}
|
|
|
|
|
2007-11-22 18:01:51 +00:00
|
|
|
/**
|
|
|
|
* Clamp an unsigned integer between an interval.
|
|
|
|
*
|
|
|
|
* This function returns a value which is between the given interval of
|
|
|
|
* min and max. If the given value is in this interval the value itself
|
|
|
|
* is returned otherwise the border of the interval is returned, according
|
|
|
|
* which side of the interval was 'left'.
|
|
|
|
*
|
|
|
|
* @note The min value must be less or equal of max or you get some
|
|
|
|
* unexpected results.
|
|
|
|
* @param a The value to clamp/truncate.
|
|
|
|
* @param min The minimum of the interval.
|
|
|
|
* @param max the maximum of the interval.
|
|
|
|
* @returns A value between min and max which is closest to a.
|
|
|
|
* @see Clamp(int, int, int)
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
static inline uint ClampU(const uint a, const uint min, const uint max)
|
2007-11-22 18:01:51 +00:00
|
|
|
{
|
2009-12-09 00:41:18 +00:00
|
|
|
return Clamp<uint>(a, min, max);
|
2007-11-22 18:01:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reduce a signed 64-bit int to a signed 32-bit one
|
|
|
|
*
|
|
|
|
* This function clamps a 64-bit integer to a 32-bit integer.
|
|
|
|
* If the 64-bit value is smaller than the smallest 32-bit integer
|
|
|
|
* value 0x80000000 this value is returned (the left one bit is the sign bit).
|
|
|
|
* If the 64-bit value is greater than the greatest 32-bit integer value 0x7FFFFFFF
|
|
|
|
* this value is returned. In all other cases the 64-bit value 'fits' in a
|
|
|
|
* 32-bits integer field and so the value is casted to int32 and returned.
|
|
|
|
*
|
|
|
|
* @param a The 64-bit value to clamps
|
|
|
|
* @return The 64-bit value reduced to a 32-bit value
|
|
|
|
* @see Clamp(int, int, int)
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
static inline int32 ClampToI32(const int64 a)
|
2007-11-22 18:01:51 +00:00
|
|
|
{
|
2019-08-06 11:19:19 +00:00
|
|
|
return static_cast<int32>(Clamp<int64>(a, INT32_MIN, INT32_MAX));
|
2007-11-22 18:01:51 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 14:30:53 +00:00
|
|
|
/**
|
2008-03-27 13:59:35 +00:00
|
|
|
* Reduce an unsigned 64-bit int to an unsigned 16-bit one
|
2008-02-20 14:30:53 +00:00
|
|
|
*
|
|
|
|
* @param a The 64-bit value to clamp
|
|
|
|
* @return The 64-bit value reduced to a 16-bit value
|
|
|
|
* @see ClampU(uint, uint, uint)
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
static inline uint16 ClampToU16(const uint64 a)
|
2008-02-20 14:30:53 +00:00
|
|
|
{
|
2010-04-12 14:12:47 +00:00
|
|
|
/* MSVC thinks, in its infinite wisdom, that int min(int, int) is a better
|
2009-12-09 09:28:47 +00:00
|
|
|
* match for min(uint64, uint) than uint64 min(uint64, uint64). As such we
|
2010-04-12 14:12:47 +00:00
|
|
|
* need to cast the UINT16_MAX to prevent MSVC from displaying its
|
|
|
|
* infinite loads of warnings. */
|
2021-01-08 10:16:18 +00:00
|
|
|
return static_cast<uint16>(std::min(a, static_cast<uint64>(UINT16_MAX)));
|
2008-02-20 14:30:53 +00:00
|
|
|
}
|
|
|
|
|
2007-11-22 18:01:51 +00:00
|
|
|
/**
|
|
|
|
* Returns the (absolute) difference between two (scalar) variables
|
|
|
|
*
|
|
|
|
* @param a The first scalar
|
|
|
|
* @param b The second scalar
|
|
|
|
* @return The absolute difference between the given scalars
|
|
|
|
*/
|
2008-06-22 15:21:51 +00:00
|
|
|
template <typename T>
|
2011-12-20 17:57:56 +00:00
|
|
|
static inline T Delta(const T a, const T b)
|
2008-06-22 15:41:38 +00:00
|
|
|
{
|
2007-11-22 18:01:51 +00:00
|
|
|
return (a < b) ? b - a : a - b;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a value is between a window started at some base point.
|
|
|
|
*
|
|
|
|
* This function checks if the value x is between the value of base
|
|
|
|
* and base+size. If x equals base this returns true. If x equals
|
|
|
|
* base+size this returns false.
|
|
|
|
*
|
|
|
|
* @param x The value to check
|
|
|
|
* @param base The base value of the interval
|
|
|
|
* @param size The size of the interval
|
|
|
|
* @return True if the value is in the interval, false else.
|
|
|
|
*/
|
2008-06-22 15:21:51 +00:00
|
|
|
template <typename T>
|
2019-03-27 23:09:33 +00:00
|
|
|
static inline bool IsInsideBS(const T x, const size_t base, const size_t size)
|
2007-11-22 18:01:51 +00:00
|
|
|
{
|
2019-03-27 23:09:33 +00:00
|
|
|
return (size_t)(x - base) < size;
|
2007-11-22 18:01:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-11-24 10:38:43 +00:00
|
|
|
* Checks if a value is in an interval.
|
2007-11-22 18:01:51 +00:00
|
|
|
*
|
2007-11-24 10:38:43 +00:00
|
|
|
* Returns true if a value is in the interval of [min, max).
|
2007-11-22 18:01:51 +00:00
|
|
|
*
|
2009-09-19 09:51:14 +00:00
|
|
|
* @param x The value to check
|
2007-11-22 18:01:51 +00:00
|
|
|
* @param min The minimum of the interval
|
|
|
|
* @param max The maximum of the interval
|
2007-11-24 10:38:43 +00:00
|
|
|
* @see IsInsideBS()
|
2007-11-22 18:01:51 +00:00
|
|
|
*/
|
2008-06-22 15:21:51 +00:00
|
|
|
template <typename T>
|
2021-11-14 15:39:17 +00:00
|
|
|
static constexpr inline bool IsInsideMM(const T x, const size_t min, const size_t max) noexcept
|
2007-11-22 18:01:51 +00:00
|
|
|
{
|
2019-03-27 23:09:33 +00:00
|
|
|
return (size_t)(x - min) < (max - min);
|
2007-11-22 18:01:51 +00:00
|
|
|
}
|
|
|
|
|
2007-12-25 09:48:53 +00:00
|
|
|
/**
|
|
|
|
* Type safe swap operation
|
|
|
|
* @param a variable to swap with b
|
|
|
|
* @param b variable to swap with a
|
|
|
|
*/
|
2008-06-22 15:21:51 +00:00
|
|
|
template <typename T>
|
2011-12-20 17:57:56 +00:00
|
|
|
static inline void Swap(T &a, T &b)
|
2007-12-25 09:48:53 +00:00
|
|
|
{
|
|
|
|
T t = a;
|
|
|
|
a = b;
|
|
|
|
b = t;
|
|
|
|
}
|
|
|
|
|
2009-08-27 13:31:26 +00:00
|
|
|
/**
|
|
|
|
* Converts a "fract" value 0..255 to "percent" value 0..100
|
|
|
|
* @param i value to convert, range 0..255
|
|
|
|
* @return value in range 0..100
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
static inline uint ToPercent8(uint i)
|
2009-08-27 13:31:26 +00:00
|
|
|
{
|
|
|
|
assert(i < 256);
|
|
|
|
return i * 101 >> 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a "fract" value 0..65535 to "percent" value 0..100
|
|
|
|
* @param i value to convert, range 0..65535
|
|
|
|
* @return value in range 0..100
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
static inline uint ToPercent16(uint i)
|
2009-08-27 13:31:26 +00:00
|
|
|
{
|
|
|
|
assert(i < 65536);
|
|
|
|
return i * 101 >> 16;
|
|
|
|
}
|
|
|
|
|
2009-03-21 01:34:31 +00:00
|
|
|
int LeastCommonMultiple(int a, int b);
|
|
|
|
int GreatestCommonDivisor(int a, int b);
|
2013-06-09 12:50:33 +00:00
|
|
|
int DivideApprox(int a, int b);
|
2009-03-21 01:34:31 +00:00
|
|
|
|
2010-04-18 14:56:05 +00:00
|
|
|
/**
|
|
|
|
* Computes ceil(a / b) for non-negative a and b.
|
|
|
|
* @param a Numerator
|
|
|
|
* @param b Denominator
|
|
|
|
* @return Quotient, rounded up
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
static inline uint CeilDiv(uint a, uint b)
|
2010-04-18 14:56:05 +00:00
|
|
|
{
|
|
|
|
return (a + b - 1) / b;
|
|
|
|
}
|
|
|
|
|
2011-12-09 22:07:00 +00:00
|
|
|
/**
|
|
|
|
* Computes ceil(a / b) * b for non-negative a and b.
|
|
|
|
* @param a Numerator
|
|
|
|
* @param b Denominator
|
|
|
|
* @return a rounded up to the nearest multiple of b.
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
static inline uint Ceil(uint a, uint b)
|
2011-12-09 22:07:00 +00:00
|
|
|
{
|
|
|
|
return CeilDiv(a, b) * b;
|
|
|
|
}
|
|
|
|
|
2010-04-18 14:56:05 +00:00
|
|
|
/**
|
2010-04-18 17:13:01 +00:00
|
|
|
* Computes round(a / b) for signed a and unsigned b.
|
2010-04-18 14:56:05 +00:00
|
|
|
* @param a Numerator
|
|
|
|
* @param b Denominator
|
|
|
|
* @return Quotient, rounded to nearest
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
static inline int RoundDivSU(int a, uint b)
|
2010-04-18 14:56:05 +00:00
|
|
|
{
|
2010-04-18 17:13:01 +00:00
|
|
|
if (a > 0) {
|
|
|
|
/* 0.5 is rounded to 1 */
|
2019-08-06 11:19:19 +00:00
|
|
|
return (a + static_cast<int>(b) / 2) / static_cast<int>(b);
|
2010-04-18 17:13:01 +00:00
|
|
|
} else {
|
|
|
|
/* -0.5 is rounded to 0 */
|
2019-08-06 11:19:19 +00:00
|
|
|
return (a - (static_cast<int>(b) - 1) / 2) / static_cast<int>(b);
|
2010-04-18 17:13:01 +00:00
|
|
|
}
|
2010-04-18 14:56:05 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 08:28:43 +00:00
|
|
|
/**
|
|
|
|
* Computes (a / b) rounded away from zero.
|
|
|
|
* @param a Numerator
|
|
|
|
* @param b Denominator
|
|
|
|
* @return Quotient, rounded away from zero
|
|
|
|
*/
|
|
|
|
static inline int DivAwayFromZero(int a, uint b)
|
|
|
|
{
|
|
|
|
const int _b = static_cast<int>(b);
|
|
|
|
if (a > 0) {
|
|
|
|
return (a + _b - 1) / _b;
|
|
|
|
} else {
|
|
|
|
/* Note: Behaviour of negative numerator division is truncation toward zero. */
|
|
|
|
return (a - _b + 1) / _b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-03 23:40:46 +00:00
|
|
|
uint32 IntSqrt(uint32 num);
|
|
|
|
|
2007-11-22 18:01:51 +00:00
|
|
|
#endif /* MATH_FUNC_HPP */
|