Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp20444rdb; Sun, 28 Jan 2024 11:26:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IF/qmkOlbhzUbEoeMJnv80ObfMFblM+FtnW9iSLLbSYbUq8ZI/cUXmM0MjwuTVGa3J/jwC2 X-Received: by 2002:a17:906:35d7:b0:a35:4a82:2046 with SMTP id p23-20020a17090635d700b00a354a822046mr2122226ejb.24.1706470017038; Sun, 28 Jan 2024 11:26:57 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706470017; cv=pass; d=google.com; s=arc-20160816; b=k7hUJBwgT9IDygiv+WwAkFB8iXsi4IJYAO3bwUJQPAla9fu8lgmT2o7r68QXuPzonS DWcRlK8+0r5NSWcatZPFDXbzmm8Yga33Y8gz8JWwqSK2ePemdQSKPIkDtcHhnnG3+xkK LB6f3pkcQSNQv+1aCChuxQUGoBaniKEu5+/xdcvmge04sdXfjpNuWO2a8JRif4jbFq/k ZtR7D/AH2FynYVVHUXuU+56GwcllLyOgRJFU+Iq675s8/xjva/JqNj+i7SgER5JyYAR7 4VrGH81PmQp6fhSZk01r37T+fRrufinrSqvdkQtb9Y1dcqrjYq5Z06DAOnxtjkEn/Oza HyUw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:content-language:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:accept-language :in-reply-to:references:message-id:date:thread-index:thread-topic :subject:cc:to:from; bh=m3WkPOx6tQxNXMeRli1rAKWLGKZTCm3EXd2zn1Oz0R0=; fh=jAOu/RGlQxfhPk7PcGNzOfYLHKZO+STAaG1daOB/TAU=; b=e7FHbuBMw+5eHPgczzXJ0EURNGcPhgXgG4Inprg2RRJNPzzlDTANDKxO0Y1yk/stqH dZ8oBck+fcclkQHkhaGdSpxqZ7e2VGQ9MWBBC5wCQakP2RZ84dIA8JiRqyevJJVh99A0 cC/jV17pNk4E+5HBXhcaZJRndlUKySA0J9ZS1GXd6TF4a2/vRfh/GrF84xJMNBS52p0M KoJ0Fs1BQyO3ul3RxwbQ1wwQduIqBKXXS0mPo+Noi01Zf6LwL7xZRgaV7U05MLxlDItg Yg+jP2vTwAQ+Crl7UFXPvYdYXOf5Ma94bU+Y95iQK4WJh7bOFSBJQ4wa9ygal9HdiTPt THzw== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=aculab.com dmarc=pass fromdomain=aculab.com); spf=pass (google.com: domain of linux-kernel+bounces-41898-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41898-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id f19-20020a170906139300b00a351ad01128si2103304ejc.520.2024.01.28.11.26.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Jan 2024 11:26:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-41898-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=aculab.com dmarc=pass fromdomain=aculab.com); spf=pass (google.com: domain of linux-kernel+bounces-41898-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41898-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 951101F22120 for ; Sun, 28 Jan 2024 19:26:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5818A31A66; Sun, 28 Jan 2024 19:26:44 +0000 (UTC) Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.86.151]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ED37B31A82 for ; Sun, 28 Jan 2024 19:26:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.58.86.151 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706470003; cv=none; b=VoUSdL3dn0dCCE2WKlb0u/d4NpSGlOARXJGu7LUhA8ej7m+mbeuLKIxY4cgzZX1TKtTQutUbCbPLF+GJhXzwlMymHcOGpdkRMLvBClQ0eSpqXJUFaiyZvxjmfpUQYL0J8tYM9FZAtee/L096bWjLzQMCox6s15TG8aNr61ap5w8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706470003; c=relaxed/simple; bh=y4rtEWAXru0KLn5/0HBoBnc39VArj/Sc52842nH+Rcc=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=fb6scEM1rn63HSVEfvjkyKVt6cpRrw+7M35SXszfB3GcOsV9Yi8wlPu6DqCeyEqRIaARb6vRMnudScPTyeZubdPyP6JCYq1ZVDl6qwmipOwL7Kt6wnpoSDRIKPjX1utpKF1LJmgvbOdG/99T5EzlfBrq/H/WCUskz8n6OVA/Ax4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM; spf=pass smtp.mailfrom=aculab.com; arc=none smtp.client-ip=185.58.86.151 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=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-287-Kf3GQTitP5eD8y4OkzSb6A-1; Sun, 28 Jan 2024 19:26:38 +0000 X-MC-Unique: Kf3GQTitP5eD8y4OkzSb6A-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 Jan 2024 19:26:13 +0000 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Sun, 28 Jan 2024 19:26:13 +0000 From: David Laight To: "'linux-kernel@vger.kernel.org'" , "'Linus Torvalds'" , 'Netdev' , "'dri-devel@lists.freedesktop.org'" CC: 'Andy Shevchenko' , 'Andrew Morton' , "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , "'Dan Carpenter'" , 'Linus Walleij' , "'David S . Miller'" , "'linux-btrfs@vger.kernel.org'" , 'Jens Axboe' Subject: RE: [PATCH next 01/11] minmax: Put all the clamp() definitions together Thread-Topic: [PATCH next 01/11] minmax: Put all the clamp() definitions together Thread-Index: AdpSH+hhFr878/ZhQb67LXPpPhAHFg== Date: Sun, 28 Jan 2024 19:26:12 +0000 Message-ID: <10148287b1ee41df80d8240afbcc54be@AcuMS.aculab.com> References: <0ca26166dd2a4ff5a674b84704ff1517@AcuMS.aculab.com> In-Reply-To: <0ca26166dd2a4ff5a674b84704ff1517@AcuMS.aculab.com> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable The defines for clamp() have got separated, move togther for readability. Update description of signedness check. Signed-off-by: David Laight --- 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 2ec559284a9f..63c45865b48a 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -57,26 +57,6 @@ =09=09__cmp(op, x, y),=09=09=09=09\ =09=09__cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y))) =20 -#define __clamp(val, lo, hi)=09\ -=09((val) >=3D (hi) ? (hi) : ((val) <=3D (lo) ? (lo) : (val))) - -#define __clamp_once(val, lo, hi, unique_val, unique_lo, unique_hi) ({=09= =09\ -=09typeof(val) unique_val =3D (val);=09=09=09=09=09=09\ -=09typeof(lo) unique_lo =3D (lo);=09=09=09=09=09=09\ -=09typeof(hi) unique_hi =3D (hi);=09=09=09=09=09=09\ -=09static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), =09\ -=09=09=09(lo) <=3D (hi), true),=09=09=09=09=09\ -=09=09"clamp() low limit " #lo " greater than high limit " #hi);=09\ -=09static_assert(__types_ok(val, lo), "clamp() 'lo' signedness error");=09= \ -=09static_assert(__types_ok(val, hi), "clamp() 'hi' signedness error");=09= \ -=09__clamp(unique_val, unique_lo, unique_hi); }) - -#define __careful_clamp(val, lo, hi) ({=09=09=09=09=09\ -=09__builtin_choose_expr(__is_constexpr((val) - (lo) + (hi)),=09\ -=09=09__clamp(val, lo, hi),=09=09=09=09=09\ -=09=09__clamp_once(val, lo, hi, __UNIQUE_ID(__val),=09=09\ -=09=09=09 __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) =20 +/** + * 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)=09__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)=09__careful_cmp(max, (type)(x), (type)(y)) + /** * min_not_zero - return the minimum that is _not_ zero, unless both are z= ero * @x: value1 @@ -134,39 +130,60 @@ =09typeof(y) __y =3D (y);=09=09=09\ =09__x =3D=3D 0 ? __y : ((__y =3D=3D 0) ? __x : min(__x, __y)); }) =20 +#define __clamp(val, lo, hi)=09\ +=09((val) >=3D (hi) ? (hi) : ((val) <=3D (lo) ? (lo) : (val))) + +#define __clamp_once(val, lo, hi, unique_val, unique_lo, unique_hi) ({=09= =09\ +=09typeof(val) unique_val =3D (val);=09=09=09=09=09=09\ +=09typeof(lo) unique_lo =3D (lo);=09=09=09=09=09=09\ +=09typeof(hi) unique_hi =3D (hi);=09=09=09=09=09=09\ +=09static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)),=09\ +=09=09=09(lo) <=3D (hi), true),=09=09=09=09=09\ +=09=09"clamp() low limit " #lo " greater than high limit " #hi);=09\ +=09static_assert(__types_ok(val, lo), "clamp() 'lo' signedness error");=09= \ +=09static_assert(__types_ok(val, hi), "clamp() 'hi' signedness error");=09= \ +=09__clamp(unique_val, unique_lo, unique_hi); }) + +#define __careful_clamp(val, lo, hi) ({=09=09=09=09=09\ +=09__builtin_choose_expr(__is_constexpr((val) - (lo) + (hi)),=09\ +=09=09__clamp(val, lo, hi),=09=09=09=09=09\ +=09=09__clamp_once(val, lo, hi, __UNIQUE_ID(__val),=09=09\ +=09=09=09 __UNIQUE_ID(__lo), __UNIQUE_ID(__hi))); }) + /** * clamp - return a value clamped to a given range with strict typecheckin= g * @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) =20 -/* - * ..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)=09__careful_cmp(min, (type)(x), (type)(y)) +#define clamp_t(type, val, lo, hi) __careful_clamp((type)(val), (type)(lo)= , (type)(hi)) =20 /** - * 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 whateve= r + * type the input argument @val is. This is useful when @val is an unsign= ed + * type and @lo and @hi are literals that will otherwise be assigned a sig= ned + * integer type. */ -#define max_t(type, x, y)=09__careful_cmp(max, (type)(x), (type)(y)) +#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) =20 /* * Do not check the array parameter using __must_be_array(). @@ -211,31 +228,6 @@ */ #define max_array(array, len) __minmax_array(max, array, len) =20 -/** - * 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 whateve= r - * type the input argument @val is. This is useful when @val is an unsign= ed - * type and @lo and @hi are literals that will otherwise be assigned a sig= ned - * 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) { =09return (val - start) < len; --=20 2.17.1 - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1= PT, UK Registration No: 1397386 (Wales)