[Mesa-dev] [PATCH 01/28] replace _mesa_is_pow_two with util_is_power_of_two_*

Dylan Baker dylan at pnwbakers.com
Fri Nov 9 18:39:54 UTC 2018


mostly this uses util_is_power_of_two_or_zero, which has the same
behavior as _mesa_is_pow_two when the input is zero. In cases where the
value is known to be != 0 ahead of time I used the _nonzero variant as
it may be faster on some platforms.
---
 src/intel/compiler/brw_reg.h                  |  2 +-
 src/intel/compiler/brw_vec4_generator.cpp     |  3 +--
 src/mesa/drivers/common/meta_blit.c           |  6 ++---
 src/mesa/drivers/dri/i915/i915_texstate.c     |  2 +-
 src/mesa/drivers/dri/nouveau/nv04_surface.c   |  4 +--
 .../drivers/dri/radeon/radeon_mipmap_tree.c   |  2 +-
 src/mesa/main/imports.h                       | 10 -------
 src/mesa/main/macros.h                        |  4 +--
 src/mesa/main/pixel.c                         |  6 ++---
 src/mesa/main/teximage.c                      | 26 +++++++++----------
 src/mesa/swrast/s_texture.c                   |  6 ++---
 src/mesa/tnl/t_vb_light.c                     |  2 +-
 12 files changed, 31 insertions(+), 42 deletions(-)

diff --git a/src/intel/compiler/brw_reg.h b/src/intel/compiler/brw_reg.h
index 46d66198a1d..b39aa1cd8e8 100644
--- a/src/intel/compiler/brw_reg.h
+++ b/src/intel/compiler/brw_reg.h
@@ -986,7 +986,7 @@ static inline struct brw_reg
 spread(struct brw_reg reg, unsigned s)
 {
    if (s) {
-      assert(_mesa_is_pow_two(s));
+      assert(util_is_power_of_two_or_zero(s));
 
       if (reg.hstride)
          reg.hstride += cvt(s) - 1;
diff --git a/src/intel/compiler/brw_vec4_generator.cpp b/src/intel/compiler/brw_vec4_generator.cpp
index 888cb358fd1..30a85b923ce 100644
--- a/src/intel/compiler/brw_vec4_generator.cpp
+++ b/src/intel/compiler/brw_vec4_generator.cpp
@@ -2057,8 +2057,7 @@ generate_code(struct brw_codegen *p,
           *
           * where they pack the four bytes from the low and high four DW.
           */
-         assert(_mesa_is_pow_two(dst.writemask) &&
-                dst.writemask != 0);
+         assert(util_is_power_of_two_nonzero(dst.writemask));
          unsigned offset = __builtin_ctz(dst.writemask);
 
          dst.type = BRW_REGISTER_TYPE_UB;
diff --git a/src/mesa/drivers/common/meta_blit.c b/src/mesa/drivers/common/meta_blit.c
index 496ef285d02..54ad72bae53 100644
--- a/src/mesa/drivers/common/meta_blit.c
+++ b/src/mesa/drivers/common/meta_blit.c
@@ -85,7 +85,7 @@ setup_glsl_msaa_blit_scaled_shader(struct gl_context *ctx,
    y_scale = samples / x_scale;
 
    /* We expect only power of 2 samples in source multisample buffer. */
-   assert(samples > 0 && _mesa_is_pow_two(samples));
+   assert(samples > 0 && util_is_power_of_two_nonzero(samples));
    while (samples >> (shader_offset + 1)) {
       shader_offset++;
    }
@@ -278,7 +278,7 @@ setup_glsl_msaa_blit_shader(struct gl_context *ctx,
    }
 
    /* We expect only power of 2 samples in source multisample buffer. */
-   assert(samples > 0 && _mesa_is_pow_two(samples));
+   assert(samples > 0 && util_is_power_of_two_nonzero(samples));
    while (samples >> (shader_offset + 1)) {
       shader_offset++;
    }
@@ -482,7 +482,7 @@ setup_glsl_msaa_blit_shader(struct gl_context *ctx,
           * (so the floating point exponent just gets increased), rather than
           * doing a naive sum and dividing.
           */
-         assert(_mesa_is_pow_two(samples));
+         assert(util_is_power_of_two_or_zero(samples));
          /* Fetch each individual sample. */
          sample_resolve = rzalloc_size(mem_ctx, 1);
          for (i = 0; i < samples; i++) {
diff --git a/src/mesa/drivers/dri/i915/i915_texstate.c b/src/mesa/drivers/dri/i915/i915_texstate.c
index f653f441ad8..d24256c81f0 100644
--- a/src/mesa/drivers/dri/i915/i915_texstate.c
+++ b/src/mesa/drivers/dri/i915/i915_texstate.c
@@ -342,7 +342,7 @@ i915_update_tex_unit(struct intel_context *intel, GLuint unit, GLuint ss3)
        * Thus, I guess we need do this for other platforms as well.
        */
       if (tObj->Target == GL_TEXTURE_CUBE_MAP_ARB &&
-          !_mesa_is_pow_two(firstImage->Height))
+          !util_is_power_of_two_or_zero(firstImage->Height))
          return false;
 
       state[I915_TEXREG_SS3] = ss3;     /* SS3_NORMALIZED_COORDS */
diff --git a/src/mesa/drivers/dri/nouveau/nv04_surface.c b/src/mesa/drivers/dri/nouveau/nv04_surface.c
index b1f0ea0a983..eca53ff7147 100644
--- a/src/mesa/drivers/dri/nouveau/nv04_surface.c
+++ b/src/mesa/drivers/dri/nouveau/nv04_surface.c
@@ -212,8 +212,8 @@ nv04_surface_copy_swizzle(struct gl_context *ctx,
 	unsigned x, y;
 
         /* Swizzled surfaces must be POT  */
-	assert(_mesa_is_pow_two(dst->width) &&
-	       _mesa_is_pow_two(dst->height));
+	assert(util_is_power_of_two_or_zero(dst->width) &&
+	       util_is_power_of_two_or_zero(dst->height));
 
 	if (context_chipset(ctx) < 0x10) {
 		BEGIN_NV04(push, NV01_SUBC(SURF, OBJECT), 1);
diff --git a/src/mesa/drivers/dri/radeon/radeon_mipmap_tree.c b/src/mesa/drivers/dri/radeon/radeon_mipmap_tree.c
index 19e62969e5c..c4b3c61cbf4 100644
--- a/src/mesa/drivers/dri/radeon/radeon_mipmap_tree.c
+++ b/src/mesa/drivers/dri/radeon/radeon_mipmap_tree.c
@@ -102,7 +102,7 @@ unsigned get_texture_image_row_stride(radeonContextPtr rmesa, mesa_format format
 	} else {
 		unsigned row_align;
 
-		if (!_mesa_is_pow_two(width) || target == GL_TEXTURE_RECTANGLE) {
+		if (!util_is_power_of_two_or_zero(width) || target == GL_TEXTURE_RECTANGLE) {
 			row_align = rmesa->texture_rect_row_align - 1;
 		} else if (tiling) {
 			unsigned tileWidth, tileHeight;
diff --git a/src/mesa/main/imports.h b/src/mesa/main/imports.h
index f461b1c052e..6193a83396f 100644
--- a/src/mesa/main/imports.h
+++ b/src/mesa/main/imports.h
@@ -205,16 +205,6 @@ static inline int IFLOOR(float f)
 #endif
 }
 
-
-/**
- * Is x a power of two?
- */
-static inline int
-_mesa_is_pow_two(int x)
-{
-   return !(x & (x - 1));
-}
-
 /**
  * Round given integer to next higer power of two
  * If X is zero result is undefined.
diff --git a/src/mesa/main/macros.h b/src/mesa/main/macros.h
index 03a228b4474..6547706b6fe 100644
--- a/src/mesa/main/macros.h
+++ b/src/mesa/main/macros.h
@@ -680,7 +680,7 @@ minify(unsigned value, unsigned levels)
 static inline uintptr_t
 ALIGN(uintptr_t value, int32_t alignment)
 {
-   assert((alignment > 0) && _mesa_is_pow_two(alignment));
+   assert((alignment > 0) && util_is_power_of_two_nonzero(alignment));
    return (((value) + (alignment) - 1) & ~((alignment) - 1));
 }
 
@@ -708,7 +708,7 @@ ALIGN_NPOT(uintptr_t value, int32_t alignment)
 static inline uintptr_t
 ROUND_DOWN_TO(uintptr_t value, int32_t alignment)
 {
-   assert((alignment > 0) && _mesa_is_pow_two(alignment));
+   assert((alignment > 0) && util_is_power_of_two_nonzero(alignment));
    return ((value) & ~(alignment - 1));
 }
 
diff --git a/src/mesa/main/pixel.c b/src/mesa/main/pixel.c
index b234bacd9b6..0c707361364 100644
--- a/src/mesa/main/pixel.c
+++ b/src/mesa/main/pixel.c
@@ -185,7 +185,7 @@ _mesa_PixelMapfv( GLenum map, GLsizei mapsize, const GLfloat *values )
 
    if (map >= GL_PIXEL_MAP_S_TO_S && map <= GL_PIXEL_MAP_I_TO_A) {
       /* test that mapsize is a power of two */
-      if (!_mesa_is_pow_two(mapsize)) {
+      if (!util_is_power_of_two_or_zero(mapsize)) {
 	 _mesa_error( ctx, GL_INVALID_VALUE, "glPixelMapfv(mapsize)" );
          return;
       }
@@ -226,7 +226,7 @@ _mesa_PixelMapuiv(GLenum map, GLsizei mapsize, const GLuint *values )
 
    if (map >= GL_PIXEL_MAP_S_TO_S && map <= GL_PIXEL_MAP_I_TO_A) {
       /* test that mapsize is a power of two */
-      if (!_mesa_is_pow_two(mapsize)) {
+      if (!util_is_power_of_two_or_zero(mapsize)) {
 	 _mesa_error( ctx, GL_INVALID_VALUE, "glPixelMapuiv(mapsize)" );
          return;
       }
@@ -281,7 +281,7 @@ _mesa_PixelMapusv(GLenum map, GLsizei mapsize, const GLushort *values )
 
    if (map >= GL_PIXEL_MAP_S_TO_S && map <= GL_PIXEL_MAP_I_TO_A) {
       /* test that mapsize is a power of two */
-      if (!_mesa_is_pow_two(mapsize)) {
+      if (!util_is_power_of_two_or_zero(mapsize)) {
 	 _mesa_error( ctx, GL_INVALID_VALUE, "glPixelMapusv(mapsize)" );
          return;
       }
diff --git a/src/mesa/main/teximage.c b/src/mesa/main/teximage.c
index 6805b47c728..9ed31840868 100644
--- a/src/mesa/main/teximage.c
+++ b/src/mesa/main/teximage.c
@@ -996,7 +996,7 @@ _mesa_legal_texture_dimensions(struct gl_context *ctx, GLenum target,
       if (width < 2 * border || width > 2 * border + maxSize)
          return GL_FALSE;
       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
-         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
+         if (width > 0 && !util_is_power_of_two_nonzero(width - 2 * border))
             return GL_FALSE;
       }
       return GL_TRUE;
@@ -1012,9 +1012,9 @@ _mesa_legal_texture_dimensions(struct gl_context *ctx, GLenum target,
       if (height < 2 * border || height > 2 * border + maxSize)
          return GL_FALSE;
       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
-         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
+         if (width > 0 && !util_is_power_of_two_nonzero(width - 2 * border))
             return GL_FALSE;
-         if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
+         if (height > 0 && !util_is_power_of_two_nonzero(height - 2 * border))
             return GL_FALSE;
       }
       return GL_TRUE;
@@ -1030,11 +1030,11 @@ _mesa_legal_texture_dimensions(struct gl_context *ctx, GLenum target,
       if (depth < 2 * border || depth > 2 * border + maxSize)
          return GL_FALSE;
       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
-         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
+         if (width > 0 && !util_is_power_of_two_nonzero(width - 2 * border))
             return GL_FALSE;
-         if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
+         if (height > 0 && !util_is_power_of_two_nonzero(height - 2 * border))
             return GL_FALSE;
-         if (depth > 0 && !_mesa_is_pow_two(depth - 2 * border))
+         if (depth > 0 && !util_is_power_of_two_nonzero(depth - 2 * border))
             return GL_FALSE;
       }
       return GL_TRUE;
@@ -1067,9 +1067,9 @@ _mesa_legal_texture_dimensions(struct gl_context *ctx, GLenum target,
       if (height < 2 * border || height > 2 * border + maxSize)
          return GL_FALSE;
       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
-         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
+         if (width > 0 && !util_is_power_of_two_nonzero(width - 2 * border))
             return GL_FALSE;
-         if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
+         if (height > 0 && !util_is_power_of_two_nonzero(height - 2 * border))
             return GL_FALSE;
       }
       return GL_TRUE;
@@ -1083,7 +1083,7 @@ _mesa_legal_texture_dimensions(struct gl_context *ctx, GLenum target,
       if (height < 0 || height > ctx->Const.MaxArrayTextureLayers)
          return GL_FALSE;
       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
-         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
+         if (width > 0 && !util_is_power_of_two_nonzero(width - 2 * border))
             return GL_FALSE;
       }
       return GL_TRUE;
@@ -1101,9 +1101,9 @@ _mesa_legal_texture_dimensions(struct gl_context *ctx, GLenum target,
       if (depth < 0 || depth > ctx->Const.MaxArrayTextureLayers)
          return GL_FALSE;
       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
-         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
+         if (width > 0 && !util_is_power_of_two_nonzero(width - 2 * border))
             return GL_FALSE;
-         if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
+         if (height > 0 && !util_is_power_of_two_nonzero(height - 2 * border))
             return GL_FALSE;
       }
       return GL_TRUE;
@@ -1122,9 +1122,9 @@ _mesa_legal_texture_dimensions(struct gl_context *ctx, GLenum target,
       if (level >= ctx->Const.MaxCubeTextureLevels)
          return GL_FALSE;
       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
-         if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
+         if (width > 0 && !util_is_power_of_two_nonzero(width - 2 * border))
             return GL_FALSE;
-         if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
+         if (height > 0 && !util_is_power_of_two_nonzero(height - 2 * border))
             return GL_FALSE;
       }
       return GL_TRUE;
diff --git a/src/mesa/swrast/s_texture.c b/src/mesa/swrast/s_texture.c
index d35bea96b92..b41cd5d57ae 100644
--- a/src/mesa/swrast/s_texture.c
+++ b/src/mesa/swrast/s_texture.c
@@ -127,9 +127,9 @@ _swrast_init_texture_image(struct gl_texture_image *texImage)
 {
    struct swrast_texture_image *swImg = swrast_texture_image(texImage);
 
-   if ((texImage->Width == 1 || _mesa_is_pow_two(texImage->Width2)) &&
-       (texImage->Height == 1 || _mesa_is_pow_two(texImage->Height2)) &&
-       (texImage->Depth == 1 || _mesa_is_pow_two(texImage->Depth2)))
+   if ((texImage->Width == 1 || util_is_power_of_two_or_zero(texImage->Width2)) &&
+       (texImage->Height == 1 || util_is_power_of_two_or_zero(texImage->Height2)) &&
+       (texImage->Depth == 1 || util_is_power_of_two_or_zero(texImage->Depth2)))
       swImg->_IsPowerOfTwo = GL_TRUE;
    else
       swImg->_IsPowerOfTwo = GL_FALSE;
diff --git a/src/mesa/tnl/t_vb_light.c b/src/mesa/tnl/t_vb_light.c
index 8d13712e218..d9650a797c3 100644
--- a/src/mesa/tnl/t_vb_light.c
+++ b/src/mesa/tnl/t_vb_light.c
@@ -399,7 +399,7 @@ static void validate_lighting( struct gl_context *ctx,
    }
    else {
       /* Power of two means only a single active light. */
-      if (_mesa_is_pow_two(ctx->Light._EnabledLights))
+      if (util_is_power_of_two_or_zero(ctx->Light._EnabledLights))
 	 tab = _tnl_light_fast_single_tab;
       else
 	 tab = _tnl_light_fast_tab;
-- 
2.19.1



More information about the mesa-dev mailing list