aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--tools/include/linux/log2.h25
1 files changed, 25 insertions, 0 deletions
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)
48} 48}
49 49
50/* 50/*
51 * round up to nearest power of two
52 */
53static inline __attribute__((const))
54unsigned long __roundup_pow_of_two(unsigned long n)
55{
56 return 1UL << fls_long(n - 1);
57}
58
59/*
51 * round down to nearest power of two 60 * round down to nearest power of two
52 */ 61 */
53static inline __attribute__((const)) 62static inline __attribute__((const))
@@ -141,6 +150,22 @@ unsigned long __rounddown_pow_of_two(unsigned long n)
141 __ilog2_u64(n) \ 150 __ilog2_u64(n) \
142 ) 151 )
143 152
153/**
154 * roundup_pow_of_two - round the given value up to nearest power of two
155 * @n - parameter
156 *
157 * round the given value up to the nearest power of two
158 * - the result is undefined when n == 0
159 * - this can be used to initialise global variables from constant data
160 */
161#define roundup_pow_of_two(n) \
162( \
163 __builtin_constant_p(n) ? ( \
164 (n == 1) ? 1 : \
165 (1UL << (ilog2((n) - 1) + 1)) \
166 ) : \
167 __roundup_pow_of_two(n) \
168 )
144 169
145/** 170/**
146 * rounddown_pow_of_two - round the given value down to nearest power of two 171 * rounddown_pow_of_two - round the given value down to nearest power of two