aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorPeter Maydell <peter.maydell@linaro.org>2017-09-07 15:26:06 +0100
committerPeter Maydell <peter.maydell@linaro.org>2017-09-07 15:26:06 +0100
commit7794b34e63fd42803e959c4989e5358f2412d325 (patch)
treed02cc131cb9f67a081a14a5f92deccf8b09190da /include
parent7e375e04422be092160635ef42e16daec1b0e4ca (diff)
parenta31fedeed764ce0b0d6097d4334c5770e74641a0 (diff)
downloadqemu-7794b34e63fd42803e959c4989e5358f2412d325.zip
qemu-7794b34e63fd42803e959c4989e5358f2412d325.tar.gz
qemu-7794b34e63fd42803e959c4989e5358f2412d325.tar.bz2
Merge remote-tracking branch 'remotes/dgilbert/tags/pull-migration-20170906a' into staging
migration pull 2017-09-06 # gpg: Signature made Wed 06 Sep 2017 19:39:23 BST # gpg: using RSA key 0x0516331EBC5BFDE7 # gpg: Good signature from "Dr. David Alan Gilbert (RH2) <dgilbert@redhat.com>" # gpg: WARNING: This key is not certified with sufficiently trusted signatures! # gpg: It is not certain that the signature belongs to the owner. # Primary key fingerprint: 45F5 C71B 4A0C B7FB 977A 9FA9 0516 331E BC5B FDE7 * remotes/dgilbert/tags/pull-migration-20170906a: migration: dump str in migrate_set_state trace snapshot/tests: Try loadvm twice migration: Reset rather than destroy main_thread_load_event runstate/migrate: Two more transitions host-utils: Simplify pow2ceil() host-utils: Proactively fix pow2floor(), switch to unsigned xbzrle: Drop unused cache_resize() migration: Report when bdrv_inactivate_all fails Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Diffstat (limited to 'include')
-rw-r--r--include/qemu/host-utils.h36
1 files changed, 22 insertions, 14 deletions
diff --git a/include/qemu/host-utils.h b/include/qemu/host-utils.h
index 95cf4f4..5ac621c 100644
--- a/include/qemu/host-utils.h
+++ b/include/qemu/host-utils.h
@@ -369,27 +369,35 @@ static inline bool is_power_of_2(uint64_t value)
return !(value & (value - 1));
}
-/* round down to the nearest power of 2*/
-static inline int64_t pow2floor(int64_t value)
+/**
+ * Return @value rounded down to the nearest power of two or zero.
+ */
+static inline uint64_t pow2floor(uint64_t value)
{
- if (!is_power_of_2(value)) {
- value = 0x8000000000000000ULL >> clz64(value);
+ if (!value) {
+ /* Avoid undefined shift by 64 */
+ return 0;
}
- return 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);
}
/**