From patchwork Wed Jul 24 14:30:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Laight X-Patchwork-Id: 13741015 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4A9F5C3DA63 for ; Wed, 24 Jul 2024 14:31:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DEF1A6B0089; Wed, 24 Jul 2024 10:31:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D9F3A6B008A; Wed, 24 Jul 2024 10:31:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C67106B008C; Wed, 24 Jul 2024 10:31:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id A6B886B0089 for ; Wed, 24 Jul 2024 10:31:38 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 15302C0A4E for ; Wed, 24 Jul 2024 14:31:38 +0000 (UTC) X-FDA: 82374884676.01.23C44C0 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.86.151]) by imf22.hostedemail.com (Postfix) with ESMTP id B95F4C002A for ; Wed, 24 Jul 2024 14:31:35 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=aculab.com; spf=pass (imf22.hostedemail.com: domain of david.laight@aculab.com designates 185.58.86.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721831460; a=rsa-sha256; cv=none; b=JCynUWCrmOILKzHQ6i0r2UgzQIStEbIviDlVjYu2JznKNHk/Kj0bUFqQKPkaXbHDnj19CI 5N8re1j/89DIG7m76mRphvOdhp1yhCGWEU9vO4s4l2Zf15cuW0Wg84FmdYgXj2wUEmbs6b Hy3XdEq/BXjFKs63oLN93vybiLqecXQ= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=aculab.com; spf=pass (imf22.hostedemail.com: domain of david.laight@aculab.com designates 185.58.86.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721831460; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0i4IP2NajFZH0aPZhVutvsRIxZ3rsE/RmZYLnb8vip0=; b=Pa9iV6hfTAzfKGRXNIxGpGnb+MJRb9mMMHVn658vFvk8XQEOHfVg62UIJpKOwICKpe5s+h 9hk9NCdCsHIoJrfWKtSxh3atwvwwb5SRaxJxVYpB8PrzhuuP4X80CDptYx48gOfZ7Kky7/ RCtAAIfrk1fbx5JDogb1OpjBfGZofTI= 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-45-vGdKy7opNf27HweZ6J__xg-1; Wed, 24 Jul 2024 15:31:32 +0100 X-MC-Unique: vGdKy7opNf27HweZ6J__xg-1 Received: from AcuMS.Aculab.com (10.202.163.4) by AcuMS.aculab.com (10.202.163.4) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Wed, 24 Jul 2024 15:30:52 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Wed, 24 Jul 2024 15:30:52 +0100 From: David Laight To: "'linux-kernel@vger.kernel.org'" , "'Linus Torvalds'" CC: "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , 'Andrew Morton' , "'Andy Shevchenko'" , 'Dan Carpenter' , 'Arnd Bergmann' , "'Jason@zx2c4.com'" , "'hch@infradead.org'" , "'pedro.falcato@gmail.com'" , 'Mateusz Guzik' , "'linux-mm@kvack.org'" Subject: [PATCH 4/7] minmax: Simplify signedness check Thread-Topic: [PATCH 4/7] minmax: Simplify signedness check Thread-Index: Adrd1i0k/JcX2h1sSAO9D37F5HIFAA== Date: Wed, 24 Jul 2024 14:30:52 +0000 Message-ID: <03601661326c4efba4e618ead15fa0e2@AcuMS.aculab.com> References: <23bdb6fc8d884ceebeb6e8b8653b8cfe@AcuMS.aculab.com> In-Reply-To: <23bdb6fc8d884ceebeb6e8b8653b8cfe@AcuMS.aculab.com> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.202.205.107] MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US X-Rspamd-Queue-Id: B95F4C002A X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 4d6wpohf66sf5tmzyq985mwbdkyqrtwx X-HE-Tag: 1721831495-363206 X-HE-Meta: U2FsdGVkX1/Z86cE7kxsv7MS7mVa2Z74poHJn/fE/SUMuSVjpyeFyglE15TGblGwWFUz3xacTwv3os2cBM81T8W+p+VYMXbqZBe8mQfbJCb/mpbl0V7BV3fW5NHc9Zg5yoBDV+GLaveD4ScuVzvy+uKYV7gaJAV+irarCSKyZ6FK/TxJxdxqATgbqnMZKjhQxBlzit20W1gvYOfX0aRupEQ7eZGnT2zxx2pcSLEhVoRZNjwPSnWu/S2ySnskn/YoDiK0ddONmvGDhshAXI3R0NIL3iP/ZaD89a8j8uSu7ZRiy17C0P9vFm8Ps5tlW/DT+gD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3g== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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. Order the expressions to avoid warnings about comparisons that are always true. Signed-off-by: David Laight --- include/linux/minmax.h | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 900eec7a28e5..d3ac65c1add7 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)) ? 0 : __if_constexpr(x, (x) + 0, -1)) >= 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 >