[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20260206222554.676171-1-david.laight.linux@gmail.com>
Date: Fri, 6 Feb 2026 22:25:54 +0000
From: david.laight.linux@...il.com
To: Peter Anvin <hpa@...or.com>,
Andy Shevchenko <andriy.shevchenko@...ux.intel.com>,
Arnd Bergmann <arnd@...nel.org>,
Christoph Hellwig <hch@...radead.org>,
"Jason A . Donenfeld" <Jason@...c4.com>,
Herve Codina <herve.codina@...tlin.com>,
Linus Torvalds <torvalds@...ux-foundation.org>,
Andrew Morton <akpm@...ux-foundation.org>,
linux-kernel@...r.kernel.org
Cc: David Laight <david.laight.linux@...il.com>
Subject: [PATCH next] minmax.h: Use auto for variables in __minmax_array()
From: David Laight <david.laight.linux@...il.com>
While 'auto __element = _array[--__len]' should remove 'const',
gcc prior to version 11 are buggy and retain it.
However forcing an integer promotion by adding zero does work.
Promoting signed/unsigned char and short to int doesn't matter here,
that happens as soon as the value is used.
Type type of the result (for char/short arrays) changes, but the value
will always be promoted to int before it is used (for any purpose) so
it isn't even worth casting the type back - all that is likely to do
is make the compiler explicitly mask it to 8/16 bits before it is
immediately promoted back to int.
Signed-off-by: David Laight <david.laight.linux@...il.com>
---
include/linux/minmax.h | 19 ++++++-------------
1 file changed, 6 insertions(+), 13 deletions(-)
diff --git a/include/linux/minmax.h b/include/linux/minmax.h
index a0158db54a04..7d437f73a6d6 100644
--- a/include/linux/minmax.h
+++ b/include/linux/minmax.h
@@ -239,20 +239,13 @@
* ...
* min = min_array(buff, nb_items);
* --- 8< ---
- *
- * The first typeof(&(array)[0]) is needed in order to support arrays of both
- * 'int *buff' and 'int buff[N]' types.
- *
- * The array can be an array of const items.
- * typeof() keeps the const qualifier. Use __unqual_scalar_typeof() in order
- * to discard the const qualifier for the __element variable.
*/
-#define __minmax_array(op, array, len) ({ \
- typeof(&(array)[0]) __array = (array); \
- typeof(len) __len = (len); \
- __unqual_scalar_typeof(__array[0]) __element = __array[--__len];\
- while (__len--) \
- __element = op(__element, __array[__len]); \
+#define __minmax_array(op, array, len) ({ \
+ auto __array = &(array)[0]; \
+ auto __len = len; \
+ auto __element = __array[--__len] + 0; \
+ while (__len--) \
+ __element = op(__element, __array[__len]); \
__element; })
/**
--
2.39.5
Powered by blists - more mailing lists