Skip to contentSkip to conversation

iio: sc27xx_adc: Use DIV_ROUND_CLOSEST() instead of open-coding it

PATCH In review Lars-Peter Clausen 3 participants 3 messages Dec 22, 2020 – Dec 29, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: sc27xx_adc: Use DIV_ROUND_CLOSEST() instead of open-coding it

Use DIV_ROUND_CLOSEST() instead of open-coding it. This makes it more clear what is going on for the casual reviewer.

Generated using the following the Coccinelle semantic patch.

// <smpl>
@@
expression x, y;
@@
-((x) + ((y) / 2)) / (y)
+DIV_ROUND_CLOSEST(x, y)
// </smpl>

---
 drivers/iio/adc/sc27xx_adc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/sc27xx_adc.c +1 -1
index aa32a1f385e2..301cf66de695 100644
@@ -307,7 +307,7 @@ static int sc27xx_adc_convert_volt(struct sc27xx_adc_data *data, int channel,
 
 	sc27xx_adc_volt_ratio(data, channel, scale, &numerator, &denominator);
 
-	return (volt * denominator + numerator / 2) / numerator;
+	return DIV_ROUND_CLOSEST(volt * denominator, numerator);
 }
 
 static int sc27xx_adc_read_processed(struct sc27xx_adc_data *data,
Signature · 1 line
2.20.1
reviewer Acked-by on v1 1/1
#1 lore raw
Quoted 14 lines · Lars-Peter Clausen

Use DIV_ROUND_CLOSEST() instead of open-coding it. This makes it more clear what is going on for the casual reviewer.

Generated using the following the Coccinelle semantic patch.

// <smpl>
@@
expression x, y;
@@

(quoted diff) +1 -1
-((x) + ((y) / 2)) / (y)
+DIV_ROUND_CLOSEST(x, y)

// </smpl>

Quoted 1 line

Thanks,
Chunyan

Quoted 20 lines
---
 drivers/iio/adc/sc27xx_adc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/sc27xx_adc.c +1 -2
index aa32a1f385e2..301cf66de695 100644
@@ -307,7 +307,7 @@ static int sc27xx_adc_convert_volt(struct sc27xx_adc_data *data, int channel,
 
        sc27xx_adc_volt_ratio(data, channel, scale, &numerator, &denominator);
 
-       return (volt * denominator + numerator / 2) / numerator;
+       return DIV_ROUND_CLOSEST(volt * denominator, numerator);
 }
 
 static int sc27xx_adc_read_processed(struct sc27xx_adc_data *data,
--

2.20.1

maintainer
#2 lore raw
Quoted 15 lines · On Wed, 23 Dec 2020 13:39:24 +0800 Chunyan Zhang <[email protected]> wrote:
Quoted 14 lines · Lars-Peter Clausen

Use DIV_ROUND_CLOSEST() instead of open-coding it. This makes it more clear what is going on for the casual reviewer.

Generated using the following the Coccinelle semantic patch.

// <smpl>
@@
expression x, y;
@@

(quoted diff) +1 -1
-((x) + ((y) / 2)) / (y)
+DIV_ROUND_CLOSEST(x, y)

// </smpl>

Quoted 3 lines
Quoted 1 line

Applied to the togreg branch of iio.git and pushed out as testing.

Thanks,

Jonathan

Quoted 24 lines

Thanks,
Chunyan

Quoted 20 lines
---
 drivers/iio/adc/sc27xx_adc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/sc27xx_adc.c +1 -2
index aa32a1f385e2..301cf66de695 100644
@@ -307,7 +307,7 @@ static int sc27xx_adc_convert_volt(struct sc27xx_adc_data *data, int channel,
 
        sc27xx_adc_volt_ratio(data, channel, scale, &numerator, &denominator);
 
-       return (volt * denominator + numerator / 2) / numerator;
+       return DIV_ROUND_CLOSEST(volt * denominator, numerator);
 }
 
 static int sc27xx_adc_read_processed(struct sc27xx_adc_data *data,
--

2.20.1