[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20250131190335.4c18fb3c@pumpkin>
Date: Fri, 31 Jan 2025 19:03:35 +0000
From: David Laight <david.laight.linux@...il.com>
To: Geert Uytterhoeven <geert+renesas@...der.be>
Cc: Michael Turquette <mturquette@...libre.com>, Stephen Boyd
<sboyd@...nel.org>, Nicolas Ferre <nicolas.ferre@...rochip.com>, Alexandre
Belloni <alexandre.belloni@...tlin.com>, Claudiu Beznea
<claudiu.beznea@...on.dev>, Giovanni Cabiddu <giovanni.cabiddu@...el.com>,
Herbert Xu <herbert@...dor.apana.org.au>, "David S . Miller"
<davem@...emloft.net>, Linus Walleij <linus.walleij@...aro.org>, Bartosz
Golaszewski <brgl@...ev.pl>, Joel Stanley <joel@....id.au>, Andrew Jeffery
<andrew@...econstruct.com.au>, Crt Mori <cmo@...exis.com>, Jonathan Cameron
<jic23@...nel.org>, Lars-Peter Clausen <lars@...afoo.de>, Jacky Huang
<ychuang3@...oton.com>, Shan-Chun Hung <schung@...oton.com>, Yury Norov
<yury.norov@...il.com>, Rasmus Villemoes <linux@...musvillemoes.dk>,
Jaroslav Kysela <perex@...ex.cz>, Takashi Iwai <tiwai@...e.com>, Johannes
Berg <johannes@...solutions.net>, Jakub Kicinski <kuba@...nel.org>, Alex
Elder <elder@...e.org>, linux-clk@...r.kernel.org,
linux-arm-kernel@...ts.infradead.org, linux-renesas-soc@...r.kernel.org,
linux-crypto@...r.kernel.org, qat-linux@...el.com,
linux-gpio@...r.kernel.org, linux-aspeed@...ts.ozlabs.org,
linux-iio@...r.kernel.org, linux-sound@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH treewide v2 1/3] bitfield: Add non-constant
field_{prep,get}() helpers
On Fri, 31 Jan 2025 14:46:51 +0100
Geert Uytterhoeven <geert+renesas@...der.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
Powered by blists - more mailing lists