51#define BIT(n) (1u << (n))
56 const T bit = Bit<T>(index);
57 return (value & bit) != 0;
74 const T bitsInT =
sizeof(
T)*CHAR_BIT;
75 const T allBits = (
T)~
T(0);
77 if(numBits >= bitsInT)
81 const T mask = (
T)((
T(1) << numBits)-1);
97inline T bits(
T num,
size_t lo_idx,
size_t hi_idx)
99 const size_t numBits = (hi_idx - lo_idx)+1;
100 T result =
T(num >> lo_idx);
101 result =
T(result & bit_mask<T>(numBits));
113inline T SetBitsTo(
T num,
size_t lo_idx,
size_t hi_idx,
size_t value)
115 const size_t numBits = (hi_idx - lo_idx)+1;
116 ASSERT(value < (
T(1) << numBits));
117 const T mask = bit_mask<T>(numBits) << lo_idx;
118 T result = num & ~mask;
119 result =
T(result | (value << lo_idx));
150 cassert(!std::numeric_limits<T>::is_signed);
151 const T mask =
T(~
T(0));
152 x -= (x >> 1) & (mask/3);
153 x = (x & (mask/15*3)) + ((x >> 2) & (mask/15*3));
154 x = (x + (x >> 4)) & (mask/255*15);
155 return T(x * (mask/255)) >> ((
sizeof(
T)-1)*CHAR_BIT);
169 return (n & (n-1)) == 0;
173#define IS_POW2(n) (((n) != 0) && ((n) & ((n)-1)) == 0)
178 const T negX =
T(~x + 1);
201 while(bit < x && bit != 0)
268 const T result = (n + multiple-1) & ~(multiple-1);
269 ASSERT(n <= result && result < n+multiple);
277 const T result = n & ~(multiple-1);
278 ASSERT(result <= n && n < result+multiple);
284#define ROUND_UP(n, multiple) (((n) + (multiple)-1) & ~((multiple)-1))
292 for(
size_t log2 = 0; log2 <
sizeof(
T)*CHAR_BIT; log2++)
bool IsBitSet(T value, size_t index)
Definition: bits.h:54
T round_down_to_pow2(T x)
round down to next larger power of two.
Definition: bits.h:253
size_t ceil_log2(T x)
ceil(log2(x))
Definition: bits.h:197
T bits(T num, size_t lo_idx, size_t hi_idx)
extract the value of bits hi_idx:lo_idx within num
Definition: bits.h:97
int floor_log2(const float x)
floor(log2(f)) fast, uses the FPU normalization hardware.
Definition: bits.cpp:39
T round_up_to_pow2(T x)
round up to next larger power of two.
Definition: bits.h:244
static size_t PopulationCount(T x)
Definition: bits.h:148
T round_down(T n, T multiple)
Definition: bits.h:274
T Bit(size_t n)
value of bit number <n>.
Definition: bits.h:40
T bit_mask(size_t numBits)
a mask that includes the lowest N bits
Definition: bits.h:72
T LeastSignificantBit(T x)
Definition: bits.h:176
bool is_pow2(T n)
Definition: bits.h:164
size_t SparsePopulationCount(T mask)
Definition: bits.h:129
T round_up(T n, T multiple)
round number up/down to the next given multiple.
Definition: bits.h:265
T MaxPowerOfTwoDivisor(T value)
Definition: bits.h:288
T ClearLeastSignificantBit(T x)
Definition: bits.h:183
T SetBitsTo(T num, size_t lo_idx, size_t hi_idx, size_t value)
set the value of bits hi_idx:lo_idx
Definition: bits.h:113
#define cassert(expr)
Compile-time assertion.
Definition: code_annotation.h:209
#define ASSERT(expr)
same as ENSURE in debug mode, does nothing in release mode.
Definition: debug.h:305
#define DEBUG_WARN_ERR(status)
display the error dialog with text corresponding to the given error code.
Definition: debug.h:326
const Status LOGIC
Definition: status.h:411
def log(severity, message)
Definition: tests.py:27
#define T(string_literal)
Definition: secure_crt.cpp:77
@ value
Definition: bits.h:214