diff options
Diffstat (limited to 'include/qemu/host-utils.h')
-rw-r--r-- | include/qemu/host-utils.h | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/include/qemu/host-utils.h b/include/qemu/host-utils.h index c27d3dc..7d36ebf 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 |