All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/3] iio: vl6180: Use DIV_ROUND_CLOSEST() instead of open-coding it
@ 2020-12-27 17:11 Lars-Peter Clausen
  2020-12-27 17:11 ` [PATCH 2/3] iio: bme680: " Lars-Peter Clausen
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Lars-Peter Clausen @ 2020-12-27 17:11 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Peter Meerwald-Stadler, Manivannan Sadhasivam, Himanshu Jha,
	linux-iio, Lars-Peter Clausen

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

Generated using the following the Coccinelle semantic patch.

// <smpl>
@r1@
expression x;
constant C1;
constant C2;
@@
 ((x) + C1) / C2

@script:python@
C1 << r1.C1;
C2 << r1.C2;
@@
try:
	if int(C1) * 2 != int(C2):
		cocci.include_match(False)
except:
	cocci.include_match(False)

@@
expression r1.x;
constant r1.C1;
constant r1.C2;
@@
-(((x) + C1) / C2)
+DIV_ROUND_CLOSEST(x, C2)
// </smpl>

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
---
 drivers/iio/light/vl6180.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iio/light/vl6180.c b/drivers/iio/light/vl6180.c
index 4775bd785e50..d47a4f6f4e87 100644
--- a/drivers/iio/light/vl6180.c
+++ b/drivers/iio/light/vl6180.c
@@ -392,7 +392,7 @@ static int vl6180_set_it(struct vl6180_data *data, int val, int val2)
 {
 	int ret, it_ms;
 
-	it_ms = (val2 + 500) / 1000; /* round to ms */
+	it_ms = DIV_ROUND_CLOSEST(val2, 1000); /* round to ms */
 	if (val != 0 || it_ms < 1 || it_ms > 512)
 		return -EINVAL;
 
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/3] iio: bme680: Use DIV_ROUND_CLOSEST() instead of open-coding it
  2020-12-27 17:11 [PATCH 1/3] iio: vl6180: Use DIV_ROUND_CLOSEST() instead of open-coding it Lars-Peter Clausen
@ 2020-12-27 17:11 ` Lars-Peter Clausen
  2020-12-27 17:11 ` [PATCH 3/3] iio: tsl2583: " Lars-Peter Clausen
  2020-12-28  5:58 ` [PATCH 1/3] iio: vl6180: " Manivannan Sadhasivam
  2 siblings, 0 replies; 5+ messages in thread
From: Lars-Peter Clausen @ 2020-12-27 17:11 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Peter Meerwald-Stadler, Manivannan Sadhasivam, Himanshu Jha,
	linux-iio, Lars-Peter Clausen

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

Generated using the following the Coccinelle semantic patch.

// <smpl>
@r1@
expression x;
constant C1;
constant C2;
@@
 ((x) + C1) / C2

@script:python@
C1 << r1.C1;
C2 << r1.C2;
@@
try:
	if int(C1) * 2 != int(C2):
		cocci.include_match(False)
except:
	cocci.include_match(False)

@@
expression r1.x;
constant r1.C1;
constant r1.C2;
@@
-(((x) + C1) / C2)
+DIV_ROUND_CLOSEST(x, C2)
// </smpl>

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
---
 drivers/iio/chemical/bme680_core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iio/chemical/bme680_core.c b/drivers/iio/chemical/bme680_core.c
index 6ea99e4cbf92..bf23cc7eb99e 100644
--- a/drivers/iio/chemical/bme680_core.c
+++ b/drivers/iio/chemical/bme680_core.c
@@ -479,7 +479,7 @@ static u8 bme680_calc_heater_res(struct bme680_data *data, u16 temp)
 	var4 = (var3 / (calib->res_heat_range + 4));
 	var5 = 131 * calib->res_heat_val + 65536;
 	heatr_res_x100 = ((var4 / var5) - 250) * 34;
-	heatr_res = (heatr_res_x100 + 50) / 100;
+	heatr_res = DIV_ROUND_CLOSEST(heatr_res_x100, 100);
 
 	return heatr_res;
 }
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 3/3] iio: tsl2583: Use DIV_ROUND_CLOSEST() instead of open-coding it
  2020-12-27 17:11 [PATCH 1/3] iio: vl6180: Use DIV_ROUND_CLOSEST() instead of open-coding it Lars-Peter Clausen
  2020-12-27 17:11 ` [PATCH 2/3] iio: bme680: " Lars-Peter Clausen
@ 2020-12-27 17:11 ` Lars-Peter Clausen
  2020-12-28  5:58 ` [PATCH 1/3] iio: vl6180: " Manivannan Sadhasivam
  2 siblings, 0 replies; 5+ messages in thread
From: Lars-Peter Clausen @ 2020-12-27 17:11 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Peter Meerwald-Stadler, Manivannan Sadhasivam, Himanshu Jha,
	linux-iio, Lars-Peter Clausen

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

Generated using the following the Coccinelle semantic patch.

// <smpl>
@r1@
expression x;
constant C1;
constant C2;
@@
 ((x) + C1) / C2

@script:python@
C1 << r1.C1;
C2 << r1.C2;
@@
try:
	if int(C1) * 2 != int(C2):
		cocci.include_match(False)
except:
	cocci.include_match(False)

@@
expression r1.x;
constant r1.C1;
constant r1.C2;
@@
-(((x) + C1) / C2)
+DIV_ROUND_CLOSEST(x, C2)
// </smpl>

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
---
 drivers/iio/light/tsl2583.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/iio/light/tsl2583.c b/drivers/iio/light/tsl2583.c
index 9e5490b7473b..0f787bfc88fc 100644
--- a/drivers/iio/light/tsl2583.c
+++ b/drivers/iio/light/tsl2583.c
@@ -285,7 +285,7 @@ static int tsl2583_get_lux(struct iio_dev *indio_dev)
 	lux64 = lux64 * chip->als_settings.als_gain_trim;
 	lux64 >>= 13;
 	lux = lux64;
-	lux = (lux + 500) / 1000;
+	lux = DIV_ROUND_CLOSEST(lux, 1000);
 
 	if (lux > TSL2583_LUX_CALC_OVER_FLOW) { /* check for overflow */
 return_max:
@@ -361,12 +361,12 @@ static int tsl2583_set_als_time(struct tsl2583_chip *chip)
 	u8 val;
 
 	/* determine als integration register */
-	als_count = (chip->als_settings.als_time * 100 + 135) / 270;
+	als_count = DIV_ROUND_CLOSEST(chip->als_settings.als_time * 100, 270);
 	if (!als_count)
 		als_count = 1; /* ensure at least one cycle */
 
 	/* convert back to time (encompasses overrides) */
-	als_time = (als_count * 27 + 5) / 10;
+	als_time = DIV_ROUND_CLOSEST(als_count * 27, 10);
 
 	val = 256 - als_count;
 	ret = i2c_smbus_write_byte_data(chip->client,
@@ -380,7 +380,7 @@ static int tsl2583_set_als_time(struct tsl2583_chip *chip)
 
 	/* set chip struct re scaling and saturation */
 	chip->als_saturation = als_count * 922; /* 90% of full scale */
-	chip->als_time_scale = (als_time + 25) / 50;
+	chip->als_time_scale = DIV_ROUND_CLOSEST(als_time, 50);
 
 	return ret;
 }
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/3] iio: vl6180: Use DIV_ROUND_CLOSEST() instead of open-coding it
  2020-12-27 17:11 [PATCH 1/3] iio: vl6180: Use DIV_ROUND_CLOSEST() instead of open-coding it Lars-Peter Clausen
  2020-12-27 17:11 ` [PATCH 2/3] iio: bme680: " Lars-Peter Clausen
  2020-12-27 17:11 ` [PATCH 3/3] iio: tsl2583: " Lars-Peter Clausen
@ 2020-12-28  5:58 ` Manivannan Sadhasivam
  2020-12-29 13:58   ` Jonathan Cameron
  2 siblings, 1 reply; 5+ messages in thread
From: Manivannan Sadhasivam @ 2020-12-28  5:58 UTC (permalink / raw)
  To: Lars-Peter Clausen
  Cc: Jonathan Cameron, Peter Meerwald-Stadler, Himanshu Jha, linux-iio

On Sun, Dec 27, 2020 at 06:11:24PM +0100, Lars-Peter Clausen wrote:
> Use DIV_ROUND_CLOSEST() instead of open-coding it. This documents intent
> and makes it more clear what is going on for the casual reviewer.
> 
> Generated using the following the Coccinelle semantic patch.
> 
> // <smpl>
> @r1@
> expression x;
> constant C1;
> constant C2;
> @@
>  ((x) + C1) / C2
> 
> @script:python@
> C1 << r1.C1;
> C2 << r1.C2;
> @@
> try:
> 	if int(C1) * 2 != int(C2):
> 		cocci.include_match(False)
> except:
> 	cocci.include_match(False)
> 
> @@
> expression r1.x;
> constant r1.C1;
> constant r1.C2;
> @@
> -(((x) + C1) / C2)
> +DIV_ROUND_CLOSEST(x, C2)
> // </smpl>
> 
> Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>

Reviewed-by: Manivannan Sadhasivam <mani@kernel.org>

Thanks,
Mani

> ---
>  drivers/iio/light/vl6180.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/light/vl6180.c b/drivers/iio/light/vl6180.c
> index 4775bd785e50..d47a4f6f4e87 100644
> --- a/drivers/iio/light/vl6180.c
> +++ b/drivers/iio/light/vl6180.c
> @@ -392,7 +392,7 @@ static int vl6180_set_it(struct vl6180_data *data, int val, int val2)
>  {
>  	int ret, it_ms;
>  
> -	it_ms = (val2 + 500) / 1000; /* round to ms */
> +	it_ms = DIV_ROUND_CLOSEST(val2, 1000); /* round to ms */
>  	if (val != 0 || it_ms < 1 || it_ms > 512)
>  		return -EINVAL;
>  
> -- 
> 2.20.1
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/3] iio: vl6180: Use DIV_ROUND_CLOSEST() instead of open-coding it
  2020-12-28  5:58 ` [PATCH 1/3] iio: vl6180: " Manivannan Sadhasivam
@ 2020-12-29 13:58   ` Jonathan Cameron
  0 siblings, 0 replies; 5+ messages in thread
From: Jonathan Cameron @ 2020-12-29 13:58 UTC (permalink / raw)
  To: Manivannan Sadhasivam
  Cc: Lars-Peter Clausen, Peter Meerwald-Stadler, Himanshu Jha, linux-iio

On Mon, 28 Dec 2020 11:28:30 +0530
Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org> wrote:

> On Sun, Dec 27, 2020 at 06:11:24PM +0100, Lars-Peter Clausen wrote:
> > Use DIV_ROUND_CLOSEST() instead of open-coding it. This documents intent
> > and makes it more clear what is going on for the casual reviewer.
> > 
> > Generated using the following the Coccinelle semantic patch.
> > 
> > // <smpl>
> > @r1@
> > expression x;
> > constant C1;
> > constant C2;
> > @@
> >  ((x) + C1) / C2
> > 
> > @script:python@
> > C1 << r1.C1;
> > C2 << r1.C2;
> > @@
> > try:
> > 	if int(C1) * 2 != int(C2):
> > 		cocci.include_match(False)
> > except:
> > 	cocci.include_match(False)
> > 
> > @@
> > expression r1.x;
> > constant r1.C1;
> > constant r1.C2;
> > @@
> > -(((x) + C1) / C2)
> > +DIV_ROUND_CLOSEST(x, C2)
> > // </smpl>
> > 
> > Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>  
> 
> Reviewed-by: Manivannan Sadhasivam <mani@kernel.org>
> 
Series applied to the togreg branch of iio.git and pushed out as testing
for the autobuilders to work their magic.

Thanks,

Jonathan

> Thanks,
> Mani
> 
> > ---
> >  drivers/iio/light/vl6180.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/drivers/iio/light/vl6180.c b/drivers/iio/light/vl6180.c
> > index 4775bd785e50..d47a4f6f4e87 100644
> > --- a/drivers/iio/light/vl6180.c
> > +++ b/drivers/iio/light/vl6180.c
> > @@ -392,7 +392,7 @@ static int vl6180_set_it(struct vl6180_data *data, int val, int val2)
> >  {
> >  	int ret, it_ms;
> >  
> > -	it_ms = (val2 + 500) / 1000; /* round to ms */
> > +	it_ms = DIV_ROUND_CLOSEST(val2, 1000); /* round to ms */
> >  	if (val != 0 || it_ms < 1 || it_ms > 512)
> >  		return -EINVAL;
> >  
> > -- 
> > 2.20.1
> >   


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-12-29 13:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-27 17:11 [PATCH 1/3] iio: vl6180: Use DIV_ROUND_CLOSEST() instead of open-coding it Lars-Peter Clausen
2020-12-27 17:11 ` [PATCH 2/3] iio: bme680: " Lars-Peter Clausen
2020-12-27 17:11 ` [PATCH 3/3] iio: tsl2583: " Lars-Peter Clausen
2020-12-28  5:58 ` [PATCH 1/3] iio: vl6180: " Manivannan Sadhasivam
2020-12-29 13:58   ` Jonathan Cameron

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.