Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp22015rdb; Sun, 28 Jan 2024 11:32:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IH1DLhN5uTnWtqB91eh4MKHKpZIxQ7SlYNl66WP76l4NRfvJKkjzP2ANdLtMqfMdi5QA8mb X-Received: by 2002:a17:906:5910:b0:a35:772b:2b9 with SMTP id h16-20020a170906591000b00a35772b02b9mr1817910ejq.75.1706470322140; Sun, 28 Jan 2024 11:32:02 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706470322; cv=pass; d=google.com; s=arc-20160816; b=xRG/WdM3C93cLSeBqI6ly0/1s0JFcZfXktWDuT9a68ccevjm+EcNzWR1/J1yOTDu/I uNGau68OKU5sx90vIR2Wjkn0s3H/I0n2IqXjte9NIVZrkO8ljL7NxRr10bh9+6lHdk8N RJPUbFg3Gs6btvwZVu+DwFjcoPtRT0qURfoj6GxjOU+J851zc8b23UgauTdtwdXD3z0c 0ApMZ49jBcPP2X4uaMiwXcdwOpYbJ72x7oa/kd1G1P4Js/UN8r7uRRYZDrsWEVAkJdns A8CW9XQrIB2JFTz5Dn+ah0VC5elYA2wipItCx04iwiuMqRhwnvw6f30wVJHQ6eKiZw9q EmFQ== 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=asWWKQZjLI1C/6ZhPmcGLyOWEU4vM2NaCeDQRDTkx3g=; fh=jAOu/RGlQxfhPk7PcGNzOfYLHKZO+STAaG1daOB/TAU=; b=LyVzcaa7Y5GOqEIH8DUGP6xKgp1vkZf/OXzFrs0yIvqm+dsiTyL+SrC5PTmsSaCXd+ 5zc87h9c8bhmhdM9WdWEwIcFMCs56u7zahbptx8YeZpYKC6yH9D/aXUTHGGGmxU43GOe 3ppYY4lFO5dvoX4nMobrVjLPthmaHBsyK/tBKo3ZJFumP8x4MIIxc+nVG2qqXLVi+6H1 R51RMXCj4qxJYLV/KyDomk6EBXvq2rh7wiajVeKHs7ILneAJyHsWGsvvgREJpD/x3+kO GGRS8W3VfMZsV4oEPswzJdyK5YVy5DXBFOCLd701ic+b5QE1tXAKal0qRk4B6LuWceuZ 3yaA== 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-41904-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41904-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 qc22-20020a170906d8b600b00a3588ab319csi813765ejb.697.2024.01.28.11.32.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Jan 2024 11:32:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-41904-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-41904-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41904-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 B5C891F220F2 for ; Sun, 28 Jan 2024 19:32:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D203C33CFB; Sun, 28 Jan 2024 19:31:51 +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 A2FD12E854 for ; Sun, 28 Jan 2024 19:31:49 +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=1706470311; cv=none; b=p3wcYktQCrjuEwthnH73dFJ3+Apzmxz/FT6T/Da5KHJ8dYsErzk7Xa7y8TX+vXGXO2SemLTsfDx7FVM7I6uHGOsbHQp7xjUIVla7WhbWNyhZnoHqBrZjUJWDFemxvo9KzNieR5+wGgdr0EI8GrfFT8E+JMCgO9ZqEnNlJrCk/Uo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706470311; c=relaxed/simple; bh=7CXwjdiDT55ZZOOhjTgeG/9fLB0VllvCffmj//4XF4Y=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=K+nrfsFAe6hwbx1lXwQC8X7Ajr+MZo1/a5ourRDDQpKo1w3EvNlw0kcX3qbjmXqoGvEuGwRAsPihZFqHHrsP0x7ooJIpUq97K22QHpBfPIVpotUpKyFapmshXGmHjXoKdBXxDTIzQcteNshHKDKuAJvRX7iKOMAQcSUjzrvIBJk= 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-169-WRvxRy7ONzyzi7NxjGX4Dw-1; Sun, 28 Jan 2024 19:31:47 +0000 X-MC-Unique: WRvxRy7ONzyzi7NxjGX4Dw-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:31:22 +0000 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Sun, 28 Jan 2024 19:31:22 +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 0711] minmax: minmax: Add __types_ok3() and optimise defines with 3 arguments Thread-Topic: [PATCH next 0711] minmax: minmax: Add __types_ok3() and optimise defines with 3 arguments Thread-Index: AdpSIJ67DKuJ6SuSTnaAcsGvmvekeg== Date: Sun, 28 Jan 2024 19:31:22 +0000 Message-ID: <78f9c15ad67940fe91ac39b9c26113be@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 min3() and max3() were added to optimise nested min(x, min(y, z)) sequences, bit only moved where the expansion was requiested. Add a separate implementation for 3 argument calls. These are never required to generate constant expressiions to remove that logic. Signed-off-by: David Laight --- include/linux/minmax.h | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) 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)