add isl_ast_expr_from_val
[isl.git] / isl_pw_templ.c
blobfb606513c716a91db9a3471f5936acd9f5c517fb
1 #include <isl/aff.h>
2 #include <isl_val_private.h>
4 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
5 #define FN(TYPE,NAME) xFN(TYPE,NAME)
6 #define xS(TYPE,NAME) struct TYPE ## _ ## NAME
7 #define S(TYPE,NAME) xS(TYPE,NAME)
9 #ifdef HAS_TYPE
10 __isl_give PW *FN(PW,alloc_size)(__isl_take isl_space *dim,
11 enum isl_fold type, int n)
12 #else
13 __isl_give PW *FN(PW,alloc_size)(__isl_take isl_space *dim, int n)
14 #endif
16 isl_ctx *ctx;
17 struct PW *pw;
19 if (!dim)
20 return NULL;
21 ctx = isl_space_get_ctx(dim);
22 isl_assert(ctx, n >= 0, goto error);
23 pw = isl_alloc(ctx, struct PW,
24 sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
25 if (!pw)
26 goto error;
28 pw->ref = 1;
29 #ifdef HAS_TYPE
30 pw->type = type;
31 #endif
32 pw->size = n;
33 pw->n = 0;
34 pw->dim = dim;
35 return pw;
36 error:
37 isl_space_free(dim);
38 return NULL;
41 #ifdef HAS_TYPE
42 __isl_give PW *FN(PW,ZERO)(__isl_take isl_space *dim, enum isl_fold type)
44 return FN(PW,alloc_size)(dim, type, 0);
46 #else
47 __isl_give PW *FN(PW,ZERO)(__isl_take isl_space *dim)
49 return FN(PW,alloc_size)(dim, 0);
51 #endif
53 __isl_give PW *FN(PW,add_piece)(__isl_take PW *pw,
54 __isl_take isl_set *set, __isl_take EL *el)
56 isl_ctx *ctx;
57 isl_space *el_dim = NULL;
59 if (!pw || !set || !el)
60 goto error;
62 if (isl_set_plain_is_empty(set) || FN(EL,EL_IS_ZERO)(el)) {
63 isl_set_free(set);
64 FN(EL,free)(el);
65 return pw;
68 ctx = isl_set_get_ctx(set);
69 #ifdef HAS_TYPE
70 if (pw->type != el->type)
71 isl_die(ctx, isl_error_invalid, "fold types don't match",
72 goto error);
73 #endif
74 el_dim = FN(EL,get_space(el));
75 isl_assert(ctx, isl_space_is_equal(pw->dim, el_dim), goto error);
76 isl_assert(ctx, pw->n < pw->size, goto error);
78 pw->p[pw->n].set = set;
79 pw->p[pw->n].FIELD = el;
80 pw->n++;
82 isl_space_free(el_dim);
83 return pw;
84 error:
85 isl_space_free(el_dim);
86 FN(PW,free)(pw);
87 isl_set_free(set);
88 FN(EL,free)(el);
89 return NULL;
92 #ifdef HAS_TYPE
93 __isl_give PW *FN(PW,alloc)(enum isl_fold type,
94 __isl_take isl_set *set, __isl_take EL *el)
95 #else
96 __isl_give PW *FN(PW,alloc)(__isl_take isl_set *set, __isl_take EL *el)
97 #endif
99 PW *pw;
101 if (!set || !el)
102 goto error;
104 #ifdef HAS_TYPE
105 pw = FN(PW,alloc_size)(FN(EL,get_space)(el), type, 1);
106 #else
107 pw = FN(PW,alloc_size)(FN(EL,get_space)(el), 1);
108 #endif
110 return FN(PW,add_piece)(pw, set, el);
111 error:
112 isl_set_free(set);
113 FN(EL,free)(el);
114 return NULL;
117 __isl_give PW *FN(PW,dup)(__isl_keep PW *pw)
119 int i;
120 PW *dup;
122 if (!pw)
123 return NULL;
125 #ifdef HAS_TYPE
126 dup = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->type, pw->n);
127 #else
128 dup = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->n);
129 #endif
130 if (!dup)
131 return NULL;
133 for (i = 0; i < pw->n; ++i)
134 dup = FN(PW,add_piece)(dup, isl_set_copy(pw->p[i].set),
135 FN(EL,copy)(pw->p[i].FIELD));
137 return dup;
140 __isl_give PW *FN(PW,cow)(__isl_take PW *pw)
142 if (!pw)
143 return NULL;
145 if (pw->ref == 1)
146 return pw;
147 pw->ref--;
148 return FN(PW,dup)(pw);
151 __isl_give PW *FN(PW,copy)(__isl_keep PW *pw)
153 if (!pw)
154 return NULL;
156 pw->ref++;
157 return pw;
160 void *FN(PW,free)(__isl_take PW *pw)
162 int i;
164 if (!pw)
165 return NULL;
166 if (--pw->ref > 0)
167 return NULL;
169 for (i = 0; i < pw->n; ++i) {
170 isl_set_free(pw->p[i].set);
171 FN(EL,free)(pw->p[i].FIELD);
173 isl_space_free(pw->dim);
174 free(pw);
176 return NULL;
179 const char *FN(PW,get_dim_name)(__isl_keep PW *pw, enum isl_dim_type type,
180 unsigned pos)
182 return pw ? isl_space_get_dim_name(pw->dim, type, pos) : NULL;
185 int FN(PW,has_dim_id)(__isl_keep PW *pw, enum isl_dim_type type, unsigned pos)
187 return pw ? isl_space_has_dim_id(pw->dim, type, pos) : -1;
190 __isl_give isl_id *FN(PW,get_dim_id)(__isl_keep PW *pw, enum isl_dim_type type,
191 unsigned pos)
193 return pw ? isl_space_get_dim_id(pw->dim, type, pos) : NULL;
196 int FN(PW,has_tuple_name)(__isl_keep PW *pw, enum isl_dim_type type)
198 return pw ? isl_space_has_tuple_name(pw->dim, type) : -1;
201 const char *FN(PW,get_tuple_name)(__isl_keep PW *pw, enum isl_dim_type type)
203 return pw ? isl_space_get_tuple_name(pw->dim, type) : NULL;
206 int FN(PW,has_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
208 return pw ? isl_space_has_tuple_id(pw->dim, type) : -1;
211 __isl_give isl_id *FN(PW,get_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
213 return pw ? isl_space_get_tuple_id(pw->dim, type) : NULL;
216 int FN(PW,IS_ZERO)(__isl_keep PW *pw)
218 if (!pw)
219 return -1;
221 return pw->n == 0;
224 #ifndef NO_REALIGN
225 __isl_give PW *FN(PW,realign_domain)(__isl_take PW *pw,
226 __isl_take isl_reordering *exp)
228 int i;
230 pw = FN(PW,cow)(pw);
231 if (!pw || !exp)
232 goto error;
234 for (i = 0; i < pw->n; ++i) {
235 pw->p[i].set = isl_set_realign(pw->p[i].set,
236 isl_reordering_copy(exp));
237 if (!pw->p[i].set)
238 goto error;
239 pw->p[i].FIELD = FN(EL,realign_domain)(pw->p[i].FIELD,
240 isl_reordering_copy(exp));
241 if (!pw->p[i].FIELD)
242 goto error;
245 pw = FN(PW,reset_domain_space)(pw, isl_space_copy(exp->dim));
247 isl_reordering_free(exp);
248 return pw;
249 error:
250 isl_reordering_free(exp);
251 FN(PW,free)(pw);
252 return NULL;
255 /* Align the parameters of "pw" to those of "model".
257 __isl_give PW *FN(PW,align_params)(__isl_take PW *pw, __isl_take isl_space *model)
259 isl_ctx *ctx;
261 if (!pw || !model)
262 goto error;
264 ctx = isl_space_get_ctx(model);
265 if (!isl_space_has_named_params(model))
266 isl_die(ctx, isl_error_invalid,
267 "model has unnamed parameters", goto error);
268 if (!isl_space_has_named_params(pw->dim))
269 isl_die(ctx, isl_error_invalid,
270 "input has unnamed parameters", goto error);
271 if (!isl_space_match(pw->dim, isl_dim_param, model, isl_dim_param)) {
272 isl_reordering *exp;
274 model = isl_space_drop_dims(model, isl_dim_in,
275 0, isl_space_dim(model, isl_dim_in));
276 model = isl_space_drop_dims(model, isl_dim_out,
277 0, isl_space_dim(model, isl_dim_out));
278 exp = isl_parameter_alignment_reordering(pw->dim, model);
279 exp = isl_reordering_extend_space(exp,
280 FN(PW,get_domain_space)(pw));
281 pw = FN(PW,realign_domain)(pw, exp);
284 isl_space_free(model);
285 return pw;
286 error:
287 isl_space_free(model);
288 FN(PW,free)(pw);
289 return NULL;
292 static __isl_give PW *FN(PW,align_params_pw_pw_and)(__isl_take PW *pw1,
293 __isl_take PW *pw2,
294 __isl_give PW *(*fn)(__isl_take PW *pw1, __isl_take PW *pw2))
296 isl_ctx *ctx;
298 if (!pw1 || !pw2)
299 goto error;
300 if (isl_space_match(pw1->dim, isl_dim_param, pw2->dim, isl_dim_param))
301 return fn(pw1, pw2);
302 ctx = FN(PW,get_ctx)(pw1);
303 if (!isl_space_has_named_params(pw1->dim) ||
304 !isl_space_has_named_params(pw2->dim))
305 isl_die(ctx, isl_error_invalid,
306 "unaligned unnamed parameters", goto error);
307 pw1 = FN(PW,align_params)(pw1, FN(PW,get_space)(pw2));
308 pw2 = FN(PW,align_params)(pw2, FN(PW,get_space)(pw1));
309 return fn(pw1, pw2);
310 error:
311 FN(PW,free)(pw1);
312 FN(PW,free)(pw2);
313 return NULL;
316 static __isl_give PW *FN(PW,align_params_pw_set_and)(__isl_take PW *pw,
317 __isl_take isl_set *set,
318 __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_set *set))
320 isl_ctx *ctx;
322 if (!pw || !set)
323 goto error;
324 if (isl_space_match(pw->dim, isl_dim_param, set->dim, isl_dim_param))
325 return fn(pw, set);
326 ctx = FN(PW,get_ctx)(pw);
327 if (!isl_space_has_named_params(pw->dim) ||
328 !isl_space_has_named_params(set->dim))
329 isl_die(ctx, isl_error_invalid,
330 "unaligned unnamed parameters", goto error);
331 pw = FN(PW,align_params)(pw, isl_set_get_space(set));
332 set = isl_set_align_params(set, FN(PW,get_space)(pw));
333 return fn(pw, set);
334 error:
335 FN(PW,free)(pw);
336 isl_set_free(set);
337 return NULL;
339 #endif
341 static __isl_give PW *FN(PW,union_add_aligned)(__isl_take PW *pw1,
342 __isl_take PW *pw2)
344 int i, j, n;
345 struct PW *res;
346 isl_ctx *ctx;
347 isl_set *set;
349 if (!pw1 || !pw2)
350 goto error;
352 ctx = isl_space_get_ctx(pw1->dim);
353 #ifdef HAS_TYPE
354 if (pw1->type != pw2->type)
355 isl_die(ctx, isl_error_invalid,
356 "fold types don't match", goto error);
357 #endif
358 isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
360 if (FN(PW,IS_ZERO)(pw1)) {
361 FN(PW,free)(pw1);
362 return pw2;
365 if (FN(PW,IS_ZERO)(pw2)) {
366 FN(PW,free)(pw2);
367 return pw1;
370 n = (pw1->n + 1) * (pw2->n + 1);
371 #ifdef HAS_TYPE
372 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), pw1->type, n);
373 #else
374 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), n);
375 #endif
377 for (i = 0; i < pw1->n; ++i) {
378 set = isl_set_copy(pw1->p[i].set);
379 for (j = 0; j < pw2->n; ++j) {
380 struct isl_set *common;
381 EL *sum;
382 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
383 isl_set_copy(pw2->p[j].set));
384 if (isl_set_plain_is_empty(common)) {
385 isl_set_free(common);
386 continue;
388 set = isl_set_subtract(set,
389 isl_set_copy(pw2->p[j].set));
391 sum = FN(EL,add_on_domain)(common,
392 FN(EL,copy)(pw1->p[i].FIELD),
393 FN(EL,copy)(pw2->p[j].FIELD));
395 res = FN(PW,add_piece)(res, common, sum);
397 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw1->p[i].FIELD));
400 for (j = 0; j < pw2->n; ++j) {
401 set = isl_set_copy(pw2->p[j].set);
402 for (i = 0; i < pw1->n; ++i)
403 set = isl_set_subtract(set,
404 isl_set_copy(pw1->p[i].set));
405 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw2->p[j].FIELD));
408 FN(PW,free)(pw1);
409 FN(PW,free)(pw2);
411 return res;
412 error:
413 FN(PW,free)(pw1);
414 FN(PW,free)(pw2);
415 return NULL;
418 /* Private version of "union_add". For isl_pw_qpolynomial and
419 * isl_pw_qpolynomial_fold, we prefer to simply call it "add".
421 static __isl_give PW *FN(PW,union_add_)(__isl_take PW *pw1, __isl_take PW *pw2)
423 return FN(PW,align_params_pw_pw_and)(pw1, pw2,
424 &FN(PW,union_add_aligned));
427 /* Make sure "pw" has room for at least "n" more pieces.
429 * If there is only one reference to pw, we extend it in place.
430 * Otherwise, we create a new PW and copy the pieces.
432 static __isl_give PW *FN(PW,grow)(__isl_take PW *pw, int n)
434 int i;
435 isl_ctx *ctx;
436 PW *res;
438 if (!pw)
439 return NULL;
440 if (pw->n + n <= pw->size)
441 return pw;
442 ctx = FN(PW,get_ctx)(pw);
443 n += pw->n;
444 if (pw->ref == 1) {
445 res = isl_realloc(ctx, pw, struct PW,
446 sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
447 if (!res)
448 return FN(PW,free)(pw);
449 res->size = n;
450 return res;
452 #ifdef HAS_TYPE
453 res = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->type, n);
454 #else
455 res = FN(PW,alloc_size)(isl_space_copy(pw->dim), n);
456 #endif
457 if (!res)
458 return FN(PW,free)(pw);
459 for (i = 0; i < pw->n; ++i)
460 res = FN(PW,add_piece)(res, isl_set_copy(pw->p[i].set),
461 FN(EL,copy)(pw->p[i].FIELD));
462 FN(PW,free)(pw);
463 return res;
466 static __isl_give PW *FN(PW,add_disjoint_aligned)(__isl_take PW *pw1,
467 __isl_take PW *pw2)
469 int i;
470 isl_ctx *ctx;
472 if (!pw1 || !pw2)
473 goto error;
475 if (pw1->size < pw1->n + pw2->n && pw1->n < pw2->n)
476 return FN(PW,add_disjoint_aligned)(pw2, pw1);
478 ctx = isl_space_get_ctx(pw1->dim);
479 #ifdef HAS_TYPE
480 if (pw1->type != pw2->type)
481 isl_die(ctx, isl_error_invalid,
482 "fold types don't match", goto error);
483 #endif
484 isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
486 if (FN(PW,IS_ZERO)(pw1)) {
487 FN(PW,free)(pw1);
488 return pw2;
491 if (FN(PW,IS_ZERO)(pw2)) {
492 FN(PW,free)(pw2);
493 return pw1;
496 pw1 = FN(PW,grow)(pw1, pw2->n);
497 if (!pw1)
498 goto error;
500 for (i = 0; i < pw2->n; ++i)
501 pw1 = FN(PW,add_piece)(pw1,
502 isl_set_copy(pw2->p[i].set),
503 FN(EL,copy)(pw2->p[i].FIELD));
505 FN(PW,free)(pw2);
507 return pw1;
508 error:
509 FN(PW,free)(pw1);
510 FN(PW,free)(pw2);
511 return NULL;
514 __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
516 return FN(PW,align_params_pw_pw_and)(pw1, pw2,
517 &FN(PW,add_disjoint_aligned));
520 /* This function is currently only used from isl_aff.c
522 static __isl_give PW *FN(PW,on_shared_domain_in)(__isl_take PW *pw1,
523 __isl_take PW *pw2, __isl_take isl_space *space,
524 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
525 __attribute__ ((unused));
527 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
528 * The result of "fn" (and therefore also of this function) lives in "space".
530 static __isl_give PW *FN(PW,on_shared_domain_in)(__isl_take PW *pw1,
531 __isl_take PW *pw2, __isl_take isl_space *space,
532 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
534 int i, j, n;
535 PW *res = NULL;
537 if (!pw1 || !pw2)
538 goto error;
540 n = pw1->n * pw2->n;
541 #ifdef HAS_TYPE
542 res = FN(PW,alloc_size)(isl_space_copy(space), pw1->type, n);
543 #else
544 res = FN(PW,alloc_size)(isl_space_copy(space), n);
545 #endif
547 for (i = 0; i < pw1->n; ++i) {
548 for (j = 0; j < pw2->n; ++j) {
549 isl_set *common;
550 EL *res_ij;
551 int empty;
553 common = isl_set_intersect(
554 isl_set_copy(pw1->p[i].set),
555 isl_set_copy(pw2->p[j].set));
556 empty = isl_set_plain_is_empty(common);
557 if (empty < 0 || empty) {
558 isl_set_free(common);
559 if (empty < 0)
560 goto error;
561 continue;
564 res_ij = fn(FN(EL,copy)(pw1->p[i].FIELD),
565 FN(EL,copy)(pw2->p[j].FIELD));
566 res_ij = FN(EL,gist)(res_ij, isl_set_copy(common));
568 res = FN(PW,add_piece)(res, common, res_ij);
572 isl_space_free(space);
573 FN(PW,free)(pw1);
574 FN(PW,free)(pw2);
575 return res;
576 error:
577 isl_space_free(space);
578 FN(PW,free)(pw1);
579 FN(PW,free)(pw2);
580 FN(PW,free)(res);
581 return NULL;
584 /* This function is currently only used from isl_aff.c
586 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
587 __isl_take PW *pw2,
588 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
589 __attribute__ ((unused));
591 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
592 * The result of "fn" is assumed to live in the same space as "pw1" and "pw2".
594 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
595 __isl_take PW *pw2,
596 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
598 isl_space *space;
600 if (!pw1 || !pw2)
601 goto error;
603 space = isl_space_copy(pw1->dim);
604 return FN(PW,on_shared_domain_in)(pw1, pw2, space, fn);
605 error:
606 FN(PW,free)(pw1);
607 FN(PW,free)(pw2);
608 return NULL;
611 #ifndef NO_NEG
612 __isl_give PW *FN(PW,neg)(__isl_take PW *pw)
614 int i;
616 if (!pw)
617 return NULL;
619 if (FN(PW,IS_ZERO)(pw))
620 return pw;
622 pw = FN(PW,cow)(pw);
623 if (!pw)
624 return NULL;
626 for (i = 0; i < pw->n; ++i) {
627 pw->p[i].FIELD = FN(EL,neg)(pw->p[i].FIELD);
628 if (!pw->p[i].FIELD)
629 return FN(PW,free)(pw);
632 return pw;
635 __isl_give PW *FN(PW,sub)(__isl_take PW *pw1, __isl_take PW *pw2)
637 return FN(PW,add)(pw1, FN(PW,neg)(pw2));
639 #endif
641 #ifndef NO_EVAL
642 __isl_give isl_qpolynomial *FN(PW,eval)(__isl_take PW *pw,
643 __isl_take isl_point *pnt)
645 int i;
646 int found = 0;
647 isl_ctx *ctx;
648 isl_space *pnt_dim = NULL;
649 isl_qpolynomial *qp;
651 if (!pw || !pnt)
652 goto error;
653 ctx = isl_point_get_ctx(pnt);
654 pnt_dim = isl_point_get_space(pnt);
655 isl_assert(ctx, isl_space_is_domain_internal(pnt_dim, pw->dim),
656 goto error);
658 for (i = 0; i < pw->n; ++i) {
659 found = isl_set_contains_point(pw->p[i].set, pnt);
660 if (found < 0)
661 goto error;
662 if (found)
663 break;
665 if (found)
666 qp = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
667 isl_point_copy(pnt));
668 else
669 qp = isl_qpolynomial_zero_on_domain(FN(PW,get_domain_space)(pw));
670 FN(PW,free)(pw);
671 isl_space_free(pnt_dim);
672 isl_point_free(pnt);
673 return qp;
674 error:
675 FN(PW,free)(pw);
676 isl_space_free(pnt_dim);
677 isl_point_free(pnt);
678 return NULL;
680 #endif
682 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
684 int i;
685 isl_set *dom;
687 if (!pw)
688 return NULL;
690 dom = isl_set_empty(FN(PW,get_domain_space)(pw));
691 for (i = 0; i < pw->n; ++i)
692 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
694 FN(PW,free)(pw);
696 return dom;
699 /* Restrict the domain of "pw" by combining each cell
700 * with "set" through a call to "fn", where "fn" may be
701 * isl_set_intersect or isl_set_intersect_params.
703 static __isl_give PW *FN(PW,intersect_aligned)(__isl_take PW *pw,
704 __isl_take isl_set *set,
705 __isl_give isl_set *(*fn)(__isl_take isl_set *set1,
706 __isl_take isl_set *set2))
708 int i;
710 if (!pw || !set)
711 goto error;
713 if (pw->n == 0) {
714 isl_set_free(set);
715 return pw;
718 pw = FN(PW,cow)(pw);
719 if (!pw)
720 goto error;
722 for (i = pw->n - 1; i >= 0; --i) {
723 isl_basic_set *aff;
724 pw->p[i].set = fn(pw->p[i].set, isl_set_copy(set));
725 if (!pw->p[i].set)
726 goto error;
727 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
728 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD,
729 aff);
730 if (!pw->p[i].FIELD)
731 goto error;
732 if (isl_set_plain_is_empty(pw->p[i].set)) {
733 isl_set_free(pw->p[i].set);
734 FN(EL,free)(pw->p[i].FIELD);
735 if (i != pw->n - 1)
736 pw->p[i] = pw->p[pw->n - 1];
737 pw->n--;
741 isl_set_free(set);
742 return pw;
743 error:
744 isl_set_free(set);
745 FN(PW,free)(pw);
746 return NULL;
749 static __isl_give PW *FN(PW,intersect_domain_aligned)(__isl_take PW *pw,
750 __isl_take isl_set *set)
752 return FN(PW,intersect_aligned)(pw, set, &isl_set_intersect);
755 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw,
756 __isl_take isl_set *context)
758 return FN(PW,align_params_pw_set_and)(pw, context,
759 &FN(PW,intersect_domain_aligned));
762 static __isl_give PW *FN(PW,intersect_params_aligned)(__isl_take PW *pw,
763 __isl_take isl_set *set)
765 return FN(PW,intersect_aligned)(pw, set, &isl_set_intersect_params);
768 /* Intersect the domain of "pw" with the parameter domain "context".
770 __isl_give PW *FN(PW,intersect_params)(__isl_take PW *pw,
771 __isl_take isl_set *context)
773 return FN(PW,align_params_pw_set_and)(pw, context,
774 &FN(PW,intersect_params_aligned));
777 static __isl_give PW *FN(PW,gist_aligned)(__isl_take PW *pw,
778 __isl_take isl_set *context,
779 __isl_give EL *(*fn_el)(__isl_take EL *el,
780 __isl_take isl_set *set),
781 __isl_give isl_set *(*fn_dom)(__isl_take isl_set *set,
782 __isl_take isl_basic_set *bset))
784 int i;
785 isl_basic_set *hull = NULL;
787 if (!pw || !context)
788 goto error;
790 if (pw->n == 0) {
791 isl_set_free(context);
792 return pw;
795 if (!isl_space_match(pw->dim, isl_dim_param,
796 context->dim, isl_dim_param)) {
797 pw = FN(PW,align_params)(pw, isl_set_get_space(context));
798 context = isl_set_align_params(context, FN(PW,get_space)(pw));
801 context = isl_set_compute_divs(context);
802 hull = isl_set_simple_hull(isl_set_copy(context));
804 pw = FN(PW,cow)(pw);
805 if (!pw)
806 goto error;
808 for (i = pw->n - 1; i >= 0; --i) {
809 isl_set *set_i;
810 int empty;
812 set_i = isl_set_intersect(isl_set_copy(pw->p[i].set),
813 isl_set_copy(context));
814 empty = isl_set_plain_is_empty(set_i);
815 pw->p[i].FIELD = fn_el(pw->p[i].FIELD, set_i);
816 pw->p[i].set = fn_dom(pw->p[i].set, isl_basic_set_copy(hull));
817 if (!pw->p[i].FIELD || !pw->p[i].set)
818 goto error;
819 if (empty) {
820 isl_set_free(pw->p[i].set);
821 FN(EL,free)(pw->p[i].FIELD);
822 if (i != pw->n - 1)
823 pw->p[i] = pw->p[pw->n - 1];
824 pw->n--;
828 isl_basic_set_free(hull);
829 isl_set_free(context);
831 return pw;
832 error:
833 FN(PW,free)(pw);
834 isl_basic_set_free(hull);
835 isl_set_free(context);
836 return NULL;
839 static __isl_give PW *FN(PW,gist_domain_aligned)(__isl_take PW *pw,
840 __isl_take isl_set *set)
842 return FN(PW,gist_aligned)(pw, set, &FN(EL,gist),
843 &isl_set_gist_basic_set);
846 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
848 return FN(PW,align_params_pw_set_and)(pw, context,
849 &FN(PW,gist_domain_aligned));
852 static __isl_give PW *FN(PW,gist_params_aligned)(__isl_take PW *pw,
853 __isl_take isl_set *set)
855 return FN(PW,gist_aligned)(pw, set, &FN(EL,gist_params),
856 &isl_set_gist_params_basic_set);
859 __isl_give PW *FN(PW,gist_params)(__isl_take PW *pw,
860 __isl_take isl_set *context)
862 return FN(PW,align_params_pw_set_and)(pw, context,
863 &FN(PW,gist_params_aligned));
866 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
868 int i, j;
870 if (!pw)
871 return NULL;
872 if (pw->n == 0)
873 return pw;
875 for (i = pw->n - 1; i >= 0; --i) {
876 for (j = i - 1; j >= 0; --j) {
877 if (!FN(EL,plain_is_equal)(pw->p[i].FIELD,
878 pw->p[j].FIELD))
879 continue;
880 pw->p[j].set = isl_set_union(pw->p[j].set,
881 pw->p[i].set);
882 FN(EL,free)(pw->p[i].FIELD);
883 if (i != pw->n - 1)
884 pw->p[i] = pw->p[pw->n - 1];
885 pw->n--;
886 break;
888 if (j >= 0)
889 continue;
890 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
891 if (!pw->p[i].set)
892 goto error;
895 return pw;
896 error:
897 FN(PW,free)(pw);
898 return NULL;
901 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
903 return pw ? isl_space_get_ctx(pw->dim) : NULL;
906 #ifndef NO_INVOLVES_DIMS
907 int FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
908 unsigned first, unsigned n)
910 int i;
911 enum isl_dim_type set_type;
913 if (!pw)
914 return -1;
915 if (pw->n == 0 || n == 0)
916 return 0;
918 set_type = type == isl_dim_in ? isl_dim_set : type;
920 for (i = 0; i < pw->n; ++i) {
921 int involves = FN(EL,involves_dims)(pw->p[i].FIELD,
922 type, first, n);
923 if (involves < 0 || involves)
924 return involves;
925 involves = isl_set_involves_dims(pw->p[i].set,
926 set_type, first, n);
927 if (involves < 0 || involves)
928 return involves;
930 return 0;
932 #endif
934 __isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
935 enum isl_dim_type type, unsigned pos, const char *s)
937 int i;
938 enum isl_dim_type set_type;
940 pw = FN(PW,cow)(pw);
941 if (!pw)
942 return NULL;
944 set_type = type == isl_dim_in ? isl_dim_set : type;
946 pw->dim = isl_space_set_dim_name(pw->dim, type, pos, s);
947 if (!pw->dim)
948 goto error;
950 for (i = 0; i < pw->n; ++i) {
951 pw->p[i].set = isl_set_set_dim_name(pw->p[i].set,
952 set_type, pos, s);
953 if (!pw->p[i].set)
954 goto error;
955 pw->p[i].FIELD = FN(EL,set_dim_name)(pw->p[i].FIELD, type, pos, s);
956 if (!pw->p[i].FIELD)
957 goto error;
960 return pw;
961 error:
962 FN(PW,free)(pw);
963 return NULL;
966 #ifndef NO_DROP_DIMS
967 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
968 enum isl_dim_type type, unsigned first, unsigned n)
970 int i;
971 enum isl_dim_type set_type;
973 if (!pw)
974 return NULL;
975 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
976 return pw;
978 set_type = type == isl_dim_in ? isl_dim_set : type;
980 pw = FN(PW,cow)(pw);
981 if (!pw)
982 return NULL;
983 pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
984 if (!pw->dim)
985 goto error;
986 for (i = 0; i < pw->n; ++i) {
987 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
988 if (!pw->p[i].FIELD)
989 goto error;
990 if (type == isl_dim_out)
991 continue;
992 pw->p[i].set = isl_set_drop(pw->p[i].set, set_type, first, n);
993 if (!pw->p[i].set)
994 goto error;
997 return pw;
998 error:
999 FN(PW,free)(pw);
1000 return NULL;
1003 /* This function is very similar to drop_dims.
1004 * The only difference is that the cells may still involve
1005 * the specified dimensions. They are removed using
1006 * isl_set_project_out instead of isl_set_drop.
1008 __isl_give PW *FN(PW,project_out)(__isl_take PW *pw,
1009 enum isl_dim_type type, unsigned first, unsigned n)
1011 int i;
1012 enum isl_dim_type set_type;
1014 if (!pw)
1015 return NULL;
1016 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
1017 return pw;
1019 set_type = type == isl_dim_in ? isl_dim_set : type;
1021 pw = FN(PW,cow)(pw);
1022 if (!pw)
1023 return NULL;
1024 pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
1025 if (!pw->dim)
1026 goto error;
1027 for (i = 0; i < pw->n; ++i) {
1028 pw->p[i].set = isl_set_project_out(pw->p[i].set,
1029 set_type, first, n);
1030 if (!pw->p[i].set)
1031 goto error;
1032 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
1033 if (!pw->p[i].FIELD)
1034 goto error;
1037 return pw;
1038 error:
1039 FN(PW,free)(pw);
1040 return NULL;
1043 /* Project the domain of pw onto its parameter space.
1045 __isl_give PW *FN(PW,project_domain_on_params)(__isl_take PW *pw)
1047 isl_space *space;
1048 unsigned n;
1050 n = FN(PW,dim)(pw, isl_dim_in);
1051 pw = FN(PW,project_out)(pw, isl_dim_in, 0, n);
1052 space = FN(PW,get_domain_space)(pw);
1053 space = isl_space_params(space);
1054 pw = FN(PW,reset_domain_space)(pw, space);
1055 return pw;
1057 #endif
1059 #ifndef NO_INSERT_DIMS
1060 __isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
1061 unsigned first, unsigned n)
1063 int i;
1064 enum isl_dim_type set_type;
1066 if (!pw)
1067 return NULL;
1068 if (n == 0 && !isl_space_is_named_or_nested(pw->dim, type))
1069 return pw;
1071 set_type = type == isl_dim_in ? isl_dim_set : type;
1073 pw = FN(PW,cow)(pw);
1074 if (!pw)
1075 return NULL;
1077 pw->dim = isl_space_insert_dims(pw->dim, type, first, n);
1078 if (!pw->dim)
1079 goto error;
1081 for (i = 0; i < pw->n; ++i) {
1082 pw->p[i].set = isl_set_insert_dims(pw->p[i].set,
1083 set_type, first, n);
1084 if (!pw->p[i].set)
1085 goto error;
1086 pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
1087 type, first, n);
1088 if (!pw->p[i].FIELD)
1089 goto error;
1092 return pw;
1093 error:
1094 FN(PW,free)(pw);
1095 return NULL;
1097 #endif
1099 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
1100 enum isl_dim_type type, unsigned pos, isl_int v)
1102 int i;
1104 if (!pw)
1105 return NULL;
1107 if (type == isl_dim_in)
1108 type = isl_dim_set;
1110 pw = FN(PW,cow)(pw);
1111 if (!pw)
1112 return NULL;
1113 for (i = 0; i < pw->n; ++i) {
1114 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
1115 if (!pw->p[i].set)
1116 goto error;
1119 return pw;
1120 error:
1121 FN(PW,free)(pw);
1122 return NULL;
1125 /* Fix the value of the variable at position "pos" of type "type" of "pw"
1126 * to be equal to "v".
1128 __isl_give PW *FN(PW,fix_val)(__isl_take PW *pw,
1129 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
1131 if (!v)
1132 return FN(PW,free)(pw);
1133 if (!isl_val_is_int(v))
1134 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
1135 "expecting integer value", goto error);
1137 pw = FN(PW,fix_dim)(pw, type, pos, v->n);
1138 isl_val_free(v);
1140 return pw;
1141 error:
1142 isl_val_free(v);
1143 return FN(PW,free)(pw);
1146 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
1148 return pw ? isl_space_dim(pw->dim, type) : 0;
1151 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
1152 enum isl_dim_type type, unsigned first, unsigned n)
1154 int i;
1156 if (!pw)
1157 return NULL;
1158 if (n == 0)
1159 return pw;
1161 if (type == isl_dim_in)
1162 type = isl_dim_set;
1164 pw = FN(PW,cow)(pw);
1165 if (!pw)
1166 return NULL;
1167 if (!pw->dim)
1168 goto error;
1169 for (i = 0; i < pw->n; ++i) {
1170 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
1171 if (!pw->p[i].set)
1172 goto error;
1175 return pw;
1176 error:
1177 FN(PW,free)(pw);
1178 return NULL;
1181 #ifndef NO_OPT
1182 /* Compute the maximal value attained by the piecewise quasipolynomial
1183 * on its domain or zero if the domain is empty.
1184 * In the worst case, the domain is scanned completely,
1185 * so the domain is assumed to be bounded.
1187 __isl_give isl_qpolynomial *FN(PW,opt)(__isl_take PW *pw, int max)
1189 int i;
1190 isl_qpolynomial *opt;
1192 if (!pw)
1193 return NULL;
1195 if (pw->n == 0) {
1196 isl_space *dim = isl_space_copy(pw->dim);
1197 FN(PW,free)(pw);
1198 return isl_qpolynomial_zero_on_domain(isl_space_domain(dim));
1201 opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
1202 isl_set_copy(pw->p[0].set), max);
1203 for (i = 1; i < pw->n; ++i) {
1204 isl_qpolynomial *opt_i;
1205 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
1206 isl_set_copy(pw->p[i].set), max);
1207 if (max)
1208 opt = isl_qpolynomial_max_cst(opt, opt_i);
1209 else
1210 opt = isl_qpolynomial_min_cst(opt, opt_i);
1213 FN(PW,free)(pw);
1214 return opt;
1217 __isl_give isl_qpolynomial *FN(PW,max)(__isl_take PW *pw)
1219 return FN(PW,opt)(pw, 1);
1222 __isl_give isl_qpolynomial *FN(PW,min)(__isl_take PW *pw)
1224 return FN(PW,opt)(pw, 0);
1226 #endif
1228 __isl_give isl_space *FN(PW,get_space)(__isl_keep PW *pw)
1230 return pw ? isl_space_copy(pw->dim) : NULL;
1233 __isl_give isl_space *FN(PW,get_domain_space)(__isl_keep PW *pw)
1235 return pw ? isl_space_domain(isl_space_copy(pw->dim)) : NULL;
1238 #ifndef NO_RESET_DIM
1239 /* Reset the space of "pw". Since we don't know if the elements
1240 * represent the spaces themselves or their domains, we pass along
1241 * both when we call their reset_space_and_domain.
1243 static __isl_give PW *FN(PW,reset_space_and_domain)(__isl_take PW *pw,
1244 __isl_take isl_space *space, __isl_take isl_space *domain)
1246 int i;
1248 pw = FN(PW,cow)(pw);
1249 if (!pw || !space || !domain)
1250 goto error;
1252 for (i = 0; i < pw->n; ++i) {
1253 pw->p[i].set = isl_set_reset_space(pw->p[i].set,
1254 isl_space_copy(domain));
1255 if (!pw->p[i].set)
1256 goto error;
1257 pw->p[i].FIELD = FN(EL,reset_space_and_domain)(pw->p[i].FIELD,
1258 isl_space_copy(space), isl_space_copy(domain));
1259 if (!pw->p[i].FIELD)
1260 goto error;
1263 isl_space_free(domain);
1265 isl_space_free(pw->dim);
1266 pw->dim = space;
1268 return pw;
1269 error:
1270 isl_space_free(domain);
1271 isl_space_free(space);
1272 FN(PW,free)(pw);
1273 return NULL;
1276 __isl_give PW *FN(PW,reset_domain_space)(__isl_take PW *pw,
1277 __isl_take isl_space *domain)
1279 isl_space *space;
1281 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
1282 FN(PW,get_space)(pw));
1283 return FN(PW,reset_space_and_domain)(pw, space, domain);
1286 __isl_give PW *FN(PW,reset_space)(__isl_take PW *pw, __isl_take isl_space *dim)
1288 isl_space *domain;
1290 domain = isl_space_domain(isl_space_copy(dim));
1291 return FN(PW,reset_space_and_domain)(pw, dim, domain);
1294 __isl_give PW *FN(PW,set_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type,
1295 __isl_take isl_id *id)
1297 isl_space *space;
1299 pw = FN(PW,cow)(pw);
1300 if (!pw)
1301 return isl_id_free(id);
1303 space = FN(PW,get_space)(pw);
1304 space = isl_space_set_tuple_id(space, type, id);
1306 return FN(PW,reset_space)(pw, space);
1309 __isl_give PW *FN(PW,set_dim_id)(__isl_take PW *pw,
1310 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1312 pw = FN(PW,cow)(pw);
1313 if (!pw)
1314 return isl_id_free(id);
1315 pw->dim = isl_space_set_dim_id(pw->dim, type, pos, id);
1316 return FN(PW,reset_space)(pw, isl_space_copy(pw->dim));
1318 #endif
1320 int FN(PW,has_equal_space)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1322 if (!pw1 || !pw2)
1323 return -1;
1325 return isl_space_is_equal(pw1->dim, pw2->dim);
1328 #ifndef NO_MORPH
1329 __isl_give PW *FN(PW,morph_domain)(__isl_take PW *pw,
1330 __isl_take isl_morph *morph)
1332 int i;
1333 isl_ctx *ctx;
1335 if (!pw || !morph)
1336 goto error;
1338 ctx = isl_space_get_ctx(pw->dim);
1339 isl_assert(ctx, isl_space_is_domain_internal(morph->dom->dim, pw->dim),
1340 goto error);
1342 pw = FN(PW,cow)(pw);
1343 if (!pw)
1344 goto error;
1345 pw->dim = isl_space_extend_domain_with_range(
1346 isl_space_copy(morph->ran->dim), pw->dim);
1347 if (!pw->dim)
1348 goto error;
1350 for (i = 0; i < pw->n; ++i) {
1351 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
1352 if (!pw->p[i].set)
1353 goto error;
1354 pw->p[i].FIELD = FN(EL,morph_domain)(pw->p[i].FIELD,
1355 isl_morph_copy(morph));
1356 if (!pw->p[i].FIELD)
1357 goto error;
1360 isl_morph_free(morph);
1362 return pw;
1363 error:
1364 FN(PW,free)(pw);
1365 isl_morph_free(morph);
1366 return NULL;
1368 #endif
1370 int FN(PW,n_piece)(__isl_keep PW *pw)
1372 return pw ? pw->n : 0;
1375 int FN(PW,foreach_piece)(__isl_keep PW *pw,
1376 int (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
1377 void *user)
1379 int i;
1381 if (!pw)
1382 return -1;
1384 for (i = 0; i < pw->n; ++i)
1385 if (fn(isl_set_copy(pw->p[i].set),
1386 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
1387 return -1;
1389 return 0;
1392 #ifndef NO_LIFT
1393 static int any_divs(__isl_keep isl_set *set)
1395 int i;
1397 if (!set)
1398 return -1;
1400 for (i = 0; i < set->n; ++i)
1401 if (set->p[i]->n_div > 0)
1402 return 1;
1404 return 0;
1407 static int foreach_lifted_subset(__isl_take isl_set *set, __isl_take EL *el,
1408 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1409 void *user), void *user)
1411 int i;
1413 if (!set || !el)
1414 goto error;
1416 for (i = 0; i < set->n; ++i) {
1417 isl_set *lift;
1418 EL *copy;
1420 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
1421 lift = isl_set_lift(lift);
1423 copy = FN(EL,copy)(el);
1424 copy = FN(EL,lift)(copy, isl_set_get_space(lift));
1426 if (fn(lift, copy, user) < 0)
1427 goto error;
1430 isl_set_free(set);
1431 FN(EL,free)(el);
1433 return 0;
1434 error:
1435 isl_set_free(set);
1436 FN(EL,free)(el);
1437 return -1;
1440 int FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
1441 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1442 void *user), void *user)
1444 int i;
1446 if (!pw)
1447 return -1;
1449 for (i = 0; i < pw->n; ++i) {
1450 isl_set *set;
1451 EL *el;
1453 set = isl_set_copy(pw->p[i].set);
1454 el = FN(EL,copy)(pw->p[i].FIELD);
1455 if (!any_divs(set)) {
1456 if (fn(set, el, user) < 0)
1457 return -1;
1458 continue;
1460 if (foreach_lifted_subset(set, el, fn, user) < 0)
1461 return -1;
1464 return 0;
1466 #endif
1468 #ifndef NO_MOVE_DIMS
1469 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
1470 enum isl_dim_type dst_type, unsigned dst_pos,
1471 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1473 int i;
1475 pw = FN(PW,cow)(pw);
1476 if (!pw)
1477 return NULL;
1479 pw->dim = isl_space_move_dims(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
1480 if (!pw->dim)
1481 goto error;
1483 for (i = 0; i < pw->n; ++i) {
1484 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
1485 dst_type, dst_pos, src_type, src_pos, n);
1486 if (!pw->p[i].FIELD)
1487 goto error;
1490 if (dst_type == isl_dim_in)
1491 dst_type = isl_dim_set;
1492 if (src_type == isl_dim_in)
1493 src_type = isl_dim_set;
1495 for (i = 0; i < pw->n; ++i) {
1496 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
1497 dst_type, dst_pos,
1498 src_type, src_pos, n);
1499 if (!pw->p[i].set)
1500 goto error;
1503 return pw;
1504 error:
1505 FN(PW,free)(pw);
1506 return NULL;
1508 #endif
1510 __isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
1512 int i;
1514 if (isl_int_is_one(v))
1515 return pw;
1516 if (pw && DEFAULT_IS_ZERO && isl_int_is_zero(v)) {
1517 PW *zero;
1518 isl_space *dim = FN(PW,get_space)(pw);
1519 #ifdef HAS_TYPE
1520 zero = FN(PW,ZERO)(dim, pw->type);
1521 #else
1522 zero = FN(PW,ZERO)(dim);
1523 #endif
1524 FN(PW,free)(pw);
1525 return zero;
1527 pw = FN(PW,cow)(pw);
1528 if (!pw)
1529 return NULL;
1530 if (pw->n == 0)
1531 return pw;
1533 #ifdef HAS_TYPE
1534 if (isl_int_is_neg(v))
1535 pw->type = isl_fold_type_negate(pw->type);
1536 #endif
1537 for (i = 0; i < pw->n; ++i) {
1538 pw->p[i].FIELD = FN(EL,scale)(pw->p[i].FIELD, v);
1539 if (!pw->p[i].FIELD)
1540 goto error;
1543 return pw;
1544 error:
1545 FN(PW,free)(pw);
1546 return NULL;
1549 /* Multiply the pieces of "pw" by "v" and return the result.
1551 __isl_give PW *FN(PW,scale_val)(__isl_take PW *pw, __isl_take isl_val *v)
1553 int i;
1555 if (!pw || !v)
1556 goto error;
1558 if (isl_val_is_one(v)) {
1559 isl_val_free(v);
1560 return pw;
1562 if (pw && DEFAULT_IS_ZERO && isl_val_is_zero(v)) {
1563 PW *zero;
1564 isl_space *space = FN(PW,get_space)(pw);
1565 #ifdef HAS_TYPE
1566 zero = FN(PW,ZERO)(space, pw->type);
1567 #else
1568 zero = FN(PW,ZERO)(space);
1569 #endif
1570 FN(PW,free)(pw);
1571 isl_val_free(v);
1572 return zero;
1574 if (pw->n == 0) {
1575 isl_val_free(v);
1576 return pw;
1578 pw = FN(PW,cow)(pw);
1579 if (!pw)
1580 goto error;
1582 #ifdef HAS_TYPE
1583 if (isl_val_is_neg(v))
1584 pw->type = isl_fold_type_negate(pw->type);
1585 #endif
1586 for (i = 0; i < pw->n; ++i) {
1587 pw->p[i].FIELD = FN(EL,scale_val)(pw->p[i].FIELD,
1588 isl_val_copy(v));
1589 if (!pw->p[i].FIELD)
1590 goto error;
1593 isl_val_free(v);
1594 return pw;
1595 error:
1596 isl_val_free(v);
1597 FN(PW,free)(pw);
1598 return NULL;
1601 __isl_give PW *FN(PW,scale)(__isl_take PW *pw, isl_int v)
1603 return FN(PW,mul_isl_int)(pw, v);
1606 static int FN(PW,qsort_set_cmp)(const void *p1, const void *p2)
1608 isl_set *set1 = *(isl_set * const *)p1;
1609 isl_set *set2 = *(isl_set * const *)p2;
1611 return isl_set_plain_cmp(set1, set2);
1614 /* We normalize in place, but if anything goes wrong we need
1615 * to return NULL, so we need to make sure we don't change the
1616 * meaning of any possible other copies of map.
1618 __isl_give PW *FN(PW,normalize)(__isl_take PW *pw)
1620 int i, j;
1621 isl_set *set;
1623 if (!pw)
1624 return NULL;
1625 for (i = 0; i < pw->n; ++i) {
1626 set = isl_set_normalize(isl_set_copy(pw->p[i].set));
1627 if (!set)
1628 return FN(PW,free)(pw);
1629 isl_set_free(pw->p[i].set);
1630 pw->p[i].set = set;
1632 qsort(pw->p, pw->n, sizeof(pw->p[0]), &FN(PW,qsort_set_cmp));
1633 for (i = pw->n - 1; i >= 1; --i) {
1634 if (!isl_set_plain_is_equal(pw->p[i - 1].set, pw->p[i].set))
1635 continue;
1636 if (!FN(EL,plain_is_equal)(pw->p[i - 1].FIELD, pw->p[i].FIELD))
1637 continue;
1638 set = isl_set_union(isl_set_copy(pw->p[i - 1].set),
1639 isl_set_copy(pw->p[i].set));
1640 if (!set)
1641 return FN(PW,free)(pw);
1642 isl_set_free(pw->p[i].set);
1643 FN(EL,free)(pw->p[i].FIELD);
1644 isl_set_free(pw->p[i - 1].set);
1645 pw->p[i - 1].set = set;
1646 for (j = i + 1; j < pw->n; ++j)
1647 pw->p[j - 1] = pw->p[j];
1648 pw->n--;
1651 return pw;
1654 /* Is pw1 obviously equal to pw2?
1655 * That is, do they have obviously identical cells and obviously identical
1656 * elements on each cell?
1658 int FN(PW,plain_is_equal)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1660 int i;
1661 int equal;
1663 if (!pw1 || !pw2)
1664 return -1;
1666 if (pw1 == pw2)
1667 return 1;
1668 if (!isl_space_is_equal(pw1->dim, pw2->dim))
1669 return 0;
1671 pw1 = FN(PW,copy)(pw1);
1672 pw2 = FN(PW,copy)(pw2);
1673 pw1 = FN(PW,normalize)(pw1);
1674 pw2 = FN(PW,normalize)(pw2);
1675 if (!pw1 || !pw2)
1676 goto error;
1678 equal = pw1->n == pw2->n;
1679 for (i = 0; equal && i < pw1->n; ++i) {
1680 equal = isl_set_plain_is_equal(pw1->p[i].set, pw2->p[i].set);
1681 if (equal < 0)
1682 goto error;
1683 if (!equal)
1684 break;
1685 equal = FN(EL,plain_is_equal)(pw1->p[i].FIELD, pw2->p[i].FIELD);
1686 if (equal < 0)
1687 goto error;
1690 FN(PW,free)(pw1);
1691 FN(PW,free)(pw2);
1692 return equal;
1693 error:
1694 FN(PW,free)(pw1);
1695 FN(PW,free)(pw2);
1696 return -1;
1699 #ifndef NO_PULLBACK
1700 static __isl_give PW *FN(PW,align_params_pw_multi_aff_and)(__isl_take PW *pw,
1701 __isl_take isl_multi_aff *ma,
1702 __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_multi_aff *ma))
1704 isl_ctx *ctx;
1705 isl_space *ma_space;
1707 ma_space = isl_multi_aff_get_space(ma);
1708 if (!pw || !ma || !ma_space)
1709 goto error;
1710 if (isl_space_match(pw->dim, isl_dim_param, ma_space, isl_dim_param)) {
1711 isl_space_free(ma_space);
1712 return fn(pw, ma);
1714 ctx = FN(PW,get_ctx)(pw);
1715 if (!isl_space_has_named_params(pw->dim) ||
1716 !isl_space_has_named_params(ma_space))
1717 isl_die(ctx, isl_error_invalid,
1718 "unaligned unnamed parameters", goto error);
1719 pw = FN(PW,align_params)(pw, ma_space);
1720 ma = isl_multi_aff_align_params(ma, FN(PW,get_space)(pw));
1721 return fn(pw, ma);
1722 error:
1723 isl_space_free(ma_space);
1724 FN(PW,free)(pw);
1725 isl_multi_aff_free(ma);
1726 return NULL;
1729 static __isl_give PW *FN(PW,align_params_pw_pw_multi_aff_and)(__isl_take PW *pw,
1730 __isl_take isl_pw_multi_aff *pma,
1731 __isl_give PW *(*fn)(__isl_take PW *pw,
1732 __isl_take isl_pw_multi_aff *ma))
1734 isl_ctx *ctx;
1735 isl_space *pma_space;
1737 pma_space = isl_pw_multi_aff_get_space(pma);
1738 if (!pw || !pma || !pma_space)
1739 goto error;
1740 if (isl_space_match(pw->dim, isl_dim_param, pma_space, isl_dim_param)) {
1741 isl_space_free(pma_space);
1742 return fn(pw, pma);
1744 ctx = FN(PW,get_ctx)(pw);
1745 if (!isl_space_has_named_params(pw->dim) ||
1746 !isl_space_has_named_params(pma_space))
1747 isl_die(ctx, isl_error_invalid,
1748 "unaligned unnamed parameters", goto error);
1749 pw = FN(PW,align_params)(pw, pma_space);
1750 pma = isl_pw_multi_aff_align_params(pma, FN(PW,get_space)(pw));
1751 return fn(pw, pma);
1752 error:
1753 isl_space_free(pma_space);
1754 FN(PW,free)(pw);
1755 isl_pw_multi_aff_free(pma);
1756 return NULL;
1759 /* Compute the pullback of "pw" by the function represented by "ma".
1760 * In other words, plug in "ma" in "pw".
1762 static __isl_give PW *FN(PW,pullback_multi_aff_aligned)(__isl_take PW *pw,
1763 __isl_take isl_multi_aff *ma)
1765 int i;
1766 isl_space *space = NULL;
1768 ma = isl_multi_aff_align_divs(ma);
1769 pw = FN(PW,cow)(pw);
1770 if (!pw || !ma)
1771 goto error;
1773 space = isl_space_join(isl_multi_aff_get_space(ma),
1774 FN(PW,get_space)(pw));
1776 for (i = 0; i < pw->n; ++i) {
1777 pw->p[i].set = isl_set_preimage_multi_aff(pw->p[i].set,
1778 isl_multi_aff_copy(ma));
1779 if (!pw->p[i].set)
1780 goto error;
1781 pw->p[i].FIELD = FN(EL,pullback_multi_aff)(pw->p[i].FIELD,
1782 isl_multi_aff_copy(ma));
1783 if (!pw->p[i].FIELD)
1784 goto error;
1787 pw = FN(PW,reset_space)(pw, space);
1788 isl_multi_aff_free(ma);
1789 return pw;
1790 error:
1791 isl_space_free(space);
1792 isl_multi_aff_free(ma);
1793 FN(PW,free)(pw);
1794 return NULL;
1797 __isl_give PW *FN(PW,pullback_multi_aff)(__isl_take PW *pw,
1798 __isl_take isl_multi_aff *ma)
1800 return FN(PW,align_params_pw_multi_aff_and)(pw, ma,
1801 &FN(PW,pullback_multi_aff_aligned));
1804 /* Compute the pullback of "pw" by the function represented by "pma".
1805 * In other words, plug in "pma" in "pw".
1807 static __isl_give PW *FN(PW,pullback_pw_multi_aff_aligned)(__isl_take PW *pw,
1808 __isl_take isl_pw_multi_aff *pma)
1810 int i;
1811 PW *res;
1813 if (!pma)
1814 goto error;
1816 if (pma->n == 0) {
1817 isl_pw_multi_aff_free(pma);
1818 res = FN(PW,empty)(FN(PW,get_space)(pw));
1819 FN(PW,free)(pw);
1820 return res;
1823 res = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
1824 isl_multi_aff_copy(pma->p[0].maff));
1825 res = FN(PW,intersect_domain)(res, isl_set_copy(pma->p[0].set));
1827 for (i = 1; i < pma->n; ++i) {
1828 PW *res_i;
1830 res_i = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
1831 isl_multi_aff_copy(pma->p[i].maff));
1832 res_i = FN(PW,intersect_domain)(res_i,
1833 isl_set_copy(pma->p[i].set));
1834 res = FN(PW,add_disjoint)(res, res_i);
1837 isl_pw_multi_aff_free(pma);
1838 FN(PW,free)(pw);
1839 return res;
1840 error:
1841 isl_pw_multi_aff_free(pma);
1842 FN(PW,free)(pw);
1843 return NULL;
1846 __isl_give PW *FN(PW,pullback_pw_multi_aff)(__isl_take PW *pw,
1847 __isl_take isl_pw_multi_aff *pma)
1849 return FN(PW,align_params_pw_pw_multi_aff_and)(pw, pma,
1850 &FN(PW,pullback_pw_multi_aff_aligned));
1852 #endif