isl_local_space_divs_known: extract out isl_local_divs_known
[isl.git] / isl_pw_templ.c
blob6b6b5ad33d9386ea292a670bfa710fffc7142934
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 /* Evaluate "pw" in the void point "pnt".
704 * In particular, return the value NaN.
706 static __isl_give isl_val *FN(PW,eval_void)(__isl_take PW *pw,
707 __isl_take isl_point *pnt)
709 isl_ctx *ctx;
711 ctx = isl_point_get_ctx(pnt);
712 FN(PW,free)(pw);
713 isl_point_free(pnt);
714 return isl_val_nan(ctx);
717 __isl_give isl_val *FN(PW,eval)(__isl_take PW *pw, __isl_take isl_point *pnt)
719 int i;
720 isl_bool is_void;
721 int found = 0;
722 isl_ctx *ctx;
723 isl_space *pnt_dim = NULL;
724 isl_val *v;
726 if (!pw || !pnt)
727 goto error;
728 ctx = isl_point_get_ctx(pnt);
729 pnt_dim = isl_point_get_space(pnt);
730 isl_assert(ctx, isl_space_is_domain_internal(pnt_dim, pw->dim),
731 goto error);
732 is_void = isl_point_is_void(pnt);
733 if (is_void < 0)
734 goto error;
735 if (is_void)
736 return FN(PW,eval_void)(pw, pnt);
738 for (i = 0; i < pw->n; ++i) {
739 found = isl_set_contains_point(pw->p[i].set, pnt);
740 if (found < 0)
741 goto error;
742 if (found)
743 break;
745 if (found)
746 v = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
747 isl_point_copy(pnt));
748 else
749 v = isl_val_zero(ctx);
750 FN(PW,free)(pw);
751 isl_space_free(pnt_dim);
752 isl_point_free(pnt);
753 return v;
754 error:
755 FN(PW,free)(pw);
756 isl_space_free(pnt_dim);
757 isl_point_free(pnt);
758 return NULL;
760 #endif
762 /* Return the parameter domain of "pw".
764 __isl_give isl_set *FN(PW,params)(__isl_take PW *pw)
766 return isl_set_params(FN(PW,domain)(pw));
769 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
771 int i;
772 isl_set *dom;
774 if (!pw)
775 return NULL;
777 dom = isl_set_empty(FN(PW,get_domain_space)(pw));
778 for (i = 0; i < pw->n; ++i)
779 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
781 FN(PW,free)(pw);
783 return dom;
786 /* Exploit the equalities in the domain of piece "i" of "pw"
787 * to simplify the associated function.
788 * If the domain of piece "i" is empty, then remove it entirely,
789 * replacing it with the final piece.
791 static int FN(PW,exploit_equalities_and_remove_if_empty)(__isl_keep PW *pw,
792 int i)
794 isl_basic_set *aff;
795 int empty = isl_set_plain_is_empty(pw->p[i].set);
797 if (empty < 0)
798 return -1;
799 if (empty) {
800 isl_set_free(pw->p[i].set);
801 FN(EL,free)(pw->p[i].FIELD);
802 if (i != pw->n - 1)
803 pw->p[i] = pw->p[pw->n - 1];
804 pw->n--;
806 return 0;
809 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
810 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD, aff);
811 if (!pw->p[i].FIELD)
812 return -1;
814 return 0;
817 /* Convert a piecewise expression defined over a parameter domain
818 * into one that is defined over a zero-dimensional set.
820 __isl_give PW *FN(PW,from_range)(__isl_take PW *pw)
822 isl_space *space;
824 if (!pw)
825 return NULL;
826 if (!isl_space_is_set(pw->dim))
827 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
828 "not living in a set space", return FN(PW,free)(pw));
830 space = FN(PW,get_space)(pw);
831 space = isl_space_from_range(space);
832 pw = FN(PW,reset_space)(pw, space);
834 return pw;
837 /* Fix the value of the given parameter or domain dimension of "pw"
838 * to be equal to "value".
840 __isl_give PW *FN(PW,fix_si)(__isl_take PW *pw, enum isl_dim_type type,
841 unsigned pos, int value)
843 int i;
845 if (!pw)
846 return NULL;
848 if (type == isl_dim_out)
849 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
850 "cannot fix output dimension", return FN(PW,free)(pw));
852 if (pw->n == 0)
853 return pw;
855 if (type == isl_dim_in)
856 type = isl_dim_set;
858 pw = FN(PW,cow)(pw);
859 if (!pw)
860 return FN(PW,free)(pw);
862 for (i = pw->n - 1; i >= 0; --i) {
863 pw->p[i].set = isl_set_fix_si(pw->p[i].set, type, pos, value);
864 if (FN(PW,exploit_equalities_and_remove_if_empty)(pw, i) < 0)
865 return FN(PW,free)(pw);
868 return pw;
871 /* Restrict the domain of "pw" by combining each cell
872 * with "set" through a call to "fn", where "fn" may be
873 * isl_set_intersect, isl_set_intersect_params or isl_set_subtract.
875 static __isl_give PW *FN(PW,restrict_domain_aligned)(__isl_take PW *pw,
876 __isl_take isl_set *set,
877 __isl_give isl_set *(*fn)(__isl_take isl_set *set1,
878 __isl_take isl_set *set2))
880 int i;
882 if (!pw || !set)
883 goto error;
885 if (pw->n == 0) {
886 isl_set_free(set);
887 return pw;
890 pw = FN(PW,cow)(pw);
891 if (!pw)
892 goto error;
894 for (i = pw->n - 1; i >= 0; --i) {
895 pw->p[i].set = fn(pw->p[i].set, isl_set_copy(set));
896 if (FN(PW,exploit_equalities_and_remove_if_empty)(pw, i) < 0)
897 goto error;
900 isl_set_free(set);
901 return pw;
902 error:
903 isl_set_free(set);
904 FN(PW,free)(pw);
905 return NULL;
908 static __isl_give PW *FN(PW,intersect_domain_aligned)(__isl_take PW *pw,
909 __isl_take isl_set *set)
911 return FN(PW,restrict_domain_aligned)(pw, set, &isl_set_intersect);
914 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw,
915 __isl_take isl_set *context)
917 return FN(PW,align_params_pw_set_and)(pw, context,
918 &FN(PW,intersect_domain_aligned));
921 static __isl_give PW *FN(PW,intersect_params_aligned)(__isl_take PW *pw,
922 __isl_take isl_set *set)
924 return FN(PW,restrict_domain_aligned)(pw, set,
925 &isl_set_intersect_params);
928 /* Intersect the domain of "pw" with the parameter domain "context".
930 __isl_give PW *FN(PW,intersect_params)(__isl_take PW *pw,
931 __isl_take isl_set *context)
933 return FN(PW,align_params_pw_set_and)(pw, context,
934 &FN(PW,intersect_params_aligned));
937 /* Subtract "domain' from the domain of "pw", assuming their
938 * parameters have been aligned.
940 static __isl_give PW *FN(PW,subtract_domain_aligned)(__isl_take PW *pw,
941 __isl_take isl_set *domain)
943 return FN(PW,restrict_domain_aligned)(pw, domain, &isl_set_subtract);
946 /* Subtract "domain' from the domain of "pw".
948 __isl_give PW *FN(PW,subtract_domain)(__isl_take PW *pw,
949 __isl_take isl_set *domain)
951 return FN(PW,align_params_pw_set_and)(pw, domain,
952 &FN(PW,subtract_domain_aligned));
955 /* Compute the gist of "pw" with respect to the domain constraints
956 * of "context" for the case where the domain of the last element
957 * of "pw" is equal to "context".
958 * Call "fn_el" to compute the gist of this element, replace
959 * its domain by the universe and drop all other elements
960 * as their domains are necessarily disjoint from "context".
962 static __isl_give PW *FN(PW,gist_last)(__isl_take PW *pw,
963 __isl_take isl_set *context,
964 __isl_give EL *(*fn_el)(__isl_take EL *el, __isl_take isl_set *set))
966 int i;
967 isl_space *space;
969 for (i = 0; i < pw->n - 1; ++i) {
970 isl_set_free(pw->p[i].set);
971 FN(EL,free)(pw->p[i].FIELD);
973 pw->p[0].FIELD = pw->p[pw->n - 1].FIELD;
974 pw->p[0].set = pw->p[pw->n - 1].set;
975 pw->n = 1;
977 space = isl_set_get_space(context);
978 pw->p[0].FIELD = fn_el(pw->p[0].FIELD, context);
979 context = isl_set_universe(space);
980 isl_set_free(pw->p[0].set);
981 pw->p[0].set = context;
983 if (!pw->p[0].FIELD || !pw->p[0].set)
984 return FN(PW,free)(pw);
986 return pw;
989 /* Compute the gist of "pw" with respect to the domain constraints
990 * of "context". Call "fn_el" to compute the gist of the elements
991 * and "fn_dom" to compute the gist of the domains.
993 * If the piecewise expression is empty or the context is the universe,
994 * then nothing can be simplified.
996 static __isl_give PW *FN(PW,gist_aligned)(__isl_take PW *pw,
997 __isl_take isl_set *context,
998 __isl_give EL *(*fn_el)(__isl_take EL *el,
999 __isl_take isl_set *set),
1000 __isl_give isl_set *(*fn_dom)(__isl_take isl_set *set,
1001 __isl_take isl_basic_set *bset))
1003 int i;
1004 int is_universe;
1005 isl_bool aligned;
1006 isl_basic_set *hull = NULL;
1008 if (!pw || !context)
1009 goto error;
1011 if (pw->n == 0) {
1012 isl_set_free(context);
1013 return pw;
1016 is_universe = isl_set_plain_is_universe(context);
1017 if (is_universe < 0)
1018 goto error;
1019 if (is_universe) {
1020 isl_set_free(context);
1021 return pw;
1024 aligned = isl_set_space_has_equal_params(context, pw->dim);
1025 if (aligned < 0)
1026 goto error;
1027 if (!aligned) {
1028 pw = FN(PW,align_params)(pw, isl_set_get_space(context));
1029 context = isl_set_align_params(context, FN(PW,get_space)(pw));
1032 pw = FN(PW,cow)(pw);
1033 if (!pw)
1034 goto error;
1036 if (pw->n == 1) {
1037 int equal;
1039 equal = isl_set_plain_is_equal(pw->p[0].set, context);
1040 if (equal < 0)
1041 goto error;
1042 if (equal)
1043 return FN(PW,gist_last)(pw, context, fn_el);
1046 context = isl_set_compute_divs(context);
1047 hull = isl_set_simple_hull(isl_set_copy(context));
1049 for (i = pw->n - 1; i >= 0; --i) {
1050 isl_set *set_i;
1051 int empty;
1053 if (i == pw->n - 1) {
1054 int equal;
1055 equal = isl_set_plain_is_equal(pw->p[i].set, context);
1056 if (equal < 0)
1057 goto error;
1058 if (equal) {
1059 isl_basic_set_free(hull);
1060 return FN(PW,gist_last)(pw, context, fn_el);
1063 set_i = isl_set_intersect(isl_set_copy(pw->p[i].set),
1064 isl_set_copy(context));
1065 empty = isl_set_plain_is_empty(set_i);
1066 pw->p[i].FIELD = fn_el(pw->p[i].FIELD, set_i);
1067 pw->p[i].set = fn_dom(pw->p[i].set, isl_basic_set_copy(hull));
1068 if (empty < 0 || !pw->p[i].FIELD || !pw->p[i].set)
1069 goto error;
1070 if (empty) {
1071 isl_set_free(pw->p[i].set);
1072 FN(EL,free)(pw->p[i].FIELD);
1073 if (i != pw->n - 1)
1074 pw->p[i] = pw->p[pw->n - 1];
1075 pw->n--;
1079 isl_basic_set_free(hull);
1080 isl_set_free(context);
1082 return pw;
1083 error:
1084 FN(PW,free)(pw);
1085 isl_basic_set_free(hull);
1086 isl_set_free(context);
1087 return NULL;
1090 static __isl_give PW *FN(PW,gist_domain_aligned)(__isl_take PW *pw,
1091 __isl_take isl_set *set)
1093 return FN(PW,gist_aligned)(pw, set, &FN(EL,gist),
1094 &isl_set_gist_basic_set);
1097 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
1099 return FN(PW,align_params_pw_set_and)(pw, context,
1100 &FN(PW,gist_domain_aligned));
1103 static __isl_give PW *FN(PW,gist_params_aligned)(__isl_take PW *pw,
1104 __isl_take isl_set *set)
1106 return FN(PW,gist_aligned)(pw, set, &FN(EL,gist_params),
1107 &isl_set_gist_params_basic_set);
1110 __isl_give PW *FN(PW,gist_params)(__isl_take PW *pw,
1111 __isl_take isl_set *context)
1113 return FN(PW,align_params_pw_set_and)(pw, context,
1114 &FN(PW,gist_params_aligned));
1117 /* Return -1 if the piece "p1" should be sorted before "p2"
1118 * and 1 if it should be sorted after "p2".
1119 * Return 0 if they do not need to be sorted in a specific order.
1121 * The two pieces are compared on the basis of their function value expressions.
1123 static int FN(PW,sort_field_cmp)(const void *p1, const void *p2, void *arg)
1125 struct FN(PW,piece) const *pc1 = p1;
1126 struct FN(PW,piece) const *pc2 = p2;
1128 return FN(EL,plain_cmp)(pc1->FIELD, pc2->FIELD);
1131 /* Sort the pieces of "pw" according to their function value
1132 * expressions and then combine pairs of adjacent pieces with
1133 * the same such expression.
1135 * The sorting is performed in place because it does not
1136 * change the meaning of "pw", but care needs to be
1137 * taken not to change any possible other copies of "pw"
1138 * in case anything goes wrong.
1140 __isl_give PW *FN(PW,sort)(__isl_take PW *pw)
1142 int i, j;
1143 isl_set *set;
1145 if (!pw)
1146 return NULL;
1147 if (pw->n <= 1)
1148 return pw;
1149 if (isl_sort(pw->p, pw->n, sizeof(pw->p[0]),
1150 &FN(PW,sort_field_cmp), NULL) < 0)
1151 return FN(PW,free)(pw);
1152 for (i = pw->n - 1; i >= 1; --i) {
1153 if (!FN(EL,plain_is_equal)(pw->p[i - 1].FIELD, pw->p[i].FIELD))
1154 continue;
1155 set = isl_set_union(isl_set_copy(pw->p[i - 1].set),
1156 isl_set_copy(pw->p[i].set));
1157 if (!set)
1158 return FN(PW,free)(pw);
1159 isl_set_free(pw->p[i].set);
1160 FN(EL,free)(pw->p[i].FIELD);
1161 isl_set_free(pw->p[i - 1].set);
1162 pw->p[i - 1].set = set;
1163 for (j = i + 1; j < pw->n; ++j)
1164 pw->p[j - 1] = pw->p[j];
1165 pw->n--;
1168 return pw;
1171 /* Coalesce the domains of "pw".
1173 * Prior to the actual coalescing, first sort the pieces such that
1174 * pieces with the same function value expression are combined
1175 * into a single piece, the combined domain of which can then
1176 * be coalesced.
1178 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
1180 int i;
1182 pw = FN(PW,sort)(pw);
1183 if (!pw)
1184 return NULL;
1186 for (i = 0; i < pw->n; ++i) {
1187 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
1188 if (!pw->p[i].set)
1189 goto error;
1192 return pw;
1193 error:
1194 FN(PW,free)(pw);
1195 return NULL;
1198 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
1200 return pw ? isl_space_get_ctx(pw->dim) : NULL;
1203 #ifndef NO_INVOLVES_DIMS
1204 isl_bool FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
1205 unsigned first, unsigned n)
1207 int i;
1208 enum isl_dim_type set_type;
1210 if (!pw)
1211 return isl_bool_error;
1212 if (pw->n == 0 || n == 0)
1213 return isl_bool_false;
1215 set_type = type == isl_dim_in ? isl_dim_set : type;
1217 for (i = 0; i < pw->n; ++i) {
1218 isl_bool involves = FN(EL,involves_dims)(pw->p[i].FIELD,
1219 type, first, n);
1220 if (involves < 0 || involves)
1221 return involves;
1222 involves = isl_set_involves_dims(pw->p[i].set,
1223 set_type, first, n);
1224 if (involves < 0 || involves)
1225 return involves;
1227 return isl_bool_false;
1229 #endif
1231 __isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
1232 enum isl_dim_type type, unsigned pos, const char *s)
1234 int i;
1235 enum isl_dim_type set_type;
1237 pw = FN(PW,cow)(pw);
1238 if (!pw)
1239 return NULL;
1241 set_type = type == isl_dim_in ? isl_dim_set : type;
1243 pw->dim = isl_space_set_dim_name(pw->dim, type, pos, s);
1244 if (!pw->dim)
1245 goto error;
1247 for (i = 0; i < pw->n; ++i) {
1248 pw->p[i].set = isl_set_set_dim_name(pw->p[i].set,
1249 set_type, pos, s);
1250 if (!pw->p[i].set)
1251 goto error;
1252 pw->p[i].FIELD = FN(EL,set_dim_name)(pw->p[i].FIELD, type, pos, s);
1253 if (!pw->p[i].FIELD)
1254 goto error;
1257 return pw;
1258 error:
1259 FN(PW,free)(pw);
1260 return NULL;
1263 #ifndef NO_DROP_DIMS
1264 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
1265 enum isl_dim_type type, unsigned first, unsigned n)
1267 int i;
1268 enum isl_dim_type set_type;
1270 if (!pw)
1271 return NULL;
1272 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
1273 return pw;
1275 set_type = type == isl_dim_in ? isl_dim_set : type;
1277 pw = FN(PW,cow)(pw);
1278 if (!pw)
1279 return NULL;
1280 pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
1281 if (!pw->dim)
1282 goto error;
1283 for (i = 0; i < pw->n; ++i) {
1284 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
1285 if (!pw->p[i].FIELD)
1286 goto error;
1287 if (type == isl_dim_out)
1288 continue;
1289 pw->p[i].set = isl_set_drop(pw->p[i].set, set_type, first, n);
1290 if (!pw->p[i].set)
1291 goto error;
1294 return pw;
1295 error:
1296 FN(PW,free)(pw);
1297 return NULL;
1300 /* This function is very similar to drop_dims.
1301 * The only difference is that the cells may still involve
1302 * the specified dimensions. They are removed using
1303 * isl_set_project_out instead of isl_set_drop.
1305 __isl_give PW *FN(PW,project_out)(__isl_take PW *pw,
1306 enum isl_dim_type type, unsigned first, unsigned n)
1308 int i;
1309 enum isl_dim_type set_type;
1311 if (!pw)
1312 return NULL;
1313 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
1314 return pw;
1316 set_type = type == isl_dim_in ? isl_dim_set : type;
1318 pw = FN(PW,cow)(pw);
1319 if (!pw)
1320 return NULL;
1321 pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
1322 if (!pw->dim)
1323 goto error;
1324 for (i = 0; i < pw->n; ++i) {
1325 pw->p[i].set = isl_set_project_out(pw->p[i].set,
1326 set_type, first, n);
1327 if (!pw->p[i].set)
1328 goto error;
1329 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
1330 if (!pw->p[i].FIELD)
1331 goto error;
1334 return pw;
1335 error:
1336 FN(PW,free)(pw);
1337 return NULL;
1340 /* Project the domain of pw onto its parameter space.
1342 __isl_give PW *FN(PW,project_domain_on_params)(__isl_take PW *pw)
1344 isl_space *space;
1345 unsigned n;
1347 n = FN(PW,dim)(pw, isl_dim_in);
1348 pw = FN(PW,project_out)(pw, isl_dim_in, 0, n);
1349 space = FN(PW,get_domain_space)(pw);
1350 space = isl_space_params(space);
1351 pw = FN(PW,reset_domain_space)(pw, space);
1352 return pw;
1354 #endif
1356 #ifndef NO_INSERT_DIMS
1357 __isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
1358 unsigned first, unsigned n)
1360 int i;
1361 enum isl_dim_type set_type;
1363 if (!pw)
1364 return NULL;
1365 if (n == 0 && !isl_space_is_named_or_nested(pw->dim, type))
1366 return pw;
1368 set_type = type == isl_dim_in ? isl_dim_set : type;
1370 pw = FN(PW,cow)(pw);
1371 if (!pw)
1372 return NULL;
1374 pw->dim = isl_space_insert_dims(pw->dim, type, first, n);
1375 if (!pw->dim)
1376 goto error;
1378 for (i = 0; i < pw->n; ++i) {
1379 pw->p[i].set = isl_set_insert_dims(pw->p[i].set,
1380 set_type, first, n);
1381 if (!pw->p[i].set)
1382 goto error;
1383 pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
1384 type, first, n);
1385 if (!pw->p[i].FIELD)
1386 goto error;
1389 return pw;
1390 error:
1391 FN(PW,free)(pw);
1392 return NULL;
1394 #endif
1396 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
1397 enum isl_dim_type type, unsigned pos, isl_int v)
1399 int i;
1401 if (!pw)
1402 return NULL;
1404 if (type == isl_dim_in)
1405 type = isl_dim_set;
1407 pw = FN(PW,cow)(pw);
1408 if (!pw)
1409 return NULL;
1410 for (i = 0; i < pw->n; ++i) {
1411 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
1412 if (FN(PW,exploit_equalities_and_remove_if_empty)(pw, i) < 0)
1413 return FN(PW,free)(pw);
1416 return pw;
1419 /* Fix the value of the variable at position "pos" of type "type" of "pw"
1420 * to be equal to "v".
1422 __isl_give PW *FN(PW,fix_val)(__isl_take PW *pw,
1423 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
1425 if (!v)
1426 return FN(PW,free)(pw);
1427 if (!isl_val_is_int(v))
1428 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
1429 "expecting integer value", goto error);
1431 pw = FN(PW,fix_dim)(pw, type, pos, v->n);
1432 isl_val_free(v);
1434 return pw;
1435 error:
1436 isl_val_free(v);
1437 return FN(PW,free)(pw);
1440 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
1442 return pw ? isl_space_dim(pw->dim, type) : 0;
1445 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
1446 enum isl_dim_type type, unsigned first, unsigned n)
1448 int i;
1450 if (!pw)
1451 return NULL;
1452 if (n == 0)
1453 return pw;
1455 if (type == isl_dim_in)
1456 type = isl_dim_set;
1458 pw = FN(PW,cow)(pw);
1459 if (!pw)
1460 return NULL;
1461 if (!pw->dim)
1462 goto error;
1463 for (i = 0; i < pw->n; ++i) {
1464 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
1465 if (!pw->p[i].set)
1466 goto error;
1469 return pw;
1470 error:
1471 FN(PW,free)(pw);
1472 return NULL;
1475 #ifndef NO_OPT
1476 /* Compute the maximal value attained by the piecewise quasipolynomial
1477 * on its domain or zero if the domain is empty.
1478 * In the worst case, the domain is scanned completely,
1479 * so the domain is assumed to be bounded.
1481 __isl_give isl_val *FN(PW,opt)(__isl_take PW *pw, int max)
1483 int i;
1484 isl_val *opt;
1486 if (!pw)
1487 return NULL;
1489 if (pw->n == 0) {
1490 opt = isl_val_zero(FN(PW,get_ctx)(pw));
1491 FN(PW,free)(pw);
1492 return opt;
1495 opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
1496 isl_set_copy(pw->p[0].set), max);
1497 for (i = 1; i < pw->n; ++i) {
1498 isl_val *opt_i;
1499 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
1500 isl_set_copy(pw->p[i].set), max);
1501 if (max)
1502 opt = isl_val_max(opt, opt_i);
1503 else
1504 opt = isl_val_min(opt, opt_i);
1507 FN(PW,free)(pw);
1508 return opt;
1511 __isl_give isl_val *FN(PW,max)(__isl_take PW *pw)
1513 return FN(PW,opt)(pw, 1);
1516 __isl_give isl_val *FN(PW,min)(__isl_take PW *pw)
1518 return FN(PW,opt)(pw, 0);
1520 #endif
1522 /* Return the space of "pw".
1524 __isl_keep isl_space *FN(PW,peek_space)(__isl_keep PW *pw)
1526 return pw ? pw->dim : NULL;
1529 __isl_give isl_space *FN(PW,get_space)(__isl_keep PW *pw)
1531 return isl_space_copy(FN(PW,peek_space)(pw));
1534 __isl_give isl_space *FN(PW,get_domain_space)(__isl_keep PW *pw)
1536 return pw ? isl_space_domain(isl_space_copy(pw->dim)) : NULL;
1539 /* Return the position of the dimension of the given type and name
1540 * in "pw".
1541 * Return -1 if no such dimension can be found.
1543 int FN(PW,find_dim_by_name)(__isl_keep PW *pw,
1544 enum isl_dim_type type, const char *name)
1546 if (!pw)
1547 return -1;
1548 return isl_space_find_dim_by_name(pw->dim, type, name);
1551 #ifndef NO_RESET_DIM
1552 /* Reset the space of "pw". Since we don't know if the elements
1553 * represent the spaces themselves or their domains, we pass along
1554 * both when we call their reset_space_and_domain.
1556 static __isl_give PW *FN(PW,reset_space_and_domain)(__isl_take PW *pw,
1557 __isl_take isl_space *space, __isl_take isl_space *domain)
1559 int i;
1561 pw = FN(PW,cow)(pw);
1562 if (!pw || !space || !domain)
1563 goto error;
1565 for (i = 0; i < pw->n; ++i) {
1566 pw->p[i].set = isl_set_reset_space(pw->p[i].set,
1567 isl_space_copy(domain));
1568 if (!pw->p[i].set)
1569 goto error;
1570 pw->p[i].FIELD = FN(EL,reset_space_and_domain)(pw->p[i].FIELD,
1571 isl_space_copy(space), isl_space_copy(domain));
1572 if (!pw->p[i].FIELD)
1573 goto error;
1576 isl_space_free(domain);
1578 isl_space_free(pw->dim);
1579 pw->dim = space;
1581 return pw;
1582 error:
1583 isl_space_free(domain);
1584 isl_space_free(space);
1585 FN(PW,free)(pw);
1586 return NULL;
1589 __isl_give PW *FN(PW,reset_domain_space)(__isl_take PW *pw,
1590 __isl_take isl_space *domain)
1592 isl_space *space;
1594 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
1595 FN(PW,get_space)(pw));
1596 return FN(PW,reset_space_and_domain)(pw, space, domain);
1599 __isl_give PW *FN(PW,reset_space)(__isl_take PW *pw, __isl_take isl_space *dim)
1601 isl_space *domain;
1603 domain = isl_space_domain(isl_space_copy(dim));
1604 return FN(PW,reset_space_and_domain)(pw, dim, domain);
1607 __isl_give PW *FN(PW,set_tuple_id)(__isl_take PW *pw, enum isl_dim_type type,
1608 __isl_take isl_id *id)
1610 isl_space *space;
1612 pw = FN(PW,cow)(pw);
1613 if (!pw)
1614 goto error;
1616 space = FN(PW,get_space)(pw);
1617 space = isl_space_set_tuple_id(space, type, id);
1619 return FN(PW,reset_space)(pw, space);
1620 error:
1621 isl_id_free(id);
1622 return FN(PW,free)(pw);
1625 /* Drop the id on the specified tuple.
1627 __isl_give PW *FN(PW,reset_tuple_id)(__isl_take PW *pw, enum isl_dim_type type)
1629 isl_space *space;
1631 if (!pw)
1632 return NULL;
1633 if (!FN(PW,has_tuple_id)(pw, type))
1634 return pw;
1636 pw = FN(PW,cow)(pw);
1637 if (!pw)
1638 return NULL;
1640 space = FN(PW,get_space)(pw);
1641 space = isl_space_reset_tuple_id(space, type);
1643 return FN(PW,reset_space)(pw, space);
1646 __isl_give PW *FN(PW,set_dim_id)(__isl_take PW *pw,
1647 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1649 pw = FN(PW,cow)(pw);
1650 if (!pw)
1651 goto error;
1652 pw->dim = isl_space_set_dim_id(pw->dim, type, pos, id);
1653 return FN(PW,reset_space)(pw, isl_space_copy(pw->dim));
1654 error:
1655 isl_id_free(id);
1656 return FN(PW,free)(pw);
1658 #endif
1660 /* Reset the user pointer on all identifiers of parameters and tuples
1661 * of the space of "pw".
1663 __isl_give PW *FN(PW,reset_user)(__isl_take PW *pw)
1665 isl_space *space;
1667 space = FN(PW,get_space)(pw);
1668 space = isl_space_reset_user(space);
1670 return FN(PW,reset_space)(pw, space);
1673 isl_bool FN(PW,has_equal_space)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1675 if (!pw1 || !pw2)
1676 return isl_bool_error;
1678 return isl_space_is_equal(pw1->dim, pw2->dim);
1681 #ifndef NO_MORPH
1682 __isl_give PW *FN(PW,morph_domain)(__isl_take PW *pw,
1683 __isl_take isl_morph *morph)
1685 int i;
1686 isl_ctx *ctx;
1688 if (!pw || !morph)
1689 goto error;
1691 ctx = isl_space_get_ctx(pw->dim);
1692 isl_assert(ctx, isl_space_is_domain_internal(morph->dom->dim, pw->dim),
1693 goto error);
1695 pw = FN(PW,cow)(pw);
1696 if (!pw)
1697 goto error;
1698 pw->dim = isl_space_extend_domain_with_range(
1699 isl_space_copy(morph->ran->dim), pw->dim);
1700 if (!pw->dim)
1701 goto error;
1703 for (i = 0; i < pw->n; ++i) {
1704 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
1705 if (!pw->p[i].set)
1706 goto error;
1707 pw->p[i].FIELD = FN(EL,morph_domain)(pw->p[i].FIELD,
1708 isl_morph_copy(morph));
1709 if (!pw->p[i].FIELD)
1710 goto error;
1713 isl_morph_free(morph);
1715 return pw;
1716 error:
1717 FN(PW,free)(pw);
1718 isl_morph_free(morph);
1719 return NULL;
1721 #endif
1723 int FN(PW,n_piece)(__isl_keep PW *pw)
1725 return pw ? pw->n : 0;
1728 isl_stat FN(PW,foreach_piece)(__isl_keep PW *pw,
1729 isl_stat (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
1730 void *user)
1732 int i;
1734 if (!pw)
1735 return isl_stat_error;
1737 for (i = 0; i < pw->n; ++i)
1738 if (fn(isl_set_copy(pw->p[i].set),
1739 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
1740 return isl_stat_error;
1742 return isl_stat_ok;
1745 #ifndef NO_LIFT
1746 static isl_bool any_divs(__isl_keep isl_set *set)
1748 int i;
1750 if (!set)
1751 return isl_bool_error;
1753 for (i = 0; i < set->n; ++i)
1754 if (set->p[i]->n_div > 0)
1755 return isl_bool_true;
1757 return isl_bool_false;
1760 static isl_stat foreach_lifted_subset(__isl_take isl_set *set,
1761 __isl_take EL *el,
1762 isl_stat (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1763 void *user), void *user)
1765 int i;
1767 if (!set || !el)
1768 goto error;
1770 for (i = 0; i < set->n; ++i) {
1771 isl_set *lift;
1772 EL *copy;
1774 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
1775 lift = isl_set_lift(lift);
1777 copy = FN(EL,copy)(el);
1778 copy = FN(EL,lift)(copy, isl_set_get_space(lift));
1780 if (fn(lift, copy, user) < 0)
1781 goto error;
1784 isl_set_free(set);
1785 FN(EL,free)(el);
1787 return isl_stat_ok;
1788 error:
1789 isl_set_free(set);
1790 FN(EL,free)(el);
1791 return isl_stat_error;
1794 isl_stat FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
1795 isl_stat (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1796 void *user), void *user)
1798 int i;
1800 if (!pw)
1801 return isl_stat_error;
1803 for (i = 0; i < pw->n; ++i) {
1804 isl_bool any;
1805 isl_set *set;
1806 EL *el;
1808 any = any_divs(pw->p[i].set);
1809 if (any < 0)
1810 return isl_stat_error;
1811 set = isl_set_copy(pw->p[i].set);
1812 el = FN(EL,copy)(pw->p[i].FIELD);
1813 if (!any) {
1814 if (fn(set, el, user) < 0)
1815 return isl_stat_error;
1816 continue;
1818 if (foreach_lifted_subset(set, el, fn, user) < 0)
1819 return isl_stat_error;
1822 return isl_stat_ok;
1824 #endif
1826 #ifndef NO_MOVE_DIMS
1827 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
1828 enum isl_dim_type dst_type, unsigned dst_pos,
1829 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1831 int i;
1833 pw = FN(PW,cow)(pw);
1834 if (!pw)
1835 return NULL;
1837 pw->dim = isl_space_move_dims(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
1838 if (!pw->dim)
1839 goto error;
1841 for (i = 0; i < pw->n; ++i) {
1842 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
1843 dst_type, dst_pos, src_type, src_pos, n);
1844 if (!pw->p[i].FIELD)
1845 goto error;
1848 if (dst_type == isl_dim_in)
1849 dst_type = isl_dim_set;
1850 if (src_type == isl_dim_in)
1851 src_type = isl_dim_set;
1853 for (i = 0; i < pw->n; ++i) {
1854 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
1855 dst_type, dst_pos,
1856 src_type, src_pos, n);
1857 if (!pw->p[i].set)
1858 goto error;
1861 return pw;
1862 error:
1863 FN(PW,free)(pw);
1864 return NULL;
1866 #endif
1868 __isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
1870 int i;
1872 if (isl_int_is_one(v))
1873 return pw;
1874 if (pw && DEFAULT_IS_ZERO && isl_int_is_zero(v)) {
1875 PW *zero;
1876 isl_space *dim = FN(PW,get_space)(pw);
1877 #ifdef HAS_TYPE
1878 zero = FN(PW,ZERO)(dim, pw->type);
1879 #else
1880 zero = FN(PW,ZERO)(dim);
1881 #endif
1882 FN(PW,free)(pw);
1883 return zero;
1885 pw = FN(PW,cow)(pw);
1886 if (!pw)
1887 return NULL;
1888 if (pw->n == 0)
1889 return pw;
1891 #ifdef HAS_TYPE
1892 if (isl_int_is_neg(v))
1893 pw->type = isl_fold_type_negate(pw->type);
1894 #endif
1895 for (i = 0; i < pw->n; ++i) {
1896 pw->p[i].FIELD = FN(EL,scale)(pw->p[i].FIELD, v);
1897 if (!pw->p[i].FIELD)
1898 goto error;
1901 return pw;
1902 error:
1903 FN(PW,free)(pw);
1904 return NULL;
1907 /* Multiply the pieces of "pw" by "v" and return the result.
1909 __isl_give PW *FN(PW,scale_val)(__isl_take PW *pw, __isl_take isl_val *v)
1911 int i;
1913 if (!pw || !v)
1914 goto error;
1916 if (isl_val_is_one(v)) {
1917 isl_val_free(v);
1918 return pw;
1920 if (pw && DEFAULT_IS_ZERO && isl_val_is_zero(v)) {
1921 PW *zero;
1922 isl_space *space = FN(PW,get_space)(pw);
1923 #ifdef HAS_TYPE
1924 zero = FN(PW,ZERO)(space, pw->type);
1925 #else
1926 zero = FN(PW,ZERO)(space);
1927 #endif
1928 FN(PW,free)(pw);
1929 isl_val_free(v);
1930 return zero;
1932 if (pw->n == 0) {
1933 isl_val_free(v);
1934 return pw;
1936 pw = FN(PW,cow)(pw);
1937 if (!pw)
1938 goto error;
1940 #ifdef HAS_TYPE
1941 if (isl_val_is_neg(v))
1942 pw->type = isl_fold_type_negate(pw->type);
1943 #endif
1944 for (i = 0; i < pw->n; ++i) {
1945 pw->p[i].FIELD = FN(EL,scale_val)(pw->p[i].FIELD,
1946 isl_val_copy(v));
1947 if (!pw->p[i].FIELD)
1948 goto error;
1951 isl_val_free(v);
1952 return pw;
1953 error:
1954 isl_val_free(v);
1955 FN(PW,free)(pw);
1956 return NULL;
1959 /* Divide the pieces of "pw" by "v" and return the result.
1961 __isl_give PW *FN(PW,scale_down_val)(__isl_take PW *pw, __isl_take isl_val *v)
1963 int i;
1965 if (!pw || !v)
1966 goto error;
1968 if (isl_val_is_one(v)) {
1969 isl_val_free(v);
1970 return pw;
1973 if (!isl_val_is_rat(v))
1974 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1975 "expecting rational factor", goto error);
1976 if (isl_val_is_zero(v))
1977 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1978 "cannot scale down by zero", goto error);
1980 if (pw->n == 0) {
1981 isl_val_free(v);
1982 return pw;
1984 pw = FN(PW,cow)(pw);
1985 if (!pw)
1986 goto error;
1988 #ifdef HAS_TYPE
1989 if (isl_val_is_neg(v))
1990 pw->type = isl_fold_type_negate(pw->type);
1991 #endif
1992 for (i = 0; i < pw->n; ++i) {
1993 pw->p[i].FIELD = FN(EL,scale_down_val)(pw->p[i].FIELD,
1994 isl_val_copy(v));
1995 if (!pw->p[i].FIELD)
1996 goto error;
1999 isl_val_free(v);
2000 return pw;
2001 error:
2002 isl_val_free(v);
2003 FN(PW,free)(pw);
2004 return NULL;
2007 __isl_give PW *FN(PW,scale)(__isl_take PW *pw, isl_int v)
2009 return FN(PW,mul_isl_int)(pw, v);
2012 /* Apply some normalization to "pw".
2013 * In particular, sort the pieces according to their function value
2014 * expressions, combining pairs of adjacent pieces with
2015 * the same such expression, and then normalize the domains of the pieces.
2017 * We normalize in place, but if anything goes wrong we need
2018 * to return NULL, so we need to make sure we don't change the
2019 * meaning of any possible other copies of "pw".
2021 __isl_give PW *FN(PW,normalize)(__isl_take PW *pw)
2023 int i;
2024 isl_set *set;
2026 pw = FN(PW,sort)(pw);
2027 if (!pw)
2028 return NULL;
2029 for (i = 0; i < pw->n; ++i) {
2030 set = isl_set_normalize(isl_set_copy(pw->p[i].set));
2031 if (!set)
2032 return FN(PW,free)(pw);
2033 isl_set_free(pw->p[i].set);
2034 pw->p[i].set = set;
2037 return pw;
2040 /* Is pw1 obviously equal to pw2?
2041 * That is, do they have obviously identical cells and obviously identical
2042 * elements on each cell?
2044 * If "pw1" or "pw2" contain any NaNs, then they are considered
2045 * not to be the same. A NaN is not equal to anything, not even
2046 * to another NaN.
2048 isl_bool FN(PW,plain_is_equal)(__isl_keep PW *pw1, __isl_keep PW *pw2)
2050 int i;
2051 isl_bool equal, has_nan;
2053 if (!pw1 || !pw2)
2054 return isl_bool_error;
2056 has_nan = FN(PW,involves_nan)(pw1);
2057 if (has_nan >= 0 && !has_nan)
2058 has_nan = FN(PW,involves_nan)(pw2);
2059 if (has_nan < 0 || has_nan)
2060 return isl_bool_not(has_nan);
2062 if (pw1 == pw2)
2063 return isl_bool_true;
2064 if (!isl_space_is_equal(pw1->dim, pw2->dim))
2065 return isl_bool_false;
2067 pw1 = FN(PW,copy)(pw1);
2068 pw2 = FN(PW,copy)(pw2);
2069 pw1 = FN(PW,normalize)(pw1);
2070 pw2 = FN(PW,normalize)(pw2);
2071 if (!pw1 || !pw2)
2072 goto error;
2074 equal = pw1->n == pw2->n;
2075 for (i = 0; equal && i < pw1->n; ++i) {
2076 equal = isl_set_plain_is_equal(pw1->p[i].set, pw2->p[i].set);
2077 if (equal < 0)
2078 goto error;
2079 if (!equal)
2080 break;
2081 equal = FN(EL,plain_is_equal)(pw1->p[i].FIELD, pw2->p[i].FIELD);
2082 if (equal < 0)
2083 goto error;
2086 FN(PW,free)(pw1);
2087 FN(PW,free)(pw2);
2088 return equal;
2089 error:
2090 FN(PW,free)(pw1);
2091 FN(PW,free)(pw2);
2092 return isl_bool_error;
2095 /* Does "pw" involve any NaNs?
2097 isl_bool FN(PW,involves_nan)(__isl_keep PW *pw)
2099 int i;
2101 if (!pw)
2102 return isl_bool_error;
2103 if (pw->n == 0)
2104 return isl_bool_false;
2106 for (i = 0; i < pw->n; ++i) {
2107 isl_bool has_nan = FN(EL,involves_nan)(pw->p[i].FIELD);
2108 if (has_nan < 0 || has_nan)
2109 return has_nan;
2112 return isl_bool_false;
2115 #ifndef NO_PULLBACK
2116 static __isl_give PW *FN(PW,align_params_pw_multi_aff_and)(__isl_take PW *pw,
2117 __isl_take isl_multi_aff *ma,
2118 __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_multi_aff *ma))
2120 isl_ctx *ctx;
2121 isl_bool equal_params;
2122 isl_space *ma_space;
2124 ma_space = isl_multi_aff_get_space(ma);
2125 if (!pw || !ma || !ma_space)
2126 goto error;
2127 equal_params = isl_space_has_equal_params(pw->dim, ma_space);
2128 if (equal_params < 0)
2129 goto error;
2130 if (equal_params) {
2131 isl_space_free(ma_space);
2132 return fn(pw, ma);
2134 ctx = FN(PW,get_ctx)(pw);
2135 if (!isl_space_has_named_params(pw->dim) ||
2136 !isl_space_has_named_params(ma_space))
2137 isl_die(ctx, isl_error_invalid,
2138 "unaligned unnamed parameters", goto error);
2139 pw = FN(PW,align_params)(pw, ma_space);
2140 ma = isl_multi_aff_align_params(ma, FN(PW,get_space)(pw));
2141 return fn(pw, ma);
2142 error:
2143 isl_space_free(ma_space);
2144 FN(PW,free)(pw);
2145 isl_multi_aff_free(ma);
2146 return NULL;
2149 static __isl_give PW *FN(PW,align_params_pw_pw_multi_aff_and)(__isl_take PW *pw,
2150 __isl_take isl_pw_multi_aff *pma,
2151 __isl_give PW *(*fn)(__isl_take PW *pw,
2152 __isl_take isl_pw_multi_aff *ma))
2154 isl_ctx *ctx;
2155 isl_bool equal_params;
2156 isl_space *pma_space;
2158 pma_space = isl_pw_multi_aff_get_space(pma);
2159 if (!pw || !pma || !pma_space)
2160 goto error;
2161 equal_params = isl_space_has_equal_params(pw->dim, pma_space);
2162 if (equal_params < 0)
2163 goto error;
2164 if (equal_params) {
2165 isl_space_free(pma_space);
2166 return fn(pw, pma);
2168 ctx = FN(PW,get_ctx)(pw);
2169 if (!isl_space_has_named_params(pw->dim) ||
2170 !isl_space_has_named_params(pma_space))
2171 isl_die(ctx, isl_error_invalid,
2172 "unaligned unnamed parameters", goto error);
2173 pw = FN(PW,align_params)(pw, pma_space);
2174 pma = isl_pw_multi_aff_align_params(pma, FN(PW,get_space)(pw));
2175 return fn(pw, pma);
2176 error:
2177 isl_space_free(pma_space);
2178 FN(PW,free)(pw);
2179 isl_pw_multi_aff_free(pma);
2180 return NULL;
2183 /* Compute the pullback of "pw" by the function represented by "ma".
2184 * In other words, plug in "ma" in "pw".
2186 static __isl_give PW *FN(PW,pullback_multi_aff_aligned)(__isl_take PW *pw,
2187 __isl_take isl_multi_aff *ma)
2189 int i;
2190 isl_space *space = NULL;
2192 ma = isl_multi_aff_align_divs(ma);
2193 pw = FN(PW,cow)(pw);
2194 if (!pw || !ma)
2195 goto error;
2197 space = isl_space_join(isl_multi_aff_get_space(ma),
2198 FN(PW,get_space)(pw));
2200 for (i = 0; i < pw->n; ++i) {
2201 pw->p[i].set = isl_set_preimage_multi_aff(pw->p[i].set,
2202 isl_multi_aff_copy(ma));
2203 if (!pw->p[i].set)
2204 goto error;
2205 pw->p[i].FIELD = FN(EL,pullback_multi_aff)(pw->p[i].FIELD,
2206 isl_multi_aff_copy(ma));
2207 if (!pw->p[i].FIELD)
2208 goto error;
2211 pw = FN(PW,reset_space)(pw, space);
2212 isl_multi_aff_free(ma);
2213 return pw;
2214 error:
2215 isl_space_free(space);
2216 isl_multi_aff_free(ma);
2217 FN(PW,free)(pw);
2218 return NULL;
2221 __isl_give PW *FN(PW,pullback_multi_aff)(__isl_take PW *pw,
2222 __isl_take isl_multi_aff *ma)
2224 return FN(PW,align_params_pw_multi_aff_and)(pw, ma,
2225 &FN(PW,pullback_multi_aff_aligned));
2228 /* Compute the pullback of "pw" by the function represented by "pma".
2229 * In other words, plug in "pma" in "pw".
2231 static __isl_give PW *FN(PW,pullback_pw_multi_aff_aligned)(__isl_take PW *pw,
2232 __isl_take isl_pw_multi_aff *pma)
2234 int i;
2235 PW *res;
2237 if (!pma)
2238 goto error;
2240 if (pma->n == 0) {
2241 isl_space *space;
2242 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
2243 FN(PW,get_space)(pw));
2244 isl_pw_multi_aff_free(pma);
2245 res = FN(PW,empty)(space);
2246 FN(PW,free)(pw);
2247 return res;
2250 res = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
2251 isl_multi_aff_copy(pma->p[0].maff));
2252 res = FN(PW,intersect_domain)(res, isl_set_copy(pma->p[0].set));
2254 for (i = 1; i < pma->n; ++i) {
2255 PW *res_i;
2257 res_i = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
2258 isl_multi_aff_copy(pma->p[i].maff));
2259 res_i = FN(PW,intersect_domain)(res_i,
2260 isl_set_copy(pma->p[i].set));
2261 res = FN(PW,add_disjoint)(res, res_i);
2264 isl_pw_multi_aff_free(pma);
2265 FN(PW,free)(pw);
2266 return res;
2267 error:
2268 isl_pw_multi_aff_free(pma);
2269 FN(PW,free)(pw);
2270 return NULL;
2273 __isl_give PW *FN(PW,pullback_pw_multi_aff)(__isl_take PW *pw,
2274 __isl_take isl_pw_multi_aff *pma)
2276 return FN(PW,align_params_pw_pw_multi_aff_and)(pw, pma,
2277 &FN(PW,pullback_pw_multi_aff_aligned));
2279 #endif