diff options
author | Zhenzhong Duan <zhenzhong.duan@intel.com> | 2024-06-05 16:30:31 +0800 |
---|---|---|
committer | Cédric Le Goater <clg@redhat.com> | 2024-06-24 23:15:30 +0200 |
commit | 6f274444c579305d14d355bab24af31ea2bef224 (patch) | |
tree | 6054ac4f2962035c04b000c0064dff570548adec /include/qemu | |
parent | 9005f928447841ed253e000d5e8220e381872cb0 (diff) | |
download | qemu-6f274444c579305d14d355bab24af31ea2bef224.zip qemu-6f274444c579305d14d355bab24af31ea2bef224.tar.gz qemu-6f274444c579305d14d355bab24af31ea2bef224.tar.bz2 |
range: Introduce range_get_last_bit()
This helper get the highest 1 bit position of the upper bound.
If the range is empty or upper bound is zero, -1 is returned.
Suggested-by: Cédric Le Goater <clg@redhat.com>
Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
Reviewed-by: Eric Auger <eric.auger@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Diffstat (limited to 'include/qemu')
-rw-r--r-- | include/qemu/range.h | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/include/qemu/range.h b/include/qemu/range.h index 205e1da..4ce694a 100644 --- a/include/qemu/range.h +++ b/include/qemu/range.h @@ -20,6 +20,8 @@ #ifndef QEMU_RANGE_H #define QEMU_RANGE_H +#include "qemu/bitops.h" + /* * Operations on 64 bit address ranges. * Notes: @@ -217,6 +219,15 @@ static inline int ranges_overlap(uint64_t first1, uint64_t len1, return !(last2 < first1 || last1 < first2); } +/* Get highest non-zero bit position of a range */ +static inline int range_get_last_bit(Range *range) +{ + if (range_is_empty(range)) { + return -1; + } + return 63 - clz64(range->upb); +} + /* * Return -1 if @a < @b, 1 @a > @b, and 0 if they touch or overlap. * Both @a and @b must not be empty. |