From: Fabio Estevam <festevam@gmail.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v2 02/13] include: Add generic bitops headers
Date: Sat, 24 Oct 2015 13:26:44 -0200 [thread overview]
Message-ID: <1445700414-28600-2-git-send-email-festevam@gmail.com> (raw)
In-Reply-To: <1445700414-28600-1-git-send-email-festevam@gmail.com>
From: Fabio Estevam <fabio.estevam@freescale.com>
Use the generic bitops header files from the kernel.
Imported from kernel 4.2.3.
Signed-off-by: Fabio Estevam <fabio.estevam@freescale.com>
Reviewed-by: Tom Rini <trini@konsulko.com>
---
Changes since v1:
- None
include/asm-generic/bitops/__ffs.h | 43 ++++++++++++++++++++++++++++++++++++++
include/asm-generic/bitops/__fls.h | 43 ++++++++++++++++++++++++++++++++++++++
include/asm-generic/bitops/fls.h | 41 ++++++++++++++++++++++++++++++++++++
include/asm-generic/bitops/fls64.h | 36 +++++++++++++++++++++++++++++++
include/linux/bitops.h | 26 +++++++++++++++++++++++
5 files changed, 189 insertions(+)
create mode 100644 include/asm-generic/bitops/__ffs.h
create mode 100644 include/asm-generic/bitops/__fls.h
create mode 100644 include/asm-generic/bitops/fls.h
create mode 100644 include/asm-generic/bitops/fls64.h
diff --git a/include/asm-generic/bitops/__ffs.h b/include/asm-generic/bitops/__ffs.h
new file mode 100644
index 0000000..9a3274a
--- /dev/null
+++ b/include/asm-generic/bitops/__ffs.h
@@ -0,0 +1,43 @@
+#ifndef _ASM_GENERIC_BITOPS___FFS_H_
+#define _ASM_GENERIC_BITOPS___FFS_H_
+
+#include <asm/types.h>
+
+/**
+ * __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)
+{
+ int num = 0;
+
+#if BITS_PER_LONG == 64
+ if ((word & 0xffffffff) == 0) {
+ num += 32;
+ word >>= 32;
+ }
+#endif
+ if ((word & 0xffff) == 0) {
+ num += 16;
+ word >>= 16;
+ }
+ if ((word & 0xff) == 0) {
+ num += 8;
+ word >>= 8;
+ }
+ if ((word & 0xf) == 0) {
+ num += 4;
+ word >>= 4;
+ }
+ if ((word & 0x3) == 0) {
+ num += 2;
+ word >>= 2;
+ }
+ if ((word & 0x1) == 0)
+ num += 1;
+ return num;
+}
+
+#endif /* _ASM_GENERIC_BITOPS___FFS_H_ */
diff --git a/include/asm-generic/bitops/__fls.h b/include/asm-generic/bitops/__fls.h
new file mode 100644
index 0000000..be24465
--- /dev/null
+++ b/include/asm-generic/bitops/__fls.h
@@ -0,0 +1,43 @@
+#ifndef _ASM_GENERIC_BITOPS___FLS_H_
+#define _ASM_GENERIC_BITOPS___FLS_H_
+
+#include <asm/types.h>
+
+/**
+ * __fls - find last (most-significant) set bit in a long word
+ * @word: the word to search
+ *
+ * Undefined if no set bit exists, so code should check against 0 first.
+ */
+static inline unsigned long __fls(unsigned long word)
+{
+ int num = BITS_PER_LONG - 1;
+
+#if BITS_PER_LONG == 64
+ if (!(word & (~0ul << 32))) {
+ num -= 32;
+ word <<= 32;
+ }
+#endif
+ if (!(word & (~0ul << (BITS_PER_LONG-16)))) {
+ num -= 16;
+ word <<= 16;
+ }
+ if (!(word & (~0ul << (BITS_PER_LONG-8)))) {
+ num -= 8;
+ word <<= 8;
+ }
+ if (!(word & (~0ul << (BITS_PER_LONG-4)))) {
+ num -= 4;
+ word <<= 4;
+ }
+ if (!(word & (~0ul << (BITS_PER_LONG-2)))) {
+ num -= 2;
+ word <<= 2;
+ }
+ if (!(word & (~0ul << (BITS_PER_LONG-1))))
+ num -= 1;
+ return num;
+}
+
+#endif /* _ASM_GENERIC_BITOPS___FLS_H_ */
diff --git a/include/asm-generic/bitops/fls.h b/include/asm-generic/bitops/fls.h
new file mode 100644
index 0000000..850859b
--- /dev/null
+++ b/include/asm-generic/bitops/fls.h
@@ -0,0 +1,41 @@
+#ifndef _ASM_GENERIC_BITOPS_FLS_H_
+#define _ASM_GENERIC_BITOPS_FLS_H_
+
+/**
+ * fls - find last (most-significant) bit set
+ * @x: the word to search
+ *
+ * This is defined the same way as ffs.
+ * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
+ */
+
+static inline int fls(int x)
+{
+ int r = 32;
+
+ if (!x)
+ return 0;
+ if (!(x & 0xffff0000u)) {
+ x <<= 16;
+ r -= 16;
+ }
+ if (!(x & 0xff000000u)) {
+ x <<= 8;
+ r -= 8;
+ }
+ if (!(x & 0xf0000000u)) {
+ x <<= 4;
+ r -= 4;
+ }
+ if (!(x & 0xc0000000u)) {
+ x <<= 2;
+ r -= 2;
+ }
+ if (!(x & 0x80000000u)) {
+ x <<= 1;
+ r -= 1;
+ }
+ return r;
+}
+
+#endif /* _ASM_GENERIC_BITOPS_FLS_H_ */
diff --git a/include/asm-generic/bitops/fls64.h b/include/asm-generic/bitops/fls64.h
new file mode 100644
index 0000000..86d403f
--- /dev/null
+++ b/include/asm-generic/bitops/fls64.h
@@ -0,0 +1,36 @@
+#ifndef _ASM_GENERIC_BITOPS_FLS64_H_
+#define _ASM_GENERIC_BITOPS_FLS64_H_
+
+#include <asm/types.h>
+
+/**
+ * fls64 - find last set bit in a 64-bit word
+ * @x: the word to search
+ *
+ * This is defined in a similar way as the libc and compiler builtin
+ * ffsll, but returns the position of the most significant set bit.
+ *
+ * fls64(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 64.
+ */
+#if BITS_PER_LONG == 32
+static inline int fls64(__u64 x)
+{
+ __u32 h = x >> 32;
+ if (h)
+ return fls(h) + 32;
+ return fls(x);
+}
+#elif BITS_PER_LONG == 64
+static inline int fls64(__u64 x)
+{
+ if (x == 0)
+ return 0;
+ return __fls(x) + 1;
+}
+#else
+#error BITS_PER_LONG not 32 or 64
+#endif
+
+#endif /* _ASM_GENERIC_BITOPS_FLS64_H_ */
diff --git a/include/linux/bitops.h b/include/linux/bitops.h
index 7b4011f..57143ac 100644
--- a/include/linux/bitops.h
+++ b/include/linux/bitops.h
@@ -139,6 +139,32 @@ static inline unsigned int generic_hweight8(unsigned int w)
# define fls generic_fls
#endif
+static inline unsigned fls_long(unsigned long l)
+{
+ if (sizeof(l) == 4)
+ return fls(l);
+ return fls64(l);
+}
+
+/**
+ * __ffs64 - find first set bit in a 64 bit word
+ * @word: The 64 bit word
+ *
+ * On 64 bit arches this is a synomyn for __ffs
+ * The result is not defined if no bits are set, so check that @word
+ * is non-zero before calling this.
+ */
+static inline unsigned long __ffs64(u64 word)
+{
+#if BITS_PER_LONG == 32
+ if (((u32)word) == 0UL)
+ return __ffs((u32)(word >> 32)) + 32;
+#elif BITS_PER_LONG != 64
+#error BITS_PER_LONG not 32 or 64
+#endif
+ return __ffs((unsigned long)word);
+}
+
/**
* __set_bit - Set a bit in memory
* @nr: the bit to set
--
1.9.1
next prev parent reply other threads:[~2015-10-24 15:26 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-10-24 15:26 [U-Boot] [PATCH v2 01/13] include: Add log2 header from the kernel Fabio Estevam
2015-10-24 15:26 ` Fabio Estevam [this message]
2015-10-25 6:50 ` [U-Boot] [PATCH v2 02/13] include: Add generic bitops headers Jagan Teki
2015-10-25 16:28 ` Fabio Estevam
2015-10-25 16:54 ` Fabio Estevam
2015-10-24 15:26 ` [U-Boot] [PATCH v2 03/12] compat: Remove is_power_of_2() definition Fabio Estevam
2015-10-25 6:53 ` Jagan Teki
2015-10-25 13:11 ` Fabio Estevam
2015-10-24 15:26 ` [U-Boot] [PATCH v2 04/12] powerpc: Remove __ilog2_u64 and ffs4 from bitops Fabio Estevam
2015-10-24 21:16 ` Tom Rini
2015-10-24 15:26 ` [U-Boot] [PATCH v2 05/12] ARM: bitops: Use the generic bitops headers Fabio Estevam
2015-10-24 21:16 ` Tom Rini
2015-10-24 15:26 ` [U-Boot] [PATCH v2 06/12] x86: " Fabio Estevam
2015-10-24 21:16 ` Tom Rini
2015-10-24 15:26 ` [U-Boot] [PATCH v2 07/12] m68k: " Fabio Estevam
2015-10-24 21:16 ` Tom Rini
2015-10-24 15:26 ` [U-Boot] [PATCH v2 08/12] blackfin: " Fabio Estevam
2015-10-24 21:17 ` Tom Rini
2015-10-24 15:26 ` [U-Boot] [PATCH v2 09/12] sh: " Fabio Estevam
2015-10-24 21:17 ` Tom Rini
2015-10-24 15:26 ` [U-Boot] [PATCH v2 10/12] sandbox: " Fabio Estevam
2015-10-24 21:17 ` Tom Rini
2015-10-24 15:26 ` [U-Boot] [PATCH v2 11/12] spi: sf_ops: Add SPI protection mechanism from the kernel Fabio Estevam
2015-10-24 23:24 ` Tom Rini
2015-10-24 15:26 ` [U-Boot] [PATCH v2 12/12] spi: Add SPI NOR protection mechanism Fabio Estevam
2015-10-24 23:24 ` Tom Rini
2015-10-25 6:44 ` [U-Boot] [PATCH v2 01/13] include: Add log2 header from the kernel Jagan Teki
2015-10-25 12:51 ` Fabio Estevam
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1445700414-28600-2-git-send-email-festevam@gmail.com \
--to=festevam@gmail.com \
--cc=u-boot@lists.denx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox