diff mbox series

[v3,06/19] range: Introduce range_get_last_bit()

Message ID 20240429065046.3688701-7-zhenzhong.duan@intel.com (mailing list archive)
State New, archived
Headers show
Series Add a host IOMMU device abstraction to check with vIOMMU | expand

Commit Message

Duan, Zhenzhong April 29, 2024, 6:50 a.m. UTC
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>
---
 include/qemu/range.h | 11 +++++++++++
 1 file changed, 11 insertions(+)

Comments

Cédric Le Goater April 30, 2024, 9:41 a.m. UTC | #1
On 4/29/24 08:50, Zhenzhong Duan wrote:
> 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>
> ---
>   include/qemu/range.h | 11 +++++++++++
>   1 file changed, 11 insertions(+)
> 
> diff --git a/include/qemu/range.h b/include/qemu/range.h
> index 205e1da76d..8e05bc1d9f 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) || !range->upb) {
> +        return -1;
> +    }
> +    return find_last_bit(&range->upb, sizeof(range->upb));

This breaks builds on 32-bit host systems.


Thanks,

C.


> +}
> +
>   /*
>    * Return -1 if @a < @b, 1 @a > @b, and 0 if they touch or overlap.
>    * Both @a and @b must not be empty.
Duan, Zhenzhong April 30, 2024, 9:58 a.m. UTC | #2
>-----Original Message-----
>From: Cédric Le Goater <clg@redhat.com>
>Subject: Re: [PATCH v3 06/19] range: Introduce range_get_last_bit()
>
>On 4/29/24 08:50, Zhenzhong Duan wrote:
>> 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>
>> ---
>>   include/qemu/range.h | 11 +++++++++++
>>   1 file changed, 11 insertions(+)
>>
>> diff --git a/include/qemu/range.h b/include/qemu/range.h
>> index 205e1da76d..8e05bc1d9f 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) || !range->upb) {
>> +        return -1;
>> +    }
>> +    return find_last_bit(&range->upb, sizeof(range->upb));
>
>This breaks builds on 32-bit host systems.

Oh, I missed 32bit build. Thanks, will fix.

Thanks
zhenzhong

>
>
>Thanks,
>
>C.
>
>
>> +}
>> +
>>   /*
>>    * Return -1 if @a < @b, 1 @a > @b, and 0 if they touch or overlap.
>>    * Both @a and @b must not be empty.
Cédric Le Goater May 2, 2024, 10:30 a.m. UTC | #3
On 4/30/24 11:58, Duan, Zhenzhong wrote:
> 
> 
>> -----Original Message-----
>> From: Cédric Le Goater <clg@redhat.com>
>> Subject: Re: [PATCH v3 06/19] range: Introduce range_get_last_bit()
>>
>> On 4/29/24 08:50, Zhenzhong Duan wrote:
>>> 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>
>>> ---
>>>    include/qemu/range.h | 11 +++++++++++
>>>    1 file changed, 11 insertions(+)
>>>
>>> diff --git a/include/qemu/range.h b/include/qemu/range.h
>>> index 205e1da76d..8e05bc1d9f 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) || !range->upb) {
>>> +        return -1;
>>> +    }
>>> +    return find_last_bit(&range->upb, sizeof(range->upb));
>>
>> This breaks builds on 32-bit host systems.
> 
> Oh, I missed 32bit build. Thanks, will fix.

This should provide the same result ?

     return 63 - clz64(range->upb);

Thanks,

C.
Duan, Zhenzhong May 6, 2024, 6:45 a.m. UTC | #4
>-----Original Message-----
>From: Cédric Le Goater <clg@redhat.com>
>Subject: Re: [PATCH v3 06/19] range: Introduce range_get_last_bit()
>
>On 4/30/24 11:58, Duan, Zhenzhong wrote:
>>
>>
>>> -----Original Message-----
>>> From: Cédric Le Goater <clg@redhat.com>
>>> Subject: Re: [PATCH v3 06/19] range: Introduce range_get_last_bit()
>>>
>>> On 4/29/24 08:50, Zhenzhong Duan wrote:
>>>> 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>
>>>> ---
>>>>    include/qemu/range.h | 11 +++++++++++
>>>>    1 file changed, 11 insertions(+)
>>>>
>>>> diff --git a/include/qemu/range.h b/include/qemu/range.h
>>>> index 205e1da76d..8e05bc1d9f 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) || !range->upb) {
>>>> +        return -1;
>>>> +    }
>>>> +    return find_last_bit(&range->upb, sizeof(range->upb));
>>>
>>> This breaks builds on 32-bit host systems.
>>
>> Oh, I missed 32bit build. Thanks, will fix.
>
>This should provide the same result ?
>
>     return 63 - clz64(range->upb);

Yes, I tried 32bit and 64bit, it works. Will use it, thanks for suggestion.

BRs.
Zhenzhong
diff mbox series

Patch

diff --git a/include/qemu/range.h b/include/qemu/range.h
index 205e1da76d..8e05bc1d9f 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) || !range->upb) {
+        return -1;
+    }
+    return find_last_bit(&range->upb, sizeof(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.