qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paul Brook <paul@nowt.org>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [6961] Avoid rounding problems in ptimer_get_count
Date: Tue, 31 Mar 2009 14:34:24 +0000	[thread overview]
Message-ID: <E1Lof2m-0002zo-MG@cvs.savannah.gnu.org> (raw)

Revision: 6961
          http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=6961
Author:   pbrook
Date:     2009-03-31 14:34:24 +0000 (Tue, 31 Mar 2009)
Log Message:
-----------
Avoid rounding problems in ptimer_get_count

Signed-off-by: Paul Brook <paul@codesourcery.com>

Modified Paths:
--------------
    trunk/hw/ptimer.c

Modified: trunk/hw/ptimer.c
===================================================================
--- trunk/hw/ptimer.c	2009-03-31 05:55:16 UTC (rev 6960)
+++ trunk/hw/ptimer.c	2009-03-31 14:34:24 UTC (rev 6961)
@@ -7,8 +7,8 @@
  */
 #include "hw.h"
 #include "qemu-timer.h"
+#include "host-utils.h"
 
-
 struct ptimer_state
 {
     int enabled; /* 0 = disabled, 1 = periodic, 2 = oneshot.  */
@@ -78,9 +78,39 @@
         } else {
             uint64_t rem;
             uint64_t div;
+            uint32_t frac;
+            int clz1, clz2;
+            int shift;
 
+            /* We need to divide time by period, where time is stored in
+               rem (64-bit integer) and period is stored in period/period_frac
+               (64.32 fixed point).
+              
+               Doing full precision division is hard, so scale values and
+               do a 64-bit division.  The result should be rounded down,
+               so that the rounding error never causes the timer to go
+               backwards.
+            */
+
             rem = s->next_event - now;
             div = s->period;
+
+            clz1 = clz64(rem);
+            clz2 = clz64(div);
+            shift = clz1 < clz2 ? clz1 : clz2;
+
+            rem <<= shift;
+            div <<= shift;
+            if (shift >= 32) {
+                div |= ((uint64_t)s->period_frac << (shift - 32));
+            } else {
+                if (shift != 0)
+                    div |= (s->period_frac >> (32 - shift));
+                /* Look at remaining bits of period_frac and round div up if 
+                   necessary.  */
+                if ((uint32_t)(s->period_frac << shift))
+                    div += 1;
+            }
             counter = rem / div;
         }
     } else {

                 reply	other threads:[~2009-03-31 14:34 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=E1Lof2m-0002zo-MG@cvs.savannah.gnu.org \
    --to=paul@nowt.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).