All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Laight <David.Laight@ACULAB.COM>
To: "'linux-kernel@vger.kernel.org'" <linux-kernel@vger.kernel.org>,
	"'Linus Torvalds'" <torvalds@linux-foundation.org>,
	'Netdev' <netdev@vger.kernel.org>,
	"'dri-devel@lists.freedesktop.org'"
	<dri-devel@lists.freedesktop.org>
Cc: 'Jens Axboe' <axboe@kernel.dk>,
	"'Matthew Wilcox (Oracle)'" <willy@infradead.org>,
	'Christoph Hellwig' <hch@infradead.org>,
	"'linux-btrfs@vger.kernel.org'" <linux-btrfs@vger.kernel.org>,
	"'Andrew Morton'" <akpm@linux-foundation.org>,
	'Andy Shevchenko' <andriy.shevchenko@linux.intel.com>,
	"'David S . Miller'" <davem@davemloft.net>,
	'Dan Carpenter' <dan.carpenter@linaro.org>,
	"'Jani Nikula'" <jani.nikula@linux.intel.com>
Subject: [PATCH next v2 03/11] minmax: Simplify signedness check
Date: Sun, 25 Feb 2024 16:49:59 +0000	[thread overview]
Message-ID: <8657dd5c2264456f8a005520a3b90e2b@AcuMS.aculab.com> (raw)
In-Reply-To: <0fff52305e584036a777f440b5f474da@AcuMS.aculab.com>

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 <david.laight@aculab.com>
---
 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 <linux/types.h>
 
 /*
- * 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 >
-- 
2.17.1

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


  parent reply	other threads:[~2024-02-25 16:50 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-25 16:46 [PATCH next v2 00/11] minmax: Optimise to reduce .i line length David Laight
2024-02-25 16:48 ` [PATCH next v2 01/11] minmax: Put all the clamp() definitions together David Laight
2024-02-25 16:49 ` [PATCH next v2 02/11] minmax: Use _Static_assert() instead of static_assert() David Laight
2024-02-26  9:28   ` Jani Nikula
2024-02-26 10:07     ` David Laight
2024-02-26 10:27       ` Jani Nikula
2024-02-25 16:49 ` David Laight [this message]
2024-02-27  1:34   ` [PATCH next v2 03/11] minmax: Simplify signedness check kernel test robot
2024-02-27  9:10     ` David Laight
2024-02-25 16:50 ` [PATCH next v2 04/11] minmax: Replace multiple __UNIQUE_ID() by directly using __COUNTER__ David Laight
2024-02-25 16:51 ` [PATCH next v2 05/11] minmax: Move the signedness check out of __cmp_once() and __clamp_once() David Laight
2024-02-25 16:52 ` [PATCH next v2 06/11] minmax: Remove 'constexpr' check from __careful_clamp() David Laight
2024-02-25 16:53 ` [PATCH next v2 07/11] minmax: minmax: Add __types_ok3() and optimise defines with 3 arguments David Laight
2024-02-25 16:53 ` [PATCH next v2 08/11] minmax: Add min_const() and max_const() David Laight
2024-02-25 17:13   ` Linus Torvalds
2024-02-25 21:09     ` David Laight
2024-02-25 21:26     ` David Laight
2024-02-26  1:11       ` Dave Airlie
2024-02-25 16:54 ` [PATCH next v2 09/11] tree-wide: minmax: Replace all the uses of max() for array sizes with max_const() David Laight
2024-02-25 16:56 ` [PATCH next v2 10/11] block: Use a boolean expression instead of max() on booleans David Laight
2024-02-25 16:56 ` [PATCH next v2 11/11] minmax: min() and max() don't need to return constant expressions David Laight
2024-02-26  9:42   ` kernel test robot
2024-02-26 10:16     ` David Laight
2024-02-26 10:56   ` kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8657dd5c2264456f8a005520a3b90e2b@AcuMS.aculab.com \
    --to=david.laight@aculab.com \
    --cc=akpm@linux-foundation.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=axboe@kernel.dk \
    --cc=dan.carpenter@linaro.org \
    --cc=davem@davemloft.net \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=hch@infradead.org \
    --cc=jani.nikula@linux.intel.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.