Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp896313rbb; Sun, 25 Feb 2024 08:50:14 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUv1f9WlnYymsBvEYyYU9XrhnC0YYko5HYQI/tt1+vQLIiYOJVop9QQK1Q+gyoMfqYXHH+3o27mKhmwE73qgIjL5huA5y1FBWgiaUIaSA== X-Google-Smtp-Source: AGHT+IGY07z117lgXBHZl9COrWwUhLoYlXCL3bDuLXIYLXAu/beU7vYCEeDXW7FWB0sCDepfbrh5 X-Received: by 2002:a05:6a20:b91b:b0:1a1:5:e883 with SMTP id fe27-20020a056a20b91b00b001a10005e883mr95226pzb.22.1708879814614; Sun, 25 Feb 2024 08:50:14 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708879814; cv=pass; d=google.com; s=arc-20160816; b=wK7xXMI+sj6dsUbJ5xaNjwCo1UFGndcSqzTA574At9bpvnxq9t65rZJ7PCkvEb9swC Kq35mU4zMw3mADrcoqdC3lOq2sztAkbjOysYYvJRmxHrfA2lNtZbyFHE1fnzgz+lPlhE 5HFgJjI5JDKgR7va/ZZeGRu3ecHbtBmaDolOxxDaHDYcJM4ak58EXJNqo0+la2ptS1q8 lZQFGt1+Y2wMeHpUHkCy3lLJ9qvmq0LheaYNyK/J9F+i8KQ9Q5oGZCHolI1uu4OkFB70 H4Vc19V+VWx0XvH4Y9EpPqyHvhy0xoENIkV0ptg3jawD2vtRp1U8rCJeGy8+G+0ORTv9 jNtg== 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=Tl95No0Gjei3R+OvX6WRb5W/BRSAwiJ28HXr42thZUg=; fh=nGE692sbVdRvha6RYbzMUXtm1nr/30sV7frdtDN2z14=; b=lm5r9wNv8gxGILBJ1WQQrI5WskdoyXQN9q7cKSFkVuE+pOnr4PY/Ypkk7mellNZM8F T2s3Pt7yW5EbPS0wU7+txTFM+HJyvgMWShZwPG/MwfNDEYnr4UCSqaai4bh2VSCupolK moZjQur8kH25kLCYvcKHsUnizjoJRVShN9l21nrdmDPaS9XSVSSwGzP5KB5xfoG5bVJf IF5OaYTDxZFlq/WWnC5WlJX/0LDM2JV/0PGdX3LEYJH/EDMILq10jMY1sIbVfjMwIchw To2rRPHDY3qwzJzt6GhZB+2xOff+2rzxVF2DrGEv8dfhMgs4uCGgpQfVER2m17sl2GJ5 zkQw==; 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-80211-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80211-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id g185-20020a636bc2000000b005db652f1ef0si2399047pgc.545.2024.02.25.08.50.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Feb 2024 08:50:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80211-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; 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-80211-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80211-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 522FA28149B for ; Sun, 25 Feb 2024 16:50:14 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 974DC1802E; Sun, 25 Feb 2024 16:50:06 +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 7A45D14006 for ; Sun, 25 Feb 2024 16:50:04 +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=1708879806; cv=none; b=FzPzRN9LoVNGe14IMTPG/uEiF5Y1gLsilVTa65TriPzoeMPen42+CDbqzr9JFTIR3ElIEW/ayx50ESEVMLXt4JvbyL8D4mkJBtwGiSHkg7jcAP1CM2/TfC7d1qrkE4yIifNZQ1cqMBoIRPSRwdjaWgr4/TvmG2wGiuwxkIZtxt8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708879806; c=relaxed/simple; bh=URi7AE+I5CngHeVQukg72Q07R1uxCOMsGMofCc3A+30=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=blEsXP66cBYuRUbt7a+Mf0k6IjsUDHr84WT5921wPD4xgN2LBH3MY2Njdd+LZOA/YVl23Lw+tnvfDovE03WnBVZ3sXJlNdZ/VMpV2dsCObJdFzjcUrNtdcZmaEC/zAEREpm+Leych5tpCdLDDfmET2zc/r8g3nMgkoyZ93ZwgGs= 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-43-W8RGFR3EPR-AcuGUd8JL4Q-1; Sun, 25 Feb 2024 16:50:00 +0000 X-MC-Unique: W8RGFR3EPR-AcuGUd8JL4Q-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:49:59 +0000 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Sun, 25 Feb 2024 16:49:59 +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 03/11] minmax: Simplify signedness check Thread-Topic: [PATCH next v2 03/11] minmax: Simplify signedness check Thread-Index: AdpoCqfCgp/0gHjwSqumBl0qZkMqdg== Date: Sun, 25 Feb 2024 16:49:59 +0000 Message-ID: <8657dd5c2264456f8a005520a3b90e2b@AcuMS.aculab.com> 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 It is enough to check that both 'x' and 'y' are valid for either a signed compare or an unsigned compare. For unsigned they must be an unsigned type or a positive constant. For signed they must be signed after unsigned char/short are promoted. The predicate for _Static_assert() only needs to be a compile-time constant not a constant integeger expression. In particular the short-circuit evaluation of || && ?: can be used to avoid the non-constantness of (pointer_type)1 in is_signed_type(). The '+ 0' in '(x) + 0 > =3D 0' is there to convert 'bool' to 'int' and avoid a compiler warning because max() gets used for 'bool' in one place (a very expensive 'or'). (The code is optimised away by two earlier checks - but the compiler still bleats.) Signed-off-by: David Laight --- include/linux/minmax.h | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 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 900eec7a28e5..c32b4b40ce01 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -8,7 +8,7 @@ #include =20 /* - * min()/max()/clamp() macros must accomplish three things: + * min()/max()/clamp() macros must accomplish several things: * * - Avoid multiple evaluations of the arguments (so side-effects like * "x++" happen only once) when non-constant. @@ -26,19 +26,17 @@ #define __typecheck(x, y) \ =09(!!(sizeof((typeof(x) *)1 =3D=3D (typeof(y) *)1))) =20 -/* is_signed_type() isn't a constexpr for pointer types */ -#define __is_signed(x) =09=09=09=09=09=09=09=09\ -=09__builtin_choose_expr(__is_constexpr(is_signed_type(typeof(x))),=09\ -=09=09is_signed_type(typeof(x)), 0) +/* Allow unsigned compares against non-negative signed constants. */ +#define __is_ok_unsigned(x) \ +=09(is_unsigned_type(typeof(x)) || (__is_constexpr(x) ? (x) + 0 >=3D 0 : 0= )) =20 -/* True for a non-negative signed int constant */ -#define __is_noneg_int(x)=09\ -=09(__builtin_choose_expr(__is_constexpr(x) && __is_signed(x), x, -1) >=3D= 0) +/* Check for signed after promoting unsigned char/short to int */ +#define __is_ok_signed(x) is_signed_type(typeof((x) + 0)) =20 -#define __types_ok(x, y) =09=09=09=09=09\ -=09(__is_signed(x) =3D=3D __is_signed(y) ||=09=09=09\ -=09=09__is_signed((x) + 0) =3D=3D __is_signed((y) + 0) ||=09\ -=09=09__is_noneg_int(x) || __is_noneg_int(y)) +/* Allow if both x and y are valid for either signed or unsigned compares.= */ +#define __types_ok(x, y)=09=09=09=09\ +=09((__is_ok_signed(x) && __is_ok_signed(y)) ||=09\ +=09 (__is_ok_unsigned(x) && __is_ok_unsigned(y))) =20 #define __cmp_op_min < #define __cmp_op_max > --=20 2.17.1 - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1= PT, UK Registration No: 1397386 (Wales)