Skip to contentSkip to conversation

bitmap: introduce BITMAP_FROM_U64()

PATCH New [email protected] 1 participant 1 message Sep 8, 2017

Subsystems (2)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

PATCH 86/126 bitmap: introduce BITMAP_FROM_U64()

From: Yury Norov <[email protected]>
Subject: bitmap: introduce BITMAP_FROM_U64()

The macro is the compile-time analogue of bitmap_from_u64() with the same
purpose: convert the 64-bit number to the properly ordered pair of 32-bit parts, suitable for filling the bitmap in 32-bit BE environment.

Use it to make test_bitmap_parselist() correct for 32-bit BE ABIs.

Tested on BE mips/qemu.

[[email protected]: tweak code comment]
Cc Noam Camus <[email protected]>
Cc Rasmus Villemoes <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Mauro Carvalho Chehab <[email protected]>

---

 include/linux/bitmap.h |   32 ++++++++++++++++++++++++++
 lib/test_bitmap.c      |   47 ++++++++++++++++++++++++++-------------
 2 files changed, 64 insertions(+), 15 deletions(-)
include/linux/bitmap.h renamed from include/linux/bitmap.h~bitmap-introduce-bitmap_from_u64 +32 -0
diff -puN include/linux/bitmap.h~bitmap-introduce-bitmap_from_u64 include/linux/bitmap.h
@@ -361,6 +361,38 @@ static inline int bitmap_parse(const cha
 }
 
 /*
+ * BITMAP_FROM_U64() - Represent u64 value in the format suitable for bitmap.
+ *
+ * Linux bitmaps are internally arrays of unsigned longs, i.e. 32-bit
+ * integers in 32-bit environment, and 64-bit integers in 64-bit one.
+ *
+ * There are four combinations of endianness and length of the word in linux
+ * ABIs: LE64, BE64, LE32 and BE32.
+ *
+ * On 64-bit kernels 64-bit LE and BE numbers are naturally ordered in
+ * bitmaps and therefore don't require any special handling.
+ *
+ * On 32-bit kernels 32-bit LE ABI orders lo word of 64-bit number in memory
+ * prior to hi, and 32-bit BE orders hi word prior to lo. The bitmap on the
+ * other hand is represented as an array of 32-bit words and the position of
+ * bit N may therefore be calculated as: word #(N/32) and bit #(N%32) in that
+ * word.  For example, bit #42 is located at 10th position of 2nd word.
+ * It matches 32-bit LE ABI, and we can simply let the compiler store 64-bit
+ * values in memory as it usually does. But for BE we need to swap hi and lo
+ * words manually.
+ *
+ * With all that, the macro BITMAP_FROM_U64() does explicit reordering of hi and
+ * lo parts of u64.  For LE32 it does nothing, and for BE environment it swaps
+ * hi and lo words, as is expected by bitmap.
+ */
+#if __BITS_PER_LONG == 64
+#define BITMAP_FROM_U64(n) (n)
+#else
+#define BITMAP_FROM_U64(n) ((unsigned long) ((u64)(n) & ULONG_MAX)), \
+				((unsigned long) ((u64)(n) >> 32))
+#endif
+
+/*
  * bitmap_from_u64 - Check and swap words within u64.
  *  @mask: source bitmap
  *  @dst:  destination bitmap
lib/test_bitmap.c renamed from lib/test_bitmap.c~bitmap-introduce-bitmap_from_u64 +32 -15

56 lines not on this page · raw message

_