bitmath_func.hpp
Go to the documentation of this file.00001
00002
00005 #ifndef BITMATH_FUNC_HPP
00006 #define BITMATH_FUNC_HPP
00007
00024 template<typename T> static inline uint GB(const T x, const uint8 s, const uint8 n)
00025 {
00026 return (x >> s) & ((1U << n) - 1);
00027 }
00028
00046 template<typename T, typename U> static inline T SB(T& x, const uint8 s, const uint8 n, const U d)
00047 {
00048 x &= (T)(~(((1U << n) - 1) << s));
00049 x |= (T)(d << s);
00050 return x;
00051 }
00052
00067 template<typename T, typename U> static inline T AB(T& x, const uint8 s, const uint8 n, const U i)
00068 {
00069 const T mask = (T)(((1U << n) - 1) << s);
00070 x = (T)((x & ~mask) | ((x + (i << s)) & mask));
00071 return x;
00072 }
00073
00085 template<typename T> static inline bool HasBit(const T x, const uint8 y)
00086 {
00087 return (x & ((T)1U << y)) != 0;
00088 }
00089
00100 #define HASBITS(x, y) ((x) & (y))
00101
00113 template<typename T> static inline T SetBit(T& x, const uint8 y)
00114 {
00115 return x = (T)(x | (T)(1U << y));
00116 }
00117
00128 #define SETBITS(x, y) ((x) |= (y))
00129
00141 template<typename T> static inline T ClrBit(T& x, const uint8 y)
00142 {
00143 return x = (T)(x & ~((T)1U << y));
00144 }
00145
00156 #define CLRBITS(x, y) ((x) &= ~(y))
00157
00169 template<typename T> static inline T ToggleBit(T& x, const uint8 y)
00170 {
00171 return x = (T)(x ^ (T)(1U << y));
00172 }
00173
00174
00176 extern const uint8 _ffb_64[64];
00177
00188 #define FIND_FIRST_BIT(x) _ffb_64[(x)]
00189
00204 static inline uint8 FindFirstBit2x64(const int value)
00205 {
00206 if ((value & 0xFF) == 0) {
00207 return FIND_FIRST_BIT((value >> 8) & 0x3F) + 8;
00208 } else {
00209 return FIND_FIRST_BIT(value & 0x3F);
00210 }
00211 }
00212
00213 uint8 FindFirstBit(uint32 x);
00214 uint8 FindLastBit(uint64 x);
00215
00226 template<typename T> static inline T KillFirstBit(T value)
00227 {
00228 return value &= (T)(value - 1);
00229 }
00230
00237 template<typename T> static inline uint CountBits(T value)
00238 {
00239 uint num;
00240
00241
00242
00243
00244
00245
00246 for (num = 0; value != 0; num++) {
00247 value &= (T)(value - 1);
00248 }
00249
00250 return num;
00251 }
00252
00261 template<typename T> static inline T ROL(const T x, const uint8 n)
00262 {
00263 return (T)(x << n | x >> (sizeof(x) * 8 - n));
00264 }
00265
00274 template<typename T> static inline T ROR(const T x, const uint8 n)
00275 {
00276 return (T)(x >> n | x << (sizeof(x) * 8 - n));
00277 }
00278
00290 #define FOR_EACH_SET_BIT(i, b) \
00291 for (i = 0; b != 0; i++, b >>= 1) \
00292 if (b & 1)
00293
00294
00295 #if defined(__APPLE__)
00296
00297
00298
00299
00300 #define BSWAP32(x) ((uint32)Endian32_Swap(x))
00301 #define BSWAP16(x) ((uint16)Endian16_Swap(x))
00302 #else
00303
00308 static inline uint32 BSWAP32(uint32 x)
00309 {
00310 return ((x >> 24) & 0xFF) | ((x >> 8) & 0xFF00) | ((x << 8) & 0xFF0000) | ((x << 24) & 0xFF000000);
00311 }
00312
00318 static inline uint16 BSWAP16(uint16 x)
00319 {
00320 return (x >> 8) | (x << 8);
00321 }
00322 #endif
00323
00324 #endif