From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:52519) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RyUC0-0005Du-D3 for qemu-devel@nongnu.org; Fri, 17 Feb 2012 15:14:09 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RyUBy-0000yY-Rw for qemu-devel@nongnu.org; Fri, 17 Feb 2012 15:14:08 -0500 Received: from mail-pw0-f45.google.com ([209.85.160.45]:44684) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RyUBy-0000yD-LY for qemu-devel@nongnu.org; Fri, 17 Feb 2012 15:14:06 -0500 Received: by pbbro12 with SMTP id ro12so4943556pbb.4 for ; Fri, 17 Feb 2012 12:14:04 -0800 (PST) Message-ID: <4F3EB508.5000804@codemonkey.ws> Date: Fri, 17 Feb 2012 14:14:00 -0600 From: Anthony Liguori MIME-Version: 1.0 References: <20120216201005.GA20920@illuin> <4F3E32E4.6030206@redhat.com> In-Reply-To: <4F3E32E4.6030206@redhat.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed 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: Michal Privoznik Cc: Michael Roth , qemu-devel@nongnu.org On 02/17/2012 04:58 AM, Michal Privoznik wrote: > 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. __builtin_popcount() will do this as a single instruction on many architectures. Regards, Anthony Liguori > > Michal > >