alsa-devel.alsa-project.org archive mirror
 help / color / mirror / Atom feed
From: Lars-Peter Clausen <lars@metafoo.de>
To: Takashi Iwai <tiwai@suse.com>
Cc: alsa-devel@alsa-project.org, Lars-Peter Clausen <lars@metafoo.de>
Subject: [PATCH 13/18] ALSA: oss: Use DIV_ROUND_CLOSEST() instead of open-coding it
Date: Wed, 23 Dec 2020 18:22:24 +0100	[thread overview]
Message-ID: <20201223172229.781-13-lars@metafoo.de> (raw)
In-Reply-To: <20201223172229.781-1-lars@metafoo.de>

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>
@@
expression x, y;
@@
-((x) + ((y) / 2)) / (y)
+DIV_ROUND_CLOSEST(x, y)
// </smpl>

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
---
 sound/core/oss/mixer_oss.c | 2 +-
 sound/core/oss/rate.c      | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/sound/core/oss/mixer_oss.c b/sound/core/oss/mixer_oss.c
index f702c96a7478..af5de08f9819 100644
--- a/sound/core/oss/mixer_oss.c
+++ b/sound/core/oss/mixer_oss.c
@@ -418,7 +418,7 @@ static long snd_mixer_oss_conv(long val, long omin, long omax, long nmin, long n
 	
 	if (orange == 0)
 		return 0;
-	return ((nrange * (val - omin)) + (orange / 2)) / orange + nmin;
+	return DIV_ROUND_CLOSEST(nrange * (val - omin), orange) + nmin;
 }
 
 /* convert from alsa native to oss values (0-100) */
diff --git a/sound/core/oss/rate.c b/sound/core/oss/rate.c
index d381f4c967c9..98269119347f 100644
--- a/sound/core/oss/rate.c
+++ b/sound/core/oss/rate.c
@@ -193,7 +193,7 @@ static snd_pcm_sframes_t rate_src_frames(struct snd_pcm_plugin *plugin, snd_pcm_
 	if (plugin->src_format.rate < plugin->dst_format.rate) {
 		res = (((frames * data->pitch) + (BITS/2)) >> SHIFT);
 	} else {
-		res = (((frames << SHIFT) + (data->pitch / 2)) / data->pitch);		
+		res = DIV_ROUND_CLOSEST(frames << SHIFT, data->pitch);
 	}
 	if (data->old_src_frames > 0) {
 		snd_pcm_sframes_t frames1 = frames, res1 = data->old_dst_frames;
@@ -224,7 +224,7 @@ static snd_pcm_sframes_t rate_dst_frames(struct snd_pcm_plugin *plugin, snd_pcm_
 		return 0;
 	data = (struct rate_priv *)plugin->extra_data;
 	if (plugin->src_format.rate < plugin->dst_format.rate) {
-		res = (((frames << SHIFT) + (data->pitch / 2)) / data->pitch);
+		res = DIV_ROUND_CLOSEST(frames << SHIFT, data->pitch);
 	} else {
 		res = (((frames * data->pitch) + (BITS/2)) >> SHIFT);
 	}
-- 
2.20.1


  parent reply	other threads:[~2020-12-23 17:46 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-23 17:22 [PATCH 01/18] ALSA: core: Use DIV_ROUND_UP() instead of open-coding it Lars-Peter Clausen
2020-12-23 17:22 ` [PATCH 02/18] ALSA: aloop: " Lars-Peter Clausen
2020-12-23 17:22 ` [PATCH 03/18] ALSA: asihpi: " Lars-Peter Clausen
2020-12-23 17:22 ` [PATCH 04/18] ALSA: bt87x: " Lars-Peter Clausen
2020-12-23 17:22 ` [PATCH 05/18] ALSA: cx46xx: " Lars-Peter Clausen
2020-12-23 17:22 ` [PATCH 06/18] ALSA: ctxfi: " Lars-Peter Clausen
2020-12-23 17:22 ` [PATCH 07/18] ALSA: dummy: " Lars-Peter Clausen
2020-12-23 17:22 ` [PATCH 08/18] ALSA: emu10k1: " Lars-Peter Clausen
2020-12-23 17:22 ` [PATCH 09/18] ALSA: hda: Use DIV_ROUND_UP()/roundup() " Lars-Peter Clausen
2020-12-23 17:22 ` [PATCH 10/18] ALSA: lola: Use DIV_ROUND_UP() " Lars-Peter Clausen
2020-12-23 17:22 ` [PATCH 11/18] ALSA: usb: " Lars-Peter Clausen
2020-12-23 17:22 ` [PATCH 12/18] ALSA: vx: Use roundup() " Lars-Peter Clausen
2020-12-23 17:22 ` Lars-Peter Clausen [this message]
2020-12-23 17:22 ` [PATCH 14/18] ALSA: sonicvibes: Use DIV_ROUND_CLOSEST() " Lars-Peter Clausen
2020-12-23 17:22 ` [PATCH 15/18] ALSA: trident: " Lars-Peter Clausen
2020-12-23 17:22 ` [PATCH 16/18] ALSA: ens1370: " Lars-Peter Clausen
2020-12-23 17:22 ` [PATCH 17/18] ALSA: sis7019: " Lars-Peter Clausen
2020-12-23 17:22 ` [PATCH 18/18] ALSA: maestro: " Lars-Peter Clausen
2020-12-25  8:16 ` [PATCH 01/18] ALSA: core: Use DIV_ROUND_UP() " Takashi Iwai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201223172229.781-13-lars@metafoo.de \
    --to=lars@metafoo.de \
    --cc=alsa-devel@alsa-project.org \
    --cc=tiwai@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).