From: arnd@arndb.de (Arnd Bergmann)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH] optimize ktime_divns for constant divisors
Date: Thu, 04 Dec 2014 13:02:33 +0100 [thread overview]
Message-ID: <2165831.DQoLFmGhIf@wuerfel> (raw)
In-Reply-To: <alpine.LFD.2.11.1412040143260.470@knanqh.ubzr>
On Thursday 04 December 2014 02:23:37 Nicolas Pitre wrote:
> On Wed, 3 Dec 2014, Arnd Bergmann wrote:
>
> > On Wednesday 03 December 2014 14:43:06 Nicolas Pitre wrote:
> > > At least on ARM, do_div() is optimized to turn constant divisors into
> > > an inline multiplication by the reciprocal value at compile time.
> > > However this optimization is missed entirely whenever ktime_divns() is
> > > used and the slow out-of-line division code is used all the time.
> > >
> > > Let ktime_divns() use do_div() inline whenever the divisor is constant
> > > and small enough. This will make things like ktime_to_us() and
> > > ktime_to_ms() much faster.
> > >
> > > Signed-off-by: Nicolas Pitre <nico@linaro.org>
> >
> > Very cool. I've been thinking about doing something similar for the
> > general case but couldn't get the math to work.
> >
> > Can you think of an architecture-independent way to ktime_to_sec,
> > ktime_to_ms, and ktime_to_us efficiently based on what you did for
> > the ARM do_div implementation?
>
> Sure. gcc generates rather shitty code on ARM compared to the output
> from my do_div() implementation. But here it is:
>
> u64 ktime_to_us(ktime_t kt)
> {
> u64 ns = ktime_to_ns(kt);
> u32 x_lo, x_hi, y_lo, y_hi;
> u64 res, carry;
>
> x_hi = ns >> 32;
> x_lo = ns;
> y_hi = 0x83126e97;
> y_lo = 0x8d4fdf3b;
>
> res = (u64)x_lo * y_lo;
> carry = (u64)(u32)res + y_lo;
> res = (res >> 32) + (carry >> 32);
>
> res += (u64)x_lo * y_hi;
> carry = (u64)(u32)res + (u64)x_hi * y_lo;
> res = (res >> 32) + (carry >> 32);
>
> res += (u64)x_hi * y_hi;
> return res >> 9;
> }
Ok, I see, thanks for the example. I also tried this on x86, and it takes
about twice as long as do_div on my Opteron, so it wouldn't be as helpful
as I hoped.
On a related note, I wonder if we can come up with a more efficient
implementation for do_div on ARMv7ve, and I think we should add the
Makefile logic to build with -march=armv7ve when we know that we do
not need to support processors without idiv.
Arnd
WARNING: multiple messages have this Message-ID (diff)
From: Arnd Bergmann <arnd@arndb.de>
To: Nicolas Pitre <nicolas.pitre@linaro.org>
Cc: linux-arm-kernel@lists.infradead.org,
Thomas Gleixner <tglx@linutronix.de>,
John Stultz <john.stultz@linaro.org>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] optimize ktime_divns for constant divisors
Date: Thu, 04 Dec 2014 13:02:33 +0100 [thread overview]
Message-ID: <2165831.DQoLFmGhIf@wuerfel> (raw)
In-Reply-To: <alpine.LFD.2.11.1412040143260.470@knanqh.ubzr>
On Thursday 04 December 2014 02:23:37 Nicolas Pitre wrote:
> On Wed, 3 Dec 2014, Arnd Bergmann wrote:
>
> > On Wednesday 03 December 2014 14:43:06 Nicolas Pitre wrote:
> > > At least on ARM, do_div() is optimized to turn constant divisors into
> > > an inline multiplication by the reciprocal value at compile time.
> > > However this optimization is missed entirely whenever ktime_divns() is
> > > used and the slow out-of-line division code is used all the time.
> > >
> > > Let ktime_divns() use do_div() inline whenever the divisor is constant
> > > and small enough. This will make things like ktime_to_us() and
> > > ktime_to_ms() much faster.
> > >
> > > Signed-off-by: Nicolas Pitre <nico@linaro.org>
> >
> > Very cool. I've been thinking about doing something similar for the
> > general case but couldn't get the math to work.
> >
> > Can you think of an architecture-independent way to ktime_to_sec,
> > ktime_to_ms, and ktime_to_us efficiently based on what you did for
> > the ARM do_div implementation?
>
> Sure. gcc generates rather shitty code on ARM compared to the output
> from my do_div() implementation. But here it is:
>
> u64 ktime_to_us(ktime_t kt)
> {
> u64 ns = ktime_to_ns(kt);
> u32 x_lo, x_hi, y_lo, y_hi;
> u64 res, carry;
>
> x_hi = ns >> 32;
> x_lo = ns;
> y_hi = 0x83126e97;
> y_lo = 0x8d4fdf3b;
>
> res = (u64)x_lo * y_lo;
> carry = (u64)(u32)res + y_lo;
> res = (res >> 32) + (carry >> 32);
>
> res += (u64)x_lo * y_hi;
> carry = (u64)(u32)res + (u64)x_hi * y_lo;
> res = (res >> 32) + (carry >> 32);
>
> res += (u64)x_hi * y_hi;
> return res >> 9;
> }
Ok, I see, thanks for the example. I also tried this on x86, and it takes
about twice as long as do_div on my Opteron, so it wouldn't be as helpful
as I hoped.
On a related note, I wonder if we can come up with a more efficient
implementation for do_div on ARMv7ve, and I think we should add the
Makefile logic to build with -march=armv7ve when we know that we do
not need to support processors without idiv.
Arnd
next prev parent reply other threads:[~2014-12-04 12:02 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-12-03 19:43 [PATCH] optimize ktime_divns for constant divisors Nicolas Pitre
2014-12-03 19:43 ` Nicolas Pitre
2014-12-03 20:03 ` Arnd Bergmann
2014-12-03 20:03 ` Arnd Bergmann
2014-12-04 7:23 ` Nicolas Pitre
2014-12-04 7:23 ` Nicolas Pitre
2014-12-04 12:02 ` Arnd Bergmann [this message]
2014-12-04 12:02 ` Arnd Bergmann
2014-12-04 13:46 ` Nicolas Pitre
2014-12-04 13:46 ` Nicolas Pitre
2014-12-04 14:56 ` Arnd Bergmann
2014-12-04 14:56 ` Arnd Bergmann
2014-12-04 16:47 ` Nicolas Pitre
2014-12-04 16:47 ` Nicolas Pitre
[not found] ` <OF0EDEDB1C.C03829F7-ON48257DA5.00062083-48257DA5.0007628B@zte.com.cn>
2014-12-05 4:30 ` Nicolas Pitre
2014-12-05 4:30 ` Nicolas Pitre
2014-12-05 10:08 ` Arnd Bergmann
2014-12-05 10:08 ` Arnd Bergmann
2014-12-05 17:15 ` Nicolas Pitre
2014-12-05 17:15 ` Nicolas Pitre
2014-12-03 20:16 ` Robert Jarzmik
2014-12-03 20:16 ` Robert Jarzmik
2014-12-03 20:37 ` Nicolas Pitre
2014-12-03 20:37 ` Nicolas Pitre
2014-12-05 18:00 ` Nicolas Pitre
2014-12-05 18:00 ` Nicolas Pitre
2014-12-05 21:03 ` Arnd Bergmann
2014-12-05 21:03 ` Arnd Bergmann
2014-12-18 21:21 ` John Stultz
2014-12-18 21:21 ` John Stultz
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=2165831.DQoLFmGhIf@wuerfel \
--to=arnd@arndb.de \
--cc=linux-arm-kernel@lists.infradead.org \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.