1 #include <isl_constraint.h>
2 #include "isl_map_private.h"
4 static unsigned n(struct isl_constraint *c, enum isl_dim_type type)
6 return isl_basic_map_dim(c->bmap, type);
9 static unsigned offset(struct isl_constraint *c, enum isl_dim_type type)
11 struct isl_dim *dim = c->bmap->dim;
13 case isl_dim_param: return 1;
14 case isl_dim_in: return 1 + dim->nparam;
15 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
16 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
20 static unsigned basic_set_offset(struct isl_basic_set *bset,
21 enum isl_dim_type type)
23 struct isl_dim *dim = bset->dim;
25 case isl_dim_param: return 1;
26 case isl_dim_in: return 1 + dim->nparam;
27 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
28 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
32 struct isl_constraint *isl_basic_map_constraint(struct isl_basic_map *bmap,
35 struct isl_constraint *constraint;
40 constraint = isl_alloc_type(bmap->ctx, struct isl_constraint);
44 constraint->ctx = bmap->ctx;
45 isl_ctx_ref(constraint->ctx);
47 constraint->bmap = bmap;
48 constraint->line = line;
52 isl_basic_map_free(bmap);
56 struct isl_constraint *isl_basic_set_constraint(struct isl_basic_set *bset,
59 return isl_basic_map_constraint((struct isl_basic_map *)bset, line);
62 struct isl_constraint *isl_equality_alloc(struct isl_dim *dim)
64 struct isl_basic_map *bmap;
69 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
73 isl_basic_map_alloc_equality(bmap);
74 isl_seq_clr(bmap->eq[0], 1 + isl_basic_map_total_dim(bmap));
75 return isl_basic_map_constraint(bmap, &bmap->eq[0]);
78 struct isl_constraint *isl_inequality_alloc(struct isl_dim *dim)
80 struct isl_basic_map *bmap;
85 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 1);
89 isl_basic_map_alloc_inequality(bmap);
90 isl_seq_clr(bmap->ineq[0], 1 + isl_basic_map_total_dim(bmap));
91 return isl_basic_map_constraint(bmap, &bmap->ineq[0]);
94 struct isl_constraint *isl_constraint_dup(struct isl_constraint *c)
99 return isl_basic_map_constraint(isl_basic_map_copy(c->bmap), c->line);
102 struct isl_constraint *isl_constraint_cow(struct isl_constraint *c)
110 return isl_constraint_dup(c);
113 struct isl_constraint *isl_constraint_copy(struct isl_constraint *constraint)
122 struct isl_constraint *isl_constraint_free(struct isl_constraint *c)
130 isl_basic_map_free(c->bmap);
131 isl_ctx_deref(c->ctx);
135 struct isl_constraint *isl_basic_set_first_constraint(
136 struct isl_basic_set *bset)
138 struct isl_constraint *c;
144 return isl_basic_set_constraint(bset, &bset->eq[0]);
146 if (bset->n_ineq > 0)
147 return isl_basic_set_constraint(bset, &bset->ineq[0]);
149 isl_basic_set_free(bset);
153 struct isl_constraint *isl_constraint_next(struct isl_constraint *c)
155 c = isl_constraint_cow(c);
157 if (c->line >= c->bmap->eq + c->bmap->n_eq && c->line < c->bmap->ineq)
158 c->line = c->bmap->ineq;
159 if (c->line < c->bmap->ineq + c->bmap->n_ineq)
161 isl_constraint_free(c);
165 int isl_constraint_is_equal(struct isl_constraint *constraint1,
166 struct isl_constraint *constraint2)
168 if (!constraint1 || !constraint2)
170 return constraint1->bmap == constraint2->bmap &&
171 constraint1->line == constraint2->line;
174 struct isl_basic_set *isl_basic_set_add_constraint(
175 struct isl_basic_set *bset, struct isl_constraint *constraint)
177 if (!bset || !constraint)
180 isl_assert(constraint->ctx,
181 isl_dim_equal(bset->dim, constraint->bmap->dim), goto error);
183 bset = isl_basic_set_intersect(bset,
184 isl_basic_set_copy((struct isl_basic_set *)constraint->bmap));
185 isl_constraint_free(constraint);
188 isl_basic_set_free(bset);
189 isl_constraint_free(constraint);
193 int isl_constraint_dim(struct isl_constraint *constraint,
194 enum isl_dim_type type)
198 return n(constraint, type);
201 void isl_constraint_get_constant(struct isl_constraint *constraint, isl_int *v)
205 isl_int_set(*v, constraint->line[0][0]);
208 void isl_constraint_get_coefficient(struct isl_constraint *constraint,
209 enum isl_dim_type type, int pos, isl_int *v)
214 isl_assert(constraint->ctx, pos < n(constraint, type), return);
215 isl_int_set(*v, constraint->line[0][offset(constraint, type) + pos]);
218 struct isl_div *isl_constraint_div(struct isl_constraint *constraint, int pos)
223 isl_assert(constraint->ctx, pos < n(constraint, isl_dim_div),
225 return isl_basic_map_div(constraint->bmap, constraint->bmap->div+pos);
228 void isl_constraint_set_constant(struct isl_constraint *constraint, isl_int v)
232 isl_int_set(constraint->line[0][0], v);
235 void isl_constraint_set_coefficient(struct isl_constraint *constraint,
236 enum isl_dim_type type, int pos, isl_int v)
241 isl_assert(constraint->ctx, pos < n(constraint, type), return);
242 isl_int_set(constraint->line[0][offset(constraint, type) + pos], v);
245 void isl_constraint_clear(struct isl_constraint *constraint)
247 struct isl_basic_set *bset;
252 total = isl_basic_map_total_dim(constraint->bmap);
253 isl_seq_clr(constraint->line[0], 1 + total);
256 struct isl_constraint *isl_constraint_negate(struct isl_constraint *constraint)
263 isl_assert(constraint->ctx, !isl_constraint_is_equality(constraint),
265 isl_assert(constraint->ctx, constraint->bmap->ref == 1, goto error);
266 total = isl_basic_map_total_dim(constraint->bmap);
267 isl_seq_neg(constraint->line[0], constraint->line[0], 1 + total);
268 isl_int_sub_ui(constraint->line[0][0], constraint->line[0][0], 1);
269 F_CLR(constraint->bmap, ISL_BASIC_MAP_NORMALIZED);
272 isl_constraint_free(constraint);
276 int isl_constraint_is_equality(struct isl_constraint *constraint)
280 return constraint->line < constraint->bmap->eq + constraint->bmap->n_eq;
284 struct isl_basic_set *isl_basic_set_from_constraint(
285 struct isl_constraint *constraint)
288 struct isl_basic_set *constraint_bset, *bset;
297 isl_assert(constraint->ctx,n(constraint, isl_dim_in) == 0, goto error);
299 constraint_bset = (struct isl_basic_set *)constraint->bmap;
300 bset = isl_basic_set_universe_like(constraint_bset);
301 bset = isl_basic_set_align_divs(bset, constraint_bset);
302 nparam = isl_basic_set_n_param(bset);
303 dim = isl_basic_set_n_dim(bset);
304 bset = isl_basic_set_extend(bset, nparam, dim, 0, 1, 1);
305 if (isl_constraint_is_equality(constraint)) {
306 k = isl_basic_set_alloc_equality(bset);
312 k = isl_basic_set_alloc_inequality(bset);
317 total = isl_basic_set_total_dim(bset);
318 isl_seq_cpy(c, constraint->line[0], 1 + total);
319 isl_constraint_free(constraint);
322 isl_constraint_free(constraint);
323 isl_basic_set_free(bset);
327 int isl_basic_set_has_defining_equality(
328 struct isl_basic_set *bset, enum isl_dim_type type, int pos,
329 struct isl_constraint **c)
337 offset = basic_set_offset(bset, type);
338 total = isl_basic_set_total_dim(bset);
339 isl_assert(bset->ctx, pos < isl_basic_set_dim(bset, type), return -1);
340 for (i = 0; i < bset->n_eq; ++i)
341 if (!isl_int_is_zero(bset->eq[i][offset + pos]) &&
342 isl_seq_first_non_zero(bset->eq[i]+offset+pos+1,
343 1+total-offset-pos-1) == -1) {
344 *c= isl_basic_set_constraint(isl_basic_set_copy(bset),
351 int isl_basic_set_has_defining_inequalities(
352 struct isl_basic_set *bset, enum isl_dim_type type, int pos,
353 struct isl_constraint **lower,
354 struct isl_constraint **upper)
360 isl_int **lower_line, **upper_line;
364 offset = basic_set_offset(bset, type);
365 total = isl_basic_set_total_dim(bset);
366 isl_assert(bset->ctx, pos < isl_basic_set_dim(bset, type), return -1);
368 for (i = 0; i < bset->n_ineq; ++i) {
369 if (isl_int_is_zero(bset->ineq[i][offset + pos]))
371 if (isl_int_is_one(bset->ineq[i][offset + pos]))
373 if (isl_int_is_negone(bset->ineq[i][offset + pos]))
375 if (isl_seq_first_non_zero(bset->ineq[i]+offset+pos+1,
376 1+total-offset-pos-1) != -1)
378 for (j = i + i; j < bset->n_ineq; ++j) {
379 if (!isl_seq_is_neg(bset->ineq[i]+1, bset->ineq[j]+1,
382 isl_int_add(m, bset->ineq[i][0], bset->ineq[j][0]);
383 if (isl_int_abs_ge(m, bset->ineq[i][offset+pos]))
386 if (isl_int_is_pos(bset->ineq[i][offset+pos])) {
387 lower_line = &bset->ineq[i];
388 upper_line = &bset->ineq[j];
390 lower_line = &bset->ineq[j];
391 upper_line = &bset->ineq[i];
393 *lower = isl_basic_set_constraint(
394 isl_basic_set_copy(bset), lower_line);
395 *upper = isl_basic_set_constraint(
396 isl_basic_set_copy(bset), upper_line);