public inbox for newlib-cvs@sourceware.org
help / color / mirror / Atom feed
From: Sebastian Huber <sh@sourceware.org>
To: newlib-cvs@sourceware.org
Subject: [newlib-cygwin] bitset(9): Introduce BIT_FOREACH_ISSET and BIT_FOREACH_ISCLR
Date: Fri, 24 Jun 2022 05:42:49 +0000 (GMT)	[thread overview]
Message-ID: <20220624054249.87D6938618D7@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=37a3e59636c01a0246b96e4432670b8afcfb7e80

commit 37a3e59636c01a0246b96e4432670b8afcfb7e80
Author: Mark Johnston <markj@FreeBSD.org>
Date:   Tue Sep 21 11:32:23 2021 -0400

    bitset(9): Introduce BIT_FOREACH_ISSET and BIT_FOREACH_ISCLR
    
    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:
    
    while ((cpu = CPU_FFS(&cpus)) != 0) {
            cpu--;
            CPU_CLR(cpu, &cpus);
            <do something>;
    }
    
    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.
    
    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.
    
    The new macros address both problems.
    
    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/rtems/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;							\
 })
 
+/* 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 } }


                 reply	other threads:[~2022-06-24  5:42 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220624054249.87D6938618D7@sourceware.org \
    --to=sh@sourceware.org \
    --cc=newlib-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).