fb11e647
vrobic
reseau statique a...
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
/*
* Copyright (C) 2015 INRIA
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @defgroup sys_bitfield Bitfields
* @ingroup sys
* @brief Bitfields of arbitrary length
* @file
* @{
*
* @brief bitfields operations on bitfields of arbitrary length
*
* @note Code taken mostly from
* <a href="http://stackoverflow.com/questions/1590893/error-trying-to-define-a-1-024-bit-128-byte-bit-field">
* Stackoverflow, User Christoph</a>
*
* @author Oliver Hahm <oliver.hahm@inria.fr>
*/
#ifndef BITFIELD_H
#define BITFIELD_H
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Declare a bitfield of a given size
*
* @note SIZE should be a constant expression. This avoids variable length
* arrays.
*/
#define BITFIELD(NAME, SIZE) uint8_t NAME[((SIZE) + 7) / 8]
/**
* @brief Set the bit to 1
*
* @param[in,out] field The bitfield
* @param[in] idx The number of the bit to set
*/
static inline void bf_set(uint8_t field[], size_t idx)
{
field[idx / 8] |= (1u << (idx % 8));
}
/**
* @brief Clear the bit
*
* @param[in,out] field The bitfield
* @param[in] idx The number of the bit to clear
*/
static inline void bf_unset(uint8_t field[], size_t idx)
{
field[idx / 8] &= ~(1u << (idx % 8));
}
/**
* @brief Toggle the bit
*
* @param[in,out] field The bitfield
* @param[in] idx The number of the bit to toggle
*/
static inline void bf_toggle(uint8_t field[], size_t idx)
{
field[idx / 8] ^= (1u << (idx % 8));
}
/**
* @brief Check if the bet is set
*
* @param[in,out] field The bitfield
* @param[in] idx The number of the bit to check
*/
static inline bool bf_isset(uint8_t field[], size_t idx)
{
return (field[idx / 8] & (1u << (idx % 8)));
}
/**
* @brief 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.
*
* @param[in,out] field The bitfield
* @param[in] size The size of the bitfield
*
* @return number of bit that was set
* @return -1 if no bit was unset
*/
int bf_get_unset(uint8_t field[], int size);
#ifdef __cplusplus
}
#endif
/** @} */
#endif /* BITFIELD_H */
|