From 6997282c9200a9473191b54941e12f1b366e5cbe Mon Sep 17 00:00:00 2001 From: Sven Verdoolaege Date: Tue, 9 Apr 2013 18:33:48 +0200 Subject: [PATCH] add isl_aff_set_coefficient_val Signed-off-by: Sven Verdoolaege --- doc/user.pod | 7 ++++++- include/isl/aff.h | 2 ++ isl_aff.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/doc/user.pod b/doc/user.pod index 6410eec3..191e4f01 100644 --- a/doc/user.pod +++ b/doc/user.pod @@ -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, C, C and C set the I of the constant or coefficient, while -C sets the constant as a whole. +C and C set +the constant or coefficient as a whole. C and C add an integer value to the possibly rational constant or coefficient. The C functions add an integer value to diff --git a/include/isl/aff.h b/include/isl/aff.h index f76bfb46..c09d4574 100644 --- a/include/isl/aff.h +++ b/include/isl/aff.h @@ -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); diff --git a/isl_aff.c b/isl_aff.c index a0b780bc..b789c73c 100644 --- 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) { -- 2.11.4.GIT