diff --git a/src/shared/ringbuf.c b/src/shared/ringbuf.c
index 3e5c7d3..f9c4376 100644
--- a/src/shared/ringbuf.c
+++ b/src/shared/ringbuf.c
#define RINGBUF_RESET 0
+/* Find last (most siginificant) set bit */
+static inline unsigned int fls(unsigned int x)
+{
+ return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
+}
+
+/* Round up to nearest power of two */
static inline unsigned int align_power2(unsigned int u)
{
- return 1 << ((sizeof(u) * 8) - __builtin_clz(u - 1));
+ return 1 << fls(u - 1);
}
struct ringbuf *ringbuf_new(size_t size)
diff --git a/unit/test-ringbuf.c b/unit/test-ringbuf.c
index 908a0d3..e28e04b 100644
--- a/unit/test-ringbuf.c
+++ b/unit/test-ringbuf.c
return x + 1;
}
+static unsigned int fls(unsigned int x)
+{
+ return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
+}
+
static unsigned int align_power2(unsigned int u)
{
- return 1 << ((sizeof(u) * 8) - __builtin_clz(u - 1));
+ return 1 << fls(u - 1);
}
static void test_power2(void)