linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / include / asm-i386 / bitops.h
index 8fd938f..88e6ca2 100644 (file)
  *
  * 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 +43,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 +60,7 @@ static inline void __set_bit(int nr, volatile unsigned long * addr)
 {
        __asm__(
                "btsl %1,%0"
-               :"=m" (ADDR)
+               :"+m" (ADDR)
                :"Ir" (nr));
 }
 
@@ -73,7 +78,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 +86,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 +105,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 +114,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 +123,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 +133,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 +142,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 +162,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 +172,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 +182,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 +202,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 +214,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 +233,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 +247,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 +310,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 +332,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,25 +367,7 @@ 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)
+#define fls64(x)   generic_fls64(x)
 
 #ifdef __KERNEL__
 
@@ -412,6 +409,23 @@ static inline int ffs(int x)
        return r+1;
 }
 
+/**
+ * fls - find last bit set
+ * @x: the word to search
+ *
+ * This is defined the same way as ffs.
+ */
+static inline int fls(int x)
+{
+       int r;
+
+       __asm__("bsrl %1,%0\n\t"
+               "jnz 1f\n\t"
+               "movl $-1,%0\n"
+               "1:" : "=r" (r) : "rm" (x));
+       return r+1;
+}
+
 /**
  * hweightN - returns the hamming weight of a N-bit word
  * @x: the word to weigh