x86: finalize bitops unification
[sfrench/cifs-2.6.git] / include / asm-x86 / bitops.h
index 1ae7b270a1efe8d1cc9abe8c4260d54d714ecc0f..daf1a72bdc4b3d492b1e377f40011cc45892e3ce 100644 (file)
@@ -67,7 +67,6 @@ static inline void __set_bit(int nr, volatile void *addr)
                     : "Ir" (nr) : "memory");
 }
 
-
 /**
  * clear_bit - Clears a bit in memory
  * @nr: Bit to clear
@@ -304,12 +303,138 @@ static int test_bit(int nr, const volatile unsigned long *addr);
 
 #undef BASE_ADDR
 #undef BIT_ADDR
-#undef ADDR
+/**
+ * __ffs - find first set 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__("bsf %1,%0"
+               :"=r" (word)
+               :"rm" (word));
+       return word;
+}
+
+/**
+ * ffz - find first zero bit in word
+ * @word: The word to search
+ *
+ * Undefined if no zero exists, so code should check against ~0UL first.
+ */
+static inline unsigned long ffz(unsigned long word)
+{
+       __asm__("bsf %1,%0"
+               :"=r" (word)
+               :"r" (~word));
+       return word;
+}
 
-#ifdef CONFIG_X86_32
-# include "bitops_32.h"
+/*
+ * __fls: find last set bit in word
+ * @word: The word to search
+ *
+ * Undefined if no zero exists, so code should check against ~0UL first.
+ */
+static inline unsigned long __fls(unsigned long word)
+{
+       __asm__("bsr %1,%0"
+               :"=r" (word)
+               :"rm" (word));
+       return word;
+}
+
+#ifdef __KERNEL__
+/**
+ * ffs - find first set bit in word
+ * @x: the word to search
+ *
+ * This is defined the same way as the libc and compiler builtin ffs
+ * routines, therefore differs in spirit from the other bitops.
+ *
+ * ffs(value) returns 0 if value is 0 or the position of the first
+ * set bit if value is nonzero. The first (least significant) bit
+ * is at position 1.
+ */
+static inline int ffs(int x)
+{
+       int r;
+#ifdef CONFIG_X86_CMOV
+       __asm__("bsfl %1,%0\n\t"
+               "cmovzl %2,%0"
+               : "=r" (r) : "rm" (x), "r" (-1));
 #else
-# include "bitops_64.h"
+       __asm__("bsfl %1,%0\n\t"
+               "jnz 1f\n\t"
+               "movl $-1,%0\n"
+               "1:" : "=r" (r) : "rm" (x));
 #endif
+       return r + 1;
+}
+
+/**
+ * fls - find last set bit in word
+ * @x: the word to search
+ *
+ * This is defined in a similar way as the libc and compiler builtin
+ * ffs, but returns the position of the most significant set bit.
+ *
+ * fls(value) returns 0 if value is 0 or the position of the last
+ * set bit if value is nonzero. The last (most significant) bit is
+ * at position 32.
+ */
+static inline int fls(int x)
+{
+       int r;
+#ifdef CONFIG_X86_CMOV
+       __asm__("bsrl %1,%0\n\t"
+               "cmovzl %2,%0"
+               : "=&r" (r) : "rm" (x), "rm" (-1));
+#else
+       __asm__("bsrl %1,%0\n\t"
+               "jnz 1f\n\t"
+               "movl $-1,%0\n"
+               "1:" : "=r" (r) : "rm" (x));
+#endif
+       return r + 1;
+}
+#endif /* __KERNEL__ */
+
+#undef ADDR
+
+static inline void set_bit_string(unsigned long *bitmap,
+               unsigned long i, int len)
+{
+       unsigned long end = i + len;
+       while (i < end) {
+               __set_bit(i, bitmap);
+               i++;
+       }
+}
+
+#ifdef __KERNEL__
+
+#include <asm-generic/bitops/sched.h>
+
+#define ARCH_HAS_FAST_MULTIPLIER 1
+
+#include <asm-generic/bitops/hweight.h>
+
+#endif /* __KERNEL__ */
+
+#include <asm-generic/bitops/fls64.h>
+
+#ifdef __KERNEL__
+
+#include <asm-generic/bitops/ext2-non-atomic.h>
+
+#define ext2_set_bit_atomic(lock, nr, addr)                    \
+       test_and_set_bit((nr), (unsigned long *)(addr))
+#define ext2_clear_bit_atomic(lock, nr, addr)                  \
+       test_and_clear_bit((nr), (unsigned long *)(addr))
+
+#include <asm-generic/bitops/minix.h>
 
+#endif /* __KERNEL__ */
 #endif /* _ASM_X86_BITOPS_H */