public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Michal Schmidt <mschmidt@redhat.com>
To: Yury Norov <yury.norov@gmail.com>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH] bitfield.h: add FIELD_MAX_CONST
Date: Wed, 15 May 2024 19:27:31 +0200	[thread overview]
Message-ID: <20240515172732.288391-1-mschmidt@redhat.com> (raw)

FIELD_MAX_CONST is like FIELD_MAX, but it can be used where statement
expressions are forbidden. For example, using FIELD_MAX in a
static_assert gives:
  error: braced-group within expression allowed only inside a function

It can be used also in array declarations, where using FIELD_MAX would
trigger a warning :
  warning: ISO C90 forbids variable length array ‘buf’ [-Wvla]
(It's a bit surprising, because despite the warning, gcc calculated
the array size correctly at compile time.)

A simplified example of what I actually want to use in a driver:
  #define DATA_SIZE_M GENMASK(3, 0)
  #define MAX_DATA_SIZE FIELD_MAX_CONST(DATA_SIZE_M)
  static void f(void) {
  	char buf[MAX_DATA_SIZE];
  	/* ... */
  }

In the implementation, reuse the existing compile-time checks from
FIELD_PREP_CONST.

Signed-off-by: Michal Schmidt <mschmidt@redhat.com>
---
 include/linux/bitfield.h | 34 +++++++++++++++++++++++++++-------
 1 file changed, 27 insertions(+), 7 deletions(-)

diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
index 63928f173223..50bbab317319 100644
--- a/include/linux/bitfield.h
+++ b/include/linux/bitfield.h
@@ -76,6 +76,16 @@
 					      (1ULL << __bf_shf(_mask))); \
 	})
 
+#define __BF_FIELD_CHECK_CONST(_mask, _val)				\
+	(								\
+		/* mask must be non-zero */				\
+		BUILD_BUG_ON_ZERO((_mask) == 0) +			\
+		/* check if value fits */				\
+		BUILD_BUG_ON_ZERO(~((_mask) >> __bf_shf(_mask)) & (_val)) + \
+		/* check if mask is contiguous */			\
+		__BF_CHECK_POW2((_mask) + (1ULL << __bf_shf(_mask)))	\
+	)
+
 /**
  * FIELD_MAX() - produce the maximum value representable by a field
  * @_mask: shifted mask defining the field's length and position
@@ -89,6 +99,22 @@
 		(typeof(_mask))((_mask) >> __bf_shf(_mask));		\
 	})
 
+/**
+ * FIELD_MAX_CONST() - produce the maximum value representable by a field
+ * @_mask: shifted mask defining the field's length and position
+ *
+ * FIELD_MAX_CONST() returns the maximum value that can be held in
+ * the field specified by @_mask.
+ *
+ * Unlike FIELD_MAX(), it can be used where statement expressions can't.
+ * Error checking is less comfortable for this version.
+ */
+#define FIELD_MAX_CONST(_mask)						\
+	(								\
+		__BF_FIELD_CHECK_CONST(_mask, 0ULL) +			\
+		(typeof(_mask))((_mask) >> __bf_shf(_mask))		\
+	)
+
 /**
  * FIELD_FIT() - check if value fits in the field
  * @_mask: shifted mask defining the field's length and position
@@ -132,13 +158,7 @@
  */
 #define FIELD_PREP_CONST(_mask, _val)					\
 	(								\
-		/* mask must be non-zero */				\
-		BUILD_BUG_ON_ZERO((_mask) == 0) +			\
-		/* check if value fits */				\
-		BUILD_BUG_ON_ZERO(~((_mask) >> __bf_shf(_mask)) & (_val)) + \
-		/* check if mask is contiguous */			\
-		__BF_CHECK_POW2((_mask) + (1ULL << __bf_shf(_mask))) +	\
-		/* and create the value */				\
+		__BF_FIELD_CHECK_CONST(_mask, _val) +			\
 		(((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask))	\
 	)
 
-- 
2.44.0


             reply	other threads:[~2024-05-15 17:28 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-15 17:27 Michal Schmidt [this message]
2024-05-20 19:29 ` [PATCH] bitfield.h: add FIELD_MAX_CONST Yury Norov
2024-05-20 22:26   ` Jakub Kicinski
2024-05-21 12:33   ` Alex Elder
2024-05-21 14:44     ` Michal Schmidt
2024-05-21 19:52       ` 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=20240515172732.288391-1-mschmidt@redhat.com \
    --to=mschmidt@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --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