public inbox for linux-gpio@vger.kernel.org
 help / color / mirror / Atom feed
From: Suraj Sonawane <surajsonawane0215@gmail.com>
To: Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Dan Carpenter <dan.carpenter@linaro.org>
Cc: Linus Walleij <linus.walleij@linaro.org>,
	Bartosz Golaszewski <brgl@bgdev.pl>,
	linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] gpio: gpio-exar: replace division condition with direct comparison
Date: Fri, 15 Nov 2024 18:55:41 +0530	[thread overview]
Message-ID: <090f78b1-88c9-44e5-959f-07b4f97cd1bc@gmail.com> (raw)
In-Reply-To: <ZzcWGJxqMJVYd4Tp@black.fi.intel.com>

On 15/11/24 15:06, Andy Shevchenko wrote:
> + Dan
> 
> I have to comment on this change as it's a bit controversial.
> 
> TL;DR: this patch is not more than a (harmless?) noise.
> 
> On Wed, Nov 13, 2024 at 01:46:59AM +0530, Suraj Sonawane wrote:
>> Fix an issue detected by the Smatch tool:
>>
>> 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'
> 
> This message does not really explain why.
> 
>> The division 'pin / 8' was used to check if the pin number is 8 or greater,
>> which can be confusing and less readable.
> 
> It's inaccurate description. Everyone who is familiar with GPIO HW is
> also familiar with line grouping in banks. Here is the clear statement
> "get the bank number (where 8 lines per bank), and if it's 0 do this,
> else do that". It might be in the future that (new version of) HW will
> gain more banks and we would return to "division".
> 
>> Replacing it with 'pin >= 8' makes the code clearer by directly
>> comparing the pin number.
> 
> I don't think this statement is fully true. See above.
> 
>> This also removes reliance on integer division,
> 
> On top of that "division" here uses power-of-two divisor, which any
> optimizing (and this code I think won't ever be built without
> optimization turned on) compiler (I think from the very beginning of
> the Linux kernel project) knows how to convert to right shifts on
> the platforms that support that (and how many do not nowadays? 0?).
> 
> Additionally in the cases when we have a / 8; a % 8 type of expressions
> coupled together, the compiler actually may issue an integer division
> assembly instructions on some ISAs where it gives two values in one
> go. Replacing like the above might break that (if the compiler is old
> or not clever enough).
> 
>> which can be harder to understand
> 
> No, "division" by power-of-two numbers is very well understandble.
> 
>> and may introduce subtle bugs in the future.
> 
> What bugs?
> 
> The bottom line is that: I recommend to work with smatch developers
> to amend smatch instead.
> 
> P.S. I wouldn't like to see similar patches to other GPIO drivers,
> especially those that use a / 8; a % 8 type of expressions together.
> 

I understand your points about the familiarity of line grouping in GPIO 
hardware and the optimization behavior of compilers for power-of-two 
division. I initially thought this could be a good fix as I have seen 
similar changes before. Thank you for the feedback—I will keep this in 
mind before submitting such patches in the future.

Best regards,
Suraj Sonawane

  parent reply	other threads:[~2024-11-15 13:25 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-12 20:16 [PATCH] gpio: gpio-exar: replace division condition with direct comparison Suraj Sonawane
2024-11-13 15:31 ` Bartosz Golaszewski
2024-11-14  8:55   ` Suraj Sonawane
2024-11-15  9:36 ` Andy Shevchenko
2024-11-15 11:55   ` Dan Carpenter
2024-11-15 11:58     ` Bartosz Golaszewski
2024-11-15 13:25   ` Suraj Sonawane [this message]
2024-11-18  9:23     ` Andy Shevchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=090f78b1-88c9-44e5-959f-07b4f97cd1bc@gmail.com \
    --to=surajsonawane0215@gmail.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=brgl@bgdev.pl \
    --cc=dan.carpenter@linaro.org \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox