yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
0a122b
From 5a3112e24aa41c50703739ccf419bad6136eb797 Mon Sep 17 00:00:00 2001
0a122b
From: Markus Armbruster <armbru@redhat.com>
0a122b
Date: Wed, 22 Jan 2014 12:13:55 +0100
0a122b
Subject: [PATCH 4/4] qdev: Use clz in print_size
0a122b
0a122b
RH-Author: Markus Armbruster <armbru@redhat.com>
0a122b
Message-id: <1390392835-21809-3-git-send-email-armbru@redhat.com>
0a122b
Patchwork-id: 56888
0a122b
O-Subject: [PATCH 7.0 qemu-kvm 2/2] qdev: Use clz in print_size
0a122b
Bugzilla: 1034876
0a122b
RH-Acked-by: Marcel Apfelbaum <marcel.a@redhat.com>
0a122b
RH-Acked-by: Orit Wasserman <owasserm@redhat.com>
0a122b
RH-Acked-by: Dr. David Alan Gilbert (git) <dgilbert@redhat.com>
0a122b
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
0a122b
0a122b
From: Richard Henderson <rth@twiddle.net>
0a122b
0a122b
We can compute a floor log2 value with clz rather than a division loop.
0a122b
0a122b
Signed-off-by: Richard Henderson <rth@twiddle.net>
0a122b
Message-id: 1375208443-17288-3-git-send-email-rth@twiddle.net
0a122b
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
0a122b
(cherry picked from commit 1197cbb9eda1dc82e2fa1815ca62bc3de158353e)
0a122b
Signed-off-by: Markus Armbruster <armbru@redhat.com>
0a122b
---
0a122b
 hw/core/qdev-properties.c | 20 +++++++++++++-------
0a122b
 1 file changed, 13 insertions(+), 7 deletions(-)
0a122b
0a122b
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
0a122b
---
0a122b
 hw/core/qdev-properties.c |   20 +++++++++++++-------
0a122b
 1 files changed, 13 insertions(+), 7 deletions(-)
0a122b
0a122b
diff --git a/hw/core/qdev-properties.c b/hw/core/qdev-properties.c
0a122b
index d6d10c9..dc8ae69 100644
0a122b
--- a/hw/core/qdev-properties.c
0a122b
+++ b/hw/core/qdev-properties.c
0a122b
@@ -1172,15 +1172,21 @@ static int parse_size(DeviceState *dev, Property *prop, const char *str)
0a122b
 
0a122b
 static int print_size(DeviceState *dev, Property *prop, char *dest, size_t len)
0a122b
 {
0a122b
-    uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
0a122b
-    char suffixes[] = {'T', 'G', 'M', 'K', 'B'};
0a122b
-    int i = 0;
0a122b
-    uint64_t div;
0a122b
+    static const char suffixes[] = { 'B', 'K', 'M', 'G', 'T' };
0a122b
+    uint64_t div, val = *(uint64_t *)qdev_get_prop_ptr(dev, prop);
0a122b
+    int i;
0a122b
 
0a122b
-    for (div = 1ULL << 40; !(*ptr / div) ; div >>= 10) {
0a122b
-        i++;
0a122b
+    /* Compute floor(log2(val)).  */
0a122b
+    i = 64 - clz64(val);
0a122b
+
0a122b
+    /* Find the power of 1024 that we'll display as the units.  */
0a122b
+    i /= 10;
0a122b
+    if (i >= ARRAY_SIZE(suffixes)) {
0a122b
+        i = ARRAY_SIZE(suffixes) - 1;
0a122b
     }
0a122b
-    return snprintf(dest, len, "%0.03f%c", (double)*ptr/div, suffixes[i]);
0a122b
+    div = 1ULL << (i * 10);
0a122b
+
0a122b
+    return snprintf(dest, len, "%0.03f%c", (double)val/div, suffixes[i]);
0a122b
 }
0a122b
 
0a122b
 PropertyInfo qdev_prop_size = {
0a122b
-- 
0a122b
1.7.1
0a122b