1 .\" Copyright (c) 2015 Conrad Meyer <cem@FreeBSD.org>
2 .\" All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
13 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
14 .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
15 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
17 .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23 .\" POSSIBILITY OF SUCH DAMAGE.
27 .Dd September 20, 2021
34 .Nm BITSET_T_INITIALIZER ,
48 .Nm BIT_FOREACH_ISSET ,
49 .Nm BIT_FOREACH_ISCLR ,
64 .Nm BIT_SET_ATOMIC_ACQ ,
65 .Nm BIT_TEST_SET_ATOMIC ,
66 .Nm BIT_TEST_CLR_ATOMIC ,
69 .Nm BIT_COPY_STORE_REL
70 .Nd bitset manipulation macros
75 .Fn BITSET_DEFINE "STRUCTNAME" "const SETSIZE"
76 .Fn BITSET_T_INITIALIZER "ARRAY_CONTENTS"
77 .Fn BITSET_FSET "N_WORDS"
79 .Fn BIT_CLR "const SETSIZE" "size_t bit" "struct STRUCTNAME *bitset"
80 .Fn BIT_COPY "const SETSIZE" "struct STRUCTNAME *from" "struct STRUCTNAME *to"
82 .Fn BIT_ISSET "const SETSIZE" "size_t bit" "struct STRUCTNAME *bitset"
83 .Fn BIT_SET "const SETSIZE" "size_t bit" "struct STRUCTNAME *bitset"
84 .Fn BIT_ZERO "const SETSIZE" "struct STRUCTNAME *bitset"
85 .Fn BIT_FILL "const SETSIZE" "struct STRUCTNAME *bitset"
86 .Fn BIT_SETOF "const SETSIZE" "size_t bit" "struct STRUCTNAME *bitset"
88 .Fn BIT_EMPTY "const SETSIZE" "struct STRUCTNAME *bitset"
90 .Fn BIT_ISFULLSET "const SETSIZE" "struct STRUCTNAME *bitset"
92 .Fn BIT_FFS "const SETSIZE" "struct STRUCTNAME *bitset"
94 .Fn BIT_FFS_AT "const SETSIZE" "struct STRUCTNAME *bitset" "long start"
96 .Fn BIT_FLS "const SETSIZE" "struct STRUCTNAME *bitset"
100 .Fa "const struct STRUCTNAME *bitset"
102 .Fo BIT_FOREACH_ISCLR
105 .Fa "const struct STRUCTNAME *bitset"
108 .Fn BIT_COUNT "const SETSIZE" "struct STRUCTNAME *bitset"
111 .Fa "const SETSIZE" "struct STRUCTNAME *haystack" "struct STRUCTNAME *needle"
115 .Fa "const SETSIZE" "struct STRUCTNAME *bitset1" "struct STRUCTNAME *bitset2"
119 .Fa "const SETSIZE" "struct STRUCTNAME *bitset1" "struct STRUCTNAME *bitset2"
121 .Fn BIT_OR "const SETSIZE" "struct STRUCTNAME *dst" "struct STRUCTNAME *src"
124 .Fa "struct STRUCTNAME *dst"
125 .Fa "struct STRUCTNAME *src1"
126 .Fa "struct STRUCTNAME *src2"
128 .Fn BIT_AND "const SETSIZE" "struct STRUCTNAME *dst" "struct STRUCTNAME *src"
131 .Fa "struct STRUCTNAME *dst"
132 .Fa "struct STRUCTNAME *src1"
133 .Fa "struct STRUCTNAME *src2"
135 .Fn BIT_ANDNOT "const SETSIZE" "struct STRUCTNAME *dst" "struct STRUCTNAME *src"
138 .Fa "struct STRUCTNAME *dst"
139 .Fa "struct STRUCTNAME *src1"
140 .Fa "struct STRUCTNAME *src2"
142 .Fn BIT_XOR "const SETSIZE" "struct STRUCTNAME *dst" "struct STRUCTNAME *src"
145 .Fa "struct STRUCTNAME *dst"
146 .Fa "struct STRUCTNAME *src1"
147 .Fa "struct STRUCTNAME *src2"
150 .Fn BIT_CLR_ATOMIC "const SETSIZE" "size_t bit" "struct STRUCTNAME *bitset"
151 .Fn BIT_SET_ATOMIC "const SETSIZE" "size_t bit" "struct STRUCTNAME *bitset"
152 .Fn BIT_SET_ATOMIC_ACQ "const SETSIZE" "size_t bit" "struct STRUCTNAME *bitset"
154 .Fn BIT_TEST_SET_ATOMIC "const SETSIZE" "size_t bit" "struct STRUCTNAME *bitset"
156 .Fn BIT_TEST_CLR_ATOMIC "const SETSIZE" "size_t bit" "struct STRUCTNAME *bitset"
159 .Fa "const SETSIZE" "struct STRUCTNAME *dst" "struct STRUCTNAME *src"
162 .Fa "const SETSIZE" "struct STRUCTNAME *dst" "struct STRUCTNAME *src"
164 .Fo BIT_COPY_STORE_REL
165 .Fa "const SETSIZE" "struct STRUCTNAME *from" "struct STRUCTNAME *to"
170 family of macros provide a flexible and efficient bitset implementation if the
171 maximum size of the set is known at compilation.
172 Throughout this manual page, the name
174 refers to the size of the bitset in bits.
175 Individual bits in bitsets are referenced with indices zero through
184 macro defines a bitset struct
186 with room to represent
191 .Fn BITSET_T_INITIALIZER
192 macro allows one to initialize a bitset struct with a compile time literal
197 macro generates a compile time literal, usable by
198 .Fn BITSET_T_INITIALIZER ,
199 representing a full bitset (all bits set).
201 .Fn BITSET_T_INITIALIZER
205 .Sx BITSET_T_INITIALIZER EXAMPLE
212 .Bd -literal -offset indent
213 __bitset_words(SETSIZE)
220 in the bitset pointed to by
224 macro is identical, but the bit is cleared atomically.
226 .Fn BIT_TEST_CLR_ATOMIC
227 macro atomically clears the bit and returns whether it was set.
231 macro copies the contents of the bitset
235 .Fn BIT_COPY_STORE_REL
236 is similar, but copies component machine words from
240 with atomic store with release semantics.
243 is composed of multiple machine words,
244 .Fn BIT_COPY_STORE_REL
245 performs multiple individually atomic operations.)
251 in the bitset pointed to by
255 macro is identical, but the bit is set atomically.
257 .Fn BIT_SET_ATOMIC_ACQ
258 macro sets the bit with acquire semantics.
260 .Fn BIT_TEST_SET_ATOMIC
261 macro atomically sets the bit and returns whether it was set.
265 macro clears all bits in
270 macro sets all bits in
275 macro clears all bits in
277 before setting only bit
294 is full (all bits set).
298 macro returns the 1-index of the first (lowest) set bit in
305 to use the non-zero result of
309 index parameter to any other
311 macro, you must subtract one from the result.
315 macro returns the 1-index of the first (lowest) set bit in
317 which is greater than the given 1-indexed
319 or zero if no bits in
327 macro returns the 1-index of the last (highest) set bit in
334 to use the non-zero result of
338 index parameter to any other
340 macro, you must subtract one from the result.
343 .Fn BIT_FOREACH_ISSET
344 macro can be used to iterate over all set bits in
348 must have been declared with type
350 and upon each iteration
352 is set to the index of successive set bits.
355 after the loop terminates is undefined.
357 .Fn BIT_FOREACH_ISCLR
358 iterates over all clear bits in
363 macro returns the total number of set bits in
383 have any common bits.
388 is not the empty set.)
401 macro sets bits present in
407 equivalent of the scalar:
412 is similar, but sets bits in the component machine words in
417 is composed of multiple machine words,
419 performs multiple individually atomic operations.)
427 and assigns the result to
431 equivalent of the scalar:
440 macro clears bits absent from
446 equivalent of the scalar:
451 is similar, with the same atomic semantics as
460 and assigns the result to
464 equivalent of the scalar:
473 macro clears bits set in
479 equivalent of the scalar:
490 and assigns the result to
494 equivalent of the scalar:
503 macro toggles bits set in
509 equivalent of the scalar:
520 and assigns the result to
524 equivalent of the scalar:
530 .Sh BITSET_T_INITIALIZER EXAMPLE
532 BITSET_DEFINE(_myset, MYSETSIZE);
536 /* Initialize myset to filled (all bits set) */
537 myset = BITSET_T_INITIALIZER(BITSET_FSET(__bitset_words(MYSETSIZE)));
539 /* Initialize myset to only the lowest bit set */
540 myset = BITSET_T_INITIALIZER(0x1);
548 macros first appeared in
553 released in July 2014.
555 This manual page first appeared in
561 macros were generalized and pulled out of
568 .An Attilio Rao Aq Mt attilio@FreeBSD.org .
569 This manual page was written by
570 .An Conrad Meyer Aq Mt cem@FreeBSD.org .
574 argument to all of these macros must match the value given to
577 Unlike every other reference to individual set members, which are zero-indexed,
582 return a one-indexed result (or zero if the set is empty).