RE: [RFT PATCH 5/7] gpio: exar: unduplicate address and offset computation

From: David Laight
Date: Mon Nov 02 2020 - 06:28:05 EST


From: Andy Shevchenko
> Sent: 02 November 2020 10:59
>
> On Mon, Oct 26, 2020 at 4:23 PM Bartosz Golaszewski <brgl@xxxxxxxx> wrote:
>
> ...
>
> > +static unsigned int
> > +exar_offset_to_sel_addr(struct exar_gpio_chip *exar_gpio, unsigned int offset)
> > +{
> > + return (offset + exar_gpio->first_pin) / 8 ? EXAR_OFFSET_MPIOSEL_HI
> > + : EXAR_OFFSET_MPIOSEL_LO;
> > +}
> > +
> > +static unsigned int
> > +exar_offset_to_lvl_addr(struct exar_gpio_chip *exar_gpio, unsigned int offset)
> > +{
> > + return (offset + exar_gpio->first_pin) / 8 ? EXAR_OFFSET_MPIOLVL_HI
> > + : EXAR_OFFSET_MPIOLVL_LO;
> > +}
> > +
> > +static unsigned int
> > +exar_offset_to_bit(struct exar_gpio_chip *exar_gpio, unsigned int offset)
> > +{
> > + return (offset + exar_gpio->first_pin) % 8;
> > +}
>
> Answering to your question...
>
> It can be done line this:
>
> static unsigned int exar_offset_to_bank_and_bit(..., *bit)
> {
> *bit = (offset + exar_gpio->first_pin) % 8;
> return (offset + exar_gpio->first_pin) / 8;
> }

That is likely to require the compiler reload exar_gpio->first_pin
after the write to *bit.

> static unsigned int exar_offset_to_lvl_addr_and_bit(, *bit)
> {
> return exar_offset_to_bank_and_bit(..., bit) ?
> EXAR_OFFSET_MPIOLVL_HI : EXAR_OFFSET_MPIOLVL_LO;
> }

Gah why is it using divide then ?: ?
AFAICT (from the above) there are at most 16 pins.

Much better would be using:
tmp = offset + exar_gpio->first_pin;
*bit = tmp & 7;
return tmp & 8;

Inlined the compiler may well compute:
exar_offset_to_bank_and_bit() ? HI : LO;
as:
LO + (HI - LO) * exar_offset_to_bank_and_bit().
The latter term is likely to be just (tmp & 8) >> n.

I also bet the code actually wants (1 << bit).

David

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