add isl_aff_mod_val
[platform/upstream/isl.git] / bound.c
diff --git a/bound.c b/bound.c
index 327705c..9a0ee8d 100644 (file)
--- a/bound.c
+++ b/bound.c
@@ -1,7 +1,8 @@
 #include <assert.h>
-#include <isl_stream.h>
+#include <isl/stream.h>
 #include <isl_polynomial_private.h>
 #include <isl_scan.h>
+#include <isl/options.h>
 
 struct bound_options {
        struct isl_options      *isl;
@@ -10,15 +11,15 @@ struct bound_options {
        int                      continue_on_error;
 };
 
-struct isl_arg bound_options_arg[] = {
-ISL_ARG_CHILD(struct bound_options, isl, "isl", isl_options_arg)
-ISL_ARG_BOOL(struct bound_options, verify, 'T', "verify", 0)
-ISL_ARG_BOOL(struct bound_options, print_all, 'A', "print-all", 0)
-ISL_ARG_BOOL(struct bound_options, continue_on_error, '\0', "continue-on-error", 0)
-ISL_ARG_END
-};
+ISL_ARGS_START(struct bound_options, bound_options_args)
+ISL_ARG_CHILD(struct bound_options, isl, "isl", &isl_options_args,
+       "isl options")
+ISL_ARG_BOOL(struct bound_options, verify, 'T', "verify", 0, NULL)
+ISL_ARG_BOOL(struct bound_options, print_all, 'A', "print-all", 0, NULL)
+ISL_ARG_BOOL(struct bound_options, continue_on_error, '\0', "continue-on-error", 0, NULL)
+ISL_ARGS_END
 
-ISL_ARG_DEF(bound_options, struct bound_options, bound_options_arg)
+ISL_ARG_DEF(bound_options, struct bound_options, bound_options_args)
 
 static __isl_give isl_set *set_bounds(__isl_take isl_set *set)
 {
@@ -50,25 +51,27 @@ struct verify_point_bound {
        int exact;
        int error;
 
-       isl_pw_qpolynomial *pwqp;
        isl_pw_qpolynomial_fold *pwf;
+       isl_pw_qpolynomial_fold *bound;
 };
 
 static int verify_point(__isl_take isl_point *pnt, void *user)
 {
        int i;
+       unsigned nvar;
        unsigned nparam;
        struct verify_point_bound *vpb = (struct verify_point_bound *) user;
        isl_int t;
-       isl_pw_qpolynomial *pwqp;
+       isl_ctx *ctx;
+       isl_pw_qpolynomial_fold *pwf;
        isl_qpolynomial *bound = NULL;
        isl_qpolynomial *opt = NULL;
        isl_set *dom = NULL;
+       isl_printer *p;
        const char *minmax;
        int bounded;
        int sign;
        int ok;
-       int cst;
        FILE *out = vpb->options->print_all ? stdout : stderr;
 
        vpb->n--;
@@ -81,35 +84,42 @@ static int verify_point(__isl_take isl_point *pnt, void *user)
                sign = -1;
        }
 
+       ctx = isl_point_get_ctx(pnt);
+       p = isl_printer_to_file(ctx, out);
+
        isl_int_init(t);
 
-       pwqp = isl_pw_qpolynomial_copy(vpb->pwqp);
+       pwf = isl_pw_qpolynomial_fold_copy(vpb->pwf);
 
-       nparam = isl_pw_qpolynomial_dim(pwqp, isl_dim_param);
+       nparam = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_param);
        for (i = 0; i < nparam; ++i) {
                isl_point_get_coordinate(pnt, isl_dim_param, i, &t);
-               pwqp = isl_pw_qpolynomial_fix_dim(pwqp, isl_dim_param, i, t);
+               pwf = isl_pw_qpolynomial_fold_fix_dim(pwf, isl_dim_param, i, t);
        }
 
-       bound = isl_pw_qpolynomial_fold_eval(isl_pw_qpolynomial_fold_copy(vpb->pwf),
-                                               isl_point_copy(pnt));
+       bound = isl_pw_qpolynomial_fold_eval(
+                                   isl_pw_qpolynomial_fold_copy(vpb->bound),
+                                   isl_point_copy(pnt));
 
-       dom = isl_pw_qpolynomial_domain(isl_pw_qpolynomial_copy(pwqp));
+       dom = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf));
        bounded = isl_set_is_bounded(dom);
 
        if (bounded < 0)
                goto error;
 
        if (!bounded)
-               opt = isl_pw_qpolynomial_eval(isl_pw_qpolynomial_copy(pwqp),
+               opt = isl_pw_qpolynomial_fold_eval(
+                                   isl_pw_qpolynomial_fold_copy(pwf),
                                    isl_set_sample_point(isl_set_copy(dom)));
        else if (sign > 0)
-               opt = isl_pw_qpolynomial_max(isl_pw_qpolynomial_copy(pwqp));
+               opt = isl_pw_qpolynomial_fold_max(isl_pw_qpolynomial_fold_copy(pwf));
        else
-               opt = isl_pw_qpolynomial_min(isl_pw_qpolynomial_copy(pwqp));
+               opt = isl_pw_qpolynomial_fold_min(isl_pw_qpolynomial_fold_copy(pwf));
 
+       nvar = isl_set_dim(dom, isl_dim_set);
+       opt = isl_qpolynomial_project_domain_on_params(opt);
        if (vpb->exact && bounded)
-               ok = isl_qpolynomial_is_equal(opt, bound);
+               ok = isl_qpolynomial_plain_is_equal(opt, bound);
        else if (sign > 0)
                ok = isl_qpolynomial_le_cst(opt, bound);
        else
@@ -118,24 +128,28 @@ static int verify_point(__isl_take isl_point *pnt, void *user)
                goto error;
 
        if (vpb->options->print_all || !ok) {
-               fprintf(out, "%s(", minmax);
+               p = isl_printer_print_str(p, minmax);
+               p = isl_printer_print_str(p, "(");
                for (i = 0; i < nparam; ++i) {
                        if (i)
-                               fprintf(out, ", ");
+                               p = isl_printer_print_str(p, ", ");
                        isl_point_get_coordinate(pnt, isl_dim_param, i, &t);
-                       isl_int_print(out, t, 0);
+                       p = isl_printer_print_isl_int(p, t);
                }
-               fprintf(out, ") = ");
-               isl_qpolynomial_print(bound, out, ISL_FORMAT_ISL);
-               fprintf(out, ", %s = ", bounded ? "opt" : "sample");
-               isl_qpolynomial_print(opt, out, ISL_FORMAT_ISL);
+               p = isl_printer_print_str(p, ") = ");
+               p = isl_printer_print_qpolynomial(p, bound);
+               p = isl_printer_print_str(p, ", ");
+               p = isl_printer_print_str(p, bounded ? "opt" : "sample");
+               p = isl_printer_print_str(p, " = ");
+               p = isl_printer_print_qpolynomial(p, opt);
                if (ok)
-                       fprintf(out, ". OK\n");
+                       p = isl_printer_print_str(p, ". OK");
                else
-                       fprintf(out, ". NOT OK\n");
+                       p = isl_printer_print_str(p, ". NOT OK");
+               p = isl_printer_end_line(p);
        } else if ((vpb->n % vpb->stride) == 0) {
-               printf("o");
-               fflush(stdout);
+               p = isl_printer_print_str(p, "o");
+               p = isl_printer_flush(p);
        }
 
        if (0) {
@@ -143,7 +157,7 @@ error:
                ok = 0;
        }
 
-       isl_pw_qpolynomial_free(pwqp);
+       isl_pw_qpolynomial_fold_free(pwf);
        isl_qpolynomial_free(bound);
        isl_qpolynomial_free(opt);
        isl_point_free(pnt);
@@ -151,6 +165,8 @@ error:
 
        isl_int_clear(t);
 
+       isl_printer_free(p);
+
        if (!ok)
                vpb->error = 1;
 
@@ -160,8 +176,8 @@ error:
        return (vpb->n >= 1 && ok) ? 0 : -1;
 }
 
-static int check_solution(__isl_take isl_pw_qpolynomial *pwqp,
-       __isl_take isl_pw_qpolynomial_fold *pwf, int exact,
+static int check_solution(__isl_take isl_pw_qpolynomial_fold *pwf,
+       __isl_take isl_pw_qpolynomial_fold *bound, int exact,
        struct bound_options *options)
 {
        struct verify_point_bound vpb;
@@ -170,9 +186,8 @@ static int check_solution(__isl_take isl_pw_qpolynomial *pwqp,
        isl_set *context;
        int i, r, n;
 
-       dom = isl_pw_qpolynomial_domain(isl_pw_qpolynomial_copy(pwqp));
-       context = isl_set_remove(isl_set_copy(dom), isl_dim_set,
-                                       0, isl_set_dim(dom, isl_dim_set));
+       dom = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf));
+       context = isl_set_params(isl_set_copy(dom));
        context = isl_set_remove_divs(context);
        context = set_bounds(context);
 
@@ -188,8 +203,8 @@ static int check_solution(__isl_take isl_pw_qpolynomial *pwqp,
        isl_int_clear(count);
 
        vpb.options = options;
-       vpb.pwqp = pwqp;
        vpb.pwf = pwf;
+       vpb.bound = bound;
        vpb.n = n;
        vpb.stride = n > 70 ? 1 + (n + 1)/70 : 1;
        vpb.error = 0;
@@ -206,8 +221,8 @@ static int check_solution(__isl_take isl_pw_qpolynomial *pwqp,
 
        isl_set_free(context);
        isl_set_free(dom);
-       isl_pw_qpolynomial_free(pwqp);
        isl_pw_qpolynomial_fold_free(pwf);
+       isl_pw_qpolynomial_fold_free(bound);
 
        if (!options->print_all)
                printf("\n");
@@ -223,10 +238,10 @@ static int check_solution(__isl_take isl_pw_qpolynomial *pwqp,
 int main(int argc, char **argv)
 {
        isl_ctx *ctx;
-       isl_pw_qpolynomial *pwqp;
-       isl_pw_qpolynomial *copy;
+       isl_pw_qpolynomial_fold *copy;
        isl_pw_qpolynomial_fold *pwf;
        struct isl_stream *s;
+       struct isl_obj obj;
        struct bound_options *options;
        int exact;
        int r = 0;
@@ -235,16 +250,24 @@ int main(int argc, char **argv)
        assert(options);
        argc = bound_options_parse(options, argc, argv, ISL_ARG_ALL);
 
-       ctx = isl_ctx_alloc_with_options(options->isl);
-       options->isl = NULL;
+       ctx = isl_ctx_alloc_with_options(&bound_options_args, options);
 
        s = isl_stream_new_file(ctx, stdin);
-       pwqp = isl_stream_read_pw_qpolynomial(s);
+       obj = isl_stream_read_obj(s);
+       if (obj.type == isl_obj_pw_qpolynomial)
+               pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
+                                                                 obj.v);
+       else if (obj.type == isl_obj_pw_qpolynomial_fold)
+               pwf = obj.v;
+       else {
+               obj.type->free(obj.v);
+               isl_die(ctx, isl_error_invalid, "invalid input", goto error);
+       }
 
        if (options->verify)
-               copy = isl_pw_qpolynomial_copy(pwqp);
+               copy = isl_pw_qpolynomial_fold_copy(pwf);
 
-       pwf = isl_pw_qpolynomial_bound_range(pwqp, isl_fold_max, &exact);
+       pwf = isl_pw_qpolynomial_fold_bound(pwf, &exact);
        pwf = isl_pw_qpolynomial_fold_coalesce(pwf);
 
        if (options->verify) {
@@ -257,10 +280,10 @@ int main(int argc, char **argv)
                isl_pw_qpolynomial_fold_free(pwf);
        }
 
+error:
        isl_stream_free(s);
 
        isl_ctx_free(ctx);
-       free(options);
 
        return r;
 }