public inbox for newlib-cvs@sourceware.org
help / color / mirror / Atom feed
* [newlib-cygwin] bitset: Reimplement BIT_FOREACH_IS(SET|CLR)
@ 2022-06-24  5:43 Sebastian Huber
  0 siblings, 0 replies; only message in thread
From: Sebastian Huber @ 2022-06-24  5:43 UTC (permalink / raw)
  To: newlib-cvs

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

commit 27e8401c46015273962b401c04bd17eefed662ed
Author: Mark Johnston <markj@FreeBSD.org>
Date:   Sat Oct 16 09:38:26 2021 -0400

    bitset: Reimplement BIT_FOREACH_IS(SET|CLR)
    
    Eliminate the nested loops and re-implement following a suggestion from
    rlibby.
    
    Add some simple regression tests.
    
    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/rtems/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;							\
 })
 
-/* 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_ADVANCE(_s, i, p, op) __extension__ ({		\
+	int __found;							\
+	for (;;) {							\
+		if (__bits != 0) {					\
+			int __bit = ffsl(__bits) - 1;			\
+			__bits &= ~(1ul << __bit);			\
+			(i) = __i * _BITSET_BITS + __bit;		\
+			__found = 1;					\
+			break;						\
+		}							\
+		if (++__i == __bitset_words(_s)) {			\
+			__found = 0;					\
+			break;						\
+		}							\
+		__bits = op((p)->__bits[__i]);				\
+	}								\
+	__found != 0;							\
+})
+
+/*
+ * Non-destructively loop over all set or clear bits in the set.
+ */
+#define _BIT_FOREACH(_s, i, p, op)					\
+	for (long __i = -1, __bits = 0;					\
+	    _BIT_FOREACH_ADVANCE(_s, i, p, op); )
 
 #define	BIT_FOREACH_ISSET(_s, i, p)	_BIT_FOREACH(_s, i, p, )
 #define	BIT_FOREACH_ISCLR(_s, i, p)	_BIT_FOREACH(_s, i, p, ~)


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-24  5:43 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-24  5:43 [newlib-cygwin] bitset: Reimplement BIT_FOREACH_IS(SET|CLR) Sebastian Huber

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).