bitops patches
v3 cover bitops patches
Improve bit operations.
v2->v3
1/2: drop also now useless casts.
2/2: unify to 'unsigned long' which is used by kernel. Drop bool patch (3).
v1->v2
Split from single patch.
Blue Swirl (2):
bitops: drop volatile qualifier
bitops: fix types
bitops.h | 52 +++++++++++++++++++++++++++------------------------- 1 files changed, 27 insertions(+), 25 deletions(-)
Signature · 1 line
1.7.2.5
PATCH v3 1/2 bitops: drop volatile qualifier
Qualifier 'volatile' is not useful for applications, it's too strict for single threaded code but does not give the real atomicity guarantees needed for multithreaded code.
Drop them and now useless casts.
--- bitops.h | 26 +++++++++++++------------- 1 files changed, 13 insertions(+), 13 deletions(-)
bitops.h +13 -13
@@ -114,10 +114,10 @@ static inline unsigned long ffz(unsigned long word)* @nr: the bit to set * @addr: the address to start counting from */ -static inline void set_bit (int nr,volatile unsigned long *addr) +static inline void set_bit (int nr,unsigned long *addr) {unsigned long mask =BIT_MASK (nr); -unsigned long *p = ((unsigned long *)addr) +BIT_WORD (nr); +unsigned long *p = addr +BIT_WORD (nr); *p |= mask; }
@@ -127,10 +127,10 @@ static inline void set_bit(int nr, volatile unsigned long *addr)* @nr: Bit to clear * @addr: Address to start counting from */ -static inline void clear_bit (int nr,volatile unsigned long *addr) +static inline void clear_bit (int nr,unsigned long *addr) {unsigned long mask =BIT_MASK (nr); -unsigned long *p = ((unsigned long *)addr) +BIT_WORD (nr); +unsigned long *p = addr +BIT_WORD (nr); *p &= ~mask; }
@@ -140,10 +140,10 @@ static inline void clear_bit(int nr, volatile unsigned long *addr)* @nr: Bit to change * @addr: Address to start counting from */ -static inline void change_bit (int nr,volatile unsigned long *addr) +static inline void change_bit (int nr,unsigned long *addr) {unsigned long mask =BIT_MASK (nr); -unsigned long *p = ((unsigned long *)addr) +BIT_WORD (nr); +unsigned long *p = addr +BIT_WORD (nr); *p ^= mask; }
@@ -153,10 +153,10 @@ static inline void change_bit(int nr, volatile unsigned long *addr)* @nr: Bit to set * @addr: Address to count from */ -static inline int test_and_set_bit (int nr,volatile unsigned long *addr) +static inline int test_and_set_bit (int nr,unsigned long *addr) {unsigned long mask =BIT_MASK (nr); -unsigned long *p = ((unsigned long *)addr) +BIT_WORD (nr); +unsigned long *p = addr +BIT_WORD (nr);unsigned long old = *p; *p = old | mask;
@@ -168,10 +168,10 @@ static inline int test_and_set_bit(int nr, volatile unsigned long *addr)* @nr: Bit to clear * @addr: Address to count from */ -static inline int test_and_clear_bit (int nr,volatile unsigned long *addr) +static inline int test_and_clear_bit (int nr,unsigned long *addr) {unsigned long mask =BIT_MASK (nr); -unsigned long *p = ((unsigned long *)addr) +BIT_WORD (nr); +unsigned long *p = addr +BIT_WORD (nr);unsigned long old = *p; *p = old & ~mask;
@@ -183,10 +183,10 @@ static inline int test_and_clear_bit(int nr, volatile unsigned long *addr)* @nr: Bit to change * @addr: Address to count from */ -static inline int test_and_change_bit (int nr,volatile unsigned long *addr) +static inline int test_and_change_bit (int nr,unsigned long *addr) {unsigned long mask =BIT_MASK (nr); -unsigned long *p = ((unsigned long *)addr) +BIT_WORD (nr); +unsigned long *p = addr +BIT_WORD (nr);unsigned long old = *p; *p = old ^ mask;
@@ -198,7 +198,7 @@ static inline int test_and_change_bit(int nr, volatile unsigned long *addr)* @nr: bit number to test * @addr: Address to start counting from */ -static inline int test_bit (int nr,const volatile unsigned long *addr) +static inline int test_bit (int nr,const unsigned long *addr) {return 1UL & (addr[BIT_WORD (nr)] >> (nr & (BITS_PER_LONG -1 ))); }
Signature · 1 line
1.7.2.5
Re: [Qemu-devel] [PATCH v3 1/2] bitops: drop volatile qualifier
Quoted 7 lines · Blue Swirl
Qualifier 'volatile' is not useful for applications, it's too strict for single threaded code but does not give the real atomicity guarantees needed for multithreaded code.
Drop them and now useless casts.
Signed-off-by Blue Swirl <[email protected]>
-- PMM
Re: [Qemu-devel] [PATCH v3 1/2] bitops: drop volatile qualifier
In reply to Blue Swirlbitops.h static inline unsigned long ffz(unsigned long word) · line 120
Quoted 13 lines · Blue Swirl
Qualifier 'volatile' is not useful for applications, it's too strict for single threaded code but does not give the real atomicity guarantees needed for multithreaded code.
Drop them and now useless casts.
bitops.h +2
-2-static inline void set_bit (int nr,volatile unsigned long *addr) +static inline void set_bit (int nr,unsigned long *addr) {unsigned long mask =BIT_MASK (nr); -unsigned long *p = ((unsigned long *)addr) +BIT_WORD (nr); +unsigned long *p = addr +BIT_WORD (nr);
The diff looks weird, because you are converting TAB to space on your affected lines but not cleaning up the neighboring lines. Is it worth a preliminary patch to whitespace-sanitize things?
Signature · 2 lines
Eric Blake [email protected] +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v3 1/2] bitops: drop volatile qualifier
Quoted 8 lines · Eric Blake
Quoted 6 lines · Blue Swirl
Qualifier 'volatile' is not useful for applications, it's too strict for single threaded code but does not give the real atomicity guarantees needed for multithreaded code.
Drop them and now useless casts.
Quoted 10 lines
bitops.h static inline unsigned long ffz(unsigned long word) · line 120
Quoted 6 lines
bitops.h +2
-2-static inline void set_bit (int nr,volatile unsigned long *addr) +static inline void set_bit (int nr,unsigned long *addr) {unsigned long mask =BIT_MASK (nr); -unsigned long *p = ((unsigned long *)addr) +BIT_WORD (nr); +unsigned long *p = addr +BIT_WORD (nr);The diff looks weird, because you are converting TAB to space on your affected lines but not cleaning up the neighboring lines. Is it worth a preliminary patch to whitespace-sanitize things?
That is close to reformatting, this is the usual way these days.
Quoted 5 lines
--
Eric Blake [email protected] +1-919-301-3266
Libvirt virtualization library http://libvirt.org
PATCH v3 2/2 bitops: fix types
In reply to Blue Swirlbitops.h uses inconsistently 'unsigned long' and 'int' for bit numbers.
Unify to 'unsigned long' because it generates better code on x86_64. Adjust asserts accordingly.
--- bitops.h | 40 +++++++++++++++++++++------------------- 1 files changed, 21 insertions(+), 19 deletions(-)
bitops.h +21 -19
@@ -30,7 +30,7 @@*/ static unsigned long bitops_ffsl (unsigned long word) { -int num =0 ; +unsigned long num =0 ;#if LONG_MAX >0x7FFFFFFF if ((word &0xffffffff ) ==0 ) {
@@ -68,7 +68,7 @@ static unsigned long bitops_ffsl(unsigned long word)*/ static inline unsigned long bitops_flsl (unsigned long word) { -int num =BITS_PER_LONG -1 ; +unsigned long num =BITS_PER_LONG -1 ;#if LONG_MAX >0x7FFFFFFF if (!(word & (~0ul <<32 ))) {
@@ -114,7 +114,7 @@ static inline unsigned long ffz(unsigned long word)* @nr: the bit to set * @addr: the address to start counting from */ -static inline void set_bit (int nr,unsigned long *addr) +static inline void set_bit (unsigned long nr,unsigned long *addr) {unsigned long mask =BIT_MASK (nr);unsigned long *p = addr +BIT_WORD (nr);
@@ -127,7 +127,7 @@ static inline void set_bit(int nr, unsigned long *addr)* @nr: Bit to clear * @addr: Address to start counting from */ -static inline void clear_bit (int nr,unsigned long *addr) +static inline void clear_bit (unsigned long nr,unsigned long *addr) {unsigned long mask =BIT_MASK (nr);unsigned long *p = addr +BIT_WORD (nr);
@@ -140,7 +140,7 @@ static inline void clear_bit(int nr, unsigned long *addr)* @nr: Bit to change * @addr: Address to start counting from */ -static inline void change_bit (int nr,unsigned long *addr) +static inline void change_bit (unsigned long nr,unsigned long *addr) {unsigned long mask =BIT_MASK (nr);unsigned long *p = addr +BIT_WORD (nr);
@@ -153,7 +153,7 @@ static inline void change_bit(int nr, unsigned long *addr)* @nr: Bit to set * @addr: Address to count from */ -static inline int test_and_set_bit (int nr,unsigned long *addr) +static inline int test_and_set_bit (unsigned long nr,unsigned long *addr) {unsigned long mask =BIT_MASK (nr);unsigned long *p = addr +BIT_WORD (nr);
@@ -168,7 +168,7 @@ static inline int test_and_set_bit(int nr, unsigned long *addr)* @nr: Bit to clear * @addr: Address to count from */ -static inline int test_and_clear_bit (int nr,unsigned long *addr) +static inline int test_and_clear_bit (unsigned long nr,unsigned long *addr) {unsigned long mask =BIT_MASK (nr);unsigned long *p = addr +BIT_WORD (nr);
@@ -183,7 +183,7 @@ static inline int test_and_clear_bit(int nr, unsigned long *addr)* @nr: Bit to change * @addr: Address to count from */ -static inline int test_and_change_bit (int nr,unsigned long *addr) +static inline int test_and_change_bit (unsigned long nr,unsigned long *addr) {unsigned long mask =BIT_MASK (nr);unsigned long *p = addr +BIT_WORD (nr);
@@ -198,7 +198,7 @@ static inline int test_and_change_bit(int nr, unsigned long *addr)* @nr: bit number to test * @addr: Address to start counting from */ -static inline int test_bit (int nr,const unsigned long *addr) +static inline int test_bit (unsigned long nr,const unsigned long *addr) {return 1UL & (addr[BIT_WORD (nr)] >> (nr & (BITS_PER_LONG -1 ))); }
@@ -282,9 +282,10 @@ static inline unsigned long hweight_long(unsigned long w)* * Returns: the value of the bit field extracted from the input value. */ -static inline uint32_t extract32 (uint32_t value,int start,int length) +static inline uint32_t extract32 (uint32_t value,unsigned long start, +unsigned long length) { -assert (start >=0 && length >0 && length <=32 - start); +assert (start <32 && length >0 && length <=32 && start + length <=32 );return (value >> start) & (~0U >> (32 - length)); }
@@ -301,9 +302,10 @@ static inline uint32_t extract32(uint32_t value, int start, int length)* * Returns: the value of the bit field extracted from the input value. */ -static inline uint64_t extract64 (uint64_t value,int start,int length) +static inline uint64_t extract64 (uint64_t value,unsigned long start, +unsigned long length) { -assert (start >=0 && length >0 && length <=64 - start); +assert (start <64 && length >0 && length <=64 && start + length <=64 );return (value >> start) & (~0ULL >> (64 - length)); }
@@ -324,11 +326,11 @@ static inline uint64_t extract64(uint64_t value, int start, int length)* * Returns: the modified @value. */ -static inline uint32_t deposit32 (uint32_t value,int start,int length, -uint32_t fieldval) +static inline uint32_t deposit32 (uint32_t value,unsigned long start, +unsigned long length,uint32_t fieldval) {uint32_t mask; -assert (start >=0 && length >0 && length <=32 - start); +assert (start <32 && length >0 && length <=32 && start + length <=32 ); mask = (~0U >> (32 - length)) << start;return (value & ~mask) | ((fieldval << start) & mask); }
@@ -350,11 +352,11 @@ static inline uint32_t deposit32(uint32_t value, int start, int length,* * Returns: the modified @value. */ -static inline uint64_t deposit64 (uint64_t value,int start,int length, -uint64_t fieldval) +static inline uint64_t deposit64 (uint64_t value,unsigned long start, +unsigned long length,uint64_t fieldval) {uint64_t mask; -assert (start >=0 && length >0 && length <=64 - start); +assert (start <64 && length >0 && length <=64 && start + length <=64 ); mask = (~0ULL >> (64 - length)) << start;return (value & ~mask) | ((fieldval << start) & mask); }
Signature · 1 line
1.7.2.5
Re: [Qemu-devel] [PATCH v3 2/2] bitops: fix types
Quoted 5 lines · Blue Swirl
bitops.huses inconsistently 'unsigned long' and 'int' for bit numbers.Unify to 'unsigned long' because it generates better code on
x86_64. Adjust asserts accordingly.
Actually, plain unsigned generates the best code. unsigned longs require an extra byte for many instructions. It also requires more stack space if spilled. Unsigned does not, but is compatible with unsigned long in case it needs to be used in pointer arithmetic (unlike signed int which needs a sign extension instruction).
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [Qemu-devel] [PATCH v3 2/2] bitops: fix types
In reply to Blue SwirlQuoted 4 lines · Blue Swirl
bitops.huses inconsistently 'unsigned long' and 'int' for bit numbers.Unify to 'unsigned long' because it generates better code on
x86_64. Adjust asserts accordingly.
Still disagree with this patch, for the record.
-- PMM
Re: [Qemu-devel] [PATCH v3 2/2] bitops: fix types
Quoted 7 lines · Peter Maydell
Quoted 4 lines · Blue Swirl
bitops.huses inconsistently 'unsigned long' and 'int' for bit numbers.Unify to 'unsigned long' because it generates better code on
x86_64. Adjust asserts accordingly.Still disagree with this patch, for the record.
So do I.
Changing tried-and-true code for some unproven performance gain is a bad idea.
In this particular case, it additionally deviates from the code's source.
If you feel your patch is a worthwhile improvement, please take it to LKML, so the kernel and future borrowers of this code can profit. Copying free code without at least trying to contribute improvements back to the source isn't proper.
Re: [Qemu-devel] [PATCH v3 2/2] bitops: fix types
Quoted 8 lines · Markus Armbruster
Quoted 6 lines · Peter Maydell
Quoted 4 lines · Blue Swirl
bitops.huses inconsistently 'unsigned long' and 'int' for bit numbers.Unify to 'unsigned long' because it generates better code on
x86_64. Adjust asserts accordingly.
Quoted 6 lines
Quoted 1 line
Still disagree with this patch, for the record.
So do I.
Changing tried-and-true code for some unproven performance gain is a bad idea.
No it isn't and that is not the case.
Quoted 3 lines
In this particular case, it additionally deviates from the code's source.
I'm getting a strong feeling that it's a bad idea to reuse any Linux kernel sources since they are seen as divine and untouchable, unlike for example BSD queue macros. Perhaps BSD have also bit field ops which we could use instead? There's also the licensing problem with GPLv2only in some cases.
Quoted 5 lines
If you feel your patch is a worthwhile improvement, please take it to LKML, so the kernel and future borrowers of this code can profit. Copying free code without at least trying to contribute improvements back to the source isn't proper.
The original kernel functions use 'unsigned long'. The bit field functions introduced by Peter use 'int' but those are not in the kernel tree, so there's nothing to fix (except the two first hunks) from the kernel point of view. Also 'volatile' makes sense when kernel is banging HW registers.
Re: [Qemu-devel] [PATCH v3 2/2] bitops: fix types
Quoted 3 lines · Blue Swirl
I'm getting a strong feeling that it's a bad idea to reuse any Linux kernel sources since they are seen as divine and untouchable, unlike for example BSD queue macros.
We should also try to avoid deviations in our queue macros, and I think we do (eg commit 6095aa8 added functionality by moving us closer into sync with the BSD macros rather than by reinventing the wheel which was IIRC what the initial pre-code-review patch did).
-- PMM
Re: [Qemu-devel] [PATCH v3 2/2] bitops: fix types
Quoted 4 lines · Peter Maydell
Quoted 3 lines · Blue Swirl
I'm getting a strong feeling that it's a bad idea to reuse any Linux kernel sources since they are seen as divine and untouchable, unlike for example BSD queue macros.
Reusing good code that solves the problem at hand can be a bad idea if you can't resist the temptation to tinker with it, yet can't be bothered to upstream your improvements. Then you might as well build your own bikeshed from scratch :)
Quoted 1 line
We should also try to avoid deviations in our queue macros,
Agree.
Quoted 4 lines
and I think we do (eg commit 6095aa8 added functionality by moving us closer into sync with the BSD macros rather than by reinventing the wheel which was IIRC what the initial pre-code-review patch did).
Re: [Qemu-devel] [PATCH v3 2/2] bitops: fix types
Quoted 11 lines · Markus Armbruster
Quoted 4 lines · Peter Maydell
Quoted 3 lines · Blue Swirl
I'm getting a strong feeling that it's a bad idea to reuse any Linux kernel sources since they are seen as divine and untouchable, unlike for example BSD queue macros.
Reusing good code that solves the problem at hand can be a bad idea if you can't resist the temptation to tinker with it, yet can't be bothered to upstream your improvements. Then you might as well build your own bikeshed from scratch :)
There's nothing wrong in tinkering with reused good code. As I explained, there's little point to upstream these changes, so 'not bothering' is false accusation.
Quoted 4 lines
Quoted 2 lines
We should also try to avoid deviations in our queue macros,
Agree.
Avoiding "deviations" can be secondary to many other needs.
Quoted 5 lines
Quoted 5 lines
and I think we do (eg commit 6095aa8 added functionality by moving us closer into sync with the BSD macros rather than by reinventing the wheel which was IIRC what the initial pre-code-review patch did).