Changeset - r28525:8f27064cc980
[Not reviewed]
master
0 1 0
Rubidium - 11 months ago 2024-01-20 15:11:43
rubidium@openttd.org
Codechange: add constexpr to math functions where applicable
1 file changed with 17 insertions and 17 deletions:
0 comments (0 inline, 0 general)
src/core/math_func.hpp
Show inline comments
 
@@ -17,13 +17,13 @@
 
 *
 
 * @note assumes variable to be signed
 
 * @param a The value we want to unsign
 
 * @return The unsigned value
 
 */
 
template <typename T>
 
inline T abs(const T a)
 
constexpr T abs(const T a)
 
{
 
	return (a < (T)0) ? -a : a;
 
}
 

	
 
/**
 
 * Return the smallest multiple of n equal or greater than x
 
@@ -31,13 +31,13 @@ inline T abs(const T a)
 
 * @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
 
 */
 
template <typename T>
 
inline T Align(const T x, uint n)
 
constexpr T Align(const T x, uint n)
 
{
 
	assert((n & (n - 1)) == 0 && n != 0);
 
	n--;
 
	return (T)((x + n) & ~((T)n));
 
}
 

	
 
@@ -49,13 +49,13 @@ inline T Align(const T x, uint n)
 
 * @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>
 
inline T *AlignPtr(T *x, uint n)
 
constexpr T *AlignPtr(T *x, uint n)
 
{
 
	static_assert(sizeof(size_t) == sizeof(void *));
 
	return reinterpret_cast<T *>(Align((size_t)x, n));
 
}
 

	
 
/**
 
@@ -73,13 +73,13 @@ inline T *AlignPtr(T *x, uint n)
 
 * @param max the maximum of the interval.
 
 * @returns A value between min and max which is closest to a.
 
 * @see ClampU(uint, uint, uint)
 
 * @see Clamp(int, int, int)
 
 */
 
template <typename T>
 
inline T Clamp(const T a, const T min, const T max)
 
constexpr T Clamp(const T a, const T min, const T max)
 
{
 
	assert(min <= max);
 
	if (a <= min) return min;
 
	if (a >= max) return max;
 
	return a;
 
}
 
@@ -96,13 +96,13 @@ inline T Clamp(const T a, const T min, c
 
 * @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.
 
 */
 
template <typename T>
 
inline T SoftClamp(const T a, const T min, const T max)
 
constexpr T SoftClamp(const T a, const T min, const T max)
 
{
 
	if (min > max) {
 
		using U = std::make_unsigned_t<T>;
 
		return min - (U(min) - max) / 2;
 
	}
 
	if (a <= min) return min;
 
@@ -123,13 +123,13 @@ inline T SoftClamp(const T a, const T mi
 
 * @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)
 
 */
 
inline int Clamp(const int a, const int min, const int max)
 
constexpr int Clamp(const int a, const int min, const int max)
 
{
 
	return Clamp<int>(a, min, max);
 
}
 

	
 
/**
 
 * Clamp an unsigned integer between an interval.
 
@@ -144,13 +144,13 @@ inline int Clamp(const int a, const int 
 
 * @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)
 
 */
 
inline uint ClampU(const uint a, const uint min, const uint max)
 
constexpr uint ClampU(const uint a, const uint min, const uint max)
 
{
 
	return Clamp<uint>(a, min, max);
 
}
 

	
 
/**
 
 * Clamp the given value down to lie within the requested type.
 
@@ -228,13 +228,13 @@ constexpr To ClampTo(From value)
 
 *
 
 * @param a The first scalar
 
 * @param b The second scalar
 
 * @return The absolute difference between the given scalars
 
 */
 
template <typename T>
 
inline T Delta(const T a, const T b)
 
constexpr T Delta(const T a, const T b)
 
{
 
	return (a < b) ? b - a : a - b;
 
}
 

	
 
/**
 
 * Checks if a value is between a window started at some base point.
 
@@ -246,13 +246,13 @@ inline T Delta(const T a, const T b)
 
 * @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.
 
 */
 
template <typename T>
 
inline bool IsInsideBS(const T x, const size_t base, const size_t size)
 
constexpr bool IsInsideBS(const T x, const size_t base, const size_t size)
 
{
 
	return (size_t)(x - base) < size;
 
}
 

	
 
/**
 
 * Checks if a value is in an interval.
 
@@ -262,13 +262,13 @@ inline bool IsInsideBS(const T x, const 
 
 * @param x The value to check
 
 * @param min The minimum of the interval
 
 * @param max The maximum of the interval
 
 * @see IsInsideBS()
 
 */
 
template <typename T, std::enable_if_t<std::disjunction_v<std::is_convertible<T, size_t>, std::is_base_of<StrongTypedefBase, T>>, int> = 0>
 
static constexpr inline bool IsInsideMM(const T x, const size_t min, const size_t max) noexcept
 
constexpr bool IsInsideMM(const T x, const size_t min, const size_t max) noexcept
 
{
 
	if constexpr (std::is_base_of_v<StrongTypedefBase, T>) {
 
		return (size_t)(x.base() - min) < (max - min);
 
	} else {
 
		return (size_t)(x - min) < (max - min);
 
	}
 
@@ -277,36 +277,36 @@ static constexpr inline bool IsInsideMM(
 
/**
 
 * Type safe swap operation
 
 * @param a variable to swap with b
 
 * @param b variable to swap with a
 
 */
 
template <typename T>
 
inline void Swap(T &a, T &b)
 
constexpr void Swap(T &a, T &b)
 
{
 
	T t = a;
 
	a = b;
 
	b = t;
 
}
 

	
 
/**
 
 * 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
 
 */
 
inline uint ToPercent8(uint i)
 
constexpr uint ToPercent8(uint i)
 
{
 
	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
 
 */
 
inline uint ToPercent16(uint i)
 
constexpr uint ToPercent16(uint i)
 
{
 
	assert(i < 65536);
 
	return i * 101 >> 16;
 
}
 

	
 
int DivideApprox(int a, int b);
 
@@ -314,35 +314,35 @@ int DivideApprox(int a, int b);
 
/**
 
 * Computes ceil(a / b) for non-negative a and b.
 
 * @param a Numerator
 
 * @param b Denominator
 
 * @return Quotient, rounded up
 
 */
 
inline uint CeilDiv(uint a, uint b)
 
constexpr uint CeilDiv(uint a, uint b)
 
{
 
	return (a + b - 1) / b;
 
}
 

	
 
/**
 
 * 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.
 
 */
 
inline uint Ceil(uint a, uint b)
 
constexpr uint Ceil(uint a, uint b)
 
{
 
	return CeilDiv(a, b) * b;
 
}
 

	
 
/**
 
 * Computes round(a / b) for signed a and unsigned b.
 
 * @param a Numerator
 
 * @param b Denominator
 
 * @return Quotient, rounded to nearest
 
 */
 
inline int RoundDivSU(int a, uint b)
 
constexpr int RoundDivSU(int a, uint b)
 
{
 
	if (a > 0) {
 
		/* 0.5 is rounded to 1 */
 
		return (a + static_cast<int>(b) / 2) / static_cast<int>(b);
 
	} else {
 
		/* -0.5 is rounded to 0 */
 
@@ -353,13 +353,13 @@ inline int RoundDivSU(int a, uint b)
 
/**
 
 * Computes (a / b) rounded away from zero.
 
 * @param a Numerator
 
 * @param b Denominator
 
 * @return Quotient, rounded away from zero
 
 */
 
inline int DivAwayFromZero(int a, uint b)
 
constexpr 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. */
0 comments (0 inline, 0 general)