public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: david.laight.linux@gmail.com
To: "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>,
	linux-kernel@vger.kernel.org (open list)
Cc: David Laight <david.laight.linux@gmail.com>
Subject: [PATCH 1/1] bitfield.h: Ensure FIELD_PREP_CONST() is constant
Date: Fri, 10 Apr 2026 10:09:27 +0100	[thread overview]
Message-ID: <20260410090927.60484-1-david.laight.linux@gmail.com> (raw)

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>
---

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.

 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  9:09 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20260410090927.60484-1-david.laight.linux@gmail.com \
    --to=david.laight.linux@gmail.com \
    --cc=Jonathan.Cameron@huawei.com \
    --cc=alexandre.belloni@bootlin.com \
    --cc=andriy.shevchenko@intel.com \
    --cc=cmo@melexis.com \
    --cc=geert+renesas@glider.be \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox