From: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
To: Yury Norov <yury.norov@gmail.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Cc: linux-kernel@vger.kernel.org, linux-sparse@vger.kernel.org,
Rikard Falkeborn <rikard.falkeborn@gmail.com>,
Vincent Mailhol <mailhol.vincent@wanadoo.fr>
Subject: [PATCH v2] linux/bits: simplify GENMASK_INPUT_CHECK()
Date: Tue, 12 Nov 2024 01:43:30 +0900 [thread overview]
Message-ID: <20241111164743.339117-2-mailhol.vincent@wanadoo.fr> (raw)
Because of the shortcut logic of the && operator, below expression:
__builtin_choose_expr(condition, boolean_expression, false)
can be simplified as:
condition && boolean_expression
Applied to GENMASK_INPUT_CHECK(),
__builtin_choose_expr(__is_constexpr((l) > (h)), (l) > (h), 0)
can be replaced by:
__is_constexpr((l) > (h)) && (l) > (h)
Finally, above expression is nearly the same as the expansion of
statically_true((l) > (h)), except from the use of __is_constexpr()
instead of __builtin_constant_p().
Introduce _statically_true() which is similar to statically_true()
but with __is_constexpr(). Apply _statically_true() to simplify
GENMASK_INPUT_CHECK().
Signed-off-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
---
This change passes the unit tests from CONFIG_BITS_TEST, including the
extra negative tests provided under #ifdef TEST_GENMASK_FAILURES [1].
[1] commit 6d511020e13d ("lib/test_bits.c: add tests of GENMASK")
Link: https://git.kernel.org/torvalds/c/6d511020e13d
** Changelog **
v1 -> v2:
- introduce _statically_true(), taking inspiration from
statically_true() as introduced in commit 22f546873149 ("minmax:
improve macro expansion and type checking")
Link: https://lore.kernel.org/all/20240609073513.256179-1-mailhol.vincent@wanadoo.fr/
---
include/linux/bits.h | 5 ++---
include/linux/compiler.h | 1 +
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/include/linux/bits.h b/include/linux/bits.h
index 60044b608817..01713e1eda56 100644
--- a/include/linux/bits.h
+++ b/include/linux/bits.h
@@ -20,9 +20,8 @@
*/
#if !defined(__ASSEMBLY__)
#include <linux/build_bug.h>
-#define GENMASK_INPUT_CHECK(h, l) \
- (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \
- __is_constexpr((l) > (h)), (l) > (h), 0)))
+#include <linux/compiler.h>
+#define GENMASK_INPUT_CHECK(h, l) BUILD_BUG_ON_ZERO(_statically_true((l) > (h)))
#else
/*
* BUILD_BUG_ON_ZERO is not available in h files included from asm files,
diff --git a/include/linux/compiler.h b/include/linux/compiler.h
index 4d4e23b6e3e7..fee66166eca2 100644
--- a/include/linux/compiler.h
+++ b/include/linux/compiler.h
@@ -307,6 +307,7 @@ static inline void *offset_to_ptr(const int *off)
* values to determine that the condition is statically true.
*/
#define statically_true(x) (__builtin_constant_p(x) && (x))
+#define _statically_true(x) (__is_constexpr(x) && (x))
/*
* This is needed in functions which generate the stack canary, see
--
2.45.2
next reply other threads:[~2024-11-11 16:47 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-11 16:43 Vincent Mailhol [this message]
2024-11-11 17:32 ` [PATCH v2] linux/bits: simplify GENMASK_INPUT_CHECK() Linus Torvalds
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=20241111164743.339117-2-mailhol.vincent@wanadoo.fr \
--to=mailhol.vincent@wanadoo.fr \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sparse@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=luc.vanoostenryck@gmail.com \
--cc=rikard.falkeborn@gmail.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).