cryptospore / rpms / qemu-kvm

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