All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yury Norov <ynorov@nvidia.com>
To: david.laight.linux@gmail.com
Cc: "Geert Uytterhoeven" <geert+renesas@glider.be>,
	"Alexandre Belloni" <alexandre.belloni@bootlin.com>,
	"Jonathan Cameron" <Jonathan.Cameron@huawei.com>,
	"Crt Mori" <cmo@melexis.com>, "Nuno Sá" <nuno.sa@analog.com>,
	"Richard Genoud" <richard.genoud@bootlin.com>,
	"Andy Shevchenko" <andriy.shevchenko@intel.com>,
	"Yury Norov" <yury.norov@gmail.com>,
	"Rasmus Villemoes" <linux@rasmusvillemoes.dk>,
	"Matt Coster" <matt.coster@imgtec.com>,
	"open list" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 1/1] bitfield.h: Ensure FIELD_PREP_CONST() is constant
Date: Fri, 10 Apr 2026 12:55:25 -0400	[thread overview]
Message-ID: <adkrfaO9xfZvhlRj@yury> (raw)
In-Reply-To: <20260410090927.60484-1-david.laight.linux@gmail.com>

+ Matt Coster

On Fri, Apr 10, 2026 at 10:09:27AM +0100, david.laight.linux@gmail.com wrote:
> From: David Laight <david.laight.linux@gmail.com>
> 
> Some versions of gcc report that some expressions involving
> __builtin_ffsll(1ull << 63) are not integer constant expressions.
> 
> Rework FIELD_PREP_CONST() to avoid the issue.
> 
> Signed-off-by: David Laight <david.laight.linux@gmail.com>

Thanks, David.

So, the original patch that spotted the problem is:

https://lore.kernel.org/all/20260409-field-prep-fix-v1-1-f0e9ae64f63c@imgtec.com/

Let's keep Matt in the loop, at least.

Please keep the details from the original patch. "Some versions of
some expressions" style is too uncertain.

> ---
> 
> Note that when 'val' is a variable 'val << constant' is likely
> to execute faster than 'val * (1 << constant)'.
> So the normal FIELD_PREP() is best left alone.

Do you have any numbers? I'd prefer to have the codebase consistent
when possible.

Thanks,
Yury
 
>  include/linux/bitfield.h | 9 ++++-----
>  1 file changed, 4 insertions(+), 5 deletions(-)
> 
> diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
> index 54aeeef1f0ec..f21765851d5c 100644
> --- a/include/linux/bitfield.h
> +++ b/include/linux/bitfield.h
> @@ -45,6 +45,7 @@
>   */
>  
>  #define __bf_shf(x) (__builtin_ffsll(x) - 1)
> +#define __bf_low_bit(mask) ((mask) & (~(mask) + 1))
>  
>  #define __scalar_type_to_unsigned_cases(type)				\
>  		unsigned type:	(unsigned type)0,			\
> @@ -138,8 +139,6 @@
>  		__FIELD_PREP(_mask, _val, "FIELD_PREP: ");		\
>  	})
>  
> -#define __BF_CHECK_POW2(n)	BUILD_BUG_ON_ZERO(((n) & ((n) - 1)) != 0)
> -
>  /**
>   * FIELD_PREP_CONST() - prepare a constant bitfield element
>   * @_mask: shifted mask defining the field's length and position
> @@ -157,11 +156,11 @@
>  		/* mask must be non-zero */				\
>  		BUILD_BUG_ON_ZERO((_mask) == 0) +			\
>  		/* check if value fits */				\
> -		BUILD_BUG_ON_ZERO(~((_mask) >> __bf_shf(_mask)) & (_val)) + \
> +		BUILD_BUG_ON_ZERO(~((_mask) / __bf_low_bit(_mask)) & (_val)) + \
>  		/* check if mask is contiguous */			\
> -		__BF_CHECK_POW2((_mask) + (1ULL << __bf_shf(_mask))) +	\
> +		BUILD_BUG_ON_ZERO((_mask) & ((_mask) + __bf_low_bit(_mask))) + \
>  		/* and create the value */				\
> -		(((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask))	\
> +		(((_val) * __bf_low_bit(_mask)) & (_mask))		\
>  	)
>  
>  /**
> -- 
> 2.39.5

  reply	other threads:[~2026-04-10 16:55 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-10  9:09 [PATCH 1/1] bitfield.h: Ensure FIELD_PREP_CONST() is constant david.laight.linux
2026-04-10 16:55 ` Yury Norov [this message]
2026-04-10 18:45   ` David Laight
2026-04-11  4:24     ` Yury Norov
2026-04-11 10:54       ` David Laight
2026-04-13 16:40         ` Yury Norov
2026-04-13 17:53           ` David Laight

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=adkrfaO9xfZvhlRj@yury \
    --to=ynorov@nvidia.com \
    --cc=Jonathan.Cameron@huawei.com \
    --cc=alexandre.belloni@bootlin.com \
    --cc=andriy.shevchenko@intel.com \
    --cc=cmo@melexis.com \
    --cc=david.laight.linux@gmail.com \
    --cc=geert+renesas@glider.be \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=matt.coster@imgtec.com \
    --cc=nuno.sa@analog.com \
    --cc=richard.genoud@bootlin.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.