Skip to contentSkip to conversation

linux/bits: simplify GENMASK_INPUT_CHECK()

PATCH Superseded Vincent Mailhol 1 participant 1 message Jun 9, 2024 Bitmap API

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux/bits: simplify GENMASK_INPUT_CHECK()

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

---
 include/linux/bits.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
include/linux/bits.h +1 -2
index 0eb24d21aac2..4cf27f329579 100644
@@ -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,
Signature · 1 line
2.43.0