From: Eric Blake <eblake@redhat.com>
To: Markus Armbruster <armbru@redhat.com>, qemu-devel@nongnu.org
Cc: "Radim Krčmář" <rkrcmar@redhat.com>
Subject: Re: [Qemu-devel] [PATCH v2 3/3] host-utils: Simplify pow2ceil()
Date: Thu, 27 Jul 2017 09:10:04 -0500 [thread overview]
Message-ID: <e7f16ea7-3d0d-b4a4-6eb1-79a029f60da7@redhat.com> (raw)
In-Reply-To: <1501148776-16890-4-git-send-email-armbru@redhat.com>
[-- Attachment #1: Type: text/plain, Size: 1176 bytes --]
On 07/27/2017 04:46 AM, Markus Armbruster wrote:
> Cc: Radim Krčmář <rkrcmar@redhat.com>
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
> 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 <eblake@redhat.com>
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 619 bytes --]
prev parent reply other threads:[~2017-07-27 14:10 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-07-27 9:46 [Qemu-devel] [PATCH v2 0/3] Proactive pow2floor() fix, and dead code removal Markus Armbruster
2017-07-27 9:46 ` [Qemu-devel] [PATCH v2 1/3] xbzrle: Drop unused cache_resize() Markus Armbruster
2017-07-27 13:57 ` Juan Quintela
2017-07-27 9:46 ` [Qemu-devel] [PATCH v2 2/3] host-utils: Proactively fix pow2floor(), switch to unsigned Markus Armbruster
2017-07-27 14:08 ` Eric Blake
2017-07-27 9:46 ` [Qemu-devel] [PATCH v2 3/3] host-utils: Simplify pow2ceil() Markus Armbruster
2017-07-27 14:10 ` Eric Blake [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=e7f16ea7-3d0d-b4a4-6eb1-79a029f60da7@redhat.com \
--to=eblake@redhat.com \
--cc=armbru@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=rkrcmar@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).