[PATCH 16/18] tools: Adopt roundup_pow_of_two

From: Arnaldo Carvalho de Melo
Date: Tue Dec 16 2014 - 11:59:05 EST


From: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>

To replace equivalent code used in the mmap_pages command line
parameter handling in tools/perf.

Cc: Adrian Hunter <adrian.hunter@xxxxxxxxx>
Cc: Borislav Petkov <bp@xxxxxxx>
Cc: David Ahern <dsahern@xxxxxxxxx>
Cc: Don Zickus <dzickus@xxxxxxxxxx>
Cc: Frederic Weisbecker <fweisbec@xxxxxxxxx>
Cc: Jiri Olsa <jolsa@xxxxxxxxxx>
Cc: Mike Galbraith <efault@xxxxxx>
Cc: Namhyung Kim <namhyung@xxxxxxxxxx>
Cc: Paul Mackerras <paulus@xxxxxxxxx>
Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
Cc: Stephane Eranian <eranian@xxxxxxxxxx>
Link: http://lkml.kernel.org/n/tip-i44zs02xt4zexfxywpklo7km@xxxxxxxxxxxxxx
Signed-off-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
---
tools/include/linux/log2.h | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)

diff --git a/tools/include/linux/log2.h b/tools/include/linux/log2.h
index 990b138362d6..41446668ccce 100644
--- a/tools/include/linux/log2.h
+++ b/tools/include/linux/log2.h
@@ -48,6 +48,15 @@ bool is_power_of_2(unsigned long n)
}

/*
+ * 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);
+}
+
+/*
* round down to nearest power of two
*/
static inline __attribute__((const))
@@ -141,6 +150,22 @@ unsigned long __rounddown_pow_of_two(unsigned long n)
__ilog2_u64(n) \
)

+/**
+ * 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) \
+ )

/**
* rounddown_pow_of_two - round the given value down to nearest power of two
--
1.9.3

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/