Skip to contentSkip to conversation

replace qemu_fls() with pow2ceil()/pow2floor()

PATCH Superseded Peter Maydell 2 participants 12 messages Jul 23, 2015 – Jul 24, 2015

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover replace qemu_fls() with pow2ceil()/pow2floor()

We have a qemu_fls() function which is just a silly wrapper around clz32() and which is used in only a handful of places in the codebase. It turns out that all of those are really trying to round up or down to a power of 2, which is something we have utility functions for. This series replaces all the qemu_fls() calls with pow2ceil() or pow2floor(), and then removes the now-unused function.

For the case where you really want to do bit counting rather than just power-of-2 rounding, you should use the clz/clo functions directly.

(I've set myself a little goal of "try to do one cleanup a day"; that may be a bit ambitious, so we'll see...)

Peter Maydell (5):
hw/pci: Use pow2ceil() rather than hand-calculation hw/virtio/virtio-pci: Use pow2ceil() rather than hand-calculation hw/block/nvme.c: Use pow2ceil() rather than hand-calculation exec.c: Use pow2floor() rather than hand-calculation Remove unused qemu_fls function

 exec.c                 | 4 +---
 hw/block/nvme.c        | 2 +-
 hw/pci/msix.c          | 4 +---
 hw/pci/pci.c           | 4 +---
 hw/virtio/virtio-pci.c | 4 +---
 include/qemu-common.h  | 1 -
 util/cutils.c          | 5 -----
 7 files changed, 5 insertions(+), 19 deletions(-)
Signature · 1 line
1.9.1
#1 lore raw

PATCH 1/5 hw/pci: Use pow2ceil() rather than hand-calculation

A couple of places in hw/pci use an inline calculation to round a size up to the next largest power of 2. We have a utility routine for this, so use it.

(The behaviour of the old code is different if the size value is 0 -- it would leave it as 0 rather than rounding up to 1, but in both cases we know the size can't be 0.
In the case where the size value had bit 31 set, the old code would invoke undefined behaviour; the new code will give a result of 0. Presumably that could never happen either.)

---
 hw/pci/msix.c | 4 +---
 hw/pci/pci.c  | 4 +---
 2 files changed, 2 insertions(+), 6 deletions(-)
hw/pci/msix.c +1 -3
index 7716bf3..2fdada4 100644
@@ -314,9 +314,7 @@ int msix_init_exclusive_bar(PCIDevice *dev, unsigned short nentries,
         bar_size = bar_pba_offset + bar_pba_size;
     }
 
-    if (bar_size & (bar_size - 1)) {
-        bar_size = 1 << qemu_fls(bar_size);
-    }
+    bar_size = pow2ceil(bar_size);
 
     name = g_strdup_printf("%s-msix", dev->name);
     memory_region_init(&dev->msix_exclusive_bar, OBJECT(dev), name, bar_size);
hw/pci/pci.c +1 -3
index a017614..502da8d 100644
@@ -2065,9 +2065,7 @@ static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom,
         g_free(path);
         return;
     }
-    if (size & (size - 1)) {
-        size = 1 << qemu_fls(size);
-    }
+    size = pow2ceil(size);
 
     vmsd = qdev_get_vmsd(DEVICE(pdev));
 
Signature · 1 line
1.9.1
#2 lore raw
In reply to Peter Maydell
Quoted 8 lines · Peter Maydell

We have a qemu_fls() function which is just a silly wrapper around clz32() and which is used in only a handful of places in the codebase. It turns out that all of those are really trying to round up or down to a power of 2, which is something we have utility functions for. This series replaces all the qemu_fls() calls with pow2ceil() or pow2floor(), and then removes the now-unused function.

Quoted 6 lines

For the case where you really want to do bit counting rather than just power-of-2 rounding, you should use the clz/clo functions directly.

(I've set myself a little goal of "try to do one cleanup a day"; that may be a bit ambitious, so we'll see...)

If it turns out to be ambitious, even just "record one cleanup a day in BiteSizedTasks" would be awesome.

Paolo

#3 lore raw

PATCH 2/5 hw/virtio/virtio-pci: Use pow2ceil() rather than hand-calculation

In reply to Peter Maydell

Use the utility function pow2ceil() for rounding up to the next largest power of 2, rather than inline calculation.

---
 hw/virtio/virtio-pci.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
hw/virtio/virtio-pci.c +1 -3
index 283401a..845f52f 100644
@@ -1497,9 +1497,7 @@ static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
     if (legacy) {
         size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev)
             + virtio_bus_get_vdev_config_len(bus);
-        if (size & (size - 1)) {
-            size = 1 << qemu_fls(size);
-        }
+        size = pow2ceil(size);
 
         memory_region_init_io(&proxy->bar, OBJECT(proxy),
                               &virtio_pci_config_ops,
Signature · 1 line
1.9.1
#4 lore raw

PATCH 3/5 hw/block/nvme.c: Use pow2ceil() rather than hand-calculation

In reply to Peter Maydell

Use pow2ceil() to round up to the next power of 2, rather than an inline calculation.

---
 hw/block/nvme.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/block/nvme.c +1 -1
index 40d4880..5da41b2 100644
@@ -805,7 +805,7 @@ static int nvme_init(PCIDevice *pci_dev)
 
     n->num_namespaces = 1;
     n->num_queues = 64;
-    n->reg_size = 1 << qemu_fls(0x1004 + 2 * (n->num_queues + 1) * 4);
+    n->reg_size = pow2ceil(0x1004 + 2 * (n->num_queues + 1) * 4);
     n->ns_size = bs_size / (uint64_t)n->num_namespaces;
 
     n->namespaces = g_new0(NvmeNamespace, n->num_namespaces);
Signature · 1 line
1.9.1
#5 lore raw

PATCH 4/5 exec.c: Use pow2floor() rather than hand-calculation

In reply to Peter Maydell

Use pow2floor() to round down to the nearest power of 2, rather than an inline calculation.

---
 exec.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
exec.c +1 -3
index 7d60e15..4710e2d 100644
@@ -2371,9 +2371,7 @@ static int memory_access_size(MemoryRegion *mr, unsigned l, hwaddr addr)
     if (l > access_size_max) {
         l = access_size_max;
     }
-    if (l & (l - 1)) {
-        l = 1 << (qemu_fls(l) - 1);
-    }
+    l = pow2floor(l);
 
     return l;
 }
Signature · 1 line
1.9.1
#6 lore raw

PATCH 5/5 Remove unused qemu_fls function

In reply to Peter Maydell

Nothing uses qemu_fls() any more, so delete it.

---
 include/qemu-common.h | 1 -
 util/cutils.c         | 5 -----
 2 files changed, 6 deletions(-)
include/qemu-common.h +0 -1
index 237d654..bc6f8f8 100644
@@ -157,7 +157,6 @@ int stristart(const char *str, const char *val, const char **ptr);
 int qemu_strnlen(const char *s, int max_len);
 char *qemu_strsep(char **input, const char *delim);
 time_t mktimegm(struct tm *tm);
-int qemu_fls(int i);
 int qemu_fdatasync(int fd);
 int fcntl_setfl(int fd, int flag);
 int qemu_parse_fd(const char *param);
util/cutils.c +0 -5
index 5d1c9eb..43aafde 100644
@@ -145,11 +145,6 @@ time_t mktimegm(struct tm *tm)
     return t;
 }
 
-int qemu_fls(int i)
-{
-    return 32 - clz32(i);
-}
-
 /*
  * Make sure data goes on disk, but if possible do not bother to
  * write out the inode just for timestamp updates.
Signature · 1 line
1.9.1
#7 lore raw
In reply to Peter Maydell
Quoted 7 lines · Peter Maydell

We have a qemu_fls() function which is just a silly wrapper around clz32() and which is used in only a handful of places in the codebase. It turns out that all of those are really trying to round up or down to a power of 2, which is something we have utility functions for. This series replaces all the qemu_fls() calls with pow2ceil() or pow2floor(), and then removes the now-unused function.

The series looks good, but I'd prefer (especially for patch 4 which is in a fast path) if pow2ceil and pow2floor were made inline.

BTW,

    return 1ULL << (64 - nlz);

can be implemented as "rotate_right(1ULL, nlz)" (the latter is (1ULL << (64 - nlz)) | (1ULL >> nlz), which only differs in the undefined case nlz == 0). However, this is probably something for the compiler to implement, not really something to do in our sources.

Paolo

Quoted 23 lines

For the case where you really want to do bit counting rather than just power-of-2 rounding, you should use the clz/clo functions directly.

(I've set myself a little goal of "try to do one cleanup a day"; that may be a bit ambitious, so we'll see...)

Peter Maydell (5):
hw/pci: Use pow2ceil() rather than hand-calculation hw/virtio/virtio-pci: Use pow2ceil() rather than hand-calculation hw/block/nvme.c: Use pow2ceil() rather than hand-calculation exec.c: Use pow2floor() rather than hand-calculation Remove unused qemu_fls function

 exec.c                 | 4 +---
 hw/block/nvme.c        | 2 +-
 hw/pci/msix.c          | 4 +---
 hw/pci/pci.c           | 4 +---
 hw/virtio/virtio-pci.c | 4 +---
 include/qemu-common.h  | 1 -
 util/cutils.c          | 5 -----
 7 files changed, 5 insertions(+), 19 deletions(-)
#8 lore raw
Quoted 11 lines · Paolo Bonzini
Quoted 7 lines · Peter Maydell

We have a qemu_fls() function which is just a silly wrapper around clz32() and which is used in only a handful of places in the codebase. It turns out that all of those are really trying to round up or down to a power of 2, which is something we have utility functions for. This series replaces all the qemu_fls() calls with pow2ceil() or pow2floor(), and then removes the now-unused function.

Quoted 2 lines

The series looks good, but I'd prefer (especially for patch 4 which is in a fast path) if pow2ceil and pow2floor were made inline.

Yeah, I was wondering if I was going to be asked to do that. Note that qemu_fls() is/was *not* inline, though, so you're taking an out-of-line function call on these code paths already.

-- PMM

#9 lore raw
Quoted 6 lines · Peter Maydell
Quoted 2 lines
Quoted 2 lines

The series looks good, but I'd prefer (especially for patch 4 which is in a fast path) if pow2ceil and pow2floor were made inline.

Yeah, I was wondering if I was going to be asked to do that. Note that qemu_fls() is/was *not* inline, though, so you're taking an out-of-line function call on these code paths already.

But in patch 4 the call is only in the rare case where "l & (l - 1)" is not zero.

Paolo

#10 lore raw
Quoted 5 lines · Paolo Bonzini
Quoted 2 lines · Peter Maydell
Quoted 2 lines
Quoted 2 lines

The series looks good, but I'd prefer (especially for patch 4 which is in a fast path) if pow2ceil and pow2floor were made inline.

Quoted 7 lines
Quoted 4 lines

Yeah, I was wondering if I was going to be asked to do that. Note that qemu_fls() is/was *not* inline, though, so you're taking an out-of-line function call on these code paths already.

But in patch 4 the call is only in the rare case where "l & (l - 1)" is not zero.

True. Any preferences for which header file to put them in? qemu-common.h is where the prototype is currently and where the inline is_power_of_2() is defined...

thanks
-- PMM

#11 lore raw
Quoted 5 lines · Peter Maydell
Quoted 2 lines
Quoted 2 lines

But in patch 4 the call is only in the rare case where "l & (l - 1)" is not zero.

True. Any preferences for which header file to put them in? qemu-common.h is where the prototype is currently and where the inline is_power_of_2() is defined...

Either there, or host-utils.h.

Paolo