qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Michal Privoznik <mprivozn@redhat.com>
To: Michael Roth <mdroth@linux.vnet.ibm.com>
Cc: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH] qemu-ga: Add guest-getip command
Date: Fri, 17 Feb 2012 11:58:44 +0100	[thread overview]
Message-ID: <4F3E32E4.6030206@redhat.com> (raw)
In-Reply-To: <20120216201005.GA20920@illuin>

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 <mprivozn@redhat.com>
>> ---
>>  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

  reply	other threads:[~2012-02-17 10:58 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-16 17:25 [Qemu-devel] [PATCH] qemu-ga: Add guest-getip command Michal Privoznik
2012-02-16 20:10 ` Michael Roth
2012-02-17 10:58   ` Michal Privoznik [this message]
2012-02-17 20:14     ` Anthony Liguori

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4F3E32E4.6030206@redhat.com \
    --to=mprivozn@redhat.com \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).