From: "Jan Beulich" Subject: [PATCH] x86: use tzcnt instead of bsf Date: Fri, 23 Jan 2015 16:39:51 +0000 Message-ID: <54C287670200007800059004@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartC0F55B47.1__=" Return-path: Received: from mail6.bemta4.messagelabs.com ([85.158.143.247]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1YEhGx-0001TE-HW for xen-devel@lists.xenproject.org; Fri, 23 Jan 2015 16:39:51 +0000 List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel Cc: Andrew Cooper , Keir Fraser List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__PartC0F55B47.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Following a compiler change done in 2012, make use of the fact that for non-zero input BSF and TZCNT produce the same numeric result (EFLAGS setting differs), and that CPUs not knowing of TZCNT will treat the instruction as BSF (i.e. ignore what looks like a REP prefix to them). The assumption here is that TZCNT would never have worse performance than BSF. Also extend the asm() input in find_first_set_bit() to allow memory operands. Signed-off-by: Jan Beulich --- Thanks to Andrew for noticing that I forgot to post this for Xen after a similar change got accepted into the Linux kernel. --- a/xen/arch/x86/bitops.c +++ b/xen/arch/x86/bitops.c @@ -62,7 +62,7 @@ unsigned int __find_first_zero_bit( " je 2f\n\t" " xor -"STR(BITS_PER_LONG/8)"(%2),%3\n\t" " jz 1b\n\t" - " bsf %3,%0\n\t" + " rep; bsf %3,%0\n\t" " lea -"STR(BITS_PER_LONG/8)"(%2),%2\n\t" "2: sub %%ebx,%%edi\n\t" " shl $3,%%edi\n\t" --- a/xen/arch/x86/hvm/vpic.c +++ b/xen/arch/x86/hvm/vpic.c @@ -56,7 +56,7 @@ static int vpic_get_priority(struct hvm_ return VPIC_PRIO_NONE; =20 /* prio =3D ffs(mask ROR vpic->priority_add); */ - asm ( "ror %%cl,%b1 ; bsf %1,%0" + asm ( "ror %%cl,%b1 ; rep; bsf %1,%0" : "=3Dr" (prio) : "q" ((uint32_t)mask), "c" (vpic->priority_add)= ); return prio; } --- a/xen/include/asm-x86/bitops.h +++ b/xen/include/asm-x86/bitops.h @@ -382,7 +382,7 @@ static inline unsigned int __scanbit(uns */ static inline unsigned int find_first_set_bit(unsigned long word) { - asm ( "bsf %1,%0" : "=3Dr" (word) : "r" (word) ); + asm ( "rep; bsf %1,%0" : "=3Dr" (word) : "rm" (word) ); return (unsigned int)word; } =20 --=__PartC0F55B47.1__= Content-Type: text/plain; name="x86-use-tzcnt.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-use-tzcnt.patch" x86: use tzcnt instead of bsf=0A=0AFollowing a compiler change done in = 2012, make use of the fact that for=0Anon-zero input BSF and TZCNT produce = the same numeric result (EFLAGS=0Asetting differs), and that CPUs not = knowing of TZCNT will treat the=0Ainstruction as BSF (i.e. ignore what = looks like a REP prefix to them).=0AThe assumption here is that TZCNT = would never have worse performance=0Athan BSF.=0A=0AAlso extend the asm() = input in find_first_set_bit() to allow memory=0Aoperands.=0A=0ASigned-off-b= y: Jan Beulich =0A---=0AThanks to Andrew for noticing = that I forgot to post this for Xen after=0Aa similar change got accepted = into the Linux kernel.=0A=0A--- a/xen/arch/x86/bitops.c=0A+++ b/xen/arch/x8= 6/bitops.c=0A@@ -62,7 +62,7 @@ unsigned int __find_first_zero_bit(=0A = " je 2f\n\t"=0A " xor -"STR(BITS_PER_LONG/8)"(%2),%3\n\t"=0A= " jz 1b\n\t"=0A- " bsf %3,%0\n\t"=0A+ " rep; = bsf %3,%0\n\t"=0A " lea -"STR(BITS_PER_LONG/8)"(%2),%2\n\t"=0A = "2: sub %%ebx,%%edi\n\t"=0A " shl $3,%%edi\n\t"=0A--- = a/xen/arch/x86/hvm/vpic.c=0A+++ b/xen/arch/x86/hvm/vpic.c=0A@@ -56,7 +56,7 = @@ static int vpic_get_priority(struct hvm_=0A return VPIC_PRIO_NON= E;=0A =0A /* prio =3D ffs(mask ROR vpic->priority_add); */=0A- asm = ( "ror %%cl,%b1 ; bsf %1,%0"=0A+ asm ( "ror %%cl,%b1 ; rep; bsf = %1,%0"=0A : "=3Dr" (prio) : "q" ((uint32_t)mask), "c" (vpic->prio= rity_add) );=0A return prio;=0A }=0A--- a/xen/include/asm-x86/bitops.h= =0A+++ b/xen/include/asm-x86/bitops.h=0A@@ -382,7 +382,7 @@ static inline = unsigned int __scanbit(uns=0A */=0A static inline unsigned int find_first_= set_bit(unsigned long word)=0A {=0A- asm ( "bsf %1,%0" : "=3Dr" (word) = : "r" (word) );=0A+ asm ( "rep; bsf %1,%0" : "=3Dr" (word) : "rm" = (word) );=0A return (unsigned int)word;=0A }=0A =0A --=__PartC0F55B47.1__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__PartC0F55B47.1__=--