Skip to contentSkip to conversation

utils: Add up_pow_of_two()

RFC PATCH In review Alexey Kardashevskiy 2 participants 3 messages Feb 23, 2015

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 utils: Add up_pow_of_two()

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

---

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(+)
include/qemu/host-utils.h +11 -0
index d4f21c9..b1ad164 100644
@@ -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
Signature · 1 line
2.0.0
#1 lore raw
Quoted 7 lines · Alexey Kardashevskiy

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

---

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()?

Maybe separate out the "round up to power of 2" and "log2" parts into separate functions? If you do that then you could call the first one of those pow2ceil(), to match our existing pow2floor().

-- PMM

#2 lore raw
Quoted 6 lines · Peter Maydell
Quoted 5 lines · Alexey Kardashevskiy

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

---

Quoted 7 lines
Quoted 2 lines

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()?

Maybe separate out the "round up to power of 2" and "log2" parts into separate functions? If you do that then you could call the first one of those pow2ceil(), to match our existing pow2floor().

good point, powe2ceil() is enough for me, I'll repost it in a sec. thanks!

Signature · 1 line
Alexey