Diff between d1009b35c460d700bab7214a3ef2c38e2ab069c7 and 39a53c09a0c62cf221ff90a4228293cdfcd1f847

Changed Files

File Additions Deletions Status
src/shared/ringbuf.c +8 -1 modified
unit/test-ringbuf.c +6 -1 modified

Full Patch

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
@@ -48,9 +48,16 @@ struct ringbuf {
 
 #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
@@ -44,9 +44,14 @@ static unsigned int nlpo2(unsigned int x)
 	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)