add isl_aff_set_coefficient_val
authorSven Verdoolaege <skimo@kotnet.org>
Tue, 9 Apr 2013 16:33:48 +0000 (18:33 +0200)
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 6410eec..191e4f0 100644 (file)
@@ -3677,6 +3677,10 @@ It can be modified using
        __isl_give isl_aff *isl_aff_set_coefficient_si(
                __isl_take isl_aff *aff,
                enum isl_dim_type type, int pos, int v);
+       __isl_give isl_aff *isl_aff_set_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_set_denominator(
                __isl_take isl_aff *aff, isl_int v);
 
@@ -3717,7 +3721,8 @@ It can be modified using
 Note that C<isl_aff_set_constant>, C<isl_aff_set_constant_si>,
 C<isl_aff_set_coefficient> and C<isl_aff_set_coefficient_si>
 set the I<numerator> of the constant or coefficient, while
-C<isl_aff_set_constant_val> sets the constant as a whole.
+C<isl_aff_set_constant_val> and C<isl_aff_set_coefficient_val> set
+the constant or coefficient as a whole.
 C<add_constant> and C<add_coefficient> add an integer value to
 the possibly rational constant or coefficient.
 The C<add_constant_num> functions add an integer value to
index f76bfb4..c09d457 100644 (file)
@@ -48,6 +48,8 @@ __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
        enum isl_dim_type type, int pos, isl_int v);
 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
        enum isl_dim_type type, int pos, int v);
+__isl_give isl_aff *isl_aff_set_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_set_denominator(__isl_take isl_aff *aff, isl_int v);
 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v);
 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v);
index a0b780b..b789c73 100644 (file)
--- a/isl_aff.c
+++ b/isl_aff.c
@@ -661,6 +661,66 @@ __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
        return aff;
 }
 
+/* Replace the coefficient of the variable of type "type" at position "pos"
+ * of "aff" by "v".
+ */
+__isl_give isl_aff *isl_aff_set_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 (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);
+
+       pos += isl_local_space_offset(aff->ls, type);
+       if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
+           isl_int_eq(aff->v->el[0], v->d)) {
+               isl_val_free(v);
+               return aff;
+       }
+
+       aff = isl_aff_cow(aff);
+       if (!aff)
+               goto error;
+       aff->v = isl_vec_cow(aff->v);
+       if (!aff->v)
+               goto error;
+
+       if (isl_int_eq(aff->v->el[0], v->d)) {
+               isl_int_set(aff->v->el[1 + pos], v->n);
+       } else if (isl_int_is_one(v->d)) {
+               isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
+       } else {
+               isl_seq_scale(aff->v->el + 1,
+                               aff->v->el + 1, v->d, aff->v->size - 1);
+               isl_int_mul(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(__isl_take isl_aff *aff,
        enum isl_dim_type type, int pos, isl_int v)
 {