On 06/28/2012 06:06 AM, Max Filippov wrote:
> On Wed, Jun 27, 2012 at 8:55 PM, Eric Blake <ebl...@redhat.com> wrote:
>> On 06/27/2012 04:34 AM, Orit Wasserman wrote:
> 

>> 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);
> }

Indeed.  I knew about the gcc builtin for clz, but didn't suggest it
because it is not standardized, and didn't realize that qemu had already
wrapped it in a nice function (and one which is portable even when not
compiling with gcc).  It's a shame that libc provides ffs() but not
clz() (since the two operations are rather symmetric (just differing in
which direction they find the first bit), and both useful in
bit-twiddling operations.

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

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to