diff mbox series

[libdrm,2/2] include: Avoid potentially infinite loop in log2_int().

Message ID 20201028100602.168752-2-pgofman@codeweavers.com (mailing list archive)
State New, archived
Headers show
Series [libdrm,1/2] include: Factor out log2_int() function. | expand

Commit Message

Paul Gofman Oct. 28, 2020, 10:06 a.m. UTC
Signed-off-by: Paul Gofman <pgofman@codeweavers.com>
---
 util_math.h | 3 +++
 1 file changed, 3 insertions(+)

Comments

Pekka Paalanen Oct. 30, 2020, 11:07 a.m. UTC | #1
On Wed, 28 Oct 2020 13:06:02 +0300
Paul Gofman <pgofman@codeweavers.com> wrote:

> Signed-off-by: Paul Gofman <pgofman@codeweavers.com>
> ---
>  util_math.h | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/util_math.h b/util_math.h
> index e2fa95f5..f6bbe192 100644
> --- a/util_math.h
> +++ b/util_math.h
> @@ -38,6 +38,9 @@ static inline unsigned log2_int(unsigned x)
>      if (x < 2) {
>          return 0;
>      }
> +    if (x & 0x80000000) {
> +        return 31;
> +    }
>      for (l = 2; ; l++) {
>          if ((unsigned)(1 << l) > x) {
>              return l - 1;

Hi,

I guess that does it, but it seems quite a lot of code that could be
a two-liner:
http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious


Thanks,
pq
Paul Gofman Oct. 30, 2020, 1:07 p.m. UTC | #2
On 10/30/20 14:07, Pekka Paalanen wrote:
> On Wed, 28 Oct 2020 13:06:02 +0300
> Paul Gofman <pgofman@codeweavers.com> wrote:
>
>> Signed-off-by: Paul Gofman <pgofman@codeweavers.com>
>> ---
>>  util_math.h | 3 +++
>>  1 file changed, 3 insertions(+)
>>
>> diff --git a/util_math.h b/util_math.h
>> index e2fa95f5..f6bbe192 100644
>> --- a/util_math.h
>> +++ b/util_math.h
>> @@ -38,6 +38,9 @@ static inline unsigned log2_int(unsigned x)
>>      if (x < 2) {
>>          return 0;
>>      }
>> +    if (x & 0x80000000) {
>> +        return 31;
>> +    }
>>      for (l = 2; ; l++) {
>>          if ((unsigned)(1 << l) > x) {
>>              return l - 1;
> Hi,
>
> I guess that does it, but it seems quite a lot of code that could be
> a two-liner:
> http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
>
>
> Thanks,
> pq

Hello,

    thanks, I've sent the updated patches with simplified implementation.

Regards,

    Paul.
diff mbox series

Patch

diff --git a/util_math.h b/util_math.h
index e2fa95f5..f6bbe192 100644
--- a/util_math.h
+++ b/util_math.h
@@ -38,6 +38,9 @@  static inline unsigned log2_int(unsigned x)
     if (x < 2) {
         return 0;
     }
+    if (x & 0x80000000) {
+        return 31;
+    }
     for (l = 2; ; l++) {
         if ((unsigned)(1 << l) > x) {
             return l - 1;