Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757493Ab0BLRc7 (ORCPT ); Fri, 12 Feb 2010 12:32:59 -0500 Received: from terminus.zytor.com ([198.137.202.10]:57409 "EHLO mail.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757319Ab0BLRc5 (ORCPT ); Fri, 12 Feb 2010 12:32:57 -0500 Message-ID: <4B758FC0.1020600@zytor.com> Date: Fri, 12 Feb 2010 09:28:32 -0800 From: "H. Peter Anvin" User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.7) Gecko/20100120 Fedora/3.0.1-1.fc11 Thunderbird/3.0.1 MIME-Version: 1.0 To: Borislav Petkov CC: Borislav Petkov , Peter Zijlstra , Andrew Morton , Wu Fengguang , LKML , Jamie Lokier , Roland Dreier , Al Viro , "linux-fsdevel@vger.kernel.org" , Ingo Molnar , Brian Gerst Subject: Re: [PATCH 2/5] bitops: compile time optimization for hweight_long(CONSTANT) References: <1265299457.22001.72.camel@laptop> <20100205121139.GA9044@aftab> <4B6C93A2.1090302@zytor.com> <20100206093659.GA28326@aftab> <4B6E1DA3.50204@zytor.com> <20100208092845.GB12618@a1.tnic> <4B6FDAED.9060204@zytor.com> <20100208095945.GA14740@a1.tnic> <20100211172424.GB19779@aftab> <4B743F7D.3090605@zytor.com> <20100212170649.GC3114@aftab> In-Reply-To: <20100212170649.GC3114@aftab> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2242 Lines: 70 On 02/12/2010 09:06 AM, Borislav Petkov wrote: > On Thu, Feb 11, 2010 at 09:33:49AM -0800, H. Peter Anvin wrote: >> You don't do the push/pop inline -- if you're going to take the hit of >> pushing this into the caller, it's better to list them as explicit >> clobbers and let the compiler figure out how to do it. The point of >> doing an explicit push/pop is that it can be pushed into the out-of-line >> subroutine. > > Doh! Of course, this was wrong. See below. > > diff --git a/arch/x86/lib/hweight.c b/arch/x86/lib/hweight.c > new file mode 100644 > index 0000000..9b4bfa5 > --- /dev/null > +++ b/arch/x86/lib/hweight.c > @@ -0,0 +1,72 @@ > +#include > +#include > +#include > + > +#ifdef CONFIG_64BIT > +/* popcnt %rdi, %rax */ > +#define POPCNT ".byte 0xf3\n\t.byte 0x48\n\t.byte 0x0f\n\t.byte 0xb8\n\t.byte 0xc7" > +#define REG_IN "D" > +#define REG_OUT "a" > +#else > +/* popcnt %eax, %eax */ > +#define POPCNT ".byte 0xf3\n\t.byte 0x0f\n\t.byte 0xb8\n\t.byte 0xc0" > +#define REG_IN "a" > +#define REG_OUT "a" > +#endif > + > +/* > + * Those are called out-of-line in the alternative below and are added here only > + * so that gcc is able to figure out which registers have been clobbered by > + * __sw_hweightXX so that it could restore their values before returning from > + * the __arch_hweightXX versions. See also . > + */ > +unsigned int _sw_hweight32(unsigned int w) > +{ > + return __sw_hweight32(w); > +} > + > +unsigned long _sw_hweight64(__u64 w) > +{ > + return __sw_hweight64(w); > +} > + > +unsigned int __arch_hweight32(unsigned int w) > +{ > + unsigned int res = 0; > + > + asm (ALTERNATIVE("call _sw_hweight32", POPCNT, X86_FEATURE_POPCNT) > + : "="REG_OUT (res) > + : REG_IN (w)); > + Now you have no clobbers and no assembly wrapper. That really doesn't work. -hpa -- H. Peter Anvin, Intel Open Source Technology Center I work for Intel. I don't speak on their behalf. -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/