diff options
author | Markus Armbruster <armbru@redhat.com> | 2017-07-27 11:46:16 +0200 |
---|---|---|
committer | Dr. David Alan Gilbert <dgilbert@redhat.com> | 2017-09-06 14:38:03 +0100 |
commit | 362aaf14576d2bc3f0117b87cfb595d48cda3d64 (patch) | |
tree | 501f19735e35e5705ca87a48177181505764559f /include | |
parent | 43c64a093dbe8bffa4eb774685dee535bdffaffb (diff) |
host-utils: Simplify pow2ceil()
Cc: Radim Krčmář <rkrcmar@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <1501148776-16890-4-git-send-email-armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Diffstat (limited to 'include')
-rw-r--r-- | include/qemu/host-utils.h | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/include/qemu/host-utils.h b/include/qemu/host-utils.h index 6c6005f5cf..5ac621cf1f 100644 --- a/include/qemu/host-utils.h +++ b/include/qemu/host-utils.h @@ -381,18 +381,23 @@ static inline uint64_t pow2floor(uint64_t value) return 0x8000000000000000ull >> clz64(value); } -/* round up to the nearest power of 2 (0 if overflow) */ +/* + * Return @value rounded up to the nearest power of two modulo 2^64. + * This is *zero* for @value > 2^63, so be careful. + */ 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; + int n = clz64(value - 1); + + if (!n) { + /* + * @value - 1 has no leading zeroes, thus @value - 1 >= 2^63 + * Therefore, either @value == 0 or @value > 2^63. + * If it's 0, return 1, else return 0. + */ + return !value; } - return 1ULL << (64 - nlz); + return 0x8000000000000000ull >> (n - 1); } /** |