Index: share/man/man9/bitset.9 =================================================================== --- share/man/man9/bitset.9 +++ share/man/man9/bitset.9 @@ -24,7 +24,7 @@ .\" .\" $FreeBSD$ .\" -.Dd December 12, 2019 +.Dd Auguest 10, 2020 .Dt BITSET 9 .Os .Sh NAME @@ -43,6 +43,7 @@ .Nm BIT_EMPTY , .Nm BIT_ISFULLSET , .Nm BIT_FFS , +.Nm BIT_FFS_AT , .Nm BIT_FLS , .Nm BIT_COUNT , .Nm BIT_SUBSET , @@ -86,6 +87,8 @@ .Ft int .Fn BIT_FFS "const SETSIZE" "struct STRUCTNAME *bitset" .Ft int +.Fn BIT_FFS_AT "const SETSIZE" "struct STRUCTNAME *bitset" "int start" +.Ft int .Fn BIT_FLS "const SETSIZE" "struct STRUCTNAME *bitset" .Ft int .Fn BIT_COUNT "const SETSIZE" "struct STRUCTNAME *bitset" @@ -285,6 +288,18 @@ macro, you must subtract one from the result. .Pp The +.Fn BIT_FFS_AT +macro returns the 1-index of the first (lowest) set bit in +.Fa bitset , +which is greater than the given +.Fa start , +or zero if no bits in +.Fa bitset +greater than +.Fa start +are set. +.Pp +The .Fn BIT_FLS macro returns the 1-index of the last (highest) set bit in .Fa bitset , @@ -518,7 +533,8 @@ .Fn BITSET_DEFINE . .Pp Unlike every other reference to individual set members, which are zero-indexed, -.Fn BIT_FFS +.Fn BIT_FFS , +.Fn BIT_FFS_AT and .Fn BIT_FLS return a one-indexed result (or zero if the set is empty). Index: sys/sys/bitset.h =================================================================== --- sys/sys/bitset.h +++ sys/sys/bitset.h @@ -222,6 +222,24 @@ __bit; \ }) +#define BIT_FFS_AT(_s, p, start) __extension__ ({ \ + __size_t __i; \ + int __bit; \ + long __test; \ + \ + __bit = 0; \ + __i = __bitset_word(_s, start); \ + if (__i < __bitset_words((_s))) { \ + __test = (p)->__bits[__i] & \ + (~0UL << ((start) % _BITSET_BITS)); \ + while (__test == 0 && ++__i < __bitset_words((_s))) \ + __test = (p)->__bits[__i]; \ + if (__i < __bitset_words((_s))) \ + __bit = ffsl(__test) + __i * _BITSET_BITS; \ + } \ + __bit; \ +}) + #define BIT_FLS(_s, p) __extension__ ({ \ __size_t __i; \ int __bit; \