linux-cvs-patches
[Top] [All Lists]

CVS Update@linux-mips.org: linux

To: linux-cvs-patches@linux-mips.org
Subject: CVS Update@linux-mips.org: linux
From: ralf@linux-mips.org
Date: Tue, 12 Jul 2005 13:50:37 +0100
Reply-to: linux-mips@linux-mips.org
Sender: linux-cvs-patches-bounce@linux-mips.org
CVSROOT:        /home/cvs
Module name:    linux
Changes by:     ralf@ftp.linux-mips.org 05/07/12 13:50:30

Modified files:
        include/asm-mips: bitops.h 

Log message:
        Use clz / dclz on MIPS32 / MIPS64 processors.

diff -urN linux/include/asm-mips/bitops.h linux/include/asm-mips/bitops.h
--- linux/include/asm-mips/bitops.h     2005/07/11 10:03:32     1.60
+++ linux/include/asm-mips/bitops.h     2005/07/12 12:50:30     1.61
@@ -546,17 +546,60 @@
        return 1UL & (addr[nr >> SZLONG_LOG] >> (nr & SZLONG_MASK));
 }
 
+#ifdef CONFIG_CPU_MIPS32_R1
 /*
- * ffz - find first zero in word.
+ * Return the bit position (0..31) of the most significant 1 bit in a word
+ * Returns -1 if no 1 bit exists
+ */
+static __inline__ int __ilog2(unsigned long x)
+{
+       int lz;
+
+       __asm__ (
+       "       .set    push                                            \n"
+       "       .set    mips32                                          \n"
+       "       clz     %0, %1                                          \n"
+       "       .set    pop                                             \n"
+       : "=r" (lz)
+       : "r" (x));
+
+       return 31 - lz;
+}
+#elif defined(CONFIG_CPU_MIPS64_R1)
+/*
+ * Return the bit position (0..63) of the most significant 1 bit in a word
+ * Returns -1 if no 1 bit exists
+ */
+static __inline__ int __ilog2(unsigned long x)
+{
+       int lz;
+
+       __asm__ (
+       "       .set    push                                            \n"
+       "       .set    mips64                                          \n"
+       "       dclz    %0, %1                                          \n"
+       "       .set    pop                                             \n"
+       : "=r" (lz)
+       : "r" (x));
+
+       return 63 - lz;
+}
+#endif
+
+/*
+ * __ffs - find first bit in word.
  * @word: The word to search
  *
- * Undefined if no zero exists, so code should check against ~0UL first.
+ * Returns 0..SZLONG-1
+ * Undefined if no bit exists, so code should check against 0 first.
  */
-static inline unsigned long ffz(unsigned long word)
+static inline unsigned long __ffs(unsigned long word)
 {
+#if defined(CONFIG_CPU_MIPS32_R1) || defined(CONFIG_CPU_MIPS64_R1)
+       return __ilog2(word & -word);
+#else
        int b = 0, s;
 
-       word = ~word;
 #ifdef CONFIG_MIPS32
        s = 16; if (word << 16 != 0) s = 0; b += s; word >>= s;
        s =  8; if (word << 24 != 0) s = 0; b += s; word >>= s;
@@ -572,26 +615,74 @@
        s =  2; if (word << 62 != 0) s = 0; b += s; word >>= s;
        s =  1; if (word << 63 != 0) s = 0; b += s;
 #endif
-
        return b;
+#endif
 }
 
 /*
- * __ffs - find first bit in word.
+ * ffs - find first bit set.
  * @word: The word to search
  *
- * Undefined if no bit exists, so code should check against 0 first.
+ * Returns 1..SZLONG
+ * Returns 0 if no bit exists
  */
-static inline unsigned long __ffs(unsigned long word)
+
+static inline unsigned long ffs(unsigned long word)
+{
+       if (!word) return 0;
+       return __ffs(word) + 1;
+}
+
+/*
+ * flz - find last zero in word.
+ * @word: The word to search
+ *
+ * Returns 0..SZLONG-1
+ * Undefined if no zero exists, so code should check against ~0UL first.
+ */
+static inline unsigned long flz(unsigned long word)
 {
-       return ffz(~word);
+#if defined(CONFIG_CPU_MIPS32_R1) || defined(CONFIG_CPU_MIPS64_R1)
+       return __ilog2(~word);
+#else
+#if defined(CONFIG_MIPS32)
+       int r = 31, s;
+       word = ~word;
+       s = 16; if ((word & 0xffff0000)) s = 0; r -= s; word <<= s;
+       s = 8;  if ((word & 0xff000000)) s = 0; r -= s; word <<= s;
+       s = 4;  if ((word & 0xf0000000)) s = 0; r -= s; word <<= s;
+       s = 2;  if ((word & 0xc0000000)) s = 0; r -= s; word <<= s;
+       s = 1;  if ((word & 0x80000000)) s = 0; r -= s;
+#endif
+#if defined(CONFIG_MIPS64)
+       int r = 63;
+       word = ~word;
+       s = 32; if ((word & 0xffffffff00000000)) s = 0; r -= s; word <<= s;
+       s = 16; if ((word & 0xffff000000000000)) s = 0; r -= s; word <<= s;
+       s = 8;  if ((word & 0xff00000000000000)) s = 0; r -= s; word <<= s;
+       s = 4;  if ((word & 0xf000000000000000)) s = 0; r -= s; word <<= s;
+       s = 2;  if ((word & 0xc000000000000000)) s = 0; r -= s; word <<= s;
+       s = 1;  if ((word & 0x8000000000000000)) s = 0; r -= s;
+#endif
+       return r;
+#endif
 }
 
 /*
- * fls: find last bit set.
+ * fls - find last bit set.
+ * @word: The word to search
+ *
+ * Returns 1..SZLONG
+ * Returns 0 if no bit exists
  */
+static inline unsigned long fls(unsigned long word)
+{
+       if (word == 0)
+               return 0;
+
+       return flz(~word) + 1;
+}
 
-#define fls(x) generic_fls(x)
 
 /*
  * find_next_zero_bit - find the first zero bit in a memory region
@@ -728,17 +819,6 @@
 }
 
 /*
- * ffs - find first bit set
- * @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 above ffz (man ffs).
- */
-
-#define ffs(x) generic_ffs(x)
-
-/*
  * hweightN - returns the hamming weight of a N-bit word
  * @x: the word to weigh
  *

<Prev in Thread] Current Thread [Next in Thread>