Bitfield Manipulation Functions. More...
Go to the source code of this file.
Data Structures | |
struct | bitfield_field32 |
A field of a 32-bit bitfield. More... | |
Typedefs | |
typedef struct bitfield_field32 | bitfield_field32_t |
A field of a 32-bit bitfield. More... | |
typedef uint32_t | bitfield_bit32_index_t |
A single bit in a 32-bit bitfield. More... | |
Functions | |
OT_WARN_UNUSED_RESULT uint32_t | bitfield_field32_read (uint32_t bitfield, bitfield_field32_t field) |
Reads a value from field in bitfield . More... | |
OT_WARN_UNUSED_RESULT uint32_t | bitfield_field32_write (uint32_t bitfield, bitfield_field32_t field, uint32_t value) |
Writes value to field in bitfield . More... | |
OT_WARN_UNUSED_RESULT bitfield_field32_t | bitfield_bit32_to_field32 (bitfield_bit32_index_t bit_index) |
Turns a bitfield_bit32_index_t into a bitfield_field32_t (which is more general). More... | |
OT_WARN_UNUSED_RESULT bool | bitfield_bit32_read (uint32_t bitfield, bitfield_bit32_index_t bit_index) |
Reads the bit_index th bit in bitfield . More... | |
OT_WARN_UNUSED_RESULT uint32_t | bitfield_bit32_write (uint32_t bitfield, bitfield_bit32_index_t bit_index, bool value) |
Writes value to the bit_index th bit in bitfield . More... | |
OT_WARN_UNUSED_RESULT uint32_t | bitfield_bit32_copy (uint32_t dest, bitfield_bit32_index_t dest_bit, uint32_t src, bitfield_bit32_index_t src_bit) |
Copies a bit from one bit set to the other. More... | |
OT_WARN_UNUSED_RESULT int32_t | bitfield_find_first_set32 (int32_t bitfield) |
Find First Set Bit. More... | |
OT_WARN_UNUSED_RESULT int32_t | bitfield_count_leading_zeroes32 (uint32_t bitfield) |
Count Leading Zeroes. More... | |
OT_WARN_UNUSED_RESULT int32_t | bitfield_count_trailing_zeroes32 (uint32_t bitfield) |
Count Trailing Zeroes. More... | |
OT_WARN_UNUSED_RESULT int32_t | bitfield_popcount32 (uint32_t bitfield) |
Count Set Bits. More... | |
OT_WARN_UNUSED_RESULT int32_t | bitfield_parity32 (uint32_t bitfield) |
Parity. More... | |
OT_WARN_UNUSED_RESULT uint32_t | bitfield_byteswap32 (uint32_t bitfield) |
Byte Swap. More... | |
OT_WARN_UNUSED_RESULT bool | bitfield_is_power_of_two32 (uint32_t bitfield) |
Check whether the bitfield value is power of two aligned. More... | |
Bitfield Manipulation Functions.
Definition in file bitfield.h.
struct bitfield_field32 |
A field of a 32-bit bitfield.
The following field definition: { .mask = 0b11, .index = 12 }
Denotes the X-marked bits in the following 32-bit bitfield:
field: 0b--------'--------'--XX----'-------- index: 31 0
Restrictions: The index plus the width of the mask must not be greater than 31.
Definition at line 35 of file bitfield.h.
Data Fields | ||
---|---|---|
uint32_t | index | The field position in the bitfield, counting from the zero-bit. |
uint32_t | mask |
The field mask. Usually all ones. |
typedef uint32_t bitfield_bit32_index_t |
A single bit in a 32-bit bitfield.
This denotes the position of a single bit, counting from the zero-bit.
For instance, (bitfield_bit_index_t)4
denotes the X-marked bit in the following 32-bit bitfield:
field: 0b--------'--------'--------'---X---- index: 31 0
Restrictions: The value must not be greater than 31.
Definition at line 93 of file bitfield.h.
typedef struct bitfield_field32 bitfield_field32_t |
A field of a 32-bit bitfield.
The following field definition: { .mask = 0b11, .index = 12 }
Denotes the X-marked bits in the following 32-bit bitfield:
field: 0b--------'--------'--XX----'-------- index: 31 0
Restrictions: The index plus the width of the mask must not be greater than 31.
|
inline |
Copies a bit from one bit set to the other.
dest | Bitfield to write to. |
dest_bit | Bit to write to. |
src | Bitfield to read from. |
src_bit | Bit to read from. |
dest
with the copied bit applied. Definition at line 151 of file bitfield.h.
|
inline |
Reads the bit_index
th bit in bitfield
.
bitfield | Bitfield to get the bit from. |
bit_index | Bit to read. |
true
if the bit was one, false
otherwise. Definition at line 119 of file bitfield.h.
|
inline |
Turns a bitfield_bit32_index_t
into a bitfield_field32_t
(which is more general).
bit_index | The corresponding single bit to turn into a field. |
bit_index
. Definition at line 103 of file bitfield.h.
|
inline |
Writes value
to the bit_index
th bit in bitfield
.
bitfield | Bitfield to update the bit in. |
bit_index | Bit to update. |
value | Bit value to write to bitfield . |
bitfield
with the bit_index
th bit set to value
. Definition at line 134 of file bitfield.h.
|
inline |
Byte Swap.
Returns field
with the order of the bytes reversed. Bytes here always means exactly 8 bits.
For instance, byteswap(field)
of the below 32-bit value returns 1
.
field: 0bAAAAAAAA'BBBBBBBB'CCCCCCCC'DDDDDDDD index: 31 0
returns: 0bDDDDDDDD'CCCCCCCC'BBBBBBBB'AAAAAAAA
This is the canonical definition for the GCC/Clang builtin __builtin_bswap32
.
bitfield | Bitfield to reverse bytes of. |
bitfield
with the order of bytes reversed. Definition at line 292 of file bitfield.h.
|
inline |
Count Leading Zeroes.
Returns the number of leading 0-bits in bitfield
, starting at the most significant bit position. If bitfield
is 0, the result is 32, to match the RISC-V B Extension.
For instance, bitfield_count_leading_zeroes32(field)
of the below 32-bit value returns 16
.
field: 0b00000000'00000000'11111111'00010000 index: 31 0
This is the canonical definition for the GCC/Clang builtin __builtin_clz
, and hence returns a signed integer.
bitfield | Bitfield to count leading 0-bits from. |
bitfield
. Definition at line 201 of file bitfield.h.
|
inline |
Count Trailing Zeroes.
Returns the number of trailing 0-bits in bitfield
, starting at the least significant bit position. If bitfield
is 0, the result is 32, to match the RISC-V B Extension.
For instance, bitfield_count_trailing_zeroes32(field)
of the below 32-bit value returns 4
.
field: 0b00000000'00000000'11111111'00010000 index: 31 0
This is the canonical definition for the GCC/Clang builtin __builtin_ctz
, and hence returns a signed integer.
bitfield | Bitfield to count trailing 0-bits from. |
bitfield
. Definition at line 225 of file bitfield.h.
|
inline |
Reads a value from field
in bitfield
.
This function uses the field
parameter to read the value from bitfield
. The resulting value will be shifted right and zero-extended so the field's zero-bit is the return value's zero-bit.
bitfield | Bitfield to get the field from. |
field | Field to read out from. |
field
from bitfield
. Definition at line 54 of file bitfield.h.
|
inline |
Writes value
to field
in bitfield
.
This function uses the field
parameter to set specific bits in bitfield
. The relevant portion of bitfield
is zeroed before the bits are set to value
.
bitfield | Bitfield to set the field in. |
field | Field within bitfield to be set. |
value | Value for the new field. |
bitfield
with field
set to value
. Definition at line 72 of file bitfield.h.
|
inline |
Find First Set Bit.
Returns one plus the index of the least-significant 1-bit of a 32-bit word.
For instance, bitfield_find_first_set32(field)
of the below 32-bit value returns 5
.
field: 0b00000000'00000000'11111111'00010000 index: 31 0
This is the canonical definition for the GCC/Clang builtin __builtin_ffs
, and hence takes and returns a signed integer.
bitfield | Bitfield to find the first set bit in. |
bitfield
. Definition at line 177 of file bitfield.h.
|
inline |
Check whether the bitfield value is power of two aligned.
Zero will also return false.
bitfield | Value to be verified. |
Definition at line 305 of file bitfield.h.
|
inline |
Parity.
Returns the number of 1-bits in bitfield
, modulo 2.
For instance, bitfield_parity32(field)
of the below 32-bit value returns 1
.
field: 0b00000000'00000000'11111111'00010000 index: 31 0
This is the canonical definition for the GCC/Clang builtin __builtin_parity
, and hence returns a signed integer.
bitfield | Bitfield to count 1-bits from. |
bitfield
, modulo 2. Definition at line 269 of file bitfield.h.
|
inline |
Count Set Bits.
Returns the number of 1-bits in bitfield
.
For instance, bitfield_popcount32(field)
of the below 32-bit value returns 9
.
field: 0b00000000'00000000'11111111'00010000 index: 31 0
This is the canonical definition for the GCC/Clang builtin __builtin_popcount
, and hence returns a signed integer.
bitfield | Bitfield to count 1-bits from. |
bitfield
. Definition at line 247 of file bitfield.h.