tron@2186: /* $Id$ */ tron@2186: belugas@6201: /** @file macros.h */ belugas@6201: truelight@0: #ifndef MACROS_H truelight@0: #define MACROS_H truelight@0: rubidium@7326: /** rubidium@7326: * Fetch n bits from x, started at bit s. rubidium@7326: * rubidium@7326: * This macro can be used to fetch n bits from the value x. The rubidium@7326: * s value set the startposition to read. The startposition is rubidium@7326: * count from the LSB and starts at 0. The result starts at a rubidium@7326: * LSB, as this isn't just an and-bitmask but also some rubidium@7326: * bit-shifting operations. GB(0xFF, 2, 1) will so rubidium@7326: * return 0x01 (0000 0001) instead of rubidium@7326: * 0x04 (0000 0100). rubidium@7326: * rubidium@7326: * @param x The value to read some bits. rubidium@7326: * @param s The startposition to read some bits. rubidium@7326: * @param n The number of bits to read. rubidium@7326: * @return The selected bits, aligned to a LSB. rubidium@7326: */ tron@2663: #define GB(x, s, n) (((x) >> (s)) & ((1U << (n)) - 1)) rubidium@7326: rubidium@7326: /** Set n bits from x starting at bit s to d rubidium@7326: * rubidium@7326: * This macro sets n bits from x which started as bit s to the value of rubidium@7326: * d. The parameters x, s and n works the same as the parameters of rubidium@7326: * #GB. The result is saved in x again. Unused bits in the window rubidium@7326: * provided by n are set to 0 if the value of b isn't "big" enough. rubidium@7326: * This is not a bug, its a feature. rubidium@7326: * rubidium@7326: * @note Parameter x must be a variable as the result is saved there. rubidium@7326: * @note To avoid unexpecting results the value of b should not use more rubidium@7326: * space as the provided space of n bits (log2) rubidium@7326: * @param x The variable to change some bits rubidium@7326: * @param s The startposition for the new bits rubidium@7326: * @param n The size/window for the new bits rubidium@7326: * @param d The actually new bits to save in the defined position. rubidium@7326: * @return The new value of x rubidium@7326: */ tron@2663: #define SB(x, s, n, d) ((x) = ((x) & ~(((1U << (n)) - 1) << (s))) | ((d) << (s))) rubidium@7326: rubidium@7326: /** Add i to n bits of x starting at bit s. rubidium@7326: * rubidium@7326: * This add the value of i on n bits of x starting at bit s. The parameters x, rubidium@7326: * s, i are similar to #GB besides x must be a variable as the result are rubidium@7326: * saved there. An overflow does not affect the following bits of the given rubidium@7326: * bit window and is simply ignored. rubidium@7326: * rubidium@7326: * @note Parameter x must be a variable as the result is saved there. rubidium@7326: * @param x The variable to add some bits at some position rubidium@7326: * @param s The startposition of the addition rubidium@7326: * @param n The size/window for the addition rubidium@7326: * @param i The value to add at the given startposition in the given window. rubidium@7326: * @return The new value of x rubidium@7326: */ 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: rubidium@7326: /** rubidium@7326: * Returns the maximum of two values. rubidium@7326: * rubidium@7326: * This function returns the greater value of two given values. rubidium@7326: * If they are equal the value of a is returned. rubidium@7326: * rubidium@7326: * @param a The first value rubidium@7326: * @param b The second value rubidium@7326: * @return The greater value or a if equals rubidium@7326: */ celestar@5603: template celestar@5603: static inline T max(T a, T b) celestar@5603: { celestar@5603: return a >= b ? a : b; celestar@5603: } celestar@5601: rubidium@7326: /** rubidium@7326: * Returns the minimum of two values. rubidium@7326: * rubidium@7326: * This function returns the smaller value of two given values. rubidium@7326: * If they are equal the value of b is returned. rubidium@7326: * rubidium@7326: * @param a The first value rubidium@7326: * @param b The second value rubidium@7326: * @return The smaller value or b if equals rubidium@7326: */ rubidium@6957: template rubidium@6957: static inline T min(T a, T b) rubidium@6957: { rubidium@6957: return a < b ? a : b; rubidium@6957: } rubidium@6957: rubidium@7326: /** rubidium@7326: * Returns the minimum of two integer. rubidium@7326: * rubidium@7326: * This function returns the smaller value of two given integers. rubidium@7326: * rubidium@7326: * @param a The first integer rubidium@7326: * @param b The second integer rubidium@7326: * @return The smaller value rubidium@7326: */ tron@500: static inline int min(int a, int b) { if (a <= b) return a; return b; } truelight@0: rubidium@7326: /** rubidium@7326: * Returns the minimum of two unsigned integers. rubidium@7326: * rubidium@7326: * This function returns the smaller value of two given unsigned integers. rubidium@7326: * rubidium@7326: * @param a The first unsigned integer rubidium@7326: * @param b The second unsigned integer rubidium@7326: * @return The smaller value rubidium@7326: */ tron@500: static inline uint minu(uint a, uint b) { if (a <= b) return a; return b; } truelight@0: rubidium@7326: /** rubidium@7326: * Clamp an integer between an interval. rubidium@7326: * rubidium@7326: * This function returns a value which is between the given interval of rubidium@7326: * min and max. If the given value is in this interval the value itself rubidium@7326: * is returned otherwise the border of the interval is returned, according rubidium@7326: * which side of the interval was 'left'. rubidium@7326: * rubidium@7326: * @note The min value must be less or equal of max or you get some rubidium@7326: * unexpected results. rubidium@7326: * @param a The value to clamp/truncate. rubidium@7326: * @param min The minimum of the interval. rubidium@7326: * @param max the maximum of the interval. rubidium@7326: * @returns A value between min and max which is closest to a. rubidium@7326: * @see clampu(uint, uint, uint) rubidium@7326: */ 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: rubidium@7326: /** rubidium@7326: * Clamp an unsigned integer between an interval. rubidium@7326: * rubidium@7326: * This function returns a value which is between the given interval of rubidium@7326: * min and max. If the given value is in this interval the value itself rubidium@7326: * is returned otherwise the border of the interval is returned, according rubidium@7326: * which side of the interval was 'left'. rubidium@7326: * rubidium@7326: * @note The min value must be less or equal of max or you get some rubidium@7326: * unexpected results. rubidium@7326: * @param a The value to clamp/truncate. rubidium@7326: * @param min The minimum of the interval. rubidium@7326: * @param max the maximum of the interval. rubidium@7326: * @returns A value between min and max which is closest to a. rubidium@7326: * @see clamp(int, int, int) rubidium@7326: */ 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: rubidium@7326: /** rubidium@7326: * Reduce a signed 64-bit int to a signed 32-bit one rubidium@7326: * rubidium@7326: * This function clamps a 64-bit integer to a 32-bit integer. rubidium@7326: * If the 64-bit value is smaller than the smallest 32-bit integer rubidium@7326: * value 0x80000000 this value is returned (the left one bit is the sign bit). rubidium@7326: * If the 64-bit value is greater than the greatest 32-bit integer value 0x7FFFFFFF rubidium@7326: * this value is returned. In all other cases the 64-bit value 'fits' in a rubidium@7326: * 32-bits integer field and so the value is casted to int32 and returned. rubidium@7326: * rubidium@7326: * @param a The 64-bit value to clamps rubidium@7326: * @return The 64-bit value reduced to a 32-bit value rubidium@7326: * @see clamp(int, int, int) rubidium@7326: */ rubidium@6990: static inline int32 ClampToI32(int64 a) rubidium@6990: { rubidium@6990: if (a <= (int32)0x80000000) return 0x80000000; rubidium@6990: if (a >= (int32)0x7FFFFFFF) return 0x7FFFFFFF; rubidium@6990: return (int32)a; rubidium@6990: } rubidium@6990: rubidium@7326: /** rubidium@7326: * Multiply two integer values and shift the results to right. rubidium@7326: * rubidium@7326: * This function multiplies two integer values. The result is rubidium@7326: * shifted by the amount of shift to right. rubidium@7326: * rubidium@7326: * @param a The first integer rubidium@7326: * @param b The second integer rubidium@7326: * @param shift The amount to shift the value to right. rubidium@7326: * @return The shifted result rubidium@7326: */ tron@6110: static inline int32 BIGMULSS(int32 a, int32 b, int shift) tron@6110: { tron@6110: return (int32)((int64)a * (int64)b >> shift); truelight@0: } truelight@0: rubidium@7326: /** rubidium@7326: * Multiply two unsigned integers and shift the results to right. rubidium@7326: * rubidium@7326: * This function multiplies two unsigned integers. The result is rubidium@7326: * shifted by the amount of shift to right. rubidium@7326: * rubidium@7326: * @param a The first unsigned integer rubidium@7326: * @param b The second unsigned integer rubidium@7326: * @param shift The amount to shift the value to right. rubidium@7326: * @return The shifted result rubidium@7326: */ tron@6110: static inline uint32 BIGMULUS(uint32 a, uint32 b, int shift) tron@6110: { tron@6110: return (uint32)((uint64)a * (uint64)b >> shift); truelight@0: } truelight@0: truelight@0: rubidium@7326: /** rubidium@7326: * Checks if a value is between a window started at some base point. rubidium@7326: * rubidium@7326: * This macro checks if the value x is between the value of base rubidium@7326: * and base+size. If x equals base this returns true. If x equals rubidium@7326: * base+size this returns false. rubidium@7326: * rubidium@7326: * @param x The value to check rubidium@7326: * @param base The base value of the interval rubidium@7326: * @param size The size of the interval rubidium@7326: * @return True if the value is in the interval, false else. rubidium@7326: */ 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: rubidium@7326: /** rubidium@7326: * Checks if a bit in a value is set. rubidium@7326: * rubidium@7326: * This function checks if a bit inside a value is set or not. rubidium@7326: * The y value specific the position of the bit, started at the rubidium@7326: * LSB and count from 0. rubidium@7326: * rubidium@7326: * @param x The value to check rubidium@7326: * @param y The position of the bit to check, started from the LSB rubidium@7326: * @return True if the bit is set, false else. rubidium@7326: */ tron@6110: template static inline bool HASBIT(T x, int y) celestar@6010: { tron@6110: return (x & ((T)1 << y)) != 0; celestar@6010: } truelight@0: rubidium@7326: /** rubidium@7326: * Set a bit in a variable. rubidium@7326: * rubidium@7326: * This function sets a bit in a variable. The variable is changed rubidium@7326: * and the value is also returned. Parameter y defines the bit and rubidium@7326: * starts at the LSB with 0. rubidium@7326: * rubidium@7326: * @param x The variable to set a bit rubidium@7326: * @param y The bit position to set rubidium@7326: * @return The new value of the old value with the bit set rubidium@7326: */ tron@6110: template static inline T SETBIT(T& x, int y) celestar@6010: { tron@6110: return x |= (T)1 << y; celestar@6010: } celestar@6010: rubidium@7326: /** rubidium@7326: * Clears a bit in a variable. rubidium@7326: * rubidium@7326: * This function clears a bit in a variable. The variable is rubidium@7326: * changed and the value is also returned. Parameter y defines the bit rubidium@7326: * to clear and starts at the LSB with 0. rubidium@7326: * rubidium@7326: * @param x The variable to clear the bit rubidium@7326: * @param y The bit position to clear rubidium@7326: * @return The new value of the old value with the bit cleared rubidium@7326: */ tron@6110: template static inline T CLRBIT(T& x, int y) celestar@6010: { tron@6110: return x &= ~((T)1 << y); celestar@6010: } celestar@6010: rubidium@7326: /** rubidium@7326: * Toggles a bit in a variable. rubidium@7326: * rubidium@7326: * This function toggles a bit in a variable. The variable is rubidium@7326: * changed and the value is also returned. Parameter y defines the bit rubidium@7326: * to toggle and starts at the LSB with 0. rubidium@7326: * rubidium@7326: * @param x The varliable to toggle the bit rubidium@7326: * @param y The bit position to toggle rubidium@7326: * @return The new value of the old value with the bit toggled rubidium@7326: */ tron@6110: template static inline T TOGGLEBIT(T& x, int y) celestar@6010: { tron@6110: return x ^= (T)1 << y; celestar@6010: } celestar@6010: truelight@0: belugas@6201: /* checking more bits. Maybe unneccessary, but easy to use */ rubidium@7326: /** rubidium@7326: * Check several bits in a value. rubidium@7326: * rubidium@7326: * This macro checks if a value contains at least one bit of an other rubidium@7326: * value. rubidium@7326: * rubidium@7326: * @param x The first value rubidium@7326: * @param y The second value rubidium@7326: * @return True if at least one bit is set in both values, false else. rubidium@7326: */ rubidium@6491: #define HASBITS(x, y) ((x) & (y)) rubidium@7326: rubidium@7326: /** rubidium@7326: * Sets several bits in a variable. rubidium@7326: * rubidium@7326: * This macro sets several bits in a variable. The bits to set are provided rubidium@7326: * by a value. The new value is also returned. rubidium@7326: * rubidium@7326: * @param x The variable to set some bits rubidium@7326: * @param y The value with set bits for setting them in the variable rubidium@7326: * @return The new value of x rubidium@7326: */ rubidium@6491: #define SETBITS(x, y) ((x) |= (y)) rubidium@7326: rubidium@7326: /** rubidium@7326: * Clears several bits in a variable. rubidium@7326: * rubidium@7326: * This macro clears several bits in a variable. The bits to clear are rubidium@7326: * provided by a value. The new value is also returned. rubidium@7326: * rubidium@7326: * @param x The variable to clear some bits rubidium@7326: * @param y The value with set bits for clearing them in the variable rubidium@7326: * @return The new value of x rubidium@7326: */ rubidium@6491: #define CLRBITS(x, y) ((x) &= ~(y)) truelight@0: peter1138@5668: #define GENERAL_SPRITE_COLOR(color) ((color) + PALETTE_RECOLOR_START) peter1138@5668: #define PLAYER_SPRITE_COLOR(owner) (GENERAL_SPRITE_COLOR(_player_colors[owner])) truelight@0: truelight@0: extern const byte _ffb_64[128]; rubidium@7326: rubidium@7326: /** rubidium@7326: * Returns the first occure of a bit in a 6-bit value (from right). rubidium@7326: * rubidium@7326: * Returns the position of the first bit that is not zero, counted from the rubidium@7326: * LSB. Ie, 110100 returns 2, 000001 returns 0, etc. When x == 0 returns darkvater@22: * 0. rubidium@7326: * rubidium@7326: * @param x The 6-bit value to check the first zero-bit rubidium@7326: * @return The first position of a bit started from the LSB or 0 if x is 0. darkvater@22: */ truelight@0: #define FIND_FIRST_BIT(x) _ffb_64[(x)] rubidium@7326: rubidium@7326: /** rubidium@7326: * Returns a value with the first occured of a bit set to zero. rubidium@7326: * rubidium@7326: * Returns x with the first bit from LSB that is not zero set rubidium@7326: * to zero. So, 110100 returns 110000, 000001 returns 000000, etc. rubidium@7326: * rubidium@7326: * @param x The value to returned a new value rubidium@7326: * @return The value which the first bit is set to zero darkvater@22: */ rubidium@6491: #define KILL_FIRST_BIT(x) _ffb_64[(x) + 64] truelight@0: rubidium@7326: /** rubidium@7326: * Finds the position of the first bit in an integer. rubidium@7326: * rubidium@7326: * This function returns the position of the first bit set in the rubidium@7326: * integer. It does only check the bits of the bitmask rubidium@7326: * 0x3F3F (0011111100111111) and checks only the rubidium@7326: * bits of the bitmask 0x3F00 if and only if the rubidium@7326: * lower part 0x00FF is 0. This results the bits at 0x00C0 must rubidium@7326: * be also zero to check the bits at 0x3F00. rubidium@7326: * rubidium@7326: * @param value The value to check the first bits rubidium@7326: * @return The position of the first bit which is set rubidium@7326: * @see FIND_FIRST_BIT rubidium@7326: */ 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: } truelight@0: } truelight@0: rubidium@7326: /** rubidium@7326: * Clear the first bit in an integer. rubidium@7326: * rubidium@7326: * This function returns a value where the first bit (from LSB) rubidium@7326: * is cleared. This function checks, similar to FindFirstBit2x64, rubidium@7326: * the bits at 0x3F3F. rubidium@7326: * rubidium@7326: * @param value The value to clear the first bit rubidium@7326: * @return The new value with the first bit cleared rubidium@7326: * @see KILL_FIRST_BIT rubidium@7326: * @see FindFirstBit2x64 rubidium@7326: */ 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: rubidium@7326: /** rubidium@7326: * Returns true if value a has only one bit set to 1 rubidium@7326: * rubidium@7326: * This macro returns true if only one bit is set. rubidium@7326: * rubidium@7326: * @param a The value to check rubidium@7326: * @return True if only one bit is set, false else rubidium@7326: */ celestar@3812: #define HAS_SINGLE_BIT(a) ( ((a) & ((a) - 1)) == 0) celestar@3812: rubidium@7326: /** rubidium@7326: * Checks if a byte is in an interval. rubidium@7326: * rubidium@7326: * This macro returns true if a byte value is in the interval of [min, max). rubidium@7326: * rubidium@7326: * @param a The byte value to check rubidium@7326: * @param min The minimum of the interval rubidium@7326: * @param max The maximum of the interval rubidium@7326: * @see IS_INSIDE_1D rubidium@7326: */ rubidium@6491: #define IS_BYTE_INSIDE(a, min, max) ((byte)((a) - (min)) < (byte)((max) - (min))) rubidium@7326: rubidium@7326: /** rubidium@7326: * Checks if an int is in an interval. rubidium@7326: * rubidium@7326: * This macro returns true if a integer value is in the interval of [min, max). rubidium@7326: * rubidium@7326: * @param a The integer value to check rubidium@7326: * @param min The minimum of the interval rubidium@7326: * @param max The maximum of the interval rubidium@7326: * @see IS_INSIDE_1D rubidium@7326: */ rubidium@6491: #define IS_INT_INSIDE(a, min, max) ((uint)((a) - (min)) < (uint)((max) - (min))) truelight@0: rubidium@7326: /** rubidium@7326: * Flips a coin with a given probability. rubidium@7326: * rubidium@7326: * This macro can be used to get true or false randomized according to a rubidium@7326: * given probability. The parameter a and b create a percent value with rubidium@7326: * (a/b). The macro returns true in (a/b) percent. rubidium@7326: * rubidium@7326: * @param a The numerator of the fraction rubidium@7326: * @param b The denominator of the fraction, must of course not be null rubidium@7326: * @return True in (a/b) percent rubidium@7326: */ rubidium@7326: #define CHANCE16(a, b) ((uint16)Random() <= (uint16)((65536 * (a)) / (b))) truelight@0: rubidium@7326: /** rubidium@7326: * Flips a coin with a given probability and saves the randomize-number in a variable. rubidium@7326: * rubidium@7326: * This macro uses the same parameters as the CHANCE16 marco. The third parameter rubidium@7326: * must be a variable the randomize-number from Random() is saved in. rubidium@7326: * rubidium@7326: * @param a The numerator of the fraction, see CHANCE16 rubidium@7326: * @param b The denominator of the fraction, see CHANCE16 rubidium@7326: * @param r The variable to save the randomize-number from Random() rubidium@7326: * @return True in (a/b) percent rubidium@7326: */ rubidium@6491: #define CHANCE16R(a, b, r) ((uint16)(r = Random()) <= (uint16)((65536 * (a)) / (b))) rubidium@7326: rubidium@7326: /** rubidium@7326: * Checks if a given randomize-number is below a given probability. rubidium@7326: * rubidium@7326: * This macro is used to check if the given probability by the fraction of (a/b) rubidium@7326: * is greater than the given randomize-number v. rubidium@7326: * rubidium@7326: * @param a The numerator of the fraction, see CHANCE16 rubidium@7326: * @param b The denominator of the fraction, see CHANCE16 rubidium@7326: * @param v The given randomize-number rubidium@7326: * @return True if v is less or equals (a/b) rubidium@7326: */ rubidium@6491: #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 */