[Mesa-dev] [PATCH 20/29] mesa: Use bitmask/ffs to iterate enabled clip planes.

Mathias.Froehlich at gmx.net Mathias.Froehlich at gmx.net
Tue May 24 06:49:03 UTC 2016


From: Mathias Fröhlich <mathias.froehlich at web.de>

Replaces an iterate and test bit in a bitmask loop by a
loop only iterating over the bits set in the bitmask.

Signed-off-by: Mathias Fröhlich <Mathias.Froehlich at web.de>
---
 src/mesa/drivers/common/meta.c | 23 +++++++--------
 src/mesa/main/matrix.c         | 19 ++++++------
 src/mesa/main/rastpos.c        | 23 ++++++++-------
 src/mesa/tnl/t_vb_cliptmp.h    | 54 +++++++++++++++++------------------
 src/mesa/tnl/t_vb_program.c    | 65 +++++++++++++++++++++---------------------
 src/mesa/tnl/t_vb_vertex.c     | 58 ++++++++++++++++++-------------------
 6 files changed, 121 insertions(+), 121 deletions(-)

diff --git a/src/mesa/drivers/common/meta.c b/src/mesa/drivers/common/meta.c
index 6dcbc8b..499ff0d 100644
--- a/src/mesa/drivers/common/meta.c
+++ b/src/mesa/drivers/common/meta.c
@@ -682,12 +682,13 @@ _mesa_meta_begin(struct gl_context *ctx, GLbitfield state)
    }
 
    if (state & MESA_META_CLIP) {
+      GLbitfield mask;
       save->ClipPlanesEnabled = ctx->Transform.ClipPlanesEnabled;
-      if (ctx->Transform.ClipPlanesEnabled) {
-         GLuint i;
-         for (i = 0; i < ctx->Const.MaxClipPlanes; i++) {
-            _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_FALSE);
-         }
+      mask = ctx->Transform.ClipPlanesEnabled;
+      while (mask) {
+         const int i = ffs(mask) - 1;
+         mask ^= (1u << i);
+         _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_FALSE);
       }
    }
 
@@ -1090,13 +1091,11 @@ _mesa_meta_end(struct gl_context *ctx)
    }
 
    if (state & MESA_META_CLIP) {
-      if (save->ClipPlanesEnabled) {
-         GLuint i;
-         for (i = 0; i < ctx->Const.MaxClipPlanes; i++) {
-            if (save->ClipPlanesEnabled & (1 << i)) {
-               _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_TRUE);
-            }
-         }
+      GLbitfield mask = save->ClipPlanesEnabled;
+      while (mask) {
+         const int i = ffs(mask) - 1;
+         mask ^= (1u << i);
+         _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_TRUE);
       }
    }
 
diff --git a/src/mesa/main/matrix.c b/src/mesa/main/matrix.c
index 5ff5ac5..2cd0418 100644
--- a/src/mesa/main/matrix.c
+++ b/src/mesa/main/matrix.c
@@ -554,20 +554,21 @@ _mesa_MultTransposeMatrixd( const GLdouble *m )
 static void
 update_projection( struct gl_context *ctx )
 {
+   GLbitfield mask;
+
    _math_matrix_analyse( ctx->ProjectionMatrixStack.Top );
 
    /* Recompute clip plane positions in clipspace.  This is also done
     * in _mesa_ClipPlane().
     */
-   if (ctx->Transform.ClipPlanesEnabled) {
-      GLuint p;
-      for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
-	 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
-	    _mesa_transform_vector( ctx->Transform._ClipUserPlane[p],
-				 ctx->Transform.EyeUserPlane[p],
-				 ctx->ProjectionMatrixStack.Top->inv );
-	 }
-      }
+   mask = ctx->Transform.ClipPlanesEnabled;
+   while (mask) {
+      int p = ffs(mask) - 1;
+      mask ^= (1u << p);
+
+      _mesa_transform_vector( ctx->Transform._ClipUserPlane[p],
+                              ctx->Transform.EyeUserPlane[p],
+                              ctx->ProjectionMatrixStack.Top->inv );
    }
 }
 
diff --git a/src/mesa/main/rastpos.c b/src/mesa/main/rastpos.c
index 6ab439e..4a0a2b4 100644
--- a/src/mesa/main/rastpos.c
+++ b/src/mesa/main/rastpos.c
@@ -90,17 +90,18 @@ viewclip_point_z( const GLfloat v[] )
 static GLuint
 userclip_point( struct gl_context *ctx, const GLfloat v[] )
 {
-   GLuint p;
-
-   for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
-      if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
-	 GLfloat dot = v[0] * ctx->Transform._ClipUserPlane[p][0]
-		     + v[1] * ctx->Transform._ClipUserPlane[p][1]
-		     + v[2] * ctx->Transform._ClipUserPlane[p][2]
-		     + v[3] * ctx->Transform._ClipUserPlane[p][3];
-         if (dot < 0.0F) {
-            return 0;
-         }
+   GLbitfield mask = ctx->Transform.ClipPlanesEnabled;
+   while (mask) {
+      int p = ffs(mask) - 1;
+      GLfloat dot = v[0] * ctx->Transform._ClipUserPlane[p][0]
+         + v[1] * ctx->Transform._ClipUserPlane[p][1]
+         + v[2] * ctx->Transform._ClipUserPlane[p][2]
+         + v[3] * ctx->Transform._ClipUserPlane[p][3];
+
+      mask ^= (1u << p);
+
+      if (dot < 0.0F) {
+         return 0;
       }
    }
 
diff --git a/src/mesa/tnl/t_vb_cliptmp.h b/src/mesa/tnl/t_vb_cliptmp.h
index 12181f0..8142855 100644
--- a/src/mesa/tnl/t_vb_cliptmp.h
+++ b/src/mesa/tnl/t_vb_cliptmp.h
@@ -124,7 +124,6 @@ TAG(clip_line)( struct gl_context *ctx, GLuint v0, GLuint v1, GLubyte mask )
    GLuint newvert = VB->Count;
    GLfloat t0 = 0;
    GLfloat t1 = 0;
-   GLuint p;
    const GLuint v0_orig = v0;
 
    if (mask & CLIP_FRUSTUM_BITS) {
@@ -137,14 +136,15 @@ TAG(clip_line)( struct gl_context *ctx, GLuint v0, GLuint v1, GLubyte mask )
    }
 
    if (mask & CLIP_USER_BIT) {
-      for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
-	 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
-            const GLfloat a = ctx->Transform._ClipUserPlane[p][0];
-            const GLfloat b = ctx->Transform._ClipUserPlane[p][1];
-            const GLfloat c = ctx->Transform._ClipUserPlane[p][2];
-            const GLfloat d = ctx->Transform._ClipUserPlane[p][3];
-	    LINE_CLIP( CLIP_USER_BIT, a, b, c, d );
-	 }
+      GLbitfield enabled = ctx->Transform.ClipPlanesEnabled;
+      while (enabled) {
+         int p = ffs(enabled) - 1;
+         const GLfloat a = ctx->Transform._ClipUserPlane[p][0];
+         const GLfloat b = ctx->Transform._ClipUserPlane[p][1];
+         const GLfloat c = ctx->Transform._ClipUserPlane[p][2];
+         const GLfloat d = ctx->Transform._ClipUserPlane[p][3];
+         enabled ^= (1u << p);
+         LINE_CLIP( CLIP_USER_BIT, a, b, c, d );
       }
    }
 
@@ -194,7 +194,6 @@ TAG(clip_tri)( struct gl_context *ctx, GLuint v0, GLuint v1, GLuint v2, GLubyte
    GLuint pv = v2;
    GLuint vlist[2][MAX_CLIPPED_VERTICES];
    GLuint *inlist = vlist[0], *outlist = vlist[1];
-   GLuint p;
    GLuint n = 3;
 
    ASSIGN_3V(inlist, v2, v0, v1 ); /* pv rotated to slot zero */
@@ -226,14 +225,15 @@ TAG(clip_tri)( struct gl_context *ctx, GLuint v0, GLuint v1, GLuint v2, GLubyte
    }
 
    if (mask & CLIP_USER_BIT) {
-      for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
-         if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
-            const GLfloat a = ctx->Transform._ClipUserPlane[p][0];
-            const GLfloat b = ctx->Transform._ClipUserPlane[p][1];
-            const GLfloat c = ctx->Transform._ClipUserPlane[p][2];
-            const GLfloat d = ctx->Transform._ClipUserPlane[p][3];
-            POLY_CLIP( CLIP_USER_BIT, a, b, c, d );
-         }
+      GLbitfield enabled = ctx->Transform.ClipPlanesEnabled;
+      while (enabled) {
+         int p = ffs(enabled) - 1;
+         const GLfloat a = ctx->Transform._ClipUserPlane[p][0];
+         const GLfloat b = ctx->Transform._ClipUserPlane[p][1];
+         const GLfloat c = ctx->Transform._ClipUserPlane[p][2];
+         const GLfloat d = ctx->Transform._ClipUserPlane[p][3];
+         enabled ^= (1u << p);
+         POLY_CLIP( CLIP_USER_BIT, a, b, c, d );
       }
    }
 
@@ -274,7 +274,6 @@ TAG(clip_quad)( struct gl_context *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint
    GLuint pv = v3;
    GLuint vlist[2][MAX_CLIPPED_VERTICES];
    GLuint *inlist = vlist[0], *outlist = vlist[1];
-   GLuint p;
    GLuint n = 4;
 
    ASSIGN_4V(inlist, v3, v0, v1, v2 ); /* pv rotated to slot zero */
@@ -289,14 +288,15 @@ TAG(clip_quad)( struct gl_context *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint
    }
 
    if (mask & CLIP_USER_BIT) {
-      for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
-	 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
-            const GLfloat a = ctx->Transform._ClipUserPlane[p][0];
-            const GLfloat b = ctx->Transform._ClipUserPlane[p][1];
-            const GLfloat c = ctx->Transform._ClipUserPlane[p][2];
-            const GLfloat d = ctx->Transform._ClipUserPlane[p][3];
-	    POLY_CLIP( CLIP_USER_BIT, a, b, c, d );
-	 }
+      GLbitfield enabled = ctx->Transform.ClipPlanesEnabled;
+      while (enabled) {
+         int p = ffs(enabled) - 1;
+         const GLfloat a = ctx->Transform._ClipUserPlane[p][0];
+         const GLfloat b = ctx->Transform._ClipUserPlane[p][1];
+         const GLfloat c = ctx->Transform._ClipUserPlane[p][2];
+         const GLfloat d = ctx->Transform._ClipUserPlane[p][3];
+         enabled ^= (1u << p);
+         POLY_CLIP( CLIP_USER_BIT, a, b, c, d );
       }
    }
 
diff --git a/src/mesa/tnl/t_vb_program.c b/src/mesa/tnl/t_vb_program.c
index 21fd6cd..5d5dd1d 100644
--- a/src/mesa/tnl/t_vb_program.c
+++ b/src/mesa/tnl/t_vb_program.c
@@ -84,40 +84,39 @@ userclip( struct gl_context *ctx,
           GLubyte *clipormask,
           GLubyte *clipandmask )
 {
-   GLuint p;
-
-   for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
-      if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
-	 GLuint nr, i;
-	 const GLfloat a = ctx->Transform._ClipUserPlane[p][0];
-	 const GLfloat b = ctx->Transform._ClipUserPlane[p][1];
-	 const GLfloat c = ctx->Transform._ClipUserPlane[p][2];
-	 const GLfloat d = ctx->Transform._ClipUserPlane[p][3];
-         GLfloat *coord = (GLfloat *)clip->data;
-         GLuint stride = clip->stride;
-         GLuint count = clip->count;
-
-	 for (nr = 0, i = 0 ; i < count ; i++) {
-	    GLfloat dp = (coord[0] * a + 
-			  coord[1] * b +
-			  coord[2] * c +
-			  coord[3] * d);
-
-	    if (dp < 0) {
-	       nr++;
-	       clipmask[i] |= CLIP_USER_BIT;
-	    }
-
-	    STRIDE_F(coord, stride);
-	 }
+   GLbitfield mask = ctx->Transform.ClipPlanesEnabled;
+   while (mask) {
+      int p = ffs(mask) - 1;
+      GLuint nr, i;
+      const GLfloat a = ctx->Transform._ClipUserPlane[p][0];
+      const GLfloat b = ctx->Transform._ClipUserPlane[p][1];
+      const GLfloat c = ctx->Transform._ClipUserPlane[p][2];
+      const GLfloat d = ctx->Transform._ClipUserPlane[p][3];
+      GLfloat *coord = (GLfloat *)clip->data;
+      GLuint stride = clip->stride;
+      GLuint count = clip->count;
+      mask ^= (1u << p);
+
+      for (nr = 0, i = 0 ; i < count ; i++) {
+         GLfloat dp = (coord[0] * a +
+                       coord[1] * b +
+                       coord[2] * c +
+                       coord[3] * d);
+
+         if (dp < 0) {
+            nr++;
+            clipmask[i] |= CLIP_USER_BIT;
+         }
+
+         STRIDE_F(coord, stride);
+      }
 
-	 if (nr > 0) {
-	    *clipormask |= CLIP_USER_BIT;
-	    if (nr == count) {
-	       *clipandmask |= CLIP_USER_BIT;
-	       return;
-	    }
-	 }
+      if (nr > 0) {
+         *clipormask |= CLIP_USER_BIT;
+         if (nr == count) {
+            *clipandmask |= CLIP_USER_BIT;
+            return;
+         }
       }
    }
 }
diff --git a/src/mesa/tnl/t_vb_vertex.c b/src/mesa/tnl/t_vb_vertex.c
index b56d680..e8a9050 100644
--- a/src/mesa/tnl/t_vb_vertex.c
+++ b/src/mesa/tnl/t_vb_vertex.c
@@ -63,40 +63,40 @@ static void NAME( struct gl_context *ctx,				\
 		  GLubyte *clipormask,				\
 		  GLubyte *clipandmask )			\
 {								\
-   GLuint p;							\
+   GLbitfield mask = ctx->Transform.ClipPlanesEnabled;          \
+   while (mask) {                                               \
+      int p = ffs(mask) - 1;                                    \
+      GLuint nr, i;						\
+      const GLfloat a = ctx->Transform._ClipUserPlane[p][0];	\
+      const GLfloat b = ctx->Transform._ClipUserPlane[p][1];	\
+      const GLfloat c = ctx->Transform._ClipUserPlane[p][2];	\
+      const GLfloat d = ctx->Transform._ClipUserPlane[p][3];	\
+      GLfloat *coord = (GLfloat *)clip->data;                   \
+      GLuint stride = clip->stride;				\
+      GLuint count = clip->count;				\
+      mask ^= (1u << p);                                        \
 								\
-   for (p = 0; p < ctx->Const.MaxClipPlanes; p++)		\
-      if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {	\
-	 GLuint nr, i;						\
-	 const GLfloat a = ctx->Transform._ClipUserPlane[p][0];	\
-	 const GLfloat b = ctx->Transform._ClipUserPlane[p][1];	\
-	 const GLfloat c = ctx->Transform._ClipUserPlane[p][2];	\
-	 const GLfloat d = ctx->Transform._ClipUserPlane[p][3];	\
-         GLfloat *coord = (GLfloat *)clip->data;		\
-         GLuint stride = clip->stride;				\
-         GLuint count = clip->count;				\
+      for (nr = 0, i = 0 ; i < count ; i++) {                   \
+         GLfloat dp = coord[0] * a + coord[1] * b;		\
+         if (SZ > 2) dp += coord[2] * c;			\
+         if (SZ > 3) dp += coord[3] * d; else dp += d;          \
 								\
-	 for (nr = 0, i = 0 ; i < count ; i++) {		\
-	    GLfloat dp = coord[0] * a + coord[1] * b;		\
-	    if (SZ > 2) dp += coord[2] * c;			\
-	    if (SZ > 3) dp += coord[3] * d; else dp += d;	\
+         if (dp < 0) {                                          \
+            nr++;						\
+            clipmask[i] |= CLIP_USER_BIT;			\
+         }							\
 								\
-	    if (dp < 0) {					\
-	       nr++;						\
-	       clipmask[i] |= CLIP_USER_BIT;			\
-	    }							\
+         STRIDE_F(coord, stride);				\
+      }                                                         \
 								\
-	    STRIDE_F(coord, stride);				\
-	 }							\
-								\
-	 if (nr > 0) {						\
-	    *clipormask |= CLIP_USER_BIT;			\
-	    if (nr == count) {					\
-	       *clipandmask |= CLIP_USER_BIT;			\
-	       return;						\
-	    }							\
-	 }							\
+      if (nr > 0) {						\
+         *clipormask |= CLIP_USER_BIT;                          \
+         if (nr == count) {					\
+            *clipandmask |= CLIP_USER_BIT;			\
+            return;						\
+         }							\
       }								\
+   }								\
 }
 
 
-- 
2.5.5



More information about the mesa-dev mailing list