Pyrogenesis  trunk
Classes | Macros | Functions
bits.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  CeilLog2< N >
 
struct  CeilLog2< 1 >
 
struct  CeilLog2< 0 >
 

Macros

#define BIT(n)   (1u << (n))
 pretty much the same as Bit<unsigned>. More...
 
#define IS_POW2(n)   (((n) != 0) && ((n) & ((n)-1)) == 0)
 
#define ROUND_UP(n, multiple)   (((n) + (multiple)-1) & ~((multiple)-1))
 

Functions

template<typename T >
T Bit (size_t n)
 value of bit number <n>. More...
 
template<typename T >
bool IsBitSet (T value, size_t index)
 
template<typename T >
T bit_mask (size_t numBits)
 a mask that includes the lowest N bits More...
 
template<typename T >
T bits (T num, size_t lo_idx, size_t hi_idx)
 extract the value of bits hi_idx:lo_idx within num More...
 
template<typename T >
T SetBitsTo (T num, size_t lo_idx, size_t hi_idx, size_t value)
 set the value of bits hi_idx:lo_idx More...
 
template<typename T >
size_t SparsePopulationCount (T mask)
 
template<typename T >
static size_t PopulationCount (T x)
 
template<typename T >
bool is_pow2 (T n)
 
template<typename T >
T LeastSignificantBit (T x)
 
template<typename T >
T ClearLeastSignificantBit (T x)
 
template<typename T >
size_t ceil_log2 (T x)
 ceil(log2(x)) More...
 
int floor_log2 (const float x)
 floor(log2(f)) fast, uses the FPU normalization hardware. More...
 
template<typename T >
T round_up_to_pow2 (T x)
 round up to next larger power of two. More...
 
template<typename T >
T round_down_to_pow2 (T x)
 round down to next larger power of two. More...
 
template<typename T >
T round_up (T n, T multiple)
 round number up/down to the next given multiple. More...
 
template<typename T >
T round_down (T n, T multiple)
 
template<typename T >
T MaxPowerOfTwoDivisor (T value)
 

Macro Definition Documentation

◆ BIT

#define BIT (   n)    (1u << (n))

pretty much the same as Bit<unsigned>.

this is intended for the initialization of enum values, where a compile-time constant is required.

◆ IS_POW2

#define IS_POW2 (   n)    (((n) != 0) && ((n) & ((n)-1)) == 0)

◆ ROUND_UP

#define ROUND_UP (   n,
  multiple 
)    (((n) + (multiple)-1) & ~((multiple)-1))

Function Documentation

◆ Bit()

template<typename T >
T Bit ( size_t  n)
inline

value of bit number <n>.

Parameters
nbit index.

requirements:

  • T should be an unsigned type
  • n must be in [0, CHAR_BIT*sizeof(T)), else the result is undefined!

◆ bit_mask()

template<typename T >
T bit_mask ( size_t  numBits)
inline

a mask that includes the lowest N bits

Parameters
numBitsNumber of bits in mask.

◆ bits()

template<typename T >
T bits ( T  num,
size_t  lo_idx,
size_t  hi_idx 
)
inline

extract the value of bits hi_idx:lo_idx within num

example: bits(0x69, 2, 5) == 0x0A

Parameters
numnumber whose bits are to be extracted
lo_idxbit index of lowest bit to include
hi_idxbit index of highest bit to include
Returns
value of extracted bits.

◆ ceil_log2()

template<typename T >
size_t ceil_log2 ( T  x)
inline

ceil(log2(x))

Parameters
x(unsigned integer)
Returns
ceiling of the base-2 logarithm (i.e. rounded up) or zero if the input is zero.

◆ ClearLeastSignificantBit()

template<typename T >
T ClearLeastSignificantBit ( T  x)
inline

◆ floor_log2()

int floor_log2 ( const float  x)

floor(log2(f)) fast, uses the FPU normalization hardware.

Parameters
x(float) input; MUST be > 0, else results are undefined.
Returns
floor of the base-2 logarithm (i.e. rounded down).

◆ is_pow2()

template<typename T >
bool is_pow2 ( T  n)
inline
Returns
whether the given number is a power of two.

◆ IsBitSet()

template<typename T >
bool IsBitSet ( T  value,
size_t  index 
)
inline

◆ LeastSignificantBit()

template<typename T >
T LeastSignificantBit ( T  x)
inline

◆ MaxPowerOfTwoDivisor()

template<typename T >
T MaxPowerOfTwoDivisor ( T  value)
inline

◆ PopulationCount()

template<typename T >
static size_t PopulationCount ( T  x)
inlinestatic
Returns
number of 1-bits in mask. execution time is logarithmic in the total number of bits. supports up to 128-bit integers (if their arithmetic operators are defined). [http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel]

◆ round_down()

template<typename T >
T round_down ( T  n,
T  multiple 
)
inline

◆ round_down_to_pow2()

template<typename T >
T round_down_to_pow2 ( T  x)
inline

round down to next larger power of two.

◆ round_up()

template<typename T >
T round_up ( T  n,
T  multiple 
)
inline

round number up/down to the next given multiple.

Parameters
nNumber to round.
multipleMust be a power of two.

◆ round_up_to_pow2()

template<typename T >
T round_up_to_pow2 ( T  x)
inline

round up to next larger power of two.

◆ SetBitsTo()

template<typename T >
T SetBitsTo ( T  num,
size_t  lo_idx,
size_t  hi_idx,
size_t  value 
)
inline

set the value of bits hi_idx:lo_idx

Parameters
lo_idxbit index of lowest bit to include
hi_idxbit index of highest bit to include
valuenew value to be assigned to these bits

◆ SparsePopulationCount()

template<typename T >
size_t SparsePopulationCount ( T  mask)
inline
Returns
number of 1-bits in mask. execution time is proportional to number of 1-bits in mask.