]> asedeno.scripts.mit.edu Git - linux.git/blobdiff - include/linux/log2.h
Merge tag 'm68k-for-v5.3-tag2' of git://git.kernel.org/pub/scm/linux/kernel/git/geert...
[linux.git] / include / linux / log2.h
index 1aec01365ed4548b87dd1dfec93fc441bcf73576..83a4a3ca3e8a76f4a80b8740bd8f82b2d68fae59 100644 (file)
@@ -220,4 +220,38 @@ int __order_base_2(unsigned long n)
                ilog2((n) - 1) + 1) :           \
        __order_base_2(n)                       \
 )
+
+static inline __attribute__((const))
+int __bits_per(unsigned long n)
+{
+       if (n < 2)
+               return 1;
+       if (is_power_of_2(n))
+               return order_base_2(n) + 1;
+       return order_base_2(n);
+}
+
+/**
+ * bits_per - calculate the number of bits required for the argument
+ * @n: parameter
+ *
+ * This is constant-capable and can be used for compile time
+ * initializations, e.g bitfields.
+ *
+ * The first few values calculated by this routine:
+ * bf(0) = 1
+ * bf(1) = 1
+ * bf(2) = 2
+ * bf(3) = 2
+ * bf(4) = 3
+ * ... and so on.
+ */
+#define bits_per(n)                            \
+(                                              \
+       __builtin_constant_p(n) ? (             \
+               ((n) == 0 || (n) == 1)          \
+                       ? 1 : ilog2(n) + 1      \
+       ) :                                     \
+       __bits_per(n)                           \
+)
 #endif /* _LINUX_LOG2_H */