From patchwork Mon Nov 18 19:12:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Laight X-Patchwork-Id: 13878985 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 C7B65D591AC for ; Mon, 18 Nov 2024 19:12:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 595A76B0095; Mon, 18 Nov 2024 14:12:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 545676B0096; Mon, 18 Nov 2024 14:12:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3E6E06B0099; Mon, 18 Nov 2024 14:12:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 21C206B0096 for ; Mon, 18 Nov 2024 14:12:57 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id CE039AC994 for ; Mon, 18 Nov 2024 19:12:56 +0000 (UTC) X-FDA: 82800159708.17.5A0BACB Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.86.151]) by imf08.hostedemail.com (Postfix) with ESMTP id 51CED160006 for ; Mon, 18 Nov 2024 19:12:21 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=none; spf=pass (imf08.hostedemail.com: domain of david.laight@aculab.com designates 185.58.86.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=1731957114; 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=MC36Hplj10fV2ksNU9xBGYy7o0UNgQO/YpkVzJnfO/w=; b=TVT5fMQtiVdnRbEH/4PDerNE3oibj4tSR2MC7LYbkuW1edMe9DPgaPHnmKbH2dCFTKx1+R neSUANHfOLA3NX/kdqI/umNCWp/AOvYWyKiy6VRBhoO3kH3cTDnBWLofio3j3x2Y+N21TX RztIdLkXeiIG2V5iToZmcuIPKajJUnE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731957114; a=rsa-sha256; cv=none; b=UTx6C7Yhf/8J/0UY+oRAuyolN8WvVCw5E82fcVN3KVFUuZhFjZXBKZ0r9+fnl1BGk1oOGN 24uz+oHTtAZQg/SyRVUnfAlxXrTrjrApnfZi7g/CoQ9sATFSP3aSXhoogpfd4xy4LnMxhi GfXc73QLOXsZ274o0KiTugeXa+LIErU= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=none; spf=pass (imf08.hostedemail.com: domain of david.laight@aculab.com designates 185.58.86.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com; dmarc=pass (policy=none) header.from=aculab.com 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-27-JS3bqrNRMXmEnncm00apUg-1; Mon, 18 Nov 2024 19:12:51 +0000 X-MC-Unique: JS3bqrNRMXmEnncm00apUg-1 X-Mimecast-MFC-AGG-ID: JS3bqrNRMXmEnncm00apUg 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; Mon, 18 Nov 2024 19:12:50 +0000 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Mon, 18 Nov 2024 19:12:50 +0000 From: David Laight To: Linus Torvalds CC: 'Arnd Bergmann' , "'linux-kernel@vger.kernel.org'" , 'Jens Axboe' , "'Matthew Wilcox'" , 'Christoph Hellwig' , 'Andrew Morton' , 'Andy Shevchenko' , 'Dan Carpenter' , "'Jason A . Donenfeld'" , "'pedro.falcato@gmail.com'" , 'Mateusz Guzik' , "'linux-mm@kvack.org'" , "'Lorenzo Stoakes'" Subject: [PATCH next 3/7] minmax.h: Reduce the #define expansion of min(), max() and clamp() Thread-Topic: [PATCH next 3/7] minmax.h: Reduce the #define expansion of min(), max() and clamp() Thread-Index: Ads57dnbMgzUo0QtRCKjFtFmfCRzXg== Date: Mon, 18 Nov 2024 19:12:50 +0000 Message-ID: <051afc171806425da991908ed8688a98@AcuMS.aculab.com> References: In-Reply-To: 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-MFC-PROC-ID: H8SxEPoWcF743HTflWPuYhgGU3BYrh1U6xX_ncpeOAs_1731957170 X-Mimecast-Originator: aculab.com Content-Language: en-US X-Stat-Signature: mukmgpbz3xpzwq9nxbbsjdeeeegr61es X-Rspam-User: X-Rspamd-Queue-Id: 51CED160006 X-Rspamd-Server: rspam02 X-HE-Tag: 1731957141-666431 X-HE-Meta: U2FsdGVkX18hU7c/Na2+Lp4ZIu8BJrszHqG/JZSu5aqjh6fEhClG7TyxCpTDRnAEMAp2fBXjudVct2zctrSTffwwsJVb7fQuy1+0sRNxq+dA+sCup9XQDZV9jQ2qY1VznLRYncxCc1+wK3mNaiVR5GAuMEMO5R2B1bkYBsTjhkGPvZMQhpQ5S0vlnXYEN2uPWvvIbB8Sm/pPeIWnLcct9xqqhND3Mar3vKpkzkFBRvowYtcwDwUwjATMAoByzaNAJKPJ7L8EEWFrlfUnjrQNdYot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CvBzLEzDQ1oECIqS90nydHKCGPwjgX7MYDbxy/SQTG7NH0gvfJWyRWi9BlioUMVJdHKuR4SeOkKUhxQ== 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: Since the test for signed values being non-negative only relies on __builtion_constant_p() (not is_constexpr()) it can use the 'ux' variable instead of the caller supplied expression. This means that the #define parameters are only expanded twice. Once in the code and once quoted in the error message. Signed-off-by: David Laight --- include/linux/minmax.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 24e4b372649a..6f7ea669d305 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -46,10 +46,10 @@ * comparison, and these expressions only need to be careful to not cause * warnings for pointer use. */ -#define __signed_type_use(x, ux) (2 + __is_nonneg(x, ux)) -#define __unsigned_type_use(x, ux) (1 + 2 * (sizeof(ux) < 4)) -#define __sign_use(x, ux) (is_signed_type(typeof(ux)) ? \ - __signed_type_use(x, ux) : __unsigned_type_use(x, ux)) +#define __signed_type_use(ux) (2 + __is_nonneg(ux)) +#define __unsigned_type_use(ux) (1 + 2 * (sizeof(ux) < 4)) +#define __sign_use(ux) (is_signed_type(typeof(ux)) ? \ + __signed_type_use(ux) : __unsigned_type_use(ux)) /* * Check whether a signed value is always non-negative. @@ -71,13 +71,13 @@ #else #define __signed_type(ux) typeof(__builtin_choose_expr(sizeof(ux) > 4, 1LL, 1L)) #endif -#define __is_nonneg(x, ux) statically_true((__signed_type(ux))(x) >= 0) +#define __is_nonneg(ux) statically_true((__signed_type(ux))(ux) >= 0) -#define __types_ok(x, y, ux, uy) \ - (__sign_use(x, ux) & __sign_use(y, uy)) +#define __types_ok(ux, uy) \ + (__sign_use(ux) & __sign_use(uy)) -#define __types_ok3(x, y, z, ux, uy, uz) \ - (__sign_use(x, ux) & __sign_use(y, uy) & __sign_use(z, uz)) +#define __types_ok3(ux, uy, uz) \ + (__sign_use(ux) & __sign_use(uy) & __sign_use(uz)) #define __cmp_op_min < #define __cmp_op_max > @@ -92,7 +92,7 @@ #define __careful_cmp_once(op, x, y, ux, uy) ({ \ __auto_type ux = (x); __auto_type uy = (y); \ - BUILD_BUG_ON_MSG(!__types_ok(x, y, ux, uy), \ + BUILD_BUG_ON_MSG(!__types_ok(ux, uy), \ #op"("#x", "#y") signedness error"); \ __cmp(op, ux, uy); }) @@ -109,7 +109,7 @@ static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \ (lo) <= (hi), true), \ "clamp() low limit " #lo " greater than high limit " #hi); \ - BUILD_BUG_ON_MSG(!__types_ok3(val, lo, hi, uval, ulo, uhi), \ + BUILD_BUG_ON_MSG(!__types_ok3(uval, ulo, uhi), \ "clamp("#val", "#lo", "#hi") signedness error"); \ __clamp(uval, ulo, uhi); }) @@ -149,7 +149,7 @@ #define __careful_op3(op, x, y, z, ux, uy, uz) ({ \ __auto_type ux = (x); __auto_type uy = (y);__auto_type uz = (z);\ - BUILD_BUG_ON_MSG(!__types_ok3(x, y, z, ux, uy, uz), \ + BUILD_BUG_ON_MSG(!__types_ok3(ux, uy, uz), \ #op"3("#x", "#y", "#z") signedness error"); \ __cmp(op, ux, __cmp(op, uy, uz)); })