From patchwork Wed Jul 24 14:33:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Laight X-Patchwork-Id: 13741018 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 7D222C3DA63 for ; Wed, 24 Jul 2024 14:33:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1961C6B007B; Wed, 24 Jul 2024 10:33:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 146796B0093; Wed, 24 Jul 2024 10:33:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F281C6B0095; Wed, 24 Jul 2024 10:33:56 -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 D396D6B007B for ; Wed, 24 Jul 2024 10:33:56 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 7CE47120A1A for ; Wed, 24 Jul 2024 14:33:56 +0000 (UTC) X-FDA: 82374890472.24.C77B64B Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) by imf01.hostedemail.com (Postfix) with ESMTP id 29E624003E for ; Wed, 24 Jul 2024 14:33:53 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=aculab.com; spf=pass (imf01.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=1721831595; a=rsa-sha256; cv=none; b=mExjfPEP6y0BSSxymYaZfIiciOCQttM93HQ27qXnD0ElZl/8bzX9pBp7A8E03JJtNhXw6r gFOr9ufIhAZoLqllmt1+qQAkbxHCKG0FUjWjXZSbpLicMZJ5i0/o4A7s9Wt8nbS2e6r3Fv ExfTTg/dL3PPpE5z48umHZivcKVoIe8= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=aculab.com; spf=pass (imf01.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=1721831595; 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=ia6SmT3DZLa6vhYN89Py8rxkJqhB7jo1tQHkEeKAQLU=; b=1nTC19OA/e6VKFEiC5P+mksMJcJUac0jkp8XqwKjjv73+HqKg5jBBTDv5TyGk1FeC57Dbv tYT/ynSqOavx+M8svuQBaFWyejixPQtACCEfX4q/zEeFKIP0GaLckpZMKHtAsoT4+bZSgr Y10HKaEcigGtIectolAEqoY/WbDeKL4= 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-310-eKrR_BFuOPKLqYqDBV76gg-1; Wed, 24 Jul 2024 15:33:51 +0100 X-MC-Unique: eKrR_BFuOPKLqYqDBV76gg-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:33:11 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Wed, 24 Jul 2024 15:33:11 +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 7/7] minmax: minmax: Add __types_ok3() and optimise defines with 3 arguments Thread-Topic: [PATCH 7/7] minmax: minmax: Add __types_ok3() and optimise defines with 3 arguments Thread-Index: Adrd1nwhwSRYGwj9RxiIgqAQhw9FiA== Date: Wed, 24 Jul 2024 14:33:11 +0000 Message-ID: <3484b7fcd2c74655bd685e5a7030c284@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-Server: rspam12 X-Rspamd-Queue-Id: 29E624003E X-Stat-Signature: xpzim6xb9butaftkm3fpsybxmo3iz9sq X-Rspam-User: X-HE-Tag: 1721831633-485815 X-HE-Meta: U2FsdGVkX18/0hTjJa2b34HgrKUmfxKyKpVp3LbsNMg9CzXj2F4AMGWFhixuRo49spSmD/rpAn8bhm71aSkccCDNcbcHKWr3xbLzX7Ud6D1JglPdpJFj0S2L5COUdTr4f6CUT3w+yfMbqq258Qr2HvGoHJFpSvVuKLaEcmZynprIz8ZOPRSe7G1igk4vY2eanHjjBfn+ja0T+WWIkGWMT2o0terRmmTbB0EyYsQiRW9AjT7YgNK6oOHknH7/81kEknfbVry2eHfd7cJGc/idlIiU4pvPUEoYvk2/FVb7EOI3zl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smc4JYNoD0oZWh9BKoK0Rm/wdjbwtCNXMg== 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, bit only moved where the expansion was requiested. Add a separate implementation for 3 argument calls. These are never required to generate constant expressiions to remove that logic. Signed-off-by: David Laight --- include/linux/minmax.h | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 2fb63efbeb0e..4bbc82c589cf 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -38,6 +38,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 > @@ -90,13 +95,24 @@ */ #define umax(x, y) __careful_cmp(max, __zero_extend(x), __zero_extend(y)) +#define __cmp_once3(op, x, y, z, uniq) ({ \ + typeof(x) __x_##uniq = (x); \ + typeof(x) __y_##uniq = (y); \ + typeof(x) __z_##uniq = (z); \ + __cmp(op, __cmp(op, __x_##uniq, __y_##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 @@ -104,7 +120,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 @@ -139,10 +155,9 @@ typeof(val) unique_val = (val); \ typeof(lo) unique_lo = (lo); \ typeof(hi) unique_hi = (hi); \ - _Static_assert(__if_constexpr((lo) <= (hi), (lo) <= (hi), true), \ + _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) ({ \