qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Richard Henderson <rth@twiddle.net>
To: qemu-devel@nongnu.org
Cc: vasilis.liaskovitis@profitbricks.com, imammedo@redhat.com,
	aliguori@us.ibm.com, Richard Henderson <rth@grit.twiddle.net>,
	afaerber@suse.de
Subject: [Qemu-devel] [PATCH 2/2] qdev: Use clz in print_size
Date: Tue, 30 Jul 2013 08:18:34 -1000	[thread overview]
Message-ID: <1375208314-17233-3-git-send-email-rth@twiddle.net> (raw)
In-Reply-To: <1375208314-17233-1-git-send-email-rth@twiddle.net>

From: Richard Henderson <rth@grit.twiddle.net>

We can compute a floor log2 value with clz rather than a division loop.

Signed-off-by: Richard Henderson <rth@grit.twiddle.net>
---
 hw/core/qdev-properties.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/hw/core/qdev-properties.c b/hw/core/qdev-properties.c
index d6d10c9..dc8ae69 100644
--- a/hw/core/qdev-properties.c
+++ b/hw/core/qdev-properties.c
@@ -1172,15 +1172,21 @@ static int parse_size(DeviceState *dev, Property *prop, const char *str)
 
 static int print_size(DeviceState *dev, Property *prop, char *dest, size_t len)
 {
-    uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
-    char suffixes[] = {'T', 'G', 'M', 'K', 'B'};
-    int i = 0;
-    uint64_t div;
+    static const char suffixes[] = { 'B', 'K', 'M', 'G', 'T' };
+    uint64_t div, val = *(uint64_t *)qdev_get_prop_ptr(dev, prop);
+    int i;
 
-    for (div = 1ULL << 40; !(*ptr / div) ; div >>= 10) {
-        i++;
+    /* Compute floor(log2(val)).  */
+    i = 64 - clz64(val);
+
+    /* Find the power of 1024 that we'll display as the units.  */
+    i /= 10;
+    if (i >= ARRAY_SIZE(suffixes)) {
+        i = ARRAY_SIZE(suffixes) - 1;
     }
-    return snprintf(dest, len, "%0.03f%c", (double)*ptr/div, suffixes[i]);
+    div = 1ULL << (i * 10);
+
+    return snprintf(dest, len, "%0.03f%c", (double)val/div, suffixes[i]);
 }
 
 PropertyInfo qdev_prop_size = {
-- 
1.8.3.1

      parent reply	other threads:[~2013-07-30 18:19 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-30 18:18 [Qemu-devel] [PATCH 0/2] Fix qdev 32-bit compilation Richard Henderson
2013-07-30 18:18 ` [Qemu-devel] [PATCH 1/2] qdev: Fix 32-bit compilation in print_size Richard Henderson
2013-07-30 18:18 ` Richard Henderson [this message]

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=1375208314-17233-3-git-send-email-rth@twiddle.net \
    --to=rth@twiddle.net \
    --cc=afaerber@suse.de \
    --cc=aliguori@us.ibm.com \
    --cc=imammedo@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=rth@grit.twiddle.net \
    --cc=vasilis.liaskovitis@profitbricks.com \
    /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).