[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250401223731.7102103d@pumpkin>
Date: Tue, 1 Apr 2025 22:37:31 +0100
From: David Laight <david.laight.linux@...il.com>
To: Nicolas Pitre <nico@...xnic.net>
Cc: Uwe Kleine-König <u.kleine-koenig@...libre.com>,
Andrew Morton <akpm@...ux-foundation.org>, linux-kernel@...r.kernel.org
Subject: Re: [PATCH] math64: Provide an uprounding variant of
mul_u64_u64_div_u64()
On Tue, 1 Apr 2025 16:30:34 -0400 (EDT)
Nicolas Pitre <nico@...xnic.net> wrote:
> On Tue, 1 Apr 2025, Nicolas Pitre wrote:
>
> > On Tue, 1 Apr 2025, David Laight wrote:
> >
> > > Looking at the C version, I wonder if the two ilog2() calls are needed.
> > > They may not be cheap, and are the same as checking 'n_hi == 0'.
> >
> > Which two calls? I see only one.
>
> Hmmm, sorry. If by ilog2() you mean the clz's then those are cheap. Most
> CPUs have a dedicated instruction for that. The ctz, though, is more
> expensive (unless it is ARMv7 and above with an RBIT instruction).
The code (6.14-rc6) starts:
u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 c)
{
if (ilog2(a) + ilog2(b) <= 62)
return div64_u64(a * b, c);
Now ilog2() is (probably) much the same as clz - but not all cpu have it.
Indeed clz(a) + clz(b) >= 64 would be a more obvious test.
On 32bit a check for a >> 32 | b >> 32 before the multiply might be sane.
> > And please explain how it can be the same as checking 'n_hi == 0'.
>
> This question still stands.
'ni_hi == 0' is exactly the condition under which you can do a 64 bit divide.
Since it is when 'a * b' fits in 64 bits.
If you assume that most calls need the 128bit product (or why use the function)
then there is little point adding code to optimise for the unusual case.
David
>
>
> Nicolas
Powered by blists - more mailing lists