提交 c59bd568 编写于 作者: H H. Peter Anvin

x86, hweight: Use a 32-bit popcnt for __arch_hweight32()

Use a 32-bit popcnt instruction for __arch_hweight32(), even on
x86-64.  Even though the input register will *usually* be
zero-extended due to the standard operation of the hardware, it isn't
necessarily so if the input value was the result of truncating a
64-bit operation.

Note: the POPCNT32 variant used on x86-64 has a technically
unnecessary REX prefix to make it five bytes long, the same as a CALL
instruction, therefore avoiding an unnecessary NOP.
Reported-by: NLinus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: NH. Peter Anvin <hpa@linux.intel.com>
Cc: Borislav Petkov <borislav.petkov@amd.com>
LKML-Reference: <alpine.LFD.2.00.1005171443060.4195@i5.linux-foundation.org>
上级 4677d4a5
...@@ -2,13 +2,15 @@ ...@@ -2,13 +2,15 @@
#define _ASM_X86_HWEIGHT_H #define _ASM_X86_HWEIGHT_H
#ifdef CONFIG_64BIT #ifdef CONFIG_64BIT
/* popcnt %edi, %eax -- redundant REX prefix for alignment */
#define POPCNT32 ".byte 0xf3,0x40,0x0f,0xb8,0xc7"
/* popcnt %rdi, %rax */ /* popcnt %rdi, %rax */
#define POPCNT ".byte 0xf3,0x48,0x0f,0xb8,0xc7" #define POPCNT64 ".byte 0xf3,0x48,0x0f,0xb8,0xc7"
#define REG_IN "D" #define REG_IN "D"
#define REG_OUT "a" #define REG_OUT "a"
#else #else
/* popcnt %eax, %eax */ /* popcnt %eax, %eax */
#define POPCNT ".byte 0xf3,0x0f,0xb8,0xc0" #define POPCNT32 ".byte 0xf3,0x0f,0xb8,0xc0"
#define REG_IN "a" #define REG_IN "a"
#define REG_OUT "a" #define REG_OUT "a"
#endif #endif
...@@ -23,7 +25,7 @@ static inline unsigned int __arch_hweight32(unsigned int w) ...@@ -23,7 +25,7 @@ static inline unsigned int __arch_hweight32(unsigned int w)
{ {
unsigned int res = 0; unsigned int res = 0;
asm (ALTERNATIVE("call __sw_hweight32", POPCNT, X86_FEATURE_POPCNT) asm (ALTERNATIVE("call __sw_hweight32", POPCNT32, X86_FEATURE_POPCNT)
: "="REG_OUT (res) : "="REG_OUT (res)
: REG_IN (w)); : REG_IN (w));
...@@ -48,7 +50,7 @@ static inline unsigned long __arch_hweight64(__u64 w) ...@@ -48,7 +50,7 @@ static inline unsigned long __arch_hweight64(__u64 w)
return __arch_hweight32((u32)w) + return __arch_hweight32((u32)w) +
__arch_hweight32((u32)(w >> 32)); __arch_hweight32((u32)(w >> 32));
#else #else
asm (ALTERNATIVE("call __sw_hweight64", POPCNT, X86_FEATURE_POPCNT) asm (ALTERNATIVE("call __sw_hweight64", POPCNT64, X86_FEATURE_POPCNT)
: "="REG_OUT (res) : "="REG_OUT (res)
: REG_IN (w)); : REG_IN (w));
#endif /* CONFIG_X86_32 */ #endif /* CONFIG_X86_32 */
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册