Skip to contentSkip to conversation

core/hweight changes for v2.6.35

GIT PULL In review Ingo Molnar 7 participants 12 messages May 17, 2010 – Aug 14, 2010

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

Linus,

Please pull the latest core-hweight-for-linus git tree from:

git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip.git core-hweight-for-linus

Thanks,

Ingo

------------------>
Borislav Petkov (2):
x86: Add optimized popcnt variants
arch, hweight: Fix compilation errors

Peter Zijlstra (1):
bitops: Optimize hweight() by making use of compile-time evaluation

 arch/alpha/include/asm/bitops.h            |   18 +++++----
 arch/ia64/include/asm/bitops.h             |   11 +++--
 arch/sparc/include/asm/bitops_64.h         |   11 +++--
 arch/x86/Kconfig                           |    5 ++
 arch/x86/include/asm/alternative.h         |    9 +++-
 arch/x86/include/asm/arch_hweight.h        |   59 ++++++++++++++++++++++++++++
 arch/x86/include/asm/bitops.h              |    4 +-
 include/asm-generic/bitops/arch_hweight.h  |   25 ++++++++++++
 include/asm-generic/bitops/const_hweight.h |   42 ++++++++++++++++++++
 include/asm-generic/bitops/hweight.h       |    8 +---
 include/linux/bitops.h                     |   30 ++------------
 lib/Makefile                               |    3 +
 lib/hweight.c                              |   19 +++++----
 scripts/Makefile.lib                       |    4 ++
 14 files changed, 186 insertions(+), 62 deletions(-)
 create mode 100644 arch/x86/include/asm/arch_hweight.h
 create mode 100644 include/asm-generic/bitops/arch_hweight.h
 create mode 100644 include/asm-generic/bitops/const_hweight.h
arch/alpha/include/asm/bitops.h +10 -8
index 15f3ae2..296da1d 100644
@@ -405,29 +405,31 @@ static inline int fls(int x)
 
 #if defined(CONFIG_ALPHA_EV6) && defined(CONFIG_ALPHA_EV67)
 /* Whee.  EV67 can calculate it directly.  */
-static inline unsigned long hweight64(unsigned long w)
+static inline unsigned long __arch_hweight64(unsigned long w)
 {
 	return __kernel_ctpop(w);
 }
 
-static inline unsigned int hweight32(unsigned int w)
+static inline unsigned int __arch_weight32(unsigned int w)
 {
-	return hweight64(w);
+	return __arch_hweight64(w);
 }
 
-static inline unsigned int hweight16(unsigned int w)
+static inline unsigned int __arch_hweight16(unsigned int w)
 {
-	return hweight64(w & 0xffff);
+	return __arch_hweight64(w & 0xffff);
 }
 
-static inline unsigned int hweight8(unsigned int w)
+static inline unsigned int __arch_hweight8(unsigned int w)
 {
-	return hweight64(w & 0xff);
+	return __arch_hweight64(w & 0xff);
 }
 #else
-#include <asm-generic/bitops/hweight.h>
+#include <asm-generic/bitops/arch_hweight.h>
 #endif
 
+#include <asm-generic/bitops/const_hweight.h>
+
 #endif /* __KERNEL__ */
 
 #include <asm-generic/bitops/find.h>
arch/ia64/include/asm/bitops.h +6 -5
index 6ebc229..9da3df6 100644
@@ -437,17 +437,18 @@ __fls (unsigned long x)
  * hweightN: returns the hamming weight (i.e. the number
  * of bits set) of a N-bit word
  */
-static __inline__ unsigned long
-hweight64 (unsigned long x)
+static __inline__ unsigned long __arch_hweight64(unsigned long x)
 {
 	unsigned long result;
 	result = ia64_popcnt(x);
 	return result;
 }
 
-#define hweight32(x)	(unsigned int) hweight64((x) & 0xfffffffful)
-#define hweight16(x)	(unsigned int) hweight64((x) & 0xfffful)
-#define hweight8(x)	(unsigned int) hweight64((x) & 0xfful)
+#define __arch_hweight32(x) ((unsigned int) __arch_hweight64((x) & 0xfffffffful))
+#define __arch_hweight16(x) ((unsigned int) __arch_hweight64((x) & 0xfffful))
+#define __arch_hweight8(x)  ((unsigned int) __arch_hweight64((x) & 0xfful))
+
+#include <asm-generic/bitops/const_hweight.h>
 
 #endif /* __KERNEL__ */
 
arch/sparc/include/asm/bitops_64.h +6 -5
index e72ac9c..766121a 100644
@@ -44,7 +44,7 @@ extern void change_bit(unsigned long nr, volatile unsigned long *addr);
 
 #ifdef ULTRA_HAS_POPULATION_COUNT
 
-static inline unsigned int hweight64(unsigned long w)
+static inline unsigned int __arch_hweight64(unsigned long w)
 {
 	unsigned int res;
 
@@ -52,7 +52,7 @@ static inline unsigned int hweight64(unsigned long w)
 	return res;
 }
 
-static inline unsigned int hweight32(unsigned int w)
+static inline unsigned int __arch_hweight32(unsigned int w)
 {
 	unsigned int res;
 
@@ -60,7 +60,7 @@ static inline unsigned int hweight32(unsigned int w)
 	return res;
 }
 
-static inline unsigned int hweight16(unsigned int w)
+static inline unsigned int __arch_hweight16(unsigned int w)
 {
 	unsigned int res;
 
@@ -68,7 +68,7 @@ static inline unsigned int hweight16(unsigned int w)
 	return res;
 }
 
-static inline unsigned int hweight8(unsigned int w)
+static inline unsigned int __arch_hweight8(unsigned int w)
 {
 	unsigned int res;
 
@@ -78,9 +78,10 @@ static inline unsigned int hweight8(unsigned int w)
 
 #else
 
-#include <asm-generic/bitops/hweight.h>
+#include <asm-generic/bitops/arch_hweight.h>
 
 #endif
+#include <asm-generic/bitops/const_hweight.h>
 #include <asm-generic/bitops/lock.h>
 #endif /* __KERNEL__ */
 
arch/x86/Kconfig +5 -0
index 0eacb1f..89d8c54 100644
@@ -238,6 +238,11 @@ config X86_32_LAZY_GS
 	def_bool y
 	depends on X86_32 && !CC_STACKPROTECTOR
 
+config ARCH_HWEIGHT_CFLAGS
+	string
+	default "-fcall-saved-ecx -fcall-saved-edx" if X86_32
+	default "-fcall-saved-rdi -fcall-saved-rsi -fcall-saved-rdx -fcall-saved-rcx -fcall-saved-r8 -fcall-saved-r9 -fcall-saved-r10 -fcall-saved-r11" if X86_64
+
 config KTIME_SCALAR
 	def_bool X86_32
 source "init/Kconfig"
arch/x86/include/asm/alternative.h +6 -3
index b09ec55..67dae51 100644
@@ -39,9 +39,6 @@
 #define LOCK_PREFIX ""
 #endif
 
-/* This must be included *after* the definition of LOCK_PREFIX */
-#include <asm/cpufeature.h>
-
 struct alt_instr {
 	u8 *instr;		/* original instruction */
 	u8 *replacement;
@@ -96,6 +93,12 @@ static inline int alternatives_text_reserved(void *start, void *end)
       ".previous"
 
 /*
+ * This must be included *after* the definition of ALTERNATIVE due to
+ * <asm/arch_hweight.h>
+ */
+#include <asm/cpufeature.h>
+
+/*
  * Alternative instructions for different CPU types or capabilities.
  *
  * This allows to use optimized instructions even on generic binary
arch/x86/include/asm/arch_hweight.h new file +59 -0

59 lines not on this page · raw message

arch/x86/include/asm/bitops.h +3 -1
index 02b47a6..545776e 100644
@@ -444,7 +444,9 @@ static inline int fls(int x)
 
 #define ARCH_HAS_FAST_MULTIPLIER 1
 
-#include <asm-generic/bitops/hweight.h>
+#include <asm/arch_hweight.h>
+
+#include <asm-generic/bitops/const_hweight.h>
 
 #endif /* __KERNEL__ */
 
include/asm-generic/bitops/arch_hweight.h new file +25 -0
new file mode 100644
index 0000000..6a211f4
@@ -0,0 +1,25 @@
+#ifndef _ASM_GENERIC_BITOPS_ARCH_HWEIGHT_H_
+#define _ASM_GENERIC_BITOPS_ARCH_HWEIGHT_H_
+
+#include <asm/types.h>
+
+static inline unsigned int __arch_hweight32(unsigned int w)
+{
+	return __sw_hweight32(w);
+}
+
+static inline unsigned int __arch_hweight16(unsigned int w)
+{
+	return __sw_hweight16(w);
+}
+
+static inline unsigned int __arch_hweight8(unsigned int w)
+{
+	return __sw_hweight8(w);
+}
+
+static inline unsigned long __arch_hweight64(__u64 w)
+{
+	return __sw_hweight64(w);
+}
+#endif /* _ASM_GENERIC_BITOPS_HWEIGHT_H_ */
include/asm-generic/bitops/const_hweight.h new file +42 -0

42 lines not on this page · raw message

include/asm-generic/bitops/hweight.h +2 -6
index fbbc383..a94d651 100644
@@ -1,11 +1,7 @@
 #ifndef _ASM_GENERIC_BITOPS_HWEIGHT_H_
 #define _ASM_GENERIC_BITOPS_HWEIGHT_H_
 
-#include <asm/types.h>
-
-extern unsigned int hweight32(unsigned int w);
-extern unsigned int hweight16(unsigned int w);
-extern unsigned int hweight8(unsigned int w);
-extern unsigned long hweight64(__u64 w);
+#include <asm-generic/bitops/arch_hweight.h>
+#include <asm-generic/bitops/const_hweight.h>
 
 #endif /* _ASM_GENERIC_BITOPS_HWEIGHT_H_ */
include/linux/bitops.h +5 -25
index b793898..26caa60 100644
@@ -10,6 +10,11 @@
 #define BITS_TO_LONGS(nr)	DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
 #endif
 
+extern unsigned int __sw_hweight8(unsigned int w);
+extern unsigned int __sw_hweight16(unsigned int w);
+extern unsigned int __sw_hweight32(unsigned int w);
+extern unsigned long __sw_hweight64(__u64 w);
+
 /*
  * Include this here because some architectures need generic_ffs/fls in
  * scope
@@ -47,31 +52,6 @@ static inline unsigned long hweight_long(unsigned long w)
 	return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
 }
 
-/*
- * Clearly slow versions of the hweightN() functions, their benefit is
- * of course compile time evaluation of constant arguments.
- */
-#define HWEIGHT8(w)					\
-      (	BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) +	\
-	(!!((w) & (1ULL << 0))) +			\
-	(!!((w) & (1ULL << 1))) +			\
-	(!!((w) & (1ULL << 2))) +			\
-	(!!((w) & (1ULL << 3))) +			\
-	(!!((w) & (1ULL << 4))) +			\
-	(!!((w) & (1ULL << 5))) +			\
-	(!!((w) & (1ULL << 6))) +			\
-	(!!((w) & (1ULL << 7)))	)
-
-#define HWEIGHT16(w) (HWEIGHT8(w)  + HWEIGHT8((w) >> 8))
-#define HWEIGHT32(w) (HWEIGHT16(w) + HWEIGHT16((w) >> 16))
-#define HWEIGHT64(w) (HWEIGHT32(w) + HWEIGHT32((w) >> 32))
-
-/*
- * Type invariant version that simply casts things to the
- * largest type.
- */
-#define HWEIGHT(w)   HWEIGHT64((u64)(w))
-
 /**
  * rol32 - rotate a 32-bit value left
  * @word: value to rotate
lib/Makefile +3 -0
index 2e152ae..abe63a8 100644
@@ -39,7 +39,10 @@ lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o
 lib-$(CONFIG_GENERIC_FIND_FIRST_BIT) += find_next_bit.o
 lib-$(CONFIG_GENERIC_FIND_NEXT_BIT) += find_next_bit.o
 obj-$(CONFIG_GENERIC_FIND_LAST_BIT) += find_last_bit.o
+
+CFLAGS_hweight.o = $(subst $(quote),,$(CONFIG_ARCH_HWEIGHT_CFLAGS))
 obj-$(CONFIG_GENERIC_HWEIGHT) += hweight.o
+
 obj-$(CONFIG_LOCK_KERNEL) += kernel_lock.o
 obj-$(CONFIG_BTREE) += btree.o
 obj-$(CONFIG_DEBUG_PREEMPT) += smp_processor_id.o
lib/hweight.c +10 -9
index 63ee4eb..3c79d50 100644
@@ -9,7 +9,7 @@
  * The Hamming Weight of a number is the total number of bits set in it.
  */
 
-unsigned int hweight32(unsigned int w)
+unsigned int __sw_hweight32(unsigned int w)
 {
 #ifdef ARCH_HAS_FAST_MULTIPLIER
 	w -= (w >> 1) & 0x55555555;
@@ -24,29 +24,30 @@ unsigned int hweight32(unsigned int w)
 	return (res + (res >> 16)) & 0x000000FF;
 #endif
 }
-EXPORT_SYMBOL(hweight32);
+EXPORT_SYMBOL(__sw_hweight32);
 
-unsigned int hweight16(unsigned int w)
+unsigned int __sw_hweight16(unsigned int w)
 {
 	unsigned int res = w - ((w >> 1) & 0x5555);
 	res = (res & 0x3333) + ((res >> 2) & 0x3333);
 	res = (res + (res >> 4)) & 0x0F0F;
 	return (res + (res >> 8)) & 0x00FF;
 }
-EXPORT_SYMBOL(hweight16);
+EXPORT_SYMBOL(__sw_hweight16);
 
-unsigned int hweight8(unsigned int w)
+unsigned int __sw_hweight8(unsigned int w)
 {
 	unsigned int res = w - ((w >> 1) & 0x55);
 	res = (res & 0x33) + ((res >> 2) & 0x33);
 	return (res + (res >> 4)) & 0x0F;
 }
-EXPORT_SYMBOL(hweight8);
+EXPORT_SYMBOL(__sw_hweight8);
 
-unsigned long hweight64(__u64 w)
+unsigned long __sw_hweight64(__u64 w)
 {
 #if BITS_PER_LONG == 32
-	return hweight32((unsigned int)(w >> 32)) + hweight32((unsigned int)w);
+	return __sw_hweight32((unsigned int)(w >> 32)) +
+	       __sw_hweight32((unsigned int)w);
 #elif BITS_PER_LONG == 64
 #ifdef ARCH_HAS_FAST_MULTIPLIER
 	w -= (w >> 1) & 0x5555555555555555ul;
@@ -63,4 +64,4 @@ unsigned long hweight64(__u64 w)
 #endif
 #endif
 }
-EXPORT_SYMBOL(hweight64);
+EXPORT_SYMBOL(__sw_hweight64);
scripts/Makefile.lib +4 -0
index f9bdf26..cbcd654 100644
@@ -245,3 +245,7 @@ quiet_cmd_lzo = LZO    $@
 cmd_lzo = (cat $(filter-out FORCE,$^) | \
 	lzop -9 && $(call size_append, $(filter-out FORCE,$^))) > $@ || \
 	(rm -f $@ ; false)
+
+# misc stuff
+# ---------------------------------------------------------------------------
+quote:="
#1 lore raw
Quoted 6 lines · Ingo Molnar
arch/x86/include/asm/arch_hweight.h new file +5 -0
+#ifdef CONFIG_64BIT
+/* popcnt %rdi, %rax */
+#define POPCNT ".byte 0xf3,0x48,0x0f,0xb8,0xc7"
+#define REG_IN "D"
+#define REG_OUT "a"

...

Quoted 6 lines
arch/x86/include/asm/arch_hweight.h new file +6 -0
+/*
+ * __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.
+ */

arch/x86/include/asm/arch_hweight.h line 31

Quoted 10 lines
arch/x86/include/asm/arch_hweight.h new file +10 -0
+static inline unsigned int __arch_hweight32(unsigned int w)
+{
+	unsigned int res = 0;
+
+	asm (ALTERNATIVE("call __sw_hweight32", POPCNT, X86_FEATURE_POPCNT)
+		     : "="REG_OUT (res)
+		     : REG_IN (w));
+
+	return res;
+}

I do not believe this is correct.

On x86-64, you are using a 64-bit instruction, but

	REG_IN (w)

does _not_ guarantee that the register is zero in the high bits.

Yes, yes, in practice it _probably_ is, because the register almost certainly got loaded with some kind of zero-extending mov instruction. But as far as I can tell, the code is buggy. You're telling gcc that you are using a 32-bit register, but you're actually counting bits in the full 64 bits.

Am I missing something?

Linus

#2 lore raw
Quoted 35 lines · Linus Torvalds
Quoted 6 lines · Ingo Molnar
arch/x86/include/asm/arch_hweight.h new file +5 -0
+#ifdef CONFIG_64BIT
+/* popcnt %rdi, %rax */
+#define POPCNT ".byte 0xf3,0x48,0x0f,0xb8,0xc7"
+#define REG_IN "D"
+#define REG_OUT "a"

...

Quoted 6 lines
arch/x86/include/asm/arch_hweight.h new file +6 -0
+/*
+ * __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.
+ */

arch/x86/include/asm/arch_hweight.h line 31

Quoted 10 lines
arch/x86/include/asm/arch_hweight.h new file +10 -0
+static inline unsigned int __arch_hweight32(unsigned int w)
+{
+	unsigned int res = 0;
+
+	asm (ALTERNATIVE("call __sw_hweight32", POPCNT, X86_FEATURE_POPCNT)
+		     : "="REG_OUT (res)
+		     : REG_IN (w));
+
+	return res;
+}

I do not believe this is correct.

On x86-64, you are using a 64-bit instruction, but

	REG_IN (w)

does _not_ guarantee that the register is zero in the high bits.

Quoted 8 lines

Yes, yes, in practice it _probably_ is, because the register almost certainly got loaded with some kind of zero-extending mov instruction. But as far as I can tell, the code is buggy. You're telling gcc that you are using a 32-bit register, but you're actually counting bits in the full 64 bits.

Am I missing something?

No, you're absolutely right; the input can be any value including sign-extended. The best fix for this is probably to use a 32-bit instruction in this case -- I will check in such a patch and update the pull branch. If that's okay with you we'll send you a new pull request in a bit.

The embarrassing part is I think I spotted this problem at one point, but forgot about it in dealing with a conflict between this and another patchset.

-hpa

#3 lore raw

[tip:core/hweight] x86, hweight: Use a 32-bit popcnt for __arch_hweight32()

In reply to Linus Torvalds
Commit-ID:  c59bd5688299cddb71183e156e7a3c1409b90df2
Gitweb:     http://git.kernel.org/tip/c59bd5688299cddb71183e156e7a3c1409b90df2
Author:     H. Peter Anvin <[email protected]>
AuthorDate: Mon, 17 May 2010 15:13:23 -0700
Committer:  H. Peter Anvin <[email protected]>
CommitDate: Mon, 17 May 2010 15:17:16 -0700

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.

LKML-Reference: <[email protected]>

---
 arch/x86/include/asm/arch_hweight.h |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)
arch/x86/include/asm/arch_hweight.h +6 -4
index d1fc3c2..9686c3d 100644
@@ -2,13 +2,15 @@
 #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 POPCNT ".byte 0xf3,0x48,0x0f,0xb8,0xc7"
+#define POPCNT64 ".byte 0xf3,0x48,0x0f,0xb8,0xc7"
 #define REG_IN "D"
 #define REG_OUT "a"
 #else
 /* popcnt %eax, %eax */
-#define POPCNT ".byte 0xf3,0x0f,0xb8,0xc0"
+#define POPCNT32 ".byte 0xf3,0x0f,0xb8,0xc0"
 #define REG_IN "a"
 #define REG_OUT "a"
 #endif
@@ -23,7 +25,7 @@ static inline unsigned int __arch_hweight32(unsigned int w)
 {
 	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_IN (w));
 
@@ -48,7 +50,7 @@ static inline unsigned long __arch_hweight64(__u64 w)
 	return  __arch_hweight32((u32)w) +
 		__arch_hweight32((u32)(w >> 32));
 #else
-	asm (ALTERNATIVE("call __sw_hweight64", POPCNT, X86_FEATURE_POPCNT)
+	asm (ALTERNATIVE("call __sw_hweight64", POPCNT64, X86_FEATURE_POPCNT)
 		     : "="REG_OUT (res)
 		     : REG_IN (w));
 #endif /* CONFIG_X86_32 */
#4 lore raw

[GIT PULL] UPDATED - core/hweight changes for v2.6.35

In reply to Linus Torvalds

Hi Linus,

Here is the updated core/hweight tree.

The following changes since commit 0fdf86754f70e813845af4abaa805165ce57a0bb:
Linus Torvalds (1):

        Merge branch 'urgent' of git://git.kernel.org/.../brodo/pcmcia-2.6

are available in the git repository at:

git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip.git core-hweight-for-linus

Borislav Petkov (2):
x86: Add optimized popcnt variants
arch, hweight: Fix compilation errors

H. Peter Anvin (1):
x86, hweight: Use a 32-bit popcnt for __arch_hweight32()

Peter Zijlstra (1):
bitops: Optimize hweight() by making use of compile-time evaluation

 arch/alpha/include/asm/bitops.h            |   18 +++++----
 arch/ia64/include/asm/bitops.h             |   11 +++--
 arch/sparc/include/asm/bitops_64.h         |   11 +++--
 arch/x86/Kconfig                           |    5 ++
 arch/x86/include/asm/alternative.h         |    9 +++-
 arch/x86/include/asm/arch_hweight.h        |   61 ++++++++++++++++++++++++++++
 arch/x86/include/asm/bitops.h              |    4 +-
 include/asm-generic/bitops/arch_hweight.h  |   25 +++++++++++
 include/asm-generic/bitops/const_hweight.h |   42 +++++++++++++++++++
 include/asm-generic/bitops/hweight.h       |    8 +---
 include/linux/bitops.h                     |   30 ++-----------
 lib/Makefile                               |    3 +
 lib/hweight.c                              |   19 +++++----
 scripts/Makefile.lib                       |    4 ++
 14 files changed, 188 insertions(+), 62 deletions(-)
 create mode 100644 arch/x86/include/asm/arch_hweight.h
 create mode 100644 include/asm-generic/bitops/arch_hweight.h
 create mode 100644 include/asm-generic/bitops/const_hweight.h
arch/alpha/include/asm/bitops.h +10 -8
index 15f3ae2..296da1d 100644
@@ -405,29 +405,31 @@ static inline int fls(int x)
 
 #if defined(CONFIG_ALPHA_EV6) && defined(CONFIG_ALPHA_EV67)
 /* Whee.  EV67 can calculate it directly.  */
-static inline unsigned long hweight64(unsigned long w)
+static inline unsigned long __arch_hweight64(unsigned long w)
 {
 	return __kernel_ctpop(w);
 }
 
-static inline unsigned int hweight32(unsigned int w)
+static inline unsigned int __arch_weight32(unsigned int w)
 {
-	return hweight64(w);
+	return __arch_hweight64(w);
 }
 
-static inline unsigned int hweight16(unsigned int w)
+static inline unsigned int __arch_hweight16(unsigned int w)
 {
-	return hweight64(w & 0xffff);
+	return __arch_hweight64(w & 0xffff);
 }
 
-static inline unsigned int hweight8(unsigned int w)
+static inline unsigned int __arch_hweight8(unsigned int w)
 {
-	return hweight64(w & 0xff);
+	return __arch_hweight64(w & 0xff);
 }
 #else
-#include <asm-generic/bitops/hweight.h>
+#include <asm-generic/bitops/arch_hweight.h>
 #endif
 
+#include <asm-generic/bitops/const_hweight.h>
+
 #endif /* __KERNEL__ */
 
 #include <asm-generic/bitops/find.h>
arch/ia64/include/asm/bitops.h +6 -5
index 6ebc229..9da3df6 100644
@@ -437,17 +437,18 @@ __fls (unsigned long x)
  * hweightN: returns the hamming weight (i.e. the number
  * of bits set) of a N-bit word
  */
-static __inline__ unsigned long
-hweight64 (unsigned long x)
+static __inline__ unsigned long __arch_hweight64(unsigned long x)
 {
 	unsigned long result;
 	result = ia64_popcnt(x);
 	return result;
 }
 
-#define hweight32(x)	(unsigned int) hweight64((x) & 0xfffffffful)
-#define hweight16(x)	(unsigned int) hweight64((x) & 0xfffful)
-#define hweight8(x)	(unsigned int) hweight64((x) & 0xfful)
+#define __arch_hweight32(x) ((unsigned int) __arch_hweight64((x) & 0xfffffffful))
+#define __arch_hweight16(x) ((unsigned int) __arch_hweight64((x) & 0xfffful))
+#define __arch_hweight8(x)  ((unsigned int) __arch_hweight64((x) & 0xfful))
+
+#include <asm-generic/bitops/const_hweight.h>
 
 #endif /* __KERNEL__ */
 
arch/sparc/include/asm/bitops_64.h +6 -5
index e72ac9c..766121a 100644
@@ -44,7 +44,7 @@ extern void change_bit(unsigned long nr, volatile unsigned long *addr);
 
 #ifdef ULTRA_HAS_POPULATION_COUNT
 
-static inline unsigned int hweight64(unsigned long w)
+static inline unsigned int __arch_hweight64(unsigned long w)
 {
 	unsigned int res;
 
@@ -52,7 +52,7 @@ static inline unsigned int hweight64(unsigned long w)
 	return res;
 }
 
-static inline unsigned int hweight32(unsigned int w)
+static inline unsigned int __arch_hweight32(unsigned int w)
 {
 	unsigned int res;
 
@@ -60,7 +60,7 @@ static inline unsigned int hweight32(unsigned int w)
 	return res;
 }
 
-static inline unsigned int hweight16(unsigned int w)
+static inline unsigned int __arch_hweight16(unsigned int w)
 {
 	unsigned int res;
 
@@ -68,7 +68,7 @@ static inline unsigned int hweight16(unsigned int w)
 	return res;
 }
 
-static inline unsigned int hweight8(unsigned int w)
+static inline unsigned int __arch_hweight8(unsigned int w)
 {
 	unsigned int res;
 
@@ -78,9 +78,10 @@ static inline unsigned int hweight8(unsigned int w)
 
 #else
 
-#include <asm-generic/bitops/hweight.h>
+#include <asm-generic/bitops/arch_hweight.h>
 
 #endif
+#include <asm-generic/bitops/const_hweight.h>
 #include <asm-generic/bitops/lock.h>
 #endif /* __KERNEL__ */
 
arch/x86/Kconfig +5 -0
index 0eacb1f..89d8c54 100644
@@ -238,6 +238,11 @@ config X86_32_LAZY_GS
 	def_bool y
 	depends on X86_32 && !CC_STACKPROTECTOR
 
+config ARCH_HWEIGHT_CFLAGS
+	string
+	default "-fcall-saved-ecx -fcall-saved-edx" if X86_32
+	default "-fcall-saved-rdi -fcall-saved-rsi -fcall-saved-rdx -fcall-saved-rcx -fcall-saved-r8 -fcall-saved-r9 -fcall-saved-r10 -fcall-saved-r11" if X86_64
+
 config KTIME_SCALAR
 	def_bool X86_32
 source "init/Kconfig"
arch/x86/include/asm/alternative.h +6 -3
index b09ec55..67dae51 100644
@@ -39,9 +39,6 @@
 #define LOCK_PREFIX ""
 #endif
 
-/* This must be included *after* the definition of LOCK_PREFIX */
-#include <asm/cpufeature.h>
-
 struct alt_instr {
 	u8 *instr;		/* original instruction */
 	u8 *replacement;
@@ -96,6 +93,12 @@ static inline int alternatives_text_reserved(void *start, void *end)
       ".previous"
 
 /*
+ * This must be included *after* the definition of ALTERNATIVE due to
+ * <asm/arch_hweight.h>
+ */
+#include <asm/cpufeature.h>
+
+/*
  * Alternative instructions for different CPU types or capabilities.
  *
  * This allows to use optimized instructions even on generic binary
arch/x86/include/asm/arch_hweight.h new file +61 -0

61 lines not on this page · raw message

arch/x86/include/asm/bitops.h +3 -1
index 02b47a6..545776e 100644
@@ -444,7 +444,9 @@ static inline int fls(int x)
 
 #define ARCH_HAS_FAST_MULTIPLIER 1
 
-#include <asm-generic/bitops/hweight.h>
+#include <asm/arch_hweight.h>
+
+#include <asm-generic/bitops/const_hweight.h>
 
 #endif /* __KERNEL__ */
 
include/asm-generic/bitops/arch_hweight.h new file +25 -0
new file mode 100644
index 0000000..6a211f4
@@ -0,0 +1,25 @@
+#ifndef _ASM_GENERIC_BITOPS_ARCH_HWEIGHT_H_
+#define _ASM_GENERIC_BITOPS_ARCH_HWEIGHT_H_
+
+#include <asm/types.h>
+
+static inline unsigned int __arch_hweight32(unsigned int w)
+{
+	return __sw_hweight32(w);
+}
+
+static inline unsigned int __arch_hweight16(unsigned int w)
+{
+	return __sw_hweight16(w);
+}
+
+static inline unsigned int __arch_hweight8(unsigned int w)
+{
+	return __sw_hweight8(w);
+}
+
+static inline unsigned long __arch_hweight64(__u64 w)
+{
+	return __sw_hweight64(w);
+}
+#endif /* _ASM_GENERIC_BITOPS_HWEIGHT_H_ */
include/asm-generic/bitops/const_hweight.h new file +42 -0

42 lines not on this page · raw message

include/asm-generic/bitops/hweight.h +2 -6
index fbbc383..a94d651 100644
@@ -1,11 +1,7 @@
 #ifndef _ASM_GENERIC_BITOPS_HWEIGHT_H_
 #define _ASM_GENERIC_BITOPS_HWEIGHT_H_
 
-#include <asm/types.h>
-
-extern unsigned int hweight32(unsigned int w);
-extern unsigned int hweight16(unsigned int w);
-extern unsigned int hweight8(unsigned int w);
-extern unsigned long hweight64(__u64 w);
+#include <asm-generic/bitops/arch_hweight.h>
+#include <asm-generic/bitops/const_hweight.h>
 
 #endif /* _ASM_GENERIC_BITOPS_HWEIGHT_H_ */
include/linux/bitops.h +5 -25
index b793898..26caa60 100644
@@ -10,6 +10,11 @@
 #define BITS_TO_LONGS(nr)	DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
 #endif
 
+extern unsigned int __sw_hweight8(unsigned int w);
+extern unsigned int __sw_hweight16(unsigned int w);
+extern unsigned int __sw_hweight32(unsigned int w);
+extern unsigned long __sw_hweight64(__u64 w);
+
 /*
  * Include this here because some architectures need generic_ffs/fls in
  * scope
@@ -47,31 +52,6 @@ static inline unsigned long hweight_long(unsigned long w)
 	return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
 }
 
-/*
- * Clearly slow versions of the hweightN() functions, their benefit is
- * of course compile time evaluation of constant arguments.
- */
-#define HWEIGHT8(w)					\
-      (	BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) +	\
-	(!!((w) & (1ULL << 0))) +			\
-	(!!((w) & (1ULL << 1))) +			\
-	(!!((w) & (1ULL << 2))) +			\
-	(!!((w) & (1ULL << 3))) +			\
-	(!!((w) & (1ULL << 4))) +			\
-	(!!((w) & (1ULL << 5))) +			\
-	(!!((w) & (1ULL << 6))) +			\
-	(!!((w) & (1ULL << 7)))	)
-
-#define HWEIGHT16(w) (HWEIGHT8(w)  + HWEIGHT8((w) >> 8))
-#define HWEIGHT32(w) (HWEIGHT16(w) + HWEIGHT16((w) >> 16))
-#define HWEIGHT64(w) (HWEIGHT32(w) + HWEIGHT32((w) >> 32))
-
-/*
- * Type invariant version that simply casts things to the
- * largest type.
- */
-#define HWEIGHT(w)   HWEIGHT64((u64)(w))
-
 /**
  * rol32 - rotate a 32-bit value left
  * @word: value to rotate
lib/Makefile +3 -0
index 2e152ae..abe63a8 100644
@@ -39,7 +39,10 @@ lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o
 lib-$(CONFIG_GENERIC_FIND_FIRST_BIT) += find_next_bit.o
 lib-$(CONFIG_GENERIC_FIND_NEXT_BIT) += find_next_bit.o
 obj-$(CONFIG_GENERIC_FIND_LAST_BIT) += find_last_bit.o
+
+CFLAGS_hweight.o = $(subst $(quote),,$(CONFIG_ARCH_HWEIGHT_CFLAGS))
 obj-$(CONFIG_GENERIC_HWEIGHT) += hweight.o
+
 obj-$(CONFIG_LOCK_KERNEL) += kernel_lock.o
 obj-$(CONFIG_BTREE) += btree.o
 obj-$(CONFIG_DEBUG_PREEMPT) += smp_processor_id.o
lib/hweight.c +10 -9
index 63ee4eb..3c79d50 100644
@@ -9,7 +9,7 @@
  * The Hamming Weight of a number is the total number of bits set in it.
  */
 
-unsigned int hweight32(unsigned int w)
+unsigned int __sw_hweight32(unsigned int w)
 {
 #ifdef ARCH_HAS_FAST_MULTIPLIER
 	w -= (w >> 1) & 0x55555555;
@@ -24,29 +24,30 @@ unsigned int hweight32(unsigned int w)
 	return (res + (res >> 16)) & 0x000000FF;
 #endif
 }
-EXPORT_SYMBOL(hweight32);
+EXPORT_SYMBOL(__sw_hweight32);
 
-unsigned int hweight16(unsigned int w)
+unsigned int __sw_hweight16(unsigned int w)
 {
 	unsigned int res = w - ((w >> 1) & 0x5555);
 	res = (res & 0x3333) + ((res >> 2) & 0x3333);
 	res = (res + (res >> 4)) & 0x0F0F;
 	return (res + (res >> 8)) & 0x00FF;
 }
-EXPORT_SYMBOL(hweight16);
+EXPORT_SYMBOL(__sw_hweight16);
 
-unsigned int hweight8(unsigned int w)
+unsigned int __sw_hweight8(unsigned int w)
 {
 	unsigned int res = w - ((w >> 1) & 0x55);
 	res = (res & 0x33) + ((res >> 2) & 0x33);
 	return (res + (res >> 4)) & 0x0F;
 }
-EXPORT_SYMBOL(hweight8);
+EXPORT_SYMBOL(__sw_hweight8);
 
-unsigned long hweight64(__u64 w)
+unsigned long __sw_hweight64(__u64 w)
 {
 #if BITS_PER_LONG == 32
-	return hweight32((unsigned int)(w >> 32)) + hweight32((unsigned int)w);
+	return __sw_hweight32((unsigned int)(w >> 32)) +
+	       __sw_hweight32((unsigned int)w);
 #elif BITS_PER_LONG == 64
 #ifdef ARCH_HAS_FAST_MULTIPLIER
 	w -= (w >> 1) & 0x5555555555555555ul;
@@ -63,4 +64,4 @@ unsigned long hweight64(__u64 w)
 #endif
 #endif
 }
-EXPORT_SYMBOL(hweight64);
+EXPORT_SYMBOL(__sw_hweight64);
scripts/Makefile.lib +4 -0
index f9bdf26..cbcd654 100644
@@ -245,3 +245,7 @@ quiet_cmd_lzo = LZO    $@
 cmd_lzo = (cat $(filter-out FORCE,$^) | \
 	lzop -9 && $(call size_append, $(filter-out FORCE,$^))) > $@ || \
 	(rm -f $@ ; false)
+
+# misc stuff
+# ---------------------------------------------------------------------------
+quote:="
#5 lore raw
In reply to Ingo Molnar
Quoted 7 lines · Ingo Molnar

 arch/alpha/include/asm/bitops.h            |   18 +++++---- arch/ia64/include/asm/bitops.h             |   11 +++-- arch/sparc/include/asm/bitops_64.h         |   11 +++-- arch/x86/include/asm/bitops.h              |    4 +- include/asm-generic/bitops/arch_hweight.h  |   25 ++++++++++++ include/asm-generic/bitops/const_hweight.h |   42 ++++++++++++++++++++ include/asm-generic/bitops/hweight.h       |    8 +---

did this miss Blackfin because the original patch was against the 2.6.34 tree ? just wondering why it now build fails ...

doing a simple grep shows that the new "tile" arch may also be broken as it uses "hweight32" ...

considering __sw_hweightX only exist when the generic hweight is in play, wouldnt it make sense to have include/asm-generic/bitops/arch_hweight.h not always define things ? then most arches can simply pull in include/asm-generic/bitops/hweight.h without having to worry about the random inner details of hweight cruft.

(quoted diff) +0 -0

b/include/asm-generic/bitops/arch_hweight.h
index 6a211f4..3a62e38 100644

include/asm-generic/bitops/arch_hweight.h +10 -0
@@ -1,8 +1,15 @@
 #ifndef _ASM_GENERIC_BITOPS_ARCH_HWEIGHT_H_
 #define _ASM_GENERIC_BITOPS_ARCH_HWEIGHT_H_
 
+#ifdef CONFIG_GENERIC_HWEIGHT
+
 #include <asm/types.h>
 
+extern unsigned int __sw_hweight8(unsigned int w);
+extern unsigned int __sw_hweight16(unsigned int w);
+extern unsigned int __sw_hweight32(unsigned int w);
+extern unsigned long __sw_hweight64(__u64 w);
+
 static inline unsigned int __arch_hweight32(unsigned int w)
 {
    return __sw_hweight32(w);
@@ -22,4 +29,7 @@ static inline unsigned long __arch_hweight64(__u64 w)
 {
    return __sw_hweight64(w);
 }
+
+#endif /* CONFIG_GENERIC_HWEIGHT */
+
 #endif /* _ASM_GENERIC_BITOPS_HWEIGHT_H_ */
include/linux/bitops.h +0 -5
index fc68053..2e58012 100644
@@ -10,11 +10,6 @@
 #define BITS_TO_LONGS(nr)  DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
 #endif
 
-extern unsigned int __sw_hweight8(unsigned int w);
-extern unsigned int __sw_hweight16(unsigned int w);
-extern unsigned int __sw_hweight32(unsigned int w);
-extern unsigned long __sw_hweight64(__u64 w);
-
 /*
  * Include this here because some architectures need generic_ffs/fls in
  * scope

-mike

#6 lore raw
Quoted 15 lines · Mike Frysinger
Quoted 7 lines · Ingo Molnar
 arch/alpha/include/asm/bitops.h            |   18 +++++----
 arch/ia64/include/asm/bitops.h             |   11 +++--
 arch/sparc/include/asm/bitops_64.h         |   11 +++--
 arch/x86/include/asm/bitops.h              |    4 +-
 include/asm-generic/bitops/arch_hweight.h  |   25 ++++++++++++
 include/asm-generic/bitops/const_hweight.h |   42 ++++++++++++++++++++
 include/asm-generic/bitops/hweight.h       |    8 +---

did this miss Blackfin because the original patch was against the 2.6.34 tree ? just wondering why it now build fails ...

doing a simple grep shows that the new "tile" arch may also be broken as it uses "hweight32" ...

Quoted 7 lines

considering __sw_hweightX only exist when the generic hweight is in play, wouldnt it make sense to have include/asm-generic/bitops/arch_hweight.h not always define things ? then most arches can simply pull in include/asm-generic/bitops/hweight.h without having to worry about the random inner details of hweight cruft.

__sw_hweightX can exist even when generic hweight isn't in use per se, because the arch implementation can wrapper the software implementation.
This is the case on x86, for example -- most x86 CPUs don't have popcnt
yet, so on those the x86 implementation end up calling the __sw_hweight*() implementations.

-hpa

#7 lore raw
Quoted 24 lines · H. Peter Anvin
Quoted 15 lines · Mike Frysinger
Quoted 7 lines · Ingo Molnar

 arch/alpha/include/asm/bitops.h            |   18 +++++---- arch/ia64/include/asm/bitops.h             |   11 +++-- arch/sparc/include/asm/bitops_64.h         |   11 +++-- arch/x86/include/asm/bitops.h              |    4 +- include/asm-generic/bitops/arch_hweight.h  |   25 ++++++++++++ include/asm-generic/bitops/const_hweight.h |   42 ++++++++++++++++++++ include/asm-generic/bitops/hweight.h       |    8 +---

did this miss Blackfin because the original patch was against the 2.6.34 tree ?  just wondering why it now build fails ...

doing a simple grep shows that the new "tile" arch may also be broken as it uses "hweight32" ...

Quoted 7 lines

considering __sw_hweightX only exist when the generic hweight is in play, wouldnt it make sense to have include/asm-generic/bitops/arch_hweight.h not always define things ? then most arches can simply pull in include/asm-generic/bitops/hweight.h without having to worry about the random inner details of hweight cruft.

Quoted 5 lines

__sw_hweightX can exist even when generic hweight isn't in use per se, because the arch implementation can wrapper the software implementation. This is the case on x86, for example -- most x86 CPUs don't have popcnt yet, so on those the x86 implementation end up calling the __sw_hweight*() implementations.

but those targets still define CONFIG_GENERIC_HWEIGHT right ? so at the least, we should be wrapping the prototypes in linux/bitops.h with that ...
-mike

#8 lore raw
Quoted 5 lines · Mike Frysinger

but those targets still define CONFIG_GENERIC_HWEIGHT right ? so at the least, we should be wrapping the prototypes in linux/bitops.h with that ...

Yes, I guess they do.

-hpa

maintainer
#9 lore raw

From: "H. Peter Anvin" <[email protected]>
Date: Fri, Aug 13, 2010 at 05:11:14PM -0700

Quoted 8 lines · Mike Frysinger
Quoted 5 lines

but those targets still define CONFIG_GENERIC_HWEIGHT right ? so at the least, we should be wrapping the prototypes in linux/bitops.h with that ...

Yes, I guess they do.

Actually, Mike's issue is valid, if I understand it correctly. From looking at <arch/blackfin/include/asm/bitops.h>, Blackfin doesn't need the generic software variants __sw_hweightXX at all, right?

In that case, <include/linux/bitops.h> is pulling in needlessly unresolved symbols for arches which have their hw versions of hweight and don't need sw variants. However, if you want to include <asm-generic/bitops/hweight.h> and benefit from the compile-time evaluation of the const hweight args, you're going to need to rename the hweightXX() functions in <arch/blackfin/include/asm/bitops.h> to __arch_hweightXX but that shouldn't be a problem I guess :).

Let me give your patch from couple of mails ago a quick spin...

Signature · 2 lines
Regards/Gruss,
    Boris.
#10 lore raw

PATCH 1/1 arch/tile: Rename the hweight() implementations to __arch_hweight().

In reply to Mike Frysinger

See commit 1527bc8b928dd1399c3d3467dd47d9ede210978a.

---

Mike, thanks for the pointer to the hweight issue. Since we are using __builtin_popcount() and the compiler can do constant propagation, etc., with it at compile-time, this is less critical, but it seems like the easiest way to get the definitions of the HWEIGHT macros.

 arch/tile/include/asm/bitops.h |    9 +++++----
 1 files changed, 5 insertions(+), 4 deletions(-)
arch/tile/include/asm/bitops.h +5 -4
index 84600f3..6832b4b 100644
@@ -98,26 +98,27 @@ static inline int fls64(__u64 w)
 	return (sizeof(__u64) * 8) - __builtin_clzll(w);
 }
 
-static inline unsigned int hweight32(unsigned int w)
+static inline unsigned int __arch_hweight32(unsigned int w)
 {
 	return __builtin_popcount(w);
 }
 
-static inline unsigned int hweight16(unsigned int w)
+static inline unsigned int __arch_hweight16(unsigned int w)
 {
 	return __builtin_popcount(w & 0xffff);
 }
 
-static inline unsigned int hweight8(unsigned int w)
+static inline unsigned int __arch_hweight8(unsigned int w)
 {
 	return __builtin_popcount(w & 0xff);
 }
 
-static inline unsigned long hweight64(__u64 w)
+static inline unsigned long __arch_hweight64(__u64 w)
 {
 	return __builtin_popcountll(w);
 }
 
+#include <asm-generic/bitops/const_hweight.h>
 #include <asm-generic/bitops/lock.h>
 #include <asm-generic/bitops/sched.h>
 #include <asm-generic/bitops/ext2-non-atomic.h>
Signature · 1 line
1.6.5.2
#11 lore raw

Re: [PATCH] arch/tile: Rename the hweight() implementations to __arch_hweight().

Quoted 4 lines · Chris Metcalf

Mike, thanks for the pointer to the hweight issue.  Since we are using __builtin_popcount() and the compiler can do constant propagation, etc., with it at compile-time, this is less critical, but it seems like the easiest way to get the definitions of the HWEIGHT macros.

interesting ... i wasnt aware of the popcount builtin. the Blackfin ISA has a "ONES" insn which does this. guess we should implement the popcount builtin too.
-mike