From patchwork Sun Jul 28 14:20:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Laight X-Patchwork-Id: 13744005 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 E4A44C3DA64 for ; Sun, 28 Jul 2024 14:21:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3783F6B007B; Sun, 28 Jul 2024 10:21:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 328676B0083; Sun, 28 Jul 2024 10:21:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1F0136B0085; Sun, 28 Jul 2024 10:21:33 -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 03FF06B007B for ; Sun, 28 Jul 2024 10:21:32 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 7BAF01202F5 for ; Sun, 28 Jul 2024 14:21:32 +0000 (UTC) X-FDA: 82389374424.07.89187D6 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) by imf24.hostedemail.com (Postfix) with ESMTP id 24487180003 for ; Sun, 28 Jul 2024 14:21:29 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=none; spf=pass (imf24.hostedemail.com: domain of david.laight@aculab.com designates 185.58.85.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com; dmarc=pass (policy=none) header.from=aculab.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722176463; a=rsa-sha256; cv=none; b=NkG1hI4Ga7v9CNlPmS/mErRmpaCNVD6URaF3iR+QDURgDvTEBWan+2n6uJz75t7S7UBUTS Lu0JVNWDYlD0u3OjenmNP3iFxrnSHiEzSf3hFYP0Glcv47i+W/RpPx4FjlL7D7jAluGUWY WflTf93ojJE6fgsG03/lx54+CTjYDZU= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; spf=pass (imf24.hostedemail.com: domain of david.laight@aculab.com designates 185.58.85.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com; dmarc=pass (policy=none) header.from=aculab.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722176463; 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=1ScHcBhe6toW73FIdpd2QEq4DTHDTnTuCAdGm27/GBA=; b=6cM44dCNlN4fFtlmKVNu+Ud3dMiqUvCn/rjWejar7+cKXsoWsVGbm0Fa1x+GqD1Mp1TZdH 5SQghZcxmUPTwnoRQNv1BFgxhyKCJ7v4bF5nk0+G9LRDjbdNShHxIoSAIjQYOo6Zzayvdo lsL3eLH4bO6BAdP7aR+F87aDcjonB9A= 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-240-gyu3qikDN8SBJ-lv0Ijmhg-1; Sun, 28 Jul 2024 15:21:25 +0100 X-MC-Unique: gyu3qikDN8SBJ-lv0Ijmhg-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 Jul 2024 15:20:41 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Sun, 28 Jul 2024 15:20:41 +0100 From: David Laight To: "'linux-kernel@vger.kernel.org'" CC: 'Linus Torvalds' , 'Jens Axboe' , "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , 'Andrew Morton' , 'Andy Shevchenko' , 'Dan Carpenter' , 'Arnd Bergmann' , "'Jason@zx2c4.com'" , "'pedro.falcato@gmail.com'" , 'Mateusz Guzik' , "'linux-mm@kvack.org'" , 'Lorenzo Stoakes' Subject: [PATCH v2 4/8] minmax: Simplify signedness check Thread-Topic: [PATCH v2 4/8] minmax: Simplify signedness check Thread-Index: Adrg+WoBXlsaZZWKRzqVZpRDUvrsDQ== Date: Sun, 28 Jul 2024 14:20:41 +0000 Message-ID: <74e0b027a908461da879b69b0e12c0de@AcuMS.aculab.com> References: <402c3c617c29465c898b1af55e3c6095@AcuMS.aculab.com> In-Reply-To: <402c3c617c29465c898b1af55e3c6095@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-Stat-Signature: ujarzgnjum9by36q9emiyrdoioxfwymp X-Rspamd-Queue-Id: 24487180003 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1722176489-159046 X-HE-Meta: U2FsdGVkX1/TfbmluhMq8KEdv2wuYfdudatbOff9Y8O9RRs4ArqA95ZvdKM0xOfHMJls9eNoxbiFu560NSmy7qXRPi2t8UOz8HsxolHnFEL6bWhqHjUwkthUhCqGcS1prcFGib+PmigFRhGgMar+G3sIL24GLeOF8iS0TxORlX90rbWMCqhwMSab1odpEbKhz686pKN/MfFqH/CnsqkMmBfWj4/50v6k5+yitDC78nXrvZj1l6Az6VlJ7QYTkjF2ZQWXtGWNZow0z9NKx1076EgZLpr0eai6FW6foux9Xxm/TO0virNbt9ZN3lyRE17oY0XzdZ7lmnYe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KQ== 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 --- Changes for v2: - Wrap is_signed_type() to avoid issues with pointer types because (foo *)1 isn't a compile time constant. - Remove the '+ 0' from __is_ok_unsigned(). This converted 'bool' to 'int' to avoid a compiler warning and is no longer needed because of the implicit conversion dome by ?:. include/linux/minmax.h | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index ab64b2e73ae5..b9b5348a3879 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,20 @@ #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) +#define __is_signed(x) \ + __if_constexpr((typeof(x))1, is_signed_type(typeof(x)), 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) +/* Allow unsigned compares against non-negative signed constants. */ +#define __is_ok_unsigned(x) \ + ((!__is_signed((x)) ? 0 : __if_constexpr(x, x, -1)) >= 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)) +/* Check for signed after promoting unsigned char/short to int */ +#define __is_ok_signed(x) __is_signed((x) + 0) + +/* 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 >