]> git.karo-electronics.de Git - mv-sheeva.git/blobdiff - include/asm-frv/bitops.h
[PATCH] FRV: Implement fls64()
[mv-sheeva.git] / include / asm-frv / bitops.h
index 6344d06390b9fea57456b694cdff7a52dc99b910..591eecc1f8cd2e43a672674e36ca38661e19bbff 100644 (file)
@@ -14,7 +14,6 @@
 #ifndef _ASM_BITOPS_H
 #define _ASM_BITOPS_H
 
-#include <linux/config.h>
 #include <linux/compiler.h>
 #include <asm/byteorder.h>
 #include <asm/system.h>
@@ -162,19 +161,72 @@ static inline int __test_bit(int nr, const volatile void * addr)
 #include <asm-generic/bitops/__ffs.h>
 #include <asm-generic/bitops/find.h>
 
-/*
- * fls: find last bit set.
+/**
+ * fls - find last bit set
+ * @x: the word to search
+ *
+ * This is defined the same way as ffs:
+ * - return 32..1 to indicate bit 31..0 most significant bit set
+ * - return 0 to indicate no bits set
  */
 #define fls(x)                                         \
 ({                                                     \
        int bit;                                        \
                                                        \
-       asm("scan %1,gr0,%0" : "=r"(bit) : "r"(x));     \
+       asm("   subcc   %1,gr0,gr0,icc0         \n"     \
+           "   ckne    icc0,cc4                \n"     \
+           "   cscan.p %1,gr0,%0       ,cc4,#1 \n"     \
+           "   csub    %0,%0,%0        ,cc4,#0 \n"     \
+           "   csub    %2,%0,%0        ,cc4,#1 \n"     \
+           : "=&r"(bit)                                \
+           : "r"(x), "r"(32)                           \
+           : "icc0", "cc4"                             \
+           );                                          \
                                                        \
-       bit ? 33 - bit : bit;                           \
+       bit;                                            \
 })
 
-#include <asm-generic/bitops/fls64.h>
+/**
+ * fls64 - find last bit set in a 64-bit value
+ * @n: the value to search
+ *
+ * This is defined the same way as ffs:
+ * - return 64..1 to indicate bit 63..0 most significant bit set
+ * - return 0 to indicate no bits set
+ */
+static inline __attribute__((const))
+int fls64(u64 n)
+{
+       union {
+               u64 ll;
+               struct { u32 h, l; };
+       } _;
+       int bit, x, y;
+
+       _.ll = n;
+
+       asm("   subcc.p         %3,gr0,gr0,icc0         \n"
+           "   subcc           %4,gr0,gr0,icc1         \n"
+           "   ckne            icc0,cc4                \n"
+           "   ckne            icc1,cc5                \n"
+           "   norcr           cc4,cc5,cc6             \n"
+           "   csub.p          %0,%0,%0        ,cc6,1  \n"
+           "   orcr            cc5,cc4,cc4             \n"
+           "   andcr           cc4,cc5,cc4             \n"
+           "   cscan.p         %3,gr0,%0       ,cc4,0  \n"
+           "   setlos          #64,%1                  \n"
+           "   cscan.p         %4,gr0,%0       ,cc4,1  \n"
+           "   setlos          #32,%2                  \n"
+           "   csub.p          %1,%0,%0        ,cc4,0  \n"
+           "   csub            %2,%0,%0        ,cc4,1  \n"
+           : "=&r"(bit), "=r"(x), "=r"(y)
+           : "0r"(_.h), "r"(_.l)
+           : "icc0", "icc1", "cc4", "cc5", "cc6"
+           );
+       return bit;
+
+}
+
 #include <asm-generic/bitops/sched.h>
 #include <asm-generic/bitops/hweight.h>