gallivm: Pass condition masks as an unsigned bitmask.
authorJosé Fonseca <jfonseca@vmware.com>
Thu, 2 Sep 2010 10:32:09 +0000 (11:32 +0100)
committerJosé Fonseca <jfonseca@vmware.com>
Sun, 5 Sep 2010 09:17:51 +0000 (10:17 +0100)
Much more convenient than boolean arrays.

src/gallium/auxiliary/gallivm/lp_bld_const.c
src/gallium/auxiliary/gallivm/lp_bld_const.h
src/gallium/auxiliary/gallivm/lp_bld_logic.c
src/gallium/auxiliary/gallivm/lp_bld_logic.h
src/gallium/auxiliary/gallivm/lp_bld_swizzle.c
src/gallium/drivers/llvmpipe/lp_bld_blend_aos.c

index e42ff31ac7aaf08204c8d70bfe42a9b35f41c665..dd839c0bea56ce5f60bb01974140ef3f74dcef37 100644 (file)
@@ -382,9 +382,12 @@ lp_build_const_aos(struct lp_type type,
 }
 
 
+/**
+ * @param mask TGSI_WRITEMASK_xxx
+ */
 LLVMValueRef
 lp_build_const_mask_aos(struct lp_type type,
-                        const boolean cond[4])
+                        unsigned mask)
 {
    LLVMTypeRef elem_type = LLVMIntType(type.width);
    LLVMValueRef masks[LP_MAX_VECTOR_LENGTH];
@@ -392,9 +395,13 @@ lp_build_const_mask_aos(struct lp_type type,
 
    assert(type.length <= LP_MAX_VECTOR_LENGTH);
 
-   for(j = 0; j < type.length; j += 4)
-      for(i = 0; i < 4; ++i)
-         masks[j + i] = LLVMConstInt(elem_type, cond[i] ? ~0 : 0, 0);
+   for (j = 0; j < type.length; j += 4) {
+      for( i = 0; i < 4; ++i) {
+         masks[j + i] = LLVMConstInt(elem_type,
+                                     mask & (1 << i) ? ~0ULL : 0,
+                                     1);
+      }
+   }
 
    return LLVMConstVector(masks, type.length);
 }
index 7ee8fff1407db33472416a78f203558a046bd39c..6b1fc590c177f8fbfb76186c3a5bb3501b6027d0 100644 (file)
@@ -104,7 +104,7 @@ lp_build_const_aos(struct lp_type type,
 
 LLVMValueRef
 lp_build_const_mask_aos(struct lp_type type,
-                        const boolean cond[4]);
+                        unsigned mask);
 
 
 static INLINE LLVMValueRef
index 7d7db3b0d92d93f1c6bfd390665f64d02192ee62..db259814914fc7aee8fb03e8b64671e2891f2d0a 100644 (file)
@@ -482,24 +482,30 @@ lp_build_select(struct lp_build_context *bld,
 }
 
 
+/**
+ * Return mask ? a : b;
+ *
+ * mask is a TGSI_WRITEMASK_xxx.
+ */
 LLVMValueRef
 lp_build_select_aos(struct lp_build_context *bld,
+                    unsigned mask,
                     LLVMValueRef a,
-                    LLVMValueRef b,
-                    const boolean cond[4])
+                    LLVMValueRef b)
 {
    const struct lp_type type = bld->type;
    const unsigned n = type.length;
    unsigned i, j;
 
+   assert((mask & ~0xf) == 0);
    assert(lp_check_value(type, a));
    assert(lp_check_value(type, b));
 
    if(a == b)
       return a;
-   if(cond[0] && cond[1] && cond[2] && cond[3])
+   if((mask & 0xf) == 0xf)
       return a;
-   if(!cond[0] && !cond[1] && !cond[2] && !cond[3])
+   if((mask & 0xf) == 0x0)
       return b;
    if(a == bld->undef || b == bld->undef)
       return bld->undef;
@@ -522,7 +528,9 @@ lp_build_select_aos(struct lp_build_context *bld,
 
       for(j = 0; j < n; j += 4)
          for(i = 0; i < 4; ++i)
-            shuffles[j + i] = LLVMConstInt(elem_type, (cond[i] ? 0 : n) + j + i, 0);
+            shuffles[j + i] = LLVMConstInt(elem_type,
+                                           (mask & (1 << i) ? 0 : n) + j + i,
+                                           0);
 
       return LLVMBuildShuffleVector(bld->builder, a, b, LLVMConstVector(shuffles, n), "");
    }
@@ -531,16 +539,17 @@ lp_build_select_aos(struct lp_build_context *bld,
       /* XXX: Unfortunately select of vectors do not work */
       /* Use a select */
       LLVMTypeRef elem_type = LLVMInt1Type();
-      LLVMValueRef cond[LP_MAX_VECTOR_LENGTH];
+      LLVMValueRef cond_vec[LP_MAX_VECTOR_LENGTH];
 
       for(j = 0; j < n; j += 4)
          for(i = 0; i < 4; ++i)
-            cond[j + i] = LLVMConstInt(elem_type, cond[i] ? 1 : 0, 0);
+            cond_vec[j + i] = LLVMConstInt(elem_type,
+                                           mask & (1 << i) ? 1 : 0, 0);
 
-      return LLVMBuildSelect(bld->builder, LLVMConstVector(cond, n), a, b, "");
+      return LLVMBuildSelect(bld->builder, LLVMConstVector(cond_vec, n), a, b, "");
 #else
-      LLVMValueRef mask = lp_build_const_mask_aos(type, cond);
-      return lp_build_select(bld, mask, a, b);
+      LLVMValueRef mask_vec = lp_build_const_mask_aos(type, mask);
+      return lp_build_select(bld, mask_vec, a, b);
 #endif
    }
 }
index 4e7b4c9938eb49ab1523a9c9c21880c26417fc3a..111daad9712a8abf4aa8dd2086e4adccc81c7e93 100644 (file)
@@ -77,9 +77,9 @@ lp_build_select(struct lp_build_context *bld,
 
 LLVMValueRef
 lp_build_select_aos(struct lp_build_context *bld,
+                    unsigned mask,
                     LLVMValueRef a,
-                    LLVMValueRef b,
-                    const boolean cond[4]);
+                    LLVMValueRef b);
 
 
 LLVMValueRef
index 20cf96ca66991ed28303aecf1fa47cb1dcd4da65..fced983396c81a950fa5e823a430d88db9bf8f94 100644 (file)
@@ -139,13 +139,10 @@ lp_build_broadcast_aos(struct lp_build_context *bld,
          { 1, -2},
          {-1, -2}
       };
-      boolean cond[4];
       unsigned i;
 
-      memset(cond, 0, sizeof cond);
-      cond[channel] = 1;
-
-      a = LLVMBuildAnd(bld->builder, a, lp_build_const_mask_aos(type, cond), "");
+      a = LLVMBuildAnd(bld->builder, a,
+                       lp_build_const_mask_aos(type, 1 << channel), "");
 
       /*
        * Build a type where each element is an integer that cover the four
@@ -282,7 +279,7 @@ lp_build_swizzle_aos(struct lp_build_context *bld,
        */
       LLVMValueRef res;
       struct lp_type type4;
-      boolean cond[4];
+      unsigned cond = 0;
       unsigned chan;
       int shift;
 
@@ -290,9 +287,11 @@ lp_build_swizzle_aos(struct lp_build_context *bld,
        * Start with a mixture of 1 and 0.
        */
       for (chan = 0; chan < 4; ++chan) {
-         cond[chan] = swizzles[chan] == PIPE_SWIZZLE_ONE ? TRUE : FALSE;
+         if (swizzles[chan] == PIPE_SWIZZLE_ONE) {
+            cond |= 1 << chan;
+         }
       }
-      res = lp_build_select_aos(bld, bld->one, bld->zero, cond);
+      res = lp_build_select_aos(bld, cond, bld->one, bld->zero);
 
       /*
        * Build a type where each element is an integer that cover the four
index 09e983305719de4dca9f92932d2b92089d32de31..e49d353b749fa8cfae1c32800a0923682e7a9832 100644 (file)
@@ -205,9 +205,8 @@ lp_build_blend_swizzle(struct lp_build_blend_aos_context *bld,
    }
 
    if (rgb != alpha) {
-      boolean cond[4] = {0, 0, 0, 0};
-      cond[alpha_swizzle] = 1;
-      swizzled_rgb = lp_build_select_aos(&bld->base, alpha, swizzled_rgb, cond);
+      swizzled_rgb = lp_build_select_aos(&bld->base, 1 << alpha_swizzle,
+                                         alpha, swizzled_rgb);
    }
 
    return swizzled_rgb;