diff --git a/share/man/man9/Makefile b/share/man/man9/Makefile --- a/share/man/man9/Makefile +++ b/share/man/man9/Makefile @@ -623,6 +623,8 @@ bitset.9 BIT_FFS.9 \ bitset.9 BIT_FFS_AT.9 \ bitset.9 BIT_FLS.9 \ + bitset.9 BIT_FOREACH_ISSET.9 \ + bitset.9 BIT_FOREACH_ISCLR.9 \ bitset.9 BIT_COUNT.9 \ bitset.9 BIT_SUBSET.9 \ bitset.9 BIT_OVERLAP.9 \ diff --git a/share/man/man9/bitset.9 b/share/man/man9/bitset.9 --- a/share/man/man9/bitset.9 +++ b/share/man/man9/bitset.9 @@ -24,7 +24,7 @@ .\" .\" $FreeBSD$ .\" -.Dd December 31, 2020 +.Dd September 20, 2021 .Dt BITSET 9 .Os .Sh NAME @@ -45,6 +45,8 @@ .Nm BIT_FFS , .Nm BIT_FFS_AT , .Nm BIT_FLS , +.Nm BIT_FOREACH_ISSET , +.Nm BIT_FOREACH_ISCLR , .Nm BIT_COUNT , .Nm BIT_SUBSET , .Nm BIT_OVERLAP , @@ -92,9 +94,18 @@ .Fn BIT_FFS_AT "const SETSIZE" "struct STRUCTNAME *bitset" "long start" .Ft long .Fn BIT_FLS "const SETSIZE" "struct STRUCTNAME *bitset" +.Fo BIT_FOREACH_ISSET +.Fa "const SETSIZE" +.Fa "size_t bit" +.Fa "const struct STRUCTNAME *bitset" +.Fc +.Fo BIT_FOREACH_ISCLR +.Fa "const SETSIZE" +.Fa "size_t bit" +.Fa "const struct STRUCTNAME *bitset" +.Fc .Ft long .Fn BIT_COUNT "const SETSIZE" "struct STRUCTNAME *bitset" -.\" .Ft bool .Fo BIT_SUBSET .Fa "const SETSIZE" "struct STRUCTNAME *haystack" "struct STRUCTNAME *needle" @@ -329,6 +340,25 @@ macro, you must subtract one from the result. .Pp The +.Fn BIT_FOREACH_ISSET +macro can be used to iterate over all set bits in +.Fa bitset . +The index variable +.Fa bit +must have been declared with type +.Ft int , +and upon each iteration +.Fa bit +is set to the index of successive set bits. +The value of +.Fa bit +after the loop terminates is undefined. +Similarly, +.Fn BIT_FOREACH_ISCLR +iterates over all clear bits in +.Fa bitset . +.Pp +The .Fn BIT_COUNT macro returns the total number of set bits in .Fa bitset . diff --git a/sys/sys/bitset.h b/sys/sys/bitset.h --- a/sys/sys/bitset.h +++ b/sys/sys/bitset.h @@ -271,6 +271,16 @@ __count; \ }) +/* Non-destructively loop over all set or clear bits in the set. */ +#define _BIT_FOREACH(_s, i, p, op) \ + for (__size_t __i = 0; __i < __bitset_words(_s); __i++) \ + for (long __j = op((p)->__bits[__i]), __b = ffsl(__j); \ + (i = (__b - 1) + __i * _BITSET_BITS), __j != 0; \ + __j &= ~(1l << i), __b = ffsl(__j)) + +#define BIT_FOREACH_ISSET(_s, i, p) _BIT_FOREACH(_s, i, p, ) +#define BIT_FOREACH_ISCLR(_s, i, p) _BIT_FOREACH(_s, i, p, ~) + #define BITSET_T_INITIALIZER(x) \ { .__bits = { x } }