Loading...
Searching...
No Matches
bitfield.h File Reference

bitfields operations on bitfields of arbitrary length More...

Detailed Description

bitfields operations on bitfields of arbitrary length

Note
Code taken mostly from Stackoverflow, User Christoph
Author
Oliver Hahm olive.nosp@m.r.ha.nosp@m.hm@in.nosp@m.ria..nosp@m.fr

Definition in file bitfield.h.

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include "irq.h"
+ Include dependency graph for bitfield.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

#define BITFIELD(NAME, SIZE)   uint8_t NAME[((SIZE) + 7) / 8]
 Declare a bitfield of a given size.
 
static void bf_set (uint8_t field[], size_t idx)
 Set the bit to 1.
 
static void bf_set_atomic (uint8_t field[], size_t idx)
 Atomically set the bit to 1.
 
static void bf_unset (uint8_t field[], size_t idx)
 Clear the bit.
 
static void bf_unset_atomic (uint8_t field[], size_t idx)
 Atomically clear the bit.
 
static void bf_toggle (uint8_t field[], size_t idx)
 Toggle the bit.
 
static void bf_toggle_atomic (uint8_t field[], size_t idx)
 Atomically toggle the bit.
 
static bool bf_isset (const uint8_t field[], size_t idx)
 Check if the bet is set.
 
static void bf_or (uint8_t out[], const uint8_t a[], const uint8_t b[], size_t len)
 Perform a bitwise OR operation on two bitfields out = a | b
 
static void bf_or_atomic (uint8_t out[], const uint8_t a[], const uint8_t b[], size_t len)
 Atomically perform a bitwise OR operation on two bitfields out = a | b
 
static void bf_and (uint8_t out[], const uint8_t a[], const uint8_t b[], size_t len)
 Perform a bitwise AND operation on two bitfields out = a & b
 
static void bf_and_atomic (uint8_t out[], const uint8_t a[], const uint8_t b[], size_t len)
 Atomically perform a bitwise AND operation on two bitfields out = a & b
 
static void bf_xor (uint8_t out[], const uint8_t a[], const uint8_t b[], size_t len)
 Perform a bitwise XOR operation on two bitfields out = a ^ b
 
static void bf_xor_atomic (uint8_t out[], const uint8_t a[], const uint8_t b[], size_t len)
 Atomically perform a bitwise XOR operation on two bitfields out = a ^ b
 
static void bf_inv (uint8_t out[], const uint8_t a[], size_t len)
 Perform a bitwise NOT operation on a bitfield out = ~a
 
static void bf_inv_atomic (uint8_t out[], const uint8_t a[], size_t len)
 Atomically perform a bitwise NOT operation on a bitfield out = ~a
 
int bf_get_unset (uint8_t field[], size_t len)
 Atomically get the number of an unset bit and set it.
 
int bf_find_first_set (const uint8_t field[], size_t size)
 Get the index of the first set bit in the field.
 
int bf_find_first_unset (const uint8_t field[], size_t size)
 Get the index of the zero bit in the field.
 
void bf_set_all (uint8_t field[], size_t size)
 Set all bits in the bitfield to 1.
 
static void bf_set_all_atomic (uint8_t field[], size_t size)
 Atomically set all bits in the bitfield to 1.
 
void bf_clear_all (uint8_t field[], size_t size)
 Clear all bits in the bitfield to 0.
 
static void bf_clear_all_atomic (uint8_t field[], size_t size)
 Atomically clear all bits in the bitfield to 0.
 
unsigned bf_popcnt (const uint8_t field[], size_t size)
 Count set bits in the bitfield.
 

Macro Definition Documentation

◆ BITFIELD

#define BITFIELD (   NAME,
  SIZE 
)    uint8_t NAME[((SIZE) + 7) / 8]

Declare a bitfield of a given size.

Note
SIZE should be a constant expression. This avoids variable length arrays.

Definition at line 48 of file bitfield.h.

Function Documentation

◆ bf_and()

static void bf_and ( uint8_t  out[],
const uint8_t  a[],
const uint8_t  b[],
size_t  len 
)
inlinestatic

Perform a bitwise AND operation on two bitfields out = a & b

Precondition
The size of a, b and out must be at least len bits
Note
This operation will also affect unused bits of the bytes that make up the bitfield.
Parameters
[out]outThe resulting bitfield
[in]aThe first bitfield
[in]bThe second bitfield
[in]lenThe number of bits in the bitfields

Definition at line 191 of file bitfield.h.

◆ bf_and_atomic()

static void bf_and_atomic ( uint8_t  out[],
const uint8_t  a[],
const uint8_t  b[],
size_t  len 
)
inlinestatic

Atomically perform a bitwise AND operation on two bitfields out = a & b

Precondition
The size of a, b and out must be at least len bits
Note
This operation will also affect unused bits of the bytes that make up the bitfield.
Parameters
[out]outThe resulting bitfield
[in]aThe first bitfield
[in]bThe second bitfield
[in]lenThe number of bits in the bitfields

Definition at line 213 of file bitfield.h.

◆ bf_clear_all()

void bf_clear_all ( uint8_t  field[],
size_t  size 
)

Clear all bits in the bitfield to 0.

Parameters
[in]fieldThe bitfield
[in]sizeThe number of bits in the bitfield

◆ bf_clear_all_atomic()

static void bf_clear_all_atomic ( uint8_t  field[],
size_t  size 
)
inlinestatic

Atomically clear all bits in the bitfield to 0.

Parameters
[in]fieldThe bitfield
[in]sizeThe number of bits in the bitfield

Definition at line 376 of file bitfield.h.

◆ bf_find_first_set()

int bf_find_first_set ( const uint8_t  field[],
size_t  size 
)

Get the index of the first set bit in the field.

Parameters
[in]fieldThe bitfield
[in]sizeThe number of bits in the bitfield
Returns
number of the first set bit
-1 if no bit is set

◆ bf_find_first_unset()

int bf_find_first_unset ( const uint8_t  field[],
size_t  size 
)

Get the index of the zero bit in the field.

Parameters
[in]fieldThe bitfield
[in]sizeThe number of bits in the bitfield
Returns
number of the first unset bit
-1 if all bits are set

◆ bf_get_unset()

int bf_get_unset ( uint8_t  field[],
size_t  len 
)

Atomically get the number of an unset bit and set it.

This function can be used to record e.g., empty entries in an array.

Parameters
[in,out]fieldThe bitfield
[in]lenThe number of bits in the bitfield to consider
Returns
number of bit that was set
-1 if no bit was unset

◆ bf_inv()

static void bf_inv ( uint8_t  out[],
const uint8_t  a[],
size_t  len 
)
inlinestatic

Perform a bitwise NOT operation on a bitfield out = ~a

Precondition
The size of a and out must be at least len bits
Note
This operation will also affect unused bits of the bytes that make up the bitfield.
Parameters
[out]outThe resulting bitfield
[in]aThe bitfield to invert
[in]lenThe number of bits in the bitfield

Definition at line 278 of file bitfield.h.

◆ bf_inv_atomic()

static void bf_inv_atomic ( uint8_t  out[],
const uint8_t  a[],
size_t  len 
)
inlinestatic

Atomically perform a bitwise NOT operation on a bitfield out = ~a

Precondition
The size of a and out must be at least len bits
Note
This operation will also affect unused bits of the bytes that make up the bitfield.
Parameters
[out]outThe resulting bitfield
[in]aThe bitfield to invert
[in]lenThe number of bits in the bitfield

Definition at line 299 of file bitfield.h.

◆ bf_isset()

static bool bf_isset ( const uint8_t  field[],
size_t  idx 
)
inlinestatic

Check if the bet is set.

Parameters
[in,out]fieldThe bitfield
[in]idxThe number of the bit to check

Definition at line 128 of file bitfield.h.

◆ bf_or()

static void bf_or ( uint8_t  out[],
const uint8_t  a[],
const uint8_t  b[],
size_t  len 
)
inlinestatic

Perform a bitwise OR operation on two bitfields out = a | b

Precondition
The size of a, b and out must be at least len bits
Note
This operation will also affect unused bits of the bytes that make up the bitfield.
Parameters
[out]outThe resulting bitfield
[in]aThe first bitfield
[in]bThe second bitfield
[in]lenThe number of bits in the bitfields

Definition at line 147 of file bitfield.h.

◆ bf_or_atomic()

static void bf_or_atomic ( uint8_t  out[],
const uint8_t  a[],
const uint8_t  b[],
size_t  len 
)
inlinestatic

Atomically perform a bitwise OR operation on two bitfields out = a | b

Precondition
The size of a, b and out must be at least len bits
Note
This operation will also affect unused bits of the bytes that make up the bitfield.
Parameters
[out]outThe resulting bitfield
[in]aThe first bitfield
[in]bThe second bitfield
[in]lenThe number of bits in the bitfields

Definition at line 169 of file bitfield.h.

◆ bf_popcnt()

unsigned bf_popcnt ( const uint8_t  field[],
size_t  size 
)

Count set bits in the bitfield.

Parameters
[in]fieldThe bitfield
[in]sizeThe number of bits in the bitfield
Returns
number of '1' bits in the bitfield

◆ bf_set()

static void bf_set ( uint8_t  field[],
size_t  idx 
)
inlinestatic

Set the bit to 1.

Parameters
[in,out]fieldThe bitfield
[in]idxThe number of the bit to set

Definition at line 56 of file bitfield.h.

◆ bf_set_all()

void bf_set_all ( uint8_t  field[],
size_t  size 
)

Set all bits in the bitfield to 1.

Parameters
[in]fieldThe bitfield
[in]sizeThe number of bits in the bitfield

◆ bf_set_all_atomic()

static void bf_set_all_atomic ( uint8_t  field[],
size_t  size 
)
inlinestatic

Atomically set all bits in the bitfield to 1.

Parameters
[in]fieldThe bitfield
[in]sizeThe number of bits in the bitfield

Definition at line 355 of file bitfield.h.

◆ bf_set_atomic()

static void bf_set_atomic ( uint8_t  field[],
size_t  idx 
)
inlinestatic

Atomically set the bit to 1.

Parameters
[in,out]fieldThe bitfield
[in]idxThe number of the bit to set

Definition at line 67 of file bitfield.h.

◆ bf_toggle()

static void bf_toggle ( uint8_t  field[],
size_t  idx 
)
inlinestatic

Toggle the bit.

Parameters
[in,out]fieldThe bitfield
[in]idxThe number of the bit to toggle

Definition at line 104 of file bitfield.h.

◆ bf_toggle_atomic()

static void bf_toggle_atomic ( uint8_t  field[],
size_t  idx 
)
inlinestatic

Atomically toggle the bit.

Parameters
[in,out]fieldThe bitfield
[in]idxThe number of the bit to toggle

Definition at line 115 of file bitfield.h.

◆ bf_unset()

static void bf_unset ( uint8_t  field[],
size_t  idx 
)
inlinestatic

Clear the bit.

Parameters
[in,out]fieldThe bitfield
[in]idxThe number of the bit to clear

Definition at line 80 of file bitfield.h.

◆ bf_unset_atomic()

static void bf_unset_atomic ( uint8_t  field[],
size_t  idx 
)
inlinestatic

Atomically clear the bit.

Parameters
[in,out]fieldThe bitfield
[in]idxThe number of the bit to clear

Definition at line 91 of file bitfield.h.

◆ bf_xor()

static void bf_xor ( uint8_t  out[],
const uint8_t  a[],
const uint8_t  b[],
size_t  len 
)
inlinestatic

Perform a bitwise XOR operation on two bitfields out = a ^ b

Precondition
The size of a, b and out must be at least len bits
Note
This operation will also affect unused bits of the bytes that make up the bitfield.
Parameters
[out]outThe resulting bitfield
[in]aThe first bitfield
[in]bThe second bitfield
[in]lenThe number of bits in the bitfields

Definition at line 235 of file bitfield.h.

◆ bf_xor_atomic()

static void bf_xor_atomic ( uint8_t  out[],
const uint8_t  a[],
const uint8_t  b[],
size_t  len 
)
inlinestatic

Atomically perform a bitwise XOR operation on two bitfields out = a ^ b

Precondition
The size of a, b and out must be at least len bits
Note
This operation will also affect unused bits of the bytes that make up the bitfield.
Parameters
[out]outThe resulting bitfield
[in]aThe first bitfield
[in]bThe second bitfield
[in]lenThe number of bits in the bitfields

Definition at line 257 of file bitfield.h.