Skip to contentSkip to conversation

x86: drop REG_OUT macro from hweight functions

PATCH In review Alexey Dobriyan 4 participants 7 messages Jul 28, 2019 – Aug 7, 2025 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: drop REG_OUT macro from hweight functions

Output register is always RAX/EAX.

---

 arch/x86/include/asm/arch_hweight.h |    6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
arch/x86/include/asm/arch_hweight.h +2 -4
@@ -6,10 +6,8 @@
 
 #ifdef CONFIG_64BIT
 #define REG_IN "D"
-#define REG_OUT "a"
 #else
 #define REG_IN "a"
-#define REG_OUT "a"
 #endif
 
 static __always_inline unsigned int __arch_hweight32(unsigned int w)
@@ -17,7 +15,7 @@ static __always_inline unsigned int __arch_hweight32(unsigned int w)
 	unsigned int res;
 
 	asm (ALTERNATIVE("call __sw_hweight32", "popcntl %1, %0", X86_FEATURE_POPCNT)
-			 : "="REG_OUT (res)
+			 : "=a" (res)
 			 : REG_IN (w));
 
 	return res;
@@ -45,7 +43,7 @@ static __always_inline unsigned long __arch_hweight64(__u64 w)
 	unsigned long res;
 
 	asm (ALTERNATIVE("call __sw_hweight64", "popcntq %1, %0", X86_FEATURE_POPCNT)
-			 : "="REG_OUT (res)
+			 : "=a" (res)
 			 : REG_IN (w));
 
 	return res;
#1 lore raw
Quoted 31 lines · Alexey Dobriyan

Output register is always RAX/EAX.

---

 arch/x86/include/asm/arch_hweight.h |    6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
arch/x86/include/asm/arch_hweight.h +1 -3
@@ -6,10 +6,8 @@
 
 #ifdef CONFIG_64BIT
 #define REG_IN "D"
-#define REG_OUT "a"
 #else
 #define REG_IN "a"
-#define REG_OUT "a"
 #endif
 
 static __always_inline unsigned int __arch_hweight32(unsigned int w)
@@ -17,7 +15,7 @@ static __always_inline unsigned int __arch_hweight32(unsigned int w)
 	unsigned int res;
 
 	asm (ALTERNATIVE("call __sw_hweight32", "popcntl %1, %0", X86_FEATURE_POPCNT)
-			 : "="REG_OUT (res)
+			 : "=a" (res)
 			 : REG_IN (w));
 
 	return res;

arch/x86/include/asm/arch_hweight.h static __always_inline unsigned long __arch_hweight64(__u64 w) · line 49

Quoted 8 lines
arch/x86/include/asm/arch_hweight.h +1 -1
@@ -45,7 +43,7 @@ static __always_inline unsigned long __arch_hweight64(__u64 w)
 	unsigned long res;
 
 	asm (ALTERNATIVE("call __sw_hweight64", "popcntq %1, %0", X86_FEATURE_POPCNT)
-			 : "="REG_OUT (res)
+			 : "=a" (res)
 			 : REG_IN (w));
 
 	return res;

I _think_ something like the below should also work:

(fwiw _ASM_ARG 5 and 6 are broken, as are all the QLWB variants)

---

arch/x86/include/asm/arch_hweight.h +5 -12
index ba88edd0d58b..88704612b2f7 100644
@@ -3,22 +3,15 @@
 #define _ASM_X86_HWEIGHT_H
 
 #include <asm/cpufeatures.h>
-
-#ifdef CONFIG_64BIT
-#define REG_IN "D"
-#define REG_OUT "a"
-#else
-#define REG_IN "a"
-#define REG_OUT "a"
-#endif
+#include <asm/asm.h>
 
 static __always_inline unsigned int __arch_hweight32(unsigned int w)
 {
 	unsigned int res;
 
 	asm (ALTERNATIVE("call __sw_hweight32", "popcntl %1, %0", X86_FEATURE_POPCNT)
-			 : "="REG_OUT (res)
-			 : REG_IN (w));
+			 : "=a" (res)
+			 : _ASM_ARG1 (w));
 
 	return res;
 }
@@ -45,8 +38,8 @@ static __always_inline unsigned long __arch_hweight64(__u64 w)
 	unsigned long res;
 
 	asm (ALTERNATIVE("call __sw_hweight64", "popcntq %1, %0", X86_FEATURE_POPCNT)
-			 : "="REG_OUT (res)
-			 : REG_IN (w));
+			 : "=a" (res)
+			 : _ASM_ARG1 (w));
 
 	return res;
 }
#2 lore raw
Quoted 3 lines · Peter Zijlstra

I _think_ something like the below should also work:

(fwiw _ASM_ARG 5 and 6 are broken, as are all the QLWB variants)

Fixed that, because

Quoted 18 lines

---

arch/x86/include/asm/arch_hweight.h +1 -8
index ba88edd0d58b..88704612b2f7 100644
@@ -3,22 +3,15 @@
 #define _ASM_X86_HWEIGHT_H
 
 #include <asm/cpufeatures.h>
-
-#ifdef CONFIG_64BIT
-#define REG_IN "D"
-#define REG_OUT "a"
-#else
-#define REG_IN "a"
-#define REG_OUT "a"
-#endif
+#include <asm/asm.h>

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

Quoted 10 lines
arch/x86/include/asm/arch_hweight.h +2 -2
 
 static __always_inline unsigned int __arch_hweight32(unsigned int w)
 {
 	unsigned int res;
 
 	asm (ALTERNATIVE("call __sw_hweight32", "popcntl %1, %0", X86_FEATURE_POPCNT)
-			 : "="REG_OUT (res)
-			 : REG_IN (w));
+			 : "=a" (res)
+			 : _ASM_ARG1 (w));

That needs _ASM_ARG1L because popcntl..

---

arch/x86/include/asm/arch_hweight.h +5 -12
index ba88edd0d58b..3cab7f382a43 100644
@@ -3,22 +3,15 @@
 #define _ASM_X86_HWEIGHT_H
 
 #include <asm/cpufeatures.h>
-
-#ifdef CONFIG_64BIT
-#define REG_IN "D"
-#define REG_OUT "a"
-#else
-#define REG_IN "a"
-#define REG_OUT "a"
-#endif
+#include <asm/asm.h>
 
 static __always_inline unsigned int __arch_hweight32(unsigned int w)
 {
 	unsigned int res;
 
 	asm (ALTERNATIVE("call __sw_hweight32", "popcntl %1, %0", X86_FEATURE_POPCNT)
-			 : "="REG_OUT (res)
-			 : REG_IN (w));
+			 : "=a" (res)
+			 : _ASM_ARG1L (w));
 
 	return res;
 }
@@ -45,8 +38,8 @@ static __always_inline unsigned long __arch_hweight64(__u64 w)
 	unsigned long res;
 
 	asm (ALTERNATIVE("call __sw_hweight64", "popcntq %1, %0", X86_FEATURE_POPCNT)
-			 : "="REG_OUT (res)
-			 : REG_IN (w));
+			 : "=a" (res)
+			 : _ASM_ARG1 (w));
 
 	return res;
 }
arch/x86/include/asm/asm.h +32 -39

85 lines not on this page · raw message

#3 lore raw

arch/x86/include/asm/asm.h line 97

Quoted 6 lines · Peter Zijlstra
arch/x86/include/asm/asm.h +6 -0
+#define _ASM_ARG1B	__ASM_FORM_RAW(dil)
+#define _ASM_ARG2B	__ASM_FORM_RAW(sil)
+#define _ASM_ARG3B	__ASM_FORM_RAW(dl)
+#define _ASM_ARG4B	__ASM_FORM_RAW(cl)
+#define _ASM_ARG5B	__ASM_FORM_RAW(r8b)
+#define _ASM_ARG6B	__ASM_FORM_RAW(r9b)

I preprocessed percpu code once to see what precisely it does because it was easier than wading through forest of macroes.

Hopefully x86 assembly won't get to that level.

#4 lore raw
Quoted 10 lines · Alexey Dobriyan

arch/x86/include/asm/asm.h line 97

Quoted 6 lines · Peter Zijlstra
arch/x86/include/asm/asm.h +6 -0
+#define _ASM_ARG1B	__ASM_FORM_RAW(dil)
+#define _ASM_ARG2B	__ASM_FORM_RAW(sil)
+#define _ASM_ARG3B	__ASM_FORM_RAW(dl)
+#define _ASM_ARG4B	__ASM_FORM_RAW(cl)
+#define _ASM_ARG5B	__ASM_FORM_RAW(r8b)
+#define _ASM_ARG6B	__ASM_FORM_RAW(r9b)

I preprocessed percpu code once to see what precisely it does because it was easier than wading through forest of macroes.

Per cpu is easy, try reading the tracepoint code ;-)

#5 lore raw
Quoted 12 lines · Peter Zijlstra
Quoted 10 lines · Alexey Dobriyan

arch/x86/include/asm/asm.h line 97

Quoted 6 lines · Peter Zijlstra
arch/x86/include/asm/asm.h +6 -0
+#define _ASM_ARG1B	__ASM_FORM_RAW(dil)
+#define _ASM_ARG2B	__ASM_FORM_RAW(sil)
+#define _ASM_ARG3B	__ASM_FORM_RAW(dl)
+#define _ASM_ARG4B	__ASM_FORM_RAW(cl)
+#define _ASM_ARG5B	__ASM_FORM_RAW(r8b)
+#define _ASM_ARG6B	__ASM_FORM_RAW(r9b)

I preprocessed percpu code once to see what precisely it does because it was easier than wading through forest of macroes.

Quoted 1 line

Per cpu is easy, try reading the tracepoint code ;-)

Sometimes it's the .s file one ends up wanting to check out...

Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
maintainer
#6 lore raw
In reply to Peter Zijlstra
Quoted 31 lines · Peter Zijlstra
Quoted 3 lines · Peter Zijlstra

I _think_ something like the below should also work:

(fwiw _ASM_ARG 5 and 6 are broken, as are all the QLWB variants)

Fixed that, because

Quoted 23 lines

---

arch/x86/include/asm/arch_hweight.h +1 -8
index ba88edd0d58b..88704612b2f7 100644
@@ -3,22 +3,15 @@
 #define _ASM_X86_HWEIGHT_H
 
 #include <asm/cpufeatures.h>
-
-#ifdef CONFIG_64BIT
-#define REG_IN "D"
-#define REG_OUT "a"
-#else
-#define REG_IN "a"
-#define REG_OUT "a"
-#endif
+#include <asm/asm.h>
 
 static __always_inline unsigned int __arch_hweight32(unsigned int w)
 {
 	unsigned int res;
 
Quoted 7 lines

arch/x86/include/asm/arch_hweight.h

Quoted 5 lines
arch/x86/include/asm/arch_hweight.h +2 -2
 	asm (ALTERNATIVE("call __sw_hweight32", "popcntl %1, %0", X86_FEATURE_POPCNT)
-			 : "="REG_OUT (res)
-			 : REG_IN (w));
+			 : "=a" (res)
+			 : _ASM_ARG1 (w));

That needs _ASM_ARG1L because popcntl..

Frankly, the ASM_ARG* thing is less readable than simply having "=a" there.

ASM_ARG<number> naming needs optimizing.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette