Re: [PATCH] regulator: aat2870-regulator: replace division condition with direct comparison

From: Suraj Sonawane
Date: Thu Nov 14 2024 - 03:54:21 EST


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