isl_map_read_from_str: parse tuple entries with leading minus sign
[isl.git] / isl_bound.c
blobbdddfad1a59b48d7bb4b1215a8c713e77258ecaf
1 /*
2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8 * 91893 Orsay, France
9 */
11 #include <isl_bound.h>
12 #include <isl_bernstein.h>
13 #include <isl_range.h>
14 #include <isl_polynomial_private.h>
16 /* Compute a bound on the polynomial defined over the parametric polytope
17 * using either range propagation or bernstein expansion and
18 * store the result in bound->pwf and bound->pwf_tight.
19 * Since bernstein expansion requires bounded domains, we apply
20 * range propagation on unbounded domains. Otherwise, we respect the choice
21 * of the user.
23 static int compressed_guarded_poly_bound(__isl_take isl_basic_set *bset,
24 __isl_take isl_qpolynomial *poly, void *user)
26 struct isl_bound *bound = (struct isl_bound *)user;
27 int bounded;
29 if (!bset || !poly)
30 goto error;
32 if (bset->ctx->opt->bound == ISL_BOUND_RANGE)
33 return isl_qpolynomial_bound_on_domain_range(bset, poly, bound);
35 bounded = isl_basic_set_is_bounded(bset);
36 if (bounded < 0)
37 goto error;
38 if (bounded)
39 return isl_qpolynomial_bound_on_domain_bernstein(bset, poly, bound);
40 else
41 return isl_qpolynomial_bound_on_domain_range(bset, poly, bound);
42 error:
43 isl_basic_set_free(bset);
44 isl_qpolynomial_free(poly);
45 return -1;
48 static int unwrapped_guarded_poly_bound(__isl_take isl_basic_set *bset,
49 __isl_take isl_qpolynomial *poly, void *user)
51 struct isl_bound *bound = (struct isl_bound *)user;
52 isl_pw_qpolynomial_fold *top_pwf;
53 isl_pw_qpolynomial_fold *top_pwf_tight;
54 isl_dim *dim;
55 isl_morph *morph;
56 unsigned orig_nvar, final_nvar;
57 int r;
59 bset = isl_basic_set_detect_equalities(bset);
61 if (!bset)
62 goto error;
64 if (bset->n_eq == 0)
65 return compressed_guarded_poly_bound(bset, poly, user);
67 orig_nvar = isl_basic_set_dim(bset, isl_dim_set);
69 morph = isl_basic_set_full_compression(bset);
71 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
72 poly = isl_qpolynomial_morph(poly, isl_morph_copy(morph));
74 final_nvar = isl_basic_set_dim(bset, isl_dim_set);
76 dim = isl_morph_get_ran_dim(morph);
77 dim = isl_dim_drop(dim, isl_dim_set, 0, isl_dim_size(dim, isl_dim_set));
79 top_pwf = bound->pwf;
80 top_pwf_tight = bound->pwf_tight;
82 bound->pwf = isl_pw_qpolynomial_fold_zero(isl_dim_copy(dim),
83 bound->type);
84 bound->pwf_tight = isl_pw_qpolynomial_fold_zero(dim, bound->type);
86 r = compressed_guarded_poly_bound(bset, poly, user);
88 morph = isl_morph_remove_dom_dims(morph, isl_dim_set, 0, orig_nvar);
89 morph = isl_morph_remove_ran_dims(morph, isl_dim_set, 0, final_nvar);
90 morph = isl_morph_inverse(morph);
92 bound->pwf = isl_pw_qpolynomial_fold_morph(bound->pwf,
93 isl_morph_copy(morph));
94 bound->pwf_tight = isl_pw_qpolynomial_fold_morph(bound->pwf_tight, morph);
96 bound->pwf = isl_pw_qpolynomial_fold_fold(top_pwf, bound->pwf);
97 bound->pwf_tight = isl_pw_qpolynomial_fold_fold(top_pwf_tight,
98 bound->pwf_tight);
100 return r;
101 error:
102 isl_basic_set_free(bset);
103 isl_qpolynomial_free(poly);
104 return -1;
107 static int guarded_poly_bound(__isl_take isl_basic_set *bset,
108 __isl_take isl_qpolynomial *poly, void *user)
110 struct isl_bound *bound = (struct isl_bound *)user;
111 isl_dim *dim;
112 isl_dim *target_dim;
113 isl_pw_qpolynomial_fold *top_pwf;
114 isl_pw_qpolynomial_fold *top_pwf_tight;
115 int nparam;
116 int n_in;
117 int r;
119 if (!isl_basic_set_is_wrapping(bset))
120 return unwrapped_guarded_poly_bound(bset, poly, user);
122 target_dim = isl_basic_set_get_dim(bset);
123 target_dim = isl_dim_unwrap(target_dim);
124 target_dim = isl_dim_domain(target_dim);
126 nparam = isl_dim_size(target_dim, isl_dim_param);
127 n_in = isl_dim_size(target_dim, isl_dim_set);
129 bset = isl_basic_set_move_dims(bset, isl_dim_param, nparam,
130 isl_dim_set, 0, n_in);
131 poly = isl_qpolynomial_move_dims(poly, isl_dim_param, nparam,
132 isl_dim_set, 0, n_in);
134 dim = isl_basic_set_get_dim(bset);
135 dim = isl_dim_drop(dim, isl_dim_set, 0, isl_dim_size(dim, isl_dim_set));
137 top_pwf = bound->pwf;
138 top_pwf_tight = bound->pwf_tight;
140 bound->pwf = isl_pw_qpolynomial_fold_zero(isl_dim_copy(dim),
141 bound->type);
142 bound->pwf_tight = isl_pw_qpolynomial_fold_zero(dim, bound->type);
144 r = unwrapped_guarded_poly_bound(bset, poly, user);
146 bound->pwf = isl_pw_qpolynomial_fold_reset_dim(bound->pwf,
147 isl_dim_copy(target_dim));
148 bound->pwf_tight = isl_pw_qpolynomial_fold_reset_dim(bound->pwf_tight,
149 target_dim);
151 bound->pwf = isl_pw_qpolynomial_fold_fold(top_pwf, bound->pwf);
152 bound->pwf_tight = isl_pw_qpolynomial_fold_fold(top_pwf_tight,
153 bound->pwf_tight);
155 return r;
158 static int guarded_qp(__isl_take isl_qpolynomial *qp, void *user)
160 struct isl_bound *bound = (struct isl_bound *)user;
161 int r;
163 r = isl_qpolynomial_as_polynomial_on_domain(qp, bound->bset,
164 &guarded_poly_bound, user);
165 isl_qpolynomial_free(qp);
166 return r;
169 static int basic_guarded_fold(__isl_take isl_basic_set *bset, void *user)
171 struct isl_bound *bound = (struct isl_bound *)user;
172 int r;
174 bound->bset = bset;
175 r = isl_qpolynomial_fold_foreach_qpolynomial(bound->fold,
176 &guarded_qp, user);
177 isl_basic_set_free(bset);
178 return r;
181 static int guarded_fold(__isl_take isl_set *set,
182 __isl_take isl_qpolynomial_fold *fold, void *user)
184 struct isl_bound *bound = (struct isl_bound *)user;
186 if (!set || !fold)
187 goto error;
189 set = isl_set_make_disjoint(set);
191 bound->fold = fold;
192 bound->type = isl_qpolynomial_fold_get_type(fold);
194 if (isl_set_foreach_basic_set(set, &basic_guarded_fold, bound) < 0)
195 goto error;
197 isl_set_free(set);
198 isl_qpolynomial_fold_free(fold);
200 return 0;
201 error:
202 isl_set_free(set);
203 isl_qpolynomial_fold_free(fold);
204 return -1;
207 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_bound(
208 __isl_take isl_pw_qpolynomial_fold *pwf, int *tight)
210 isl_dim *dim;
211 unsigned nvar;
212 struct isl_bound bound;
213 int covers;
215 if (!pwf)
216 return NULL;
218 dim = isl_pw_qpolynomial_fold_get_dim(pwf);
219 nvar = isl_dim_size(dim, isl_dim_set);
221 if (isl_pw_qpolynomial_fold_is_zero(pwf)) {
222 isl_pw_qpolynomial_fold_free(pwf);
223 dim = isl_dim_drop(dim, isl_dim_set, 0, nvar);
224 if (tight)
225 *tight = 1;
226 return isl_pw_qpolynomial_fold_zero(dim, pwf->type);
229 if (nvar == 0) {
230 isl_dim_free(dim);
231 if (tight)
232 *tight = 1;
233 return pwf;
236 if (isl_dim_is_wrapping(dim)) {
237 dim = isl_dim_unwrap(dim);
238 nvar = isl_dim_size(dim, isl_dim_out);
239 dim = isl_dim_domain(dim);
240 if (nvar == 0) {
241 if (tight)
242 *tight = 1;
243 return isl_pw_qpolynomial_fold_reset_dim(pwf, dim);
245 } else
246 dim = isl_dim_drop(dim, isl_dim_set, 0, nvar);
248 bound.pwf = isl_pw_qpolynomial_fold_zero(isl_dim_copy(dim), pwf->type);
249 bound.pwf_tight = isl_pw_qpolynomial_fold_zero(isl_dim_copy(dim),
250 pwf->type);
251 bound.check_tight = !!tight;
253 if (isl_pw_qpolynomial_fold_foreach_lifted_piece(pwf,
254 guarded_fold, &bound) < 0)
255 goto error;
257 covers = isl_pw_qpolynomial_fold_covers(bound.pwf_tight, bound.pwf);
258 if (covers < 0)
259 goto error;
261 if (tight)
262 *tight = covers;
264 isl_dim_free(dim);
265 isl_pw_qpolynomial_fold_free(pwf);
267 if (covers) {
268 isl_pw_qpolynomial_fold_free(bound.pwf);
269 return bound.pwf_tight;
272 bound.pwf = isl_pw_qpolynomial_fold_fold(bound.pwf, bound.pwf_tight);
274 return bound.pwf;
275 error:
276 isl_pw_qpolynomial_fold_free(bound.pwf_tight);
277 isl_pw_qpolynomial_fold_free(bound.pwf);
278 isl_pw_qpolynomial_fold_free(pwf);
279 isl_dim_free(dim);
280 return NULL;
283 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_bound(
284 __isl_take isl_pw_qpolynomial *pwqp, enum isl_fold type, int *tight)
286 isl_pw_qpolynomial_fold *pwf;
288 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(type, pwqp);
289 return isl_pw_qpolynomial_fold_bound(pwf, tight);
292 struct isl_union_bound_data {
293 enum isl_fold type;
294 int tight;
295 isl_union_pw_qpolynomial_fold *res;
298 static int bound_pw(__isl_take isl_pw_qpolynomial *pwqp, void *user)
300 struct isl_union_bound_data *data = user;
301 isl_pw_qpolynomial_fold *pwf;
303 pwf = isl_pw_qpolynomial_bound(pwqp, data->type,
304 data->tight ? &data->tight : NULL);
305 data->res = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
306 data->res, pwf);
308 return 0;
311 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_bound(
312 __isl_take isl_union_pw_qpolynomial *upwqp,
313 enum isl_fold type, int *tight)
315 isl_dim *dim;
316 struct isl_union_bound_data data = { type, 1, NULL };
318 if (!upwqp)
319 return NULL;
321 if (!tight)
322 data.tight = 0;
324 dim = isl_union_pw_qpolynomial_get_dim(upwqp);
325 data.res = isl_union_pw_qpolynomial_fold_zero(dim, type);
326 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp,
327 &bound_pw, &data) < 0)
328 goto error;
330 isl_union_pw_qpolynomial_free(upwqp);
331 if (tight)
332 *tight = data.tight;
334 return data.res;
335 error:
336 isl_union_pw_qpolynomial_free(upwqp);
337 isl_union_pw_qpolynomial_fold_free(data.res);
338 return NULL;