qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [RFC PATCH] utils: Add up_pow_of_two()


From: Alexey Kardashevskiy
Subject: [Qemu-devel] [RFC PATCH] utils: Add up_pow_of_two()
Date: Mon, 23 Feb 2015 18:49:56 +1100

This adds a helper to get closest bigger power-of-two value.

Signed-off-by: Alexey Kardashevskiy <address@hidden>
---

It is a log2 of round up to power of two. Is there anything like this
in QEMU already? Any better name than up_pow_of_two()?

I'll use this little helper later to calculate a DMA window shift as
POWERPC PAPR architecture operates with shifts rather than bytes or TCE entries.

Thanks!


---
 include/qemu/host-utils.h | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/include/qemu/host-utils.h b/include/qemu/host-utils.h
index d4f21c9..b1ad164 100644
--- a/include/qemu/host-utils.h
+++ b/include/qemu/host-utils.h
@@ -361,6 +361,17 @@ static inline int ctpop64(uint64_t val)
 #endif
 }
 
+static inline int up_pow_of_two(uint64_t x)
+{
+    int m = 63 - clz64(x);
+
+    if ((1ULL << m) == x) {
+        return m;
+    }
+
+    return m + 1;
+}
+
 /* Host type specific sizes of these routines.  */
 
 #if ULONG_MAX == UINT32_MAX
-- 
2.0.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]