[PATCH] bitset(9): Fix BIT_FLS()

Sebastian Huber sebastian.huber at embedded-brains.de
Tue Jul 11 08:19:08 UTC 2017


Its embarrassing, but I got the loop wrong.  The iteration index is
unsigned, so testing for larger than or equal to zero makes little
sense.
---
 sys/sys/bitset.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/sys/sys/bitset.h b/sys/sys/bitset.h
index 1ed19531b3b..566d9448420 100644
--- a/sys/sys/bitset.h
+++ b/sys/sys/bitset.h
@@ -218,10 +218,10 @@
 	int __bit;							\
 									\
 	__bit = 0;							\
-	for (__i = __bitset_words((_s)) - 1; __i >= 0; __i--) {		\
-		if ((p)->__bits[__i] != 0) {				\
-			__bit = flsl((p)->__bits[__i]);			\
-			__bit += __i * _BITSET_BITS;			\
+	for (__i = __bitset_words((_s)); __i > 0; __i--) {		\
+		if ((p)->__bits[__i - 1] != 0) {			\
+			__bit = flsl((p)->__bits[__i - 1]);		\
+			__bit += (__i - 1) * _BITSET_BITS;		\
 			break;						\
 		}							\
 	}								\
-- 
2.12.3



More information about the freebsd-hackers mailing list