replace qemu_fls() with pow2ceil()/pow2floor()
v2 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.
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(-)
Signature · 1 line
1.9.1
PATCH v2 1/6 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
@@ -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
@@ -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
Re: [Qemu-devel] [PATCH v2 1/6] hw/pci: Use pow2ceil() rather than hand-calculation
Quoted 4 lines · Peter Maydell
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.
Quoted 8 lines
(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 <[email protected]>
Quoted 37 lines
--- hw/pci/msix.c | 4 +--- hw/pci/pci.c | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-)hw/pci/msix.c +1
-3@@ -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@@ -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
PATCH v2 2/6 hw/virtio/virtio-pci: Use pow2ceil() rather than hand-calculation
In reply to Peter MaydellUse 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
@@ -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
Re: [Qemu-devel] [PATCH v2 2/6] hw/virtio/virtio-pci: Use pow2ceil() rather than hand-calculation
Quoted 4 lines · Peter Maydell
Use the utility function
pow2ceil()for rounding up to the next largest power of 2, rather than inline calculation.Signed-off-by Peter Maydell <[email protected]>
Quoted 21 lines
--- hw/virtio/virtio-pci.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)hw/virtio/virtio-pci.c +1
-3@@ -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
PATCH v2 3/6 hw/block/nvme.c: Use pow2ceil() rather than hand-calculation
In reply to Peter MaydellUse 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
@@ -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
Re: [Qemu-devel] [PATCH v2 3/6] hw/block/nvme.c: Use pow2ceil() rather than hand-calculation
Quoted 13 lines · Peter Maydell
Use
pow2ceil()to round up to the next power of 2, rather than an inline calculation.Signed-off-by Peter Maydell <[email protected]>--- hw/block/nvme.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/block/nvme.c +0
-0
hw/block/nvme.c static int nvme_init(PCIDevice *pci_dev) · line 808
Quoted 5 lines
hw/block/nvme.c +1
-1@@ -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. :)
Quoted 4 lines
hw/block/nvme.c +0
-0n->ns_size = bs_size / (uint64_t )n->num_namespaces; n->namespaces =g_new0 (NvmeNamespace, n->num_namespaces);
PATCH v2 4/6 exec.c: Use pow2floor() rather than hand-calculation
In reply to Peter MaydellUse 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
@@ -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
Re: [Qemu-devel] [PATCH v2 4/6] exec.c: Use pow2floor() rather than hand-calculation
Quoted 13 lines · Peter Maydell
Use
pow2floor()to round down to the nearest power of 2, rather than an inline calculation.Signed-off-by Peter Maydell <[email protected]>--- exec.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)exec.c +0
-0
exec.c static int memory_access_size(MemoryRegion *mr, unsigned l, hwaddr addr) · line 2374
Quoted 7 lines
exec.c +1
-3@@ -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);
Quoted 3 lines
exec.c +0
-0return l; }
PATCH v2 5/6 Remove unused qemu_fls function
In reply to Peter MaydellNothing 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
@@ -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
@@ -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
Re: [Qemu-devel] [PATCH v2 5/6] Remove unused qemu_fls function
Quoted 27 lines · Peter Maydell
Nothing uses
qemu_fls()any more, so delete it.Signed-off-by Peter Maydell <[email protected]>--- include/qemu-common.h | 1 - util/cutils.c | 5 ----- 2 files changed, 6 deletions(-)include/qemu-common.h +0
-1@@ -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
-0@@ -145,11 +145,6 @@ time_t mktimegm(struct tm *tm)return t;
util/cutils.c time_t mktimegm(struct tm *tm) · line 150
Quoted 10 lines
util/cutils.c +0
-5} -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.
PATCH v2 6/6 Make pow2ceil() and pow2floor() inline
In reply to Peter MaydellSince 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.
--- include/qemu-common.h | 16 +--------------- include/qemu/host-utils.h | 33 +++++++++++++++++++++++++++++++++ util/cutils.c | 23 ----------------------- 3 files changed, 34 insertions(+), 38 deletions(-)
include/qemu-common.h +1 -15
@@ -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" /*
include/qemu/host-utils.h +33 -0
@@ -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
util/cutils.c +0 -23
@@ -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
Signature · 1 line
1.9.1
Re: [Qemu-devel] [PATCH v2 6/6] Make pow2ceil() and pow2floor() inline
Quoted 127 lines · Peter Maydell
Since the
pow2floor()function is now used in a hot code path, make it inline; for consistency, providepow2ceil()as an inline function too.Because these functions use
ctz64()we have to put the inline versions intohost-utils.h, so they have access toctz64(), and move the inlineis_power_of_2()along with them.We then need to include
host-utils.hfromqemu-common.hso that the files which use these functions viaqemu-common.hstill have access to them.Signed-off-by Peter Maydell <[email protected]>--- include/qemu-common.h | 16 +--------------- include/qemu/host-utils.h | 33 +++++++++++++++++++++++++++++++++ util/cutils.c | 23 ----------------------- 3 files changed, 34 insertions(+), 38 deletions(-)include/qemu-common.h +1
-15@@ -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" /* include/qemu/host-utils.h +33
-0@@ -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 util/cutils.c +0
-16@@ -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;
util/cutils.c int qemu_parse_fd(const char *param) · line 474
Quoted 10 lines
util/cutils.c +0
-7- } -if (!nlz) { -return 0 ; - } -return 1ULL << (64 - nlz); -} -/* * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128) * Input is limited to 14-bit numbers
Ping?
(Patches 1 and 2 have been reviewed; thanks.)
-- PMM
Quoted 35 lines · Peter Maydell
We have a
qemu_fls()function which is just a silly wrapper aroundclz32()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 theqemu_fls()calls withpow2ceil()orpow2floor(), 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: Usepow2ceil()rather than hand-calculation hw/virtio/virtio-pci: Usepow2ceil()rather than hand-calculationhw/block/nvme.c: Usepow2ceil()rather than hand-calculationexec.c: Usepow2floor()rather than hand-calculation Remove unusedqemu_flsfunction
Makepow2ceil()andpow2floor()inlineexec.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
Ping?
Paolo, do you want to take these, should I just apply them to master, or what?
thanks
-- PMM
Quoted 32 lines · Peter Maydell
We have a
qemu_fls()function which is just a silly wrapper aroundclz32()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 theqemu_fls()calls withpow2ceil()orpow2floor(), 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: Usepow2ceil()rather than hand-calculation hw/virtio/virtio-pci: Usepow2ceil()rather than hand-calculationhw/block/nvme.c: Usepow2ceil()rather than hand-calculationexec.c: Usepow2floor()rather than hand-calculation Remove unusedqemu_flsfunction
Makepow2ceil()andpow2floor()inlineexec.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(-)
Quoted 4 lines · Peter Maydell
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
Quoted 10 lines · Paolo Bonzini
Quoted 4 lines · Peter Maydell
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