mirror of https://github.com/OpenTTD/OpenTTD
Codechange: add constexpr to math functions where applicable
parent
dfe70181f1
commit
086cbd0d72
|
@ -20,7 +20,7 @@
|
||||||
* @return The unsigned value
|
* @return The unsigned value
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline T abs(const T a)
|
constexpr T abs(const T a)
|
||||||
{
|
{
|
||||||
return (a < (T)0) ? -a : a;
|
return (a < (T)0) ? -a : a;
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ inline T abs(const T a)
|
||||||
* @return The smallest multiple of n equal or greater than x
|
* @return The smallest multiple of n equal or greater than x
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
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);
|
assert((n & (n - 1)) == 0 && n != 0);
|
||||||
n--;
|
n--;
|
||||||
|
@ -52,7 +52,7 @@ inline T Align(const T x, uint n)
|
||||||
* @see Align()
|
* @see Align()
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline T *AlignPtr(T *x, uint n)
|
constexpr T *AlignPtr(T *x, uint n)
|
||||||
{
|
{
|
||||||
static_assert(sizeof(size_t) == sizeof(void *));
|
static_assert(sizeof(size_t) == sizeof(void *));
|
||||||
return reinterpret_cast<T *>(Align((size_t)x, n));
|
return reinterpret_cast<T *>(Align((size_t)x, n));
|
||||||
|
@ -76,7 +76,7 @@ inline T *AlignPtr(T *x, uint n)
|
||||||
* @see Clamp(int, int, int)
|
* @see Clamp(int, int, int)
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
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);
|
assert(min <= max);
|
||||||
if (a <= min) return min;
|
if (a <= min) return min;
|
||||||
|
@ -99,7 +99,7 @@ inline T Clamp(const T a, const T min, const T max)
|
||||||
* @returns A value between min and max which is closest to a.
|
* @returns A value between min and max which is closest to a.
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
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) {
|
if (min > max) {
|
||||||
using U = std::make_unsigned_t<T>;
|
using U = std::make_unsigned_t<T>;
|
||||||
|
@ -126,7 +126,7 @@ inline T SoftClamp(const T a, const T min, const T max)
|
||||||
* @returns A value between min and max which is closest to a.
|
* @returns A value between min and max which is closest to a.
|
||||||
* @see ClampU(uint, uint, uint)
|
* @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);
|
return Clamp<int>(a, min, max);
|
||||||
}
|
}
|
||||||
|
@ -147,7 +147,7 @@ inline int Clamp(const int a, const int min, const int max)
|
||||||
* @returns A value between min and max which is closest to a.
|
* @returns A value between min and max which is closest to a.
|
||||||
* @see Clamp(int, int, int)
|
* @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);
|
return Clamp<uint>(a, min, max);
|
||||||
}
|
}
|
||||||
|
@ -231,7 +231,7 @@ constexpr To ClampTo(From value)
|
||||||
* @return The absolute difference between the given scalars
|
* @return The absolute difference between the given scalars
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
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;
|
return (a < b) ? b - a : a - b;
|
||||||
}
|
}
|
||||||
|
@ -249,7 +249,7 @@ inline T Delta(const T a, const T b)
|
||||||
* @return True if the value is in the interval, false else.
|
* @return True if the value is in the interval, false else.
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
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;
|
return (size_t)(x - base) < size;
|
||||||
}
|
}
|
||||||
|
@ -265,7 +265,7 @@ inline bool IsInsideBS(const T x, const size_t base, const size_t size)
|
||||||
* @see IsInsideBS()
|
* @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>
|
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>) {
|
if constexpr (std::is_base_of_v<StrongTypedefBase, T>) {
|
||||||
return (size_t)(x.base() - min) < (max - min);
|
return (size_t)(x.base() - min) < (max - min);
|
||||||
|
@ -280,7 +280,7 @@ static constexpr inline bool IsInsideMM(const T x, const size_t min, const size_
|
||||||
* @param b variable to swap with a
|
* @param b variable to swap with a
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline void Swap(T &a, T &b)
|
constexpr void Swap(T &a, T &b)
|
||||||
{
|
{
|
||||||
T t = a;
|
T t = a;
|
||||||
a = b;
|
a = b;
|
||||||
|
@ -292,7 +292,7 @@ inline void Swap(T &a, T &b)
|
||||||
* @param i value to convert, range 0..255
|
* @param i value to convert, range 0..255
|
||||||
* @return value in range 0..100
|
* @return value in range 0..100
|
||||||
*/
|
*/
|
||||||
inline uint ToPercent8(uint i)
|
constexpr uint ToPercent8(uint i)
|
||||||
{
|
{
|
||||||
assert(i < 256);
|
assert(i < 256);
|
||||||
return i * 101 >> 8;
|
return i * 101 >> 8;
|
||||||
|
@ -303,7 +303,7 @@ inline uint ToPercent8(uint i)
|
||||||
* @param i value to convert, range 0..65535
|
* @param i value to convert, range 0..65535
|
||||||
* @return value in range 0..100
|
* @return value in range 0..100
|
||||||
*/
|
*/
|
||||||
inline uint ToPercent16(uint i)
|
constexpr uint ToPercent16(uint i)
|
||||||
{
|
{
|
||||||
assert(i < 65536);
|
assert(i < 65536);
|
||||||
return i * 101 >> 16;
|
return i * 101 >> 16;
|
||||||
|
@ -317,7 +317,7 @@ int DivideApprox(int a, int b);
|
||||||
* @param b Denominator
|
* @param b Denominator
|
||||||
* @return Quotient, rounded up
|
* @return Quotient, rounded up
|
||||||
*/
|
*/
|
||||||
inline uint CeilDiv(uint a, uint b)
|
constexpr uint CeilDiv(uint a, uint b)
|
||||||
{
|
{
|
||||||
return (a + b - 1) / b;
|
return (a + b - 1) / b;
|
||||||
}
|
}
|
||||||
|
@ -328,7 +328,7 @@ inline uint CeilDiv(uint a, uint b)
|
||||||
* @param b Denominator
|
* @param b Denominator
|
||||||
* @return a rounded up to the nearest multiple of b.
|
* @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;
|
return CeilDiv(a, b) * b;
|
||||||
}
|
}
|
||||||
|
@ -339,7 +339,7 @@ inline uint Ceil(uint a, uint b)
|
||||||
* @param b Denominator
|
* @param b Denominator
|
||||||
* @return Quotient, rounded to nearest
|
* @return Quotient, rounded to nearest
|
||||||
*/
|
*/
|
||||||
inline int RoundDivSU(int a, uint b)
|
constexpr int RoundDivSU(int a, uint b)
|
||||||
{
|
{
|
||||||
if (a > 0) {
|
if (a > 0) {
|
||||||
/* 0.5 is rounded to 1 */
|
/* 0.5 is rounded to 1 */
|
||||||
|
@ -356,7 +356,7 @@ inline int RoundDivSU(int a, uint b)
|
||||||
* @param b Denominator
|
* @param b Denominator
|
||||||
* @return Quotient, rounded away from zero
|
* @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);
|
const int _b = static_cast<int>(b);
|
||||||
if (a > 0) {
|
if (a > 0) {
|
||||||
|
|
Loading…
Reference in New Issue