public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] blackfin: optimize ffz, __ffs, ffs, __fls, and fls functions
@ 2016-04-15  7:52 zengzhaoxiu
  2016-04-15  8:23 ` [PATCH V2] " zengzhaoxiu
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: zengzhaoxiu @ 2016-04-15  7:52 UTC (permalink / raw)
  To: linux-kernel; +Cc: Zhaoxiu Zeng, Steven Miao, adi-buildroot-devel

From: Zhaoxiu Zeng <zhaoxiu.zeng@gmail.com>

blackfin has popcount instruction (ONES), we can do the efficient
computing (ffz, __ffs, ffs, __fls, and fls) use this instruction.

Adapted from "https://en.wikipedia.org/wiki/Find_first_set" and
arch/ia64/include/asm/bitops.h.

Signed-off-by: Zhaoxiu Zeng <zhaoxiu.zeng@gmail.com>
---
 arch/blackfin/include/asm/bitops.h | 73 +++++++++++++++++++++++++++++++++++---
 1 file changed, 68 insertions(+), 5 deletions(-)

diff --git a/arch/blackfin/include/asm/bitops.h b/arch/blackfin/include/asm/bitops.h
index b298b65..602e7c0 100644
--- a/arch/blackfin/include/asm/bitops.h
+++ b/arch/blackfin/include/asm/bitops.h
@@ -9,10 +9,6 @@
 
 #include <linux/compiler.h>
 
-#include <asm-generic/bitops/__ffs.h>
-#include <asm-generic/bitops/ffz.h>
-#include <asm-generic/bitops/fls.h>
-#include <asm-generic/bitops/__fls.h>
 #include <asm-generic/bitops/fls64.h>
 #include <asm-generic/bitops/find.h>
 
@@ -21,7 +17,6 @@
 #endif
 
 #include <asm-generic/bitops/sched.h>
-#include <asm-generic/bitops/ffs.h>
 #include <asm-generic/bitops/const_hweight.h>
 #include <asm-generic/bitops/lock.h>
 
@@ -137,4 +132,72 @@ static inline unsigned int __arch_hweight8(unsigned int w)
 	return __arch_hweight32(w & 0xff);
 }
 
+/**
+ * ffz - find the first zero bit in a long word
+ * @x: The long word to find the bit in
+ *
+ * Returns the bit-number (0..31) of the first (least significant) zero bit.
+ * Undefined if no zero exists, so code should check against ~0UL first...
+ */
+static inline unsigned long ffz(unsigned long x)
+{
+	return __arch_hweight32(x & (~x - 1));
+}
+
+/**
+ * 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).
+ */
+static inline int ffs(int x)
+{
+	if (!x)
+		return 0;
+	return __arch_hweight32(x ^ ((unsigned int)x - 1));
+}
+
+/**
+ * __ffs - find first bit in word.
+ * @x: The word to search
+ *
+ * Undefined if no bit exists, so code should check against 0 first.
+ */
+static inline unsigned long __ffs(unsigned long x)
+{
+	return __arch_hweight32(~x & (x - 1));
+}
+
+/*
+ * Find the last (most significant) bit set.  Returns 0 for x==0 and
+ * bits are numbered from 1..32 (e.g., fls(9) == 4).
+ */
+static inline int fls(int x)
+{
+	if (!x)
+		return 0;
+	x |= x >> 1;
+	x |= x >> 2;
+	x |= x >> 4;
+	x |= x >> 8;
+	x |= x >> 16;
+	return __arch_hweight32(x);
+}
+
+/*
+ * Find the last (most significant) bit set.  Undefined for x==0.
+ * Bits are numbered from 0..31 (e.g., __fls(9) == 3).
+ */
+static inline unsigned long __fls(unsigned long x)
+{
+	x |= x >> 1;
+	x |= x >> 2;
+	x |= x >> 4;
+	x |= x >> 8;
+	x |= x >> 16;
+	return __arch_hweight32(x) - 1;
+}
+
 #endif				/* _BLACKFIN_BITOPS_H */
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2016-04-15 16:19 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-04-15  7:52 [PATCH] blackfin: optimize ffz, __ffs, ffs, __fls, and fls functions zengzhaoxiu
2016-04-15  8:23 ` [PATCH V2] " zengzhaoxiu
2016-04-15  9:16 ` [PATCH] " kbuild test robot
2016-04-15 15:20 ` [PATCH V3] " zengzhaoxiu
2016-04-15 16:08   ` Joe Perches
2016-04-15 16:18     ` Zhaoxiu Zeng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox