util: Add and use util_is_power_of_two_nonzero

Signed-off-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Eduardo Lima Mitev <elima@igalia.com>
This commit is contained in:
Ian Romanick 2017-11-13 13:00:53 -08:00
parent d76c204d05
commit 22fbb5c594
5 changed files with 19 additions and 13 deletions

View File

@ -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:

View 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. */

View 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,

View 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 */

View 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:
*