2014-12-27 16:42:08 +01:00
|
|
|
#ifndef _ASM_X86_HWEIGHT_H
|
|
|
|
#define _ASM_X86_HWEIGHT_H
|
|
|
|
|
|
|
|
#ifdef CONFIG_64BIT
|
|
|
|
/* popcnt %edi, %eax -- redundant REX prefix for alignment */
|
|
|
|
#define POPCNT32 ".byte 0xf3,0x40,0x0f,0xb8,0xc7"
|
|
|
|
/* popcnt %rdi, %rax */
|
|
|
|
#define POPCNT64 ".byte 0xf3,0x48,0x0f,0xb8,0xc7"
|
|
|
|
#define REG_IN "D"
|
|
|
|
#define REG_OUT "a"
|
|
|
|
#else
|
|
|
|
/* popcnt %eax, %eax */
|
|
|
|
#define POPCNT32 ".byte 0xf3,0x0f,0xb8,0xc0"
|
|
|
|
#define REG_IN "a"
|
|
|
|
#define REG_OUT "a"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* __sw_hweightXX are called from within the alternatives below
|
|
|
|
* and callee-clobbered registers need to be taken care of. See
|
|
|
|
* ARCH_HWEIGHT_CFLAGS in <arch/x86/Kconfig> for the respective
|
|
|
|
* compiler switches.
|
|
|
|
*/
|
2016-01-20 05:19:53 +01:00
|
|
|
static __always_inline unsigned int __arch_hweight32(unsigned int w)
|
2014-12-27 16:42:08 +01:00
|
|
|
{
|
2016-01-20 05:19:53 +01:00
|
|
|
unsigned int res = w - ((w >> 1) & 0x55555555);
|
|
|
|
res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
|
|
|
|
res = (res + (res >> 4)) & 0x0F0F0F0F;
|
|
|
|
res = res + (res >> 8);
|
|
|
|
return (res + (res >> 16)) & 0x000000FF;
|
2014-12-27 16:42:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned int __arch_hweight16(unsigned int w)
|
|
|
|
{
|
|
|
|
return __arch_hweight32(w & 0xffff);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned int __arch_hweight8(unsigned int w)
|
|
|
|
{
|
|
|
|
return __arch_hweight32(w & 0xff);
|
|
|
|
}
|
|
|
|
|
2016-01-20 05:19:53 +01:00
|
|
|
#ifdef CONFIG_X86_32
|
2014-12-27 16:42:08 +01:00
|
|
|
static inline unsigned long __arch_hweight64(__u64 w)
|
|
|
|
{
|
|
|
|
return __arch_hweight32((u32)w) +
|
|
|
|
__arch_hweight32((u32)(w >> 32));
|
2016-01-20 05:19:53 +01:00
|
|
|
}
|
2014-12-27 16:42:08 +01:00
|
|
|
#else
|
2016-01-20 05:19:53 +01:00
|
|
|
static __always_inline unsigned long __arch_hweight64(__u64 w)
|
|
|
|
{
|
|
|
|
unsigned long res = 0;
|
|
|
|
|
2014-12-27 16:42:08 +01:00
|
|
|
asm (ALTERNATIVE("call __sw_hweight64", POPCNT64, X86_FEATURE_POPCNT)
|
|
|
|
: "="REG_OUT (res)
|
|
|
|
: REG_IN (w));
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2016-01-20 05:19:53 +01:00
|
|
|
#endif /* CONFIG_X86_32 */
|
2014-12-27 16:42:08 +01:00
|
|
|
|
|
|
|
#endif
|