From patchwork Sun Feb 25 16:49:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Laight X-Patchwork-Id: 206091 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp1656879dyb; Sun, 25 Feb 2024 08:50:29 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUOOzMNREV9Lhz8bXoz/QOEKdoxIBGmKDlfguew+OVTLXaHl3GZ0j63fARKtQ5VZhPdxB7vEgfPfcJtahRwfNM1Oy9jNA== X-Google-Smtp-Source: AGHT+IEO47544gzhsEzSEH94A8c6B8fk9Ts9hMe7cHOlwMJ5vJVySHVUjP/ABDA+5jE7xfwp0oTC X-Received: by 2002:a17:906:3618:b0:a3e:55e6:35db with SMTP id q24-20020a170906361800b00a3e55e635dbmr3318102ejb.45.1708879829241; Sun, 25 Feb 2024 08:50:29 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708879829; cv=pass; d=google.com; s=arc-20160816; b=dxJ3+U4FkEYnUSoAW3Mdt/xphiWxIfMxd+btSsQQEveLDeBj2MZTxSVL+lC58hZ53q ccvq3yMcOX63S4KXiSg4YX28VxJyhDwtxBHs5oQHpe1gu6ZhiOYY4I2KUWQlNOFLh4Gn Lh9rwjT5O2ePAHDXLnMmjtV/USvuLpo5N1c97h18Bvr+rvXeeGpJh/nNJPbVdhk2mJQK TLkSj86DbiS5n/wzikOyMxjh3wxoqP/x5OmAKWgWnSdSZ1Ksm/jp49ce9juxDeVM9cxn jCjGAlBweNHtb0ZOrWacWbNfbUOO5TjVgVbysY/IZ1pxVy/WmTuo3cLze8+sGkVTPlpp tYkg== 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-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80211-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 z13-20020a1709063acd00b00a3e5053186dsi1353315ejd.206.2024.02.25.08.50.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Feb 2024 08:50:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80211-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-80211-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80211-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 D89091F21A8E for ; Sun, 25 Feb 2024 16:50:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A3E23199B8; Sun, 25 Feb 2024 16:50:07 +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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791890375689695338 X-GMAIL-MSGID: 1791890375689695338 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 > = 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 /* - * 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) \ (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1))) -/* is_signed_type() isn't a constexpr for pointer types */ -#define __is_signed(x) \ - __builtin_choose_expr(__is_constexpr(is_signed_type(typeof(x))), \ - is_signed_type(typeof(x)), 0) +/* Allow unsigned compares against non-negative signed constants. */ +#define __is_ok_unsigned(x) \ + (is_unsigned_type(typeof(x)) || (__is_constexpr(x) ? (x) + 0 >= 0 : 0)) -/* True for a non-negative signed int constant */ -#define __is_noneg_int(x) \ - (__builtin_choose_expr(__is_constexpr(x) && __is_signed(x), x, -1) >= 0) +/* Check for signed after promoting unsigned char/short to int */ +#define __is_ok_signed(x) is_signed_type(typeof((x) + 0)) -#define __types_ok(x, y) \ - (__is_signed(x) == __is_signed(y) || \ - __is_signed((x) + 0) == __is_signed((y) + 0) || \ - __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) \ + ((__is_ok_signed(x) && __is_ok_signed(y)) || \ + (__is_ok_unsigned(x) && __is_ok_unsigned(y))) #define __cmp_op_min < #define __cmp_op_max >