QP/C  5.9.8
qpset.h
Go to the documentation of this file.
1 
40 #ifndef qpset_h
41 #define qpset_h
42 
43 #if (QF_MAX_ACTIVE < 1) || (64 < QF_MAX_ACTIVE)
44  #error "QF_MAX_ACTIVE not defined or out of range. Valid range is 1..64"
45 #endif
46 
47 #if (QF_MAX_ACTIVE <= 32)
48 
49 /****************************************************************************/
56 typedef struct {
57  uint32_t volatile bits;
58 } QPSet;
59 
61 #define QPSet_setEmpty(me_) ((me_)->bits = (uint32_t)0)
62 
64 #define QPSet_isEmpty(me_) ((me_)->bits == (uint32_t)0)
65 
67 #define QPSet_notEmpty(me_) ((me_)->bits != (uint32_t)0)
68 
70 #define QPSet_hasElement(me_, n_) \
71  (((me_)->bits & ((uint32_t)1 << ((n_) - (uint_fast8_t)1))) != (uint32_t)0)
72 
74 #define QPSet_insert(me_, n_) \
75  ((me_)->bits |= (uint32_t)((uint32_t)1 << ((n_) - (uint_fast8_t)1)))
76 
78 #define QPSet_remove(me_, n_) \
79  ((me_)->bits &= (uint32_t)(~((uint32_t)1U << ((n_) - (uint_fast8_t)1))))
80 
84 #define QPSet_findMax(me_, n_) \
85  ((n_) = QF_LOG2((me_)->bits))
86 
87 #else /* QF_MAX_ACTIVE > 32 */
88 
89 /****************************************************************************/
96 typedef struct {
97  uint32_t volatile bits[2];
98 } QPSet;
99 
101 #define QPSet_setEmpty(me_) do { \
102  (me_)->bits[0] = (uint32_t)0; \
103  (me_)->bits[1] = (uint32_t)0; \
104 } while (0)
105 
107 /* the following logic avoids UB in volatile access for MISRA compliantce */
108 #define QPSet_isEmpty(me_) \
109  (((me_)->bits[0] == (uint32_t)0) \
110  ? ((me_)->bits[1] == (uint32_t)0) \
111  : 0)
112 
114 /* the following logic avoids UB in volatile access for MISRA compliantce */
115 #define QPSet_notEmpty(me_) \
116  (((me_)->bits[0] != (uint32_t)0) \
117  ? 1 \
118  : ((me_)->bits[1] != (uint32_t)0))
119 
121 #define QPSet_hasElement(me_, n_) \
122  (((n_) <= (uint_fast8_t)32) \
123  ? (((me_)->bits[0] & ((uint32_t)1 << ((n_) - (uint_fast8_t)1 ))) \
124  != (uint32_t)0) \
125  : (((me_)->bits[1] & ((uint32_t)1 << ((n_) - (uint_fast8_t)33))) \
126  != (uint32_t)0))
127 
129 #define QPSet_insert(me_, n_) do { \
130  if ((n_) <= (uint_fast8_t)32) { \
131  ((me_)->bits[0] |= ((uint32_t)1 << ((n_) - (uint_fast8_t)1 ))); \
132  } \
133  else { \
134  ((me_)->bits[1] |= ((uint32_t)1 << ((n_) - (uint_fast8_t)33))); \
135  } \
136 } while (0)
137 
139 #define QPSet_remove(me_, n_) do { \
140  if ((n_) <= (uint_fast8_t)32) { \
141  ((me_)->bits[0] &= \
142  (uint32_t)(~((uint32_t)1 << ((n_) - (uint_fast8_t)1 )))); \
143  } \
144  else { \
145  ((me_)->bits[1] &= \
146  (uint32_t)(~((uint32_t)1 << ((n_) - (uint_fast8_t)33)))); \
147  } \
148 } while (0)
149 
153 #define QPSet_findMax(me_, n_) \
154  ((n_) = ((me_)->bits[1] != (uint32_t)0) \
155  ? (QF_LOG2((me_)->bits[1]) + (uint_fast8_t)32) \
156  : (QF_LOG2((me_)->bits[0])))
157 
158 #endif /* QF_MAX_ACTIVE */
159 
160 
161 /****************************************************************************/
162 /* Log-base-2 calculations ...*/
163 #ifndef QF_LOG2
164 
171  extern uint8_t const QF_log2Lkup[256];
172 
173 #if (__STDC_VERSION__ >= 199901L) /* is it C99 compiler? */
174 
182  inline uint_fast8_t QF_LOG2(uint32_t x) {
183  uint_fast8_t n;
184  uint_fast8_t i;
185 
186  if ((x >> 16) != (uint32_t)0) {
187  if ((x >> 24) != (uint32_t)0) {
188  i = (uint_fast8_t)(x >> 24);
189  n = (uint_fast8_t)24;
190  }
191  else {
192  i = (uint_fast8_t)(x >> 16);
193  n = (uint_fast8_t)16;
194  }
195  }
196  else {
197  if ((x >> 8) != (uint32_t)0) {
198  i = (uint_fast8_t)(x >> 8);
199  n = (uint_fast8_t)8;
200  }
201  else {
202  i = (uint_fast8_t)(x);
203  n = (uint_fast8_t)0;
204  }
205  }
206  return (uint_fast8_t)QF_log2Lkup[i] + n;
207  }
208 
209 #else /* older C compiler */
210  uint_fast8_t QF_LOG2(uint32_t x);
211 
212 #endif /* __STDC_VERSION__ */
213 
214 #endif /* QF_LOG2 */
215 
216 #endif /* qpset_h */
unsigned char uint8_t
exact-width 8-bit unsigned int
Definition: stdint.h:28
unsigned int uint_fast8_t
fast at-least 8-bit unsigned int
Definition: stdint.h:35
unsigned long uint32_t
exact-width 32-bit unsigned int
Definition: stdint.h:30
uint8_t const QF_log2Lkup[256]
Lookup table for (log2(n) + 1), where n = 0..255.
Definition: qf_act.c:172
Priority Set of up to 32 elements.
Definition: qpset.h:56
uint32_t volatile bits
bitmask with a bit for each element
Definition: qpset.h:57