doc: fix typo
[platform/upstream/isl.git] / isl_ilp.c
index 1994a68..63261bf 100644 (file)
--- a/isl_ilp.c
+++ b/isl_ilp.c
@@ -1,7 +1,7 @@
 /*
  * Copyright 2008-2009 Katholieke Universiteit Leuven
  *
- * Use of this software is governed by the GNU LGPLv2.1 license
+ * Use of this software is governed by the MIT license
  *
  * Written by Sven Verdoolaege, K.U.Leuven, Departement
  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
@@ -16,6 +16,7 @@
 #include <isl_aff_private.h>
 #include <isl_local_space_private.h>
 #include <isl_mat_private.h>
+#include <isl_val_private.h>
 
 /* 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
@@ -42,7 +43,7 @@ static struct isl_basic_set *unit_box_base_points(struct isl_basic_set *bset)
        }
 
        total = isl_basic_set_total_dim(bset);
-       unit_box = isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset),
+       unit_box = isl_basic_set_alloc_space(isl_basic_set_get_space(bset),
                                        0, 0, bset->n_ineq);
 
        for (i = 0; i < bset->n_ineq; ++i) {
@@ -375,12 +376,13 @@ enum isl_lp_result isl_basic_set_opt(__isl_keep isl_basic_set *bset, int max,
        isl_mat *bset_div = NULL;
        isl_mat *div = NULL;
        enum isl_lp_result res;
+       int bset_n_div;
 
        if (!bset || !obj)
                return isl_lp_error;
 
        ctx = isl_aff_get_ctx(obj);
-       if (!isl_dim_equal(bset->dim, obj->ls->dim))
+       if (!isl_space_is_equal(bset->dim, obj->ls->dim))
                isl_die(ctx, isl_error_invalid,
                        "spaces don't match", return isl_lp_error);
        if (!isl_int_is_one(obj->v->el[0]))
@@ -388,14 +390,15 @@ enum isl_lp_result isl_basic_set_opt(__isl_keep isl_basic_set *bset, int max,
                        "expecting integer affine expression",
                        return isl_lp_error);
 
-       if (bset->n_div == 0 && obj->ls->div->n_row == 0)
+       bset_n_div = isl_basic_set_dim(bset, isl_dim_div);
+       if (bset_n_div == 0 && obj->ls->div->n_row == 0)
                return basic_set_opt(bset, max, obj, opt);
 
        bset = isl_basic_set_copy(bset);
        obj = isl_aff_copy(obj);
 
        bset_div = extract_divs(bset);
-       exp1 = isl_alloc_array(ctx, int, bset_div->n_row);
+       exp1 = isl_alloc_array(ctx, int, bset_n_div);
        exp2 = isl_alloc_array(ctx, int, obj->ls->div->n_row);
        if (!bset_div || !exp1 || !exp2)
                goto error;
@@ -427,8 +430,10 @@ error:
 
 /* Compute the minimum (maximum if max is set) of the integer affine
  * expression obj over the points in set and put the result in *opt.
+ *
+ * The parameters are assumed to have been aligned.
  */
-enum isl_lp_result isl_set_opt(__isl_keep isl_set *set, int max,
+static enum isl_lp_result isl_set_opt_aligned(__isl_keep isl_set *set, int max,
        __isl_keep isl_aff *obj, isl_int *opt)
 {
        int i;
@@ -466,6 +471,34 @@ enum isl_lp_result isl_set_opt(__isl_keep isl_set *set, int max,
        return empty ? isl_lp_empty : isl_lp_ok;
 }
 
+/* Compute the minimum (maximum if max is set) of the integer affine
+ * expression obj over the points in set and put the result in *opt.
+ */
+enum isl_lp_result isl_set_opt(__isl_keep isl_set *set, int max,
+       __isl_keep isl_aff *obj, isl_int *opt)
+{
+       enum isl_lp_result res;
+
+       if (!set || !obj)
+               return isl_lp_error;
+
+       if (isl_space_match(set->dim, isl_dim_param,
+                           obj->ls->dim, isl_dim_param))
+               return isl_set_opt_aligned(set, max, obj, opt);
+
+       set = isl_set_copy(set);
+       obj = isl_aff_copy(obj);
+       set = isl_set_align_params(set, isl_aff_get_domain_space(obj));
+       obj = isl_aff_align_params(obj, isl_set_get_space(set));
+
+       res = isl_set_opt_aligned(set, max, obj, opt);
+
+       isl_set_free(set);
+       isl_aff_free(obj);
+
+       return res;
+}
+
 enum isl_lp_result isl_basic_set_max(__isl_keep isl_basic_set *bset,
        __isl_keep isl_aff *obj, isl_int *opt)
 {
@@ -477,3 +510,128 @@ enum isl_lp_result isl_set_max(__isl_keep isl_set *set,
 {
        return isl_set_opt(set, 1, obj, opt);
 }
+
+enum isl_lp_result isl_set_min(__isl_keep isl_set *set,
+       __isl_keep isl_aff *obj, isl_int *opt)
+{
+       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);
+}
+
+/* Return the minimum (maximum if max is set) of the integer affine
+ * expression "obj" over the points in "set".
+ *
+ * Return infinity or negative infinity if the optimal value is unbounded and
+ * NaN if "bset" is empty.
+ *
+ * Call isl_set_opt and translate the results.
+ */
+__isl_give isl_val *isl_set_opt_val(__isl_keep isl_set *set, int max,
+       __isl_keep isl_aff *obj)
+{
+       isl_ctx *ctx;
+       isl_val *res;
+       enum isl_lp_result lp_res;
+
+       if (!set || !obj)
+               return NULL;
+
+       ctx = isl_aff_get_ctx(obj);
+       res = isl_val_alloc(ctx);
+       if (!res)
+               return NULL;
+       lp_res = isl_set_opt(set, max, obj, &res->n);
+       return convert_lp_result(lp_res, res, max);
+}
+
+/* Return the minimum of the integer affine
+ * expression "obj" over the points in "set".
+ *
+ * Return infinity or negative infinity if the optimal value is unbounded and
+ * NaN if "bset" is empty.
+ */
+__isl_give isl_val *isl_set_min_val(__isl_keep isl_set *set,
+       __isl_keep isl_aff *obj)
+{
+       return isl_set_opt_val(set, 0, obj);
+}
+
+/* Return the maximum of the integer affine
+ * expression "obj" over the points in "set".
+ *
+ * Return infinity or negative infinity if the optimal value is unbounded and
+ * NaN if "bset" is empty.
+ */
+__isl_give isl_val *isl_set_max_val(__isl_keep isl_set *set,
+       __isl_keep isl_aff *obj)
+{
+       return isl_set_opt_val(set, 1, obj);
+}