All of lore.kernel.org
 help / color / mirror / Atom feed
From: zengzhaoxiu@163.com
To: linux-kernel@vger.kernel.org
Cc: Zhaoxiu Zeng <zhaoxiu.zeng@gmail.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, "H. Peter Anvin" <hpa@zytor.com>,
	x86@kernel.org, Borislav Petkov <bp@suse.de>,
	Denys Vlasenko <dvlasenk@redhat.com>
Subject: [PATCH V3 09/29] Add x86-specific parity functions
Date: Thu, 14 Apr 2016 11:08:36 +0800	[thread overview]
Message-ID: <1460603323-4906-1-git-send-email-zengzhaoxiu@163.com> (raw)
In-Reply-To: <1460601525-3822-1-git-send-email-zengzhaoxiu@163.com>

From: Zhaoxiu Zeng <zhaoxiu.zeng@gmail.com>

Use alternatives, lifted from arch_hweight

Signed-off-by: Zhaoxiu Zeng <zhaoxiu.zeng@gmail.com>
---
 arch/x86/include/asm/arch_hweight.h |   5 ++
 arch/x86/include/asm/arch_parity.h  | 117 ++++++++++++++++++++++++++++++++++++
 arch/x86/include/asm/bitops.h       |   4 +-
 3 files changed, 125 insertions(+), 1 deletion(-)
 create mode 100644 arch/x86/include/asm/arch_parity.h

diff --git a/arch/x86/include/asm/arch_hweight.h b/arch/x86/include/asm/arch_hweight.h
index 02e799f..c79d50d 100644
--- a/arch/x86/include/asm/arch_hweight.h
+++ b/arch/x86/include/asm/arch_hweight.h
@@ -63,4 +63,9 @@ static __always_inline unsigned long __arch_hweight64(__u64 w)
 }
 #endif /* CONFIG_X86_32 */
 
+#undef POPCNT32
+#undef POPCNT64
+#undef REG_IN
+#undef REG_OUT
+
 #endif
diff --git a/arch/x86/include/asm/arch_parity.h b/arch/x86/include/asm/arch_parity.h
new file mode 100644
index 0000000..c4d08e2
--- /dev/null
+++ b/arch/x86/include/asm/arch_parity.h
@@ -0,0 +1,117 @@
+#ifndef _ASM_X86_PARITY_H
+#define _ASM_X86_PARITY_H
+
+#include <asm/cpufeatures.h>
+
+/*
+ * the generic version use the Parity Flag directly
+ *
+ * Parity flag - Set if the least-significant byte of the
+ *               result contains an even number of 1 bits;
+ *               cleared otherwise.
+ */
+
+static inline unsigned int __arch_parity4(unsigned int w)
+{
+	unsigned int res = 0;
+
+	asm("test $0xf, %1; setpo %b0"
+		: "+q" (res)
+		: "r" (w)
+		: "cc");
+
+	return res;
+}
+
+static inline unsigned int __arch_parity8(unsigned int w)
+{
+	unsigned int res = 0;
+
+	asm("test %1, %1; setpo %b0"
+		: "+q" (res)
+		: "r" (w)
+		: "cc");
+
+	return res;
+}
+
+static inline unsigned int __arch_parity16(unsigned int w)
+{
+	unsigned int res = 0;
+
+	asm("xor %h1, %b1; setpo %b0"
+		: "+q" (res), "+q" (w)
+		: : "cc");
+
+	return res;
+}
+
+#ifdef CONFIG_64BIT
+/* popcnt %eax, %eax -- redundant REX prefix for alignment */
+#define POPCNT32 ".byte 0xf3,0x40,0x0f,0xb8,0xc0"
+/* popcnt %rax, %rax */
+#define POPCNT64 ".byte 0xf3,0x48,0x0f,0xb8,0xc0"
+#else
+/* popcnt %eax, %eax */
+#define POPCNT32 ".byte 0xf3,0x0f,0xb8,0xc0"
+#endif
+
+static __always_inline unsigned int __arch_parity32(unsigned int w)
+{
+	unsigned int res;
+	unsigned int tmp;
+
+	asm(ALTERNATIVE(
+		"	mov	%%eax, %1	\n"
+		"	shr	$16, %%eax	\n"
+		"	xor	%1, %%eax	\n"
+		"	xor	%%ah, %%al	\n"
+		"	mov	$0, %%eax	\n"
+		"	setpo	%%al	\n",
+		POPCNT32 "			\n"
+		"	and	$1, %%eax	\n",
+		X86_FEATURE_POPCNT)
+		: "=a" (res), "=&r" (tmp)
+		: "a" (w)
+		: "cc");
+
+	return res;
+}
+
+#ifdef CONFIG_X86_32
+static inline unsigned int __arch_parity64(__u64 w)
+{
+	return __arch_parity32((u32)w ^ (u32)(w >> 32));
+}
+#else
+static __always_inline unsigned int __arch_parity64(__u64 w)
+{
+	unsigned int res;
+	__u64 tmp;
+
+	asm(ALTERNATIVE(
+		"	mov	%%rax, %1	\n"
+		"	shr	$32, %%rax	\n"
+		"	xor	%k1, %%eax	\n"
+		"	mov	%%eax, %k1	\n"
+		"	shr	$16, %%eax	\n"
+		"	xor	%k1, %%eax 	\n"
+		"	xor	%%ah, %%al	\n"
+		"	mov	$0, %%eax	\n"
+		"	setpo	%%al	\n",
+		POPCNT64 "			\n"
+		"	and	$1, %%eax	\n",
+		X86_FEATURE_POPCNT)
+		: "=a" (res), "=&r" (tmp)
+		: "a" (w)
+		: "cc");
+
+	return res;
+}
+#endif /* CONFIG_X86_32 */
+
+#undef POPCNT32
+#undef POPCNT64
+
+#endif
+
diff --git a/arch/x86/include/asm/bitops.h b/arch/x86/include/asm/bitops.h
index 7766d1c..f5b0122 100644
--- a/arch/x86/include/asm/bitops.h
+++ b/arch/x86/include/asm/bitops.h
@@ -498,9 +498,11 @@ static __always_inline int fls64(__u64 x)
 #include <asm-generic/bitops/sched.h>
 
 #include <asm/arch_hweight.h>
-
 #include <asm-generic/bitops/const_hweight.h>
 
+#include <asm/arch_parity.h>
+#include <asm-generic/bitops/const_parity.h>
+
 #include <asm-generic/bitops/le.h>
 
 #include <asm-generic/bitops/ext2-atomic-setbit.h>
-- 
2.5.0

  parent reply	other threads:[~2016-04-14  3:09 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-14  2:36 [PATCH V3 00/29] bitops: add parity functions zengzhaoxiu
2016-04-14  2:36 ` zengzhaoxiu
2016-04-14  2:36 ` zengzhaoxiu
2016-04-14  2:36 ` zengzhaoxiu
2016-04-14  3:04 ` [PATCH V3 02/29] Include generic parity.h in some architectures' bitops.h zengzhaoxiu
2016-04-14  3:04   ` zengzhaoxiu
2016-04-14  3:04   ` zengzhaoxiu at 163.com
2016-04-14  3:04   ` zengzhaoxiu
2016-04-14  3:04   ` zengzhaoxiu
2016-04-14  3:04   ` zengzhaoxiu
2016-04-14  3:04 ` zengzhaoxiu
2016-04-14  3:05 ` [PATCH V3 03/29] Add alpha-specific parity functions zengzhaoxiu
2016-04-14  3:05 ` [PATCH V3 04/29] Add blackfin-specific " zengzhaoxiu
2016-04-14  3:05 ` [PATCH V3 05/29] Add ia64-specific " zengzhaoxiu
2016-04-14  3:05   ` zengzhaoxiu
2016-04-14  3:05 ` [PATCH V3 06/29] Tile and MIPS (if has usable __builtin_popcount) use popcount " zengzhaoxiu
2016-04-15 19:26   ` Chris Metcalf
2016-04-15 19:26     ` Chris Metcalf
2016-04-14  3:06 ` [PATCH V3 07/29] Add powerpc-specific " zengzhaoxiu
2016-04-14  3:08 ` [PATCH V3 08/29] Add sparc-specific " zengzhaoxiu
2016-04-14  3:08   ` zengzhaoxiu
2016-04-14  3:08 ` zengzhaoxiu [this message]
2016-04-14  3:08 ` [PATCH V3 10/29] sunrpc: use parity8 zengzhaoxiu
2016-04-14  3:09 ` [PATCH V3 11/29] mips: use parity functions in cerr-sb1.c zengzhaoxiu
2016-04-14  3:09 ` [PATCH V3 12/29] bch: use parity32 zengzhaoxiu
2016-04-14  3:09 ` [PATCH V3 13/29] media: use parity8 in vivid-vbi-gen.c zengzhaoxiu
2016-04-14  3:09 ` [PATCH V3 14/29] media: use parity functions in saa7115 zengzhaoxiu
2016-04-14  3:10 ` [PATCH V3 15/29] input: use parity32 in grip_mp zengzhaoxiu
2016-04-14  3:10 ` [PATCH V3 16/29] input: use parity64 in sidewinder zengzhaoxiu
2016-04-14  3:10 ` [PATCH V3 17/29] input: use parity16 in ams_delta_serio zengzhaoxiu
2016-04-14  3:10 ` [PATCH V3 18/29] scsi: use parity32 in isci's phy zengzhaoxiu
2016-04-14  3:10 ` [PATCH V3 19/29] mtd: use parity16 in ssfdc zengzhaoxiu
2016-04-14  3:11 ` [PATCH V3 20/29] mtd: use parity functions in inftlcore zengzhaoxiu
2016-04-14  3:11 ` [PATCH V3 21/29] crypto: use parity functions in qat_hal zengzhaoxiu
2016-04-14  3:11 ` [PATCH V3 22/29] mtd: use parity16 in sm_ftl zengzhaoxiu
2016-04-14  3:11 ` [PATCH V3 23/29] ethernet: use parity8 in sun/niu.c zengzhaoxiu
2016-04-14  3:11 ` [PATCH V3 24/29] input: use parity8 in pcips2 zengzhaoxiu
2016-04-14  3:11 ` [PATCH V3 25/29] input: use parity8 in sa1111ps2 zengzhaoxiu
2016-04-14  3:12 ` [PATCH V3 26/29] iio: use parity32 in adxrs450 zengzhaoxiu
2016-04-17 11:32   ` Jonathan Cameron
2016-04-14  3:12 ` [PATCH V3 27/29] serial: use parity32 in max3100 zengzhaoxiu
2016-04-28 21:01   ` Greg Kroah-Hartman
2016-04-14  3:12 ` [PATCH V3 28/29] input: use parity8 in elantech zengzhaoxiu
2016-04-14  3:12 ` [PATCH V3 29/29] ethernet: use parity8 in broadcom/tg3.c zengzhaoxiu
2016-04-14  4:18 ` [PATCH V3 00/29] bitops: add parity functions zengzhaoxiu
2016-04-14  4:18   ` zengzhaoxiu at 163.com
2016-04-14  4:18   ` zengzhaoxiu
2016-04-14  4:18   ` zengzhaoxiu
2016-04-14  4:18   ` zengzhaoxiu
2016-04-14  4:18   ` zengzhaoxiu
2016-04-14  4:18 ` zengzhaoxiu
2016-04-17 12:38 ` Dmitry Torokhov
2016-04-17 12:38   ` Dmitry Torokhov
2016-04-17 12:38   ` Dmitry Torokhov
2016-04-17 12:38   ` Dmitry Torokhov

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=1460603323-4906-1-git-send-email-zengzhaoxiu@163.com \
    --to=zengzhaoxiu@163.com \
    --cc=bp@suse.de \
    --cc=dvlasenk@redhat.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    --cc=zhaoxiu.zeng@gmail.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.