Re: drivers/gpio/gpio-exar.c:52 exar_offset_to_sel_addr() warn: replace divide condition 'pin / 8' with 'pin >= 8'

From: Andy Shevchenko
Date: Tue Dec 27 2022 - 13:31:23 EST


On Tue, Dec 27, 2022 at 7:46 PM Dan Carpenter <error27@xxxxxxxxx> wrote:
> On Sun, Dec 25, 2022 at 12:50:46PM +0100, Andy Shevchenko wrote:
> > > Andy Shevchenko <andy.shevchenko@xxxxxxxxx> kirjoitti 25.12.2022 kello 12.45:
> > >>> Dan Carpenter <error27@xxxxxxxxx> kirjoitti 24.12.2022 kello 20.30:
> > >>> On Sat, Dec 24, 2022 at 05:19:27PM +0100, Andy Shevchenko wrote:
> > >>>> Dan Carpenter <error27@xxxxxxxxx> kirjoitti 23.12.2022 kello 11.54:

...

> > >>>> smatch warnings:
> > >>>> drivers/gpio/gpio-exar.c:52 exar_offset_to_sel_addr() warn: replace divide condition 'pin / 8' with 'pin >= 8'
> > >>>> drivers/gpio/gpio-exar.c:62 exar_offset_to_lvl_addr() warn: replace divide condition 'pin / 8' with 'pin >= 8'
> > >>>
> > >>> I don’t think this is a good advice. If we want to limit that, we need
> > >>> to check also upper limit. But. The GPIO framework does that. So,
> > >>> changing / to >= is bogus.
> > >>
> > >> How is checking pin / 8 not mathematically equivalent to pin >= 8?
> > >
> > > The point is that semantically the / is better in case this code will ever support more than two banks of pins.
> >
> > On top of that it’s paired with pin % 8.
>
> I noticed that, but it's a common bug though that a lot of people
> accidentally write if (pin / 8) when if ((pin % 8) == 0) is intended.

Probably. Here the pin/8 is the correct approach, it shows the bank
number, where each bank is out of 8 pins.

> For example:

Thanks, but it's unrelated to this case.

--
With Best Regards,
Andy Shevchenko