qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v2] utils: Add pow2ceil()
@ 2015-02-23 12:23 Alexey Kardashevskiy
  2015-02-23 13:59 ` Markus Armbruster
  0 siblings, 1 reply; 14+ messages in thread
From: Alexey Kardashevskiy @ 2015-02-23 12:23 UTC (permalink / raw)
  To: qemu-devel; +Cc: Alexey Kardashevskiy, Peter Maydell

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

Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
---
Changes:
v2:
* s/up_pow_of_two/pow2ceil/
---
 include/qemu-common.h | 2 ++
 util/cutils.c         | 9 +++++++++
 2 files changed, 11 insertions(+)

diff --git a/include/qemu-common.h b/include/qemu-common.h
index 644b46d..ae29748 100644
--- a/include/qemu-common.h
+++ b/include/qemu-common.h
@@ -417,6 +417,8 @@ static inline bool is_power_of_2(uint64_t value)
 
 /* round down to the nearest power of 2*/
 int64_t pow2floor(int64_t value);
+/* round up to the nearest power of 2*/
+int64_t pow2ceil(int64_t value);
 
 #include "qemu/module.h"
 
diff --git a/util/cutils.c b/util/cutils.c
index dbe7412..ecaa440 100644
--- a/util/cutils.c
+++ b/util/cutils.c
@@ -483,6 +483,15 @@ int64_t pow2floor(int64_t value)
     return value;
 }
 
+/* round up to the nearest power of 2*/
+int64_t pow2ceil(int64_t value)
+{
+    if (!is_power_of_2(value)) {
+        value = 0x8000000000000000ULL >> (clz64(value) - 1);
+    }
+    return value;
+}
+
 /*
  * Implementation of  ULEB128 (http://en.wikipedia.org/wiki/LEB128)
  * Input is limited to 14-bit numbers
-- 
2.0.0

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

end of thread, other threads:[~2015-03-13 19:04 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-02-23 12:23 [Qemu-devel] [PATCH v2] utils: Add pow2ceil() Alexey Kardashevskiy
2015-02-23 13:59 ` Markus Armbruster
2015-02-23 16:17   ` Eric Blake
2015-02-23 17:40     ` Markus Armbruster
2015-02-23 21:20       ` Eric Blake
2015-02-24  9:39         ` Markus Armbruster
2015-02-24 11:39           ` Peter Maydell
2015-02-24 13:09             ` Markus Armbruster
2015-02-25  0:40   ` Alexey Kardashevskiy
2015-02-25 10:45     ` Markus Armbruster
2015-03-12 15:29       ` Richard Henderson
2015-03-12 16:45         ` Eric Blake
2015-03-13 19:04           ` Richard Henderson
2015-03-13  7:33         ` Markus Armbruster

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).