From patchwork Mon Nov 18 19:15:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Laight X-Patchwork-Id: 13878988 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 3AD59D591AD for ; Mon, 18 Nov 2024 19:15:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CC3D46B0085; Mon, 18 Nov 2024 14:15:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C721D6B0088; Mon, 18 Nov 2024 14:15:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B88456B0095; Mon, 18 Nov 2024 14:15:12 -0500 (EST) 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 9A32E6B0085 for ; Mon, 18 Nov 2024 14:15:12 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 57A4E1604A0 for ; Mon, 18 Nov 2024 19:15:12 +0000 (UTC) X-FDA: 82800167436.12.C8D5E7D Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.86.151]) by imf25.hostedemail.com (Postfix) with ESMTP id 13D84A001D for ; Mon, 18 Nov 2024 19:14:33 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=none; spf=pass (imf25.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=1731957109; 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=9maQU4wGvYW3paDE/WtpSZ7O4+dlztwiKjoAdeMmpl4=; b=BJLtMax9rOt51KuRgxArZbUDO7Pq66eaQPq/JprwxyC7nA3KTrh62G1805QLGj0wTC3oD6 u1RbcKy/ks6vcLGW7imNR7FWm59tLZDo4YzY9bj/tyhd4f3qirPvRPEcaJy1S4gm6qVe7G GZnvsrmDKocNsCMmPEQm2WI/ockJzpM= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=none; spf=pass (imf25.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731957109; a=rsa-sha256; cv=none; b=iZuvxW3BedP7TVuuL+YQYkQbrALjF9AT/tHBfowUFc5WxVwTKlqT5Y3vFZV6WBqUxCdPP5 IVYwmgHXUYBoB5vTGGTavbEr6BB5WU3a87hF1aqeGIG4uVZLWksPjbw8a6ugUiHCSmKNMv 2EsI1AZq1lNJ8dJe6zofnexwf1dUbus= 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-1-Li-56pJWM8mS-HZ_UNxcng-1; Mon, 18 Nov 2024 19:15:06 +0000 X-MC-Unique: Li-56pJWM8mS-HZ_UNxcng-1 X-Mimecast-MFC-AGG-ID: Li-56pJWM8mS-HZ_UNxcng 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:15:05 +0000 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Mon, 18 Nov 2024 19:15:05 +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 6/7] minmax.h: Simplify the variants of clamp() Thread-Topic: [PATCH next 6/7] minmax.h: Simplify the variants of clamp() Thread-Index: Ads57ipn63eWKuUFSOOk3Sda6NOThQ== Date: Mon, 18 Nov 2024 19:15:05 +0000 Message-ID: <8f69f4deac014f558bab186444bac2e8@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: PIn-oo-fgqRBt4vMFrIktXgS1ArQ__ypHjZdvNct5nA_1731957305 X-Mimecast-Originator: aculab.com Content-Language: en-US X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 13D84A001D X-Stat-Signature: 59ancfq8rm9ju1udrre5s5jdffhbmo4j X-Rspam-User: X-HE-Tag: 1731957273-58419 X-HE-Meta: U2FsdGVkX18dpZweQB2Ae3cTbogIU9EWrVZdI5nTpJVyBFFkM9E5Kds6nhjlZsM+GBykBYTl71z2RxgPtw8J5oibCzsQ3ahToudxTK0qsK+ufmEH0HKvZEpf/oJAFsgf2S7zqhBGYPjoxq8OqJKPME8jixx1lhwut3hmJC9is48lcnIQBOk4LP7Ci69cCgg84N5I8x7Z5lIU5HVPZ6fTTMUGKHyBppyUND6f7cYd6s24AEi1u1jwK/O7JwUyOI7SVWSEP0h6XQAOGHHX25qYFwmDx0w9W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w5e2w4zdm+VwpTj0LOJ/YaKpadthfkLspIjdp4gcPagLgM73ikeE/lBhduNBgKNsDY3Vk0BvG5DkX572S51PL0t5gkPUX3+a 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: Always pass a 'type' through to __clamp_once(), pass '__auto_type' from clamp() itself. The expansion of __types_ok3() is reasonable so it isn't worth the added complexity of avoiding it when a fixed type is used for all three values. 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 75fb7a6ad4c6..2bbdd5b5e07e 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -183,29 +183,29 @@ #define __clamp(val, lo, hi) \ ((val) >= (hi) ? (hi) : ((val) <= (lo) ? (lo) : (val))) -#define __clamp_once(val, lo, hi, uval, ulo, uhi) ({ \ - __auto_type uval = (val); \ - __auto_type ulo = (lo); \ - __auto_type uhi = (hi); \ +#define __clamp_once(type, val, lo, hi, uval, ulo, uhi) ({ \ + type uval = (val); \ + type ulo = (lo); \ + type uhi = (hi); \ BUILD_BUG_ON_MSG(statically_true(ulo > uhi), \ "clamp() low limit " #lo " greater than high limit " #hi); \ BUILD_BUG_ON_MSG(!__types_ok3(uval, ulo, uhi), \ "clamp("#val", "#lo", "#hi") signedness error"); \ __clamp(uval, ulo, uhi); }) -#define __careful_clamp(val, lo, hi) \ - __clamp_once(val, lo, hi, __UNIQUE_ID(v_), __UNIQUE_ID(l_), __UNIQUE_ID(h_)) +#define __careful_clamp(type, val, lo, hi) \ + __clamp_once(type, val, lo, hi, __UNIQUE_ID(v_), __UNIQUE_ID(l_), __UNIQUE_ID(h_)) /** - * clamp - return a value clamped to a given range with strict typechecking + * clamp - return a value clamped to a given range with typechecking * @val: current value * @lo: lowest allowable value * @hi: highest allowable value * - * This macro does strict typechecking of @lo/@hi to make sure they are of the - * same type as @val. See the unnecessary pointer comparisons. + * This macro checks @val/@lo/@hi to make sure they have compatible + * signedness. */ -#define clamp(val, lo, hi) __careful_clamp(val, lo, hi) +#define clamp(val, lo, hi) __careful_clamp(__auto_type, val, lo, hi) /** * clamp_t - return a value clamped to a given range using a given type @@ -217,7 +217,7 @@ * This macro does no typechecking and uses temporary variables of type * @type to make all the comparisons. */ -#define clamp_t(type, val, lo, hi) __careful_clamp((type)(val), (type)(lo), (type)(hi)) +#define clamp_t(type, val, lo, hi) __careful_clamp(type, val, lo, hi) /** * clamp_val - return a value clamped to a given range using val's type @@ -230,7 +230,7 @@ * type and @lo and @hi are literals that will otherwise be assigned a signed * integer type. */ -#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) +#define clamp_val(val, lo, hi) __careful_clamp(typeof(val), val, lo, hi) /* * Do not check the array parameter using __must_be_array().