[Top][All Lists]
[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
- [Qemu-devel] [PATCH v13 01/13] Add MigrationParams structure, (continued)
- [Qemu-devel] [PATCH v13 01/13] Add MigrationParams structure, Orit Wasserman, 2012/06/27
- [Qemu-devel] [PATCH v13 03/13] Add XBZRLE documentation, Orit Wasserman, 2012/06/27
- [Qemu-devel] [PATCH v13 02/13] Add migration capabilites, Orit Wasserman, 2012/06/27
- [Qemu-devel] [PATCH v13 04/13] Add cache handling functions, Orit Wasserman, 2012/06/27
- Re: [Qemu-devel] [PATCH v13 04/13] Add cache handling functions, Blue Swirl, 2012/06/27
- [Qemu-devel] [PATCH v13 06/13] Add save_block_hdr function, Orit Wasserman, 2012/06/27
- [Qemu-devel] [PATCH v13 05/13] Add uleb encoding/decoding functions, Orit Wasserman, 2012/06/27
- [Qemu-devel] [PATCH v13 07/13] Add debugging infrastructure, Orit Wasserman, 2012/06/27
- [Qemu-devel] [PATCH v13 08/13] Change ram_save_block to return -1 if there are no more changes, Orit Wasserman, 2012/06/27