All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Maydell <peter.maydell@linaro.org>
To: Richard Henderson <richard.henderson@linaro.org>
Cc: "Damien Hedde" <damien.hedde@greensocs.com>,
	"Aleksandar Rikalo" <aleksandar.rikalo@syrmia.com>,
	"QEMU Developers" <qemu-devel@nongnu.org>,
	"Luc Michel" <luc.michel@greensocs.com>,
	"Philippe Mathieu-Daudé" <f4bug@amsat.org>
Subject: Re: [PATCH 1/4] clock: Introduce clock_ticks_to_ns()
Date: Thu, 10 Dec 2020 20:47:35 +0000	[thread overview]
Message-ID: <CAFEAcA_keq-NV+YNwAyVWbkHxGYFR+Ud3hnh5b1KujpQXh68hw@mail.gmail.com> (raw)
In-Reply-To: <fc447673-dada-5f00-8c52-29d90e6db641@linaro.org>

On Tue, 8 Dec 2020 at 23:39, Richard Henderson
<richard.henderson@linaro.org> wrote:
>
> On 12/8/20 12:15 PM, Peter Maydell wrote:
> > +static inline uint64_t clock_ticks_to_ns(const Clock *clk, uint64_t ticks)
> > +{
> > +    uint64_t ns_low, ns_high;
> > +
> > +    /*
> > +     * clk->period is the period in units of 2^-32 ns, so
> > +     * (clk->period * ticks) is the required length of time in those
> > +     * units, and we can convert to nanoseconds by multiplying by
> > +     * 2^32, which is the same as shifting the 128-bit multiplication
> > +     * result right by 32.
> > +     */
> > +    mulu64(&ns_low, &ns_high, clk->period, ticks);
> > +    return ns_low >> 32 | ns_high << 32;
>
> With the shift, you're discarding the high 32 bits of the result.  You'll lose
> those same bits if you shift one of the inputs left by 32, and use only the
> high part of the result, e.g.
>
>     mulu(&discard, &ret, clk->period, ticks << 32);
>     return ret;
>
> Which on some hosts, e.g. aarch64, only requires umulh and not two multiply
> instructions.

We can't do this if we want to allow a full 64-bit 'ticks' input, right?

> Either way, I wonder if you want to either use uint32_t ticks, or assert that
> ticks <= UINT32_MAX?  Or if you don't shift ticks, assert that ns_high <=
> UINT32_MAX, so that you don't lose output bits?

So I think my plan for v2 of this series is just to add in the
saturation-to-INT64_MAX logic.

thanks
-- PMM


  parent reply	other threads:[~2020-12-10 20:49 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-08 18:15 [PATCH 0/4] clock: Get rid of clock_get_ns() Peter Maydell
2020-12-08 18:15 ` [PATCH 1/4] clock: Introduce clock_ticks_to_ns() Peter Maydell
2020-12-08 23:39   ` Richard Henderson
2020-12-09  8:49     ` Luc Michel
2020-12-09 14:11       ` Richard Henderson
2020-12-09 14:26         ` Peter Maydell
2020-12-09 15:05           ` Richard Henderson
2020-12-10 20:47     ` Peter Maydell [this message]
2020-12-11  1:36       ` Richard Henderson
2020-12-08 18:15 ` [PATCH 2/4] target/mips: Don't use clock_get_ns() in clock period calculation Peter Maydell
2020-12-08 23:41   ` Richard Henderson
2020-12-09  8:50   ` Luc Michel
2020-12-08 18:15 ` [PATCH 3/4] clock: Remove clock_get_ns() Peter Maydell
2020-12-08 23:43   ` Richard Henderson
2020-12-09  8:50   ` Luc Michel
2020-12-08 18:15 ` [PATCH 4/4] clock: Define and use new clock_display_freq() Peter Maydell
2020-12-08 23:50   ` Richard Henderson
2020-12-09  8:50   ` Luc Michel
2020-12-11 13:50 ` [PATCH 0/4] clock: Get rid of clock_get_ns() Philippe Mathieu-Daudé
2020-12-11 14:01   ` Peter Maydell

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=CAFEAcA_keq-NV+YNwAyVWbkHxGYFR+Ud3hnh5b1KujpQXh68hw@mail.gmail.com \
    --to=peter.maydell@linaro.org \
    --cc=aleksandar.rikalo@syrmia.com \
    --cc=damien.hedde@greensocs.com \
    --cc=f4bug@amsat.org \
    --cc=luc.michel@greensocs.com \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.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.