2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2008, Jeffrey Roberson <jeff@freebsd.org>
7 * Copyright (c) 2008 Nokia Corporation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice unmodified, this list of conditions, and the following
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #ifndef _SYS_BITSET_H_
33 #define _SYS_BITSET_H_
36 * Whether expr is both constant and true. Result is itself constant.
37 * Used to enable optimizations for sets with a known small size.
39 #define __constexpr_cond(expr) (__builtin_constant_p((expr)) && (expr))
41 #define __bitset_mask(_s, n) \
42 (1UL << (__constexpr_cond(__bitset_words((_s)) == 1) ? \
43 (__size_t)(n) : ((n) % _BITSET_BITS)))
45 #define __bitset_word(_s, n) \
46 (__constexpr_cond(__bitset_words((_s)) == 1) ? \
47 0 : ((n) / _BITSET_BITS))
49 #define __BIT_CLR(_s, n, p) \
50 ((p)->__bits[__bitset_word(_s, n)] &= ~__bitset_mask((_s), (n)))
52 #define __BIT_COPY(_s, f, t) (void)(*(t) = *(f))
54 #define __BIT_ISSET(_s, n, p) \
55 ((((p)->__bits[__bitset_word(_s, n)] & __bitset_mask((_s), (n))) != 0))
57 #define __BIT_SET(_s, n, p) \
58 ((p)->__bits[__bitset_word(_s, n)] |= __bitset_mask((_s), (n)))
60 #define __BIT_ZERO(_s, p) do { \
62 for (__i = 0; __i < __bitset_words((_s)); __i++) \
63 (p)->__bits[__i] = 0L; \
66 #define __BIT_FILL(_s, p) do { \
68 for (__i = 0; __i < __bitset_words((_s)); __i++) \
69 (p)->__bits[__i] = -1L; \
72 #define __BIT_SETOF(_s, n, p) do { \
74 (p)->__bits[__bitset_word(_s, n)] = __bitset_mask((_s), (n)); \
78 #define __BIT_EMPTY(_s, p) __extension__ ({ \
80 for (__i = 0; __i < __bitset_words((_s)); __i++) \
81 if ((p)->__bits[__i]) \
83 __i == __bitset_words((_s)); \
87 #define __BIT_ISFULLSET(_s, p) __extension__ ({ \
89 for (__i = 0; __i < __bitset_words((_s)); __i++) \
90 if ((p)->__bits[__i] != (long)-1) \
92 __i == __bitset_words((_s)); \
95 /* Is c a subset of p. */
96 #define __BIT_SUBSET(_s, p, c) __extension__ ({ \
98 for (__i = 0; __i < __bitset_words((_s)); __i++) \
99 if (((c)->__bits[__i] & \
100 (p)->__bits[__i]) != \
103 __i == __bitset_words((_s)); \
106 /* Are there any common bits between b & c? */
107 #define __BIT_OVERLAP(_s, p, c) __extension__ ({ \
109 for (__i = 0; __i < __bitset_words((_s)); __i++) \
110 if (((c)->__bits[__i] & \
111 (p)->__bits[__i]) != 0) \
113 __i != __bitset_words((_s)); \
116 /* Compare two sets, returns 0 if equal 1 otherwise. */
117 #define __BIT_CMP(_s, p, c) __extension__ ({ \
119 for (__i = 0; __i < __bitset_words((_s)); __i++) \
120 if (((c)->__bits[__i] != \
123 __i != __bitset_words((_s)); \
126 #define __BIT_OR(_s, d, s) do { \
128 for (__i = 0; __i < __bitset_words((_s)); __i++) \
129 (d)->__bits[__i] |= (s)->__bits[__i]; \
132 #define __BIT_OR2(_s, d, s1, s2) do { \
134 for (__i = 0; __i < __bitset_words((_s)); __i++) \
135 (d)->__bits[__i] = (s1)->__bits[__i] | (s2)->__bits[__i];\
138 #define __BIT_AND(_s, d, s) do { \
140 for (__i = 0; __i < __bitset_words((_s)); __i++) \
141 (d)->__bits[__i] &= (s)->__bits[__i]; \
144 #define __BIT_AND2(_s, d, s1, s2) do { \
146 for (__i = 0; __i < __bitset_words((_s)); __i++) \
147 (d)->__bits[__i] = (s1)->__bits[__i] & (s2)->__bits[__i];\
150 #define __BIT_ANDNOT(_s, d, s) do { \
152 for (__i = 0; __i < __bitset_words((_s)); __i++) \
153 (d)->__bits[__i] &= ~(s)->__bits[__i]; \
156 #define __BIT_ANDNOT2(_s, d, s1, s2) do { \
158 for (__i = 0; __i < __bitset_words((_s)); __i++) \
159 (d)->__bits[__i] = (s1)->__bits[__i] & ~(s2)->__bits[__i];\
162 #define __BIT_XOR(_s, d, s) do { \
164 for (__i = 0; __i < __bitset_words((_s)); __i++) \
165 (d)->__bits[__i] ^= (s)->__bits[__i]; \
168 #define __BIT_XOR2(_s, d, s1, s2) do { \
170 for (__i = 0; __i < __bitset_words((_s)); __i++) \
171 (d)->__bits[__i] = (s1)->__bits[__i] ^ (s2)->__bits[__i];\
175 * Note, the atomic(9) API is not consistent between clear/set and
176 * testandclear/testandset in whether the value argument is a mask
180 #define __BIT_CLR_ATOMIC(_s, n, p) \
181 atomic_clear_long(&(p)->__bits[__bitset_word(_s, n)], \
182 __bitset_mask((_s), n))
184 #define __BIT_SET_ATOMIC(_s, n, p) \
185 atomic_set_long(&(p)->__bits[__bitset_word(_s, n)], \
186 __bitset_mask((_s), n))
188 #define __BIT_SET_ATOMIC_ACQ(_s, n, p) \
189 atomic_set_acq_long(&(p)->__bits[__bitset_word(_s, n)], \
190 __bitset_mask((_s), n))
192 #define __BIT_TEST_CLR_ATOMIC(_s, n, p) \
193 (atomic_testandclear_long( \
194 &(p)->__bits[__bitset_word((_s), (n))], (n)) != 0)
196 #define __BIT_TEST_SET_ATOMIC(_s, n, p) \
197 (atomic_testandset_long( \
198 &(p)->__bits[__bitset_word((_s), (n))], (n)) != 0)
200 /* Convenience functions catering special cases. */
201 #define __BIT_AND_ATOMIC(_s, d, s) do { \
203 for (__i = 0; __i < __bitset_words((_s)); __i++) \
204 atomic_clear_long(&(d)->__bits[__i], \
205 ~(s)->__bits[__i]); \
208 #define __BIT_OR_ATOMIC(_s, d, s) do { \
210 for (__i = 0; __i < __bitset_words((_s)); __i++) \
211 atomic_set_long(&(d)->__bits[__i], \
215 #define __BIT_COPY_STORE_REL(_s, f, t) do { \
217 for (__i = 0; __i < __bitset_words((_s)); __i++) \
218 atomic_store_rel_long(&(t)->__bits[__i], \
223 * Note that `start` and the returned value from __BIT_FFS_AT are
224 * 1-based bit indices.
226 #define __BIT_FFS_AT(_s, p, start) __extension__ ({ \
228 long __bit, __mask; \
230 __mask = ~0UL << ((start) % _BITSET_BITS); \
232 for (__i = __bitset_word((_s), (start)); \
233 __i < __bitset_words((_s)); \
235 if (((p)->__bits[__i] & __mask) != 0) { \
236 __bit = ffsl((p)->__bits[__i] & __mask); \
237 __bit += __i * _BITSET_BITS; \
245 #define __BIT_FFS(_s, p) __BIT_FFS_AT((_s), (p), 0)
247 #define __BIT_FLS(_s, p) __extension__ ({ \
252 for (__i = __bitset_words((_s)); __i > 0; __i--) { \
253 if ((p)->__bits[__i - 1] != 0) { \
254 __bit = flsl((p)->__bits[__i - 1]); \
255 __bit += (__i - 1) * _BITSET_BITS; \
262 #define __BIT_COUNT(_s, p) __extension__ ({ \
267 for (__i = 0; __i < __bitset_words((_s)); __i++) \
268 __count += __bitcountl((p)->__bits[__i]); \
272 #define __BIT_FOREACH_ADVANCE(_s, i, p, op) __extension__ ({ \
276 int __bit = ffsl(__bits) - 1; \
277 __bits &= ~(1ul << __bit); \
278 (i) = __i * _BITSET_BITS + __bit; \
282 if (++__i == __bitset_words(_s)) { \
286 __bits = op((p)->__bits[__i]); \
292 * Non-destructively loop over all set or clear bits in the set.
294 #define __BIT_FOREACH(_s, i, p, op) \
295 for (long __i = -1, __bits = 0; \
296 __BIT_FOREACH_ADVANCE(_s, i, p, op); )
298 #define __BIT_FOREACH_ISSET(_s, i, p) __BIT_FOREACH(_s, i, p, )
299 #define __BIT_FOREACH_ISCLR(_s, i, p) __BIT_FOREACH(_s, i, p, ~)
301 #define __BITSET_T_INITIALIZER(x) \
304 #define __BITSET_FSET(n) \
305 [ 0 ... ((n) - 1) ] = (-1L)
307 #define __BITSET_SIZE(_s) (__bitset_words((_s)) * sizeof(long))
309 #if defined(_KERNEL) || defined(_WANT_FREEBSD_BITSET)
310 #define BIT_AND(_s, d, s) __BIT_AND(_s, d, s)
311 #define BIT_AND2(_s, d, s1, s2) __BIT_AND2(_s, d, s1, s2)
312 #define BIT_ANDNOT(_s, d, s) __BIT_ANDNOT(_s, d, s)
313 #define BIT_ANDNOT2(_s, d, s1, s2) __BIT_ANDNOT2(_s, d, s1, s2)
314 #define BIT_AND_ATOMIC(_s, d, s) __BIT_AND_ATOMIC(_s, d, s)
315 #define BIT_CLR(_s, n, p) __BIT_CLR(_s, n, p)
316 #define BIT_CLR_ATOMIC(_s, n, p) __BIT_CLR_ATOMIC(_s, n, p)
317 #define BIT_CMP(_s, p, c) __BIT_CMP(_s, p, c)
318 #define BIT_COPY(_s, f, t) __BIT_COPY(_s, f, t)
319 #define BIT_COPY_STORE_REL(_s, f, t) __BIT_COPY_STORE_REL(_s, f, t)
320 #define BIT_COUNT(_s, p) __BIT_COUNT(_s, p)
321 #define BIT_EMPTY(_s, p) __BIT_EMPTY(_s, p)
322 #define BIT_FFS(_s, p) __BIT_FFS(_s, p)
323 #define BIT_FFS_AT(_s, p, start) __BIT_FFS_AT(_s, p, start)
324 #define BIT_FILL(_s, p) __BIT_FILL(_s, p)
325 #define BIT_FLS(_s, p) __BIT_FLS(_s, p)
326 #define BIT_FOREACH(_s, i, p, op) __BIT_FOREACH(_s, i, p, op)
327 #define BIT_FOREACH_ISCLR(_s, i, p) __BIT_FOREACH_ISCLR(_s, i, p)
328 #define BIT_FOREACH_ISSET(_s, i, p) __BIT_FOREACH_ISSET(_s, i, p)
329 #define BIT_ISFULLSET(_s, p) __BIT_ISFULLSET(_s, p)
330 #define BIT_ISSET(_s, n, p) __BIT_ISSET(_s, n, p)
331 #define BIT_OR(_s, d, s) __BIT_OR(_s, d, s)
332 #define BIT_OR2(_s, d, s1, s2) __BIT_OR2(_s, d, s1, s2)
333 #define BIT_OR_ATOMIC(_s, d, s) __BIT_OR_ATOMIC(_s, d, s)
334 #define BIT_OVERLAP(_s, p, c) __BIT_OVERLAP(_s, p, c)
335 #define BIT_SET(_s, n, p) __BIT_SET(_s, n, p)
336 #define BIT_SETOF(_s, n, p) __BIT_SETOF(_s, n, p)
337 #define BIT_SET_ATOMIC(_s, n, p) __BIT_SET_ATOMIC(_s, n, p)
338 #define BIT_SET_ATOMIC_ACQ(_s, n, p) __BIT_SET_ATOMIC_ACQ(_s, n, p)
339 #define BIT_SUBSET(_s, p, c) __BIT_SUBSET(_s, p, c)
340 #define BIT_TEST_CLR_ATOMIC(_s, n, p) __BIT_TEST_CLR_ATOMIC(_s, n, p)
341 #define BIT_TEST_SET_ATOMIC(_s, n, p) __BIT_TEST_SET_ATOMIC(_s, n, p)
342 #define BIT_XOR(_s, d, s) __BIT_XOR(_s, d, s)
343 #define BIT_XOR2(_s, d, s1, s2) __BIT_XOR2(_s, d, s1, s2)
344 #define BIT_ZERO(_s, p) __BIT_ZERO(_s, p)
348 * Dynamically allocate a bitset.
350 #define BITSET_ALLOC(_s, mt, mf) malloc(__BITSET_SIZE((_s)), mt, (mf))
351 #define BITSET_FREE(p, mt) free(p, mt)
354 #define BITSET_FSET(n) __BITSET_FSET(n)
355 #define BITSET_SIZE(_s) __BITSET_SIZE(_s)
356 #define BITSET_T_INITIALIZER(x) __BITSET_T_INITIALIZER(x)
357 #endif /* defined(_KERNEL) || defined(_WANT_FREEBSD_BITSET) */
359 #endif /* !_SYS_BITSET_H_ */