Loading...
Searching...
No Matches
bitfield.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2015 INRIA
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
26
27#include <stdint.h>
28#include <stdbool.h>
29#include <stddef.h>
30#include <string.h>
31#include "irq.h"
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
43#define BITFIELD(NAME, SIZE) uint8_t NAME[((SIZE) + 7) / 8]
44
51static inline void bf_set(uint8_t field[], size_t idx)
52{
53 field[idx / 8] |= (1u << (7 - (idx % 8)));
54}
55
62static inline void bf_set_atomic(uint8_t field[], size_t idx)
63{
64 unsigned state = irq_disable();
65 bf_set(field, idx);
66 irq_restore(state);
67}
68
75static inline void bf_unset(uint8_t field[], size_t idx)
76{
77 field[idx / 8] &= ~(1u << (7 - (idx % 8)));
78}
79
86static inline void bf_unset_atomic(uint8_t field[], size_t idx)
87{
88 unsigned state = irq_disable();
89 bf_unset(field, idx);
90 irq_restore(state);
91}
92
99static inline void bf_toggle(uint8_t field[], size_t idx)
100{
101 field[idx / 8] ^= (1u << (7 - (idx % 8)));
102}
103
110static inline void bf_toggle_atomic(uint8_t field[], size_t idx)
111{
112 unsigned state = irq_disable();
113 bf_toggle(field, idx);
114 irq_restore(state);
115}
116
123static inline bool bf_isset(const uint8_t field[], size_t idx)
124{
125 return (field[idx / 8] & (1u << (7 - (idx % 8))));
126}
127
142static inline void bf_or(uint8_t out[], const uint8_t a[], const uint8_t b[], size_t len)
143{
144 len = (len + 7) / 8;
145 while (len--) {
146 out[len] = a[len] | b[len];
147 }
148}
149
164static inline void bf_or_atomic(uint8_t out[], const uint8_t a[],
165 const uint8_t b[], size_t len)
166{
167 unsigned state = irq_disable();
168 bf_or(out, a, b, len);
169 irq_restore(state);
170}
171
186static inline void bf_and(uint8_t out[], const uint8_t a[], const uint8_t b[], size_t len)
187{
188 len = (len + 7) / 8;
189 while (len--) {
190 out[len] = a[len] & b[len];
191 }
192}
193
208static inline void bf_and_atomic(uint8_t out[], const uint8_t a[],
209 const uint8_t b[], size_t len)
210{
211 unsigned state = irq_disable();
212 bf_and(out, a, b, len);
213 irq_restore(state);
214}
215
230static inline void bf_xor(uint8_t out[], const uint8_t a[], const uint8_t b[], size_t len)
231{
232 len = (len + 7) / 8;
233 while (len--) {
234 out[len] = a[len] ^ b[len];
235 }
236}
237
252static inline void bf_xor_atomic(uint8_t out[], const uint8_t a[],
253 const uint8_t b[], size_t len)
254{
255 unsigned state = irq_disable();
256 bf_xor(out, a, b, len);
257 irq_restore(state);
258}
259
273static inline void bf_inv(uint8_t out[], const uint8_t a[], size_t len)
274{
275 len = (len + 7) / 8;
276 while (len--) {
277 out[len] = ~a[len];
278 }
279}
280
294static inline void bf_inv_atomic(uint8_t out[], const uint8_t a[], size_t len)
295{
296 unsigned state = irq_disable();
297 bf_inv(out, a, len);
298 irq_restore(state);
299}
300
312int bf_get_unset(uint8_t field[], size_t len);
313
323int bf_find_first_set(const uint8_t field[], size_t size);
324
334int bf_find_first_unset(const uint8_t field[], size_t size);
335
342void bf_set_all(uint8_t field[], size_t size);
343
350static inline void bf_set_all_atomic(uint8_t field[], size_t size)
351{
352 unsigned state = irq_disable();
353 bf_set_all(field, size);
354 irq_restore(state);
355}
356
363void bf_clear_all(uint8_t field[], size_t size);
364
371static inline void bf_clear_all_atomic(uint8_t field[], size_t size)
372{
373 unsigned state = irq_disable();
374 bf_clear_all(field, size);
375 irq_restore(state);
376}
377
386unsigned bf_popcnt(const uint8_t field[], size_t size);
387
388#ifdef __cplusplus
389}
390#endif
391
MAYBE_INLINE void irq_restore(unsigned state)
This function restores the IRQ disable bit in the status register to the value contained within passe...
MAYBE_INLINE unsigned irq_disable(void)
This function sets the IRQ disable bit in the status register.
static void bf_toggle_atomic(uint8_t field[], size_t idx)
Atomically toggle the bit.
Definition bitfield.h:110
static void bf_set(uint8_t field[], size_t idx)
Set the bit to 1.
Definition bitfield.h:51
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
Definition bitfield.h:294
static void bf_unset_atomic(uint8_t field[], size_t idx)
Atomically clear the bit.
Definition bitfield.h:86
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
Definition bitfield.h:164
static void bf_clear_all_atomic(uint8_t field[], size_t size)
Atomically clear all bits in the bitfield to 0.
Definition bitfield.h:371
static void bf_set_all_atomic(uint8_t field[], size_t size)
Atomically set all bits in the bitfield to 1.
Definition bitfield.h:350
int bf_find_first_unset(const uint8_t field[], size_t size)
Get the index of the zero bit in the field.
void bf_clear_all(uint8_t field[], size_t size)
Clear all bits in the bitfield to 0.
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
Definition bitfield.h:208
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
Definition bitfield.h:230
static void bf_set_atomic(uint8_t field[], size_t idx)
Atomically set the bit to 1.
Definition bitfield.h:62
static void bf_toggle(uint8_t field[], size_t idx)
Toggle the bit.
Definition bitfield.h:99
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
Definition bitfield.h:186
int bf_find_first_set(const uint8_t field[], size_t size)
Get the index of the first set bit in the field.
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
Definition bitfield.h:252
static void bf_unset(uint8_t field[], size_t idx)
Clear the bit.
Definition bitfield.h:75
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
Definition bitfield.h:142
unsigned bf_popcnt(const uint8_t field[], size_t size)
Count set bits in the bitfield.
static void bf_inv(uint8_t out[], const uint8_t a[], size_t len)
Perform a bitwise NOT operation on a bitfield out = ~a
Definition bitfield.h:273
static bool bf_isset(const uint8_t field[], size_t idx)
Check if the bet is set.
Definition bitfield.h:123
void bf_set_all(uint8_t field[], size_t size)
Set all bits in the bitfield to 1.
int bf_get_unset(uint8_t field[], size_t len)
Atomically get the number of an unset bit and set it.
IRQ driver interface.