Loading...
Searching...
No Matches
bitfield.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 INRIA
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser
5 * General Public License v2.1. See the file LICENSE in the top level
6 * directory for more details.
7 */
8
9#pragma once
10
29
30#include <stdint.h>
31#include <stdbool.h>
32#include <stddef.h>
33#include <string.h>
34#include "irq.h"
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
46#define BITFIELD(NAME, SIZE) uint8_t NAME[((SIZE) + 7) / 8]
47
54static inline void bf_set(uint8_t field[], size_t idx)
55{
56 field[idx / 8] |= (1u << (7 - (idx % 8)));
57}
58
65static inline void bf_set_atomic(uint8_t field[], size_t idx)
66{
67 unsigned state = irq_disable();
68 bf_set(field, idx);
69 irq_restore(state);
70}
71
78static inline void bf_unset(uint8_t field[], size_t idx)
79{
80 field[idx / 8] &= ~(1u << (7 - (idx % 8)));
81}
82
89static inline void bf_unset_atomic(uint8_t field[], size_t idx)
90{
91 unsigned state = irq_disable();
92 bf_unset(field, idx);
93 irq_restore(state);
94}
95
102static inline void bf_toggle(uint8_t field[], size_t idx)
103{
104 field[idx / 8] ^= (1u << (7 - (idx % 8)));
105}
106
113static inline void bf_toggle_atomic(uint8_t field[], size_t idx)
114{
115 unsigned state = irq_disable();
116 bf_toggle(field, idx);
117 irq_restore(state);
118}
119
126static inline bool bf_isset(const uint8_t field[], size_t idx)
127{
128 return (field[idx / 8] & (1u << (7 - (idx % 8))));
129}
130
145static inline void bf_or(uint8_t out[], const uint8_t a[], const uint8_t b[], size_t len)
146{
147 len = (len + 7) / 8;
148 while (len--) {
149 out[len] = a[len] | b[len];
150 }
151}
152
167static inline void bf_or_atomic(uint8_t out[], const uint8_t a[],
168 const uint8_t b[], size_t len)
169{
170 unsigned state = irq_disable();
171 bf_or(out, a, b, len);
172 irq_restore(state);
173}
174
189static inline void bf_and(uint8_t out[], const uint8_t a[], const uint8_t b[], size_t len)
190{
191 len = (len + 7) / 8;
192 while (len--) {
193 out[len] = a[len] & b[len];
194 }
195}
196
211static inline void bf_and_atomic(uint8_t out[], const uint8_t a[],
212 const uint8_t b[], size_t len)
213{
214 unsigned state = irq_disable();
215 bf_and(out, a, b, len);
216 irq_restore(state);
217}
218
233static inline void bf_xor(uint8_t out[], const uint8_t a[], const uint8_t b[], size_t len)
234{
235 len = (len + 7) / 8;
236 while (len--) {
237 out[len] = a[len] ^ b[len];
238 }
239}
240
255static inline void bf_xor_atomic(uint8_t out[], const uint8_t a[],
256 const uint8_t b[], size_t len)
257{
258 unsigned state = irq_disable();
259 bf_xor(out, a, b, len);
260 irq_restore(state);
261}
262
276static inline void bf_inv(uint8_t out[], const uint8_t a[], size_t len)
277{
278 len = (len + 7) / 8;
279 while (len--) {
280 out[len] = ~a[len];
281 }
282}
283
297static inline void bf_inv_atomic(uint8_t out[], const uint8_t a[], size_t len)
298{
299 unsigned state = irq_disable();
300 bf_inv(out, a, len);
301 irq_restore(state);
302}
303
315int bf_get_unset(uint8_t field[], size_t len);
316
326int bf_find_first_set(const uint8_t field[], size_t size);
327
337int bf_find_first_unset(const uint8_t field[], size_t size);
338
345void bf_set_all(uint8_t field[], size_t size);
346
353static inline void bf_set_all_atomic(uint8_t field[], size_t size)
354{
355 unsigned state = irq_disable();
356 bf_set_all(field, size);
357 irq_restore(state);
358}
359
366void bf_clear_all(uint8_t field[], size_t size);
367
374static inline void bf_clear_all_atomic(uint8_t field[], size_t size)
375{
376 unsigned state = irq_disable();
377 bf_clear_all(field, size);
378 irq_restore(state);
379}
380
389unsigned bf_popcnt(const uint8_t field[], size_t size);
390
391#ifdef __cplusplus
392}
393#endif
394
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:113
static void bf_set(uint8_t field[], size_t idx)
Set the bit to 1.
Definition bitfield.h:54
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:297
static void bf_unset_atomic(uint8_t field[], size_t idx)
Atomically clear the bit.
Definition bitfield.h:89
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:167
static void bf_clear_all_atomic(uint8_t field[], size_t size)
Atomically clear all bits in the bitfield to 0.
Definition bitfield.h:374
static void bf_set_all_atomic(uint8_t field[], size_t size)
Atomically set all bits in the bitfield to 1.
Definition bitfield.h:353
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:211
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:233
static void bf_set_atomic(uint8_t field[], size_t idx)
Atomically set the bit to 1.
Definition bitfield.h:65
static void bf_toggle(uint8_t field[], size_t idx)
Toggle the bit.
Definition bitfield.h:102
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:189
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:255
static void bf_unset(uint8_t field[], size_t idx)
Clear the bit.
Definition bitfield.h:78
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:145
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:276
static bool bf_isset(const uint8_t field[], size_t idx)
Check if the bet is set.
Definition bitfield.h:126
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.