* [Qemu-devel] [PATCH v2 0/6] replace qemu_fls() with pow2ceil()/pow2floor()
@ 2015-07-24 12:33 Peter Maydell
2015-07-24 12:33 ` [Qemu-devel] [PATCH v2 1/6] hw/pci: Use pow2ceil() rather than hand-calculation Peter Maydell
` (7 more replies)
0 siblings, 8 replies; 17+ messages in thread
From: Peter Maydell @ 2015-07-24 12:33 UTC (permalink / raw)
To: qemu-devel
Cc: Kevin Wolf, qemu-block, Michael S. Tsirkin, patches, Keith Busch,
Paolo Bonzini
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.
No changes from v1 to v2 except for a new patch 6 which moves
the pow2ceil and pow2floor functions to inline.
Peter Maydell (6):
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
Make pow2ceil() and pow2floor() inline
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 | 17 +----------------
include/qemu/host-utils.h | 33 +++++++++++++++++++++++++++++++++
util/cutils.c | 28 ----------------------------
8 files changed, 39 insertions(+), 57 deletions(-)
--
1.9.1
^ permalink raw reply [flat|nested] 17+ messages in thread
* [Qemu-devel] [PATCH v2 1/6] hw/pci: Use pow2ceil() rather than hand-calculation
2015-07-24 12:33 [Qemu-devel] [PATCH v2 0/6] replace qemu_fls() with pow2ceil()/pow2floor() Peter Maydell
@ 2015-07-24 12:33 ` Peter Maydell
2015-08-12 7:35 ` Michael S. Tsirkin
2015-07-24 12:33 ` [Qemu-devel] [PATCH v2 2/6] hw/virtio/virtio-pci: " Peter Maydell
` (6 subsequent siblings)
7 siblings, 1 reply; 17+ messages in thread
From: Peter Maydell @ 2015-07-24 12:33 UTC (permalink / raw)
To: qemu-devel
Cc: Kevin Wolf, qemu-block, Michael S. Tsirkin, patches, Keith Busch,
Paolo Bonzini
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.)
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
hw/pci/msix.c | 4 +---
hw/pci/pci.c | 4 +---
2 files changed, 2 insertions(+), 6 deletions(-)
diff --git a/hw/pci/msix.c b/hw/pci/msix.c
index 7716bf3..2fdada4 100644
--- a/hw/pci/msix.c
+++ b/hw/pci/msix.c
@@ -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);
diff --git a/hw/pci/pci.c b/hw/pci/pci.c
index a017614..502da8d 100644
--- a/hw/pci/pci.c
+++ b/hw/pci/pci.c
@@ -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));
--
1.9.1
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [Qemu-devel] [PATCH v2 2/6] hw/virtio/virtio-pci: Use pow2ceil() rather than hand-calculation
2015-07-24 12:33 [Qemu-devel] [PATCH v2 0/6] replace qemu_fls() with pow2ceil()/pow2floor() Peter Maydell
2015-07-24 12:33 ` [Qemu-devel] [PATCH v2 1/6] hw/pci: Use pow2ceil() rather than hand-calculation Peter Maydell
@ 2015-07-24 12:33 ` Peter Maydell
2015-08-12 7:36 ` Michael S. Tsirkin
2015-07-24 12:33 ` [Qemu-devel] [PATCH v2 3/6] hw/block/nvme.c: " Peter Maydell
` (5 subsequent siblings)
7 siblings, 1 reply; 17+ messages in thread
From: Peter Maydell @ 2015-07-24 12:33 UTC (permalink / raw)
To: qemu-devel
Cc: Kevin Wolf, qemu-block, Michael S. Tsirkin, patches, Keith Busch,
Paolo Bonzini
Use the utility function pow2ceil() for rounding up to the next
largest power of 2, rather than inline calculation.
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
hw/virtio/virtio-pci.c | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c
index 283401a..845f52f 100644
--- a/hw/virtio/virtio-pci.c
+++ b/hw/virtio/virtio-pci.c
@@ -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,
--
1.9.1
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [Qemu-devel] [PATCH v2 3/6] hw/block/nvme.c: Use pow2ceil() rather than hand-calculation
2015-07-24 12:33 [Qemu-devel] [PATCH v2 0/6] replace qemu_fls() with pow2ceil()/pow2floor() Peter Maydell
2015-07-24 12:33 ` [Qemu-devel] [PATCH v2 1/6] hw/pci: Use pow2ceil() rather than hand-calculation Peter Maydell
2015-07-24 12:33 ` [Qemu-devel] [PATCH v2 2/6] hw/virtio/virtio-pci: " Peter Maydell
@ 2015-07-24 12:33 ` Peter Maydell
2015-08-14 23:41 ` Paolo Bonzini
2015-07-24 12:33 ` [Qemu-devel] [PATCH v2 4/6] exec.c: Use pow2floor() " Peter Maydell
` (4 subsequent siblings)
7 siblings, 1 reply; 17+ messages in thread
From: Peter Maydell @ 2015-07-24 12:33 UTC (permalink / raw)
To: qemu-devel
Cc: Kevin Wolf, qemu-block, Michael S. Tsirkin, patches, Keith Busch,
Paolo Bonzini
Use pow2ceil() to round up to the next power of 2, rather
than an inline calculation.
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
hw/block/nvme.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/hw/block/nvme.c b/hw/block/nvme.c
index 40d4880..5da41b2 100644
--- a/hw/block/nvme.c
+++ b/hw/block/nvme.c
@@ -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);
--
1.9.1
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [Qemu-devel] [PATCH v2 4/6] exec.c: Use pow2floor() rather than hand-calculation
2015-07-24 12:33 [Qemu-devel] [PATCH v2 0/6] replace qemu_fls() with pow2ceil()/pow2floor() Peter Maydell
` (2 preceding siblings ...)
2015-07-24 12:33 ` [Qemu-devel] [PATCH v2 3/6] hw/block/nvme.c: " Peter Maydell
@ 2015-07-24 12:33 ` Peter Maydell
2015-08-14 23:41 ` Paolo Bonzini
2015-07-24 12:33 ` [Qemu-devel] [PATCH v2 5/6] Remove unused qemu_fls function Peter Maydell
` (3 subsequent siblings)
7 siblings, 1 reply; 17+ messages in thread
From: Peter Maydell @ 2015-07-24 12:33 UTC (permalink / raw)
To: qemu-devel
Cc: Kevin Wolf, qemu-block, Michael S. Tsirkin, patches, Keith Busch,
Paolo Bonzini
Use pow2floor() to round down to the nearest power of 2,
rather than an inline calculation.
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
exec.c | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/exec.c b/exec.c
index 7d60e15..4710e2d 100644
--- a/exec.c
+++ b/exec.c
@@ -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;
}
--
1.9.1
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [Qemu-devel] [PATCH v2 5/6] Remove unused qemu_fls function
2015-07-24 12:33 [Qemu-devel] [PATCH v2 0/6] replace qemu_fls() with pow2ceil()/pow2floor() Peter Maydell
` (3 preceding siblings ...)
2015-07-24 12:33 ` [Qemu-devel] [PATCH v2 4/6] exec.c: Use pow2floor() " Peter Maydell
@ 2015-07-24 12:33 ` Peter Maydell
2015-08-14 23:41 ` Paolo Bonzini
2015-07-24 12:33 ` [Qemu-devel] [PATCH v2 6/6] Make pow2ceil() and pow2floor() inline Peter Maydell
` (2 subsequent siblings)
7 siblings, 1 reply; 17+ messages in thread
From: Peter Maydell @ 2015-07-24 12:33 UTC (permalink / raw)
To: qemu-devel
Cc: Kevin Wolf, qemu-block, Michael S. Tsirkin, patches, Keith Busch,
Paolo Bonzini
Nothing uses qemu_fls() any more, so delete it.
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
include/qemu-common.h | 1 -
util/cutils.c | 5 -----
2 files changed, 6 deletions(-)
diff --git a/include/qemu-common.h b/include/qemu-common.h
index 237d654..bc6f8f8 100644
--- a/include/qemu-common.h
+++ b/include/qemu-common.h
@@ -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);
diff --git a/util/cutils.c b/util/cutils.c
index 5d1c9eb..43aafde 100644
--- a/util/cutils.c
+++ b/util/cutils.c
@@ -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.
--
1.9.1
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [Qemu-devel] [PATCH v2 6/6] Make pow2ceil() and pow2floor() inline
2015-07-24 12:33 [Qemu-devel] [PATCH v2 0/6] replace qemu_fls() with pow2ceil()/pow2floor() Peter Maydell
` (4 preceding siblings ...)
2015-07-24 12:33 ` [Qemu-devel] [PATCH v2 5/6] Remove unused qemu_fls function Peter Maydell
@ 2015-07-24 12:33 ` Peter Maydell
2015-08-14 23:42 ` Paolo Bonzini
2015-08-14 10:11 ` [Qemu-devel] [PATCH v2 0/6] replace qemu_fls() with pow2ceil()/pow2floor() Peter Maydell
2015-09-07 11:04 ` Peter Maydell
7 siblings, 1 reply; 17+ messages in thread
From: Peter Maydell @ 2015-07-24 12:33 UTC (permalink / raw)
To: qemu-devel
Cc: Kevin Wolf, qemu-block, Michael S. Tsirkin, patches, Keith Busch,
Paolo Bonzini
Since the pow2floor() function is now used in a hot code path,
make it inline; for consistency, provide pow2ceil() as an inline
function too.
Because these functions use ctz64() we have to put the inline
versions into host-utils.h, so they have access to ctz64(),
and move the inline is_power_of_2() along with them.
We then need to include host-utils.h from qemu-common.h so that
the files which use these functions via qemu-common.h still have
access to them.
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
include/qemu-common.h | 16 +---------------
include/qemu/host-utils.h | 33 +++++++++++++++++++++++++++++++++
util/cutils.c | 23 -----------------------
3 files changed, 34 insertions(+), 38 deletions(-)
diff --git a/include/qemu-common.h b/include/qemu-common.h
index bc6f8f8..3d4279c 100644
--- a/include/qemu-common.h
+++ b/include/qemu-common.h
@@ -43,6 +43,7 @@
#include <signal.h>
#include "glib-compat.h"
#include "qemu/option.h"
+#include "qemu/host-utils.h"
#ifdef _WIN32
#include "sysemu/os-win32.h"
@@ -405,21 +406,6 @@ static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
/* Round number up to multiple */
#define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m))
-static inline bool is_power_of_2(uint64_t value)
-{
- if (!value) {
- return 0;
- }
-
- return !(value & (value - 1));
-}
-
-/* round down to the nearest power of 2*/
-int64_t pow2floor(int64_t value);
-
-/* round up to the nearest power of 2 (0 if overflow) */
-uint64_t pow2ceil(uint64_t value);
-
#include "qemu/module.h"
/*
diff --git a/include/qemu/host-utils.h b/include/qemu/host-utils.h
index d4f21c9..9ed5cdd 100644
--- a/include/qemu/host-utils.h
+++ b/include/qemu/host-utils.h
@@ -27,6 +27,7 @@
#include "qemu/compiler.h" /* QEMU_GNUC_PREREQ */
#include <limits.h>
+#include <stdbool.h>
#ifdef CONFIG_INT128
static inline void mulu64(uint64_t *plow, uint64_t *phigh,
@@ -379,4 +380,36 @@ static inline int ctpop64(uint64_t val)
# error Unknown sizeof long
#endif
+static inline bool is_power_of_2(uint64_t value)
+{
+ if (!value) {
+ return 0;
+ }
+
+ return !(value & (value - 1));
+}
+
+/* round down to the nearest power of 2*/
+static inline int64_t pow2floor(int64_t value)
+{
+ if (!is_power_of_2(value)) {
+ value = 0x8000000000000000ULL >> clz64(value);
+ }
+ return value;
+}
+
+/* round up to the nearest power of 2 (0 if overflow) */
+static inline uint64_t pow2ceil(uint64_t value)
+{
+ uint8_t nlz = clz64(value);
+
+ if (is_power_of_2(value)) {
+ return value;
+ }
+ if (!nlz) {
+ return 0;
+ }
+ return 1ULL << (64 - nlz);
+}
+
#endif
diff --git a/util/cutils.c b/util/cutils.c
index 43aafde..9234452 100644
--- a/util/cutils.c
+++ b/util/cutils.c
@@ -469,29 +469,6 @@ int qemu_parse_fd(const char *param)
return fd;
}
-/* round down to the nearest power of 2*/
-int64_t pow2floor(int64_t value)
-{
- if (!is_power_of_2(value)) {
- value = 0x8000000000000000ULL >> clz64(value);
- }
- return value;
-}
-
-/* round up to the nearest power of 2 (0 if overflow) */
-uint64_t pow2ceil(uint64_t value)
-{
- uint8_t nlz = clz64(value);
-
- if (is_power_of_2(value)) {
- return value;
- }
- if (!nlz) {
- return 0;
- }
- return 1ULL << (64 - nlz);
-}
-
/*
* Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
* Input is limited to 14-bit numbers
--
1.9.1
^ permalink raw reply related [flat|nested] 17+ messages in thread
* Re: [Qemu-devel] [PATCH v2 1/6] hw/pci: Use pow2ceil() rather than hand-calculation
2015-07-24 12:33 ` [Qemu-devel] [PATCH v2 1/6] hw/pci: Use pow2ceil() rather than hand-calculation Peter Maydell
@ 2015-08-12 7:35 ` Michael S. Tsirkin
0 siblings, 0 replies; 17+ messages in thread
From: Michael S. Tsirkin @ 2015-08-12 7:35 UTC (permalink / raw)
To: Peter Maydell
Cc: Kevin Wolf, qemu-block, patches, qemu-devel, Keith Busch,
Paolo Bonzini
On Fri, Jul 24, 2015 at 01:33:07PM +0100, Peter Maydell wrote:
> 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.)
>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
> ---
> hw/pci/msix.c | 4 +---
> hw/pci/pci.c | 4 +---
> 2 files changed, 2 insertions(+), 6 deletions(-)
>
> diff --git a/hw/pci/msix.c b/hw/pci/msix.c
> index 7716bf3..2fdada4 100644
> --- a/hw/pci/msix.c
> +++ b/hw/pci/msix.c
> @@ -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);
> diff --git a/hw/pci/pci.c b/hw/pci/pci.c
> index a017614..502da8d 100644
> --- a/hw/pci/pci.c
> +++ b/hw/pci/pci.c
> @@ -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));
>
> --
> 1.9.1
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [Qemu-devel] [PATCH v2 2/6] hw/virtio/virtio-pci: Use pow2ceil() rather than hand-calculation
2015-07-24 12:33 ` [Qemu-devel] [PATCH v2 2/6] hw/virtio/virtio-pci: " Peter Maydell
@ 2015-08-12 7:36 ` Michael S. Tsirkin
0 siblings, 0 replies; 17+ messages in thread
From: Michael S. Tsirkin @ 2015-08-12 7:36 UTC (permalink / raw)
To: Peter Maydell
Cc: Kevin Wolf, qemu-block, patches, qemu-devel, Keith Busch,
Paolo Bonzini
On Fri, Jul 24, 2015 at 01:33:08PM +0100, Peter Maydell wrote:
> Use the utility function pow2ceil() for rounding up to the next
> largest power of 2, rather than inline calculation.
>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
> ---
> hw/virtio/virtio-pci.c | 4 +---
> 1 file changed, 1 insertion(+), 3 deletions(-)
>
> diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c
> index 283401a..845f52f 100644
> --- a/hw/virtio/virtio-pci.c
> +++ b/hw/virtio/virtio-pci.c
> @@ -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,
> --
> 1.9.1
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [Qemu-devel] [PATCH v2 0/6] replace qemu_fls() with pow2ceil()/pow2floor()
2015-07-24 12:33 [Qemu-devel] [PATCH v2 0/6] replace qemu_fls() with pow2ceil()/pow2floor() Peter Maydell
` (5 preceding siblings ...)
2015-07-24 12:33 ` [Qemu-devel] [PATCH v2 6/6] Make pow2ceil() and pow2floor() inline Peter Maydell
@ 2015-08-14 10:11 ` Peter Maydell
2015-09-07 11:04 ` Peter Maydell
7 siblings, 0 replies; 17+ messages in thread
From: Peter Maydell @ 2015-08-14 10:11 UTC (permalink / raw)
To: QEMU Developers
Cc: Kevin Wolf, Qemu-block, Patch Tracking, Michael S. Tsirkin,
Keith Busch, Paolo Bonzini
Ping?
(Patches 1 and 2 have been reviewed; thanks.)
-- PMM
On 24 July 2015 at 13:33, Peter Maydell <peter.maydell@linaro.org> wrote:
> 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.
>
> No changes from v1 to v2 except for a new patch 6 which moves
> the pow2ceil and pow2floor functions to inline.
>
> Peter Maydell (6):
> 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
> Make pow2ceil() and pow2floor() inline
>
> 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 | 17 +----------------
> include/qemu/host-utils.h | 33 +++++++++++++++++++++++++++++++++
> util/cutils.c | 28 ----------------------------
> 8 files changed, 39 insertions(+), 57 deletions(-)
>
> --
> 1.9.1
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [Qemu-devel] [PATCH v2 3/6] hw/block/nvme.c: Use pow2ceil() rather than hand-calculation
2015-07-24 12:33 ` [Qemu-devel] [PATCH v2 3/6] hw/block/nvme.c: " Peter Maydell
@ 2015-08-14 23:41 ` Paolo Bonzini
0 siblings, 0 replies; 17+ messages in thread
From: Paolo Bonzini @ 2015-08-14 23:41 UTC (permalink / raw)
To: Peter Maydell, qemu-devel
Cc: Kevin Wolf, Keith Busch, patches, qemu-block, Michael S. Tsirkin
On 24/07/2015 14:33, Peter Maydell wrote:
> Use pow2ceil() to round up to the next power of 2, rather
> than an inline calculation.
>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
> hw/block/nvme.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/hw/block/nvme.c b/hw/block/nvme.c
> index 40d4880..5da41b2 100644
> --- a/hw/block/nvme.c
> +++ b/hw/block/nvme.c
> @@ -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);
The pre-patch version only worked because the expression was never a
power of two. Won't miss the cleverness. :)
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
> n->ns_size = bs_size / (uint64_t)n->num_namespaces;
>
> n->namespaces = g_new0(NvmeNamespace, n->num_namespaces);
>
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [Qemu-devel] [PATCH v2 4/6] exec.c: Use pow2floor() rather than hand-calculation
2015-07-24 12:33 ` [Qemu-devel] [PATCH v2 4/6] exec.c: Use pow2floor() " Peter Maydell
@ 2015-08-14 23:41 ` Paolo Bonzini
0 siblings, 0 replies; 17+ messages in thread
From: Paolo Bonzini @ 2015-08-14 23:41 UTC (permalink / raw)
To: Peter Maydell, qemu-devel
Cc: Kevin Wolf, Keith Busch, patches, qemu-block, Michael S. Tsirkin
On 24/07/2015 14:33, Peter Maydell wrote:
> Use pow2floor() to round down to the nearest power of 2,
> rather than an inline calculation.
>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
> exec.c | 4 +---
> 1 file changed, 1 insertion(+), 3 deletions(-)
>
> diff --git a/exec.c b/exec.c
> index 7d60e15..4710e2d 100644
> --- a/exec.c
> +++ b/exec.c
> @@ -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);
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
> return l;
> }
>
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [Qemu-devel] [PATCH v2 5/6] Remove unused qemu_fls function
2015-07-24 12:33 ` [Qemu-devel] [PATCH v2 5/6] Remove unused qemu_fls function Peter Maydell
@ 2015-08-14 23:41 ` Paolo Bonzini
0 siblings, 0 replies; 17+ messages in thread
From: Paolo Bonzini @ 2015-08-14 23:41 UTC (permalink / raw)
To: Peter Maydell, qemu-devel
Cc: Kevin Wolf, Keith Busch, patches, qemu-block, Michael S. Tsirkin
On 24/07/2015 14:33, Peter Maydell wrote:
> Nothing uses qemu_fls() any more, so delete it.
>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
> include/qemu-common.h | 1 -
> util/cutils.c | 5 -----
> 2 files changed, 6 deletions(-)
>
> diff --git a/include/qemu-common.h b/include/qemu-common.h
> index 237d654..bc6f8f8 100644
> --- a/include/qemu-common.h
> +++ b/include/qemu-common.h
> @@ -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);
> diff --git a/util/cutils.c b/util/cutils.c
> index 5d1c9eb..43aafde 100644
> --- a/util/cutils.c
> +++ b/util/cutils.c
> @@ -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.
>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [Qemu-devel] [PATCH v2 6/6] Make pow2ceil() and pow2floor() inline
2015-07-24 12:33 ` [Qemu-devel] [PATCH v2 6/6] Make pow2ceil() and pow2floor() inline Peter Maydell
@ 2015-08-14 23:42 ` Paolo Bonzini
0 siblings, 0 replies; 17+ messages in thread
From: Paolo Bonzini @ 2015-08-14 23:42 UTC (permalink / raw)
To: Peter Maydell, qemu-devel
Cc: Kevin Wolf, Keith Busch, patches, qemu-block, Michael S. Tsirkin
On 24/07/2015 14:33, Peter Maydell wrote:
> Since the pow2floor() function is now used in a hot code path,
> make it inline; for consistency, provide pow2ceil() as an inline
> function too.
>
> Because these functions use ctz64() we have to put the inline
> versions into host-utils.h, so they have access to ctz64(),
> and move the inline is_power_of_2() along with them.
>
> We then need to include host-utils.h from qemu-common.h so that
> the files which use these functions via qemu-common.h still have
> access to them.
>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
> include/qemu-common.h | 16 +---------------
> include/qemu/host-utils.h | 33 +++++++++++++++++++++++++++++++++
> util/cutils.c | 23 -----------------------
> 3 files changed, 34 insertions(+), 38 deletions(-)
>
> diff --git a/include/qemu-common.h b/include/qemu-common.h
> index bc6f8f8..3d4279c 100644
> --- a/include/qemu-common.h
> +++ b/include/qemu-common.h
> @@ -43,6 +43,7 @@
> #include <signal.h>
> #include "glib-compat.h"
> #include "qemu/option.h"
> +#include "qemu/host-utils.h"
>
> #ifdef _WIN32
> #include "sysemu/os-win32.h"
> @@ -405,21 +406,6 @@ static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
> /* Round number up to multiple */
> #define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m))
>
> -static inline bool is_power_of_2(uint64_t value)
> -{
> - if (!value) {
> - return 0;
> - }
> -
> - return !(value & (value - 1));
> -}
> -
> -/* round down to the nearest power of 2*/
> -int64_t pow2floor(int64_t value);
> -
> -/* round up to the nearest power of 2 (0 if overflow) */
> -uint64_t pow2ceil(uint64_t value);
> -
> #include "qemu/module.h"
>
> /*
> diff --git a/include/qemu/host-utils.h b/include/qemu/host-utils.h
> index d4f21c9..9ed5cdd 100644
> --- a/include/qemu/host-utils.h
> +++ b/include/qemu/host-utils.h
> @@ -27,6 +27,7 @@
>
> #include "qemu/compiler.h" /* QEMU_GNUC_PREREQ */
> #include <limits.h>
> +#include <stdbool.h>
>
> #ifdef CONFIG_INT128
> static inline void mulu64(uint64_t *plow, uint64_t *phigh,
> @@ -379,4 +380,36 @@ static inline int ctpop64(uint64_t val)
> # error Unknown sizeof long
> #endif
>
> +static inline bool is_power_of_2(uint64_t value)
> +{
> + if (!value) {
> + return 0;
> + }
> +
> + return !(value & (value - 1));
> +}
> +
> +/* round down to the nearest power of 2*/
> +static inline int64_t pow2floor(int64_t value)
> +{
> + if (!is_power_of_2(value)) {
> + value = 0x8000000000000000ULL >> clz64(value);
> + }
> + return value;
> +}
> +
> +/* round up to the nearest power of 2 (0 if overflow) */
> +static inline uint64_t pow2ceil(uint64_t value)
> +{
> + uint8_t nlz = clz64(value);
> +
> + if (is_power_of_2(value)) {
> + return value;
> + }
> + if (!nlz) {
> + return 0;
> + }
> + return 1ULL << (64 - nlz);
> +}
> +
> #endif
> diff --git a/util/cutils.c b/util/cutils.c
> index 43aafde..9234452 100644
> --- a/util/cutils.c
> +++ b/util/cutils.c
> @@ -469,29 +469,6 @@ int qemu_parse_fd(const char *param)
> return fd;
> }
>
> -/* round down to the nearest power of 2*/
> -int64_t pow2floor(int64_t value)
> -{
> - if (!is_power_of_2(value)) {
> - value = 0x8000000000000000ULL >> clz64(value);
> - }
> - return value;
> -}
> -
> -/* round up to the nearest power of 2 (0 if overflow) */
> -uint64_t pow2ceil(uint64_t value)
> -{
> - uint8_t nlz = clz64(value);
> -
> - if (is_power_of_2(value)) {
> - return value;
> - }
> - if (!nlz) {
> - return 0;
> - }
> - return 1ULL << (64 - nlz);
> -}
> -
> /*
> * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
> * Input is limited to 14-bit numbers
>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [Qemu-devel] [PATCH v2 0/6] replace qemu_fls() with pow2ceil()/pow2floor()
2015-07-24 12:33 [Qemu-devel] [PATCH v2 0/6] replace qemu_fls() with pow2ceil()/pow2floor() Peter Maydell
` (6 preceding siblings ...)
2015-08-14 10:11 ` [Qemu-devel] [PATCH v2 0/6] replace qemu_fls() with pow2ceil()/pow2floor() Peter Maydell
@ 2015-09-07 11:04 ` Peter Maydell
2015-09-07 12:34 ` Paolo Bonzini
7 siblings, 1 reply; 17+ messages in thread
From: Peter Maydell @ 2015-09-07 11:04 UTC (permalink / raw)
To: QEMU Developers
Cc: Kevin Wolf, Qemu-block, Patch Tracking, Michael S. Tsirkin,
Keith Busch, Paolo Bonzini
Ping?
Paolo, do you want to take these, should I just apply them
to master, or what?
thanks
-- PMM
On 24 July 2015 at 13:33, Peter Maydell <peter.maydell@linaro.org> wrote:
> 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.
>
> No changes from v1 to v2 except for a new patch 6 which moves
> the pow2ceil and pow2floor functions to inline.
>
> Peter Maydell (6):
> 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
> Make pow2ceil() and pow2floor() inline
>
> 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 | 17 +----------------
> include/qemu/host-utils.h | 33 +++++++++++++++++++++++++++++++++
> util/cutils.c | 28 ----------------------------
> 8 files changed, 39 insertions(+), 57 deletions(-)
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [Qemu-devel] [PATCH v2 0/6] replace qemu_fls() with pow2ceil()/pow2floor()
2015-09-07 11:04 ` Peter Maydell
@ 2015-09-07 12:34 ` Paolo Bonzini
2015-09-07 15:07 ` Peter Maydell
0 siblings, 1 reply; 17+ messages in thread
From: Paolo Bonzini @ 2015-09-07 12:34 UTC (permalink / raw)
To: Peter Maydell, QEMU Developers
Cc: Kevin Wolf, Keith Busch, Patch Tracking, Qemu-block,
Michael S. Tsirkin
On 07/09/2015 13:04, Peter Maydell wrote:
> Ping?
>
> Paolo, do you want to take these, should I just apply them
> to master, or what?
Sorry, I thought you were applying these or pulling them through your
trees. Let me know if you still want me to take them.
Paolo
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [Qemu-devel] [PATCH v2 0/6] replace qemu_fls() with pow2ceil()/pow2floor()
2015-09-07 12:34 ` Paolo Bonzini
@ 2015-09-07 15:07 ` Peter Maydell
0 siblings, 0 replies; 17+ messages in thread
From: Peter Maydell @ 2015-09-07 15:07 UTC (permalink / raw)
To: Paolo Bonzini
Cc: Kevin Wolf, Qemu-block, Patch Tracking, Michael S. Tsirkin,
QEMU Developers, Keith Busch
On 7 September 2015 at 13:34, Paolo Bonzini <pbonzini@redhat.com> wrote:
>
>
> On 07/09/2015 13:04, Peter Maydell wrote:
>> Ping?
>>
>> Paolo, do you want to take these, should I just apply them
>> to master, or what?
>
> Sorry, I thought you were applying these or pulling them through your
> trees. Let me know if you still want me to take them.
OK, I've applied this to master.
thanks
-- PMM
^ permalink raw reply [flat|nested] 17+ messages in thread
end of thread, other threads:[~2015-09-07 15:14 UTC | newest]
Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-07-24 12:33 [Qemu-devel] [PATCH v2 0/6] replace qemu_fls() with pow2ceil()/pow2floor() Peter Maydell
2015-07-24 12:33 ` [Qemu-devel] [PATCH v2 1/6] hw/pci: Use pow2ceil() rather than hand-calculation Peter Maydell
2015-08-12 7:35 ` Michael S. Tsirkin
2015-07-24 12:33 ` [Qemu-devel] [PATCH v2 2/6] hw/virtio/virtio-pci: " Peter Maydell
2015-08-12 7:36 ` Michael S. Tsirkin
2015-07-24 12:33 ` [Qemu-devel] [PATCH v2 3/6] hw/block/nvme.c: " Peter Maydell
2015-08-14 23:41 ` Paolo Bonzini
2015-07-24 12:33 ` [Qemu-devel] [PATCH v2 4/6] exec.c: Use pow2floor() " Peter Maydell
2015-08-14 23:41 ` Paolo Bonzini
2015-07-24 12:33 ` [Qemu-devel] [PATCH v2 5/6] Remove unused qemu_fls function Peter Maydell
2015-08-14 23:41 ` Paolo Bonzini
2015-07-24 12:33 ` [Qemu-devel] [PATCH v2 6/6] Make pow2ceil() and pow2floor() inline Peter Maydell
2015-08-14 23:42 ` Paolo Bonzini
2015-08-14 10:11 ` [Qemu-devel] [PATCH v2 0/6] replace qemu_fls() with pow2ceil()/pow2floor() Peter Maydell
2015-09-07 11:04 ` Peter Maydell
2015-09-07 12:34 ` Paolo Bonzini
2015-09-07 15:07 ` Peter Maydell
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).