Re: [PATCH v10 01/10] bitops: Introduce the for_each_set_clump8 macro

From: Andy Shevchenko
Date: Fri Mar 22 2019 - 14:23:14 EST


On Thu, Mar 14, 2019 at 09:30:02PM +0900, William Breathitt Gray wrote:
> This macro iterates for each 8-bit group of bits (clump) with set bits,
> within a bitmap memory region. For each iteration, "start" is set to the
> bit offset of the found clump, while the respective clump value is
> stored to the location pointed by "clump". Additionally, the
> bitmap_get_value8 and bitmap_set_value8 functions are introduced to
> respectively get and set an 8-bit value in a bitmap memory region.

> +void bitmap_set_value8(unsigned long *const bitmap, const unsigned int size,
> + const unsigned long value, const unsigned int start)
> +{
> + const size_t index = BIT_WORD(start);
> + const unsigned int offset = start % BITS_PER_LONG;
> + const unsigned int low_width = (offset + 8 > BITS_PER_LONG) ?
> + BITS_PER_LONG - offset : 8;

> + const unsigned long low_mask = GENMASK(offset + low_width - 1, offset);

I think

unsigned long low_mask = GENMASK(low_width - 1, 0) << offset;

will generate better code.

> + const unsigned int high_width = 8 - low_width;
> + const unsigned long high_mask = GENMASK(high_width - 1, 0);
> +
> + /* set lower portion */
> + bitmap[index] &= ~low_mask;
> + bitmap[index] |= value << offset;
> +
> + /* set higher portion if space available in bitmap */
> + if (high_width && start + 8 <= size) {
> + bitmap[index + 1] &= ~high_mask;
> + bitmap[index + 1] |= value >> low_width;
> + }
> +}
> +EXPORT_SYMBOL(bitmap_set_value8);

--
With Best Regards,
Andy Shevchenko