QP/C  7.0.0
Real-Time Embedded Framework
qpset.h
Go to the documentation of this file.
1/*============================================================================
2* QP/C Real-Time Embedded Framework (RTEF)
3* Copyright (C) 2005 Quantum Leaps, LLC. All rights reserved.
4*
5* SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-QL-commercial
6*
7* This software is dual-licensed under the terms of the open source GNU
8* General Public License version 3 (or any later version), or alternatively,
9* under the terms of one of the closed source Quantum Leaps commercial
10* licenses.
11*
12* The terms of the open source GNU General Public License version 3
13* can be found at: <www.gnu.org/licenses/gpl-3.0>
14*
15* The terms of the closed source Quantum Leaps commercial licenses
16* can be found at: <www.state-machine.com/licensing>
17*
18* Redistributions in source code must retain this top-level comment block.
19* Plagiarizing this software to sidestep the license obligations is illegal.
20*
21* Contact information:
22* <www.state-machine.com>
23* <info@state-machine.com>
24============================================================================*/
32#ifndef QPSET_H
33#define QPSET_H
34
35#ifndef QF_MAX_ACTIVE
36 /* default value when NOT defined */
37 #define QF_MAX_ACTIVE 32U
38#endif
39
40#if (QF_MAX_ACTIVE < 1U) || (64U < QF_MAX_ACTIVE)
41 #error "QF_MAX_ACTIVE out of range. Valid range is 1U..64U"
42#elif (QF_MAX_ACTIVE <= 8U)
43 typedef uint8_t QPSetBits;
44#elif (QF_MAX_ACTIVE <= 16U)
45 typedef uint16_t QPSetBits;
46#else
48 typedef uint32_t QPSetBits;
49#endif
50
51/*==========================================================================*/
52/* Log-base-2 calculations ...*/
53#ifndef QF_LOG2
54 uint_fast8_t QF_LOG2(QPSetBits x);
55#endif /* QF_LOG2 */
56
57#if (QF_MAX_ACTIVE <= 32U)
58
59/*==========================================================================*/
65typedef struct {
66 QPSetBits volatile bits;
67} QPSet;
68
70static inline void QPSet_setEmpty(QPSet * const me) {
71 me->bits = 0U;
72}
73
75static inline bool QPSet_isEmpty(QPSet * const me) {
76 return me->bits == 0U;
77}
78
80static inline bool QPSet_notEmpty(QPSet * const me) {
81 return me->bits != 0U;
82}
83
85static inline bool QPSet_hasElement(QPSet * const me, uint_fast8_t const n) {
86 return (me->bits & ((QPSetBits)1 << (n - 1U))) != 0U;
87}
88
90static inline void QPSet_insert(QPSet * const me, uint_fast8_t const n) {
91 me->bits |= ((QPSetBits)1 << (n - 1U));
92}
93
95static inline void QPSet_remove(QPSet * const me, uint_fast8_t const n) {
96 me->bits &= (QPSetBits)(~((QPSetBits)1 << (n - 1U)));
97}
98
102static inline uint_fast8_t QPSet_findMax(QPSet * const me) {
103 return QF_LOG2(me->bits);
104}
105
106#else /* QF_MAX_ACTIVE > 32U */
107
108/*==========================================================================*/
114typedef struct {
115 uint32_t volatile bits[2];
116} QPSet;
117
119static inline void QPSet_setEmpty(QPSet * const me) {
120 me->bits[0] = 0U;
121 me->bits[1] = 0U;
122}
123
125static inline bool QPSet_isEmpty(QPSet * const me) {
126 /* the following logic avoids UB in volatile access for MISRA */
127 return (me->bits[0] == 0U)
128 ? (me->bits[1] == 0U)
129 : false;
130}
131
133static inline bool QPSet_notEmpty(QPSet * const me) {
134 /* the following logic avoids UB in volatile access for MISRA */
135 return (me->bits[0] != 0U)
136 ? true
137 : (me->bits[1] != 0U);
138}
139
141static inline bool QPSet_hasElement(QPSet * const me, uint_fast8_t const n) {
142 return (n <= 32U)
143 ? ((me->bits[0] & ((uint32_t)1 << (n - 1U ))) != 0U)
144 : ((me->bits[1] & ((uint32_t)1 << (n - 33U))) != 0U);
145}
146
148static inline void QPSet_insert(QPSet * const me, uint_fast8_t const n) {
149 if (n <= 32U) {
150 me->bits[0] |= ((uint32_t)1 << (n - 1U ));
151 }
152 else {
153 me->bits[1] |= ((uint32_t)1 << (n - 33U));
154 }
155}
156
158static inline void QPSet_remove(QPSet * const me, uint_fast8_t const n) {
159 if (n <= 32U) {
160 me->bits[0] &= (uint32_t)(~((uint32_t)1 << (n - 1U )));
161 }
162 else {
163 me->bits[1] &= (uint32_t)(~((uint32_t)1 << (n - 33U)));
164 }
165}
166
170static inline uint_fast8_t QPSet_findMax(QPSet * const me) {
171 return (me->bits[1] != 0U)
172 ? (QF_LOG2(me->bits[1]) + 32U)
173 : QF_LOG2(me->bits[0]);
174}
175
176#endif /* QF_MAX_ACTIVE */
177
178#endif /* QPSET_H */
static bool QPSet_notEmpty(QPSet *const me)
Definition: qpset.h:80
static void QPSet_setEmpty(QPSet *const me)
Definition: qpset.h:70
QPSetBits volatile bits
Definition: qpset.h:66
uint8_t QPSetBits
Definition: qpset.h:43
static uint_fast8_t QPSet_findMax(QPSet *const me)
Definition: qpset.h:102
static void QPSet_insert(QPSet *const me, uint_fast8_t const n)
Definition: qpset.h:90
static void QPSet_remove(QPSet *const me, uint_fast8_t const n)
Definition: qpset.h:95
static bool QPSet_isEmpty(QPSet *const me)
Definition: qpset.h:75
static bool QPSet_hasElement(QPSet *const me, uint_fast8_t const n)
Definition: qpset.h:85
Definition: qpset.h:65
#define QF_LOG2(n_)
Definition: qf_act.c:142