llvmpipe: Cleanup constant helpers.
authorJosé Fonseca <jfonseca@vmware.com>
Fri, 7 Aug 2009 00:18:49 +0000 (01:18 +0100)
committerJosé Fonseca <jfonseca@vmware.com>
Sat, 29 Aug 2009 08:21:23 +0000 (09:21 +0100)
src/gallium/drivers/llvmpipe/lp_bld_const.c
src/gallium/drivers/llvmpipe/lp_bld_const.h
src/gallium/drivers/llvmpipe/lp_bld_swizzle.c
src/gallium/drivers/llvmpipe/lp_bld_type.c
src/gallium/drivers/llvmpipe/lp_bld_type.h

index fe1c627eeebadfb7fe0a88268676202c6707154e..d36a61023426c93508cc260c10022431c4fd8441 100644 (file)
 #include "lp_bld_const.h"
 
 
+/**
+ * Shift of the unity.
+ *
+ * Same as lp_const_scale(), but in terms of shifts.
+ */
+unsigned
+lp_const_shift(union lp_type type)
+{
+   if(type.fixed)
+      return type.width/2;
+   else if(type.norm)
+      return type.sign ? type.width - 1 : type.width;
+   else
+      return 0;
+}
+
+
+static unsigned
+lp_const_offset(union lp_type type)
+{
+   if(type.floating || type.fixed)
+      return 0;
+   else if(type.norm)
+      return 1;
+   else
+      return 0;
+}
+
+
+/**
+ * Scaling factor between the LLVM native value and its interpretation.
+ *
+ * This is 1.0 for all floating types and unnormalized integers, and something
+ * else for the fixed points types and normalized integers.
+ */
+double
+lp_const_scale(union lp_type type)
+{
+   unsigned long long llscale;
+   double dscale;
+
+   llscale = (unsigned long long)1 << lp_const_shift(type);
+   llscale -= lp_const_offset(type);
+   dscale = (double)llscale;
+   assert((unsigned long long)dscale == llscale);
+
+   return dscale;
+}
+
+
 LLVMValueRef
 lp_build_undef(union lp_type type)
 {
@@ -92,6 +142,49 @@ lp_build_one(union lp_type type)
 }
                
 
+LLVMValueRef
+lp_build_const_uni(union lp_type type,
+                   double val)
+{
+   LLVMTypeRef elem_type = lp_build_elem_type(type);
+   LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
+   unsigned i;
+
+   assert(type.length <= LP_MAX_VECTOR_LENGTH);
+
+   if(type.floating) {
+      elems[0] = LLVMConstReal(elem_type, val);
+   }
+   else {
+      double dscale = lp_const_scale(type);
+
+      elems[0] = LLVMConstInt(elem_type, val*dscale + 0.5, 0);
+   }
+
+   for(i = 1; i < type.length; ++i)
+      elems[i] = elems[0];
+
+   return LLVMConstVector(elems, type.length);
+}
+
+
+LLVMValueRef
+lp_build_int_const_uni(union lp_type type,
+                       long long val)
+{
+   LLVMTypeRef elem_type = lp_build_int_elem_type(type);
+   LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
+   unsigned i;
+
+   assert(type.length <= LP_MAX_VECTOR_LENGTH);
+
+   for(i = 0; i < type.length; ++i)
+      elems[i] = LLVMConstInt(elem_type, val, type.sign ? 1 : 0);
+
+   return LLVMConstVector(elems, type.length);
+}
+
+
 LLVMValueRef
 lp_build_const_aos(union lp_type type, 
                    double r, double g, double b, double a, 
@@ -117,20 +210,7 @@ lp_build_const_aos(union lp_type type,
       elems[swizzle[3]] = LLVMConstReal(elem_type, a);
    }
    else {
-      unsigned shift;
-      long long llscale;
-      double dscale;
-
-      if(type.fixed)
-         shift = type.width/2;
-      else if(type.norm)
-         shift = type.sign ? type.width - 1 : type.width;
-      else
-         shift = 0;
-
-      llscale = (long long)1 << shift;
-      dscale = (double)llscale;
-      assert((long long)dscale == llscale);
+      double dscale = lp_const_scale(type);
 
       elems[swizzle[0]] = LLVMConstInt(elem_type, r*dscale + 0.5, 0);
       elems[swizzle[1]] = LLVMConstInt(elem_type, g*dscale + 0.5, 0);
@@ -145,23 +225,6 @@ lp_build_const_aos(union lp_type type,
 }
 
 
-LLVMValueRef
-lp_build_const_shift(union lp_type type,
-                     int c)
-{
-   LLVMTypeRef elem_type = LLVMIntType(type.width);
-   LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
-   unsigned i;
-
-   assert(type.length <= LP_MAX_VECTOR_LENGTH);
-
-   for(i = 0; i < type.length; ++i)
-      elems[i] = LLVMConstInt(elem_type, c, 0);
-
-   return LLVMConstVector(elems, type.length);
-}
-
-
 LLVMValueRef
 lp_build_const_mask_aos(union lp_type type,
                         boolean cond[4])
index 98ed8911a554b0c960a99da0092a918a50193f49..ed6b185fceb4586bbcc2bcdd27492ee7785eabc5 100644 (file)
 
 #include <llvm-c/Core.h>  
 
+#include <pipe/p_compiler.h>
+
 
 union lp_type type;
 
 
+unsigned
+lp_const_shift(union lp_type type);
+
+
+double
+lp_const_scale(union lp_type type);
+
+
 LLVMValueRef
 lp_build_undef(union lp_type type);
 
@@ -56,14 +66,19 @@ lp_build_one(union lp_type type);
 
 
 LLVMValueRef
-lp_build_const_aos(union lp_type type, 
-                   double r, double g, double b, double a, 
-                   const unsigned char *swizzle);
+lp_build_const_uni(union lp_type type,
+                   double val);
+
+
+LLVMValueRef
+lp_build_int_const_uni(union lp_type type,
+                       long long val);
 
 
 LLVMValueRef
-lp_build_const_shift(union lp_type type,
-                     int c);
+lp_build_const_aos(union lp_type type, 
+                   double r, double g, double b, double a, 
+                   const unsigned char *swizzle);
 
 
 LLVMValueRef
index 8cda4a48ba35b3e7c49651fa9c7c5f4a3721fde0..1f83bf67828cb837757b837a3367b8d5f34f9b41 100644 (file)
@@ -99,9 +99,9 @@ lp_build_broadcast_aos(struct lp_build_context *bld,
 #endif
 
          if(shift > 0)
-            tmp = LLVMBuildLShr(bld->builder, a, lp_build_const_shift(type4, shift*type.width), "");
+            tmp = LLVMBuildLShr(bld->builder, a, lp_build_int_const_uni(type4, shift*type.width), "");
          if(shift < 0)
-            tmp = LLVMBuildShl(bld->builder, a, lp_build_const_shift(type4, -shift*type.width), "");
+            tmp = LLVMBuildShl(bld->builder, a, lp_build_int_const_uni(type4, -shift*type.width), "");
 
          assert(tmp);
          if(tmp)
index e2abd04f608994c32c9391db6252f5603b6b9334..1e95a1a349d90bffec6b737ebc9da65c59ce8927 100644 (file)
@@ -37,7 +37,7 @@ lp_build_elem_type(union lp_type type)
    if (type.floating) {
       assert(type.sign);
       switch(type.width) {
-         case 32:
+      case 32:
          return LLVMFloatType();
          break;
       case 64:
@@ -63,7 +63,7 @@ lp_build_vec_type(union lp_type type)
 
 
 /**
- * This function is a mirrot of lp_build_elem_type() above.
+ * This function is a mirror of lp_build_elem_type() above.
  *
  * XXX: I'm not sure if it wouldn't be easier/efficient to just recreate the
  * type and check for identity.
@@ -140,3 +140,18 @@ lp_check_value(union lp_type type, LLVMValueRef val)
 
    return lp_check_vec_type(type, vec_type);
 }
+
+
+LLVMTypeRef
+lp_build_int_elem_type(union lp_type type)
+{
+   return LLVMIntType(type.width);
+}
+
+
+LLVMTypeRef
+lp_build_int_vec_type(union lp_type type)
+{
+   LLVMTypeRef elem_type = lp_build_int_elem_type(type);
+   return LLVMVectorType(elem_type, type.length);
+}
index 0eeaf7b6aa91e1ad1d91391f989099fff3102c87..566a86ed0656590e76a60c59c2643c5745f0fc38 100644 (file)
@@ -154,4 +154,12 @@ boolean
 lp_check_value(union lp_type type, LLVMValueRef val);
 
 
+LLVMTypeRef
+lp_build_int_elem_type(union lp_type type);
+
+
+LLVMTypeRef
+lp_build_int_vec_type(union lp_type type);
+
+
 #endif /* !LP_BLD_TYPE_H */