Software APIs
Data Structures | Typedefs | Functions
bitfield.h File Reference

(ae68723)

Bitfield Manipulation Functions. More...

#include <stdbool.h>
#include <stdint.h>
#include "sw/device/lib/base/macros.h"

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

Detailed Description

Bitfield Manipulation Functions.

Definition in file bitfield.h.


Data Structure Documentation

◆ bitfield_field32

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 Documentation

◆ bitfield_bit32_index_t

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.

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

Function Documentation

◆ bitfield_bit32_copy()

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 
)
inline

Copies a bit from one bit set to the other.

Parameters
destBitfield to write to.
dest_bitBit to write to.
srcBitfield to read from.
src_bitBit to read from.
Returns
dest with the copied bit applied.

Definition at line 151 of file bitfield.h.

◆ bitfield_bit32_read()

OT_WARN_UNUSED_RESULT bool bitfield_bit32_read ( uint32_t  bitfield,
bitfield_bit32_index_t  bit_index 
)
inline

Reads the bit_indexth bit in bitfield.

Parameters
bitfieldBitfield to get the bit from.
bit_indexBit to read.
Returns
true if the bit was one, false otherwise.

Definition at line 119 of file bitfield.h.

◆ bitfield_bit32_to_field32()

OT_WARN_UNUSED_RESULT bitfield_field32_t bitfield_bit32_to_field32 ( bitfield_bit32_index_t  bit_index)
inline

Turns a bitfield_bit32_index_t into a bitfield_field32_t (which is more general).

Parameters
bit_indexThe corresponding single bit to turn into a field.
Returns
A 1-bit field that corresponds to bit_index.

Definition at line 103 of file bitfield.h.

◆ bitfield_bit32_write()

OT_WARN_UNUSED_RESULT uint32_t bitfield_bit32_write ( uint32_t  bitfield,
bitfield_bit32_index_t  bit_index,
bool  value 
)
inline

Writes value to the bit_indexth bit in bitfield.

Parameters
bitfieldBitfield to update the bit in.
bit_indexBit to update.
valueBit value to write to bitfield.
Returns
bitfield with the bit_indexth bit set to value.

Definition at line 134 of file bitfield.h.

◆ bitfield_byteswap32()

OT_WARN_UNUSED_RESULT uint32_t bitfield_byteswap32 ( uint32_t  bitfield)
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.

Parameters
bitfieldBitfield to reverse bytes of.
Returns
bitfield with the order of bytes reversed.

Definition at line 292 of file bitfield.h.

◆ bitfield_count_leading_zeroes32()

OT_WARN_UNUSED_RESULT int32_t bitfield_count_leading_zeroes32 ( uint32_t  bitfield)
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.

Parameters
bitfieldBitfield to count leading 0-bits from.
Returns
The number of leading 0-bits in bitfield.

Definition at line 201 of file bitfield.h.

◆ bitfield_count_trailing_zeroes32()

OT_WARN_UNUSED_RESULT int32_t bitfield_count_trailing_zeroes32 ( uint32_t  bitfield)
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.

Parameters
bitfieldBitfield to count trailing 0-bits from.
Returns
The number of trailing 0-bits in bitfield.

Definition at line 225 of file bitfield.h.

◆ bitfield_field32_read()

OT_WARN_UNUSED_RESULT uint32_t bitfield_field32_read ( uint32_t  bitfield,
bitfield_field32_t  field 
)
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.

Parameters
bitfieldBitfield to get the field from.
fieldField to read out from.
Returns
Zero-extended field from bitfield.

Definition at line 54 of file bitfield.h.

◆ bitfield_field32_write()

OT_WARN_UNUSED_RESULT uint32_t bitfield_field32_write ( uint32_t  bitfield,
bitfield_field32_t  field,
uint32_t  value 
)
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.

Parameters
bitfieldBitfield to set the field in.
fieldField within bitfield to be set.
valueValue for the new field.
Returns
bitfield with field set to value.

Definition at line 72 of file bitfield.h.

◆ bitfield_find_first_set32()

OT_WARN_UNUSED_RESULT int32_t bitfield_find_first_set32 ( int32_t  bitfield)
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.

Parameters
bitfieldBitfield to find the first set bit in.
Returns
One plus the index of the least-significant 1-bit of bitfield.

Definition at line 177 of file bitfield.h.

◆ bitfield_is_power_of_two32()

OT_WARN_UNUSED_RESULT bool bitfield_is_power_of_two32 ( uint32_t  bitfield)
inline

Check whether the bitfield value is power of two aligned.

Zero will also return false.

Parameters
bitfieldValue to be verified.
Returns
True if bitfield is power of two, otherwise false.

Definition at line 305 of file bitfield.h.

◆ bitfield_parity32()

OT_WARN_UNUSED_RESULT int32_t bitfield_parity32 ( uint32_t  bitfield)
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.

Parameters
bitfieldBitfield to count 1-bits from.
Returns
The number of 1-bits in bitfield, modulo 2.

Definition at line 269 of file bitfield.h.

◆ bitfield_popcount32()

OT_WARN_UNUSED_RESULT int32_t bitfield_popcount32 ( uint32_t  bitfield)
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.

Parameters
bitfieldBitfield to count 1-bits from.
Returns
The number of 1-bits in bitfield.

Definition at line 247 of file bitfield.h.