Skip to contentSkip to conversation

Fix qdev 32-bit compilation

PATCH Superseded Richard Henderson 1 participant 3 messages Jul 30, 2013

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Fix qdev 32-bit compilation

From: Richard Henderson <[email protected]>

The patch e8cd45c78f53501e75bd455140da63d1b7ed3685 broke compilation on 32-bit with a 40-bit shift of "long int".

The first patch is the trivial fix for that bug.

The second patch is an optional improvement that avoids the division loop entirely.

r~

Richard Henderson (2):
qdev: Fix 32-bit compilation in print_size
qdev: Use clz in print_size

 hw/core/qdev-properties.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/2 qdev: Fix 32-bit compilation in print_size

From: Richard Henderson <[email protected]>

---
 hw/core/qdev-properties.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/core/qdev-properties.c +1 -1
index 8d43a8d..d6d10c9 100644
@@ -1177,7 +1177,7 @@ static int print_size(DeviceState *dev, Property *prop, char *dest, size_t len)
     int i = 0;
     uint64_t div;
 
-    for (div = (long int)1 << 40; !(*ptr / div) ; div >>= 10) {
+    for (div = 1ULL << 40; !(*ptr / div) ; div >>= 10) {
         i++;
     }
     return snprintf(dest, len, "%0.03f%c", (double)*ptr/div, suffixes[i]);
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH 2/2 qdev: Use clz in print_size

In reply to Richard Henderson

From: Richard Henderson <[email protected]>

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

---
 hw/core/qdev-properties.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
hw/core/qdev-properties.c +13 -7
index d6d10c9..dc8ae69 100644
@@ -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 = {
Signature · 1 line
1.8.3.1