linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/4] media: Use DIV_ROUND_CLOSEST directly
@ 2019-09-05 16:14 zhong jiang
  2019-09-05 16:14 ` [PATCH 1/4] media: dvb-frontends: Use DIV_ROUND_CLOSEST directly to make it readable zhong jiang
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: zhong jiang @ 2019-09-05 16:14 UTC (permalink / raw)
  To: mchehab; +Cc: hansverk, daniel.vetter, zhongjiang, linux-media, linux-kernel

With the following help of Coccinelle. I find out some place can be replaced.

-(((x) + ((__divisor) / 2)) / (__divisor))
+ DIV_ROUND_CLOSEST(x,__divisor)

zhong jiang (4):
  media: dvb-frontends: Use DIV_ROUND_CLOSEST directly to make it
    readable
  media: tuners/qm1d1c0042: Use DIV_ROUND_CLOSEST directly to make it
    readable
  media: uvcvideo: Use DIV_ROUND_CLOSEST directly to make it readable
  media: v4l2-dv-timings: Use DIV_ROUND_CLOSEST directly to make it
    readable

 drivers/media/dvb-frontends/mt312.c       | 2 +-
 drivers/media/tuners/qm1d1c0042.c         | 2 +-
 drivers/media/usb/uvc/uvc_ctrl.c          | 4 ++--
 drivers/media/v4l2-core/v4l2-dv-timings.c | 2 +-
 4 files changed, 5 insertions(+), 5 deletions(-)

-- 
1.7.12.4


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

* [PATCH 1/4] media: dvb-frontends: Use DIV_ROUND_CLOSEST directly to make it readable
  2019-09-05 16:14 [PATCH 0/4] media: Use DIV_ROUND_CLOSEST directly zhong jiang
@ 2019-09-05 16:14 ` zhong jiang
  2019-10-01 11:15   ` Sean Young
  2019-09-05 16:14 ` [PATCH 2/4] media: tuners/qm1d1c0042: " zhong jiang
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: zhong jiang @ 2019-09-05 16:14 UTC (permalink / raw)
  To: mchehab; +Cc: hansverk, daniel.vetter, zhongjiang, linux-media, linux-kernel

The kernel.h macro DIV_ROUND_CLOSEST performs the computation (x + d/2)/d
but is perhaps more readable.

Signed-off-by: zhong jiang <zhongjiang@huawei.com>
---
 drivers/media/dvb-frontends/mt312.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/media/dvb-frontends/mt312.c b/drivers/media/dvb-frontends/mt312.c
index 7cae7d6..251ff41 100644
--- a/drivers/media/dvb-frontends/mt312.c
+++ b/drivers/media/dvb-frontends/mt312.c
@@ -137,7 +137,7 @@ static inline int mt312_writereg(struct mt312_state *state,
 
 static inline u32 mt312_div(u32 a, u32 b)
 {
-	return (a + (b / 2)) / b;
+	return DIV_ROUND_CLOSEST(a, b);
 }
 
 static int mt312_reset(struct mt312_state *state, const u8 full)
-- 
1.7.12.4


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

* [PATCH 2/4] media: tuners/qm1d1c0042: Use DIV_ROUND_CLOSEST directly to make it readable
  2019-09-05 16:14 [PATCH 0/4] media: Use DIV_ROUND_CLOSEST directly zhong jiang
  2019-09-05 16:14 ` [PATCH 1/4] media: dvb-frontends: Use DIV_ROUND_CLOSEST directly to make it readable zhong jiang
@ 2019-09-05 16:14 ` zhong jiang
  2019-09-05 16:14 ` [PATCH 3/4] media: uvcvideo: " zhong jiang
  2019-09-05 16:14 ` [PATCH 4/4] media: v4l2-dv-timings: " zhong jiang
  3 siblings, 0 replies; 7+ messages in thread
From: zhong jiang @ 2019-09-05 16:14 UTC (permalink / raw)
  To: mchehab; +Cc: hansverk, daniel.vetter, zhongjiang, linux-media, linux-kernel

The kernel.h macro DIV_ROUND_CLOSEST performs the computation (x + d/2)/d
but is perhaps more readable.

Signed-off-by: zhong jiang <zhongjiang@huawei.com>
---
 drivers/media/tuners/qm1d1c0042.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/media/tuners/qm1d1c0042.c b/drivers/media/tuners/qm1d1c0042.c
index 83ca5dc..0e26d22 100644
--- a/drivers/media/tuners/qm1d1c0042.c
+++ b/drivers/media/tuners/qm1d1c0042.c
@@ -206,7 +206,7 @@ static int qm1d1c0042_set_params(struct dvb_frontend *fe)
 	if (ret < 0)
 		return ret;
 
-	a = (freq + state->cfg.xtal_freq / 2) / state->cfg.xtal_freq;
+	a = DIV_ROUND_CLOSEST(freq, state->cfg.xtal_freq);
 
 	state->regs[0x06] &= 0x40;
 	state->regs[0x06] |= (a - 12) / 4;
-- 
1.7.12.4


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

* [PATCH 3/4] media: uvcvideo: Use DIV_ROUND_CLOSEST directly to make it readable
  2019-09-05 16:14 [PATCH 0/4] media: Use DIV_ROUND_CLOSEST directly zhong jiang
  2019-09-05 16:14 ` [PATCH 1/4] media: dvb-frontends: Use DIV_ROUND_CLOSEST directly to make it readable zhong jiang
  2019-09-05 16:14 ` [PATCH 2/4] media: tuners/qm1d1c0042: " zhong jiang
@ 2019-09-05 16:14 ` zhong jiang
  2019-09-05 16:14 ` [PATCH 4/4] media: v4l2-dv-timings: " zhong jiang
  3 siblings, 0 replies; 7+ messages in thread
From: zhong jiang @ 2019-09-05 16:14 UTC (permalink / raw)
  To: mchehab; +Cc: hansverk, daniel.vetter, zhongjiang, linux-media, linux-kernel

The kernel.h macro DIV_ROUND_CLOSEST performs the computation (x + d/2)/d
but is perhaps more readable.

Signed-off-by: zhong jiang <zhongjiang@huawei.com>
---
 drivers/media/usb/uvc/uvc_ctrl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c
index e399b9f..9f3697161 100644
--- a/drivers/media/usb/uvc/uvc_ctrl.c
+++ b/drivers/media/usb/uvc/uvc_ctrl.c
@@ -1604,8 +1604,8 @@ int uvc_ctrl_set(struct uvc_fh *handle,
 		if (step == 0)
 			step = 1;
 
-		xctrl->value = min + ((u32)(xctrl->value - min) + step / 2)
-			     / step * step;
+		xctrl->value = min + DIV_ROUND_CLOSEST((u32)(xctrl->value - min),
+							step) * step;
 		if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
 			xctrl->value = clamp(xctrl->value, min, max);
 		else
-- 
1.7.12.4


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

* [PATCH 4/4] media: v4l2-dv-timings: Use DIV_ROUND_CLOSEST directly to make it readable
  2019-09-05 16:14 [PATCH 0/4] media: Use DIV_ROUND_CLOSEST directly zhong jiang
                   ` (2 preceding siblings ...)
  2019-09-05 16:14 ` [PATCH 3/4] media: uvcvideo: " zhong jiang
@ 2019-09-05 16:14 ` zhong jiang
  3 siblings, 0 replies; 7+ messages in thread
From: zhong jiang @ 2019-09-05 16:14 UTC (permalink / raw)
  To: mchehab; +Cc: hansverk, daniel.vetter, zhongjiang, linux-media, linux-kernel

The kernel.h macro DIV_ROUND_CLOSEST performs the computation (x + d/2)/d
but is perhaps more readable.

Signed-off-by: zhong jiang <zhongjiang@huawei.com>
---
 drivers/media/v4l2-core/v4l2-dv-timings.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/media/v4l2-core/v4l2-dv-timings.c b/drivers/media/v4l2-core/v4l2-dv-timings.c
index 4f23e93..2b399c0 100644
--- a/drivers/media/v4l2-core/v4l2-dv-timings.c
+++ b/drivers/media/v4l2-core/v4l2-dv-timings.c
@@ -757,7 +757,7 @@ bool v4l2_detect_gtf(unsigned frame_height,
 	pix_clk = pix_clk / GTF_PXL_CLK_GRAN * GTF_PXL_CLK_GRAN;
 
 	hsync = (frame_width * 8 + 50) / 100;
-	hsync = ((hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN) * GTF_CELL_GRAN;
+	hsync = DIV_ROUND_CLOSEST(hsync, GTF_CELL_GRAN) * GTF_CELL_GRAN;
 
 	h_fp = h_blank / 2 - hsync;
 
-- 
1.7.12.4


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

* Re: [PATCH 1/4] media: dvb-frontends: Use DIV_ROUND_CLOSEST directly to make it readable
  2019-09-05 16:14 ` [PATCH 1/4] media: dvb-frontends: Use DIV_ROUND_CLOSEST directly to make it readable zhong jiang
@ 2019-10-01 11:15   ` Sean Young
  2019-10-09 14:24     ` zhong jiang
  0 siblings, 1 reply; 7+ messages in thread
From: Sean Young @ 2019-10-01 11:15 UTC (permalink / raw)
  To: zhong jiang; +Cc: mchehab, hansverk, daniel.vetter, linux-media, linux-kernel

Hi,

On Fri, Sep 06, 2019 at 12:14:49AM +0800, zhong jiang wrote:
> The kernel.h macro DIV_ROUND_CLOSEST performs the computation (x + d/2)/d
> but is perhaps more readable.
> 
> Signed-off-by: zhong jiang <zhongjiang@huawei.com>
> ---
>  drivers/media/dvb-frontends/mt312.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/media/dvb-frontends/mt312.c b/drivers/media/dvb-frontends/mt312.c
> index 7cae7d6..251ff41 100644
> --- a/drivers/media/dvb-frontends/mt312.c
> +++ b/drivers/media/dvb-frontends/mt312.c
> @@ -137,7 +137,7 @@ static inline int mt312_writereg(struct mt312_state *state,
>  
>  static inline u32 mt312_div(u32 a, u32 b)
>  {
> -	return (a + (b / 2)) / b;
> +	return DIV_ROUND_CLOSEST(a, b);

Well spotted, that is absolutely correct. However now mt312_div() is just
a wrapper for DIV_ROUND_CLOSEST() -- and even marked inline. Really all
the callers to mt312_div() should be replaced with DIV_ROUND_CLOSEST().

Thanks,

Sean

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

* Re: [PATCH 1/4] media: dvb-frontends: Use DIV_ROUND_CLOSEST directly to make it readable
  2019-10-01 11:15   ` Sean Young
@ 2019-10-09 14:24     ` zhong jiang
  0 siblings, 0 replies; 7+ messages in thread
From: zhong jiang @ 2019-10-09 14:24 UTC (permalink / raw)
  To: Sean Young; +Cc: mchehab, hansverk, daniel.vetter, linux-media, linux-kernel

On 2019/10/1 19:15, Sean Young wrote:
> Hi,
>
> On Fri, Sep 06, 2019 at 12:14:49AM +0800, zhong jiang wrote:
>> The kernel.h macro DIV_ROUND_CLOSEST performs the computation (x + d/2)/d
>> but is perhaps more readable.
>>
>> Signed-off-by: zhong jiang <zhongjiang@huawei.com>
>> ---
>>  drivers/media/dvb-frontends/mt312.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/media/dvb-frontends/mt312.c b/drivers/media/dvb-frontends/mt312.c
>> index 7cae7d6..251ff41 100644
>> --- a/drivers/media/dvb-frontends/mt312.c
>> +++ b/drivers/media/dvb-frontends/mt312.c
>> @@ -137,7 +137,7 @@ static inline int mt312_writereg(struct mt312_state *state,
>>  
>>  static inline u32 mt312_div(u32 a, u32 b)
>>  {
>> -	return (a + (b / 2)) / b;
>> +	return DIV_ROUND_CLOSEST(a, b);
> Well spotted, that is absolutely correct. However now mt312_div() is just
> a wrapper for DIV_ROUND_CLOSEST() -- and even marked inline. Really all
> the callers to mt312_div() should be replaced with DIV_ROUND_CLOSEST().
Thanks for your suggestion.   I will use DIV_ROUND_CLOSEST directly in v2.

Sincerely,
zhong jiang
> Thanks,
>
> Sean
>
> .
>



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

end of thread, other threads:[~2019-10-09 14:24 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-05 16:14 [PATCH 0/4] media: Use DIV_ROUND_CLOSEST directly zhong jiang
2019-09-05 16:14 ` [PATCH 1/4] media: dvb-frontends: Use DIV_ROUND_CLOSEST directly to make it readable zhong jiang
2019-10-01 11:15   ` Sean Young
2019-10-09 14:24     ` zhong jiang
2019-09-05 16:14 ` [PATCH 2/4] media: tuners/qm1d1c0042: " zhong jiang
2019-09-05 16:14 ` [PATCH 3/4] media: uvcvideo: " zhong jiang
2019-09-05 16:14 ` [PATCH 4/4] media: v4l2-dv-timings: " zhong jiang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).