util: remove util_is_pot in favor of util_is_power_of_two
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_const.c
index 53447757e8e23ba6f24a0c089af4761983f52cc1..e42ff31ac7aaf08204c8d70bfe42a9b35f41c665 100644 (file)
@@ -221,8 +221,16 @@ lp_build_undef(struct lp_type type)
 LLVMValueRef
 lp_build_zero(struct lp_type type)
 {
-   LLVMTypeRef vec_type = lp_build_vec_type(type);
-   return LLVMConstNull(vec_type);
+   if (type.length == 1) {
+      if (type.floating)
+         return LLVMConstReal(LLVMFloatType(), 0.0);
+      else
+         return LLVMConstInt(LLVMIntType(type.width), 0, 0);
+   }
+   else {
+      LLVMTypeRef vec_type = lp_build_vec_type(type);
+      return LLVMConstNull(vec_type);
+   }
 }
                
 
@@ -255,7 +263,7 @@ lp_build_one(struct lp_type type)
       if(type.sign)
          /* TODO: Unfortunately this caused "Tried to create a shift operation
           * on a non-integer type!" */
-         vec = LLVMConstLShr(vec, lp_build_int_const_scalar(type, 1));
+         vec = LLVMConstLShr(vec, lp_build_const_int_vec(type, 1));
 #endif
 
       return vec;
@@ -272,36 +280,50 @@ lp_build_one(struct lp_type type)
                
 
 /**
- * Build constant-valued vector from a scalar value.
+ * Build constant-valued element from a scalar value.
  */
 LLVMValueRef
-lp_build_const_scalar(struct lp_type type,
-                      double val)
+lp_build_const_elem(struct 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);
+   LLVMValueRef elem;
 
    if(type.floating) {
-      elems[0] = LLVMConstReal(elem_type, val);
+      elem = LLVMConstReal(elem_type, val);
    }
    else {
       double dscale = lp_const_scale(type);
 
-      elems[0] = LLVMConstInt(elem_type, val*dscale + 0.5, 0);
+      elem = LLVMConstInt(elem_type, val*dscale + 0.5, 0);
    }
 
-   for(i = 1; i < type.length; ++i)
-      elems[i] = elems[0];
+   return elem;
+}
 
-   return LLVMConstVector(elems, type.length);
+
+/**
+ * Build constant-valued vector from a scalar value.
+ */
+LLVMValueRef
+lp_build_const_vec(struct lp_type type,
+                   double val)
+{
+   if (type.length == 1) {
+      return lp_build_const_elem(type, val);
+   } else {
+      LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
+      unsigned i;
+      elems[0] = lp_build_const_elem(type, val);
+      for(i = 1; i < type.length; ++i)
+         elems[i] = elems[0];
+      return LLVMConstVector(elems, type.length);
+   }
 }
 
 
 LLVMValueRef
-lp_build_int_const_scalar(struct lp_type type,
+lp_build_const_int_vec(struct lp_type type,
                           long long val)
 {
    LLVMTypeRef elem_type = lp_build_int_elem_type(type);
@@ -313,6 +335,9 @@ lp_build_int_const_scalar(struct lp_type type,
    for(i = 0; i < type.length; ++i)
       elems[i] = LLVMConstInt(elem_type, val, type.sign ? 1 : 0);
 
+   if (type.length == 1)
+      return elems[0];
+
    return LLVMConstVector(elems, type.length);
 }