From 83df5261cdcb85fcfd9aebeaaf77f377216dd82e Mon Sep 17 00:00:00 2001 From: Sven Verdoolaege Date: Tue, 16 Apr 2013 15:34:13 +0200 Subject: [PATCH] add isl_basic_set_max_val Signed-off-by: Sven Verdoolaege --- doc/user.pod | 9 ++++++- include/isl/ilp.h | 3 +++ isl_ilp.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/doc/user.pod b/doc/user.pod index 13a4530..b009aef 100644 --- a/doc/user.pod +++ b/doc/user.pod @@ -2576,6 +2576,9 @@ a singleton subset of the input. Otherwise, return an empty set. enum isl_lp_result isl_basic_set_max( __isl_keep isl_basic_set *bset, __isl_keep isl_aff *obj, isl_int *opt) + __isl_give isl_val *isl_basic_set_max_val( + __isl_keep isl_basic_set *bset, + __isl_keep isl_aff *obj); enum isl_lp_result isl_set_min(__isl_keep isl_set *set, __isl_keep isl_aff *obj, isl_int *opt); enum isl_lp_result isl_set_max(__isl_keep isl_set *set, @@ -2584,7 +2587,11 @@ a singleton subset of the input. Otherwise, return an empty set. Compute the minimum or maximum of the integer affine expression C over the points in C, returning the result in C. The return value may be one of C, -C, C or C. +C, C or C, in case of +an C. If the result is an C then +the result is C in case of an error, the optimal value in case +there is one, infinity if the problem is unbounded and +NaN if the problem is empty. =item * Parametric optimization diff --git a/include/isl/ilp.h b/include/isl/ilp.h index 5b7c3bc..f910b1d 100644 --- a/include/isl/ilp.h +++ b/include/isl/ilp.h @@ -12,6 +12,7 @@ #include #include +#include #if defined(__cplusplus) extern "C" { @@ -22,6 +23,8 @@ enum isl_lp_result isl_basic_set_solve_ilp(struct isl_basic_set *bset, int max, struct isl_vec **sol_p); enum isl_lp_result isl_basic_set_max(__isl_keep isl_basic_set *bset, __isl_keep isl_aff *obj, isl_int *opt); +__isl_give isl_val *isl_basic_set_max_val(__isl_keep isl_basic_set *bset, + __isl_keep isl_aff *obj); enum isl_lp_result isl_set_min(__isl_keep isl_set *set, __isl_keep isl_aff *obj, isl_int *opt); enum isl_lp_result isl_set_max(__isl_keep isl_set *set, diff --git a/isl_ilp.c b/isl_ilp.c index e6bc624..1f6b70d 100644 --- a/isl_ilp.c +++ b/isl_ilp.c @@ -16,6 +16,7 @@ #include #include #include +#include /* Given a basic set "bset", construct a basic set U such that for * each element x in U, the whole unit box positioned at x is inside @@ -513,3 +514,72 @@ enum isl_lp_result isl_set_min(__isl_keep isl_set *set, { return isl_set_opt(set, 0, obj, opt); } + +/* Convert the result of a function that returns an isl_lp_result + * to an isl_val. The numerator of "v" is set to the optimal value + * if lp_res is isl_lp_ok. "max" is set if a maximum was computed. + * + * Return "v" with denominator set to 1 if lp_res is isl_lp_ok. + * Return NULL on error. + * Return a NaN if lp_res is isl_lp_empty. + * Return infinity or negative infinity if lp_res is isl_lp_unbounded, + * depending on "max". + */ +static __isl_give isl_val *convert_lp_result(enum isl_lp_result lp_res, + __isl_take isl_val *v, int max) +{ + isl_ctx *ctx; + + if (lp_res == isl_lp_ok) { + isl_int_set_si(v->d, 1); + return isl_val_normalize(v); + } + ctx = isl_val_get_ctx(v); + isl_val_free(v); + if (lp_res == isl_lp_error) + return NULL; + if (lp_res == isl_lp_empty) + return isl_val_nan(ctx); + if (max) + return isl_val_infty(ctx); + else + return isl_val_neginfty(ctx); +} + +/* Return the minimum (maximum if max is set) of the integer affine + * expression "obj" over the points in "bset". + * + * Return infinity or negative infinity if the optimal value is unbounded and + * NaN if "bset" is empty. + * + * Call isl_basic_set_opt and translate the results. + */ +__isl_give isl_val *isl_basic_set_opt_val(__isl_keep isl_basic_set *bset, + int max, __isl_keep isl_aff *obj) +{ + isl_ctx *ctx; + isl_val *res; + enum isl_lp_result lp_res; + + if (!bset || !obj) + return NULL; + + ctx = isl_aff_get_ctx(obj); + res = isl_val_alloc(ctx); + if (!res) + return NULL; + lp_res = isl_basic_set_opt(bset, max, obj, &res->n); + return convert_lp_result(lp_res, res, max); +} + +/* Return the maximum of the integer affine + * expression "obj" over the points in "bset". + * + * Return infinity or negative infinity if the optimal value is unbounded and + * NaN if "bset" is empty. + */ +__isl_give isl_val *isl_basic_set_max_val(__isl_keep isl_basic_set *bset, + __isl_keep isl_aff *obj) +{ + return isl_basic_set_opt_val(bset, 1, obj); +} -- 2.7.4