linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: David Laight <David.Laight@ACULAB.COM>
To: "'linux-kernel@vger.kernel.org'" <linux-kernel@vger.kernel.org>,
	"'Linus Torvalds'" <torvalds@linuxfoundation.org>
Cc: "'Matthew Wilcox (Oracle)'" <willy@infradead.org>,
	'Christoph Hellwig' <hch@infradead.org>,
	'Andrew Morton' <akpm@linux-foundation.org>,
	"'Andy Shevchenko'" <andriy.shevchenko@linux.intel.com>,
	'Dan Carpenter' <dan.carpenter@linaro.org>,
	'Arnd Bergmann' <arnd@kernel.org>,
	"'Jason@zx2c4.com'" <Jason@zx2c4.com>,
	"'hch@infradead.org'" <hch@infradead.org>,
	"'pedro.falcato@gmail.com'" <pedro.falcato@gmail.com>,
	'Mateusz Guzik' <mjguzik@gmail.com>,
	"'linux-mm@kvack.org'" <linux-mm@kvack.org>
Subject: [PATCH 3/7] compiler.h: Add __if_constexpr(expr, if_const, if_not_const)
Date: Wed, 24 Jul 2024 14:29:52 +0000	[thread overview]
Message-ID: <9751d18defea406fa698630637d8e7db@AcuMS.aculab.com> (raw)
In-Reply-To: <23bdb6fc8d884ceebeb6e8b8653b8cfe@AcuMS.aculab.com>

__if_constexpr(expr, if_const, if_not_const) returns 'if_const' if 'expr'
is a 'constant integer expression' otherwise 'if_not_const'.
The two values may have different types.

Redefine __is_constextpr(expr) as __if_constexpr(expr, 1, 0).

Implemented using _Generic() for portibility.

Add proper kerndoc comments.

Signed-off-by: David Laight <david.laight@aculab.com>
---
 include/linux/compiler.h | 65 +++++++++++++---------------------------
 1 file changed, 21 insertions(+), 44 deletions(-)

diff --git a/include/linux/compiler.h b/include/linux/compiler.h
index 2594553bb30b..7d559e390011 100644
--- a/include/linux/compiler.h
+++ b/include/linux/compiler.h
@@ -242,52 +242,29 @@ static inline void *offset_to_ptr(const int *off)
 /* &a[0] degrades to a pointer: a different type from an array */
 #define __must_be_array(a)	BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
 
-/*
- * This returns a constant expression while determining if an argument is
- * a constant expression, most importantly without evaluating the argument.
- * Glory to Martin Uecker <Martin.Uecker@med.uni-goettingen.de>
+/**
+ * __if_constexpr - Check whether an expression is an 'integer
+ *		constant expression' 
+ * @expr: Expression to test, not evaluated, can be a pointer
+ * @if_const: return value if constant
+ * @if_not_const: return value if not constant
+ *
+ * The return values @if_const and @if_not_const can have different types.
  *
- * Details:
- * - sizeof() return an integer constant expression, and does not evaluate
- *   the value of its operand; it only examines the type of its operand.
- * - The results of comparing two integer constant expressions is also
- *   an integer constant expression.
- * - The first literal "8" isn't important. It could be any literal value.
- * - The second literal "8" is to avoid warnings about unaligned pointers;
- *   this could otherwise just be "1".
- * - (long)(x) is used to avoid warnings about 64-bit types on 32-bit
- *   architectures.
- * - The C Standard defines "null pointer constant", "(void *)0", as
- *   distinct from other void pointers.
- * - If (x) is an integer constant expression, then the "* 0l" resolves
- *   it into an integer constant expression of value 0. Since it is cast to
- *   "void *", this makes the second operand a null pointer constant.
- * - If (x) is not an integer constant expression, then the second operand
- *   resolves to a void pointer (but not a null pointer constant: the value
- *   is not an integer constant 0).
- * - The conditional operator's third operand, "(int *)8", is an object
- *   pointer (to type "int").
- * - The behavior (including the return type) of the conditional operator
- *   ("operand1 ? operand2 : operand3") depends on the kind of expressions
- *   given for the second and third operands. This is the central mechanism
- *   of the macro:
- *   - When one operand is a null pointer constant (i.e. when x is an integer
- *     constant expression) and the other is an object pointer (i.e. our
- *     third operand), the conditional operator returns the type of the
- *     object pointer operand (i.e. "int *"). Here, within the sizeof(), we
- *     would then get:
- *       sizeof(*((int *)(...))  == sizeof(int)  == 4
- *   - When one operand is a void pointer (i.e. when x is not an integer
- *     constant expression) and the other is an object pointer (i.e. our
- *     third operand), the conditional operator returns a "void *" type.
- *     Here, within the sizeof(), we would then get:
- *       sizeof(*((void *)(...)) == sizeof(void) == 1
- * - The equality comparison to "sizeof(int)" therefore depends on (x):
- *     sizeof(int) == sizeof(int)     (x) was a constant expression
- *     sizeof(int) != sizeof(void)    (x) was not a constant expression
+ * Relies on typeof(x ? NULL : ptr_type) being ptr_type and
+ * typeof(x ? (void *)y : ptr_type) being 'void *'.
+ */
+#define __if_constexpr(expr, if_const, if_not_const)		\
+	_Generic(0 ? ((void *)((long)(expr) * 0l)) : (char *)0,	\
+		char *: (if_const),				\
+		void *: (if_not_const))
+
+/**
+ * __is_constexpr - Return 1 for an 'integer constant expression'
+ *		0 otherwise.
+ * @expr: expression to check, not evaluated
  */
-#define __is_constexpr(x) \
-	(sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
+#define __is_constexpr(expr) __if_constexpr((expr), 1, 0)
 
 /*
  * Whether 'type' is a signed type or an unsigned type. Supports scalar types,
-- 
2.17.1

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)



  parent reply	other threads:[~2024-07-24 14:30 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-24 14:26 [PATCH 0/7] minmax: reduce compilation time David Laight
2024-07-24 14:28 ` [PATCH 1/7] minmax: Put all the clamp() definitions together David Laight
2024-07-24 14:29 ` [PATCH 2/7] minmax: Use _Static_assert() instead of static_assert() David Laight
2024-07-24 14:29 ` David Laight [this message]
2024-07-24 17:32   ` [PATCH 3/7] compiler.h: Add __if_constexpr(expr, if_const, if_not_const) Arnd Bergmann
2024-07-25  9:12     ` David Laight
2024-07-24 19:48   ` Linus Torvalds
2024-07-25  8:45     ` David Laight
2024-07-24 14:30 ` [PATCH 4/7] minmax: Simplify signedness check David Laight
2024-07-24 16:48   ` Arnd Bergmann
2024-07-24 20:02     ` Linus Torvalds
2024-07-25  9:00       ` David Laight
2024-07-25 17:02         ` Linus Torvalds
2024-07-26  9:43           ` Lorenzo Stoakes
2024-07-26 12:57             ` David Laight
2024-07-26 13:27               ` Lorenzo Stoakes
2024-07-25 13:24   ` kernel test robot
2024-07-25 16:39     ` David Laight
2024-07-24 14:31 ` [PATCH 5/7] minmax: Factor out the zero-extension logic from umin/umax David Laight
2024-07-24 14:32 ` [PATCH 6/7] minmax: Optimise _Static_assert() check in clamp() David Laight
2024-07-24 14:33 ` [PATCH 7/7] minmax: minmax: Add __types_ok3() and optimise defines with 3 arguments David Laight
2024-07-24 17:03   ` Arnd Bergmann
2024-07-25  9:07     ` David Laight
2024-07-24 19:34 ` [PATCH 0/7] minmax: reduce compilation time Lorenzo Stoakes
2024-07-24 19:52   ` Linus Torvalds
2024-07-26 18:12     ` Lorenzo Stoakes
2024-07-26 18:24       ` Linus Torvalds
2024-07-26 18:56         ` Lorenzo Stoakes
2024-07-26 19:21           ` Lorenzo Stoakes
2024-07-26 21:36             ` Linus Torvalds
2024-07-26 21:46               ` Jens Axboe
2024-07-26 22:48               ` Linus Torvalds
2024-07-27 15:30                 ` Jens Axboe
2024-07-27 15:38                   ` Jens Axboe
2024-07-27 16:31                     ` Lorenzo Stoakes
2024-07-27 16:36                       ` Jens Axboe
2024-07-27 16:41                         ` Lorenzo Stoakes
2024-07-27 16:52                           ` Jens Axboe
2024-07-27 16:56                             ` Lorenzo Stoakes
2024-07-28 11:32                       ` David Laight
2024-07-27  4:13               ` Linus Torvalds
2024-07-27  4:14                 ` Linus Torvalds
2024-07-27  8:08                 ` David Laight
2024-07-27 18:58                   ` Lorenzo Stoakes
2024-07-27 19:21                     ` Linus Torvalds
2024-07-28 11:17                     ` David Laight
2024-07-28 13:07                       ` Lorenzo Stoakes
2024-07-27 17:33                 ` Matthew Wilcox
2024-07-27 18:16                   ` Linus Torvalds
2024-07-27  8:07             ` Lorenzo Stoakes
2024-07-27 16:26               ` Linus Torvalds
2024-07-27 18:44                 ` Lorenzo Stoakes
2024-07-30  4:10                 ` Linus Torvalds
2024-07-30 10:36                   ` Arnd Bergmann
2024-07-28 17:57           ` Geert Uytterhoeven
2024-07-28 18:43             ` Lorenzo Stoakes
2024-07-26 21:32         ` David Laight
2024-07-26 21:38           ` 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=9751d18defea406fa698630637d8e7db@AcuMS.aculab.com \
    --to=david.laight@aculab.com \
    --cc=Jason@zx2c4.com \
    --cc=akpm@linux-foundation.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=arnd@kernel.org \
    --cc=dan.carpenter@linaro.org \
    --cc=hch@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mjguzik@gmail.com \
    --cc=pedro.falcato@gmail.com \
    --cc=torvalds@linuxfoundation.org \
    --cc=willy@infradead.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