From patchwork Sun Jul 28 14:24:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Laight X-Patchwork-Id: 13744009 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 7FBB4C3DA64 for ; Sun, 28 Jul 2024 14:25:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1585F6B0085; Sun, 28 Jul 2024 10:25:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 109726B0088; Sun, 28 Jul 2024 10:25:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F12C36B0089; Sun, 28 Jul 2024 10:25:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id D55946B0085 for ; Sun, 28 Jul 2024 10:25:39 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 5B53F12030A for ; Sun, 28 Jul 2024 14:25:39 +0000 (UTC) X-FDA: 82389384798.19.AAEF048 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.86.151]) by imf18.hostedemail.com (Postfix) with ESMTP id 0510B1C0027 for ; Sun, 28 Jul 2024 14:25:36 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=none; spf=pass (imf18.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=1722176685; 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=LvEureUuX7ccYh/zv402RgXxViJraceq25mQbkP5p1Q=; b=nNoN3c1mekz0B3vAonBimO9/Vro7Syidm6SkFFwZdyXcI6lz6bvJY2HO7TBguABBcYKSdu lgkjUUzLbga97fcRL8rh2lBLBZrx425gBpZ4eEDvfpYoxkqZPEOLKwwmlPL0RgOZdObcfY b8PnlyreXdn8hrFg8yZWrAWv9ksV2yM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722176685; a=rsa-sha256; cv=none; b=yPJHP/YWw04ank6yoWdnTbZ5sd8/xjbW1/9KJjyj71fFkjJUQTRekNUmJN0moG/5BrfRcv 05XuaZREWQ3WPNLBJxjcsXgQlUuhTO81IksBs2juJIxo6StAAb76EGPaIoK/vrVpLcvDYQ ruU5MMiR5J92zZylYPOpOnFTdMmOgRo= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=none; spf=pass (imf18.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-225-Ran_uoH8NAunlrVN1XlTGw-1; Sun, 28 Jul 2024 15:25:31 +0100 X-MC-Unique: Ran_uoH8NAunlrVN1XlTGw-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:24:46 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Sun, 28 Jul 2024 15:24:46 +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 8/8] minmax: minmax: Add __types_ok3() and optimise defines with 3 arguments Thread-Topic: [PATCH v2 8/8] minmax: minmax: Add __types_ok3() and optimise defines with 3 arguments Thread-Index: Adrg+fzAGOYJcfIuRPCxFJC4Oy7+aw== Date: Sun, 28 Jul 2024 14:24:46 +0000 Message-ID: <75f18af7162c4414be28a890e9504c8d@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-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 0510B1C0027 X-Stat-Signature: tcwr44x1kwee8mzhg3j5dho554kt7xww X-HE-Tag: 1722176736-471577 X-HE-Meta: U2FsdGVkX1/IGR/7sGPJMiJ/ThheUVKD3eCg8PFVRrLVXQyDNPKquiC3AS9B7tBQSF3nRuuR0KzPWErvU/eMxRx/r0zeWcSYzD9GdUY8dpfH7InjaEBDxzD1akinErWJBzvstNrBaO1B1A8XhziF9jm29d32gnqCSQdqBLZy8yMzoWqroBCdbc8aeHkVIVjdzXVbIR8td+BvE66z1eySQs4YmGX3AzEQPlScp1k+0PYKLuEn4h9MlVlALSr/XvOaLSH8lZJk1Hy257hP+lughXd74suKcGhfHJ2xzoshA1Z+s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jfaIfx49xnloC6i73+C4KoJMEBEJMRyUy7E/oXAftcUWwBF7QUhirGJDdA6V 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: min3() and max3() were added to optimise nested min(x, min(y, z)) sequences, but only moved where the expansion was requested. Add a separate implementation for 3 argument calls. These are never required to generate constant expressiions so remove that logic. Signed-off-by: David Laight --- v2 (was pacth 7/7): - Use __auto_type. - Use an extra __xy local to slightly reduce the expansion. - Fix typos in the commit message. include/linux/minmax.h | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index cb3515824a64..e1e31a827547 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -41,6 +41,11 @@ ((__is_ok_signed(x) && __is_ok_signed(y)) || \ (__is_ok_unsigned(x) && __is_ok_unsigned(y))) +/* Check three values for min3(), max3() and clamp() */ +#define __types_ok3(x, y, z) \ + ((__is_ok_signed(x) && __is_ok_signed(y) && __is_ok_signed(z)) || \ + (__is_ok_unsigned(x) && __is_ok_unsigned(y) && __is_ok_unsigned(z))) + #define __cmp_op_min < #define __cmp_op_max > @@ -93,13 +98,25 @@ */ #define umax(x, y) __careful_cmp(max, __zero_extend(x), __zero_extend(y)) +#define __cmp_once3(op, x, y, z, uniq) ({ \ + __auto_type __x_##uniq = (x); \ + __auto_type __y_##uniq = (y); \ + __auto_type __z_##uniq = (z); \ + __auto_type __xy_##uniq = __cmp(op, __x_##uniq, __y_##uniq); \ + __cmp(op, __xy_##uniq, __z_##uniq); }) + +#define __careful_cmp3(op, x, y, z, uniq) ({ \ + _Static_assert(__types_ok3(x, y, z), \ + #op "3(" #x ", " #y ", " #z ") signedness error"); \ + __cmp_once3(op, x, y, z, uniq); }) + /** * min3 - return minimum of three values * @x: first value * @y: second value * @z: third value */ -#define min3(x, y, z) min((typeof(x))min(x, y), z) +#define min3(x, y, z) __careful_cmp3(min, x, y, z, __COUNTER__) /** * max3 - return maximum of three values @@ -107,7 +124,7 @@ * @y: second value * @z: third value */ -#define max3(x, y, z) max((typeof(x))max(x, y), z) +#define max3(x, y, z) __careful_cmp3(max, x, y, z, __COUNTER__) /** * min_t - return minimum of two values, using the specified type @@ -144,8 +161,7 @@ __auto_type unique_hi = (hi); \ _Static_assert(__if_constexpr((lo) <= (hi), (lo) <= (hi), true), \ "clamp() low limit " #lo " greater than high limit " #hi); \ - _Static_assert(__types_ok(val, lo), "clamp() 'lo' signedness error"); \ - _Static_assert(__types_ok(val, hi), "clamp() 'hi' signedness error"); \ + _Static_assert(__types_ok3(val, lo, hi), "clamp() signedness error"); \ __clamp(unique_val, unique_lo, unique_hi); }) #define __careful_clamp(val, lo, hi) ({ \