]> git.kernelconcepts.de Git - karo-tx-linux.git/commitdiff
x86: Prefer TZCNT over BFS
authorJan Beulich <JBeulich@suse.com>
Mon, 10 Sep 2012 11:24:43 +0000 (12:24 +0100)
committerIngo Molnar <mingo@kernel.org>
Thu, 13 Sep 2012 15:44:01 +0000 (17:44 +0200)
Following a relatively recent compiler change, make use of the
fact that for non-zero input BSF and TZCNT produce the same
result, 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.

For the moment, only do this when the respective generic-CPU
option is selected (as there are no specific-CPU options
covering the CPUs supporting TZCNT), and don't do that when size
optimization was requested.

Signed-off-by: Jan Beulich <jbeulich@suse.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Link: http://lkml.kernel.org/r/504DEA1B020000780009A277@nat28.tlf.novell.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
arch/x86/include/asm/bitops.h

index ebaee695394e5feb4c424000cabda8d34f5b9df3..b2af6645ea7eadb12d47a8d1aee3ad7be0a2377b 100644 (file)
@@ -347,6 +347,19 @@ static int test_bit(int nr, const volatile unsigned long *addr);
         ? constant_test_bit((nr), (addr))      \
         : variable_test_bit((nr), (addr)))
 
+#if (defined(CONFIG_X86_GENERIC) || defined(CONFIG_GENERIC_CPU)) \
+    && !defined(CONFIG_CC_OPTIMIZE_FOR_SIZE)
+/*
+ * Since BSF and TZCNT have sufficiently similar semantics for the purposes
+ * for which we use them here, BMI-capable hardware will decode the prefixed
+ * variant as 'tzcnt ...' and may execute that faster than 'bsf ...', while
+ * older hardware will ignore the REP prefix and decode it as 'bsf ...'.
+ */
+# define BSF_PREFIX "rep;"
+#else
+# define BSF_PREFIX
+#endif
+
 /**
  * __ffs - find first set bit in word
  * @word: The word to search
@@ -355,7 +368,7 @@ static int test_bit(int nr, const volatile unsigned long *addr);
  */
 static inline unsigned long __ffs(unsigned long word)
 {
-       asm("bsf %1,%0"
+       asm(BSF_PREFIX "bsf %1,%0"
                : "=r" (word)
                : "rm" (word));
        return word;
@@ -369,12 +382,14 @@ static inline unsigned long __ffs(unsigned long word)
  */
 static inline unsigned long ffz(unsigned long word)
 {
-       asm("bsf %1,%0"
+       asm(BSF_PREFIX "bsf %1,%0"
                : "=r" (word)
                : "r" (~word));
        return word;
 }
 
+#undef BSF_PREFIX
+
 /*
  * __fls: find last set bit in word
  * @word: The word to search