Re: [PATCH v7 1/4] bitops: Introduce the the for_each_set_clump macro

From: Rikard Falkeborn
Date: Wed Jun 03 2020 - 17:53:22 EST


On Wed, Jun 03, 2020 at 11:49:37AM +0300, Andy Shevchenko wrote:
> On Tue, Jun 2, 2020 at 10:01 PM Rikard Falkeborn
> <rikard.falkeborn@xxxxxxxxx> wrote:
> > On Mon, Jun 01, 2020 at 11:33:30AM +0300, Andy Shevchenko wrote:
> > > On Mon, Jun 01, 2020 at 12:37:16AM +0200, Rikard Falkeborn wrote:
> > > > On Sun, May 31, 2020 at 02:00:45PM +0300, Andy Shevchenko wrote:
>
> ...
>
> > > > If we cast to int, we don't need to worry about the signedness. If
> > > > someone enters a value that can't be cast to int, there will still
> > > > be a compiler warning about shift out of range.
> > >
> > > If the argument unsigned long long will it be the warning (it should not)?
> >
> > No, there should be no warning there.
> >
> > The inputs to GENMASK() needs to be between 0 and 31 (or 63 depending on the
> > size of unsigned long). For any other values, there will be undefined behaviour,
> > since the operands to the shifts in __GENMASK will be too large (or negative).
>
> What I'm implying here that argument may be not constant, and compiler
> can't know their values at hand.
> So, in the following snippet
>
> foo(unsigned long long x)
> {
> u32 y;
> y = GENMASK(x, 0);
> }
>
> when you cast x to int wouldn't be a warning of possible value
> reduction (even if we know that it won't be higher than 63/31)?

Got it, no I was unable to trigger any warnings like that (but I still
can't reproduce to original warning, so take that with a grain of salt).
I'd be very surprised if compilers warned for explicit casts but I'll
send a proper patch soon to let the build robot try it.

Rikard
>
> --
> With Best Regards,
> Andy Shevchenko