From: Paul Brook <paul@codesourcery.com>
To: Peter Crosthwaite <peter.crosthwaite@petalogix.com>
Cc: Peter Maydell <peter.maydell@linaro.org>,
monstr@monstr.eu, qemu-devel@nongnu.org,
John Linn <john.linn@xilinx.com>,
duyl@xilinx.com, linnj@xilinx.com, edgar.iglesias@gmail.com,
afaerber@suse.de, john.williams@petalogix.com
Subject: Re: [Qemu-devel] [PATCH v6 2/4] cadence_ttc: initial version of device model
Date: Mon, 27 Feb 2012 15:45:08 +0000 [thread overview]
Message-ID: <201202271545.08365.paul@codesourcery.com> (raw)
In-Reply-To: <CAEgOgz6Ruq+Uxti8zG2qsb5rkrVGuoF0mMC9gJHE54ABSXZNzg@mail.gmail.com>
> On Tue, Feb 21, 2012 at 11:04 PM, Paul Brook <paul@codesourcery.com> wrote:
> >> > +static inline int64_t is_between(int64_t x, int64_t a, int64_t b)
> >> > +{
> >> > + if (a < b) {
> >> > + return x > a && x <= b;
> >> > + }
> >> > + return x < a && x >= b;
> >> > +}
> >>
> >> This looks slightly odd -- should the boundary condition for whether
> >> a value equal to the max/min really change depending on :whether a
> >> or b is greater?
>
> The function determines whether x is in-between a and b exclusive of
> a, inclusive of b, so it is consistent with itself in that regard.
>
> > This is a ugly hack. Instead of figuring out whether we have a count-up
> > or count-down timer the code checks for both, and have the "in_between"
> > function magically DTRT. I haven't followed the paths through in enough
> > detail to figure out whether it gets all the corner cases right.
>
> Is it really a "hack"?? For count up b will always be greater than a,
> and for count down the reverse. I suppose I could assert these
> conditions at the call site for peace of mind? The invocation from
> cadence_timer_run doesn't care whether it is count up of count down,
> it really does just only care if the match value is in-between the
> current timer value and the next timer value, which is exactly what
> this function determines.
When you explain it like this, it makes a more sense. But this isn't
immediately obvious from the code. It took me at least a couple of readings
to figure out what was going on. This is exactly the sort of thing that should
be described in comments. A function with a very generic name is used in a
way that has fairly subtle implications. There's a good chance someone[1]
will come along in a few months/years, reuse this function and "fix" the
wierdness at the same time.
Annother non-obvious detail is the way you handle overflow. Specifically you
check a range both plus and minus the wrap value before wrapping the final
count. This is certainly confusing/surprising when you first encounter it.
Very large steps result in overlapping ranges, which triggers [in this case
harmless] warning bells.
Thinking about that, I realised why I don't like the following line:
> + s->reg_value = (uint32_t)((x + interval) % interval);
This assumes x > -interval, which is not always true.
Paul
[1] "someone" includes me. After I've forgotten this obscure detail.
next prev parent reply other threads:[~2012-02-27 15:45 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-02-20 1:45 [Qemu-devel] [PATCH v6 0/4] Zynq-7000 EPP platform model Peter A. G. Crosthwaite
2012-02-20 1:37 ` Peter Crosthwaite
2012-02-20 1:45 ` [Qemu-devel] [PATCH v6 1/4] cadence_uart: initial version of device model Peter A. G. Crosthwaite
2012-02-20 18:58 ` Peter Maydell
2012-02-23 1:41 ` Peter Crosthwaite
2012-02-20 1:45 ` [Qemu-devel] [PATCH v6 2/4] cadence_ttc: " Peter A. G. Crosthwaite
2012-02-20 19:32 ` Peter Maydell
2012-02-21 13:04 ` Paul Brook
2012-02-23 1:24 ` Peter Crosthwaite
2012-02-27 15:45 ` Paul Brook [this message]
2012-02-28 1:04 ` Peter Crosthwaite
2012-02-28 1:55 ` Peter Crosthwaite
2012-02-28 12:07 ` Paul Brook
2012-02-28 13:42 ` Peter Crosthwaite
2012-02-23 1:42 ` Peter Crosthwaite
2012-02-20 1:45 ` [Qemu-devel] [PATCH v6 3/4] cadence_gem: " Peter A. G. Crosthwaite
2012-02-20 1:45 ` [Qemu-devel] [PATCH v6 4/4] xilinx_zynq: machine model initial version Peter A. G. Crosthwaite
2012-02-20 6:25 ` [Qemu-devel] [PULL] Zynq-7000 EPP platform model Peter Crosthwaite
2012-02-20 7:24 ` Andreas Färber
2012-02-24 16:37 ` Anthony Liguori
2012-02-27 4:31 ` Peter Crosthwaite
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=201202271545.08365.paul@codesourcery.com \
--to=paul@codesourcery.com \
--cc=afaerber@suse.de \
--cc=duyl@xilinx.com \
--cc=edgar.iglesias@gmail.com \
--cc=john.linn@xilinx.com \
--cc=john.williams@petalogix.com \
--cc=linnj@xilinx.com \
--cc=monstr@monstr.eu \
--cc=peter.crosthwaite@petalogix.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.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;
as well as URLs for NNTP newsgroup(s).