mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Laight <David.Laight@ACULAB.COM>
To: 'Geert Uytterhoeven' <geert@linux-m68k.org>,
	Serge Semin <fancer.lancer@gmail.com>
Cc: "wuyonggang001@208suo.com" <wuyonggang001@208suo.com>,
	"mturquette@baylibre.com" <mturquette@baylibre.com>,
	"sboyd@kernel.org" <sboyd@kernel.org>,
	"linux-clk@vger.kernel.org" <linux-clk@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: RE: [PATCH] clk: baikal-t1: Using div64_ Ul replaces do_ Div() function
Date: Mon, 24 Jul 2023 14:01:27 +0000	[thread overview]
Message-ID: <daccab41116d4c88823ab7fc84846077@AcuMS.aculab.com> (raw)
In-Reply-To: <CAMuHMdUHDK9CCJPoMgLQBrXjk9VWszYF17dUU=9JtQ8XX=QAPA@mail.gmail.com>

From: Geert Uytterhoeven
> Sent: 24 July 2023 14:39
> 
> Hi Serge,
> 
> On Mon, Jul 24, 2023 at 3:13 PM Serge Semin <fancer.lancer@gmail.com> wrote:
> > On Mon, Jul 24, 2023 at 12:04:19PM +0200, Geert Uytterhoeven wrote:
> > > On Wed, Jun 14, 2023 at 8:07 AM <wuyonggang001@208suo.com> wrote:
> > > > Fix the following coccicheck warning:
> > > >
> > > > drivers/clk/baikal-t1/ccu-pll.c:81:1-7: WARNING: do_div() does a
> > > > 64-by-32 division, please consider using div64_ul instead.
> > > >
> > > > Signed-off-by: Yonggang Wu <wuyonggang001@208suo.com>
> > >
> > > Thanks for your patch, which is now commit b93d1331ea266dea
> > > ("clk: baikal-t1: Using div64_ Ul replaces do_ Div() function")
> > > in clk/clk-next.
> > >
> > > > b/drivers/clk/baikal-t1/ccu-pll.c
> > > > index 13ef28001439..d41735c6956a 100644
> > > > --- a/drivers/clk/baikal-t1/ccu-pll.c
> > > > +++ b/drivers/clk/baikal-t1/ccu-pll.c
> 
> > > > @@ -78,9 +78,9 @@ static inline unsigned long ccu_pll_calc_freq(unsigned
> > > > long ref_clk,
> > > >   {
> > > >       u64 tmp = ref_clk;
> > > >
> >
> > > > -    do_div(tmp, nr);
> > > > +    div64_ul(tmp, nr);
> > > >       tmp *= nf;
> > > > -    do_div(tmp, od);
> > > > +    div64_ul(tmp, od);
> > > >
> > > >       return tmp;
> > >
> > > Likewise.
> >
> > Right. This will also break the driver.
> >
> > > But as ref_clk is unsigned long, there is no need to use div64_ul()
> > > for the first division, and this can be simplified to:
> > >
> > >     u64 tmp = (u64)(ref_clk / nr) * nf;
> > >     return div64_ul(tmp, od);
> >
> > Absolutely right. My intention of using the do_div() anyway was for
> > the sake of the code unification.
> >
> > >
> > > To avoid loss of precision, it might be better to reverse the order
> > > of the division and multiplication:
> > >
> >
> > >     u64 tmp = (u64)ref_clk * nf / nr;
> >
> > Alas exactly this code will cause the compilation error on the 32-bit
> > platform:
> > ccu-pll.c:(.text+0x458): undefined reference to `__udivdi3'
> >
> > That's why I am using the do_div() here. I would have rather used the
> > div64_ul() instead as this patch suggests, but I haven't known about its
> > existence up to this moment.
> 
> Bummer, that was a silly mistake on my side...
> (Initially, I didn't write the cast to u64 there, as all of ref_clk, nf, and nr
>  are unsigned long.  Then I realized "ref_clk * nf" might overflow on
>  32-bit, thus requiring a 64-bit result. And I added the cast...)

But on 32bit the result is 'long'.
So it will overflow unless do_div() is also valid.

The analysis need to look at the domain of the values.
The warning and suggestion to use div64_ul() is pretty much always
wrong.

div64_ul() is going to be horribly slow on 32bit.
Also on 64bit Intel cpu the 128/64 divide takes twice as long as 64/32
even when the values are small.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

  reply	other threads:[~2023-07-24 14:01 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20230612033904.34921-1-zhanglibing@cdjrlc.com>
     [not found] ` <0dc9409b662180ed29cbc281f0f076b7@208suo.com>
2023-06-14  5:45   ` wuyonggang001
2023-07-19 22:08     ` Stephen Boyd
2023-07-24 10:04     ` Geert Uytterhoeven
2023-07-24 13:13       ` Serge Semin
2023-07-24 13:38         ` Geert Uytterhoeven
2023-07-24 14:01           ` David Laight [this message]
2023-07-24 14:11           ` Serge Semin
2023-07-27 12:28             ` Serge Semin
2023-07-29  3:24               ` Stephen Boyd
2023-07-31 23:55                 ` Serge Semin

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=daccab41116d4c88823ab7fc84846077@AcuMS.aculab.com \
    --to=david.laight@aculab.com \
    --cc=fancer.lancer@gmail.com \
    --cc=geert@linux-m68k.org \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mturquette@baylibre.com \
    --cc=sboyd@kernel.org \
    --cc=wuyonggang001@208suo.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

Powered by JetHome