Skip to contentSkip to conversation

bitmap: refine and move BITMAP_{FIRST/LAST}_WORD_MASK

PATCH Applied Wei Yang 3 participants 5 messages Mar 5, 2016 – Sep 29, 2016

lore
Share & tools
lore

Wei Yang

author
#0 lore raw

PATCH 1/1 bitmap: refine and move BITMAP_{FIRST/LAST}_WORD_MASK

According to linux kernel commit <89c1e79eb30> ("linux/bitmap.h: improve BITMAP_{LAST,FIRST}_WORD_MASK"), these two macro could be improved.

This patch takes this change and also move them all in header file.

---
 include/qemu/bitmap.h | 7 ++-----
 util/bitmap.c         | 2 --
 2 files changed, 2 insertions(+), 7 deletions(-)
include/qemu/bitmap.h +2 -5
index 0e33fa5..864982d 100644
@@ -58,11 +58,8 @@
  * find_next_bit(addr, nbits, bit)	Position next set bit in *addr >= bit
  */
 
-#define BITMAP_LAST_WORD_MASK(nbits)                                    \
-    (                                                                   \
-        ((nbits) % BITS_PER_LONG) ?                                     \
-        (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL                       \
-        )
+#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
+#define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
 
 #define DECLARE_BITMAP(name,bits)                  \
         unsigned long name[BITS_TO_LONGS(bits)]
util/bitmap.c +0 -2
index 40aadfb..43ed011 100644
@@ -157,8 +157,6 @@ int slow_bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
     return result != 0;
 }
 
-#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
-
 void bitmap_set(unsigned long *map, long start, long nr)
 {
     unsigned long *p = map + BIT_WORD(start);
Signature · 1 line
2.5.0
#1 lore raw

Is this (quite old) patch still relevant?

Thanks,

/mjt

Quoted 43 lines · Wei Yang

According to linux kernel commit <89c1e79eb30> ("linux/bitmap.h: improve BITMAP_{LAST,FIRST}_WORD_MASK"), these two macro could be improved.

This patch takes this change and also move them all in header file.

---
 include/qemu/bitmap.h | 7 ++-----
 util/bitmap.c         | 2 --
 2 files changed, 2 insertions(+), 7 deletions(-)
include/qemu/bitmap.h +2 -5
index 0e33fa5..864982d 100644
@@ -58,11 +58,8 @@
  * find_next_bit(addr, nbits, bit)	Position next set bit in *addr >= bit
  */
 
-#define BITMAP_LAST_WORD_MASK(nbits)                                    \
-    (                                                                   \
-        ((nbits) % BITS_PER_LONG) ?                                     \
-        (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL                       \
-        )
+#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
+#define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
 
 #define DECLARE_BITMAP(name,bits)                  \
         unsigned long name[BITS_TO_LONGS(bits)]
util/bitmap.c +0 -2
index 40aadfb..43ed011 100644
@@ -157,8 +157,6 @@ int slow_bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
     return result != 0;
 }
 
-#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
-
 void bitmap_set(unsigned long *map, long start, long nr)
 {
     unsigned long *p = map + BIT_WORD(start);
#2 lore raw
Quoted 1 line · Michael Tokarev

Is this (quite old) patch still relevant?

Yes, I think it's a nice cleanup. The interesting bit is that it expands nbits exactly once.

Paolo

Quoted 60 lines

Thanks,

/mjt

Quoted 52 lines · Wei Yang

According to linux kernel commit <89c1e79eb30> ("linux/bitmap.h: improve BITMAP_{LAST,FIRST}_WORD_MASK"), these two macro could be improved.

This patch takes this change and also move them all in header file.

---
 include/qemu/bitmap.h | 7 ++-----
 util/bitmap.c         | 2 --
 2 files changed, 2 insertions(+), 7 deletions(-)
include/qemu/bitmap.h +0 -0
index 0e33fa5..864982d 100644
@@ -58,11 +58,8 @@
  * find_next_bit(addr, nbits, bit)    Position next set bit in *addr
Quoted 1 line

= bit

*/

-#define
BITMAP_LAST_WORD_MASK(nbits) \
-
( \
- ((nbits) % BITS_PER_LONG)
? \
- (1UL<<((nbits) % BITS_PER_LONG))-1 :
~0UL \

include/qemu/bitmap.h +1 -1
-        )
+#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) &

(BITS_PER_LONG - 1)))
+#define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))

#define DECLARE_BITMAP(name,bits) \
unsigned long name[BITS_TO_LONGS(bits)]

util/bitmap.c +0 -2
index 40aadfb..43ed011 100644
@@ -157,8 +157,6 @@ int slow_bitmap_andnot(unsigned long *dst, const
 unsigned long *bitmap1,
     return result != 0;
 }
 
-#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) %
 BITS_PER_LONG))
-
 void bitmap_set(unsigned long *map, long start, long nr)
 {
     unsigned long *p = map + BIT_WORD(start);
#3 lore raw
In reply to Wei Yang
Quoted 4 lines · Wei Yang

According to linux kernel commit <89c1e79eb30> ("linux/bitmap.h: improve BITMAP_{LAST,FIRST}_WORD_MASK"), these two macro could be improved.

This patch takes this change and also move them all in header file.

Applied to -trivial, thank you!

/mjt

author
#4 lore raw
Quoted 8 lines · Michael Tokarev
Quoted 4 lines · Wei Yang

According to linux kernel commit <89c1e79eb30> ("linux/bitmap.h: improve BITMAP_{LAST,FIRST}_WORD_MASK"), these two macro could be improved.

This patch takes this change and also move them all in header file.

Applied to -trivial, thank you!

Thanks :-) Have a good day~

Quoted 1 line

/mjt

Signature · 2 lines
Wei Yang
Help you, Help me