NeKernel dev
Loading...
Searching...
No Matches
utl Namespace Reference

Functions

template<class T, size_t N>
constexpr size_t countof (T(&)[N])
 Helper to get number of elements in array.
template<class T>
constexpr auto bit_size ()
 Size of object in terms of bits.
template<class T>
constexpr T bit_full ()
 Integer with all bits set to 1.
template<class T>
constexpr T bit_wrap ()
 Wrap around mask for power of two number of bits within given integer type. For example: [ bit_wrap<uint8_t> = 8 - 1 = 0b111 ] [ bit_wrap<uint16_t> = 16 - 1 = 0b1111 ] [ bit_wrap<uint32_t> = 32 - 1 = 0b11111 ].
template<class T>
constexpr auto bit_shft ()
 Number of bits to fit bit_wrap<T> result, in other words bit width of (sizeof(T) * 8 - 1). For example: [ bit_shft<uint8_t> = bit_width(0b111) = 3 ] [ bit_shft<uint16_t> = bit_width(0b1111) = 4 ] [ bit_shft<uint32_t> = bit_width(0b11111) = 5 ].
template<class T>
constexpr auto bit_ceil (auto x)
 Round up division by number of bits within given integer type, which sizeof(T) * 8 is power of two.
constexpr unsigned cntlz (auto x)
 Count leading zeros.
constexpr unsigned cnttz (auto x)
 Count trailing zeros.
template<class T>
constexpr size_t words_in_bits (size_t n)
 Get number of words (integers) required to store N bits.
constexpr size_t bytes_in_bits (size_t n)
 Get number of bytes required to store N bits.
template<class T = unsigned>
constexpr T bit (int n)
 Make integer with bit at given position.
template<class T>
constexpr bool get_bit (T x, int n)
 Get n-th bit of an integer.
template<class T>
constexpr void set_bit (T &x, int n)
 Set n-th bit of an integer.
template<class T>
constexpr void clr_bit (T &x, int n)
 Clear n-th bit of an integer.
template<class T>
constexpr bool get_arr_bit (const T *p, unsigned n)
 Get n-th bit in array of words (starting from LSB).
template<class T>
constexpr void set_arr_bit (T *p, unsigned n)
 Set n-th bit in array of words (starting from LSB).
template<class T>
constexpr void clr_arr_bit (T *p, unsigned n)
 Clear n-th bit in array of words (starting from LSB).
template<class T, size_t L>
constexpr void shift_left (T(&x)[L])
 Shift bits left in array of integer elements from least significant bit and considering 0-th byte as the right most. uint16_t example: 0b10000000'11100001 ==> 0b00000001'11000010.

Function Documentation

◆ bit()

template<class T = unsigned>
T utl::bit ( int n)
constexpr

Make integer with bit at given position.

Template Parameters
TInetegr type
Parameters
nBit position
Returns
Integer with set bit

◆ bit_ceil()

template<class T>
auto utl::bit_ceil ( auto x)
constexpr

Round up division by number of bits within given integer type, which sizeof(T) * 8 is power of two.

Template Parameters
TInetegr type
Parameters
xDividend
Returns
Quotient

◆ bit_full()

template<class T>
T utl::bit_full ( )
constexpr

Integer with all bits set to 1.

Template Parameters
TInteger type
Returns
All set integer

◆ bit_shft()

template<class T>
auto utl::bit_shft ( )
constexpr

Number of bits to fit bit_wrap<T> result, in other words bit width of (sizeof(T) * 8 - 1). For example: [ bit_shft<uint8_t> = bit_width(0b111) = 3 ] [ bit_shft<uint16_t> = bit_width(0b1111) = 4 ] [ bit_shft<uint32_t> = bit_width(0b11111) = 5 ].

Template Parameters
TInteger type
Returns
Number of bits to shift to divide by sizeof(T) * 8

◆ bit_size()

template<class T>
auto utl::bit_size ( )
constexpr

Size of object in terms of bits.

Template Parameters
TObject type
Returns
Number of bits

◆ bit_wrap()

template<class T>
T utl::bit_wrap ( )
constexpr

Wrap around mask for power of two number of bits within given integer type. For example: [ bit_wrap<uint8_t> = 8 - 1 = 0b111 ] [ bit_wrap<uint16_t> = 16 - 1 = 0b1111 ] [ bit_wrap<uint32_t> = 32 - 1 = 0b11111 ].

Template Parameters
TInteger type
Returns
Wrap around mask for number of bits

◆ bytes_in_bits()

size_t utl::bytes_in_bits ( size_t n)
constexpr

Get number of bytes required to store N bits.

Parameters
nNumber of bits to store
Returns
Number of bytes

◆ clr_arr_bit()

template<class T>
void utl::clr_arr_bit ( T * p,
unsigned n )
constexpr

Clear n-th bit in array of words (starting from LSB).

Template Parameters
TWord type
Parameters
pArray of words
nIndex of bit to clear

◆ clr_bit()

template<class T>
void utl::clr_bit ( T & x,
int n )
constexpr

Clear n-th bit of an integer.

Template Parameters
TInteger type
Parameters
xInteger
nBit position from LSB

◆ cntlz()

unsigned utl::cntlz ( auto x)
constexpr

Count leading zeros.

Parameters
xUnsigned integer argument
Returns
Number of leading zeros

◆ cnttz()

unsigned utl::cnttz ( auto x)
constexpr

Count trailing zeros.

Parameters
xUnsigned integer argument
Returns
Number of trailing zeros

◆ countof()

template<class T, size_t N>
size_t utl::countof ( T(&) [N])
constexpr

Helper to get number of elements in array.

Template Parameters
TAuto-deduced element type
NAuto-deduced number of elements
Returns
Array size

◆ get_arr_bit()

template<class T>
bool utl::get_arr_bit ( const T * p,
unsigned n )
constexpr

Get n-th bit in array of words (starting from LSB).

Template Parameters
TWord type
Parameters
pArray of words
nIndex of bit to get
Returns
true if set

◆ get_bit()

template<class T>
bool utl::get_bit ( T x,
int n )
constexpr

Get n-th bit of an integer.

Template Parameters
TInteger type
Parameters
xInteger
nBit position from LSB
Returns
true if set

◆ set_arr_bit()

template<class T>
void utl::set_arr_bit ( T * p,
unsigned n )
constexpr

Set n-th bit in array of words (starting from LSB).

Template Parameters
TWord type
Parameters
pArray of words
nIndex of bit to set

◆ set_bit()

template<class T>
void utl::set_bit ( T & x,
int n )
constexpr

Set n-th bit of an integer.

Template Parameters
TInteger type
Parameters
xInteger
nBit position from LSB

◆ shift_left()

template<class T, size_t L>
void utl::shift_left ( T(&) x[L])
constexpr

Shift bits left in array of integer elements from least significant bit and considering 0-th byte as the right most. uint16_t example: 0b10000000'11100001 ==> 0b00000001'11000010.

Template Parameters
TInteger type
LLength of array
Parameters
xArray of integers, nullptr not acceptable!
lenNumber of elements

◆ words_in_bits()

template<class T>
size_t utl::words_in_bits ( size_t n)
constexpr

Get number of words (integers) required to store N bits.

Template Parameters
TWord integer type
Parameters
nNumber of bits to store
Returns
Number of words