Index: src/macros.h =================================================================== --- src/macros.h (Revision 11369) +++ src/macros.h (Arbeitskopie) @@ -8,7 +8,7 @@ /** * Fetch n bits from x, started at bit s. * - * This macro can be used to fetch n bits from the value x. The + * This can be used to fetch n bits from the value x. The * s value set the startposition to read. The startposition is * count from the LSB and starts at 0. The result starts at a * LSB, as this isn't just an and-bitmask but also some @@ -21,11 +21,14 @@ * @param n The number of bits to read. * @return The selected bits, aligned to a LSB. */ -#define GB(x, s, n) (((x) >> (s)) & ((1U << (n)) - 1)) +template static inline uint GB(const T x, const uint8 s, const uint8 n) +{ + return (x >> s) & ((1U << n) - 1); +} /** Set n bits from x starting at bit s to d * - * This macro sets n bits from x which started as bit s to the value of + * This sets n bits from x which started as bit s to the value of * d. The parameters x, s and n works the same as the parameters of * #GB. The result is saved in x again. Unused bits in the window * provided by n are set to 0 if the value of b isn't "big" enough. @@ -40,7 +43,12 @@ * @param d The actually new bits to save in the defined position. * @return The new value of x */ -#define SB(x, s, n, d) ((x) = ((x) & ~(((1U << (n)) - 1) << (s))) | ((d) << (s))) +template static inline T SB(T& x, const uint8 s, const uint8 n, const U d) +{ + x &= (T)(~(((1U << n) - 1) << s)); + x |= (T)(d << s); + return x; +} /** Add i to n bits of x starting at bit s. * @@ -56,7 +64,12 @@ * @param i The value to add at the given startposition in the given window. * @return The new value of x */ -#define AB(x, s, n, i) ((x) = ((x) & ~(((1U << (n)) - 1) << (s))) | (((x) + ((i) << (s))) & (((1U << (n)) - 1) << (s)))) +template static inline T AB(T& x, const uint8 s, const uint8 n, const U i) +{ + x &= (T)(~(((1U << n) - 1) << s)); + x |= (T)((x + (i << s)) & (((1U << n) - 1) << s)); + return x; +} #ifdef min #undef min @@ -227,22 +240,6 @@ /** - * Checks if a value is between a window started at some base point. - * - * This macro checks if the value x is between the value of base - * and base+size. If x equals base this returns true. If x equals - * base+size this returns false. - * - * @param x The value to check - * @param base The base value of the interval - * @param size The size of the interval - * @return True if the value is in the interval, false else. - */ -/* 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)) ) - -/** * Checks if a bit in a value is set. * * This function checks if a bit inside a value is set or not. @@ -253,7 +250,7 @@ * @param y The position of the bit to check, started from the LSB * @return True if the bit is set, false else. */ -template static inline bool HASBIT(const T x, const int8 y) +template static inline bool HASBIT(const T x, const uint8 y) { return (x & ((T)1U << y)) != 0; } @@ -269,7 +266,7 @@ * @param y The bit position to set * @return The new value of the old value with the bit set */ -template static inline T SETBIT(T& x, const int8 y) +template static inline T SETBIT(T& x, const uint8 y) { return x |= (T)1U << y; } @@ -285,7 +282,7 @@ * @param y The bit position to clear * @return The new value of the old value with the bit cleared */ -template static inline T CLRBIT(T& x, const int8 y) +template static inline T CLRBIT(T& x, const uint8 y) { return x &= ~((T)1U << y); } @@ -301,7 +298,7 @@ * @param y The bit position to toggle * @return The new value of the old value with the bit toggled */ -template static inline T TOGGLEBIT(T& x, const int8 y) +template static inline T TOGGLEBIT(T& x, const uint8 y) { return x ^= (T)1U << y; } @@ -458,6 +455,25 @@ } /** + * Checks if a value is between a window started at some base point. + * + * This checks if the value x is between the value of base + * and base+size. If x equals base this returns true. If x equals + * base+size this returns false. + * + * @param x The value to check + * @param base The base value of the interval + * @param size The size of the interval + * @return True if the value is in the interval, false else. + */ +/* OPT: optimized into an unsigned comparison */ +//#define IS_INSIDE_1D(x, base, size) ((x) >= (base) && (x) < (base) + (size)) +template static inline bool IS_INSIDE_1D(const T x, const int base, const uint size) +{ + return (uint)(x - base) < size; +} + +/** * Checks if a byte is in an interval. * * This macro returns true if a byte value is in the interval of [min, max). @@ -492,7 +508,7 @@ * @param b The denominator of the fraction, must of course not be null * @return True in (a/b) percent */ -#define CHANCE16(a, b) ((uint16)Random() <= (uint16)((65536 * (a)) / (b))) +#define CHANCE16(a, b) ((uint16)Random() <= (uint16)((UINT16_MAX * (a)) / (b))) /** * Flips a coin with a given probability and saves the randomize-number in a variable. @@ -505,7 +521,7 @@ * @param r The variable to save the randomize-number from Random() * @return True in (a/b) percent */ -#define CHANCE16R(a, b, r) ((uint16)(r = Random()) <= (uint16)((65536 * (a)) / (b))) +#define CHANCE16R(a, b, r) ((uint16)(r = Random()) <= (uint16)((UINT16_MAX * (a)) / (b))) /** * Checks if a given randomize-number is below a given probability. @@ -518,7 +534,7 @@ * @param v The given randomize-number * @return True if v is less or equals (a/b) */ -#define CHANCE16I(a, b, v) ((uint16)(v) <= (uint16)((65536 * (a)) / (b))) +#define CHANCE16I(a, b, v) ((uint16)(v) <= (uint16)((UINT16_MAX * (a)) / (b))) #define for_each_bit(_i, _b) \ @@ -544,13 +560,24 @@ /** - * ROtate x Left/Right by n (must be >= 0) + * ROtate x Left by n * @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))) +template static inline T ROL(const T x, const uint8 n) +{ + return (T)(x << n | x >> (sizeof(x) * 8 - n)); +} /** + * ROtate x Right by n + * @note Assumes a byte has 8 bits + */ +template static inline T ROR(const T x, const uint8 n) +{ + return (T)(x >> n | x << (sizeof(x) * 8 - n)); +} + +/** * Return the smallest multiple of n equal or greater than x * @note n must be a power of 2 */