[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 4/8] host-utils: Simplify pow2ceil()
From: |
Dr. David Alan Gilbert (git) |
Subject: |
[Qemu-devel] [PULL 4/8] host-utils: Simplify pow2ceil() |
Date: |
Wed, 6 Sep 2017 19:41:29 +0100 |
From: Markus Armbruster <address@hidden>
Cc: Radim Krčmář <address@hidden>
Signed-off-by: Markus Armbruster <address@hidden>
Message-Id: <address@hidden>
Reviewed-by: Eric Blake <address@hidden>
Signed-off-by: Dr. David Alan Gilbert <address@hidden>
---
include/qemu/host-utils.h | 23 ++++++++++++++---------
1 file changed, 14 insertions(+), 9 deletions(-)
diff --git a/include/qemu/host-utils.h b/include/qemu/host-utils.h
index 6c6005f5cf..5ac621cf1f 100644
--- a/include/qemu/host-utils.h
+++ b/include/qemu/host-utils.h
@@ -381,18 +381,23 @@ static inline uint64_t pow2floor(uint64_t value)
return 0x8000000000000000ull >> clz64(value);
}
-/* round up to the nearest power of 2 (0 if overflow) */
+/*
+ * Return @value rounded up to the nearest power of two modulo 2^64.
+ * This is *zero* for @value > 2^63, so be careful.
+ */
static inline uint64_t pow2ceil(uint64_t value)
{
- uint8_t nlz = clz64(value);
-
- if (is_power_of_2(value)) {
- return value;
- }
- if (!nlz) {
- return 0;
+ int n = clz64(value - 1);
+
+ if (!n) {
+ /*
+ * @value - 1 has no leading zeroes, thus @value - 1 >= 2^63
+ * Therefore, either @value == 0 or @value > 2^63.
+ * If it's 0, return 1, else return 0.
+ */
+ return !value;
}
- return 1ULL << (64 - nlz);
+ return 0x8000000000000000ull >> (n - 1);
}
/**
--
2.13.5
- [Qemu-devel] [PULL 0/8] migration queue, Dr. David Alan Gilbert (git), 2017/09/06
- [Qemu-devel] [PULL 1/8] migration: Report when bdrv_inactivate_all fails, Dr. David Alan Gilbert (git), 2017/09/06
- [Qemu-devel] [PULL 3/8] host-utils: Proactively fix pow2floor(), switch to unsigned, Dr. David Alan Gilbert (git), 2017/09/06
- [Qemu-devel] [PULL 4/8] host-utils: Simplify pow2ceil(),
Dr. David Alan Gilbert (git) <=
- [Qemu-devel] [PULL 2/8] xbzrle: Drop unused cache_resize(), Dr. David Alan Gilbert (git), 2017/09/06
- [Qemu-devel] [PULL 5/8] runstate/migrate: Two more transitions, Dr. David Alan Gilbert (git), 2017/09/06
- [Qemu-devel] [PULL 7/8] snapshot/tests: Try loadvm twice, Dr. David Alan Gilbert (git), 2017/09/06
- [Qemu-devel] [PULL 6/8] migration: Reset rather than destroy main_thread_load_event, Dr. David Alan Gilbert (git), 2017/09/06
- [Qemu-devel] [PULL 8/8] migration: dump str in migrate_set_state trace, Dr. David Alan Gilbert (git), 2017/09/06
- Re: [Qemu-devel] [PULL 0/8] migration queue, Peter Maydell, 2017/09/07