V4L/DVB (12483): Use DIV_ROUND_CLOSEST
authorJulia Lawall <julia@diku.dk>
Sat, 1 Aug 2009 19:48:41 +0000 (16:48 -0300)
committerMauro Carvalho Chehab <mchehab@redhat.com>
Sat, 12 Sep 2009 15:19:08 +0000 (12:19 -0300)
The kernel.h macro DIV_ROUND_CLOSEST performs the computation (x + d/2)/d
but is perhaps more readable.

The semantic patch that makes this change is as follows:
(http://www.emn.fr/x-info/coccinelle/)

// <smpl>
@haskernel@
@@

@depends on haskernel@
expression x,__divisor;
@@

- (((x) + ((__divisor) / 2)) / (__divisor))
+ DIV_ROUND_CLOSEST(x,__divisor)
// </smpl>

Signed-off-by: Julia Lawall <julia@diku.dk>
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
drivers/media/dvb/frontends/dib7000p.c
drivers/media/dvb/frontends/stb6100.c
drivers/media/dvb/frontends/tda10021.c
drivers/media/dvb/frontends/ves1820.c
drivers/media/dvb/pluto2/pluto2.c
drivers/media/video/tuner-core.c
drivers/media/video/v4l1-compat.c

index 8217e5b38f47064e464942ff3b75fa3b8e8ce53c..fc96fbf03d6dc9d1bbf9cce827ac950184c6d1d5 100644 (file)
@@ -883,7 +883,7 @@ static void dib7000p_spur_protect(struct dib7000p_state *state, u32 rf_khz, u32
        255, 255, 255, 255, 255, 255};
 
        u32 xtal = state->cfg.bw->xtal_hz / 1000;
-       int f_rel = ( (rf_khz + xtal/2) / xtal) * xtal - rf_khz;
+       int f_rel = DIV_ROUND_CLOSEST(rf_khz, xtal) * xtal - rf_khz;
        int k;
        int coef_re[8],coef_im[8];
        int bw_khz = bw;
index 1ed5a7db4c5eec887eaceb749ee6b454d79856c3..60ee18a94f43228b74a944cadad7c5ed430301d2 100644 (file)
@@ -367,7 +367,9 @@ static int stb6100_set_frequency(struct dvb_frontend *fe, u32 frequency)
        /* N(I) = floor(f(VCO) / (f(XTAL) * (PSD2 ? 2 : 1)))    */
        nint = fvco / (state->reference << psd2);
        /* N(F) = round(f(VCO) / f(XTAL) * (PSD2 ? 2 : 1) - N(I)) * 2 ^ 9       */
-       nfrac = (((fvco - (nint * state->reference << psd2)) << (9 - psd2)) + state->reference / 2) / state->reference;
+       nfrac = DIV_ROUND_CLOSEST((fvco - (nint * state->reference << psd2))
+                                        << (9 - psd2),
+                                 state->reference);
        dprintk(verbose, FE_DEBUG, 1,
                "frequency = %u, srate = %u, g = %u, odiv = %u, psd2 = %u, fxtal = %u, osm = %u, fvco = %u, N(I) = %u, N(F) = %u",
                frequency, srate, (unsigned int)g, (unsigned int)odiv,
index f5d7b3277a2fa585360aaafda331669a11ecd95a..6c1dbf9288d8b34d652bdf4b3465f3d46d0d4b1e 100644 (file)
@@ -176,7 +176,7 @@ static int tda10021_set_symbolrate (struct tda10021_state* state, u32 symbolrate
        tmp =  ((symbolrate << 4) % FIN) << 8;
        ratio = (ratio << 8) + tmp / FIN;
        tmp = (tmp % FIN) << 8;
-       ratio = (ratio << 8) + (tmp + FIN/2) / FIN;
+       ratio = (ratio << 8) + DIV_ROUND_CLOSEST(tmp, FIN);
 
        BDR = ratio;
        BDRI = (((XIN << 5) / symbolrate) + 1) / 2;
index 6e78e486551599bc47d76addb4e0af3e284175b6..550a07a8a9975d253b9e39482d5e4cc0b040adb8 100644 (file)
@@ -165,7 +165,7 @@ static int ves1820_set_symbolrate(struct ves1820_state *state, u32 symbolrate)
        tmp = ((symbolrate << 4) % fin) << 8;
        ratio = (ratio << 8) + tmp / fin;
        tmp = (tmp % fin) << 8;
-       ratio = (ratio << 8) + (tmp + fin / 2) / fin;
+       ratio = (ratio << 8) + DIV_ROUND_CLOSEST(tmp, fin);
 
        BDR = ratio;
        BDRI = (((state->config->xin << 5) / symbolrate) + 1) / 2;
index 598eaf8acc6e4ef4881fe1a862716142698b5c97..80d14a065bad0755fda4af3f27edb0b9fbe18308 100644 (file)
@@ -439,7 +439,7 @@ static inline u32 divide(u32 numerator, u32 denominator)
        if (denominator == 0)
                return ~0;
 
-       return (numerator + denominator / 2) / denominator;
+       return DIV_ROUND_CLOSEST(numerator, denominator);
 }
 
 /* LG Innotek TDTE-E001P (Infineon TUA6034) */
index 537594211a90e1ccb6fa49b74e188b642ae0c6ae..2816f1839230c1131aef1298d5e5b3157dc75ae9 100644 (file)
@@ -819,8 +819,8 @@ static int tuner_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
 
                fe_tuner_ops->get_frequency(&t->fe, &abs_freq);
                f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
-                       (abs_freq * 2 + 125/2) / 125 :
-                       (abs_freq + 62500/2) / 62500;
+                       DIV_ROUND_CLOSEST(abs_freq * 2, 125) :
+                       DIV_ROUND_CLOSEST(abs_freq, 62500);
                return 0;
        }
        f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
index 02f2a6d18b4582987e1ce0ade96ba510738093ff..761fbd64db5850adfeba11a8cc3b32ba0f7f75a4 100644 (file)
@@ -76,9 +76,8 @@ get_v4l_control(struct file             *file,
                        dprintk("VIDIOC_G_CTRL: %d\n", err);
                        return 0;
                }
-               return ((ctrl2.value - qctrl2.minimum) * 65535
-                        + (qctrl2.maximum - qctrl2.minimum) / 2)
-                       / (qctrl2.maximum - qctrl2.minimum);
+               return DIV_ROUND_CLOSEST((ctrl2.value-qctrl2.minimum) * 65535,
+                                        qctrl2.maximum - qctrl2.minimum);
        }
        return 0;
 }