isl_pw_qpolynomial_intersect_domain: simplify polynomials using equalities
[isl.git] / isl_pw_templ.c
blobbff9466012fdc582ac0ea38fa2784901b546c2c5
1 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
2 #define FN(TYPE,NAME) xFN(TYPE,NAME)
3 #define xS(TYPE,NAME) struct TYPE ## _ ## NAME
4 #define S(TYPE,NAME) xS(TYPE,NAME)
6 static __isl_give PW *FN(PW,alloc_)(__isl_take isl_dim *dim, int n)
8 struct PW *pw;
10 if (!dim)
11 return NULL;
12 isl_assert(dim->ctx, n >= 0, goto error);
13 pw = isl_alloc(dim->ctx, struct PW,
14 sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
15 if (!pw)
16 goto error;
18 pw->ref = 1;
19 pw->size = n;
20 pw->n = 0;
21 pw->dim = dim;
22 return pw;
23 error:
24 isl_dim_free(dim);
25 return NULL;
28 __isl_give PW *FN(PW,zero)(__isl_take isl_dim *dim)
30 return FN(PW,alloc_)(dim, 0);
33 __isl_give PW *FN(PW,add_piece)(__isl_take PW *pw,
34 __isl_take isl_set *set, __isl_take EL *el)
36 if (!pw || !set || !el)
37 goto error;
39 if (isl_set_fast_is_empty(set) || FN(EL,IS_ZERO)(el)) {
40 isl_set_free(set);
41 FN(EL,free)(el);
42 return pw;
45 isl_assert(set->ctx, isl_dim_equal(pw->dim, el->dim), goto error);
46 isl_assert(set->ctx, pw->n < pw->size, goto error);
48 pw->p[pw->n].set = set;
49 pw->p[pw->n].FIELD = el;
50 pw->n++;
52 return pw;
53 error:
54 FN(PW,free)(pw);
55 isl_set_free(set);
56 FN(EL,free)(el);
57 return NULL;
60 __isl_give PW *FN(PW,alloc)(__isl_take isl_set *set, __isl_take EL *el)
62 PW *pw;
64 if (!set || !el)
65 goto error;
67 pw = FN(PW,alloc_)(isl_set_get_dim(set), 1);
69 return FN(PW,add_piece)(pw, set, el);
70 error:
71 isl_set_free(set);
72 FN(EL,free)(el);
73 return NULL;
76 __isl_give PW *FN(PW,dup)(__isl_keep PW *pw)
78 int i;
79 PW *dup;
81 if (!pw)
82 return NULL;
84 dup = FN(PW,alloc_)(isl_dim_copy(pw->dim), pw->n);
85 if (!dup)
86 return NULL;
88 for (i = 0; i < pw->n; ++i)
89 dup = FN(PW,add_piece)(dup, isl_set_copy(pw->p[i].set),
90 FN(EL,copy)(pw->p[i].FIELD));
92 return dup;
93 error:
94 FN(PW,free)(dup);
95 return NULL;
98 __isl_give PW *FN(PW,cow)(__isl_take PW *pw)
100 if (!pw)
101 return NULL;
103 if (pw->ref == 1)
104 return pw;
105 pw->ref--;
106 return FN(PW,dup)(pw);
109 __isl_give PW *FN(PW,copy)(__isl_keep PW *pw)
111 if (!pw)
112 return NULL;
114 pw->ref++;
115 return pw;
118 void FN(PW,free)(__isl_take PW *pw)
120 int i;
122 if (!pw)
123 return;
124 if (--pw->ref > 0)
125 return;
127 for (i = 0; i < pw->n; ++i) {
128 isl_set_free(pw->p[i].set);
129 FN(EL,free)(pw->p[i].FIELD);
131 isl_dim_free(pw->dim);
132 free(pw);
135 int FN(PW,is_zero)(__isl_keep PW *pw)
137 if (!pw)
138 return -1;
140 return pw->n == 0;
143 __isl_give PW *FN(PW,add)(__isl_take PW *pw1, __isl_take PW *pw2)
145 int i, j, n;
146 struct PW *res;
147 isl_set *set;
149 if (!pw1 || !pw2)
150 goto error;
152 isl_assert(pw1->dim->ctx, isl_dim_equal(pw1->dim, pw2->dim), goto error);
154 if (FN(PW,is_zero)(pw1)) {
155 FN(PW,free)(pw1);
156 return pw2;
159 if (FN(PW,is_zero)(pw2)) {
160 FN(PW,free)(pw2);
161 return pw1;
164 n = (pw1->n + 1) * (pw2->n + 1);
165 res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), n);
167 for (i = 0; i < pw1->n; ++i) {
168 set = isl_set_copy(pw1->p[i].set);
169 for (j = 0; j < pw2->n; ++j) {
170 struct isl_set *common;
171 EL *sum;
172 set = isl_set_subtract(set,
173 isl_set_copy(pw2->p[j].set));
174 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
175 isl_set_copy(pw2->p[j].set));
176 if (isl_set_fast_is_empty(common)) {
177 isl_set_free(common);
178 continue;
181 sum = ADD(common, FN(EL,copy)(pw1->p[i].FIELD),
182 FN(EL,copy)(pw2->p[j].FIELD));
184 res = FN(PW,add_piece)(res, common, sum);
186 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw1->p[i].FIELD));
189 for (j = 0; j < pw2->n; ++j) {
190 set = isl_set_copy(pw2->p[j].set);
191 for (i = 0; i < pw1->n; ++i)
192 set = isl_set_subtract(set,
193 isl_set_copy(pw1->p[i].set));
194 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw2->p[j].FIELD));
197 FN(PW,free)(pw1);
198 FN(PW,free)(pw2);
200 return res;
201 error:
202 FN(PW,free)(pw1);
203 FN(PW,free)(pw2);
204 return NULL;
207 __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
209 int i;
210 PW *res;
212 if (!pw1 || !pw2)
213 goto error;
215 isl_assert(pw1->dim->ctx, isl_dim_equal(pw1->dim, pw2->dim), goto error);
217 if (FN(PW,is_zero)(pw1)) {
218 FN(PW,free)(pw1);
219 return pw2;
222 if (FN(PW,is_zero)(pw2)) {
223 FN(PW,free)(pw2);
224 return pw1;
227 res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), pw1->n + pw2->n);
229 for (i = 0; i < pw1->n; ++i)
230 res = FN(PW,add_piece)(res,
231 isl_set_copy(pw1->p[i].set),
232 FN(EL,copy)(pw1->p[i].FIELD));
234 for (i = 0; i < pw2->n; ++i)
235 res = FN(PW,add_piece)(res,
236 isl_set_copy(pw2->p[i].set),
237 FN(EL,copy)(pw2->p[i].FIELD));
239 FN(PW,free)(pw1);
240 FN(PW,free)(pw2);
242 return res;
243 error:
244 FN(PW,free)(pw1);
245 FN(PW,free)(pw2);
246 return NULL;
249 __isl_give isl_qpolynomial *FN(PW,eval)(__isl_take PW *pw,
250 __isl_take isl_point *pnt)
252 int i;
253 int found = 0;
254 isl_qpolynomial *qp;
256 if (!pw || !pnt)
257 goto error;
258 isl_assert(pnt->dim->ctx, isl_dim_equal(pnt->dim, pw->dim), goto error);
260 for (i = 0; i < pw->n; ++i) {
261 found = isl_set_contains_point(pw->p[i].set, pnt);
262 if (found < 0)
263 goto error;
264 if (found)
265 break;
267 if (found)
268 qp = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
269 isl_point_copy(pnt));
270 else
271 qp = isl_qpolynomial_zero(isl_dim_copy(pw->dim));
272 FN(PW,free)(pw);
273 isl_point_free(pnt);
274 return qp;
275 error:
276 FN(PW,free)(pw);
277 isl_point_free(pnt);
278 return NULL;
281 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
283 int i;
284 isl_set *dom;
286 if (!pw)
287 return NULL;
289 dom = isl_set_empty(isl_dim_copy(pw->dim));
290 for (i = 0; i < pw->n; ++i)
291 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
293 FN(PW,free)(pw);
295 return dom;
298 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw, __isl_take isl_set *set)
300 int i;
302 if (!pw || !set)
303 goto error;
305 if (pw->n == 0) {
306 isl_set_free(set);
307 return pw;
310 pw = FN(PW,cow)(pw);
311 if (!pw)
312 goto error;
314 for (i = pw->n - 1; i >= 0; --i) {
315 isl_basic_set *aff;
316 pw->p[i].set = isl_set_intersect(pw->p[i].set, isl_set_copy(set));
317 if (!pw->p[i].set)
318 goto error;
319 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
320 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD,
321 aff);
322 if (isl_set_fast_is_empty(pw->p[i].set)) {
323 isl_set_free(pw->p[i].set);
324 FN(EL,free)(pw->p[i].FIELD);
325 if (i != pw->n - 1)
326 pw->p[i] = pw->p[pw->n - 1];
327 pw->n--;
331 isl_set_free(set);
332 return pw;
333 error:
334 isl_set_free(set);
335 FN(PW,free)(pw);
336 return NULL;
339 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
341 int i;
342 isl_basic_set *hull = NULL;
344 if (!pw || !context)
345 goto error;
347 if (pw->n == 0) {
348 isl_set_free(context);
349 return pw;
352 hull = isl_set_simple_hull(isl_set_copy(context));
354 pw = FN(PW,cow)(pw);
355 if (!pw)
356 goto error;
358 for (i = 0; i < pw->n; ++i) {
359 pw->p[i].set = isl_set_gist_basic_set(pw->p[i].set,
360 isl_basic_set_copy(hull));
361 if (!pw->p[i].set)
362 goto error;
365 isl_basic_set_free(hull);
366 isl_set_free(context);
368 return pw;
369 error:
370 FN(PW,free)(pw);
371 isl_basic_set_free(hull);
372 isl_set_free(context);
373 return NULL;
376 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
378 int i, j;
380 if (!pw)
381 return NULL;
382 if (pw->n == 0)
383 return pw;
385 for (i = pw->n - 1; i >= 0; --i) {
386 for (j = i - 1; j >= 0; --j) {
387 if (!FN(EL,is_equal)(pw->p[i].FIELD, pw->p[j].FIELD))
388 continue;
389 pw->p[j].set = isl_set_union(pw->p[j].set,
390 pw->p[i].set);
391 FN(EL,free)(pw->p[i].FIELD);
392 if (i != pw->n - 1)
393 pw->p[i] = pw->p[pw->n - 1];
394 pw->n--;
395 break;
397 if (j >= 0)
398 continue;
399 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
400 if (!pw->p[i].set)
401 goto error;
404 return pw;
405 error:
406 FN(PW,free)(pw);
407 return NULL;
410 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
412 return pw ? pw->dim->ctx : NULL;
415 int FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
416 unsigned first, unsigned n)
418 int i;
420 if (!pw)
421 return -1;
422 if (pw->n == 0 || n == 0)
423 return 0;
424 for (i = 0; i < pw->n; ++i) {
425 int involves = FN(EL,involves_dims)(pw->p[i].FIELD,
426 type, first, n);
427 if (involves < 0 || involves)
428 return involves;
430 return 0;
433 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
434 enum isl_dim_type type, unsigned first, unsigned n)
436 int i;
438 if (!pw)
439 return NULL;
440 if (n == 0 && !isl_dim_get_tuple_name(pw->dim, type))
441 return pw;
443 pw = FN(PW,cow)(pw);
444 if (!pw)
445 return NULL;
446 pw->dim = isl_dim_drop(pw->dim, type, first, n);
447 if (!pw->dim)
448 goto error;
449 for (i = 0; i < pw->n; ++i) {
450 pw->p[i].set = isl_set_drop(pw->p[i].set, type, first, n);
451 if (!pw->p[i].set)
452 goto error;
453 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
454 if (!pw->p[i].FIELD)
455 goto error;
458 return pw;
459 error:
460 FN(PW,free)(pw);
461 return NULL;
464 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
465 enum isl_dim_type type, unsigned pos, isl_int v)
467 int i;
469 if (!pw)
470 return NULL;
472 pw = FN(PW,cow)(pw);
473 if (!pw)
474 return NULL;
475 for (i = 0; i < pw->n; ++i) {
476 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
477 if (!pw->p[i].set)
478 goto error;
481 return pw;
482 error:
483 FN(PW,free)(pw);
484 return NULL;
487 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
489 return pw ? isl_dim_size(pw->dim, type) : 0;
492 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
493 enum isl_dim_type type, unsigned first, unsigned n)
495 int i;
497 if (!pw)
498 return NULL;
499 if (n == 0)
500 return pw;
502 pw = FN(PW,cow)(pw);
503 if (!pw)
504 return NULL;
505 if (!pw->dim)
506 goto error;
507 for (i = 0; i < pw->n; ++i) {
508 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
509 if (!pw->p[i].set)
510 goto error;
513 return pw;
514 error:
515 FN(PW,free)(pw);
516 return NULL;
519 /* Compute the maximal value attained by the piecewise quasipolynomial
520 * on its domain or zero if the domain is empty.
521 * In the worst case, the domain is scanned completely,
522 * so the domain is assumed to be bounded.
524 __isl_give isl_qpolynomial *FN(PW,opt)(__isl_take PW *pw, int max)
526 int i;
527 isl_qpolynomial *opt;
529 if (!pw)
530 return NULL;
532 if (pw->n == 0) {
533 isl_dim *dim = isl_dim_copy(pw->dim);
534 FN(PW,free)(pw);
535 return isl_qpolynomial_zero(dim);
538 opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
539 isl_set_copy(pw->p[0].set), max);
540 for (i = 1; i < pw->n; ++i) {
541 isl_qpolynomial *opt_i;
542 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
543 isl_set_copy(pw->p[i].set), max);
544 if (max)
545 opt = isl_qpolynomial_max_cst(opt, opt_i);
546 else
547 opt = isl_qpolynomial_min_cst(opt, opt_i);
550 FN(PW,free)(pw);
551 return opt;
554 __isl_give isl_qpolynomial *FN(PW,max)(__isl_take PW *pw)
556 return FN(PW,opt)(pw, 1);
559 __isl_give isl_qpolynomial *FN(PW,min)(__isl_take PW *pw)
561 return FN(PW,opt)(pw, 0);
564 __isl_give isl_dim *FN(PW,get_dim)(__isl_keep PW *pw)
566 return pw ? isl_dim_copy(pw->dim) : NULL;
569 __isl_give PW *FN(PW,reset_dim)(__isl_take PW *pw, __isl_take isl_dim *dim)
571 int i;
573 pw = FN(PW,cow)(pw);
574 if (!pw || !dim)
575 goto error;
577 for (i = 0; i < pw->n; ++i) {
578 pw->p[i].set = isl_set_reset_dim(pw->p[i].set,
579 isl_dim_copy(dim));
580 if (!pw->p[i].set)
581 goto error;
582 pw->p[i].FIELD = FN(EL,reset_dim)(pw->p[i].FIELD,
583 isl_dim_copy(dim));
584 if (!pw->p[i].FIELD)
585 goto error;
587 isl_dim_free(pw->dim);
588 pw->dim = dim;
590 return pw;
591 error:
592 isl_dim_free(dim);
593 FN(PW,free)(pw);
594 return NULL;
597 int FN(PW,has_equal_dim)(__isl_keep PW *pw1, __isl_keep PW *pw2)
599 if (!pw1 || !pw2)
600 return -1;
602 return isl_dim_equal(pw1->dim, pw2->dim);
605 __isl_give PW *FN(PW,morph)(__isl_take PW *pw, __isl_take isl_morph *morph)
607 int i;
609 if (!pw || !morph)
610 goto error;
612 isl_assert(pw->dim->ctx, isl_dim_equal(pw->dim, morph->dom->dim),
613 goto error);
615 pw = FN(PW,cow)(pw);
616 if (!pw)
617 goto error;
618 isl_dim_free(pw->dim);
619 pw->dim = isl_dim_copy(morph->ran->dim);
620 if (!pw->dim)
621 goto error;
623 for (i = 0; i < pw->n; ++i) {
624 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
625 if (!pw->p[i].set)
626 goto error;
627 pw->p[i].FIELD = FN(EL,morph)(pw->p[i].FIELD,
628 isl_morph_copy(morph));
629 if (!pw->p[i].FIELD)
630 goto error;
633 isl_morph_free(morph);
635 return pw;
636 error:
637 FN(PW,free)(pw);
638 isl_morph_free(morph);
639 return NULL;
642 int FN(PW,foreach_piece)(__isl_keep PW *pw,
643 int (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
644 void *user)
646 int i;
648 if (!pw)
649 return -1;
651 for (i = 0; i < pw->n; ++i)
652 if (fn(isl_set_copy(pw->p[i].set),
653 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
654 return -1;
656 return 0;
659 static int any_divs(__isl_keep isl_set *set)
661 int i;
663 if (!set)
664 return -1;
666 for (i = 0; i < set->n; ++i)
667 if (set->p[i]->n_div > 0)
668 return 1;
670 return 0;
673 static int foreach_lifted_subset(__isl_take isl_set *set, __isl_take EL *el,
674 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
675 void *user), void *user)
677 int i;
679 if (!set || !el)
680 goto error;
682 for (i = 0; i < set->n; ++i) {
683 isl_set *lift;
684 EL *copy;
686 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
687 lift = isl_set_lift(lift);
689 copy = FN(EL,copy)(el);
690 copy = FN(EL,lift)(copy, isl_set_get_dim(lift));
692 if (fn(lift, copy, user) < 0)
693 goto error;
696 isl_set_free(set);
697 FN(EL,free)(el);
699 return 0;
700 error:
701 isl_set_free(set);
702 FN(EL,free)(el);
703 return -1;
706 int FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
707 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
708 void *user), void *user)
710 int i;
712 if (!pw)
713 return -1;
715 for (i = 0; i < pw->n; ++i) {
716 isl_set *set;
717 EL *el;
719 set = isl_set_copy(pw->p[i].set);
720 el = FN(EL,copy)(pw->p[i].FIELD);
721 if (!any_divs(set)) {
722 if (fn(set, el, user) < 0)
723 return -1;
724 continue;
726 if (foreach_lifted_subset(set, el, fn, user) < 0)
727 return -1;
730 return 0;
733 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
734 enum isl_dim_type dst_type, unsigned dst_pos,
735 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
737 int i;
739 pw = FN(PW,cow)(pw);
740 if (!pw)
741 return NULL;
743 pw->dim = isl_dim_move(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
744 if (!pw->dim)
745 goto error;
747 for (i = 0; i < pw->n; ++i) {
748 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
749 dst_type, dst_pos,
750 src_type, src_pos, n);
751 if (!pw->p[i].set)
752 goto error;
753 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
754 dst_type, dst_pos, src_type, src_pos, n);
755 if (!pw->p[i].FIELD)
756 goto error;
759 return pw;
760 error:
761 FN(PW,free)(pw);
762 return NULL;