|
Embedded Artistry Framework
Embedded Systems C++ Framework
|
Functions and macros that operate in a bitwise manner. More...
|
Classes | |
| struct | embutil::countbits< tval > |
| Template struct for counting set bits and significant bits at compile-time. More... | |
Alignment Macros | |
| #define | align_up(num, align) (((num) + ((align)-1)) & ~((align)-1)) |
| Increase a number to the next aligned value. More... | |
| #define | IS_POWER_2(x) (!((x) & ((x)-1))) |
| Check if a number is a power of 2. More... | |
| #define | IS_ALIGNED(val, align) ((val & (align - 1)) == 0) |
| Check if a value is aligned. More... | |
Bitfield Macros | |
| #define | SET_BIT(x) (1 << x) |
| Set bit at position X. More... | |
| #define | EXTRACT_BITFIELD(value, high_bit, low_bit) (((value) >> (low_bit)) & N_SET_BITS((high_bit) - (low_bit) + 1)) |
| Extract a field of bits from a value. More... | |
Count Bits Macros | |
| #define | N_SET_BITS(n_ones) ((1 << (n_ones)) - 1) |
| Count the number of set bits. More... | |
Alignment Functions | |
| template<typename TIntegralType > | |
| constexpr bool | embutil::is_power_2 (TIntegralType val) noexcept |
| Check if a number is a power of 2. More... | |
| template<typename TType > | |
| constexpr bool | embutil::is_aligned (const TType val, const size_t align) noexcept |
| Checks if a value meets a specified alignment. More... | |
Bitfield Functions | |
| template<typename TIntegralType , typename... Args> | |
| constexpr TIntegralType | embutil::bitmask (Args &&... args) noexcept |
| A C++ 17 bitmask generator. More... | |
Functions and macros that operate in a bitwise manner.
| struct embutil::countbits |
Template struct for counting set bits and significant bits at compile-time.
Examples:
| tval | Unsigned integral value to analyze. |
Public Types | |
| using | t_ = countbits< tval/2 > |
Static Public Attributes | |
| static constexpr unsigned | set = ((tval & 1) != 0) ? t_::set + 1 : t_::set |
| Shows how many bits in the value are set. More... | |
| static constexpr unsigned | significant_bits = (tval != 0) ? t_::significant_bits + 1 : 0 |
| shows how many bits in the value matter More... | |
| using embutil::countbits< tval >::t_ = countbits<tval / 2> |
|
static |
Shows how many bits in the value are set.
|
static |
shows how many bits in the value matter
| #define align_up | ( | num, | |
| align | |||
| ) | (((num) + ((align)-1)) & ~((align)-1)) |
Increase a number to the next aligned value.
| num | The number to start with. |
| align | The target alignment. |
| #define EXTRACT_BITFIELD | ( | value, | |
| high_bit, | |||
| low_bit | |||
| ) | (((value) >> (low_bit)) & N_SET_BITS((high_bit) - (low_bit) + 1)) |
Extract a field of bits from a value.
Given a number, such as: 0b11010100
If we wanted to extract bits 7..5, we would supply: high_bit = 7 low_bit = 5
And the result would be 0b110
| value | The value to extract the bitfield from. |
| high_bit | the bit representing the upper limit of the bitfield. |
| low_bit | the bit representing the lower limit of the bitfield. |
| #define IS_ALIGNED | ( | val, | |
| align | |||
| ) | ((val & (align - 1)) == 0) |
Check if a value is aligned.
| val | The number to check. |
| align | The target alignment. |
| #define IS_POWER_2 | ( | x | ) | (!((x) & ((x)-1))) |
Check if a number is a power of 2.
| x | The number to check. |
| #define N_SET_BITS | ( | n_ones | ) | ((1 << (n_ones)) - 1) |
Count the number of set bits.
| n_ones | The number to check. |
| #define SET_BIT | ( | x | ) | (1 << x) |
Set bit at position X.
| x | The bit position to set. |
|
noexcept |
A C++ 17 bitmask generator.
This function was inspired by Martin Moene.
Example: bitmask<uint8_t>(6, 5, 0) -> 0b0110'0001
| TIntegralType | The desired type for the bitmask. |
| Args | A variadic list corresponding to bits to be set. Args is detected by the compiler. |
| args | The variadic list of bits to set in the bitmask. |
|
inlinenoexcept |
Checks if a value meets a specified alignment.
Behavior for integral types: check if the value is aligned.
Behavior for pointers: check if the pointer is aligned.
Behavior for non-integral non-pointer types: Take the address and determine if it is aligned.
| TType | the type of data to check the alignment of. TType can be integral, pointer, or non-integral and non-pointer. The behavior of the function changes depending on the type. |
| val | The value to check the alignment of. |
| align | The target alignment. |
References IS_ALIGNED, and r.
Referenced by os::posix::Thread::Thread().
|
noexcept |
Check if a number is a power of 2.
| TIntegralType | The type of the number to check. This type does not need to be manually specified. The compiler should automatically deduce this type. TIntegralType must be an integral type. |
| val | The number to evaluate. |
References IS_POWER_2.
1.8.15