2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#ifndef MACROS_H
|
|
|
|
#define MACROS_H
|
|
|
|
|
2005-01-07 17:02:43 +00:00
|
|
|
#include "map.h"
|
|
|
|
|
2005-11-16 13:11:28 +00:00
|
|
|
/// Fetch n bits starting at bit s from x
|
|
|
|
#define GB(x, s, n) (((x) >> (s)) & ((1U << (n)) - 1))
|
|
|
|
/// Set n bits starting at bit s in x to d
|
|
|
|
#define SB(x, s, n, d) ((x) = ((x) & ~(((1U << (n)) - 1) << (s))) | ((d) << (s)))
|
|
|
|
/// Add i to the n bits starting at bit s in x
|
|
|
|
#define AB(x, s, n, i) ((x) = ((x) & ~(((1U << (n)) - 1) << (s))) | (((x) + ((i) << (s))) & (((1U << (n)) - 1) << (s))))
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
#ifdef min
|
2004-08-09 17:04:08 +00:00
|
|
|
#undef min
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef max
|
|
|
|
#undef max
|
|
|
|
#endif
|
|
|
|
|
2004-11-24 13:19:48 +00:00
|
|
|
static inline int min(int a, int b) { if (a <= b) return a; return b; }
|
|
|
|
static inline int max(int a, int b) { if (a >= b) return a; return b; }
|
|
|
|
static inline int64 max64(int64 a, int64 b) { if (a >= b) return a; return b; }
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-11-24 13:19:48 +00:00
|
|
|
static inline uint minu(uint a, uint b) { if (a <= b) return a; return b; }
|
|
|
|
static inline uint maxu(uint a, uint b) { if (a >= b) return a; return b; }
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
|
2005-02-22 18:28:20 +00:00
|
|
|
static inline int clamp(int a, int min, int max)
|
|
|
|
{
|
|
|
|
if (a <= min) return min;
|
|
|
|
if (a >= max) return max;
|
|
|
|
return a;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
|
2004-11-24 13:19:48 +00:00
|
|
|
static inline int32 BIGMULSS(int32 a, int32 b, int shift) {
|
2004-08-09 17:04:08 +00:00
|
|
|
return (int32)(((int64)(a) * (int64)(b)) >> (shift));
|
|
|
|
}
|
|
|
|
|
2004-11-24 13:19:48 +00:00
|
|
|
static inline int64 BIGMULSS64(int64 a, int64 b, int shift) {
|
2004-09-13 20:38:36 +00:00
|
|
|
return ((a) * (b)) >> (shift);
|
|
|
|
}
|
|
|
|
|
2004-11-24 13:19:48 +00:00
|
|
|
static inline uint32 BIGMULUS(uint32 a, uint32 b, int shift) {
|
2004-08-09 17:04:08 +00:00
|
|
|
return (uint32)(((uint64)(a) * (uint64)(b)) >> (shift));
|
|
|
|
}
|
|
|
|
|
2004-11-24 13:19:48 +00:00
|
|
|
static inline int64 BIGMULS(int32 a, int32 b) {
|
2004-08-09 17:04:08 +00:00
|
|
|
return (int32)(((int64)(a) * (int64)(b)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* OPT: optimized into an unsigned comparison */
|
|
|
|
//#define IS_INSIDE_1D(x, base, size) ((x) >= (base) && (x) < (base) + (size))
|
|
|
|
#define IS_INSIDE_1D(x, base, size) ( (uint)((x) - (base)) < ((uint)(size)) )
|
|
|
|
|
|
|
|
|
2005-01-09 16:02:06 +00:00
|
|
|
#define HASBIT(x,y) ((x) & (1 << (y)))
|
|
|
|
#define SETBIT(x,y) ((x) |= (1 << (y)))
|
|
|
|
#define CLRBIT(x,y) ((x) &= ~(1 << (y)))
|
|
|
|
#define TOGGLEBIT(x,y) ((x) ^= (1 << (y)))
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// checking more bits. Maybe unneccessary, but easy to use
|
|
|
|
#define HASBITS(x,y) ((x) & (y))
|
|
|
|
#define SETBITS(x,y) ((x) |= (y))
|
|
|
|
#define CLRBITS(x,y) ((x) &= ~(y))
|
|
|
|
|
2005-07-24 15:56:31 +00:00
|
|
|
#define PLAYER_SPRITE_COLOR(owner) ( (_player_colors[owner] + 0x307) << PALETTE_SPRITE_START)
|
|
|
|
#define SPRITE_PALETTE(x) ((x) | PALETTE_MODIFIER_COLOR)
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
extern const byte _ffb_64[128];
|
2004-08-11 22:07:08 +00:00
|
|
|
/* Returns the position of the first bit that is not zero, counted from the
|
|
|
|
* left. Ie, 10110100 returns 2, 00000001 returns 0, etc. When x == 0 returns
|
|
|
|
* 0.
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
#define FIND_FIRST_BIT(x) _ffb_64[(x)]
|
2004-08-11 22:07:08 +00:00
|
|
|
/* Returns x with the first bit that is not zero, counted from the left, set
|
|
|
|
* to zero. So, 10110100 returns 10110000, 00000001 returns 00000000, etc.
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
#define KILL_FIRST_BIT(x) _ffb_64[(x)+64]
|
|
|
|
|
2004-11-24 13:19:48 +00:00
|
|
|
static inline int FindFirstBit2x64(int value)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-01-31 11:23:10 +00:00
|
|
|
/*
|
2004-08-09 17:04:08 +00:00
|
|
|
int i = 0;
|
|
|
|
if ( (byte) value == 0) {
|
|
|
|
i += 8;
|
|
|
|
value >>= 8;
|
|
|
|
}
|
|
|
|
return i + FIND_FIRST_BIT(value & 0x3F);
|
2005-01-31 11:23:10 +00:00
|
|
|
|
|
|
|
Faster ( or at least cleaner ) implementation below?
|
|
|
|
*/
|
2005-11-16 13:11:28 +00:00
|
|
|
if (GB(value, 0, 8) == 0) {
|
|
|
|
return FIND_FIRST_BIT(GB(value, 8, 6)) + 8;
|
2005-01-31 11:23:10 +00:00
|
|
|
} else {
|
2005-11-16 13:11:28 +00:00
|
|
|
return FIND_FIRST_BIT(GB(value, 0, 6));
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
static inline int KillFirstBit2x64(int value)
|
|
|
|
{
|
2005-11-16 13:11:28 +00:00
|
|
|
if (GB(value, 0, 8) == 0) {
|
|
|
|
return KILL_FIRST_BIT(GB(value, 8, 6)) << 8;
|
2005-01-31 11:23:10 +00:00
|
|
|
} else {
|
2005-11-16 13:11:28 +00:00
|
|
|
return value & (KILL_FIRST_BIT(GB(value, 0, 6)) | 0x3F00);
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* [min,max), strictly less than */
|
|
|
|
#define IS_BYTE_INSIDE(a,min,max) ((byte)((a)-(min)) < (byte)((max)-(min)))
|
|
|
|
#define IS_INT_INSIDE(a,min,max) ((uint)((a)-(min)) < (uint)((max)-(min)))
|
|
|
|
|
|
|
|
|
|
|
|
#define CHANCE16(a,b) ((uint16)Random() <= (uint16)((65536 * a) / b))
|
|
|
|
#define CHANCE16R(a,b,r) ((uint16)(r=Random()) <= (uint16)((65536 * a) / b))
|
|
|
|
#define CHANCE16I(a,b,v) ((uint16)(v) <= (uint16)((65536 * a) / b))
|
|
|
|
|
|
|
|
|
|
|
|
#define for_each_bit(_i,_b) \
|
|
|
|
for(_i=0; _b!=0; _i++,_b>>=1) \
|
|
|
|
if (_b&1)
|
|
|
|
|
|
|
|
#define abs myabs
|
|
|
|
|
|
|
|
|
2004-11-24 13:19:48 +00:00
|
|
|
static inline int intxchg_(int *a, int b) { int t = *a; *a = b; return t; }
|
2004-08-09 17:04:08 +00:00
|
|
|
#define intswap(a,b) ((b) = intxchg_(&(a), (b)))
|
2005-06-01 11:52:44 +00:00
|
|
|
static inline int uintxchg_(uint *a, uint b) { uint t = *a; *a = b; return t; }
|
|
|
|
#define uintswap(a,b) ((b) = uintxchg_(&(a), (b)))
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-11-24 13:19:48 +00:00
|
|
|
static inline int myabs(int a) { if (a<0) a = -a; return a; }
|
|
|
|
static inline int64 myabs64(int64 a) { if (a<0) a = -a; return a; }
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-11-24 13:19:48 +00:00
|
|
|
static inline void swap_byte(byte *a, byte *b) { byte t = *a; *a = *b; *b = t; }
|
|
|
|
static inline void swap_uint16(uint16 *a, uint16 *b) { uint16 t = *a; *a = *b; *b = t; }
|
|
|
|
static inline void swap_int16(int16 *a, int16 *b) { int16 t = *a; *a = *b; *b = t; }
|
2005-01-25 21:43:57 +00:00
|
|
|
static inline void swap_int32(int32 *a, int32 *b) { int32 t = *a; *a = *b; *b = t; }
|
2004-11-24 13:19:48 +00:00
|
|
|
static inline void swap_tile(TileIndex *a, TileIndex *b) { TileIndex t = *a; *a = *b; *b = t; }
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(TTD_LITTLE_ENDIAN)
|
2004-09-12 21:49:38 +00:00
|
|
|
# define READ_LE_UINT16(b) (*(const uint16*)(b))
|
2004-08-09 17:04:08 +00:00
|
|
|
#elif defined(TTD_BIG_ENDIAN)
|
2004-11-24 13:19:48 +00:00
|
|
|
static inline uint16 READ_LE_UINT16(const void *b) {
|
2004-09-12 21:49:38 +00:00
|
|
|
return ((const byte*)b)[0] + (((const byte*)b)[1] << 8);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-07-17 09:41:28 +00:00
|
|
|
/**
|
|
|
|
* ROtate x Left/Right by n (must be >= 0)
|
|
|
|
* @note Assumes a byte has 8 bits
|
|
|
|
*/
|
|
|
|
#define ROL(x, n) ((x) << (n) | (x) >> (sizeof(x) * 8 - (n)))
|
|
|
|
#define ROR(x, n) ((x) >> (n) | (x) << (sizeof(x) * 8 - (n)))
|
2005-05-22 07:12:09 +00:00
|
|
|
|
2005-09-08 12:48:26 +00:00
|
|
|
/**
|
|
|
|
* Return the smallest multiple of n equal or greater than x
|
|
|
|
* @note n must be a power of 2
|
|
|
|
*/
|
|
|
|
#define ALIGN(x, n) (((x) + (n) - 1) & ~((n) - 1))
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#endif /* MACROS_H */
|