From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1921) id 87D6938618D7; Fri, 24 Jun 2022 05:42:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 87D6938618D7 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(9): Introduce BIT_FOREACH_ISSET and BIT_FOREACH_ISCLR X-Act-Checkin: newlib-cygwin X-Git-Author: Mark Johnston X-Git-Refname: refs/heads/master X-Git-Oldrev: 7c03cdf47e2a664a193a0c97269a353232d3fd8f X-Git-Newrev: 37a3e59636c01a0246b96e4432670b8afcfb7e80 Message-Id: <20220624054249.87D6938618D7@sourceware.org> Date: Fri, 24 Jun 2022 05:42:49 +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:42:49 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D37a3e59636c= 01a0246b96e4432670b8afcfb7e80 commit 37a3e59636c01a0246b96e4432670b8afcfb7e80 Author: Mark Johnston Date: Tue Sep 21 11:32:23 2021 -0400 bitset(9): Introduce BIT_FOREACH_ISSET and BIT_FOREACH_ISCLR =20 These allow one to non-destructively iterate over the set or clear bits in a bitset. The motivation is that we have several code fragments which iterate over a CPU set like this: =20 while ((cpu =3D CPU_FFS(&cpus)) !=3D 0) { cpu--; CPU_CLR(cpu, &cpus); ; } =20 This is slow since CPU_FFS begins the search at the beginning of the bitset each time. On amd64 and arm64, CPU sets have size 256, so there are four limbs in the bitset and we do a lot of unnecessary scanning. =20 A second problem is that this is destructive, so code which needs to preserve the original set has to make a copy. In particular, we have quite a few functions which take a cpuset_t parameter by value, meaning that each call has to copy the 32 byte cpuset_t. =20 The new macros address both problems. =20 Reviewed by: cem, kib MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D32028 Diff: --- newlib/libc/sys/rtems/include/sys/bitset.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/newlib/libc/sys/rtems/include/sys/bitset.h b/newlib/libc/sys/r= tems/include/sys/bitset.h index 913fa290d..35c21f6ae 100644 --- a/newlib/libc/sys/rtems/include/sys/bitset.h +++ b/newlib/libc/sys/rtems/include/sys/bitset.h @@ -274,6 +274,16 @@ __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_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 =3D { x } }