[PATCH] linux/bits: simplify GENMASK_INPUT_CHECK()

From: Vincent Mailhol
Date: Sun Jun 09 2024 - 03:35:47 EST


The __builtin_choose_expr() works with a wide range of expressions.
When these are Boolean expressions, some logical simplifications can
be applied.

This is the case of:

__builtin_choose_expr(condition, boolean_expression, false);

which can be simplified to:

condition && boolean_expression;

Because of the shortcut logic of the && operator, this works even if
the condition is, for example, __is_constexpr().

Apply above simplification to GENMASK_INPUT_CHECK().

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

Signed-off-by: Vincent Mailhol <mailhol.vincent@xxxxxxxxxx>
---
include/linux/bits.h | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/include/linux/bits.h b/include/linux/bits.h
index 0eb24d21aac2..4cf27f329579 100644
--- a/include/linux/bits.h
+++ b/include/linux/bits.h
@@ -21,8 +21,7 @@
#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)))
+ BUILD_BUG_ON_ZERO(__is_constexpr((l) > (h)) && (l) > (h))
#else
/*
* BUILD_BUG_ON_ZERO is not available in h files included from asm files,
--
2.43.0