[PATCH 01/12] useful: Add utility to find the next power of two
Denis Kenzior <[email protected]> Mon, 22 Jul 2024 14:04:18 -0500
| Newsgroups | dev.linux.lists.ell |
|---|---|
| Message-ID | <[email protected]> |
---
ell/useful.h | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/ell/useful.h b/ell/useful.h
index 7bb924bcab8c..504ea089cb21 100644
--- a/ell/useful.h
+++ b/ell/useful.h
@@ -52,6 +52,12 @@ static inline unsigned char bit_field(const unsigned char oct,
return (oct >> start) & mask;
}
+/* Must be called with n >= 2 and n <= ULONG_MAX / 2 + 1 */
+static inline unsigned long roundup_pow_of_two(unsigned long n)
+{
+ return 1UL << (sizeof(unsigned long) * 8 - __builtin_clzl(n - 1));
+}
+
#define DIV_ROUND_CLOSEST(x, divisor) \
({ \
typeof(divisor) _d = (divisor); \
--
2.45.2