Skip to contentSkip to conversation

lib: bitmap_[empty,full]: remove code duplication

PATCH Superseded Yury Norov 2 participants 5 messages Mar 16, 2015 – Apr 1, 2015

Subsystems (2)
lore
Share & tools
lore

Yury Norov

author maintainer
#0 lore raw

PATCH 1/1 lib: bitmap_[empty,full]: remove code duplication

Function 'bitmap_empty' has it's own implementation. But it's clearly as simple as:

	"find_first_bit(src, nbits) == nbits"

The same is true for 'bitmap_full'.

Underscored versions of 'bitmap_[empty,full]' are not needed anymore and so removed too.

Boot-tested on Core i7-2630QM.

---
 include/linux/bitmap.h | 10 ++--------
 lib/bitmap.c           | 30 ------------------------------
 2 files changed, 2 insertions(+), 38 deletions(-)
include/linux/bitmap.h +2 -8
index dbfbf49..a56e250 100644
@@ -285,18 +285,12 @@ static inline int bitmap_subset(const unsigned long *src1,
 
 static inline int bitmap_empty(const unsigned long *src, unsigned nbits)
 {
-	if (small_const_nbits(nbits))
-		return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
-	else
-		return __bitmap_empty(src, nbits);
+	return find_first_bit(src, nbits) == nbits;
 }
 
 static inline int bitmap_full(const unsigned long *src, unsigned int nbits)
 {
-	if (small_const_nbits(nbits))
-		return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
-	else
-		return __bitmap_full(src, nbits);
+	return find_first_zero_bit(src, nbits) == nbits;
 }
 
 static inline int bitmap_weight(const unsigned long *src, unsigned int nbits)
lib/bitmap.c +0 -30
index d456f4c1..64c0926 100644
@@ -42,36 +42,6 @@
  * for the best explanations of this ordering.
  */
 
-int __bitmap_empty(const unsigned long *bitmap, unsigned int bits)
-{
-	unsigned int k, lim = bits/BITS_PER_LONG;
-	for (k = 0; k < lim; ++k)
-		if (bitmap[k])
-			return 0;
-
-	if (bits % BITS_PER_LONG)
-		if (bitmap[k] & BITMAP_LAST_WORD_MASK(bits))
-			return 0;
-
-	return 1;
-}
-EXPORT_SYMBOL(__bitmap_empty);
-
-int __bitmap_full(const unsigned long *bitmap, unsigned int bits)
-{
-	unsigned int k, lim = bits/BITS_PER_LONG;
-	for (k = 0; k < lim; ++k)
-		if (~bitmap[k])
-			return 0;
-
-	if (bits % BITS_PER_LONG)
-		if (~bitmap[k] & BITMAP_LAST_WORD_MASK(bits))
-			return 0;
-
-	return 1;
-}
-EXPORT_SYMBOL(__bitmap_full);
-
 int __bitmap_equal(const unsigned long *bitmap1,
 		const unsigned long *bitmap2, unsigned int bits)
 {
Signature · 1 line
2.1.0
maintainer
#1 lore raw
Quoted 13 lines · On Mon, 16 Mar 2015 20:56:39 +0300 Yury Norov <[email protected]> wrote:

Function 'bitmap_empty' has it's own implementation. But it's clearly as simple as:

	"find_first_bit(src, nbits) == nbits"

The same is true for 'bitmap_full'.

Underscored versions of 'bitmap_[empty,full]' are not needed anymore and so removed too.

Boot-tested on Core i7-2630QM.

include/linux/bitmap.h static inline int bitmap_subset(const unsigned long *src1, · line 289

Quoted 9 lines
include/linux/bitmap.h +1 -4
@@ -285,18 +285,12 @@ static inline int bitmap_subset(const unsigned long *src1,
 
 static inline int bitmap_empty(const unsigned long *src, unsigned nbits)
 {
-	if (small_const_nbits(nbits))
-		return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
-	else
-		return __bitmap_empty(src, nbits);
+	return find_first_bit(src, nbits) == nbits;
 }

But we lost the small_const_nbits() optimization, and that will be a common case.

Would it be better to do

	if (small_const_nbits(...))
		...
	else
		find_first_bit(...);

?

author maintainer
#2 lore raw

PATCH v2 1/1 lib: bitmap_[empty,full]: remove code duplication

In reply to Yury Norov

Function 'bitmap_empty' has it's own implementation. But it's clearly as simple as:

	"find_first_bit(src, nbits) == nbits"
The same is true for 'bitmap_full'.
---
 include/linux/bitmap.h |  8 ++++----
 lib/bitmap.c           | 30 ------------------------------
 2 files changed, 4 insertions(+), 34 deletions(-)
include/linux/bitmap.h +4 -4
index be4fa5d..ea17cca 100644
@@ -283,16 +283,16 @@ static inline int bitmap_empty(const unsigned long *src, unsigned nbits)
 {
 	if (small_const_nbits(nbits))
 		return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
-	else
-		return __bitmap_empty(src, nbits);
+
+	return find_first_bit(src, nbits) == nbits;
 }
 
 static inline int bitmap_full(const unsigned long *src, unsigned int nbits)
 {
 	if (small_const_nbits(nbits))
 		return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
-	else
-		return __bitmap_full(src, nbits);
+
+	return find_first_zero_bit(src, nbits) == nbits;
 }
 
 static inline int bitmap_weight(const unsigned long *src, unsigned int nbits)
lib/bitmap.c +0 -30
index d456f4c1..64c0926 100644
@@ -42,36 +42,6 @@
  * for the best explanations of this ordering.
  */
 
-int __bitmap_empty(const unsigned long *bitmap, unsigned int bits)
-{
-	unsigned int k, lim = bits/BITS_PER_LONG;
-	for (k = 0; k < lim; ++k)
-		if (bitmap[k])
-			return 0;
-
-	if (bits % BITS_PER_LONG)
-		if (bitmap[k] & BITMAP_LAST_WORD_MASK(bits))
-			return 0;
-
-	return 1;
-}
-EXPORT_SYMBOL(__bitmap_empty);
-
-int __bitmap_full(const unsigned long *bitmap, unsigned int bits)
-{
-	unsigned int k, lim = bits/BITS_PER_LONG;
-	for (k = 0; k < lim; ++k)
-		if (~bitmap[k])
-			return 0;
-
-	if (bits % BITS_PER_LONG)
-		if (~bitmap[k] & BITMAP_LAST_WORD_MASK(bits))
-			return 0;
-
-	return 1;
-}
-EXPORT_SYMBOL(__bitmap_full);
-
 int __bitmap_equal(const unsigned long *bitmap1,
 		const unsigned long *bitmap2, unsigned int bits)
 {
Signature · 1 line
2.1.0
maintainer
#3 lore raw
Quoted 4 lines · On Sun, 29 Mar 2015 05:03:55 +0300 Yury Norov <[email protected]> wrote:

Function 'bitmap_empty' has it's own implementation. But it's clearly as simple as:

	"find_first_bit(src, nbits) == nbits"

The same is true for 'bitmap_full'.

Looks OK.

Please send a Signed-off-by: for this patch.

author maintainer
#4 lore raw
Quoted 9 lines · Andrew Morton
Quoted 4 lines · On Sun, 29 Mar 2015 05:03:55 +0300 Yury Norov <[email protected]> wrote:

Function 'bitmap_empty' has it's own implementation. But it's clearly as simple as:

	"find_first_bit(src, nbits) == nbits"

The same is true for 'bitmap_full'.

Looks OK.

Please send a Signed-off-by: for this patch.

Hello Andrew,

Is it enough?

BR,
Yury Norov