mirror of https://github.com/OpenTTD/OpenTTD
(svn r11490) -Codechange: Split the math functions to their own header
-Codechange: Replace the rest of the math macros with functionsrelease/0.6
parent
1d56af1d33
commit
737aec4db6
|
@ -426,6 +426,9 @@
|
||||||
<File
|
<File
|
||||||
RelativePath=".\..\src\console.h">
|
RelativePath=".\..\src\console.h">
|
||||||
</File>
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\..\src\core\math_func.hpp">
|
||||||
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\..\src\core\random_func.hpp">
|
RelativePath=".\..\src\core\random_func.hpp">
|
||||||
</File>
|
</File>
|
||||||
|
|
|
@ -799,6 +799,10 @@
|
||||||
RelativePath=".\..\src\console.h"
|
RelativePath=".\..\src\console.h"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\..\src\core\math_func.hpp"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\..\src\core\random_func.hpp"
|
RelativePath=".\..\src\core\random_func.hpp"
|
||||||
>
|
>
|
||||||
|
|
|
@ -796,6 +796,10 @@
|
||||||
RelativePath=".\..\src\console.h"
|
RelativePath=".\..\src\console.h"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\..\src\core\math_func.hpp"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\..\src\core\random_func.hpp"
|
RelativePath=".\..\src\core\random_func.hpp"
|
||||||
>
|
>
|
||||||
|
|
|
@ -108,6 +108,7 @@ cargopacket.h
|
||||||
cargotype.h
|
cargotype.h
|
||||||
command.h
|
command.h
|
||||||
console.h
|
console.h
|
||||||
|
core/math_func.hpp
|
||||||
core/random_func.hpp
|
core/random_func.hpp
|
||||||
currency.h
|
currency.h
|
||||||
date.h
|
date.h
|
||||||
|
|
|
@ -0,0 +1,229 @@
|
||||||
|
/* $Id */
|
||||||
|
|
||||||
|
/** @file math_func.hpp */
|
||||||
|
|
||||||
|
#ifndef MATH_FUNC_HPP
|
||||||
|
#define MATH_FUNC_HPP
|
||||||
|
|
||||||
|
#ifdef min
|
||||||
|
#undef min
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef max
|
||||||
|
#undef max
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef abs
|
||||||
|
#undef abs
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the maximum of two values.
|
||||||
|
*
|
||||||
|
* This function returns the greater value of two given values.
|
||||||
|
* If they are equal the value of a is returned.
|
||||||
|
*
|
||||||
|
* @param a The first value
|
||||||
|
* @param b The second value
|
||||||
|
* @return The greater value or a if equals
|
||||||
|
*/
|
||||||
|
template<typename T> static inline T max(const T a, const T b)
|
||||||
|
{
|
||||||
|
return (a >= b) ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the minimum of two values.
|
||||||
|
*
|
||||||
|
* This function returns the smaller value of two given values.
|
||||||
|
* If they are equal the value of b is returned.
|
||||||
|
*
|
||||||
|
* @param a The first value
|
||||||
|
* @param b The second value
|
||||||
|
* @return The smaller value or b if equals
|
||||||
|
*/
|
||||||
|
template<typename T> static inline T min(const T a, const T b)
|
||||||
|
{
|
||||||
|
return (a < b) ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the minimum of two integer.
|
||||||
|
*
|
||||||
|
* This function returns the smaller value of two given integers.
|
||||||
|
*
|
||||||
|
* @param a The first integer
|
||||||
|
* @param b The second integer
|
||||||
|
* @return The smaller value
|
||||||
|
*/
|
||||||
|
static inline int min(const int a, const int b)
|
||||||
|
{
|
||||||
|
return (a < b) ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the minimum of two unsigned integers.
|
||||||
|
*
|
||||||
|
* This function returns the smaller value of two given unsigned integers.
|
||||||
|
*
|
||||||
|
* @param a The first unsigned integer
|
||||||
|
* @param b The second unsigned integer
|
||||||
|
* @return The smaller value
|
||||||
|
*/
|
||||||
|
static inline uint minu(const uint a, const uint b)
|
||||||
|
{
|
||||||
|
return (a < b) ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
template <typename T> static inline T abs(T a)
|
||||||
|
{
|
||||||
|
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
|
||||||
|
*/
|
||||||
|
template<typename T> static inline T Align(const T x, uint n)
|
||||||
|
{
|
||||||
|
n--;
|
||||||
|
return (T)((x + n) & ~(n));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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)
|
||||||
|
*/
|
||||||
|
static inline int Clamp(const int a, const int min, const int max)
|
||||||
|
{
|
||||||
|
if (a <= min) return min;
|
||||||
|
if (a >= max) return max;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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)
|
||||||
|
*/
|
||||||
|
static inline uint ClampU(const uint a, const uint min, const uint max)
|
||||||
|
{
|
||||||
|
if (a <= min) return min;
|
||||||
|
if (a >= max) return max;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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)
|
||||||
|
*/
|
||||||
|
static inline int32 ClampToI32(const int64 a)
|
||||||
|
{
|
||||||
|
if (a <= (int32)0x80000000) return 0x80000000;
|
||||||
|
if (a >= (int32)0x7FFFFFFF) return 0x7FFFFFFF;
|
||||||
|
return (int32)a;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
template <typename T> static inline 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.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
template<typename T> static inline bool IS_INSIDE_1D(const T x, const int base, const uint size)
|
||||||
|
{
|
||||||
|
return (uint)(x - base) < size;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a byte is in an interval.
|
||||||
|
*
|
||||||
|
* Returns true if a byte value is in the interval of [min, max).
|
||||||
|
*
|
||||||
|
* @param a The byte value to check
|
||||||
|
* @param min The minimum of the interval
|
||||||
|
* @param max The maximum of the interval
|
||||||
|
* @see IS_INSIDE_1D
|
||||||
|
*/
|
||||||
|
template<typename T> static inline bool IS_BYTE_INSIDE(const T x, const byte min, const byte max)
|
||||||
|
{
|
||||||
|
return (byte)(x - min) < (max - min);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if an int is in an interval.
|
||||||
|
*
|
||||||
|
* Returns true if a integer value is in the interval of [min, max).
|
||||||
|
*
|
||||||
|
* @param a The integer value to check
|
||||||
|
* @param min The minimum of the interval
|
||||||
|
* @param max The maximum of the interval
|
||||||
|
* @see IS_INSIDE_1D
|
||||||
|
*/
|
||||||
|
template<typename T> static inline bool IS_INT_INSIDE(const T x, const int min, const uint max)
|
||||||
|
{
|
||||||
|
return (uint)(x - min) < (max - min);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* MATH_FUNC_HPP */
|
|
@ -39,9 +39,6 @@ template<typename T> void Swap(T& a, T& b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** returns the (absolute) difference between two (scalar) variables */
|
|
||||||
template <typename T> static inline T delta(T a, T b) { return a < b ? b - a : a - b; }
|
|
||||||
|
|
||||||
/** Some enums need to have allowed incrementing (i.e. StationClassID) */
|
/** Some enums need to have allowed incrementing (i.e. StationClassID) */
|
||||||
#define DECLARE_POSTFIX_INCREMENT(type) \
|
#define DECLARE_POSTFIX_INCREMENT(type) \
|
||||||
FORCEINLINE type operator ++(type& e, int) \
|
FORCEINLINE type operator ++(type& e, int) \
|
||||||
|
|
205
src/macros.h
205
src/macros.h
|
@ -5,6 +5,8 @@
|
||||||
#ifndef MACROS_H
|
#ifndef MACROS_H
|
||||||
#define MACROS_H
|
#define MACROS_H
|
||||||
|
|
||||||
|
#include "core/math_func.hpp"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch n bits from x, started at bit s.
|
* Fetch n bits from x, started at bit s.
|
||||||
*
|
*
|
||||||
|
@ -71,155 +73,6 @@ template<typename T, typename U> static inline T AB(T& x, const uint8 s, const u
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef min
|
|
||||||
#undef min
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef max
|
|
||||||
#undef max
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef abs
|
|
||||||
#undef abs
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the maximum of two values.
|
|
||||||
*
|
|
||||||
* This function returns the greater value of two given values.
|
|
||||||
* If they are equal the value of a is returned.
|
|
||||||
*
|
|
||||||
* @param a The first value
|
|
||||||
* @param b The second value
|
|
||||||
* @return The greater value or a if equals
|
|
||||||
*/
|
|
||||||
template<typename T> static inline T max(const T a, const T b)
|
|
||||||
{
|
|
||||||
return a >= b ? a : b;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the minimum of two values.
|
|
||||||
*
|
|
||||||
* This function returns the smaller value of two given values.
|
|
||||||
* If they are equal the value of b is returned.
|
|
||||||
*
|
|
||||||
* @param a The first value
|
|
||||||
* @param b The second value
|
|
||||||
* @return The smaller value or b if equals
|
|
||||||
*/
|
|
||||||
template<typename T> static inline T min(const T a, const T b)
|
|
||||||
{
|
|
||||||
return a < b ? a : b;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the minimum of two integer.
|
|
||||||
*
|
|
||||||
* This function returns the smaller value of two given integers.
|
|
||||||
*
|
|
||||||
* @param a The first integer
|
|
||||||
* @param b The second integer
|
|
||||||
* @return The smaller value
|
|
||||||
*/
|
|
||||||
static inline int min(const int a, const int b)
|
|
||||||
{
|
|
||||||
return a < b ? a : b;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the minimum of two unsigned integers.
|
|
||||||
*
|
|
||||||
* This function returns the smaller value of two given unsigned integers.
|
|
||||||
*
|
|
||||||
* @param a The first unsigned integer
|
|
||||||
* @param b The second unsigned integer
|
|
||||||
* @return The smaller value
|
|
||||||
*/
|
|
||||||
static inline uint minu(const uint a, const uint b)
|
|
||||||
{
|
|
||||||
return a < b ? a : b;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
|
||||||
*/
|
|
||||||
template <typename T> static inline T abs(T a)
|
|
||||||
{
|
|
||||||
return (a < (T)0) ? -a : a;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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)
|
|
||||||
*/
|
|
||||||
static inline int Clamp(const int a, const int min, const int max)
|
|
||||||
{
|
|
||||||
if (a <= min) return min;
|
|
||||||
if (a >= max) return max;
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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)
|
|
||||||
*/
|
|
||||||
static inline uint ClampU(const uint a, const uint min, const uint max)
|
|
||||||
{
|
|
||||||
if (a <= min) return min;
|
|
||||||
if (a >= max) return max;
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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)
|
|
||||||
*/
|
|
||||||
static inline int32 ClampToI32(const int64 a)
|
|
||||||
{
|
|
||||||
if (a <= (int32)0x80000000) return 0x80000000;
|
|
||||||
if (a >= (int32)0x7FFFFFFF) return 0x7FFFFFFF;
|
|
||||||
return (int32)a;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if a bit in a value is set.
|
* Checks if a bit in a value is set.
|
||||||
*
|
*
|
||||||
|
@ -408,47 +261,6 @@ template<typename T> static inline uint CountBits(T value)
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
template<typename T> static inline bool IS_INSIDE_1D(const T x, const int base, const uint size)
|
|
||||||
{
|
|
||||||
return (uint)(x - base) < size;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if a byte is in an interval.
|
|
||||||
*
|
|
||||||
* This macro returns true if a byte value is in the interval of [min, max).
|
|
||||||
*
|
|
||||||
* @param a The byte value to check
|
|
||||||
* @param min The minimum of the interval
|
|
||||||
* @param max The maximum of the interval
|
|
||||||
* @see IS_INSIDE_1D
|
|
||||||
*/
|
|
||||||
#define IS_BYTE_INSIDE(a, min, max) ((byte)((a) - (min)) < (byte)((max) - (min)))
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if an int is in an interval.
|
|
||||||
*
|
|
||||||
* This macro returns true if a integer value is in the interval of [min, max).
|
|
||||||
*
|
|
||||||
* @param a The integer value to check
|
|
||||||
* @param min The minimum of the interval
|
|
||||||
* @param max The maximum of the interval
|
|
||||||
* @see IS_INSIDE_1D
|
|
||||||
*/
|
|
||||||
#define IS_INT_INSIDE(a, min, max) ((uint)((a) - (min)) < (uint)((max) - (min)))
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Flips a coin with a given probability.
|
* Flips a coin with a given probability.
|
||||||
*
|
*
|
||||||
|
@ -545,19 +357,6 @@ template<typename T> static inline T ROR(const T x, const uint8 n)
|
||||||
return (T)(x >> n | x << (sizeof(x) * 8 - n));
|
return (T)(x >> n | x << (sizeof(x) * 8 - n));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
|
||||||
*/
|
|
||||||
template<typename T> static inline T Align(const T x, uint n) {
|
|
||||||
n--;
|
|
||||||
return (T)((x + n) & ~(n));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** return the largest value that can be entered in a variable.
|
/** return the largest value that can be entered in a variable.
|
||||||
*/
|
*/
|
||||||
#define MAX_UVALUE(type) ((type)~(type)0)
|
#define MAX_UVALUE(type) ((type)~(type)0)
|
||||||
|
|
Loading…
Reference in New Issue