regulator: aat2870-regulator: replace division condition with direct comparison
PATCH 1/1 regulator: aat2870-regulator: replace division condition with direct comparison
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.
--- drivers/regulator/aat2870-regulator.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/regulator/aat2870-regulator.c +1 -1
@@ -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;
Signature · 1 line
2.34.1
Mark Brown
maintainerQuoted 8 lines · Suraj Sonawane
Fix an issue detected by the Smatch tool:
drivers/regulator/aat2870-regulator.c:142aat2870_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.
drivers/regulator/aat2870-regulator.c static struct aat2870_regulator *aat2870_get_regulator(int id) · line 143
Quoted 3 lines
drivers/regulator/aat2870-regulator.c +1
-1- 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.
Quoted 14 lines · Mark Brown
Quoted 8 lines · Suraj Sonawane
Fix an issue detected by the Smatch tool:
drivers/regulator/aat2870-regulator.c:142aat2870_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.
Quoted 6 lines
drivers/regulator/aat2870-regulator.c static struct aat2870_regulator *aat2870_get_regulator(int id) · line 143
Quoted 3 lines
drivers/regulator/aat2870-regulator.c +1
-1- 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