replace CLooG test cases with schedule tree versions
[isl.git] / isl_pw_templ.c
blobf9e2a5fdcdf2d20d4f4f83462c49d72090a1be03
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/aff.h>
15 #include <isl_val_private.h>
17 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
18 #define FN(TYPE,NAME) xFN(TYPE,NAME)
19 #define xS(TYPE,NAME) struct TYPE ## _ ## NAME
20 #define S(TYPE,NAME) xS(TYPE,NAME)
22 #ifdef HAS_TYPE
23 __isl_give PW *FN(PW,alloc_size)(__isl_take isl_space *dim,
24 enum isl_fold type, int n)
25 #else
26 __isl_give PW *FN(PW,alloc_size)(__isl_take isl_space *dim, int n)
27 #endif
29 isl_ctx *ctx;
30 struct PW *pw;
32 if (!dim)
33 return NULL;
34 ctx = isl_space_get_ctx(dim);
35 isl_assert(ctx, n >= 0, goto error);
36 pw = isl_alloc(ctx, struct PW,
37 sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
38 if (!pw)
39 goto error;
41 pw->ref = 1;
42 #ifdef HAS_TYPE
43 pw->type = type;
44 #endif
45 pw->size = n;
46 pw->n = 0;
47 pw->dim = dim;
48 return pw;
49 error:
50 isl_space_free(dim);
51 return NULL;
54 #ifdef HAS_TYPE
55 __isl_give PW *FN(PW,ZERO)(__isl_take isl_space *dim, enum isl_fold type)
57 return FN(PW,alloc_size)(dim, type, 0);
59 #else
60 __isl_give PW *FN(PW,ZERO)(__isl_take isl_space *dim)
62 return FN(PW,alloc_size)(dim, 0);
64 #endif
66 __isl_give PW *FN(PW,add_piece)(__isl_take PW *pw,
67 __isl_take isl_set *set, __isl_take EL *el)
69 isl_ctx *ctx;
70 isl_space *el_dim = NULL;
72 if (!pw || !set || !el)
73 goto error;
75 if (isl_set_plain_is_empty(set) || FN(EL,EL_IS_ZERO)(el)) {
76 isl_set_free(set);
77 FN(EL,free)(el);
78 return pw;
81 ctx = isl_set_get_ctx(set);
82 #ifdef HAS_TYPE
83 if (pw->type != el->type)
84 isl_die(ctx, isl_error_invalid, "fold types don't match",
85 goto error);
86 #endif
87 el_dim = FN(EL,get_space(el));
88 isl_assert(ctx, isl_space_is_equal(pw->dim, el_dim), goto error);
89 isl_assert(ctx, pw->n < pw->size, goto error);
91 pw->p[pw->n].set = set;
92 pw->p[pw->n].FIELD = el;
93 pw->n++;
95 isl_space_free(el_dim);
96 return pw;
97 error:
98 isl_space_free(el_dim);
99 FN(PW,free)(pw);
100 isl_set_free(set);
101 FN(EL,free)(el);
102 return NULL;
105 #ifdef HAS_TYPE
106 __isl_give PW *FN(PW,alloc)(enum isl_fold type,
107 __isl_take isl_set *set, __isl_take EL *el)
108 #else
109 __isl_give PW *FN(PW,alloc)(__isl_take isl_set *set, __isl_take EL *el)
110 #endif
112 PW *pw;
114 if (!set || !el)
115 goto error;
117 #ifdef HAS_TYPE
118 pw = FN(PW,alloc_size)(FN(EL,get_space)(el), type, 1);
119 #else
120 pw = FN(PW,alloc_size)(FN(EL,get_space)(el), 1);
121 #endif
123 return FN(PW,add_piece)(pw, set, el);
124 error:
125 isl_set_free(set);
126 FN(EL,free)(el);
127 return NULL;
130 __isl_give PW *FN(PW,dup)(__isl_keep PW *pw)
132 int i;
133 PW *dup;
135 if (!pw)
136 return NULL;
138 #ifdef HAS_TYPE
139 dup = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->type, pw->n);
140 #else
141 dup = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->n);
142 #endif
143 if (!dup)
144 return NULL;
146 for (i = 0; i < pw->n; ++i)
147 dup = FN(PW,add_piece)(dup, isl_set_copy(pw->p[i].set),
148 FN(EL,copy)(pw->p[i].FIELD));
150 return dup;
153 __isl_give PW *FN(PW,cow)(__isl_take PW *pw)
155 if (!pw)
156 return NULL;
158 if (pw->ref == 1)
159 return pw;
160 pw->ref--;
161 return FN(PW,dup)(pw);
164 __isl_give PW *FN(PW,copy)(__isl_keep PW *pw)
166 if (!pw)
167 return NULL;
169 pw->ref++;
170 return pw;
173 __isl_null PW *FN(PW,free)(__isl_take PW *pw)
175 int i;
177 if (!pw)
178 return NULL;
179 if (--pw->ref > 0)
180 return NULL;
182 for (i = 0; i < pw->n; ++i) {
183 isl_set_free(pw->p[i].set);
184 FN(EL,free)(pw->p[i].FIELD);
186 isl_space_free(pw->dim);
187 free(pw);
189 return NULL;
192 const char *FN(PW,get_dim_name)(__isl_keep PW *pw, enum isl_dim_type type,
193 unsigned pos)
195 return pw ? isl_space_get_dim_name(pw->dim, type, pos) : NULL;
198 int FN(PW,has_dim_id)(__isl_keep PW *pw, enum isl_dim_type type, unsigned pos)
200 return pw ? isl_space_has_dim_id(pw->dim, type, pos) : -1;
203 __isl_give isl_id *FN(PW,get_dim_id)(__isl_keep PW *pw, enum isl_dim_type type,
204 unsigned pos)
206 return pw ? isl_space_get_dim_id(pw->dim, type, pos) : NULL;
209 int FN(PW,has_tuple_name)(__isl_keep PW *pw, enum isl_dim_type type)
211 return pw ? isl_space_has_tuple_name(pw->dim, type) : -1;
214 const char *FN(PW,get_tuple_name)(__isl_keep PW *pw, enum isl_dim_type type)
216 return pw ? isl_space_get_tuple_name(pw->dim, type) : NULL;
219 int FN(PW,has_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
221 return pw ? isl_space_has_tuple_id(pw->dim, type) : -1;
224 __isl_give isl_id *FN(PW,get_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
226 return pw ? isl_space_get_tuple_id(pw->dim, type) : NULL;
229 int FN(PW,IS_ZERO)(__isl_keep PW *pw)
231 if (!pw)
232 return -1;
234 return pw->n == 0;
237 #ifndef NO_REALIGN
238 __isl_give PW *FN(PW,realign_domain)(__isl_take PW *pw,
239 __isl_take isl_reordering *exp)
241 int i;
243 pw = FN(PW,cow)(pw);
244 if (!pw || !exp)
245 goto error;
247 for (i = 0; i < pw->n; ++i) {
248 pw->p[i].set = isl_set_realign(pw->p[i].set,
249 isl_reordering_copy(exp));
250 if (!pw->p[i].set)
251 goto error;
252 pw->p[i].FIELD = FN(EL,realign_domain)(pw->p[i].FIELD,
253 isl_reordering_copy(exp));
254 if (!pw->p[i].FIELD)
255 goto error;
258 pw = FN(PW,reset_domain_space)(pw, isl_space_copy(exp->dim));
260 isl_reordering_free(exp);
261 return pw;
262 error:
263 isl_reordering_free(exp);
264 FN(PW,free)(pw);
265 return NULL;
268 /* Align the parameters of "pw" to those of "model".
270 __isl_give PW *FN(PW,align_params)(__isl_take PW *pw, __isl_take isl_space *model)
272 isl_ctx *ctx;
274 if (!pw || !model)
275 goto error;
277 ctx = isl_space_get_ctx(model);
278 if (!isl_space_has_named_params(model))
279 isl_die(ctx, isl_error_invalid,
280 "model has unnamed parameters", goto error);
281 if (!isl_space_has_named_params(pw->dim))
282 isl_die(ctx, isl_error_invalid,
283 "input has unnamed parameters", goto error);
284 if (!isl_space_match(pw->dim, isl_dim_param, model, isl_dim_param)) {
285 isl_reordering *exp;
287 model = isl_space_drop_dims(model, isl_dim_in,
288 0, isl_space_dim(model, isl_dim_in));
289 model = isl_space_drop_dims(model, isl_dim_out,
290 0, isl_space_dim(model, isl_dim_out));
291 exp = isl_parameter_alignment_reordering(pw->dim, model);
292 exp = isl_reordering_extend_space(exp,
293 FN(PW,get_domain_space)(pw));
294 pw = FN(PW,realign_domain)(pw, exp);
297 isl_space_free(model);
298 return pw;
299 error:
300 isl_space_free(model);
301 FN(PW,free)(pw);
302 return NULL;
305 static __isl_give PW *FN(PW,align_params_pw_pw_and)(__isl_take PW *pw1,
306 __isl_take PW *pw2,
307 __isl_give PW *(*fn)(__isl_take PW *pw1, __isl_take PW *pw2))
309 isl_ctx *ctx;
311 if (!pw1 || !pw2)
312 goto error;
313 if (isl_space_match(pw1->dim, isl_dim_param, pw2->dim, isl_dim_param))
314 return fn(pw1, pw2);
315 ctx = FN(PW,get_ctx)(pw1);
316 if (!isl_space_has_named_params(pw1->dim) ||
317 !isl_space_has_named_params(pw2->dim))
318 isl_die(ctx, isl_error_invalid,
319 "unaligned unnamed parameters", goto error);
320 pw1 = FN(PW,align_params)(pw1, FN(PW,get_space)(pw2));
321 pw2 = FN(PW,align_params)(pw2, FN(PW,get_space)(pw1));
322 return fn(pw1, pw2);
323 error:
324 FN(PW,free)(pw1);
325 FN(PW,free)(pw2);
326 return NULL;
329 static __isl_give PW *FN(PW,align_params_pw_set_and)(__isl_take PW *pw,
330 __isl_take isl_set *set,
331 __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_set *set))
333 isl_ctx *ctx;
335 if (!pw || !set)
336 goto error;
337 if (isl_space_match(pw->dim, isl_dim_param, set->dim, isl_dim_param))
338 return fn(pw, set);
339 ctx = FN(PW,get_ctx)(pw);
340 if (!isl_space_has_named_params(pw->dim) ||
341 !isl_space_has_named_params(set->dim))
342 isl_die(ctx, isl_error_invalid,
343 "unaligned unnamed parameters", goto error);
344 pw = FN(PW,align_params)(pw, isl_set_get_space(set));
345 set = isl_set_align_params(set, FN(PW,get_space)(pw));
346 return fn(pw, set);
347 error:
348 FN(PW,free)(pw);
349 isl_set_free(set);
350 return NULL;
352 #endif
354 static __isl_give PW *FN(PW,union_add_aligned)(__isl_take PW *pw1,
355 __isl_take PW *pw2)
357 int i, j, n;
358 struct PW *res;
359 isl_ctx *ctx;
360 isl_set *set;
362 if (!pw1 || !pw2)
363 goto error;
365 ctx = isl_space_get_ctx(pw1->dim);
366 #ifdef HAS_TYPE
367 if (pw1->type != pw2->type)
368 isl_die(ctx, isl_error_invalid,
369 "fold types don't match", goto error);
370 #endif
371 isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
373 if (FN(PW,IS_ZERO)(pw1)) {
374 FN(PW,free)(pw1);
375 return pw2;
378 if (FN(PW,IS_ZERO)(pw2)) {
379 FN(PW,free)(pw2);
380 return pw1;
383 n = (pw1->n + 1) * (pw2->n + 1);
384 #ifdef HAS_TYPE
385 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), pw1->type, n);
386 #else
387 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), n);
388 #endif
390 for (i = 0; i < pw1->n; ++i) {
391 set = isl_set_copy(pw1->p[i].set);
392 for (j = 0; j < pw2->n; ++j) {
393 struct isl_set *common;
394 EL *sum;
395 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
396 isl_set_copy(pw2->p[j].set));
397 if (isl_set_plain_is_empty(common)) {
398 isl_set_free(common);
399 continue;
401 set = isl_set_subtract(set,
402 isl_set_copy(pw2->p[j].set));
404 sum = FN(EL,add_on_domain)(common,
405 FN(EL,copy)(pw1->p[i].FIELD),
406 FN(EL,copy)(pw2->p[j].FIELD));
408 res = FN(PW,add_piece)(res, common, sum);
410 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw1->p[i].FIELD));
413 for (j = 0; j < pw2->n; ++j) {
414 set = isl_set_copy(pw2->p[j].set);
415 for (i = 0; i < pw1->n; ++i)
416 set = isl_set_subtract(set,
417 isl_set_copy(pw1->p[i].set));
418 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw2->p[j].FIELD));
421 FN(PW,free)(pw1);
422 FN(PW,free)(pw2);
424 return res;
425 error:
426 FN(PW,free)(pw1);
427 FN(PW,free)(pw2);
428 return NULL;
431 /* Private version of "union_add". For isl_pw_qpolynomial and
432 * isl_pw_qpolynomial_fold, we prefer to simply call it "add".
434 static __isl_give PW *FN(PW,union_add_)(__isl_take PW *pw1, __isl_take PW *pw2)
436 return FN(PW,align_params_pw_pw_and)(pw1, pw2,
437 &FN(PW,union_add_aligned));
440 /* Make sure "pw" has room for at least "n" more pieces.
442 * If there is only one reference to pw, we extend it in place.
443 * Otherwise, we create a new PW and copy the pieces.
445 static __isl_give PW *FN(PW,grow)(__isl_take PW *pw, int n)
447 int i;
448 isl_ctx *ctx;
449 PW *res;
451 if (!pw)
452 return NULL;
453 if (pw->n + n <= pw->size)
454 return pw;
455 ctx = FN(PW,get_ctx)(pw);
456 n += pw->n;
457 if (pw->ref == 1) {
458 res = isl_realloc(ctx, pw, struct PW,
459 sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
460 if (!res)
461 return FN(PW,free)(pw);
462 res->size = n;
463 return res;
465 #ifdef HAS_TYPE
466 res = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->type, n);
467 #else
468 res = FN(PW,alloc_size)(isl_space_copy(pw->dim), n);
469 #endif
470 if (!res)
471 return FN(PW,free)(pw);
472 for (i = 0; i < pw->n; ++i)
473 res = FN(PW,add_piece)(res, isl_set_copy(pw->p[i].set),
474 FN(EL,copy)(pw->p[i].FIELD));
475 FN(PW,free)(pw);
476 return res;
479 static __isl_give PW *FN(PW,add_disjoint_aligned)(__isl_take PW *pw1,
480 __isl_take PW *pw2)
482 int i;
483 isl_ctx *ctx;
485 if (!pw1 || !pw2)
486 goto error;
488 if (pw1->size < pw1->n + pw2->n && pw1->n < pw2->n)
489 return FN(PW,add_disjoint_aligned)(pw2, pw1);
491 ctx = isl_space_get_ctx(pw1->dim);
492 #ifdef HAS_TYPE
493 if (pw1->type != pw2->type)
494 isl_die(ctx, isl_error_invalid,
495 "fold types don't match", goto error);
496 #endif
497 isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
499 if (FN(PW,IS_ZERO)(pw1)) {
500 FN(PW,free)(pw1);
501 return pw2;
504 if (FN(PW,IS_ZERO)(pw2)) {
505 FN(PW,free)(pw2);
506 return pw1;
509 pw1 = FN(PW,grow)(pw1, pw2->n);
510 if (!pw1)
511 goto error;
513 for (i = 0; i < pw2->n; ++i)
514 pw1 = FN(PW,add_piece)(pw1,
515 isl_set_copy(pw2->p[i].set),
516 FN(EL,copy)(pw2->p[i].FIELD));
518 FN(PW,free)(pw2);
520 return pw1;
521 error:
522 FN(PW,free)(pw1);
523 FN(PW,free)(pw2);
524 return NULL;
527 __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
529 return FN(PW,align_params_pw_pw_and)(pw1, pw2,
530 &FN(PW,add_disjoint_aligned));
533 /* This function is currently only used from isl_aff.c
535 static __isl_give PW *FN(PW,on_shared_domain_in)(__isl_take PW *pw1,
536 __isl_take PW *pw2, __isl_take isl_space *space,
537 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
538 __attribute__ ((unused));
540 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
541 * The result of "fn" (and therefore also of this function) lives in "space".
543 static __isl_give PW *FN(PW,on_shared_domain_in)(__isl_take PW *pw1,
544 __isl_take PW *pw2, __isl_take isl_space *space,
545 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
547 int i, j, n;
548 PW *res = NULL;
550 if (!pw1 || !pw2)
551 goto error;
553 n = pw1->n * pw2->n;
554 #ifdef HAS_TYPE
555 res = FN(PW,alloc_size)(isl_space_copy(space), pw1->type, n);
556 #else
557 res = FN(PW,alloc_size)(isl_space_copy(space), n);
558 #endif
560 for (i = 0; i < pw1->n; ++i) {
561 for (j = 0; j < pw2->n; ++j) {
562 isl_set *common;
563 EL *res_ij;
564 int empty;
566 common = isl_set_intersect(
567 isl_set_copy(pw1->p[i].set),
568 isl_set_copy(pw2->p[j].set));
569 empty = isl_set_plain_is_empty(common);
570 if (empty < 0 || empty) {
571 isl_set_free(common);
572 if (empty < 0)
573 goto error;
574 continue;
577 res_ij = fn(FN(EL,copy)(pw1->p[i].FIELD),
578 FN(EL,copy)(pw2->p[j].FIELD));
579 res_ij = FN(EL,gist)(res_ij, isl_set_copy(common));
581 res = FN(PW,add_piece)(res, common, res_ij);
585 isl_space_free(space);
586 FN(PW,free)(pw1);
587 FN(PW,free)(pw2);
588 return res;
589 error:
590 isl_space_free(space);
591 FN(PW,free)(pw1);
592 FN(PW,free)(pw2);
593 FN(PW,free)(res);
594 return NULL;
597 /* This function is currently only used from isl_aff.c
599 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
600 __isl_take PW *pw2,
601 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
602 __attribute__ ((unused));
604 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
605 * The result of "fn" is assumed to live in the same space as "pw1" and "pw2".
607 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
608 __isl_take PW *pw2,
609 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
611 isl_space *space;
613 if (!pw1 || !pw2)
614 goto error;
616 space = isl_space_copy(pw1->dim);
617 return FN(PW,on_shared_domain_in)(pw1, pw2, space, fn);
618 error:
619 FN(PW,free)(pw1);
620 FN(PW,free)(pw2);
621 return NULL;
624 #ifndef NO_NEG
625 __isl_give PW *FN(PW,neg)(__isl_take PW *pw)
627 int i;
629 if (!pw)
630 return NULL;
632 if (FN(PW,IS_ZERO)(pw))
633 return pw;
635 pw = FN(PW,cow)(pw);
636 if (!pw)
637 return NULL;
639 for (i = 0; i < pw->n; ++i) {
640 pw->p[i].FIELD = FN(EL,neg)(pw->p[i].FIELD);
641 if (!pw->p[i].FIELD)
642 return FN(PW,free)(pw);
645 return pw;
647 #endif
649 #ifndef NO_SUB
650 __isl_give PW *FN(PW,sub)(__isl_take PW *pw1, __isl_take PW *pw2)
652 return FN(PW,add)(pw1, FN(PW,neg)(pw2));
654 #endif
656 #ifndef NO_EVAL
657 __isl_give isl_val *FN(PW,eval)(__isl_take PW *pw, __isl_take isl_point *pnt)
659 int i;
660 int found = 0;
661 isl_ctx *ctx;
662 isl_space *pnt_dim = NULL;
663 isl_val *v;
665 if (!pw || !pnt)
666 goto error;
667 ctx = isl_point_get_ctx(pnt);
668 pnt_dim = isl_point_get_space(pnt);
669 isl_assert(ctx, isl_space_is_domain_internal(pnt_dim, pw->dim),
670 goto error);
672 for (i = 0; i < pw->n; ++i) {
673 found = isl_set_contains_point(pw->p[i].set, pnt);
674 if (found < 0)
675 goto error;
676 if (found)
677 break;
679 if (found)
680 v = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
681 isl_point_copy(pnt));
682 else
683 v = isl_val_zero(ctx);
684 FN(PW,free)(pw);
685 isl_space_free(pnt_dim);
686 isl_point_free(pnt);
687 return v;
688 error:
689 FN(PW,free)(pw);
690 isl_space_free(pnt_dim);
691 isl_point_free(pnt);
692 return NULL;
694 #endif
696 /* Return the parameter domain of "pw".
698 __isl_give isl_set *FN(PW,params)(__isl_take PW *pw)
700 return isl_set_params(FN(PW,domain)(pw));
703 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
705 int i;
706 isl_set *dom;
708 if (!pw)
709 return NULL;
711 dom = isl_set_empty(FN(PW,get_domain_space)(pw));
712 for (i = 0; i < pw->n; ++i)
713 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
715 FN(PW,free)(pw);
717 return dom;
720 /* Exploit the equalities in the domain of piece "i" of "pw"
721 * to simplify the associated function.
722 * If the domain of piece "i" is empty, then remove it entirely,
723 * replacing it with the final piece.
725 static int FN(PW,exploit_equalities_and_remove_if_empty)(__isl_keep PW *pw,
726 int i)
728 isl_basic_set *aff;
729 int empty = isl_set_plain_is_empty(pw->p[i].set);
731 if (empty < 0)
732 return -1;
733 if (empty) {
734 isl_set_free(pw->p[i].set);
735 FN(EL,free)(pw->p[i].FIELD);
736 if (i != pw->n - 1)
737 pw->p[i] = pw->p[pw->n - 1];
738 pw->n--;
740 return 0;
743 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
744 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD, aff);
745 if (!pw->p[i].FIELD)
746 return -1;
748 return 0;
751 /* Convert a piecewise expression defined over a parameter domain
752 * into one that is defined over a zero-dimensional set.
754 __isl_give PW *FN(PW,from_range)(__isl_take PW *pw)
756 isl_space *space;
758 if (!pw)
759 return NULL;
760 if (!isl_space_is_set(pw->dim))
761 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
762 "not living in a set space", return FN(PW,free)(pw));
764 space = FN(PW,get_space)(pw);
765 space = isl_space_from_range(space);
766 pw = FN(PW,reset_space)(pw, space);
768 return pw;
771 /* Fix the value of the given parameter or domain dimension of "pw"
772 * to be equal to "value".
774 __isl_give PW *FN(PW,fix_si)(__isl_take PW *pw, enum isl_dim_type type,
775 unsigned pos, int value)
777 int i;
779 if (!pw)
780 return NULL;
782 if (type == isl_dim_out)
783 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
784 "cannot fix output dimension", return FN(PW,free)(pw));
786 if (pw->n == 0)
787 return pw;
789 if (type == isl_dim_in)
790 type = isl_dim_set;
792 pw = FN(PW,cow)(pw);
793 if (!pw)
794 return FN(PW,free)(pw);
796 for (i = pw->n - 1; i >= 0; --i) {
797 pw->p[i].set = isl_set_fix_si(pw->p[i].set, type, pos, value);
798 if (FN(PW,exploit_equalities_and_remove_if_empty)(pw, i) < 0)
799 return FN(PW,free)(pw);
802 return pw;
805 /* Restrict the domain of "pw" by combining each cell
806 * with "set" through a call to "fn", where "fn" may be
807 * isl_set_intersect, isl_set_intersect_params or isl_set_subtract.
809 static __isl_give PW *FN(PW,restrict_domain_aligned)(__isl_take PW *pw,
810 __isl_take isl_set *set,
811 __isl_give isl_set *(*fn)(__isl_take isl_set *set1,
812 __isl_take isl_set *set2))
814 int i;
816 if (!pw || !set)
817 goto error;
819 if (pw->n == 0) {
820 isl_set_free(set);
821 return pw;
824 pw = FN(PW,cow)(pw);
825 if (!pw)
826 goto error;
828 for (i = pw->n - 1; i >= 0; --i) {
829 pw->p[i].set = fn(pw->p[i].set, isl_set_copy(set));
830 if (FN(PW,exploit_equalities_and_remove_if_empty)(pw, i) < 0)
831 goto error;
834 isl_set_free(set);
835 return pw;
836 error:
837 isl_set_free(set);
838 FN(PW,free)(pw);
839 return NULL;
842 static __isl_give PW *FN(PW,intersect_domain_aligned)(__isl_take PW *pw,
843 __isl_take isl_set *set)
845 return FN(PW,restrict_domain_aligned)(pw, set, &isl_set_intersect);
848 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw,
849 __isl_take isl_set *context)
851 return FN(PW,align_params_pw_set_and)(pw, context,
852 &FN(PW,intersect_domain_aligned));
855 static __isl_give PW *FN(PW,intersect_params_aligned)(__isl_take PW *pw,
856 __isl_take isl_set *set)
858 return FN(PW,restrict_domain_aligned)(pw, set,
859 &isl_set_intersect_params);
862 /* Intersect the domain of "pw" with the parameter domain "context".
864 __isl_give PW *FN(PW,intersect_params)(__isl_take PW *pw,
865 __isl_take isl_set *context)
867 return FN(PW,align_params_pw_set_and)(pw, context,
868 &FN(PW,intersect_params_aligned));
871 /* Subtract "domain' from the domain of "pw", assuming their
872 * parameters have been aligned.
874 static __isl_give PW *FN(PW,subtract_domain_aligned)(__isl_take PW *pw,
875 __isl_take isl_set *domain)
877 return FN(PW,restrict_domain_aligned)(pw, domain, &isl_set_subtract);
880 /* Subtract "domain' from the domain of "pw".
882 __isl_give PW *FN(PW,subtract_domain)(__isl_take PW *pw,
883 __isl_take isl_set *domain)
885 return FN(PW,align_params_pw_set_and)(pw, domain,
886 &FN(PW,subtract_domain_aligned));
889 /* Compute the gist of "pw" with respect to the domain constraints
890 * of "context" for the case where the domain of the last element
891 * of "pw" is equal to "context".
892 * Call "fn_el" to compute the gist of this element, replace
893 * its domain by the universe and drop all other elements
894 * as their domains are necessarily disjoint from "context".
896 static __isl_give PW *FN(PW,gist_last)(__isl_take PW *pw,
897 __isl_take isl_set *context,
898 __isl_give EL *(*fn_el)(__isl_take EL *el, __isl_take isl_set *set))
900 int i;
901 isl_space *space;
903 for (i = 0; i < pw->n - 1; ++i) {
904 isl_set_free(pw->p[i].set);
905 FN(EL,free)(pw->p[i].FIELD);
907 pw->p[0].FIELD = pw->p[pw->n - 1].FIELD;
908 pw->p[0].set = pw->p[pw->n - 1].set;
909 pw->n = 1;
911 space = isl_set_get_space(context);
912 pw->p[0].FIELD = fn_el(pw->p[0].FIELD, context);
913 context = isl_set_universe(space);
914 isl_set_free(pw->p[0].set);
915 pw->p[0].set = context;
917 if (!pw->p[0].FIELD || !pw->p[0].set)
918 return FN(PW,free)(pw);
920 return pw;
923 /* Compute the gist of "pw" with respect to the domain constraints
924 * of "context". Call "fn_el" to compute the gist of the elements
925 * and "fn_dom" to compute the gist of the domains.
927 * If the piecewise expression is empty or the context is the universe,
928 * then nothing can be simplified.
930 static __isl_give PW *FN(PW,gist_aligned)(__isl_take PW *pw,
931 __isl_take isl_set *context,
932 __isl_give EL *(*fn_el)(__isl_take EL *el,
933 __isl_take isl_set *set),
934 __isl_give isl_set *(*fn_dom)(__isl_take isl_set *set,
935 __isl_take isl_basic_set *bset))
937 int i;
938 int is_universe;
939 isl_basic_set *hull = NULL;
941 if (!pw || !context)
942 goto error;
944 if (pw->n == 0) {
945 isl_set_free(context);
946 return pw;
949 is_universe = isl_set_plain_is_universe(context);
950 if (is_universe < 0)
951 goto error;
952 if (is_universe) {
953 isl_set_free(context);
954 return pw;
957 if (!isl_space_match(pw->dim, isl_dim_param,
958 context->dim, isl_dim_param)) {
959 pw = FN(PW,align_params)(pw, isl_set_get_space(context));
960 context = isl_set_align_params(context, FN(PW,get_space)(pw));
963 pw = FN(PW,cow)(pw);
964 if (!pw)
965 goto error;
967 if (pw->n == 1) {
968 int equal;
970 equal = isl_set_plain_is_equal(pw->p[0].set, context);
971 if (equal < 0)
972 goto error;
973 if (equal)
974 return FN(PW,gist_last)(pw, context, fn_el);
977 context = isl_set_compute_divs(context);
978 hull = isl_set_simple_hull(isl_set_copy(context));
980 for (i = pw->n - 1; i >= 0; --i) {
981 isl_set *set_i;
982 int empty;
984 if (i == pw->n - 1) {
985 int equal;
986 equal = isl_set_plain_is_equal(pw->p[i].set, context);
987 if (equal < 0)
988 goto error;
989 if (equal) {
990 isl_basic_set_free(hull);
991 return FN(PW,gist_last)(pw, context, fn_el);
994 set_i = isl_set_intersect(isl_set_copy(pw->p[i].set),
995 isl_set_copy(context));
996 empty = isl_set_plain_is_empty(set_i);
997 pw->p[i].FIELD = fn_el(pw->p[i].FIELD, set_i);
998 pw->p[i].set = fn_dom(pw->p[i].set, isl_basic_set_copy(hull));
999 if (empty < 0 || !pw->p[i].FIELD || !pw->p[i].set)
1000 goto error;
1001 if (empty) {
1002 isl_set_free(pw->p[i].set);
1003 FN(EL,free)(pw->p[i].FIELD);
1004 if (i != pw->n - 1)
1005 pw->p[i] = pw->p[pw->n - 1];
1006 pw->n--;
1010 isl_basic_set_free(hull);
1011 isl_set_free(context);
1013 return pw;
1014 error:
1015 FN(PW,free)(pw);
1016 isl_basic_set_free(hull);
1017 isl_set_free(context);
1018 return NULL;
1021 static __isl_give PW *FN(PW,gist_domain_aligned)(__isl_take PW *pw,
1022 __isl_take isl_set *set)
1024 return FN(PW,gist_aligned)(pw, set, &FN(EL,gist),
1025 &isl_set_gist_basic_set);
1028 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
1030 return FN(PW,align_params_pw_set_and)(pw, context,
1031 &FN(PW,gist_domain_aligned));
1034 static __isl_give PW *FN(PW,gist_params_aligned)(__isl_take PW *pw,
1035 __isl_take isl_set *set)
1037 return FN(PW,gist_aligned)(pw, set, &FN(EL,gist_params),
1038 &isl_set_gist_params_basic_set);
1041 __isl_give PW *FN(PW,gist_params)(__isl_take PW *pw,
1042 __isl_take isl_set *context)
1044 return FN(PW,align_params_pw_set_and)(pw, context,
1045 &FN(PW,gist_params_aligned));
1048 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
1050 int i, j;
1052 if (!pw)
1053 return NULL;
1054 if (pw->n == 0)
1055 return pw;
1057 for (i = pw->n - 1; i >= 0; --i) {
1058 for (j = i - 1; j >= 0; --j) {
1059 if (!FN(EL,plain_is_equal)(pw->p[i].FIELD,
1060 pw->p[j].FIELD))
1061 continue;
1062 pw->p[j].set = isl_set_union(pw->p[j].set,
1063 pw->p[i].set);
1064 FN(EL,free)(pw->p[i].FIELD);
1065 if (i != pw->n - 1)
1066 pw->p[i] = pw->p[pw->n - 1];
1067 pw->n--;
1068 break;
1070 if (j >= 0)
1071 continue;
1072 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
1073 if (!pw->p[i].set)
1074 goto error;
1077 return pw;
1078 error:
1079 FN(PW,free)(pw);
1080 return NULL;
1083 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
1085 return pw ? isl_space_get_ctx(pw->dim) : NULL;
1088 #ifndef NO_INVOLVES_DIMS
1089 int FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
1090 unsigned first, unsigned n)
1092 int i;
1093 enum isl_dim_type set_type;
1095 if (!pw)
1096 return -1;
1097 if (pw->n == 0 || n == 0)
1098 return 0;
1100 set_type = type == isl_dim_in ? isl_dim_set : type;
1102 for (i = 0; i < pw->n; ++i) {
1103 int involves = FN(EL,involves_dims)(pw->p[i].FIELD,
1104 type, first, n);
1105 if (involves < 0 || involves)
1106 return involves;
1107 involves = isl_set_involves_dims(pw->p[i].set,
1108 set_type, first, n);
1109 if (involves < 0 || involves)
1110 return involves;
1112 return 0;
1114 #endif
1116 __isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
1117 enum isl_dim_type type, unsigned pos, const char *s)
1119 int i;
1120 enum isl_dim_type set_type;
1122 pw = FN(PW,cow)(pw);
1123 if (!pw)
1124 return NULL;
1126 set_type = type == isl_dim_in ? isl_dim_set : type;
1128 pw->dim = isl_space_set_dim_name(pw->dim, type, pos, s);
1129 if (!pw->dim)
1130 goto error;
1132 for (i = 0; i < pw->n; ++i) {
1133 pw->p[i].set = isl_set_set_dim_name(pw->p[i].set,
1134 set_type, pos, s);
1135 if (!pw->p[i].set)
1136 goto error;
1137 pw->p[i].FIELD = FN(EL,set_dim_name)(pw->p[i].FIELD, type, pos, s);
1138 if (!pw->p[i].FIELD)
1139 goto error;
1142 return pw;
1143 error:
1144 FN(PW,free)(pw);
1145 return NULL;
1148 #ifndef NO_DROP_DIMS
1149 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
1150 enum isl_dim_type type, unsigned first, unsigned n)
1152 int i;
1153 enum isl_dim_type set_type;
1155 if (!pw)
1156 return NULL;
1157 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
1158 return pw;
1160 set_type = type == isl_dim_in ? isl_dim_set : type;
1162 pw = FN(PW,cow)(pw);
1163 if (!pw)
1164 return NULL;
1165 pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
1166 if (!pw->dim)
1167 goto error;
1168 for (i = 0; i < pw->n; ++i) {
1169 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
1170 if (!pw->p[i].FIELD)
1171 goto error;
1172 if (type == isl_dim_out)
1173 continue;
1174 pw->p[i].set = isl_set_drop(pw->p[i].set, set_type, first, n);
1175 if (!pw->p[i].set)
1176 goto error;
1179 return pw;
1180 error:
1181 FN(PW,free)(pw);
1182 return NULL;
1185 /* This function is very similar to drop_dims.
1186 * The only difference is that the cells may still involve
1187 * the specified dimensions. They are removed using
1188 * isl_set_project_out instead of isl_set_drop.
1190 __isl_give PW *FN(PW,project_out)(__isl_take PW *pw,
1191 enum isl_dim_type type, unsigned first, unsigned n)
1193 int i;
1194 enum isl_dim_type set_type;
1196 if (!pw)
1197 return NULL;
1198 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
1199 return pw;
1201 set_type = type == isl_dim_in ? isl_dim_set : type;
1203 pw = FN(PW,cow)(pw);
1204 if (!pw)
1205 return NULL;
1206 pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
1207 if (!pw->dim)
1208 goto error;
1209 for (i = 0; i < pw->n; ++i) {
1210 pw->p[i].set = isl_set_project_out(pw->p[i].set,
1211 set_type, first, n);
1212 if (!pw->p[i].set)
1213 goto error;
1214 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
1215 if (!pw->p[i].FIELD)
1216 goto error;
1219 return pw;
1220 error:
1221 FN(PW,free)(pw);
1222 return NULL;
1225 /* Project the domain of pw onto its parameter space.
1227 __isl_give PW *FN(PW,project_domain_on_params)(__isl_take PW *pw)
1229 isl_space *space;
1230 unsigned n;
1232 n = FN(PW,dim)(pw, isl_dim_in);
1233 pw = FN(PW,project_out)(pw, isl_dim_in, 0, n);
1234 space = FN(PW,get_domain_space)(pw);
1235 space = isl_space_params(space);
1236 pw = FN(PW,reset_domain_space)(pw, space);
1237 return pw;
1239 #endif
1241 #ifndef NO_INSERT_DIMS
1242 __isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
1243 unsigned first, unsigned n)
1245 int i;
1246 enum isl_dim_type set_type;
1248 if (!pw)
1249 return NULL;
1250 if (n == 0 && !isl_space_is_named_or_nested(pw->dim, type))
1251 return pw;
1253 set_type = type == isl_dim_in ? isl_dim_set : type;
1255 pw = FN(PW,cow)(pw);
1256 if (!pw)
1257 return NULL;
1259 pw->dim = isl_space_insert_dims(pw->dim, type, first, n);
1260 if (!pw->dim)
1261 goto error;
1263 for (i = 0; i < pw->n; ++i) {
1264 pw->p[i].set = isl_set_insert_dims(pw->p[i].set,
1265 set_type, first, n);
1266 if (!pw->p[i].set)
1267 goto error;
1268 pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
1269 type, first, n);
1270 if (!pw->p[i].FIELD)
1271 goto error;
1274 return pw;
1275 error:
1276 FN(PW,free)(pw);
1277 return NULL;
1279 #endif
1281 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
1282 enum isl_dim_type type, unsigned pos, isl_int v)
1284 int i;
1286 if (!pw)
1287 return NULL;
1289 if (type == isl_dim_in)
1290 type = isl_dim_set;
1292 pw = FN(PW,cow)(pw);
1293 if (!pw)
1294 return NULL;
1295 for (i = 0; i < pw->n; ++i) {
1296 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
1297 if (FN(PW,exploit_equalities_and_remove_if_empty)(pw, i) < 0)
1298 return FN(PW,free)(pw);
1301 return pw;
1304 /* Fix the value of the variable at position "pos" of type "type" of "pw"
1305 * to be equal to "v".
1307 __isl_give PW *FN(PW,fix_val)(__isl_take PW *pw,
1308 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
1310 if (!v)
1311 return FN(PW,free)(pw);
1312 if (!isl_val_is_int(v))
1313 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
1314 "expecting integer value", goto error);
1316 pw = FN(PW,fix_dim)(pw, type, pos, v->n);
1317 isl_val_free(v);
1319 return pw;
1320 error:
1321 isl_val_free(v);
1322 return FN(PW,free)(pw);
1325 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
1327 return pw ? isl_space_dim(pw->dim, type) : 0;
1330 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
1331 enum isl_dim_type type, unsigned first, unsigned n)
1333 int i;
1335 if (!pw)
1336 return NULL;
1337 if (n == 0)
1338 return pw;
1340 if (type == isl_dim_in)
1341 type = isl_dim_set;
1343 pw = FN(PW,cow)(pw);
1344 if (!pw)
1345 return NULL;
1346 if (!pw->dim)
1347 goto error;
1348 for (i = 0; i < pw->n; ++i) {
1349 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
1350 if (!pw->p[i].set)
1351 goto error;
1354 return pw;
1355 error:
1356 FN(PW,free)(pw);
1357 return NULL;
1360 #ifndef NO_OPT
1361 /* Compute the maximal value attained by the piecewise quasipolynomial
1362 * on its domain or zero if the domain is empty.
1363 * In the worst case, the domain is scanned completely,
1364 * so the domain is assumed to be bounded.
1366 __isl_give isl_val *FN(PW,opt)(__isl_take PW *pw, int max)
1368 int i;
1369 isl_val *opt;
1371 if (!pw)
1372 return NULL;
1374 if (pw->n == 0) {
1375 opt = isl_val_zero(FN(PW,get_ctx)(pw));
1376 FN(PW,free)(pw);
1377 return opt;
1380 opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
1381 isl_set_copy(pw->p[0].set), max);
1382 for (i = 1; i < pw->n; ++i) {
1383 isl_val *opt_i;
1384 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
1385 isl_set_copy(pw->p[i].set), max);
1386 if (max)
1387 opt = isl_val_max(opt, opt_i);
1388 else
1389 opt = isl_val_min(opt, opt_i);
1392 FN(PW,free)(pw);
1393 return opt;
1396 __isl_give isl_val *FN(PW,max)(__isl_take PW *pw)
1398 return FN(PW,opt)(pw, 1);
1401 __isl_give isl_val *FN(PW,min)(__isl_take PW *pw)
1403 return FN(PW,opt)(pw, 0);
1405 #endif
1407 __isl_give isl_space *FN(PW,get_space)(__isl_keep PW *pw)
1409 return pw ? isl_space_copy(pw->dim) : NULL;
1412 __isl_give isl_space *FN(PW,get_domain_space)(__isl_keep PW *pw)
1414 return pw ? isl_space_domain(isl_space_copy(pw->dim)) : NULL;
1417 /* Return the position of the dimension of the given type and name
1418 * in "pw".
1419 * Return -1 if no such dimension can be found.
1421 int FN(PW,find_dim_by_name)(__isl_keep PW *pw,
1422 enum isl_dim_type type, const char *name)
1424 if (!pw)
1425 return -1;
1426 return isl_space_find_dim_by_name(pw->dim, type, name);
1429 #ifndef NO_RESET_DIM
1430 /* Reset the space of "pw". Since we don't know if the elements
1431 * represent the spaces themselves or their domains, we pass along
1432 * both when we call their reset_space_and_domain.
1434 static __isl_give PW *FN(PW,reset_space_and_domain)(__isl_take PW *pw,
1435 __isl_take isl_space *space, __isl_take isl_space *domain)
1437 int i;
1439 pw = FN(PW,cow)(pw);
1440 if (!pw || !space || !domain)
1441 goto error;
1443 for (i = 0; i < pw->n; ++i) {
1444 pw->p[i].set = isl_set_reset_space(pw->p[i].set,
1445 isl_space_copy(domain));
1446 if (!pw->p[i].set)
1447 goto error;
1448 pw->p[i].FIELD = FN(EL,reset_space_and_domain)(pw->p[i].FIELD,
1449 isl_space_copy(space), isl_space_copy(domain));
1450 if (!pw->p[i].FIELD)
1451 goto error;
1454 isl_space_free(domain);
1456 isl_space_free(pw->dim);
1457 pw->dim = space;
1459 return pw;
1460 error:
1461 isl_space_free(domain);
1462 isl_space_free(space);
1463 FN(PW,free)(pw);
1464 return NULL;
1467 __isl_give PW *FN(PW,reset_domain_space)(__isl_take PW *pw,
1468 __isl_take isl_space *domain)
1470 isl_space *space;
1472 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
1473 FN(PW,get_space)(pw));
1474 return FN(PW,reset_space_and_domain)(pw, space, domain);
1477 __isl_give PW *FN(PW,reset_space)(__isl_take PW *pw, __isl_take isl_space *dim)
1479 isl_space *domain;
1481 domain = isl_space_domain(isl_space_copy(dim));
1482 return FN(PW,reset_space_and_domain)(pw, dim, domain);
1485 __isl_give PW *FN(PW,set_tuple_id)(__isl_take PW *pw, enum isl_dim_type type,
1486 __isl_take isl_id *id)
1488 isl_space *space;
1490 pw = FN(PW,cow)(pw);
1491 if (!pw)
1492 goto error;
1494 space = FN(PW,get_space)(pw);
1495 space = isl_space_set_tuple_id(space, type, id);
1497 return FN(PW,reset_space)(pw, space);
1498 error:
1499 isl_id_free(id);
1500 return FN(PW,free)(pw);
1503 /* Drop the id on the specified tuple.
1505 __isl_give PW *FN(PW,reset_tuple_id)(__isl_take PW *pw, enum isl_dim_type type)
1507 isl_space *space;
1509 if (!pw)
1510 return NULL;
1511 if (!FN(PW,has_tuple_id)(pw, type))
1512 return pw;
1514 pw = FN(PW,cow)(pw);
1515 if (!pw)
1516 return NULL;
1518 space = FN(PW,get_space)(pw);
1519 space = isl_space_reset_tuple_id(space, type);
1521 return FN(PW,reset_space)(pw, space);
1524 __isl_give PW *FN(PW,set_dim_id)(__isl_take PW *pw,
1525 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1527 pw = FN(PW,cow)(pw);
1528 if (!pw)
1529 goto error;
1530 pw->dim = isl_space_set_dim_id(pw->dim, type, pos, id);
1531 return FN(PW,reset_space)(pw, isl_space_copy(pw->dim));
1532 error:
1533 isl_id_free(id);
1534 return FN(PW,free)(pw);
1536 #endif
1538 /* Reset the user pointer on all identifiers of parameters and tuples
1539 * of the space of "pw".
1541 __isl_give PW *FN(PW,reset_user)(__isl_take PW *pw)
1543 isl_space *space;
1545 space = FN(PW,get_space)(pw);
1546 space = isl_space_reset_user(space);
1548 return FN(PW,reset_space)(pw, space);
1551 int FN(PW,has_equal_space)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1553 if (!pw1 || !pw2)
1554 return -1;
1556 return isl_space_is_equal(pw1->dim, pw2->dim);
1559 #ifndef NO_MORPH
1560 __isl_give PW *FN(PW,morph_domain)(__isl_take PW *pw,
1561 __isl_take isl_morph *morph)
1563 int i;
1564 isl_ctx *ctx;
1566 if (!pw || !morph)
1567 goto error;
1569 ctx = isl_space_get_ctx(pw->dim);
1570 isl_assert(ctx, isl_space_is_domain_internal(morph->dom->dim, pw->dim),
1571 goto error);
1573 pw = FN(PW,cow)(pw);
1574 if (!pw)
1575 goto error;
1576 pw->dim = isl_space_extend_domain_with_range(
1577 isl_space_copy(morph->ran->dim), pw->dim);
1578 if (!pw->dim)
1579 goto error;
1581 for (i = 0; i < pw->n; ++i) {
1582 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
1583 if (!pw->p[i].set)
1584 goto error;
1585 pw->p[i].FIELD = FN(EL,morph_domain)(pw->p[i].FIELD,
1586 isl_morph_copy(morph));
1587 if (!pw->p[i].FIELD)
1588 goto error;
1591 isl_morph_free(morph);
1593 return pw;
1594 error:
1595 FN(PW,free)(pw);
1596 isl_morph_free(morph);
1597 return NULL;
1599 #endif
1601 int FN(PW,n_piece)(__isl_keep PW *pw)
1603 return pw ? pw->n : 0;
1606 int FN(PW,foreach_piece)(__isl_keep PW *pw,
1607 int (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
1608 void *user)
1610 int i;
1612 if (!pw)
1613 return -1;
1615 for (i = 0; i < pw->n; ++i)
1616 if (fn(isl_set_copy(pw->p[i].set),
1617 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
1618 return -1;
1620 return 0;
1623 #ifndef NO_LIFT
1624 static int any_divs(__isl_keep isl_set *set)
1626 int i;
1628 if (!set)
1629 return -1;
1631 for (i = 0; i < set->n; ++i)
1632 if (set->p[i]->n_div > 0)
1633 return 1;
1635 return 0;
1638 static int foreach_lifted_subset(__isl_take isl_set *set, __isl_take EL *el,
1639 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1640 void *user), void *user)
1642 int i;
1644 if (!set || !el)
1645 goto error;
1647 for (i = 0; i < set->n; ++i) {
1648 isl_set *lift;
1649 EL *copy;
1651 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
1652 lift = isl_set_lift(lift);
1654 copy = FN(EL,copy)(el);
1655 copy = FN(EL,lift)(copy, isl_set_get_space(lift));
1657 if (fn(lift, copy, user) < 0)
1658 goto error;
1661 isl_set_free(set);
1662 FN(EL,free)(el);
1664 return 0;
1665 error:
1666 isl_set_free(set);
1667 FN(EL,free)(el);
1668 return -1;
1671 int FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
1672 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1673 void *user), void *user)
1675 int i;
1677 if (!pw)
1678 return -1;
1680 for (i = 0; i < pw->n; ++i) {
1681 isl_set *set;
1682 EL *el;
1684 set = isl_set_copy(pw->p[i].set);
1685 el = FN(EL,copy)(pw->p[i].FIELD);
1686 if (!any_divs(set)) {
1687 if (fn(set, el, user) < 0)
1688 return -1;
1689 continue;
1691 if (foreach_lifted_subset(set, el, fn, user) < 0)
1692 return -1;
1695 return 0;
1697 #endif
1699 #ifndef NO_MOVE_DIMS
1700 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
1701 enum isl_dim_type dst_type, unsigned dst_pos,
1702 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1704 int i;
1706 pw = FN(PW,cow)(pw);
1707 if (!pw)
1708 return NULL;
1710 pw->dim = isl_space_move_dims(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
1711 if (!pw->dim)
1712 goto error;
1714 for (i = 0; i < pw->n; ++i) {
1715 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
1716 dst_type, dst_pos, src_type, src_pos, n);
1717 if (!pw->p[i].FIELD)
1718 goto error;
1721 if (dst_type == isl_dim_in)
1722 dst_type = isl_dim_set;
1723 if (src_type == isl_dim_in)
1724 src_type = isl_dim_set;
1726 for (i = 0; i < pw->n; ++i) {
1727 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
1728 dst_type, dst_pos,
1729 src_type, src_pos, n);
1730 if (!pw->p[i].set)
1731 goto error;
1734 return pw;
1735 error:
1736 FN(PW,free)(pw);
1737 return NULL;
1739 #endif
1741 __isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
1743 int i;
1745 if (isl_int_is_one(v))
1746 return pw;
1747 if (pw && DEFAULT_IS_ZERO && isl_int_is_zero(v)) {
1748 PW *zero;
1749 isl_space *dim = FN(PW,get_space)(pw);
1750 #ifdef HAS_TYPE
1751 zero = FN(PW,ZERO)(dim, pw->type);
1752 #else
1753 zero = FN(PW,ZERO)(dim);
1754 #endif
1755 FN(PW,free)(pw);
1756 return zero;
1758 pw = FN(PW,cow)(pw);
1759 if (!pw)
1760 return NULL;
1761 if (pw->n == 0)
1762 return pw;
1764 #ifdef HAS_TYPE
1765 if (isl_int_is_neg(v))
1766 pw->type = isl_fold_type_negate(pw->type);
1767 #endif
1768 for (i = 0; i < pw->n; ++i) {
1769 pw->p[i].FIELD = FN(EL,scale)(pw->p[i].FIELD, v);
1770 if (!pw->p[i].FIELD)
1771 goto error;
1774 return pw;
1775 error:
1776 FN(PW,free)(pw);
1777 return NULL;
1780 /* Multiply the pieces of "pw" by "v" and return the result.
1782 __isl_give PW *FN(PW,scale_val)(__isl_take PW *pw, __isl_take isl_val *v)
1784 int i;
1786 if (!pw || !v)
1787 goto error;
1789 if (isl_val_is_one(v)) {
1790 isl_val_free(v);
1791 return pw;
1793 if (pw && DEFAULT_IS_ZERO && isl_val_is_zero(v)) {
1794 PW *zero;
1795 isl_space *space = FN(PW,get_space)(pw);
1796 #ifdef HAS_TYPE
1797 zero = FN(PW,ZERO)(space, pw->type);
1798 #else
1799 zero = FN(PW,ZERO)(space);
1800 #endif
1801 FN(PW,free)(pw);
1802 isl_val_free(v);
1803 return zero;
1805 if (pw->n == 0) {
1806 isl_val_free(v);
1807 return pw;
1809 pw = FN(PW,cow)(pw);
1810 if (!pw)
1811 goto error;
1813 #ifdef HAS_TYPE
1814 if (isl_val_is_neg(v))
1815 pw->type = isl_fold_type_negate(pw->type);
1816 #endif
1817 for (i = 0; i < pw->n; ++i) {
1818 pw->p[i].FIELD = FN(EL,scale_val)(pw->p[i].FIELD,
1819 isl_val_copy(v));
1820 if (!pw->p[i].FIELD)
1821 goto error;
1824 isl_val_free(v);
1825 return pw;
1826 error:
1827 isl_val_free(v);
1828 FN(PW,free)(pw);
1829 return NULL;
1832 /* Divide the pieces of "pw" by "v" and return the result.
1834 __isl_give PW *FN(PW,scale_down_val)(__isl_take PW *pw, __isl_take isl_val *v)
1836 int i;
1838 if (!pw || !v)
1839 goto error;
1841 if (isl_val_is_one(v)) {
1842 isl_val_free(v);
1843 return pw;
1846 if (!isl_val_is_rat(v))
1847 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1848 "expecting rational factor", goto error);
1849 if (isl_val_is_zero(v))
1850 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1851 "cannot scale down by zero", goto error);
1853 if (pw->n == 0) {
1854 isl_val_free(v);
1855 return pw;
1857 pw = FN(PW,cow)(pw);
1858 if (!pw)
1859 goto error;
1861 #ifdef HAS_TYPE
1862 if (isl_val_is_neg(v))
1863 pw->type = isl_fold_type_negate(pw->type);
1864 #endif
1865 for (i = 0; i < pw->n; ++i) {
1866 pw->p[i].FIELD = FN(EL,scale_down_val)(pw->p[i].FIELD,
1867 isl_val_copy(v));
1868 if (!pw->p[i].FIELD)
1869 goto error;
1872 isl_val_free(v);
1873 return pw;
1874 error:
1875 isl_val_free(v);
1876 FN(PW,free)(pw);
1877 return NULL;
1880 __isl_give PW *FN(PW,scale)(__isl_take PW *pw, isl_int v)
1882 return FN(PW,mul_isl_int)(pw, v);
1885 static int FN(PW,qsort_set_cmp)(const void *p1, const void *p2)
1887 isl_set *set1 = *(isl_set * const *)p1;
1888 isl_set *set2 = *(isl_set * const *)p2;
1890 return isl_set_plain_cmp(set1, set2);
1893 /* We normalize in place, but if anything goes wrong we need
1894 * to return NULL, so we need to make sure we don't change the
1895 * meaning of any possible other copies of map.
1897 __isl_give PW *FN(PW,normalize)(__isl_take PW *pw)
1899 int i, j;
1900 isl_set *set;
1902 if (!pw)
1903 return NULL;
1904 for (i = 0; i < pw->n; ++i) {
1905 set = isl_set_normalize(isl_set_copy(pw->p[i].set));
1906 if (!set)
1907 return FN(PW,free)(pw);
1908 isl_set_free(pw->p[i].set);
1909 pw->p[i].set = set;
1911 qsort(pw->p, pw->n, sizeof(pw->p[0]), &FN(PW,qsort_set_cmp));
1912 for (i = pw->n - 1; i >= 1; --i) {
1913 if (!isl_set_plain_is_equal(pw->p[i - 1].set, pw->p[i].set))
1914 continue;
1915 if (!FN(EL,plain_is_equal)(pw->p[i - 1].FIELD, pw->p[i].FIELD))
1916 continue;
1917 set = isl_set_union(isl_set_copy(pw->p[i - 1].set),
1918 isl_set_copy(pw->p[i].set));
1919 if (!set)
1920 return FN(PW,free)(pw);
1921 isl_set_free(pw->p[i].set);
1922 FN(EL,free)(pw->p[i].FIELD);
1923 isl_set_free(pw->p[i - 1].set);
1924 pw->p[i - 1].set = set;
1925 for (j = i + 1; j < pw->n; ++j)
1926 pw->p[j - 1] = pw->p[j];
1927 pw->n--;
1930 return pw;
1933 /* Is pw1 obviously equal to pw2?
1934 * That is, do they have obviously identical cells and obviously identical
1935 * elements on each cell?
1937 int FN(PW,plain_is_equal)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1939 int i;
1940 int equal;
1942 if (!pw1 || !pw2)
1943 return -1;
1945 if (pw1 == pw2)
1946 return 1;
1947 if (!isl_space_is_equal(pw1->dim, pw2->dim))
1948 return 0;
1950 pw1 = FN(PW,copy)(pw1);
1951 pw2 = FN(PW,copy)(pw2);
1952 pw1 = FN(PW,normalize)(pw1);
1953 pw2 = FN(PW,normalize)(pw2);
1954 if (!pw1 || !pw2)
1955 goto error;
1957 equal = pw1->n == pw2->n;
1958 for (i = 0; equal && i < pw1->n; ++i) {
1959 equal = isl_set_plain_is_equal(pw1->p[i].set, pw2->p[i].set);
1960 if (equal < 0)
1961 goto error;
1962 if (!equal)
1963 break;
1964 equal = FN(EL,plain_is_equal)(pw1->p[i].FIELD, pw2->p[i].FIELD);
1965 if (equal < 0)
1966 goto error;
1969 FN(PW,free)(pw1);
1970 FN(PW,free)(pw2);
1971 return equal;
1972 error:
1973 FN(PW,free)(pw1);
1974 FN(PW,free)(pw2);
1975 return -1;
1978 #ifndef NO_PULLBACK
1979 static __isl_give PW *FN(PW,align_params_pw_multi_aff_and)(__isl_take PW *pw,
1980 __isl_take isl_multi_aff *ma,
1981 __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_multi_aff *ma))
1983 isl_ctx *ctx;
1984 isl_space *ma_space;
1986 ma_space = isl_multi_aff_get_space(ma);
1987 if (!pw || !ma || !ma_space)
1988 goto error;
1989 if (isl_space_match(pw->dim, isl_dim_param, ma_space, isl_dim_param)) {
1990 isl_space_free(ma_space);
1991 return fn(pw, ma);
1993 ctx = FN(PW,get_ctx)(pw);
1994 if (!isl_space_has_named_params(pw->dim) ||
1995 !isl_space_has_named_params(ma_space))
1996 isl_die(ctx, isl_error_invalid,
1997 "unaligned unnamed parameters", goto error);
1998 pw = FN(PW,align_params)(pw, ma_space);
1999 ma = isl_multi_aff_align_params(ma, FN(PW,get_space)(pw));
2000 return fn(pw, ma);
2001 error:
2002 isl_space_free(ma_space);
2003 FN(PW,free)(pw);
2004 isl_multi_aff_free(ma);
2005 return NULL;
2008 static __isl_give PW *FN(PW,align_params_pw_pw_multi_aff_and)(__isl_take PW *pw,
2009 __isl_take isl_pw_multi_aff *pma,
2010 __isl_give PW *(*fn)(__isl_take PW *pw,
2011 __isl_take isl_pw_multi_aff *ma))
2013 isl_ctx *ctx;
2014 isl_space *pma_space;
2016 pma_space = isl_pw_multi_aff_get_space(pma);
2017 if (!pw || !pma || !pma_space)
2018 goto error;
2019 if (isl_space_match(pw->dim, isl_dim_param, pma_space, isl_dim_param)) {
2020 isl_space_free(pma_space);
2021 return fn(pw, pma);
2023 ctx = FN(PW,get_ctx)(pw);
2024 if (!isl_space_has_named_params(pw->dim) ||
2025 !isl_space_has_named_params(pma_space))
2026 isl_die(ctx, isl_error_invalid,
2027 "unaligned unnamed parameters", goto error);
2028 pw = FN(PW,align_params)(pw, pma_space);
2029 pma = isl_pw_multi_aff_align_params(pma, FN(PW,get_space)(pw));
2030 return fn(pw, pma);
2031 error:
2032 isl_space_free(pma_space);
2033 FN(PW,free)(pw);
2034 isl_pw_multi_aff_free(pma);
2035 return NULL;
2038 /* Compute the pullback of "pw" by the function represented by "ma".
2039 * In other words, plug in "ma" in "pw".
2041 static __isl_give PW *FN(PW,pullback_multi_aff_aligned)(__isl_take PW *pw,
2042 __isl_take isl_multi_aff *ma)
2044 int i;
2045 isl_space *space = NULL;
2047 ma = isl_multi_aff_align_divs(ma);
2048 pw = FN(PW,cow)(pw);
2049 if (!pw || !ma)
2050 goto error;
2052 space = isl_space_join(isl_multi_aff_get_space(ma),
2053 FN(PW,get_space)(pw));
2055 for (i = 0; i < pw->n; ++i) {
2056 pw->p[i].set = isl_set_preimage_multi_aff(pw->p[i].set,
2057 isl_multi_aff_copy(ma));
2058 if (!pw->p[i].set)
2059 goto error;
2060 pw->p[i].FIELD = FN(EL,pullback_multi_aff)(pw->p[i].FIELD,
2061 isl_multi_aff_copy(ma));
2062 if (!pw->p[i].FIELD)
2063 goto error;
2066 pw = FN(PW,reset_space)(pw, space);
2067 isl_multi_aff_free(ma);
2068 return pw;
2069 error:
2070 isl_space_free(space);
2071 isl_multi_aff_free(ma);
2072 FN(PW,free)(pw);
2073 return NULL;
2076 __isl_give PW *FN(PW,pullback_multi_aff)(__isl_take PW *pw,
2077 __isl_take isl_multi_aff *ma)
2079 return FN(PW,align_params_pw_multi_aff_and)(pw, ma,
2080 &FN(PW,pullback_multi_aff_aligned));
2083 /* Compute the pullback of "pw" by the function represented by "pma".
2084 * In other words, plug in "pma" in "pw".
2086 static __isl_give PW *FN(PW,pullback_pw_multi_aff_aligned)(__isl_take PW *pw,
2087 __isl_take isl_pw_multi_aff *pma)
2089 int i;
2090 PW *res;
2092 if (!pma)
2093 goto error;
2095 if (pma->n == 0) {
2096 isl_space *space;
2097 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
2098 FN(PW,get_space)(pw));
2099 isl_pw_multi_aff_free(pma);
2100 res = FN(PW,empty)(space);
2101 FN(PW,free)(pw);
2102 return res;
2105 res = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
2106 isl_multi_aff_copy(pma->p[0].maff));
2107 res = FN(PW,intersect_domain)(res, isl_set_copy(pma->p[0].set));
2109 for (i = 1; i < pma->n; ++i) {
2110 PW *res_i;
2112 res_i = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
2113 isl_multi_aff_copy(pma->p[i].maff));
2114 res_i = FN(PW,intersect_domain)(res_i,
2115 isl_set_copy(pma->p[i].set));
2116 res = FN(PW,add_disjoint)(res, res_i);
2119 isl_pw_multi_aff_free(pma);
2120 FN(PW,free)(pw);
2121 return res;
2122 error:
2123 isl_pw_multi_aff_free(pma);
2124 FN(PW,free)(pw);
2125 return NULL;
2128 __isl_give PW *FN(PW,pullback_pw_multi_aff)(__isl_take PW *pw,
2129 __isl_take isl_pw_multi_aff *pma)
2131 return FN(PW,align_params_pw_pw_multi_aff_and)(pw, pma,
2132 &FN(PW,pullback_pw_multi_aff_aligned));
2134 #endif