From patchwork Sun Jul 28 14:19: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: 13744004 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 619B1C3DA64 for ; Sun, 28 Jul 2024 14:20:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EE9656B007B; Sun, 28 Jul 2024 10:20:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E98E36B0083; Sun, 28 Jul 2024 10:20:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D60C26B0085; Sun, 28 Jul 2024 10:20:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id BA3FB6B007B for ; Sun, 28 Jul 2024 10:20:44 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 6EBEFA02F5 for ; Sun, 28 Jul 2024 14:20:44 +0000 (UTC) X-FDA: 82389372408.09.E68D8D6 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) by imf25.hostedemail.com (Postfix) with ESMTP id 3118BA0019 for ; Sun, 28 Jul 2024 14:20:41 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=none; spf=pass (imf25.hostedemail.com: domain of david.laight@aculab.com designates 185.58.85.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com; dmarc=pass (policy=none) header.from=aculab.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722176415; a=rsa-sha256; cv=none; b=dFvNjwporjQ57msI1aIlHIT/O4oaRg2fjDAWTMP3MftpqhxELxmyWnoBDjs3j1eb34q6KY TSXA71G1lvDu4s0EiIqq0xDcwn0B1XR09WvXmpyeIydT1XtBmqBH5M6pAChfV0iqrrZz02 rtKISpYqQTyO/YyZUF5Qeg1toue8Fqw= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=none; spf=pass (imf25.hostedemail.com: domain of david.laight@aculab.com designates 185.58.85.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com; dmarc=pass (policy=none) header.from=aculab.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722176415; 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=i+qXpk5ITjdTzby+FlajwQpXJK3tpsKkVHmafvHvxRg=; b=Ymj3e6O47DPLTn4ZunS1JFThgQR37IgpRw/dqXc8t7vq/OqGMeN7082vbTtQPy8RPWHT+7 QAGXRDtjUgdXcvQuNnCyOj6+L2fOWEyteIx83xlmbJoJ0EzSavaUe8R8Ekje72w0GBvIrE jHU3Eh1ofwc9Mv4FuNHc1ulris/AY2w= 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-176-ASX4DscfNW6QCi1PP5Mc6g-1; Sun, 28 Jul 2024 15:20:36 +0100 X-MC-Unique: ASX4DscfNW6QCi1PP5Mc6g-1 Received: from AcuMS.Aculab.com (10.202.163.6) by AcuMS.aculab.com (10.202.163.6) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Sun, 28 Jul 2024 15:19:52 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Sun, 28 Jul 2024 15:19:52 +0100 From: David Laight To: "'linux-kernel@vger.kernel.org'" CC: 'Linus Torvalds' , 'Jens Axboe' , "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , 'Andrew Morton' , 'Andy Shevchenko' , 'Dan Carpenter' , 'Arnd Bergmann' , "'Jason@zx2c4.com'" , "'pedro.falcato@gmail.com'" , 'Mateusz Guzik' , "'linux-mm@kvack.org'" , 'Lorenzo Stoakes' Subject: [PATCH v2 3/8] compiler.h: Add __if_constexpr(expr, if_const, if_not_const) Thread-Topic: [PATCH v2 3/8] compiler.h: Add __if_constexpr(expr, if_const, if_not_const) Thread-Index: Adrg+UWL9B3xHw3xQ5CeKplMo1TYvg== Date: Sun, 28 Jul 2024 14:19:52 +0000 Message-ID: <2e12aefe29884e578283129411e1df26@AcuMS.aculab.com> References: <402c3c617c29465c898b1af55e3c6095@AcuMS.aculab.com> In-Reply-To: <402c3c617c29465c898b1af55e3c6095@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-Stat-Signature: ixxon9dyu1ix6ugfrma3n1q6mzxqrujm X-Rspamd-Queue-Id: 3118BA0019 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1722176441-996756 X-HE-Meta: U2FsdGVkX184C48JqLnofDZplzLwM+EVLag+JxrfdN+lKQC0dFSX95UlSDCfiOiuvQ27m8WlWKgBqPwxSokzFmkeSPShOT7SQzMRuFXk5ea+IohYiIiRTbl1OdG/e8hy4rWBGUvyi/M1u+jfymp9BXedlrZtPohTKY0jugXnwfYAsSVny+MPYzR5lwjgR5inRg+la4gF60JFEljQnxgnIS6B4d5TBn4LqaKfzxOXmSrrbTCZTLTo2odreH4UbDcLZxF8bTd4GyTkK74WxOXq+n2N4Lk+R0RSTno7q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ztmQAk+hO/5YPJWnPuEwmaJnHyqHAUEwX7Z8+phuPLy9LDYsZdf17QCmaR68+M 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. __is_constexpr(expr) is equivalent to __if_constexpr(expr, 1, 0). Signed-off-by: David Laight --- v2: - Don't change __is_constexpr() include/linux/compiler.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/include/linux/compiler.h b/include/linux/compiler.h index 2594553bb30b..35d5b2fa4786 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -242,6 +242,23 @@ 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])) +/** + * __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. + * + * 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)) + /* * This returns a constant expression while determining if an argument is * a constant expression, most importantly without evaluating the argument.