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... | |
Macros | |
#define | BITFIELD_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) |
All the bitfield functions are pure (they do not modify their arguments), so the result must be used. 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 | |
BITFIELD_WARN_UNUSED_RESULT uint32_t | bitfield_field32_read (uint32_t bitfield, bitfield_field32_t field) |
Reads a value from field in bitfield . More... | |
BITFIELD_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... | |
BITFIELD_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... | |
BITFIELD_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... | |
BITFIELD_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... | |
BITFIELD_WARN_UNUSED_RESULT int32_t | bitfield_find_first_set32 (int32_t bitfield) |
Find First Set Bit. More... | |
BITFIELD_WARN_UNUSED_RESULT int32_t | bitfield_count_leading_zeroes32 (uint32_t bitfield) |
Count Leading Zeroes. More... | |
BITFIELD_WARN_UNUSED_RESULT int32_t | bitfield_count_trailing_zeroes32 (uint32_t bitfield) |
Count Trailing Zeroes. More... | |
BITFIELD_WARN_UNUSED_RESULT int32_t | bitfield_popcount32 (uint32_t bitfield) |
Count Set Bits. More... | |
BITFIELD_WARN_UNUSED_RESULT int32_t | bitfield_parity32 (uint32_t bitfield) |
Parity. More... | |
BITFIELD_WARN_UNUSED_RESULT uint32_t | bitfield_byteswap32 (uint32_t bitfield) |
Byte Swap. 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 39 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. |
#define BITFIELD_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) |
All the bitfield functions are pure (they do not modify their arguments), so the result must be used.
We enable warnings to ensure this happens.
Definition at line 24 of file bitfield.h.
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 97 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 |
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 122 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 107 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 137 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 277 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 186 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 210 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 58 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 76 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 162 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 254 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 232 of file bitfield.h.