isl_map.c: extract out shared isl_map_check_range
[isl.git] / isl_pw_templ.c
blobab19dd30e9e2955c2409ab21ebf2877c5def14cd
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/id.h>
15 #include <isl/aff.h>
16 #include <isl_sort.h>
17 #include <isl_val_private.h>
19 #include <isl_pw_macro.h>
21 #ifdef HAS_TYPE
22 __isl_give PW *FN(PW,alloc_size)(__isl_take isl_space *dim,
23 enum isl_fold type, int n)
24 #else
25 __isl_give PW *FN(PW,alloc_size)(__isl_take isl_space *dim, int n)
26 #endif
28 isl_ctx *ctx;
29 struct PW *pw;
31 if (!dim)
32 return NULL;
33 ctx = isl_space_get_ctx(dim);
34 isl_assert(ctx, n >= 0, goto error);
35 pw = isl_alloc(ctx, struct PW,
36 sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
37 if (!pw)
38 goto error;
40 pw->ref = 1;
41 #ifdef HAS_TYPE
42 pw->type = type;
43 #endif
44 pw->size = n;
45 pw->n = 0;
46 pw->dim = dim;
47 return pw;
48 error:
49 isl_space_free(dim);
50 return NULL;
53 #ifdef HAS_TYPE
54 __isl_give PW *FN(PW,ZERO)(__isl_take isl_space *dim, enum isl_fold type)
56 return FN(PW,alloc_size)(dim, type, 0);
58 #else
59 __isl_give PW *FN(PW,ZERO)(__isl_take isl_space *dim)
61 return FN(PW,alloc_size)(dim, 0);
63 #endif
65 __isl_give PW *FN(PW,add_piece)(__isl_take PW *pw,
66 __isl_take isl_set *set, __isl_take EL *el)
68 isl_ctx *ctx;
69 isl_space *el_dim = NULL;
71 if (!pw || !set || !el)
72 goto error;
74 if (isl_set_plain_is_empty(set) || FN(EL,EL_IS_ZERO)(el)) {
75 isl_set_free(set);
76 FN(EL,free)(el);
77 return pw;
80 ctx = isl_set_get_ctx(set);
81 #ifdef HAS_TYPE
82 if (pw->type != el->type)
83 isl_die(ctx, isl_error_invalid, "fold types don't match",
84 goto error);
85 #endif
86 el_dim = FN(EL,get_space(el));
87 isl_assert(ctx, isl_space_is_equal(pw->dim, el_dim), goto error);
88 isl_assert(ctx, pw->n < pw->size, goto error);
90 pw->p[pw->n].set = set;
91 pw->p[pw->n].FIELD = el;
92 pw->n++;
94 isl_space_free(el_dim);
95 return pw;
96 error:
97 isl_space_free(el_dim);
98 FN(PW,free)(pw);
99 isl_set_free(set);
100 FN(EL,free)(el);
101 return NULL;
104 /* Does the space of "set" correspond to that of the domain of "el".
106 static isl_bool FN(PW,compatible_domain)(__isl_keep EL *el,
107 __isl_keep isl_set *set)
109 isl_bool ok;
110 isl_space *el_space, *set_space;
112 if (!set || !el)
113 return isl_bool_error;
114 set_space = isl_set_get_space(set);
115 el_space = FN(EL,get_space)(el);
116 ok = isl_space_is_domain_internal(set_space, el_space);
117 isl_space_free(el_space);
118 isl_space_free(set_space);
119 return ok;
122 /* Check that the space of "set" corresponds to that of the domain of "el".
124 static isl_stat FN(PW,check_compatible_domain)(__isl_keep EL *el,
125 __isl_keep isl_set *set)
127 isl_bool ok;
129 ok = FN(PW,compatible_domain)(el, set);
130 if (ok < 0)
131 return isl_stat_error;
132 if (!ok)
133 isl_die(isl_set_get_ctx(set), isl_error_invalid,
134 "incompatible spaces", return isl_stat_error);
136 return isl_stat_ok;
139 #ifdef HAS_TYPE
140 __isl_give PW *FN(PW,alloc)(enum isl_fold type,
141 __isl_take isl_set *set, __isl_take EL *el)
142 #else
143 __isl_give PW *FN(PW,alloc)(__isl_take isl_set *set, __isl_take EL *el)
144 #endif
146 PW *pw;
148 if (FN(PW,check_compatible_domain)(el, set) < 0)
149 goto error;
151 #ifdef HAS_TYPE
152 pw = FN(PW,alloc_size)(FN(EL,get_space)(el), type, 1);
153 #else
154 pw = FN(PW,alloc_size)(FN(EL,get_space)(el), 1);
155 #endif
157 return FN(PW,add_piece)(pw, set, el);
158 error:
159 isl_set_free(set);
160 FN(EL,free)(el);
161 return NULL;
164 __isl_give PW *FN(PW,dup)(__isl_keep PW *pw)
166 int i;
167 PW *dup;
169 if (!pw)
170 return NULL;
172 #ifdef HAS_TYPE
173 dup = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->type, pw->n);
174 #else
175 dup = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->n);
176 #endif
177 if (!dup)
178 return NULL;
180 for (i = 0; i < pw->n; ++i)
181 dup = FN(PW,add_piece)(dup, isl_set_copy(pw->p[i].set),
182 FN(EL,copy)(pw->p[i].FIELD));
184 return dup;
187 __isl_give PW *FN(PW,cow)(__isl_take PW *pw)
189 if (!pw)
190 return NULL;
192 if (pw->ref == 1)
193 return pw;
194 pw->ref--;
195 return FN(PW,dup)(pw);
198 __isl_give PW *FN(PW,copy)(__isl_keep PW *pw)
200 if (!pw)
201 return NULL;
203 pw->ref++;
204 return pw;
207 __isl_null PW *FN(PW,free)(__isl_take PW *pw)
209 int i;
211 if (!pw)
212 return NULL;
213 if (--pw->ref > 0)
214 return NULL;
216 for (i = 0; i < pw->n; ++i) {
217 isl_set_free(pw->p[i].set);
218 FN(EL,free)(pw->p[i].FIELD);
220 isl_space_free(pw->dim);
221 free(pw);
223 return NULL;
226 const char *FN(PW,get_dim_name)(__isl_keep PW *pw, enum isl_dim_type type,
227 unsigned pos)
229 return pw ? isl_space_get_dim_name(pw->dim, type, pos) : NULL;
232 isl_bool FN(PW,has_dim_id)(__isl_keep PW *pw, enum isl_dim_type type,
233 unsigned pos)
235 return pw ? isl_space_has_dim_id(pw->dim, type, pos) : isl_bool_error;
238 __isl_give isl_id *FN(PW,get_dim_id)(__isl_keep PW *pw, enum isl_dim_type type,
239 unsigned pos)
241 return pw ? isl_space_get_dim_id(pw->dim, type, pos) : NULL;
244 isl_bool FN(PW,has_tuple_name)(__isl_keep PW *pw, enum isl_dim_type type)
246 return pw ? isl_space_has_tuple_name(pw->dim, type) : isl_bool_error;
249 const char *FN(PW,get_tuple_name)(__isl_keep PW *pw, enum isl_dim_type type)
251 return pw ? isl_space_get_tuple_name(pw->dim, type) : NULL;
254 isl_bool FN(PW,has_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
256 return pw ? isl_space_has_tuple_id(pw->dim, type) : isl_bool_error;
259 __isl_give isl_id *FN(PW,get_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
261 return pw ? isl_space_get_tuple_id(pw->dim, type) : NULL;
264 isl_bool FN(PW,IS_ZERO)(__isl_keep PW *pw)
266 if (!pw)
267 return isl_bool_error;
269 return pw->n == 0;
272 #ifndef NO_REALIGN
273 __isl_give PW *FN(PW,realign_domain)(__isl_take PW *pw,
274 __isl_take isl_reordering *exp)
276 int i;
278 pw = FN(PW,cow)(pw);
279 if (!pw || !exp)
280 goto error;
282 for (i = 0; i < pw->n; ++i) {
283 pw->p[i].set = isl_set_realign(pw->p[i].set,
284 isl_reordering_copy(exp));
285 if (!pw->p[i].set)
286 goto error;
287 pw->p[i].FIELD = FN(EL,realign_domain)(pw->p[i].FIELD,
288 isl_reordering_copy(exp));
289 if (!pw->p[i].FIELD)
290 goto error;
293 pw = FN(PW,reset_domain_space)(pw, isl_space_copy(exp->dim));
295 isl_reordering_free(exp);
296 return pw;
297 error:
298 isl_reordering_free(exp);
299 FN(PW,free)(pw);
300 return NULL;
303 /* Align the parameters of "pw" to those of "model".
305 __isl_give PW *FN(PW,align_params)(__isl_take PW *pw, __isl_take isl_space *model)
307 isl_ctx *ctx;
308 isl_bool equal_params;
310 if (!pw || !model)
311 goto error;
313 ctx = isl_space_get_ctx(model);
314 if (!isl_space_has_named_params(model))
315 isl_die(ctx, isl_error_invalid,
316 "model has unnamed parameters", goto error);
317 if (!isl_space_has_named_params(pw->dim))
318 isl_die(ctx, isl_error_invalid,
319 "input has unnamed parameters", goto error);
320 equal_params = isl_space_has_equal_params(pw->dim, model);
321 if (equal_params < 0)
322 goto error;
323 if (!equal_params) {
324 isl_reordering *exp;
326 model = isl_space_drop_dims(model, isl_dim_in,
327 0, isl_space_dim(model, isl_dim_in));
328 model = isl_space_drop_dims(model, isl_dim_out,
329 0, isl_space_dim(model, isl_dim_out));
330 exp = isl_parameter_alignment_reordering(pw->dim, model);
331 exp = isl_reordering_extend_space(exp,
332 FN(PW,get_domain_space)(pw));
333 pw = FN(PW,realign_domain)(pw, exp);
336 isl_space_free(model);
337 return pw;
338 error:
339 isl_space_free(model);
340 FN(PW,free)(pw);
341 return NULL;
344 static __isl_give PW *FN(PW,align_params_pw_pw_and)(__isl_take PW *pw1,
345 __isl_take PW *pw2,
346 __isl_give PW *(*fn)(__isl_take PW *pw1, __isl_take PW *pw2))
348 isl_ctx *ctx;
349 isl_bool equal_params;
351 if (!pw1 || !pw2)
352 goto error;
353 equal_params = isl_space_has_equal_params(pw1->dim, pw2->dim);
354 if (equal_params < 0)
355 goto error;
356 if (equal_params)
357 return fn(pw1, pw2);
358 ctx = FN(PW,get_ctx)(pw1);
359 if (!isl_space_has_named_params(pw1->dim) ||
360 !isl_space_has_named_params(pw2->dim))
361 isl_die(ctx, isl_error_invalid,
362 "unaligned unnamed parameters", goto error);
363 pw1 = FN(PW,align_params)(pw1, FN(PW,get_space)(pw2));
364 pw2 = FN(PW,align_params)(pw2, FN(PW,get_space)(pw1));
365 return fn(pw1, pw2);
366 error:
367 FN(PW,free)(pw1);
368 FN(PW,free)(pw2);
369 return NULL;
372 static __isl_give PW *FN(PW,align_params_pw_set_and)(__isl_take PW *pw,
373 __isl_take isl_set *set,
374 __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_set *set))
376 isl_ctx *ctx;
377 isl_bool aligned;
379 if (!pw || !set)
380 goto error;
381 aligned = isl_set_space_has_equal_params(set, pw->dim);
382 if (aligned < 0)
383 goto error;
384 if (aligned)
385 return fn(pw, set);
386 ctx = FN(PW,get_ctx)(pw);
387 if (!isl_space_has_named_params(pw->dim) ||
388 !isl_space_has_named_params(set->dim))
389 isl_die(ctx, isl_error_invalid,
390 "unaligned unnamed parameters", goto error);
391 pw = FN(PW,align_params)(pw, isl_set_get_space(set));
392 set = isl_set_align_params(set, FN(PW,get_space)(pw));
393 return fn(pw, set);
394 error:
395 FN(PW,free)(pw);
396 isl_set_free(set);
397 return NULL;
399 #endif
401 static __isl_give PW *FN(PW,union_add_aligned)(__isl_take PW *pw1,
402 __isl_take PW *pw2)
404 int i, j, n;
405 struct PW *res;
406 isl_ctx *ctx;
407 isl_set *set;
409 if (!pw1 || !pw2)
410 goto error;
412 ctx = isl_space_get_ctx(pw1->dim);
413 #ifdef HAS_TYPE
414 if (pw1->type != pw2->type)
415 isl_die(ctx, isl_error_invalid,
416 "fold types don't match", goto error);
417 #endif
418 isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
420 if (FN(PW,IS_ZERO)(pw1)) {
421 FN(PW,free)(pw1);
422 return pw2;
425 if (FN(PW,IS_ZERO)(pw2)) {
426 FN(PW,free)(pw2);
427 return pw1;
430 n = (pw1->n + 1) * (pw2->n + 1);
431 #ifdef HAS_TYPE
432 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), pw1->type, n);
433 #else
434 res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), n);
435 #endif
437 for (i = 0; i < pw1->n; ++i) {
438 set = isl_set_copy(pw1->p[i].set);
439 for (j = 0; j < pw2->n; ++j) {
440 struct isl_set *common;
441 EL *sum;
442 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
443 isl_set_copy(pw2->p[j].set));
444 if (isl_set_plain_is_empty(common)) {
445 isl_set_free(common);
446 continue;
448 set = isl_set_subtract(set,
449 isl_set_copy(pw2->p[j].set));
451 sum = FN(EL,add_on_domain)(common,
452 FN(EL,copy)(pw1->p[i].FIELD),
453 FN(EL,copy)(pw2->p[j].FIELD));
455 res = FN(PW,add_piece)(res, common, sum);
457 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw1->p[i].FIELD));
460 for (j = 0; j < pw2->n; ++j) {
461 set = isl_set_copy(pw2->p[j].set);
462 for (i = 0; i < pw1->n; ++i)
463 set = isl_set_subtract(set,
464 isl_set_copy(pw1->p[i].set));
465 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw2->p[j].FIELD));
468 FN(PW,free)(pw1);
469 FN(PW,free)(pw2);
471 return res;
472 error:
473 FN(PW,free)(pw1);
474 FN(PW,free)(pw2);
475 return NULL;
478 /* Private version of "union_add". For isl_pw_qpolynomial and
479 * isl_pw_qpolynomial_fold, we prefer to simply call it "add".
481 static __isl_give PW *FN(PW,union_add_)(__isl_take PW *pw1, __isl_take PW *pw2)
483 return FN(PW,align_params_pw_pw_and)(pw1, pw2,
484 &FN(PW,union_add_aligned));
487 /* Make sure "pw" has room for at least "n" more pieces.
489 * If there is only one reference to pw, we extend it in place.
490 * Otherwise, we create a new PW and copy the pieces.
492 static __isl_give PW *FN(PW,grow)(__isl_take PW *pw, int n)
494 int i;
495 isl_ctx *ctx;
496 PW *res;
498 if (!pw)
499 return NULL;
500 if (pw->n + n <= pw->size)
501 return pw;
502 ctx = FN(PW,get_ctx)(pw);
503 n += pw->n;
504 if (pw->ref == 1) {
505 res = isl_realloc(ctx, pw, struct PW,
506 sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
507 if (!res)
508 return FN(PW,free)(pw);
509 res->size = n;
510 return res;
512 #ifdef HAS_TYPE
513 res = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->type, n);
514 #else
515 res = FN(PW,alloc_size)(isl_space_copy(pw->dim), n);
516 #endif
517 if (!res)
518 return FN(PW,free)(pw);
519 for (i = 0; i < pw->n; ++i)
520 res = FN(PW,add_piece)(res, isl_set_copy(pw->p[i].set),
521 FN(EL,copy)(pw->p[i].FIELD));
522 FN(PW,free)(pw);
523 return res;
526 static __isl_give PW *FN(PW,add_disjoint_aligned)(__isl_take PW *pw1,
527 __isl_take PW *pw2)
529 int i;
530 isl_ctx *ctx;
532 if (!pw1 || !pw2)
533 goto error;
535 if (pw1->size < pw1->n + pw2->n && pw1->n < pw2->n)
536 return FN(PW,add_disjoint_aligned)(pw2, pw1);
538 ctx = isl_space_get_ctx(pw1->dim);
539 #ifdef HAS_TYPE
540 if (pw1->type != pw2->type)
541 isl_die(ctx, isl_error_invalid,
542 "fold types don't match", goto error);
543 #endif
544 isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
546 if (FN(PW,IS_ZERO)(pw1)) {
547 FN(PW,free)(pw1);
548 return pw2;
551 if (FN(PW,IS_ZERO)(pw2)) {
552 FN(PW,free)(pw2);
553 return pw1;
556 pw1 = FN(PW,grow)(pw1, pw2->n);
557 if (!pw1)
558 goto error;
560 for (i = 0; i < pw2->n; ++i)
561 pw1 = FN(PW,add_piece)(pw1,
562 isl_set_copy(pw2->p[i].set),
563 FN(EL,copy)(pw2->p[i].FIELD));
565 FN(PW,free)(pw2);
567 return pw1;
568 error:
569 FN(PW,free)(pw1);
570 FN(PW,free)(pw2);
571 return NULL;
574 __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
576 return FN(PW,align_params_pw_pw_and)(pw1, pw2,
577 &FN(PW,add_disjoint_aligned));
580 /* This function is currently only used from isl_aff.c
582 static __isl_give PW *FN(PW,on_shared_domain_in)(__isl_take PW *pw1,
583 __isl_take PW *pw2, __isl_take isl_space *space,
584 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
585 __attribute__ ((unused));
587 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
588 * The result of "fn" (and therefore also of this function) lives in "space".
590 static __isl_give PW *FN(PW,on_shared_domain_in)(__isl_take PW *pw1,
591 __isl_take PW *pw2, __isl_take isl_space *space,
592 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
594 int i, j, n;
595 PW *res = NULL;
597 if (!pw1 || !pw2)
598 goto error;
600 n = pw1->n * pw2->n;
601 #ifdef HAS_TYPE
602 res = FN(PW,alloc_size)(isl_space_copy(space), pw1->type, n);
603 #else
604 res = FN(PW,alloc_size)(isl_space_copy(space), n);
605 #endif
607 for (i = 0; i < pw1->n; ++i) {
608 for (j = 0; j < pw2->n; ++j) {
609 isl_set *common;
610 EL *res_ij;
611 int empty;
613 common = isl_set_intersect(
614 isl_set_copy(pw1->p[i].set),
615 isl_set_copy(pw2->p[j].set));
616 empty = isl_set_plain_is_empty(common);
617 if (empty < 0 || empty) {
618 isl_set_free(common);
619 if (empty < 0)
620 goto error;
621 continue;
624 res_ij = fn(FN(EL,copy)(pw1->p[i].FIELD),
625 FN(EL,copy)(pw2->p[j].FIELD));
626 res_ij = FN(EL,gist)(res_ij, isl_set_copy(common));
628 res = FN(PW,add_piece)(res, common, res_ij);
632 isl_space_free(space);
633 FN(PW,free)(pw1);
634 FN(PW,free)(pw2);
635 return res;
636 error:
637 isl_space_free(space);
638 FN(PW,free)(pw1);
639 FN(PW,free)(pw2);
640 FN(PW,free)(res);
641 return NULL;
644 /* This function is currently only used from isl_aff.c
646 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
647 __isl_take PW *pw2,
648 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
649 __attribute__ ((unused));
651 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
652 * The result of "fn" is assumed to live in the same space as "pw1" and "pw2".
654 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
655 __isl_take PW *pw2,
656 __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
658 isl_space *space;
660 if (!pw1 || !pw2)
661 goto error;
663 space = isl_space_copy(pw1->dim);
664 return FN(PW,on_shared_domain_in)(pw1, pw2, space, fn);
665 error:
666 FN(PW,free)(pw1);
667 FN(PW,free)(pw2);
668 return NULL;
671 #ifndef NO_NEG
672 __isl_give PW *FN(PW,neg)(__isl_take PW *pw)
674 int i;
676 if (!pw)
677 return NULL;
679 if (FN(PW,IS_ZERO)(pw))
680 return pw;
682 pw = FN(PW,cow)(pw);
683 if (!pw)
684 return NULL;
686 for (i = 0; i < pw->n; ++i) {
687 pw->p[i].FIELD = FN(EL,neg)(pw->p[i].FIELD);
688 if (!pw->p[i].FIELD)
689 return FN(PW,free)(pw);
692 return pw;
694 #endif
696 #ifndef NO_SUB
697 __isl_give PW *FN(PW,sub)(__isl_take PW *pw1, __isl_take PW *pw2)
699 return FN(PW,add)(pw1, FN(PW,neg)(pw2));
701 #endif
703 /* Return the parameter domain of "pw".
705 __isl_give isl_set *FN(PW,params)(__isl_take PW *pw)
707 return isl_set_params(FN(PW,domain)(pw));
710 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
712 int i;
713 isl_set *dom;
715 if (!pw)
716 return NULL;
718 dom = isl_set_empty(FN(PW,get_domain_space)(pw));
719 for (i = 0; i < pw->n; ++i)
720 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
722 FN(PW,free)(pw);
724 return dom;
727 /* Exploit the equalities in the domain of piece "i" of "pw"
728 * to simplify the associated function.
729 * If the domain of piece "i" is empty, then remove it entirely,
730 * replacing it with the final piece.
732 static int FN(PW,exploit_equalities_and_remove_if_empty)(__isl_keep PW *pw,
733 int i)
735 isl_basic_set *aff;
736 int empty = isl_set_plain_is_empty(pw->p[i].set);
738 if (empty < 0)
739 return -1;
740 if (empty) {
741 isl_set_free(pw->p[i].set);
742 FN(EL,free)(pw->p[i].FIELD);
743 if (i != pw->n - 1)
744 pw->p[i] = pw->p[pw->n - 1];
745 pw->n--;
747 return 0;
750 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
751 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD, aff);
752 if (!pw->p[i].FIELD)
753 return -1;
755 return 0;
758 /* Convert a piecewise expression defined over a parameter domain
759 * into one that is defined over a zero-dimensional set.
761 __isl_give PW *FN(PW,from_range)(__isl_take PW *pw)
763 isl_space *space;
765 if (!pw)
766 return NULL;
767 if (!isl_space_is_set(pw->dim))
768 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
769 "not living in a set space", return FN(PW,free)(pw));
771 space = FN(PW,get_space)(pw);
772 space = isl_space_from_range(space);
773 pw = FN(PW,reset_space)(pw, space);
775 return pw;
778 /* Fix the value of the given parameter or domain dimension of "pw"
779 * to be equal to "value".
781 __isl_give PW *FN(PW,fix_si)(__isl_take PW *pw, enum isl_dim_type type,
782 unsigned pos, int value)
784 int i;
786 if (!pw)
787 return NULL;
789 if (type == isl_dim_out)
790 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
791 "cannot fix output dimension", return FN(PW,free)(pw));
793 if (pw->n == 0)
794 return pw;
796 if (type == isl_dim_in)
797 type = isl_dim_set;
799 pw = FN(PW,cow)(pw);
800 if (!pw)
801 return FN(PW,free)(pw);
803 for (i = pw->n - 1; i >= 0; --i) {
804 pw->p[i].set = isl_set_fix_si(pw->p[i].set, type, pos, value);
805 if (FN(PW,exploit_equalities_and_remove_if_empty)(pw, i) < 0)
806 return FN(PW,free)(pw);
809 return pw;
812 /* Restrict the domain of "pw" by combining each cell
813 * with "set" through a call to "fn", where "fn" may be
814 * isl_set_intersect, isl_set_intersect_params or isl_set_subtract.
816 static __isl_give PW *FN(PW,restrict_domain_aligned)(__isl_take PW *pw,
817 __isl_take isl_set *set,
818 __isl_give isl_set *(*fn)(__isl_take isl_set *set1,
819 __isl_take isl_set *set2))
821 int i;
823 if (!pw || !set)
824 goto error;
826 if (pw->n == 0) {
827 isl_set_free(set);
828 return pw;
831 pw = FN(PW,cow)(pw);
832 if (!pw)
833 goto error;
835 for (i = pw->n - 1; i >= 0; --i) {
836 pw->p[i].set = fn(pw->p[i].set, isl_set_copy(set));
837 if (FN(PW,exploit_equalities_and_remove_if_empty)(pw, i) < 0)
838 goto error;
841 isl_set_free(set);
842 return pw;
843 error:
844 isl_set_free(set);
845 FN(PW,free)(pw);
846 return NULL;
849 static __isl_give PW *FN(PW,intersect_domain_aligned)(__isl_take PW *pw,
850 __isl_take isl_set *set)
852 return FN(PW,restrict_domain_aligned)(pw, set, &isl_set_intersect);
855 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw,
856 __isl_take isl_set *context)
858 return FN(PW,align_params_pw_set_and)(pw, context,
859 &FN(PW,intersect_domain_aligned));
862 static __isl_give PW *FN(PW,intersect_params_aligned)(__isl_take PW *pw,
863 __isl_take isl_set *set)
865 return FN(PW,restrict_domain_aligned)(pw, set,
866 &isl_set_intersect_params);
869 /* Intersect the domain of "pw" with the parameter domain "context".
871 __isl_give PW *FN(PW,intersect_params)(__isl_take PW *pw,
872 __isl_take isl_set *context)
874 return FN(PW,align_params_pw_set_and)(pw, context,
875 &FN(PW,intersect_params_aligned));
878 /* Subtract "domain' from the domain of "pw", assuming their
879 * parameters have been aligned.
881 static __isl_give PW *FN(PW,subtract_domain_aligned)(__isl_take PW *pw,
882 __isl_take isl_set *domain)
884 return FN(PW,restrict_domain_aligned)(pw, domain, &isl_set_subtract);
887 /* Subtract "domain' from the domain of "pw".
889 __isl_give PW *FN(PW,subtract_domain)(__isl_take PW *pw,
890 __isl_take isl_set *domain)
892 return FN(PW,align_params_pw_set_and)(pw, domain,
893 &FN(PW,subtract_domain_aligned));
896 /* Compute the gist of "pw" with respect to the domain constraints
897 * of "context" for the case where the domain of the last element
898 * of "pw" is equal to "context".
899 * Call "fn_el" to compute the gist of this element, replace
900 * its domain by the universe and drop all other elements
901 * as their domains are necessarily disjoint from "context".
903 static __isl_give PW *FN(PW,gist_last)(__isl_take PW *pw,
904 __isl_take isl_set *context,
905 __isl_give EL *(*fn_el)(__isl_take EL *el, __isl_take isl_set *set))
907 int i;
908 isl_space *space;
910 for (i = 0; i < pw->n - 1; ++i) {
911 isl_set_free(pw->p[i].set);
912 FN(EL,free)(pw->p[i].FIELD);
914 pw->p[0].FIELD = pw->p[pw->n - 1].FIELD;
915 pw->p[0].set = pw->p[pw->n - 1].set;
916 pw->n = 1;
918 space = isl_set_get_space(context);
919 pw->p[0].FIELD = fn_el(pw->p[0].FIELD, context);
920 context = isl_set_universe(space);
921 isl_set_free(pw->p[0].set);
922 pw->p[0].set = context;
924 if (!pw->p[0].FIELD || !pw->p[0].set)
925 return FN(PW,free)(pw);
927 return pw;
930 /* Compute the gist of "pw" with respect to the domain constraints
931 * of "context". Call "fn_el" to compute the gist of the elements
932 * and "fn_dom" to compute the gist of the domains.
934 * If the piecewise expression is empty or the context is the universe,
935 * then nothing can be simplified.
937 static __isl_give PW *FN(PW,gist_aligned)(__isl_take PW *pw,
938 __isl_take isl_set *context,
939 __isl_give EL *(*fn_el)(__isl_take EL *el,
940 __isl_take isl_set *set),
941 __isl_give isl_set *(*fn_dom)(__isl_take isl_set *set,
942 __isl_take isl_basic_set *bset))
944 int i;
945 int is_universe;
946 isl_bool aligned;
947 isl_basic_set *hull = NULL;
949 if (!pw || !context)
950 goto error;
952 if (pw->n == 0) {
953 isl_set_free(context);
954 return pw;
957 is_universe = isl_set_plain_is_universe(context);
958 if (is_universe < 0)
959 goto error;
960 if (is_universe) {
961 isl_set_free(context);
962 return pw;
965 aligned = isl_set_space_has_equal_params(context, pw->dim);
966 if (aligned < 0)
967 goto error;
968 if (!aligned) {
969 pw = FN(PW,align_params)(pw, isl_set_get_space(context));
970 context = isl_set_align_params(context, FN(PW,get_space)(pw));
973 pw = FN(PW,cow)(pw);
974 if (!pw)
975 goto error;
977 if (pw->n == 1) {
978 int equal;
980 equal = isl_set_plain_is_equal(pw->p[0].set, context);
981 if (equal < 0)
982 goto error;
983 if (equal)
984 return FN(PW,gist_last)(pw, context, fn_el);
987 context = isl_set_compute_divs(context);
988 hull = isl_set_simple_hull(isl_set_copy(context));
990 for (i = pw->n - 1; i >= 0; --i) {
991 isl_set *set_i;
992 int empty;
994 if (i == pw->n - 1) {
995 int equal;
996 equal = isl_set_plain_is_equal(pw->p[i].set, context);
997 if (equal < 0)
998 goto error;
999 if (equal) {
1000 isl_basic_set_free(hull);
1001 return FN(PW,gist_last)(pw, context, fn_el);
1004 set_i = isl_set_intersect(isl_set_copy(pw->p[i].set),
1005 isl_set_copy(context));
1006 empty = isl_set_plain_is_empty(set_i);
1007 pw->p[i].FIELD = fn_el(pw->p[i].FIELD, set_i);
1008 pw->p[i].set = fn_dom(pw->p[i].set, isl_basic_set_copy(hull));
1009 if (empty < 0 || !pw->p[i].FIELD || !pw->p[i].set)
1010 goto error;
1011 if (empty) {
1012 isl_set_free(pw->p[i].set);
1013 FN(EL,free)(pw->p[i].FIELD);
1014 if (i != pw->n - 1)
1015 pw->p[i] = pw->p[pw->n - 1];
1016 pw->n--;
1020 isl_basic_set_free(hull);
1021 isl_set_free(context);
1023 return pw;
1024 error:
1025 FN(PW,free)(pw);
1026 isl_basic_set_free(hull);
1027 isl_set_free(context);
1028 return NULL;
1031 static __isl_give PW *FN(PW,gist_domain_aligned)(__isl_take PW *pw,
1032 __isl_take isl_set *set)
1034 return FN(PW,gist_aligned)(pw, set, &FN(EL,gist),
1035 &isl_set_gist_basic_set);
1038 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
1040 return FN(PW,align_params_pw_set_and)(pw, context,
1041 &FN(PW,gist_domain_aligned));
1044 static __isl_give PW *FN(PW,gist_params_aligned)(__isl_take PW *pw,
1045 __isl_take isl_set *set)
1047 return FN(PW,gist_aligned)(pw, set, &FN(EL,gist_params),
1048 &isl_set_gist_params_basic_set);
1051 __isl_give PW *FN(PW,gist_params)(__isl_take PW *pw,
1052 __isl_take isl_set *context)
1054 return FN(PW,align_params_pw_set_and)(pw, context,
1055 &FN(PW,gist_params_aligned));
1058 /* Return -1 if the piece "p1" should be sorted before "p2"
1059 * and 1 if it should be sorted after "p2".
1060 * Return 0 if they do not need to be sorted in a specific order.
1062 * The two pieces are compared on the basis of their function value expressions.
1064 static int FN(PW,sort_field_cmp)(const void *p1, const void *p2, void *arg)
1066 struct FN(PW,piece) const *pc1 = p1;
1067 struct FN(PW,piece) const *pc2 = p2;
1069 return FN(EL,plain_cmp)(pc1->FIELD, pc2->FIELD);
1072 /* Sort the pieces of "pw" according to their function value
1073 * expressions and then combine pairs of adjacent pieces with
1074 * the same such expression.
1076 * The sorting is performed in place because it does not
1077 * change the meaning of "pw", but care needs to be
1078 * taken not to change any possible other copies of "pw"
1079 * in case anything goes wrong.
1081 __isl_give PW *FN(PW,sort)(__isl_take PW *pw)
1083 int i, j;
1084 isl_set *set;
1086 if (!pw)
1087 return NULL;
1088 if (pw->n <= 1)
1089 return pw;
1090 if (isl_sort(pw->p, pw->n, sizeof(pw->p[0]),
1091 &FN(PW,sort_field_cmp), NULL) < 0)
1092 return FN(PW,free)(pw);
1093 for (i = pw->n - 1; i >= 1; --i) {
1094 if (!FN(EL,plain_is_equal)(pw->p[i - 1].FIELD, pw->p[i].FIELD))
1095 continue;
1096 set = isl_set_union(isl_set_copy(pw->p[i - 1].set),
1097 isl_set_copy(pw->p[i].set));
1098 if (!set)
1099 return FN(PW,free)(pw);
1100 isl_set_free(pw->p[i].set);
1101 FN(EL,free)(pw->p[i].FIELD);
1102 isl_set_free(pw->p[i - 1].set);
1103 pw->p[i - 1].set = set;
1104 for (j = i + 1; j < pw->n; ++j)
1105 pw->p[j - 1] = pw->p[j];
1106 pw->n--;
1109 return pw;
1112 /* Coalesce the domains of "pw".
1114 * Prior to the actual coalescing, first sort the pieces such that
1115 * pieces with the same function value expression are combined
1116 * into a single piece, the combined domain of which can then
1117 * be coalesced.
1119 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
1121 int i;
1123 pw = FN(PW,sort)(pw);
1124 if (!pw)
1125 return NULL;
1127 for (i = 0; i < pw->n; ++i) {
1128 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
1129 if (!pw->p[i].set)
1130 goto error;
1133 return pw;
1134 error:
1135 FN(PW,free)(pw);
1136 return NULL;
1139 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
1141 return pw ? isl_space_get_ctx(pw->dim) : NULL;
1144 #ifndef NO_INVOLVES_DIMS
1145 isl_bool FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
1146 unsigned first, unsigned n)
1148 int i;
1149 enum isl_dim_type set_type;
1151 if (!pw)
1152 return isl_bool_error;
1153 if (pw->n == 0 || n == 0)
1154 return isl_bool_false;
1156 set_type = type == isl_dim_in ? isl_dim_set : type;
1158 for (i = 0; i < pw->n; ++i) {
1159 isl_bool involves = FN(EL,involves_dims)(pw->p[i].FIELD,
1160 type, first, n);
1161 if (involves < 0 || involves)
1162 return involves;
1163 involves = isl_set_involves_dims(pw->p[i].set,
1164 set_type, first, n);
1165 if (involves < 0 || involves)
1166 return involves;
1168 return isl_bool_false;
1170 #endif
1172 __isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
1173 enum isl_dim_type type, unsigned pos, const char *s)
1175 int i;
1176 enum isl_dim_type set_type;
1178 pw = FN(PW,cow)(pw);
1179 if (!pw)
1180 return NULL;
1182 set_type = type == isl_dim_in ? isl_dim_set : type;
1184 pw->dim = isl_space_set_dim_name(pw->dim, type, pos, s);
1185 if (!pw->dim)
1186 goto error;
1188 for (i = 0; i < pw->n; ++i) {
1189 pw->p[i].set = isl_set_set_dim_name(pw->p[i].set,
1190 set_type, pos, s);
1191 if (!pw->p[i].set)
1192 goto error;
1193 pw->p[i].FIELD = FN(EL,set_dim_name)(pw->p[i].FIELD, type, pos, s);
1194 if (!pw->p[i].FIELD)
1195 goto error;
1198 return pw;
1199 error:
1200 FN(PW,free)(pw);
1201 return NULL;
1204 #ifndef NO_DROP_DIMS
1205 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
1206 enum isl_dim_type type, unsigned first, unsigned n)
1208 int i;
1209 enum isl_dim_type set_type;
1211 if (!pw)
1212 return NULL;
1213 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
1214 return pw;
1216 set_type = type == isl_dim_in ? isl_dim_set : type;
1218 pw = FN(PW,cow)(pw);
1219 if (!pw)
1220 return NULL;
1221 pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
1222 if (!pw->dim)
1223 goto error;
1224 for (i = 0; i < pw->n; ++i) {
1225 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
1226 if (!pw->p[i].FIELD)
1227 goto error;
1228 if (type == isl_dim_out)
1229 continue;
1230 pw->p[i].set = isl_set_drop(pw->p[i].set, set_type, first, n);
1231 if (!pw->p[i].set)
1232 goto error;
1235 return pw;
1236 error:
1237 FN(PW,free)(pw);
1238 return NULL;
1241 /* This function is very similar to drop_dims.
1242 * The only difference is that the cells may still involve
1243 * the specified dimensions. They are removed using
1244 * isl_set_project_out instead of isl_set_drop.
1246 __isl_give PW *FN(PW,project_out)(__isl_take PW *pw,
1247 enum isl_dim_type type, unsigned first, unsigned n)
1249 int i;
1250 enum isl_dim_type set_type;
1252 if (!pw)
1253 return NULL;
1254 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
1255 return pw;
1257 set_type = type == isl_dim_in ? isl_dim_set : type;
1259 pw = FN(PW,cow)(pw);
1260 if (!pw)
1261 return NULL;
1262 pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
1263 if (!pw->dim)
1264 goto error;
1265 for (i = 0; i < pw->n; ++i) {
1266 pw->p[i].set = isl_set_project_out(pw->p[i].set,
1267 set_type, first, n);
1268 if (!pw->p[i].set)
1269 goto error;
1270 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
1271 if (!pw->p[i].FIELD)
1272 goto error;
1275 return pw;
1276 error:
1277 FN(PW,free)(pw);
1278 return NULL;
1281 /* Project the domain of pw onto its parameter space.
1283 __isl_give PW *FN(PW,project_domain_on_params)(__isl_take PW *pw)
1285 isl_space *space;
1286 unsigned n;
1288 n = FN(PW,dim)(pw, isl_dim_in);
1289 pw = FN(PW,project_out)(pw, isl_dim_in, 0, n);
1290 space = FN(PW,get_domain_space)(pw);
1291 space = isl_space_params(space);
1292 pw = FN(PW,reset_domain_space)(pw, space);
1293 return pw;
1295 #endif
1297 #ifndef NO_INSERT_DIMS
1298 __isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
1299 unsigned first, unsigned n)
1301 int i;
1302 enum isl_dim_type set_type;
1304 if (!pw)
1305 return NULL;
1306 if (n == 0 && !isl_space_is_named_or_nested(pw->dim, type))
1307 return pw;
1309 set_type = type == isl_dim_in ? isl_dim_set : type;
1311 pw = FN(PW,cow)(pw);
1312 if (!pw)
1313 return NULL;
1315 pw->dim = isl_space_insert_dims(pw->dim, type, first, n);
1316 if (!pw->dim)
1317 goto error;
1319 for (i = 0; i < pw->n; ++i) {
1320 pw->p[i].set = isl_set_insert_dims(pw->p[i].set,
1321 set_type, first, n);
1322 if (!pw->p[i].set)
1323 goto error;
1324 pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
1325 type, first, n);
1326 if (!pw->p[i].FIELD)
1327 goto error;
1330 return pw;
1331 error:
1332 FN(PW,free)(pw);
1333 return NULL;
1335 #endif
1337 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
1338 enum isl_dim_type type, unsigned pos, isl_int v)
1340 int i;
1342 if (!pw)
1343 return NULL;
1345 if (type == isl_dim_in)
1346 type = isl_dim_set;
1348 pw = FN(PW,cow)(pw);
1349 if (!pw)
1350 return NULL;
1351 for (i = 0; i < pw->n; ++i) {
1352 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
1353 if (FN(PW,exploit_equalities_and_remove_if_empty)(pw, i) < 0)
1354 return FN(PW,free)(pw);
1357 return pw;
1360 /* Fix the value of the variable at position "pos" of type "type" of "pw"
1361 * to be equal to "v".
1363 __isl_give PW *FN(PW,fix_val)(__isl_take PW *pw,
1364 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
1366 if (!v)
1367 return FN(PW,free)(pw);
1368 if (!isl_val_is_int(v))
1369 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,
1370 "expecting integer value", goto error);
1372 pw = FN(PW,fix_dim)(pw, type, pos, v->n);
1373 isl_val_free(v);
1375 return pw;
1376 error:
1377 isl_val_free(v);
1378 return FN(PW,free)(pw);
1381 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
1383 return pw ? isl_space_dim(pw->dim, type) : 0;
1386 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
1387 enum isl_dim_type type, unsigned first, unsigned n)
1389 int i;
1391 if (!pw)
1392 return NULL;
1393 if (n == 0)
1394 return pw;
1396 if (type == isl_dim_in)
1397 type = isl_dim_set;
1399 pw = FN(PW,cow)(pw);
1400 if (!pw)
1401 return NULL;
1402 if (!pw->dim)
1403 goto error;
1404 for (i = 0; i < pw->n; ++i) {
1405 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
1406 if (!pw->p[i].set)
1407 goto error;
1410 return pw;
1411 error:
1412 FN(PW,free)(pw);
1413 return NULL;
1416 #ifndef NO_OPT
1417 /* Compute the maximal value attained by the piecewise quasipolynomial
1418 * on its domain or zero if the domain is empty.
1419 * In the worst case, the domain is scanned completely,
1420 * so the domain is assumed to be bounded.
1422 __isl_give isl_val *FN(PW,opt)(__isl_take PW *pw, int max)
1424 int i;
1425 isl_val *opt;
1427 if (!pw)
1428 return NULL;
1430 if (pw->n == 0) {
1431 opt = isl_val_zero(FN(PW,get_ctx)(pw));
1432 FN(PW,free)(pw);
1433 return opt;
1436 opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
1437 isl_set_copy(pw->p[0].set), max);
1438 for (i = 1; i < pw->n; ++i) {
1439 isl_val *opt_i;
1440 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
1441 isl_set_copy(pw->p[i].set), max);
1442 if (max)
1443 opt = isl_val_max(opt, opt_i);
1444 else
1445 opt = isl_val_min(opt, opt_i);
1448 FN(PW,free)(pw);
1449 return opt;
1452 __isl_give isl_val *FN(PW,max)(__isl_take PW *pw)
1454 return FN(PW,opt)(pw, 1);
1457 __isl_give isl_val *FN(PW,min)(__isl_take PW *pw)
1459 return FN(PW,opt)(pw, 0);
1461 #endif
1463 /* Return the space of "pw".
1465 __isl_keep isl_space *FN(PW,peek_space)(__isl_keep PW *pw)
1467 return pw ? pw->dim : NULL;
1470 __isl_give isl_space *FN(PW,get_space)(__isl_keep PW *pw)
1472 return isl_space_copy(FN(PW,peek_space)(pw));
1475 __isl_give isl_space *FN(PW,get_domain_space)(__isl_keep PW *pw)
1477 return pw ? isl_space_domain(isl_space_copy(pw->dim)) : NULL;
1480 /* Return the position of the dimension of the given type and name
1481 * in "pw".
1482 * Return -1 if no such dimension can be found.
1484 int FN(PW,find_dim_by_name)(__isl_keep PW *pw,
1485 enum isl_dim_type type, const char *name)
1487 if (!pw)
1488 return -1;
1489 return isl_space_find_dim_by_name(pw->dim, type, name);
1492 #ifndef NO_RESET_DIM
1493 /* Reset the space of "pw". Since we don't know if the elements
1494 * represent the spaces themselves or their domains, we pass along
1495 * both when we call their reset_space_and_domain.
1497 static __isl_give PW *FN(PW,reset_space_and_domain)(__isl_take PW *pw,
1498 __isl_take isl_space *space, __isl_take isl_space *domain)
1500 int i;
1502 pw = FN(PW,cow)(pw);
1503 if (!pw || !space || !domain)
1504 goto error;
1506 for (i = 0; i < pw->n; ++i) {
1507 pw->p[i].set = isl_set_reset_space(pw->p[i].set,
1508 isl_space_copy(domain));
1509 if (!pw->p[i].set)
1510 goto error;
1511 pw->p[i].FIELD = FN(EL,reset_space_and_domain)(pw->p[i].FIELD,
1512 isl_space_copy(space), isl_space_copy(domain));
1513 if (!pw->p[i].FIELD)
1514 goto error;
1517 isl_space_free(domain);
1519 isl_space_free(pw->dim);
1520 pw->dim = space;
1522 return pw;
1523 error:
1524 isl_space_free(domain);
1525 isl_space_free(space);
1526 FN(PW,free)(pw);
1527 return NULL;
1530 __isl_give PW *FN(PW,reset_domain_space)(__isl_take PW *pw,
1531 __isl_take isl_space *domain)
1533 isl_space *space;
1535 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
1536 FN(PW,get_space)(pw));
1537 return FN(PW,reset_space_and_domain)(pw, space, domain);
1540 __isl_give PW *FN(PW,reset_space)(__isl_take PW *pw, __isl_take isl_space *dim)
1542 isl_space *domain;
1544 domain = isl_space_domain(isl_space_copy(dim));
1545 return FN(PW,reset_space_and_domain)(pw, dim, domain);
1548 __isl_give PW *FN(PW,set_tuple_id)(__isl_take PW *pw, enum isl_dim_type type,
1549 __isl_take isl_id *id)
1551 isl_space *space;
1553 pw = FN(PW,cow)(pw);
1554 if (!pw)
1555 goto error;
1557 space = FN(PW,get_space)(pw);
1558 space = isl_space_set_tuple_id(space, type, id);
1560 return FN(PW,reset_space)(pw, space);
1561 error:
1562 isl_id_free(id);
1563 return FN(PW,free)(pw);
1566 /* Drop the id on the specified tuple.
1568 __isl_give PW *FN(PW,reset_tuple_id)(__isl_take PW *pw, enum isl_dim_type type)
1570 isl_space *space;
1572 if (!pw)
1573 return NULL;
1574 if (!FN(PW,has_tuple_id)(pw, type))
1575 return pw;
1577 pw = FN(PW,cow)(pw);
1578 if (!pw)
1579 return NULL;
1581 space = FN(PW,get_space)(pw);
1582 space = isl_space_reset_tuple_id(space, type);
1584 return FN(PW,reset_space)(pw, space);
1587 __isl_give PW *FN(PW,set_dim_id)(__isl_take PW *pw,
1588 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1590 pw = FN(PW,cow)(pw);
1591 if (!pw)
1592 goto error;
1593 pw->dim = isl_space_set_dim_id(pw->dim, type, pos, id);
1594 return FN(PW,reset_space)(pw, isl_space_copy(pw->dim));
1595 error:
1596 isl_id_free(id);
1597 return FN(PW,free)(pw);
1599 #endif
1601 /* Reset the user pointer on all identifiers of parameters and tuples
1602 * of the space of "pw".
1604 __isl_give PW *FN(PW,reset_user)(__isl_take PW *pw)
1606 isl_space *space;
1608 space = FN(PW,get_space)(pw);
1609 space = isl_space_reset_user(space);
1611 return FN(PW,reset_space)(pw, space);
1614 isl_bool FN(PW,has_equal_space)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1616 if (!pw1 || !pw2)
1617 return isl_bool_error;
1619 return isl_space_is_equal(pw1->dim, pw2->dim);
1622 #ifndef NO_MORPH
1623 __isl_give PW *FN(PW,morph_domain)(__isl_take PW *pw,
1624 __isl_take isl_morph *morph)
1626 int i;
1627 isl_ctx *ctx;
1629 if (!pw || !morph)
1630 goto error;
1632 ctx = isl_space_get_ctx(pw->dim);
1633 isl_assert(ctx, isl_space_is_domain_internal(morph->dom->dim, pw->dim),
1634 goto error);
1636 pw = FN(PW,cow)(pw);
1637 if (!pw)
1638 goto error;
1639 pw->dim = isl_space_extend_domain_with_range(
1640 isl_space_copy(morph->ran->dim), pw->dim);
1641 if (!pw->dim)
1642 goto error;
1644 for (i = 0; i < pw->n; ++i) {
1645 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
1646 if (!pw->p[i].set)
1647 goto error;
1648 pw->p[i].FIELD = FN(EL,morph_domain)(pw->p[i].FIELD,
1649 isl_morph_copy(morph));
1650 if (!pw->p[i].FIELD)
1651 goto error;
1654 isl_morph_free(morph);
1656 return pw;
1657 error:
1658 FN(PW,free)(pw);
1659 isl_morph_free(morph);
1660 return NULL;
1662 #endif
1664 int FN(PW,n_piece)(__isl_keep PW *pw)
1666 return pw ? pw->n : 0;
1669 isl_stat FN(PW,foreach_piece)(__isl_keep PW *pw,
1670 isl_stat (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
1671 void *user)
1673 int i;
1675 if (!pw)
1676 return isl_stat_error;
1678 for (i = 0; i < pw->n; ++i)
1679 if (fn(isl_set_copy(pw->p[i].set),
1680 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
1681 return isl_stat_error;
1683 return isl_stat_ok;
1686 #ifndef NO_LIFT
1687 static isl_bool any_divs(__isl_keep isl_set *set)
1689 int i;
1691 if (!set)
1692 return isl_bool_error;
1694 for (i = 0; i < set->n; ++i)
1695 if (set->p[i]->n_div > 0)
1696 return isl_bool_true;
1698 return isl_bool_false;
1701 static isl_stat foreach_lifted_subset(__isl_take isl_set *set,
1702 __isl_take EL *el,
1703 isl_stat (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1704 void *user), void *user)
1706 int i;
1708 if (!set || !el)
1709 goto error;
1711 for (i = 0; i < set->n; ++i) {
1712 isl_set *lift;
1713 EL *copy;
1715 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
1716 lift = isl_set_lift(lift);
1718 copy = FN(EL,copy)(el);
1719 copy = FN(EL,lift)(copy, isl_set_get_space(lift));
1721 if (fn(lift, copy, user) < 0)
1722 goto error;
1725 isl_set_free(set);
1726 FN(EL,free)(el);
1728 return isl_stat_ok;
1729 error:
1730 isl_set_free(set);
1731 FN(EL,free)(el);
1732 return isl_stat_error;
1735 isl_stat FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
1736 isl_stat (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1737 void *user), void *user)
1739 int i;
1741 if (!pw)
1742 return isl_stat_error;
1744 for (i = 0; i < pw->n; ++i) {
1745 isl_bool any;
1746 isl_set *set;
1747 EL *el;
1749 any = any_divs(pw->p[i].set);
1750 if (any < 0)
1751 return isl_stat_error;
1752 set = isl_set_copy(pw->p[i].set);
1753 el = FN(EL,copy)(pw->p[i].FIELD);
1754 if (!any) {
1755 if (fn(set, el, user) < 0)
1756 return isl_stat_error;
1757 continue;
1759 if (foreach_lifted_subset(set, el, fn, user) < 0)
1760 return isl_stat_error;
1763 return isl_stat_ok;
1765 #endif
1767 #ifndef NO_MOVE_DIMS
1768 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
1769 enum isl_dim_type dst_type, unsigned dst_pos,
1770 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1772 int i;
1774 pw = FN(PW,cow)(pw);
1775 if (!pw)
1776 return NULL;
1778 pw->dim = isl_space_move_dims(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
1779 if (!pw->dim)
1780 goto error;
1782 for (i = 0; i < pw->n; ++i) {
1783 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
1784 dst_type, dst_pos, src_type, src_pos, n);
1785 if (!pw->p[i].FIELD)
1786 goto error;
1789 if (dst_type == isl_dim_in)
1790 dst_type = isl_dim_set;
1791 if (src_type == isl_dim_in)
1792 src_type = isl_dim_set;
1794 for (i = 0; i < pw->n; ++i) {
1795 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
1796 dst_type, dst_pos,
1797 src_type, src_pos, n);
1798 if (!pw->p[i].set)
1799 goto error;
1802 return pw;
1803 error:
1804 FN(PW,free)(pw);
1805 return NULL;
1807 #endif
1809 __isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
1811 int i;
1813 if (isl_int_is_one(v))
1814 return pw;
1815 if (pw && DEFAULT_IS_ZERO && isl_int_is_zero(v)) {
1816 PW *zero;
1817 isl_space *dim = FN(PW,get_space)(pw);
1818 #ifdef HAS_TYPE
1819 zero = FN(PW,ZERO)(dim, pw->type);
1820 #else
1821 zero = FN(PW,ZERO)(dim);
1822 #endif
1823 FN(PW,free)(pw);
1824 return zero;
1826 pw = FN(PW,cow)(pw);
1827 if (!pw)
1828 return NULL;
1829 if (pw->n == 0)
1830 return pw;
1832 #ifdef HAS_TYPE
1833 if (isl_int_is_neg(v))
1834 pw->type = isl_fold_type_negate(pw->type);
1835 #endif
1836 for (i = 0; i < pw->n; ++i) {
1837 pw->p[i].FIELD = FN(EL,scale)(pw->p[i].FIELD, v);
1838 if (!pw->p[i].FIELD)
1839 goto error;
1842 return pw;
1843 error:
1844 FN(PW,free)(pw);
1845 return NULL;
1848 /* Multiply the pieces of "pw" by "v" and return the result.
1850 __isl_give PW *FN(PW,scale_val)(__isl_take PW *pw, __isl_take isl_val *v)
1852 int i;
1854 if (!pw || !v)
1855 goto error;
1857 if (isl_val_is_one(v)) {
1858 isl_val_free(v);
1859 return pw;
1861 if (pw && DEFAULT_IS_ZERO && isl_val_is_zero(v)) {
1862 PW *zero;
1863 isl_space *space = FN(PW,get_space)(pw);
1864 #ifdef HAS_TYPE
1865 zero = FN(PW,ZERO)(space, pw->type);
1866 #else
1867 zero = FN(PW,ZERO)(space);
1868 #endif
1869 FN(PW,free)(pw);
1870 isl_val_free(v);
1871 return zero;
1873 if (pw->n == 0) {
1874 isl_val_free(v);
1875 return pw;
1877 pw = FN(PW,cow)(pw);
1878 if (!pw)
1879 goto error;
1881 #ifdef HAS_TYPE
1882 if (isl_val_is_neg(v))
1883 pw->type = isl_fold_type_negate(pw->type);
1884 #endif
1885 for (i = 0; i < pw->n; ++i) {
1886 pw->p[i].FIELD = FN(EL,scale_val)(pw->p[i].FIELD,
1887 isl_val_copy(v));
1888 if (!pw->p[i].FIELD)
1889 goto error;
1892 isl_val_free(v);
1893 return pw;
1894 error:
1895 isl_val_free(v);
1896 FN(PW,free)(pw);
1897 return NULL;
1900 /* Divide the pieces of "pw" by "v" and return the result.
1902 __isl_give PW *FN(PW,scale_down_val)(__isl_take PW *pw, __isl_take isl_val *v)
1904 int i;
1906 if (!pw || !v)
1907 goto error;
1909 if (isl_val_is_one(v)) {
1910 isl_val_free(v);
1911 return pw;
1914 if (!isl_val_is_rat(v))
1915 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1916 "expecting rational factor", goto error);
1917 if (isl_val_is_zero(v))
1918 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1919 "cannot scale down by zero", goto error);
1921 if (pw->n == 0) {
1922 isl_val_free(v);
1923 return pw;
1925 pw = FN(PW,cow)(pw);
1926 if (!pw)
1927 goto error;
1929 #ifdef HAS_TYPE
1930 if (isl_val_is_neg(v))
1931 pw->type = isl_fold_type_negate(pw->type);
1932 #endif
1933 for (i = 0; i < pw->n; ++i) {
1934 pw->p[i].FIELD = FN(EL,scale_down_val)(pw->p[i].FIELD,
1935 isl_val_copy(v));
1936 if (!pw->p[i].FIELD)
1937 goto error;
1940 isl_val_free(v);
1941 return pw;
1942 error:
1943 isl_val_free(v);
1944 FN(PW,free)(pw);
1945 return NULL;
1948 __isl_give PW *FN(PW,scale)(__isl_take PW *pw, isl_int v)
1950 return FN(PW,mul_isl_int)(pw, v);
1953 /* Apply some normalization to "pw".
1954 * In particular, sort the pieces according to their function value
1955 * expressions, combining pairs of adjacent pieces with
1956 * the same such expression, and then normalize the domains of the pieces.
1958 * We normalize in place, but if anything goes wrong we need
1959 * to return NULL, so we need to make sure we don't change the
1960 * meaning of any possible other copies of "pw".
1962 __isl_give PW *FN(PW,normalize)(__isl_take PW *pw)
1964 int i;
1965 isl_set *set;
1967 pw = FN(PW,sort)(pw);
1968 if (!pw)
1969 return NULL;
1970 for (i = 0; i < pw->n; ++i) {
1971 set = isl_set_normalize(isl_set_copy(pw->p[i].set));
1972 if (!set)
1973 return FN(PW,free)(pw);
1974 isl_set_free(pw->p[i].set);
1975 pw->p[i].set = set;
1978 return pw;
1981 /* Is pw1 obviously equal to pw2?
1982 * That is, do they have obviously identical cells and obviously identical
1983 * elements on each cell?
1985 * If "pw1" or "pw2" contain any NaNs, then they are considered
1986 * not to be the same. A NaN is not equal to anything, not even
1987 * to another NaN.
1989 isl_bool FN(PW,plain_is_equal)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1991 int i;
1992 isl_bool equal, has_nan;
1994 if (!pw1 || !pw2)
1995 return isl_bool_error;
1997 has_nan = FN(PW,involves_nan)(pw1);
1998 if (has_nan >= 0 && !has_nan)
1999 has_nan = FN(PW,involves_nan)(pw2);
2000 if (has_nan < 0 || has_nan)
2001 return isl_bool_not(has_nan);
2003 if (pw1 == pw2)
2004 return isl_bool_true;
2005 if (!isl_space_is_equal(pw1->dim, pw2->dim))
2006 return isl_bool_false;
2008 pw1 = FN(PW,copy)(pw1);
2009 pw2 = FN(PW,copy)(pw2);
2010 pw1 = FN(PW,normalize)(pw1);
2011 pw2 = FN(PW,normalize)(pw2);
2012 if (!pw1 || !pw2)
2013 goto error;
2015 equal = pw1->n == pw2->n;
2016 for (i = 0; equal && i < pw1->n; ++i) {
2017 equal = isl_set_plain_is_equal(pw1->p[i].set, pw2->p[i].set);
2018 if (equal < 0)
2019 goto error;
2020 if (!equal)
2021 break;
2022 equal = FN(EL,plain_is_equal)(pw1->p[i].FIELD, pw2->p[i].FIELD);
2023 if (equal < 0)
2024 goto error;
2027 FN(PW,free)(pw1);
2028 FN(PW,free)(pw2);
2029 return equal;
2030 error:
2031 FN(PW,free)(pw1);
2032 FN(PW,free)(pw2);
2033 return isl_bool_error;
2036 /* Does "pw" involve any NaNs?
2038 isl_bool FN(PW,involves_nan)(__isl_keep PW *pw)
2040 int i;
2042 if (!pw)
2043 return isl_bool_error;
2044 if (pw->n == 0)
2045 return isl_bool_false;
2047 for (i = 0; i < pw->n; ++i) {
2048 isl_bool has_nan = FN(EL,involves_nan)(pw->p[i].FIELD);
2049 if (has_nan < 0 || has_nan)
2050 return has_nan;
2053 return isl_bool_false;
2056 #ifndef NO_PULLBACK
2057 static __isl_give PW *FN(PW,align_params_pw_multi_aff_and)(__isl_take PW *pw,
2058 __isl_take isl_multi_aff *ma,
2059 __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_multi_aff *ma))
2061 isl_ctx *ctx;
2062 isl_bool equal_params;
2063 isl_space *ma_space;
2065 ma_space = isl_multi_aff_get_space(ma);
2066 if (!pw || !ma || !ma_space)
2067 goto error;
2068 equal_params = isl_space_has_equal_params(pw->dim, ma_space);
2069 if (equal_params < 0)
2070 goto error;
2071 if (equal_params) {
2072 isl_space_free(ma_space);
2073 return fn(pw, ma);
2075 ctx = FN(PW,get_ctx)(pw);
2076 if (!isl_space_has_named_params(pw->dim) ||
2077 !isl_space_has_named_params(ma_space))
2078 isl_die(ctx, isl_error_invalid,
2079 "unaligned unnamed parameters", goto error);
2080 pw = FN(PW,align_params)(pw, ma_space);
2081 ma = isl_multi_aff_align_params(ma, FN(PW,get_space)(pw));
2082 return fn(pw, ma);
2083 error:
2084 isl_space_free(ma_space);
2085 FN(PW,free)(pw);
2086 isl_multi_aff_free(ma);
2087 return NULL;
2090 static __isl_give PW *FN(PW,align_params_pw_pw_multi_aff_and)(__isl_take PW *pw,
2091 __isl_take isl_pw_multi_aff *pma,
2092 __isl_give PW *(*fn)(__isl_take PW *pw,
2093 __isl_take isl_pw_multi_aff *ma))
2095 isl_ctx *ctx;
2096 isl_bool equal_params;
2097 isl_space *pma_space;
2099 pma_space = isl_pw_multi_aff_get_space(pma);
2100 if (!pw || !pma || !pma_space)
2101 goto error;
2102 equal_params = isl_space_has_equal_params(pw->dim, pma_space);
2103 if (equal_params < 0)
2104 goto error;
2105 if (equal_params) {
2106 isl_space_free(pma_space);
2107 return fn(pw, pma);
2109 ctx = FN(PW,get_ctx)(pw);
2110 if (!isl_space_has_named_params(pw->dim) ||
2111 !isl_space_has_named_params(pma_space))
2112 isl_die(ctx, isl_error_invalid,
2113 "unaligned unnamed parameters", goto error);
2114 pw = FN(PW,align_params)(pw, pma_space);
2115 pma = isl_pw_multi_aff_align_params(pma, FN(PW,get_space)(pw));
2116 return fn(pw, pma);
2117 error:
2118 isl_space_free(pma_space);
2119 FN(PW,free)(pw);
2120 isl_pw_multi_aff_free(pma);
2121 return NULL;
2124 /* Compute the pullback of "pw" by the function represented by "ma".
2125 * In other words, plug in "ma" in "pw".
2127 static __isl_give PW *FN(PW,pullback_multi_aff_aligned)(__isl_take PW *pw,
2128 __isl_take isl_multi_aff *ma)
2130 int i;
2131 isl_space *space = NULL;
2133 ma = isl_multi_aff_align_divs(ma);
2134 pw = FN(PW,cow)(pw);
2135 if (!pw || !ma)
2136 goto error;
2138 space = isl_space_join(isl_multi_aff_get_space(ma),
2139 FN(PW,get_space)(pw));
2141 for (i = 0; i < pw->n; ++i) {
2142 pw->p[i].set = isl_set_preimage_multi_aff(pw->p[i].set,
2143 isl_multi_aff_copy(ma));
2144 if (!pw->p[i].set)
2145 goto error;
2146 pw->p[i].FIELD = FN(EL,pullback_multi_aff)(pw->p[i].FIELD,
2147 isl_multi_aff_copy(ma));
2148 if (!pw->p[i].FIELD)
2149 goto error;
2152 pw = FN(PW,reset_space)(pw, space);
2153 isl_multi_aff_free(ma);
2154 return pw;
2155 error:
2156 isl_space_free(space);
2157 isl_multi_aff_free(ma);
2158 FN(PW,free)(pw);
2159 return NULL;
2162 __isl_give PW *FN(PW,pullback_multi_aff)(__isl_take PW *pw,
2163 __isl_take isl_multi_aff *ma)
2165 return FN(PW,align_params_pw_multi_aff_and)(pw, ma,
2166 &FN(PW,pullback_multi_aff_aligned));
2169 /* Compute the pullback of "pw" by the function represented by "pma".
2170 * In other words, plug in "pma" in "pw".
2172 static __isl_give PW *FN(PW,pullback_pw_multi_aff_aligned)(__isl_take PW *pw,
2173 __isl_take isl_pw_multi_aff *pma)
2175 int i;
2176 PW *res;
2178 if (!pma)
2179 goto error;
2181 if (pma->n == 0) {
2182 isl_space *space;
2183 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
2184 FN(PW,get_space)(pw));
2185 isl_pw_multi_aff_free(pma);
2186 res = FN(PW,empty)(space);
2187 FN(PW,free)(pw);
2188 return res;
2191 res = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
2192 isl_multi_aff_copy(pma->p[0].maff));
2193 res = FN(PW,intersect_domain)(res, isl_set_copy(pma->p[0].set));
2195 for (i = 1; i < pma->n; ++i) {
2196 PW *res_i;
2198 res_i = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
2199 isl_multi_aff_copy(pma->p[i].maff));
2200 res_i = FN(PW,intersect_domain)(res_i,
2201 isl_set_copy(pma->p[i].set));
2202 res = FN(PW,add_disjoint)(res, res_i);
2205 isl_pw_multi_aff_free(pma);
2206 FN(PW,free)(pw);
2207 return res;
2208 error:
2209 isl_pw_multi_aff_free(pma);
2210 FN(PW,free)(pw);
2211 return NULL;
2214 __isl_give PW *FN(PW,pullback_pw_multi_aff)(__isl_take PW *pw,
2215 __isl_take isl_pw_multi_aff *pma)
2217 return FN(PW,align_params_pw_pw_multi_aff_and)(pw, pma,
2218 &FN(PW,pullback_pw_multi_aff_aligned));
2220 #endif