From patchwork Sun Feb 25 16:53:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Laight X-Patchwork-Id: 206095 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp1657782dyb; Sun, 25 Feb 2024 08:53:33 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX7MjJE4/BGbLgeAEmJwDg0HEKM6gWDOZ/dNF63JIr/F0smHDcuHR2DK6xVxoC+doG4MqF+Pba79kmfKzC52pr7otw1iA== X-Google-Smtp-Source: AGHT+IEGJnpP9/5J2c/kCZJOZF/1opFArbnTBWr8XdONBOg1H6qdnhaiENJli5Cn7Nyrm2B0v+cw X-Received: by 2002:a17:906:3411:b0:a43:1415:2f1e with SMTP id c17-20020a170906341100b00a4314152f1emr1527190ejb.69.1708880013358; Sun, 25 Feb 2024 08:53:33 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708880013; cv=pass; d=google.com; s=arc-20160816; b=YyXxqG/JRvVhcZ3Cacp4890j2cUyL0fkuEuVlMCgmaBoksJc/u794wCkj8K2GfVfCg lY5SK9jlq4RekH6Evqc3MHR5glUfvPA/YCyiYdRkseOGEtsJBcRDdckqAwRxON1ZBhyA K/OTSyTmtQXTAjfrf+Z0jwwM6gU0qkfPjqNfUJ/Gk15sZHF04cDRI6vDBhZLrUIJZ2XR SZb9zSJeL+1eTG1E2W1+SMg6syELS7HmiSWNDibMzScIMlCZjOlBc1TsUb2/tMk0KRIk kvIkiwwDkuxQ0UTUl4AxW5yqEOoko5OzW7Gj4mcqEGwLEuWTcoQGzxr/cl+8E60KIljC FlVQ== 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-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80216-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id r21-20020a170906365500b00a42f385aed9si1251447ejb.561.2024.02.25.08.53.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Feb 2024 08:53:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80216-ouuuleilei=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-80216-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80216-ouuuleilei=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 CBA721F2163E for ; Sun, 25 Feb 2024 16:53:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 230521AAA5; Sun, 25 Feb 2024 16:53:12 +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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791890569176660579 X-GMAIL-MSGID: 1791890569176660579 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 @@ ((__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 > @@ -87,13 +92,24 @@ #define umax(x, y) \ __careful_cmp(max, __zero_extend(x), _zero_extend(y), __COUNTER__) +#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 @@ -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__) /** * min_t - return minimum of two values, using the specified type @@ -142,8 +158,7 @@ __clamp(__val_##uniq, __lo_##uniq, __hi_##uniq); }) #define __careful_clamp(val, lo, hi, uniq) ({ \ - _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_once(val, lo, hi, uniq); }) /**