tron@2186: /* $Id$ */ tron@2186: KUDr@6285: /** @file macros.h */ KUDr@6285: truelight@0: #ifndef MACROS_H truelight@0: #define MACROS_H truelight@0: KUDr@6285: /* Fetch n bits starting at bit s from x */ tron@2663: #define GB(x, s, n) (((x) >> (s)) & ((1U << (n)) - 1)) KUDr@6285: /* 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))) KUDr@6285: /* 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: celestar@5854: template celestar@5854: static inline T max(T a, T b) celestar@5854: { celestar@5854: return a >= b ? a : b; celestar@5854: } celestar@5852: tron@500: static inline int min(int a, int 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; } 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: Darkvater@3352: static inline uint clampu(uint a, uint min, uint max) Darkvater@3352: { Darkvater@3352: if (a <= min) return min; Darkvater@3352: if (a >= max) return max; Darkvater@3352: return a; Darkvater@3352: } truelight@0: bjarni@6268: static inline int32 BIGMULSS(int32 a, int32 b, int shift) bjarni@6268: { bjarni@6268: return (int32)((int64)a * (int64)b >> shift); truelight@0: } truelight@0: bjarni@6268: static inline uint32 BIGMULUS(uint32 a, uint32 b, int shift) bjarni@6268: { bjarni@6268: return (uint32)((uint64)a * (uint64)b >> shift); truelight@0: } 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: bjarni@6268: template static inline bool HASBIT(T x, int y) KUDr@6254: { bjarni@6268: return (x & ((T)1 << y)) != 0; KUDr@6254: } truelight@0: bjarni@6268: template static inline T SETBIT(T& x, int y) KUDr@6254: { bjarni@6268: return x |= (T)1 << y; KUDr@6254: } KUDr@6254: bjarni@6268: template static inline T CLRBIT(T& x, int y) KUDr@6254: { bjarni@6268: return x &= ~((T)1 << y); KUDr@6254: } KUDr@6254: bjarni@6268: template static inline T TOGGLEBIT(T& x, int y) KUDr@6254: { bjarni@6268: return x ^= (T)1 << y; KUDr@6254: } KUDr@6254: truelight@0: KUDr@6285: /* checking more bits. Maybe unneccessary, but easy to use */ KUDr@6308: #define HASBITS(x, y) ((x) & (y)) KUDr@6308: #define SETBITS(x, y) ((x) |= (y)) KUDr@6308: #define CLRBITS(x, y) ((x) &= ~(y)) truelight@0: peter1138@5919: #define GENERAL_SPRITE_COLOR(color) ((color) + PALETTE_RECOLOR_START) peter1138@5919: #define PLAYER_SPRITE_COLOR(owner) (GENERAL_SPRITE_COLOR(_player_colors[owner])) 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: */ KUDr@6308: #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: celestar@3812: /** returns true if value a has only one bit set to 1 */ celestar@3812: #define HAS_SINGLE_BIT(a) ( ((a) & ((a) - 1)) == 0) celestar@3812: truelight@0: /* [min,max), strictly less than */ KUDr@6308: #define IS_BYTE_INSIDE(a, min, max) ((byte)((a) - (min)) < (byte)((max) - (min))) KUDr@6308: #define IS_INT_INSIDE(a, min, max) ((uint)((a) - (min)) < (uint)((max) - (min))) truelight@0: truelight@0: KUDr@6308: #define CHANCE16(a, b) ((uint16)Random() <= (uint16)((65536 * (a)) / (b))) KUDr@6308: #define CHANCE16R(a, b, r) ((uint16)(r = Random()) <= (uint16)((65536 * (a)) / (b))) KUDr@6308: #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: Darkvater@2966: static inline uint16 ReadLE16Aligned(const void* x) Darkvater@2966: { Darkvater@2966: return FROM_LE16(*(const uint16*)x); Darkvater@2966: } truelight@0: Darkvater@2966: static inline uint16 ReadLE16Unaligned(const void* x) Darkvater@2966: { Darkvater@2966: #ifdef OTTD_ALIGNMENT Darkvater@2966: return ((const byte*)x)[0] | ((const byte*)x)[1] << 8; Darkvater@2966: #else Darkvater@2966: return FROM_LE16(*(const uint16*)x); truelight@0: #endif Darkvater@2966: } Darkvater@2966: 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@4300: /** return the largest value that can be entered in a variable. truelight@4300: */ truelight@4300: #define MAX_UVALUE(type) ((type)~(type)0) truelight@4300: truelight@0: #endif /* MACROS_H */