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

Paul Gofman pgofman at codeweavers.com
Fri Oct 30 13:06:12 UTC 2020


Signed-off-by: Paul Gofman <pgofman at codeweavers.com>
---
v2:
    - simplify log2_int implementation.

 util_math.h | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/util_math.h b/util_math.h
index e2fa95f5..32297349 100644
--- a/util_math.h
+++ b/util_math.h
@@ -33,16 +33,11 @@
 
 static inline unsigned log2_int(unsigned x)
 {
-    unsigned l;
+    unsigned l = 0;
 
-    if (x < 2) {
-        return 0;
-    }
-    for (l = 2; ; l++) {
-        if ((unsigned)(1 << l) > x) {
-            return l - 1;
-        }
-    }
-    return 0;
+    while (x >>= 1)
+      ++l;
+
+    return l;
 }
 #endif /*_UTIL_MATH_H_*/
-- 
2.28.0



More information about the dri-devel mailing list