gallivm,draw,llvmpipe: Support wider native registers.
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_const.c
index e42ff31ac7aaf08204c8d70bfe42a9b35f41c665..35799a1ef8e431a13ba6c7ef8e094a7e826d5d9b 100644 (file)
 #include <float.h>
 
 #include "util/u_debug.h"
+#include "util/u_math.h"
+#include "util/u_half.h"
 
 #include "lp_bld_type.h"
 #include "lp_bld_const.h"
+#include "lp_bld_init.h"
 
 
 unsigned
@@ -48,10 +51,12 @@ lp_mantissa(struct lp_type type)
 
    if(type.floating) {
       switch(type.width) {
+      case 16:
+         return 10;
       case 32:
          return 23;
       case 64:
-         return 53;
+         return 52;
       default:
          assert(0);
          return 0;
@@ -134,6 +139,8 @@ lp_const_min(struct lp_type type)
 
    if (type.floating) {
       switch(type.width) {
+      case 16:
+         return -65504;
       case 32:
          return -FLT_MAX;
       case 64:
@@ -167,6 +174,8 @@ lp_const_max(struct lp_type type)
 
    if (type.floating) {
       switch(type.width) {
+      case 16:
+         return 65504;
       case 32:
          return FLT_MAX;
       case 64:
@@ -194,6 +203,8 @@ lp_const_eps(struct lp_type type)
 {
    if (type.floating) {
       switch(type.width) {
+      case 16:
+         return 2E-10;
       case 32:
          return FLT_EPSILON;
       case 64:
@@ -211,31 +222,31 @@ lp_const_eps(struct lp_type type)
 
 
 LLVMValueRef
-lp_build_undef(struct lp_type type)
+lp_build_undef(struct gallivm_state *gallivm, struct lp_type type)
 {
-   LLVMTypeRef vec_type = lp_build_vec_type(type);
+   LLVMTypeRef vec_type = lp_build_vec_type(gallivm, type);
    return LLVMGetUndef(vec_type);
 }
                
 
 LLVMValueRef
-lp_build_zero(struct lp_type type)
+lp_build_zero(struct gallivm_state *gallivm, struct lp_type type)
 {
    if (type.length == 1) {
       if (type.floating)
-         return LLVMConstReal(LLVMFloatType(), 0.0);
+         return lp_build_const_float(gallivm, 0.0);
       else
-         return LLVMConstInt(LLVMIntType(type.width), 0, 0);
+         return LLVMConstInt(LLVMIntTypeInContext(gallivm->context, type.width), 0, 0);
    }
    else {
-      LLVMTypeRef vec_type = lp_build_vec_type(type);
+      LLVMTypeRef vec_type = lp_build_vec_type(gallivm, type);
       return LLVMConstNull(vec_type);
    }
 }
                
 
 LLVMValueRef
-lp_build_one(struct lp_type type)
+lp_build_one(struct gallivm_state *gallivm, struct lp_type type)
 {
    LLVMTypeRef elem_type;
    LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
@@ -243,9 +254,11 @@ lp_build_one(struct lp_type type)
 
    assert(type.length <= LP_MAX_VECTOR_LENGTH);
 
-   elem_type = lp_build_elem_type(type);
+   elem_type = lp_build_elem_type(gallivm, type);
 
-   if(type.floating)
+   if(type.floating && type.width == 16)
+      elems[0] = LLVMConstInt(elem_type, util_float_to_half(1.0f), 0);
+   else if(type.floating)
       elems[0] = LLVMConstReal(elem_type, 1.0);
    else if(type.fixed)
       elems[0] = LLVMConstInt(elem_type, 1LL << (type.width/2), 0);
@@ -283,19 +296,22 @@ lp_build_one(struct lp_type type)
  * Build constant-valued element from a scalar value.
  */
 LLVMValueRef
-lp_build_const_elem(struct lp_type type,
+lp_build_const_elem(struct gallivm_state *gallivm,
+                    struct lp_type type,
                     double val)
 {
-   LLVMTypeRef elem_type = lp_build_elem_type(type);
+   LLVMTypeRef elem_type = lp_build_elem_type(gallivm, type);
    LLVMValueRef elem;
 
-   if(type.floating) {
+   if(type.floating && type.width == 16) {
+      elem = LLVMConstInt(elem_type, util_float_to_half((float)val), 0);
+   } else if(type.floating) {
       elem = LLVMConstReal(elem_type, val);
    }
    else {
       double dscale = lp_const_scale(type);
 
-      elem = LLVMConstInt(elem_type, val*dscale + 0.5, 0);
+      elem = LLVMConstInt(elem_type, round(val*dscale), 0);
    }
 
    return elem;
@@ -306,15 +322,15 @@ lp_build_const_elem(struct lp_type type,
  * Build constant-valued vector from a scalar value.
  */
 LLVMValueRef
-lp_build_const_vec(struct lp_type type,
+lp_build_const_vec(struct gallivm_state *gallivm, struct lp_type type,
                    double val)
 {
    if (type.length == 1) {
-      return lp_build_const_elem(type, val);
+      return lp_build_const_elem(gallivm, type, val);
    } else {
       LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
       unsigned i;
-      elems[0] = lp_build_const_elem(type, val);
+      elems[0] = lp_build_const_elem(gallivm, type, val);
       for(i = 1; i < type.length; ++i)
          elems[i] = elems[0];
       return LLVMConstVector(elems, type.length);
@@ -323,10 +339,10 @@ lp_build_const_vec(struct lp_type type,
 
 
 LLVMValueRef
-lp_build_const_int_vec(struct lp_type type,
-                          long long val)
+lp_build_const_int_vec(struct gallivm_state *gallivm, struct lp_type type,
+                       long long val)
 {
-   LLVMTypeRef elem_type = lp_build_int_elem_type(type);
+   LLVMTypeRef elem_type = lp_build_int_elem_type(gallivm, type);
    LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
    unsigned i;
 
@@ -343,7 +359,8 @@ lp_build_const_int_vec(struct lp_type type,
 
 
 LLVMValueRef
-lp_build_const_aos(struct lp_type type, 
+lp_build_const_aos(struct gallivm_state *gallivm,
+                   struct lp_type type, 
                    double r, double g, double b, double a, 
                    const unsigned char *swizzle)
 {
@@ -355,25 +372,15 @@ lp_build_const_aos(struct lp_type type,
    assert(type.length % 4 == 0);
    assert(type.length <= LP_MAX_VECTOR_LENGTH);
 
-   elem_type = lp_build_elem_type(type);
+   elem_type = lp_build_elem_type(gallivm, type);
 
    if(swizzle == NULL)
       swizzle = default_swizzle;
 
-   if(type.floating) {
-      elems[swizzle[0]] = LLVMConstReal(elem_type, r);
-      elems[swizzle[1]] = LLVMConstReal(elem_type, g);
-      elems[swizzle[2]] = LLVMConstReal(elem_type, b);
-      elems[swizzle[3]] = LLVMConstReal(elem_type, a);
-   }
-   else {
-      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);
-      elems[swizzle[2]] = LLVMConstInt(elem_type, b*dscale + 0.5, 0);
-      elems[swizzle[3]] = LLVMConstInt(elem_type, a*dscale + 0.5, 0);
-   }
+   elems[swizzle[0]] = lp_build_const_elem(gallivm, type, r);
+   elems[swizzle[1]] = lp_build_const_elem(gallivm, type, g);
+   elems[swizzle[2]] = lp_build_const_elem(gallivm, type, b);
+   elems[swizzle[3]] = lp_build_const_elem(gallivm, type, a);
 
    for(i = 4; i < type.length; ++i)
       elems[i] = elems[i % 4];
@@ -382,19 +389,93 @@ 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])
+lp_build_const_mask_aos(struct gallivm_state *gallivm,
+                        struct lp_type type,
+                        unsigned mask)
 {
-   LLVMTypeRef elem_type = LLVMIntType(type.width);
+   LLVMTypeRef elem_type = LLVMIntTypeInContext(gallivm->context, type.width);
    LLVMValueRef masks[LP_MAX_VECTOR_LENGTH];
    unsigned i, j;
 
    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);
 }
+
+
+/**
+ * Performs lp_build_const_mask_aos, but first swizzles the mask
+ */
+LLVMValueRef
+lp_build_const_mask_aos_swizzled(struct gallivm_state *gallivm,
+                        struct lp_type type,
+                        unsigned mask,
+                        const unsigned char *swizzle)
+{
+   mask =
+           ((mask & (1 << swizzle[0])) >> swizzle[0])
+        | (((mask & (1 << swizzle[1])) >> swizzle[1]) << 1)
+        | (((mask & (1 << swizzle[2])) >> swizzle[2]) << 2)
+        | (((mask & (1 << swizzle[3])) >> swizzle[3]) << 3);
+
+   return lp_build_const_mask_aos(gallivm, type, mask);
+}
+
+
+/**
+ * Build a zero-terminated constant string.
+ */
+LLVMValueRef
+lp_build_const_string(struct gallivm_state *gallivm,
+                      const char *str)
+{
+   unsigned len = strlen(str) + 1;
+   LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
+   LLVMValueRef string = LLVMAddGlobal(gallivm->module, LLVMArrayType(i8, len), "");
+   LLVMSetGlobalConstant(string, TRUE);
+   LLVMSetLinkage(string, LLVMInternalLinkage);
+   LLVMSetInitializer(string, LLVMConstStringInContext(gallivm->context, str, len, TRUE));
+   string = LLVMConstBitCast(string, LLVMPointerType(i8, 0));
+   return string;
+}
+
+
+/**
+ * Build a callable function pointer.
+ *
+ * We use function pointer constants instead of LLVMAddGlobalMapping()
+ * to work around a bug in LLVM 2.6, and for efficiency/simplicity.
+ */
+LLVMValueRef
+lp_build_const_func_pointer(struct gallivm_state *gallivm,
+                            const void *ptr,
+                            LLVMTypeRef ret_type,
+                            LLVMTypeRef *arg_types,
+                            unsigned num_args,
+                            const char *name)
+{
+   LLVMTypeRef function_type;
+   LLVMValueRef function;
+
+   function_type = LLVMFunctionType(ret_type, arg_types, num_args, 0);
+
+   function = lp_build_const_int_pointer(gallivm, ptr);
+
+   function = LLVMBuildBitCast(gallivm->builder, function,
+                               LLVMPointerType(function_type, 0),
+                               name);
+
+   return function;
+}