[U-Boot] [PATCH 1/2] bitops: Add support for order_base_2()
Fabio Estevam
festevam at gmail.com
Tue Sep 29 04:45:52 CEST 2015
From: Fabio Estevam <fabio.estevam at freescale.com>
Add support for the order_base_2() macro from the Linux kernel.
This is useful for the SPI NOR unlock function.
Signed-off-by: Fabio Estevam <fabio.estevam at freescale.com>
---
arch/arm/include/asm/bitops.h | 56 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 56 insertions(+)
diff --git a/arch/arm/include/asm/bitops.h b/arch/arm/include/asm/bitops.h
index 9b78043..b9b6d21 100644
--- a/arch/arm/include/asm/bitops.h
+++ b/arch/arm/include/asm/bitops.h
@@ -108,6 +108,62 @@ static inline int __ilog2(unsigned int x)
return generic_fls(x) - 1;
}
+static inline int fls64(__u64 x)
+{
+ if (x == 0)
+ return 0;
+ return generic_fls(x) + 1;
+}
+
+static inline unsigned fls_long(unsigned long l)
+{
+ if (sizeof(l) == 4)
+ return generic_fls(l);
+ return fls64(l);
+}
+
+/*
+ * round up to nearest power of two
+ */
+static inline __attribute__((const))
+unsigned long __roundup_pow_of_two(unsigned long n)
+{
+ return 1UL << fls_long(n - 1);
+}
+
+/**
+ * roundup_pow_of_two - round the given value up to nearest power of two
+ * @n - parameter
+ *
+ * round the given value up to the nearest power of two
+ * - the result is undefined when n == 0
+ * - this can be used to initialise global variables from constant data
+ */
+#define roundup_pow_of_two(n) \
+( \
+ __builtin_constant_p(n) ? ( \
+ (n == 1) ? 1 : \
+ (1UL << (__ilog2((n) - 1) + 1)) \
+ ) : \
+ __roundup_pow_of_two(n) \
+)
+
+/**
+ * order_base_2 - calculate the (rounded up) base 2 order of the argument
+ * @n: parameter
+ *
+ * The first few values calculated by this routine:
+ * ob2(0) = 0
+ * ob2(1) = 0
+ * ob2(2) = 1
+ * ob2(3) = 2
+ * ob2(4) = 2
+ * ob2(5) = 3
+ * ... and so on.
+ */
+
+#define order_base_2(n) __ilog2(roundup_pow_of_two(n))
+
/*
* ffz = Find First Zero in word. Undefined if no zero exists,
* so code should check against ~0UL first..
--
1.9.1
More information about the U-Boot
mailing list