isl_bernstein.c: remove unused variable
[isl.git] / isl_pw_templ.c
bloba3c76a195711f655c50c89ddb2357d676b564374
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_plain_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;
128 __isl_give PW *FN(PW,cow)(__isl_take PW *pw)
130 if (!pw)
131 return NULL;
133 if (pw->ref == 1)
134 return pw;
135 pw->ref--;
136 return FN(PW,dup)(pw);
139 __isl_give PW *FN(PW,copy)(__isl_keep PW *pw)
141 if (!pw)
142 return NULL;
144 pw->ref++;
145 return pw;
148 void FN(PW,free)(__isl_take PW *pw)
150 int i;
152 if (!pw)
153 return;
154 if (--pw->ref > 0)
155 return;
157 for (i = 0; i < pw->n; ++i) {
158 isl_set_free(pw->p[i].set);
159 FN(EL,free)(pw->p[i].FIELD);
161 isl_dim_free(pw->dim);
162 free(pw);
165 int FN(PW,is_zero)(__isl_keep PW *pw)
167 if (!pw)
168 return -1;
170 return pw->n == 0;
173 __isl_give PW *FN(PW,add)(__isl_take PW *pw1, __isl_take PW *pw2)
175 int i, j, n;
176 struct PW *res;
177 isl_set *set;
179 if (!pw1 || !pw2)
180 goto error;
182 #ifdef HAS_TYPE
183 if (pw1->type != pw2->type)
184 isl_die(pw1->dim->ctx, isl_error_invalid,
185 "fold types don't match", goto error);
186 #endif
187 isl_assert(pw1->dim->ctx, isl_dim_equal(pw1->dim, pw2->dim), goto error);
189 if (FN(PW,is_zero)(pw1)) {
190 FN(PW,free)(pw1);
191 return pw2;
194 if (FN(PW,is_zero)(pw2)) {
195 FN(PW,free)(pw2);
196 return pw1;
199 n = (pw1->n + 1) * (pw2->n + 1);
200 #ifdef HAS_TYPE
201 res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), pw1->type, n);
202 #else
203 res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), n);
204 #endif
206 for (i = 0; i < pw1->n; ++i) {
207 set = isl_set_copy(pw1->p[i].set);
208 for (j = 0; j < pw2->n; ++j) {
209 struct isl_set *common;
210 EL *sum;
211 set = isl_set_subtract(set,
212 isl_set_copy(pw2->p[j].set));
213 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
214 isl_set_copy(pw2->p[j].set));
215 if (isl_set_plain_is_empty(common)) {
216 isl_set_free(common);
217 continue;
220 sum = FN(EL,add_on_domain)(common,
221 FN(EL,copy)(pw1->p[i].FIELD),
222 FN(EL,copy)(pw2->p[j].FIELD));
224 res = FN(PW,add_piece)(res, common, sum);
226 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw1->p[i].FIELD));
229 for (j = 0; j < pw2->n; ++j) {
230 set = isl_set_copy(pw2->p[j].set);
231 for (i = 0; i < pw1->n; ++i)
232 set = isl_set_subtract(set,
233 isl_set_copy(pw1->p[i].set));
234 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw2->p[j].FIELD));
237 FN(PW,free)(pw1);
238 FN(PW,free)(pw2);
240 return res;
241 error:
242 FN(PW,free)(pw1);
243 FN(PW,free)(pw2);
244 return NULL;
247 __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
249 int i;
250 PW *res;
252 if (!pw1 || !pw2)
253 goto error;
255 #ifdef HAS_TYPE
256 if (pw1->type != pw2->type)
257 isl_die(pw1->dim->ctx, isl_error_invalid,
258 "fold types don't match", goto error);
259 #endif
260 isl_assert(pw1->dim->ctx, isl_dim_equal(pw1->dim, pw2->dim), goto error);
262 if (FN(PW,is_zero)(pw1)) {
263 FN(PW,free)(pw1);
264 return pw2;
267 if (FN(PW,is_zero)(pw2)) {
268 FN(PW,free)(pw2);
269 return pw1;
272 #ifdef HAS_TYPE
273 res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), pw1->type, pw1->n + pw2->n);
274 #else
275 res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), pw1->n + pw2->n);
276 #endif
278 for (i = 0; i < pw1->n; ++i)
279 res = FN(PW,add_piece)(res,
280 isl_set_copy(pw1->p[i].set),
281 FN(EL,copy)(pw1->p[i].FIELD));
283 for (i = 0; i < pw2->n; ++i)
284 res = FN(PW,add_piece)(res,
285 isl_set_copy(pw2->p[i].set),
286 FN(EL,copy)(pw2->p[i].FIELD));
288 FN(PW,free)(pw1);
289 FN(PW,free)(pw2);
291 return res;
292 error:
293 FN(PW,free)(pw1);
294 FN(PW,free)(pw2);
295 return NULL;
298 __isl_give isl_qpolynomial *FN(PW,eval)(__isl_take PW *pw,
299 __isl_take isl_point *pnt)
301 int i;
302 int found = 0;
303 isl_qpolynomial *qp;
305 if (!pw || !pnt)
306 goto error;
307 isl_assert(pnt->dim->ctx, isl_dim_equal(pnt->dim, pw->dim), goto error);
309 for (i = 0; i < pw->n; ++i) {
310 found = isl_set_contains_point(pw->p[i].set, pnt);
311 if (found < 0)
312 goto error;
313 if (found)
314 break;
316 if (found)
317 qp = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
318 isl_point_copy(pnt));
319 else
320 qp = isl_qpolynomial_zero(isl_dim_copy(pw->dim));
321 FN(PW,free)(pw);
322 isl_point_free(pnt);
323 return qp;
324 error:
325 FN(PW,free)(pw);
326 isl_point_free(pnt);
327 return NULL;
330 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
332 int i;
333 isl_set *dom;
335 if (!pw)
336 return NULL;
338 dom = isl_set_empty(isl_dim_copy(pw->dim));
339 for (i = 0; i < pw->n; ++i)
340 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
342 FN(PW,free)(pw);
344 return dom;
347 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw, __isl_take isl_set *set)
349 int i;
351 if (!pw || !set)
352 goto error;
354 if (pw->n == 0) {
355 isl_set_free(set);
356 return pw;
359 pw = FN(PW,cow)(pw);
360 if (!pw)
361 goto error;
363 for (i = pw->n - 1; i >= 0; --i) {
364 isl_basic_set *aff;
365 pw->p[i].set = isl_set_intersect(pw->p[i].set, isl_set_copy(set));
366 if (!pw->p[i].set)
367 goto error;
368 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
369 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD,
370 aff);
371 if (isl_set_plain_is_empty(pw->p[i].set)) {
372 isl_set_free(pw->p[i].set);
373 FN(EL,free)(pw->p[i].FIELD);
374 if (i != pw->n - 1)
375 pw->p[i] = pw->p[pw->n - 1];
376 pw->n--;
380 isl_set_free(set);
381 return pw;
382 error:
383 isl_set_free(set);
384 FN(PW,free)(pw);
385 return NULL;
388 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
390 int i;
391 isl_basic_set *hull = NULL;
393 if (!pw || !context)
394 goto error;
396 if (pw->n == 0) {
397 isl_set_free(context);
398 return pw;
401 context = isl_set_compute_divs(context);
402 hull = isl_set_simple_hull(isl_set_copy(context));
404 pw = FN(PW,cow)(pw);
405 if (!pw)
406 goto error;
408 for (i = pw->n - 1; i >= 0; --i) {
409 pw->p[i].set = isl_set_intersect(pw->p[i].set,
410 isl_set_copy(context));
411 if (!pw->p[i].set)
412 goto error;
413 pw->p[i].FIELD = FN(EL,gist)(pw->p[i].FIELD,
414 isl_set_copy(pw->p[i].set));
415 pw->p[i].set = isl_set_gist_basic_set(pw->p[i].set,
416 isl_basic_set_copy(hull));
417 if (!pw->p[i].set)
418 goto error;
419 if (isl_set_plain_is_empty(pw->p[i].set)) {
420 isl_set_free(pw->p[i].set);
421 FN(EL,free)(pw->p[i].FIELD);
422 if (i != pw->n - 1)
423 pw->p[i] = pw->p[pw->n - 1];
424 pw->n--;
428 isl_basic_set_free(hull);
429 isl_set_free(context);
431 return pw;
432 error:
433 FN(PW,free)(pw);
434 isl_basic_set_free(hull);
435 isl_set_free(context);
436 return NULL;
439 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
441 int i, j;
443 if (!pw)
444 return NULL;
445 if (pw->n == 0)
446 return pw;
448 for (i = pw->n - 1; i >= 0; --i) {
449 for (j = i - 1; j >= 0; --j) {
450 if (!FN(EL,is_equal)(pw->p[i].FIELD, pw->p[j].FIELD))
451 continue;
452 pw->p[j].set = isl_set_union(pw->p[j].set,
453 pw->p[i].set);
454 FN(EL,free)(pw->p[i].FIELD);
455 if (i != pw->n - 1)
456 pw->p[i] = pw->p[pw->n - 1];
457 pw->n--;
458 break;
460 if (j >= 0)
461 continue;
462 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
463 if (!pw->p[i].set)
464 goto error;
467 return pw;
468 error:
469 FN(PW,free)(pw);
470 return NULL;
473 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
475 return pw ? pw->dim->ctx : NULL;
478 int FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
479 unsigned first, unsigned n)
481 int i;
483 if (!pw)
484 return -1;
485 if (pw->n == 0 || n == 0)
486 return 0;
487 for (i = 0; i < pw->n; ++i) {
488 int involves = FN(EL,involves_dims)(pw->p[i].FIELD,
489 type, first, n);
490 if (involves < 0 || involves)
491 return involves;
492 involves = isl_set_involves_dims(pw->p[i].set, type, first, n);
493 if (involves < 0 || involves)
494 return involves;
496 return 0;
499 __isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
500 enum isl_dim_type type, unsigned pos, const char *s)
502 int i;
504 pw = FN(PW,cow)(pw);
505 if (!pw)
506 return NULL;
508 pw->dim = isl_dim_set_name(pw->dim, type, pos, s);
509 if (!pw->dim)
510 goto error;
512 for (i = 0; i < pw->n; ++i) {
513 pw->p[i].set = isl_set_set_dim_name(pw->p[i].set, type, pos, s);
514 if (!pw->p[i].set)
515 goto error;
516 pw->p[i].FIELD = FN(EL,set_dim_name)(pw->p[i].FIELD, type, pos, s);
517 if (!pw->p[i].FIELD)
518 goto error;
521 return pw;
522 error:
523 FN(PW,free)(pw);
524 return NULL;
527 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
528 enum isl_dim_type type, unsigned first, unsigned n)
530 int i;
532 if (!pw)
533 return NULL;
534 if (n == 0 && !isl_dim_get_tuple_name(pw->dim, type))
535 return pw;
537 pw = FN(PW,cow)(pw);
538 if (!pw)
539 return NULL;
540 pw->dim = isl_dim_drop(pw->dim, type, first, n);
541 if (!pw->dim)
542 goto error;
543 for (i = 0; i < pw->n; ++i) {
544 pw->p[i].set = isl_set_drop(pw->p[i].set, type, first, n);
545 if (!pw->p[i].set)
546 goto error;
547 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
548 if (!pw->p[i].FIELD)
549 goto error;
552 return pw;
553 error:
554 FN(PW,free)(pw);
555 return NULL;
558 __isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
559 unsigned first, unsigned n)
561 int i;
563 if (!pw)
564 return NULL;
565 if (n == 0 && !isl_dim_is_named_or_nested(pw->dim, type))
566 return pw;
568 pw = FN(PW,cow)(pw);
569 if (!pw)
570 return NULL;
572 pw->dim = isl_dim_insert(pw->dim, type, first, n);
573 if (!pw->dim)
574 goto error;
576 for (i = 0; i < pw->n; ++i) {
577 pw->p[i].set = isl_set_insert(pw->p[i].set, type, first, n);
578 if (!pw->p[i].set)
579 goto error;
580 pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
581 type, first, n);
582 if (!pw->p[i].FIELD)
583 goto error;
586 return pw;
587 error:
588 FN(PW,free)(pw);
589 return NULL;
592 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
593 enum isl_dim_type type, unsigned pos, isl_int v)
595 int i;
597 if (!pw)
598 return NULL;
600 pw = FN(PW,cow)(pw);
601 if (!pw)
602 return NULL;
603 for (i = 0; i < pw->n; ++i) {
604 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
605 if (!pw->p[i].set)
606 goto error;
609 return pw;
610 error:
611 FN(PW,free)(pw);
612 return NULL;
615 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
617 return pw ? isl_dim_size(pw->dim, type) : 0;
620 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
621 enum isl_dim_type type, unsigned first, unsigned n)
623 int i;
625 if (!pw)
626 return NULL;
627 if (n == 0)
628 return pw;
630 pw = FN(PW,cow)(pw);
631 if (!pw)
632 return NULL;
633 if (!pw->dim)
634 goto error;
635 for (i = 0; i < pw->n; ++i) {
636 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
637 if (!pw->p[i].set)
638 goto error;
641 return pw;
642 error:
643 FN(PW,free)(pw);
644 return NULL;
647 /* Compute the maximal value attained by the piecewise quasipolynomial
648 * on its domain or zero if the domain is empty.
649 * In the worst case, the domain is scanned completely,
650 * so the domain is assumed to be bounded.
652 __isl_give isl_qpolynomial *FN(PW,opt)(__isl_take PW *pw, int max)
654 int i;
655 isl_qpolynomial *opt;
657 if (!pw)
658 return NULL;
660 if (pw->n == 0) {
661 isl_dim *dim = isl_dim_copy(pw->dim);
662 FN(PW,free)(pw);
663 return isl_qpolynomial_zero(dim);
666 opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
667 isl_set_copy(pw->p[0].set), max);
668 for (i = 1; i < pw->n; ++i) {
669 isl_qpolynomial *opt_i;
670 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
671 isl_set_copy(pw->p[i].set), max);
672 if (max)
673 opt = isl_qpolynomial_max_cst(opt, opt_i);
674 else
675 opt = isl_qpolynomial_min_cst(opt, opt_i);
678 FN(PW,free)(pw);
679 return opt;
682 __isl_give isl_qpolynomial *FN(PW,max)(__isl_take PW *pw)
684 return FN(PW,opt)(pw, 1);
687 __isl_give isl_qpolynomial *FN(PW,min)(__isl_take PW *pw)
689 return FN(PW,opt)(pw, 0);
692 __isl_give isl_dim *FN(PW,get_dim)(__isl_keep PW *pw)
694 return pw ? isl_dim_copy(pw->dim) : NULL;
697 __isl_give PW *FN(PW,reset_dim)(__isl_take PW *pw, __isl_take isl_dim *dim)
699 int i;
701 pw = FN(PW,cow)(pw);
702 if (!pw || !dim)
703 goto error;
705 for (i = 0; i < pw->n; ++i) {
706 pw->p[i].set = isl_set_reset_dim(pw->p[i].set,
707 isl_dim_copy(dim));
708 if (!pw->p[i].set)
709 goto error;
710 pw->p[i].FIELD = FN(EL,reset_dim)(pw->p[i].FIELD,
711 isl_dim_copy(dim));
712 if (!pw->p[i].FIELD)
713 goto error;
715 isl_dim_free(pw->dim);
716 pw->dim = dim;
718 return pw;
719 error:
720 isl_dim_free(dim);
721 FN(PW,free)(pw);
722 return NULL;
725 int FN(PW,has_equal_dim)(__isl_keep PW *pw1, __isl_keep PW *pw2)
727 if (!pw1 || !pw2)
728 return -1;
730 return isl_dim_equal(pw1->dim, pw2->dim);
733 __isl_give PW *FN(PW,morph)(__isl_take PW *pw, __isl_take isl_morph *morph)
735 int i;
737 if (!pw || !morph)
738 goto error;
740 isl_assert(pw->dim->ctx, isl_dim_equal(pw->dim, morph->dom->dim),
741 goto error);
743 pw = FN(PW,cow)(pw);
744 if (!pw)
745 goto error;
746 isl_dim_free(pw->dim);
747 pw->dim = isl_dim_copy(morph->ran->dim);
748 if (!pw->dim)
749 goto error;
751 for (i = 0; i < pw->n; ++i) {
752 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
753 if (!pw->p[i].set)
754 goto error;
755 pw->p[i].FIELD = FN(EL,morph)(pw->p[i].FIELD,
756 isl_morph_copy(morph));
757 if (!pw->p[i].FIELD)
758 goto error;
761 isl_morph_free(morph);
763 return pw;
764 error:
765 FN(PW,free)(pw);
766 isl_morph_free(morph);
767 return NULL;
770 int FN(PW,foreach_piece)(__isl_keep PW *pw,
771 int (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
772 void *user)
774 int i;
776 if (!pw)
777 return -1;
779 for (i = 0; i < pw->n; ++i)
780 if (fn(isl_set_copy(pw->p[i].set),
781 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
782 return -1;
784 return 0;
787 static int any_divs(__isl_keep isl_set *set)
789 int i;
791 if (!set)
792 return -1;
794 for (i = 0; i < set->n; ++i)
795 if (set->p[i]->n_div > 0)
796 return 1;
798 return 0;
801 static int foreach_lifted_subset(__isl_take isl_set *set, __isl_take EL *el,
802 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
803 void *user), void *user)
805 int i;
807 if (!set || !el)
808 goto error;
810 for (i = 0; i < set->n; ++i) {
811 isl_set *lift;
812 EL *copy;
814 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
815 lift = isl_set_lift(lift);
817 copy = FN(EL,copy)(el);
818 copy = FN(EL,lift)(copy, isl_set_get_dim(lift));
820 if (fn(lift, copy, user) < 0)
821 goto error;
824 isl_set_free(set);
825 FN(EL,free)(el);
827 return 0;
828 error:
829 isl_set_free(set);
830 FN(EL,free)(el);
831 return -1;
834 int FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
835 int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
836 void *user), void *user)
838 int i;
840 if (!pw)
841 return -1;
843 for (i = 0; i < pw->n; ++i) {
844 isl_set *set;
845 EL *el;
847 set = isl_set_copy(pw->p[i].set);
848 el = FN(EL,copy)(pw->p[i].FIELD);
849 if (!any_divs(set)) {
850 if (fn(set, el, user) < 0)
851 return -1;
852 continue;
854 if (foreach_lifted_subset(set, el, fn, user) < 0)
855 return -1;
858 return 0;
861 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
862 enum isl_dim_type dst_type, unsigned dst_pos,
863 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
865 int i;
867 pw = FN(PW,cow)(pw);
868 if (!pw)
869 return NULL;
871 pw->dim = isl_dim_move(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
872 if (!pw->dim)
873 goto error;
875 for (i = 0; i < pw->n; ++i) {
876 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
877 dst_type, dst_pos,
878 src_type, src_pos, n);
879 if (!pw->p[i].set)
880 goto error;
881 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
882 dst_type, dst_pos, src_type, src_pos, n);
883 if (!pw->p[i].FIELD)
884 goto error;
887 return pw;
888 error:
889 FN(PW,free)(pw);
890 return NULL;
893 __isl_give PW *FN(PW,realign)(__isl_take PW *pw, __isl_take isl_reordering *exp)
895 int i;
897 pw = FN(PW,cow)(pw);
898 if (!pw || !exp)
899 return NULL;
901 for (i = 0; i < pw->n; ++i) {
902 pw->p[i].set = isl_set_realign(pw->p[i].set,
903 isl_reordering_copy(exp));
904 if (!pw->p[i].set)
905 goto error;
906 pw->p[i].FIELD = FN(EL,realign)(pw->p[i].FIELD,
907 isl_reordering_copy(exp));
908 if (!pw->p[i].FIELD)
909 goto error;
912 pw = FN(PW,reset_dim)(pw, isl_dim_copy(exp->dim));
914 isl_reordering_free(exp);
915 return pw;
916 error:
917 isl_reordering_free(exp);
918 FN(PW,free)(pw);
919 return NULL;
922 __isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
924 int i;
926 if (isl_int_is_one(v))
927 return pw;
928 if (pw && isl_int_is_zero(v)) {
929 PW *zero;
930 isl_dim *dim = FN(PW,get_dim)(pw);
931 #ifdef HAS_TYPE
932 zero = FN(PW,zero)(dim, pw->type);
933 #else
934 zero = FN(PW,zero)(dim);
935 #endif
936 FN(PW,free)(pw);
937 return zero;
939 pw = FN(PW,cow)(pw);
940 if (!pw)
941 return NULL;
942 if (pw->n == 0)
943 return pw;
945 #ifdef HAS_TYPE
946 if (isl_int_is_neg(v))
947 pw->type = isl_fold_type_negate(pw->type);
948 #endif
949 for (i = 0; i < pw->n; ++i) {
950 pw->p[i].FIELD = FN(EL,mul_isl_int)(pw->p[i].FIELD, v);
951 if (!pw->p[i].FIELD)
952 goto error;
955 return pw;
956 error:
957 FN(PW,free)(pw);
958 return NULL;