linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* drivers/gpio/gpio-exar.c:52 exar_offset_to_sel_addr() warn: replace divide condition 'pin / 8' with 'pin >= 8'
@ 2022-12-23 10:54 Dan Carpenter
  2022-12-24 16:19 ` Andy Shevchenko
  0 siblings, 1 reply; 8+ messages in thread
From: Dan Carpenter @ 2022-12-23 10:54 UTC (permalink / raw)
  To: oe-kbuild, Qingtao Cao
  Cc: lkp, oe-kbuild-all, linux-kernel, Bartosz Golaszewski, Andy Shevchenko

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
head:   f9ff5644bcc04221bae56f922122f2b7f5d24d62
commit: 5134272f9f3f71d4e1f3aa15cb09321af49b3646 gpio: exar: access MPIO registers on cascaded chips
config: ia64-randconfig-m031-20221218
compiler: ia64-linux-gcc (GCC) 12.1.0

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>
| Reported-by: Dan Carpenter <error27@gmail.com>

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'

vim +52 drivers/gpio/gpio-exar.c

696868d0a79c21 Bartosz Golaszewski 2020-09-30  47  static unsigned int
696868d0a79c21 Bartosz Golaszewski 2020-09-30  48  exar_offset_to_sel_addr(struct exar_gpio_chip *exar_gpio, unsigned int offset)
696868d0a79c21 Bartosz Golaszewski 2020-09-30  49  {
5134272f9f3f71 Qingtao Cao         2022-09-02  50  	unsigned int pin = exar_gpio->first_pin + (offset % 16);
5134272f9f3f71 Qingtao Cao         2022-09-02  51  	unsigned int cascaded = offset / 16;
5134272f9f3f71 Qingtao Cao         2022-09-02 @52  	unsigned int addr = pin / 8 ? EXAR_OFFSET_MPIOSEL_HI : EXAR_OFFSET_MPIOSEL_LO;

Why not change this to pin > 8 instead.  Comparisons are faster than
divide ops.  Way more readable too.

5134272f9f3f71 Qingtao Cao         2022-09-02  53  
5134272f9f3f71 Qingtao Cao         2022-09-02  54  	return addr + (cascaded ? exar_gpio->cascaded_offset : 0);
696868d0a79c21 Bartosz Golaszewski 2020-09-30  55  }
696868d0a79c21 Bartosz Golaszewski 2020-09-30  56  
696868d0a79c21 Bartosz Golaszewski 2020-09-30  57  static unsigned int
696868d0a79c21 Bartosz Golaszewski 2020-09-30  58  exar_offset_to_lvl_addr(struct exar_gpio_chip *exar_gpio, unsigned int offset)
696868d0a79c21 Bartosz Golaszewski 2020-09-30  59  {
5134272f9f3f71 Qingtao Cao         2022-09-02  60  	unsigned int pin = exar_gpio->first_pin + (offset % 16);
5134272f9f3f71 Qingtao Cao         2022-09-02  61  	unsigned int cascaded = offset / 16;
5134272f9f3f71 Qingtao Cao         2022-09-02 @62  	unsigned int addr = pin / 8 ? EXAR_OFFSET_MPIOLVL_HI : EXAR_OFFSET_MPIOLVL_LO;
5134272f9f3f71 Qingtao Cao         2022-09-02  63  
5134272f9f3f71 Qingtao Cao         2022-09-02  64  	return addr + (cascaded ? exar_gpio->cascaded_offset : 0);
696868d0a79c21 Bartosz Golaszewski 2020-09-30  65  }

-- 
0-DAY CI Kernel Test Service
https://01.org/lkp


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: drivers/gpio/gpio-exar.c:52 exar_offset_to_sel_addr() warn: replace divide condition 'pin / 8' with 'pin >= 8'
  2022-12-23 10:54 drivers/gpio/gpio-exar.c:52 exar_offset_to_sel_addr() warn: replace divide condition 'pin / 8' with 'pin >= 8' Dan Carpenter
@ 2022-12-24 16:19 ` Andy Shevchenko
  2022-12-24 19:30   ` Dan Carpenter
  0 siblings, 1 reply; 8+ messages in thread
From: Andy Shevchenko @ 2022-12-24 16:19 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: oe-kbuild, Qingtao Cao, lkp, oe-kbuild-all, linux-kernel,
	Bartosz Golaszewski



Lähetetty iPhonesta

> Dan Carpenter <error27@gmail.com> kirjoitti 23.12.2022 kello 11.54:
> 
> tree:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
> head:   f9ff5644bcc04221bae56f922122f2b7f5d24d62
> commit: 5134272f9f3f71d4e1f3aa15cb09321af49b3646 gpio: exar: access MPIO registers on cascaded chips
> config: ia64-randconfig-m031-20221218
> compiler: ia64-linux-gcc (GCC) 12.1.0
> 
> If you fix the issue, kindly add following tag where applicable
> | Reported-by: kernel test robot <lkp@intel.com>
> | Reported-by: Dan Carpenter <error27@gmail.com>
> 
> 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.


> vim +52 drivers/gpio/gpio-exar.c
> 
> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  47  static unsigned int
> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  48  exar_offset_to_sel_addr(struct exar_gpio_chip *exar_gpio, unsigned int offset)
> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  49  {
> 5134272f9f3f71 Qingtao Cao         2022-09-02  50      unsigned int pin = exar_gpio->first_pin + (offset % 16);
> 5134272f9f3f71 Qingtao Cao         2022-09-02  51      unsigned int cascaded = offset / 16;
> 5134272f9f3f71 Qingtao Cao         2022-09-02 @52      unsigned int addr = pin / 8 ? EXAR_OFFSET_MPIOSEL_HI : EXAR_OFFSET_MPIOSEL_LO;
> 
> Why not change this to pin > 8 instead.  Comparisons are faster than
> divide ops.  Way more readable too.
> 
> 5134272f9f3f71 Qingtao Cao         2022-09-02  53  
> 5134272f9f3f71 Qingtao Cao         2022-09-02  54      return addr + (cascaded ? exar_gpio->cascaded_offset : 0);
> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  55  }
> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  56  
> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  57  static unsigned int
> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  58  exar_offset_to_lvl_addr(struct exar_gpio_chip *exar_gpio, unsigned int offset)
> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  59  {
> 5134272f9f3f71 Qingtao Cao         2022-09-02  60      unsigned int pin = exar_gpio->first_pin + (offset % 16);
> 5134272f9f3f71 Qingtao Cao         2022-09-02  61      unsigned int cascaded = offset / 16;
> 5134272f9f3f71 Qingtao Cao         2022-09-02 @62      unsigned int addr = pin / 8 ? EXAR_OFFSET_MPIOLVL_HI : EXAR_OFFSET_MPIOLVL_LO;
> 5134272f9f3f71 Qingtao Cao         2022-09-02  63  
> 5134272f9f3f71 Qingtao Cao         2022-09-02  64      return addr + (cascaded ? exar_gpio->cascaded_offset : 0);
> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  65  }
> 
> -- 
> 0-DAY CI Kernel Test Service
> https://01.org/lkp
> 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: drivers/gpio/gpio-exar.c:52 exar_offset_to_sel_addr() warn: replace divide condition 'pin / 8' with 'pin >= 8'
  2022-12-24 16:19 ` Andy Shevchenko
@ 2022-12-24 19:30   ` Dan Carpenter
  2022-12-24 19:36     ` Dan Carpenter
  2022-12-25 11:45     ` Andy Shevchenko
  0 siblings, 2 replies; 8+ messages in thread
From: Dan Carpenter @ 2022-12-24 19:30 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: oe-kbuild, Qingtao Cao, lkp, oe-kbuild-all, linux-kernel,
	Bartosz Golaszewski

On Sat, Dec 24, 2022 at 05:19:27PM +0100, Andy Shevchenko wrote:
> 
> 
> Lähetetty iPhonesta
> 
> > Dan Carpenter <error27@gmail.com> kirjoitti 23.12.2022 kello 11.54:
> > 
> > tree:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
> > head:   f9ff5644bcc04221bae56f922122f2b7f5d24d62
> > commit: 5134272f9f3f71d4e1f3aa15cb09321af49b3646 gpio: exar: access MPIO registers on cascaded chips
> > config: ia64-randconfig-m031-20221218
> > compiler: ia64-linux-gcc (GCC) 12.1.0
> > 
> > If you fix the issue, kindly add following tag where applicable
> > | Reported-by: kernel test robot <lkp@intel.com>
> > | Reported-by: Dan Carpenter <error27@gmail.com>
> > 
> > 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?

I don't understand this code at all.  The divide is inscrutable  Is it
storing something in in the lower 3 bits and something in bit 4?  In
that case it might be nicer to just check (pin & BIT(4)).

regards,
dan carpenter

> 
> 
> > vim +52 drivers/gpio/gpio-exar.c
> > 
> > 696868d0a79c21 Bartosz Golaszewski 2020-09-30  47  static unsigned int
> > 696868d0a79c21 Bartosz Golaszewski 2020-09-30  48  exar_offset_to_sel_addr(struct exar_gpio_chip *exar_gpio, unsigned int offset)
> > 696868d0a79c21 Bartosz Golaszewski 2020-09-30  49  {
> > 5134272f9f3f71 Qingtao Cao         2022-09-02  50      unsigned int pin = exar_gpio->first_pin + (offset % 16);
> > 5134272f9f3f71 Qingtao Cao         2022-09-02  51      unsigned int cascaded = offset / 16;
> > 5134272f9f3f71 Qingtao Cao         2022-09-02 @52      unsigned int addr = pin / 8 ? EXAR_OFFSET_MPIOSEL_HI : EXAR_OFFSET_MPIOSEL_LO;
> > 
> > Why not change this to pin > 8 instead.  Comparisons are faster than
> > divide ops.  Way more readable too.
> > 
> > 5134272f9f3f71 Qingtao Cao         2022-09-02  53  
> > 5134272f9f3f71 Qingtao Cao         2022-09-02  54      return addr + (cascaded ? exar_gpio->cascaded_offset : 0);
> > 696868d0a79c21 Bartosz Golaszewski 2020-09-30  55  }
> > 696868d0a79c21 Bartosz Golaszewski 2020-09-30  56  
> > 696868d0a79c21 Bartosz Golaszewski 2020-09-30  57  static unsigned int
> > 696868d0a79c21 Bartosz Golaszewski 2020-09-30  58  exar_offset_to_lvl_addr(struct exar_gpio_chip *exar_gpio, unsigned int offset)
> > 696868d0a79c21 Bartosz Golaszewski 2020-09-30  59  {
> > 5134272f9f3f71 Qingtao Cao         2022-09-02  60      unsigned int pin = exar_gpio->first_pin + (offset % 16);
> > 5134272f9f3f71 Qingtao Cao         2022-09-02  61      unsigned int cascaded = offset / 16;
> > 5134272f9f3f71 Qingtao Cao         2022-09-02 @62      unsigned int addr = pin / 8 ? EXAR_OFFSET_MPIOLVL_HI : EXAR_OFFSET_MPIOLVL_LO;
> > 5134272f9f3f71 Qingtao Cao         2022-09-02  63  
> > 5134272f9f3f71 Qingtao Cao         2022-09-02  64      return addr + (cascaded ? exar_gpio->cascaded_offset : 0);
> > 696868d0a79c21 Bartosz Golaszewski 2020-09-30  65  }
> > 
> > -- 
> > 0-DAY CI Kernel Test Service
> > https://01.org/lkp
> > 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: drivers/gpio/gpio-exar.c:52 exar_offset_to_sel_addr() warn: replace divide condition 'pin / 8' with 'pin >= 8'
  2022-12-24 19:30   ` Dan Carpenter
@ 2022-12-24 19:36     ` Dan Carpenter
  2022-12-25 11:45     ` Andy Shevchenko
  1 sibling, 0 replies; 8+ messages in thread
From: Dan Carpenter @ 2022-12-24 19:36 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: oe-kbuild, Qingtao Cao, lkp, oe-kbuild-all, linux-kernel,
	Bartosz Golaszewski

On Sat, Dec 24, 2022 at 10:30:39PM +0300, Dan Carpenter wrote:
> On Sat, Dec 24, 2022 at 05:19:27PM +0100, Andy Shevchenko wrote:
> > 
> > 
> > Lähetetty iPhonesta
> > 
> > > Dan Carpenter <error27@gmail.com> kirjoitti 23.12.2022 kello 11.54:
> > > 
> > > tree:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
> > > head:   f9ff5644bcc04221bae56f922122f2b7f5d24d62
> > > commit: 5134272f9f3f71d4e1f3aa15cb09321af49b3646 gpio: exar: access MPIO registers on cascaded chips
> > > config: ia64-randconfig-m031-20221218
> > > compiler: ia64-linux-gcc (GCC) 12.1.0
> > > 
> > > If you fix the issue, kindly add following tag where applicable
> > > | Reported-by: kernel test robot <lkp@intel.com>
> > > | Reported-by: Dan Carpenter <error27@gmail.com>
> > > 
> > > 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?
> 
> I don't understand this code at all.  The divide is inscrutable  Is it
> storing something in in the lower 3 bits and something in bit 4?  In
> that case it might be nicer to just check (pin & BIT(4)).
> 

Or a macro which does:

#define GET_UPPER_BIT_THING(pin) ((pin >> 8) & 0xMASK)

regards,
dan carpenter


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: drivers/gpio/gpio-exar.c:52 exar_offset_to_sel_addr() warn: replace divide condition 'pin / 8' with 'pin >= 8'
  2022-12-24 19:30   ` Dan Carpenter
  2022-12-24 19:36     ` Dan Carpenter
@ 2022-12-25 11:45     ` Andy Shevchenko
  2022-12-25 11:50       ` Andy Shevchenko
  1 sibling, 1 reply; 8+ messages in thread
From: Andy Shevchenko @ 2022-12-25 11:45 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: oe-kbuild, Qingtao Cao, lkp, oe-kbuild-all, linux-kernel,
	Bartosz Golaszewski



Lähetetty iPhonesta

> Dan Carpenter <error27@gmail.com> kirjoitti 24.12.2022 kello 20.30:
> 
>> On Sat, Dec 24, 2022 at 05:19:27PM +0100, Andy Shevchenko wrote:
>> 
>> 
>> Lähetetty iPhonesta
>> 
>>> Dan Carpenter <error27@gmail.com> kirjoitti 23.12.2022 kello 11.54:
>>> 
>>> tree:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
>>> head:   f9ff5644bcc04221bae56f922122f2b7f5d24d62
>>> commit: 5134272f9f3f71d4e1f3aa15cb09321af49b3646 gpio: exar: access MPIO registers on cascaded chips
>>> config: ia64-randconfig-m031-20221218
>>> compiler: ia64-linux-gcc (GCC) 12.1.0
>>> 
>>> If you fix the issue, kindly add following tag where applicable
>>> | Reported-by: kernel test robot <lkp@intel.com>
>>> | Reported-by: Dan Carpenter <error27@gmail.com>
>>> 
>>> 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.


> I don't understand this code at all.  The divide is inscrutable  Is it
> storing something in in the lower 3 bits and something in bit 4?  In
> that case it might be nicer to just check (pin & BIT(4)).
> 
> regards,
> dan carpenter
> 
>> 
>> 
>>> vim +52 drivers/gpio/gpio-exar.c
>>> 
>>> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  47  static unsigned int
>>> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  48  exar_offset_to_sel_addr(struct exar_gpio_chip *exar_gpio, unsigned int offset)
>>> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  49  {
>>> 5134272f9f3f71 Qingtao Cao         2022-09-02  50      unsigned int pin = exar_gpio->first_pin + (offset % 16);
>>> 5134272f9f3f71 Qingtao Cao         2022-09-02  51      unsigned int cascaded = offset / 16;
>>> 5134272f9f3f71 Qingtao Cao         2022-09-02 @52      unsigned int addr = pin / 8 ? EXAR_OFFSET_MPIOSEL_HI : EXAR_OFFSET_MPIOSEL_LO;
>>> 
>>> Why not change this to pin > 8 instead.  Comparisons are faster than
>>> divide ops.  Way more readable too.
>>> 
>>> 5134272f9f3f71 Qingtao Cao         2022-09-02  53  
>>> 5134272f9f3f71 Qingtao Cao         2022-09-02  54      return addr + (cascaded ? exar_gpio->cascaded_offset : 0);
>>> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  55  }
>>> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  56  
>>> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  57  static unsigned int
>>> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  58  exar_offset_to_lvl_addr(struct exar_gpio_chip *exar_gpio, unsigned int offset)
>>> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  59  {
>>> 5134272f9f3f71 Qingtao Cao         2022-09-02  60      unsigned int pin = exar_gpio->first_pin + (offset % 16);
>>> 5134272f9f3f71 Qingtao Cao         2022-09-02  61      unsigned int cascaded = offset / 16;
>>> 5134272f9f3f71 Qingtao Cao         2022-09-02 @62      unsigned int addr = pin / 8 ? EXAR_OFFSET_MPIOLVL_HI : EXAR_OFFSET_MPIOLVL_LO;
>>> 5134272f9f3f71 Qingtao Cao         2022-09-02  63  
>>> 5134272f9f3f71 Qingtao Cao         2022-09-02  64      return addr + (cascaded ? exar_gpio->cascaded_offset : 0);
>>> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  65  }
>>> 
>>> -- 
>>> 0-DAY CI Kernel Test Service
>>> https://01.org/lkp
>>> 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: drivers/gpio/gpio-exar.c:52 exar_offset_to_sel_addr() warn: replace divide condition 'pin / 8' with 'pin >= 8'
  2022-12-25 11:45     ` Andy Shevchenko
@ 2022-12-25 11:50       ` Andy Shevchenko
  2022-12-27 17:46         ` Dan Carpenter
  0 siblings, 1 reply; 8+ messages in thread
From: Andy Shevchenko @ 2022-12-25 11:50 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: oe-kbuild, Qingtao Cao, lkp, oe-kbuild-all, linux-kernel,
	Bartosz Golaszewski



Lähetetty iPhonesta

> Andy Shevchenko <andy.shevchenko@gmail.com> kirjoitti 25.12.2022 kello 12.45:
> 
> 
> 
> Lähetetty iPhonesta
> 
>>> Dan Carpenter <error27@gmail.com> kirjoitti 24.12.2022 kello 20.30:
>>> 
>>> On Sat, Dec 24, 2022 at 05:19:27PM +0100, Andy Shevchenko wrote:
>>> 
>>> 
>>> Lähetetty iPhonesta
>>> 
>>>> Dan Carpenter <error27@gmail.com> kirjoitti 23.12.2022 kello 11.54:
>>>> 
>>>> tree:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
>>>> head:   f9ff5644bcc04221bae56f922122f2b7f5d24d62
>>>> commit: 5134272f9f3f71d4e1f3aa15cb09321af49b3646 gpio: exar: access MPIO registers on cascaded chips
>>>> config: ia64-randconfig-m031-20221218
>>>> compiler: ia64-linux-gcc (GCC) 12.1.0
>>>> 
>>>> If you fix the issue, kindly add following tag where applicable
>>>> | Reported-by: kernel test robot <lkp@intel.com>
>>>> | Reported-by: Dan Carpenter <error27@gmail.com>
>>>> 
>>>> 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 don't understand this code at all.  The divide is inscrutable  Is it
>> storing something in in the lower 3 bits and something in bit 4?  In
>> that case it might be nicer to just check (pin & BIT(4)).
>> 
>> regards,
>> dan carpenter
>> 
>>> 
>>> 
>>>> vim +52 drivers/gpio/gpio-exar.c
>>>> 
>>>> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  47  static unsigned int
>>>> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  48  exar_offset_to_sel_addr(struct exar_gpio_chip *exar_gpio, unsigned int offset)
>>>> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  49  {
>>>> 5134272f9f3f71 Qingtao Cao         2022-09-02  50      unsigned int pin = exar_gpio->first_pin + (offset % 16);
>>>> 5134272f9f3f71 Qingtao Cao         2022-09-02  51      unsigned int cascaded = offset / 16;
>>>> 5134272f9f3f71 Qingtao Cao         2022-09-02 @52      unsigned int addr = pin / 8 ? EXAR_OFFSET_MPIOSEL_HI : EXAR_OFFSET_MPIOSEL_LO;
>>>> 
>>>> Why not change this to pin > 8 instead.  Comparisons are faster than
>>>> divide ops.  Way more readable too.
>>>> 
>>>> 5134272f9f3f71 Qingtao Cao         2022-09-02  53  
>>>> 5134272f9f3f71 Qingtao Cao         2022-09-02  54      return addr + (cascaded ? exar_gpio->cascaded_offset : 0);
>>>> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  55  }
>>>> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  56  
>>>> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  57  static unsigned int
>>>> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  58  exar_offset_to_lvl_addr(struct exar_gpio_chip *exar_gpio, unsigned int offset)
>>>> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  59  {
>>>> 5134272f9f3f71 Qingtao Cao         2022-09-02  60      unsigned int pin = exar_gpio->first_pin + (offset % 16);
>>>> 5134272f9f3f71 Qingtao Cao         2022-09-02  61      unsigned int cascaded = offset / 16;
>>>> 5134272f9f3f71 Qingtao Cao         2022-09-02 @62      unsigned int addr = pin / 8 ? EXAR_OFFSET_MPIOLVL_HI : EXAR_OFFSET_MPIOLVL_LO;
>>>> 5134272f9f3f71 Qingtao Cao         2022-09-02  63  
>>>> 5134272f9f3f71 Qingtao Cao         2022-09-02  64      return addr + (cascaded ? exar_gpio->cascaded_offset : 0);
>>>> 696868d0a79c21 Bartosz Golaszewski 2020-09-30  65  }
>>>> 
>>>> -- 
>>>> 0-DAY CI Kernel Test Service
>>>> https://01.org/lkp
>>>> 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: drivers/gpio/gpio-exar.c:52 exar_offset_to_sel_addr() warn: replace divide condition 'pin / 8' with 'pin >= 8'
  2022-12-25 11:50       ` Andy Shevchenko
@ 2022-12-27 17:46         ` Dan Carpenter
  2022-12-27 18:30           ` Andy Shevchenko
  0 siblings, 1 reply; 8+ messages in thread
From: Dan Carpenter @ 2022-12-27 17:46 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: oe-kbuild, Qingtao Cao, lkp, oe-kbuild-all, linux-kernel,
	Bartosz Golaszewski

On Sun, Dec 25, 2022 at 12:50:46PM +0100, Andy Shevchenko wrote:
> 
> 
> Lähetetty iPhonesta
> 
> > Andy Shevchenko <andy.shevchenko@gmail.com> kirjoitti 25.12.2022 kello 12.45:
> > 
> > 
> > 
> > Lähetetty iPhonesta
> > 
> >>> Dan Carpenter <error27@gmail.com> kirjoitti 24.12.2022 kello 20.30:
> >>> 
> >>> On Sat, Dec 24, 2022 at 05:19:27PM +0100, Andy Shevchenko wrote:
> >>> 
> >>> 
> >>> Lähetetty iPhonesta
> >>> 
> >>>> Dan Carpenter <error27@gmail.com> kirjoitti 23.12.2022 kello 11.54:
> >>>> 
> >>>> tree:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
> >>>> head:   f9ff5644bcc04221bae56f922122f2b7f5d24d62
> >>>> commit: 5134272f9f3f71d4e1f3aa15cb09321af49b3646 gpio: exar: access MPIO registers on cascaded chips
> >>>> config: ia64-randconfig-m031-20221218
> >>>> compiler: ia64-linux-gcc (GCC) 12.1.0
> >>>> 
> >>>> If you fix the issue, kindly add following tag where applicable
> >>>> | Reported-by: kernel test robot <lkp@intel.com>
> >>>> | Reported-by: Dan Carpenter <error27@gmail.com>
> >>>> 
> >>>> 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.

For example:

drivers/rtc/rtc-m48t59.c
   132          M48T59_WRITE((bin2bcd(tm->tm_mon + 1) & 0x1F), M48T59_MONTH);
   133          M48T59_WRITE(bin2bcd(year % 100), M48T59_YEAR);
   134  
   135          if (pdata->type == M48T59RTC_TYPE_M48T59 && (year / 100))
                                                             ^^^^^^^^^^
This code is pretty clearly an example of where people accidentally uses
/ to mean "divides cleanly".  (I have not patched or reported this code,
btw so if anyone wants an easy patch to send it's available).

   136                  val = (M48T59_WDAY_CEB | M48T59_WDAY_CB);
   137          val |= (bin2bcd(tm->tm_wday) & 0x07);
   138          M48T59_WRITE(val, M48T59_WDAY);

regards,
dan carpenter


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: drivers/gpio/gpio-exar.c:52 exar_offset_to_sel_addr() warn: replace divide condition 'pin / 8' with 'pin >= 8'
  2022-12-27 17:46         ` Dan Carpenter
@ 2022-12-27 18:30           ` Andy Shevchenko
  0 siblings, 0 replies; 8+ messages in thread
From: Andy Shevchenko @ 2022-12-27 18:30 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: oe-kbuild, Qingtao Cao, lkp, oe-kbuild-all, linux-kernel,
	Bartosz Golaszewski

On Tue, Dec 27, 2022 at 7:46 PM Dan Carpenter <error27@gmail.com> wrote:
> On Sun, Dec 25, 2022 at 12:50:46PM +0100, Andy Shevchenko wrote:
> > > Andy Shevchenko <andy.shevchenko@gmail.com> kirjoitti 25.12.2022 kello 12.45:
> > >>> Dan Carpenter <error27@gmail.com> kirjoitti 24.12.2022 kello 20.30:
> > >>> On Sat, Dec 24, 2022 at 05:19:27PM +0100, Andy Shevchenko wrote:
> > >>>> Dan Carpenter <error27@gmail.com> 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

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-12-27 18:31 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-23 10:54 drivers/gpio/gpio-exar.c:52 exar_offset_to_sel_addr() warn: replace divide condition 'pin / 8' with 'pin >= 8' Dan Carpenter
2022-12-24 16:19 ` Andy Shevchenko
2022-12-24 19:30   ` Dan Carpenter
2022-12-24 19:36     ` Dan Carpenter
2022-12-25 11:45     ` Andy Shevchenko
2022-12-25 11:50       ` Andy Shevchenko
2022-12-27 17:46         ` Dan Carpenter
2022-12-27 18:30           ` Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).