diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/qemu-common.h | 16 | ||||
-rw-r--r-- | include/qemu/host-utils.h | 33 |
2 files changed, 34 insertions, 15 deletions
diff --git a/include/qemu-common.h b/include/qemu-common.h index f328821650..efaf919884 100644 --- a/include/qemu-common.h +++ b/include/qemu-common.h @@ -24,6 +24,7 @@ #include "glib-compat.h" #include "qemu/option.h" +#include "qemu/host-utils.h" /* HOST_LONG_BITS is the size of a native pointer in bits. */ #if UINTPTR_MAX == UINT32_MAX @@ -416,21 +417,6 @@ static inline uint8_t from_bcd(uint8_t val) /* 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 c27d3dc898..7d36ebfd5b 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, @@ -408,4 +409,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 |