ALSA: trident: Use DIV_ROUND_CLOSEST() instead of open-coding it
authorLars-Peter Clausen <lars@metafoo.de>
Wed, 23 Dec 2020 17:22:26 +0000 (18:22 +0100)
committerTakashi Iwai <tiwai@suse.de>
Fri, 25 Dec 2020 08:11:55 +0000 (09:11 +0100)
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)

@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>
Link: https://lore.kernel.org/r/20201223172229.781-15-lars@metafoo.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
sound/pci/trident/trident_main.c

index 6e50376..8d0d0d8 100644 (file)
@@ -678,7 +678,7 @@ static unsigned int snd_trident_convert_rate(unsigned int rate)
        else if (rate == 48000)
                delta = 0x1000;
        else
-               delta = (((rate << 12) + 24000) / 48000) & 0x0000ffff;
+               delta = DIV_ROUND_CLOSEST(rate << 12, 48000) & 0x0000ffff;
        return delta;
 }
 
@@ -1034,7 +1034,7 @@ static int snd_trident_capture_prepare(struct snd_pcm_substream *substream)
        ESO_bytes++;
 
        // Set channel sample rate, 4.12 format
-       val = (((unsigned int) 48000L << 12) + (runtime->rate/2)) / runtime->rate;
+       val = DIV_ROUND_CLOSEST(48000U << 12, runtime->rate);
        outw(val, TRID_REG(trident, T4D_SBDELTA_DELTA_R));
 
        // Set channel interrupt blk length