linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/1] minmax.h: Slightly relax the type checking done by min() and max().
@ 2022-11-25 15:00 David Laight
  2022-11-27 20:36 ` Linus Torvalds
  2022-11-28  9:13 ` [PATCH 1/1] minmax.h: " David Laight
  0 siblings, 2 replies; 11+ messages in thread
From: David Laight @ 2022-11-25 15:00 UTC (permalink / raw)
  To: LKML, Andy Shevchenko, Andrew Morton
  Cc: Steven Rostedt, 'Joe Perches', Linus Torvalds

Slightly relax the type checking done by min() and max().
- Promote signed/unsiged char/short to int prior to the type test.
  This matches what the compiler does before doing the comparison.
- Skip the type test if either argument is a positive 'int' constant.
  Instead cast the constant to 'int', the compiler may promote it
  back to 'unsigned int' when doing the test.

Reduces the need to use min_t/max_t() and the possibly unwanted
  side effects if a type that is too small is specified.

Signed-off-by: David Laight <David.Laight@aculab.com>
---
 include/linux/minmax.h | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

diff --git a/include/linux/minmax.h b/include/linux/minmax.h
index 5433c08fcc68..77de234cf502 100644
--- a/include/linux/minmax.h
+++ b/include/linux/minmax.h
@@ -9,15 +9,28 @@
  *
  * - avoid multiple evaluations of the arguments (so side-effects like
  *   "x++" happen only once) when non-constant.
- * - perform strict type-checking (to generate warnings instead of
- *   nasty runtime surprises). See the "unnecessary" pointer comparison
- *   in __typecheck().
+ * - perform type-checking (to generate warnings instead of nasty runtime
+ *   surprises). See the "unnecessary" pointer comparison in __typecheck().
  * - retain result as a constant expressions when called with only
  *   constant expressions (to avoid tripping VLA warnings in stack
  *   allocation usage).
+ *
+ * The type-check need not be strict in all cases:
+ * - char and short can be promoted to int.
+ * - comparisons against non-negative constant integers can be done by
+ *   casting the constant to (int).
  */
+#define __is_constzero(x) \
+	(sizeof(*(1 ? ((void *)((long)(x))) : (int *)0)) == sizeof(void *))
+
+#define __is_positive_int(x) __is_constzero((long)(x) >> 31)
+#define __maybe_int_cast(x) __builtin_choose_expr(__is_positive_int(x), (int)(long)(x), x)
+
+#define __typecheck_ptr_type(x, y) \
+	(1 ? (void *)!(__is_positive_int(x) || __is_positive_int(y)) : (typeof((x) + 0) *)0)
+
 #define __typecheck(x, y) \
-	(!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
+	(!!(sizeof(__typecheck_ptr_type(x, y) == __typecheck_ptr_type(y, x))))
 
 #define __no_side_effects(x, y) \
 		(__is_constexpr(x) && __is_constexpr(y))
@@ -25,7 +38,8 @@
 #define __safe_cmp(x, y) \
 		(__typecheck(x, y) && __no_side_effects(x, y))
 
-#define __cmp(x, y, op)	((x) op (y) ? (x) : (y))
+#define __cmp_maybe_int(x, y, op)	((x) op (y) ? (x) : (y))
+#define __cmp(x, y, op)	__cmp_maybe_int(__maybe_int_cast(x), __maybe_int_cast(y), op)
 
 #define __cmp_once(x, y, unique_x, unique_y, op) ({	\
 		typeof(x) unique_x = (x);		\
-- 
2.17.1

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


^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2022-12-22 22:33 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-25 15:00 [PATCH 1/1] minmax.h: Slightly relax the type checking done by min() and max() David Laight
2022-11-27 20:36 ` Linus Torvalds
2022-11-27 21:42   ` David Laight
2022-11-27 21:54     ` Linus Torvalds
2022-11-27 22:26       ` David Laight
2022-11-28  2:42         ` Linus Torvalds
2022-11-28  9:10           ` David Laight
2022-11-28 19:42             ` Linus Torvalds
2022-12-22 22:32               ` [RFC PATCH v2 0/1] " David Laight
2022-12-22 22:33                 ` Subject: [RFC PATCH v2 1/1] " David Laight
2022-11-28  9:13 ` [PATCH 1/1] minmax.h: " David Laight

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).