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: Eric Blake
Subject: Re: [Qemu-devel] [PATCH v2] utils: Add pow2ceil()
Date: Mon, 23 Feb 2015 09:17:50 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0

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 is the parameter int64_t?  Wouldn't it be more useful to have:

uint64_t pow2floor(uint64_t value)

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

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

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

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

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