* [PATCH next] minmax.h: Use auto for variables in __minmax_array()
@ 2026-02-06 22:25 david.laight.linux
2026-02-06 22:41 ` Andrew Morton
2026-02-07 10:50 ` David Laight
0 siblings, 2 replies; 7+ messages in thread
From: david.laight.linux @ 2026-02-06 22:25 UTC (permalink / raw)
To: Peter Anvin, Andy Shevchenko, Arnd Bergmann, Christoph Hellwig,
Jason A . Donenfeld, Herve Codina, Linus Torvalds, Andrew Morton,
linux-kernel
Cc: David Laight
From: David Laight <david.laight.linux@gmail.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@gmail.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
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH next] minmax.h: Use auto for variables in __minmax_array()
2026-02-06 22:25 [PATCH next] minmax.h: Use auto for variables in __minmax_array() david.laight.linux
@ 2026-02-06 22:41 ` Andrew Morton
2026-02-07 10:25 ` David Laight
2026-02-07 10:50 ` David Laight
1 sibling, 1 reply; 7+ messages in thread
From: Andrew Morton @ 2026-02-06 22:41 UTC (permalink / raw)
To: david.laight.linux
Cc: Peter Anvin, Andy Shevchenko, Arnd Bergmann, Christoph Hellwig,
Jason A . Donenfeld, Herve Codina, Linus Torvalds, linux-kernel
On Fri, 6 Feb 2026 22:25:54 +0000 david.laight.linux@gmail.com wrote:
> From: David Laight <david.laight.linux@gmail.com>
>
> While 'auto __element = _array[--__len]' should remove 'const',
> gcc prior to version 11 are buggy and retain it.
With what effect?
> 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
s/Type type/Type/ ?
> 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.
I'm not understanding the motivation for this change. Is there some
compilation issue to be addressed?
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH next] minmax.h: Use auto for variables in __minmax_array()
2026-02-06 22:41 ` Andrew Morton
@ 2026-02-07 10:25 ` David Laight
2026-02-08 2:25 ` Andrew Morton
0 siblings, 1 reply; 7+ messages in thread
From: David Laight @ 2026-02-07 10:25 UTC (permalink / raw)
To: Andrew Morton
Cc: Peter Anvin, Andy Shevchenko, Arnd Bergmann, Christoph Hellwig,
Jason A . Donenfeld, Herve Codina, Linus Torvalds, linux-kernel
On Fri, 6 Feb 2026 14:41:35 -0800
Andrew Morton <akpm@linux-foundation.org> wrote:
> On Fri, 6 Feb 2026 22:25:54 +0000 david.laight.linux@gmail.com wrote:
>
> > From: David Laight <david.laight.linux@gmail.com>
> >
> > While 'auto __element = _array[--__len]' should remove 'const',
> > gcc prior to version 11 are buggy and retain it.
>
> With what effect?
If you have:
int f(const int x)
{
auto y = x;
y++;
return y;
}
gcc prior to 11.0 error that y is const.
So in this case the loop can't change __element.
The constness is also kept by 'auto y = +x' which does integer promotion
(useful for converting enums) and both -x and ~x.
> > 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
>
> s/Type type/Type/ ?
Actually s/Type/the/
> > 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.
>
> I'm not understanding the motivation for this change. Is there some
> compilation issue to be addressed?
Mainly unqual_scalar_typeof() being horrid.
There is an ongoing long thread about its use in the arm64 LTO READ_ONCE().
Newer compilers do have a builtin, and there are some shorter alternatives
that work in some places.
But here is just isn't needed.
So one less place to check.
I did mean to copy the main contributers to that thread, but forgot.
David
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH next] minmax.h: Use auto for variables in __minmax_array()
2026-02-07 10:25 ` David Laight
@ 2026-02-08 2:25 ` Andrew Morton
2026-02-08 11:33 ` David Laight
0 siblings, 1 reply; 7+ messages in thread
From: Andrew Morton @ 2026-02-08 2:25 UTC (permalink / raw)
To: David Laight
Cc: Peter Anvin, Andy Shevchenko, Arnd Bergmann, Christoph Hellwig,
Jason A . Donenfeld, Herve Codina, Linus Torvalds, linux-kernel
On Sat, 7 Feb 2026 10:25:51 +0000 David Laight <david.laight.linux@gmail.com> wrote:
> On Fri, 6 Feb 2026 14:41:35 -0800
> Andrew Morton <akpm@linux-foundation.org> wrote:
>
> > On Fri, 6 Feb 2026 22:25:54 +0000 david.laight.linux@gmail.com wrote:
> >
> > > From: David Laight <david.laight.linux@gmail.com>
> > >
> > > While 'auto __element = _array[--__len]' should remove 'const',
> > > gcc prior to version 11 are buggy and retain it.
> >
> > With what effect?
>
> If you have:
> int f(const int x)
> {
> auto y = x;
> y++;
> return y;
> }
> gcc prior to 11.0 error that y is const.
> So in this case the loop can't change __element.
Still not undersanding, sorry. Does this patch fix a build issue with
any compiler/kernel combination?
> > I'm not understanding the motivation for this change. Is there some
> > compilation issue to be addressed?
>
> Mainly unqual_scalar_typeof() being horrid.
> There is an ongoing long thread about its use in the arm64 LTO READ_ONCE().
> Newer compilers do have a builtin, and there are some shorter alternatives
> that work in some places.
> But here is just isn't needed.
> So one less place to check.
OK, so it's a cleanup.
> I did mean to copy the main contributers to that thread, but forgot.
I think a v2 would be good please, to clarify the effects of and
motivation for the change.
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH next] minmax.h: Use auto for variables in __minmax_array()
2026-02-08 2:25 ` Andrew Morton
@ 2026-02-08 11:33 ` David Laight
0 siblings, 0 replies; 7+ messages in thread
From: David Laight @ 2026-02-08 11:33 UTC (permalink / raw)
To: Andrew Morton
Cc: Peter Anvin, Andy Shevchenko, Arnd Bergmann, Christoph Hellwig,
Jason A . Donenfeld, Herve Codina, Linus Torvalds, linux-kernel
On Sat, 7 Feb 2026 18:25:59 -0800
Andrew Morton <akpm@linux-foundation.org> wrote:
> On Sat, 7 Feb 2026 10:25:51 +0000 David Laight <david.laight.linux@gmail.com> wrote:
>
> > On Fri, 6 Feb 2026 14:41:35 -0800
> > Andrew Morton <akpm@linux-foundation.org> wrote:
> >
> > > On Fri, 6 Feb 2026 22:25:54 +0000 david.laight.linux@gmail.com wrote:
> > >
> > > > From: David Laight <david.laight.linux@gmail.com>
> > > >
> > > > While 'auto __element = _array[--__len]' should remove 'const',
> > > > gcc prior to version 11 are buggy and retain it.
> > >
> > > With what effect?
> >
> > If you have:
> > int f(const int x)
> > {
> > auto y = x;
> > y++;
> > return y;
> > }
> > gcc prior to 11.0 error that y is const.
> > So in this case the loop can't change __element.
>
> Still not undersanding, sorry. Does this patch fix a build issue with
> any compiler/kernel combination?
>
> > > I'm not understanding the motivation for this change. Is there some
> > > compilation issue to be addressed?
> >
> > Mainly unqual_scalar_typeof() being horrid.
> > There is an ongoing long thread about its use in the arm64 LTO READ_ONCE().
> > Newer compilers do have a builtin, and there are some shorter alternatives
> > that work in some places.
> > But here is just isn't needed.
> > So one less place to check.
>
> OK, so it's a cleanup.
>
> > I did mean to copy the main contributers to that thread, but forgot.
>
> I think a v2 would be good please, to clarify the effects of and
> motivation for the change.
Ok - I always expect to do a v2, the way of the world.
David
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH next] minmax.h: Use auto for variables in __minmax_array()
2026-02-06 22:25 [PATCH next] minmax.h: Use auto for variables in __minmax_array() david.laight.linux
2026-02-06 22:41 ` Andrew Morton
@ 2026-02-07 10:50 ` David Laight
2026-02-10 1:38 ` Marco Elver
1 sibling, 1 reply; 7+ messages in thread
From: David Laight @ 2026-02-07 10:50 UTC (permalink / raw)
To: Peter Anvin, Andy Shevchenko, Arnd Bergmann, Christoph Hellwig,
Jason A . Donenfeld, Herve Codina, Linus Torvalds, Andrew Morton,
linux-kernel
Cc: Marco Elver, Will Deacon, Peter Zijlstra
On Fri, 6 Feb 2026 22:25:54 +0000
david.laight.linux@gmail.com wrote:
Cc the people discussing unqual_scalar_typeof() for arm64 LTO READ_ONCE().
> From: David Laight <david.laight.linux@gmail.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@gmail.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; })
>
> /**
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH next] minmax.h: Use auto for variables in __minmax_array()
2026-02-07 10:50 ` David Laight
@ 2026-02-10 1:38 ` Marco Elver
0 siblings, 0 replies; 7+ messages in thread
From: Marco Elver @ 2026-02-10 1:38 UTC (permalink / raw)
To: David Laight
Cc: Peter Anvin, Andy Shevchenko, Arnd Bergmann, Christoph Hellwig,
Jason A . Donenfeld, Herve Codina, Linus Torvalds, Andrew Morton,
linux-kernel, Will Deacon, Peter Zijlstra
On Sat, 7 Feb 2026 at 11:50, David Laight <david.laight.linux@gmail.com> wrote:
>
> On Fri, 6 Feb 2026 22:25:54 +0000
> david.laight.linux@gmail.com wrote:
>
> Cc the people discussing unqual_scalar_typeof() for arm64 LTO READ_ONCE().
>
>
> > From: David Laight <david.laight.linux@gmail.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@gmail.com>
Acked-by: Marco Elver <elver@google.com>
I'm looking forward to the day we can delete __unqual_scalar_typeof()
and all the other hacks.
Thanks!
> > ---
> > 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; })
> >
> > /**
>
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-02-10 1:39 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-06 22:25 [PATCH next] minmax.h: Use auto for variables in __minmax_array() david.laight.linux
2026-02-06 22:41 ` Andrew Morton
2026-02-07 10:25 ` David Laight
2026-02-08 2:25 ` Andrew Morton
2026-02-08 11:33 ` David Laight
2026-02-07 10:50 ` David Laight
2026-02-10 1:38 ` Marco Elver
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox