Skip to contentSkip to conversation

Bitmap: Optimized division operation to shift operation

PATCH In review Wang Qing 3 participants 3 messages Apr 15, 2020 Bitmap API

lore
Share & tools
lore

Wang Qing

author
#0 lore raw

PATCH 1/1 Bitmap: Optimized division operation to shift operation

On some processors, the / operate will call the compiler`s div lib, which is low efficient. Bitmap is performance sensitive, We can replace the / operation with shift.

---
 include/linux/bitmap.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
include/linux/bitmap.h +3 -3
index 99058eb..85ff982 100644
@@ -337,7 +337,7 @@ static inline int bitmap_equal(const unsigned long *src1,
 		return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
 	if (__builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
 	    IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
-		return !memcmp(src1, src2, nbits / 8);
+		return !memcmp(src1, src2, nbits >> 3);
 	return __bitmap_equal(src1, src2, nbits);
 }
 
@@ -411,7 +411,7 @@ static __always_inline void bitmap_set(unsigned long *map, unsigned int start,
 		 IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
 		 __builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
 		 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
-		memset((char *)map + start / 8, 0xff, nbits / 8);
+		memset((char *)map + (start >> 3), 0xff, nbits >> 3);
 	else
 		__bitmap_set(map, start, nbits);
 }
@@ -425,7 +425,7 @@ static __always_inline void bitmap_clear(unsigned long *map, unsigned int start,
 		 IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
 		 __builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
 		 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
-		memset((char *)map + start / 8, 0, nbits / 8);
+		memset((char *)map + (start >> 3), 0, nbits >> 3);
 	else
 		__bitmap_clear(map, start, nbits);
 }
Signature · 1 line
2.7.4
#1 lore raw
Quoted 3 lines · Wang Qing

On some processors, the / operate will call the compiler`s div lib, which is low efficient. Bitmap is performance sensitive, We can replace the / operation with shift.

Seems more like bad compilers than useful code changes unless you can specify what compilers and what processors.

include/linux/bitmap.h

Quoted 1 line
include/linux/bitmap.h +0 -0

[]

Quoted 6 lines
include/linux/bitmap.h +1 -1
@@ -337,7 +337,7 @@ static inline int bitmap_equal(const unsigned long *src1,
 		return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
 	if (__builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
 	    IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
-		return !memcmp(src1, src2, nbits / 8);
+		return !memcmp(src1, src2, nbits >> 3);
#2 lore raw
In reply to Wang Qing
Quoted 14 lines · Wang Qing

On some processors, the / operate will call the compiler`s div lib, which is low efficient. Bitmap is performance sensitive, We can replace the / operation with shift.

---
 include/linux/bitmap.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
include/linux/bitmap.h +0 -0
index 99058eb..85ff982 100644

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

Quoted 7 lines
include/linux/bitmap.h +1 -1
@@ -337,7 +337,7 @@ static inline int bitmap_equal(const unsigned long *src1,
 		return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
 	if (__builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
 	    IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
-		return !memcmp(src1, src2, nbits / 8);
+		return !memcmp(src1, src2, nbits >> 3);
 	return __bitmap_equal(src1, src2, nbits);
 }

If your compiler gets this wrong, set it on fire and scatter its remains.