All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: Alexey Kardashevskiy <aik@ozlabs.ru>
Cc: Peter Maydell <peter.maydell@linaro.org>, qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH v2] utils: Add pow2ceil()
Date: Mon, 23 Feb 2015 14:59:00 +0100	[thread overview]
Message-ID: <877fv8sp97.fsf@blackfin.pond.sub.org> (raw)
In-Reply-To: <1424694237-22786-1-git-send-email-aik@ozlabs.ru> (Alexey Kardashevskiy's message of "Mon, 23 Feb 2015 23:23:57 +1100")

Alexey Kardashevskiy <aik@ozlabs.ru> writes:

> This adds a helper to get closest bigger power-of-two value.
>
> Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
> ---
> Changes:
> v2:
> * s/up_pow_of_two/pow2ceil/
> ---
>  include/qemu-common.h | 2 ++
>  util/cutils.c         | 9 +++++++++
>  2 files changed, 11 insertions(+)
>
> diff --git a/include/qemu-common.h b/include/qemu-common.h
> index 644b46d..ae29748 100644
> --- a/include/qemu-common.h
> +++ b/include/qemu-common.h
> @@ -417,6 +417,8 @@ static inline bool is_power_of_2(uint64_t value)
>  
>  /* round down to the nearest power of 2*/
>  int64_t pow2floor(int64_t value);
> +/* round up to the nearest power of 2*/
> +int64_t pow2ceil(int64_t value);
>  
>  #include "qemu/module.h"
>  
> diff --git a/util/cutils.c b/util/cutils.c
> index dbe7412..ecaa440 100644
> --- a/util/cutils.c
> +++ b/util/cutils.c
> @@ -483,6 +483,15 @@ int64_t pow2floor(int64_t value)
>      return value;
>  }
>  
> +/* round up to the nearest power of 2*/
> +int64_t pow2ceil(int64_t value)
> +{
> +    if (!is_power_of_2(value)) {
> +        value = 0x8000000000000000ULL >> (clz64(value) - 1);
> +    }
> +    return value;
> +}
> +
>  /*
>   * Implementation of  ULEB128 (http://en.wikipedia.org/wiki/LEB128)
>   * Input is limited to 14-bit numbers

pow2ceil(INT64_MIN) = INT64_MIN.  Should be 1.

pow2ceil(INT64_MAX) = INT64_MIN.  Garbage.

Related: "round down to the nearest power of 2" is defined only for x >
0, but our pow2floor(x) happily returns garbage then.

In particular we return 0x8000000000000000ULL >> 64 when value is 0,.
Undefined behavior.

Here's how I'd do these functions:

int64_t pow2floor(int64_t value)
{
    assert(value > 0);
    return 0x8000000000000000u >> clz64(value);
}

int64_t pow2ceil(int64_t value)
{
    assert(value <= 0x4000000000000000)
    if (value <= 1)
	return 1;
    return 0x8000000000000000u >> (clz64(value - 1) - 1);
}

  reply	other threads:[~2015-02-23 13:59 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-23 12:23 [Qemu-devel] [PATCH v2] utils: Add pow2ceil() Alexey Kardashevskiy
2015-02-23 13:59 ` Markus Armbruster [this message]
2015-02-23 16:17   ` Eric Blake
2015-02-23 17:40     ` Markus Armbruster
2015-02-23 21:20       ` Eric Blake
2015-02-24  9:39         ` Markus Armbruster
2015-02-24 11:39           ` Peter Maydell
2015-02-24 13:09             ` Markus Armbruster
2015-02-25  0:40   ` Alexey Kardashevskiy
2015-02-25 10:45     ` Markus Armbruster
2015-03-12 15:29       ` Richard Henderson
2015-03-12 16:45         ` Eric Blake
2015-03-13 19:04           ` Richard Henderson
2015-03-13  7:33         ` Markus Armbruster

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=877fv8sp97.fsf@blackfin.pond.sub.org \
    --to=armbru@redhat.com \
    --cc=aik@ozlabs.ru \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.