qemu-devel
[Top][All Lists]
Advanced

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

Re: [Qemu-devel] [PATCH v13 04/13] Add cache handling functions


From: Max Filippov
Subject: Re: [Qemu-devel] [PATCH v13 04/13] Add cache handling functions
Date: Thu, 28 Jun 2012 16:06:40 +0400

On Wed, Jun 27, 2012 at 8:55 PM, Eric Blake <address@hidden> wrote:
> On 06/27/2012 04:34 AM, Orit Wasserman wrote:

[...]

>> +
>> +    /* round down to the nearest power of 2 */
>> +    if (!is_power_of_2(num_pages)) {
>> +        num_pages = 1 << ffs(num_pages);
>
> That's not how you round down.  For example, if I passed in 0x5, you end
> up giving me 1 << ffs(5) == 1 << 1 == 2, but the correct answer should be 4.
>
> http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
> and http://aggregate.org/MAGIC/#Leading%20Zero%20Count give some hints
> about what you really want to be doing; offhand, I came up with this (it
> works because you already rejected negative num_pages):
>
> if (!is_power_of_2(num_pages)) {
>    num_pages |= num_pages >> 1;
>    num_pages |= num_pages >> 2;
>    num_pages |= num_pages >> 4;
>    num_pages |= num_pages >> 8;
>    num_pages |= num_pages >> 16;
>    num_pages |= num_pages >> 32;
>    num_pages -= num_pages / 2;
> }

Or

if (!is_power_of_2(num_pages)) {
    num_pages = 0x8000000000000000ULL >> clz64(num_pages);
}

[...]

-- 
Thanks.
-- Max



reply via email to

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