diff mbox

[RFC] utils: Add up_pow_of_two()

Message ID 1424677796-3044-1-git-send-email-aik@ozlabs.ru
State New
Headers show

Commit Message

Alexey Kardashevskiy Feb. 23, 2015, 7:49 a.m. UTC
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(+)

Comments

Peter Maydell Feb. 23, 2015, 8:54 a.m. UTC | #1
On 23 February 2015 at 16:49, Alexey Kardashevskiy <aik@ozlabs.ru> wrote:
> 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()?

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
Alexey Kardashevskiy Feb. 23, 2015, 12:20 p.m. UTC | #2
On 02/23/2015 07:54 PM, Peter Maydell wrote:
> On 23 February 2015 at 16:49, Alexey Kardashevskiy <aik@ozlabs.ru> wrote:
>> 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()?
>
> 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!
diff mbox

Patch

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