[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PATCH] qemu-ga: Add guest-getip command
From: |
Michal Privoznik |
Subject: |
Re: [Qemu-devel] [PATCH] qemu-ga: Add guest-getip command |
Date: |
Fri, 17 Feb 2012 11:58:44 +0100 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:10.0.1) Gecko/20120213 Thunderbird/10.0.1 |
On 16.02.2012 21:10, Michael Roth wrote:
> On Thu, Feb 16, 2012 at 06:25:03PM +0100, Michal Privoznik wrote:
>> This command returns an array of:
>>
>> [ifname, ipaddr, ipaddr_family, prefix, hwaddr]
>>
>> for each interface in the system that has an IP address.
>> Currently, only IPv4 and IPv6 are supported.
>
> Cool stuff, seems pretty useful. Some comments below:
>
>>
>> Signed-off-by: Michal Privoznik <address@hidden>
>> ---
>> qapi-schema-guest.json | 16 +++++
>> qga/guest-agent-commands.c | 156
>> ++++++++++++++++++++++++++++++++++++++++++++
>> 2 files changed, 172 insertions(+), 0 deletions(-)
>>
>> +
>> +/* Count the number of '1' bits in @x */
>> +static int32_t
>> +count_one_bits(uint32_t x)
>> +{
>> + x = ((x & 0xaaaaaaaaU) >> 1) + (x & 0x55555555U);
>> + x = ((x & 0xccccccccU) >> 2) + (x & 0x33333333U);
>> + x = (x >> 16) + (x & 0xffff);
>> + x = ((x & 0xf0f0) >> 4) + (x & 0x0f0f);
>> + return (x >> 8) + (x & 0x00ff);
>> +}
>
> I think my brain is too small for this. Why not just:
>
> for (i = 0, count = 0; i < 32; i++) {
> if (x & (1 << i)) {
> count++;
> }
> }
> return count;
>
> ?
That algorithm I've used computes what is known as Hamming weight:
http://en.wikipedia.org/wiki/Hamming_weight#Efficient_implementation
It has advantage of being constant in time. However, I must admit, it is
not easy readable so I'd better use something more obvious.
Thanks for review, I'll sent v2.
Michal