Re: [PATCH 2/2] lib: add test for bitmap_parselist()

From: Rasmus Villemoes
Date: Thu Aug 10 2017 - 03:30:39 EST


> From be0e663b804daff0d0512e72cf94b5143270bd29 Mon Sep 17 00:00:00 2001
> From: Yury Norov <ynorov@xxxxxxxxxxxxxxxxxx>
> Date: Thu, 10 Aug 2017 01:25:46 +0300
> Subject: [PATCH] bitmap: introduce BITMAP_FROM_U64() and use it in test for
> bitmap_parselist()
>
> 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
> +

The 32 bit version probably needs to come in two flavours depending on
little/big endian, no?

Could you also throw in some casts so that the behaviour is consistent
regardless of the type of the expression n, and so that both elements
in the pair are guaranteed to have the same type (unsigned long,
preferably, since that's what we're initializing). I.e., cast n to
(u64), do arithmetic, cast result to (unsigned long).