2 #include <isl_stream.h>
3 #include <isl_polynomial_private.h>
7 struct isl_options *isl;
10 int continue_on_error;
13 struct isl_arg bound_options_arg[] = {
14 ISL_ARG_CHILD(struct bound_options, isl, "isl", isl_options_arg, "isl options")
15 ISL_ARG_BOOL(struct bound_options, verify, 'T', "verify", 0, NULL)
16 ISL_ARG_BOOL(struct bound_options, print_all, 'A', "print-all", 0, NULL)
17 ISL_ARG_BOOL(struct bound_options, continue_on_error, '\0', "continue-on-error", 0, NULL)
21 ISL_ARG_DEF(bound_options, struct bound_options, bound_options_arg)
23 static __isl_give isl_set *set_bounds(__isl_take isl_set *set)
30 nparam = isl_set_dim(set, isl_dim_param);
31 r = nparam >= 8 ? 5 : nparam >= 5 ? 15 : 50;
33 pt = isl_set_sample_point(isl_set_copy(set));
34 pt2 = isl_point_copy(pt);
36 for (i = 0; i < nparam; ++i) {
37 pt = isl_point_add_ui(pt, isl_dim_param, i, r);
38 pt2 = isl_point_sub_ui(pt2, isl_dim_param, i, r);
41 box = isl_set_box_from_points(pt, pt2);
43 return isl_set_intersect(set, box);
46 struct verify_point_bound {
47 struct bound_options *options;
53 isl_pw_qpolynomial *pwqp;
54 isl_pw_qpolynomial_fold *pwf;
57 static int verify_point(__isl_take isl_point *pnt, void *user)
61 struct verify_point_bound *vpb = (struct verify_point_bound *) user;
63 isl_pw_qpolynomial *pwqp;
64 isl_qpolynomial *bound = NULL;
65 isl_qpolynomial *opt = NULL;
72 FILE *out = vpb->options->print_all ? stdout : stderr;
86 pwqp = isl_pw_qpolynomial_copy(vpb->pwqp);
88 nparam = isl_pw_qpolynomial_dim(pwqp, isl_dim_param);
89 for (i = 0; i < nparam; ++i) {
90 isl_point_get_coordinate(pnt, isl_dim_param, i, &t);
91 pwqp = isl_pw_qpolynomial_fix_dim(pwqp, isl_dim_param, i, t);
94 bound = isl_pw_qpolynomial_fold_eval(isl_pw_qpolynomial_fold_copy(vpb->pwf),
97 dom = isl_pw_qpolynomial_domain(isl_pw_qpolynomial_copy(pwqp));
98 bounded = isl_set_is_bounded(dom);
104 opt = isl_pw_qpolynomial_eval(isl_pw_qpolynomial_copy(pwqp),
105 isl_set_sample_point(isl_set_copy(dom)));
107 opt = isl_pw_qpolynomial_max(isl_pw_qpolynomial_copy(pwqp));
109 opt = isl_pw_qpolynomial_min(isl_pw_qpolynomial_copy(pwqp));
111 if (vpb->exact && bounded)
112 ok = isl_qpolynomial_is_equal(opt, bound);
114 ok = isl_qpolynomial_le_cst(opt, bound);
116 ok = isl_qpolynomial_le_cst(bound, opt);
120 if (vpb->options->print_all || !ok) {
121 fprintf(out, "%s(", minmax);
122 for (i = 0; i < nparam; ++i) {
125 isl_point_get_coordinate(pnt, isl_dim_param, i, &t);
126 isl_int_print(out, t, 0);
128 fprintf(out, ") = ");
129 isl_qpolynomial_print(bound, out, ISL_FORMAT_ISL);
130 fprintf(out, ", %s = ", bounded ? "opt" : "sample");
131 isl_qpolynomial_print(opt, out, ISL_FORMAT_ISL);
133 fprintf(out, ". OK\n");
135 fprintf(out, ". NOT OK\n");
136 } else if ((vpb->n % vpb->stride) == 0) {
146 isl_pw_qpolynomial_free(pwqp);
147 isl_qpolynomial_free(bound);
148 isl_qpolynomial_free(opt);
157 if (vpb->options->continue_on_error)
160 return (vpb->n >= 1 && ok) ? 0 : -1;
163 static int check_solution(__isl_take isl_pw_qpolynomial *pwqp,
164 __isl_take isl_pw_qpolynomial_fold *pwf, int exact,
165 struct bound_options *options)
167 struct verify_point_bound vpb;
173 dom = isl_pw_qpolynomial_domain(isl_pw_qpolynomial_copy(pwqp));
174 context = isl_set_remove(isl_set_copy(dom), isl_dim_set,
175 0, isl_set_dim(dom, isl_dim_set));
176 context = isl_set_remove_divs(context);
177 context = set_bounds(context);
182 isl_int_set_si(max, 200);
183 r = isl_set_count_upto(context, max, &count);
185 n = isl_int_get_si(count);
188 isl_int_clear(count);
190 vpb.options = options;
194 vpb.stride = n > 70 ? 1 + (n + 1)/70 : 1;
198 if (!options->print_all) {
199 for (i = 0; i < vpb.n; i += vpb.stride)
205 isl_set_foreach_point(context, verify_point, &vpb);
207 isl_set_free(context);
209 isl_pw_qpolynomial_free(pwqp);
210 isl_pw_qpolynomial_fold_free(pwf);
212 if (!options->print_all)
216 fprintf(stderr, "Check failed !\n");
223 int main(int argc, char **argv)
226 isl_pw_qpolynomial *pwqp;
227 isl_pw_qpolynomial *copy;
228 isl_pw_qpolynomial_fold *pwf;
229 struct isl_stream *s;
230 struct bound_options *options;
234 options = bound_options_new_with_defaults();
236 argc = bound_options_parse(options, argc, argv, ISL_ARG_ALL);
238 ctx = isl_ctx_alloc_with_options(bound_options_arg, options);
240 s = isl_stream_new_file(ctx, stdin);
241 pwqp = isl_stream_read_pw_qpolynomial(s);
244 copy = isl_pw_qpolynomial_copy(pwqp);
246 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, &exact);
247 pwf = isl_pw_qpolynomial_fold_coalesce(pwf);
249 if (options->verify) {
250 r = check_solution(copy, pwf, exact, options);
253 printf("# NOT exact\n");
254 isl_pw_qpolynomial_fold_print(pwf, stdout, 0);
255 fprintf(stdout, "\n");
256 isl_pw_qpolynomial_fold_free(pwf);