From: David Laight <david.laight.linux@gmail.com>
To: Geert Uytterhoeven <geert+renesas@glider.be>
Cc: Michael Turquette <mturquette@baylibre.com>,
Stephen Boyd <sboyd@kernel.org>,
Nicolas Ferre <nicolas.ferre@microchip.com>,
Alexandre Belloni <alexandre.belloni@bootlin.com>,
Claudiu Beznea <claudiu.beznea@tuxon.dev>,
Giovanni Cabiddu <giovanni.cabiddu@intel.com>,
Herbert Xu <herbert@gondor.apana.org.au>,
"David S . Miller" <davem@davemloft.net>,
Linus Walleij <linus.walleij@linaro.org>,
Bartosz Golaszewski <brgl@bgdev.pl>,
Joel Stanley <joel@jms.id.au>,
Andrew Jeffery <andrew@codeconstruct.com.au>,
Crt Mori <cmo@melexis.com>, Jonathan Cameron <jic23@kernel.org>,
Lars-Peter Clausen <lars@metafoo.de>,
Jacky Huang <ychuang3@nuvoton.com>,
Shan-Chun Hung <schung@nuvoton.com>,
Yury Norov <yury.norov@gmail.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.com>,
Johannes Berg <johannes@sipsolutions.net>,
Jakub Kicinski <kuba@kernel.org>, Alex Elder <elder@ieee.org>,
linux-clk@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
linux-renesas-soc@vger.kernel.org, linux-crypto@vger.kernel.org,
qat-linux@intel.com, linux-gpio@vger.kernel.org,
linux-aspeed@lists.ozlabs.org, linux-iio@vger.kernel.org,
linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH treewide v2 1/3] bitfield: Add non-constant field_{prep,get}() helpers
Date: Fri, 31 Jan 2025 19:03:35 +0000 [thread overview]
Message-ID: <20250131190335.4c18fb3c@pumpkin> (raw)
In-Reply-To: <1824412519cb8791ab428065116927ee7b77cf35.1738329459.git.geert+renesas@glider.be>
On Fri, 31 Jan 2025 14:46:51 +0100
Geert Uytterhoeven <geert+renesas@glider.be> wrote:
> The existing FIELD_{GET,PREP}() macros are limited to compile-time
> constants. However, it is very common to prepare or extract bitfield
> elements where the bitfield mask is not a compile-time constant.
>
> To avoid this limitation, the AT91 clock driver and several other
> drivers already have their own non-const field_{prep,get}() macros.
> Make them available for general use by consolidating them in
> <linux/bitfield.h>, and improve them slightly:
> 1. Avoid evaluating macro parameters more than once,
> 2. Replace "ffs() - 1" by "__ffs()",
> 3. Support 64-bit use on 32-bit architectures.
...
> diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
> index 63928f1732230700..c62324a9fcc81241 100644
> --- a/include/linux/bitfield.h
> +++ b/include/linux/bitfield.h
> @@ -203,4 +203,38 @@ __MAKE_OP(64)
> #undef __MAKE_OP
> #undef ____MAKE_OP
>
> +/**
> + * field_prep() - prepare a bitfield element
> + * @_mask: shifted mask defining the field's length and position
> + * @_val: value to put in the field
> + *
> + * field_prep() masks and shifts up the value. The result should be
> + * combined with other fields of the bitfield using logical OR.
> + * Unlike FIELD_PREP(), @_mask is not limited to a compile-time constant.
> + */
> +#define field_prep(_mask, _val) \
You don't need an _ prefix on the 'parameters' - it doesn't gain anything.
> + ({ \
> + typeof(_mask) __mask = (_mask); \
Use: __auto_type __mask = (_mask);
> + unsigned int __shift = sizeof(_mask) <= 4 ? \
> + __ffs(__mask) : __ffs64(__mask); \
> + (((typeof(_mask))(_val) << __shift) & (__mask)); \
There are a lot of () in that line, perhaps:
__auto_type(__mask) = (_mask);
typeof (__mask) __val = (_val);
unsigned int __shift = ...;
(__val << __shift) & __mask;
Note the typeof (__mask) - avoids line-length 'bloat' when the arguments are non-trivial.
David
next prev parent reply other threads:[~2025-01-31 19:03 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-31 13:46 [PATCH v2 0/3] Non-const bitfield helpers Geert Uytterhoeven
2025-01-31 13:46 ` [PATCH treewide v2 1/3] bitfield: Add non-constant field_{prep,get}() helpers Geert Uytterhoeven
2025-01-31 16:29 ` Alexandre Belloni
2025-01-31 16:32 ` Jonathan Cameron
2025-01-31 19:03 ` David Laight [this message]
2025-02-14 10:28 ` Geert Uytterhoeven
2025-02-02 8:26 ` Vincent Mailhol
2025-02-02 17:53 ` Yury Norov
2025-02-03 7:44 ` Johannes Berg
2025-02-03 13:36 ` Vincent Mailhol
2025-02-03 13:59 ` Geert Uytterhoeven
2025-02-03 15:41 ` Vincent Mailhol
2025-02-03 16:48 ` Yury Norov
2025-02-14 11:03 ` Geert Uytterhoeven
2025-02-14 14:39 ` Yury Norov
2025-02-03 15:31 ` Johannes Berg
2025-02-04 15:30 ` Jakub Kicinski
2025-02-14 11:00 ` Geert Uytterhoeven
2025-01-31 13:46 ` [PATCH v2 2/3] clk: renesas: Use bitfield helpers Geert Uytterhoeven
2025-01-31 13:46 ` [PATCH v2 3/3] soc: " Geert Uytterhoeven
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=20250131190335.4c18fb3c@pumpkin \
--to=david.laight.linux@gmail.com \
--cc=alexandre.belloni@bootlin.com \
--cc=andrew@codeconstruct.com.au \
--cc=brgl@bgdev.pl \
--cc=claudiu.beznea@tuxon.dev \
--cc=cmo@melexis.com \
--cc=davem@davemloft.net \
--cc=elder@ieee.org \
--cc=geert+renesas@glider.be \
--cc=giovanni.cabiddu@intel.com \
--cc=herbert@gondor.apana.org.au \
--cc=jic23@kernel.org \
--cc=joel@jms.id.au \
--cc=johannes@sipsolutions.net \
--cc=kuba@kernel.org \
--cc=lars@metafoo.de \
--cc=linus.walleij@linaro.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-aspeed@lists.ozlabs.org \
--cc=linux-clk@vger.kernel.org \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-gpio@vger.kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-renesas-soc@vger.kernel.org \
--cc=linux-sound@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=mturquette@baylibre.com \
--cc=nicolas.ferre@microchip.com \
--cc=perex@perex.cz \
--cc=qat-linux@intel.com \
--cc=sboyd@kernel.org \
--cc=schung@nuvoton.com \
--cc=tiwai@suse.com \
--cc=ychuang3@nuvoton.com \
--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;
as well as URLs for NNTP newsgroup(s).