Re: [PATCH 00/21] lib: add alternatives for GENMASK()

From: david laight

Date: Thu Nov 27 2025 - 04:37:35 EST


On Wed, 26 Nov 2025 15:47:28 -0800
Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:

> On Wed, 26 Nov 2025 at 14:17, david laight <david.laight@xxxxxxxxxx> wrote:
> >
> > Mark B. will accuse you of abusing ?: :-)
>
> Compared to '__is_constexpr()', this is child's play. Not *that* is
> abusing the ternary op.
>
> > I've just looked at a .i file.
> > GENMASK() currently expands to 855 chars plus four copies of each argument.
>
> Yeah, I don't love it. It's a horrid macro. But because of the odd
> order of arguments, it needs all that crazy checking.
>
> That said, I do think it could be simplified. Particularly if we just
> make the rule be that GENMASK _has_ to take just constant values.
>
> Right now, I think 99.9% of all users are constants, and we spend a
> lot of effort on the 0.1% that isn't.

There is the other 0.1% that actually need an 'integer constant expression'.
They stop you using ({ ...}).
I doubt there are many (if any) for FIELD_PREP() and FIELD_GET() and using
auto _mask = mask;
would shrink those massively.
(If there are any I suspect you are the only person who can fix them
in one release cycle.)

I've not looked closely at the expansions, but as well as some is_constexpr()
(that could probably be 'statically_true()') there is the _Generic() that
generates u8/u16/u32/u64 based on 'something'.
That is mostly entirely pointless, you just need to pick u32 or u64, the u8/u16
get promoted to 'int' as soon as they are used - so it only makes a difference
to code that looks at the type of the variable/result.

In spite of the faffing, the type of FIELD_GET() is u64.

David

>
> Linus
>