mirror of https://github.com/OpenTTD/OpenTTD
(svn r11369) -Codechange [FS#1390]: changes some int to int8 in macros.h, as they describe a bit-position, which fits perfectly in an int8 (max 64) (skidd13)
-Codechange [FS#1390]: added consts in macros.h functions, so compilers can optimise better (skidd13) -Codechange [FS#1390]: remove HAS_SINGLE_BIT, as COUNTBITS does the same (skidd13)release/0.6
parent
8932887524
commit
71ba45cdfb
56
src/macros.h
56
src/macros.h
|
@ -76,8 +76,7 @@
|
||||||
* @param b The second value
|
* @param b The second value
|
||||||
* @return The greater value or a if equals
|
* @return The greater value or a if equals
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
template<typename T> static inline T max(const T a, const T b)
|
||||||
static inline T max(T a, T b)
|
|
||||||
{
|
{
|
||||||
return a >= b ? a : b;
|
return a >= b ? a : b;
|
||||||
}
|
}
|
||||||
|
@ -92,8 +91,7 @@ static inline T max(T a, T b)
|
||||||
* @param b The second value
|
* @param b The second value
|
||||||
* @return The smaller value or b if equals
|
* @return The smaller value or b if equals
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
template<typename T> static inline T min(const T a, const T b)
|
||||||
static inline T min(T a, T b)
|
|
||||||
{
|
{
|
||||||
return a < b ? a : b;
|
return a < b ? a : b;
|
||||||
}
|
}
|
||||||
|
@ -107,7 +105,11 @@ static inline T min(T a, T b)
|
||||||
* @param b The second integer
|
* @param b The second integer
|
||||||
* @return The smaller value
|
* @return The smaller value
|
||||||
*/
|
*/
|
||||||
static inline int min(int a, int b) { if (a <= b) return a; return b; }
|
static inline int min(const int a, const int b)
|
||||||
|
{
|
||||||
|
if (a <= b) return a;
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the minimum of two unsigned integers.
|
* Returns the minimum of two unsigned integers.
|
||||||
|
@ -118,7 +120,11 @@ static inline int min(int a, int b) { if (a <= b) return a; return b; }
|
||||||
* @param b The second unsigned integer
|
* @param b The second unsigned integer
|
||||||
* @return The smaller value
|
* @return The smaller value
|
||||||
*/
|
*/
|
||||||
static inline uint minu(uint a, uint b) { if (a <= b) return a; return b; }
|
static inline uint minu(const uint a, const uint b)
|
||||||
|
{
|
||||||
|
if (a <= b) return a;
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clamp an integer between an interval.
|
* Clamp an integer between an interval.
|
||||||
|
@ -136,7 +142,7 @@ static inline uint minu(uint a, uint b) { if (a <= b) return a; return b; }
|
||||||
* @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)
|
||||||
*/
|
*/
|
||||||
static inline int clamp(int a, int min, int max)
|
static inline int clamp(const int a, const int min, const int max)
|
||||||
{
|
{
|
||||||
if (a <= min) return min;
|
if (a <= min) return min;
|
||||||
if (a >= max) return max;
|
if (a >= max) return max;
|
||||||
|
@ -159,7 +165,7 @@ static inline int clamp(int a, int min, 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)
|
||||||
*/
|
*/
|
||||||
static inline uint clampu(uint a, uint min, uint max)
|
static inline uint clampu(const uint a, const uint min, const uint max)
|
||||||
{
|
{
|
||||||
if (a <= min) return min;
|
if (a <= min) return min;
|
||||||
if (a >= max) return max;
|
if (a >= max) return max;
|
||||||
|
@ -180,7 +186,7 @@ static inline uint clampu(uint a, uint min, uint max)
|
||||||
* @return The 64-bit value reduced to a 32-bit value
|
* @return The 64-bit value reduced to a 32-bit value
|
||||||
* @see clamp(int, int, int)
|
* @see clamp(int, int, int)
|
||||||
*/
|
*/
|
||||||
static inline int32 ClampToI32(int64 a)
|
static inline int32 ClampToI32(const int64 a)
|
||||||
{
|
{
|
||||||
if (a <= (int32)0x80000000) return 0x80000000;
|
if (a <= (int32)0x80000000) return 0x80000000;
|
||||||
if (a >= (int32)0x7FFFFFFF) return 0x7FFFFFFF;
|
if (a >= (int32)0x7FFFFFFF) return 0x7FFFFFFF;
|
||||||
|
@ -198,7 +204,7 @@ static inline int32 ClampToI32(int64 a)
|
||||||
* @param shift The amount to shift the value to right.
|
* @param shift The amount to shift the value to right.
|
||||||
* @return The shifted result
|
* @return The shifted result
|
||||||
*/
|
*/
|
||||||
static inline int32 BIGMULSS(int32 a, int32 b, int shift)
|
static inline int32 BIGMULSS(const int32 a, const int32 b, const int8 shift)
|
||||||
{
|
{
|
||||||
return (int32)((int64)a * (int64)b >> shift);
|
return (int32)((int64)a * (int64)b >> shift);
|
||||||
}
|
}
|
||||||
|
@ -214,7 +220,7 @@ static inline int32 BIGMULSS(int32 a, int32 b, int shift)
|
||||||
* @param shift The amount to shift the value to right.
|
* @param shift The amount to shift the value to right.
|
||||||
* @return The shifted result
|
* @return The shifted result
|
||||||
*/
|
*/
|
||||||
static inline uint32 BIGMULUS(uint32 a, uint32 b, int shift)
|
static inline uint32 BIGMULUS(const uint32 a, const uint32 b, const int8 shift)
|
||||||
{
|
{
|
||||||
return (uint32)((uint64)a * (uint64)b >> shift);
|
return (uint32)((uint64)a * (uint64)b >> shift);
|
||||||
}
|
}
|
||||||
|
@ -247,9 +253,9 @@ static inline uint32 BIGMULUS(uint32 a, uint32 b, int shift)
|
||||||
* @param y The position of the bit to check, started from the LSB
|
* @param y The position of the bit to check, started from the LSB
|
||||||
* @return True if the bit is set, false else.
|
* @return True if the bit is set, false else.
|
||||||
*/
|
*/
|
||||||
template<typename T> static inline bool HASBIT(T x, int y)
|
template<typename T> static inline bool HASBIT(const T x, const int8 y)
|
||||||
{
|
{
|
||||||
return (x & ((T)1 << y)) != 0;
|
return (x & ((T)1U << y)) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -263,9 +269,9 @@ template<typename T> static inline bool HASBIT(T x, int y)
|
||||||
* @param y The bit position to set
|
* @param y The bit position to set
|
||||||
* @return The new value of the old value with the bit set
|
* @return The new value of the old value with the bit set
|
||||||
*/
|
*/
|
||||||
template<typename T> static inline T SETBIT(T& x, int y)
|
template<typename T> static inline T SETBIT(T& x, const int8 y)
|
||||||
{
|
{
|
||||||
return x |= (T)1 << y;
|
return x |= (T)1U << y;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -279,9 +285,9 @@ template<typename T> static inline T SETBIT(T& x, int y)
|
||||||
* @param y The bit position to clear
|
* @param y The bit position to clear
|
||||||
* @return The new value of the old value with the bit cleared
|
* @return The new value of the old value with the bit cleared
|
||||||
*/
|
*/
|
||||||
template<typename T> static inline T CLRBIT(T& x, int y)
|
template<typename T> static inline T CLRBIT(T& x, const int8 y)
|
||||||
{
|
{
|
||||||
return x &= ~((T)1 << y);
|
return x &= ~((T)1U << y);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -295,9 +301,9 @@ template<typename T> static inline T CLRBIT(T& x, int y)
|
||||||
* @param y The bit position to toggle
|
* @param y The bit position to toggle
|
||||||
* @return The new value of the old value with the bit toggled
|
* @return The new value of the old value with the bit toggled
|
||||||
*/
|
*/
|
||||||
template<typename T> static inline T TOGGLEBIT(T& x, int y)
|
template<typename T> static inline T TOGGLEBIT(T& x, const int8 y)
|
||||||
{
|
{
|
||||||
return x ^= (T)1 << y;
|
return x ^= (T)1U << y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -437,7 +443,7 @@ static inline int KillFirstBit2x64(int value)
|
||||||
*/
|
*/
|
||||||
template<typename T> static inline uint COUNTBITS(T value)
|
template<typename T> static inline uint COUNTBITS(T value)
|
||||||
{
|
{
|
||||||
uint num;
|
register uint num;
|
||||||
|
|
||||||
/* This loop is only called once for every bit set by clearing the lowest
|
/* This loop is only called once for every bit set by clearing the lowest
|
||||||
* bit in each loop. The number of bits is therefore equal to the number of
|
* bit in each loop. The number of bits is therefore equal to the number of
|
||||||
|
@ -451,16 +457,6 @@ template<typename T> static inline uint COUNTBITS(T value)
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true if value a has only one bit set to 1
|
|
||||||
*
|
|
||||||
* This macro returns true if only one bit is set.
|
|
||||||
*
|
|
||||||
* @param a The value to check
|
|
||||||
* @return True if only one bit is set, false else
|
|
||||||
*/
|
|
||||||
#define HAS_SINGLE_BIT(a) ( ((a) & ((a) - 1)) == 0)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if a byte is in an interval.
|
* Checks if a byte is in an interval.
|
||||||
*
|
*
|
||||||
|
|
|
@ -906,7 +906,7 @@ Foundation GetRoadFoundation(Slope tileh, RoadBits bits)
|
||||||
* created directly, but the state itself is still perfectly drawable.
|
* created directly, but the state itself is still perfectly drawable.
|
||||||
* However, as we do not want this to be build directly, we need to check
|
* However, as we do not want this to be build directly, we need to check
|
||||||
* for that situation in here. */
|
* for that situation in here. */
|
||||||
return (tileh != 0 && HAS_SINGLE_BIT(bits)) ? FOUNDATION_LEVELED : FOUNDATION_NONE;
|
return (tileh != 0 && COUNTBITS(bits) == 1) ? FOUNDATION_LEVELED : FOUNDATION_NONE;
|
||||||
}
|
}
|
||||||
if ((~_valid_tileh_slopes_road[1][tileh] & bits) == 0) return FOUNDATION_LEVELED;
|
if ((~_valid_tileh_slopes_road[1][tileh] & bits) == 0) return FOUNDATION_LEVELED;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue