From patchwork Sun Jul 28 14:17:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Laight X-Patchwork-Id: 13744002 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 57873C3DA64 for ; Sun, 28 Jul 2024 14:18:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CC3FD6B007B; Sun, 28 Jul 2024 10:18:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C4DB66B0083; Sun, 28 Jul 2024 10:18:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AEEDA6B0085; Sun, 28 Jul 2024 10:18:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 8E1296B007B for ; Sun, 28 Jul 2024 10:18:27 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 522AAA31A7 for ; Sun, 28 Jul 2024 14:18:27 +0000 (UTC) X-FDA: 82389366654.08.9B9C025 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) by imf23.hostedemail.com (Postfix) with ESMTP id F2E19140012 for ; Sun, 28 Jul 2024 14:18:24 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=aculab.com; spf=pass (imf23.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=1722176246; 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=DjZVGJZdnfX63W8j/hevc8uS49pFHS0tB9VoukTPFEw=; b=2MMl0yqociK0spSv187vsiAhCjJHHxeeTHT6soiDllgEYEgv5WIvZXemrIlQnXdpsGkLKx 3r2+NJ2aVUxz0froJG7aWMMGCbz6r15ZSNgL8JyO1hWURTn1u2CXSxaHTWbTy3KXV0Qlk/ a+P5rep4EdiApHXExrTN31rQqXdIYLM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722176246; a=rsa-sha256; cv=none; b=5KWttwuSzaoAIP4faySqFmfOf7d2w5+44PuWPnZrVHG5zd78QcmJdTDTyNW7EheLPprI9+ sh2TELt8UWC1Wpjgm5ZXGtdIzqnqj4REx4eDipkplqC3q/2ZqqpwOpYIb4hyfUvzuy2O7L 9G3oLrI7up0z7ylRItIFDc1ulBxQFcI= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=aculab.com; spf=pass (imf23.hostedemail.com: domain of david.laight@aculab.com designates 185.58.85.151 as permitted sender) smtp.mailfrom=david.laight@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-68-U7LTviH9N2qGDn-NaZVzAQ-1; Sun, 28 Jul 2024 15:18:20 +0100 X-MC-Unique: U7LTviH9N2qGDn-NaZVzAQ-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:17:36 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Sun, 28 Jul 2024 15:17:36 +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 1/8] minmax: Put all the clamp() definitions together Thread-Topic: [PATCH v2 1/8] minmax: Put all the clamp() definitions together Thread-Index: Adrg+PsJWBOjJVDsQ/+qaYGHKHGutQ== Date: Sun, 28 Jul 2024 14:17:35 +0000 Message-ID: <5cd3e11780df40b0b771da5548966ebd@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-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: F2E19140012 X-Stat-Signature: jop63age4hq7uxp7exqjis8ipswy1ewj X-Rspam-User: X-HE-Tag: 1722176304-605801 X-HE-Meta: U2FsdGVkX1+tDmmox8dfBtJmjLRY/al7vsoiS/nW4dhKtiW+rKbdNUhc0pe4sUjRdqq6yqyw7QtCZm3J/sB5gtR3qULi3P+aXLUJkYWgR7amiNCe+KYqlcvk7pNS6H/JQjiiH+iuc+1Ioi72cANnc3cx7GrinJr3auhbM5gL0pj1ApWci7mJJNDdZHhfIhBTAgTY3AVS/5hPTeXj7UaHLkQ5wn8Ni54purxOjBpyOsNVTTsSp1fWzpPnVREos7LkYGngK3ZK3JnMtOMuv/GTa/DZ4ocVMkkueEyCOKNsPj90YtH55iJskt89IPE55N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OkQ== 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: The defines for clamp() have got separated, move togther for readability. Update description of signedness check. Signed-off-by: David Laight --- v2: - No change. include/linux/minmax.h | 120 +++++++++++++++++++---------------------- 1 file changed, 56 insertions(+), 64 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index a7ef65f78933..cea63a8ac80f 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -57,26 +57,6 @@ __cmp(op, x, y), \ __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y))) -#define __clamp(val, lo, hi) \ - ((val) >= (hi) ? (hi) : ((val) <= (lo) ? (lo) : (val))) - -#define __clamp_once(val, lo, hi, unique_val, unique_lo, unique_hi) ({ \ - typeof(val) unique_val = (val); \ - typeof(lo) unique_lo = (lo); \ - typeof(hi) unique_hi = (hi); \ - static_assert(__builtin_choose_expr(__is_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"); \ - __clamp(unique_val, unique_lo, unique_hi); }) - -#define __careful_clamp(val, lo, hi) ({ \ - __builtin_choose_expr(__is_constexpr((val) - (lo) + (hi)), \ - __clamp(val, lo, hi), \ - __clamp_once(val, lo, hi, __UNIQUE_ID(__val), \ - __UNIQUE_ID(__lo), __UNIQUE_ID(__hi))); }) - /** * min - return minimum of two values of the same or compatible types * @x: first value @@ -124,6 +104,22 @@ */ #define max3(x, y, z) max((typeof(x))max(x, y), z) +/** + * min_t - return minimum of two values, using the specified type + * @type: data type to use + * @x: first value + * @y: second value + */ +#define min_t(type, x, y) __careful_cmp(min, (type)(x), (type)(y)) + +/** + * max_t - return maximum of two values, using the specified type + * @type: data type to use + * @x: first value + * @y: second value + */ +#define max_t(type, x, y) __careful_cmp(max, (type)(x), (type)(y)) + /** * min_not_zero - return the minimum that is _not_ zero, unless both are zero * @x: value1 @@ -134,39 +130,60 @@ typeof(y) __y = (y); \ __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); }) +#define __clamp(val, lo, hi) \ + ((val) >= (hi) ? (hi) : ((val) <= (lo) ? (lo) : (val))) + +#define __clamp_once(val, lo, hi, unique_val, unique_lo, unique_hi) ({ \ + typeof(val) unique_val = (val); \ + typeof(lo) unique_lo = (lo); \ + typeof(hi) unique_hi = (hi); \ + static_assert(__builtin_choose_expr(__is_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"); \ + __clamp(unique_val, unique_lo, unique_hi); }) + +#define __careful_clamp(val, lo, hi) ({ \ + __builtin_choose_expr(__is_constexpr((val) - (lo) + (hi)), \ + __clamp(val, lo, hi), \ + __clamp_once(val, lo, hi, __UNIQUE_ID(__val), \ + __UNIQUE_ID(__lo), __UNIQUE_ID(__hi))); }) + /** * clamp - return a value clamped to a given range with strict 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 that @val, @lo and @hi have the same signedness. */ #define clamp(val, lo, hi) __careful_clamp(val, lo, hi) -/* - * ..and if you can't take the strict - * types, you can specify one yourself. - * - * Or not use min/max/clamp at all, of course. - */ - /** - * min_t - return minimum of two values, using the specified type - * @type: data type to use - * @x: first value - * @y: second value + * clamp_t - return a value clamped to a given range using a given type + * @type: the type of variable to use + * @val: current value + * @lo: minimum allowable value + * @hi: maximum allowable value + * + * This macro does no typechecking and uses temporary variables of type + * @type to make all the comparisons. */ -#define min_t(type, x, y) __careful_cmp(min, (type)(x), (type)(y)) +#define clamp_t(type, val, lo, hi) __careful_clamp((type)(val), (type)(lo), (type)(hi)) /** - * max_t - return maximum of two values, using the specified type - * @type: data type to use - * @x: first value - * @y: second value + * clamp_val - return a value clamped to a given range using val's type + * @val: current value + * @lo: minimum allowable value + * @hi: maximum allowable value + * + * This macro does no typechecking and uses temporary variables of whatever + * type the input argument @val is. This is useful when @val is an unsigned + * type and @lo and @hi are literals that will otherwise be assigned a signed + * integer type. */ -#define max_t(type, x, y) __careful_cmp(max, (type)(x), (type)(y)) +#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) /* * Do not check the array parameter using __must_be_array(). @@ -211,31 +228,6 @@ */ #define max_array(array, len) __minmax_array(max, array, len) -/** - * clamp_t - return a value clamped to a given range using a given type - * @type: the type of variable to use - * @val: current value - * @lo: minimum allowable value - * @hi: maximum allowable value - * - * 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)) - -/** - * clamp_val - return a value clamped to a given range using val's type - * @val: current value - * @lo: minimum allowable value - * @hi: maximum allowable value - * - * This macro does no typechecking and uses temporary variables of whatever - * type the input argument @val is. This is useful when @val is an unsigned - * 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) - static inline bool in_range64(u64 val, u64 start, u64 len) { return (val - start) < len;