isl_qpolynomial_div_pow: allow nested divs
[isl.git] / isl_pw_templ.c
blob7f780b25a668426f22216fe10a014b947cb960c9
1 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
2 #define FN(TYPE,NAME) xFN(TYPE,NAME)
3 #define xS(TYPE,NAME) struct TYPE ## _ ## NAME
4 #define S(TYPE,NAME) xS(TYPE,NAME)
6 #ifdef HAS_TYPE
7 static __isl_give PW *FN(PW,alloc_)(__isl_take isl_dim *dim,
8 enum isl_fold type, int n)
9 #else
10 static __isl_give PW *FN(PW,alloc_)(__isl_take isl_dim *dim, int n)
11 #endif
13 struct PW *pw;
15 if (!dim)
16 return NULL;
17 isl_assert(dim->ctx, n >= 0, goto error);
18 pw = isl_alloc(dim->ctx, struct PW,
19 sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
20 if (!pw)
21 goto error;
23 pw->ref = 1;
24 #ifdef HAS_TYPE
25 pw->type = type;
26 #endif
27 pw->size = n;
28 pw->n = 0;
29 pw->dim = dim;
30 return pw;
31 error:
32 isl_dim_free(dim);
33 return NULL;
36 #ifdef HAS_TYPE
37 __isl_give PW *FN(PW,zero)(__isl_take isl_dim *dim, enum isl_fold type)
39 return FN(PW,alloc_)(dim, type, 0);
41 #else
42 __isl_give PW *FN(PW,zero)(__isl_take isl_dim *dim)
44 return FN(PW,alloc_)(dim, 0);
46 #endif
48 __isl_give PW *FN(PW,add_piece)(__isl_take PW *pw,
49 __isl_take isl_set *set, __isl_take EL *el)
51 if (!pw || !set || !el)
52 goto error;
54 if (isl_set_fast_is_empty(set) || FN(EL,IS_ZERO)(el)) {
55 isl_set_free(set);
56 FN(EL,free)(el);
57 return pw;
60 #ifdef HAS_TYPE
61 if (pw->type != el->type)
62 isl_die(set->ctx, isl_error_invalid, "fold types don't match",
63 goto error);
64 #endif
65 isl_assert(set->ctx, isl_dim_equal(pw->dim, el->dim), goto error);
66 isl_assert(set->ctx, pw->n < pw->size, goto error);
68 pw->p[pw->n].set = set;
69 pw->p[pw->n].FIELD = el;
70 pw->n++;
72 return pw;
73 error:
74 FN(PW,free)(pw);
75 isl_set_free(set);
76 FN(EL,free)(el);
77 return NULL;
80 #ifdef HAS_TYPE
81 __isl_give PW *FN(PW,alloc)(enum isl_fold type,
82 __isl_take isl_set *set, __isl_take EL *el)
83 #else
84 __isl_give PW *FN(PW,alloc)(__isl_take isl_set *set, __isl_take EL *el)
85 #endif
87 PW *pw;
89 if (!set || !el)
90 goto error;
92 #ifdef HAS_TYPE
93 pw = FN(PW,alloc_)(isl_set_get_dim(set), type, 1);
94 #else
95 pw = FN(PW,alloc_)(isl_set_get_dim(set), 1);
96 #endif
98 return FN(PW,add_piece)(pw, set, el);
99 error:
100 isl_set_free(set);
101 FN(EL,free)(el);
102 return NULL;
105 __isl_give PW *FN(PW,dup)(__isl_keep PW *pw)
107 int i;
108 PW *dup;
110 if (!pw)
111 return NULL;
113 #ifdef HAS_TYPE
114 dup = FN(PW,alloc_)(isl_dim_copy(pw->dim), pw->type, pw->n);
115 #else
116 dup = FN(PW,alloc_)(isl_dim_copy(pw->dim), pw->n);
117 #endif
118 if (!dup)
119 return NULL;
121 for (i = 0; i < pw->n; ++i)
122 dup = FN(PW,add_piece)(dup, isl_set_copy(pw->p[i].set),
123 FN(EL,copy)(pw->p[i].FIELD));
125 return dup;
126 error:
127 FN(PW,free)(dup);
128 return NULL;
131 __isl_give PW *FN(PW,cow)(__isl_take PW *pw)
133 if (!pw)
134 return NULL;
136 if (pw->ref == 1)
137 return pw;
138 pw->ref--;
139 return FN(PW,dup)(pw);
142 __isl_give PW *FN(PW,copy)(__isl_keep PW *pw)
144 if (!pw)
145 return NULL;
147 pw->ref++;
148 return pw;
151 void FN(PW,free)(__isl_take PW *pw)
153 int i;
155 if (!pw)
156 return;
157 if (--pw->ref > 0)
158 return;
160 for (i = 0; i < pw->n; ++i) {
161 isl_set_free(pw->p[i].set);
162 FN(EL,free)(pw->p[i].FIELD);
164 isl_dim_free(pw->dim);
165 free(pw);
168 int FN(PW,is_zero)(__isl_keep PW *pw)
170 if (!pw)
171 return -1;
173 return pw->n == 0;
176 __isl_give PW *FN(PW,add)(__isl_take PW *pw1, __isl_take PW *pw2)
178 int i, j, n;
179 struct PW *res;
180 isl_set *set;
182 if (!pw1 || !pw2)
183 goto error;
185 #ifdef HAS_TYPE
186 if (pw1->type != pw2->type)
187 isl_die(pw1->dim->ctx, isl_error_invalid,
188 "fold types don't match", goto error);
189 #endif
190 isl_assert(pw1->dim->ctx, isl_dim_equal(pw1->dim, pw2->dim), goto error);
192 if (FN(PW,is_zero)(pw1)) {
193 FN(PW,free)(pw1);
194 return pw2;
197 if (FN(PW,is_zero)(pw2)) {
198 FN(PW,free)(pw2);
199 return pw1;
202 n = (pw1->n + 1) * (pw2->n + 1);
203 #ifdef HAS_TYPE
204 res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), pw1->type, n);
205 #else
206 res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), n);
207 #endif
209 for (i = 0; i < pw1->n; ++i) {
210 set = isl_set_copy(pw1->p[i].set);
211 for (j = 0; j < pw2->n; ++j) {
212 struct isl_set *common;
213 EL *sum;
214 set = isl_set_subtract(set,
215 isl_set_copy(pw2->p[j].set));
216 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
217 isl_set_copy(pw2->p[j].set));
218 if (isl_set_fast_is_empty(common)) {
219 isl_set_free(common);
220 continue;
223 sum = FN(EL,add_on_domain)(common,
224 FN(EL,copy)(pw1->p[i].FIELD),
225 FN(EL,copy)(pw2->p[j].FIELD));
227 res = FN(PW,add_piece)(res, common, sum);
229 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw1->p[i].FIELD));
232 for (j = 0; j < pw2->n; ++j) {
233 set = isl_set_copy(pw2->p[j].set);
234 for (i = 0; i < pw1->n; ++i)
235 set = isl_set_subtract(set,
236 isl_set_copy(pw1->p[i].set));
237 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw2->p[j].FIELD));
240 FN(PW,free)(pw1);
241 FN(PW,free)(pw2);
243 return res;
244 error:
245 FN(PW,free)(pw1);
246 FN(PW,free)(pw2);
247 return NULL;
250 __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
252 int i;
253 PW *res;
255 if (!pw1 || !pw2)
256 goto error;
258 #ifdef HAS_TYPE
259 if (pw1->type != pw2->type)
260 isl_die(pw1->dim->ctx, isl_error_invalid,
261 "fold types don't match", goto error);
262 #endif
263 isl_assert(pw1->dim->ctx, isl_dim_equal(pw1->dim, pw2->dim), goto error);
265 if (FN(PW,is_zero)(pw1)) {
266 FN(PW,free)(pw1);
267 return pw2;
270 if (FN(PW,is_zero)(pw2)) {
271 FN(PW,free)(pw2);
272 return pw1;
275 #ifdef HAS_TYPE
276 res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), pw1->type, pw1->n + pw2->n);
277 #else
278 res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), pw1->n + pw2->n);
279 #endif
281 for (i = 0; i < pw1->n; ++i)
282 res = FN(PW,add_piece)(res,
283 isl_set_copy(pw1->p[i].set),
284 FN(EL,copy)(pw1->p[i].FIELD));
286 for (i = 0; i < pw2->n; ++i)
287 res = FN(PW,add_piece)(res,
288 isl_set_copy(pw2->p[i].set),
289 FN(EL,copy)(pw2->p[i].FIELD));
291 FN(PW,free)(pw1);
292 FN(PW,free)(pw2);
294 return res;
295 error:
296 FN(PW,free)(pw1);
297 FN(PW,free)(pw2);
298 return NULL;
301 __isl_give isl_qpolynomial *FN(PW,eval)(__isl_take PW *pw,
302 __isl_take isl_point *pnt)
304 int i;
305 int found = 0;
306 isl_qpolynomial *qp;
308 if (!pw || !pnt)
309 goto error;
310 isl_assert(pnt->dim->ctx, isl_dim_equal(pnt->dim, pw->dim), goto error);
312 for (i = 0; i < pw->n; ++i) {
313 found = isl_set_contains_point(pw->p[i].set, pnt);
314 if (found < 0)
315 goto error;
316 if (found)
317 break;
319 if (found)
320 qp = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
321 isl_point_copy(pnt));
322 else
323 qp = isl_qpolynomial_zero(isl_dim_copy(pw->dim));
324 FN(PW,free)(pw);
325 isl_point_free(pnt);
326 return qp;
327 error:
328 FN(PW,free)(pw);
329 isl_point_free(pnt);
330 return NULL;
333 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
335 int i;
336 isl_set *dom;
338 if (!pw)
339 return NULL;
341 dom = isl_set_empty(isl_dim_copy(pw->dim));
342 for (i = 0; i < pw->n; ++i)
343 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
345 FN(PW,free)(pw);
347 return dom;
350 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw, __isl_take isl_set *set)
352 int i;
354 if (!pw || !set)
355 goto error;
357 if (pw->n == 0) {
358 isl_set_free(set);
359 return pw;
362 pw = FN(PW,cow)(pw);
363 if (!pw)
364 goto error;
366 for (i = pw->n - 1; i >= 0; --i) {
367 isl_basic_set *aff;
368 pw->p[i].set = isl_set_intersect(pw->p[i].set, isl_set_copy(set));
369 if (!pw->p[i].set)
370 goto error;
371 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
372 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD,
373 aff);
374 if (isl_set_fast_is_empty(pw->p[i].set)) {
375 isl_set_free(pw->p[i].set);
376 FN(EL,free)(pw->p[i].FIELD);
377 if (i != pw->n - 1)
378 pw->p[i] = pw->p[pw->n - 1];
379 pw->n--;
383 isl_set_free(set);
384 return pw;
385 error:
386 isl_set_free(set);
387 FN(PW,free)(pw);
388 return NULL;
391 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
393 int i;
394 isl_basic_set *hull = NULL;
396 if (!pw || !context)
397 goto error;
399 if (pw->n == 0) {
400 isl_set_free(context);
401 return pw;
404 context = isl_set_compute_divs(context);
405 hull = isl_set_simple_hull(isl_set_copy(context));
407 pw = FN(PW,cow)(pw);
408 if (!pw)
409 goto error;
411 for (i = pw->n - 1; i >= 0; --i) {
412 isl_basic_set *aff;
413 pw->p[i].set = isl_set_intersect(pw->p[i].set,
414 isl_set_copy(context));
415 if (!pw->p[i].set)
416 goto error;
417 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
418 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD,
419 aff);
420 pw->p[i].set = isl_set_gist_basic_set(pw->p[i].set,
421 isl_basic_set_copy(hull));
422 if (!pw->p[i].set)
423 goto error;
424 if (isl_set_fast_is_empty(pw->p[i].set)) {
425 isl_set_free(pw->p[i].set);
426 FN(EL,free)(pw->p[i].FIELD);
427 if (i != pw->n - 1)
428 pw->p[i] = pw->p[pw->n - 1];
429 pw->n--;
433 isl_basic_set_free(hull);
434 isl_set_free(context);
436 return pw;
437 error:
438 FN(PW,free)(pw);
439 isl_basic_set_free(hull);
440 isl_set_free(context);
441 return NULL;
444 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
446 int i, j;
448 if (!pw)
449 return NULL;
450 if (pw->n == 0)
451 return pw;
453 for (i = pw->n - 1; i >= 0; --i) {
454 for (j = i - 1; j >= 0; --j) {
455 if (!FN(EL,is_equal)(pw->p[i].FIELD, pw->p[j].FIELD))
456 continue;
457 pw->p[j].set = isl_set_union(pw->p[j].set,
458 pw->p[i].set);
459 FN(EL,free)(pw->p[i].FIELD);
460 if (i != pw->n - 1)
461 pw->p[i] = pw->p[pw->n - 1];
462 pw->n--;
463 break;
465 if (j >= 0)
466 continue;
467 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
468 if (!pw->p[i].set)
469 goto error;
472 return pw;
473 error:
474 FN(PW,free)(pw);
475 return NULL;
478 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
480 return pw ? pw->dim->ctx : NULL;
483 int FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
484 unsigned first, unsigned n)
486 int i;
488 if (!pw)
489 return -1;
490 if (pw->n == 0 || n == 0)
491 return 0;
492 for (i = 0; i < pw->n; ++i) {
493 int involves = FN(EL,involves_dims)(pw->p[i].FIELD,
494 type, first, n);
495 if (involves < 0 || involves)
496 return involves;
498 return 0;
501 __isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
502 enum isl_dim_type type, unsigned pos, const char *s)
504 int i;
506 pw = FN(PW,cow)(pw);
507 if (!pw)
508 return NULL;
510 pw->dim = isl_dim_set_name(pw->dim, type, pos, s);
511 if (!pw->dim)
512 goto error;
514 for (i = 0; i < pw->n; ++i) {
515 pw->p[i].set = isl_set_set_dim_name(pw->p[i].set, type, pos, s);
516 if (!pw->p[i].set)
517 goto error;
518 pw->p[i].FIELD = FN(EL,set_dim_name)(pw->p[i].FIELD, type, pos, s);
519 if (!pw->p[i].FIELD)
520 goto error;
523 return pw;
524 error:
525 FN(PW,free)(pw);
526 return NULL;
529 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
530 enum isl_dim_type type, unsigned first, unsigned n)
532 int i;
534 if (!pw)
535 return NULL;
536 if (n == 0 && !isl_dim_get_tuple_name(pw->dim, type))
537 return pw;
539 pw = FN(PW,cow)(pw);
540 if (!pw)
541 return NULL;
542 pw->dim = isl_dim_drop(pw->dim, type, first, n);
543 if (!pw->dim)
544 goto error;
545 for (i = 0; i < pw->n; ++i) {
546 pw->p[i].set = isl_set_drop(pw->p[i].set, type, first, n);
547 if (!pw->p[i].set)
548 goto error;
549 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
550 if (!pw->p[i].FIELD)
551 goto error;
554 return pw;
555 error:
556 FN(PW,free)(pw);
557 return NULL;
560 __isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
561 unsigned first, unsigned n)
563 int i;
565 if (!pw)
566 return NULL;
567 if (n == 0 && !isl_dim_is_named_or_nested(pw->dim, type))
568 return pw;
570 pw = FN(PW,cow)(pw);
571 if (!pw)
572 return NULL;
574 pw->dim = isl_dim_insert(pw->dim, type, first, n);
575 if (!pw->dim)
576 goto error;
578 for (i = 0; i < pw->n; ++i) {
579 pw->p[i].set = isl_set_insert(pw->p[i].set, type, first, n);
580 if (!pw->p[i].set)
581 goto error;
582 pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
583 type, first, n);
584 if (!pw->p[i].FIELD)
585 goto error;
588 return pw;
589 error:
590 FN(PW,free)(pw);
591 return NULL;
594 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
595 enum isl_dim_type type, unsigned pos, isl_int v)
597 int i;
599 if (!pw)
600 return NULL;
602 pw = FN(PW,cow)(pw);
603 if (!pw)
604 return NULL;
605 for (i = 0; i < pw->n; ++i) {
606 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
607 if (!pw->p[i].set)
608 goto error;
611 return pw;
612 error:
613 FN(PW,free)(pw);
614 return NULL;
617 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
619 return pw ? isl_dim_size(pw->dim, type) : 0;
622 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
623 enum isl_dim_type type, unsigned first, unsigned n)
625 int i;
627 if (!pw)
628 return NULL;
629 if (n == 0)
630 return pw;
632 pw = FN(PW,cow)(pw);
633 if (!pw)
634 return NULL;
635 if (!pw->dim)
636 goto error;
637 for (i = 0; i < pw->n; ++i) {
638 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
639 if (!pw->p[i].set)
640 goto error;
643 return pw;
644 error:
645 FN(PW,free)(pw);
646 return NULL;
649 /* Compute the maximal value attained by the piecewise quasipolynomial
650 * on its domain or zero if the domain is empty.
651 * In the worst case, the domain is scanned completely,
652 * so the domain is assumed to be bounded.
654 __isl_give isl_qpolynomial *FN(PW,opt)(__isl_take PW *pw, int max)
656 int i;
657 isl_qpolynomial *opt;
659 if (!pw)
660 return NULL;
662 if (pw->n == 0) {
663 isl_dim *dim = isl_dim_copy(pw->dim);
664 FN(PW,free)(pw);
665 return isl_qpolynomial_zero(dim);
668 opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
669 isl_set_copy(pw->p[0].set), max);
670 for (i = 1; i < pw->n; ++i) {
671 isl_qpolynomial *opt_i;
672 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
673 isl_set_copy(pw->p[i].set), max);
674 if (max)
675 opt = isl_qpolynomial_max_cst(opt, opt_i);
676 else
677 opt = isl_qpolynomial_min_cst(opt, opt_i);
680 FN(PW,free)(pw);
681 return opt;
684 __isl_give isl_qpolynomial *FN(PW,max)(__isl_take PW *pw)
686 return FN(PW,opt)(pw, 1);
689 __isl_give isl_qpolynomial *FN(PW,min)(__isl_take PW *pw)
691 return FN(PW,opt)(pw, 0);
694 __isl_give isl_dim *FN(PW,get_dim)(__isl_keep PW *pw)
696 return pw ? isl_dim_copy(pw->dim) : NULL;
699 __isl_give PW *FN(PW,reset_dim)(__isl_take PW *pw, __isl_take isl_dim *dim)
701 int i;
703 pw = FN(PW,cow)(pw);
704 if (!pw || !dim)
705 goto error;
707 for (i = 0; i < pw->n; ++i) {
708 pw->p[i].set = isl_set_reset_dim(pw->p[i].set,
709 isl_dim_copy(dim));
710 if (!pw->p[i].set)
711 goto error;
712 pw->p[i].FIELD = FN(EL,reset_dim)(pw->p[i].FIELD,
713 isl_dim_copy(dim));
714 if (!pw->p[i].FIELD)
715 goto error;
717 isl_dim_free(pw->dim);
718 pw->dim = dim;
720 return pw;
721 error:
722 isl_dim_free(dim);
723 FN(PW,free)(pw);
724 return NULL;
727 int FN(PW,has_equal_dim)(__isl_keep PW *pw1, __isl_keep PW *pw2)
729 if (!pw1 || !pw2)
730 return -1;
732 return isl_dim_equal(pw1->dim, pw2->dim);
735 __isl_give PW *FN(PW,morph)(__isl_take PW *pw, __isl_take isl_morph *morph)
737 int i;
739 if (!pw || !morph)
740 goto error;
742 isl_assert(pw->dim->ctx, isl_dim_equal(pw->dim, morph->dom->dim),
743 goto error);
745 pw = FN(PW,cow)(pw);
746 if (!pw)
747 goto error;
748 isl_dim_free(pw->dim);
749 pw->dim = isl_dim_copy(morph->ran->dim);
750 if (!pw->dim)
751 goto error;
753 for (i = 0; i < pw->n; ++i) {
754 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
755 if (!pw->p[i].set)
756 goto error;
757 pw->p[i].FIELD = FN(EL,morph)(pw->p[i].FIELD,
758 isl_morph_copy(morph));
759 if (!pw->p[i].FIELD)
760 goto error;
763 isl_morph_free(morph);
765 return pw;
766 error:
767 FN(PW,free)(pw);
768 isl_morph_free(morph);
769 return NULL;
772 int FN(PW,foreach_piece)(__isl_keep PW *pw,
773 int (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
774 void *user)
776 int i;
778 if (!pw)
779 return -1;
781 for (i = 0; i < pw->n; ++i)
782 if (fn(isl_set_copy(pw->p[i].set),
783 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
784 return -1;
786 return 0;
789 static int any_divs(__isl_keep isl_set *set)
791 int i;
793 if (!set)
794 return -1;
796 for (i = 0; i < set->n; ++i)
797 if (set->p[i]->n_div > 0)
798 return 1;
800 return 0;
803 static int foreach_lifted_subset(__isl_take isl_set *set, __isl_take EL *el,
804 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
805 void *user), void *user)
807 int i;
809 if (!set || !el)
810 goto error;
812 for (i = 0; i < set->n; ++i) {
813 isl_set *lift;
814 EL *copy;
816 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
817 lift = isl_set_lift(lift);
819 copy = FN(EL,copy)(el);
820 copy = FN(EL,lift)(copy, isl_set_get_dim(lift));
822 if (fn(lift, copy, user) < 0)
823 goto error;
826 isl_set_free(set);
827 FN(EL,free)(el);
829 return 0;
830 error:
831 isl_set_free(set);
832 FN(EL,free)(el);
833 return -1;
836 int FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
837 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
838 void *user), void *user)
840 int i;
842 if (!pw)
843 return -1;
845 for (i = 0; i < pw->n; ++i) {
846 isl_set *set;
847 EL *el;
849 set = isl_set_copy(pw->p[i].set);
850 el = FN(EL,copy)(pw->p[i].FIELD);
851 if (!any_divs(set)) {
852 if (fn(set, el, user) < 0)
853 return -1;
854 continue;
856 if (foreach_lifted_subset(set, el, fn, user) < 0)
857 return -1;
860 return 0;
863 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
864 enum isl_dim_type dst_type, unsigned dst_pos,
865 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
867 int i;
869 pw = FN(PW,cow)(pw);
870 if (!pw)
871 return NULL;
873 pw->dim = isl_dim_move(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
874 if (!pw->dim)
875 goto error;
877 for (i = 0; i < pw->n; ++i) {
878 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
879 dst_type, dst_pos,
880 src_type, src_pos, n);
881 if (!pw->p[i].set)
882 goto error;
883 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
884 dst_type, dst_pos, src_type, src_pos, n);
885 if (!pw->p[i].FIELD)
886 goto error;
889 return pw;
890 error:
891 FN(PW,free)(pw);
892 return NULL;
895 __isl_give PW *FN(PW,realign)(__isl_take PW *pw, __isl_take isl_reordering *exp)
897 int i;
899 pw = FN(PW,cow)(pw);
900 if (!pw || !exp)
901 return NULL;
903 for (i = 0; i < pw->n; ++i) {
904 pw->p[i].set = isl_set_realign(pw->p[i].set,
905 isl_reordering_copy(exp));
906 if (!pw->p[i].set)
907 goto error;
908 pw->p[i].FIELD = FN(EL,realign)(pw->p[i].FIELD,
909 isl_reordering_copy(exp));
910 if (!pw->p[i].FIELD)
911 goto error;
914 pw = FN(PW,reset_dim)(pw, isl_dim_copy(exp->dim));
916 isl_reordering_free(exp);
917 return pw;
918 error:
919 isl_reordering_free(exp);
920 FN(PW,free)(pw);
921 return NULL;