make isl_pw_*_{take,restore}_space static
[isl.git] / isl_pw_templ.c
blob7c504e35022818948331002d364ca70ac7885f59
1 /*
2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * 91893 Orsay, France
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 #include <isl/id.h>
15 #include <isl/aff.h>
16 #include <isl_sort.h>
17 #include <isl_val_private.h>
19 #include <isl_pw_macro.h>
21 #include "opt_type.h"
23 __isl_give PW *FN(PW,alloc_size)(__isl_take isl_space *space
24 OPT_TYPE_PARAM, int n)
26 isl_ctx *ctx;
27 struct PW *pw;
29 if (!space)
30 return NULL;
31 ctx = isl_space_get_ctx(space);
32 isl_assert(ctx, n >= 0, goto error);
33 pw = isl_alloc(ctx, struct PW,
34 sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
35 if (!pw)
36 goto error;
38 pw->ref = 1;
39 OPT_SET_TYPE(pw->, type);
40 pw->size = n;
41 pw->n = 0;
42 pw->dim = space;
43 return pw;
44 error:
45 isl_space_free(space);
46 return NULL;
49 __isl_give PW *FN(PW,ZERO)(__isl_take isl_space *space OPT_TYPE_PARAM)
51 return FN(PW,alloc_size)(space OPT_TYPE_ARG(NO_LOC), 0);
54 /* Add a piece with domain "set" and base expression "el"
55 * to the piecewise expression "pw".
57 * Do this independently of the values of "set" and "el",
58 * such that this function can be used by isl_pw_*_dup.
60 static __isl_give PW *FN(PW,add_dup_piece)(__isl_take PW *pw,
61 __isl_take isl_set *set, __isl_take EL *el)
63 isl_ctx *ctx;
64 isl_space *el_dim = NULL;
66 if (!pw || !set || !el)
67 goto error;
69 ctx = isl_set_get_ctx(set);
70 if (!OPT_EQUAL_TYPES(pw->, el->))
71 isl_die(ctx, isl_error_invalid, "fold types don't match",
72 goto error);
73 el_dim = FN(EL,get_space(el));
74 isl_assert(ctx, isl_space_is_equal(pw->dim, el_dim), goto error);
75 isl_assert(ctx, pw->n < pw->size, goto error);
77 pw->p[pw->n].set = set;
78 pw->p[pw->n].FIELD = el;
79 pw->n++;
81 isl_space_free(el_dim);
82 return pw;
83 error:
84 isl_space_free(el_dim);
85 FN(PW,free)(pw);
86 isl_set_free(set);
87 FN(EL,free)(el);
88 return NULL;
91 /* Add a piece with domain "set" and base expression "el"
92 * to the piecewise expression "pw", provided the domain
93 * is not obviously empty and the base expression
94 * is not equal to the default value.
96 __isl_give PW *FN(PW,add_piece)(__isl_take PW *pw,
97 __isl_take isl_set *set, __isl_take EL *el)
99 isl_bool skip;
101 skip = isl_set_plain_is_empty(set);
102 if (skip >= 0 && !skip)
103 skip = FN(EL,EL_IS_ZERO)(el);
104 if (skip >= 0 && !skip)
105 return FN(PW,add_dup_piece)(pw, set, el);
107 isl_set_free(set);
108 FN(EL,free)(el);
109 if (skip < 0)
110 return FN(PW,free)(pw);
111 return pw;
114 /* Does the space of "set" correspond to that of the domain of "el".
116 static isl_bool FN(PW,compatible_domain)(__isl_keep EL *el,
117 __isl_keep isl_set *set)
119 isl_bool ok;
120 isl_space *el_space, *set_space;
122 if (!set || !el)
123 return isl_bool_error;
124 set_space = isl_set_get_space(set);
125 el_space = FN(EL,get_space)(el);
126 ok = isl_space_is_domain_internal(set_space, el_space);
127 isl_space_free(el_space);
128 isl_space_free(set_space);
129 return ok;
132 /* Check that the space of "set" corresponds to that of the domain of "el".
134 static isl_stat FN(PW,check_compatible_domain)(__isl_keep EL *el,
135 __isl_keep isl_set *set)
137 isl_bool ok;
139 ok = FN(PW,compatible_domain)(el, set);
140 if (ok < 0)
141 return isl_stat_error;
142 if (!ok)
143 isl_die(isl_set_get_ctx(set), isl_error_invalid,
144 "incompatible spaces", return isl_stat_error);
146 return isl_stat_ok;
149 __isl_give PW *FN(PW,alloc)(OPT_TYPE_PARAM_FIRST
150 __isl_take isl_set *set, __isl_take EL *el)
152 PW *pw;
154 if (FN(PW,check_compatible_domain)(el, set) < 0)
155 goto error;
157 pw = FN(PW,alloc_size)(FN(EL,get_space)(el) OPT_TYPE_ARG(NO_LOC), 1);
159 return FN(PW,add_piece)(pw, set, el);
160 error:
161 isl_set_free(set);
162 FN(EL,free)(el);
163 return NULL;
166 __isl_give PW *FN(PW,dup)(__isl_keep PW *pw)
168 int i;
169 PW *dup;
171 if (!pw)
172 return NULL;
174 dup = FN(PW,alloc_size)(isl_space_copy(pw->dim)
175 OPT_TYPE_ARG(pw->), pw->n);
176 if (!dup)
177 return NULL;
179 for (i = 0; i < pw->n; ++i)
180 dup = FN(PW,add_dup_piece)(dup, isl_set_copy(pw->p[i].set),
181 FN(EL,copy)(pw->p[i].FIELD));
183 return dup;
186 __isl_give PW *FN(PW,cow)(__isl_take PW *pw)
188 if (!pw)
189 return NULL;
191 if (pw->ref == 1)
192 return pw;
193 pw->ref--;
194 return FN(PW,dup)(pw);
197 __isl_give PW *FN(PW,copy)(__isl_keep PW *pw)
199 if (!pw)
200 return NULL;
202 pw->ref++;
203 return pw;
206 __isl_null PW *FN(PW,free)(__isl_take PW *pw)
208 int i;
210 if (!pw)
211 return NULL;
212 if (--pw->ref > 0)
213 return NULL;
215 for (i = 0; i < pw->n; ++i) {
216 isl_set_free(pw->p[i].set);
217 FN(EL,free)(pw->p[i].FIELD);
219 isl_space_free(pw->dim);
220 free(pw);
222 return NULL;
225 /* Return the space of "pw".
227 __isl_keep isl_space *FN(PW,peek_space)(__isl_keep PW *pw)
229 return pw ? pw->dim : NULL;
232 __isl_give isl_space *FN(PW,get_space)(__isl_keep PW *pw)
234 return isl_space_copy(FN(PW,peek_space)(pw));
237 /* Return the space of "pw".
238 * This may be either a copy or the space itself
239 * if there is only one reference to "pw".
240 * This allows the space to be modified inplace
241 * if both the piecewise expression and its space have only a single reference.
242 * The caller is not allowed to modify "pw" between this call and
243 * a subsequent call to isl_pw_*_restore_*.
244 * The only exception is that isl_pw_*_free can be called instead.
246 static __isl_give isl_space *FN(PW,take_space)(__isl_keep PW *pw)
248 isl_space *space;
250 if (!pw)
251 return NULL;
252 if (pw->ref != 1)
253 return FN(PW,get_space)(pw);
254 space = pw->dim;
255 pw->dim = NULL;
256 return space;
259 /* Set the space of "pw" to "space", where the space of "pw" may be missing
260 * due to a preceding call to isl_pw_*_take_space.
261 * However, in this case, "pw" only has a single reference and
262 * then the call to isl_pw_*_cow has no effect.
264 static __isl_give PW *FN(PW,restore_space)(__isl_take PW *pw,
265 __isl_take isl_space *space)
267 if (!pw || !space)
268 goto error;
270 if (pw->dim == space) {
271 isl_space_free(space);
272 return pw;
275 pw = FN(PW,cow)(pw);
276 if (!pw)
277 goto error;
278 isl_space_free(pw->dim);
279 pw->dim = space;
281 return pw;
282 error:
283 FN(PW,free)(pw);
284 isl_space_free(space);
285 return NULL;
288 /* Check that "pos" is a valid position for a cell in "pw".
290 static isl_stat FN(PW,check_pos)(__isl_keep PW *pw, int pos)
292 if (!pw)
293 return isl_stat_error;
294 if (pos < 0 || pos >= pw->n)
295 isl_die(FN(PW,get_ctx)(pw), isl_error_internal,
296 "position out of bounds", return isl_stat_error);
297 return isl_stat_ok;
300 /* Return the cell at position "pos" in "pw".
302 static __isl_keep isl_set *FN(PW,peek_domain_at)(__isl_keep PW *pw, int pos)
304 if (FN(PW,check_pos)(pw, pos) < 0)
305 return NULL;
306 return pw->p[pos].set;
309 /* Return a copy of the cell at position "pos" in "pw".
311 __isl_give isl_set *FN(PW,get_domain_at)(__isl_keep PW *pw, int pos)
313 return isl_set_copy(FN(PW,peek_domain_at)(pw, pos));
316 /* Return the cell at position "pos" in "pw".
317 * This may be either a copy or the cell itself
318 * if there is only one reference to "pw".
319 * This allows the cell to be modified inplace
320 * if both the piecewise expression and this cell
321 * have only a single reference.
322 * The caller is not allowed to modify "pw" between this call and
323 * the subsequent call to isl_pw_*_restore_domain_at.
324 * The only exception is that isl_pw_*_free can be called instead.
326 static __isl_give isl_set *FN(PW,take_domain_at)(__isl_keep PW *pw, int pos)
328 isl_set *domain;
330 if (!pw)
331 return NULL;
332 if (pw->ref != 1)
333 return FN(PW,get_domain_at)(pw, pos);
334 if (FN(PW,check_pos)(pw, pos) < 0)
335 return NULL;
336 domain = pw->p[pos].set;
337 pw->p[pos].set = NULL;
338 return domain;
341 /* Set the cell at position "pos" in "pw" to "el",
342 * where this cell may be missing
343 * due to a preceding call to isl_pw_*_take_domain_at.
344 * However, in this case, "pw" only has a single reference and
345 * then the call to isl_pw_*_cow has no effect.
347 static __isl_give PW *FN(PW,restore_domain_at)(__isl_take PW *pw, int pos,
348 __isl_take isl_set *domain)
350 if (FN(PW,check_pos)(pw, pos) < 0 || !domain)
351 goto error;
353 if (pw->p[pos].set == domain) {
354 isl_set_free(domain);
355 return pw;
358 pw = FN(PW,cow)(pw);
359 if (!pw)
360 goto error;
361 isl_set_free(pw->p[pos].set);
362 pw->p[pos].set = domain;
364 return pw;
365 error:
366 FN(PW,free)(pw);
367 isl_set_free(domain);
368 return NULL;
371 /* Return the base expression associated to
372 * the cell at position "pos" in "pw".
374 static __isl_keep EL *FN(PW,peek_base_at)(__isl_keep PW *pw, int pos)
376 if (FN(PW,check_pos)(pw, pos) < 0)
377 return NULL;
378 return pw->p[pos].FIELD;
381 /* Return a copy of the base expression associated to
382 * the cell at position "pos" in "pw".
384 __isl_give EL *FN(PW,get_base_at)(__isl_keep PW *pw, int pos)
386 return FN(EL,copy)(FN(PW,peek_base_at)(pw, pos));
389 /* Return the base expression associated to
390 * the cell at position "pos" in "pw".
391 * This may be either a copy or the base expression itself
392 * if there is only one reference to "pw".
393 * This allows the base expression to be modified inplace
394 * if both the piecewise expression and this base expression
395 * have only a single reference.
396 * The caller is not allowed to modify "pw" between this call and
397 * a subsequent call to isl_pw_*_restore_*.
398 * The only exception is that isl_pw_*_free can be called instead.
400 __isl_give EL *FN(PW,take_base_at)(__isl_keep PW *pw, int pos)
402 EL *el;
404 if (!pw)
405 return NULL;
406 if (pw->ref != 1)
407 return FN(PW,get_base_at)(pw, pos);
408 if (FN(PW,check_pos)(pw, pos) < 0)
409 return NULL;
410 el = pw->p[pos].FIELD;
411 pw->p[pos].FIELD = NULL;
412 return el;
415 /* Set the base expression associated to
416 * the cell at position "pos" in "pw" to "el",
417 * where this base expression may be missing
418 * due to a preceding call to isl_pw_*_take_base_at.
419 * However, in this case, "pw" only has a single reference and
420 * then the call to isl_pw_*_cow has no effect.
421 * If "inplace" is set, then replacing the base expression by "el"
422 * is known not to change the meaning of "pw". It can therefore be replaced
423 * in all references to "pw".
425 static __isl_give PW *FN(PW,restore_base_at_)(__isl_take PW *pw, int pos,
426 __isl_take EL *el, int inplace)
428 if (FN(PW,check_pos)(pw, pos) < 0 || !el)
429 goto error;
431 if (pw->p[pos].FIELD == el) {
432 FN(EL,free)(el);
433 return pw;
436 if (!inplace)
437 pw = FN(PW,cow)(pw);
438 if (!pw)
439 goto error;
440 FN(EL,free)(pw->p[pos].FIELD);
441 pw->p[pos].FIELD = el;
443 return pw;
444 error:
445 FN(PW,free)(pw);
446 FN(EL,free)(el);
447 return NULL;
450 /* Set the base expression associated to
451 * the cell at position "pos" in "pw" to "el",
452 * where this base expression may be missing
453 * due to a preceding call to isl_pw_*_take_base_at.
455 __isl_give PW *FN(PW,restore_base_at)(__isl_take PW *pw, int pos,
456 __isl_take EL *el)
458 return FN(PW,restore_base_at_)(pw, pos, el, 0);
461 /* Set the base expression associated to
462 * the cell at position "pos" in "pw" to "el",
463 * where this base expression may be missing
464 * due to a preceding call to isl_pw_*_take_base_at.
465 * Furthermore, replacing the base expression by "el"
466 * is known not to change the meaning of "pw".
468 static __isl_give PW *FN(PW,restore_base_at_inplace)(__isl_take PW *pw, int pos,
469 __isl_take EL *el)
471 return FN(PW,restore_base_at_)(pw, pos, el, 1);
474 /* Create a piecewise expression with the given base expression on a universe
475 * domain.
477 static __isl_give PW *FN(FN(FN(PW,from),BASE),type_base)(__isl_take EL *el
478 OPT_TYPE_PARAM)
480 isl_set *dom = isl_set_universe(FN(EL,get_domain_space)(el));
481 return FN(PW,alloc)(OPT_TYPE_ARG_FIRST(NO_LOC) dom, el);
484 /* Create a piecewise expression with the given base expression on a universe
485 * domain.
487 * If the default value of this piecewise type is zero and
488 * if "el" is effectively zero, then create an empty piecewise expression
489 * instead.
491 static __isl_give PW *FN(FN(FN(PW,from),BASE),type)(__isl_take EL *el
492 OPT_TYPE_PARAM)
494 isl_bool is_zero;
495 isl_space *space;
497 if (!DEFAULT_IS_ZERO)
498 return FN(FN(FN(PW,from),BASE),type_base)(el
499 OPT_TYPE_ARG(NO_LOC));
500 is_zero = FN(EL,EL_IS_ZERO)(el);
501 if (is_zero < 0)
502 goto error;
503 if (!is_zero)
504 return FN(FN(FN(PW,from),BASE),type_base)(el
505 OPT_TYPE_ARG(NO_LOC));
506 space = FN(EL,get_space)(el);
507 FN(EL,free)(el);
508 return FN(PW,ZERO)(space OPT_TYPE_ARG(NO_LOC));
509 error:
510 FN(EL,free)(el);
511 return NULL;
514 #ifdef HAS_TYPE
515 /* Create a piecewise expression with the given base expression on a universe
516 * domain.
518 * Pass along the type as an extra argument for improved uniformity
519 * with piecewise types that do not have a fold type.
521 __isl_give PW *FN(FN(PW,from),BASE)(__isl_take EL *el)
523 enum isl_fold type = FN(EL,get_type)(el);
524 return FN(FN(FN(PW,from),BASE),type)(el, type);
526 #else
527 __isl_give PW *FN(FN(PW,from),BASE)(__isl_take EL *el)
529 return FN(FN(FN(PW,from),BASE),type)(el);
531 #endif
533 const char *FN(PW,get_dim_name)(__isl_keep PW *pw, enum isl_dim_type type,
534 unsigned pos)
536 return pw ? isl_space_get_dim_name(pw->dim, type, pos) : NULL;
539 isl_bool FN(PW,has_dim_id)(__isl_keep PW *pw, enum isl_dim_type type,
540 unsigned pos)
542 return pw ? isl_space_has_dim_id(pw->dim, type, pos) : isl_bool_error;
545 __isl_give isl_id *FN(PW,get_dim_id)(__isl_keep PW *pw, enum isl_dim_type type,
546 unsigned pos)
548 return pw ? isl_space_get_dim_id(pw->dim, type, pos) : NULL;
551 isl_bool FN(PW,has_tuple_name)(__isl_keep PW *pw, enum isl_dim_type type)
553 return pw ? isl_space_has_tuple_name(pw->dim, type) : isl_bool_error;
556 const char *FN(PW,get_tuple_name)(__isl_keep PW *pw, enum isl_dim_type type)
558 return pw ? isl_space_get_tuple_name(pw->dim, type) : NULL;
561 isl_bool FN(PW,has_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
563 return pw ? isl_space_has_tuple_id(pw->dim, type) : isl_bool_error;
566 __isl_give isl_id *FN(PW,get_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
568 return pw ? isl_space_get_tuple_id(pw->dim, type) : NULL;
571 isl_bool FN(PW,IS_ZERO)(__isl_keep PW *pw)
573 if (!pw)
574 return isl_bool_error;
576 return isl_bool_ok(pw->n == 0);
579 static __isl_give PW *FN(PW,realign_domain)(__isl_take PW *pw,
580 __isl_take isl_reordering *exp)
582 int i;
583 isl_size n;
585 n = FN(PW,n_piece)(pw);
586 if (n < 0 || !exp)
587 goto error;
589 for (i = 0; i < n; ++i) {
590 isl_set *domain;
591 EL *el;
593 domain = FN(PW,take_domain_at)(pw, i);
594 domain = isl_set_realign(domain, isl_reordering_copy(exp));
595 pw = FN(PW,restore_domain_at)(pw, i, domain);
597 el = FN(PW,take_base_at)(pw, i);
598 el = FN(EL,realign_domain)(el, isl_reordering_copy(exp));
599 pw = FN(PW,restore_base_at)(pw, i, el);
602 pw = FN(PW,reset_domain_space)(pw, isl_reordering_get_space(exp));
604 isl_reordering_free(exp);
605 return pw;
606 error:
607 isl_reordering_free(exp);
608 FN(PW,free)(pw);
609 return NULL;
612 #undef TYPE
613 #define TYPE PW
615 #include "isl_check_named_params_templ.c"
617 /* Align the parameters of "pw" to those of "model".
619 __isl_give PW *FN(PW,align_params)(__isl_take PW *pw, __isl_take isl_space *model)
621 isl_ctx *ctx;
622 isl_bool equal_params;
624 if (!pw || !model)
625 goto error;
627 ctx = isl_space_get_ctx(model);
628 if (!isl_space_has_named_params(model))
629 isl_die(ctx, isl_error_invalid,
630 "model has unnamed parameters", goto error);
631 if (FN(PW,check_named_params)(pw) < 0)
632 goto error;
633 equal_params = isl_space_has_equal_params(pw->dim, model);
634 if (equal_params < 0)
635 goto error;
636 if (!equal_params) {
637 isl_space *space;
638 isl_reordering *exp;
640 space = FN(PW,get_domain_space)(pw);
641 exp = isl_parameter_alignment_reordering(space, model);
642 isl_space_free(space);
643 pw = FN(PW,realign_domain)(pw, exp);
646 isl_space_free(model);
647 return pw;
648 error:
649 isl_space_free(model);
650 FN(PW,free)(pw);
651 return NULL;
654 #undef TYPE
655 #define TYPE PW
657 static
658 #include "isl_align_params_bin_templ.c"
660 #undef SUFFIX
661 #define SUFFIX set
662 #undef ARG1
663 #define ARG1 PW
664 #undef ARG2
665 #define ARG2 isl_set
667 static
668 #include "isl_align_params_templ.c"
670 #undef TYPE
671 #define TYPE PW
673 #include "isl_type_has_equal_space_bin_templ.c"
674 #include "isl_type_check_equal_space_templ.c"
676 /* Private version of "union_add". For isl_pw_qpolynomial and
677 * isl_pw_qpolynomial_fold, we prefer to simply call it "add".
679 static __isl_give PW *FN(PW,union_add_)(__isl_take PW *pw1, __isl_take PW *pw2)
681 int i, j, n;
682 struct PW *res;
683 isl_ctx *ctx;
684 isl_set *set;
686 if (FN(PW,align_params_bin)(&pw1, &pw2) < 0)
687 goto error;
689 ctx = isl_space_get_ctx(pw1->dim);
690 if (!OPT_EQUAL_TYPES(pw1->, pw2->))
691 isl_die(ctx, isl_error_invalid,
692 "fold types don't match", goto error);
693 if (FN(PW,check_equal_space)(pw1, pw2) < 0)
694 goto error;
696 if (FN(PW,IS_ZERO)(pw1)) {
697 FN(PW,free)(pw1);
698 return pw2;
701 if (FN(PW,IS_ZERO)(pw2)) {
702 FN(PW,free)(pw2);
703 return pw1;
706 n = (pw1->n + 1) * (pw2->n + 1);
707 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim)
708 OPT_TYPE_ARG(pw1->), n);
710 for (i = 0; i < pw1->n; ++i) {
711 set = isl_set_copy(pw1->p[i].set);
712 for (j = 0; j < pw2->n; ++j) {
713 struct isl_set *common;
714 EL *sum;
715 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
716 isl_set_copy(pw2->p[j].set));
717 if (isl_set_plain_is_empty(common)) {
718 isl_set_free(common);
719 continue;
721 set = isl_set_subtract(set,
722 isl_set_copy(pw2->p[j].set));
724 sum = FN(EL,add_on_domain)(common,
725 FN(EL,copy)(pw1->p[i].FIELD),
726 FN(EL,copy)(pw2->p[j].FIELD));
728 res = FN(PW,add_piece)(res, common, sum);
730 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw1->p[i].FIELD));
733 for (j = 0; j < pw2->n; ++j) {
734 set = isl_set_copy(pw2->p[j].set);
735 for (i = 0; i < pw1->n; ++i)
736 set = isl_set_subtract(set,
737 isl_set_copy(pw1->p[i].set));
738 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw2->p[j].FIELD));
741 FN(PW,free)(pw1);
742 FN(PW,free)(pw2);
744 return res;
745 error:
746 FN(PW,free)(pw1);
747 FN(PW,free)(pw2);
748 return NULL;
751 /* Make sure "pw" has room for at least "n" more pieces.
753 * If there is only one reference to pw, we extend it in place.
754 * Otherwise, we create a new PW and copy the pieces.
756 static __isl_give PW *FN(PW,grow)(__isl_take PW *pw, int n)
758 int i;
759 isl_ctx *ctx;
760 PW *res;
762 if (!pw)
763 return NULL;
764 if (pw->n + n <= pw->size)
765 return pw;
766 ctx = FN(PW,get_ctx)(pw);
767 n += pw->n;
768 if (pw->ref == 1) {
769 res = isl_realloc(ctx, pw, struct PW,
770 sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
771 if (!res)
772 return FN(PW,free)(pw);
773 res->size = n;
774 return res;
776 res = FN(PW,alloc_size)(isl_space_copy(pw->dim) OPT_TYPE_ARG(pw->), n);
777 if (!res)
778 return FN(PW,free)(pw);
779 for (i = 0; i < pw->n; ++i)
780 res = FN(PW,add_piece)(res, isl_set_copy(pw->p[i].set),
781 FN(EL,copy)(pw->p[i].FIELD));
782 FN(PW,free)(pw);
783 return res;
786 __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
788 int i;
789 isl_ctx *ctx;
791 if (FN(PW,align_params_bin)(&pw1, &pw2) < 0)
792 goto error;
794 if (pw1->size < pw1->n + pw2->n && pw1->n < pw2->n)
795 return FN(PW,add_disjoint)(pw2, pw1);
797 ctx = isl_space_get_ctx(pw1->dim);
798 if (!OPT_EQUAL_TYPES(pw1->, pw2->))
799 isl_die(ctx, isl_error_invalid,
800 "fold types don't match", goto error);
801 if (FN(PW,check_equal_space)(pw1, pw2) < 0)
802 goto error;
804 if (FN(PW,IS_ZERO)(pw1)) {
805 FN(PW,free)(pw1);
806 return pw2;
809 if (FN(PW,IS_ZERO)(pw2)) {
810 FN(PW,free)(pw2);
811 return pw1;
814 pw1 = FN(PW,grow)(pw1, pw2->n);
815 if (!pw1)
816 goto error;
818 for (i = 0; i < pw2->n; ++i)
819 pw1 = FN(PW,add_piece)(pw1,
820 isl_set_copy(pw2->p[i].set),
821 FN(EL,copy)(pw2->p[i].FIELD));
823 FN(PW,free)(pw2);
825 return pw1;
826 error:
827 FN(PW,free)(pw1);
828 FN(PW,free)(pw2);
829 return NULL;
832 /* This function is currently only used from isl_aff.c
834 static __isl_give PW *FN(PW,on_shared_domain_in)(__isl_take PW *pw1,
835 __isl_take PW *pw2, __isl_take isl_space *space,
836 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
837 __attribute__ ((unused));
839 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
840 * The result of "fn" (and therefore also of this function) lives in "space".
842 static __isl_give PW *FN(PW,on_shared_domain_in)(__isl_take PW *pw1,
843 __isl_take PW *pw2, __isl_take isl_space *space,
844 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
846 int i, j, n;
847 PW *res = NULL;
849 if (!pw1 || !pw2)
850 goto error;
852 n = pw1->n * pw2->n;
853 res = FN(PW,alloc_size)(isl_space_copy(space) OPT_TYPE_ARG(pw1->), n);
855 for (i = 0; i < pw1->n; ++i) {
856 for (j = 0; j < pw2->n; ++j) {
857 isl_set *common;
858 EL *res_ij;
859 int empty;
861 common = isl_set_intersect(
862 isl_set_copy(pw1->p[i].set),
863 isl_set_copy(pw2->p[j].set));
864 empty = isl_set_plain_is_empty(common);
865 if (empty < 0 || empty) {
866 isl_set_free(common);
867 if (empty < 0)
868 goto error;
869 continue;
872 res_ij = fn(FN(EL,copy)(pw1->p[i].FIELD),
873 FN(EL,copy)(pw2->p[j].FIELD));
874 res_ij = FN(EL,gist)(res_ij, isl_set_copy(common));
876 res = FN(PW,add_piece)(res, common, res_ij);
880 isl_space_free(space);
881 FN(PW,free)(pw1);
882 FN(PW,free)(pw2);
883 return res;
884 error:
885 isl_space_free(space);
886 FN(PW,free)(pw1);
887 FN(PW,free)(pw2);
888 FN(PW,free)(res);
889 return NULL;
892 /* This function is currently only used from isl_aff.c
894 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
895 __isl_take PW *pw2,
896 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
897 __attribute__ ((unused));
899 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
900 * The result of "fn" is assumed to live in the same space as "pw1" and "pw2".
902 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
903 __isl_take PW *pw2,
904 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
906 isl_space *space;
908 if (FN(PW,check_equal_space)(pw1, pw2) < 0)
909 goto error;
911 space = isl_space_copy(pw1->dim);
912 return FN(PW,on_shared_domain_in)(pw1, pw2, space, fn);
913 error:
914 FN(PW,free)(pw1);
915 FN(PW,free)(pw2);
916 return NULL;
919 /* Return the parameter domain of "pw".
921 __isl_give isl_set *FN(PW,params)(__isl_take PW *pw)
923 return isl_set_params(FN(PW,domain)(pw));
926 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
928 int i;
929 isl_set *dom;
931 if (!pw)
932 return NULL;
934 dom = isl_set_empty(FN(PW,get_domain_space)(pw));
935 for (i = 0; i < pw->n; ++i)
936 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
938 FN(PW,free)(pw);
940 return dom;
943 /* Exploit the equalities in the domain of piece "i" of "pw"
944 * to simplify the associated function.
945 * If the domain of piece "i" is empty, then remove it entirely,
946 * replacing it with the final piece.
948 static __isl_give PW *FN(PW,exploit_equalities_and_remove_if_empty)(
949 __isl_take PW *pw, int i)
951 EL *el;
952 isl_set *domain;
953 isl_basic_set *aff;
954 int empty;
956 domain = FN(PW,peek_domain_at)(pw, i);
957 empty = isl_set_plain_is_empty(domain);
958 if (empty < 0)
959 return FN(PW,free)(pw);
960 if (empty) {
961 isl_set_free(pw->p[i].set);
962 FN(EL,free)(pw->p[i].FIELD);
963 if (i != pw->n - 1)
964 pw->p[i] = pw->p[pw->n - 1];
965 pw->n--;
967 return pw;
970 aff = isl_set_affine_hull(FN(PW,get_domain_at)(pw, i));
971 el = FN(PW,take_base_at)(pw, i);
972 el = FN(EL,substitute_equalities)(el, aff);
973 pw = FN(PW,restore_base_at_inplace)(pw, i, el);
975 return pw;
978 /* Convert a piecewise expression defined over a parameter domain
979 * into one that is defined over a zero-dimensional set.
981 __isl_give PW *FN(PW,from_range)(__isl_take PW *pw)
983 isl_space *space;
985 if (!pw)
986 return NULL;
987 if (!isl_space_is_set(pw->dim))
988 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
989 "not living in a set space", return FN(PW,free)(pw));
991 space = FN(PW,get_space)(pw);
992 space = isl_space_from_range(space);
993 pw = FN(PW,reset_space)(pw, space);
995 return pw;
998 /* Fix the value of the given parameter or domain dimension of "pw"
999 * to be equal to "value".
1001 __isl_give PW *FN(PW,fix_si)(__isl_take PW *pw, enum isl_dim_type type,
1002 unsigned pos, int value)
1004 int i;
1005 isl_size n;
1007 n = FN(PW,n_piece)(pw);
1008 if (n < 0)
1009 return FN(PW,free)(pw);
1011 if (type == isl_dim_out)
1012 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
1013 "cannot fix output dimension", return FN(PW,free)(pw));
1015 if (type == isl_dim_in)
1016 type = isl_dim_set;
1018 for (i = n - 1; i >= 0; --i) {
1019 isl_set *domain;
1021 domain = FN(PW,take_domain_at)(pw, i);
1022 domain = isl_set_fix_si(domain, type, pos, value);
1023 pw = FN(PW,restore_domain_at)(pw, i, domain);
1024 pw = FN(PW,exploit_equalities_and_remove_if_empty)(pw, i);
1027 return pw;
1030 /* Restrict the domain of "pw" by combining each cell
1031 * with "set" through a call to "fn", where "fn" may be
1032 * isl_set_intersect, isl_set_intersect_params, isl_set_intersect_factor_domain,
1033 * isl_set_intersect_factor_range or isl_set_subtract.
1035 static __isl_give PW *FN(PW,restrict_domain)(__isl_take PW *pw,
1036 __isl_take isl_set *set,
1037 __isl_give isl_set *(*fn)(__isl_take isl_set *set1,
1038 __isl_take isl_set *set2))
1040 int i;
1041 isl_size n;
1043 FN(PW,align_params_set)(&pw, &set);
1044 n = FN(PW,n_piece)(pw);
1045 if (n < 0 || !set)
1046 goto error;
1048 for (i = n - 1; i >= 0; --i) {
1049 isl_set *domain;
1051 domain = FN(PW,take_domain_at)(pw, i);
1052 domain = fn(domain, isl_set_copy(set));
1053 pw = FN(PW,restore_domain_at)(pw, i, domain);
1054 pw = FN(PW,exploit_equalities_and_remove_if_empty)(pw, i);
1057 isl_set_free(set);
1058 return pw;
1059 error:
1060 isl_set_free(set);
1061 FN(PW,free)(pw);
1062 return NULL;
1065 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw,
1066 __isl_take isl_set *context)
1068 return FN(PW,restrict_domain)(pw, context, &isl_set_intersect);
1071 /* Intersect the domain of "pw" with the parameter domain "context".
1073 __isl_give PW *FN(PW,intersect_params)(__isl_take PW *pw,
1074 __isl_take isl_set *context)
1076 return FN(PW,restrict_domain)(pw, context, &isl_set_intersect_params);
1079 /* Given a piecewise expression "pw" with domain in a space [A -> B] and
1080 * a set in the space A, intersect the domain with the set.
1082 __isl_give PW *FN(PW,intersect_domain_wrapped_domain)(__isl_take PW *pw,
1083 __isl_take isl_set *set)
1085 return FN(PW,restrict_domain)(pw, set,
1086 &isl_set_intersect_factor_domain);
1089 /* Given a piecewise expression "pw" with domain in a space [A -> B] and
1090 * a set in the space B, intersect the domain with the set.
1092 __isl_give PW *FN(PW,intersect_domain_wrapped_range)(__isl_take PW *pw,
1093 __isl_take isl_set *set)
1095 return FN(PW,restrict_domain)(pw, set, &isl_set_intersect_factor_range);
1098 /* Subtract "domain' from the domain of "pw".
1100 __isl_give PW *FN(PW,subtract_domain)(__isl_take PW *pw,
1101 __isl_take isl_set *domain)
1103 return FN(PW,restrict_domain)(pw, domain, &isl_set_subtract);
1106 /* Return -1 if the piece "p1" should be sorted before "p2"
1107 * and 1 if it should be sorted after "p2".
1108 * Return 0 if they do not need to be sorted in a specific order.
1110 * The two pieces are compared on the basis of their function value expressions.
1112 static int FN(PW,sort_field_cmp)(const void *p1, const void *p2, void *arg)
1114 struct FN(PW,piece) const *pc1 = p1;
1115 struct FN(PW,piece) const *pc2 = p2;
1117 return FN(EL,plain_cmp)(pc1->FIELD, pc2->FIELD);
1120 /* Sort the pieces of "pw" according to their function value
1121 * expressions and then combine pairs of adjacent pieces with
1122 * the same such expression.
1124 * The sorting is performed in place because it does not
1125 * change the meaning of "pw", but care needs to be
1126 * taken not to change any possible other copies of "pw"
1127 * in case anything goes wrong.
1129 static __isl_give PW *FN(PW,sort_unique)(__isl_take PW *pw)
1131 int i, j;
1132 isl_set *set;
1134 if (!pw)
1135 return NULL;
1136 if (pw->n <= 1)
1137 return pw;
1138 if (isl_sort(pw->p, pw->n, sizeof(pw->p[0]),
1139 &FN(PW,sort_field_cmp), NULL) < 0)
1140 return FN(PW,free)(pw);
1141 for (i = pw->n - 1; i >= 1; --i) {
1142 isl_bool equal;
1143 EL *el, *el_prev;
1144 isl_set *set_prev;
1146 el = FN(PW,peek_base_at)(pw, i);
1147 el_prev = FN(PW,peek_base_at)(pw, i - 1);
1148 equal = FN(EL,plain_is_equal)(el, el_prev);
1149 if (equal < 0)
1150 return FN(PW,free)(pw);
1151 if (!equal)
1152 continue;
1153 set = FN(PW,get_domain_at)(pw, i);
1154 set_prev = FN(PW,get_domain_at)(pw, i - 1);
1155 set = isl_set_union(set_prev, set);
1156 if (!set)
1157 return FN(PW,free)(pw);
1158 isl_set_free(pw->p[i].set);
1159 FN(EL,free)(pw->p[i].FIELD);
1160 isl_set_free(pw->p[i - 1].set);
1161 pw->p[i - 1].set = set;
1162 for (j = i + 1; j < pw->n; ++j)
1163 pw->p[j - 1] = pw->p[j];
1164 pw->n--;
1167 return pw;
1170 /* Compute the gist of "pw" with respect to the domain constraints
1171 * of "context" for the case where the domain of the last element
1172 * of "pw" is equal to "context".
1173 * Compute the gist of this element, replace
1174 * its domain by the universe and drop all other elements
1175 * as their domains are necessarily disjoint from "context".
1177 static __isl_give PW *FN(PW,gist_last)(__isl_take PW *pw,
1178 __isl_take isl_set *context)
1180 int i;
1181 isl_space *space;
1182 EL *el;
1184 for (i = 0; i < pw->n - 1; ++i) {
1185 isl_set_free(pw->p[i].set);
1186 FN(EL,free)(pw->p[i].FIELD);
1188 pw->p[0].FIELD = pw->p[pw->n - 1].FIELD;
1189 pw->p[0].set = pw->p[pw->n - 1].set;
1190 pw->n = 1;
1192 space = isl_set_get_space(context);
1193 el = FN(PW,take_base_at)(pw, 0);
1194 el = FN(EL,gist)(el, context);
1195 pw = FN(PW,restore_base_at)(pw, 0, el);
1196 context = isl_set_universe(space);
1197 pw = FN(PW,restore_domain_at)(pw, 0, context);
1199 return pw;
1202 /* Compute the gist of "pw" with respect to the domain constraints
1203 * of "context".
1204 * Call "fn_dom" to compute the gist of the domains and
1205 * "intersect_context" to intersect the domain with the context.
1207 * If the piecewise expression is empty or the context is the universe,
1208 * then nothing can be simplified.
1209 * If "pw" has a single domain and it is equal to "context",
1210 * then simply replace the domain by the universe.
1211 * Combine duplicate function value expressions first
1212 * to increase the chance of "pw" having a single domain.
1214 static __isl_give PW *FN(PW,gist_fn)(__isl_take PW *pw,
1215 __isl_take isl_set *context,
1216 __isl_give isl_set *(*fn_dom)(__isl_take isl_set *set,
1217 __isl_take isl_basic_set *bset),
1218 __isl_give isl_set *intersect_context(__isl_take isl_set *set,
1219 __isl_take isl_set *context))
1221 int i;
1222 int is_universe;
1223 isl_basic_set *hull = NULL;
1225 pw = FN(PW,sort_unique)(pw);
1226 if (!pw || !context)
1227 goto error;
1229 if (pw->n == 0) {
1230 isl_set_free(context);
1231 return pw;
1234 is_universe = isl_set_plain_is_universe(context);
1235 if (is_universe < 0)
1236 goto error;
1237 if (is_universe) {
1238 isl_set_free(context);
1239 return pw;
1242 FN(PW,align_params_set)(&pw, &context);
1244 pw = FN(PW,cow)(pw);
1245 if (!pw)
1246 goto error;
1248 if (pw->n == 1) {
1249 int equal;
1251 equal = isl_set_plain_is_equal(pw->p[0].set, context);
1252 if (equal < 0)
1253 goto error;
1254 if (equal)
1255 return FN(PW,gist_last)(pw, context);
1258 context = isl_set_compute_divs(context);
1259 hull = isl_set_simple_hull(isl_set_copy(context));
1261 for (i = pw->n - 1; i >= 0; --i) {
1262 isl_set *set_i;
1263 EL *el;
1264 int empty;
1266 if (i == pw->n - 1) {
1267 int equal;
1268 equal = isl_set_plain_is_equal(pw->p[i].set, context);
1269 if (equal < 0)
1270 goto error;
1271 if (equal) {
1272 isl_basic_set_free(hull);
1273 return FN(PW,gist_last)(pw, context);
1276 set_i = FN(PW,get_domain_at)(pw, i);
1277 set_i = intersect_context(set_i, isl_set_copy(context));
1278 empty = isl_set_plain_is_empty(set_i);
1279 el = FN(PW,take_base_at)(pw, i);
1280 el = FN(EL,gist)(el, set_i);
1281 pw = FN(PW,restore_base_at)(pw, i, el);
1282 set_i = FN(PW,take_domain_at)(pw, i);
1283 set_i = fn_dom(set_i, isl_basic_set_copy(hull));
1284 pw = FN(PW,restore_domain_at)(pw, i, set_i);
1285 if (empty < 0 || !pw)
1286 goto error;
1287 if (empty) {
1288 isl_set_free(pw->p[i].set);
1289 FN(EL,free)(pw->p[i].FIELD);
1290 if (i != pw->n - 1)
1291 pw->p[i] = pw->p[pw->n - 1];
1292 pw->n--;
1296 isl_basic_set_free(hull);
1297 isl_set_free(context);
1299 return pw;
1300 error:
1301 FN(PW,free)(pw);
1302 isl_basic_set_free(hull);
1303 isl_set_free(context);
1304 return NULL;
1307 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
1309 return FN(PW,gist_fn)(pw, context, &isl_set_gist_basic_set,
1310 &isl_set_intersect);
1313 __isl_give PW *FN(PW,gist_params)(__isl_take PW *pw,
1314 __isl_take isl_set *context)
1316 return FN(PW,gist_fn)(pw, context, &isl_set_gist_params_basic_set,
1317 &isl_set_intersect_params);
1320 /* Coalesce the domains of "pw".
1322 * Prior to the actual coalescing, first sort the pieces such that
1323 * pieces with the same function value expression are combined
1324 * into a single piece, the combined domain of which can then
1325 * be coalesced.
1327 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
1329 int i;
1330 isl_size n;
1332 pw = FN(PW,sort_unique)(pw);
1333 n = FN(PW,n_piece)(pw);
1334 if (n < 0)
1335 return FN(PW,free)(pw);
1337 for (i = 0; i < n; ++i) {
1338 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
1339 if (!pw->p[i].set)
1340 goto error;
1343 return pw;
1344 error:
1345 FN(PW,free)(pw);
1346 return NULL;
1349 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
1351 return pw ? isl_space_get_ctx(pw->dim) : NULL;
1354 isl_bool FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
1355 unsigned first, unsigned n)
1357 int i;
1358 enum isl_dim_type set_type;
1360 if (!pw)
1361 return isl_bool_error;
1362 if (pw->n == 0 || n == 0)
1363 return isl_bool_false;
1365 set_type = type == isl_dim_in ? isl_dim_set : type;
1367 for (i = 0; i < pw->n; ++i) {
1368 isl_bool involves = FN(EL,involves_dims)(pw->p[i].FIELD,
1369 type, first, n);
1370 if (involves < 0 || involves)
1371 return involves;
1372 involves = isl_set_involves_dims(pw->p[i].set,
1373 set_type, first, n);
1374 if (involves < 0 || involves)
1375 return involves;
1377 return isl_bool_false;
1380 __isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
1381 enum isl_dim_type type, unsigned pos, const char *s)
1383 isl_space *space;
1385 space = FN(PW,get_space)(pw);
1386 space = isl_space_set_dim_name(space, type, pos, s);
1387 return FN(PW,reset_space)(pw, space);
1390 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
1391 enum isl_dim_type type, unsigned first, unsigned n)
1393 int i;
1394 isl_size n_piece;
1395 enum isl_dim_type set_type;
1396 isl_space *space;
1398 n_piece = FN(PW,n_piece)(pw);
1399 if (n_piece < 0)
1400 return FN(PW,free)(pw);
1401 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
1402 return pw;
1404 set_type = type == isl_dim_in ? isl_dim_set : type;
1406 space = FN(PW,take_space)(pw);
1407 space = isl_space_drop_dims(space, type, first, n);
1408 pw = FN(PW,restore_space)(pw, space);
1409 for (i = 0; i < n_piece; ++i) {
1410 isl_set *domain;
1411 EL *el;
1413 el = FN(PW,take_base_at)(pw, i);
1414 el = FN(EL,drop_dims)(el, type, first, n);
1415 pw = FN(PW,restore_base_at)(pw, i, el);
1416 if (type == isl_dim_out)
1417 continue;
1418 domain = FN(PW,take_domain_at)(pw, i);
1419 domain = isl_set_drop(domain, set_type, first, n);
1420 pw = FN(PW,restore_domain_at)(pw, i, domain);
1423 return pw;
1426 /* This function is very similar to drop_dims.
1427 * The only difference is that the cells may still involve
1428 * the specified dimensions. They are removed using
1429 * isl_set_project_out instead of isl_set_drop.
1431 __isl_give PW *FN(PW,project_out)(__isl_take PW *pw,
1432 enum isl_dim_type type, unsigned first, unsigned n)
1434 int i;
1435 isl_size n_piece;
1436 enum isl_dim_type set_type;
1437 isl_space *space;
1439 n_piece = FN(PW,n_piece)(pw);
1440 if (n_piece < 0)
1441 return FN(PW,free)(pw);
1442 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
1443 return pw;
1445 set_type = type == isl_dim_in ? isl_dim_set : type;
1447 space = FN(PW,take_space)(pw);
1448 space = isl_space_drop_dims(space, type, first, n);
1449 pw = FN(PW,restore_space)(pw, space);
1450 for (i = 0; i < n_piece; ++i) {
1451 isl_set *domain;
1452 EL *el;
1454 domain = FN(PW,take_domain_at)(pw, i);
1455 domain = isl_set_project_out(domain, set_type, first, n);
1456 pw = FN(PW,restore_domain_at)(pw, i, domain);
1457 el = FN(PW,take_base_at)(pw, i);
1458 el = FN(EL,drop_dims)(el, type, first, n);
1459 pw = FN(PW,restore_base_at)(pw, i, el);
1462 return pw;
1465 /* Project the domain of pw onto its parameter space.
1467 __isl_give PW *FN(PW,project_domain_on_params)(__isl_take PW *pw)
1469 isl_space *space;
1470 isl_size n;
1472 n = FN(PW,dim)(pw, isl_dim_in);
1473 if (n < 0)
1474 return FN(PW,free)(pw);
1475 pw = FN(PW,project_out)(pw, isl_dim_in, 0, n);
1476 space = FN(PW,get_domain_space)(pw);
1477 space = isl_space_params(space);
1478 pw = FN(PW,reset_domain_space)(pw, space);
1479 return pw;
1482 /* Drop all parameters not referenced by "pw".
1484 __isl_give PW *FN(PW,drop_unused_params)(__isl_take PW *pw)
1486 isl_size n;
1487 int i;
1489 if (FN(PW,check_named_params)(pw) < 0)
1490 return FN(PW,free)(pw);
1492 n = FN(PW,dim)(pw, isl_dim_param);
1493 if (n < 0)
1494 return FN(PW,free)(pw);
1495 for (i = n - 1; i >= 0; i--) {
1496 isl_bool involves;
1498 involves = FN(PW,involves_dims)(pw, isl_dim_param, i, 1);
1499 if (involves < 0)
1500 return FN(PW,free)(pw);
1501 if (!involves)
1502 pw = FN(PW,drop_dims)(pw, isl_dim_param, i, 1);
1505 return pw;
1508 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
1509 enum isl_dim_type type, unsigned pos, isl_int v)
1511 int i;
1512 isl_size n;
1514 n = FN(PW,n_piece)(pw);
1515 if (n < 0)
1516 return FN(PW,free)(pw);
1518 if (type == isl_dim_in)
1519 type = isl_dim_set;
1521 for (i = 0; i < n; ++i) {
1522 isl_set *domain;
1524 domain = FN(PW,take_domain_at)(pw, i);
1525 domain = isl_set_fix(domain, type, pos, v);
1526 pw = FN(PW,restore_domain_at)(pw, i, domain);
1527 pw = FN(PW,exploit_equalities_and_remove_if_empty)(pw, i);
1530 return pw;
1533 /* Fix the value of the variable at position "pos" of type "type" of "pw"
1534 * to be equal to "v".
1536 __isl_give PW *FN(PW,fix_val)(__isl_take PW *pw,
1537 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
1539 if (!v)
1540 return FN(PW,free)(pw);
1541 if (!isl_val_is_int(v))
1542 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
1543 "expecting integer value", goto error);
1545 pw = FN(PW,fix_dim)(pw, type, pos, v->n);
1546 isl_val_free(v);
1548 return pw;
1549 error:
1550 isl_val_free(v);
1551 return FN(PW,free)(pw);
1554 isl_size FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
1556 return isl_space_dim(FN(PW,peek_space)(pw), type);
1559 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
1560 enum isl_dim_type type, unsigned first, unsigned n)
1562 int i;
1563 isl_size n_piece;
1565 n_piece = FN(PW,n_piece)(pw);
1566 if (n_piece < 0)
1567 return FN(PW,free)(pw);
1568 if (n == 0)
1569 return pw;
1571 if (type == isl_dim_in)
1572 type = isl_dim_set;
1574 for (i = 0; i < n; ++i) {
1575 isl_set *domain;
1577 domain = FN(PW,take_domain_at)(pw, i);
1578 domain = isl_set_split_dims(domain, type, first, n);
1579 pw = FN(PW,restore_domain_at)(pw, i, domain);
1582 return pw;
1585 __isl_give isl_space *FN(PW,get_domain_space)(__isl_keep PW *pw)
1587 return pw ? isl_space_domain(isl_space_copy(pw->dim)) : NULL;
1590 /* Return the position of the dimension of the given type and name
1591 * in "pw".
1592 * Return -1 if no such dimension can be found.
1594 int FN(PW,find_dim_by_name)(__isl_keep PW *pw,
1595 enum isl_dim_type type, const char *name)
1597 if (!pw)
1598 return -1;
1599 return isl_space_find_dim_by_name(pw->dim, type, name);
1602 /* Return the position of the dimension of the given type and identifier
1603 * in "pw".
1604 * Return -1 if no such dimension can be found.
1606 static int FN(PW,find_dim_by_id)(__isl_keep PW *pw,
1607 enum isl_dim_type type, __isl_keep isl_id *id)
1609 isl_space *space;
1611 space = FN(PW,peek_space)(pw);
1612 return isl_space_find_dim_by_id(space, type, id);
1615 /* Does the piecewise expression "pw" depend in any way
1616 * on the parameter with identifier "id"?
1618 isl_bool FN(PW,involves_param_id)(__isl_keep PW *pw, __isl_keep isl_id *id)
1620 int pos;
1622 if (!pw || !id)
1623 return isl_bool_error;
1624 if (pw->n == 0)
1625 return isl_bool_false;
1627 pos = FN(PW,find_dim_by_id)(pw, isl_dim_param, id);
1628 if (pos < 0)
1629 return isl_bool_false;
1630 return FN(PW,involves_dims)(pw, isl_dim_param, pos, 1);
1633 /* Reset the space of "pw". Since we don't know if the elements
1634 * represent the spaces themselves or their domains, we pass along
1635 * both when we call their reset_space_and_domain.
1637 static __isl_give PW *FN(PW,reset_space_and_domain)(__isl_take PW *pw,
1638 __isl_take isl_space *space, __isl_take isl_space *domain)
1640 int i;
1641 isl_size n;
1643 n = FN(PW,n_piece)(pw);
1644 if (n < 0 || !space || !domain)
1645 goto error;
1647 for (i = 0; i < n; ++i) {
1648 isl_set *set;
1649 EL *el;
1651 set = FN(PW,take_domain_at)(pw, i);
1652 set = isl_set_reset_space(set, isl_space_copy(domain));
1653 pw = FN(PW,restore_domain_at)(pw, i, set);
1654 el = FN(PW,take_base_at)(pw, i);
1655 el = FN(EL,reset_space_and_domain)(el,
1656 isl_space_copy(space), isl_space_copy(domain));
1657 pw = FN(PW,restore_base_at)(pw, i, el);
1660 isl_space_free(domain);
1662 pw = FN(PW,restore_space)(pw, space);
1664 return pw;
1665 error:
1666 isl_space_free(domain);
1667 isl_space_free(space);
1668 FN(PW,free)(pw);
1669 return NULL;
1672 __isl_give PW *FN(PW,reset_domain_space)(__isl_take PW *pw,
1673 __isl_take isl_space *domain)
1675 isl_space *space;
1677 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
1678 FN(PW,get_space)(pw));
1679 return FN(PW,reset_space_and_domain)(pw, space, domain);
1682 __isl_give PW *FN(PW,reset_space)(__isl_take PW *pw,
1683 __isl_take isl_space *space)
1685 isl_space *domain;
1687 domain = isl_space_domain(isl_space_copy(space));
1688 return FN(PW,reset_space_and_domain)(pw, space, domain);
1691 __isl_give PW *FN(PW,set_tuple_id)(__isl_take PW *pw, enum isl_dim_type type,
1692 __isl_take isl_id *id)
1694 isl_space *space;
1696 pw = FN(PW,cow)(pw);
1697 if (!pw)
1698 goto error;
1700 space = FN(PW,get_space)(pw);
1701 space = isl_space_set_tuple_id(space, type, id);
1703 return FN(PW,reset_space)(pw, space);
1704 error:
1705 isl_id_free(id);
1706 return FN(PW,free)(pw);
1709 /* Drop the id on the specified tuple.
1711 __isl_give PW *FN(PW,reset_tuple_id)(__isl_take PW *pw, enum isl_dim_type type)
1713 isl_space *space;
1715 if (!pw)
1716 return NULL;
1717 if (!FN(PW,has_tuple_id)(pw, type))
1718 return pw;
1720 pw = FN(PW,cow)(pw);
1721 if (!pw)
1722 return NULL;
1724 space = FN(PW,get_space)(pw);
1725 space = isl_space_reset_tuple_id(space, type);
1727 return FN(PW,reset_space)(pw, space);
1730 __isl_give PW *FN(PW,set_dim_id)(__isl_take PW *pw,
1731 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1733 isl_space *space;
1735 space = FN(PW,get_space)(pw);
1736 space = isl_space_set_dim_id(space, type, pos, id);
1737 return FN(PW,reset_space)(pw, space);
1740 /* Reset the user pointer on all identifiers of parameters and tuples
1741 * of the space of "pw".
1743 __isl_give PW *FN(PW,reset_user)(__isl_take PW *pw)
1745 isl_space *space;
1747 space = FN(PW,get_space)(pw);
1748 space = isl_space_reset_user(space);
1750 return FN(PW,reset_space)(pw, space);
1753 isl_size FN(PW,n_piece)(__isl_keep PW *pw)
1755 return pw ? pw->n : isl_size_error;
1758 isl_stat FN(PW,foreach_piece)(__isl_keep PW *pw,
1759 isl_stat (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
1760 void *user)
1762 int i;
1764 if (!pw)
1765 return isl_stat_error;
1767 for (i = 0; i < pw->n; ++i)
1768 if (fn(isl_set_copy(pw->p[i].set),
1769 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
1770 return isl_stat_error;
1772 return isl_stat_ok;
1775 /* Does "test" succeed on every cell of "pw"?
1777 isl_bool FN(PW,every_piece)(__isl_keep PW *pw,
1778 isl_bool (*test)(__isl_keep isl_set *set,
1779 __isl_keep EL *el, void *user), void *user)
1781 int i;
1783 if (!pw)
1784 return isl_bool_error;
1786 for (i = 0; i < pw->n; ++i) {
1787 isl_bool r;
1789 r = test(pw->p[i].set, pw->p[i].FIELD, user);
1790 if (r < 0 || !r)
1791 return r;
1794 return isl_bool_true;
1797 /* Is "pw" defined over a single universe domain?
1799 * If the default value of this piecewise type is zero,
1800 * then a "pw" with a zero number of cells is also accepted
1801 * as it represents the default zero value.
1803 isl_bool FN(FN(PW,isa),BASE)(__isl_keep PW *pw)
1805 isl_size n;
1807 n = FN(PW,n_piece)(pw);
1808 if (n < 0)
1809 return isl_bool_error;
1810 if (DEFAULT_IS_ZERO && n == 0)
1811 return isl_bool_true;
1812 if (n != 1)
1813 return isl_bool_false;
1814 return isl_set_plain_is_universe(FN(PW,peek_domain_at)(pw, 0));
1817 /* Return a zero base expression in the same space (and of the same type)
1818 * as "pw".
1820 static __isl_give EL *FN(EL,zero_like_type)(__isl_take PW *pw OPT_TYPE_PARAM)
1822 isl_space *space;
1824 space = FN(PW,get_space)(pw);
1825 FN(PW,free)(pw);
1826 return FN(EL,zero_in_space)(space OPT_TYPE_ARG(NO_LOC));
1829 #ifndef HAS_TYPE
1830 /* Return a zero base expression in the same space as "pw".
1832 static __isl_give EL *FN(EL,zero_like)(__isl_take PW *pw)
1834 return FN(EL,zero_like_type)(pw);
1836 #else
1837 /* Return a zero base expression in the same space and of the same type
1838 * as "pw".
1840 * Pass along the type as an explicit argument for uniform handling
1841 * in isl_*_zero_like_type.
1843 static __isl_give EL *FN(EL,zero_like)(__isl_take PW *pw)
1845 enum isl_fold type;
1847 type = FN(PW,get_type)(pw);
1848 if (type < 0)
1849 goto error;
1850 return FN(EL,zero_like_type)(pw, type);
1851 error:
1852 FN(PW,free)(pw);
1853 return NULL;
1855 #endif
1857 /* Given that "pw" is defined over a single universe domain,
1858 * return the base expression associated to this domain.
1860 * If the number of cells is zero, then "pw" is of a piecewise type
1861 * with a default zero value and effectively represents zero.
1862 * In this case, create a zero base expression in the same space
1863 * (and with the same type).
1864 * Otherwise, simply extract the associated base expression.
1866 __isl_give EL *FN(FN(PW,as),BASE)(__isl_take PW *pw)
1868 isl_bool is_total;
1869 isl_size n;
1870 EL *el;
1872 is_total = FN(FN(PW,isa),BASE)(pw);
1873 if (is_total < 0)
1874 goto error;
1875 if (!is_total)
1876 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
1877 "expecting single total function", goto error);
1878 n = FN(PW,n_piece)(pw);
1879 if (n < 0)
1880 goto error;
1881 if (n == 0)
1882 return FN(EL,zero_like)(pw);
1883 el = FN(PW,take_base_at)(pw, 0);
1884 FN(PW,free)(pw);
1885 return el;
1886 error:
1887 FN(PW,free)(pw);
1888 return NULL;
1891 #ifdef HAS_TYPE
1892 /* Negate the type of "pw".
1894 static __isl_give PW *FN(PW,negate_type)(__isl_take PW *pw)
1896 pw = FN(PW,cow)(pw);
1897 if (!pw)
1898 return NULL;
1899 pw->type = isl_fold_type_negate(pw->type);
1900 return pw;
1902 #else
1903 /* Negate the type of "pw".
1904 * Since "pw" does not have a type, do nothing.
1906 static __isl_give PW *FN(PW,negate_type)(__isl_take PW *pw)
1908 return pw;
1910 #endif
1912 __isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
1914 int i;
1915 isl_size n;
1917 if (isl_int_is_one(v))
1918 return pw;
1919 if (pw && DEFAULT_IS_ZERO && isl_int_is_zero(v)) {
1920 PW *zero;
1921 isl_space *space = FN(PW,get_space)(pw);
1922 zero = FN(PW,ZERO)(space OPT_TYPE_ARG(pw->));
1923 FN(PW,free)(pw);
1924 return zero;
1926 if (isl_int_is_neg(v))
1927 pw = FN(PW,negate_type)(pw);
1929 n = FN(PW,n_piece)(pw);
1930 if (n < 0)
1931 return FN(PW,free)(pw);
1932 for (i = 0; i < n; ++i) {
1933 EL *el;
1935 el = FN(PW,take_base_at)(pw, i);
1936 el = FN(EL,scale)(el, v);
1937 pw = FN(PW,restore_base_at)(pw, i, el);
1940 return pw;
1943 /* Multiply the pieces of "pw" by "v" and return the result.
1945 __isl_give PW *FN(PW,scale_val)(__isl_take PW *pw, __isl_take isl_val *v)
1947 int i;
1948 isl_size n;
1950 if (!pw || !v)
1951 goto error;
1953 if (isl_val_is_one(v)) {
1954 isl_val_free(v);
1955 return pw;
1957 if (pw && DEFAULT_IS_ZERO && isl_val_is_zero(v)) {
1958 PW *zero;
1959 isl_space *space = FN(PW,get_space)(pw);
1960 zero = FN(PW,ZERO)(space OPT_TYPE_ARG(pw->));
1961 FN(PW,free)(pw);
1962 isl_val_free(v);
1963 return zero;
1965 if (isl_val_is_neg(v))
1966 pw = FN(PW,negate_type)(pw);
1967 n = FN(PW,n_piece)(pw);
1968 if (n < 0)
1969 goto error;
1971 for (i = 0; i < n; ++i) {
1972 EL *el;
1974 el = FN(PW,take_base_at)(pw, i);
1975 el = FN(EL,scale_val)(el, isl_val_copy(v));
1976 pw = FN(PW,restore_base_at)(pw, i, el);
1979 isl_val_free(v);
1980 return pw;
1981 error:
1982 isl_val_free(v);
1983 FN(PW,free)(pw);
1984 return NULL;
1987 /* Divide the pieces of "pw" by "v" and return the result.
1989 __isl_give PW *FN(PW,scale_down_val)(__isl_take PW *pw, __isl_take isl_val *v)
1991 int i;
1992 isl_size n;
1994 if (!pw || !v)
1995 goto error;
1997 if (isl_val_is_one(v)) {
1998 isl_val_free(v);
1999 return pw;
2002 if (!isl_val_is_rat(v))
2003 isl_die(isl_val_get_ctx(v), isl_error_invalid,
2004 "expecting rational factor", goto error);
2005 if (isl_val_is_zero(v))
2006 isl_die(isl_val_get_ctx(v), isl_error_invalid,
2007 "cannot scale down by zero", goto error);
2009 if (isl_val_is_neg(v))
2010 pw = FN(PW,negate_type)(pw);
2011 n = FN(PW,n_piece)(pw);
2012 if (n < 0)
2013 goto error;
2015 for (i = 0; i < n; ++i) {
2016 EL *el;
2018 el = FN(PW,take_base_at)(pw, i);
2019 el = FN(EL,scale_down_val)(el, isl_val_copy(v));
2020 pw = FN(PW,restore_base_at)(pw, i, el);
2023 isl_val_free(v);
2024 return pw;
2025 error:
2026 isl_val_free(v);
2027 FN(PW,free)(pw);
2028 return NULL;
2031 __isl_give PW *FN(PW,scale)(__isl_take PW *pw, isl_int v)
2033 return FN(PW,mul_isl_int)(pw, v);
2036 /* Apply some normalization to "pw".
2037 * In particular, sort the pieces according to their function value
2038 * expressions, combining pairs of adjacent pieces with
2039 * the same such expression, and then normalize the domains of the pieces.
2041 * We normalize in place, but if anything goes wrong we need
2042 * to return NULL, so we need to make sure we don't change the
2043 * meaning of any possible other copies of "pw".
2045 static __isl_give PW *FN(PW,normalize)(__isl_take PW *pw)
2047 int i;
2048 isl_set *set;
2050 pw = FN(PW,sort_unique)(pw);
2051 if (!pw)
2052 return NULL;
2053 for (i = 0; i < pw->n; ++i) {
2054 set = isl_set_normalize(isl_set_copy(pw->p[i].set));
2055 if (!set)
2056 return FN(PW,free)(pw);
2057 isl_set_free(pw->p[i].set);
2058 pw->p[i].set = set;
2061 return pw;
2064 /* Is pw1 obviously equal to pw2?
2065 * That is, do they have obviously identical cells and obviously identical
2066 * elements on each cell?
2068 * If "pw1" or "pw2" contain any NaNs, then they are considered
2069 * not to be the same. A NaN is not equal to anything, not even
2070 * to another NaN.
2072 isl_bool FN(PW,plain_is_equal)(__isl_keep PW *pw1, __isl_keep PW *pw2)
2074 int i;
2075 isl_bool equal, has_nan;
2077 if (!pw1 || !pw2)
2078 return isl_bool_error;
2080 has_nan = FN(PW,involves_nan)(pw1);
2081 if (has_nan >= 0 && !has_nan)
2082 has_nan = FN(PW,involves_nan)(pw2);
2083 if (has_nan < 0 || has_nan)
2084 return isl_bool_not(has_nan);
2086 if (pw1 == pw2)
2087 return isl_bool_true;
2088 equal = FN(PW,has_equal_space)(pw1, pw2);
2089 if (equal < 0 || !equal)
2090 return equal;
2092 pw1 = FN(PW,copy)(pw1);
2093 pw2 = FN(PW,copy)(pw2);
2094 pw1 = FN(PW,normalize)(pw1);
2095 pw2 = FN(PW,normalize)(pw2);
2096 if (!pw1 || !pw2)
2097 goto error;
2099 equal = isl_bool_ok(pw1->n == pw2->n);
2100 for (i = 0; equal && i < pw1->n; ++i) {
2101 equal = isl_set_plain_is_equal(pw1->p[i].set, pw2->p[i].set);
2102 if (equal < 0)
2103 goto error;
2104 if (!equal)
2105 break;
2106 equal = FN(EL,plain_is_equal)(pw1->p[i].FIELD, pw2->p[i].FIELD);
2107 if (equal < 0)
2108 goto error;
2111 FN(PW,free)(pw1);
2112 FN(PW,free)(pw2);
2113 return equal;
2114 error:
2115 FN(PW,free)(pw1);
2116 FN(PW,free)(pw2);
2117 return isl_bool_error;
2120 /* Does "pw" involve any NaNs?
2122 isl_bool FN(PW,involves_nan)(__isl_keep PW *pw)
2124 int i;
2126 if (!pw)
2127 return isl_bool_error;
2128 if (pw->n == 0)
2129 return isl_bool_false;
2131 for (i = 0; i < pw->n; ++i) {
2132 isl_bool has_nan = FN(EL,involves_nan)(pw->p[i].FIELD);
2133 if (has_nan < 0 || has_nan)
2134 return has_nan;
2137 return isl_bool_false;