Skip to contentSkip to conversation

RISCV/bitops: Use Zbb to provide arch-optimised bitops

PATCH In review Andrew Cooper 3 participants 16 messages Feb 26, 2025 – Mar 26, 2025 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 RISCV/bitops: Use Zbb to provide arch-optimised bitops

---
CC: Oleksii Kurochko <[email protected]>

Depends on "xen/riscv: make zbb as mandatory"

---
 xen/arch/riscv/include/asm/bitops.h | 7 +++++++
 1 file changed, 7 insertions(+)
xen/arch/riscv/include/asm/bitops.h +7 -0
index d22eec1e87c7..df3df93520c5 100644
@@ -125,6 +125,13 @@ static inline void clear_bit(int nr, volatile void *p)
 #undef NOT
 #undef __AMO
 
+#define arch_ffs(x)     ((x) ? 1 + __builtin_ctz(x) : 0)
+#define arch_ffsl(x)    ((x) ? 1 + __builtin_ctzl(x) : 0)
+#define arch_fls(x)     ((x) ? 32 - __builtin_clz(x) : 0)
+#define arch_flsl(x)    ((x) ? BITS_PER_LONG - __builtin_clzl(x) : 0)
+
+#define arch_heightl(x) __builtin_popcountl(x)
+
 #endif /* ASM__RISCV__BITOPS_H */
 
 /*

base-commit: 7cf163879c5add0a4f7f9c987b61f04f8f7051b1
prerequisite-patch-id: 9ee1f7ebf5d34b1c565ee2d3d4fb319164bb8bcd
prerequisite-patch-id: 8a05c87c8d051a3ac0820887f676bbd318e4ae88
prerequisite-patch-id: 6b56e42d130d8b5ee39457b6760b05cc6e16b049
prerequisite-patch-id: c139f1f5741d695cd5e5aa6be904edcb61b73885
prerequisite-patch-id: 99f8b701000e9ee11060934e627a988ddf9aaaa7

Signature · 1 line
2.39.5
#1 lore raw
Quoted 17 lines · Andrew Cooper

---
CC: Oleksii Kurochko<[email protected]>

Depends on "xen/riscv: make zbb as mandatory"

---
  xen/arch/riscv/include/asm/bitops.h | 7 +++++++
  1 file changed, 7 insertions(+)
xen/arch/riscv/include/asm/bitops.h +0 -0
index d22eec1e87c7..df3df93520c5 100644
@@ -125,6 +125,13 @@ static inline void clear_bit(int nr, volatile void *p)
  #undef NOT
  #undef __AMO
 

xen/arch/riscv/include/asm/bitops.h static inline void clear_bit(int nr, volatile void *p) · line 137

Quoted 10 lines
xen/arch/riscv/include/asm/bitops.h +7 -0
+#define arch_ffs(x)     ((x) ? 1 + __builtin_ctz(x) : 0)
+#define arch_ffsl(x)    ((x) ? 1 + __builtin_ctzl(x) : 0)
+#define arch_fls(x)     ((x) ? 32 - __builtin_clz(x) : 0)
+#define arch_flsl(x)    ((x) ? BITS_PER_LONG - __builtin_clzl(x) : 0)
+
+#define arch_heightl(x) __builtin_popcountl(x)
+
  #endif /* ASM__RISCV__BITOPS_H */
 
  /*

LGRM: Reviewed-by: Oleksii Kurochko<[email protected]>

Thanks.

Quoted 6 lines

base-commit: 7cf163879c5add0a4f7f9c987b61f04f8f7051b1
prerequisite-patch-id: 9ee1f7ebf5d34b1c565ee2d3d4fb319164bb8bcd
prerequisite-patch-id: 8a05c87c8d051a3ac0820887f676bbd318e4ae88
prerequisite-patch-id: 6b56e42d130d8b5ee39457b6760b05cc6e16b049
prerequisite-patch-id: c139f1f5741d695cd5e5aa6be904edcb61b73885
prerequisite-patch-id: 99f8b701000e9ee11060934e627a988ddf9aaaa7

Could you please tell me how do you generate this one?

~ Oleksii

#2 lore raw
Quoted 28 lines · Oleksii Kurochko
Quoted 25 lines · Andrew Cooper

---
CC: Oleksii Kurochko <[email protected]>
Depends on "xen/riscv: make zbb as mandatory"

---
 xen/arch/riscv/include/asm/bitops.h | 7 +++++++
 1 file changed, 7 insertions(+)
xen/arch/riscv/include/asm/bitops.h +7 -0
index d22eec1e87c7..df3df93520c5 100644
@@ -125,6 +125,13 @@ static inline void clear_bit(int nr, volatile void *p)
 #undef NOT
 #undef __AMO
 
+#define arch_ffs(x)     ((x) ? 1 + __builtin_ctz(x) : 0)
+#define arch_ffsl(x)    ((x) ? 1 + __builtin_ctzl(x) : 0)
+#define arch_fls(x)     ((x) ? 32 - __builtin_clz(x) : 0)
+#define arch_flsl(x)    ((x) ? BITS_PER_LONG - __builtin_clzl(x) : 0)
+
+#define arch_heightl(x) __builtin_popcountl(x)
+
 #endif /* ASM__RISCV__BITOPS_H */
 
Quoted 2 lines

xen/arch/riscv/include/asm/bitops.h static inline void clear_bit(int nr, volatile void *p) · line 137

Quoted 1 line
xen/arch/riscv/include/asm/bitops.h +0 -0
 /*

LGRM: Reviewed-by: Oleksii Kurochko <[email protected]>

Thanks.  This form is much easier than making alternatives for it.

Quoted 8 lines

Thanks.

Quoted 6 lines

base-commit: 7cf163879c5add0a4f7f9c987b61f04f8f7051b1
prerequisite-patch-id: 9ee1f7ebf5d34b1c565ee2d3d4fb319164bb8bcd
prerequisite-patch-id: 8a05c87c8d051a3ac0820887f676bbd318e4ae88
prerequisite-patch-id: 6b56e42d130d8b5ee39457b6760b05cc6e16b049
prerequisite-patch-id: c139f1f5741d695cd5e5aa6be904edcb61b73885
prerequisite-patch-id: 99f8b701000e9ee11060934e627a988ddf9aaaa7

Could you please tell me how do you generate this one?

In gitconfig,

[format]
        useAutoBase = "whenAble"

or --base=auto on a git format-patch command.

This is a poor example.  Those prereq ids are:

* 307e136282d8 - (HEAD -> riscv-isa) RISCV/bitops: Use Zbb to provide arch-optimised bitops (7 hours ago) <Andrew Cooper>
* 519dcd50e4cd - xen/riscv: make zbb as mandatory (12 hours ago) <Oleksii Kurochko>
* c9bf2a9ac22c - xen/riscv: identify specific ISA supported by cpu (12 hours ago) <Oleksii Kurochko>
* 9014de63aa14 - automation: drop debian:11-riscv64 container (12 hours ago) <Oleksii Kurochko>
* 5febb98d11f3 - xen/riscv: drop CONFIG_RISCV_ISA_RV64G (12 hours ago) <Oleksii Kurochko>
* 5a7c9fd746af - xen/README: add compiler and binutils versions for RISCV-64 (12 hours ago) <Oleksii Kurochko>
* 7cf163879c5a - (xenbits/staging, xenbits/master, upstream/staging, upstream/master, origin/staging, origin/master, origin/HEAD, staging, pending, master) PPC: Activate UBSAN in testing (12 hours ago) <Andrew Cooper>

as I pulled your series in locally.

~Andrew

#3 lore raw
In reply to Andrew Cooper

xen/arch/riscv/include/asm/bitops.h static inline void clear_bit(int nr, volatile void *p) · line 130

Quoted 9 lines · Andrew Cooper
xen/arch/riscv/include/asm/bitops.h +3 -0
@@ -125,6 +125,13 @@ static inline void clear_bit(int nr, volatile void *p)
 #undef NOT
 #undef __AMO
 
+#define arch_ffs(x)     ((x) ? 1 + __builtin_ctz(x) : 0)
+#define arch_ffsl(x)    ((x) ? 1 + __builtin_ctzl(x) : 0)
+#define arch_fls(x)     ((x) ? 32 - __builtin_clz(x) : 0)

I fear you won't like me to say this, but can't we avoid baking in yet another assumption on sizeof(int) == 4, by using at least sizeof(int) * 8 here (yet better might be sizeof(int) * BITS_PER_BYTE)?

xen/arch/riscv/include/asm/bitops.h static inline void clear_bit(int nr, volatile void *p) · line 133

Quoted 3 lines
xen/arch/riscv/include/asm/bitops.h +3 -0
+#define arch_flsl(x)    ((x) ? BITS_PER_LONG - __builtin_clzl(x) : 0)
+
+#define arch_heightl(x) __builtin_popcountl(x)

arch_hweightl()?

Jan

#4 lore raw
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Andrew Cooper
xen/arch/riscv/include/asm/bitops.h +0 -0
@@ -125,6 +125,13 @@ static inline void clear_bit(int nr, volatile void *p)
 #undef NOT
 #undef __AMO
 
Quoted 6 lines

xen/arch/riscv/include/asm/bitops.h static inline void clear_bit(int nr, volatile void *p) · line 130

Quoted 3 lines
xen/arch/riscv/include/asm/bitops.h +3 -0
+#define arch_ffs(x)     ((x) ? 1 + __builtin_ctz(x) : 0)
+#define arch_ffsl(x)    ((x) ? 1 + __builtin_ctzl(x) : 0)
+#define arch_fls(x)     ((x) ? 32 - __builtin_clz(x) : 0)

I fear you won't like me to say this, but can't we avoid baking in yet another assumption on sizeof(int) == 4, by using at least sizeof(int) * 8 here (yet better might be sizeof(int) * BITS_PER_BYTE)?

Yes and no.

No, because 32 here is consistent with ARM and PPC when it comes to arch_fls().  Given the effort it took to get these consistent, I'm not interested in letting them diverge.

But, if someone wants to introduce BITS_PER_INT to mirror BITS_PER_LONG and use it consistently, then that would be ok too.

Quoted 5 lines

xen/arch/riscv/include/asm/bitops.h static inline void clear_bit(int nr, volatile void *p) · line 133

Quoted 4 lines
xen/arch/riscv/include/asm/bitops.h +3 -0
+#define arch_flsl(x)    ((x) ? BITS_PER_LONG - __builtin_clzl(x) : 0)
+
+#define arch_heightl(x) __builtin_popcountl(x)

arch_hweightl()?

Oops yes.  And RISC-V does have two uses, via __bitmap_weight.

~Andrew

#5 lore raw
Quoted 15 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Andrew Cooper
xen/arch/riscv/include/asm/bitops.h +0 -0
@@ -125,6 +125,13 @@ static inline void clear_bit(int nr, volatile void *p)
 #undef NOT
 #undef __AMO
 
Quoted 6 lines

xen/arch/riscv/include/asm/bitops.h static inline void clear_bit(int nr, volatile void *p) · line 130

Quoted 3 lines
xen/arch/riscv/include/asm/bitops.h +3 -0
+#define arch_ffs(x)     ((x) ? 1 + __builtin_ctz(x) : 0)
+#define arch_ffsl(x)    ((x) ? 1 + __builtin_ctzl(x) : 0)
+#define arch_fls(x)     ((x) ? 32 - __builtin_clz(x) : 0)

I fear you won't like me to say this, but can't we avoid baking in yet another assumption on sizeof(int) == 4, by using at least sizeof(int) * 8 here (yet better might be sizeof(int) * BITS_PER_BYTE)?

Quoted 8 lines

Yes and no.

No, because 32 here is consistent with ARM and PPC when it comes to arch_fls().  Given the effort it took to get these consistent, I'm not interested in letting them diverge.

But, if someone wants to introduce BITS_PER_INT to mirror BITS_PER_LONG and use it consistently, then that would be ok too.

I was actually hoping to eliminate BITS_PER_LONG at some point, in favor of using sizeof(long) * BITS_PER_BYTE. (Surely in common code we could retain a shorthand of that name, if so desired, but I see no reason why each arch would need to provide all three BITS_PER_{BYTE,INT,LONG}.)

Jan

#6 lore raw
Quoted 15 lines · Jan Beulich
Quoted 14 lines · Andrew Cooper
Quoted 13 lines · Jan Beulich

xen/arch/riscv/include/asm/bitops.h static inline void clear_bit(int nr, volatile void *p) · line 130

Quoted 9 lines · Andrew Cooper
xen/arch/riscv/include/asm/bitops.h +3 -0
@@ -125,6 +125,13 @@ static inline void clear_bit(int nr, volatile void *p)
 #undef NOT
 #undef __AMO
 
+#define arch_ffs(x)     ((x) ? 1 + __builtin_ctz(x) : 0)
+#define arch_ffsl(x)    ((x) ? 1 + __builtin_ctzl(x) : 0)
+#define arch_fls(x)     ((x) ? 32 - __builtin_clz(x) : 0)

I fear you won't like me to say this, but can't we avoid baking in yet another assumption on sizeof(int) == 4, by using at least sizeof(int) * 8 here (yet better might be sizeof(int) * BITS_PER_BYTE)?

Quoted 8 lines
Quoted 8 lines

Yes and no.

No, because 32 here is consistent with ARM and PPC when it comes to arch_fls().  Given the effort it took to get these consistent, I'm not interested in letting them diverge.

But, if someone wants to introduce BITS_PER_INT to mirror BITS_PER_LONG and use it consistently, then that would be ok too.

Oleksii: I see your patch is committed, but when I said "use it consistently", I meant "patch ARM and PPC too".

Quoted 4 lines

I was actually hoping to eliminate BITS_PER_LONG at some point, in favor of using sizeof(long) * BITS_PER_BYTE. (Surely in common code we could retain a shorthand of that name, if so desired, but I see no reason why each arch would need to provide all three BITS_PER_{BYTE,INT,LONG}.)

The concern is legibility and clarity.

This:

    ((x) ? 32 - __builtin_clz(x) : 0)

is a clear expression in a way that this:

    ((x) ? (sizeof(int) * BITS_PER_BYTE) - __builtin_clz(x) : 0)

is not.  The problem is the extra binary expression, and this:

    ((x) ? BITS_PER_INT - __builtin_clz(x) : 0)

is still clear, because the reader doesn't have to perform a multiply to just to figure out what's going on.

It is definitely stupid to have each architecture provide their own BITS_PER_*.  The compiler is in a superior position to provide those details, and it should be in a common location.

I don't particularly mind how those constants are derived, but one key thing that BITS_PER_* can do which sizeof() can't is be used in #ifdef/etc.

The following files use BITS_PER_LONG preprocessing:

arch/arm/include/asm/div64.h
arch/x86/cpu/mcheck/mce.c
arch/x86/smpboot.c
common/bitops.c
common/coverage/gcov.h
common/coverage/llvm.c
common/cpu.c
common/event_channel.c
common/time.c
common/ubsan/ubsan.c
include/asm-generic/div64.h
include/xen/cpumask.h
include/xen/inttypes.h
include/xen/nodemask.h
include/xen/sched.h
include/xen/xxhash.h
lib/find-next-bit.c
lib/generic-ffsl.c
lib/generic-flsl.c
lib/generic-hweightl.c

And I really don't think they can be replaced with a sizeof().

~Andrew

#7 lore raw
Quoted 50 lines · Andrew Cooper
Quoted 15 lines · Jan Beulich
Quoted 14 lines · Andrew Cooper
Quoted 13 lines · Jan Beulich

xen/arch/riscv/include/asm/bitops.h static inline void clear_bit(int nr, volatile void *p) · line 130

Quoted 9 lines · Andrew Cooper
xen/arch/riscv/include/asm/bitops.h +3 -0
@@ -125,6 +125,13 @@ static inline void clear_bit(int nr, volatile void *p)
 #undef NOT
 #undef __AMO
 
+#define arch_ffs(x)     ((x) ? 1 + __builtin_ctz(x) : 0)
+#define arch_ffsl(x)    ((x) ? 1 + __builtin_ctzl(x) : 0)
+#define arch_fls(x)     ((x) ? 32 - __builtin_clz(x) : 0)

I fear you won't like me to say this, but can't we avoid baking in yet another assumption on sizeof(int) == 4, by using at least sizeof(int) * 8 here (yet better might be sizeof(int) * BITS_PER_BYTE)?

Quoted 8 lines
Quoted 8 lines

Yes and no.

No, because 32 here is consistent with ARM and PPC when it comes to arch_fls().  Given the effort it took to get these consistent, I'm not interested in letting them diverge.

But, if someone wants to introduce BITS_PER_INT to mirror BITS_PER_LONG and use it consistently, then that would be ok too.

Oleksii: I see your patch is committed, but when I said "use it consistently", I meant "patch ARM and PPC too".

Quoted 4 lines

I was actually hoping to eliminate BITS_PER_LONG at some point, in favor of using sizeof(long) * BITS_PER_BYTE. (Surely in common code we could retain a shorthand of that name, if so desired, but I see no reason why each arch would need to provide all three BITS_PER_{BYTE,INT,LONG}.)

The concern is legibility and clarity.

This:

    ((x) ? 32 - __builtin_clz(x) : 0)

is a clear expression in a way that this:

    ((x) ? (sizeof(int) * BITS_PER_BYTE) - __builtin_clz(x) : 0)

is not.  The problem is the extra binary expression, and this:

    ((x) ? BITS_PER_INT - __builtin_clz(x) : 0)

is still clear, because the reader doesn't have to perform a multiply to just to figure out what's going on.

Quoted 6 lines

It is definitely stupid to have each architecture provide their own BITS_PER_*.  The compiler is in a superior position to provide those details, and it should be in a common location.

I don't particularly mind how those constants are derived, but one key thing that BITS_PER_* can do which sizeof() can't is be used in #ifdef/etc.

This is a fair point indeed.

Jan

#8 lore raw
In reply to Andrew Cooper
Quoted 18 lines · Andrew Cooper
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Andrew Cooper
xen/arch/riscv/include/asm/bitops.h +0 -0
@@ -125,6 +125,13 @@ static inline void clear_bit(int nr, volatile void *p)
  #undef NOT
  #undef __AMO
 
Quoted 6 lines

xen/arch/riscv/include/asm/bitops.h static inline void clear_bit(int nr, volatile void *p) · line 130

Quoted 3 lines
xen/arch/riscv/include/asm/bitops.h +3 -0
+#define arch_ffs(x)     ((x) ? 1 + __builtin_ctz(x) : 0)
+#define arch_ffsl(x)    ((x) ? 1 + __builtin_ctzl(x) : 0)
+#define arch_fls(x)     ((x) ? 32 - __builtin_clz(x) : 0)

I fear you won't like me to say this, but can't we avoid baking in yet another assumption on sizeof(int) == 4, by using at least sizeof(int) * 8 here (yet better might be sizeof(int) * BITS_PER_BYTE)?

Yes and no.

Quoted 8 lines
Quoted 6 lines
Quoted 6 lines

No, because 32 here is consistent with ARM and PPC when it comes to arch_fls().  Given the effort it took to get these consistent, I'm not interested in letting them diverge.

But, if someone wants to introduce BITS_PER_INT to mirror BITS_PER_LONG and use it consistently, then that would be ok too.

Oleksii: I see your patch is committed, but when I said "use it consistently", I meant "patch ARM and PPC too".

I planned to do that in a separate patch next week.

Quoted 22 lines
Quoted 4 lines

I was actually hoping to eliminate BITS_PER_LONG at some point, in favor of using sizeof(long) * BITS_PER_BYTE. (Surely in common code we could retain a shorthand of that name, if so desired, but I see no reason why each arch would need to provide all three BITS_PER_{BYTE,INT,LONG}.)

The concern is legibility and clarity.

This:

    ((x) ? 32 - __builtin_clz(x) : 0)

is a clear expression in a way that this:

    ((x) ? (sizeof(int) * BITS_PER_BYTE) - __builtin_clz(x) : 0)

is not.  The problem is the extra binary expression, and this:

    ((x) ? BITS_PER_INT - __builtin_clz(x) : 0)

is still clear, because the reader doesn't have to perform a multiply to just to figure out what's going on.

Quoted 6 lines

It is definitely stupid to have each architecture provide their own BITS_PER_*.  The compiler is in a superior position to provide those details, and it should be in a common location.

I don't particularly mind how those constants are derived, but one key thing that BITS_PER_* can do which sizeof() can't is be used in #ifdef/etc.

What about moving them to xen/config.h? (if it isn't the best one place, any suggestion which is better?)

#define BYTES_PER_INT  (1 << INT_BYTEORDER)
#define BITS_PER_INT  (BYTES_PER_INT << 3)

#define BYTES_PER_LONG (1 << LONG_BYTEORDER)
#define BITS_PER_LONG (BYTES_PER_LONG << 3)
#define BITS_PER_BYTE 8

Also, it seems like the follwoing could be moved there too:

#define POINTER_ALIGN BYTES_PER_LONG

#define BITS_PER_LLONG 64

#define BITS_PER_BYTE 8

Only, BITS_PER_XEN_ULONG is looking different for x86, so should be still in arch specific config.h.

~ Oleksii

Quoted 27 lines

The following files use BITS_PER_LONG preprocessing:

arch/arm/include/asm/div64.h
arch/x86/cpu/mcheck/mce.c
arch/x86/smpboot.c
common/bitops.c
common/coverage/gcov.h
common/coverage/llvm.c
common/cpu.c
common/event_channel.c
common/time.c
common/ubsan/ubsan.c
include/asm-generic/div64.h
include/xen/cpumask.h
include/xen/inttypes.h
include/xen/nodemask.h
include/xen/sched.h
include/xen/xxhash.h
lib/find-next-bit.c
lib/generic-ffsl.c
lib/generic-flsl.c
lib/generic-hweightl.c

And I really don't think they can be replaced with a sizeof().

~Andrew

#9 lore raw
Quoted 36 lines · Oleksii Kurochko
Quoted 23 lines · Andrew Cooper
Quoted 4 lines · Jan Beulich

I was actually hoping to eliminate BITS_PER_LONG at some point, in favor of using sizeof(long) * BITS_PER_BYTE. (Surely in common code we could retain a shorthand of that name, if so desired, but I see no reason why each arch would need to provide all three BITS_PER_{BYTE,INT,LONG}.)

The concern is legibility and clarity.

This:

    ((x) ? 32 - __builtin_clz(x) : 0)

is a clear expression in a way that this:

    ((x) ? (sizeof(int) * BITS_PER_BYTE) - __builtin_clz(x) : 0)

is not.  The problem is the extra binary expression, and this:

    ((x) ? BITS_PER_INT - __builtin_clz(x) : 0)

is still clear, because the reader doesn't have to perform a multiply to just to figure out what's going on.

Quoted 6 lines

It is definitely stupid to have each architecture provide their own BITS_PER_*.  The compiler is in a superior position to provide those details, and it should be in a common location.

I don't particularly mind how those constants are derived, but one key thing that BITS_PER_* can do which sizeof() can't is be used in #ifdef/etc.

What about moving them to xen/config.h? (if it isn't the best one place, any suggestion which is better?)

#define BYTES_PER_INT  (1 << INT_BYTEORDER)
#define BITS_PER_INT  (BYTES_PER_INT << 3)

Quoted 7 lines

#define BYTES_PER_LONG (1 << LONG_BYTEORDER)
#define BITS_PER_LONG (BYTES_PER_LONG << 3)
#define BITS_PER_BYTE 8

Also, it seems like the follwoing could be moved there too:

#define POINTER_ALIGN BYTES_PER_LONG

This one is likely fine to move.

Quoted 1 line

#define BITS_PER_LLONG 64

This one is only fine to move imo when converted to

#define BITS_PER_LONG (BYTES_PER_LLONG << 3)

Quoted 1 line

#define BITS_PER_BYTE 8

Personally I'd rather leave this per-arch. The others can truly be derived; this one can't be. If we centralize, imo we should also convert the " << 3" to " * BITS_PER_BYTE".

Jan

#10 lore raw
Quoted 31 lines · Jan Beulich
Quoted 30 lines · Oleksii Kurochko
Quoted 29 lines · Andrew Cooper
Quoted 4 lines · Jan Beulich

I was actually hoping to eliminate BITS_PER_LONG at some point, in favor of using sizeof(long) * BITS_PER_BYTE. (Surely in common code we could retain a shorthand of that name, if so desired, but I see no reason why each arch would need to provide all three BITS_PER_{BYTE,INT,LONG}.)

The concern is legibility and clarity.

This:

    ((x) ? 32 - __builtin_clz(x) : 0)

is a clear expression in a way that this:

    ((x) ? (sizeof(int) * BITS_PER_BYTE) - __builtin_clz(x) : 0)

is not.  The problem is the extra binary expression, and this:

    ((x) ? BITS_PER_INT - __builtin_clz(x) : 0)

is still clear, because the reader doesn't have to perform a multiply to just to figure out what's going on.

It is definitely stupid to have each architecture provide their own BITS_PER_*.  The compiler is in a superior position to provide those details, and it should be in a common location.

I don't particularly mind how those constants are derived, but one key thing that BITS_PER_* can do which sizeof() can't is be used in #ifdef/etc.

Quoted 8 lines
Quoted 8 lines

What about moving them to xen/config.h? (if it isn't the best one place, any suggestion which is better?)

#define BYTES_PER_INT  (1 << INT_BYTEORDER)
#define BITS_PER_INT  (BYTES_PER_INT << 3)

#define BYTES_PER_LONG (1 << LONG_BYTEORDER)
#define BITS_PER_LONG (BYTES_PER_LONG << 3)
#define BITS_PER_BYTE 8

The *_BYTEORDER's are useless indirection.  BITS_PER_* should be defined straight up, and this will simplify quite a lot of preprocessing.

Quoted 10 lines
Quoted 4 lines

Also, it seems like the follwoing could be moved there too:

#define POINTER_ALIGN BYTES_PER_LONG

This one is likely fine to move.

Quoted 1 line

#define BITS_PER_LLONG 64

This one is only fine to move imo when converted to

#define BITS_PER_LONG (BYTES_PER_LLONG << 3)

Erm?  That's mixing long and long long types.  Presuming that's an errant L, then sure.

Quoted 5 lines
Quoted 2 lines

#define BITS_PER_BYTE 8

Personally I'd rather leave this per-arch. The others can truly be derived; this one can't be. If we centralize, imo we should also convert the " << 3" to " * BITS_PER_BYTE".

It is highly unlikely that Xen will ever run on a system where CHAR_BIT isn't 8.

So I suggest it stays central to reduce complexity.  If an arch ever needs to change it, the complexity can be added then.

~Andrew

#11 lore raw
Quoted 34 lines · Andrew Cooper
Quoted 33 lines · Jan Beulich
Quoted 32 lines · Oleksii Kurochko
Quoted 23 lines · Andrew Cooper
Quoted 4 lines · Jan Beulich

I was actually hoping to eliminate BITS_PER_LONG at some point, in favor of using sizeof(long) * BITS_PER_BYTE. (Surely in common code we could retain a shorthand of that name, if so desired, but I see no reason why each arch would need to provide all three BITS_PER_{BYTE,INT,LONG}.)

The concern is legibility and clarity.

This:

    ((x) ? 32 - __builtin_clz(x) : 0)

is a clear expression in a way that this:

    ((x) ? (sizeof(int) * BITS_PER_BYTE) - __builtin_clz(x) : 0)

is not.  The problem is the extra binary expression, and this:

    ((x) ? BITS_PER_INT - __builtin_clz(x) : 0)

is still clear, because the reader doesn't have to perform a multiply to just to figure out what's going on.

Quoted 6 lines

It is definitely stupid to have each architecture provide their own BITS_PER_*.  The compiler is in a superior position to provide those details, and it should be in a common location.

I don't particularly mind how those constants are derived, but one key thing that BITS_PER_* can do which sizeof() can't is be used in #ifdef/etc.

What about moving them to xen/config.h? (if it isn't the best one place, any suggestion which is better?)

Quoted 8 lines
Quoted 6 lines
Quoted 6 lines

#define BYTES_PER_INT  (1 << INT_BYTEORDER)
#define BITS_PER_INT  (BYTES_PER_INT << 3)

#define BYTES_PER_LONG (1 << LONG_BYTEORDER)
#define BITS_PER_LONG (BYTES_PER_LONG << 3)
#define BITS_PER_BYTE 8

The *_BYTEORDER's are useless indirection.  BITS_PER_* should be defined straight up, and this will simplify quite a lot of preprocessing.

Could we really drop *_BYTEORDER?

For example, LONG_BYTEORDER for Arm could be either 2 or 3 depends on Arm32 or Arm64 is used.

Quoted 14 lines
Quoted 10 lines
Quoted 4 lines

Also, it seems like the follwoing could be moved there too:

#define POINTER_ALIGN BYTES_PER_LONG

This one is likely fine to move.

Quoted 1 line

#define BITS_PER_LLONG 64

This one is only fine to move imo when converted to

#define BITS_PER_LONG (BYTES_PER_LLONG << 3)

Erm?  That's mixing long and long long types.  Presuming that's an errant L, then sure.

Quoted 1 line
Quoted 1 line

#define BITS_PER_BYTE 8

Quoted 8 lines
Quoted 3 lines

Personally I'd rather leave this per-arch. The others can truly be derived; this one can't be. If we centralize, imo we should also convert the " << 3" to " * BITS_PER_BYTE".

It is highly unlikely that Xen will ever run on a system where CHAR_BIT isn't 8.

So I suggest it stays central to reduce complexity.  If an arch ever needs to change it, the complexity can be added then.

Does it make sense to ifdef that? Or, at least, before defintion of BITS_PER_BYTE something like:
#if CHAR_BIT != 8
#error "CHAR_BIT isn't 8"
#endif

~ Oleksii

#12 lore raw
Quoted 42 lines · Oleksii Kurochko
Quoted 40 lines · Andrew Cooper
Quoted 39 lines · Jan Beulich
Quoted 38 lines · Oleksii Kurochko
Quoted 23 lines · Andrew Cooper
Quoted 4 lines · Jan Beulich

I was actually hoping to eliminate BITS_PER_LONG at some point, in favor of using sizeof(long) * BITS_PER_BYTE. (Surely in common code we could retain a shorthand of that name, if so desired, but I see no reason why each arch would need to provide all three BITS_PER_{BYTE,INT,LONG}.)

The concern is legibility and clarity.

This:

    ((x) ? 32 - __builtin_clz(x) : 0)

is a clear expression in a way that this:

    ((x) ? (sizeof(int) * BITS_PER_BYTE) - __builtin_clz(x) : 0)

is not.  The problem is the extra binary expression, and this:

    ((x) ? BITS_PER_INT - __builtin_clz(x) : 0)

is still clear, because the reader doesn't have to perform a multiply to just to figure out what's going on.

Quoted 6 lines

It is definitely stupid to have each architecture provide their own BITS_PER_*.  The compiler is in a superior position to provide those details, and it should be in a common location.

I don't particularly mind how those constants are derived, but one key thing that BITS_PER_* can do which sizeof() can't is be used in #ifdef/etc.

What about moving them to xen/config.h? (if it isn't the best one place, any suggestion which is better?)

#define BYTES_PER_INT  (1 << INT_BYTEORDER)
#define BITS_PER_INT  (BYTES_PER_INT << 3)

#define BYTES_PER_LONG (1 << LONG_BYTEORDER)
#define BITS_PER_LONG (BYTES_PER_LONG << 3)
#define BITS_PER_BYTE 8

Quoted 6 lines
Quoted 2 lines

The *_BYTEORDER's are useless indirection.  BITS_PER_* should be defined straight up, and this will simplify quite a lot of preprocessing.

Could we really drop *_BYTEORDER?

For example, LONG_BYTEORDER for Arm could be either 2 or 3 depends on Arm32 or Arm64 is used.

The can still #define BITS_PER_LONG to 32 or 64 respectively, can't they?

Quoted 19 lines
Quoted 19 lines
Quoted 9 lines
Quoted 3 lines

Also, it seems like the follwoing could be moved there too:

#define POINTER_ALIGN BYTES_PER_LONG

This one is likely fine to move.

Quoted 1 line

#define BITS_PER_LLONG 64

This one is only fine to move imo when converted to

#define BITS_PER_LONG (BYTES_PER_LLONG << 3)

Erm?  That's mixing long and long long types.  Presuming that's an errant L, then sure.

Quoted 4 lines
Quoted 1 line

#define BITS_PER_BYTE 8

Personally I'd rather leave this per-arch. The others can truly be derived; this one can't be. If we centralize, imo we should also convert the " << 3" to " * BITS_PER_BYTE".

It is highly unlikely that Xen will ever run on a system where CHAR_BIT isn't 8.

Quoted 7 lines
Quoted 2 lines

So I suggest it stays central to reduce complexity.  If an arch ever needs to change it, the complexity can be added then.

Does it make sense to ifdef that? Or, at least, before defintion of BITS_PER_BYTE something like:
#if CHAR_BIT != 8
#error "CHAR_BIT isn't 8"
#endif

Where would CHAR_BIT come from? Oh, perhaps you mean __CHAR_BIT__? If all compilers we can build with supply that value, we could indeed centrally use either

#define BITS_PER_BYTE __CHAR_BIT__

or

#define BITS_PER_BYTE 8
#if BITS_PER_BYTE != __CHAR_BIT__

# error "..."
#endif

Jan

#13 lore raw
Quoted 42 lines · Jan Beulich
Quoted 41 lines · Oleksii Kurochko
Quoted 40 lines · Andrew Cooper
Quoted 39 lines · Jan Beulich
Quoted 38 lines · Oleksii Kurochko
Quoted 23 lines · Andrew Cooper
Quoted 4 lines · Jan Beulich

I was actually hoping to eliminate BITS_PER_LONG at some point, in favor of using sizeof(long) * BITS_PER_BYTE. (Surely in common code we could retain a shorthand of that name, if so desired, but I see no reason why each arch would need to provide all three BITS_PER_{BYTE,INT,LONG}.)

The concern is legibility and clarity.

This:

       ((x) ? 32 - __builtin_clz(x) : 0)

is a clear expression in a way that this:

       ((x) ? (sizeof(int) * BITS_PER_BYTE) - __builtin_clz(x) : 0)

is not.  The problem is the extra binary expression, and this:

       ((x) ? BITS_PER_INT - __builtin_clz(x) : 0)

is still clear, because the reader doesn't have to perform a multiply to just to figure out what's going on.

Quoted 6 lines

It is definitely stupid to have each architecture provide their own BITS_PER_*.  The compiler is in a superior position to provide those details, and it should be in a common location.

I don't particularly mind how those constants are derived, but one key thing that BITS_PER_* can do which sizeof() can't is be used in #ifdef/etc.

What about moving them to xen/config.h? (if it isn't the best one place, any suggestion which is better?)

#define BYTES_PER_INT  (1 << INT_BYTEORDER)
#define BITS_PER_INT  (BYTES_PER_INT << 3)

#define BYTES_PER_LONG (1 << LONG_BYTEORDER)
#define BITS_PER_LONG (BYTES_PER_LONG << 3)
#define BITS_PER_BYTE 8

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

The *_BYTEORDER's are useless indirection.  BITS_PER_* should be defined straight up, and this will simplify quite a lot of preprocessing.

Could we really drop *_BYTEORDER?

For example, LONG_BYTEORDER for Arm could be either 2 or 3 depends on Arm32 or Arm64 is used.

The can still #define BITS_PER_LONG to 32 or 64 respectively, can't they?

Yes, but then if we want to move it to xen/config.h then it will be needed to:
in Arm's asm/config.h to have something like:

  #ifdef CONFIG_ARM_32
      #define BITS_PER_LONG 32

#endif

and then in xen/config.h

  #ifndef BITS_PER_LONG
      #define BITS_PER_LONG 64

#endif

But I wanted to not have #ifndef BITS_PER_LONG in xen/config.h. (or using CONFIG_ARM_32 in xen/config.h)

If it is okay to have this #ifndef in xen/config.h then we can do in that way.

Quoted 30 lines
Quoted 20 lines
Quoted 20 lines
Quoted 10 lines
Quoted 4 lines

Also, it seems like the follwoing could be moved there too:

#define POINTER_ALIGN BYTES_PER_LONG

This one is likely fine to move.

Quoted 1 line

#define BITS_PER_LLONG 64

This one is only fine to move imo when converted to

#define BITS_PER_LONG (BYTES_PER_LLONG << 3)

Erm?  That's mixing long and long long types.  Presuming that's an errant L, then sure.

Quoted 4 lines
Quoted 1 line

#define BITS_PER_BYTE 8

Personally I'd rather leave this per-arch. The others can truly be derived; this one can't be. If we centralize, imo we should also convert the " << 3" to " * BITS_PER_BYTE".

It is highly unlikely that Xen will ever run on a system where CHAR_BIT isn't 8.

Quoted 6 lines
Quoted 2 lines

So I suggest it stays central to reduce complexity.  If an arch ever needs to change it, the complexity can be added then.

Does it make sense to ifdef that? Or, at least, before defintion of BITS_PER_BYTE something like:
#if CHAR_BIT != 8
#error "CHAR_BIT isn't 8"
#endif

Where would CHAR_BIT come from? Oh, perhaps you mean __CHAR_BIT__? If all compilers we can build with supply that value, we could indeed centrally use either

Quoted 8 lines

#define BITS_PER_BYTE __CHAR_BIT__

or

#define BITS_PER_BYTE 8
#if BITS_PER_BYTE != __CHAR_BIT__

# error "..."
#endif

Sorry, I meant __CHAR_BIT__.

Thanks.

~ Oleksii

#14 lore raw
Quoted 57 lines · Oleksii Kurochko
Quoted 42 lines · Jan Beulich
Quoted 41 lines · Oleksii Kurochko
Quoted 40 lines · Andrew Cooper
Quoted 39 lines · Jan Beulich
Quoted 38 lines · Oleksii Kurochko
Quoted 23 lines · Andrew Cooper
Quoted 4 lines · Jan Beulich

I was actually hoping to eliminate BITS_PER_LONG at some point, in favor of using sizeof(long) * BITS_PER_BYTE. (Surely in common code we could retain a shorthand of that name, if so desired, but I see no reason why each arch would need to provide all three BITS_PER_{BYTE,INT,LONG}.)

The concern is legibility and clarity.

This:

       ((x) ? 32 - __builtin_clz(x) : 0)

is a clear expression in a way that this:

       ((x) ? (sizeof(int) * BITS_PER_BYTE) - __builtin_clz(x) : 0)

is not.  The problem is the extra binary expression, and this:

       ((x) ? BITS_PER_INT - __builtin_clz(x) : 0)

is still clear, because the reader doesn't have to perform a multiply to just to figure out what's going on.

Quoted 6 lines

It is definitely stupid to have each architecture provide their own BITS_PER_*.  The compiler is in a superior position to provide those details, and it should be in a common location.

I don't particularly mind how those constants are derived, but one key thing that BITS_PER_* can do which sizeof() can't is be used in #ifdef/etc.

What about moving them to xen/config.h? (if it isn't the best one place, any suggestion which is better?)

#define BYTES_PER_INT  (1 << INT_BYTEORDER)
#define BITS_PER_INT  (BYTES_PER_INT << 3)

#define BYTES_PER_LONG (1 << LONG_BYTEORDER)
#define BITS_PER_LONG (BYTES_PER_LONG << 3)
#define BITS_PER_BYTE 8

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

The *_BYTEORDER's are useless indirection.  BITS_PER_* should be defined straight up, and this will simplify quite a lot of preprocessing.

Could we really drop *_BYTEORDER?

For example, LONG_BYTEORDER for Arm could be either 2 or 3 depends on Arm32 or Arm64 is used.

The can still #define BITS_PER_LONG to 32 or 64 respectively, can't they?

Yes, but then if we want to move it to xen/config.h then it will be needed to:
in Arm's asm/config.h to have something like:

   #ifdef CONFIG_ARM_32
       #define BITS_PER_LONG 32
   #endif
Quoted 6 lines

and then in xen/config.h

   #ifndef BITS_PER_LONG
       #define BITS_PER_LONG 64

#endif

But I wanted to not have #ifndef BITS_PER_LONG in xen/config.h. (or using CONFIG_ARM_32 in xen/config.h)

Whatever the fundamental definitions that can vary per arch - those should imo live in asm/config.h. For the case here, if we deem *_BYTEORDER as fundamental, they go there (and BITS_PER_* go into xen/config.h). If we deem BITS_PER_* fundamental, they go into asm/config.h.

Stuff that we expect to remain uniform across ports (BITS_PER_BYTE, possibly also BITS_PER_INT BITS_PER_LLONG) may also go into xen/config.h, as long as we're okay with the possible future churn if a port appeared that has different needs.

I agree that there better wouldn't be #ifndef for such in xen/config.h.

Jan

#15 lore raw
Quoted 67 lines · Jan Beulich
Quoted 55 lines · Oleksii Kurochko
Quoted 42 lines · Jan Beulich
Quoted 41 lines · Oleksii Kurochko
Quoted 40 lines · Andrew Cooper
Quoted 39 lines · Jan Beulich
Quoted 38 lines · Oleksii Kurochko
Quoted 23 lines · Andrew Cooper
Quoted 4 lines · Jan Beulich

I was actually hoping to eliminate BITS_PER_LONG at some point, in favor of using sizeof(long) * BITS_PER_BYTE. (Surely in common code we could retain a shorthand of that name, if so desired, but I see no reason why each arch would need to provide all three BITS_PER_{BYTE,INT,LONG}.)

The concern is legibility and clarity.

This:

       ((x) ? 32 - __builtin_clz(x) : 0)

is a clear expression in a way that this:

       ((x) ? (sizeof(int) * BITS_PER_BYTE) - __builtin_clz(x) : 0)

is not.  The problem is the extra binary expression, and this:

       ((x) ? BITS_PER_INT - __builtin_clz(x) : 0)

is still clear, because the reader doesn't have to perform a multiply to just to figure out what's going on.

Quoted 6 lines

It is definitely stupid to have each architecture provide their own BITS_PER_*.  The compiler is in a superior position to provide those details, and it should be in a common location.

I don't particularly mind how those constants are derived, but one key thing that BITS_PER_* can do which sizeof() can't is be used in #ifdef/etc.

What about moving them to xen/config.h? (if it isn't the best one place, any suggestion which is better?)

#define BYTES_PER_INT  (1 << INT_BYTEORDER)
#define BITS_PER_INT  (BYTES_PER_INT << 3)

#define BYTES_PER_LONG (1 << LONG_BYTEORDER)
#define BITS_PER_LONG (BYTES_PER_LONG << 3)
#define BITS_PER_BYTE 8

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

The *_BYTEORDER's are useless indirection.  BITS_PER_* should be defined straight up, and this will simplify quite a lot of preprocessing.

Could we really drop *_BYTEORDER?

For example, LONG_BYTEORDER for Arm could be either 2 or 3 depends on Arm32 or Arm64 is used.

The can still #define BITS_PER_LONG to 32 or 64 respectively, can't they?

Yes, but then if we want to move it to xen/config.h then it will be needed to:
in Arm's asm/config.h to have something like:

   #ifdef CONFIG_ARM_32
       #define BITS_PER_LONG 32
   #endif
Quoted 6 lines

and then in xen/config.h

   #ifndef BITS_PER_LONG
       #define BITS_PER_LONG 64

#endif

But I wanted to not have #ifndef BITS_PER_LONG in xen/config.h. (or using CONFIG_ARM_32 in xen/config.h)

Whatever the fundamental definitions that can vary per arch - those should imo live in asm/config.h. For the case here, if we deem *_BYTEORDER as fundamental, they go there (and BITS_PER_* go into xen/config.h). If we deem BITS_PER_* fundamental, they go into asm/config.h.

Quoted 6 lines

Stuff that we expect to remain uniform across ports (BITS_PER_BYTE, possibly also BITS_PER_INT BITS_PER_LLONG) may also go into xen/config.h, as long as we're okay with the possible future churn if a port appeared that has different needs.

I agree that there better wouldn't be #ifndef for such in xen/config.h.

With a new toolchain baseline, we get both of these concepts directly from the compiler environment.

I don't see any need for arch-specific overrides to these.  They ultimately come from the -march used.

~Andrew