X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=include%2Fasm-i386%2Fbitops.h;h=1c780fa1e762e966a6f6255dbf0e5bbf4a326e65;hb=97bf2856c6014879bd04983a3e9dfcdac1e7fe85;hp=8fd938fa988687c97c7696ed70152011c999daa9;hpb=9bf4aaab3e101692164d49b7ca357651eb691cb6;p=linux-2.6.git diff --git a/include/asm-i386/bitops.h b/include/asm-i386/bitops.h index 8fd938fa9..1c780fa1e 100644 --- a/include/asm-i386/bitops.h +++ b/include/asm-i386/bitops.h @@ -5,8 +5,8 @@ * Copyright 1992, Linus Torvalds. */ -#include #include +#include /* * These have to be done with inline assembly: that way the bit-setting @@ -16,12 +16,6 @@ * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1). */ -#ifdef CONFIG_SMP -#define LOCK_PREFIX "lock ; " -#else -#define LOCK_PREFIX "" -#endif - #define ADDR (*(volatile long *) addr) /** @@ -31,6 +25,11 @@ * * This function is atomic and may not be reordered. See __set_bit() * if you do not require the atomic guarantees. + * + * Note: there are no guarantees that this function will not be reordered + * on non x86 architectures, so if you are writting portable code, + * make sure not to rely on its reordering guarantees. + * * Note that @nr may be almost arbitrarily large; this function is not * restricted to acting on a single-word quantity. */ @@ -38,7 +37,7 @@ static inline void set_bit(int nr, volatile unsigned long * addr) { __asm__ __volatile__( LOCK_PREFIX "btsl %1,%0" - :"=m" (ADDR) + :"+m" (ADDR) :"Ir" (nr)); } @@ -55,7 +54,7 @@ static inline void __set_bit(int nr, volatile unsigned long * addr) { __asm__( "btsl %1,%0" - :"=m" (ADDR) + :"+m" (ADDR) :"Ir" (nr)); } @@ -73,7 +72,7 @@ static inline void clear_bit(int nr, volatile unsigned long * addr) { __asm__ __volatile__( LOCK_PREFIX "btrl %1,%0" - :"=m" (ADDR) + :"+m" (ADDR) :"Ir" (nr)); } @@ -81,7 +80,7 @@ static inline void __clear_bit(int nr, volatile unsigned long * addr) { __asm__ __volatile__( "btrl %1,%0" - :"=m" (ADDR) + :"+m" (ADDR) :"Ir" (nr)); } #define smp_mb__before_clear_bit() barrier() @@ -100,7 +99,7 @@ static inline void __change_bit(int nr, volatile unsigned long * addr) { __asm__ __volatile__( "btcl %1,%0" - :"=m" (ADDR) + :"+m" (ADDR) :"Ir" (nr)); } @@ -109,7 +108,8 @@ static inline void __change_bit(int nr, volatile unsigned long * addr) * @nr: Bit to change * @addr: Address to start counting from * - * change_bit() is atomic and may not be reordered. + * change_bit() is atomic and may not be reordered. It may be + * reordered on other architectures than x86. * Note that @nr may be almost arbitrarily large; this function is not * restricted to acting on a single-word quantity. */ @@ -117,7 +117,7 @@ static inline void change_bit(int nr, volatile unsigned long * addr) { __asm__ __volatile__( LOCK_PREFIX "btcl %1,%0" - :"=m" (ADDR) + :"+m" (ADDR) :"Ir" (nr)); } @@ -127,6 +127,7 @@ static inline void change_bit(int nr, volatile unsigned long * addr) * @addr: Address to count from * * This operation is atomic and cannot be reordered. + * It may be reordered on other architectures than x86. * It also implies a memory barrier. */ static inline int test_and_set_bit(int nr, volatile unsigned long * addr) @@ -135,7 +136,7 @@ static inline int test_and_set_bit(int nr, volatile unsigned long * addr) __asm__ __volatile__( LOCK_PREFIX "btsl %2,%1\n\tsbbl %0,%0" - :"=r" (oldbit),"=m" (ADDR) + :"=r" (oldbit),"+m" (ADDR) :"Ir" (nr) : "memory"); return oldbit; } @@ -155,7 +156,7 @@ static inline int __test_and_set_bit(int nr, volatile unsigned long * addr) __asm__( "btsl %2,%1\n\tsbbl %0,%0" - :"=r" (oldbit),"=m" (ADDR) + :"=r" (oldbit),"+m" (ADDR) :"Ir" (nr)); return oldbit; } @@ -165,7 +166,8 @@ static inline int __test_and_set_bit(int nr, volatile unsigned long * addr) * @nr: Bit to clear * @addr: Address to count from * - * This operation is atomic and cannot be reordered. + * This operation is atomic and cannot be reordered. + * It can be reorderdered on other architectures other than x86. * It also implies a memory barrier. */ static inline int test_and_clear_bit(int nr, volatile unsigned long * addr) @@ -174,7 +176,7 @@ static inline int test_and_clear_bit(int nr, volatile unsigned long * addr) __asm__ __volatile__( LOCK_PREFIX "btrl %2,%1\n\tsbbl %0,%0" - :"=r" (oldbit),"=m" (ADDR) + :"=r" (oldbit),"+m" (ADDR) :"Ir" (nr) : "memory"); return oldbit; } @@ -194,7 +196,7 @@ static inline int __test_and_clear_bit(int nr, volatile unsigned long *addr) __asm__( "btrl %2,%1\n\tsbbl %0,%0" - :"=r" (oldbit),"=m" (ADDR) + :"=r" (oldbit),"+m" (ADDR) :"Ir" (nr)); return oldbit; } @@ -206,7 +208,7 @@ static inline int __test_and_change_bit(int nr, volatile unsigned long *addr) __asm__ __volatile__( "btcl %2,%1\n\tsbbl %0,%0" - :"=r" (oldbit),"=m" (ADDR) + :"=r" (oldbit),"+m" (ADDR) :"Ir" (nr) : "memory"); return oldbit; } @@ -225,7 +227,7 @@ static inline int test_and_change_bit(int nr, volatile unsigned long* addr) __asm__ __volatile__( LOCK_PREFIX "btcl %2,%1\n\tsbbl %0,%0" - :"=r" (oldbit),"=m" (ADDR) + :"=r" (oldbit),"+m" (ADDR) :"Ir" (nr) : "memory"); return oldbit; } @@ -239,7 +241,7 @@ static inline int test_and_change_bit(int nr, volatile unsigned long* addr) static int test_bit(int nr, const volatile void * addr); #endif -static inline int constant_test_bit(int nr, const volatile unsigned long *addr) +static __always_inline int constant_test_bit(int nr, const volatile unsigned long *addr) { return ((1UL << (nr & 31)) & (addr[nr >> 5])) != 0; } @@ -302,6 +304,20 @@ static inline int find_first_zero_bit(const unsigned long *addr, unsigned size) */ int find_next_zero_bit(const unsigned long *addr, int size, int offset); +/** + * __ffs - find first bit in word. + * @word: The word to search + * + * Undefined if no bit exists, so code should check against 0 first. + */ +static inline unsigned long __ffs(unsigned long word) +{ + __asm__("bsfl %1,%0" + :"=r" (word) + :"rm" (word)); + return word; +} + /** * find_first_bit - find the first set bit in a memory region * @addr: The address to start the search at @@ -310,24 +326,17 @@ int find_next_zero_bit(const unsigned long *addr, int size, int offset); * Returns the bit-number of the first set bit, not the number of the byte * containing a bit. */ -static inline int find_first_bit(const unsigned long *addr, unsigned size) +static inline unsigned find_first_bit(const unsigned long *addr, unsigned size) { - int d0, d1; - int res; - - /* This looks at memory. Mark it volatile to tell gcc not to move it around */ - __asm__ __volatile__( - "xorl %%eax,%%eax\n\t" - "repe; scasl\n\t" - "jz 1f\n\t" - "leal -4(%%edi),%%edi\n\t" - "bsfl (%%edi),%%eax\n" - "1:\tsubl %%ebx,%%edi\n\t" - "shll $3,%%edi\n\t" - "addl %%edi,%%eax" - :"=a" (res), "=&c" (d0), "=&D" (d1) - :"1" ((size + 31) >> 5), "2" (addr), "b" (addr) : "memory"); - return res; + unsigned x = 0; + + while (x < size) { + unsigned long val = *addr++; + if (val) + return __ffs(val) + x; + x += (sizeof(*addr)<<3); + } + return x; } /** @@ -352,46 +361,9 @@ static inline unsigned long ffz(unsigned long word) return word; } -/** - * __ffs - find first bit in word. - * @word: The word to search - * - * Undefined if no bit exists, so code should check against 0 first. - */ -static inline unsigned long __ffs(unsigned long word) -{ - __asm__("bsfl %1,%0" - :"=r" (word) - :"rm" (word)); - return word; -} - -/* - * fls: find last bit set. - */ - -#define fls(x) generic_fls(x) - #ifdef __KERNEL__ -/* - * Every architecture must define this function. It's the fastest - * way of searching a 140-bit bitmap where the first 100 bits are - * unlikely to be set. It's guaranteed that at least one of the 140 - * bits is cleared. - */ -static inline int sched_find_first_bit(const unsigned long *b) -{ - if (unlikely(b[0])) - return __ffs(b[0]); - if (unlikely(b[1])) - return __ffs(b[1]) + 32; - if (unlikely(b[2])) - return __ffs(b[2]) + 64; - if (b[3]) - return __ffs(b[3]) + 96; - return __ffs(b[4]) + 128; -} +#include /** * ffs - find first bit set @@ -413,41 +385,38 @@ static inline int ffs(int x) } /** - * hweightN - returns the hamming weight of a N-bit word - * @x: the word to weigh + * fls - find last bit set + * @x: the word to search * - * The Hamming Weight of a number is the total number of bits set in it. + * This is defined the same way as ffs. */ +static inline int fls(int x) +{ + int r; -#define hweight32(x) generic_hweight32(x) -#define hweight16(x) generic_hweight16(x) -#define hweight8(x) generic_hweight8(x) + __asm__("bsrl %1,%0\n\t" + "jnz 1f\n\t" + "movl $-1,%0\n" + "1:" : "=r" (r) : "rm" (x)); + return r+1; +} + +#include #endif /* __KERNEL__ */ +#include + #ifdef __KERNEL__ -#define ext2_set_bit(nr,addr) \ - __test_and_set_bit((nr),(unsigned long*)addr) +#include + #define ext2_set_bit_atomic(lock,nr,addr) \ test_and_set_bit((nr),(unsigned long*)addr) -#define ext2_clear_bit(nr, addr) \ - __test_and_clear_bit((nr),(unsigned long*)addr) #define ext2_clear_bit_atomic(lock,nr, addr) \ test_and_clear_bit((nr),(unsigned long*)addr) -#define ext2_test_bit(nr, addr) test_bit((nr),(unsigned long*)addr) -#define ext2_find_first_zero_bit(addr, size) \ - find_first_zero_bit((unsigned long*)addr, size) -#define ext2_find_next_zero_bit(addr, size, off) \ - find_next_zero_bit((unsigned long*)addr, size, off) - -/* Bitmap functions for the minix filesystem. */ -#define minix_test_and_set_bit(nr,addr) __test_and_set_bit(nr,(void*)addr) -#define minix_set_bit(nr,addr) __set_bit(nr,(void*)addr) -#define minix_test_and_clear_bit(nr,addr) __test_and_clear_bit(nr,(void*)addr) -#define minix_test_bit(nr,addr) test_bit(nr,(void*)addr) -#define minix_find_first_zero_bit(addr,size) \ - find_first_zero_bit((void*)addr,size) + +#include #endif /* __KERNEL__ */