xref: /freebsd/share/man/man9/bitset.9 (revision 5e04571cf3cf4024be926976a6abf19626df30be)
17ebf4122SConrad Meyer.\" Copyright (c) 2015 Conrad Meyer <cem@FreeBSD.org>
27ebf4122SConrad Meyer.\" All rights reserved.
37ebf4122SConrad Meyer.\"
47ebf4122SConrad Meyer.\" Redistribution and use in source and binary forms, with or without
57ebf4122SConrad Meyer.\" modification, are permitted provided that the following conditions
67ebf4122SConrad Meyer.\" are met:
77ebf4122SConrad Meyer.\" 1. Redistributions of source code must retain the above copyright
87ebf4122SConrad Meyer.\"    notice, this list of conditions and the following disclaimer.
97ebf4122SConrad Meyer.\" 2. Redistributions in binary form must reproduce the above copyright
107ebf4122SConrad Meyer.\"    notice, this list of conditions and the following disclaimer in the
117ebf4122SConrad Meyer.\"    documentation and/or other materials provided with the distribution.
127ebf4122SConrad Meyer.\"
137ebf4122SConrad Meyer.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
147ebf4122SConrad Meyer.\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
157ebf4122SConrad Meyer.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
167ebf4122SConrad Meyer.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
177ebf4122SConrad Meyer.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
187ebf4122SConrad Meyer.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
197ebf4122SConrad Meyer.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
207ebf4122SConrad Meyer.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
217ebf4122SConrad Meyer.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
227ebf4122SConrad Meyer.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
237ebf4122SConrad Meyer.\" POSSIBILITY OF SUCH DAMAGE.
247ebf4122SConrad Meyer.\"
257ebf4122SConrad Meyer.\" $FreeBSD$
267ebf4122SConrad Meyer.\"
27dfd3bde5SMark Johnston.Dd September 20, 2021
287ebf4122SConrad Meyer.Dt BITSET 9
297ebf4122SConrad Meyer.Os
307ebf4122SConrad Meyer.Sh NAME
317ebf4122SConrad Meyer.Nm bitset(9)
327ebf4122SConrad Meyer\(em
337ebf4122SConrad Meyer.Nm BITSET_DEFINE ,
347ebf4122SConrad Meyer.Nm BITSET_T_INITIALIZER ,
357ebf4122SConrad Meyer.Nm BITSET_FSET ,
367ebf4122SConrad Meyer.Nm BIT_CLR ,
377ebf4122SConrad Meyer.Nm BIT_COPY ,
387ebf4122SConrad Meyer.Nm BIT_ISSET ,
397ebf4122SConrad Meyer.Nm BIT_SET ,
407ebf4122SConrad Meyer.Nm BIT_ZERO ,
417ebf4122SConrad Meyer.Nm BIT_FILL ,
427ebf4122SConrad Meyer.Nm BIT_SETOF ,
437ebf4122SConrad Meyer.Nm BIT_EMPTY ,
447ebf4122SConrad Meyer.Nm BIT_ISFULLSET ,
457ebf4122SConrad Meyer.Nm BIT_FFS ,
46f8782001SD Scott Phillips.Nm BIT_FFS_AT ,
471550c622SKonstantin Belousov.Nm BIT_FLS ,
48dfd3bde5SMark Johnston.Nm BIT_FOREACH_ISSET ,
49dfd3bde5SMark Johnston.Nm BIT_FOREACH_ISCLR ,
507ebf4122SConrad Meyer.Nm BIT_COUNT ,
517ebf4122SConrad Meyer.Nm BIT_SUBSET ,
527ebf4122SConrad Meyer.Nm BIT_OVERLAP ,
537ebf4122SConrad Meyer.Nm BIT_CMP ,
547ebf4122SConrad Meyer.Nm BIT_OR ,
55e058e1c4SKonstantin Belousov.Nm BIT_OR2 ,
567ebf4122SConrad Meyer.Nm BIT_AND ,
57e058e1c4SKonstantin Belousov.Nm BIT_AND2 ,
589825eadfSRyan Libby.Nm BIT_ANDNOT ,
599825eadfSRyan Libby.Nm BIT_ANDNOT2 ,
60e058e1c4SKonstantin Belousov.Nm BIT_XOR ,
61e058e1c4SKonstantin Belousov.Nm BIT_XOR2 ,
627ebf4122SConrad Meyer.Nm BIT_CLR_ATOMIC ,
637ebf4122SConrad Meyer.Nm BIT_SET_ATOMIC ,
647ebf4122SConrad Meyer.Nm BIT_SET_ATOMIC_ACQ ,
65ae4a8e52SRyan Libby.Nm BIT_TEST_SET_ATOMIC ,
66ae4a8e52SRyan Libby.Nm BIT_TEST_CLR_ATOMIC ,
677ebf4122SConrad Meyer.Nm BIT_AND_ATOMIC ,
687ebf4122SConrad Meyer.Nm BIT_OR_ATOMIC ,
697ebf4122SConrad Meyer.Nm BIT_COPY_STORE_REL
707ebf4122SConrad Meyer.Nd bitset manipulation macros
717ebf4122SConrad Meyer.Sh SYNOPSIS
727ebf4122SConrad Meyer.In sys/_bitset.h
737ebf4122SConrad Meyer.In sys/bitset.h
747ebf4122SConrad Meyer.\"
757ebf4122SConrad Meyer.Fn BITSET_DEFINE "STRUCTNAME" "const SETSIZE"
767ebf4122SConrad Meyer.Fn BITSET_T_INITIALIZER "ARRAY_CONTENTS"
777ebf4122SConrad Meyer.Fn BITSET_FSET "N_WORDS"
787ebf4122SConrad Meyer.\"
797ebf4122SConrad Meyer.Fn BIT_CLR "const SETSIZE" "size_t bit" "struct STRUCTNAME *bitset"
807ebf4122SConrad Meyer.Fn BIT_COPY "const SETSIZE" "struct STRUCTNAME *from" "struct STRUCTNAME *to"
817ebf4122SConrad Meyer.Ft bool
827ebf4122SConrad Meyer.Fn BIT_ISSET "const SETSIZE" "size_t bit" "struct STRUCTNAME *bitset"
837ebf4122SConrad Meyer.Fn BIT_SET "const SETSIZE" "size_t bit" "struct STRUCTNAME *bitset"
847ebf4122SConrad Meyer.Fn BIT_ZERO "const SETSIZE" "struct STRUCTNAME *bitset"
857ebf4122SConrad Meyer.Fn BIT_FILL "const SETSIZE" "struct STRUCTNAME *bitset"
867ebf4122SConrad Meyer.Fn BIT_SETOF "const SETSIZE" "size_t bit" "struct STRUCTNAME *bitset"
877ebf4122SConrad Meyer.Ft bool
887ebf4122SConrad Meyer.Fn BIT_EMPTY "const SETSIZE" "struct STRUCTNAME *bitset"
897ebf4122SConrad Meyer.Ft bool
907ebf4122SConrad Meyer.Fn BIT_ISFULLSET "const SETSIZE" "struct STRUCTNAME *bitset"
9126a3bf76SD Scott Phillips.Ft long
927ebf4122SConrad Meyer.Fn BIT_FFS "const SETSIZE" "struct STRUCTNAME *bitset"
9326a3bf76SD Scott Phillips.Ft long
9426a3bf76SD Scott Phillips.Fn BIT_FFS_AT "const SETSIZE" "struct STRUCTNAME *bitset" "long start"
9526a3bf76SD Scott Phillips.Ft long
961550c622SKonstantin Belousov.Fn BIT_FLS "const SETSIZE" "struct STRUCTNAME *bitset"
97dfd3bde5SMark Johnston.Fo BIT_FOREACH_ISSET
98dfd3bde5SMark Johnston.Fa "const SETSIZE"
99dfd3bde5SMark Johnston.Fa "size_t bit"
100dfd3bde5SMark Johnston.Fa "const struct STRUCTNAME *bitset"
101dfd3bde5SMark Johnston.Fc
102dfd3bde5SMark Johnston.Fo BIT_FOREACH_ISCLR
103dfd3bde5SMark Johnston.Fa "const SETSIZE"
104dfd3bde5SMark Johnston.Fa "size_t bit"
105dfd3bde5SMark Johnston.Fa "const struct STRUCTNAME *bitset"
106dfd3bde5SMark Johnston.Fc
10726a3bf76SD Scott Phillips.Ft long
1087ebf4122SConrad Meyer.Fn BIT_COUNT "const SETSIZE" "struct STRUCTNAME *bitset"
1097ebf4122SConrad Meyer.Ft bool
1107ebf4122SConrad Meyer.Fo BIT_SUBSET
1117ebf4122SConrad Meyer.Fa "const SETSIZE" "struct STRUCTNAME *haystack" "struct STRUCTNAME *needle"
1127ebf4122SConrad Meyer.Fc
1137ebf4122SConrad Meyer.Ft bool
1147ebf4122SConrad Meyer.Fo BIT_OVERLAP
1157ebf4122SConrad Meyer.Fa "const SETSIZE" "struct STRUCTNAME *bitset1" "struct STRUCTNAME *bitset2"
1167ebf4122SConrad Meyer.Fc
1177ebf4122SConrad Meyer.Ft bool
1187ebf4122SConrad Meyer.Fo BIT_CMP
1197ebf4122SConrad Meyer.Fa "const SETSIZE" "struct STRUCTNAME *bitset1" "struct STRUCTNAME *bitset2"
1207ebf4122SConrad Meyer.Fc
1217ebf4122SConrad Meyer.Fn BIT_OR "const SETSIZE" "struct STRUCTNAME *dst" "struct STRUCTNAME *src"
122e058e1c4SKonstantin Belousov.Fo BIT_OR2
123e058e1c4SKonstantin Belousov.Fa "const SETSIZE"
124e058e1c4SKonstantin Belousov.Fa "struct STRUCTNAME *dst"
125e058e1c4SKonstantin Belousov.Fa "struct STRUCTNAME *src1"
126e058e1c4SKonstantin Belousov.Fa "struct STRUCTNAME *src2"
127e058e1c4SKonstantin Belousov.Fc
1287ebf4122SConrad Meyer.Fn BIT_AND "const SETSIZE" "struct STRUCTNAME *dst" "struct STRUCTNAME *src"
129e058e1c4SKonstantin Belousov.Fo BIT_AND2
130e058e1c4SKonstantin Belousov.Fa "const SETSIZE"
131e058e1c4SKonstantin Belousov.Fa "struct STRUCTNAME *dst"
132e058e1c4SKonstantin Belousov.Fa "struct STRUCTNAME *src1"
133e058e1c4SKonstantin Belousov.Fa "struct STRUCTNAME *src2"
134e058e1c4SKonstantin Belousov.Fc
1359825eadfSRyan Libby.Fn BIT_ANDNOT "const SETSIZE" "struct STRUCTNAME *dst" "struct STRUCTNAME *src"
1369825eadfSRyan Libby.Fo BIT_ANDNOT2
137e058e1c4SKonstantin Belousov.Fa "const SETSIZE"
138e058e1c4SKonstantin Belousov.Fa "struct STRUCTNAME *dst"
139e058e1c4SKonstantin Belousov.Fa "struct STRUCTNAME *src1"
140e058e1c4SKonstantin Belousov.Fa "struct STRUCTNAME *src2"
141e058e1c4SKonstantin Belousov.Fc
142e058e1c4SKonstantin Belousov.Fn BIT_XOR "const SETSIZE" "struct STRUCTNAME *dst" "struct STRUCTNAME *src"
143e058e1c4SKonstantin Belousov.Fo BIT_XOR2
144e058e1c4SKonstantin Belousov.Fa "const SETSIZE"
145e058e1c4SKonstantin Belousov.Fa "struct STRUCTNAME *dst"
146e058e1c4SKonstantin Belousov.Fa "struct STRUCTNAME *src1"
147e058e1c4SKonstantin Belousov.Fa "struct STRUCTNAME *src2"
148e058e1c4SKonstantin Belousov.Fc
1497ebf4122SConrad Meyer.\"
1507ebf4122SConrad Meyer.Fn BIT_CLR_ATOMIC "const SETSIZE" "size_t bit" "struct STRUCTNAME *bitset"
1517ebf4122SConrad Meyer.Fn BIT_SET_ATOMIC "const SETSIZE" "size_t bit" "struct STRUCTNAME *bitset"
1527ebf4122SConrad Meyer.Fn BIT_SET_ATOMIC_ACQ "const SETSIZE" "size_t bit" "struct STRUCTNAME *bitset"
153ae4a8e52SRyan Libby.Ft bool
154ae4a8e52SRyan Libby.Fn BIT_TEST_SET_ATOMIC "const SETSIZE" "size_t bit" "struct STRUCTNAME *bitset"
155ae4a8e52SRyan Libby.Ft bool
156ae4a8e52SRyan Libby.Fn BIT_TEST_CLR_ATOMIC "const SETSIZE" "size_t bit" "struct STRUCTNAME *bitset"
1577ebf4122SConrad Meyer.\"
1587ebf4122SConrad Meyer.Fo BIT_AND_ATOMIC
1597ebf4122SConrad Meyer.Fa "const SETSIZE" "struct STRUCTNAME *dst" "struct STRUCTNAME *src"
1607ebf4122SConrad Meyer.Fc
1617ebf4122SConrad Meyer.Fo BIT_OR_ATOMIC
1627ebf4122SConrad Meyer.Fa "const SETSIZE" "struct STRUCTNAME *dst" "struct STRUCTNAME *src"
1637ebf4122SConrad Meyer.Fc
1647ebf4122SConrad Meyer.Fo BIT_COPY_STORE_REL
1657ebf4122SConrad Meyer.Fa "const SETSIZE" "struct STRUCTNAME *from" "struct STRUCTNAME *to"
1667ebf4122SConrad Meyer.Fc
167*5e04571cSStefan Eßer.Fd #define _WANT_FREEBSD_BITSET
1687ebf4122SConrad Meyer.Sh DESCRIPTION
1697ebf4122SConrad MeyerThe
1707ebf4122SConrad Meyer.Nm
1717ebf4122SConrad Meyerfamily of macros provide a flexible and efficient bitset implementation if the
1727ebf4122SConrad Meyermaximum size of the set is known at compilation.
1737ebf4122SConrad MeyerThroughout this manual page, the name
1747ebf4122SConrad Meyer.Fa SETSIZE
1757ebf4122SConrad Meyerrefers to the size of the bitset in bits.
1767ebf4122SConrad MeyerIndividual bits in bitsets are referenced with indices zero through
1777ebf4122SConrad Meyer.Fa SETSIZE - 1 .
1787ebf4122SConrad MeyerOne example use of
1797ebf4122SConrad Meyer.In sys/bitset.h
1807ebf4122SConrad Meyeris
1817ebf4122SConrad Meyer.In sys/cpuset.h .
1827ebf4122SConrad Meyer.Pp
183*5e04571cSStefan EßerThese macros are meant to be used in the kernel and are visible if
184*5e04571cSStefan Eßer.Dv _KERNEL is defined when
185*5e04571cSStefan Eßer.In sys/_bitset.h
186*5e04571cSStefan Eßeror
187*5e04571cSStefan Eßer.In sys/bitset.h
188*5e04571cSStefan Eßerare included in a program.
189*5e04571cSStefan EßerUserland programs must define
190*5e04571cSStefan Eßer.Dv _WANT_FREEBSD_BITSET
191*5e04571cSStefan Eßerbefore including these files to make the macros visible.
192*5e04571cSStefan Eßer.Pp
1937ebf4122SConrad MeyerThe
1947ebf4122SConrad Meyer.Fn BITSET_DEFINE
1957ebf4122SConrad Meyermacro defines a bitset struct
1967ebf4122SConrad Meyer.Fa STRUCTNAME
1977ebf4122SConrad Meyerwith room to represent
1987ebf4122SConrad Meyer.Fa SETSIZE
1997ebf4122SConrad Meyerbits.
2007ebf4122SConrad Meyer.Pp
2017ebf4122SConrad MeyerThe
2027ebf4122SConrad Meyer.Fn BITSET_T_INITIALIZER
2037ebf4122SConrad Meyermacro allows one to initialize a bitset struct with a compile time literal
2047ebf4122SConrad Meyervalue.
2057ebf4122SConrad Meyer.Pp
2067ebf4122SConrad MeyerThe
2077ebf4122SConrad Meyer.Fn BITSET_FSET
2087ebf4122SConrad Meyermacro generates a compile time literal, usable by
2097ebf4122SConrad Meyer.Fn BITSET_T_INITIALIZER ,
2107ebf4122SConrad Meyerrepresenting a full bitset (all bits set).
2117ebf4122SConrad MeyerFor examples of
2127ebf4122SConrad Meyer.Fn BITSET_T_INITIALIZER
2137ebf4122SConrad Meyerand
2147ebf4122SConrad Meyer.Fn BITSET_FSET
2157ebf4122SConrad Meyerusage, see the
2167ebf4122SConrad Meyer.Sx BITSET_T_INITIALIZER EXAMPLE
2177ebf4122SConrad Meyersection.
2187ebf4122SConrad MeyerThe
2197ebf4122SConrad Meyer.Fa N_WORDS
2207ebf4122SConrad Meyerparameter to
2217ebf4122SConrad Meyer.Fn BITSET_FSET
2227ebf4122SConrad Meyershould be:
2237ebf4122SConrad Meyer.Bd -literal -offset indent
2247ebf4122SConrad Meyer__bitset_words(SETSIZE)
2257ebf4122SConrad Meyer.Ed
2267ebf4122SConrad Meyer.Pp
2277ebf4122SConrad MeyerThe
2287ebf4122SConrad Meyer.Fn BIT_CLR
2297ebf4122SConrad Meyermacro clears bit
2307ebf4122SConrad Meyer.Fa bit
2317ebf4122SConrad Meyerin the bitset pointed to by
2327ebf4122SConrad Meyer.Fa bitset .
2337ebf4122SConrad MeyerThe
2347ebf4122SConrad Meyer.Fn BIT_CLR_ATOMIC
2357ebf4122SConrad Meyermacro is identical, but the bit is cleared atomically.
236ae4a8e52SRyan LibbyThe
237ae4a8e52SRyan Libby.Fn BIT_TEST_CLR_ATOMIC
238ae4a8e52SRyan Libbymacro atomically clears the bit and returns whether it was set.
2397ebf4122SConrad Meyer.Pp
2407ebf4122SConrad MeyerThe
2417ebf4122SConrad Meyer.Fn BIT_COPY
2427ebf4122SConrad Meyermacro copies the contents of the bitset
2437ebf4122SConrad Meyer.Fa from
2447ebf4122SConrad Meyerto the bitset
2457ebf4122SConrad Meyer.Fa to .
2467ebf4122SConrad Meyer.Fn BIT_COPY_STORE_REL
2477ebf4122SConrad Meyeris similar, but copies component machine words from
2487ebf4122SConrad Meyer.Fa from
2497ebf4122SConrad Meyerand writes them to
2507ebf4122SConrad Meyer.Fa to
2517ebf4122SConrad Meyerwith atomic store with release semantics.
2527ebf4122SConrad Meyer(That is, if
2537ebf4122SConrad Meyer.Fa to
2547ebf4122SConrad Meyeris composed of multiple machine words,
2557ebf4122SConrad Meyer.Fn BIT_COPY_STORE_REL
2567ebf4122SConrad Meyerperforms multiple individually atomic operations.)
2577ebf4122SConrad Meyer.Pp
2587ebf4122SConrad MeyerThe
2597ebf4122SConrad Meyer.Fn BIT_SET
2607ebf4122SConrad Meyermacro sets bit
2617ebf4122SConrad Meyer.Fa bit
2627ebf4122SConrad Meyerin the bitset pointed to by
2637ebf4122SConrad Meyer.Fa bitset .
2647ebf4122SConrad MeyerThe
2657ebf4122SConrad Meyer.Fn BIT_SET_ATOMIC
2667ebf4122SConrad Meyermacro is identical, but the bit is set atomically.
2677ebf4122SConrad MeyerThe
2687ebf4122SConrad Meyer.Fn BIT_SET_ATOMIC_ACQ
2697ebf4122SConrad Meyermacro sets the bit with acquire semantics.
270ae4a8e52SRyan LibbyThe
271ae4a8e52SRyan Libby.Fn BIT_TEST_SET_ATOMIC
272ae4a8e52SRyan Libbymacro atomically sets the bit and returns whether it was set.
2737ebf4122SConrad Meyer.Pp
2747ebf4122SConrad MeyerThe
2757ebf4122SConrad Meyer.Fn BIT_ZERO
2767ebf4122SConrad Meyermacro clears all bits in
2777ebf4122SConrad Meyer.Fa bitset .
2787ebf4122SConrad Meyer.Pp
2797ebf4122SConrad MeyerThe
2807ebf4122SConrad Meyer.Fn BIT_FILL
2817ebf4122SConrad Meyermacro sets all bits in
2827ebf4122SConrad Meyer.Fa bitset .
2837ebf4122SConrad Meyer.Pp
2847ebf4122SConrad MeyerThe
2857ebf4122SConrad Meyer.Fn BIT_SETOF
2867ebf4122SConrad Meyermacro clears all bits in
2877ebf4122SConrad Meyer.Fa bitset
2887ebf4122SConrad Meyerbefore setting only bit
2897ebf4122SConrad Meyer.Fa bit .
2907ebf4122SConrad Meyer.Pp
2917ebf4122SConrad MeyerThe
2927ebf4122SConrad Meyer.Fn BIT_EMPTY
2937ebf4122SConrad Meyermacro returns
2947ebf4122SConrad Meyer.Dv true
2957ebf4122SConrad Meyerif
2967ebf4122SConrad Meyer.Fa bitset
2977ebf4122SConrad Meyeris empty.
2987ebf4122SConrad Meyer.Pp
2997ebf4122SConrad MeyerThe
3007ebf4122SConrad Meyer.Fn BIT_ISFULLSET
3017ebf4122SConrad Meyermacro returns
3027ebf4122SConrad Meyer.Dv true
3037ebf4122SConrad Meyerif
3047ebf4122SConrad Meyer.Fa bitset
3057ebf4122SConrad Meyeris full (all bits set).
3067ebf4122SConrad Meyer.Pp
3077ebf4122SConrad MeyerThe
3087ebf4122SConrad Meyer.Fn BIT_FFS
3097ebf4122SConrad Meyermacro returns the 1-index of the first (lowest) set bit in
3107ebf4122SConrad Meyer.Fa bitset ,
3117ebf4122SConrad Meyeror zero if
3127ebf4122SConrad Meyer.Fa bitset
3137ebf4122SConrad Meyeris empty.
3147ebf4122SConrad MeyerLike with
3157ebf4122SConrad Meyer.Xr ffs 3 ,
3167ebf4122SConrad Meyerto use the non-zero result of
3177ebf4122SConrad Meyer.Fn BIT_FFS
3187ebf4122SConrad Meyeras a
3197ebf4122SConrad Meyer.Fa bit
3207ebf4122SConrad Meyerindex parameter to any other
3217ebf4122SConrad Meyer.Nm
3227ebf4122SConrad Meyermacro, you must subtract one from the result.
3237ebf4122SConrad Meyer.Pp
3247ebf4122SConrad MeyerThe
325f8782001SD Scott Phillips.Fn BIT_FFS_AT
326f8782001SD Scott Phillipsmacro returns the 1-index of the first (lowest) set bit in
327f8782001SD Scott Phillips.Fa bitset ,
328f8782001SD Scott Phillipswhich is greater than the given 1-indexed
329f8782001SD Scott Phillips.Fa start ,
330f8782001SD Scott Phillipsor zero if no bits in
331f8782001SD Scott Phillips.Fa bitset
332f8782001SD Scott Phillipsgreater than
333f8782001SD Scott Phillips.Fa start
334f8782001SD Scott Phillipsare set.
335f8782001SD Scott Phillips.Pp
336f8782001SD Scott PhillipsThe
3371550c622SKonstantin Belousov.Fn BIT_FLS
3381550c622SKonstantin Belousovmacro returns the 1-index of the last (highest) set bit in
3391550c622SKonstantin Belousov.Fa bitset ,
3401550c622SKonstantin Belousovor zero if
3411550c622SKonstantin Belousov.Fa bitset
3421550c622SKonstantin Belousovis empty.
3431550c622SKonstantin BelousovLike with
3441550c622SKonstantin Belousov.Xr fls 3 ,
3451550c622SKonstantin Belousovto use the non-zero result of
3461550c622SKonstantin Belousov.Fn BIT_FLS
3471550c622SKonstantin Belousovas a
3481550c622SKonstantin Belousov.Fa bit
3491550c622SKonstantin Belousovindex parameter to any other
3501550c622SKonstantin Belousov.Nm
3511550c622SKonstantin Belousovmacro, you must subtract one from the result.
3521550c622SKonstantin Belousov.Pp
3531550c622SKonstantin BelousovThe
354dfd3bde5SMark Johnston.Fn BIT_FOREACH_ISSET
355dfd3bde5SMark Johnstonmacro can be used to iterate over all set bits in
356dfd3bde5SMark Johnston.Fa bitset .
357dfd3bde5SMark JohnstonThe index variable
358dfd3bde5SMark Johnston.Fa bit
359dfd3bde5SMark Johnstonmust have been declared with type
360dfd3bde5SMark Johnston.Ft int ,
361dfd3bde5SMark Johnstonand upon each iteration
362dfd3bde5SMark Johnston.Fa bit
363dfd3bde5SMark Johnstonis set to the index of successive set bits.
364dfd3bde5SMark JohnstonThe value of
365dfd3bde5SMark Johnston.Fa bit
366dfd3bde5SMark Johnstonafter the loop terminates is undefined.
367dfd3bde5SMark JohnstonSimilarly,
368dfd3bde5SMark Johnston.Fn BIT_FOREACH_ISCLR
369dfd3bde5SMark Johnstoniterates over all clear bits in
370dfd3bde5SMark Johnston.Fa bitset .
37151425cb2SMark JohnstonIn the loop body, the currently indexed bit may be set or cleared.
37251425cb2SMark JohnstonHowever, setting or clearing bits other than the currently indexed
37351425cb2SMark Johnstonbit does not guarantee that they will or will not be returned in
37451425cb2SMark Johnstonsubsequent iterations of the same loop.
375dfd3bde5SMark Johnston.Pp
376dfd3bde5SMark JohnstonThe
3777ebf4122SConrad Meyer.Fn BIT_COUNT
3787ebf4122SConrad Meyermacro returns the total number of set bits in
3797ebf4122SConrad Meyer.Fa bitset .
3807ebf4122SConrad Meyer.Pp
3817ebf4122SConrad MeyerThe
3827ebf4122SConrad Meyer.Fn BIT_SUBSET
3837ebf4122SConrad Meyermacro returns
3847ebf4122SConrad Meyer.Dv true
3857ebf4122SConrad Meyerif
3867ebf4122SConrad Meyer.Fa needle
3877ebf4122SConrad Meyeris a subset of
3887ebf4122SConrad Meyer.Fa haystack .
3897ebf4122SConrad Meyer.Pp
3907ebf4122SConrad MeyerThe
3917ebf4122SConrad Meyer.Fn BIT_OVERLAP
3927ebf4122SConrad Meyermacro returns
3937ebf4122SConrad Meyer.Dv true
3947ebf4122SConrad Meyerif
3957ebf4122SConrad Meyer.Fa bitset1
3967ebf4122SConrad Meyerand
3977ebf4122SConrad Meyer.Fa bitset2
3987ebf4122SConrad Meyerhave any common bits.
3997ebf4122SConrad Meyer(That is, if
4007ebf4122SConrad Meyer.Fa bitset1
4017ebf4122SConrad MeyerAND
4027ebf4122SConrad Meyer.Fa bitset2
4037ebf4122SConrad Meyeris not the empty set.)
4047ebf4122SConrad Meyer.Pp
4057ebf4122SConrad MeyerThe
4067ebf4122SConrad Meyer.Fn BIT_CMP
4077ebf4122SConrad Meyermacro returns
4087ebf4122SConrad Meyer.Dv true
4097ebf4122SConrad Meyerif
4107ebf4122SConrad Meyer.Fa bitset1
4117ebf4122SConrad Meyeris NOT equal to
4127ebf4122SConrad Meyer.Fa bitset2 .
4137ebf4122SConrad Meyer.Pp
4147ebf4122SConrad MeyerThe
4157ebf4122SConrad Meyer.Fn BIT_OR
4167ebf4122SConrad Meyermacro sets bits present in
4177ebf4122SConrad Meyer.Fa src
4187ebf4122SConrad Meyerin
4197ebf4122SConrad Meyer.Fa dst .
4207ebf4122SConrad Meyer(It is the
4217ebf4122SConrad Meyer.Nm
4227ebf4122SConrad Meyerequivalent of the scalar:
4237ebf4122SConrad Meyer.Fa dst
4247ebf4122SConrad Meyer|=
4257ebf4122SConrad Meyer.Fa src . )
4267ebf4122SConrad Meyer.Fn BIT_OR_ATOMIC
4277ebf4122SConrad Meyeris similar, but sets bits in the component machine words in
4287ebf4122SConrad Meyer.Fa dst
4297ebf4122SConrad Meyeratomically.
4307ebf4122SConrad Meyer(That is, if
4317ebf4122SConrad Meyer.Fa dst
4327ebf4122SConrad Meyeris composed of multiple machine words,
4337ebf4122SConrad Meyer.Fn BIT_OR_ATOMIC
4347ebf4122SConrad Meyerperforms multiple individually atomic operations.)
4357ebf4122SConrad Meyer.Pp
4367ebf4122SConrad MeyerThe
437e058e1c4SKonstantin Belousov.Fn BIT_OR2
438e058e1c4SKonstantin Belousovmacro computes
439e058e1c4SKonstantin Belousov.Fa src1
440e058e1c4SKonstantin Belousovbitwise or
441e058e1c4SKonstantin Belousov.Fa src2
442e058e1c4SKonstantin Belousovand assigns the result to
443e058e1c4SKonstantin Belousov.Fa dst .
444e058e1c4SKonstantin Belousov(It is the
445e058e1c4SKonstantin Belousov.Nm
446e058e1c4SKonstantin Belousovequivalent of the scalar:
447e058e1c4SKonstantin Belousov.Fa dst
448e058e1c4SKonstantin Belousov=
449e058e1c4SKonstantin Belousov.Fa src1
450e058e1c4SKonstantin Belousov|
451e058e1c4SKonstantin Belousov.Fa src2 . )
452e058e1c4SKonstantin Belousov.Pp
453e058e1c4SKonstantin BelousovThe
4547ebf4122SConrad Meyer.Fn BIT_AND
4557ebf4122SConrad Meyermacro clears bits absent from
4567ebf4122SConrad Meyer.Fa src
4577ebf4122SConrad Meyerfrom
4587ebf4122SConrad Meyer.Fa dst .
4597ebf4122SConrad Meyer(It is the
4607ebf4122SConrad Meyer.Nm
4617ebf4122SConrad Meyerequivalent of the scalar:
4627ebf4122SConrad Meyer.Fa dst
4637ebf4122SConrad Meyer&=
4647ebf4122SConrad Meyer.Fa src . )
4657ebf4122SConrad Meyer.Fn BIT_AND_ATOMIC
4667ebf4122SConrad Meyeris similar, with the same atomic semantics as
4677ebf4122SConrad Meyer.Fn BIT_OR_ATOMIC .
4687ebf4122SConrad Meyer.Pp
4697ebf4122SConrad MeyerThe
470e058e1c4SKonstantin Belousov.Fn BIT_AND2
471e058e1c4SKonstantin Belousovmacro computes
472e058e1c4SKonstantin Belousov.Fa src1
473e058e1c4SKonstantin Belousovbitwise and
474e058e1c4SKonstantin Belousov.Fa src2
475e058e1c4SKonstantin Belousovand assigns the result to
476e058e1c4SKonstantin Belousov.Fa dst .
477e058e1c4SKonstantin Belousov(It is the
478e058e1c4SKonstantin Belousov.Nm
479e058e1c4SKonstantin Belousovequivalent of the scalar:
480e058e1c4SKonstantin Belousov.Fa dst
481e058e1c4SKonstantin Belousov=
482e058e1c4SKonstantin Belousov.Fa src1
483e058e1c4SKonstantin Belousov&
484e058e1c4SKonstantin Belousov.Fa src2 . )
485e058e1c4SKonstantin Belousov.Pp
486e058e1c4SKonstantin BelousovThe
4879825eadfSRyan Libby.Fn BIT_ANDNOT
4887ebf4122SConrad Meyermacro clears bits set in
4897ebf4122SConrad Meyer.Fa src
4907ebf4122SConrad Meyerfrom
4917ebf4122SConrad Meyer.Fa dst .
4927ebf4122SConrad Meyer(It is the
4937ebf4122SConrad Meyer.Nm
4947ebf4122SConrad Meyerequivalent of the scalar:
4957ebf4122SConrad Meyer.Fa dst
4967ebf4122SConrad Meyer&=
4977ebf4122SConrad Meyer.Fa ~ src . )
498e058e1c4SKonstantin Belousov.Pp
499e058e1c4SKonstantin BelousovThe
5009825eadfSRyan Libby.Fn BIT_ANDNOT2
501e058e1c4SKonstantin Belousovmacro computes
502e058e1c4SKonstantin Belousov.Fa src1
503e058e1c4SKonstantin Belousovbitwise and not
504e058e1c4SKonstantin Belousov.Fa src2
505e058e1c4SKonstantin Belousovand assigns the result to
506e058e1c4SKonstantin Belousov.Fa dst .
507e058e1c4SKonstantin Belousov(It is the
508e058e1c4SKonstantin Belousov.Nm
509e058e1c4SKonstantin Belousovequivalent of the scalar:
510e058e1c4SKonstantin Belousov.Fa dst
511e058e1c4SKonstantin Belousov=
512e058e1c4SKonstantin Belousov.Fa src1
513e058e1c4SKonstantin Belousov& ~
514e058e1c4SKonstantin Belousov.Fa src2 . )
515e058e1c4SKonstantin Belousov.Pp
516e058e1c4SKonstantin BelousovThe
517e058e1c4SKonstantin Belousov.Fn BIT_XOR
518e058e1c4SKonstantin Belousovmacro toggles bits set in
519e058e1c4SKonstantin Belousov.Fa src
520e058e1c4SKonstantin Belousovin
521e058e1c4SKonstantin Belousov.Fa dst .
522e058e1c4SKonstantin Belousov(It is the
523e058e1c4SKonstantin Belousov.Nm
524e058e1c4SKonstantin Belousovequivalent of the scalar:
525e058e1c4SKonstantin Belousov.Fa dst
526e058e1c4SKonstantin Belousov^=
527e058e1c4SKonstantin Belousov.Fa src . )
528e058e1c4SKonstantin Belousov.Pp
529e058e1c4SKonstantin BelousovThe
530e058e1c4SKonstantin Belousov.Fn BIT_XOR2
531e058e1c4SKonstantin Belousovmacro computes
532e058e1c4SKonstantin Belousov.Fa src1
533e058e1c4SKonstantin Belousovbitwise exclusive or
534e058e1c4SKonstantin Belousov.Fa src2
535e058e1c4SKonstantin Belousovand assigns the result to
536e058e1c4SKonstantin Belousov.Fa dst .
537e058e1c4SKonstantin Belousov(It is the
538e058e1c4SKonstantin Belousov.Nm
539e058e1c4SKonstantin Belousovequivalent of the scalar:
540e058e1c4SKonstantin Belousov.Fa dst
541e058e1c4SKonstantin Belousov=
542e058e1c4SKonstantin Belousov.Fa src1
543e058e1c4SKonstantin Belousov^
544e058e1c4SKonstantin Belousov.Fa src2 . )
5457ebf4122SConrad Meyer.Sh BITSET_T_INITIALIZER EXAMPLE
5467ebf4122SConrad Meyer.Bd -literal
5477ebf4122SConrad MeyerBITSET_DEFINE(_myset, MYSETSIZE);
5487ebf4122SConrad Meyer
5497ebf4122SConrad Meyerstruct _myset myset;
5507ebf4122SConrad Meyer
5517ebf4122SConrad Meyer/* Initialize myset to filled (all bits set) */
5527ebf4122SConrad Meyermyset = BITSET_T_INITIALIZER(BITSET_FSET(__bitset_words(MYSETSIZE)));
5537ebf4122SConrad Meyer
5547ebf4122SConrad Meyer/* Initialize myset to only the lowest bit set */
5557ebf4122SConrad Meyermyset = BITSET_T_INITIALIZER(0x1);
5567ebf4122SConrad Meyer.Ed
5577ebf4122SConrad Meyer.Sh SEE ALSO
5585546be25SConrad Meyer.Xr bitstring 3 ,
5595546be25SConrad Meyer.Xr cpuset 9
5607ebf4122SConrad Meyer.Sh HISTORY
5617ebf4122SConrad MeyerThe
5627ebf4122SConrad Meyer.Nm
5637ebf4122SConrad Meyermacros first appeared in
5647ebf4122SConrad Meyer.Fx 10.0
5657ebf4122SConrad Meyerin January 2014.
5667ebf4122SConrad MeyerThey were MFCed to
5677ebf4122SConrad Meyer.Fx 9.3 ,
5687ebf4122SConrad Meyerreleased in July 2014.
5697ebf4122SConrad Meyer.Pp
5707ebf4122SConrad MeyerThis manual page first appeared in
5717ebf4122SConrad Meyer.Fx 11.0 .
5727ebf4122SConrad Meyer.Sh AUTHORS
5737ebf4122SConrad Meyer.An -nosplit
5747ebf4122SConrad MeyerThe
5757ebf4122SConrad Meyer.Nm
5765546be25SConrad Meyermacros were generalized and pulled out of
5777ebf4122SConrad Meyer.In sys/cpuset.h
5785546be25SConrad Meyeras
5797ebf4122SConrad Meyer.In sys/_bitset.h
5807ebf4122SConrad Meyerand
5817ebf4122SConrad Meyer.In sys/bitset.h
5827ebf4122SConrad Meyerby
5837ebf4122SConrad Meyer.An Attilio Rao Aq Mt attilio@FreeBSD.org .
5847ebf4122SConrad MeyerThis manual page was written by
5857ebf4122SConrad Meyer.An Conrad Meyer Aq Mt cem@FreeBSD.org .
5867ebf4122SConrad Meyer.Sh CAVEATS
5877ebf4122SConrad MeyerThe
5887ebf4122SConrad Meyer.Fa SETSIZE
5897ebf4122SConrad Meyerargument to all of these macros must match the value given to
5907ebf4122SConrad Meyer.Fn BITSET_DEFINE .
5917ebf4122SConrad Meyer.Pp
5927ebf4122SConrad MeyerUnlike every other reference to individual set members, which are zero-indexed,
593f8782001SD Scott Phillips.Fn BIT_FFS ,
594f8782001SD Scott Phillips.Fn BIT_FFS_AT
595e967aa26SKonstantin Belousovand
596e967aa26SKonstantin Belousov.Fn BIT_FLS
597e967aa26SKonstantin Belousovreturn a one-indexed result (or zero if the set is empty).
598*5e04571cSStefan Eßer.Pp
599*5e04571cSStefan EßerIn order to use the macros defined in
600*5e04571cSStefan Eßer.In sys/bitset.h
601*5e04571cSStefan Eßerand
602*5e04571cSStefan Eßer.In sys/_bitset.h
603*5e04571cSStefan Eßerin userland programs,
604*5e04571cSStefan Eßer.Dv _WANT_FREEBSD_BITSET
605*5e04571cSStefan Eßerhas to be defined before including the header files.
606*5e04571cSStefan EßerThis requirements exists to prevent a name space pollution due to macros defined in
607*5e04571cSStefan Eßer.Nm
608*5e04571cSStefan Eßerin programs that include
609*5e04571cSStefan Eßer.In sys/cpuset.h
610*5e04571cSStefan Eßeror
611*5e04571cSStefan Eßer.In sched.h .
612