drop ISL_DIM_H defines
[isl.git] / isl_pw_templ.c
blob4d3580742d6e3f8a533c3244bbf9fde75db9fbbd
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_sort.h>
16 #include <isl_val_private.h>
18 #include <isl_pw_macro.h>
20 #ifdef HAS_TYPE
21 __isl_give PW *FN(PW,alloc_size)(__isl_take isl_space *dim,
22 enum isl_fold type, int n)
23 #else
24 __isl_give PW *FN(PW,alloc_size)(__isl_take isl_space *dim, int n)
25 #endif
27 isl_ctx *ctx;
28 struct PW *pw;
30 if (!dim)
31 return NULL;
32 ctx = isl_space_get_ctx(dim);
33 isl_assert(ctx, n >= 0, goto error);
34 pw = isl_alloc(ctx, struct PW,
35 sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
36 if (!pw)
37 goto error;
39 pw->ref = 1;
40 #ifdef HAS_TYPE
41 pw->type = type;
42 #endif
43 pw->size = n;
44 pw->n = 0;
45 pw->dim = dim;
46 return pw;
47 error:
48 isl_space_free(dim);
49 return NULL;
52 #ifdef HAS_TYPE
53 __isl_give PW *FN(PW,ZERO)(__isl_take isl_space *dim, enum isl_fold type)
55 return FN(PW,alloc_size)(dim, type, 0);
57 #else
58 __isl_give PW *FN(PW,ZERO)(__isl_take isl_space *dim)
60 return FN(PW,alloc_size)(dim, 0);
62 #endif
64 __isl_give PW *FN(PW,add_piece)(__isl_take PW *pw,
65 __isl_take isl_set *set, __isl_take EL *el)
67 isl_ctx *ctx;
68 isl_space *el_dim = NULL;
70 if (!pw || !set || !el)
71 goto error;
73 if (isl_set_plain_is_empty(set) || FN(EL,EL_IS_ZERO)(el)) {
74 isl_set_free(set);
75 FN(EL,free)(el);
76 return pw;
79 ctx = isl_set_get_ctx(set);
80 #ifdef HAS_TYPE
81 if (pw->type != el->type)
82 isl_die(ctx, isl_error_invalid, "fold types don't match",
83 goto error);
84 #endif
85 el_dim = FN(EL,get_space(el));
86 isl_assert(ctx, isl_space_is_equal(pw->dim, el_dim), goto error);
87 isl_assert(ctx, pw->n < pw->size, goto error);
89 pw->p[pw->n].set = set;
90 pw->p[pw->n].FIELD = el;
91 pw->n++;
93 isl_space_free(el_dim);
94 return pw;
95 error:
96 isl_space_free(el_dim);
97 FN(PW,free)(pw);
98 isl_set_free(set);
99 FN(EL,free)(el);
100 return NULL;
103 /* Does the space of "set" correspond to that of the domain of "el".
105 static isl_bool FN(PW,compatible_domain)(__isl_keep EL *el,
106 __isl_keep isl_set *set)
108 isl_bool ok;
109 isl_space *el_space, *set_space;
111 if (!set || !el)
112 return isl_bool_error;
113 set_space = isl_set_get_space(set);
114 el_space = FN(EL,get_space)(el);
115 ok = isl_space_is_domain_internal(set_space, el_space);
116 isl_space_free(el_space);
117 isl_space_free(set_space);
118 return ok;
121 /* Check that the space of "set" corresponds to that of the domain of "el".
123 static isl_stat FN(PW,check_compatible_domain)(__isl_keep EL *el,
124 __isl_keep isl_set *set)
126 isl_bool ok;
128 ok = FN(PW,compatible_domain)(el, set);
129 if (ok < 0)
130 return isl_stat_error;
131 if (!ok)
132 isl_die(isl_set_get_ctx(set), isl_error_invalid,
133 "incompatible spaces", return isl_stat_error);
135 return isl_stat_ok;
138 #ifdef HAS_TYPE
139 __isl_give PW *FN(PW,alloc)(enum isl_fold type,
140 __isl_take isl_set *set, __isl_take EL *el)
141 #else
142 __isl_give PW *FN(PW,alloc)(__isl_take isl_set *set, __isl_take EL *el)
143 #endif
145 PW *pw;
147 if (FN(PW,check_compatible_domain)(el, set) < 0)
148 goto error;
150 #ifdef HAS_TYPE
151 pw = FN(PW,alloc_size)(FN(EL,get_space)(el), type, 1);
152 #else
153 pw = FN(PW,alloc_size)(FN(EL,get_space)(el), 1);
154 #endif
156 return FN(PW,add_piece)(pw, set, el);
157 error:
158 isl_set_free(set);
159 FN(EL,free)(el);
160 return NULL;
163 __isl_give PW *FN(PW,dup)(__isl_keep PW *pw)
165 int i;
166 PW *dup;
168 if (!pw)
169 return NULL;
171 #ifdef HAS_TYPE
172 dup = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->type, pw->n);
173 #else
174 dup = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->n);
175 #endif
176 if (!dup)
177 return NULL;
179 for (i = 0; i < pw->n; ++i)
180 dup = FN(PW,add_piece)(dup, isl_set_copy(pw->p[i].set),
181 FN(EL,copy)(pw->p[i].FIELD));
183 return dup;
186 __isl_give PW *FN(PW,cow)(__isl_take PW *pw)
188 if (!pw)
189 return NULL;
191 if (pw->ref == 1)
192 return pw;
193 pw->ref--;
194 return FN(PW,dup)(pw);
197 __isl_give PW *FN(PW,copy)(__isl_keep PW *pw)
199 if (!pw)
200 return NULL;
202 pw->ref++;
203 return pw;
206 __isl_null PW *FN(PW,free)(__isl_take PW *pw)
208 int i;
210 if (!pw)
211 return NULL;
212 if (--pw->ref > 0)
213 return NULL;
215 for (i = 0; i < pw->n; ++i) {
216 isl_set_free(pw->p[i].set);
217 FN(EL,free)(pw->p[i].FIELD);
219 isl_space_free(pw->dim);
220 free(pw);
222 return NULL;
225 const char *FN(PW,get_dim_name)(__isl_keep PW *pw, enum isl_dim_type type,
226 unsigned pos)
228 return pw ? isl_space_get_dim_name(pw->dim, type, pos) : NULL;
231 isl_bool FN(PW,has_dim_id)(__isl_keep PW *pw, enum isl_dim_type type,
232 unsigned pos)
234 return pw ? isl_space_has_dim_id(pw->dim, type, pos) : isl_bool_error;
237 __isl_give isl_id *FN(PW,get_dim_id)(__isl_keep PW *pw, enum isl_dim_type type,
238 unsigned pos)
240 return pw ? isl_space_get_dim_id(pw->dim, type, pos) : NULL;
243 isl_bool FN(PW,has_tuple_name)(__isl_keep PW *pw, enum isl_dim_type type)
245 return pw ? isl_space_has_tuple_name(pw->dim, type) : isl_bool_error;
248 const char *FN(PW,get_tuple_name)(__isl_keep PW *pw, enum isl_dim_type type)
250 return pw ? isl_space_get_tuple_name(pw->dim, type) : NULL;
253 isl_bool FN(PW,has_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
255 return pw ? isl_space_has_tuple_id(pw->dim, type) : isl_bool_error;
258 __isl_give isl_id *FN(PW,get_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
260 return pw ? isl_space_get_tuple_id(pw->dim, type) : NULL;
263 isl_bool FN(PW,IS_ZERO)(__isl_keep PW *pw)
265 if (!pw)
266 return isl_bool_error;
268 return pw->n == 0;
271 #ifndef NO_REALIGN
272 __isl_give PW *FN(PW,realign_domain)(__isl_take PW *pw,
273 __isl_take isl_reordering *exp)
275 int i;
277 pw = FN(PW,cow)(pw);
278 if (!pw || !exp)
279 goto error;
281 for (i = 0; i < pw->n; ++i) {
282 pw->p[i].set = isl_set_realign(pw->p[i].set,
283 isl_reordering_copy(exp));
284 if (!pw->p[i].set)
285 goto error;
286 pw->p[i].FIELD = FN(EL,realign_domain)(pw->p[i].FIELD,
287 isl_reordering_copy(exp));
288 if (!pw->p[i].FIELD)
289 goto error;
292 pw = FN(PW,reset_domain_space)(pw, isl_space_copy(exp->dim));
294 isl_reordering_free(exp);
295 return pw;
296 error:
297 isl_reordering_free(exp);
298 FN(PW,free)(pw);
299 return NULL;
302 /* Align the parameters of "pw" to those of "model".
304 __isl_give PW *FN(PW,align_params)(__isl_take PW *pw, __isl_take isl_space *model)
306 isl_ctx *ctx;
307 isl_bool equal_params;
309 if (!pw || !model)
310 goto error;
312 ctx = isl_space_get_ctx(model);
313 if (!isl_space_has_named_params(model))
314 isl_die(ctx, isl_error_invalid,
315 "model has unnamed parameters", goto error);
316 if (!isl_space_has_named_params(pw->dim))
317 isl_die(ctx, isl_error_invalid,
318 "input has unnamed parameters", goto error);
319 equal_params = isl_space_has_equal_params(pw->dim, model);
320 if (equal_params < 0)
321 goto error;
322 if (!equal_params) {
323 isl_reordering *exp;
325 model = isl_space_drop_dims(model, isl_dim_in,
326 0, isl_space_dim(model, isl_dim_in));
327 model = isl_space_drop_dims(model, isl_dim_out,
328 0, isl_space_dim(model, isl_dim_out));
329 exp = isl_parameter_alignment_reordering(pw->dim, model);
330 exp = isl_reordering_extend_space(exp,
331 FN(PW,get_domain_space)(pw));
332 pw = FN(PW,realign_domain)(pw, exp);
335 isl_space_free(model);
336 return pw;
337 error:
338 isl_space_free(model);
339 FN(PW,free)(pw);
340 return NULL;
343 static __isl_give PW *FN(PW,align_params_pw_pw_and)(__isl_take PW *pw1,
344 __isl_take PW *pw2,
345 __isl_give PW *(*fn)(__isl_take PW *pw1, __isl_take PW *pw2))
347 isl_ctx *ctx;
348 isl_bool equal_params;
350 if (!pw1 || !pw2)
351 goto error;
352 equal_params = isl_space_has_equal_params(pw1->dim, pw2->dim);
353 if (equal_params < 0)
354 goto error;
355 if (equal_params)
356 return fn(pw1, pw2);
357 ctx = FN(PW,get_ctx)(pw1);
358 if (!isl_space_has_named_params(pw1->dim) ||
359 !isl_space_has_named_params(pw2->dim))
360 isl_die(ctx, isl_error_invalid,
361 "unaligned unnamed parameters", goto error);
362 pw1 = FN(PW,align_params)(pw1, FN(PW,get_space)(pw2));
363 pw2 = FN(PW,align_params)(pw2, FN(PW,get_space)(pw1));
364 return fn(pw1, pw2);
365 error:
366 FN(PW,free)(pw1);
367 FN(PW,free)(pw2);
368 return NULL;
371 static __isl_give PW *FN(PW,align_params_pw_set_and)(__isl_take PW *pw,
372 __isl_take isl_set *set,
373 __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_set *set))
375 isl_ctx *ctx;
376 isl_bool aligned;
378 if (!pw || !set)
379 goto error;
380 aligned = isl_set_space_has_equal_params(set, pw->dim);
381 if (aligned < 0)
382 goto error;
383 if (aligned)
384 return fn(pw, set);
385 ctx = FN(PW,get_ctx)(pw);
386 if (!isl_space_has_named_params(pw->dim) ||
387 !isl_space_has_named_params(set->dim))
388 isl_die(ctx, isl_error_invalid,
389 "unaligned unnamed parameters", goto error);
390 pw = FN(PW,align_params)(pw, isl_set_get_space(set));
391 set = isl_set_align_params(set, FN(PW,get_space)(pw));
392 return fn(pw, set);
393 error:
394 FN(PW,free)(pw);
395 isl_set_free(set);
396 return NULL;
398 #endif
400 static __isl_give PW *FN(PW,union_add_aligned)(__isl_take PW *pw1,
401 __isl_take PW *pw2)
403 int i, j, n;
404 struct PW *res;
405 isl_ctx *ctx;
406 isl_set *set;
408 if (!pw1 || !pw2)
409 goto error;
411 ctx = isl_space_get_ctx(pw1->dim);
412 #ifdef HAS_TYPE
413 if (pw1->type != pw2->type)
414 isl_die(ctx, isl_error_invalid,
415 "fold types don't match", goto error);
416 #endif
417 isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
419 if (FN(PW,IS_ZERO)(pw1)) {
420 FN(PW,free)(pw1);
421 return pw2;
424 if (FN(PW,IS_ZERO)(pw2)) {
425 FN(PW,free)(pw2);
426 return pw1;
429 n = (pw1->n + 1) * (pw2->n + 1);
430 #ifdef HAS_TYPE
431 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), pw1->type, n);
432 #else
433 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), n);
434 #endif
436 for (i = 0; i < pw1->n; ++i) {
437 set = isl_set_copy(pw1->p[i].set);
438 for (j = 0; j < pw2->n; ++j) {
439 struct isl_set *common;
440 EL *sum;
441 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
442 isl_set_copy(pw2->p[j].set));
443 if (isl_set_plain_is_empty(common)) {
444 isl_set_free(common);
445 continue;
447 set = isl_set_subtract(set,
448 isl_set_copy(pw2->p[j].set));
450 sum = FN(EL,add_on_domain)(common,
451 FN(EL,copy)(pw1->p[i].FIELD),
452 FN(EL,copy)(pw2->p[j].FIELD));
454 res = FN(PW,add_piece)(res, common, sum);
456 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw1->p[i].FIELD));
459 for (j = 0; j < pw2->n; ++j) {
460 set = isl_set_copy(pw2->p[j].set);
461 for (i = 0; i < pw1->n; ++i)
462 set = isl_set_subtract(set,
463 isl_set_copy(pw1->p[i].set));
464 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw2->p[j].FIELD));
467 FN(PW,free)(pw1);
468 FN(PW,free)(pw2);
470 return res;
471 error:
472 FN(PW,free)(pw1);
473 FN(PW,free)(pw2);
474 return NULL;
477 /* Private version of "union_add". For isl_pw_qpolynomial and
478 * isl_pw_qpolynomial_fold, we prefer to simply call it "add".
480 static __isl_give PW *FN(PW,union_add_)(__isl_take PW *pw1, __isl_take PW *pw2)
482 return FN(PW,align_params_pw_pw_and)(pw1, pw2,
483 &FN(PW,union_add_aligned));
486 /* Make sure "pw" has room for at least "n" more pieces.
488 * If there is only one reference to pw, we extend it in place.
489 * Otherwise, we create a new PW and copy the pieces.
491 static __isl_give PW *FN(PW,grow)(__isl_take PW *pw, int n)
493 int i;
494 isl_ctx *ctx;
495 PW *res;
497 if (!pw)
498 return NULL;
499 if (pw->n + n <= pw->size)
500 return pw;
501 ctx = FN(PW,get_ctx)(pw);
502 n += pw->n;
503 if (pw->ref == 1) {
504 res = isl_realloc(ctx, pw, struct PW,
505 sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
506 if (!res)
507 return FN(PW,free)(pw);
508 res->size = n;
509 return res;
511 #ifdef HAS_TYPE
512 res = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->type, n);
513 #else
514 res = FN(PW,alloc_size)(isl_space_copy(pw->dim), n);
515 #endif
516 if (!res)
517 return FN(PW,free)(pw);
518 for (i = 0; i < pw->n; ++i)
519 res = FN(PW,add_piece)(res, isl_set_copy(pw->p[i].set),
520 FN(EL,copy)(pw->p[i].FIELD));
521 FN(PW,free)(pw);
522 return res;
525 static __isl_give PW *FN(PW,add_disjoint_aligned)(__isl_take PW *pw1,
526 __isl_take PW *pw2)
528 int i;
529 isl_ctx *ctx;
531 if (!pw1 || !pw2)
532 goto error;
534 if (pw1->size < pw1->n + pw2->n && pw1->n < pw2->n)
535 return FN(PW,add_disjoint_aligned)(pw2, pw1);
537 ctx = isl_space_get_ctx(pw1->dim);
538 #ifdef HAS_TYPE
539 if (pw1->type != pw2->type)
540 isl_die(ctx, isl_error_invalid,
541 "fold types don't match", goto error);
542 #endif
543 isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
545 if (FN(PW,IS_ZERO)(pw1)) {
546 FN(PW,free)(pw1);
547 return pw2;
550 if (FN(PW,IS_ZERO)(pw2)) {
551 FN(PW,free)(pw2);
552 return pw1;
555 pw1 = FN(PW,grow)(pw1, pw2->n);
556 if (!pw1)
557 goto error;
559 for (i = 0; i < pw2->n; ++i)
560 pw1 = FN(PW,add_piece)(pw1,
561 isl_set_copy(pw2->p[i].set),
562 FN(EL,copy)(pw2->p[i].FIELD));
564 FN(PW,free)(pw2);
566 return pw1;
567 error:
568 FN(PW,free)(pw1);
569 FN(PW,free)(pw2);
570 return NULL;
573 __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
575 return FN(PW,align_params_pw_pw_and)(pw1, pw2,
576 &FN(PW,add_disjoint_aligned));
579 /* This function is currently only used from isl_aff.c
581 static __isl_give PW *FN(PW,on_shared_domain_in)(__isl_take PW *pw1,
582 __isl_take PW *pw2, __isl_take isl_space *space,
583 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
584 __attribute__ ((unused));
586 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
587 * The result of "fn" (and therefore also of this function) lives in "space".
589 static __isl_give PW *FN(PW,on_shared_domain_in)(__isl_take PW *pw1,
590 __isl_take PW *pw2, __isl_take isl_space *space,
591 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
593 int i, j, n;
594 PW *res = NULL;
596 if (!pw1 || !pw2)
597 goto error;
599 n = pw1->n * pw2->n;
600 #ifdef HAS_TYPE
601 res = FN(PW,alloc_size)(isl_space_copy(space), pw1->type, n);
602 #else
603 res = FN(PW,alloc_size)(isl_space_copy(space), n);
604 #endif
606 for (i = 0; i < pw1->n; ++i) {
607 for (j = 0; j < pw2->n; ++j) {
608 isl_set *common;
609 EL *res_ij;
610 int empty;
612 common = isl_set_intersect(
613 isl_set_copy(pw1->p[i].set),
614 isl_set_copy(pw2->p[j].set));
615 empty = isl_set_plain_is_empty(common);
616 if (empty < 0 || empty) {
617 isl_set_free(common);
618 if (empty < 0)
619 goto error;
620 continue;
623 res_ij = fn(FN(EL,copy)(pw1->p[i].FIELD),
624 FN(EL,copy)(pw2->p[j].FIELD));
625 res_ij = FN(EL,gist)(res_ij, isl_set_copy(common));
627 res = FN(PW,add_piece)(res, common, res_ij);
631 isl_space_free(space);
632 FN(PW,free)(pw1);
633 FN(PW,free)(pw2);
634 return res;
635 error:
636 isl_space_free(space);
637 FN(PW,free)(pw1);
638 FN(PW,free)(pw2);
639 FN(PW,free)(res);
640 return NULL;
643 /* This function is currently only used from isl_aff.c
645 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
646 __isl_take PW *pw2,
647 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
648 __attribute__ ((unused));
650 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
651 * The result of "fn" is assumed to live in the same space as "pw1" and "pw2".
653 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
654 __isl_take PW *pw2,
655 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
657 isl_space *space;
659 if (!pw1 || !pw2)
660 goto error;
662 space = isl_space_copy(pw1->dim);
663 return FN(PW,on_shared_domain_in)(pw1, pw2, space, fn);
664 error:
665 FN(PW,free)(pw1);
666 FN(PW,free)(pw2);
667 return NULL;
670 #ifndef NO_NEG
671 __isl_give PW *FN(PW,neg)(__isl_take PW *pw)
673 int i;
675 if (!pw)
676 return NULL;
678 if (FN(PW,IS_ZERO)(pw))
679 return pw;
681 pw = FN(PW,cow)(pw);
682 if (!pw)
683 return NULL;
685 for (i = 0; i < pw->n; ++i) {
686 pw->p[i].FIELD = FN(EL,neg)(pw->p[i].FIELD);
687 if (!pw->p[i].FIELD)
688 return FN(PW,free)(pw);
691 return pw;
693 #endif
695 #ifndef NO_SUB
696 __isl_give PW *FN(PW,sub)(__isl_take PW *pw1, __isl_take PW *pw2)
698 return FN(PW,add)(pw1, FN(PW,neg)(pw2));
700 #endif
702 /* Return the parameter domain of "pw".
704 __isl_give isl_set *FN(PW,params)(__isl_take PW *pw)
706 return isl_set_params(FN(PW,domain)(pw));
709 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
711 int i;
712 isl_set *dom;
714 if (!pw)
715 return NULL;
717 dom = isl_set_empty(FN(PW,get_domain_space)(pw));
718 for (i = 0; i < pw->n; ++i)
719 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
721 FN(PW,free)(pw);
723 return dom;
726 /* Exploit the equalities in the domain of piece "i" of "pw"
727 * to simplify the associated function.
728 * If the domain of piece "i" is empty, then remove it entirely,
729 * replacing it with the final piece.
731 static int FN(PW,exploit_equalities_and_remove_if_empty)(__isl_keep PW *pw,
732 int i)
734 isl_basic_set *aff;
735 int empty = isl_set_plain_is_empty(pw->p[i].set);
737 if (empty < 0)
738 return -1;
739 if (empty) {
740 isl_set_free(pw->p[i].set);
741 FN(EL,free)(pw->p[i].FIELD);
742 if (i != pw->n - 1)
743 pw->p[i] = pw->p[pw->n - 1];
744 pw->n--;
746 return 0;
749 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
750 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD, aff);
751 if (!pw->p[i].FIELD)
752 return -1;
754 return 0;
757 /* Convert a piecewise expression defined over a parameter domain
758 * into one that is defined over a zero-dimensional set.
760 __isl_give PW *FN(PW,from_range)(__isl_take PW *pw)
762 isl_space *space;
764 if (!pw)
765 return NULL;
766 if (!isl_space_is_set(pw->dim))
767 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
768 "not living in a set space", return FN(PW,free)(pw));
770 space = FN(PW,get_space)(pw);
771 space = isl_space_from_range(space);
772 pw = FN(PW,reset_space)(pw, space);
774 return pw;
777 /* Fix the value of the given parameter or domain dimension of "pw"
778 * to be equal to "value".
780 __isl_give PW *FN(PW,fix_si)(__isl_take PW *pw, enum isl_dim_type type,
781 unsigned pos, int value)
783 int i;
785 if (!pw)
786 return NULL;
788 if (type == isl_dim_out)
789 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
790 "cannot fix output dimension", return FN(PW,free)(pw));
792 if (pw->n == 0)
793 return pw;
795 if (type == isl_dim_in)
796 type = isl_dim_set;
798 pw = FN(PW,cow)(pw);
799 if (!pw)
800 return FN(PW,free)(pw);
802 for (i = pw->n - 1; i >= 0; --i) {
803 pw->p[i].set = isl_set_fix_si(pw->p[i].set, type, pos, value);
804 if (FN(PW,exploit_equalities_and_remove_if_empty)(pw, i) < 0)
805 return FN(PW,free)(pw);
808 return pw;
811 /* Restrict the domain of "pw" by combining each cell
812 * with "set" through a call to "fn", where "fn" may be
813 * isl_set_intersect, isl_set_intersect_params or isl_set_subtract.
815 static __isl_give PW *FN(PW,restrict_domain_aligned)(__isl_take PW *pw,
816 __isl_take isl_set *set,
817 __isl_give isl_set *(*fn)(__isl_take isl_set *set1,
818 __isl_take isl_set *set2))
820 int i;
822 if (!pw || !set)
823 goto error;
825 if (pw->n == 0) {
826 isl_set_free(set);
827 return pw;
830 pw = FN(PW,cow)(pw);
831 if (!pw)
832 goto error;
834 for (i = pw->n - 1; i >= 0; --i) {
835 pw->p[i].set = fn(pw->p[i].set, isl_set_copy(set));
836 if (FN(PW,exploit_equalities_and_remove_if_empty)(pw, i) < 0)
837 goto error;
840 isl_set_free(set);
841 return pw;
842 error:
843 isl_set_free(set);
844 FN(PW,free)(pw);
845 return NULL;
848 static __isl_give PW *FN(PW,intersect_domain_aligned)(__isl_take PW *pw,
849 __isl_take isl_set *set)
851 return FN(PW,restrict_domain_aligned)(pw, set, &isl_set_intersect);
854 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw,
855 __isl_take isl_set *context)
857 return FN(PW,align_params_pw_set_and)(pw, context,
858 &FN(PW,intersect_domain_aligned));
861 static __isl_give PW *FN(PW,intersect_params_aligned)(__isl_take PW *pw,
862 __isl_take isl_set *set)
864 return FN(PW,restrict_domain_aligned)(pw, set,
865 &isl_set_intersect_params);
868 /* Intersect the domain of "pw" with the parameter domain "context".
870 __isl_give PW *FN(PW,intersect_params)(__isl_take PW *pw,
871 __isl_take isl_set *context)
873 return FN(PW,align_params_pw_set_and)(pw, context,
874 &FN(PW,intersect_params_aligned));
877 /* Subtract "domain' from the domain of "pw", assuming their
878 * parameters have been aligned.
880 static __isl_give PW *FN(PW,subtract_domain_aligned)(__isl_take PW *pw,
881 __isl_take isl_set *domain)
883 return FN(PW,restrict_domain_aligned)(pw, domain, &isl_set_subtract);
886 /* Subtract "domain' from the domain of "pw".
888 __isl_give PW *FN(PW,subtract_domain)(__isl_take PW *pw,
889 __isl_take isl_set *domain)
891 return FN(PW,align_params_pw_set_and)(pw, domain,
892 &FN(PW,subtract_domain_aligned));
895 /* Compute the gist of "pw" with respect to the domain constraints
896 * of "context" for the case where the domain of the last element
897 * of "pw" is equal to "context".
898 * Call "fn_el" to compute the gist of this element, replace
899 * its domain by the universe and drop all other elements
900 * as their domains are necessarily disjoint from "context".
902 static __isl_give PW *FN(PW,gist_last)(__isl_take PW *pw,
903 __isl_take isl_set *context,
904 __isl_give EL *(*fn_el)(__isl_take EL *el, __isl_take isl_set *set))
906 int i;
907 isl_space *space;
909 for (i = 0; i < pw->n - 1; ++i) {
910 isl_set_free(pw->p[i].set);
911 FN(EL,free)(pw->p[i].FIELD);
913 pw->p[0].FIELD = pw->p[pw->n - 1].FIELD;
914 pw->p[0].set = pw->p[pw->n - 1].set;
915 pw->n = 1;
917 space = isl_set_get_space(context);
918 pw->p[0].FIELD = fn_el(pw->p[0].FIELD, context);
919 context = isl_set_universe(space);
920 isl_set_free(pw->p[0].set);
921 pw->p[0].set = context;
923 if (!pw->p[0].FIELD || !pw->p[0].set)
924 return FN(PW,free)(pw);
926 return pw;
929 /* Compute the gist of "pw" with respect to the domain constraints
930 * of "context". Call "fn_el" to compute the gist of the elements
931 * and "fn_dom" to compute the gist of the domains.
933 * If the piecewise expression is empty or the context is the universe,
934 * then nothing can be simplified.
936 static __isl_give PW *FN(PW,gist_aligned)(__isl_take PW *pw,
937 __isl_take isl_set *context,
938 __isl_give EL *(*fn_el)(__isl_take EL *el,
939 __isl_take isl_set *set),
940 __isl_give isl_set *(*fn_dom)(__isl_take isl_set *set,
941 __isl_take isl_basic_set *bset))
943 int i;
944 int is_universe;
945 isl_bool aligned;
946 isl_basic_set *hull = NULL;
948 if (!pw || !context)
949 goto error;
951 if (pw->n == 0) {
952 isl_set_free(context);
953 return pw;
956 is_universe = isl_set_plain_is_universe(context);
957 if (is_universe < 0)
958 goto error;
959 if (is_universe) {
960 isl_set_free(context);
961 return pw;
964 aligned = isl_set_space_has_equal_params(context, pw->dim);
965 if (aligned < 0)
966 goto error;
967 if (!aligned) {
968 pw = FN(PW,align_params)(pw, isl_set_get_space(context));
969 context = isl_set_align_params(context, FN(PW,get_space)(pw));
972 pw = FN(PW,cow)(pw);
973 if (!pw)
974 goto error;
976 if (pw->n == 1) {
977 int equal;
979 equal = isl_set_plain_is_equal(pw->p[0].set, context);
980 if (equal < 0)
981 goto error;
982 if (equal)
983 return FN(PW,gist_last)(pw, context, fn_el);
986 context = isl_set_compute_divs(context);
987 hull = isl_set_simple_hull(isl_set_copy(context));
989 for (i = pw->n - 1; i >= 0; --i) {
990 isl_set *set_i;
991 int empty;
993 if (i == pw->n - 1) {
994 int equal;
995 equal = isl_set_plain_is_equal(pw->p[i].set, context);
996 if (equal < 0)
997 goto error;
998 if (equal) {
999 isl_basic_set_free(hull);
1000 return FN(PW,gist_last)(pw, context, fn_el);
1003 set_i = isl_set_intersect(isl_set_copy(pw->p[i].set),
1004 isl_set_copy(context));
1005 empty = isl_set_plain_is_empty(set_i);
1006 pw->p[i].FIELD = fn_el(pw->p[i].FIELD, set_i);
1007 pw->p[i].set = fn_dom(pw->p[i].set, isl_basic_set_copy(hull));
1008 if (empty < 0 || !pw->p[i].FIELD || !pw->p[i].set)
1009 goto error;
1010 if (empty) {
1011 isl_set_free(pw->p[i].set);
1012 FN(EL,free)(pw->p[i].FIELD);
1013 if (i != pw->n - 1)
1014 pw->p[i] = pw->p[pw->n - 1];
1015 pw->n--;
1019 isl_basic_set_free(hull);
1020 isl_set_free(context);
1022 return pw;
1023 error:
1024 FN(PW,free)(pw);
1025 isl_basic_set_free(hull);
1026 isl_set_free(context);
1027 return NULL;
1030 static __isl_give PW *FN(PW,gist_domain_aligned)(__isl_take PW *pw,
1031 __isl_take isl_set *set)
1033 return FN(PW,gist_aligned)(pw, set, &FN(EL,gist),
1034 &isl_set_gist_basic_set);
1037 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
1039 return FN(PW,align_params_pw_set_and)(pw, context,
1040 &FN(PW,gist_domain_aligned));
1043 static __isl_give PW *FN(PW,gist_params_aligned)(__isl_take PW *pw,
1044 __isl_take isl_set *set)
1046 return FN(PW,gist_aligned)(pw, set, &FN(EL,gist_params),
1047 &isl_set_gist_params_basic_set);
1050 __isl_give PW *FN(PW,gist_params)(__isl_take PW *pw,
1051 __isl_take isl_set *context)
1053 return FN(PW,align_params_pw_set_and)(pw, context,
1054 &FN(PW,gist_params_aligned));
1057 /* Return -1 if the piece "p1" should be sorted before "p2"
1058 * and 1 if it should be sorted after "p2".
1059 * Return 0 if they do not need to be sorted in a specific order.
1061 * The two pieces are compared on the basis of their function value expressions.
1063 static int FN(PW,sort_field_cmp)(const void *p1, const void *p2, void *arg)
1065 struct FN(PW,piece) const *pc1 = p1;
1066 struct FN(PW,piece) const *pc2 = p2;
1068 return FN(EL,plain_cmp)(pc1->FIELD, pc2->FIELD);
1071 /* Sort the pieces of "pw" according to their function value
1072 * expressions and then combine pairs of adjacent pieces with
1073 * the same such expression.
1075 * The sorting is performed in place because it does not
1076 * change the meaning of "pw", but care needs to be
1077 * taken not to change any possible other copies of "pw"
1078 * in case anything goes wrong.
1080 __isl_give PW *FN(PW,sort)(__isl_take PW *pw)
1082 int i, j;
1083 isl_set *set;
1085 if (!pw)
1086 return NULL;
1087 if (pw->n <= 1)
1088 return pw;
1089 if (isl_sort(pw->p, pw->n, sizeof(pw->p[0]),
1090 &FN(PW,sort_field_cmp), NULL) < 0)
1091 return FN(PW,free)(pw);
1092 for (i = pw->n - 1; i >= 1; --i) {
1093 if (!FN(EL,plain_is_equal)(pw->p[i - 1].FIELD, pw->p[i].FIELD))
1094 continue;
1095 set = isl_set_union(isl_set_copy(pw->p[i - 1].set),
1096 isl_set_copy(pw->p[i].set));
1097 if (!set)
1098 return FN(PW,free)(pw);
1099 isl_set_free(pw->p[i].set);
1100 FN(EL,free)(pw->p[i].FIELD);
1101 isl_set_free(pw->p[i - 1].set);
1102 pw->p[i - 1].set = set;
1103 for (j = i + 1; j < pw->n; ++j)
1104 pw->p[j - 1] = pw->p[j];
1105 pw->n--;
1108 return pw;
1111 /* Coalesce the domains of "pw".
1113 * Prior to the actual coalescing, first sort the pieces such that
1114 * pieces with the same function value expression are combined
1115 * into a single piece, the combined domain of which can then
1116 * be coalesced.
1118 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
1120 int i;
1122 pw = FN(PW,sort)(pw);
1123 if (!pw)
1124 return NULL;
1126 for (i = 0; i < pw->n; ++i) {
1127 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
1128 if (!pw->p[i].set)
1129 goto error;
1132 return pw;
1133 error:
1134 FN(PW,free)(pw);
1135 return NULL;
1138 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
1140 return pw ? isl_space_get_ctx(pw->dim) : NULL;
1143 #ifndef NO_INVOLVES_DIMS
1144 isl_bool FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
1145 unsigned first, unsigned n)
1147 int i;
1148 enum isl_dim_type set_type;
1150 if (!pw)
1151 return isl_bool_error;
1152 if (pw->n == 0 || n == 0)
1153 return isl_bool_false;
1155 set_type = type == isl_dim_in ? isl_dim_set : type;
1157 for (i = 0; i < pw->n; ++i) {
1158 isl_bool involves = FN(EL,involves_dims)(pw->p[i].FIELD,
1159 type, first, n);
1160 if (involves < 0 || involves)
1161 return involves;
1162 involves = isl_set_involves_dims(pw->p[i].set,
1163 set_type, first, n);
1164 if (involves < 0 || involves)
1165 return involves;
1167 return isl_bool_false;
1169 #endif
1171 __isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
1172 enum isl_dim_type type, unsigned pos, const char *s)
1174 int i;
1175 enum isl_dim_type set_type;
1177 pw = FN(PW,cow)(pw);
1178 if (!pw)
1179 return NULL;
1181 set_type = type == isl_dim_in ? isl_dim_set : type;
1183 pw->dim = isl_space_set_dim_name(pw->dim, type, pos, s);
1184 if (!pw->dim)
1185 goto error;
1187 for (i = 0; i < pw->n; ++i) {
1188 pw->p[i].set = isl_set_set_dim_name(pw->p[i].set,
1189 set_type, pos, s);
1190 if (!pw->p[i].set)
1191 goto error;
1192 pw->p[i].FIELD = FN(EL,set_dim_name)(pw->p[i].FIELD, type, pos, s);
1193 if (!pw->p[i].FIELD)
1194 goto error;
1197 return pw;
1198 error:
1199 FN(PW,free)(pw);
1200 return NULL;
1203 #ifndef NO_DROP_DIMS
1204 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
1205 enum isl_dim_type type, unsigned first, unsigned n)
1207 int i;
1208 enum isl_dim_type set_type;
1210 if (!pw)
1211 return NULL;
1212 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
1213 return pw;
1215 set_type = type == isl_dim_in ? isl_dim_set : type;
1217 pw = FN(PW,cow)(pw);
1218 if (!pw)
1219 return NULL;
1220 pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
1221 if (!pw->dim)
1222 goto error;
1223 for (i = 0; i < pw->n; ++i) {
1224 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
1225 if (!pw->p[i].FIELD)
1226 goto error;
1227 if (type == isl_dim_out)
1228 continue;
1229 pw->p[i].set = isl_set_drop(pw->p[i].set, set_type, first, n);
1230 if (!pw->p[i].set)
1231 goto error;
1234 return pw;
1235 error:
1236 FN(PW,free)(pw);
1237 return NULL;
1240 /* This function is very similar to drop_dims.
1241 * The only difference is that the cells may still involve
1242 * the specified dimensions. They are removed using
1243 * isl_set_project_out instead of isl_set_drop.
1245 __isl_give PW *FN(PW,project_out)(__isl_take PW *pw,
1246 enum isl_dim_type type, unsigned first, unsigned n)
1248 int i;
1249 enum isl_dim_type set_type;
1251 if (!pw)
1252 return NULL;
1253 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
1254 return pw;
1256 set_type = type == isl_dim_in ? isl_dim_set : type;
1258 pw = FN(PW,cow)(pw);
1259 if (!pw)
1260 return NULL;
1261 pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
1262 if (!pw->dim)
1263 goto error;
1264 for (i = 0; i < pw->n; ++i) {
1265 pw->p[i].set = isl_set_project_out(pw->p[i].set,
1266 set_type, first, n);
1267 if (!pw->p[i].set)
1268 goto error;
1269 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, 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;
1280 /* Project the domain of pw onto its parameter space.
1282 __isl_give PW *FN(PW,project_domain_on_params)(__isl_take PW *pw)
1284 isl_space *space;
1285 unsigned n;
1287 n = FN(PW,dim)(pw, isl_dim_in);
1288 pw = FN(PW,project_out)(pw, isl_dim_in, 0, n);
1289 space = FN(PW,get_domain_space)(pw);
1290 space = isl_space_params(space);
1291 pw = FN(PW,reset_domain_space)(pw, space);
1292 return pw;
1294 #endif
1296 #ifndef NO_INSERT_DIMS
1297 __isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
1298 unsigned first, unsigned n)
1300 int i;
1301 enum isl_dim_type set_type;
1303 if (!pw)
1304 return NULL;
1305 if (n == 0 && !isl_space_is_named_or_nested(pw->dim, type))
1306 return pw;
1308 set_type = type == isl_dim_in ? isl_dim_set : type;
1310 pw = FN(PW,cow)(pw);
1311 if (!pw)
1312 return NULL;
1314 pw->dim = isl_space_insert_dims(pw->dim, type, first, n);
1315 if (!pw->dim)
1316 goto error;
1318 for (i = 0; i < pw->n; ++i) {
1319 pw->p[i].set = isl_set_insert_dims(pw->p[i].set,
1320 set_type, first, n);
1321 if (!pw->p[i].set)
1322 goto error;
1323 pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
1324 type, first, n);
1325 if (!pw->p[i].FIELD)
1326 goto error;
1329 return pw;
1330 error:
1331 FN(PW,free)(pw);
1332 return NULL;
1334 #endif
1336 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
1337 enum isl_dim_type type, unsigned pos, isl_int v)
1339 int i;
1341 if (!pw)
1342 return NULL;
1344 if (type == isl_dim_in)
1345 type = isl_dim_set;
1347 pw = FN(PW,cow)(pw);
1348 if (!pw)
1349 return NULL;
1350 for (i = 0; i < pw->n; ++i) {
1351 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
1352 if (FN(PW,exploit_equalities_and_remove_if_empty)(pw, i) < 0)
1353 return FN(PW,free)(pw);
1356 return pw;
1359 /* Fix the value of the variable at position "pos" of type "type" of "pw"
1360 * to be equal to "v".
1362 __isl_give PW *FN(PW,fix_val)(__isl_take PW *pw,
1363 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
1365 if (!v)
1366 return FN(PW,free)(pw);
1367 if (!isl_val_is_int(v))
1368 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
1369 "expecting integer value", goto error);
1371 pw = FN(PW,fix_dim)(pw, type, pos, v->n);
1372 isl_val_free(v);
1374 return pw;
1375 error:
1376 isl_val_free(v);
1377 return FN(PW,free)(pw);
1380 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
1382 return pw ? isl_space_dim(pw->dim, type) : 0;
1385 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
1386 enum isl_dim_type type, unsigned first, unsigned n)
1388 int i;
1390 if (!pw)
1391 return NULL;
1392 if (n == 0)
1393 return pw;
1395 if (type == isl_dim_in)
1396 type = isl_dim_set;
1398 pw = FN(PW,cow)(pw);
1399 if (!pw)
1400 return NULL;
1401 if (!pw->dim)
1402 goto error;
1403 for (i = 0; i < pw->n; ++i) {
1404 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
1405 if (!pw->p[i].set)
1406 goto error;
1409 return pw;
1410 error:
1411 FN(PW,free)(pw);
1412 return NULL;
1415 #ifndef NO_OPT
1416 /* Compute the maximal value attained by the piecewise quasipolynomial
1417 * on its domain or zero if the domain is empty.
1418 * In the worst case, the domain is scanned completely,
1419 * so the domain is assumed to be bounded.
1421 __isl_give isl_val *FN(PW,opt)(__isl_take PW *pw, int max)
1423 int i;
1424 isl_val *opt;
1426 if (!pw)
1427 return NULL;
1429 if (pw->n == 0) {
1430 opt = isl_val_zero(FN(PW,get_ctx)(pw));
1431 FN(PW,free)(pw);
1432 return opt;
1435 opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
1436 isl_set_copy(pw->p[0].set), max);
1437 for (i = 1; i < pw->n; ++i) {
1438 isl_val *opt_i;
1439 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
1440 isl_set_copy(pw->p[i].set), max);
1441 if (max)
1442 opt = isl_val_max(opt, opt_i);
1443 else
1444 opt = isl_val_min(opt, opt_i);
1447 FN(PW,free)(pw);
1448 return opt;
1451 __isl_give isl_val *FN(PW,max)(__isl_take PW *pw)
1453 return FN(PW,opt)(pw, 1);
1456 __isl_give isl_val *FN(PW,min)(__isl_take PW *pw)
1458 return FN(PW,opt)(pw, 0);
1460 #endif
1462 /* Return the space of "pw".
1464 __isl_keep isl_space *FN(PW,peek_space)(__isl_keep PW *pw)
1466 return pw ? pw->dim : NULL;
1469 __isl_give isl_space *FN(PW,get_space)(__isl_keep PW *pw)
1471 return isl_space_copy(FN(PW,peek_space)(pw));
1474 __isl_give isl_space *FN(PW,get_domain_space)(__isl_keep PW *pw)
1476 return pw ? isl_space_domain(isl_space_copy(pw->dim)) : NULL;
1479 /* Return the position of the dimension of the given type and name
1480 * in "pw".
1481 * Return -1 if no such dimension can be found.
1483 int FN(PW,find_dim_by_name)(__isl_keep PW *pw,
1484 enum isl_dim_type type, const char *name)
1486 if (!pw)
1487 return -1;
1488 return isl_space_find_dim_by_name(pw->dim, type, name);
1491 #ifndef NO_RESET_DIM
1492 /* Reset the space of "pw". Since we don't know if the elements
1493 * represent the spaces themselves or their domains, we pass along
1494 * both when we call their reset_space_and_domain.
1496 static __isl_give PW *FN(PW,reset_space_and_domain)(__isl_take PW *pw,
1497 __isl_take isl_space *space, __isl_take isl_space *domain)
1499 int i;
1501 pw = FN(PW,cow)(pw);
1502 if (!pw || !space || !domain)
1503 goto error;
1505 for (i = 0; i < pw->n; ++i) {
1506 pw->p[i].set = isl_set_reset_space(pw->p[i].set,
1507 isl_space_copy(domain));
1508 if (!pw->p[i].set)
1509 goto error;
1510 pw->p[i].FIELD = FN(EL,reset_space_and_domain)(pw->p[i].FIELD,
1511 isl_space_copy(space), isl_space_copy(domain));
1512 if (!pw->p[i].FIELD)
1513 goto error;
1516 isl_space_free(domain);
1518 isl_space_free(pw->dim);
1519 pw->dim = space;
1521 return pw;
1522 error:
1523 isl_space_free(domain);
1524 isl_space_free(space);
1525 FN(PW,free)(pw);
1526 return NULL;
1529 __isl_give PW *FN(PW,reset_domain_space)(__isl_take PW *pw,
1530 __isl_take isl_space *domain)
1532 isl_space *space;
1534 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
1535 FN(PW,get_space)(pw));
1536 return FN(PW,reset_space_and_domain)(pw, space, domain);
1539 __isl_give PW *FN(PW,reset_space)(__isl_take PW *pw, __isl_take isl_space *dim)
1541 isl_space *domain;
1543 domain = isl_space_domain(isl_space_copy(dim));
1544 return FN(PW,reset_space_and_domain)(pw, dim, domain);
1547 __isl_give PW *FN(PW,set_tuple_id)(__isl_take PW *pw, enum isl_dim_type type,
1548 __isl_take isl_id *id)
1550 isl_space *space;
1552 pw = FN(PW,cow)(pw);
1553 if (!pw)
1554 goto error;
1556 space = FN(PW,get_space)(pw);
1557 space = isl_space_set_tuple_id(space, type, id);
1559 return FN(PW,reset_space)(pw, space);
1560 error:
1561 isl_id_free(id);
1562 return FN(PW,free)(pw);
1565 /* Drop the id on the specified tuple.
1567 __isl_give PW *FN(PW,reset_tuple_id)(__isl_take PW *pw, enum isl_dim_type type)
1569 isl_space *space;
1571 if (!pw)
1572 return NULL;
1573 if (!FN(PW,has_tuple_id)(pw, type))
1574 return pw;
1576 pw = FN(PW,cow)(pw);
1577 if (!pw)
1578 return NULL;
1580 space = FN(PW,get_space)(pw);
1581 space = isl_space_reset_tuple_id(space, type);
1583 return FN(PW,reset_space)(pw, space);
1586 __isl_give PW *FN(PW,set_dim_id)(__isl_take PW *pw,
1587 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1589 pw = FN(PW,cow)(pw);
1590 if (!pw)
1591 goto error;
1592 pw->dim = isl_space_set_dim_id(pw->dim, type, pos, id);
1593 return FN(PW,reset_space)(pw, isl_space_copy(pw->dim));
1594 error:
1595 isl_id_free(id);
1596 return FN(PW,free)(pw);
1598 #endif
1600 /* Reset the user pointer on all identifiers of parameters and tuples
1601 * of the space of "pw".
1603 __isl_give PW *FN(PW,reset_user)(__isl_take PW *pw)
1605 isl_space *space;
1607 space = FN(PW,get_space)(pw);
1608 space = isl_space_reset_user(space);
1610 return FN(PW,reset_space)(pw, space);
1613 isl_bool FN(PW,has_equal_space)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1615 if (!pw1 || !pw2)
1616 return isl_bool_error;
1618 return isl_space_is_equal(pw1->dim, pw2->dim);
1621 #ifndef NO_MORPH
1622 __isl_give PW *FN(PW,morph_domain)(__isl_take PW *pw,
1623 __isl_take isl_morph *morph)
1625 int i;
1626 isl_ctx *ctx;
1628 if (!pw || !morph)
1629 goto error;
1631 ctx = isl_space_get_ctx(pw->dim);
1632 isl_assert(ctx, isl_space_is_domain_internal(morph->dom->dim, pw->dim),
1633 goto error);
1635 pw = FN(PW,cow)(pw);
1636 if (!pw)
1637 goto error;
1638 pw->dim = isl_space_extend_domain_with_range(
1639 isl_space_copy(morph->ran->dim), pw->dim);
1640 if (!pw->dim)
1641 goto error;
1643 for (i = 0; i < pw->n; ++i) {
1644 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
1645 if (!pw->p[i].set)
1646 goto error;
1647 pw->p[i].FIELD = FN(EL,morph_domain)(pw->p[i].FIELD,
1648 isl_morph_copy(morph));
1649 if (!pw->p[i].FIELD)
1650 goto error;
1653 isl_morph_free(morph);
1655 return pw;
1656 error:
1657 FN(PW,free)(pw);
1658 isl_morph_free(morph);
1659 return NULL;
1661 #endif
1663 int FN(PW,n_piece)(__isl_keep PW *pw)
1665 return pw ? pw->n : 0;
1668 isl_stat FN(PW,foreach_piece)(__isl_keep PW *pw,
1669 isl_stat (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
1670 void *user)
1672 int i;
1674 if (!pw)
1675 return isl_stat_error;
1677 for (i = 0; i < pw->n; ++i)
1678 if (fn(isl_set_copy(pw->p[i].set),
1679 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
1680 return isl_stat_error;
1682 return isl_stat_ok;
1685 #ifndef NO_LIFT
1686 static isl_bool any_divs(__isl_keep isl_set *set)
1688 int i;
1690 if (!set)
1691 return isl_bool_error;
1693 for (i = 0; i < set->n; ++i)
1694 if (set->p[i]->n_div > 0)
1695 return isl_bool_true;
1697 return isl_bool_false;
1700 static isl_stat foreach_lifted_subset(__isl_take isl_set *set,
1701 __isl_take EL *el,
1702 isl_stat (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1703 void *user), void *user)
1705 int i;
1707 if (!set || !el)
1708 goto error;
1710 for (i = 0; i < set->n; ++i) {
1711 isl_set *lift;
1712 EL *copy;
1714 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
1715 lift = isl_set_lift(lift);
1717 copy = FN(EL,copy)(el);
1718 copy = FN(EL,lift)(copy, isl_set_get_space(lift));
1720 if (fn(lift, copy, user) < 0)
1721 goto error;
1724 isl_set_free(set);
1725 FN(EL,free)(el);
1727 return isl_stat_ok;
1728 error:
1729 isl_set_free(set);
1730 FN(EL,free)(el);
1731 return isl_stat_error;
1734 isl_stat FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
1735 isl_stat (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1736 void *user), void *user)
1738 int i;
1740 if (!pw)
1741 return isl_stat_error;
1743 for (i = 0; i < pw->n; ++i) {
1744 isl_bool any;
1745 isl_set *set;
1746 EL *el;
1748 any = any_divs(pw->p[i].set);
1749 if (any < 0)
1750 return isl_stat_error;
1751 set = isl_set_copy(pw->p[i].set);
1752 el = FN(EL,copy)(pw->p[i].FIELD);
1753 if (!any) {
1754 if (fn(set, el, user) < 0)
1755 return isl_stat_error;
1756 continue;
1758 if (foreach_lifted_subset(set, el, fn, user) < 0)
1759 return isl_stat_error;
1762 return isl_stat_ok;
1764 #endif
1766 #ifndef NO_MOVE_DIMS
1767 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
1768 enum isl_dim_type dst_type, unsigned dst_pos,
1769 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1771 int i;
1773 pw = FN(PW,cow)(pw);
1774 if (!pw)
1775 return NULL;
1777 pw->dim = isl_space_move_dims(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
1778 if (!pw->dim)
1779 goto error;
1781 for (i = 0; i < pw->n; ++i) {
1782 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
1783 dst_type, dst_pos, src_type, src_pos, n);
1784 if (!pw->p[i].FIELD)
1785 goto error;
1788 if (dst_type == isl_dim_in)
1789 dst_type = isl_dim_set;
1790 if (src_type == isl_dim_in)
1791 src_type = isl_dim_set;
1793 for (i = 0; i < pw->n; ++i) {
1794 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
1795 dst_type, dst_pos,
1796 src_type, src_pos, n);
1797 if (!pw->p[i].set)
1798 goto error;
1801 return pw;
1802 error:
1803 FN(PW,free)(pw);
1804 return NULL;
1806 #endif
1808 __isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
1810 int i;
1812 if (isl_int_is_one(v))
1813 return pw;
1814 if (pw && DEFAULT_IS_ZERO && isl_int_is_zero(v)) {
1815 PW *zero;
1816 isl_space *dim = FN(PW,get_space)(pw);
1817 #ifdef HAS_TYPE
1818 zero = FN(PW,ZERO)(dim, pw->type);
1819 #else
1820 zero = FN(PW,ZERO)(dim);
1821 #endif
1822 FN(PW,free)(pw);
1823 return zero;
1825 pw = FN(PW,cow)(pw);
1826 if (!pw)
1827 return NULL;
1828 if (pw->n == 0)
1829 return pw;
1831 #ifdef HAS_TYPE
1832 if (isl_int_is_neg(v))
1833 pw->type = isl_fold_type_negate(pw->type);
1834 #endif
1835 for (i = 0; i < pw->n; ++i) {
1836 pw->p[i].FIELD = FN(EL,scale)(pw->p[i].FIELD, v);
1837 if (!pw->p[i].FIELD)
1838 goto error;
1841 return pw;
1842 error:
1843 FN(PW,free)(pw);
1844 return NULL;
1847 /* Multiply the pieces of "pw" by "v" and return the result.
1849 __isl_give PW *FN(PW,scale_val)(__isl_take PW *pw, __isl_take isl_val *v)
1851 int i;
1853 if (!pw || !v)
1854 goto error;
1856 if (isl_val_is_one(v)) {
1857 isl_val_free(v);
1858 return pw;
1860 if (pw && DEFAULT_IS_ZERO && isl_val_is_zero(v)) {
1861 PW *zero;
1862 isl_space *space = FN(PW,get_space)(pw);
1863 #ifdef HAS_TYPE
1864 zero = FN(PW,ZERO)(space, pw->type);
1865 #else
1866 zero = FN(PW,ZERO)(space);
1867 #endif
1868 FN(PW,free)(pw);
1869 isl_val_free(v);
1870 return zero;
1872 if (pw->n == 0) {
1873 isl_val_free(v);
1874 return pw;
1876 pw = FN(PW,cow)(pw);
1877 if (!pw)
1878 goto error;
1880 #ifdef HAS_TYPE
1881 if (isl_val_is_neg(v))
1882 pw->type = isl_fold_type_negate(pw->type);
1883 #endif
1884 for (i = 0; i < pw->n; ++i) {
1885 pw->p[i].FIELD = FN(EL,scale_val)(pw->p[i].FIELD,
1886 isl_val_copy(v));
1887 if (!pw->p[i].FIELD)
1888 goto error;
1891 isl_val_free(v);
1892 return pw;
1893 error:
1894 isl_val_free(v);
1895 FN(PW,free)(pw);
1896 return NULL;
1899 /* Divide the pieces of "pw" by "v" and return the result.
1901 __isl_give PW *FN(PW,scale_down_val)(__isl_take PW *pw, __isl_take isl_val *v)
1903 int i;
1905 if (!pw || !v)
1906 goto error;
1908 if (isl_val_is_one(v)) {
1909 isl_val_free(v);
1910 return pw;
1913 if (!isl_val_is_rat(v))
1914 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1915 "expecting rational factor", goto error);
1916 if (isl_val_is_zero(v))
1917 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1918 "cannot scale down by zero", goto error);
1920 if (pw->n == 0) {
1921 isl_val_free(v);
1922 return pw;
1924 pw = FN(PW,cow)(pw);
1925 if (!pw)
1926 goto error;
1928 #ifdef HAS_TYPE
1929 if (isl_val_is_neg(v))
1930 pw->type = isl_fold_type_negate(pw->type);
1931 #endif
1932 for (i = 0; i < pw->n; ++i) {
1933 pw->p[i].FIELD = FN(EL,scale_down_val)(pw->p[i].FIELD,
1934 isl_val_copy(v));
1935 if (!pw->p[i].FIELD)
1936 goto error;
1939 isl_val_free(v);
1940 return pw;
1941 error:
1942 isl_val_free(v);
1943 FN(PW,free)(pw);
1944 return NULL;
1947 __isl_give PW *FN(PW,scale)(__isl_take PW *pw, isl_int v)
1949 return FN(PW,mul_isl_int)(pw, v);
1952 /* Apply some normalization to "pw".
1953 * In particular, sort the pieces according to their function value
1954 * expressions, combining pairs of adjacent pieces with
1955 * the same such expression, and then normalize the domains of the pieces.
1957 * We normalize in place, but if anything goes wrong we need
1958 * to return NULL, so we need to make sure we don't change the
1959 * meaning of any possible other copies of "pw".
1961 __isl_give PW *FN(PW,normalize)(__isl_take PW *pw)
1963 int i;
1964 isl_set *set;
1966 pw = FN(PW,sort)(pw);
1967 if (!pw)
1968 return NULL;
1969 for (i = 0; i < pw->n; ++i) {
1970 set = isl_set_normalize(isl_set_copy(pw->p[i].set));
1971 if (!set)
1972 return FN(PW,free)(pw);
1973 isl_set_free(pw->p[i].set);
1974 pw->p[i].set = set;
1977 return pw;
1980 /* Is pw1 obviously equal to pw2?
1981 * That is, do they have obviously identical cells and obviously identical
1982 * elements on each cell?
1984 * If "pw1" or "pw2" contain any NaNs, then they are considered
1985 * not to be the same. A NaN is not equal to anything, not even
1986 * to another NaN.
1988 isl_bool FN(PW,plain_is_equal)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1990 int i;
1991 isl_bool equal, has_nan;
1993 if (!pw1 || !pw2)
1994 return isl_bool_error;
1996 has_nan = FN(PW,involves_nan)(pw1);
1997 if (has_nan >= 0 && !has_nan)
1998 has_nan = FN(PW,involves_nan)(pw2);
1999 if (has_nan < 0 || has_nan)
2000 return isl_bool_not(has_nan);
2002 if (pw1 == pw2)
2003 return isl_bool_true;
2004 if (!isl_space_is_equal(pw1->dim, pw2->dim))
2005 return isl_bool_false;
2007 pw1 = FN(PW,copy)(pw1);
2008 pw2 = FN(PW,copy)(pw2);
2009 pw1 = FN(PW,normalize)(pw1);
2010 pw2 = FN(PW,normalize)(pw2);
2011 if (!pw1 || !pw2)
2012 goto error;
2014 equal = pw1->n == pw2->n;
2015 for (i = 0; equal && i < pw1->n; ++i) {
2016 equal = isl_set_plain_is_equal(pw1->p[i].set, pw2->p[i].set);
2017 if (equal < 0)
2018 goto error;
2019 if (!equal)
2020 break;
2021 equal = FN(EL,plain_is_equal)(pw1->p[i].FIELD, pw2->p[i].FIELD);
2022 if (equal < 0)
2023 goto error;
2026 FN(PW,free)(pw1);
2027 FN(PW,free)(pw2);
2028 return equal;
2029 error:
2030 FN(PW,free)(pw1);
2031 FN(PW,free)(pw2);
2032 return isl_bool_error;
2035 /* Does "pw" involve any NaNs?
2037 isl_bool FN(PW,involves_nan)(__isl_keep PW *pw)
2039 int i;
2041 if (!pw)
2042 return isl_bool_error;
2043 if (pw->n == 0)
2044 return isl_bool_false;
2046 for (i = 0; i < pw->n; ++i) {
2047 isl_bool has_nan = FN(EL,involves_nan)(pw->p[i].FIELD);
2048 if (has_nan < 0 || has_nan)
2049 return has_nan;
2052 return isl_bool_false;
2055 #ifndef NO_PULLBACK
2056 static __isl_give PW *FN(PW,align_params_pw_multi_aff_and)(__isl_take PW *pw,
2057 __isl_take isl_multi_aff *ma,
2058 __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_multi_aff *ma))
2060 isl_ctx *ctx;
2061 isl_bool equal_params;
2062 isl_space *ma_space;
2064 ma_space = isl_multi_aff_get_space(ma);
2065 if (!pw || !ma || !ma_space)
2066 goto error;
2067 equal_params = isl_space_has_equal_params(pw->dim, ma_space);
2068 if (equal_params < 0)
2069 goto error;
2070 if (equal_params) {
2071 isl_space_free(ma_space);
2072 return fn(pw, ma);
2074 ctx = FN(PW,get_ctx)(pw);
2075 if (!isl_space_has_named_params(pw->dim) ||
2076 !isl_space_has_named_params(ma_space))
2077 isl_die(ctx, isl_error_invalid,
2078 "unaligned unnamed parameters", goto error);
2079 pw = FN(PW,align_params)(pw, ma_space);
2080 ma = isl_multi_aff_align_params(ma, FN(PW,get_space)(pw));
2081 return fn(pw, ma);
2082 error:
2083 isl_space_free(ma_space);
2084 FN(PW,free)(pw);
2085 isl_multi_aff_free(ma);
2086 return NULL;
2089 static __isl_give PW *FN(PW,align_params_pw_pw_multi_aff_and)(__isl_take PW *pw,
2090 __isl_take isl_pw_multi_aff *pma,
2091 __isl_give PW *(*fn)(__isl_take PW *pw,
2092 __isl_take isl_pw_multi_aff *ma))
2094 isl_ctx *ctx;
2095 isl_bool equal_params;
2096 isl_space *pma_space;
2098 pma_space = isl_pw_multi_aff_get_space(pma);
2099 if (!pw || !pma || !pma_space)
2100 goto error;
2101 equal_params = isl_space_has_equal_params(pw->dim, pma_space);
2102 if (equal_params < 0)
2103 goto error;
2104 if (equal_params) {
2105 isl_space_free(pma_space);
2106 return fn(pw, pma);
2108 ctx = FN(PW,get_ctx)(pw);
2109 if (!isl_space_has_named_params(pw->dim) ||
2110 !isl_space_has_named_params(pma_space))
2111 isl_die(ctx, isl_error_invalid,
2112 "unaligned unnamed parameters", goto error);
2113 pw = FN(PW,align_params)(pw, pma_space);
2114 pma = isl_pw_multi_aff_align_params(pma, FN(PW,get_space)(pw));
2115 return fn(pw, pma);
2116 error:
2117 isl_space_free(pma_space);
2118 FN(PW,free)(pw);
2119 isl_pw_multi_aff_free(pma);
2120 return NULL;
2123 /* Compute the pullback of "pw" by the function represented by "ma".
2124 * In other words, plug in "ma" in "pw".
2126 static __isl_give PW *FN(PW,pullback_multi_aff_aligned)(__isl_take PW *pw,
2127 __isl_take isl_multi_aff *ma)
2129 int i;
2130 isl_space *space = NULL;
2132 ma = isl_multi_aff_align_divs(ma);
2133 pw = FN(PW,cow)(pw);
2134 if (!pw || !ma)
2135 goto error;
2137 space = isl_space_join(isl_multi_aff_get_space(ma),
2138 FN(PW,get_space)(pw));
2140 for (i = 0; i < pw->n; ++i) {
2141 pw->p[i].set = isl_set_preimage_multi_aff(pw->p[i].set,
2142 isl_multi_aff_copy(ma));
2143 if (!pw->p[i].set)
2144 goto error;
2145 pw->p[i].FIELD = FN(EL,pullback_multi_aff)(pw->p[i].FIELD,
2146 isl_multi_aff_copy(ma));
2147 if (!pw->p[i].FIELD)
2148 goto error;
2151 pw = FN(PW,reset_space)(pw, space);
2152 isl_multi_aff_free(ma);
2153 return pw;
2154 error:
2155 isl_space_free(space);
2156 isl_multi_aff_free(ma);
2157 FN(PW,free)(pw);
2158 return NULL;
2161 __isl_give PW *FN(PW,pullback_multi_aff)(__isl_take PW *pw,
2162 __isl_take isl_multi_aff *ma)
2164 return FN(PW,align_params_pw_multi_aff_and)(pw, ma,
2165 &FN(PW,pullback_multi_aff_aligned));
2168 /* Compute the pullback of "pw" by the function represented by "pma".
2169 * In other words, plug in "pma" in "pw".
2171 static __isl_give PW *FN(PW,pullback_pw_multi_aff_aligned)(__isl_take PW *pw,
2172 __isl_take isl_pw_multi_aff *pma)
2174 int i;
2175 PW *res;
2177 if (!pma)
2178 goto error;
2180 if (pma->n == 0) {
2181 isl_space *space;
2182 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
2183 FN(PW,get_space)(pw));
2184 isl_pw_multi_aff_free(pma);
2185 res = FN(PW,empty)(space);
2186 FN(PW,free)(pw);
2187 return res;
2190 res = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
2191 isl_multi_aff_copy(pma->p[0].maff));
2192 res = FN(PW,intersect_domain)(res, isl_set_copy(pma->p[0].set));
2194 for (i = 1; i < pma->n; ++i) {
2195 PW *res_i;
2197 res_i = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
2198 isl_multi_aff_copy(pma->p[i].maff));
2199 res_i = FN(PW,intersect_domain)(res_i,
2200 isl_set_copy(pma->p[i].set));
2201 res = FN(PW,add_disjoint)(res, res_i);
2204 isl_pw_multi_aff_free(pma);
2205 FN(PW,free)(pw);
2206 return res;
2207 error:
2208 isl_pw_multi_aff_free(pma);
2209 FN(PW,free)(pw);
2210 return NULL;
2213 __isl_give PW *FN(PW,pullback_pw_multi_aff)(__isl_take PW *pw,
2214 __isl_take isl_pw_multi_aff *pma)
2216 return FN(PW,align_params_pw_pw_multi_aff_and)(pw, pma,
2217 &FN(PW,pullback_pw_multi_aff_aligned));
2219 #endif