[Mesa-dev] [PATCH 06/12] util: Add and use util_is_power_of_two_nonzero

Ian Romanick idr at freedesktop.org
Tue Nov 21 01:24:04 UTC 2017


From: Ian Romanick <ian.d.romanick at intel.com>

Signed-off-by: Ian Romanick <ian.d.romanick at intel.com>
---
 src/compiler/nir/nir_search_helpers.h | 13 ++++---------
 src/compiler/nir/nir_validate.c       |  2 +-
 src/util/bitscan.h                    | 11 +++++++++++
 3 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/src/compiler/nir/nir_search_helpers.h b/src/compiler/nir/nir_search_helpers.h
index 200f247..c8e0c8f 100644
--- a/src/compiler/nir/nir_search_helpers.h
+++ b/src/compiler/nir/nir_search_helpers.h
@@ -28,12 +28,7 @@
 #define _NIR_SEARCH_HELPERS_
 
 #include "nir.h"
-
-static inline bool
-__is_power_of_two(unsigned int x)
-{
-   return ((x != 0) && !(x & (x - 1)));
-}
+#include "util/bitscan.h"
 
 static inline bool
 is_pos_power_of_two(nir_alu_instr *instr, unsigned src, unsigned num_components,
@@ -50,11 +45,11 @@ is_pos_power_of_two(nir_alu_instr *instr, unsigned src, unsigned num_components,
       case nir_type_int:
          if (val->i32[swizzle[i]] < 0)
             return false;
-         if (!__is_power_of_two(val->i32[swizzle[i]]))
+         if (!util_is_power_of_two_nonzero(val->i32[swizzle[i]]))
             return false;
          break;
       case nir_type_uint:
-         if (!__is_power_of_two(val->u32[swizzle[i]]))
+         if (!util_is_power_of_two_nonzero(val->u32[swizzle[i]]))
             return false;
          break;
       default:
@@ -80,7 +75,7 @@ is_neg_power_of_two(nir_alu_instr *instr, unsigned src, unsigned num_components,
       case nir_type_int:
          if (val->i32[swizzle[i]] > 0)
             return false;
-         if (!__is_power_of_two(abs(val->i32[swizzle[i]])))
+         if (!util_is_power_of_two_nonzero(abs(val->i32[swizzle[i]])))
             return false;
          break;
       default:
diff --git a/src/compiler/nir/nir_validate.c b/src/compiler/nir/nir_validate.c
index 9bf8c70..44b92aa 100644
--- a/src/compiler/nir/nir_validate.c
+++ b/src/compiler/nir/nir_validate.c
@@ -980,7 +980,7 @@ validate_var_decl(nir_variable *var, bool is_global, validate_state *state)
    validate_assert(state, is_global == nir_variable_is_global(var));
 
    /* Must have exactly one mode set */
-   validate_assert(state, util_bitcount(var->data.mode) == 1);
+   validate_assert(state, util_is_power_of_two_nonzero(var->data.mode));
 
    if (var->data.compact) {
       /* The "compact" flag is only valid on arrays of scalars. */
diff --git a/src/util/bitscan.h b/src/util/bitscan.h
index 2d4e46e..a3f2d41 100644
--- a/src/util/bitscan.h
+++ b/src/util/bitscan.h
@@ -119,6 +119,17 @@ util_is_power_of_two_or_zero(unsigned v)
    return (v & (v - 1)) == 0;
 }
 
+/* Determine if an unsigned value is a power of two.
+ *
+ * \note
+ * Zero is \b not treated as a power of two.
+ */
+static inline bool
+util_is_power_of_two_nonzero(unsigned v)
+{
+   return v != 0 && (v & (v - 1)) == 0;
+}
+
 /* For looping over a bitmask when you want to loop over consecutive bits
  * manually, for example:
  *
-- 
2.9.5



More information about the mesa-dev mailing list