: bitops cleanup and fixes
Subsystems (2)
v2 cover : bitops cleanup and fixes
Hi again!
This patch series is an update to "[PATCH 0/4]: bitops cleanup and fixes":
http://article.gmane.org/gmane.comp.boot-loaders.u-boot/66184
and contains the patches which were not accepted. The patches are:
0001-Move-__set-clear_bit-from-ubifs.h-to-bitops.h.patch
- Code style updates. I chose to not create asm-generic/include/bitops/ (Jean-Christophes comment) since I feel that should go together with a larger restructuring.
- Updated to put BIT_MASK above the include of asm/bitops.h
0002-Make-arm-bitops-endianness-independent.patch
- New patch which takes on the endianeess issue in arm bitops
0003-Define-ffs-fls-for-all-architectures.patch
- Code style updates (Wolfgangs comment).
0004-Define-test_and_set_bit-and-test_and_clear-bit-for-A.patch
- Defines test_and_set_bit etc for ARM. Uses the non-atomic __test_and_set_bit.
remove-dupliace-cr.patch was accepted into the u-boot-arm tree, so I'm not reposting it.
// Simon
PATCH v2 1/4 : Move __set/clear_bit from ubifs.h to bitops.h
__set_bit and __clear_bit are defined in ubifs.h as well as in asm/include/bitops.h for some architectures. This patch moves the generic implementation to include/linux/bitops.h and uses that unless it's defined by the architecture.
v2: Unify code style (newline between __set_bit and __clear_bit)
v3: Move BIT_MASK and BIT_WORD above the include of asm/bitops.h
--- fs/ubifs/ubifs.h | 32 -------------------------------- include/asm-avr32/bitops.h | 4 ++++ include/asm-m68k/bitops.h | 4 ++++ include/asm-nios/bitops.h | 4 ++++ include/asm-nios2/bitops.h | 4 ++++ include/asm-ppc/bitops.h | 4 ++++ include/asm-sh/bitops.h | 5 +++++ include/asm-sparc/bitops.h | 4 ++++ include/linux/bitops.h | 29 +++++++++++++++++++++++++++++ 9 files changed, 58 insertions(+), 32 deletions(-)
fs/ubifs/ubifs.h +0 -32
@@ -449,38 +449,6 @@ static inline ino_t parent_ino(struct dentry *dentry)return res; } -/* linux/include/linux/bitops.h */ - -#define BIT_MASK (nr) (1UL << ((nr) % BITS_PER_LONG)) -#define BIT_WORD (nr) ((nr) / BITS_PER_LONG) - -/* linux/include/asm-generic/bitops/non-atomic.h */ - -/** -* __set_bit - Set a bit in memory -* @nr: the bit to set -* @addr: the address to start counting from -* -* Unlike set_bit(), this function is non-atomic and may be reordered. -* 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,volatile unsigned long *addr) -{ -unsigned long mask =BIT_MASK (nr); -unsigned long *p = ((unsigned long *)addr) +BIT_WORD (nr); - - *p |= mask; -} - -static inline void __clear_bit (int nr,volatile unsigned long *addr) -{ -unsigned long mask =BIT_MASK (nr); -unsigned long *p = ((unsigned long *)addr) +BIT_WORD (nr); - - *p &= ~mask; -} -/* debug.c */ #define DEFINE_SPINLOCK (...)
include/asm-avr32/bitops.h +4 -0
@@ -22,4 +22,8 @@#ifndef __ASM_AVR32_BITOPS_H#define __ASM_AVR32_BITOPS_H +#define __set_bit (nr, addr) generic_set_bit(nr, addr) + +#define __clear_bit (nr, addr) generic_clear_bit(nr, addr) +#endif /* __ASM_AVR32_BITOPS_H */
include/asm-m68k/bitops.h +4 -0
@@ -15,6 +15,10 @@ extern int test_and_set_bit(int nr, volatile void *addr);extern int test_and_clear_bit (int nr,volatile void *addr);extern int test_and_change_bit (int nr,volatile void *addr); +#define __set_bit (nr, addr) generic_set_bit(nr, addr) + +#define __clear_bit (nr, addr) generic_clear_bit(nr, addr) +#ifdef __KERNEL__/*
include/asm-nios/bitops.h +4 -0
@@ -34,4 +34,8 @@ extern int test_and_change_bit(int nr, volatile void * addr);extern int test_bit (int nr,volatile void * a);extern int ffs (int i); +#define __set_bit (nr, addr) generic_set_bit(nr, addr) + +#define __clear_bit (nr, addr) generic_clear_bit(nr, addr) +#endif /* _ASM_NIOS_BITOPS_H */
include/asm-nios2/bitops.h +4 -0
@@ -34,4 +34,8 @@ extern int test_and_change_bit(int nr, volatile void * addr);extern int test_bit (int nr,volatile void * a);extern int ffs (int i); +#define __set_bit (nr, addr) generic_set_bit(nr, addr) + +#define __clear_bit (nr, addr) generic_clear_bit(nr, addr) +#endif /* __ASM_NIOS2_BITOPS_H */
include/asm-ppc/bitops.h +4 -0
@@ -144,6 +144,10 @@ extern __inline__ int test_and_change_bit(int nr, volatile void *addr) }#endif /* __INLINE_BITOPS */ +#define __set_bit (nr, addr) generic_set_bit(nr, addr) + +#define __clear_bit (nr, addr) generic_clear_bit(nr, addr) + extern __inline__ int test_bit(int nr, __const__ volatile void *addr) { __const__ unsigned int *p = (__const__ unsigned int *) addr;
include/asm-sh/bitops.h +5 -0
@@ -146,6 +146,11 @@ static inline int ffs (int x) }return r; } + +#define __set_bit (nr, addr) generic_set_bit(nr, addr) + +#define __clear_bit (nr, addr) generic_clear_bit(nr, addr) +#endif /* __KERNEL__ */ #endif /* __ASM_SH_BITOPS_H */
include/asm-sparc/bitops.h +4 -0
@@ -26,4 +26,8 @@#ifndef _SPARC_BITOPS_H#define _SPARC_BITOPS_H +#define __set_bit (nr, addr) generic_set_bit(nr, addr) + +#define __clear_bit (nr, addr) generic_clear_bit(nr, addr) +#endif /* _SPARC_BITOPS_H */
include/linux/bitops.h +29 -0
@@ -66,7 +66,36 @@ static inline unsigned int generic_hweight8(unsigned int w) return (res & 0x0F) + ((res >> 4) & 0x0F); } +#define BIT_MASK (nr) (1UL << ((nr) % BITS_PER_LONG)) +#define BIT_WORD (nr) ((nr) / BITS_PER_LONG) +#include <asm/bitops.h> +/* linux/include/asm-generic/bitops/non-atomic.h */ + +/** +* __set_bit - Set a bit in memory +* @nr: the bit to set +* @addr: the address to start counting from +* +* Unlike set_bit(), this function is non-atomic and may be reordered. +* If it's called on the same region of memory simultaneously, the effect +* may be that only one operation succeeds. +*/ +static inline void generic_set_bit (int nr,volatile unsigned long *addr) +{ +unsigned long mask =BIT_MASK (nr); +unsigned long *p = ((unsigned long *)addr) +BIT_WORD (nr); + + *p |= mask; +} + +static inline void generic_clear_bit (int nr,volatile unsigned long *addr) +{ +unsigned long mask =BIT_MASK (nr); +unsigned long *p = ((unsigned long *)addr) +BIT_WORD (nr); + + *p &= ~mask; +}#endif
Signature · 1 line
1.6.0.4
PATCH v2 1/4 : Move __set/clear_bit from ubifs.h to bitops.h
include/asm-sh/bitops.h static inline int ffs (int x) · line 152
Quoted 10 lines · On Thursday 20 August 2009 04:52:50 Simon Kagstrom wrote:
include/asm-sh/bitops.h +4
-0@@ -146,6 +146,11 @@ static inline int ffs (int x) }return r; } + +#define __set_bit (nr, addr) generic_set_bit(nr, addr) + +#define __clear_bit (nr, addr) generic_clear_bit(nr, addr)
this is just silly to put into every arch header. why not do in the common code:
#ifndef __set_bit
# define __set_bit generice_set_bit #endif -mike -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 836 bytes Desc: This is a digitally signed message part. Url : http://lists.denx.de/pipermail/u-boot/attachments/20090820/766bb19e/attachment.pgp
PATCH v2 1/4 : Move __set/clear_bit from ubifs.h to bitops.h
Dear Mike Frysinger,
Quoted 10 lines · In message <[email protected]> you wrote:
include/asm-avr32/bitops.h line 27
Quoted 4 lines
include/asm-avr32/bitops.h +3
-0+#define __set_bit (nr, addr) generic_set_bit(nr, addr) + +#define __clear_bit (nr, addr) generic_clear_bit(nr, addr)this is just silly to put into every arch header. why not do in the common code:
#ifndef__set_bit# define __set_bit generice_set_bit #endif
Because it doesn't work?
The code usually looks like this: "static inline void __set_bit(...)..."
This defines a static inline function, but _not_ a preprocessor variable of that name.
Best regards,
Wolfgang Denk
Signature · 5 lines
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de When a program is being tested, it is too late to make design changes. -- Geoffrey James, "The Tao of Programming"
PATCH v2 1/4 : Move __set/clear_bit from ubifs.h to bitops.h
Quoted 11 lines · On Thursday 20 August 2009 14:31:45 Wolfgang Denk wrote:
Quoted 9 lines · Mike Frysinger
include/asm-avr32/bitops.h line 27
Quoted 3 lines
include/asm-avr32/bitops.h +3
-0+#define __set_bit (nr, addr) generic_set_bit(nr, addr) + +#define __clear_bit (nr, addr) generic_clear_bit(nr, addr)this is just silly to put into every arch header. why not do in the common code:
#ifndef__set_bit# define __set_bit generice_set_bit #endif
Quoted 6 lines
Because it doesn't work?
The code usually looks like this: "static inline void
__set_bit(...)..."This defines a static inline function, but _not_ a preprocessor variable of that name.
i know what the code looks like. the obvious extensions was that the arches would do something like:
#define __set_bit __set_bit
to indicate that they have their own replacement
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20090820/f97fe5b8/attachment.pgp
PATCH v2 1/4 : Move __set/clear_bit from ubifs.h to bitops.h
In reply to Mike FrysingerQuoted 15 lines · On Thu, 20 Aug 2009 13:44:35 -0400 Mike Frysinger <[email protected]> wrote:
include/asm-sh/bitops.h static inline int ffs (int x) · line 152
Quoted 10 lines · On Thursday 20 August 2009 04:52:50 Simon Kagstrom wrote:
include/asm-sh/bitops.h +4
-0@@ -146,6 +146,11 @@ static inline int ffs (int x) }return r; } + +#define __set_bit (nr, addr) generic_set_bit(nr, addr) + +#define __clear_bit (nr, addr) generic_clear_bit(nr, addr)this is just silly to put into every arch header. why not do in the common code:
#ifndef__set_bit
Quoted 2 lines
# define __set_bit generice_set_bit #endif
It's a function (static inline) in some architectures.
// Simon
PATCH v2 1/4 : Move __set/clear_bit from ubifs.h to bitops.h
Quoted 19 lines · On Friday 21 August 2009 02:05:47 Simon Kagstrom wrote:
Quoted 15 lines · On Thu, 20 Aug 2009 13:44:35 -0400 Mike Frysinger wrote:
include/asm-sh/bitops.h static inline int ffs (int x) · line 152
Quoted 10 lines · On Thursday 20 August 2009 04:52:50 Simon Kagstrom wrote:
include/asm-sh/bitops.h +4
-0@@ -146,6 +146,11 @@ static inline int ffs (int x) }return r; } + +#define __set_bit (nr, addr) generic_set_bit(nr, addr) + +#define __clear_bit (nr, addr) generic_clear_bit(nr, addr)this is just silly to put into every arch header. why not do in the common code:
#ifndef__set_bitQuoted 2 lines
# define __set_bit generice_set_bit #endif
Quoted 1 line
It's a function (static inline) in some architectures.
add the obvious #define i already posted in a later e-mail. it makes a lot more sense for the non-common behavior of providing a custom implementation to add one more line per func than to try and update (and keep up-to-date) every arch using the common code.
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20090821/4c546506/attachment.pgp
PATCH v2 1/4 : Move __set/clear_bit from ubifs.h to bitops.h
Quoted 6 lines · On Fri, 21 Aug 2009 02:29:30 -0400 Mike Frysinger <[email protected]> wrote:
Quoted 6 lines
Quoted 5 lines
this is just silly to put into every arch header. why not do in the common code:
#ifndef__set_bit# define __set_bit generice_set_bit #endif
Quoted 6 lines
Quoted 1 line
It's a function (static inline) in some architectures.
add the obvious #define i already posted in a later e-mail. it makes a lot more sense for the non-common behavior of providing a custom implementation to add one more line per func than to try and update (and keep up-to-date) every arch using the common code.
Yep, I saw your mail too late. I'll rework the patch series and resubmit.
// Simon
PATCH v2 2/4 : Make arm bitops endianness-independent
In reply to Simon KagstromBring over the bitop implementations from the Linux include/asm-generic/bitops/non-atomic.h to provide endianness-independence.
--- include/asm-arm/bitops.h | 45 +++++++++++++++++++++++++++------------------ 1 files changed, 27 insertions(+), 18 deletions(-)
include/asm-arm/bitops.h +27 -18
@@ -27,57 +27,66 @@ extern void set_bit(int nr, volatile void * addr);static inline void __set_bit (int nr,volatile void *addr) { - ((unsigned char *) addr)[nr >>3 ] |= (1U << (nr &7 )); +unsigned long mask =BIT_MASK (nr); +unsigned long *p = ((unsigned long *)addr) +BIT_WORD (nr); + + *p |= mask; }extern void clear_bit (int nr,volatile void * addr);static inline void __clear_bit (int nr,volatile void *addr) { - ((unsigned char *) addr)[nr >>3 ] &= ~(1U << (nr &7 )); +unsigned long mask =BIT_MASK (nr); +unsigned long *p = ((unsigned long *)addr) +BIT_WORD (nr); + + *p &= ~mask; }extern void change_bit (int nr,volatile void * addr);static inline void __change_bit (int nr,volatile void *addr) { - ((unsigned char *) addr)[nr >>3 ] ^= (1U << (nr &7 )); +unsigned long mask =BIT_MASK (nr); +unsigned long *p = ((unsigned long *)addr) +BIT_WORD (nr); + + *p ^= mask; }extern int test_and_set_bit (int nr,volatile void * addr);static inline int __test_and_set_bit (int nr,volatile void *addr) { -unsigned int mask =1 << (nr &7 ); -unsigned int oldval; +unsigned long mask =BIT_MASK (nr); +unsigned long *p = ((unsigned long *)addr) +BIT_WORD (nr); +unsigned long old = *p; - oldval = ((unsigned char *) addr)[nr >>3 ]; - ((unsigned char *) addr)[nr >>3 ] = oldval | mask; -return oldval & mask; + *p = old | mask; +return (old & mask) !=0 ; }extern int test_and_clear_bit (int nr,volatile void * addr);static inline int __test_and_clear_bit (int nr,volatile void *addr) { -unsigned int mask =1 << (nr &7 ); -unsigned int oldval; +unsigned long mask =BIT_MASK (nr); +unsigned long *p = ((unsigned long *)addr) +BIT_WORD (nr); +unsigned long old = *p; - oldval = ((unsigned char *) addr)[nr >>3 ]; - ((unsigned char *) addr)[nr >>3 ] = oldval & ~mask; -return oldval & mask; + *p = old & ~mask; +return (old & mask) !=0 ; }extern int test_and_change_bit (int nr,volatile void * addr);static inline int __test_and_change_bit (int nr,volatile void *addr) { -unsigned int mask =1 << (nr &7 ); -unsigned int oldval; +unsigned long mask =BIT_MASK (nr); +unsigned long *p = ((unsigned long *)addr) +BIT_WORD (nr); +unsigned long old = *p; - oldval = ((unsigned char *) addr)[nr >>3 ]; - ((unsigned char *) addr)[nr >>3 ] = oldval ^ mask; -return oldval & mask; + *p = old ^ mask; +return (old & mask) !=0 ; }extern int find_first_zero_bit (void * addr,unsigned size);
Signature · 1 line
1.6.0.4
PATCH v2 3/4 : Define ffs/fls for all architectures
In reply to Simon KagstromUBIFS requires fls(), which is not defined for arm (and some other architectures) and this patch adds it. The implementation is taken from Linux and is generic. ffs() is also defined for those that miss it.
v2: Unify code style (empty line between ffs/fls)
--- include/asm-arm/bitops.h | 4 ++++ include/asm-avr32/bitops.h | 4 ++++ include/asm-i386/bitops.h | 2 ++ include/asm-m68k/bitops.h | 2 ++ include/asm-microblaze/bitops.h | 2 ++ include/asm-mips/bitops.h | 2 ++ include/asm-nios/bitops.h | 5 ++++- include/asm-nios2/bitops.h | 5 ++++- include/asm-sh/bitops.h | 2 ++ include/asm-sparc/bitops.h | 4 ++++ include/linux/bitops.h | 37 +++++++++++++++++++++++++++++++++++++ 11 files changed, 67 insertions(+), 2 deletions(-)
include/asm-arm/bitops.h +4 -0
@@ -17,6 +17,8 @@#ifdef __KERNEL__ +#include <asm/types.h> +#define smp_mb__before_clear_bit () do { } while (0)#define smp_mb__after_clear_bit () do { } while (0)
@@ -126,6 +128,8 @@ static inline unsigned long ffz(unsigned long word)#define ffs (x) generic_ffs(x) +#define fls (x) generic_fls(x) +/* * hweightN: returns the hamming weight (i.e. the number * of bits set) of a N-bit word
include/asm-avr32/bitops.h +4 -0
@@ -26,4 +26,8 @@#define __clear_bit (nr, addr) generic_clear_bit(nr, addr) +#define ffs (x) generic_ffs(x) + +#define fls (x) generic_fls(x) +#endif /* __ASM_AVR32_BITOPS_H */
include/asm-i386/bitops.h +2 -0
@@ -350,6 +350,8 @@ static __inline__ int ffs(int x)return r+1 ; } +#define fls (x) generic_fls(x) +/** * hweightN - returns the hamming weight of a N-bit word * @x: the word to weigh
include/asm-m68k/bitops.h +2 -0
@@ -56,6 +56,8 @@ extern __inline__ int ffs(int x) }#define __ffs (x) (ffs(x) - 1) +#define fls (x) generic_fls(x) +#endif /* __KERNEL__ */ #endif /* _M68K_BITOPS_H */
include/asm-microblaze/bitops.h +2 -0
@@ -266,6 +266,8 @@ found_middle:#define ffs (x) generic_ffs(x) +#define fls (x) generic_fls(x) +/* * hweightN: returns the hamming weight (i.e. the number * of bits set) of a N-bit word
include/asm-mips/bitops.h +2 -0
@@ -716,6 +716,8 @@ static __inline__ unsigned long ffz(unsigned long word)#define ffs (x) generic_ffs(x) +#define fls (x) generic_fls(x) +/* * hweightN - returns the hamming weight of a N-bit word * @x: the word to weigh
include/asm-nios/bitops.h +4 -1
@@ -32,7 +32,10 @@ extern void change_bit(unsigned long nr, volatile void *addr);extern int test_and_set_bit (int nr,volatile void * a);extern int test_and_change_bit (int nr,volatile void * addr);extern int test_bit (int nr,volatile void * a); -extern int ffs (int i); + +#define ffs (x) generic_ffs(x) + +#define fls (x) generic_fls(x)#define __set_bit (nr, addr) generic_set_bit(nr, addr)
include/asm-nios2/bitops.h +4 -1
@@ -32,7 +32,10 @@ extern void change_bit(unsigned long nr, volatile void *addr);extern int test_and_set_bit (int nr,volatile void * a);extern int test_and_change_bit (int nr,volatile void * addr);extern int test_bit (int nr,volatile void * a); -extern int ffs (int i); + +#define ffs (x) generic_ffs(x) + +#define fls (x) generic_fls(x)#define __set_bit (nr, addr) generic_set_bit(nr, addr)
include/asm-sh/bitops.h +2 -0
@@ -147,6 +147,8 @@ static inline int ffs (int x)return r; } +#define fls (x) generic_fls(x) +#define __set_bit (nr, addr) generic_set_bit(nr, addr)#define __clear_bit (nr, addr) generic_clear_bit(nr, addr)
include/asm-sparc/bitops.h +4 -0
@@ -30,4 +30,8 @@#define __clear_bit (nr, addr) generic_clear_bit(nr, addr) +#define ffs (x) generic_ffs(x) + +#define fls (x) generic_fls(x) +#endif /* _SPARC_BITOPS_H */
include/linux/bitops.h +37 -0
@@ -37,6 +37,43 @@ static inline int generic_ffs(int x)return r; } +/** +* fls - find last (most-significant) bit set +* @x: the word to search +* +* This is defined the same way as ffs. +* Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. +*/ +static inline int generic_fls (int x) +{ +int r =32 ; + +if (!x) +return 0 ; +if (!(x &0xffff0000u )) { + x <<=16 ; + r -=16 ; + } +if (!(x &0xff000000u )) { + x <<=8 ; + r -=8 ; + } +if (!(x &0xf0000000u )) { + x <<=4 ; + r -=4 ; + } +if (!(x &0xc0000000u )) { + x <<=2 ; + r -=2 ; + } +if (!(x &0x80000000u )) { + x <<=1 ; + r -=1 ; + } +return r; +} + +/* * hweightN: returns the hamming weight (i.e. the number * of bits set) of a N-bit word
Signature · 1 line
1.6.0.4
PATCH v2 4/4 : Define test_and_set_bit and test_and_clear bit for ARM
In reply to Simon KagstromNeeded for (e.g.) ubifs support to work.
--- include/asm-arm/bitops.h | 27 ++++++++++++++++++++++++--- 1 files changed, 24 insertions(+), 3 deletions(-)
include/asm-arm/bitops.h +24 -3
@@ -18,6 +18,7 @@#ifdef __KERNEL__#include <asm/types.h> +#include <asm/proc/system.h> #define smp_mb__before_clear_bit () do { } while (0)#define smp_mb__after_clear_bit () do { } while (0)
@@ -55,8 +56,6 @@ static inline void __change_bit(int nr, volatile void *addr) *p ^= mask; } -extern int test_and_set_bit (int nr,volatile void * addr); -static inline int __test_and_set_bit (int nr,volatile void *addr) {unsigned long mask =BIT_MASK (nr);
@@ -67,7 +66,17 @@ static inline int __test_and_set_bit(int nr, volatile void *addr)return (old & mask) !=0 ; } -extern int test_and_clear_bit (int nr,volatile void * addr); +static inline int test_and_set_bit (int nr,volatile void * addr) +{ +unsigned long flags; +int out; + +local_irq_save (flags); + out =__test_and_set_bit (nr, addr); +local_irq_restore (flags); + +return out; +}static inline int __test_and_clear_bit (int nr,volatile void *addr) {
@@ -79,6 +88,18 @@ static inline int __test_and_clear_bit(int nr, volatile void *addr)return (old & mask) !=0 ; } +static inline int test_and_clear_bit (int nr,volatile void * addr) +{ +unsigned long flags; +int out; + +local_irq_save (flags); + out =__test_and_clear_bit (nr, addr); +local_irq_restore (flags); + +return out; +} +extern int test_and_change_bit (int nr,volatile void * addr);static inline int __test_and_change_bit (int nr,volatile void *addr)
Signature · 1 line
1.6.0.4