bitmath_func.hpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #ifndef BITMATH_FUNC_HPP
00013 #define BITMATH_FUNC_HPP
00014
00031 template <typename T>
00032 static FORCEINLINE uint GB(const T x, const uint8 s, const uint8 n)
00033 {
00034 return (x >> s) & ((1U << n) - 1);
00035 }
00036
00054 template <typename T, typename U>
00055 static FORCEINLINE T SB(T &x, const uint8 s, const uint8 n, const U d)
00056 {
00057 x &= (T)(~(((1U << n) - 1) << s));
00058 x |= (T)(d << s);
00059 return x;
00060 }
00061
00076 template <typename T, typename U>
00077 static FORCEINLINE T AB(T &x, const uint8 s, const uint8 n, const U i)
00078 {
00079 const T mask = (T)(((1U << n) - 1) << s);
00080 x = (T)((x & ~mask) | ((x + (i << s)) & mask));
00081 return x;
00082 }
00083
00095 template <typename T>
00096 static FORCEINLINE bool HasBit(const T x, const uint8 y)
00097 {
00098 return (x & ((T)1U << y)) != 0;
00099 }
00100
00112 template <typename T>
00113 static FORCEINLINE 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>
00142 static FORCEINLINE T ClrBit(T &x, const uint8 y)
00143 {
00144 return x = (T)(x & ~((T)1U << y));
00145 }
00146
00157 #define CLRBITS(x, y) ((x) &= ~(y))
00158
00170 template <typename T>
00171 static FORCEINLINE T ToggleBit(T &x, const uint8 y)
00172 {
00173 return x = (T)(x ^ (T)(1U << y));
00174 }
00175
00176
00178 extern const uint8 _ffb_64[64];
00179
00190 #define FIND_FIRST_BIT(x) _ffb_64[(x)]
00191
00206 static FORCEINLINE uint8 FindFirstBit2x64(const int value)
00207 {
00208 if ((value & 0xFF) == 0) {
00209 return FIND_FIRST_BIT((value >> 8) & 0x3F) + 8;
00210 } else {
00211 return FIND_FIRST_BIT(value & 0x3F);
00212 }
00213 }
00214
00215 uint8 FindFirstBit(uint32 x);
00216 uint8 FindLastBit(uint64 x);
00217
00228 template <typename T>
00229 static FORCEINLINE T KillFirstBit(T value)
00230 {
00231 return value &= (T)(value - 1);
00232 }
00233
00240 template <typename T>
00241 static inline uint CountBits(T value)
00242 {
00243 uint num;
00244
00245
00246
00247
00248
00249
00250 for (num = 0; value != 0; num++) {
00251 value &= (T)(value - 1);
00252 }
00253
00254 return num;
00255 }
00256
00265 template <typename T>
00266 static FORCEINLINE T ROL(const T x, const uint8 n)
00267 {
00268 return (T)(x << n | x >> (sizeof(x) * 8 - n));
00269 }
00270
00279 template <typename T>
00280 static FORCEINLINE T ROR(const T x, const uint8 n)
00281 {
00282 return (T)(x >> n | x << (sizeof(x) * 8 - n));
00283 }
00284
00296 #define FOR_EACH_SET_BIT(i, b) \
00297 for (i = 0; b != 0; i++, b >>= 1) \
00298 if (b & 1)
00299
00300
00301 #if defined(__APPLE__)
00302
00303
00304
00305
00306 #define BSWAP32(x) ((uint32)Endian32_Swap(x))
00307 #define BSWAP16(x) ((uint16)Endian16_Swap(x))
00308 #else
00309
00314 static FORCEINLINE uint32 BSWAP32(uint32 x)
00315 {
00316 #if !defined(__ICC) && defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && __GNUC_MINOR__ >= 3))
00317
00318 return (uint32)__builtin_bswap32((int32)x);
00319 #else
00320 return ((x >> 24) & 0xFF) | ((x >> 8) & 0xFF00) | ((x << 8) & 0xFF0000) | ((x << 24) & 0xFF000000);
00321 #endif
00322 }
00323
00329 static FORCEINLINE uint16 BSWAP16(uint16 x)
00330 {
00331 return (x >> 8) | (x << 8);
00332 }
00333 #endif
00334
00335 #endif