Skip to content

Commit

Permalink
bitmap_from_arr64
Browse files Browse the repository at this point in the history
Signed-off-by: Yury Norov <yury.norov@gmail.com>
  • Loading branch information
YuryNorov committed Apr 10, 2022
1 parent 42e87f0 commit a136dfa
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
12 changes: 12 additions & 0 deletions include/linux/bitmap.h
Expand Up @@ -275,6 +275,13 @@ static inline void bitmap_copy_clear_tail(unsigned long *dst,
* therefore conversion is not needed when copying data from/to arrays of u32.
*/
#if BITS_PER_LONG == 64
#define bitmap_from_arr64(bitmap, buf, nbits) \
bitmap_copy_clear_tail((unsigned long *) (bitmap), \
(const unsigned long *) (buf), (nbits))
#define bitmap_to_arr64(buf, bitmap, nbits) \
bitmap_copy_clear_tail((unsigned long *) (buf), \
(const unsigned long *) (bitmap), (nbits))

void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf,
unsigned int nbits);
void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap,
Expand All @@ -286,6 +293,11 @@ void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap,
#define bitmap_to_arr32(buf, bitmap, nbits) \
bitmap_copy_clear_tail((unsigned long *) (buf), \
(const unsigned long *) (bitmap), (nbits))

void bitmap_from_arr64(unsigned long *bitmap, const u64 *buf,
unsigned int nbits);
void bitmap_to_arr64(u64 *buf, const unsigned long *bitmap,
unsigned int nbits);
#endif

static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
Expand Down
45 changes: 45 additions & 0 deletions lib/bitmap.c
Expand Up @@ -1533,5 +1533,50 @@ void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap, unsigned int nbits)
buf[halfwords - 1] &= (u32) (UINT_MAX >> ((-nbits) & 31));
}
EXPORT_SYMBOL(bitmap_to_arr32);
#else /* BITS_PER_LONG == 32 */
/**
* bitmap_from_arr64 - copy the contents of u64 array of bits to bitmap
* @bitmap: array of unsigned longs, the destination bitmap
* @buf: array of u64 (in host byte order), the source bitmap
* @nbits: number of bits in @bitmap
*/
void bitmap_from_arr64(unsigned long *bitmap, const u64 *buf, unsigned int nbits)
{
while (nbits > 0) {
u64 val = *buf++;

*bitmap++ = (unsigned long)val;
if (nbits > 32)
*bitmap++ = (unsigned long)(val >> 32);
nbits -= 64;
}

/* Clear tail bits in last word beyond nbits. */
if (nbits % BITS_PER_LONG)
bitmap[-1] &= BITMAP_LAST_WORD_MASK(nbits);
}
EXPORT_SYMBOL(bitmap_from_arr64);

/**
* bitmap_to_arr64 - copy the contents of bitmap to a u64 array of bits
* @buf: array of u64 (in host byte order), the dest bitmap
* @bitmap: array of unsigned longs, the source bitmap
* @nbits: number of bits in @bitmap
*/
void bitmap_to_arr64(u64 *buf, const unsigned long *bitmap, unsigned int nbits)
{
unsigned int i = 0, words = BITS_TO_LONGS(nbits);

while (i < words) {
buf[i/2] = bitmap[i++];
if (i < words)
buf[i/2] |= bitmap[i++] << 32;
}

/* Clear tail bits in last element of array beyond nbits. */
if (nbits % 64)
buf[i - 1] &= GENMASK_ULL(nbits, 0);
}
EXPORT_SYMBOL(bitmap_to_arr64);

#endif

0 comments on commit a136dfa

Please sign in to comment.