Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
bits.h File Reference
#include "lib/code_annotation.h"
#include "lib/debug.h"
#include "lib/status.h"
#include <bit>
#include <climits>
#include <cstddef>
#include <limits>
#include <type_traits>
Include dependency graph for bits.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define BIT(n)   (1u << (n))
 pretty much the same as Bit<unsigned>. More...
 
#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 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)
 

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.

◆ 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.

◆ IsBitSet()

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

◆ round_down()

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

◆ 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.