doc: update latest release of clang
[isl.git] / isl_pw_templ.c
blob2a01b3e78e5fa542a9802c14e64c5e50a3a18e09
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 __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 __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 __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 __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_reordering *exp;
639 exp = isl_parameter_alignment_reordering(pw->dim, model);
640 exp = isl_reordering_extend_space(exp,
641 FN(PW,get_domain_space)(pw));
642 pw = FN(PW,realign_domain)(pw, exp);
645 isl_space_free(model);
646 return pw;
647 error:
648 isl_space_free(model);
649 FN(PW,free)(pw);
650 return NULL;
653 #undef TYPE
654 #define TYPE PW
656 static
657 #include "isl_align_params_bin_templ.c"
659 #undef SUFFIX
660 #define SUFFIX set
661 #undef ARG1
662 #define ARG1 PW
663 #undef ARG2
664 #define ARG2 isl_set
666 static
667 #include "isl_align_params_templ.c"
669 #undef TYPE
670 #define TYPE PW
672 #include "isl_type_has_equal_space_bin_templ.c"
673 #include "isl_type_check_equal_space_templ.c"
675 /* Private version of "union_add". For isl_pw_qpolynomial and
676 * isl_pw_qpolynomial_fold, we prefer to simply call it "add".
678 static __isl_give PW *FN(PW,union_add_)(__isl_take PW *pw1, __isl_take PW *pw2)
680 int i, j, n;
681 struct PW *res;
682 isl_ctx *ctx;
683 isl_set *set;
685 if (FN(PW,align_params_bin)(&pw1, &pw2) < 0)
686 goto error;
688 ctx = isl_space_get_ctx(pw1->dim);
689 if (!OPT_EQUAL_TYPES(pw1->, pw2->))
690 isl_die(ctx, isl_error_invalid,
691 "fold types don't match", goto error);
692 if (FN(PW,check_equal_space)(pw1, pw2) < 0)
693 goto error;
695 if (FN(PW,IS_ZERO)(pw1)) {
696 FN(PW,free)(pw1);
697 return pw2;
700 if (FN(PW,IS_ZERO)(pw2)) {
701 FN(PW,free)(pw2);
702 return pw1;
705 n = (pw1->n + 1) * (pw2->n + 1);
706 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim)
707 OPT_TYPE_ARG(pw1->), n);
709 for (i = 0; i < pw1->n; ++i) {
710 set = isl_set_copy(pw1->p[i].set);
711 for (j = 0; j < pw2->n; ++j) {
712 struct isl_set *common;
713 EL *sum;
714 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
715 isl_set_copy(pw2->p[j].set));
716 if (isl_set_plain_is_empty(common)) {
717 isl_set_free(common);
718 continue;
720 set = isl_set_subtract(set,
721 isl_set_copy(pw2->p[j].set));
723 sum = FN(EL,add_on_domain)(common,
724 FN(EL,copy)(pw1->p[i].FIELD),
725 FN(EL,copy)(pw2->p[j].FIELD));
727 res = FN(PW,add_piece)(res, common, sum);
729 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw1->p[i].FIELD));
732 for (j = 0; j < pw2->n; ++j) {
733 set = isl_set_copy(pw2->p[j].set);
734 for (i = 0; i < pw1->n; ++i)
735 set = isl_set_subtract(set,
736 isl_set_copy(pw1->p[i].set));
737 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw2->p[j].FIELD));
740 FN(PW,free)(pw1);
741 FN(PW,free)(pw2);
743 return res;
744 error:
745 FN(PW,free)(pw1);
746 FN(PW,free)(pw2);
747 return NULL;
750 /* Make sure "pw" has room for at least "n" more pieces.
752 * If there is only one reference to pw, we extend it in place.
753 * Otherwise, we create a new PW and copy the pieces.
755 static __isl_give PW *FN(PW,grow)(__isl_take PW *pw, int n)
757 int i;
758 isl_ctx *ctx;
759 PW *res;
761 if (!pw)
762 return NULL;
763 if (pw->n + n <= pw->size)
764 return pw;
765 ctx = FN(PW,get_ctx)(pw);
766 n += pw->n;
767 if (pw->ref == 1) {
768 res = isl_realloc(ctx, pw, struct PW,
769 sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
770 if (!res)
771 return FN(PW,free)(pw);
772 res->size = n;
773 return res;
775 res = FN(PW,alloc_size)(isl_space_copy(pw->dim) OPT_TYPE_ARG(pw->), n);
776 if (!res)
777 return FN(PW,free)(pw);
778 for (i = 0; i < pw->n; ++i)
779 res = FN(PW,add_piece)(res, isl_set_copy(pw->p[i].set),
780 FN(EL,copy)(pw->p[i].FIELD));
781 FN(PW,free)(pw);
782 return res;
785 __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
787 int i;
788 isl_ctx *ctx;
790 if (FN(PW,align_params_bin)(&pw1, &pw2) < 0)
791 goto error;
793 if (pw1->size < pw1->n + pw2->n && pw1->n < pw2->n)
794 return FN(PW,add_disjoint)(pw2, pw1);
796 ctx = isl_space_get_ctx(pw1->dim);
797 if (!OPT_EQUAL_TYPES(pw1->, pw2->))
798 isl_die(ctx, isl_error_invalid,
799 "fold types don't match", goto error);
800 if (FN(PW,check_equal_space)(pw1, pw2) < 0)
801 goto error;
803 if (FN(PW,IS_ZERO)(pw1)) {
804 FN(PW,free)(pw1);
805 return pw2;
808 if (FN(PW,IS_ZERO)(pw2)) {
809 FN(PW,free)(pw2);
810 return pw1;
813 pw1 = FN(PW,grow)(pw1, pw2->n);
814 if (!pw1)
815 goto error;
817 for (i = 0; i < pw2->n; ++i)
818 pw1 = FN(PW,add_piece)(pw1,
819 isl_set_copy(pw2->p[i].set),
820 FN(EL,copy)(pw2->p[i].FIELD));
822 FN(PW,free)(pw2);
824 return pw1;
825 error:
826 FN(PW,free)(pw1);
827 FN(PW,free)(pw2);
828 return NULL;
831 /* This function is currently only used from isl_aff.c
833 static __isl_give PW *FN(PW,on_shared_domain_in)(__isl_take PW *pw1,
834 __isl_take PW *pw2, __isl_take isl_space *space,
835 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
836 __attribute__ ((unused));
838 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
839 * The result of "fn" (and therefore also of this function) lives in "space".
841 static __isl_give PW *FN(PW,on_shared_domain_in)(__isl_take PW *pw1,
842 __isl_take PW *pw2, __isl_take isl_space *space,
843 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
845 int i, j, n;
846 PW *res = NULL;
848 if (!pw1 || !pw2)
849 goto error;
851 n = pw1->n * pw2->n;
852 res = FN(PW,alloc_size)(isl_space_copy(space) OPT_TYPE_ARG(pw1->), n);
854 for (i = 0; i < pw1->n; ++i) {
855 for (j = 0; j < pw2->n; ++j) {
856 isl_set *common;
857 EL *res_ij;
858 int empty;
860 common = isl_set_intersect(
861 isl_set_copy(pw1->p[i].set),
862 isl_set_copy(pw2->p[j].set));
863 empty = isl_set_plain_is_empty(common);
864 if (empty < 0 || empty) {
865 isl_set_free(common);
866 if (empty < 0)
867 goto error;
868 continue;
871 res_ij = fn(FN(EL,copy)(pw1->p[i].FIELD),
872 FN(EL,copy)(pw2->p[j].FIELD));
873 res_ij = FN(EL,gist)(res_ij, isl_set_copy(common));
875 res = FN(PW,add_piece)(res, common, res_ij);
879 isl_space_free(space);
880 FN(PW,free)(pw1);
881 FN(PW,free)(pw2);
882 return res;
883 error:
884 isl_space_free(space);
885 FN(PW,free)(pw1);
886 FN(PW,free)(pw2);
887 FN(PW,free)(res);
888 return NULL;
891 /* This function is currently only used from isl_aff.c
893 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
894 __isl_take PW *pw2,
895 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
896 __attribute__ ((unused));
898 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
899 * The result of "fn" is assumed to live in the same space as "pw1" and "pw2".
901 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
902 __isl_take PW *pw2,
903 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
905 isl_space *space;
907 if (FN(PW,check_equal_space)(pw1, pw2) < 0)
908 goto error;
910 space = isl_space_copy(pw1->dim);
911 return FN(PW,on_shared_domain_in)(pw1, pw2, space, fn);
912 error:
913 FN(PW,free)(pw1);
914 FN(PW,free)(pw2);
915 return NULL;
918 /* Return the parameter domain of "pw".
920 __isl_give isl_set *FN(PW,params)(__isl_take PW *pw)
922 return isl_set_params(FN(PW,domain)(pw));
925 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
927 int i;
928 isl_set *dom;
930 if (!pw)
931 return NULL;
933 dom = isl_set_empty(FN(PW,get_domain_space)(pw));
934 for (i = 0; i < pw->n; ++i)
935 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
937 FN(PW,free)(pw);
939 return dom;
942 /* Exploit the equalities in the domain of piece "i" of "pw"
943 * to simplify the associated function.
944 * If the domain of piece "i" is empty, then remove it entirely,
945 * replacing it with the final piece.
947 static __isl_give PW *FN(PW,exploit_equalities_and_remove_if_empty)(
948 __isl_take PW *pw, int i)
950 EL *el;
951 isl_set *domain;
952 isl_basic_set *aff;
953 int empty;
955 domain = FN(PW,peek_domain_at)(pw, i);
956 empty = isl_set_plain_is_empty(domain);
957 if (empty < 0)
958 return FN(PW,free)(pw);
959 if (empty) {
960 isl_set_free(pw->p[i].set);
961 FN(EL,free)(pw->p[i].FIELD);
962 if (i != pw->n - 1)
963 pw->p[i] = pw->p[pw->n - 1];
964 pw->n--;
966 return pw;
969 aff = isl_set_affine_hull(FN(PW,get_domain_at)(pw, i));
970 el = FN(PW,take_base_at)(pw, i);
971 el = FN(EL,substitute_equalities)(el, aff);
972 pw = FN(PW,restore_base_at_inplace)(pw, i, el);
974 return pw;
977 /* Convert a piecewise expression defined over a parameter domain
978 * into one that is defined over a zero-dimensional set.
980 __isl_give PW *FN(PW,from_range)(__isl_take PW *pw)
982 isl_space *space;
984 if (!pw)
985 return NULL;
986 if (!isl_space_is_set(pw->dim))
987 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
988 "not living in a set space", return FN(PW,free)(pw));
990 space = FN(PW,get_space)(pw);
991 space = isl_space_from_range(space);
992 pw = FN(PW,reset_space)(pw, space);
994 return pw;
997 /* Fix the value of the given parameter or domain dimension of "pw"
998 * to be equal to "value".
1000 __isl_give PW *FN(PW,fix_si)(__isl_take PW *pw, enum isl_dim_type type,
1001 unsigned pos, int value)
1003 int i;
1004 isl_size n;
1006 n = FN(PW,n_piece)(pw);
1007 if (n < 0)
1008 return FN(PW,free)(pw);
1010 if (type == isl_dim_out)
1011 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
1012 "cannot fix output dimension", return FN(PW,free)(pw));
1014 if (type == isl_dim_in)
1015 type = isl_dim_set;
1017 for (i = n - 1; i >= 0; --i) {
1018 isl_set *domain;
1020 domain = FN(PW,take_domain_at)(pw, i);
1021 domain = isl_set_fix_si(domain, type, pos, value);
1022 pw = FN(PW,restore_domain_at)(pw, i, domain);
1023 pw = FN(PW,exploit_equalities_and_remove_if_empty)(pw, i);
1026 return pw;
1029 /* Restrict the domain of "pw" by combining each cell
1030 * with "set" through a call to "fn", where "fn" may be
1031 * isl_set_intersect, isl_set_intersect_params, isl_set_intersect_factor_domain,
1032 * isl_set_intersect_factor_range or isl_set_subtract.
1034 static __isl_give PW *FN(PW,restrict_domain)(__isl_take PW *pw,
1035 __isl_take isl_set *set,
1036 __isl_give isl_set *(*fn)(__isl_take isl_set *set1,
1037 __isl_take isl_set *set2))
1039 int i;
1040 isl_size n;
1042 FN(PW,align_params_set)(&pw, &set);
1043 n = FN(PW,n_piece)(pw);
1044 if (n < 0 || !set)
1045 goto error;
1047 for (i = n - 1; i >= 0; --i) {
1048 isl_set *domain;
1050 domain = FN(PW,take_domain_at)(pw, i);
1051 domain = fn(domain, isl_set_copy(set));
1052 pw = FN(PW,restore_domain_at)(pw, i, domain);
1053 pw = FN(PW,exploit_equalities_and_remove_if_empty)(pw, i);
1056 isl_set_free(set);
1057 return pw;
1058 error:
1059 isl_set_free(set);
1060 FN(PW,free)(pw);
1061 return NULL;
1064 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw,
1065 __isl_take isl_set *context)
1067 return FN(PW,restrict_domain)(pw, context, &isl_set_intersect);
1070 /* Intersect the domain of "pw" with the parameter domain "context".
1072 __isl_give PW *FN(PW,intersect_params)(__isl_take PW *pw,
1073 __isl_take isl_set *context)
1075 return FN(PW,restrict_domain)(pw, context, &isl_set_intersect_params);
1078 /* Given a piecewise expression "pw" with domain in a space [A -> B] and
1079 * a set in the space A, intersect the domain with the set.
1081 __isl_give PW *FN(PW,intersect_domain_wrapped_domain)(__isl_take PW *pw,
1082 __isl_take isl_set *set)
1084 return FN(PW,restrict_domain)(pw, set,
1085 &isl_set_intersect_factor_domain);
1088 /* Given a piecewise expression "pw" with domain in a space [A -> B] and
1089 * a set in the space B, intersect the domain with the set.
1091 __isl_give PW *FN(PW,intersect_domain_wrapped_range)(__isl_take PW *pw,
1092 __isl_take isl_set *set)
1094 return FN(PW,restrict_domain)(pw, set, &isl_set_intersect_factor_range);
1097 /* Subtract "domain' from the domain of "pw".
1099 __isl_give PW *FN(PW,subtract_domain)(__isl_take PW *pw,
1100 __isl_take isl_set *domain)
1102 return FN(PW,restrict_domain)(pw, domain, &isl_set_subtract);
1105 /* Return -1 if the piece "p1" should be sorted before "p2"
1106 * and 1 if it should be sorted after "p2".
1107 * Return 0 if they do not need to be sorted in a specific order.
1109 * The two pieces are compared on the basis of their function value expressions.
1111 static int FN(PW,sort_field_cmp)(const void *p1, const void *p2, void *arg)
1113 struct FN(PW,piece) const *pc1 = p1;
1114 struct FN(PW,piece) const *pc2 = p2;
1116 return FN(EL,plain_cmp)(pc1->FIELD, pc2->FIELD);
1119 /* Sort the pieces of "pw" according to their function value
1120 * expressions and then combine pairs of adjacent pieces with
1121 * the same such expression.
1123 * The sorting is performed in place because it does not
1124 * change the meaning of "pw", but care needs to be
1125 * taken not to change any possible other copies of "pw"
1126 * in case anything goes wrong.
1128 static __isl_give PW *FN(PW,sort_unique)(__isl_take PW *pw)
1130 int i, j;
1131 isl_set *set;
1133 if (!pw)
1134 return NULL;
1135 if (pw->n <= 1)
1136 return pw;
1137 if (isl_sort(pw->p, pw->n, sizeof(pw->p[0]),
1138 &FN(PW,sort_field_cmp), NULL) < 0)
1139 return FN(PW,free)(pw);
1140 for (i = pw->n - 1; i >= 1; --i) {
1141 isl_bool equal;
1142 EL *el, *el_prev;
1143 isl_set *set_prev;
1145 el = FN(PW,peek_base_at)(pw, i);
1146 el_prev = FN(PW,peek_base_at)(pw, i - 1);
1147 equal = FN(EL,plain_is_equal)(el, el_prev);
1148 if (equal < 0)
1149 return FN(PW,free)(pw);
1150 if (!equal)
1151 continue;
1152 set = FN(PW,get_domain_at)(pw, i);
1153 set_prev = FN(PW,get_domain_at)(pw, i - 1);
1154 set = isl_set_union(set_prev, set);
1155 if (!set)
1156 return FN(PW,free)(pw);
1157 isl_set_free(pw->p[i].set);
1158 FN(EL,free)(pw->p[i].FIELD);
1159 isl_set_free(pw->p[i - 1].set);
1160 pw->p[i - 1].set = set;
1161 for (j = i + 1; j < pw->n; ++j)
1162 pw->p[j - 1] = pw->p[j];
1163 pw->n--;
1166 return pw;
1169 /* Compute the gist of "pw" with respect to the domain constraints
1170 * of "context" for the case where the domain of the last element
1171 * of "pw" is equal to "context".
1172 * Compute the gist of this element, replace
1173 * its domain by the universe and drop all other elements
1174 * as their domains are necessarily disjoint from "context".
1176 static __isl_give PW *FN(PW,gist_last)(__isl_take PW *pw,
1177 __isl_take isl_set *context)
1179 int i;
1180 isl_space *space;
1181 EL *el;
1183 for (i = 0; i < pw->n - 1; ++i) {
1184 isl_set_free(pw->p[i].set);
1185 FN(EL,free)(pw->p[i].FIELD);
1187 pw->p[0].FIELD = pw->p[pw->n - 1].FIELD;
1188 pw->p[0].set = pw->p[pw->n - 1].set;
1189 pw->n = 1;
1191 space = isl_set_get_space(context);
1192 el = FN(PW,take_base_at)(pw, 0);
1193 el = FN(EL,gist)(el, context);
1194 pw = FN(PW,restore_base_at)(pw, 0, el);
1195 context = isl_set_universe(space);
1196 pw = FN(PW,restore_domain_at)(pw, 0, context);
1198 return pw;
1201 /* Compute the gist of "pw" with respect to the domain constraints
1202 * of "context".
1203 * Call "fn_dom" to compute the gist of the domains and
1204 * "intersect_context" to intersect the domain with the context.
1206 * If the piecewise expression is empty or the context is the universe,
1207 * then nothing can be simplified.
1208 * If "pw" has a single domain and it is equal to "context",
1209 * then simply replace the domain by the universe.
1210 * Combine duplicate function value expressions first
1211 * to increase the chance of "pw" having a single domain.
1213 static __isl_give PW *FN(PW,gist_fn)(__isl_take PW *pw,
1214 __isl_take isl_set *context,
1215 __isl_give isl_set *(*fn_dom)(__isl_take isl_set *set,
1216 __isl_take isl_basic_set *bset),
1217 __isl_give isl_set *intersect_context(__isl_take isl_set *set,
1218 __isl_take isl_set *context))
1220 int i;
1221 int is_universe;
1222 isl_basic_set *hull = NULL;
1224 pw = FN(PW,sort_unique)(pw);
1225 if (!pw || !context)
1226 goto error;
1228 if (pw->n == 0) {
1229 isl_set_free(context);
1230 return pw;
1233 is_universe = isl_set_plain_is_universe(context);
1234 if (is_universe < 0)
1235 goto error;
1236 if (is_universe) {
1237 isl_set_free(context);
1238 return pw;
1241 FN(PW,align_params_set)(&pw, &context);
1243 pw = FN(PW,cow)(pw);
1244 if (!pw)
1245 goto error;
1247 if (pw->n == 1) {
1248 int equal;
1250 equal = isl_set_plain_is_equal(pw->p[0].set, context);
1251 if (equal < 0)
1252 goto error;
1253 if (equal)
1254 return FN(PW,gist_last)(pw, context);
1257 context = isl_set_compute_divs(context);
1258 hull = isl_set_simple_hull(isl_set_copy(context));
1260 for (i = pw->n - 1; i >= 0; --i) {
1261 isl_set *set_i;
1262 EL *el;
1263 int empty;
1265 if (i == pw->n - 1) {
1266 int equal;
1267 equal = isl_set_plain_is_equal(pw->p[i].set, context);
1268 if (equal < 0)
1269 goto error;
1270 if (equal) {
1271 isl_basic_set_free(hull);
1272 return FN(PW,gist_last)(pw, context);
1275 set_i = FN(PW,get_domain_at)(pw, i);
1276 set_i = intersect_context(set_i, isl_set_copy(context));
1277 empty = isl_set_plain_is_empty(set_i);
1278 el = FN(PW,take_base_at)(pw, i);
1279 el = FN(EL,gist)(el, set_i);
1280 pw = FN(PW,restore_base_at)(pw, i, el);
1281 set_i = FN(PW,take_domain_at)(pw, i);
1282 set_i = fn_dom(set_i, isl_basic_set_copy(hull));
1283 pw = FN(PW,restore_domain_at)(pw, i, set_i);
1284 if (empty < 0 || !pw)
1285 goto error;
1286 if (empty) {
1287 isl_set_free(pw->p[i].set);
1288 FN(EL,free)(pw->p[i].FIELD);
1289 if (i != pw->n - 1)
1290 pw->p[i] = pw->p[pw->n - 1];
1291 pw->n--;
1295 isl_basic_set_free(hull);
1296 isl_set_free(context);
1298 return pw;
1299 error:
1300 FN(PW,free)(pw);
1301 isl_basic_set_free(hull);
1302 isl_set_free(context);
1303 return NULL;
1306 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
1308 return FN(PW,gist_fn)(pw, context, &isl_set_gist_basic_set,
1309 &isl_set_intersect);
1312 __isl_give PW *FN(PW,gist_params)(__isl_take PW *pw,
1313 __isl_take isl_set *context)
1315 return FN(PW,gist_fn)(pw, context, &isl_set_gist_params_basic_set,
1316 &isl_set_intersect_params);
1319 /* Coalesce the domains of "pw".
1321 * Prior to the actual coalescing, first sort the pieces such that
1322 * pieces with the same function value expression are combined
1323 * into a single piece, the combined domain of which can then
1324 * be coalesced.
1326 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
1328 int i;
1329 isl_size n;
1331 pw = FN(PW,sort_unique)(pw);
1332 n = FN(PW,n_piece)(pw);
1333 if (n < 0)
1334 return FN(PW,free)(pw);
1336 for (i = 0; i < n; ++i) {
1337 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
1338 if (!pw->p[i].set)
1339 goto error;
1342 return pw;
1343 error:
1344 FN(PW,free)(pw);
1345 return NULL;
1348 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
1350 return pw ? isl_space_get_ctx(pw->dim) : NULL;
1353 isl_bool FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
1354 unsigned first, unsigned n)
1356 int i;
1357 enum isl_dim_type set_type;
1359 if (!pw)
1360 return isl_bool_error;
1361 if (pw->n == 0 || n == 0)
1362 return isl_bool_false;
1364 set_type = type == isl_dim_in ? isl_dim_set : type;
1366 for (i = 0; i < pw->n; ++i) {
1367 isl_bool involves = FN(EL,involves_dims)(pw->p[i].FIELD,
1368 type, first, n);
1369 if (involves < 0 || involves)
1370 return involves;
1371 involves = isl_set_involves_dims(pw->p[i].set,
1372 set_type, first, n);
1373 if (involves < 0 || involves)
1374 return involves;
1376 return isl_bool_false;
1379 __isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
1380 enum isl_dim_type type, unsigned pos, const char *s)
1382 isl_space *space;
1384 space = FN(PW,get_space)(pw);
1385 space = isl_space_set_dim_name(space, type, pos, s);
1386 return FN(PW,reset_space)(pw, space);
1389 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
1390 enum isl_dim_type type, unsigned first, unsigned n)
1392 int i;
1393 isl_size n_piece;
1394 enum isl_dim_type set_type;
1395 isl_space *space;
1397 n_piece = FN(PW,n_piece)(pw);
1398 if (n_piece < 0)
1399 return FN(PW,free)(pw);
1400 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
1401 return pw;
1403 set_type = type == isl_dim_in ? isl_dim_set : type;
1405 space = FN(PW,take_space)(pw);
1406 space = isl_space_drop_dims(space, type, first, n);
1407 pw = FN(PW,restore_space)(pw, space);
1408 for (i = 0; i < n_piece; ++i) {
1409 isl_set *domain;
1410 EL *el;
1412 el = FN(PW,take_base_at)(pw, i);
1413 el = FN(EL,drop_dims)(el, type, first, n);
1414 pw = FN(PW,restore_base_at)(pw, i, el);
1415 if (type == isl_dim_out)
1416 continue;
1417 domain = FN(PW,take_domain_at)(pw, i);
1418 domain = isl_set_drop(domain, set_type, first, n);
1419 pw = FN(PW,restore_domain_at)(pw, i, domain);
1422 return pw;
1425 /* This function is very similar to drop_dims.
1426 * The only difference is that the cells may still involve
1427 * the specified dimensions. They are removed using
1428 * isl_set_project_out instead of isl_set_drop.
1430 __isl_give PW *FN(PW,project_out)(__isl_take PW *pw,
1431 enum isl_dim_type type, unsigned first, unsigned n)
1433 int i;
1434 isl_size n_piece;
1435 enum isl_dim_type set_type;
1436 isl_space *space;
1438 n_piece = FN(PW,n_piece)(pw);
1439 if (n_piece < 0)
1440 return FN(PW,free)(pw);
1441 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
1442 return pw;
1444 set_type = type == isl_dim_in ? isl_dim_set : type;
1446 space = FN(PW,take_space)(pw);
1447 space = isl_space_drop_dims(space, type, first, n);
1448 pw = FN(PW,restore_space)(pw, space);
1449 for (i = 0; i < n_piece; ++i) {
1450 isl_set *domain;
1451 EL *el;
1453 domain = FN(PW,take_domain_at)(pw, i);
1454 domain = isl_set_project_out(domain, set_type, first, n);
1455 pw = FN(PW,restore_domain_at)(pw, i, domain);
1456 el = FN(PW,take_base_at)(pw, i);
1457 el = FN(EL,drop_dims)(el, type, first, n);
1458 pw = FN(PW,restore_base_at)(pw, i, el);
1461 return pw;
1464 /* Project the domain of pw onto its parameter space.
1466 __isl_give PW *FN(PW,project_domain_on_params)(__isl_take PW *pw)
1468 isl_space *space;
1469 isl_size n;
1471 n = FN(PW,dim)(pw, isl_dim_in);
1472 if (n < 0)
1473 return FN(PW,free)(pw);
1474 pw = FN(PW,project_out)(pw, isl_dim_in, 0, n);
1475 space = FN(PW,get_domain_space)(pw);
1476 space = isl_space_params(space);
1477 pw = FN(PW,reset_domain_space)(pw, space);
1478 return pw;
1481 /* Drop all parameters not referenced by "pw".
1483 __isl_give PW *FN(PW,drop_unused_params)(__isl_take PW *pw)
1485 isl_size n;
1486 int i;
1488 if (FN(PW,check_named_params)(pw) < 0)
1489 return FN(PW,free)(pw);
1491 n = FN(PW,dim)(pw, isl_dim_param);
1492 if (n < 0)
1493 return FN(PW,free)(pw);
1494 for (i = n - 1; i >= 0; i--) {
1495 isl_bool involves;
1497 involves = FN(PW,involves_dims)(pw, isl_dim_param, i, 1);
1498 if (involves < 0)
1499 return FN(PW,free)(pw);
1500 if (!involves)
1501 pw = FN(PW,drop_dims)(pw, isl_dim_param, i, 1);
1504 return pw;
1507 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
1508 enum isl_dim_type type, unsigned pos, isl_int v)
1510 int i;
1511 isl_size n;
1513 n = FN(PW,n_piece)(pw);
1514 if (n < 0)
1515 return FN(PW,free)(pw);
1517 if (type == isl_dim_in)
1518 type = isl_dim_set;
1520 for (i = 0; i < n; ++i) {
1521 isl_set *domain;
1523 domain = FN(PW,take_domain_at)(pw, i);
1524 domain = isl_set_fix(domain, type, pos, v);
1525 pw = FN(PW,restore_domain_at)(pw, i, domain);
1526 pw = FN(PW,exploit_equalities_and_remove_if_empty)(pw, i);
1529 return pw;
1532 /* Fix the value of the variable at position "pos" of type "type" of "pw"
1533 * to be equal to "v".
1535 __isl_give PW *FN(PW,fix_val)(__isl_take PW *pw,
1536 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
1538 if (!v)
1539 return FN(PW,free)(pw);
1540 if (!isl_val_is_int(v))
1541 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
1542 "expecting integer value", goto error);
1544 pw = FN(PW,fix_dim)(pw, type, pos, v->n);
1545 isl_val_free(v);
1547 return pw;
1548 error:
1549 isl_val_free(v);
1550 return FN(PW,free)(pw);
1553 isl_size FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
1555 return isl_space_dim(FN(PW,peek_space)(pw), type);
1558 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
1559 enum isl_dim_type type, unsigned first, unsigned n)
1561 int i;
1562 isl_size n_piece;
1564 n_piece = FN(PW,n_piece)(pw);
1565 if (n_piece < 0)
1566 return FN(PW,free)(pw);
1567 if (n == 0)
1568 return pw;
1570 if (type == isl_dim_in)
1571 type = isl_dim_set;
1573 for (i = 0; i < n; ++i) {
1574 isl_set *domain;
1576 domain = FN(PW,take_domain_at)(pw, i);
1577 domain = isl_set_split_dims(domain, type, first, n);
1578 pw = FN(PW,restore_domain_at)(pw, i, domain);
1581 return pw;
1584 __isl_give isl_space *FN(PW,get_domain_space)(__isl_keep PW *pw)
1586 return pw ? isl_space_domain(isl_space_copy(pw->dim)) : NULL;
1589 /* Return the position of the dimension of the given type and name
1590 * in "pw".
1591 * Return -1 if no such dimension can be found.
1593 int FN(PW,find_dim_by_name)(__isl_keep PW *pw,
1594 enum isl_dim_type type, const char *name)
1596 if (!pw)
1597 return -1;
1598 return isl_space_find_dim_by_name(pw->dim, type, name);
1601 /* Return the position of the dimension of the given type and identifier
1602 * in "pw".
1603 * Return -1 if no such dimension can be found.
1605 static int FN(PW,find_dim_by_id)(__isl_keep PW *pw,
1606 enum isl_dim_type type, __isl_keep isl_id *id)
1608 isl_space *space;
1610 space = FN(PW,peek_space)(pw);
1611 return isl_space_find_dim_by_id(space, type, id);
1614 /* Does the piecewise expression "pw" depend in any way
1615 * on the parameter with identifier "id"?
1617 isl_bool FN(PW,involves_param_id)(__isl_keep PW *pw, __isl_keep isl_id *id)
1619 int pos;
1621 if (!pw || !id)
1622 return isl_bool_error;
1623 if (pw->n == 0)
1624 return isl_bool_false;
1626 pos = FN(PW,find_dim_by_id)(pw, isl_dim_param, id);
1627 if (pos < 0)
1628 return isl_bool_false;
1629 return FN(PW,involves_dims)(pw, isl_dim_param, pos, 1);
1632 /* Reset the space of "pw". Since we don't know if the elements
1633 * represent the spaces themselves or their domains, we pass along
1634 * both when we call their reset_space_and_domain.
1636 static __isl_give PW *FN(PW,reset_space_and_domain)(__isl_take PW *pw,
1637 __isl_take isl_space *space, __isl_take isl_space *domain)
1639 int i;
1640 isl_size n;
1642 n = FN(PW,n_piece)(pw);
1643 if (n < 0 || !space || !domain)
1644 goto error;
1646 for (i = 0; i < n; ++i) {
1647 isl_set *set;
1648 EL *el;
1650 set = FN(PW,take_domain_at)(pw, i);
1651 set = isl_set_reset_space(set, isl_space_copy(domain));
1652 pw = FN(PW,restore_domain_at)(pw, i, set);
1653 el = FN(PW,take_base_at)(pw, i);
1654 el = FN(EL,reset_space_and_domain)(el,
1655 isl_space_copy(space), isl_space_copy(domain));
1656 pw = FN(PW,restore_base_at)(pw, i, el);
1659 isl_space_free(domain);
1661 pw = FN(PW,restore_space)(pw, space);
1663 return pw;
1664 error:
1665 isl_space_free(domain);
1666 isl_space_free(space);
1667 FN(PW,free)(pw);
1668 return NULL;
1671 __isl_give PW *FN(PW,reset_domain_space)(__isl_take PW *pw,
1672 __isl_take isl_space *domain)
1674 isl_space *space;
1676 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
1677 FN(PW,get_space)(pw));
1678 return FN(PW,reset_space_and_domain)(pw, space, domain);
1681 __isl_give PW *FN(PW,reset_space)(__isl_take PW *pw,
1682 __isl_take isl_space *space)
1684 isl_space *domain;
1686 domain = isl_space_domain(isl_space_copy(space));
1687 return FN(PW,reset_space_and_domain)(pw, space, domain);
1690 __isl_give PW *FN(PW,set_tuple_id)(__isl_take PW *pw, enum isl_dim_type type,
1691 __isl_take isl_id *id)
1693 isl_space *space;
1695 pw = FN(PW,cow)(pw);
1696 if (!pw)
1697 goto error;
1699 space = FN(PW,get_space)(pw);
1700 space = isl_space_set_tuple_id(space, type, id);
1702 return FN(PW,reset_space)(pw, space);
1703 error:
1704 isl_id_free(id);
1705 return FN(PW,free)(pw);
1708 /* Drop the id on the specified tuple.
1710 __isl_give PW *FN(PW,reset_tuple_id)(__isl_take PW *pw, enum isl_dim_type type)
1712 isl_space *space;
1714 if (!pw)
1715 return NULL;
1716 if (!FN(PW,has_tuple_id)(pw, type))
1717 return pw;
1719 pw = FN(PW,cow)(pw);
1720 if (!pw)
1721 return NULL;
1723 space = FN(PW,get_space)(pw);
1724 space = isl_space_reset_tuple_id(space, type);
1726 return FN(PW,reset_space)(pw, space);
1729 __isl_give PW *FN(PW,set_dim_id)(__isl_take PW *pw,
1730 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1732 isl_space *space;
1734 space = FN(PW,get_space)(pw);
1735 space = isl_space_set_dim_id(space, type, pos, id);
1736 return FN(PW,reset_space)(pw, space);
1739 /* Reset the user pointer on all identifiers of parameters and tuples
1740 * of the space of "pw".
1742 __isl_give PW *FN(PW,reset_user)(__isl_take PW *pw)
1744 isl_space *space;
1746 space = FN(PW,get_space)(pw);
1747 space = isl_space_reset_user(space);
1749 return FN(PW,reset_space)(pw, space);
1752 isl_size FN(PW,n_piece)(__isl_keep PW *pw)
1754 return pw ? pw->n : isl_size_error;
1757 isl_stat FN(PW,foreach_piece)(__isl_keep PW *pw,
1758 isl_stat (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
1759 void *user)
1761 int i;
1763 if (!pw)
1764 return isl_stat_error;
1766 for (i = 0; i < pw->n; ++i)
1767 if (fn(isl_set_copy(pw->p[i].set),
1768 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
1769 return isl_stat_error;
1771 return isl_stat_ok;
1774 /* Does "test" succeed on every cell of "pw"?
1776 isl_bool FN(PW,every_piece)(__isl_keep PW *pw,
1777 isl_bool (*test)(__isl_keep isl_set *set,
1778 __isl_keep EL *el, void *user), void *user)
1780 int i;
1782 if (!pw)
1783 return isl_bool_error;
1785 for (i = 0; i < pw->n; ++i) {
1786 isl_bool r;
1788 r = test(pw->p[i].set, pw->p[i].FIELD, user);
1789 if (r < 0 || !r)
1790 return r;
1793 return isl_bool_true;
1796 /* Is "pw" defined over a single universe domain?
1798 * If the default value of this piecewise type is zero,
1799 * then a "pw" with a zero number of cells is also accepted
1800 * as it represents the default zero value.
1802 isl_bool FN(FN(PW,isa),BASE)(__isl_keep PW *pw)
1804 isl_size n;
1806 n = FN(PW,n_piece)(pw);
1807 if (n < 0)
1808 return isl_bool_error;
1809 if (DEFAULT_IS_ZERO && n == 0)
1810 return isl_bool_true;
1811 if (n != 1)
1812 return isl_bool_false;
1813 return isl_set_plain_is_universe(FN(PW,peek_domain_at)(pw, 0));
1816 /* Return a zero base expression in the same space (and of the same type)
1817 * as "pw".
1819 static __isl_give EL *FN(EL,zero_like_type)(__isl_take PW *pw OPT_TYPE_PARAM)
1821 isl_space *space;
1823 space = FN(PW,get_space)(pw);
1824 FN(PW,free)(pw);
1825 return FN(EL,zero_in_space)(space OPT_TYPE_ARG(NO_LOC));
1828 #ifndef HAS_TYPE
1829 /* Return a zero base expression in the same space as "pw".
1831 static __isl_give EL *FN(EL,zero_like)(__isl_take PW *pw)
1833 return FN(EL,zero_like_type)(pw);
1835 #else
1836 /* Return a zero base expression in the same space and of the same type
1837 * as "pw".
1839 * Pass along the type as an explicit argument for uniform handling
1840 * in isl_*_zero_like_type.
1842 static __isl_give EL *FN(EL,zero_like)(__isl_take PW *pw)
1844 enum isl_fold type;
1846 type = FN(PW,get_type)(pw);
1847 if (type < 0)
1848 goto error;
1849 return FN(EL,zero_like_type)(pw, type);
1850 error:
1851 FN(PW,free)(pw);
1852 return NULL;
1854 #endif
1856 /* Given that "pw" is defined over a single universe domain,
1857 * return the base expression associated to this domain.
1859 * If the number of cells is zero, then "pw" is of a piecewise type
1860 * with a default zero value and effectively represents zero.
1861 * In this case, create a zero base expression in the same space
1862 * (and with the same type).
1863 * Otherwise, simply extract the associated base expression.
1865 __isl_give EL *FN(FN(PW,as),BASE)(__isl_take PW *pw)
1867 isl_bool is_total;
1868 isl_size n;
1869 EL *el;
1871 is_total = FN(FN(PW,isa),BASE)(pw);
1872 if (is_total < 0)
1873 goto error;
1874 if (!is_total)
1875 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
1876 "expecting single total function", goto error);
1877 n = FN(PW,n_piece)(pw);
1878 if (n < 0)
1879 goto error;
1880 if (n == 0)
1881 return FN(EL,zero_like)(pw);
1882 el = FN(PW,take_base_at)(pw, 0);
1883 FN(PW,free)(pw);
1884 return el;
1885 error:
1886 FN(PW,free)(pw);
1887 return NULL;
1890 #ifdef HAS_TYPE
1891 /* Negate the type of "pw".
1893 static __isl_give PW *FN(PW,negate_type)(__isl_take PW *pw)
1895 pw = FN(PW,cow)(pw);
1896 if (!pw)
1897 return NULL;
1898 pw->type = isl_fold_type_negate(pw->type);
1899 return pw;
1901 #else
1902 /* Negate the type of "pw".
1903 * Since "pw" does not have a type, do nothing.
1905 static __isl_give PW *FN(PW,negate_type)(__isl_take PW *pw)
1907 return pw;
1909 #endif
1911 __isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
1913 int i;
1914 isl_size n;
1916 if (isl_int_is_one(v))
1917 return pw;
1918 if (pw && DEFAULT_IS_ZERO && isl_int_is_zero(v)) {
1919 PW *zero;
1920 isl_space *space = FN(PW,get_space)(pw);
1921 zero = FN(PW,ZERO)(space OPT_TYPE_ARG(pw->));
1922 FN(PW,free)(pw);
1923 return zero;
1925 if (isl_int_is_neg(v))
1926 pw = FN(PW,negate_type)(pw);
1928 n = FN(PW,n_piece)(pw);
1929 if (n < 0)
1930 return FN(PW,free)(pw);
1931 for (i = 0; i < n; ++i) {
1932 EL *el;
1934 el = FN(PW,take_base_at)(pw, i);
1935 el = FN(EL,scale)(el, v);
1936 pw = FN(PW,restore_base_at)(pw, i, el);
1939 return pw;
1942 /* Multiply the pieces of "pw" by "v" and return the result.
1944 __isl_give PW *FN(PW,scale_val)(__isl_take PW *pw, __isl_take isl_val *v)
1946 int i;
1947 isl_size n;
1949 if (!pw || !v)
1950 goto error;
1952 if (isl_val_is_one(v)) {
1953 isl_val_free(v);
1954 return pw;
1956 if (pw && DEFAULT_IS_ZERO && isl_val_is_zero(v)) {
1957 PW *zero;
1958 isl_space *space = FN(PW,get_space)(pw);
1959 zero = FN(PW,ZERO)(space OPT_TYPE_ARG(pw->));
1960 FN(PW,free)(pw);
1961 isl_val_free(v);
1962 return zero;
1964 if (isl_val_is_neg(v))
1965 pw = FN(PW,negate_type)(pw);
1966 n = FN(PW,n_piece)(pw);
1967 if (n < 0)
1968 goto error;
1970 for (i = 0; i < n; ++i) {
1971 EL *el;
1973 el = FN(PW,take_base_at)(pw, i);
1974 el = FN(EL,scale_val)(el, isl_val_copy(v));
1975 pw = FN(PW,restore_base_at)(pw, i, el);
1978 isl_val_free(v);
1979 return pw;
1980 error:
1981 isl_val_free(v);
1982 FN(PW,free)(pw);
1983 return NULL;
1986 /* Divide the pieces of "pw" by "v" and return the result.
1988 __isl_give PW *FN(PW,scale_down_val)(__isl_take PW *pw, __isl_take isl_val *v)
1990 int i;
1991 isl_size n;
1993 if (!pw || !v)
1994 goto error;
1996 if (isl_val_is_one(v)) {
1997 isl_val_free(v);
1998 return pw;
2001 if (!isl_val_is_rat(v))
2002 isl_die(isl_val_get_ctx(v), isl_error_invalid,
2003 "expecting rational factor", goto error);
2004 if (isl_val_is_zero(v))
2005 isl_die(isl_val_get_ctx(v), isl_error_invalid,
2006 "cannot scale down by zero", goto error);
2008 if (isl_val_is_neg(v))
2009 pw = FN(PW,negate_type)(pw);
2010 n = FN(PW,n_piece)(pw);
2011 if (n < 0)
2012 goto error;
2014 for (i = 0; i < n; ++i) {
2015 EL *el;
2017 el = FN(PW,take_base_at)(pw, i);
2018 el = FN(EL,scale_down_val)(el, isl_val_copy(v));
2019 pw = FN(PW,restore_base_at)(pw, i, el);
2022 isl_val_free(v);
2023 return pw;
2024 error:
2025 isl_val_free(v);
2026 FN(PW,free)(pw);
2027 return NULL;
2030 __isl_give PW *FN(PW,scale)(__isl_take PW *pw, isl_int v)
2032 return FN(PW,mul_isl_int)(pw, v);
2035 /* Apply some normalization to "pw".
2036 * In particular, sort the pieces according to their function value
2037 * expressions, combining pairs of adjacent pieces with
2038 * the same such expression, and then normalize the domains of the pieces.
2040 * We normalize in place, but if anything goes wrong we need
2041 * to return NULL, so we need to make sure we don't change the
2042 * meaning of any possible other copies of "pw".
2044 __isl_give PW *FN(PW,normalize)(__isl_take PW *pw)
2046 int i;
2047 isl_set *set;
2049 pw = FN(PW,sort_unique)(pw);
2050 if (!pw)
2051 return NULL;
2052 for (i = 0; i < pw->n; ++i) {
2053 set = isl_set_normalize(isl_set_copy(pw->p[i].set));
2054 if (!set)
2055 return FN(PW,free)(pw);
2056 isl_set_free(pw->p[i].set);
2057 pw->p[i].set = set;
2060 return pw;
2063 /* Is pw1 obviously equal to pw2?
2064 * That is, do they have obviously identical cells and obviously identical
2065 * elements on each cell?
2067 * If "pw1" or "pw2" contain any NaNs, then they are considered
2068 * not to be the same. A NaN is not equal to anything, not even
2069 * to another NaN.
2071 isl_bool FN(PW,plain_is_equal)(__isl_keep PW *pw1, __isl_keep PW *pw2)
2073 int i;
2074 isl_bool equal, has_nan;
2076 if (!pw1 || !pw2)
2077 return isl_bool_error;
2079 has_nan = FN(PW,involves_nan)(pw1);
2080 if (has_nan >= 0 && !has_nan)
2081 has_nan = FN(PW,involves_nan)(pw2);
2082 if (has_nan < 0 || has_nan)
2083 return isl_bool_not(has_nan);
2085 if (pw1 == pw2)
2086 return isl_bool_true;
2087 equal = FN(PW,has_equal_space)(pw1, pw2);
2088 if (equal < 0 || !equal)
2089 return equal;
2091 pw1 = FN(PW,copy)(pw1);
2092 pw2 = FN(PW,copy)(pw2);
2093 pw1 = FN(PW,normalize)(pw1);
2094 pw2 = FN(PW,normalize)(pw2);
2095 if (!pw1 || !pw2)
2096 goto error;
2098 equal = isl_bool_ok(pw1->n == pw2->n);
2099 for (i = 0; equal && i < pw1->n; ++i) {
2100 equal = isl_set_plain_is_equal(pw1->p[i].set, pw2->p[i].set);
2101 if (equal < 0)
2102 goto error;
2103 if (!equal)
2104 break;
2105 equal = FN(EL,plain_is_equal)(pw1->p[i].FIELD, pw2->p[i].FIELD);
2106 if (equal < 0)
2107 goto error;
2110 FN(PW,free)(pw1);
2111 FN(PW,free)(pw2);
2112 return equal;
2113 error:
2114 FN(PW,free)(pw1);
2115 FN(PW,free)(pw2);
2116 return isl_bool_error;
2119 /* Does "pw" involve any NaNs?
2121 isl_bool FN(PW,involves_nan)(__isl_keep PW *pw)
2123 int i;
2125 if (!pw)
2126 return isl_bool_error;
2127 if (pw->n == 0)
2128 return isl_bool_false;
2130 for (i = 0; i < pw->n; ++i) {
2131 isl_bool has_nan = FN(EL,involves_nan)(pw->p[i].FIELD);
2132 if (has_nan < 0 || has_nan)
2133 return has_nan;
2136 return isl_bool_false;