Skip to contentSkip to conversation

x86: use tzcnt instead of bsf

PATCH In review Jan Beulich 2 participants 2 messages Jan 23, 2015 – Jan 26, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: use tzcnt instead of bsf

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.

---
Thanks to Andrew for noticing that I forgot to post this for Xen after a similar change got accepted into the Linux kernel.

xen/arch/x86/bitops.c +1 -1
@@ -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"
xen/arch/x86/hvm/vpic.c +1 -1
@@ -56,7 +56,7 @@ static int vpic_get_priority(struct hvm_
         return VPIC_PRIO_NONE;
 
     /* prio = ffs(mask ROR vpic->priority_add); */
-    asm ( "ror %%cl,%b1 ; bsf %1,%0"
+    asm ( "ror %%cl,%b1 ; rep; bsf %1,%0"
           : "=r" (prio) : "q" ((uint32_t)mask), "c" (vpic->priority_add) );
     return prio;
 }
xen/include/asm-x86/bitops.h +1 -1
@@ -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" : "=r" (word) : "r" (word) );
+    asm ( "rep; bsf %1,%0" : "=r" (word) : "rm" (word) );
     return (unsigned int)word;
 }
 

x86: use tzcnt instead of bsf

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.

---
Thanks to Andrew for noticing that I forgot to post this for Xen after a similar change got accepted into the Linux kernel.

xen/arch/x86/bitops.c +1 -1
@@ -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"
xen/arch/x86/hvm/vpic.c +1 -1
@@ -56,7 +56,7 @@ static int vpic_get_priority(struct hvm_
         return VPIC_PRIO_NONE;
 
     /* prio = ffs(mask ROR vpic->priority_add); */
-    asm ( "ror %%cl,%b1 ; bsf %1,%0"
+    asm ( "ror %%cl,%b1 ; rep; bsf %1,%0"
           : "=r" (prio) : "q" ((uint32_t)mask), "c" (vpic->priority_add) );
     return prio;
 }
xen/include/asm-x86/bitops.h +1 -1
@@ -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" : "=r" (word) : "r" (word) );
+    asm ( "rep; bsf %1,%0" : "=r" (word) : "rm" (word) );
     return (unsigned int)word;
 }
 

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Jan Beulich

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.

Quoted 40 lines

---
Thanks to Andrew for noticing that I forgot to post this for Xen after a similar change got accepted into the Linux kernel.

xen/arch/x86/bitops.c +1 -1
@@ -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"
xen/arch/x86/hvm/vpic.c +1 -1
@@ -56,7 +56,7 @@ static int vpic_get_priority(struct hvm_
         return VPIC_PRIO_NONE;
 
     /* prio = ffs(mask ROR vpic->priority_add); */
-    asm ( "ror %%cl,%b1 ; bsf %1,%0"
+    asm ( "ror %%cl,%b1 ; rep; bsf %1,%0"
           : "=r" (prio) : "q" ((uint32_t)mask), "c" (vpic->priority_add) );
     return prio;
 }
xen/include/asm-x86/bitops.h +1 -1
@@ -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" : "=r" (word) : "r" (word) );
+    asm ( "rep; bsf %1,%0" : "=r" (word) : "rm" (word) );
     return (unsigned int)word;
 }