add isl_*_list_from_*
[platform/upstream/isl.git] / bound.c
1 #include <assert.h>
2 #include <isl/stream.h>
3 #include <isl_polynomial_private.h>
4 #include <isl_scan.h>
5
6 struct bound_options {
7         struct isl_options      *isl;
8         unsigned                 verify;
9         int                      print_all;
10         int                      continue_on_error;
11 };
12
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)
18 ISL_ARG_END
19 };
20
21 ISL_ARG_DEF(bound_options, struct bound_options, bound_options_arg)
22
23 static __isl_give isl_set *set_bounds(__isl_take isl_set *set)
24 {
25         unsigned nparam;
26         int i, r;
27         isl_point *pt, *pt2;
28         isl_set *box;
29
30         nparam = isl_set_dim(set, isl_dim_param);
31         r = nparam >= 8 ? 5 : nparam >= 5 ? 15 : 50;
32
33         pt = isl_set_sample_point(isl_set_copy(set));
34         pt2 = isl_point_copy(pt);
35
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);
39         }
40
41         box = isl_set_box_from_points(pt, pt2);
42
43         return isl_set_intersect(set, box);
44 }
45
46 struct verify_point_bound {
47         struct bound_options *options;
48         int stride;
49         int n;
50         int exact;
51         int error;
52
53         isl_pw_qpolynomial_fold *pwf;
54         isl_pw_qpolynomial_fold *bound;
55 };
56
57 static int verify_point(__isl_take isl_point *pnt, void *user)
58 {
59         int i;
60         unsigned nvar;
61         unsigned nparam;
62         struct verify_point_bound *vpb = (struct verify_point_bound *) user;
63         isl_int t;
64         isl_pw_qpolynomial_fold *pwf;
65         isl_qpolynomial *bound = NULL;
66         isl_qpolynomial *opt = NULL;
67         isl_set *dom = NULL;
68         const char *minmax;
69         int bounded;
70         int sign;
71         int ok;
72         FILE *out = vpb->options->print_all ? stdout : stderr;
73
74         vpb->n--;
75
76         if (1) {
77                 minmax = "ub";
78                 sign = 1;
79         } else {
80                 minmax = "lb";
81                 sign = -1;
82         }
83
84         isl_int_init(t);
85
86         pwf = isl_pw_qpolynomial_fold_copy(vpb->pwf);
87
88         nparam = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_param);
89         for (i = 0; i < nparam; ++i) {
90                 isl_point_get_coordinate(pnt, isl_dim_param, i, &t);
91                 pwf = isl_pw_qpolynomial_fold_fix_dim(pwf, isl_dim_param, i, t);
92         }
93
94         bound = isl_pw_qpolynomial_fold_eval(
95                                     isl_pw_qpolynomial_fold_copy(vpb->bound),
96                                     isl_point_copy(pnt));
97
98         dom = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf));
99         bounded = isl_set_is_bounded(dom);
100
101         if (bounded < 0)
102                 goto error;
103
104         if (!bounded)
105                 opt = isl_pw_qpolynomial_fold_eval(
106                                     isl_pw_qpolynomial_fold_copy(pwf),
107                                     isl_set_sample_point(isl_set_copy(dom)));
108         else if (sign > 0)
109                 opt = isl_pw_qpolynomial_fold_max(isl_pw_qpolynomial_fold_copy(pwf));
110         else
111                 opt = isl_pw_qpolynomial_fold_min(isl_pw_qpolynomial_fold_copy(pwf));
112
113         nvar = isl_set_dim(dom, isl_dim_set);
114         opt = isl_qpolynomial_drop_dims(opt, isl_dim_set, 0, nvar);
115         if (vpb->exact && bounded)
116                 ok = isl_qpolynomial_plain_is_equal(opt, bound);
117         else if (sign > 0)
118                 ok = isl_qpolynomial_le_cst(opt, bound);
119         else
120                 ok = isl_qpolynomial_le_cst(bound, opt);
121         if (ok < 0)
122                 goto error;
123
124         if (vpb->options->print_all || !ok) {
125                 fprintf(out, "%s(", minmax);
126                 for (i = 0; i < nparam; ++i) {
127                         if (i)
128                                 fprintf(out, ", ");
129                         isl_point_get_coordinate(pnt, isl_dim_param, i, &t);
130                         isl_int_print(out, t, 0);
131                 }
132                 fprintf(out, ") = ");
133                 isl_qpolynomial_print(bound, out, ISL_FORMAT_ISL);
134                 fprintf(out, ", %s = ", bounded ? "opt" : "sample");
135                 isl_qpolynomial_print(opt, out, ISL_FORMAT_ISL);
136                 if (ok)
137                         fprintf(out, ". OK\n");
138                 else
139                         fprintf(out, ". NOT OK\n");
140         } else if ((vpb->n % vpb->stride) == 0) {
141                 printf("o");
142                 fflush(stdout);
143         }
144
145         if (0) {
146 error:
147                 ok = 0;
148         }
149
150         isl_pw_qpolynomial_fold_free(pwf);
151         isl_qpolynomial_free(bound);
152         isl_qpolynomial_free(opt);
153         isl_point_free(pnt);
154         isl_set_free(dom);
155
156         isl_int_clear(t);
157
158         if (!ok)
159                 vpb->error = 1;
160
161         if (vpb->options->continue_on_error)
162                 ok = 1;
163
164         return (vpb->n >= 1 && ok) ? 0 : -1;
165 }
166
167 static int check_solution(__isl_take isl_pw_qpolynomial_fold *pwf,
168         __isl_take isl_pw_qpolynomial_fold *bound, int exact,
169         struct bound_options *options)
170 {
171         struct verify_point_bound vpb;
172         isl_int count, max;
173         isl_set *dom;
174         isl_set *context;
175         int i, r, n;
176
177         dom = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf));
178         context = isl_set_remove_dims(isl_set_copy(dom), isl_dim_set,
179                                         0, isl_set_dim(dom, isl_dim_set));
180         context = isl_set_remove_divs(context);
181         context = set_bounds(context);
182
183         isl_int_init(count);
184         isl_int_init(max);
185
186         isl_int_set_si(max, 200);
187         r = isl_set_count_upto(context, max, &count);
188         assert(r >= 0);
189         n = isl_int_get_si(count);
190
191         isl_int_clear(max);
192         isl_int_clear(count);
193
194         vpb.options = options;
195         vpb.pwf = pwf;
196         vpb.bound = bound;
197         vpb.n = n;
198         vpb.stride = n > 70 ? 1 + (n + 1)/70 : 1;
199         vpb.error = 0;
200         vpb.exact = exact;
201
202         if (!options->print_all) {
203                 for (i = 0; i < vpb.n; i += vpb.stride)
204                         printf(".");
205                 printf("\r");
206                 fflush(stdout);
207         }
208
209         isl_set_foreach_point(context, verify_point, &vpb);
210
211         isl_set_free(context);
212         isl_set_free(dom);
213         isl_pw_qpolynomial_fold_free(pwf);
214         isl_pw_qpolynomial_fold_free(bound);
215
216         if (!options->print_all)
217                 printf("\n");
218
219         if (vpb.error) {
220                 fprintf(stderr, "Check failed !\n");
221                 return -1;
222         }
223
224         return 0;
225 }
226
227 int main(int argc, char **argv)
228 {
229         isl_ctx *ctx;
230         isl_pw_qpolynomial_fold *copy;
231         isl_pw_qpolynomial_fold *pwf;
232         struct isl_stream *s;
233         struct isl_obj obj;
234         struct bound_options *options;
235         int exact;
236         int r = 0;
237
238         options = bound_options_new_with_defaults();
239         assert(options);
240         argc = bound_options_parse(options, argc, argv, ISL_ARG_ALL);
241
242         ctx = isl_ctx_alloc_with_options(bound_options_arg, options);
243
244         s = isl_stream_new_file(ctx, stdin);
245         obj = isl_stream_read_obj(s);
246         if (obj.type == isl_obj_pw_qpolynomial)
247                 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max,
248                                                                   obj.v);
249         else if (obj.type == isl_obj_pw_qpolynomial_fold)
250                 pwf = obj.v;
251         else {
252                 obj.type->free(obj.v);
253                 isl_die(ctx, isl_error_invalid, "invalid input", goto error);
254         }
255
256         if (options->verify)
257                 copy = isl_pw_qpolynomial_fold_copy(pwf);
258
259         pwf = isl_pw_qpolynomial_fold_bound(pwf, &exact);
260         pwf = isl_pw_qpolynomial_fold_coalesce(pwf);
261
262         if (options->verify) {
263                 r = check_solution(copy, pwf, exact, options);
264         } else {
265                 if (!exact)
266                         printf("# NOT exact\n");
267                 isl_pw_qpolynomial_fold_print(pwf, stdout, 0);
268                 fprintf(stdout, "\n");
269                 isl_pw_qpolynomial_fold_free(pwf);
270         }
271
272 error:
273         isl_stream_free(s);
274
275         isl_ctx_free(ctx);
276
277         return r;
278 }