[PATCH] bitmap: introduce BITMAP_FROM_U64() and use it in test for bitmap_parselist()

From: Yury Norov
Date: Wed Aug 09 2017 - 18:25:46 EST


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 to be suitable for filling the bitmap.

Signed-off-by: Yury Norov <ynorov@xxxxxxxxxxxxxxxxxx>
---
include/linux/bitmap.h | 6 ++++++
lib/test_bitmap.c | 23 +++++++++++++++++++----
2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
index 5797ca6fdfe2..bdc487e47de1 100644
--- a/include/linux/bitmap.h
+++ b/include/linux/bitmap.h
@@ -378,6 +378,12 @@ static inline void bitmap_from_u64(unsigned long *dst, u64 mask)
dst[1] = mask >> 32;
}

+#if __BITS_PER_LONG == 64
+#define BITMAP_FROM_U64(n) (n)
+#else
+#define BITMAP_FROM_U64(n) ((n) & ULONG_MAX), ((unsigned long long) (n) >> 32)
+#endif
+
#endif /* __ASSEMBLY__ */

#endif /* __LINUX_BITMAP_H */
diff --git a/lib/test_bitmap.c b/lib/test_bitmap.c
index 5b80dd94e4d1..65b8fcc41fd0 100644
--- a/lib/test_bitmap.c
+++ b/lib/test_bitmap.c
@@ -175,10 +175,25 @@ struct test_bitmap_parselist{
const int flags;
};

-static const unsigned long exp[] = {1, 2, 0x0000ffff, 0xffff0000, 0x55555555,
- 0xaaaaaaaa, 0x11111111, 0x22222222, 0xffffffff,
- 0xfffffffe, 0x3333333311111111, 0xffffffff77777777};
-static const unsigned long exp2[] = {0x3333333311111111, 0xffffffff77777777};
+static const unsigned long exp[] __initconst = {
+ BITMAP_FROM_U64(1),
+ BITMAP_FROM_U64(2),
+ BITMAP_FROM_U64(0x0000ffff),
+ BITMAP_FROM_U64(0xffff0000),
+ BITMAP_FROM_U64(0x55555555),
+ BITMAP_FROM_U64(0xaaaaaaaa),
+ BITMAP_FROM_U64(0x11111111),
+ BITMAP_FROM_U64(0x22222222),
+ BITMAP_FROM_U64(0xffffffff),
+ BITMAP_FROM_U64(0xfffffffe),
+ BITMAP_FROM_U64(0x3333333311111111),
+ BITMAP_FROM_U64(0xffffffff77777777)
+};
+
+static const unsigned long exp2[] __initconst = {
+ BITMAP_FROM_U64(0x3333333311111111),
+ BITMAP_FROM_U64(0xffffffff77777777)
+};

static const struct test_bitmap_parselist parselist_tests[] __initconst = {
{0, "0", &exp[0], 8, 0},
--
2.11.0