qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH v2] utils: Add pow2ceil()


From: Markus Armbruster
Subject: Re: [Qemu-devel] [PATCH v2] utils: Add pow2ceil()
Date: Mon, 23 Feb 2015 18:40:48 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Eric Blake <address@hidden> writes:

> On 02/23/2015 06:59 AM, Markus Armbruster wrote:
>> Alexey Kardashevskiy <address@hidden> writes:
>> 
>>> This adds a helper to get closest bigger power-of-two value.
>>>
>
>> 
>> Here's how I'd do these functions:
>> 
>> 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?

> 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.

>> 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.

>>     return 0x8000000000000000u >> (clz64(value - 1) - 1);
>> }
>> 
>> 
>> 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]