Skip to contentSkip to conversation

xen/bitops: hweight() cleanup/improvements

PATCH In review Andrew Cooper 5 participants 18 messages Sep 4, 2024 – Sep 13, 2024

lore
Share & tools
lore
#0 lore raw

v3 cover xen/bitops: hweight() cleanup/improvements

The next phase of bitops cleanup. This series:

1) Untangles the mess around hweight()
2) Removes some unwise uses of hweight()
3) Makes it work transparently for RISC-V
4) Use the POPCNT instruction on x86 when available

6 patches from the v2 posting has been committed. See the remainder for v3 changes.

Andrew Cooper (5):
xen/bitops: Reinstate the please tidy message xen/bitops: Drop the remnants of hweight{8,16}() xen/bitops: Implement hweight32() in terms of hweightl() xen/bitops: Implement hweight64() in terms of hweight{l,32}() x86/bitops: Use the POPCNT instruction when available

 xen/arch/arm/include/asm/bitops.h | 11 -----
 xen/arch/ppc/include/asm/bitops.h | 11 -----
 xen/arch/x86/include/asm/bitops.h | 32 +++++++++-----
 xen/common/bitops.c               |  3 ++
 xen/include/xen/bitops.h          | 69 +++++++-----------------------
 xen/lib/Makefile                  |  1 +
 xen/lib/x86-generic-hweightl.c    | 71 +++++++++++++++++++++++++++++++
 7 files changed, 112 insertions(+), 86 deletions(-)
 create mode 100644 xen/lib/x86-generic-hweightl.c
Signature · 1 line
2.39.2
#1 lore raw

PATCH v3 1/5 xen/bitops: Reinstate the please tidy message

Recent additions have undone prior tidying at the top of the file.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Oleksii Kurochko <[email protected]>
CC: Shawn Anastasio <[email protected]>

---
 xen/include/xen/bitops.h | 2 ++
 1 file changed, 2 insertions(+)
xen/include/xen/bitops.h +2 -0
index ed6b9ccc724f..6f8e36f1c755 100644
@@ -210,6 +210,8 @@ static always_inline bool test_bit(int nr, const volatile void *addr)
     test_bit(nr, addr);                                 \
 })
 
+/* --------------------- Please tidy above here --------------------- */
+
 static always_inline attr_const unsigned int ffs(unsigned int x)
 {
     if ( __builtin_constant_p(x) )
Signature · 1 line
2.39.2
#2 lore raw

Re: [PATCH v3 1/5] xen/bitops: Reinstate the please tidy message

Quoted 3 lines · Andrew Cooper

Recent additions have undone prior tidying at the top of the file.

Looks like despite not getting any ack (nor comments) for this in earlier rounds, you still want to keep it and you don't want to re-base subsequent changes ahead. Which is a pity; I hoped I'd get around opening up another controversy here. I'm afraid I disagree with the underlying thinking in this particular case, or perhaps better with what I assume the underlying thinking is. First: What is it that was undone? I can't spot it.

xen/include/xen/bitops.h static always_inline bool test_bit(int nr, const volatile void *addr) · line 217

Quoted 11 lines
xen/include/xen/bitops.h +2 -0
@@ -210,6 +210,8 @@ static always_inline bool test_bit(int nr, const volatile void *addr)
     test_bit(nr, addr);                                 \
 })
 
+/* --------------------- Please tidy above here --------------------- */
+
 static always_inline attr_const unsigned int ffs(unsigned int x)
 {
     if ( __builtin_constant_p(x) )

Second: How is the code above that marker any better or worse than the code below the marker? (There are differences, yet that alone doesn't make the earlier code untidy.)

Taken together: If you really think we need such markers, I'm afraid they need to come with at least an outline of what wants tidying in which way. Unless that's (sufficiently) obvious, like imo it is with the somewhat similar (in intentions) marker in msr-index.h.

Without it being clear what wants doing, I think such markers would better be omitted.

Jan

Acked-by on v3 1/5
#3 lore raw

Re: [PATCH v3 1/5] xen/bitops: Reinstate the please tidy message

In reply to Andrew Cooper
Quoted 3 lines · Andrew Cooper

Recent additions have undone prior tidying at the top of the file.

Hi Andrew,

I admit I don't understand the value and the meaning of the "Please tidy above here" comments.

However, you got one just like this one committed with ea59e7d780d9 and then removed by 5f7606c048f7. It looks like they are useful to you. As you are the one doing the tiding, and given that you had one already before:

Quoted 30 lines

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Oleksii Kurochko <[email protected]>
CC: Shawn Anastasio <[email protected]>

---
 xen/include/xen/bitops.h | 2 ++
 1 file changed, 2 insertions(+)
xen/include/xen/bitops.h +2 -0
index ed6b9ccc724f..6f8e36f1c755 100644
@@ -210,6 +210,8 @@ static always_inline bool test_bit(int nr, const volatile void *addr)
     test_bit(nr, addr);                                 \
 })
 
+/* --------------------- Please tidy above here --------------------- */
+
 static always_inline attr_const unsigned int ffs(unsigned int x)
 {
     if ( __builtin_constant_p(x) )
Signature · 1 line
2.39.2
#4 lore raw

PATCH v3 2/5 xen/bitops: Drop the remnants of hweight{8,16}()

In reply to Andrew Cooper

They are no more. No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Oleksii Kurochko <[email protected]>
CC: Shawn Anastasio <[email protected]>

---
 xen/arch/arm/include/asm/bitops.h |  2 --
 xen/arch/ppc/include/asm/bitops.h |  2 --
 xen/arch/x86/include/asm/bitops.h |  2 --
 xen/include/xen/bitops.h          | 17 -----------------
 4 files changed, 23 deletions(-)
xen/arch/arm/include/asm/bitops.h +0 -2
index 3c023103f734..91cd167b6bbb 100644
@@ -86,8 +86,6 @@ bool clear_mask16_timeout(uint16_t mask, volatile void *p,
  */
 #define hweight64(x) generic_hweight64(x)
 #define hweight32(x) generic_hweight32(x)
-#define hweight16(x) generic_hweight16(x)
-#define hweight8(x) generic_hweight8(x)
 
 #endif /* _ARM_BITOPS_H */
 /*
xen/arch/ppc/include/asm/bitops.h +0 -2
index 6e4e112b8b92..64512e949530 100644
@@ -134,7 +134,5 @@ static inline int test_and_set_bit(unsigned int nr, volatile void *addr)
  */
 #define hweight64(x) __builtin_popcountll(x)
 #define hweight32(x) __builtin_popcount(x)
-#define hweight16(x) __builtin_popcount((uint16_t)(x))
-#define hweight8(x)  __builtin_popcount((uint8_t)(x))
 
 #endif /* _ASM_PPC_BITOPS_H */
xen/arch/x86/include/asm/bitops.h +0 -2
index 8c0403405aa2..4c5b21907a64 100644
@@ -483,7 +483,5 @@ static always_inline unsigned int arch_flsl(unsigned long x)
  */
 #define hweight64(x) generic_hweight64(x)
 #define hweight32(x) generic_hweight32(x)
-#define hweight16(x) generic_hweight16(x)
-#define hweight8(x) generic_hweight8(x)
 
 #endif /* _X86_BITOPS_H */
xen/include/xen/bitops.h +0 -17
index 6f8e36f1c755..58c600155f7e 100644
@@ -413,23 +413,6 @@ static inline unsigned int generic_hweight32(unsigned int w)
     return (w + (w >> 16)) & 0xff;
 }
 
-static inline unsigned int generic_hweight16(unsigned int w)
-{
-    w -= ((w >> 1) & 0x5555);
-    w =  (w & 0x3333) + ((w >> 2) & 0x3333);
-    w =  (w + (w >> 4)) & 0x0f0f;
-
-    return (w + (w >> 8)) & 0xff;
-}
-
-static inline unsigned int generic_hweight8(unsigned int w)
-{
-    w -= ((w >> 1) & 0x55);
-    w =  (w & 0x33) + ((w >> 2) & 0x33);
-
-    return (w + (w >> 4)) & 0x0f;
-}
-
 static inline unsigned int generic_hweight64(uint64_t w)
 {
     if ( BITS_PER_LONG < 64 )
Signature · 1 line
2.39.2
#8 lore raw

PATCH v3 3/5 xen/bitops: Implement hweight32() in terms of hweightl()

In reply to Andrew Cooper

... and drop generic_hweight32().

As noted previously, the only two users of hweight32() are in __init paths.

The int-optimised form of generic_hweight() is only two instructions shorter than the long-optimised form, and even then only on architectures which lack fast multiplication, so there's no point providing an int-optimised form.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Oleksii Kurochko <[email protected]>
CC: Shawn Anastasio <[email protected]>

v2:
* Reorder with respect to the hweight64() patch
* Rerwrite the commit message
* s/__pure/attr_const/

---
 xen/arch/arm/include/asm/bitops.h | 1 -
 xen/arch/ppc/include/asm/bitops.h | 1 -
 xen/arch/x86/include/asm/bitops.h | 1 -
 xen/include/xen/bitops.h          | 5 +++++
 4 files changed, 5 insertions(+), 3 deletions(-)
xen/arch/arm/include/asm/bitops.h +0 -1
index 91cd167b6bbb..b28c25b3d52d 100644
@@ -85,7 +85,6 @@ bool clear_mask16_timeout(uint16_t mask, volatile void *p,
  * The Hamming Weight of a number is the total number of bits set in it.
  */
 #define hweight64(x) generic_hweight64(x)
-#define hweight32(x) generic_hweight32(x)
 
 #endif /* _ARM_BITOPS_H */
 /*
xen/arch/ppc/include/asm/bitops.h +0 -1
index 64512e949530..f488a7c03425 100644
@@ -133,6 +133,5 @@ static inline int test_and_set_bit(unsigned int nr, volatile void *addr)
  * The Hamming Weight of a number is the total number of bits set in it.
  */
 #define hweight64(x) __builtin_popcountll(x)
-#define hweight32(x) __builtin_popcount(x)
 
 #endif /* _ASM_PPC_BITOPS_H */
xen/arch/x86/include/asm/bitops.h +0 -1
index 4c5b21907a64..507b043b8a86 100644
@@ -482,6 +482,5 @@ static always_inline unsigned int arch_flsl(unsigned long x)
  * The Hamming Weight of a number is the total number of bits set in it.
  */
 #define hweight64(x) generic_hweight64(x)
-#define hweight32(x) generic_hweight32(x)
 
 #endif /* _X86_BITOPS_H */
xen/include/xen/bitops.h +5 -0
index 58c600155f7e..a462c3065158 100644
@@ -326,6 +326,11 @@ static always_inline attr_const unsigned int hweightl(unsigned long x)
 #endif
 }
 
+static always_inline attr_const unsigned int hweight32(uint32_t x)
+{
+    return hweightl(x);
+}
+
 /* --------------------- Please tidy below here --------------------- */
 
 #ifndef find_next_bit
Signature · 1 line
2.39.2
Acked-by on v3 3/5
#9 lore raw

Re: [PATCH v3 3/5] xen/bitops: Implement hweight32() in terms of hweightl()

Quoted 4 lines · Andrew Cooper

... and drop generic_hweight32().

As noted previously, the only two users of hweight32() are in __init paths.

Quoted 8 lines

The int-optimised form of generic_hweight() is only two instructions shorter than the long-optimised form, and even then only on architectures which lack fast multiplication, so there's no point providing an int-optimised form.

No functional change.

The patch is OK:

I was looking at docs/misra/C-language-toolchain.rst to make sure everything is listed there. We have attr_const as "__const__" noted among "Non-standard tokens".

Looks like we need to add __always_inline__ ?

Quoted 75 lines

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Oleksii Kurochko <[email protected]>
CC: Shawn Anastasio <[email protected]>

v2:
* Reorder with respect to the hweight64() patch
* Rerwrite the commit message
* s/__pure/attr_const/

---
 xen/arch/arm/include/asm/bitops.h | 1 -
 xen/arch/ppc/include/asm/bitops.h | 1 -
 xen/arch/x86/include/asm/bitops.h | 1 -
 xen/include/xen/bitops.h          | 5 +++++
 4 files changed, 5 insertions(+), 3 deletions(-)
xen/arch/arm/include/asm/bitops.h +0 -1
index 91cd167b6bbb..b28c25b3d52d 100644
@@ -85,7 +85,6 @@ bool clear_mask16_timeout(uint16_t mask, volatile void *p,
  * The Hamming Weight of a number is the total number of bits set in it.
  */
 #define hweight64(x) generic_hweight64(x)
-#define hweight32(x) generic_hweight32(x)
 
 #endif /* _ARM_BITOPS_H */
 /*
xen/arch/ppc/include/asm/bitops.h +0 -1
index 64512e949530..f488a7c03425 100644
@@ -133,6 +133,5 @@ static inline int test_and_set_bit(unsigned int nr, volatile void *addr)
  * The Hamming Weight of a number is the total number of bits set in it.
  */
 #define hweight64(x) __builtin_popcountll(x)
-#define hweight32(x) __builtin_popcount(x)
 
 #endif /* _ASM_PPC_BITOPS_H */
xen/arch/x86/include/asm/bitops.h +0 -1
index 4c5b21907a64..507b043b8a86 100644
@@ -482,6 +482,5 @@ static always_inline unsigned int arch_flsl(unsigned long x)
  * The Hamming Weight of a number is the total number of bits set in it.
  */
 #define hweight64(x) generic_hweight64(x)
-#define hweight32(x) generic_hweight32(x)
 
 #endif /* _X86_BITOPS_H */
xen/include/xen/bitops.h +5 -0
index 58c600155f7e..a462c3065158 100644
@@ -326,6 +326,11 @@ static always_inline attr_const unsigned int hweightl(unsigned long x)
 #endif
 }
 
+static always_inline attr_const unsigned int hweight32(uint32_t x)
+{
+    return hweightl(x);
+}
+
 /* --------------------- Please tidy below here --------------------- */
 
 #ifndef find_next_bit
Signature · 1 line
2.39.2
#10 lore raw

Re: [PATCH v3 3/5] xen/bitops: Implement hweight32() in terms of hweightl()

Quoted 9 lines · Stefano Stabellini
Quoted 8 lines · Andrew Cooper

... and drop generic_hweight32().

As noted previously, the only two users of hweight32() are in __init paths.

The int-optimised form of generic_hweight() is only two instructions shorter than the long-optimised form, and even then only on architectures which lack fast multiplication, so there's no point providing an int-optimised form.

Quoted 7 lines
Quoted 4 lines

No functional change.

The patch is OK:

Thanks.

Quoted 5 lines

I was looking at docs/misra/C-language-toolchain.rst to make sure everything is listed there. We have attr_const as "__const__" noted among "Non-standard tokens".

Looks like we need to add __always_inline__ ?

Luckily, no.

First, the __const__ referred to there is GCC's alternative spelling of the regular C 'const' keyword, and not the function attribute by the same name.

But, non-standard tokens are about things which survive full preprocessing and are still not standard C.  In this case, it's the __attribute__ that matters, not what's in it, and this is why we don't have a hundreds of rows in that table for __attribute__((foo, bar, baz)).

That said, I think f96e2f64576cdbb147391c7cb399d393385719a9 probably should have removed the entire row including __const__ seeing as AFAICT the last user in Xen was dropped in 1aa3c54a31a5 in 2008

~Andrew

#11 lore raw

Re: [PATCH v3 3/5] xen/bitops: Implement hweight32() in terms of hweightl()

Quoted 30 lines · Andrew Cooper
Quoted 9 lines · Stefano Stabellini
Quoted 8 lines · Andrew Cooper

... and drop generic_hweight32().

As noted previously, the only two users of hweight32() are in __init paths.

The int-optimised form of generic_hweight() is only two instructions shorter than the long-optimised form, and even then only on architectures which lack fast multiplication, so there's no point providing an int-optimised form.

Quoted 7 lines
Quoted 4 lines

No functional change.

The patch is OK:

Thanks.

Quoted 5 lines

I was looking at docs/misra/C-language-toolchain.rst to make sure everything is listed there. We have attr_const as "__const__" noted among "Non-standard tokens".

Looks like we need to add __always_inline__ ?

Luckily, no.

First, the __const__ referred to there is GCC's alternative spelling of the regular C 'const' keyword, and not the function attribute by the same name.

Quoted 8 lines

But, non-standard tokens are about things which survive full preprocessing and are still not standard C.  In this case, it's the __attribute__ that matters, not what's in it, and this is why we don't have a hundreds of rows in that table for __attribute__((foo, bar, baz)).

That said, I think f96e2f64576cdbb147391c7cb399d393385719a9 probably should have removed the entire row including __const__ seeing as AFAICT the last user in Xen was dropped in 1aa3c54a31a5 in 2008

Hmm.  Eclair disagrees.

https://gitlab.com/xen-project/people/andyhhp/xen/-/jobs/7765315981

Now I'm completely confused as to why __const__ matters and everything else inside an __attribute__(()) apparently doesn't.

CC-ing some folk.  Any ideas?

~Andrew

#12 lore raw

Re: [PATCH v3 3/5] xen/bitops: Implement hweight32() in terms of hweightl()

Quoted 42 lines · Andrew Cooper
Quoted 30 lines · Andrew Cooper
Quoted 9 lines · Stefano Stabellini
Quoted 8 lines · Andrew Cooper

... and drop generic_hweight32().

As noted previously, the only two users of hweight32() are in __init paths.

The int-optimised form of generic_hweight() is only two instructions shorter than the long-optimised form, and even then only on architectures which lack fast multiplication, so there's no point providing an int-optimised form.

Quoted 7 lines
Quoted 4 lines

No functional change.

The patch is OK:

Thanks.

Quoted 5 lines

I was looking at docs/misra/C-language-toolchain.rst to make sure everything is listed there. We have attr_const as "__const__" noted among "Non-standard tokens".

Looks like we need to add __always_inline__ ?

Luckily, no.

First, the __const__ referred to there is GCC's alternative spelling of the regular C 'const' keyword, and not the function attribute by the same name.

Quoted 8 lines

But, non-standard tokens are about things which survive full preprocessing and are still not standard C.  In this case, it's the __attribute__ that matters, not what's in it, and this is why we don't have a hundreds of rows in that table for __attribute__((foo, bar, baz)).

That said, I think f96e2f64576cdbb147391c7cb399d393385719a9 probably should have removed the entire row including __const__ seeing as AFAICT the last user in Xen was dropped in 1aa3c54a31a5 in 2008

Hmm.  Eclair disagrees.

Quoted 8 lines

https://gitlab.com/xen-project/people/andyhhp/xen/-/jobs/7765315981

Now I'm completely confused as to why __const__ matters and everything else inside an __attribute__(()) apparently doesn't.

CC-ing some folk.  Any ideas?

~Andrew

The reason why __attribute__((__const__)) is reported after removing from the configuration of STD.tokenext the __const__ token is that:
- __const__ is just an alternate keyword for const, and as such needs to be special-cased in the configuration, as it was originally;
- the __const__ inside the attribute is still a non-standard token, no matter the surrounding context, and the kind of token is determined right after preprocessing as being a keyword (albeit non-standard), as far as I'm aware;
- this only happens due to the overlap with the alternate keyword __const__; in other words, other attributes such as __transparent_union__ in __attribute__((__transparent_union__)) do not need to be listed as permitted non-standard tokens because they aren't either standard or non-standard keywords, but just ordinary identifiers.

Therefore, I suggest keeping the __const__ inside the configuration.

Signature · 2 lines
Nicola Vetrini, BSc
Software Engineer, BUGSENG srl (https://bugseng.com)
Acked-by on v3 3/5
#13 lore raw

Re: [PATCH v3 3/5] xen/bitops: Implement hweight32() in terms of hweightl()

In reply to Andrew Cooper

Hi Andrew,

Quoted 4 lines · Andrew Cooper

... and drop generic_hweight32().

As noted previously, the only two users of hweight32() are in __init paths.

Quoted 8 lines

The int-optimised form of generic_hweight() is only two instructions shorter than the long-optimised form, and even then only on architectures which lack fast multiplication, so there's no point providing an int-optimised form.

No functional change.

Thanks,
Shawn

#14 lore raw

PATCH v3 4/5 xen/bitops: Implement hweight64() in terms of hweight{l,32}()

In reply to Andrew Cooper

... and drop generic_hweight{32,64}().

This is identical on all architectures except ARM32. Add one extra SELF_TEST to check that hweight64() works when the input is split in half.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Volodymyr Babchuk <[email protected]>
CC: Bertrand Marquis <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Oleksii Kurochko <[email protected]>
CC: Shawn Anastasio <[email protected]>

v3:
* Use >=, even if it does differ from ffs()
v2:
* Reorder with respect to the hweight32() patch
* s/__pure/attr_const/

---
 xen/arch/arm/include/asm/bitops.h |  8 ------
 xen/arch/ppc/include/asm/bitops.h |  8 ------
 xen/arch/x86/include/asm/bitops.h |  8 ------
 xen/common/bitops.c               |  3 +++
 xen/include/xen/bitops.h          | 45 ++++++-------------------------
 5 files changed, 11 insertions(+), 61 deletions(-)
xen/arch/arm/include/asm/bitops.h +0 -8
index b28c25b3d52d..f163d9bb4578 100644
@@ -78,14 +78,6 @@ bool clear_mask16_timeout(uint16_t mask, volatile void *p,
 #define arch_fls(x)  ((x) ? 32 - __builtin_clz(x) : 0)
 #define arch_flsl(x) ((x) ? BITS_PER_LONG - __builtin_clzl(x) : 0)
 
-/**
- * hweightN - returns the hamming weight of a N-bit word
- * @x: the word to weigh
- *
- * The Hamming Weight of a number is the total number of bits set in it.
- */
-#define hweight64(x) generic_hweight64(x)
-
 #endif /* _ARM_BITOPS_H */
 /*
  * Local variables:
xen/arch/ppc/include/asm/bitops.h +0 -8
index f488a7c03425..c942e9432e20 100644
@@ -126,12 +126,4 @@ static inline int test_and_set_bit(unsigned int nr, volatile void *addr)
 
 #define arch_hweightl(x) __builtin_popcountl(x)
 
-/**
- * hweightN - returns the hamming weight of a N-bit word
- * @x: the word to weigh
- *
- * The Hamming Weight of a number is the total number of bits set in it.
- */
-#define hweight64(x) __builtin_popcountll(x)
-
 #endif /* _ASM_PPC_BITOPS_H */
xen/arch/x86/include/asm/bitops.h +0 -8
index 507b043b8a86..642d8e58b288 100644
@@ -475,12 +475,4 @@ static always_inline unsigned int arch_flsl(unsigned long x)
 }
 #define arch_flsl arch_flsl
 
-/**
- * hweightN - returns the hamming weight of a N-bit word
- * @x: the word to weigh
- *
- * The Hamming Weight of a number is the total number of bits set in it.
- */
-#define hweight64(x) generic_hweight64(x)
-
 #endif /* _X86_BITOPS_H */
xen/common/bitops.c +3 -0
index 5e5d20d225d7..91ae961440af 100644
@@ -144,6 +144,9 @@ static void __init test_hweight(void)
 
     CHECK(hweightl, 1 | (1UL << (BITS_PER_LONG - 1)), 2);
     CHECK(hweightl, -1UL, BITS_PER_LONG);
+
+    /* unsigned int hweight64(uint64_t) */
+    CHECK(hweight64, -1ULL, 64);
 }
 
 static void __init __constructor test_bitops(void)
xen/include/xen/bitops.h +8 -37

57 lines not on this page · raw message

Signature · 1 line
2.39.2
Acked-by on v3 4/5
#15 lore raw

Re: [PATCH v3 4/5] xen/bitops: Implement hweight64() in terms of hweight{l,32}()

Quoted 9 lines · Andrew Cooper

... and drop generic_hweight{32,64}().

This is identical on all architectures except ARM32. Add one extra SELF_TEST to check that hweight64() works when the input is split in half.

No functional change.

#16 lore raw

PATCH v3 5/5 x86/bitops: Use the POPCNT instruction when available

In reply to Andrew Cooper

It has existed in x86 CPUs since 2008, so we're only 16 years late adding support. With all the other scafolding in place, implement arch_hweightl() for x86.

The only complication is that the call to arch_generic_hweightl() is behind the compilers back. Address this by writing it in ASM and ensure that it preserves all registers.

Copy the code generation from generic_hweightl(). It's not a complicated algorithm, and is easy to regenerate if needs be, but cover it with the same unit tests as test_generic_hweightl() just for piece of mind.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

v3:
* Reinstate dropped CONFIG_SELF_TESTS
* Leave grep fodder for CODE_FILL until we can find a nicer way of doing this.

v2:
* Fix MISRA 8.2 (parameter name) and 8.5 (single declaration) regressions.
* Rename {arch->x86}-generic-hweightl.{S->c}
* Adjust ASM formating

---
 xen/arch/x86/include/asm/bitops.h | 23 ++++++++++
 xen/lib/Makefile                  |  1 +
 xen/lib/x86-generic-hweightl.c    | 71 +++++++++++++++++++++++++++++++
 3 files changed, 95 insertions(+)
 create mode 100644 xen/lib/x86-generic-hweightl.c
xen/arch/x86/include/asm/bitops.h +23 -0
index 642d8e58b288..39e37f1cbe55 100644
@@ -6,6 +6,7 @@
  */
 
 #include <asm/alternative.h>
+#include <asm/asm_defns.h>
 #include <asm/cpufeatureset.h>
 
 /*
@@ -475,4 +476,26 @@ static always_inline unsigned int arch_flsl(unsigned long x)
 }
 #define arch_flsl arch_flsl
 
+unsigned int arch_generic_hweightl(unsigned long x);
+
+static always_inline unsigned int arch_hweightl(unsigned long x)
+{
+    unsigned int r;
+
+    /*
+     * arch_generic_hweightl() is written in ASM in order to preserve all
+     * registers, as the compiler can't see the call.
+     *
+     * This limits the POPCNT instruction to using the same ABI as a function
+     * call (input in %rdi, output in %eax) but that's fine.
+     */
+    alternative_io("call arch_generic_hweightl",
+                   "popcnt %[val], %q[res]", X86_FEATURE_POPCNT,
+                   ASM_OUTPUT2([res] "=a" (r) ASM_CALL_CONSTRAINT),
+                   [val] "D" (x));
+
+    return r;
+}
+#define arch_hweightl arch_hweightl
+
 #endif /* _X86_BITOPS_H */
xen/lib/Makefile +1 -0
index b6558e108bd9..54440f628aae 100644
@@ -36,6 +36,7 @@ lib-y += strtol.o
 lib-y += strtoll.o
 lib-y += strtoul.o
 lib-y += strtoull.o
+lib-$(CONFIG_X86) += x86-generic-hweightl.o
 lib-$(CONFIG_X86) += xxhash32.o
 lib-$(CONFIG_X86) += xxhash64.o
 
xen/lib/x86-generic-hweightl.c new file +71 -0

71 lines not on this page · raw message

Signature · 1 line
2.39.2
Acked-by on v3 5/5
#17 lore raw

Re: [PATCH v3 5/5] x86/bitops: Use the POPCNT instruction when available

Quoted 8 lines · Andrew Cooper

It has existed in x86 CPUs since 2008, so we're only 16 years late adding support. With all the other scafolding in place, implement arch_hweightl() for x86.

The only complication is that the call to arch_generic_hweightl() is behind the compilers back. Address this by writing it in ASM and ensure that it preserves all registers.

Quoted 5 lines

Copy the code generation from generic_hweightl(). It's not a complicated algorithm, and is easy to regenerate if needs be, but cover it with the same unit tests as test_generic_hweightl() just for piece of mind.