From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:37642) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RyLWf-0002pI-Fl for qemu-devel@nongnu.org; Fri, 17 Feb 2012 05:58:58 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RyLWb-0000le-6Z for qemu-devel@nongnu.org; Fri, 17 Feb 2012 05:58:53 -0500 Received: from mx1.redhat.com ([209.132.183.28]:17666) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RyLWa-0000lP-R1 for qemu-devel@nongnu.org; Fri, 17 Feb 2012 05:58:49 -0500 Message-ID: <4F3E32E4.6030206@redhat.com> Date: Fri, 17 Feb 2012 11:58:44 +0100 From: Michal Privoznik MIME-Version: 1.0 References: <20120216201005.GA20920@illuin> In-Reply-To: <20120216201005.GA20920@illuin> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH] qemu-ga: Add guest-getip command List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Michael Roth Cc: qemu-devel@nongnu.org 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 >> --- >> 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