public inbox for linux-arm-kernel@lists.infradead.org
 help / color / mirror / Atom feed
From: arnd@arndb.de (Arnd Bergmann)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH] optimize ktime_divns for constant divisors
Date: Fri, 05 Dec 2014 11:08:07 +0100	[thread overview]
Message-ID: <2145860.PBxl6kLNRF@wuerfel> (raw)
In-Reply-To: <alpine.LFD.2.11.1412042056240.470@knanqh.ubzr>

On Thursday 04 December 2014 23:30:08 Nicolas Pitre wrote:
> >          res += (u64)x_lo * y_hi + (u64)x_hi * y_lo;
> 
> That, too, risk overflowing.
> 
> Let's say x_lo = 0xffffffff and x_hi = 0xffffffff.  You get:
> 
>         0xffffffff * 0x83126e97 ->  0x83126e967ced9169
>         0xffffffff * 0x8d4fdf3b ->  0x8d4fdf3a72b020c5
>                                    -------------------
>                                    0x110624dd0ef9db22e
> 
> Therefore the sum doesn't fit into a u64 variable.
> 
> It is possible to skip carry handling but only when the MSB of both 
> constants are zero.  Here it is not the case.

If I understand this right, there are two possible optimizations to
avoid the overflow:

- for anything using monotonic time, or elapsed time, we can guarantee
  that the upper bits are zero. Relying on monotonic time is a bit
  dangerous, because that would mean introducing an API that works
  with ktime_get() but not ktime_get_real(), and risk introducing
  subtle bugs.
  However, ktime_us_delta() could be optimized, and we can introduce
  similar ktime_sec_delta() and ktime_ms_delta() functions with
  the same properties.

- one could always pre-shift the ktime_t value. For a division by
  1000, we can shift right by 3 bits first, then do the multiplication
  and then do another shift. Not sure if that helps at all or if
  the extra shift operation makes this counterproductive.

	Arnd

  reply	other threads:[~2014-12-05 10:08 UTC|newest]

Thread overview: 15+ 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 20:03 ` Arnd Bergmann
2014-12-04  7:23   ` Nicolas Pitre
2014-12-04 12:02     ` Arnd Bergmann
2014-12-04 13:46       ` Nicolas Pitre
2014-12-04 14:56         ` Arnd Bergmann
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 10:08         ` Arnd Bergmann [this message]
2014-12-05 17:15           ` Nicolas Pitre
2014-12-03 20:16 ` Robert Jarzmik
2014-12-03 20:37   ` Nicolas Pitre
2014-12-05 18:00 ` Nicolas Pitre
2014-12-05 21:03   ` Arnd Bergmann
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=2145860.PBxl6kLNRF@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox