From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1921) id 9B585384F025; Fri, 24 Jun 2022 05:43:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9B585384F025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Sebastian Huber To: newlib-cvs@sourceware.org Subject: [newlib-cygwin] bitset: Reimplement BIT_FOREACH_IS(SET|CLR) X-Act-Checkin: newlib-cygwin X-Git-Author: Mark Johnston X-Git-Refname: refs/heads/master X-Git-Oldrev: 96ddb4055e747b5315c93f33a501d73703bb651a X-Git-Newrev: 27e8401c46015273962b401c04bd17eefed662ed Message-Id: <20220624054304.9B585384F025@sourceware.org> Date: Fri, 24 Jun 2022 05:43:04 +0000 (GMT) X-BeenThere: newlib-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Newlib GIT logs List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 24 Jun 2022 05:43:04 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D27e8401c460= 15273962b401c04bd17eefed662ed commit 27e8401c46015273962b401c04bd17eefed662ed Author: Mark Johnston Date: Sat Oct 16 09:38:26 2021 -0400 bitset: Reimplement BIT_FOREACH_IS(SET|CLR) =20 Eliminate the nested loops and re-implement following a suggestion from rlibby. =20 Add some simple regression tests. =20 Reviewed by: rlibby, kib MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D32472 Diff: --- newlib/libc/sys/rtems/include/sys/bitset.h | 31 ++++++++++++++++++++++++--= ---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/newlib/libc/sys/rtems/include/sys/bitset.h b/newlib/libc/sys/r= tems/include/sys/bitset.h index 35c21f6ae..88cda9dd9 100644 --- a/newlib/libc/sys/rtems/include/sys/bitset.h +++ b/newlib/libc/sys/rtems/include/sys/bitset.h @@ -274,12 +274,31 @@ __count; \ }) =20 -/* Non-destructively loop over all set or clear bits in the set. */ -#define _BIT_FOREACH(_s, i, p, op) \ - for (__size_t __i =3D 0; __i < __bitset_words(_s); __i++) \ - for (long __j =3D op((p)->__bits[__i]), __b =3D ffsl(__j); \ - (i =3D (__b - 1) + __i * _BITSET_BITS), __j !=3D 0; \ - __j &=3D ~(1l << i), __b =3D ffsl(__j)) +#define _BIT_FOREACH_ADVANCE(_s, i, p, op) __extension__ ({ \ + int __found; \ + for (;;) { \ + if (__bits !=3D 0) { \ + int __bit =3D ffsl(__bits) - 1; \ + __bits &=3D ~(1ul << __bit); \ + (i) =3D __i * _BITSET_BITS + __bit; \ + __found =3D 1; \ + break; \ + } \ + if (++__i =3D=3D __bitset_words(_s)) { \ + __found =3D 0; \ + break; \ + } \ + __bits =3D op((p)->__bits[__i]); \ + } \ + __found !=3D 0; \ +}) + +/* + * Non-destructively loop over all set or clear bits in the set. + */ +#define _BIT_FOREACH(_s, i, p, op) \ + for (long __i =3D -1, __bits =3D 0; \ + _BIT_FOREACH_ADVANCE(_s, i, p, op); ) =20 #define BIT_FOREACH_ISSET(_s, i, p) _BIT_FOREACH(_s, i, p, ) #define BIT_FOREACH_ISCLR(_s, i, p) _BIT_FOREACH(_s, i, p, ~)