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 1677ebf4122SConrad Meyer.Sh DESCRIPTION 1687ebf4122SConrad MeyerThe 1697ebf4122SConrad Meyer.Nm 1707ebf4122SConrad Meyerfamily of macros provide a flexible and efficient bitset implementation if the 1717ebf4122SConrad Meyermaximum size of the set is known at compilation. 1727ebf4122SConrad MeyerThroughout this manual page, the name 1737ebf4122SConrad Meyer.Fa SETSIZE 1747ebf4122SConrad Meyerrefers to the size of the bitset in bits. 1757ebf4122SConrad MeyerIndividual bits in bitsets are referenced with indices zero through 1767ebf4122SConrad Meyer.Fa SETSIZE - 1 . 1777ebf4122SConrad MeyerOne example use of 1787ebf4122SConrad Meyer.In sys/bitset.h 1797ebf4122SConrad Meyeris 1807ebf4122SConrad Meyer.In sys/cpuset.h . 1817ebf4122SConrad Meyer.Pp 1827ebf4122SConrad MeyerThe 1837ebf4122SConrad Meyer.Fn BITSET_DEFINE 1847ebf4122SConrad Meyermacro defines a bitset struct 1857ebf4122SConrad Meyer.Fa STRUCTNAME 1867ebf4122SConrad Meyerwith room to represent 1877ebf4122SConrad Meyer.Fa SETSIZE 1887ebf4122SConrad Meyerbits. 1897ebf4122SConrad Meyer.Pp 1907ebf4122SConrad MeyerThe 1917ebf4122SConrad Meyer.Fn BITSET_T_INITIALIZER 1927ebf4122SConrad Meyermacro allows one to initialize a bitset struct with a compile time literal 1937ebf4122SConrad Meyervalue. 1947ebf4122SConrad Meyer.Pp 1957ebf4122SConrad MeyerThe 1967ebf4122SConrad Meyer.Fn BITSET_FSET 1977ebf4122SConrad Meyermacro generates a compile time literal, usable by 1987ebf4122SConrad Meyer.Fn BITSET_T_INITIALIZER , 1997ebf4122SConrad Meyerrepresenting a full bitset (all bits set). 2007ebf4122SConrad MeyerFor examples of 2017ebf4122SConrad Meyer.Fn BITSET_T_INITIALIZER 2027ebf4122SConrad Meyerand 2037ebf4122SConrad Meyer.Fn BITSET_FSET 2047ebf4122SConrad Meyerusage, see the 2057ebf4122SConrad Meyer.Sx BITSET_T_INITIALIZER EXAMPLE 2067ebf4122SConrad Meyersection. 2077ebf4122SConrad MeyerThe 2087ebf4122SConrad Meyer.Fa N_WORDS 2097ebf4122SConrad Meyerparameter to 2107ebf4122SConrad Meyer.Fn BITSET_FSET 2117ebf4122SConrad Meyershould be: 2127ebf4122SConrad Meyer.Bd -literal -offset indent 2137ebf4122SConrad Meyer__bitset_words(SETSIZE) 2147ebf4122SConrad Meyer.Ed 2157ebf4122SConrad Meyer.Pp 2167ebf4122SConrad MeyerThe 2177ebf4122SConrad Meyer.Fn BIT_CLR 2187ebf4122SConrad Meyermacro clears bit 2197ebf4122SConrad Meyer.Fa bit 2207ebf4122SConrad Meyerin the bitset pointed to by 2217ebf4122SConrad Meyer.Fa bitset . 2227ebf4122SConrad MeyerThe 2237ebf4122SConrad Meyer.Fn BIT_CLR_ATOMIC 2247ebf4122SConrad Meyermacro is identical, but the bit is cleared atomically. 225ae4a8e52SRyan LibbyThe 226ae4a8e52SRyan Libby.Fn BIT_TEST_CLR_ATOMIC 227ae4a8e52SRyan Libbymacro atomically clears the bit and returns whether it was set. 2287ebf4122SConrad Meyer.Pp 2297ebf4122SConrad MeyerThe 2307ebf4122SConrad Meyer.Fn BIT_COPY 2317ebf4122SConrad Meyermacro copies the contents of the bitset 2327ebf4122SConrad Meyer.Fa from 2337ebf4122SConrad Meyerto the bitset 2347ebf4122SConrad Meyer.Fa to . 2357ebf4122SConrad Meyer.Fn BIT_COPY_STORE_REL 2367ebf4122SConrad Meyeris similar, but copies component machine words from 2377ebf4122SConrad Meyer.Fa from 2387ebf4122SConrad Meyerand writes them to 2397ebf4122SConrad Meyer.Fa to 2407ebf4122SConrad Meyerwith atomic store with release semantics. 2417ebf4122SConrad Meyer(That is, if 2427ebf4122SConrad Meyer.Fa to 2437ebf4122SConrad Meyeris composed of multiple machine words, 2447ebf4122SConrad Meyer.Fn BIT_COPY_STORE_REL 2457ebf4122SConrad Meyerperforms multiple individually atomic operations.) 2467ebf4122SConrad Meyer.Pp 2477ebf4122SConrad MeyerThe 2487ebf4122SConrad Meyer.Fn BIT_SET 2497ebf4122SConrad Meyermacro sets bit 2507ebf4122SConrad Meyer.Fa bit 2517ebf4122SConrad Meyerin the bitset pointed to by 2527ebf4122SConrad Meyer.Fa bitset . 2537ebf4122SConrad MeyerThe 2547ebf4122SConrad Meyer.Fn BIT_SET_ATOMIC 2557ebf4122SConrad Meyermacro is identical, but the bit is set atomically. 2567ebf4122SConrad MeyerThe 2577ebf4122SConrad Meyer.Fn BIT_SET_ATOMIC_ACQ 2587ebf4122SConrad Meyermacro sets the bit with acquire semantics. 259ae4a8e52SRyan LibbyThe 260ae4a8e52SRyan Libby.Fn BIT_TEST_SET_ATOMIC 261ae4a8e52SRyan Libbymacro atomically sets the bit and returns whether it was set. 2627ebf4122SConrad Meyer.Pp 2637ebf4122SConrad MeyerThe 2647ebf4122SConrad Meyer.Fn BIT_ZERO 2657ebf4122SConrad Meyermacro clears all bits in 2667ebf4122SConrad Meyer.Fa bitset . 2677ebf4122SConrad Meyer.Pp 2687ebf4122SConrad MeyerThe 2697ebf4122SConrad Meyer.Fn BIT_FILL 2707ebf4122SConrad Meyermacro sets all bits in 2717ebf4122SConrad Meyer.Fa bitset . 2727ebf4122SConrad Meyer.Pp 2737ebf4122SConrad MeyerThe 2747ebf4122SConrad Meyer.Fn BIT_SETOF 2757ebf4122SConrad Meyermacro clears all bits in 2767ebf4122SConrad Meyer.Fa bitset 2777ebf4122SConrad Meyerbefore setting only bit 2787ebf4122SConrad Meyer.Fa bit . 2797ebf4122SConrad Meyer.Pp 2807ebf4122SConrad MeyerThe 2817ebf4122SConrad Meyer.Fn BIT_EMPTY 2827ebf4122SConrad Meyermacro returns 2837ebf4122SConrad Meyer.Dv true 2847ebf4122SConrad Meyerif 2857ebf4122SConrad Meyer.Fa bitset 2867ebf4122SConrad Meyeris empty. 2877ebf4122SConrad Meyer.Pp 2887ebf4122SConrad MeyerThe 2897ebf4122SConrad Meyer.Fn BIT_ISFULLSET 2907ebf4122SConrad Meyermacro returns 2917ebf4122SConrad Meyer.Dv true 2927ebf4122SConrad Meyerif 2937ebf4122SConrad Meyer.Fa bitset 2947ebf4122SConrad Meyeris full (all bits set). 2957ebf4122SConrad Meyer.Pp 2967ebf4122SConrad MeyerThe 2977ebf4122SConrad Meyer.Fn BIT_FFS 2987ebf4122SConrad Meyermacro returns the 1-index of the first (lowest) set bit in 2997ebf4122SConrad Meyer.Fa bitset , 3007ebf4122SConrad Meyeror zero if 3017ebf4122SConrad Meyer.Fa bitset 3027ebf4122SConrad Meyeris empty. 3037ebf4122SConrad MeyerLike with 3047ebf4122SConrad Meyer.Xr ffs 3 , 3057ebf4122SConrad Meyerto use the non-zero result of 3067ebf4122SConrad Meyer.Fn BIT_FFS 3077ebf4122SConrad Meyeras a 3087ebf4122SConrad Meyer.Fa bit 3097ebf4122SConrad Meyerindex parameter to any other 3107ebf4122SConrad Meyer.Nm 3117ebf4122SConrad Meyermacro, you must subtract one from the result. 3127ebf4122SConrad Meyer.Pp 3137ebf4122SConrad MeyerThe 314f8782001SD Scott Phillips.Fn BIT_FFS_AT 315f8782001SD Scott Phillipsmacro returns the 1-index of the first (lowest) set bit in 316f8782001SD Scott Phillips.Fa bitset , 317f8782001SD Scott Phillipswhich is greater than the given 1-indexed 318f8782001SD Scott Phillips.Fa start , 319f8782001SD Scott Phillipsor zero if no bits in 320f8782001SD Scott Phillips.Fa bitset 321f8782001SD Scott Phillipsgreater than 322f8782001SD Scott Phillips.Fa start 323f8782001SD Scott Phillipsare set. 324f8782001SD Scott Phillips.Pp 325f8782001SD Scott PhillipsThe 3261550c622SKonstantin Belousov.Fn BIT_FLS 3271550c622SKonstantin Belousovmacro returns the 1-index of the last (highest) set bit in 3281550c622SKonstantin Belousov.Fa bitset , 3291550c622SKonstantin Belousovor zero if 3301550c622SKonstantin Belousov.Fa bitset 3311550c622SKonstantin Belousovis empty. 3321550c622SKonstantin BelousovLike with 3331550c622SKonstantin Belousov.Xr fls 3 , 3341550c622SKonstantin Belousovto use the non-zero result of 3351550c622SKonstantin Belousov.Fn BIT_FLS 3361550c622SKonstantin Belousovas a 3371550c622SKonstantin Belousov.Fa bit 3381550c622SKonstantin Belousovindex parameter to any other 3391550c622SKonstantin Belousov.Nm 3401550c622SKonstantin Belousovmacro, you must subtract one from the result. 3411550c622SKonstantin Belousov.Pp 3421550c622SKonstantin BelousovThe 343dfd3bde5SMark Johnston.Fn BIT_FOREACH_ISSET 344dfd3bde5SMark Johnstonmacro can be used to iterate over all set bits in 345dfd3bde5SMark Johnston.Fa bitset . 346dfd3bde5SMark JohnstonThe index variable 347dfd3bde5SMark Johnston.Fa bit 348dfd3bde5SMark Johnstonmust have been declared with type 349dfd3bde5SMark Johnston.Ft int , 350dfd3bde5SMark Johnstonand upon each iteration 351dfd3bde5SMark Johnston.Fa bit 352dfd3bde5SMark Johnstonis set to the index of successive set bits. 353dfd3bde5SMark JohnstonThe value of 354dfd3bde5SMark Johnston.Fa bit 355dfd3bde5SMark Johnstonafter the loop terminates is undefined. 356dfd3bde5SMark JohnstonSimilarly, 357dfd3bde5SMark Johnston.Fn BIT_FOREACH_ISCLR 358dfd3bde5SMark Johnstoniterates over all clear bits in 359dfd3bde5SMark Johnston.Fa bitset . 360*51425cb2SMark JohnstonIn the loop body, the currently indexed bit may be set or cleared. 361*51425cb2SMark JohnstonHowever, setting or clearing bits other than the currently indexed 362*51425cb2SMark Johnstonbit does not guarantee that they will or will not be returned in 363*51425cb2SMark Johnstonsubsequent iterations of the same loop. 364dfd3bde5SMark Johnston.Pp 365dfd3bde5SMark JohnstonThe 3667ebf4122SConrad Meyer.Fn BIT_COUNT 3677ebf4122SConrad Meyermacro returns the total number of set bits in 3687ebf4122SConrad Meyer.Fa bitset . 3697ebf4122SConrad Meyer.Pp 3707ebf4122SConrad MeyerThe 3717ebf4122SConrad Meyer.Fn BIT_SUBSET 3727ebf4122SConrad Meyermacro returns 3737ebf4122SConrad Meyer.Dv true 3747ebf4122SConrad Meyerif 3757ebf4122SConrad Meyer.Fa needle 3767ebf4122SConrad Meyeris a subset of 3777ebf4122SConrad Meyer.Fa haystack . 3787ebf4122SConrad Meyer.Pp 3797ebf4122SConrad MeyerThe 3807ebf4122SConrad Meyer.Fn BIT_OVERLAP 3817ebf4122SConrad Meyermacro returns 3827ebf4122SConrad Meyer.Dv true 3837ebf4122SConrad Meyerif 3847ebf4122SConrad Meyer.Fa bitset1 3857ebf4122SConrad Meyerand 3867ebf4122SConrad Meyer.Fa bitset2 3877ebf4122SConrad Meyerhave any common bits. 3887ebf4122SConrad Meyer(That is, if 3897ebf4122SConrad Meyer.Fa bitset1 3907ebf4122SConrad MeyerAND 3917ebf4122SConrad Meyer.Fa bitset2 3927ebf4122SConrad Meyeris not the empty set.) 3937ebf4122SConrad Meyer.Pp 3947ebf4122SConrad MeyerThe 3957ebf4122SConrad Meyer.Fn BIT_CMP 3967ebf4122SConrad Meyermacro returns 3977ebf4122SConrad Meyer.Dv true 3987ebf4122SConrad Meyerif 3997ebf4122SConrad Meyer.Fa bitset1 4007ebf4122SConrad Meyeris NOT equal to 4017ebf4122SConrad Meyer.Fa bitset2 . 4027ebf4122SConrad Meyer.Pp 4037ebf4122SConrad MeyerThe 4047ebf4122SConrad Meyer.Fn BIT_OR 4057ebf4122SConrad Meyermacro sets bits present in 4067ebf4122SConrad Meyer.Fa src 4077ebf4122SConrad Meyerin 4087ebf4122SConrad Meyer.Fa dst . 4097ebf4122SConrad Meyer(It is the 4107ebf4122SConrad Meyer.Nm 4117ebf4122SConrad Meyerequivalent of the scalar: 4127ebf4122SConrad Meyer.Fa dst 4137ebf4122SConrad Meyer|= 4147ebf4122SConrad Meyer.Fa src . ) 4157ebf4122SConrad Meyer.Fn BIT_OR_ATOMIC 4167ebf4122SConrad Meyeris similar, but sets bits in the component machine words in 4177ebf4122SConrad Meyer.Fa dst 4187ebf4122SConrad Meyeratomically. 4197ebf4122SConrad Meyer(That is, if 4207ebf4122SConrad Meyer.Fa dst 4217ebf4122SConrad Meyeris composed of multiple machine words, 4227ebf4122SConrad Meyer.Fn BIT_OR_ATOMIC 4237ebf4122SConrad Meyerperforms multiple individually atomic operations.) 4247ebf4122SConrad Meyer.Pp 4257ebf4122SConrad MeyerThe 426e058e1c4SKonstantin Belousov.Fn BIT_OR2 427e058e1c4SKonstantin Belousovmacro computes 428e058e1c4SKonstantin Belousov.Fa src1 429e058e1c4SKonstantin Belousovbitwise or 430e058e1c4SKonstantin Belousov.Fa src2 431e058e1c4SKonstantin Belousovand assigns the result to 432e058e1c4SKonstantin Belousov.Fa dst . 433e058e1c4SKonstantin Belousov(It is the 434e058e1c4SKonstantin Belousov.Nm 435e058e1c4SKonstantin Belousovequivalent of the scalar: 436e058e1c4SKonstantin Belousov.Fa dst 437e058e1c4SKonstantin Belousov= 438e058e1c4SKonstantin Belousov.Fa src1 439e058e1c4SKonstantin Belousov| 440e058e1c4SKonstantin Belousov.Fa src2 . ) 441e058e1c4SKonstantin Belousov.Pp 442e058e1c4SKonstantin BelousovThe 4437ebf4122SConrad Meyer.Fn BIT_AND 4447ebf4122SConrad Meyermacro clears bits absent from 4457ebf4122SConrad Meyer.Fa src 4467ebf4122SConrad Meyerfrom 4477ebf4122SConrad Meyer.Fa dst . 4487ebf4122SConrad Meyer(It is the 4497ebf4122SConrad Meyer.Nm 4507ebf4122SConrad Meyerequivalent of the scalar: 4517ebf4122SConrad Meyer.Fa dst 4527ebf4122SConrad Meyer&= 4537ebf4122SConrad Meyer.Fa src . ) 4547ebf4122SConrad Meyer.Fn BIT_AND_ATOMIC 4557ebf4122SConrad Meyeris similar, with the same atomic semantics as 4567ebf4122SConrad Meyer.Fn BIT_OR_ATOMIC . 4577ebf4122SConrad Meyer.Pp 4587ebf4122SConrad MeyerThe 459e058e1c4SKonstantin Belousov.Fn BIT_AND2 460e058e1c4SKonstantin Belousovmacro computes 461e058e1c4SKonstantin Belousov.Fa src1 462e058e1c4SKonstantin Belousovbitwise and 463e058e1c4SKonstantin Belousov.Fa src2 464e058e1c4SKonstantin Belousovand assigns the result to 465e058e1c4SKonstantin Belousov.Fa dst . 466e058e1c4SKonstantin Belousov(It is the 467e058e1c4SKonstantin Belousov.Nm 468e058e1c4SKonstantin Belousovequivalent of the scalar: 469e058e1c4SKonstantin Belousov.Fa dst 470e058e1c4SKonstantin Belousov= 471e058e1c4SKonstantin Belousov.Fa src1 472e058e1c4SKonstantin Belousov& 473e058e1c4SKonstantin Belousov.Fa src2 . ) 474e058e1c4SKonstantin Belousov.Pp 475e058e1c4SKonstantin BelousovThe 4769825eadfSRyan Libby.Fn BIT_ANDNOT 4777ebf4122SConrad Meyermacro clears bits set in 4787ebf4122SConrad Meyer.Fa src 4797ebf4122SConrad Meyerfrom 4807ebf4122SConrad Meyer.Fa dst . 4817ebf4122SConrad Meyer(It is the 4827ebf4122SConrad Meyer.Nm 4837ebf4122SConrad Meyerequivalent of the scalar: 4847ebf4122SConrad Meyer.Fa dst 4857ebf4122SConrad Meyer&= 4867ebf4122SConrad Meyer.Fa ~ src . ) 487e058e1c4SKonstantin Belousov.Pp 488e058e1c4SKonstantin BelousovThe 4899825eadfSRyan Libby.Fn BIT_ANDNOT2 490e058e1c4SKonstantin Belousovmacro computes 491e058e1c4SKonstantin Belousov.Fa src1 492e058e1c4SKonstantin Belousovbitwise and not 493e058e1c4SKonstantin Belousov.Fa src2 494e058e1c4SKonstantin Belousovand assigns the result to 495e058e1c4SKonstantin Belousov.Fa dst . 496e058e1c4SKonstantin Belousov(It is the 497e058e1c4SKonstantin Belousov.Nm 498e058e1c4SKonstantin Belousovequivalent of the scalar: 499e058e1c4SKonstantin Belousov.Fa dst 500e058e1c4SKonstantin Belousov= 501e058e1c4SKonstantin Belousov.Fa src1 502e058e1c4SKonstantin Belousov& ~ 503e058e1c4SKonstantin Belousov.Fa src2 . ) 504e058e1c4SKonstantin Belousov.Pp 505e058e1c4SKonstantin BelousovThe 506e058e1c4SKonstantin Belousov.Fn BIT_XOR 507e058e1c4SKonstantin Belousovmacro toggles bits set in 508e058e1c4SKonstantin Belousov.Fa src 509e058e1c4SKonstantin Belousovin 510e058e1c4SKonstantin Belousov.Fa dst . 511e058e1c4SKonstantin Belousov(It is the 512e058e1c4SKonstantin Belousov.Nm 513e058e1c4SKonstantin Belousovequivalent of the scalar: 514e058e1c4SKonstantin Belousov.Fa dst 515e058e1c4SKonstantin Belousov^= 516e058e1c4SKonstantin Belousov.Fa src . ) 517e058e1c4SKonstantin Belousov.Pp 518e058e1c4SKonstantin BelousovThe 519e058e1c4SKonstantin Belousov.Fn BIT_XOR2 520e058e1c4SKonstantin Belousovmacro computes 521e058e1c4SKonstantin Belousov.Fa src1 522e058e1c4SKonstantin Belousovbitwise exclusive or 523e058e1c4SKonstantin Belousov.Fa src2 524e058e1c4SKonstantin Belousovand assigns the result to 525e058e1c4SKonstantin Belousov.Fa dst . 526e058e1c4SKonstantin Belousov(It is the 527e058e1c4SKonstantin Belousov.Nm 528e058e1c4SKonstantin Belousovequivalent of the scalar: 529e058e1c4SKonstantin Belousov.Fa dst 530e058e1c4SKonstantin Belousov= 531e058e1c4SKonstantin Belousov.Fa src1 532e058e1c4SKonstantin Belousov^ 533e058e1c4SKonstantin Belousov.Fa src2 . ) 5347ebf4122SConrad Meyer.Sh BITSET_T_INITIALIZER EXAMPLE 5357ebf4122SConrad Meyer.Bd -literal 5367ebf4122SConrad MeyerBITSET_DEFINE(_myset, MYSETSIZE); 5377ebf4122SConrad Meyer 5387ebf4122SConrad Meyerstruct _myset myset; 5397ebf4122SConrad Meyer 5407ebf4122SConrad Meyer/* Initialize myset to filled (all bits set) */ 5417ebf4122SConrad Meyermyset = BITSET_T_INITIALIZER(BITSET_FSET(__bitset_words(MYSETSIZE))); 5427ebf4122SConrad Meyer 5437ebf4122SConrad Meyer/* Initialize myset to only the lowest bit set */ 5447ebf4122SConrad Meyermyset = BITSET_T_INITIALIZER(0x1); 5457ebf4122SConrad Meyer.Ed 5467ebf4122SConrad Meyer.Sh SEE ALSO 5475546be25SConrad Meyer.Xr bitstring 3 , 5485546be25SConrad Meyer.Xr cpuset 9 5497ebf4122SConrad Meyer.Sh HISTORY 5507ebf4122SConrad MeyerThe 5517ebf4122SConrad Meyer.Nm 5527ebf4122SConrad Meyermacros first appeared in 5537ebf4122SConrad Meyer.Fx 10.0 5547ebf4122SConrad Meyerin January 2014. 5557ebf4122SConrad MeyerThey were MFCed to 5567ebf4122SConrad Meyer.Fx 9.3 , 5577ebf4122SConrad Meyerreleased in July 2014. 5587ebf4122SConrad Meyer.Pp 5597ebf4122SConrad MeyerThis manual page first appeared in 5607ebf4122SConrad Meyer.Fx 11.0 . 5617ebf4122SConrad Meyer.Sh AUTHORS 5627ebf4122SConrad Meyer.An -nosplit 5637ebf4122SConrad MeyerThe 5647ebf4122SConrad Meyer.Nm 5655546be25SConrad Meyermacros were generalized and pulled out of 5667ebf4122SConrad Meyer.In sys/cpuset.h 5675546be25SConrad Meyeras 5687ebf4122SConrad Meyer.In sys/_bitset.h 5697ebf4122SConrad Meyerand 5707ebf4122SConrad Meyer.In sys/bitset.h 5717ebf4122SConrad Meyerby 5727ebf4122SConrad Meyer.An Attilio Rao Aq Mt attilio@FreeBSD.org . 5737ebf4122SConrad MeyerThis manual page was written by 5747ebf4122SConrad Meyer.An Conrad Meyer Aq Mt cem@FreeBSD.org . 5757ebf4122SConrad Meyer.Sh CAVEATS 5767ebf4122SConrad MeyerThe 5777ebf4122SConrad Meyer.Fa SETSIZE 5787ebf4122SConrad Meyerargument to all of these macros must match the value given to 5797ebf4122SConrad Meyer.Fn BITSET_DEFINE . 5807ebf4122SConrad Meyer.Pp 5817ebf4122SConrad MeyerUnlike every other reference to individual set members, which are zero-indexed, 582f8782001SD Scott Phillips.Fn BIT_FFS , 583f8782001SD Scott Phillips.Fn BIT_FFS_AT 584e967aa26SKonstantin Belousovand 585e967aa26SKonstantin Belousov.Fn BIT_FLS 586e967aa26SKonstantin Belousovreturn a one-indexed result (or zero if the set is empty). 587