util: Add and use util_is_power_of_two_nonzero
authorIan Romanick <ian.d.romanick@intel.com>
Mon, 13 Nov 2017 21:00:53 +0000 (13:00 -0800)
committerIan Romanick <ian.d.romanick@intel.com>
Thu, 29 Mar 2018 21:09:28 +0000 (14:09 -0700)
Signed-off-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Eduardo Lima Mitev <elima@igalia.com>
src/compiler/nir/nir_search_helpers.h
src/compiler/nir/nir_validate.c
src/gallium/drivers/etnaviv/etnaviv_blt.c
src/intel/compiler/brw_fs.cpp
src/util/bitscan.h

index 2d399bd5dcdcaa4978abf3681d18397b0e1c9a04..8bc6d723c34bd1ce20e0716e6db8657bbdaa5935 100644 (file)
 #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:
index 725ba43152c7eea4d3aad9f3b97b42b26a3a0dfd..565cb2ef163321a899c1667214c37431b744106c 100644 (file)
@@ -982,7 +982,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. */
index c30c11ab61ee3e1eb194b7bd7721d54a8387d761..52731a9c7707c3121d03f629eafa045fa8aeefec 100644 (file)
@@ -178,7 +178,7 @@ emit_blt_copyimage(struct etna_cmd_stream *stream, const struct blt_imgcopy_op *
 static void
 emit_blt_inplace(struct etna_cmd_stream *stream, const struct blt_inplace_op *op)
 {
-   assert(op->bpp > 0 && util_is_power_of_two_or_zero(op->bpp));
+   assert(op->bpp > 0 && util_is_power_of_two_nonzero(op->bpp));
    etna_cmd_stream_reserve(stream, 64*2); /* Never allow BLT sequences to be broken up */
    etna_set_state(stream, VIVS_BLT_ENABLE, 0x00000001);
    etna_set_state(stream, VIVS_BLT_CONFIG,
index fdcc909a3d5b68a96b91603097c95d1e23bba39c..40896db26b34851696c463fe756607c143fe534c 100644 (file)
@@ -1976,7 +1976,7 @@ struct cplx_align {
 static void
 cplx_align_assert_sane(struct cplx_align a)
 {
-   assert(a.mul > 0 && util_is_power_of_two_or_zero(a.mul));
+   assert(a.mul > 0 && util_is_power_of_two_nonzero(a.mul));
    assert(a.offset < a.mul);
 }
 
@@ -2028,7 +2028,7 @@ static void
 mark_uniform_slots_read(struct uniform_slot_info *slots,
                         unsigned num_slots, unsigned alignment)
 {
-   assert(alignment > 0 && util_is_power_of_two_or_zero(alignment));
+   assert(alignment > 0 && util_is_power_of_two_nonzero(alignment));
    assert(alignment <= CPLX_ALIGN_MAX_MUL);
 
    /* We can't align a slot to anything less than the slot size */
index 2d4e46ec0f1e4a82455e9e819eac004a4eb68677..a3f2d414bd6ec535c0f66d9d15729a2880ac2fbc 100644 (file)
@@ -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:
  *