QP/C++  7.0.1
Real-Time Embedded Framework
qpset.hpp
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//============================================================================
30
31#ifndef QPSET_HPP
32#define QPSET_HPP
33
34#ifndef QF_MAX_ACTIVE
35 // default value when NOT defined
36 #define QF_MAX_ACTIVE 32U
37#endif
38
39namespace QP {
40
41#if (QF_MAX_ACTIVE < 1U) || (64U < QF_MAX_ACTIVE)
42 #error "QF_MAX_ACTIVE out of range. Valid range is 1U..64U"
43#elif (QF_MAX_ACTIVE <= 8U)
44 using QPSetBits = std::uint8_t;
45#elif (QF_MAX_ACTIVE <= 16U)
46 using QPSetBits = std::uint16_t;
47#else
49 using QPSetBits = std::uint32_t;
50#endif
51
52//============================================================================
53// Log-base-2 calculations ...
54#ifndef QF_LOG2
55
63extern "C" std::uint_fast8_t QF_LOG2(QPSetBits x) noexcept;
64
65#endif // QF_LOG2
66
67//============================================================================
68#if (QF_MAX_ACTIVE <= 32)
78struct QPSet {
79
80 QPSetBits volatile m_bits;
81
83 void setEmpty(void) noexcept {
84 m_bits = 0U;
85 }
86
88 bool isEmpty(void) const noexcept {
89 return (m_bits == 0U);
90 }
91
93 bool notEmpty(void) const noexcept {
94 return (m_bits != 0U);
95 }
96
98 bool hasElement(std::uint_fast8_t const n) const noexcept {
99 return (m_bits & (1U << (n - 1U))) != 0U;
100 }
101
103 void insert(std::uint_fast8_t const n) noexcept {
104 m_bits = (m_bits | (1U << (n - 1U)));
105 }
106
112 void rmove(std::uint_fast8_t const n) noexcept {
113 m_bits = (m_bits &
114 static_cast<QPSetBits>(~(static_cast<QPSetBits>(1) << (n - 1U))));
115 }
116
117 std::uint_fast8_t findMax(void) const noexcept {
118 return QF_LOG2(m_bits);
119 }
120};
121
122#else // QF_MAX_ACTIVE > 32U
123
133struct QPSet {
134
136 std::uint32_t volatile m_bits[2];
137
139 void setEmpty(void) noexcept {
140 m_bits[0] = 0U;
141 m_bits[1] = 0U;
142 }
143
145 // the following logic avoids UB in volatile access for MISRA compliantce
146 bool isEmpty(void) const noexcept {
147 return (m_bits[0] == 0U) ? (m_bits[1] == 0U) : false;
148 }
149
151 // the following logic avoids UB in volatile access for MISRA compliantce
152 bool notEmpty(void) const noexcept {
153 return (m_bits[0] != 0U) ? true : (m_bits[1] != 0U);
154 }
155
157 bool hasElement(std::uint_fast8_t const n) const noexcept {
158 return (n <= 32U)
159 ? ((m_bits[0] & (static_cast<std::uint32_t>(1) << (n - 1U)))
160 != 0U)
161 : ((m_bits[1] & (static_cast<std::uint32_t>(1) << (n - 33U)))
162 != 0U);
163 }
164
166 void insert(std::uint_fast8_t const n) noexcept {
167 if (n <= 32U) {
168 m_bits[0] = (m_bits[0]
169 | (static_cast<std::uint32_t>(1) << (n - 1U)));
170 }
171 else {
172 m_bits[1] = (m_bits[1]
173 | (static_cast<std::uint32_t>(1) << (n - 33U)));
174 }
175 }
176
182 void rmove(std::uint_fast8_t const n) noexcept {
183 if (n <= 32U) {
184 (m_bits[0] = (m_bits[0]
185 & ~(static_cast<std::uint32_t>(1) << (n - 1U))));
186 }
187 else {
188 (m_bits[1] = (m_bits[1]
189 & ~(static_cast<std::uint32_t>(1) << (n - 33U))));
190 }
191 }
192
194 std::uint_fast8_t findMax(void) const noexcept {
195 return (m_bits[1] != 0U)
196 ? (QF_LOG2(m_bits[1]) + 32U)
197 : (QF_LOG2(m_bits[0]));
198 }
199};
200
201#endif // QF_MAX_ACTIVE
202
203} // namespace QP
204
205#endif // QPSET_HPP
namespace associated with the QP/C++ framework
Definition: exa_native.dox:1
std::uint32_t QPSetBits
bitmask for the internal representation of QPSet elements
Definition: qpset.hpp:49
std::uint_fast8_t QF_LOG2(QPSetBits x) noexcept
function that returns (log2(x) + 1), where x is a 32-bit bitmask
Priority Set of up to 64 elements.
Definition: qpset.hpp:133
bool isEmpty(void) const noexcept
Evaluates to true if the priority set is empty.
Definition: qpset.hpp:146
bool hasElement(std::uint_fast8_t const n) const noexcept
the function evaluates to TRUE if the priority set has the element n.
Definition: qpset.hpp:157
void insert(std::uint_fast8_t const n) noexcept
insert element n into the set, n = 1..64
Definition: qpset.hpp:166
void rmove(std::uint_fast8_t const n) noexcept
remove element n from the set, n = 1..64
Definition: qpset.hpp:182
bool notEmpty(void) const noexcept
Evaluates to true if the priority set is not empty.
Definition: qpset.hpp:152
void setEmpty(void) noexcept
Makes the priority set me_ empty.
Definition: qpset.hpp:139
std::uint32_t volatile m_bits[2]
Two 32-bit bitmasks with a bit for each element.
Definition: qpset.hpp:136
std::uint_fast8_t findMax(void) const noexcept
find the maximum element in the set, returns zero if the set is empty
Definition: qpset.hpp:194