Skip to contentSkip to conversation

x86/bitops: Introduce variable/constant pairs for __{set, clear, change}_bit()

PATCH In review Andrew Cooper 3 participants 3 messages Jan 11, 2018 – Jan 12, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/bitops: Introduce variable/constant pairs for __{set, clear, change}_bit()

Just as with test_bit, the non-atomic set/clear/change helpers can be better optimised by the compiler in the case that the nr parameter is constant, and it often is.

This results in a general replacement of mov $imm, %reg; bt* %reg, mem with the more simple op $imm, mem, reducing register pressure.

The net diffstat is:
add/remove: 0/1 grow/shrink: 5/17 up/down: 90/-301 (-211)

As a piece of minor cleanup, drop unnecessary brackets in the test_bit() macro, and fix the indentation.

---
CC: Jan Beulich <[email protected]>

---
 xen/include/asm-x86/bitops.h | 36 +++++++++++++++++++++++++++---------
 1 file changed, 27 insertions(+), 9 deletions(-)
xen/include/asm-x86/bitops.h +27 -9
index 440abb7..cbc7d36 100644
@@ -51,13 +51,19 @@ static inline void set_bit(int nr, volatile void *addr)
  * If it's called on the same region of memory simultaneously, the effect
  * may be that only one operation succeeds.
  */
-static inline void __set_bit(int nr, void *addr)
+static inline void variable_set_bit(int nr, void *addr)
 {
     asm volatile ( "btsl %1,%0" : "+m" (*(int *)addr) : "Ir" (nr) : "memory" );
 }
+static inline void constant_set_bit(int nr, void *addr)
+{
+    ((unsigned int *)addr)[nr >> 5] |= (1u << (nr & 31));
+}
 #define __set_bit(nr, addr) ({                          \
     if ( bitop_bad_size(addr) ) __bitop_bad_size();     \
-    __set_bit(nr, addr);                                \
+    __builtin_constant_p(nr) ?                          \
+        constant_set_bit(nr, addr) :                    \
+        variable_set_bit(nr, addr);                     \
 })
 
 /**
@@ -86,13 +92,19 @@ static inline void clear_bit(int nr, volatile void *addr)
  * If it's called on the same region of memory simultaneously, the effect
  * may be that only one operation succeeds.
  */
-static inline void __clear_bit(int nr, void *addr)
+static inline void variable_clear_bit(int nr, void *addr)
 {
     asm volatile ( "btrl %1,%0" : "+m" (*(int *)addr) : "Ir" (nr) : "memory" );
 }
+static inline void constant_clear_bit(int nr, void *addr)
+{
+    ((unsigned int *)addr)[nr >> 5] &= ~(1u << (nr & 31));
+}
 #define __clear_bit(nr, addr) ({                        \
     if ( bitop_bad_size(addr) ) __bitop_bad_size();     \
-    __clear_bit(nr, addr);                              \
+    __builtin_constant_p(nr) ?                          \
+        constant_clear_bit(nr, addr) :                  \
+        variable_clear_bit(nr, addr);                   \
 })
 
 /**
@@ -104,13 +116,19 @@ static inline void __clear_bit(int nr, void *addr)
  * If it's called on the same region of memory simultaneously, the effect
  * may be that only one operation succeeds.
  */
-static inline void __change_bit(int nr, void *addr)
+static inline void variable_change_bit(int nr, void *addr)
 {
     asm volatile ( "btcl %1,%0" : "+m" (*(int *)addr) : "Ir" (nr) : "memory" );
 }
+static inline void constant_change_bit(int nr, void *addr)
+{
+    ((unsigned int *)addr)[nr >> 5] ^= ~(1u << (nr & 31));
+}
 #define __change_bit(nr, addr) ({                       \
     if ( bitop_bad_size(addr) ) __bitop_bad_size();     \
-    __change_bit(nr, addr);                             \
+    __builtin_constant_p(nr) ?                          \
+        constant_change_bit(nr, addr) :                 \
+        variable_change_bit(nr, addr);                  \
 })
 
 /**
@@ -291,9 +309,9 @@ static inline int variable_test_bit(int nr, const volatile void *addr)
 
 #define test_bit(nr, addr) ({                           \
     if ( bitop_bad_size(addr) ) __bitop_bad_size();     \
-    (__builtin_constant_p(nr) ?                         \
-     constant_test_bit((nr),(addr)) :                   \
-     variable_test_bit((nr),(addr)));                   \
+    __builtin_constant_p(nr) ?                          \
+        constant_test_bit(nr, addr) :                   \
+        variable_test_bit(nr, addr);                    \
 })
 
 extern unsigned int __find_first_bit(
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Andrew Cooper

Just as with test_bit, the non-atomic set/clear/change helpers can be better
optimised by the compiler in the case that the nr parameter is constant, and
it often is.

This results in a general replacement of mov $imm, %reg; bt* %reg, mem with
the more simple op $imm, mem, reducing register pressure.

Quoted 9 lines

The net diffstat is:
add/remove: 0/1 grow/shrink: 5/17 up/down: 90/-301 (-211)

As a piece of minor cleanup, drop unnecessary brackets in the test_bit()
macro, and fix the indentation.

FWIW,

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Software Engineer @ SUSE https://www.suse.com/

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Reviewed-by on v1 1/1
#2 lore raw
In reply to Andrew Cooper
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 11.01.18 at 17:34, <[email protected]> wrote:

Just as with test_bit, the non-atomic set/clear/change helpers can be better optimised by the compiler in the case that the nr parameter is constant, and it often is.

This results in a general replacement of mov $imm, %reg; bt* %reg, mem with the more simple op $imm, mem, reducing register pressure.

Quoted 7 lines

The net diffstat is:
add/remove: 0/1 grow/shrink: 5/17 up/down: 90/-301 (-211)

As a piece of minor cleanup, drop unnecessary brackets in the test_bit() macro, and fix the indentation.

xen/include/asm-x86/bitops.h static inline int variable_test_bit(int nr, const volatile void *addr) · line 315

Quoted 11 lines
xen/include/asm-x86/bitops.h +3 -3
@@ -291,9 +309,9 @@ static inline int variable_test_bit(int nr, const volatile void *addr)
 
 #define test_bit(nr, addr) ({                           \
     if ( bitop_bad_size(addr) ) __bitop_bad_size();     \
-    (__builtin_constant_p(nr) ?                         \
-     constant_test_bit((nr),(addr)) :                   \
-     variable_test_bit((nr),(addr)));                   \
+    __builtin_constant_p(nr) ?                          \
+        constant_test_bit(nr, addr) :                   \
+        variable_test_bit(nr, addr);                    \
 })

I appreciate you also doing the cleanup here, yielding a consistent overall result.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel