add isl_seq_normalize
[isl.git] / isl_constraint.c
blob89dbfe7013f159b3881bb0582e3f693ae5f6d95f
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;
12 switch (type) {
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;
24 switch (type) {
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,
33 isl_int **line)
35 struct isl_constraint *constraint;
37 if (!bmap || !line)
38 goto error;
40 constraint = isl_alloc_type(bmap->ctx, struct isl_constraint);
41 if (!constraint)
42 goto error;
44 constraint->ctx = bmap->ctx;
45 isl_ctx_ref(constraint->ctx);
46 constraint->ref = 1;
47 constraint->bmap = bmap;
48 constraint->line = line;
50 return constraint;
51 error:
52 isl_basic_map_free(bmap);
53 return NULL;
56 struct isl_constraint *isl_basic_set_constraint(struct isl_basic_set *bset,
57 isl_int **line)
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;
66 if (!dim)
67 return NULL;
69 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
70 if (!bmap)
71 return NULL;
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;
82 if (!dim)
83 return NULL;
85 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 1);
86 if (!bmap)
87 return NULL;
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)
96 if (!c)
97 return NULL;
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)
104 if (!c)
105 return NULL;
107 if (c->ref == 1)
108 return c;
109 c->ref--;
110 return isl_constraint_dup(c);
113 struct isl_constraint *isl_constraint_copy(struct isl_constraint *constraint)
115 if (!constraint)
116 return NULL;
118 constraint->ref++;
119 return constraint;
122 struct isl_constraint *isl_constraint_free(struct isl_constraint *c)
124 if (!c)
125 return;
127 if (--c->ref > 0)
128 return;
130 isl_basic_map_free(c->bmap);
131 isl_ctx_deref(c->ctx);
132 free(c);
135 struct isl_constraint *isl_basic_set_first_constraint(
136 struct isl_basic_set *bset)
138 struct isl_constraint *c;
140 if (!bset)
141 return NULL;
143 if (bset->n_eq > 0)
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);
150 return NULL;
153 struct isl_constraint *isl_constraint_next(struct isl_constraint *c)
155 c = isl_constraint_cow(c);
156 c->line++;
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)
160 return c;
161 isl_constraint_free(c);
162 return NULL;
165 int isl_constraint_is_equal(struct isl_constraint *constraint1,
166 struct isl_constraint *constraint2)
168 if (!constraint1 || !constraint2)
169 return 0;
170 return constraint1->bmap == constraint2->bmap &&
171 constraint1->line == constraint2->line;
174 struct isl_basic_map *isl_basic_map_add_constraint(
175 struct isl_basic_map *bmap, struct isl_constraint *constraint)
177 if (!bmap || !constraint)
178 goto error;
180 isl_assert(constraint->ctx,
181 isl_dim_equal(bmap->dim, constraint->bmap->dim), goto error);
183 bmap = isl_basic_map_intersect(bmap,
184 isl_basic_map_copy(constraint->bmap));
185 isl_constraint_free(constraint);
186 return bmap;
187 error:
188 isl_basic_map_free(bmap);
189 isl_constraint_free(constraint);
190 return NULL;
193 struct isl_basic_set *isl_basic_set_add_constraint(
194 struct isl_basic_set *bset, struct isl_constraint *constraint)
196 return (struct isl_basic_set *)
197 isl_basic_map_add_constraint((struct isl_basic_map *)bset,
198 constraint);
201 struct isl_constraint *isl_constraint_add_div(struct isl_constraint *constraint,
202 struct isl_div *div, int *pos)
204 if (!constraint || !div)
205 goto error;
207 isl_assert(constraint->ctx,
208 isl_dim_equal(div->bmap->dim, constraint->bmap->dim), goto error);
209 isl_assert(constraint->ctx,
210 constraint->bmap->n_eq + constraint->bmap->n_ineq == 1, goto error);
212 constraint->bmap = isl_basic_map_extend_dim(constraint->bmap,
213 isl_dim_copy(constraint->bmap->dim), 1, 0, 0);
214 if (!constraint->bmap)
215 goto error;
216 constraint->line = &constraint->bmap->eq[0];
217 *pos = isl_basic_map_alloc_div(constraint->bmap);
218 if (*pos < 0)
219 goto error;
220 isl_seq_cpy(constraint->bmap->div[*pos], div->line[0],
221 1 + 1 + isl_basic_map_total_dim(constraint->bmap));
222 isl_div_free(div);
223 return constraint;
224 error:
225 isl_constraint_free(constraint);
226 isl_div_free(div);
227 return NULL;
230 int isl_constraint_dim(struct isl_constraint *constraint,
231 enum isl_dim_type type)
233 if (!constraint)
234 return -1;
235 return n(constraint, type);
238 void isl_constraint_get_constant(struct isl_constraint *constraint, isl_int *v)
240 if (!constraint)
241 return;
242 isl_int_set(*v, constraint->line[0][0]);
245 void isl_constraint_get_coefficient(struct isl_constraint *constraint,
246 enum isl_dim_type type, int pos, isl_int *v)
248 if (!constraint)
249 return;
251 isl_assert(constraint->ctx, pos < n(constraint, type), return);
252 isl_int_set(*v, constraint->line[0][offset(constraint, type) + pos]);
255 struct isl_div *isl_constraint_div(struct isl_constraint *constraint, int pos)
257 if (!constraint)
258 return NULL;
260 isl_assert(constraint->ctx, pos < n(constraint, isl_dim_div),
261 return NULL);
262 return isl_basic_map_div(constraint->bmap, constraint->bmap->div+pos);
265 void isl_constraint_set_constant(struct isl_constraint *constraint, isl_int v)
267 if (!constraint)
268 return;
269 isl_int_set(constraint->line[0][0], v);
272 void isl_constraint_set_coefficient(struct isl_constraint *constraint,
273 enum isl_dim_type type, int pos, isl_int v)
275 if (!constraint)
276 return;
278 isl_assert(constraint->ctx, pos < n(constraint, type), return);
279 isl_int_set(constraint->line[0][offset(constraint, type) + pos], v);
282 void isl_constraint_clear(struct isl_constraint *constraint)
284 struct isl_basic_set *bset;
285 unsigned total;
287 if (!constraint)
288 return;
289 total = isl_basic_map_total_dim(constraint->bmap);
290 isl_seq_clr(constraint->line[0], 1 + total);
293 struct isl_constraint *isl_constraint_negate(struct isl_constraint *constraint)
295 unsigned total;
297 if (!constraint)
298 return NULL;
300 isl_assert(constraint->ctx, !isl_constraint_is_equality(constraint),
301 goto error);
302 isl_assert(constraint->ctx, constraint->bmap->ref == 1, goto error);
303 total = isl_basic_map_total_dim(constraint->bmap);
304 isl_seq_neg(constraint->line[0], constraint->line[0], 1 + total);
305 isl_int_sub_ui(constraint->line[0][0], constraint->line[0][0], 1);
306 ISL_F_CLR(constraint->bmap, ISL_BASIC_MAP_NORMALIZED);
307 return constraint;
308 error:
309 isl_constraint_free(constraint);
310 return NULL;
313 int isl_constraint_is_equality(struct isl_constraint *constraint)
315 if (!constraint)
316 return -1;
317 return constraint->line < constraint->bmap->eq + constraint->bmap->n_eq;
321 struct isl_basic_set *isl_basic_set_from_constraint(
322 struct isl_constraint *constraint)
324 int k;
325 struct isl_basic_set *constraint_bset, *bset;
326 isl_int *c;
327 unsigned dim;
328 unsigned nparam;
329 unsigned total;
331 if (!constraint)
332 return NULL;
334 isl_assert(constraint->ctx,n(constraint, isl_dim_in) == 0, goto error);
336 constraint_bset = (struct isl_basic_set *)constraint->bmap;
337 bset = isl_basic_set_universe_like(constraint_bset);
338 bset = isl_basic_set_align_divs(bset, constraint_bset);
339 nparam = isl_basic_set_n_param(bset);
340 dim = isl_basic_set_n_dim(bset);
341 bset = isl_basic_set_extend(bset, nparam, dim, 0, 1, 1);
342 if (isl_constraint_is_equality(constraint)) {
343 k = isl_basic_set_alloc_equality(bset);
344 if (k < 0)
345 goto error;
346 c = bset->eq[k];
348 else {
349 k = isl_basic_set_alloc_inequality(bset);
350 if (k < 0)
351 goto error;
352 c = bset->ineq[k];
354 total = isl_basic_set_total_dim(bset);
355 isl_seq_cpy(c, constraint->line[0], 1 + total);
356 isl_constraint_free(constraint);
357 return bset;
358 error:
359 isl_constraint_free(constraint);
360 isl_basic_set_free(bset);
361 return NULL;
364 int isl_basic_set_has_defining_equality(
365 struct isl_basic_set *bset, enum isl_dim_type type, int pos,
366 struct isl_constraint **c)
368 int i;
369 unsigned offset;
370 unsigned total;
372 if (!bset)
373 return -1;
374 offset = basic_set_offset(bset, type);
375 total = isl_basic_set_total_dim(bset);
376 isl_assert(bset->ctx, pos < isl_basic_set_dim(bset, type), return -1);
377 for (i = 0; i < bset->n_eq; ++i)
378 if (!isl_int_is_zero(bset->eq[i][offset + pos]) &&
379 isl_seq_first_non_zero(bset->eq[i]+offset+pos+1,
380 1+total-offset-pos-1) == -1) {
381 *c= isl_basic_set_constraint(isl_basic_set_copy(bset),
382 &bset->eq[i]);
383 return 1;
385 return 0;
388 int isl_basic_set_has_defining_inequalities(
389 struct isl_basic_set *bset, enum isl_dim_type type, int pos,
390 struct isl_constraint **lower,
391 struct isl_constraint **upper)
393 int i, j;
394 unsigned offset;
395 unsigned total;
396 isl_int m;
397 isl_int **lower_line, **upper_line;
399 if (!bset)
400 return -1;
401 offset = basic_set_offset(bset, type);
402 total = isl_basic_set_total_dim(bset);
403 isl_assert(bset->ctx, pos < isl_basic_set_dim(bset, type), return -1);
404 isl_int_init(m);
405 for (i = 0; i < bset->n_ineq; ++i) {
406 if (isl_int_is_zero(bset->ineq[i][offset + pos]))
407 continue;
408 if (isl_int_is_one(bset->ineq[i][offset + pos]))
409 continue;
410 if (isl_int_is_negone(bset->ineq[i][offset + pos]))
411 continue;
412 if (isl_seq_first_non_zero(bset->ineq[i]+offset+pos+1,
413 1+total-offset-pos-1) != -1)
414 continue;
415 for (j = i + i; j < bset->n_ineq; ++j) {
416 if (!isl_seq_is_neg(bset->ineq[i]+1, bset->ineq[j]+1,
417 total))
418 continue;
419 isl_int_add(m, bset->ineq[i][0], bset->ineq[j][0]);
420 if (isl_int_abs_ge(m, bset->ineq[i][offset+pos]))
421 continue;
423 if (isl_int_is_pos(bset->ineq[i][offset+pos])) {
424 lower_line = &bset->ineq[i];
425 upper_line = &bset->ineq[j];
426 } else {
427 lower_line = &bset->ineq[j];
428 upper_line = &bset->ineq[i];
430 *lower = isl_basic_set_constraint(
431 isl_basic_set_copy(bset), lower_line);
432 *upper = isl_basic_set_constraint(
433 isl_basic_set_copy(bset), upper_line);
434 isl_int_clear(m);
435 return 1;
438 *lower = NULL;
439 *upper = NULL;
440 isl_int_clear(m);
441 return 0;