From: Johannes Berg <johannes@sipsolutions.net>
To: linux-wireless@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, John Garry <john.garry@huawei.com>,
Johannes Berg <johannes.berg@intel.com>
Subject: [PATCH] bitfield: use BUILD_BUG_ON_ZERO()
Date: Fri, 12 Oct 2018 11:21:04 +0200 [thread overview]
Message-ID: <20181012092104.2361-1-johannes@sipsolutions.net> (raw)
From: Johannes Berg <johannes.berg@intel.com>
John Garry requested to be able to use FIELD_PREP() and friends
in constant initializers, so the thing we can do is switch all
the compile-time assertions to use BUILD_BUG_ON_ZERO() instead
of regular BUILD_BUG_ON().
The only downside is that we lose all the error messages, but
it doesn't seem worthwhile to duplicate all of this into e.g.
CONST_FIELD_PREP() (for constants) just for that.
Requested-by: John Garry <john.garry@huawei.com>
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
---
include/linux/bitfield.h | 40 +++++++++++++++-------------------------
1 file changed, 15 insertions(+), 25 deletions(-)
diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
index 3f1ef4450a7c..1840dae6448a 100644
--- a/include/linux/bitfield.h
+++ b/include/linux/bitfield.h
@@ -49,19 +49,15 @@
#define __bf_shf(x) (__builtin_ffsll(x) - 1)
-#define __BF_FIELD_CHECK(_mask, _reg, _val, _pfx) \
- ({ \
- BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask), \
- _pfx "mask is not constant"); \
- BUILD_BUG_ON_MSG((_mask) == 0, _pfx "mask is zero"); \
- BUILD_BUG_ON_MSG(__builtin_constant_p(_val) ? \
- ~((_mask) >> __bf_shf(_mask)) & (_val) : 0, \
- _pfx "value too large for the field"); \
- BUILD_BUG_ON_MSG((_mask) > (typeof(_reg))~0ull, \
- _pfx "type of reg too small for mask"); \
- __BUILD_BUG_ON_NOT_POWER_OF_2((_mask) + \
- (1ULL << __bf_shf(_mask))); \
- })
+#define __BF_CHECK_POW2(n) BUILD_BUG_ON_ZERO(((n) & ((n) - 1)) != 0)
+
+#define __BF_FIELD_CHECK(_mask, _reg, _val) \
+ BUILD_BUG_ON_ZERO(!__builtin_constant_p(_mask)) + \
+ BUILD_BUG_ON_ZERO((_mask) == 0) + \
+ BUILD_BUG_ON_ZERO(__builtin_constant_p(_val) ? \
+ ~((_mask) >> __bf_shf(_mask)) & (_val) : 0) + \
+ BUILD_BUG_ON_ZERO((_mask) > (typeof(_reg))~0ull) + \
+ __BF_CHECK_POW2((_mask) + (1ULL << __bf_shf(_mask)))
/**
* FIELD_FIT() - check if value fits in the field
@@ -71,10 +67,8 @@
* Return: true if @_val can fit inside @_mask, false if @_val is too big.
*/
#define FIELD_FIT(_mask, _val) \
- ({ \
- __BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_FIT: "); \
- !((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask)); \
- })
+ (__BF_FIELD_CHECK(_mask, 0ULL, _val) + \
+ !((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask)))
/**
* FIELD_PREP() - prepare a bitfield element
@@ -85,10 +79,8 @@
* be combined with other fields of the bitfield using logical OR.
*/
#define FIELD_PREP(_mask, _val) \
- ({ \
- __BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_PREP: "); \
- ((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask); \
- })
+ (__BF_FIELD_CHECK(_mask, 0ULL, _val) + \
+ (((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask)))
/**
* FIELD_GET() - extract a bitfield element
@@ -99,10 +91,8 @@
* bitfield passed in as @_reg by masking and shifting it down.
*/
#define FIELD_GET(_mask, _reg) \
- ({ \
- __BF_FIELD_CHECK(_mask, _reg, 0U, "FIELD_GET: "); \
- (typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \
- })
+ (__BF_FIELD_CHECK(_mask, _reg, 0U) + \
+ ((typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask))))
extern void __compiletime_error("value doesn't fit into mask")
__field_overflow(void);
--
2.14.4
next reply other threads:[~2018-10-12 9:21 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-10-12 9:21 Johannes Berg [this message]
2018-10-12 19:45 ` Johannes Berg
2018-10-13 1:22 ` kbuild test robot
2018-10-13 1:26 ` kbuild test robot
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=20181012092104.2361-1-johannes@sipsolutions.net \
--to=johannes@sipsolutions.net \
--cc=johannes.berg@intel.com \
--cc=john.garry@huawei.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-wireless@vger.kernel.org \
/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
all inboxes | Powered by JetHome®