add isl_aff_add_coefficient_val
authorSven Verdoolaege <skimo@kotnet.org>
Sat, 23 Feb 2013 14:43:50 +0000 (15:43 +0100)
committerSven Verdoolaege <skimo@kotnet.org>
Tue, 28 May 2013 16:24:48 +0000 (18:24 +0200)
Signed-off-by: Sven Verdoolaege <skimo@kotnet.org>
doc/user.pod
include/isl/aff.h
isl_aff.c

index a35ae3f..41350f0 100644 (file)
@@ -3700,6 +3700,10 @@ It can be modified using
        __isl_give isl_aff *isl_aff_add_coefficient_si(
                __isl_take isl_aff *aff,
                enum isl_dim_type type, int pos, int v);
+       __isl_give isl_aff *isl_aff_add_coefficient_val(
+               __isl_take isl_aff *aff,
+               enum isl_dim_type type, int pos,
+               __isl_take isl_val *v);
 
        __isl_give isl_aff *isl_aff_insert_dims(
                __isl_take isl_aff *aff,
index 184ab0f..13a541a 100644 (file)
@@ -62,6 +62,8 @@ __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
        enum isl_dim_type type, int pos, isl_int v);
 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
        enum isl_dim_type type, int pos, int v);
+__isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
+       enum isl_dim_type type, int pos, __isl_take isl_val *v);
 
 int isl_aff_is_cst(__isl_keep isl_aff *aff);
 
index b0a3fa0..3210032 100644 (file)
--- a/isl_aff.c
+++ b/isl_aff.c
@@ -802,6 +802,69 @@ __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
        return aff;
 }
 
+/* Add "v" to the coefficient of the variable of type "type"
+ * at position "pos" of "aff".
+ */
+__isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
+       enum isl_dim_type type, int pos, __isl_take isl_val *v)
+{
+       if (!aff || !v)
+               goto error;
+
+       if (isl_val_is_zero(v)) {
+               isl_val_free(v);
+               return aff;
+       }
+
+       if (type == isl_dim_out)
+               isl_die(aff->v->ctx, isl_error_invalid,
+                       "output/set dimension does not have a coefficient",
+                       goto error);
+       if (type == isl_dim_in)
+               type = isl_dim_set;
+
+       if (pos >= isl_local_space_dim(aff->ls, type))
+               isl_die(aff->v->ctx, isl_error_invalid,
+                       "position out of bounds", goto error);
+
+       if (!isl_val_is_rat(v))
+               isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
+                       "expecting rational value", goto error);
+
+       aff = isl_aff_cow(aff);
+       if (!aff)
+               goto error;
+
+       aff->v = isl_vec_cow(aff->v);
+       if (!aff->v)
+               goto error;
+
+       pos += isl_local_space_offset(aff->ls, type);
+       if (isl_int_is_one(v->d)) {
+               isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
+       } else if (isl_int_eq(aff->v->el[0], v->d)) {
+               isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
+               aff->v = isl_vec_normalize(aff->v);
+               if (!aff->v)
+                       goto error;
+       } else {
+               isl_seq_scale(aff->v->el + 1,
+                               aff->v->el + 1, v->d, aff->v->size - 1);
+               isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
+               isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
+               aff->v = isl_vec_normalize(aff->v);
+               if (!aff->v)
+                       goto error;
+       }
+
+       isl_val_free(v);
+       return aff;
+error:
+       isl_aff_free(aff);
+       isl_val_free(v);
+       return NULL;
+}
+
 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
        enum isl_dim_type type, int pos, int v)
 {