tron@2186: /* $Id$ */ tron@2186: truelight@0: #ifndef MACROS_H truelight@0: #define MACROS_H truelight@0: tron@926: #include "map.h" tron@926: tron@2663: /// Fetch n bits starting at bit s from x tron@2663: #define GB(x, s, n) (((x) >> (s)) & ((1U << (n)) - 1)) tron@2663: /// Set n bits starting at bit s in x to d tron@2663: #define SB(x, s, n, d) ((x) = ((x) & ~(((1U << (n)) - 1) << (s))) | ((d) << (s))) tron@2663: /// Add i to the n bits starting at bit s in x tron@2663: #define AB(x, s, n, i) ((x) = ((x) & ~(((1U << (n)) - 1) << (s))) | (((x) + ((i) << (s))) & (((1U << (n)) - 1) << (s)))) tron@2663: truelight@193: #ifdef min truelight@0: #undef min truelight@0: #endif truelight@0: truelight@0: #ifdef max truelight@0: #undef max truelight@0: #endif truelight@0: tron@500: static inline int min(int a, int b) { if (a <= b) return a; return b; } tron@500: static inline int max(int a, int b) { if (a >= b) return a; return b; } tron@500: static inline int64 max64(int64 a, int64 b) { if (a >= b) return a; return b; } truelight@0: tron@500: static inline uint minu(uint a, uint b) { if (a <= b) return a; return b; } tron@500: static inline uint maxu(uint a, uint b) { if (a >= b) return a; return b; } truelight@0: truelight@0: tron@1400: static inline int clamp(int a, int min, int max) tron@1400: { tron@1400: if (a <= min) return min; tron@1400: if (a >= max) return max; tron@1400: return a; tron@1400: } truelight@0: truelight@0: tron@500: static inline int32 BIGMULSS(int32 a, int32 b, int shift) { truelight@0: return (int32)(((int64)(a) * (int64)(b)) >> (shift)); truelight@0: } truelight@0: tron@500: static inline int64 BIGMULSS64(int64 a, int64 b, int shift) { darkvater@236: return ((a) * (b)) >> (shift); darkvater@236: } darkvater@236: tron@500: static inline uint32 BIGMULUS(uint32 a, uint32 b, int shift) { truelight@0: return (uint32)(((uint64)(a) * (uint64)(b)) >> (shift)); truelight@0: } truelight@0: tron@500: static inline int64 BIGMULS(int32 a, int32 b) { truelight@0: return (int32)(((int64)(a) * (int64)(b))); truelight@0: } truelight@0: truelight@0: /* OPT: optimized into an unsigned comparison */ truelight@0: //#define IS_INSIDE_1D(x, base, size) ((x) >= (base) && (x) < (base) + (size)) truelight@0: #define IS_INSIDE_1D(x, base, size) ( (uint)((x) - (base)) < ((uint)(size)) ) truelight@0: truelight@0: truelight@954: #define HASBIT(x,y) ((x) & (1 << (y))) truelight@954: #define SETBIT(x,y) ((x) |= (1 << (y))) truelight@954: #define CLRBIT(x,y) ((x) &= ~(1 << (y))) truelight@954: #define TOGGLEBIT(x,y) ((x) ^= (1 << (y))) truelight@0: truelight@0: // checking more bits. Maybe unneccessary, but easy to use truelight@0: #define HASBITS(x,y) ((x) & (y)) truelight@0: #define SETBITS(x,y) ((x) |= (y)) truelight@0: #define CLRBITS(x,y) ((x) &= ~(y)) truelight@0: celestar@2187: #define PLAYER_SPRITE_COLOR(owner) ( (_player_colors[owner] + 0x307) << PALETTE_SPRITE_START) celestar@2187: #define SPRITE_PALETTE(x) ((x) | PALETTE_MODIFIER_COLOR) truelight@0: truelight@0: extern const byte _ffb_64[128]; darkvater@22: /* Returns the position of the first bit that is not zero, counted from the darkvater@22: * left. Ie, 10110100 returns 2, 00000001 returns 0, etc. When x == 0 returns darkvater@22: * 0. darkvater@22: */ truelight@0: #define FIND_FIRST_BIT(x) _ffb_64[(x)] darkvater@22: /* Returns x with the first bit that is not zero, counted from the left, set darkvater@22: * to zero. So, 10110100 returns 10110000, 00000001 returns 00000000, etc. darkvater@22: */ truelight@0: #define KILL_FIRST_BIT(x) _ffb_64[(x)+64] truelight@0: tron@500: static inline int FindFirstBit2x64(int value) truelight@0: { matthijs@1247: /* truelight@0: int i = 0; truelight@0: if ( (byte) value == 0) { truelight@0: i += 8; truelight@0: value >>= 8; truelight@0: } truelight@0: return i + FIND_FIRST_BIT(value & 0x3F); matthijs@1247: matthijs@1247: Faster ( or at least cleaner ) implementation below? matthijs@1247: */ tron@2663: if (GB(value, 0, 8) == 0) { tron@2663: return FIND_FIRST_BIT(GB(value, 8, 6)) + 8; matthijs@1247: } else { tron@2663: return FIND_FIRST_BIT(GB(value, 0, 6)); matthijs@1247: } matthijs@1247: truelight@0: } truelight@0: matthijs@1247: static inline int KillFirstBit2x64(int value) matthijs@1247: { tron@2663: if (GB(value, 0, 8) == 0) { tron@2663: return KILL_FIRST_BIT(GB(value, 8, 6)) << 8; matthijs@1247: } else { tron@2663: return value & (KILL_FIRST_BIT(GB(value, 0, 6)) | 0x3F00); matthijs@1247: } matthijs@1247: } truelight@0: truelight@0: /* [min,max), strictly less than */ truelight@0: #define IS_BYTE_INSIDE(a,min,max) ((byte)((a)-(min)) < (byte)((max)-(min))) truelight@0: #define IS_INT_INSIDE(a,min,max) ((uint)((a)-(min)) < (uint)((max)-(min))) truelight@0: truelight@0: truelight@0: #define CHANCE16(a,b) ((uint16)Random() <= (uint16)((65536 * a) / b)) truelight@0: #define CHANCE16R(a,b,r) ((uint16)(r=Random()) <= (uint16)((65536 * a) / b)) truelight@0: #define CHANCE16I(a,b,v) ((uint16)(v) <= (uint16)((65536 * a) / b)) truelight@0: truelight@0: tron@2952: #define for_each_bit(_i, _b) \ tron@2952: for (_i = 0; _b != 0; _i++, _b >>= 1) \ tron@2952: if (_b & 1) truelight@0: truelight@0: #define abs myabs truelight@0: truelight@0: tron@500: static inline int intxchg_(int *a, int b) { int t = *a; *a = b; return t; } truelight@0: #define intswap(a,b) ((b) = intxchg_(&(a), (b))) hackykid@1884: static inline int uintxchg_(uint *a, uint b) { uint t = *a; *a = b; return t; } hackykid@1884: #define uintswap(a,b) ((b) = uintxchg_(&(a), (b))) truelight@0: tron@500: static inline int myabs(int a) { if (a<0) a = -a; return a; } tron@500: static inline int64 myabs64(int64 a) { if (a<0) a = -a; return a; } truelight@0: tron@500: static inline void swap_byte(byte *a, byte *b) { byte t = *a; *a = *b; *b = t; } tron@500: static inline void swap_uint16(uint16 *a, uint16 *b) { uint16 t = *a; *a = *b; *b = t; } tron@500: static inline void swap_int16(int16 *a, int16 *b) { int16 t = *a; *a = *b; *b = t; } tron@1174: static inline void swap_int32(int32 *a, int32 *b) { int32 t = *a; *a = *b; *b = t; } tron@500: static inline void swap_tile(TileIndex *a, TileIndex *b) { TileIndex t = *a; *a = *b; *b = t; } truelight@0: truelight@0: truelight@0: truelight@0: #if defined(TTD_LITTLE_ENDIAN) darkvater@222: # define READ_LE_UINT16(b) (*(const uint16*)(b)) truelight@0: #elif defined(TTD_BIG_ENDIAN) tron@500: static inline uint16 READ_LE_UINT16(const void *b) { darkvater@222: return ((const byte*)b)[0] + (((const byte*)b)[1] << 8); truelight@0: } truelight@0: #endif truelight@0: tron@2086: /** tron@2086: * ROtate x Left/Right by n (must be >= 0) tron@2086: * @note Assumes a byte has 8 bits tron@2086: */ tron@2086: #define ROL(x, n) ((x) << (n) | (x) >> (sizeof(x) * 8 - (n))) tron@2086: #define ROR(x, n) ((x) >> (n) | (x) << (sizeof(x) * 8 - (n))) tron@1852: tron@2398: /** tron@2398: * Return the smallest multiple of n equal or greater than x tron@2398: * @note n must be a power of 2 tron@2398: */ tron@2398: #define ALIGN(x, n) (((x) + (n) - 1) & ~((n) - 1)) tron@2398: truelight@0: #endif /* MACROS_H */