Go to the documentation of this file.
10 #ifndef BITMATH_FUNC_HPP
11 #define BITMATH_FUNC_HPP
32 static inline uint
GB(
const T x,
const uint8 s,
const uint8 n)
34 return (x >> s) & (((T)1U << n) - 1);
57 template <
typename T,
typename U>
58 static inline T
SB(T &x,
const uint8 s,
const uint8 n,
const U d)
60 x &= (T)(~((((T)1U << n) - 1) << s));
82 template <
typename T,
typename U>
83 static inline T
AB(T &x,
const uint8 s,
const uint8 n,
const U i)
85 const T mask = ((((T)1U << n) - 1) << s);
86 x = (T)((x & ~mask) | ((x + (i << s)) & mask));
102 template <
typename T>
103 static inline bool HasBit(
const T x,
const uint8 y)
105 return (x & ((T)1U << y)) != 0;
120 template <
typename T>
121 static inline T
SetBit(T &x,
const uint8 y)
123 return x = (T)(x | ((T)1U << y));
136 #define SETBITS(x, y) ((x) |= (y))
150 template <
typename T>
151 static inline T
ClrBit(T &x,
const uint8 y)
153 return x = (T)(x & ~((T)1U << y));
166 #define CLRBITS(x, y) ((x) &= ~(y))
180 template <
typename T>
183 return x = (T)(x ^ ((T)1U << y));
188 extern const uint8
_ffb_64[64];
200 #define FIND_FIRST_BIT(x) _ffb_64[(x)]
218 if ((value & 0xFF) == 0) {
238 template <
typename T>
241 return value &= (T)(value - 1);
250 template <
typename T>
260 for (num = 0; value != 0; num++) {
261 value &= (T)(value - 1);
273 template <
typename T>
276 return value != 0 && (value & (value - 1)) == 0;
285 template <
typename T>
288 return (value & (value - 1)) == 0;
300 template <
typename T>
301 static inline T
ROL(
const T x,
const uint8 n)
303 if (n == 0)
return x;
304 return (T)(x << n | x >> (
sizeof(x) * 8 - n));
316 template <
typename T>
317 static inline T
ROR(
const T x,
const uint8 n)
319 if (n == 0)
return x;
320 return (T)(x >> n | x << (
sizeof(x) * 8 - n));
340 #define FOR_EACH_SET_BIT_EX(Tbitpos_type, bitpos_var, Tbitset_type, bitset_value) \
342 Tbitset_type ___FESBE_bits = (bitpos_var = (Tbitpos_type)0, bitset_value); \
343 ___FESBE_bits != (Tbitset_type)0; \
344 ___FESBE_bits = (Tbitset_type)(___FESBE_bits >> 1), bitpos_var++ \
346 if ((___FESBE_bits & 1) != 0)
361 #define FOR_EACH_SET_BIT(bitpos_var, bitset_value) FOR_EACH_SET_BIT_EX(uint, bitpos_var, uint, bitset_value)
363 #if defined(__APPLE__)
368 # define BSWAP32(x) (static_cast<uint32>(CFSwapInt32(x)))
369 # define BSWAP16(x) (static_cast<uint16>(CFSwapInt16(x)))
370 #elif defined(_MSC_VER)
372 # define BSWAP32(x) (_byteswap_ulong(x))
373 # define BSWAP16(x) (_byteswap_ushort(x))
382 #if !defined(__ICC) && defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && __GNUC_MINOR__ >= 3))
384 return static_cast<uint32
>(__builtin_bswap32(
static_cast<int32
>(x)));
386 return ((x >> 24) & 0xFF) | ((x >> 8) & 0xFF00) | ((x << 8) & 0xFF0000) | ((x << 24) & 0xFF000000);
397 return (x >> 8) | (x << 8);
static bool HasAtMostOneBit(T value)
Test whether value has at most 1 bit set.
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
static T KillFirstBit(T value)
Clear the first bit in an integer.
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
static T ClrBit(T &x, const uint8 y)
Clears a bit in a variable.
static T AB(T &x, const uint8 s, const uint8 n, const U i)
Add i to n bits of x starting at bit s.
static uint CountBits(T value)
Counts the number of set bits in a variable.
static uint16 BSWAP16(uint16 x)
Perform a 16 bits endianness bitswap on x.
static bool HasExactlyOneBit(T value)
Test whether value has exactly 1 bit set.
static T SB(T &x, const uint8 s, const uint8 n, const U d)
Set n bits in x starting at bit s to d.
static uint8 FindFirstBit2x64(const int value)
Finds the position of the first non-zero bit in an integer.
uint8 FindFirstBit(uint32 x)
Search the first set bit in a 32 bit variable.
static T ROL(const T x, const uint8 n)
ROtate x Left by n.
static uint32 BSWAP32(uint32 x)
Perform a 32 bits endianness bitswap on x.
uint8 FindLastBit(uint64 x)
Search the last set bit in a 64 bit variable.
const uint8 _ffb_64[64]
Lookup table to check which bit is set in a 6 bit variable.
static T ToggleBit(T &x, const uint8 y)
Toggles a bit in a variable.
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
static T ROR(const T x, const uint8 n)
ROtate x Right by n.
#define FIND_FIRST_BIT(x)
Returns the first non-zero bit in a 6-bit value (from right).