Re: [RFC] staging: media: atomisp: Simplyfy masking bit logic

From: Adrian Barnaś
Date: Tue Sep 02 2025 - 09:14:40 EST


> These are supposed to be opposites, right? Subword and inverse Subword.
> You could dress them up to make them look more opposite.

In fact this name is misleading as well. For me it should be named
`clear_subword()`. It is zero-ing the "subword" provided with low and
high bit boundary (start, end), to be then easily replaced with binary
or operation.
And this operation is not an inverse of extracting a subword using the
subword function.

> The problem is (and actually with the (end-1, start) above that it might
> generate a really bad code on some CPUs, so I really, really prefer the way
> when _at least_ one of the parameters is constant.

It would be easier to create a bitmask sing GENMASK_ULL and just
reverse it but if it is not safe, this is what looks fine for me and
works the same as previous function:

// Added a helper to create a mask
static inline unsigned long long subword_bitmask(unsigned int start,
unsigned int end)
{
return GENMASK_ULL(end - 1 , 0) & ~(GENMASK_ULL(start, 0) >> 1);
}

/* inverse subword bits move like this: MSB[xxxx____xxxx]LSB ->
MSB[xxxx0000xxxx]LSB */
static inline unsigned long long
clear_subword(unsigned long long w, unsigned int start, unsigned int end)
{
return w & ~subword_bitmask(start, end);
}

Best regards
Adrian Barnaś