diff options
author | Jean-Philippe Brucker <jean-philippe.brucker@arm.com> | 2018-06-18 19:42:05 +0100 |
---|---|---|
committer | Will Deacon <will.deacon@arm.com> | 2018-06-19 12:26:40 +0100 |
commit | ac70b5aae6f514465bca744496ce7fb7859c6378 (patch) | |
tree | 05f9f169f66074d8899356b556e2ea260b584f36 | |
parent | b70d1b9fc0a9ac6c47b0b9ef2fe80bf3542b0bb3 (diff) | |
download | kvmtool-ac70b5aae6f514465bca744496ce7fb7859c6378.tar.gz |
Add fls_long and roundup_pow_of_two helpers
It's always nice to have a log2 handy, and the vfio-pci code will need to
perform power of two allocation from an arbitrary size. Add fls_long and
roundup_pow_of_two, based on the GCC builtin.
Reviewed-by: Punit Agrawal <punit.agrawal@arm.com>
Signed-off-by: Jean-Philippe Brucker <jean-philippe.brucker@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
-rw-r--r-- | include/kvm/util.h | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/include/kvm/util.h b/include/kvm/util.h index 0df9f0df..4ca7aa93 100644 --- a/include/kvm/util.h +++ b/include/kvm/util.h @@ -90,6 +90,20 @@ static inline void msleep(unsigned int msecs) usleep(MSECS_TO_USECS(msecs)); } +/* + * Find last (most significant) bit set. Same implementation as Linux: + * fls(0) = 0, fls(1) = 1, fls(1UL << 63) = 64 + */ +static inline int fls_long(unsigned long x) +{ + return x ? sizeof(x) * 8 - __builtin_clzl(x) : 0; +} + +static inline unsigned long roundup_pow_of_two(unsigned long x) +{ + return x ? 1UL << fls_long(x - 1) : 0; +} + struct kvm; void *mmap_hugetlbfs(struct kvm *kvm, const char *htlbfs_path, u64 size); void *mmap_anon_or_hugetlbfs(struct kvm *kvm, const char *hugetlbfs_path, u64 size); |