From: david laight <david.laight@runbox.com>
To: Rasmus Villemoes <ravi@prevas.dk>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
"Yury Norov (NVIDIA)" <yury.norov@gmail.com>,
Linus Walleij <linus.walleij@linaro.org>,
Nicolas Frattaroli <nicolas.frattaroli@collabora.com>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH 00/21] lib: add alternatives for GENMASK()
Date: Wed, 26 Nov 2025 10:38:32 +0000 [thread overview]
Message-ID: <20251126103832.6581bbc6@pumpkin> (raw)
In-Reply-To: <87ldjtuppl.fsf@prevas.dk>
On Wed, 26 Nov 2025 10:07:34 +0100
Rasmus Villemoes <ravi@prevas.dk> wrote:
> On Sat, Oct 25 2025, Linus Torvalds <torvalds@linux-foundation.org> wrote:
>
> > On Sat, 25 Oct 2025 at 09:40, Yury Norov (NVIDIA) <yury.norov@gmail.com> wrote:
> >>
> >> GENMASK(high, low) notation reflects a common pattern to describe
> >> hardware registers. However, out of drivers context it's confusing and
> >> error-prone.
> >
> > So I obviously approve of the BITS() syntax, and am not a huge fan of
> > the odd "GENMASK()" argument ordering.
> >
> > That said, I'm not convinced about adding the other helpers. I don't
> > think "FIRST_BITS(8)" is any more readable than "BITS(0,7)", and I
> > think there's a real danger to having lots of specialized macros that
> > people then have to know what they mean.
> >
> > I have an absolutely *disgusting* trick to use the syntax
> >
> > BITS(3 ... 25)
> >
> > to do this all, but it's so disgusting and limited that I don't think
> > it's actually reasonable.
> >
> > In case somebody can come up with a cleaner model, here's the trick:
> >
> > #define LOWRANGE_0 0,
> > #define LOWRANGE_1 1,
> > #define LOWRANGE_2 2,
> > #define LOWRANGE_3 3,
> > #define LOWRANGE_4 4,
> > #define LOWRANGE_5 5,
> > // ..and so on
> >
> > #define _HIGH_VAL(x) (sizeof((char[]){[x]=1})-1)
> > #define __FIRST(x, ...) x
> > #define ___LOW_VAL(x) __FIRST(x)
> > #define __LOW_VAL(x) ___LOW_VAL(LOWRANGE_ ##x)
> > #define _LOW_VAL(x) __LOW_VAL(x)
> >
> > #define BITS(x) GENMASK(_HIGH_VAL(x), _LOW_VAL(x))
> >
> > #define TESTVAL 5
> > unsigned long val1 = BITS(3 ... 25);
> > unsigned long val2 = BITS(4);
> > unsigned long val3 = BITS(TESTVAL ... 31);
> >
> > and that syntax with either "3 ... 25" or just a plain "4" does
> > actually work. But only with fairly simple numbers.
> >
> > It doesn't work with more complex expressions (due to the nasty
> > preprocessor pasting hack), and I couldn't figure out a way to make it
> > do so.
>
> It's cute, but no, I also don't know how to make it work without the
> preprocessor concatenation stuff.
>
> There is, however, an alternative that resembles the syntax in data
> sheets even more closely:
>
> #define BITS(low_high) GENMASK((0 ? low_high), (1 ? low_high))
That is both beautiful and horrid...
>
> That'll work for
>
> unsigned long val1 = BITS(3:25);
> unsigned long val3 = BITS(TESTVAL:31);
>
> for most of the things TESTVAL might expand to - I'm not sure what would
> happen if one of the lo/hi values contains ternaries and isn't properly
> parenthesized, but nobody writes stuff like
>
> #define RESET_BIT(rev) rev == 0xaa ? 7 : 9
>
> It doesn't work for the single-bit case, but I don't think it's so bad
> to have to say
>
> unsigned long val2 = BIT(4);
>
> and obviously BITS(4:4) would work as well.
>
> It also doesn't do anything to prevent the hi-lo 11:7 order.
For constants that gets trapped.
If the correct expression in used in GENMASK() the compiler will reject
the negative shift - so doesn't need an explicit test.
Not directly related...
GENMASK() and the FIELD_GET() family need to go on massive diets.
Nest them and the pre-processor generates multi-kb lines.
Somewhere there is a (probably pointless) _Generic() for all the integer types.
(Especially since FIELD_GET() is always u64.)
FIELD_GET(mask, val) is just (val & mask) / (mask & -mask), but even that
really needs mask assigned to a local (just to reduce the line length).
For variable 'mask' you might worry about the cost of the 'divide by power of 2'
but for constants it doesn't matter.
FIELD_PREP(mask, val) is (val * (mask & -mask)) & mask
'Bloat city' comes from:
#define MASK GENMASK(9, 0);
x = FIELD_GET(MASK, MASK);
I've just looked at bitfield.h - no wonder it bloats the world.
95% of it needs deleting :-)
David
>
> Rasmus
>
next prev parent reply other threads:[~2025-11-26 10:38 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-25 16:39 [PATCH 00/21] lib: add alternatives for GENMASK() Yury Norov (NVIDIA)
2025-10-25 16:40 ` [PATCH 01/21] arc: disasm: rename BITS() for FIELD() Yury Norov (NVIDIA)
2025-10-25 16:40 ` [PATCH 02/21] iwlwifi: drop unused BITS() Yury Norov (NVIDIA)
2025-10-25 16:40 ` [PATCH 03/21] select: rename BITS() to FDS_BITS() Yury Norov (NVIDIA)
2025-10-30 9:42 ` Jan Kara
2025-10-25 16:40 ` [PATCH 04/21] ALSA: rename BITS to R_BITS Yury Norov (NVIDIA)
2025-10-25 16:40 ` [PATCH 05/21] zlib: rename BITS() to LOWBITS() Yury Norov (NVIDIA)
2025-10-25 16:40 ` [PATCH 06/21] mfd: prepare to generalize BITS() macro Yury Norov (NVIDIA)
2025-10-25 16:40 ` [PATCH 07/21] bits: Add " Yury Norov (NVIDIA)
2025-10-25 16:40 ` [PATCH 08/21] mfd: drop local " Yury Norov (NVIDIA)
2025-10-25 16:40 ` [PATCH 09/21] bits: generalize BITMAP_{FIRST,LAST}_WORD_MASK Yury Norov (NVIDIA)
2025-10-25 16:40 ` [PATCH 10/21] i2c: nomadik: don't use GENMASK() Yury Norov (NVIDIA)
2025-10-25 16:40 ` [PATCH 11/21] drivers: don't use GENMASK() in FIELD_PREP_WM16() Yury Norov (NVIDIA)
2025-10-27 8:49 ` Jani Nikula
2025-10-27 12:21 ` Nicolas Frattaroli
2025-10-27 13:10 ` Mark Brown
2025-10-25 16:40 ` [PATCH 12/21] bitmap: don't use GENMASK() Yury Norov (NVIDIA)
2025-10-25 16:40 ` [PATCH 13/21] trace: " Yury Norov (NVIDIA)
2025-10-25 16:40 ` [PATCH 14/21] lib: 842: don't use GENMASK_ULL() Yury Norov (NVIDIA)
2025-10-25 16:40 ` [PATCH 15/21] bpf: don't use GENMASK() Yury Norov (NVIDIA)
2025-10-25 16:40 ` [PATCH 16/21] kcsan: " Yury Norov (NVIDIA)
2025-10-25 16:40 ` [PATCH 17/21] mm: " Yury Norov (NVIDIA)
2025-10-25 16:40 ` [PATCH 18/21] fprobe: " Yury Norov (NVIDIA)
2025-10-25 16:40 ` [PATCH 19/21] fs: " Yury Norov (NVIDIA)
2025-10-25 16:40 ` [PATCH 20/21] fortify-string: " Yury Norov (NVIDIA)
2025-10-25 16:40 ` [PATCH 21/21] Docs: add Functions parameters order section Yury Norov (NVIDIA)
2025-10-25 17:56 ` [PATCH 00/21] lib: add alternatives for GENMASK() Linus Torvalds
2025-11-26 9:07 ` Rasmus Villemoes
2025-11-26 10:38 ` david laight [this message]
2025-11-26 19:44 ` Linus Torvalds
2025-11-26 22:17 ` david laight
2025-11-26 23:47 ` Linus Torvalds
2025-11-27 9:37 ` david laight
2025-11-27 14:11 ` Nicolas Frattaroli
2025-11-27 17:29 ` Linus Torvalds
2025-11-27 18:51 ` david laight
2025-11-29 11:46 ` david laight
-- strict thread matches above, loose matches on Subject: below --
2025-10-25 18:01 Alexey Dobriyan
2025-10-25 16:28 Yury Norov (NVIDIA)
2025-10-25 16:37 ` Yury Norov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251126103832.6581bbc6@pumpkin \
--to=david.laight@runbox.com \
--cc=linus.walleij@linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nicolas.frattaroli@collabora.com \
--cc=ravi@prevas.dk \
--cc=torvalds@linux-foundation.org \
--cc=yury.norov@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox