[Mesa-dev] [PATCH 05/14] glsl: Add constant evaluation of bit built-ins.

Chris Forbes chrisf at ijw.co.nz
Sun Apr 28 16:15:02 PDT 2013


For the series:

Reviewed-by: Chris Forbes <chrisf at ijw.co.nz>

On Mon, Apr 29, 2013 at 10:29 AM, Matt Turner <mattst88 at gmail.com> wrote:
> v2: Order bits from LSB end (31 - count) for ir_unop_find_msb.
> ---
>  src/glsl/ir_constant_expression.cpp | 123 ++++++++++++++++++++++++++++++++++++
>  1 file changed, 123 insertions(+)
>
> diff --git a/src/glsl/ir_constant_expression.cpp b/src/glsl/ir_constant_expression.cpp
> index c09e56a..66b0a64 100644
> --- a/src/glsl/ir_constant_expression.cpp
> +++ b/src/glsl/ir_constant_expression.cpp
> @@ -1248,6 +1248,102 @@ ir_expression::constant_expression_value(struct hash_table *variable_context)
>        }
>        break;
>
> +   case ir_unop_bitfield_reverse:
> +      /* http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious */
> +      for (unsigned c = 0; c < components; c++) {
> +         unsigned int v = op[0]->value.u[c]; // input bits to be reversed
> +         unsigned int r = v; // r will be reversed bits of v; first get LSB of v
> +         int s = sizeof(v) * CHAR_BIT - 1; // extra shift needed at end
> +
> +         for (v >>= 1; v; v >>= 1) {
> +            r <<= 1;
> +            r |= v & 1;
> +            s--;
> +         }
> +         r <<= s; // shift when v's highest bits are zero
> +
> +         data.u[c] = r;
> +      }
> +      break;
> +
> +   case ir_unop_bit_count:
> +      for (unsigned c = 0; c < components; c++) {
> +         unsigned count = 0;
> +         unsigned v = op[0]->value.u[c];
> +
> +         for (; v; count++) {
> +            v &= v - 1;
> +         }
> +         data.u[c] = count;
> +      }
> +      break;
> +
> +   case ir_unop_find_msb:
> +      for (unsigned c = 0; c < components; c++) {
> +         int v = op[0]->value.i[c];
> +
> +         if (v == 0 || (op[0]->type->base_type == GLSL_TYPE_INT && v == -1))
> +            data.i[c] = -1;
> +         else {
> +            int count = 0;
> +            int top_bit = op[0]->type->base_type == GLSL_TYPE_UINT
> +                          ? 0 : v & (1 << 31);
> +
> +            while (((v & (1 << 31)) == top_bit) && count != 32) {
> +               count++;
> +               v <<= 1;
> +            }
> +
> +            data.i[c] = 31 - count;
> +         }
> +      }
> +      break;
> +
> +   case ir_unop_find_lsb:
> +      for (unsigned c = 0; c < components; c++) {
> +         if (op[0]->value.i[c] == 0)
> +            data.i[c] = -1;
> +         else {
> +            unsigned pos = 0;
> +            unsigned v = op[0]->value.u[c];
> +
> +            for (; !(v & 1); v >>= 1) {
> +               pos++;
> +            }
> +            data.u[c] = pos;
> +         }
> +      }
> +      break;
> +
> +   case ir_triop_bitfield_extract: {
> +      int offset = op[1]->value.i[0];
> +      int bits = op[2]->value.i[0];
> +
> +      for (unsigned c = 0; c < components; c++) {
> +         if (bits == 0)
> +            data.u[c] = 0;
> +         else if (offset < 0 || bits < 0)
> +            data.u[c] = 0; /* Undefined, per spec. */
> +         else if (offset + bits > 32)
> +            data.u[c] = 0; /* Undefined, per spec. */
> +         else {
> +            if (op[0]->type->base_type == GLSL_TYPE_INT) {
> +               /* int so that the right shift will sign-extend. */
> +               int value = op[0]->value.i[c];
> +               value <<= 32 - bits - offset;
> +               value >>= 32 - bits;
> +               data.i[c] = value;
> +            } else {
> +               unsigned value = op[0]->value.u[c];
> +               value <<= 32 - bits - offset;
> +               value >>= 32 - bits;
> +               data.u[c] = value;
> +            }
> +         }
> +      }
> +      break;
> +   }
> +
>     case ir_triop_lrp: {
>        assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
>        assert(op[1]->type->base_type == GLSL_TYPE_FLOAT);
> @@ -1261,6 +1357,33 @@ ir_expression::constant_expression_value(struct hash_table *variable_context)
>        break;
>     }
>
> +   case ir_quadop_bitfield_insert: {
> +      int offset = op[2]->value.i[0];
> +      int bits = op[3]->value.i[0];
> +
> +      for (unsigned c = 0; c < components; c++) {
> +         if (bits == 0)
> +            data.u[c] = op[0]->value.u[c];
> +         else if (offset < 0 || bits < 0)
> +            data.u[c] = 0; /* Undefined, per spec. */
> +         else if (offset + bits > 32)
> +            data.u[c] = 0; /* Undefined, per spec. */
> +         else {
> +            unsigned insert_mask = ((1 << bits) - 1) << offset;
> +
> +            unsigned insert = op[1]->value.u[c];
> +            insert <<= offset;
> +            insert &= insert_mask;
> +
> +            unsigned base = op[0]->value.u[c];
> +            base &= ~insert_mask;
> +
> +            data.u[c] = base | insert;
> +         }
> +      }
> +      break;
> +   }
> +
>     case ir_quadop_vector:
>        for (unsigned c = 0; c < this->type->vector_elements; c++) {
>          switch (this->type->base_type) {
> --
> 1.8.1.5
>


More information about the mesa-dev mailing list