From: "Jan Beulich" <JBeulich@suse.com>
To: <mingo@elte.hu>, <tglx@linutronix.de>, <hpa@zytor.com>
Cc: <linux-kernel@vger.kernel.org>
Subject: [PATCH] x86: prefer tzcnt over bsf
Date: Mon, 10 Sep 2012 12:24:43 +0100 [thread overview]
Message-ID: <504DEA1B020000780009A277@nat28.tlf.novell.com> (raw)
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>
---
arch/x86/include/asm/bitops.h | 19 +++++++++++++++++--
1 file changed, 17 insertions(+), 2 deletions(-)
--- 3.6-rc5/arch/x86/include/asm/bitops.h
+++ 3.6-rc5-x86-bsf-tzcnt/arch/x86/include/asm/bitops.h
@@ -347,6 +347,19 @@ static int test_bit(int nr, const volati
? 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 volati
*/
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(unsign
*/
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
next reply other threads:[~2012-09-10 11:24 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-09-10 11:24 Jan Beulich [this message]
2012-09-14 6:23 ` [tip:x86/asm] x86: Prefer TZCNT over BFS tip-bot for Jan Beulich
2012-09-14 21:14 ` Linus Torvalds
2012-09-14 21:32 ` Borislav Petkov
2012-09-17 7:41 ` Jan Beulich
2012-09-17 10:00 ` Ingo Molnar
2012-09-17 10:58 ` Jan Beulich
2012-09-17 17:00 ` Linus Torvalds
2012-09-17 17:05 ` H. Peter Anvin
2012-09-17 17:13 ` Linus Torvalds
2012-09-18 6:40 ` Jan Beulich
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=504DEA1B020000780009A277@nat28.tlf.novell.com \
--to=jbeulich@suse.com \
--cc=hpa@zytor.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@elte.hu \
--cc=tglx@linutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).