[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PATCH v2 3/3] host-utils: Simplify pow2ceil()
From: |
Eric Blake |
Subject: |
Re: [Qemu-devel] [PATCH v2 3/3] host-utils: Simplify pow2ceil() |
Date: |
Thu, 27 Jul 2017 09:10:04 -0500 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 |
On 07/27/2017 04:46 AM, Markus Armbruster wrote:
> Cc: Radim Krčmář <address@hidden>
> Signed-off-by: Markus Armbruster <address@hidden>
> ---
> include/qemu/host-utils.h | 21 +++++++++++++--------
> 1 file changed, 13 insertions(+), 8 deletions(-)
>
>
> -/* 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);
> + int n = clz64(value - 1);
>
> - if (is_power_of_2(value)) {
> - return value;
> + 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;
Tricky, but the comment is worth its weight in gold.
Reviewed-by: Eric Blake <address@hidden>
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org
signature.asc
Description: OpenPGP digital signature