qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [RFC PATCH] utils: Add up_pow_of_two()
@ 2015-02-23  7:49 Alexey Kardashevskiy
  2015-02-23  8:54 ` Peter Maydell
  0 siblings, 1 reply; 3+ messages in thread
From: Alexey Kardashevskiy @ 2015-02-23  7:49 UTC (permalink / raw)
  To: qemu-devel; +Cc: Alexey Kardashevskiy, Alexander Graf

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

Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
---

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

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-02-23 12:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-02-23  7:49 [Qemu-devel] [RFC PATCH] utils: Add up_pow_of_two() Alexey Kardashevskiy
2015-02-23  8:54 ` Peter Maydell
2015-02-23 12:20   ` Alexey Kardashevskiy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).