isl_pw_qpolynomial_print: print modulo constraints in ISL_FORMAT_C
[isl.git] / isl_pw_templ.c
bloba56da74c7435feb3389ec342b7963ce3fa1146ec
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 hull = isl_set_simple_hull(isl_set_copy(context));
406 pw = FN(PW,cow)(pw);
407 if (!pw)
408 goto error;
410 for (i = 0; i < pw->n; ++i) {
411 pw->p[i].set = isl_set_gist_basic_set(pw->p[i].set,
412 isl_basic_set_copy(hull));
413 if (!pw->p[i].set)
414 goto error;
417 isl_basic_set_free(hull);
418 isl_set_free(context);
420 return pw;
421 error:
422 FN(PW,free)(pw);
423 isl_basic_set_free(hull);
424 isl_set_free(context);
425 return NULL;
428 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
430 int i, j;
432 if (!pw)
433 return NULL;
434 if (pw->n == 0)
435 return pw;
437 for (i = pw->n - 1; i >= 0; --i) {
438 for (j = i - 1; j >= 0; --j) {
439 if (!FN(EL,is_equal)(pw->p[i].FIELD, pw->p[j].FIELD))
440 continue;
441 pw->p[j].set = isl_set_union(pw->p[j].set,
442 pw->p[i].set);
443 FN(EL,free)(pw->p[i].FIELD);
444 if (i != pw->n - 1)
445 pw->p[i] = pw->p[pw->n - 1];
446 pw->n--;
447 break;
449 if (j >= 0)
450 continue;
451 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
452 if (!pw->p[i].set)
453 goto error;
456 return pw;
457 error:
458 FN(PW,free)(pw);
459 return NULL;
462 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
464 return pw ? pw->dim->ctx : NULL;
467 int FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
468 unsigned first, unsigned n)
470 int i;
472 if (!pw)
473 return -1;
474 if (pw->n == 0 || n == 0)
475 return 0;
476 for (i = 0; i < pw->n; ++i) {
477 int involves = FN(EL,involves_dims)(pw->p[i].FIELD,
478 type, first, n);
479 if (involves < 0 || involves)
480 return involves;
482 return 0;
485 __isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
486 enum isl_dim_type type, unsigned pos, const char *s)
488 int i;
490 pw = FN(PW,cow)(pw);
491 if (!pw)
492 return NULL;
494 pw->dim = isl_dim_set_name(pw->dim, type, pos, s);
495 if (!pw->dim)
496 goto error;
498 for (i = 0; i < pw->n; ++i) {
499 pw->p[i].set = isl_set_set_dim_name(pw->p[i].set, type, pos, s);
500 if (!pw->p[i].set)
501 goto error;
502 pw->p[i].FIELD = FN(EL,set_dim_name)(pw->p[i].FIELD, type, pos, s);
503 if (!pw->p[i].FIELD)
504 goto error;
507 return pw;
508 error:
509 FN(PW,free)(pw);
510 return NULL;
513 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
514 enum isl_dim_type type, unsigned first, unsigned n)
516 int i;
518 if (!pw)
519 return NULL;
520 if (n == 0 && !isl_dim_get_tuple_name(pw->dim, type))
521 return pw;
523 pw = FN(PW,cow)(pw);
524 if (!pw)
525 return NULL;
526 pw->dim = isl_dim_drop(pw->dim, type, first, n);
527 if (!pw->dim)
528 goto error;
529 for (i = 0; i < pw->n; ++i) {
530 pw->p[i].set = isl_set_drop(pw->p[i].set, type, first, n);
531 if (!pw->p[i].set)
532 goto error;
533 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
534 if (!pw->p[i].FIELD)
535 goto error;
538 return pw;
539 error:
540 FN(PW,free)(pw);
541 return NULL;
544 __isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
545 unsigned first, unsigned n)
547 int i;
549 if (!pw)
550 return NULL;
551 if (n == 0 && !isl_dim_is_named_or_nested(pw->dim, type))
552 return pw;
554 pw = FN(PW,cow)(pw);
555 if (!pw)
556 return NULL;
558 pw->dim = isl_dim_insert(pw->dim, type, first, n);
559 if (!pw->dim)
560 goto error;
562 for (i = 0; i < pw->n; ++i) {
563 pw->p[i].set = isl_set_insert(pw->p[i].set, type, first, n);
564 if (!pw->p[i].set)
565 goto error;
566 pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
567 type, first, n);
568 if (!pw->p[i].FIELD)
569 goto error;
572 return pw;
573 error:
574 FN(PW,free)(pw);
575 return NULL;
578 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
579 enum isl_dim_type type, unsigned pos, isl_int v)
581 int i;
583 if (!pw)
584 return NULL;
586 pw = FN(PW,cow)(pw);
587 if (!pw)
588 return NULL;
589 for (i = 0; i < pw->n; ++i) {
590 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
591 if (!pw->p[i].set)
592 goto error;
595 return pw;
596 error:
597 FN(PW,free)(pw);
598 return NULL;
601 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
603 return pw ? isl_dim_size(pw->dim, type) : 0;
606 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
607 enum isl_dim_type type, unsigned first, unsigned n)
609 int i;
611 if (!pw)
612 return NULL;
613 if (n == 0)
614 return pw;
616 pw = FN(PW,cow)(pw);
617 if (!pw)
618 return NULL;
619 if (!pw->dim)
620 goto error;
621 for (i = 0; i < pw->n; ++i) {
622 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
623 if (!pw->p[i].set)
624 goto error;
627 return pw;
628 error:
629 FN(PW,free)(pw);
630 return NULL;
633 /* Compute the maximal value attained by the piecewise quasipolynomial
634 * on its domain or zero if the domain is empty.
635 * In the worst case, the domain is scanned completely,
636 * so the domain is assumed to be bounded.
638 __isl_give isl_qpolynomial *FN(PW,opt)(__isl_take PW *pw, int max)
640 int i;
641 isl_qpolynomial *opt;
643 if (!pw)
644 return NULL;
646 if (pw->n == 0) {
647 isl_dim *dim = isl_dim_copy(pw->dim);
648 FN(PW,free)(pw);
649 return isl_qpolynomial_zero(dim);
652 opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
653 isl_set_copy(pw->p[0].set), max);
654 for (i = 1; i < pw->n; ++i) {
655 isl_qpolynomial *opt_i;
656 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
657 isl_set_copy(pw->p[i].set), max);
658 if (max)
659 opt = isl_qpolynomial_max_cst(opt, opt_i);
660 else
661 opt = isl_qpolynomial_min_cst(opt, opt_i);
664 FN(PW,free)(pw);
665 return opt;
668 __isl_give isl_qpolynomial *FN(PW,max)(__isl_take PW *pw)
670 return FN(PW,opt)(pw, 1);
673 __isl_give isl_qpolynomial *FN(PW,min)(__isl_take PW *pw)
675 return FN(PW,opt)(pw, 0);
678 __isl_give isl_dim *FN(PW,get_dim)(__isl_keep PW *pw)
680 return pw ? isl_dim_copy(pw->dim) : NULL;
683 __isl_give PW *FN(PW,reset_dim)(__isl_take PW *pw, __isl_take isl_dim *dim)
685 int i;
687 pw = FN(PW,cow)(pw);
688 if (!pw || !dim)
689 goto error;
691 for (i = 0; i < pw->n; ++i) {
692 pw->p[i].set = isl_set_reset_dim(pw->p[i].set,
693 isl_dim_copy(dim));
694 if (!pw->p[i].set)
695 goto error;
696 pw->p[i].FIELD = FN(EL,reset_dim)(pw->p[i].FIELD,
697 isl_dim_copy(dim));
698 if (!pw->p[i].FIELD)
699 goto error;
701 isl_dim_free(pw->dim);
702 pw->dim = dim;
704 return pw;
705 error:
706 isl_dim_free(dim);
707 FN(PW,free)(pw);
708 return NULL;
711 int FN(PW,has_equal_dim)(__isl_keep PW *pw1, __isl_keep PW *pw2)
713 if (!pw1 || !pw2)
714 return -1;
716 return isl_dim_equal(pw1->dim, pw2->dim);
719 __isl_give PW *FN(PW,morph)(__isl_take PW *pw, __isl_take isl_morph *morph)
721 int i;
723 if (!pw || !morph)
724 goto error;
726 isl_assert(pw->dim->ctx, isl_dim_equal(pw->dim, morph->dom->dim),
727 goto error);
729 pw = FN(PW,cow)(pw);
730 if (!pw)
731 goto error;
732 isl_dim_free(pw->dim);
733 pw->dim = isl_dim_copy(morph->ran->dim);
734 if (!pw->dim)
735 goto error;
737 for (i = 0; i < pw->n; ++i) {
738 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
739 if (!pw->p[i].set)
740 goto error;
741 pw->p[i].FIELD = FN(EL,morph)(pw->p[i].FIELD,
742 isl_morph_copy(morph));
743 if (!pw->p[i].FIELD)
744 goto error;
747 isl_morph_free(morph);
749 return pw;
750 error:
751 FN(PW,free)(pw);
752 isl_morph_free(morph);
753 return NULL;
756 int FN(PW,foreach_piece)(__isl_keep PW *pw,
757 int (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
758 void *user)
760 int i;
762 if (!pw)
763 return -1;
765 for (i = 0; i < pw->n; ++i)
766 if (fn(isl_set_copy(pw->p[i].set),
767 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
768 return -1;
770 return 0;
773 static int any_divs(__isl_keep isl_set *set)
775 int i;
777 if (!set)
778 return -1;
780 for (i = 0; i < set->n; ++i)
781 if (set->p[i]->n_div > 0)
782 return 1;
784 return 0;
787 static int foreach_lifted_subset(__isl_take isl_set *set, __isl_take EL *el,
788 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
789 void *user), void *user)
791 int i;
793 if (!set || !el)
794 goto error;
796 for (i = 0; i < set->n; ++i) {
797 isl_set *lift;
798 EL *copy;
800 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
801 lift = isl_set_lift(lift);
803 copy = FN(EL,copy)(el);
804 copy = FN(EL,lift)(copy, isl_set_get_dim(lift));
806 if (fn(lift, copy, user) < 0)
807 goto error;
810 isl_set_free(set);
811 FN(EL,free)(el);
813 return 0;
814 error:
815 isl_set_free(set);
816 FN(EL,free)(el);
817 return -1;
820 int FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
821 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
822 void *user), void *user)
824 int i;
826 if (!pw)
827 return -1;
829 for (i = 0; i < pw->n; ++i) {
830 isl_set *set;
831 EL *el;
833 set = isl_set_copy(pw->p[i].set);
834 el = FN(EL,copy)(pw->p[i].FIELD);
835 if (!any_divs(set)) {
836 if (fn(set, el, user) < 0)
837 return -1;
838 continue;
840 if (foreach_lifted_subset(set, el, fn, user) < 0)
841 return -1;
844 return 0;
847 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
848 enum isl_dim_type dst_type, unsigned dst_pos,
849 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
851 int i;
853 pw = FN(PW,cow)(pw);
854 if (!pw)
855 return NULL;
857 pw->dim = isl_dim_move(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
858 if (!pw->dim)
859 goto error;
861 for (i = 0; i < pw->n; ++i) {
862 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
863 dst_type, dst_pos,
864 src_type, src_pos, n);
865 if (!pw->p[i].set)
866 goto error;
867 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
868 dst_type, dst_pos, src_type, src_pos, n);
869 if (!pw->p[i].FIELD)
870 goto error;
873 return pw;
874 error:
875 FN(PW,free)(pw);
876 return NULL;
879 __isl_give PW *FN(PW,realign)(__isl_take PW *pw, __isl_take isl_reordering *exp)
881 int i;
883 pw = FN(PW,cow)(pw);
884 if (!pw || !exp)
885 return NULL;
887 for (i = 0; i < pw->n; ++i) {
888 pw->p[i].set = isl_set_realign(pw->p[i].set,
889 isl_reordering_copy(exp));
890 if (!pw->p[i].set)
891 goto error;
892 pw->p[i].FIELD = FN(EL,realign)(pw->p[i].FIELD,
893 isl_reordering_copy(exp));
894 if (!pw->p[i].FIELD)
895 goto error;
898 pw = FN(PW,reset_dim)(pw, isl_dim_copy(exp->dim));
900 isl_reordering_free(exp);
901 return pw;
902 error:
903 isl_reordering_free(exp);
904 FN(PW,free)(pw);
905 return NULL;