* [PATCH] regulator: aat2870-regulator: replace division condition with direct comparison
@ 2024-11-12 20:20 Suraj Sonawane
2024-11-13 13:39 ` Mark Brown
0 siblings, 1 reply; 3+ messages in thread
From: Suraj Sonawane @ 2024-11-12 20:20 UTC (permalink / raw)
To: Liam Girdwood; +Cc: Mark Brown, linux-kernel, Suraj Sonawane
Fix an issue detected by the Smatch tool:
drivers/regulator/aat2870-regulator.c:142 aat2870_get_regulator() warn:
replace divide condition '(id - 1) / 2' with '(id - 1) >= 2'
The division '(id - 1) / 2' was used to check if the regulator ID
is greater than or equal to 2, which can be confusing and less
readable. Replacing it with '(id - 1) >= 2' makes the code clearer
by directly comparing the regulator ID. This change also removes
reliance on integer division, which could be harder to understand
and may introduce subtle bugs.
Signed-off-by: Suraj Sonawane <surajsonawane0215@gmail.com>
---
drivers/regulator/aat2870-regulator.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/regulator/aat2870-regulator.c b/drivers/regulator/aat2870-regulator.c
index 970d86f2b..b48d2987a 100644
--- a/drivers/regulator/aat2870-regulator.c
+++ b/drivers/regulator/aat2870-regulator.c
@@ -139,7 +139,7 @@ static struct aat2870_regulator *aat2870_get_regulator(int id)
ri->enable_shift = id - AAT2870_ID_LDOA;
ri->enable_mask = 0x1 << ri->enable_shift;
- ri->voltage_addr = (id - AAT2870_ID_LDOA) / 2 ?
+ ri->voltage_addr = (id - AAT2870_ID_LDOA) >= 2 ?
AAT2870_LDO_CD : AAT2870_LDO_AB;
ri->voltage_shift = (id - AAT2870_ID_LDOA) % 2 ? 0 : 4;
ri->voltage_mask = 0xF << ri->voltage_shift;
--
2.34.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] regulator: aat2870-regulator: replace division condition with direct comparison
2024-11-12 20:20 [PATCH] regulator: aat2870-regulator: replace division condition with direct comparison Suraj Sonawane
@ 2024-11-13 13:39 ` Mark Brown
2024-11-14 8:54 ` Suraj Sonawane
0 siblings, 1 reply; 3+ messages in thread
From: Mark Brown @ 2024-11-13 13:39 UTC (permalink / raw)
To: Suraj Sonawane; +Cc: Liam Girdwood, linux-kernel
[-- Attachment #1: Type: text/plain, Size: 957 bytes --]
On Wed, Nov 13, 2024 at 01:50:41AM +0530, Suraj Sonawane wrote:
> Fix an issue detected by the Smatch tool:
>
> drivers/regulator/aat2870-regulator.c:142 aat2870_get_regulator() warn:
> replace divide condition '(id - 1) / 2' with '(id - 1) >= 2'
>
> The division '(id - 1) / 2' was used to check if the regulator ID
> is greater than or equal to 2, which can be confusing and less
> readable. Replacing it with '(id - 1) >= 2' makes the code clearer
This is absolute nonsense, the tool should be fixed instead. Writing a
division as a shift when the intent is a division is a microoptimisation
which modern compilers really should figure out where it's relevant.
> - ri->voltage_addr = (id - AAT2870_ID_LDOA) / 2 ?
> + ri->voltage_addr = (id - AAT2870_ID_LDOA) >= 2 ?
> AAT2870_LDO_CD : AAT2870_LDO_AB;
Neither version of this is particularly readable, but the new form here
seems fairly clearly worse rather than better.
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] regulator: aat2870-regulator: replace division condition with direct comparison
2024-11-13 13:39 ` Mark Brown
@ 2024-11-14 8:54 ` Suraj Sonawane
0 siblings, 0 replies; 3+ messages in thread
From: Suraj Sonawane @ 2024-11-14 8:54 UTC (permalink / raw)
To: Mark Brown; +Cc: Liam Girdwood, linux-kernel
On 13/11/24 19:09, Mark Brown wrote:
> On Wed, Nov 13, 2024 at 01:50:41AM +0530, Suraj Sonawane wrote:
>> Fix an issue detected by the Smatch tool:
>>
>> drivers/regulator/aat2870-regulator.c:142 aat2870_get_regulator() warn:
>> replace divide condition '(id - 1) / 2' with '(id - 1) >= 2'
>>
>> The division '(id - 1) / 2' was used to check if the regulator ID
>> is greater than or equal to 2, which can be confusing and less
>> readable. Replacing it with '(id - 1) >= 2' makes the code clearer
>
> This is absolute nonsense, the tool should be fixed instead. Writing a
> division as a shift when the intent is a division is a microoptimisation
> which modern compilers really should figure out where it's relevant.
>
>> - ri->voltage_addr = (id - AAT2870_ID_LDOA) / 2 ?
>> + ri->voltage_addr = (id - AAT2870_ID_LDOA) >= 2 ?
>> AAT2870_LDO_CD : AAT2870_LDO_AB;
>
> Neither version of this is particularly readable, but the new form here
> seems fairly clearly worse rather than better.
Hi Mark,
Thank you for your feedback. I see your point about the original code
being clear enough and changing it as suggested making it worse.
Best,
Suraj
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-11-14 8:54 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-11-12 20:20 [PATCH] regulator: aat2870-regulator: replace division condition with direct comparison Suraj Sonawane
2024-11-13 13:39 ` Mark Brown
2024-11-14 8:54 ` Suraj Sonawane
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®