Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp897270rbb; Sun, 25 Feb 2024 08:53:25 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU3j2yD5+nipe0vun8AAilfz9OKvi3MUuLuFvgqNYw6Y/a7wKbVsNsdRf/UeSFR5cOyv8vWocwT+zS8D9yqTSroDPjv249RI0PdIL2CxA== X-Google-Smtp-Source: AGHT+IEyDOXvf4uG5dTdlbqNX45FVlOc8kaagoRd7dMBYOARX8W5WiWzVUZu3oPYbhq44X0cufq8 X-Received: by 2002:a05:620a:35e:b0:787:b73c:38ec with SMTP id t30-20020a05620a035e00b00787b73c38ecmr6050723qkm.5.1708880005067; Sun, 25 Feb 2024 08:53:25 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708880005; cv=pass; d=google.com; s=arc-20160816; b=P/f0WxeD03qHWr6G+RqXZ3tXQI0/Zkbyd+Hz+4gbbhiVcv4og9MjeI176s+N1FNNet ICU8cMzWrVZndHryX6+vmSLYcFHkHzlei39hp45wN4J66tn652MPiISQoniApo4teFUw 67AT+O5t3WL9Zwxds4+jkDIbUzu5LghuS0pfe98Z+fQscHCMbDd2KHmuu38QuNRbzVEc TZIgxUKOZft9aksf5TydhNMxB5FRzT4WYOctaiK79FVew7fWLGNK8v9bMVodHexgJij0 UVB67//9e0/Ydweb7cbsgVPfpJ7VahtjN0kvQELXt4giecRXPr3oQqpHf1YHCibVgqwh k0iA== 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=E2uR/kIA1sSyBwch0S3wiEnZ2YjEle8TjIPSX/UDRjY=; fh=nGE692sbVdRvha6RYbzMUXtm1nr/30sV7frdtDN2z14=; b=GSLEPybM2fekhd1MHdYVJmErsZbVIsD3iCvCauiKiRm7dXbrOh0IrSt9+lFLHwXU7Y d0o6JOsCqRN3q2NoPJplbn24Wvb4qv4XVnOBfasCg0mKn3T3/qHdDga8WkjtYqnMO1HV BT1Ae7lggP2QgLrlYUpNe8Gt+0o4mEm90bvNisWfGsw8T0tuWXoAYlvG9zJcePTDjRB2 84rZOpxscT2sgtHf+XSKPTXucJMfwzRl62knfSqOstkyAY2skExKUnXOZ9Ax/ayQG21Q e5ztzz0U7ToskKgX8hfhxn6OQSgxIoCAzfMvHjUsgaJsxcwaa+tcReYfNmrcw1soPv/N 6Q2A==; dara=google.com 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-80216-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80216-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id o2-20020a05620a110200b00787986509a0si3212436qkk.422.2024.02.25.08.53.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Feb 2024 08:53:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80216-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; 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-80216-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80216-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id CD22B1C21148 for ; Sun, 25 Feb 2024 16:53:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D9D3818AF9; Sun, 25 Feb 2024 16:53:10 +0000 (UTC) Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.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 14A6D17BD4 for ; Sun, 25 Feb 2024 16:53:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.58.85.151 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708879990; cv=none; b=koGTeg7itJeDGJ4I4YsDciiaoCX6eiYX7w9TUIVb2YBIzd+34BdhS5qo3hoTvv7ppB6bclrSCO3q/leNPn5os1hymgECF9PGrlAV1LJnG9ub8wPHOpTwBwcKTxO0BM17BnkzRZyY7fpATfXpWbHpRA5hrKtBY7w5a1ug1cyClvk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708879990; c=relaxed/simple; bh=KLXPJ9GI3IiXMwtDo1lDQylD+73wL23r9MozaWaBREk=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=Ta+cCKTjPyqilkIn5fOREZtWQ/rzb4WCLuypt7RsrYhQ/UD/7THTT5qEFQwrUITdR8e43XJp/Gc9xMkLKIwsQq8h5IU3Tnkg2OHOKnpUdWPHEKFL8Sgv1v0nUkPKB9pFJ5dUCoR86fVMr8bes1jcP7YI4Qwj8ljqC2Bk+eWGQuQ= 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.85.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-274-USz1BRr9PyCo79CglO0c6g-1; Sun, 25 Feb 2024 16:53:04 +0000 X-MC-Unique: USz1BRr9PyCo79CglO0c6g-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, 25 Feb 2024 16:53:03 +0000 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Sun, 25 Feb 2024 16:53:03 +0000 From: David Laight To: "'linux-kernel@vger.kernel.org'" , "'Linus Torvalds'" , 'Netdev' , "'dri-devel@lists.freedesktop.org'" CC: 'Jens Axboe' , "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , "'linux-btrfs@vger.kernel.org'" , "'Andrew Morton'" , 'Andy Shevchenko' , "'David S . Miller'" , 'Dan Carpenter' , "'Jani Nikula'" Subject: [PATCH next v2 07/11] minmax: minmax: Add __types_ok3() and optimise defines with 3 arguments Thread-Topic: [PATCH next v2 07/11] minmax: minmax: Add __types_ok3() and optimise defines with 3 arguments Thread-Index: AdpoCxYgUiRnRfJCT+mB8NVd2q416Q== Date: Sun, 25 Feb 2024 16:53:03 +0000 Message-ID: References: <0fff52305e584036a777f440b5f474da@AcuMS.aculab.com> In-Reply-To: <0fff52305e584036a777f440b5f474da@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 min3() and max3() were added to optimise nested min(x, min(y, z)) sequences, but only moved where the expansion was requiested. Add a separate implementation for 3 argument calls. These are never required to generate constant expressions to remove that logic. Signed-off-by: David Laight --- include/linux/minmax.h | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) Changes for v2: - Typographical and spelling corrections to the commit messages. Patches unchanged. diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 5c7fce76abe5..278a390b8a4c 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -38,6 +38,11 @@ =09((__is_ok_signed(x) && __is_ok_signed(y)) ||=09\ =09 (__is_ok_unsigned(x) && __is_ok_unsigned(y))) =20 +/* Check three values for min3(), max3() and clamp() */ +#define __types_ok3(x, y, z)=09=09=09=09=09=09=09\ +=09((__is_ok_signed(x) && __is_ok_signed(y) && __is_ok_signed(z)) ||=09\ +=09 (__is_ok_unsigned(x) && __is_ok_unsigned(y) && __is_ok_unsigned(z))) + #define __cmp_op_min < #define __cmp_op_max > =20 @@ -87,13 +92,24 @@ #define umax(x, y)=09\ =09__careful_cmp(max, __zero_extend(x), _zero_extend(y), __COUNTER__) =20 +#define __cmp_once3(op, x, y, z, uniq) ({=09\ +=09typeof(x) __x_##uniq =3D (x);=09=09\ +=09typeof(x) __y_##uniq =3D (y);=09=09\ +=09typeof(x) __z_##uniq =3D (z);=09=09\ +=09__cmp(op, __cmp(op, __x_##uniq, __y_##uniq), __z_##uniq); }) + +#define __careful_cmp3(op, x, y, z, uniq) ({=09=09=09=09\ +=09static_assert(__types_ok3(x, y, z),=09=09=09=09\ +=09=09#op "3(" #x ", " #y ", " #z ") signedness error");=09\ +=09__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__) =20 /** * max3 - return maximum of three values @@ -101,7 +117,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__) =20 /** * min_t - return minimum of two values, using the specified type @@ -142,8 +158,7 @@ =09__clamp(__val_##uniq, __lo_##uniq, __hi_##uniq); }) =20 #define __careful_clamp(val, lo, hi, uniq) ({=09=09=09=09=09\ -=09_Static_assert(__types_ok(val, lo), "clamp() 'lo' signedness error");= =09\ -=09_Static_assert(__types_ok(val, hi), "clamp() 'hi' signedness error");= =09\ +=09_Static_assert(__types_ok3(val, lo, hi), "clamp() signedness error");= =09\ =09__clamp_once(val, lo, hi, uniq); }) =20 /** --=20 2.17.1 - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1= PT, UK Registration No: 1397386 (Wales)