deprecate isl_int
[isl.git] / bound.c
blob7e4bfeeab719de6e7c217d65a7be634ab0792a8c
1 #include <assert.h>
2 #include <isl/stream.h>
3 #include <isl_map_private.h>
4 #include <isl_polynomial_private.h>
5 #include <isl_scan.h>
6 #include <isl/options.h>
7 #include <isl/deprecated/point_int.h>
8 #include <isl/deprecated/polynomial_int.h>
10 struct bound_options {
11 struct isl_options *isl;
12 unsigned verify;
13 int print_all;
14 int continue_on_error;
17 ISL_ARGS_START(struct bound_options, bound_options_args)
18 ISL_ARG_CHILD(struct bound_options, isl, "isl", &isl_options_args,
19 "isl options")
20 ISL_ARG_BOOL(struct bound_options, verify, 'T', "verify", 0, NULL)
21 ISL_ARG_BOOL(struct bound_options, print_all, 'A', "print-all", 0, NULL)
22 ISL_ARG_BOOL(struct bound_options, continue_on_error, '\0', "continue-on-error", 0, NULL)
23 ISL_ARGS_END
25 ISL_ARG_DEF(bound_options, struct bound_options, bound_options_args)
27 static __isl_give isl_set *set_bounds(__isl_take isl_set *set)
29 unsigned nparam;
30 int i, r;
31 isl_point *pt, *pt2;
32 isl_set *box;
34 nparam = isl_set_dim(set, isl_dim_param);
35 r = nparam >= 8 ? 5 : nparam >= 5 ? 15 : 50;
37 pt = isl_set_sample_point(isl_set_copy(set));
38 pt2 = isl_point_copy(pt);
40 for (i = 0; i < nparam; ++i) {
41 pt = isl_point_add_ui(pt, isl_dim_param, i, r);
42 pt2 = isl_point_sub_ui(pt2, isl_dim_param, i, r);
45 box = isl_set_box_from_points(pt, pt2);
47 return isl_set_intersect(set, box);
50 struct verify_point_bound {
51 struct bound_options *options;
52 int stride;
53 int n;
54 int exact;
55 int error;
57 isl_pw_qpolynomial_fold *pwf;
58 isl_pw_qpolynomial_fold *bound;
61 static int verify_point(__isl_take isl_point *pnt, void *user)
63 int i;
64 unsigned nvar;
65 unsigned nparam;
66 struct verify_point_bound *vpb = (struct verify_point_bound *) user;
67 isl_int t;
68 isl_ctx *ctx;
69 isl_pw_qpolynomial_fold *pwf;
70 isl_qpolynomial *bound = NULL;
71 isl_qpolynomial *opt = NULL;
72 isl_set *dom = NULL;
73 isl_printer *p;
74 const char *minmax;
75 int bounded;
76 int sign;
77 int ok;
78 FILE *out = vpb->options->print_all ? stdout : stderr;
80 vpb->n--;
82 if (1) {
83 minmax = "ub";
84 sign = 1;
85 } else {
86 minmax = "lb";
87 sign = -1;
90 ctx = isl_point_get_ctx(pnt);
91 p = isl_printer_to_file(ctx, out);
93 isl_int_init(t);
95 pwf = isl_pw_qpolynomial_fold_copy(vpb->pwf);
97 nparam = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_param);
98 for (i = 0; i < nparam; ++i) {
99 isl_point_get_coordinate(pnt, isl_dim_param, i, &t);
100 pwf = isl_pw_qpolynomial_fold_fix_dim(pwf, isl_dim_param, i, t);
103 bound = isl_pw_qpolynomial_fold_eval(
104 isl_pw_qpolynomial_fold_copy(vpb->bound),
105 isl_point_copy(pnt));
107 dom = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf));
108 bounded = isl_set_is_bounded(dom);
110 if (bounded < 0)
111 goto error;
113 if (!bounded)
114 opt = isl_pw_qpolynomial_fold_eval(
115 isl_pw_qpolynomial_fold_copy(pwf),
116 isl_set_sample_point(isl_set_copy(dom)));
117 else if (sign > 0)
118 opt = isl_pw_qpolynomial_fold_max(isl_pw_qpolynomial_fold_copy(pwf));
119 else
120 opt = isl_pw_qpolynomial_fold_min(isl_pw_qpolynomial_fold_copy(pwf));
122 nvar = isl_set_dim(dom, isl_dim_set);
123 opt = isl_qpolynomial_project_domain_on_params(opt);
124 if (vpb->exact && bounded)
125 ok = isl_qpolynomial_plain_is_equal(opt, bound);
126 else if (sign > 0)
127 ok = isl_qpolynomial_le_cst(opt, bound);
128 else
129 ok = isl_qpolynomial_le_cst(bound, opt);
130 if (ok < 0)
131 goto error;
133 if (vpb->options->print_all || !ok) {
134 p = isl_printer_print_str(p, minmax);
135 p = isl_printer_print_str(p, "(");
136 for (i = 0; i < nparam; ++i) {
137 if (i)
138 p = isl_printer_print_str(p, ", ");
139 isl_point_get_coordinate(pnt, isl_dim_param, i, &t);
140 p = isl_printer_print_isl_int(p, t);
142 p = isl_printer_print_str(p, ") = ");
143 p = isl_printer_print_qpolynomial(p, bound);
144 p = isl_printer_print_str(p, ", ");
145 p = isl_printer_print_str(p, bounded ? "opt" : "sample");
146 p = isl_printer_print_str(p, " = ");
147 p = isl_printer_print_qpolynomial(p, opt);
148 if (ok)
149 p = isl_printer_print_str(p, ". OK");
150 else
151 p = isl_printer_print_str(p, ". NOT OK");
152 p = isl_printer_end_line(p);
153 } else if ((vpb->n % vpb->stride) == 0) {
154 p = isl_printer_print_str(p, "o");
155 p = isl_printer_flush(p);
158 if (0) {
159 error:
160 ok = 0;
163 isl_pw_qpolynomial_fold_free(pwf);
164 isl_qpolynomial_free(bound);
165 isl_qpolynomial_free(opt);
166 isl_point_free(pnt);
167 isl_set_free(dom);
169 isl_int_clear(t);
171 isl_printer_free(p);
173 if (!ok)
174 vpb->error = 1;
176 if (vpb->options->continue_on_error)
177 ok = 1;
179 return (vpb->n >= 1 && ok) ? 0 : -1;
182 static int check_solution(__isl_take isl_pw_qpolynomial_fold *pwf,
183 __isl_take isl_pw_qpolynomial_fold *bound, int exact,
184 struct bound_options *options)
186 struct verify_point_bound vpb;
187 isl_int count, max;
188 isl_set *dom;
189 isl_set *context;
190 int i, r, n;
192 dom = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf));
193 context = isl_set_params(isl_set_copy(dom));
194 context = isl_set_remove_divs(context);
195 context = set_bounds(context);
197 isl_int_init(count);
198 isl_int_init(max);
200 isl_int_set_si(max, 200);
201 r = isl_set_count_upto(context, max, &count);
202 assert(r >= 0);
203 n = isl_int_get_si(count);
205 isl_int_clear(max);
206 isl_int_clear(count);
208 vpb.options = options;
209 vpb.pwf = pwf;
210 vpb.bound = bound;
211 vpb.n = n;
212 vpb.stride = n > 70 ? 1 + (n + 1)/70 : 1;
213 vpb.error = 0;
214 vpb.exact = exact;
216 if (!options->print_all) {
217 for (i = 0; i < vpb.n; i += vpb.stride)
218 printf(".");
219 printf("\r");
220 fflush(stdout);
223 isl_set_foreach_point(context, verify_point, &vpb);
225 isl_set_free(context);
226 isl_set_free(dom);
227 isl_pw_qpolynomial_fold_free(pwf);
228 isl_pw_qpolynomial_fold_free(bound);
230 if (!options->print_all)
231 printf("\n");
233 if (vpb.error) {
234 fprintf(stderr, "Check failed !\n");
235 return -1;
238 return 0;
241 int main(int argc, char **argv)
243 isl_ctx *ctx;
244 isl_pw_qpolynomial_fold *copy;
245 isl_pw_qpolynomial_fold *pwf;
246 struct isl_stream *s;
247 struct isl_obj obj;
248 struct bound_options *options;
249 int exact;
250 int r = 0;
252 options = bound_options_new_with_defaults();
253 assert(options);
254 argc = bound_options_parse(options, argc, argv, ISL_ARG_ALL);
256 ctx = isl_ctx_alloc_with_options(&bound_options_args, options);
258 s = isl_stream_new_file(ctx, stdin);
259 obj = isl_stream_read_obj(s);
260 if (obj.type == isl_obj_pw_qpolynomial)
261 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
262 obj.v);
263 else if (obj.type == isl_obj_pw_qpolynomial_fold)
264 pwf = obj.v;
265 else {
266 obj.type->free(obj.v);
267 isl_die(ctx, isl_error_invalid, "invalid input", goto error);
270 if (options->verify)
271 copy = isl_pw_qpolynomial_fold_copy(pwf);
273 pwf = isl_pw_qpolynomial_fold_bound(pwf, &exact);
274 pwf = isl_pw_qpolynomial_fold_coalesce(pwf);
276 if (options->verify) {
277 r = check_solution(copy, pwf, exact, options);
278 } else {
279 if (!exact)
280 printf("# NOT exact\n");
281 isl_pw_qpolynomial_fold_print(pwf, stdout, 0);
282 fprintf(stdout, "\n");
283 isl_pw_qpolynomial_fold_free(pwf);
286 error:
287 isl_stream_free(s);
289 isl_ctx_free(ctx);
291 return r;