Skip to contentSkip to conversation

include: Factor out log2_int() function.

PATCH Superseded Paul Gofman 2 participants 4 messages Oct 28, 2020 – Oct 30, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 include: Factor out log2_int() function.

---
 radeon/radeon_surface.c | 20 +-------------------
 util_math.h             | 14 ++++++++++++++
 xf86drm.c               | 16 ----------------
 3 files changed, 15 insertions(+), 35 deletions(-)
radeon/radeon_surface.c +1 -19
index ea0a27a9..c59dcc83 100644
@@ -38,6 +38,7 @@
 #include "xf86drm.h"
 #include "radeon_drm.h"
 #include "radeon_surface.h"
+#include "util_math.h"
 
 #define CIK_TILE_MODE_COLOR_2D			14
 #define CIK_TILE_MODE_COLOR_2D_SCANOUT		10
@@ -47,10 +48,6 @@
 #define CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_512      3
 #define CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_ROW_SIZE 4
 
-#define ALIGN(value, alignment) (((value) + alignment - 1) & ~(alignment - 1))
-#define MAX2(A, B)              ((A) > (B) ? (A) : (B))
-#define MIN2(A, B)              ((A) < (B) ? (A) : (B))
-
 /* keep this private */
 enum radeon_family {
     CHIP_UNKNOWN,
@@ -887,21 +884,6 @@ static int eg_surface_init(struct radeon_surface_manager *surf_man,
     return r;
 }
 
-static unsigned log2_int(unsigned x)
-{
-    unsigned l;
-
-    if (x < 2) {
-        return 0;
-    }
-    for (l = 2; ; l++) {
-        if ((unsigned)(1 << l) > x) {
-            return l - 1;
-        }
-    }
-    return 0;
-}
-
 /* compute best tile_split, bankw, bankh, mtilea
  * depending on surface
  */
util_math.h +14 -0
index 35bf4512..e2fa95f5 100644
@@ -31,4 +31,18 @@
 #define __align_mask(value, mask)  (((value) + (mask)) & ~(mask))
 #define ALIGN(value, alignment)    __align_mask(value, (__typeof__(value))((alignment) - 1))
 
+static inline unsigned log2_int(unsigned x)
+{
+    unsigned l;
+
+    if (x < 2) {
+        return 0;
+    }
+    for (l = 2; ; l++) {
+        if ((unsigned)(1 << l) > x) {
+            return l - 1;
+        }
+    }
+    return 0;
+}
 #endif /*_UTIL_MATH_H_*/
xf86drm.c +0 -16
index dbb7c14b..ca4738e1 100644
@@ -124,22 +124,6 @@ static drmServerInfoPtr drm_server_info;
 static bool drmNodeIsDRM(int maj, int min);
 static char *drmGetMinorNameForFD(int fd, int type);
 
-static unsigned log2_int(unsigned x)
-{
-    unsigned l;
-
-    if (x < 2) {
-        return 0;
-    }
-    for (l = 2; ; l++) {
-        if ((unsigned)(1 << l) > x) {
-            return l - 1;
-        }
-    }
-    return 0;
-}
-
-
 drm_public void drmSetServerInfo(drmServerInfoPtr info)
 {
     drm_server_info = info;
Signature · 6 lines
2.28.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw

PATCH 2/2 include: Avoid potentially infinite loop in log2_int().

---
 util_math.h | 3 +++
 1 file changed, 3 insertions(+)
util_math.h +3 -0
index e2fa95f5..f6bbe192 100644
@@ -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;
Signature · 6 lines
2.28.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#2 lore raw

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

Quoted 10 lines · On Wed, 28 Oct 2020 13:06:02 +0300 Paul Gofman <[email protected]> wrote:
---
 util_math.h | 3 +++
 1 file changed, 3 insertions(+)
util_math.h +0 -0
index e2fa95f5..f6bbe192 100644

util_math.h static inline unsigned log2_int(unsigned x) · line 46

Quoted 9 lines
util_math.h +3 -0
@@ -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

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#3 lore raw

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

Quoted 24 lines · Pekka Paalanen
Quoted 10 lines · On Wed, 28 Oct 2020 13:06:02 +0300 Paul Gofman <[email protected]> wrote:
---
 util_math.h | 3 +++
 1 file changed, 3 insertions(+)
util_math.h +0 -0
index e2fa95f5..f6bbe192 100644

util_math.h static inline unsigned log2_int(unsigned x) · line 46

Quoted 9 lines
util_math.h +3 -0
@@ -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,

Quoted 7 lines

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.

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel