qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: Markus Armbruster <armbru@redhat.com>
Cc: Alexey Kardashevskiy <aik@ozlabs.ru>,
	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:20:18 -0700	[thread overview]
Message-ID: <54EB9992.20601@redhat.com> (raw)
In-Reply-To: <87sidwo7a7.fsf@blackfin.pond.sub.org>

[-- Attachment #1: Type: text/plain, Size: 1942 bytes --]

On 02/23/2015 10:40 AM, Markus Armbruster wrote:

>>> int64_t pow2floor(int64_t value)
>>> {
>>>     assert(value > 0);
>>>     return 0x8000000000000000u >> clz64(value);
>>> }
>>
>> Needs to be 0x8000000000000000ull for 32-bit machines to compile correctly.
> 
> Why?

Because 0x8000000000000000u is only required to be a 'long', and on
32-bit machines, your constant would overflow long.  See, for example,
commit 5cb6be2ca.  You NEED the 'll' suffix to ensure that the compiler
doesn't reject the constant as an overflow.

> 
>> Why is the parameter int64_t?  Wouldn't it be more useful to have:
>>
>> uint64_t pow2floor(uint64_t value)
> 
> Crossed my mind, too.  However, the existing callers pass *signed*
> arguments.

I guess it means auditing callers either way.

> 
>>> int64_t pow2ceil(int64_t value)
>>> {
>>
>> Again, why allow signed inputs?
>>
>>>     assert(value <= 0x4000000000000000)
>>>     if (value <= 1)
>>> 	return 1;
>>
>> In particular, this slams all negative values to a result of 1, which
>> doesn't necessarily make sense.
> 
> It implements a straightforward contract: return the smallest power of
> two greater or equal to the argument.  The function's domain is the set
> of int64_t arguments where this value can be represented in int64_t,
> i.e. [-2^63..2^62].
> 
> Feel free to suggest a more sensible contract.

But why should I claim that the nearest power of 2 greater than -3 is
positive 1, when I could argue that it should instead be -2 (nearest
positive or negative power of 2 rounding towards +inf) or -4 (nearest
positive or negative power of 2 rounding away from 0)?  Since there are
multiple potential contracts once negative values are involved, I find
it easier to just make the contract require a positive input to begin with.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]

  reply	other threads:[~2015-02-23 21:20 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
2015-02-23 16:17   ` Eric Blake
2015-02-23 17:40     ` Markus Armbruster
2015-02-23 21:20       ` Eric Blake [this message]
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=54EB9992.20601@redhat.com \
    --to=eblake@redhat.com \
    --cc=aik@ozlabs.ru \
    --cc=armbru@redhat.com \
    --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 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).