From patchwork Wed Jul 24 14:29:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Laight X-Patchwork-Id: 13741014 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 44BECC3DA61 for ; Wed, 24 Jul 2024 14:30:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C67806B008A; Wed, 24 Jul 2024 10:30:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C17CA6B008C; Wed, 24 Jul 2024 10:30:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ADF906B0092; Wed, 24 Jul 2024 10:30:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 8F54F6B008A for ; Wed, 24 Jul 2024 10:30:39 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 2A9FFA0A1E for ; Wed, 24 Jul 2024 14:30:39 +0000 (UTC) X-FDA: 82374882198.14.0396D09 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) by imf16.hostedemail.com (Postfix) with ESMTP id BAD81180002 for ; Wed, 24 Jul 2024 14:30:35 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=aculab.com; spf=pass (imf16.hostedemail.com: domain of david.laight@aculab.com designates 185.58.85.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721831400; a=rsa-sha256; cv=none; b=sGV1VDUUOZID+8APsn4rTEV8seqIU13wVig4wofPbw107XbHWB9KEs+V7ZeSYimBKrv38q IGUgXJjfJZlX77zXz3uAv6PKthZDzZckhJq5pDgW3tuI2gmLPLM5umAF0HEBg72iUObDLI 1DVxAADO+eoK+r33NtgEWgZuGe65HiM= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=aculab.com; spf=pass (imf16.hostedemail.com: domain of david.laight@aculab.com designates 185.58.85.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721831400; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ar7j+3KCIrodjDu2pdRMw+d11CVEJAnE4zkCQL7OOdg=; b=5oGFg9I4kl1noqJ0eXJ264EVjAJ2GTCQJhy+Zf2iaVHheC+65JEKLJQqslH8jkbDpcgqDS UvR1imgqiSgUHw3wifz1YVLJNrgruJwTPdpGjaOvcbZHo5uX/wbw5Gmka267EnsvaL/Zty GGYz0/tTzK2P4XoYHUXMcSDUPZAORVo= Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with both STARTTLS and AUTH (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id uk-mta-178-jMukQN13NBi_V9QrS7w-jQ-1; Wed, 24 Jul 2024 15:30:32 +0100 X-MC-Unique: jMukQN13NBi_V9QrS7w-jQ-1 Received: from AcuMS.Aculab.com (10.202.163.4) by AcuMS.aculab.com (10.202.163.4) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Wed, 24 Jul 2024 15:29:52 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Wed, 24 Jul 2024 15:29:52 +0100 From: David Laight To: "'linux-kernel@vger.kernel.org'" , "'Linus Torvalds'" CC: "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , 'Andrew Morton' , "'Andy Shevchenko'" , 'Dan Carpenter' , 'Arnd Bergmann' , "'Jason@zx2c4.com'" , "'hch@infradead.org'" , "'pedro.falcato@gmail.com'" , 'Mateusz Guzik' , "'linux-mm@kvack.org'" Subject: [PATCH 3/7] compiler.h: Add __if_constexpr(expr, if_const, if_not_const) Thread-Topic: [PATCH 3/7] compiler.h: Add __if_constexpr(expr, if_const, if_not_const) Thread-Index: Adrd1gZ7oS190yPcQj+hKNIa4uSuaQ== Date: Wed, 24 Jul 2024 14:29:52 +0000 Message-ID: <9751d18defea406fa698630637d8e7db@AcuMS.aculab.com> References: <23bdb6fc8d884ceebeb6e8b8653b8cfe@AcuMS.aculab.com> In-Reply-To: <23bdb6fc8d884ceebeb6e8b8653b8cfe@AcuMS.aculab.com> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.202.205.107] MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US X-Rspamd-Queue-Id: BAD81180002 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: tzr7k7h8cik4gjbpoofozcr95e4s9nii X-HE-Tag: 1721831435-585945 X-HE-Meta: U2FsdGVkX1/Vj94AOxDqaUffV/0XruxVEl/24GJfye5oj2NrSK5tmlTb8EeNPSDIBbZFmgs3GRHEGZRCY3cgEJrT9RREOGRCbUfQ0OQiN8KVarrv815yv+MKP8MtNZzFYh97b9nyf37uFUpXYS6OxZSt9SjCZy2igSFlxBgwRecI9ExOAAid5t9ofLjhilKWg1CRg2InKXo9iooUKFi3Bk1+Wt733lhyguxjiKSCcv21mlr0FzU0f+zoTBDU5ljuuytuo+4QSsxPKLQXPrQcFyiYMYmGRSw/sqJmRoKITOf9Bb7EF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q6G62y9IpNxQHBCmenPfFdaNJjNJNMmbgX0PSME2FXj8MGPHyT8zjMLYU X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: __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 --- 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 +/** + * __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,