interface/cpp.cc: cpp_generator::print_method_impl: drop unused variable
[isl.git] / isl_pw_templ.c
blob6583cd2cc3959d50d6a543f4aa32261e3147baa4
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 #ifndef NO_EVAL
703 __isl_give isl_val *FN(PW,eval)(__isl_take PW *pw, __isl_take isl_point *pnt)
705 int i;
706 int found = 0;
707 isl_ctx *ctx;
708 isl_space *pnt_dim = NULL;
709 isl_val *v;
711 if (!pw || !pnt)
712 goto error;
713 ctx = isl_point_get_ctx(pnt);
714 pnt_dim = isl_point_get_space(pnt);
715 isl_assert(ctx, isl_space_is_domain_internal(pnt_dim, pw->dim),
716 goto error);
718 for (i = 0; i < pw->n; ++i) {
719 found = isl_set_contains_point(pw->p[i].set, pnt);
720 if (found < 0)
721 goto error;
722 if (found)
723 break;
725 if (found)
726 v = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
727 isl_point_copy(pnt));
728 else
729 v = isl_val_zero(ctx);
730 FN(PW,free)(pw);
731 isl_space_free(pnt_dim);
732 isl_point_free(pnt);
733 return v;
734 error:
735 FN(PW,free)(pw);
736 isl_space_free(pnt_dim);
737 isl_point_free(pnt);
738 return NULL;
740 #endif
742 /* Return the parameter domain of "pw".
744 __isl_give isl_set *FN(PW,params)(__isl_take PW *pw)
746 return isl_set_params(FN(PW,domain)(pw));
749 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
751 int i;
752 isl_set *dom;
754 if (!pw)
755 return NULL;
757 dom = isl_set_empty(FN(PW,get_domain_space)(pw));
758 for (i = 0; i < pw->n; ++i)
759 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
761 FN(PW,free)(pw);
763 return dom;
766 /* Exploit the equalities in the domain of piece "i" of "pw"
767 * to simplify the associated function.
768 * If the domain of piece "i" is empty, then remove it entirely,
769 * replacing it with the final piece.
771 static int FN(PW,exploit_equalities_and_remove_if_empty)(__isl_keep PW *pw,
772 int i)
774 isl_basic_set *aff;
775 int empty = isl_set_plain_is_empty(pw->p[i].set);
777 if (empty < 0)
778 return -1;
779 if (empty) {
780 isl_set_free(pw->p[i].set);
781 FN(EL,free)(pw->p[i].FIELD);
782 if (i != pw->n - 1)
783 pw->p[i] = pw->p[pw->n - 1];
784 pw->n--;
786 return 0;
789 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
790 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD, aff);
791 if (!pw->p[i].FIELD)
792 return -1;
794 return 0;
797 /* Convert a piecewise expression defined over a parameter domain
798 * into one that is defined over a zero-dimensional set.
800 __isl_give PW *FN(PW,from_range)(__isl_take PW *pw)
802 isl_space *space;
804 if (!pw)
805 return NULL;
806 if (!isl_space_is_set(pw->dim))
807 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
808 "not living in a set space", return FN(PW,free)(pw));
810 space = FN(PW,get_space)(pw);
811 space = isl_space_from_range(space);
812 pw = FN(PW,reset_space)(pw, space);
814 return pw;
817 /* Fix the value of the given parameter or domain dimension of "pw"
818 * to be equal to "value".
820 __isl_give PW *FN(PW,fix_si)(__isl_take PW *pw, enum isl_dim_type type,
821 unsigned pos, int value)
823 int i;
825 if (!pw)
826 return NULL;
828 if (type == isl_dim_out)
829 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
830 "cannot fix output dimension", return FN(PW,free)(pw));
832 if (pw->n == 0)
833 return pw;
835 if (type == isl_dim_in)
836 type = isl_dim_set;
838 pw = FN(PW,cow)(pw);
839 if (!pw)
840 return FN(PW,free)(pw);
842 for (i = pw->n - 1; i >= 0; --i) {
843 pw->p[i].set = isl_set_fix_si(pw->p[i].set, type, pos, value);
844 if (FN(PW,exploit_equalities_and_remove_if_empty)(pw, i) < 0)
845 return FN(PW,free)(pw);
848 return pw;
851 /* Restrict the domain of "pw" by combining each cell
852 * with "set" through a call to "fn", where "fn" may be
853 * isl_set_intersect, isl_set_intersect_params or isl_set_subtract.
855 static __isl_give PW *FN(PW,restrict_domain_aligned)(__isl_take PW *pw,
856 __isl_take isl_set *set,
857 __isl_give isl_set *(*fn)(__isl_take isl_set *set1,
858 __isl_take isl_set *set2))
860 int i;
862 if (!pw || !set)
863 goto error;
865 if (pw->n == 0) {
866 isl_set_free(set);
867 return pw;
870 pw = FN(PW,cow)(pw);
871 if (!pw)
872 goto error;
874 for (i = pw->n - 1; i >= 0; --i) {
875 pw->p[i].set = fn(pw->p[i].set, isl_set_copy(set));
876 if (FN(PW,exploit_equalities_and_remove_if_empty)(pw, i) < 0)
877 goto error;
880 isl_set_free(set);
881 return pw;
882 error:
883 isl_set_free(set);
884 FN(PW,free)(pw);
885 return NULL;
888 static __isl_give PW *FN(PW,intersect_domain_aligned)(__isl_take PW *pw,
889 __isl_take isl_set *set)
891 return FN(PW,restrict_domain_aligned)(pw, set, &isl_set_intersect);
894 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw,
895 __isl_take isl_set *context)
897 return FN(PW,align_params_pw_set_and)(pw, context,
898 &FN(PW,intersect_domain_aligned));
901 static __isl_give PW *FN(PW,intersect_params_aligned)(__isl_take PW *pw,
902 __isl_take isl_set *set)
904 return FN(PW,restrict_domain_aligned)(pw, set,
905 &isl_set_intersect_params);
908 /* Intersect the domain of "pw" with the parameter domain "context".
910 __isl_give PW *FN(PW,intersect_params)(__isl_take PW *pw,
911 __isl_take isl_set *context)
913 return FN(PW,align_params_pw_set_and)(pw, context,
914 &FN(PW,intersect_params_aligned));
917 /* Subtract "domain' from the domain of "pw", assuming their
918 * parameters have been aligned.
920 static __isl_give PW *FN(PW,subtract_domain_aligned)(__isl_take PW *pw,
921 __isl_take isl_set *domain)
923 return FN(PW,restrict_domain_aligned)(pw, domain, &isl_set_subtract);
926 /* Subtract "domain' from the domain of "pw".
928 __isl_give PW *FN(PW,subtract_domain)(__isl_take PW *pw,
929 __isl_take isl_set *domain)
931 return FN(PW,align_params_pw_set_and)(pw, domain,
932 &FN(PW,subtract_domain_aligned));
935 /* Compute the gist of "pw" with respect to the domain constraints
936 * of "context" for the case where the domain of the last element
937 * of "pw" is equal to "context".
938 * Call "fn_el" to compute the gist of this element, replace
939 * its domain by the universe and drop all other elements
940 * as their domains are necessarily disjoint from "context".
942 static __isl_give PW *FN(PW,gist_last)(__isl_take PW *pw,
943 __isl_take isl_set *context,
944 __isl_give EL *(*fn_el)(__isl_take EL *el, __isl_take isl_set *set))
946 int i;
947 isl_space *space;
949 for (i = 0; i < pw->n - 1; ++i) {
950 isl_set_free(pw->p[i].set);
951 FN(EL,free)(pw->p[i].FIELD);
953 pw->p[0].FIELD = pw->p[pw->n - 1].FIELD;
954 pw->p[0].set = pw->p[pw->n - 1].set;
955 pw->n = 1;
957 space = isl_set_get_space(context);
958 pw->p[0].FIELD = fn_el(pw->p[0].FIELD, context);
959 context = isl_set_universe(space);
960 isl_set_free(pw->p[0].set);
961 pw->p[0].set = context;
963 if (!pw->p[0].FIELD || !pw->p[0].set)
964 return FN(PW,free)(pw);
966 return pw;
969 /* Compute the gist of "pw" with respect to the domain constraints
970 * of "context". Call "fn_el" to compute the gist of the elements
971 * and "fn_dom" to compute the gist of the domains.
973 * If the piecewise expression is empty or the context is the universe,
974 * then nothing can be simplified.
976 static __isl_give PW *FN(PW,gist_aligned)(__isl_take PW *pw,
977 __isl_take isl_set *context,
978 __isl_give EL *(*fn_el)(__isl_take EL *el,
979 __isl_take isl_set *set),
980 __isl_give isl_set *(*fn_dom)(__isl_take isl_set *set,
981 __isl_take isl_basic_set *bset))
983 int i;
984 int is_universe;
985 isl_bool aligned;
986 isl_basic_set *hull = NULL;
988 if (!pw || !context)
989 goto error;
991 if (pw->n == 0) {
992 isl_set_free(context);
993 return pw;
996 is_universe = isl_set_plain_is_universe(context);
997 if (is_universe < 0)
998 goto error;
999 if (is_universe) {
1000 isl_set_free(context);
1001 return pw;
1004 aligned = isl_set_space_has_equal_params(context, pw->dim);
1005 if (aligned < 0)
1006 goto error;
1007 if (!aligned) {
1008 pw = FN(PW,align_params)(pw, isl_set_get_space(context));
1009 context = isl_set_align_params(context, FN(PW,get_space)(pw));
1012 pw = FN(PW,cow)(pw);
1013 if (!pw)
1014 goto error;
1016 if (pw->n == 1) {
1017 int equal;
1019 equal = isl_set_plain_is_equal(pw->p[0].set, context);
1020 if (equal < 0)
1021 goto error;
1022 if (equal)
1023 return FN(PW,gist_last)(pw, context, fn_el);
1026 context = isl_set_compute_divs(context);
1027 hull = isl_set_simple_hull(isl_set_copy(context));
1029 for (i = pw->n - 1; i >= 0; --i) {
1030 isl_set *set_i;
1031 int empty;
1033 if (i == pw->n - 1) {
1034 int equal;
1035 equal = isl_set_plain_is_equal(pw->p[i].set, context);
1036 if (equal < 0)
1037 goto error;
1038 if (equal) {
1039 isl_basic_set_free(hull);
1040 return FN(PW,gist_last)(pw, context, fn_el);
1043 set_i = isl_set_intersect(isl_set_copy(pw->p[i].set),
1044 isl_set_copy(context));
1045 empty = isl_set_plain_is_empty(set_i);
1046 pw->p[i].FIELD = fn_el(pw->p[i].FIELD, set_i);
1047 pw->p[i].set = fn_dom(pw->p[i].set, isl_basic_set_copy(hull));
1048 if (empty < 0 || !pw->p[i].FIELD || !pw->p[i].set)
1049 goto error;
1050 if (empty) {
1051 isl_set_free(pw->p[i].set);
1052 FN(EL,free)(pw->p[i].FIELD);
1053 if (i != pw->n - 1)
1054 pw->p[i] = pw->p[pw->n - 1];
1055 pw->n--;
1059 isl_basic_set_free(hull);
1060 isl_set_free(context);
1062 return pw;
1063 error:
1064 FN(PW,free)(pw);
1065 isl_basic_set_free(hull);
1066 isl_set_free(context);
1067 return NULL;
1070 static __isl_give PW *FN(PW,gist_domain_aligned)(__isl_take PW *pw,
1071 __isl_take isl_set *set)
1073 return FN(PW,gist_aligned)(pw, set, &FN(EL,gist),
1074 &isl_set_gist_basic_set);
1077 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
1079 return FN(PW,align_params_pw_set_and)(pw, context,
1080 &FN(PW,gist_domain_aligned));
1083 static __isl_give PW *FN(PW,gist_params_aligned)(__isl_take PW *pw,
1084 __isl_take isl_set *set)
1086 return FN(PW,gist_aligned)(pw, set, &FN(EL,gist_params),
1087 &isl_set_gist_params_basic_set);
1090 __isl_give PW *FN(PW,gist_params)(__isl_take PW *pw,
1091 __isl_take isl_set *context)
1093 return FN(PW,align_params_pw_set_and)(pw, context,
1094 &FN(PW,gist_params_aligned));
1097 /* Return -1 if the piece "p1" should be sorted before "p2"
1098 * and 1 if it should be sorted after "p2".
1099 * Return 0 if they do not need to be sorted in a specific order.
1101 * The two pieces are compared on the basis of their function value expressions.
1103 static int FN(PW,sort_field_cmp)(const void *p1, const void *p2, void *arg)
1105 struct FN(PW,piece) const *pc1 = p1;
1106 struct FN(PW,piece) const *pc2 = p2;
1108 return FN(EL,plain_cmp)(pc1->FIELD, pc2->FIELD);
1111 /* Sort the pieces of "pw" according to their function value
1112 * expressions and then combine pairs of adjacent pieces with
1113 * the same such expression.
1115 * The sorting is performed in place because it does not
1116 * change the meaning of "pw", but care needs to be
1117 * taken not to change any possible other copies of "pw"
1118 * in case anything goes wrong.
1120 __isl_give PW *FN(PW,sort)(__isl_take PW *pw)
1122 int i, j;
1123 isl_set *set;
1125 if (!pw)
1126 return NULL;
1127 if (pw->n <= 1)
1128 return pw;
1129 if (isl_sort(pw->p, pw->n, sizeof(pw->p[0]),
1130 &FN(PW,sort_field_cmp), NULL) < 0)
1131 return FN(PW,free)(pw);
1132 for (i = pw->n - 1; i >= 1; --i) {
1133 if (!FN(EL,plain_is_equal)(pw->p[i - 1].FIELD, pw->p[i].FIELD))
1134 continue;
1135 set = isl_set_union(isl_set_copy(pw->p[i - 1].set),
1136 isl_set_copy(pw->p[i].set));
1137 if (!set)
1138 return FN(PW,free)(pw);
1139 isl_set_free(pw->p[i].set);
1140 FN(EL,free)(pw->p[i].FIELD);
1141 isl_set_free(pw->p[i - 1].set);
1142 pw->p[i - 1].set = set;
1143 for (j = i + 1; j < pw->n; ++j)
1144 pw->p[j - 1] = pw->p[j];
1145 pw->n--;
1148 return pw;
1151 /* Coalesce the domains of "pw".
1153 * Prior to the actual coalescing, first sort the pieces such that
1154 * pieces with the same function value expression are combined
1155 * into a single piece, the combined domain of which can then
1156 * be coalesced.
1158 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
1160 int i;
1162 pw = FN(PW,sort)(pw);
1163 if (!pw)
1164 return NULL;
1166 for (i = 0; i < pw->n; ++i) {
1167 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
1168 if (!pw->p[i].set)
1169 goto error;
1172 return pw;
1173 error:
1174 FN(PW,free)(pw);
1175 return NULL;
1178 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
1180 return pw ? isl_space_get_ctx(pw->dim) : NULL;
1183 #ifndef NO_INVOLVES_DIMS
1184 isl_bool FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
1185 unsigned first, unsigned n)
1187 int i;
1188 enum isl_dim_type set_type;
1190 if (!pw)
1191 return isl_bool_error;
1192 if (pw->n == 0 || n == 0)
1193 return isl_bool_false;
1195 set_type = type == isl_dim_in ? isl_dim_set : type;
1197 for (i = 0; i < pw->n; ++i) {
1198 isl_bool involves = FN(EL,involves_dims)(pw->p[i].FIELD,
1199 type, first, n);
1200 if (involves < 0 || involves)
1201 return involves;
1202 involves = isl_set_involves_dims(pw->p[i].set,
1203 set_type, first, n);
1204 if (involves < 0 || involves)
1205 return involves;
1207 return isl_bool_false;
1209 #endif
1211 __isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
1212 enum isl_dim_type type, unsigned pos, const char *s)
1214 int i;
1215 enum isl_dim_type set_type;
1217 pw = FN(PW,cow)(pw);
1218 if (!pw)
1219 return NULL;
1221 set_type = type == isl_dim_in ? isl_dim_set : type;
1223 pw->dim = isl_space_set_dim_name(pw->dim, type, pos, s);
1224 if (!pw->dim)
1225 goto error;
1227 for (i = 0; i < pw->n; ++i) {
1228 pw->p[i].set = isl_set_set_dim_name(pw->p[i].set,
1229 set_type, pos, s);
1230 if (!pw->p[i].set)
1231 goto error;
1232 pw->p[i].FIELD = FN(EL,set_dim_name)(pw->p[i].FIELD, type, pos, s);
1233 if (!pw->p[i].FIELD)
1234 goto error;
1237 return pw;
1238 error:
1239 FN(PW,free)(pw);
1240 return NULL;
1243 #ifndef NO_DROP_DIMS
1244 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
1245 enum isl_dim_type type, unsigned first, unsigned n)
1247 int i;
1248 enum isl_dim_type set_type;
1250 if (!pw)
1251 return NULL;
1252 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
1253 return pw;
1255 set_type = type == isl_dim_in ? isl_dim_set : type;
1257 pw = FN(PW,cow)(pw);
1258 if (!pw)
1259 return NULL;
1260 pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
1261 if (!pw->dim)
1262 goto error;
1263 for (i = 0; i < pw->n; ++i) {
1264 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
1265 if (!pw->p[i].FIELD)
1266 goto error;
1267 if (type == isl_dim_out)
1268 continue;
1269 pw->p[i].set = isl_set_drop(pw->p[i].set, set_type, first, n);
1270 if (!pw->p[i].set)
1271 goto error;
1274 return pw;
1275 error:
1276 FN(PW,free)(pw);
1277 return NULL;
1280 /* This function is very similar to drop_dims.
1281 * The only difference is that the cells may still involve
1282 * the specified dimensions. They are removed using
1283 * isl_set_project_out instead of isl_set_drop.
1285 __isl_give PW *FN(PW,project_out)(__isl_take PW *pw,
1286 enum isl_dim_type type, unsigned first, unsigned n)
1288 int i;
1289 enum isl_dim_type set_type;
1291 if (!pw)
1292 return NULL;
1293 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
1294 return pw;
1296 set_type = type == isl_dim_in ? isl_dim_set : type;
1298 pw = FN(PW,cow)(pw);
1299 if (!pw)
1300 return NULL;
1301 pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
1302 if (!pw->dim)
1303 goto error;
1304 for (i = 0; i < pw->n; ++i) {
1305 pw->p[i].set = isl_set_project_out(pw->p[i].set,
1306 set_type, first, n);
1307 if (!pw->p[i].set)
1308 goto error;
1309 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
1310 if (!pw->p[i].FIELD)
1311 goto error;
1314 return pw;
1315 error:
1316 FN(PW,free)(pw);
1317 return NULL;
1320 /* Project the domain of pw onto its parameter space.
1322 __isl_give PW *FN(PW,project_domain_on_params)(__isl_take PW *pw)
1324 isl_space *space;
1325 unsigned n;
1327 n = FN(PW,dim)(pw, isl_dim_in);
1328 pw = FN(PW,project_out)(pw, isl_dim_in, 0, n);
1329 space = FN(PW,get_domain_space)(pw);
1330 space = isl_space_params(space);
1331 pw = FN(PW,reset_domain_space)(pw, space);
1332 return pw;
1334 #endif
1336 #ifndef NO_INSERT_DIMS
1337 __isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
1338 unsigned first, unsigned n)
1340 int i;
1341 enum isl_dim_type set_type;
1343 if (!pw)
1344 return NULL;
1345 if (n == 0 && !isl_space_is_named_or_nested(pw->dim, type))
1346 return pw;
1348 set_type = type == isl_dim_in ? isl_dim_set : type;
1350 pw = FN(PW,cow)(pw);
1351 if (!pw)
1352 return NULL;
1354 pw->dim = isl_space_insert_dims(pw->dim, type, first, n);
1355 if (!pw->dim)
1356 goto error;
1358 for (i = 0; i < pw->n; ++i) {
1359 pw->p[i].set = isl_set_insert_dims(pw->p[i].set,
1360 set_type, first, n);
1361 if (!pw->p[i].set)
1362 goto error;
1363 pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
1364 type, first, n);
1365 if (!pw->p[i].FIELD)
1366 goto error;
1369 return pw;
1370 error:
1371 FN(PW,free)(pw);
1372 return NULL;
1374 #endif
1376 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
1377 enum isl_dim_type type, unsigned pos, isl_int v)
1379 int i;
1381 if (!pw)
1382 return NULL;
1384 if (type == isl_dim_in)
1385 type = isl_dim_set;
1387 pw = FN(PW,cow)(pw);
1388 if (!pw)
1389 return NULL;
1390 for (i = 0; i < pw->n; ++i) {
1391 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
1392 if (FN(PW,exploit_equalities_and_remove_if_empty)(pw, i) < 0)
1393 return FN(PW,free)(pw);
1396 return pw;
1399 /* Fix the value of the variable at position "pos" of type "type" of "pw"
1400 * to be equal to "v".
1402 __isl_give PW *FN(PW,fix_val)(__isl_take PW *pw,
1403 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
1405 if (!v)
1406 return FN(PW,free)(pw);
1407 if (!isl_val_is_int(v))
1408 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
1409 "expecting integer value", goto error);
1411 pw = FN(PW,fix_dim)(pw, type, pos, v->n);
1412 isl_val_free(v);
1414 return pw;
1415 error:
1416 isl_val_free(v);
1417 return FN(PW,free)(pw);
1420 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
1422 return pw ? isl_space_dim(pw->dim, type) : 0;
1425 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
1426 enum isl_dim_type type, unsigned first, unsigned n)
1428 int i;
1430 if (!pw)
1431 return NULL;
1432 if (n == 0)
1433 return pw;
1435 if (type == isl_dim_in)
1436 type = isl_dim_set;
1438 pw = FN(PW,cow)(pw);
1439 if (!pw)
1440 return NULL;
1441 if (!pw->dim)
1442 goto error;
1443 for (i = 0; i < pw->n; ++i) {
1444 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
1445 if (!pw->p[i].set)
1446 goto error;
1449 return pw;
1450 error:
1451 FN(PW,free)(pw);
1452 return NULL;
1455 #ifndef NO_OPT
1456 /* Compute the maximal value attained by the piecewise quasipolynomial
1457 * on its domain or zero if the domain is empty.
1458 * In the worst case, the domain is scanned completely,
1459 * so the domain is assumed to be bounded.
1461 __isl_give isl_val *FN(PW,opt)(__isl_take PW *pw, int max)
1463 int i;
1464 isl_val *opt;
1466 if (!pw)
1467 return NULL;
1469 if (pw->n == 0) {
1470 opt = isl_val_zero(FN(PW,get_ctx)(pw));
1471 FN(PW,free)(pw);
1472 return opt;
1475 opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
1476 isl_set_copy(pw->p[0].set), max);
1477 for (i = 1; i < pw->n; ++i) {
1478 isl_val *opt_i;
1479 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
1480 isl_set_copy(pw->p[i].set), max);
1481 if (max)
1482 opt = isl_val_max(opt, opt_i);
1483 else
1484 opt = isl_val_min(opt, opt_i);
1487 FN(PW,free)(pw);
1488 return opt;
1491 __isl_give isl_val *FN(PW,max)(__isl_take PW *pw)
1493 return FN(PW,opt)(pw, 1);
1496 __isl_give isl_val *FN(PW,min)(__isl_take PW *pw)
1498 return FN(PW,opt)(pw, 0);
1500 #endif
1502 /* Return the space of "pw".
1504 __isl_keep isl_space *FN(PW,peek_space)(__isl_keep PW *pw)
1506 return pw ? pw->dim : NULL;
1509 __isl_give isl_space *FN(PW,get_space)(__isl_keep PW *pw)
1511 return isl_space_copy(FN(PW,peek_space)(pw));
1514 __isl_give isl_space *FN(PW,get_domain_space)(__isl_keep PW *pw)
1516 return pw ? isl_space_domain(isl_space_copy(pw->dim)) : NULL;
1519 /* Return the position of the dimension of the given type and name
1520 * in "pw".
1521 * Return -1 if no such dimension can be found.
1523 int FN(PW,find_dim_by_name)(__isl_keep PW *pw,
1524 enum isl_dim_type type, const char *name)
1526 if (!pw)
1527 return -1;
1528 return isl_space_find_dim_by_name(pw->dim, type, name);
1531 #ifndef NO_RESET_DIM
1532 /* Reset the space of "pw". Since we don't know if the elements
1533 * represent the spaces themselves or their domains, we pass along
1534 * both when we call their reset_space_and_domain.
1536 static __isl_give PW *FN(PW,reset_space_and_domain)(__isl_take PW *pw,
1537 __isl_take isl_space *space, __isl_take isl_space *domain)
1539 int i;
1541 pw = FN(PW,cow)(pw);
1542 if (!pw || !space || !domain)
1543 goto error;
1545 for (i = 0; i < pw->n; ++i) {
1546 pw->p[i].set = isl_set_reset_space(pw->p[i].set,
1547 isl_space_copy(domain));
1548 if (!pw->p[i].set)
1549 goto error;
1550 pw->p[i].FIELD = FN(EL,reset_space_and_domain)(pw->p[i].FIELD,
1551 isl_space_copy(space), isl_space_copy(domain));
1552 if (!pw->p[i].FIELD)
1553 goto error;
1556 isl_space_free(domain);
1558 isl_space_free(pw->dim);
1559 pw->dim = space;
1561 return pw;
1562 error:
1563 isl_space_free(domain);
1564 isl_space_free(space);
1565 FN(PW,free)(pw);
1566 return NULL;
1569 __isl_give PW *FN(PW,reset_domain_space)(__isl_take PW *pw,
1570 __isl_take isl_space *domain)
1572 isl_space *space;
1574 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
1575 FN(PW,get_space)(pw));
1576 return FN(PW,reset_space_and_domain)(pw, space, domain);
1579 __isl_give PW *FN(PW,reset_space)(__isl_take PW *pw, __isl_take isl_space *dim)
1581 isl_space *domain;
1583 domain = isl_space_domain(isl_space_copy(dim));
1584 return FN(PW,reset_space_and_domain)(pw, dim, domain);
1587 __isl_give PW *FN(PW,set_tuple_id)(__isl_take PW *pw, enum isl_dim_type type,
1588 __isl_take isl_id *id)
1590 isl_space *space;
1592 pw = FN(PW,cow)(pw);
1593 if (!pw)
1594 goto error;
1596 space = FN(PW,get_space)(pw);
1597 space = isl_space_set_tuple_id(space, type, id);
1599 return FN(PW,reset_space)(pw, space);
1600 error:
1601 isl_id_free(id);
1602 return FN(PW,free)(pw);
1605 /* Drop the id on the specified tuple.
1607 __isl_give PW *FN(PW,reset_tuple_id)(__isl_take PW *pw, enum isl_dim_type type)
1609 isl_space *space;
1611 if (!pw)
1612 return NULL;
1613 if (!FN(PW,has_tuple_id)(pw, type))
1614 return pw;
1616 pw = FN(PW,cow)(pw);
1617 if (!pw)
1618 return NULL;
1620 space = FN(PW,get_space)(pw);
1621 space = isl_space_reset_tuple_id(space, type);
1623 return FN(PW,reset_space)(pw, space);
1626 __isl_give PW *FN(PW,set_dim_id)(__isl_take PW *pw,
1627 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1629 pw = FN(PW,cow)(pw);
1630 if (!pw)
1631 goto error;
1632 pw->dim = isl_space_set_dim_id(pw->dim, type, pos, id);
1633 return FN(PW,reset_space)(pw, isl_space_copy(pw->dim));
1634 error:
1635 isl_id_free(id);
1636 return FN(PW,free)(pw);
1638 #endif
1640 /* Reset the user pointer on all identifiers of parameters and tuples
1641 * of the space of "pw".
1643 __isl_give PW *FN(PW,reset_user)(__isl_take PW *pw)
1645 isl_space *space;
1647 space = FN(PW,get_space)(pw);
1648 space = isl_space_reset_user(space);
1650 return FN(PW,reset_space)(pw, space);
1653 isl_bool FN(PW,has_equal_space)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1655 if (!pw1 || !pw2)
1656 return isl_bool_error;
1658 return isl_space_is_equal(pw1->dim, pw2->dim);
1661 #ifndef NO_MORPH
1662 __isl_give PW *FN(PW,morph_domain)(__isl_take PW *pw,
1663 __isl_take isl_morph *morph)
1665 int i;
1666 isl_ctx *ctx;
1668 if (!pw || !morph)
1669 goto error;
1671 ctx = isl_space_get_ctx(pw->dim);
1672 isl_assert(ctx, isl_space_is_domain_internal(morph->dom->dim, pw->dim),
1673 goto error);
1675 pw = FN(PW,cow)(pw);
1676 if (!pw)
1677 goto error;
1678 pw->dim = isl_space_extend_domain_with_range(
1679 isl_space_copy(morph->ran->dim), pw->dim);
1680 if (!pw->dim)
1681 goto error;
1683 for (i = 0; i < pw->n; ++i) {
1684 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
1685 if (!pw->p[i].set)
1686 goto error;
1687 pw->p[i].FIELD = FN(EL,morph_domain)(pw->p[i].FIELD,
1688 isl_morph_copy(morph));
1689 if (!pw->p[i].FIELD)
1690 goto error;
1693 isl_morph_free(morph);
1695 return pw;
1696 error:
1697 FN(PW,free)(pw);
1698 isl_morph_free(morph);
1699 return NULL;
1701 #endif
1703 int FN(PW,n_piece)(__isl_keep PW *pw)
1705 return pw ? pw->n : 0;
1708 isl_stat FN(PW,foreach_piece)(__isl_keep PW *pw,
1709 isl_stat (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
1710 void *user)
1712 int i;
1714 if (!pw)
1715 return isl_stat_error;
1717 for (i = 0; i < pw->n; ++i)
1718 if (fn(isl_set_copy(pw->p[i].set),
1719 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
1720 return isl_stat_error;
1722 return isl_stat_ok;
1725 #ifndef NO_LIFT
1726 static isl_bool any_divs(__isl_keep isl_set *set)
1728 int i;
1730 if (!set)
1731 return isl_bool_error;
1733 for (i = 0; i < set->n; ++i)
1734 if (set->p[i]->n_div > 0)
1735 return isl_bool_true;
1737 return isl_bool_false;
1740 static isl_stat foreach_lifted_subset(__isl_take isl_set *set,
1741 __isl_take EL *el,
1742 isl_stat (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1743 void *user), void *user)
1745 int i;
1747 if (!set || !el)
1748 goto error;
1750 for (i = 0; i < set->n; ++i) {
1751 isl_set *lift;
1752 EL *copy;
1754 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
1755 lift = isl_set_lift(lift);
1757 copy = FN(EL,copy)(el);
1758 copy = FN(EL,lift)(copy, isl_set_get_space(lift));
1760 if (fn(lift, copy, user) < 0)
1761 goto error;
1764 isl_set_free(set);
1765 FN(EL,free)(el);
1767 return isl_stat_ok;
1768 error:
1769 isl_set_free(set);
1770 FN(EL,free)(el);
1771 return isl_stat_error;
1774 isl_stat FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
1775 isl_stat (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1776 void *user), void *user)
1778 int i;
1780 if (!pw)
1781 return isl_stat_error;
1783 for (i = 0; i < pw->n; ++i) {
1784 isl_bool any;
1785 isl_set *set;
1786 EL *el;
1788 any = any_divs(pw->p[i].set);
1789 if (any < 0)
1790 return isl_stat_error;
1791 set = isl_set_copy(pw->p[i].set);
1792 el = FN(EL,copy)(pw->p[i].FIELD);
1793 if (!any) {
1794 if (fn(set, el, user) < 0)
1795 return isl_stat_error;
1796 continue;
1798 if (foreach_lifted_subset(set, el, fn, user) < 0)
1799 return isl_stat_error;
1802 return isl_stat_ok;
1804 #endif
1806 #ifndef NO_MOVE_DIMS
1807 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
1808 enum isl_dim_type dst_type, unsigned dst_pos,
1809 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1811 int i;
1813 pw = FN(PW,cow)(pw);
1814 if (!pw)
1815 return NULL;
1817 pw->dim = isl_space_move_dims(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
1818 if (!pw->dim)
1819 goto error;
1821 for (i = 0; i < pw->n; ++i) {
1822 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
1823 dst_type, dst_pos, src_type, src_pos, n);
1824 if (!pw->p[i].FIELD)
1825 goto error;
1828 if (dst_type == isl_dim_in)
1829 dst_type = isl_dim_set;
1830 if (src_type == isl_dim_in)
1831 src_type = isl_dim_set;
1833 for (i = 0; i < pw->n; ++i) {
1834 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
1835 dst_type, dst_pos,
1836 src_type, src_pos, n);
1837 if (!pw->p[i].set)
1838 goto error;
1841 return pw;
1842 error:
1843 FN(PW,free)(pw);
1844 return NULL;
1846 #endif
1848 __isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
1850 int i;
1852 if (isl_int_is_one(v))
1853 return pw;
1854 if (pw && DEFAULT_IS_ZERO && isl_int_is_zero(v)) {
1855 PW *zero;
1856 isl_space *dim = FN(PW,get_space)(pw);
1857 #ifdef HAS_TYPE
1858 zero = FN(PW,ZERO)(dim, pw->type);
1859 #else
1860 zero = FN(PW,ZERO)(dim);
1861 #endif
1862 FN(PW,free)(pw);
1863 return zero;
1865 pw = FN(PW,cow)(pw);
1866 if (!pw)
1867 return NULL;
1868 if (pw->n == 0)
1869 return pw;
1871 #ifdef HAS_TYPE
1872 if (isl_int_is_neg(v))
1873 pw->type = isl_fold_type_negate(pw->type);
1874 #endif
1875 for (i = 0; i < pw->n; ++i) {
1876 pw->p[i].FIELD = FN(EL,scale)(pw->p[i].FIELD, v);
1877 if (!pw->p[i].FIELD)
1878 goto error;
1881 return pw;
1882 error:
1883 FN(PW,free)(pw);
1884 return NULL;
1887 /* Multiply the pieces of "pw" by "v" and return the result.
1889 __isl_give PW *FN(PW,scale_val)(__isl_take PW *pw, __isl_take isl_val *v)
1891 int i;
1893 if (!pw || !v)
1894 goto error;
1896 if (isl_val_is_one(v)) {
1897 isl_val_free(v);
1898 return pw;
1900 if (pw && DEFAULT_IS_ZERO && isl_val_is_zero(v)) {
1901 PW *zero;
1902 isl_space *space = FN(PW,get_space)(pw);
1903 #ifdef HAS_TYPE
1904 zero = FN(PW,ZERO)(space, pw->type);
1905 #else
1906 zero = FN(PW,ZERO)(space);
1907 #endif
1908 FN(PW,free)(pw);
1909 isl_val_free(v);
1910 return zero;
1912 if (pw->n == 0) {
1913 isl_val_free(v);
1914 return pw;
1916 pw = FN(PW,cow)(pw);
1917 if (!pw)
1918 goto error;
1920 #ifdef HAS_TYPE
1921 if (isl_val_is_neg(v))
1922 pw->type = isl_fold_type_negate(pw->type);
1923 #endif
1924 for (i = 0; i < pw->n; ++i) {
1925 pw->p[i].FIELD = FN(EL,scale_val)(pw->p[i].FIELD,
1926 isl_val_copy(v));
1927 if (!pw->p[i].FIELD)
1928 goto error;
1931 isl_val_free(v);
1932 return pw;
1933 error:
1934 isl_val_free(v);
1935 FN(PW,free)(pw);
1936 return NULL;
1939 /* Divide the pieces of "pw" by "v" and return the result.
1941 __isl_give PW *FN(PW,scale_down_val)(__isl_take PW *pw, __isl_take isl_val *v)
1943 int i;
1945 if (!pw || !v)
1946 goto error;
1948 if (isl_val_is_one(v)) {
1949 isl_val_free(v);
1950 return pw;
1953 if (!isl_val_is_rat(v))
1954 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1955 "expecting rational factor", goto error);
1956 if (isl_val_is_zero(v))
1957 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1958 "cannot scale down by zero", goto error);
1960 if (pw->n == 0) {
1961 isl_val_free(v);
1962 return pw;
1964 pw = FN(PW,cow)(pw);
1965 if (!pw)
1966 goto error;
1968 #ifdef HAS_TYPE
1969 if (isl_val_is_neg(v))
1970 pw->type = isl_fold_type_negate(pw->type);
1971 #endif
1972 for (i = 0; i < pw->n; ++i) {
1973 pw->p[i].FIELD = FN(EL,scale_down_val)(pw->p[i].FIELD,
1974 isl_val_copy(v));
1975 if (!pw->p[i].FIELD)
1976 goto error;
1979 isl_val_free(v);
1980 return pw;
1981 error:
1982 isl_val_free(v);
1983 FN(PW,free)(pw);
1984 return NULL;
1987 __isl_give PW *FN(PW,scale)(__isl_take PW *pw, isl_int v)
1989 return FN(PW,mul_isl_int)(pw, v);
1992 /* Apply some normalization to "pw".
1993 * In particular, sort the pieces according to their function value
1994 * expressions, combining pairs of adjacent pieces with
1995 * the same such expression, and then normalize the domains of the pieces.
1997 * We normalize in place, but if anything goes wrong we need
1998 * to return NULL, so we need to make sure we don't change the
1999 * meaning of any possible other copies of "pw".
2001 __isl_give PW *FN(PW,normalize)(__isl_take PW *pw)
2003 int i;
2004 isl_set *set;
2006 pw = FN(PW,sort)(pw);
2007 if (!pw)
2008 return NULL;
2009 for (i = 0; i < pw->n; ++i) {
2010 set = isl_set_normalize(isl_set_copy(pw->p[i].set));
2011 if (!set)
2012 return FN(PW,free)(pw);
2013 isl_set_free(pw->p[i].set);
2014 pw->p[i].set = set;
2017 return pw;
2020 /* Is pw1 obviously equal to pw2?
2021 * That is, do they have obviously identical cells and obviously identical
2022 * elements on each cell?
2024 * If "pw1" or "pw2" contain any NaNs, then they are considered
2025 * not to be the same. A NaN is not equal to anything, not even
2026 * to another NaN.
2028 isl_bool FN(PW,plain_is_equal)(__isl_keep PW *pw1, __isl_keep PW *pw2)
2030 int i;
2031 isl_bool equal, has_nan;
2033 if (!pw1 || !pw2)
2034 return isl_bool_error;
2036 has_nan = FN(PW,involves_nan)(pw1);
2037 if (has_nan >= 0 && !has_nan)
2038 has_nan = FN(PW,involves_nan)(pw2);
2039 if (has_nan < 0 || has_nan)
2040 return isl_bool_not(has_nan);
2042 if (pw1 == pw2)
2043 return isl_bool_true;
2044 if (!isl_space_is_equal(pw1->dim, pw2->dim))
2045 return isl_bool_false;
2047 pw1 = FN(PW,copy)(pw1);
2048 pw2 = FN(PW,copy)(pw2);
2049 pw1 = FN(PW,normalize)(pw1);
2050 pw2 = FN(PW,normalize)(pw2);
2051 if (!pw1 || !pw2)
2052 goto error;
2054 equal = pw1->n == pw2->n;
2055 for (i = 0; equal && i < pw1->n; ++i) {
2056 equal = isl_set_plain_is_equal(pw1->p[i].set, pw2->p[i].set);
2057 if (equal < 0)
2058 goto error;
2059 if (!equal)
2060 break;
2061 equal = FN(EL,plain_is_equal)(pw1->p[i].FIELD, pw2->p[i].FIELD);
2062 if (equal < 0)
2063 goto error;
2066 FN(PW,free)(pw1);
2067 FN(PW,free)(pw2);
2068 return equal;
2069 error:
2070 FN(PW,free)(pw1);
2071 FN(PW,free)(pw2);
2072 return isl_bool_error;
2075 /* Does "pw" involve any NaNs?
2077 isl_bool FN(PW,involves_nan)(__isl_keep PW *pw)
2079 int i;
2081 if (!pw)
2082 return isl_bool_error;
2083 if (pw->n == 0)
2084 return isl_bool_false;
2086 for (i = 0; i < pw->n; ++i) {
2087 isl_bool has_nan = FN(EL,involves_nan)(pw->p[i].FIELD);
2088 if (has_nan < 0 || has_nan)
2089 return has_nan;
2092 return isl_bool_false;
2095 #ifndef NO_PULLBACK
2096 static __isl_give PW *FN(PW,align_params_pw_multi_aff_and)(__isl_take PW *pw,
2097 __isl_take isl_multi_aff *ma,
2098 __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_multi_aff *ma))
2100 isl_ctx *ctx;
2101 isl_bool equal_params;
2102 isl_space *ma_space;
2104 ma_space = isl_multi_aff_get_space(ma);
2105 if (!pw || !ma || !ma_space)
2106 goto error;
2107 equal_params = isl_space_has_equal_params(pw->dim, ma_space);
2108 if (equal_params < 0)
2109 goto error;
2110 if (equal_params) {
2111 isl_space_free(ma_space);
2112 return fn(pw, ma);
2114 ctx = FN(PW,get_ctx)(pw);
2115 if (!isl_space_has_named_params(pw->dim) ||
2116 !isl_space_has_named_params(ma_space))
2117 isl_die(ctx, isl_error_invalid,
2118 "unaligned unnamed parameters", goto error);
2119 pw = FN(PW,align_params)(pw, ma_space);
2120 ma = isl_multi_aff_align_params(ma, FN(PW,get_space)(pw));
2121 return fn(pw, ma);
2122 error:
2123 isl_space_free(ma_space);
2124 FN(PW,free)(pw);
2125 isl_multi_aff_free(ma);
2126 return NULL;
2129 static __isl_give PW *FN(PW,align_params_pw_pw_multi_aff_and)(__isl_take PW *pw,
2130 __isl_take isl_pw_multi_aff *pma,
2131 __isl_give PW *(*fn)(__isl_take PW *pw,
2132 __isl_take isl_pw_multi_aff *ma))
2134 isl_ctx *ctx;
2135 isl_bool equal_params;
2136 isl_space *pma_space;
2138 pma_space = isl_pw_multi_aff_get_space(pma);
2139 if (!pw || !pma || !pma_space)
2140 goto error;
2141 equal_params = isl_space_has_equal_params(pw->dim, pma_space);
2142 if (equal_params < 0)
2143 goto error;
2144 if (equal_params) {
2145 isl_space_free(pma_space);
2146 return fn(pw, pma);
2148 ctx = FN(PW,get_ctx)(pw);
2149 if (!isl_space_has_named_params(pw->dim) ||
2150 !isl_space_has_named_params(pma_space))
2151 isl_die(ctx, isl_error_invalid,
2152 "unaligned unnamed parameters", goto error);
2153 pw = FN(PW,align_params)(pw, pma_space);
2154 pma = isl_pw_multi_aff_align_params(pma, FN(PW,get_space)(pw));
2155 return fn(pw, pma);
2156 error:
2157 isl_space_free(pma_space);
2158 FN(PW,free)(pw);
2159 isl_pw_multi_aff_free(pma);
2160 return NULL;
2163 /* Compute the pullback of "pw" by the function represented by "ma".
2164 * In other words, plug in "ma" in "pw".
2166 static __isl_give PW *FN(PW,pullback_multi_aff_aligned)(__isl_take PW *pw,
2167 __isl_take isl_multi_aff *ma)
2169 int i;
2170 isl_space *space = NULL;
2172 ma = isl_multi_aff_align_divs(ma);
2173 pw = FN(PW,cow)(pw);
2174 if (!pw || !ma)
2175 goto error;
2177 space = isl_space_join(isl_multi_aff_get_space(ma),
2178 FN(PW,get_space)(pw));
2180 for (i = 0; i < pw->n; ++i) {
2181 pw->p[i].set = isl_set_preimage_multi_aff(pw->p[i].set,
2182 isl_multi_aff_copy(ma));
2183 if (!pw->p[i].set)
2184 goto error;
2185 pw->p[i].FIELD = FN(EL,pullback_multi_aff)(pw->p[i].FIELD,
2186 isl_multi_aff_copy(ma));
2187 if (!pw->p[i].FIELD)
2188 goto error;
2191 pw = FN(PW,reset_space)(pw, space);
2192 isl_multi_aff_free(ma);
2193 return pw;
2194 error:
2195 isl_space_free(space);
2196 isl_multi_aff_free(ma);
2197 FN(PW,free)(pw);
2198 return NULL;
2201 __isl_give PW *FN(PW,pullback_multi_aff)(__isl_take PW *pw,
2202 __isl_take isl_multi_aff *ma)
2204 return FN(PW,align_params_pw_multi_aff_and)(pw, ma,
2205 &FN(PW,pullback_multi_aff_aligned));
2208 /* Compute the pullback of "pw" by the function represented by "pma".
2209 * In other words, plug in "pma" in "pw".
2211 static __isl_give PW *FN(PW,pullback_pw_multi_aff_aligned)(__isl_take PW *pw,
2212 __isl_take isl_pw_multi_aff *pma)
2214 int i;
2215 PW *res;
2217 if (!pma)
2218 goto error;
2220 if (pma->n == 0) {
2221 isl_space *space;
2222 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
2223 FN(PW,get_space)(pw));
2224 isl_pw_multi_aff_free(pma);
2225 res = FN(PW,empty)(space);
2226 FN(PW,free)(pw);
2227 return res;
2230 res = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
2231 isl_multi_aff_copy(pma->p[0].maff));
2232 res = FN(PW,intersect_domain)(res, isl_set_copy(pma->p[0].set));
2234 for (i = 1; i < pma->n; ++i) {
2235 PW *res_i;
2237 res_i = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
2238 isl_multi_aff_copy(pma->p[i].maff));
2239 res_i = FN(PW,intersect_domain)(res_i,
2240 isl_set_copy(pma->p[i].set));
2241 res = FN(PW,add_disjoint)(res, res_i);
2244 isl_pw_multi_aff_free(pma);
2245 FN(PW,free)(pw);
2246 return res;
2247 error:
2248 isl_pw_multi_aff_free(pma);
2249 FN(PW,free)(pw);
2250 return NULL;
2253 __isl_give PW *FN(PW,pullback_pw_multi_aff)(__isl_take PW *pw,
2254 __isl_take isl_pw_multi_aff *pma)
2256 return FN(PW,align_params_pw_pw_multi_aff_and)(pw, pma,
2257 &FN(PW,pullback_pw_multi_aff_aligned));
2259 #endif