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)
7 static __isl_give PW
*FN(PW
,alloc_
)(__isl_take isl_dim
*dim
,
8 enum isl_fold type
, int n
)
10 static __isl_give PW
*FN(PW
,alloc_
)(__isl_take isl_dim
*dim
, int n
)
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
)));
37 __isl_give PW
*FN(PW
,zero
)(__isl_take isl_dim
*dim
, enum isl_fold type
)
39 return FN(PW
,alloc_
)(dim
, type
, 0);
42 __isl_give PW
*FN(PW
,zero
)(__isl_take isl_dim
*dim
)
44 return FN(PW
,alloc_
)(dim
, 0);
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
)
54 if (isl_set_fast_is_empty(set
) || FN(EL
,IS_ZERO
)(el
)) {
61 if (pw
->type
!= el
->type
)
62 isl_die(set
->ctx
, isl_error_invalid
, "fold types don't match",
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
;
81 __isl_give PW
*FN(PW
,alloc
)(enum isl_fold type
,
82 __isl_take isl_set
*set
, __isl_take EL
*el
)
84 __isl_give PW
*FN(PW
,alloc
)(__isl_take isl_set
*set
, __isl_take EL
*el
)
93 pw
= FN(PW
,alloc_
)(isl_set_get_dim(set
), type
, 1);
95 pw
= FN(PW
,alloc_
)(isl_set_get_dim(set
), 1);
98 return FN(PW
,add_piece
)(pw
, set
, el
);
105 __isl_give PW
*FN(PW
,dup
)(__isl_keep PW
*pw
)
114 dup
= FN(PW
,alloc_
)(isl_dim_copy(pw
->dim
), pw
->type
, pw
->n
);
116 dup
= FN(PW
,alloc_
)(isl_dim_copy(pw
->dim
), pw
->n
);
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
));
131 __isl_give PW
*FN(PW
,cow
)(__isl_take PW
*pw
)
139 return FN(PW
,dup
)(pw
);
142 __isl_give PW
*FN(PW
,copy
)(__isl_keep PW
*pw
)
151 void FN(PW
,free
)(__isl_take PW
*pw
)
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
);
168 int FN(PW
,is_zero
)(__isl_keep PW
*pw
)
176 __isl_give PW
*FN(PW
,add
)(__isl_take PW
*pw1
, __isl_take PW
*pw2
)
186 if (pw1
->type
!= pw2
->type
)
187 isl_die(pw1
->dim
->ctx
, isl_error_invalid
,
188 "fold types don't match", goto error
);
190 isl_assert(pw1
->dim
->ctx
, isl_dim_equal(pw1
->dim
, pw2
->dim
), goto error
);
192 if (FN(PW
,is_zero
)(pw1
)) {
197 if (FN(PW
,is_zero
)(pw2
)) {
202 n
= (pw1
->n
+ 1) * (pw2
->n
+ 1);
204 res
= FN(PW
,alloc_
)(isl_dim_copy(pw1
->dim
), pw1
->type
, n
);
206 res
= FN(PW
,alloc_
)(isl_dim_copy(pw1
->dim
), n
);
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
;
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
);
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
));
250 __isl_give PW
*FN(PW
,add_disjoint
)(__isl_take PW
*pw1
, __isl_take PW
*pw2
)
259 if (pw1
->type
!= pw2
->type
)
260 isl_die(pw1
->dim
->ctx
, isl_error_invalid
,
261 "fold types don't match", goto error
);
263 isl_assert(pw1
->dim
->ctx
, isl_dim_equal(pw1
->dim
, pw2
->dim
), goto error
);
265 if (FN(PW
,is_zero
)(pw1
)) {
270 if (FN(PW
,is_zero
)(pw2
)) {
276 res
= FN(PW
,alloc_
)(isl_dim_copy(pw1
->dim
), pw1
->type
, pw1
->n
+ pw2
->n
);
278 res
= FN(PW
,alloc_
)(isl_dim_copy(pw1
->dim
), pw1
->n
+ pw2
->n
);
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
));
301 __isl_give isl_qpolynomial
*FN(PW
,eval
)(__isl_take PW
*pw
,
302 __isl_take isl_point
*pnt
)
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
);
320 qp
= FN(EL
,eval
)(FN(EL
,copy
)(pw
->p
[i
].FIELD
),
321 isl_point_copy(pnt
));
323 qp
= isl_qpolynomial_zero(isl_dim_copy(pw
->dim
));
333 __isl_give isl_set
*FN(PW
,domain
)(__isl_take PW
*pw
)
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
));
350 __isl_give PW
*FN(PW
,intersect_domain
)(__isl_take PW
*pw
, __isl_take isl_set
*set
)
366 for (i
= pw
->n
- 1; i
>= 0; --i
) {
368 pw
->p
[i
].set
= isl_set_intersect(pw
->p
[i
].set
, isl_set_copy(set
));
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
,
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
);
378 pw
->p
[i
] = pw
->p
[pw
->n
- 1];
391 __isl_give PW
*FN(PW
,gist
)(__isl_take PW
*pw
, __isl_take isl_set
*context
)
394 isl_basic_set
*hull
= NULL
;
400 isl_set_free(context
);
404 context
= isl_set_compute_divs(context
);
405 hull
= isl_set_simple_hull(isl_set_copy(context
));
411 for (i
= pw
->n
- 1; i
>= 0; --i
) {
412 pw
->p
[i
].set
= isl_set_intersect(pw
->p
[i
].set
,
413 isl_set_copy(context
));
416 pw
->p
[i
].FIELD
= FN(EL
,gist
)(pw
->p
[i
].FIELD
,
417 isl_set_copy(pw
->p
[i
].set
));
418 pw
->p
[i
].set
= isl_set_gist_basic_set(pw
->p
[i
].set
,
419 isl_basic_set_copy(hull
));
422 if (isl_set_fast_is_empty(pw
->p
[i
].set
)) {
423 isl_set_free(pw
->p
[i
].set
);
424 FN(EL
,free
)(pw
->p
[i
].FIELD
);
426 pw
->p
[i
] = pw
->p
[pw
->n
- 1];
431 isl_basic_set_free(hull
);
432 isl_set_free(context
);
437 isl_basic_set_free(hull
);
438 isl_set_free(context
);
442 __isl_give PW
*FN(PW
,coalesce
)(__isl_take PW
*pw
)
451 for (i
= pw
->n
- 1; i
>= 0; --i
) {
452 for (j
= i
- 1; j
>= 0; --j
) {
453 if (!FN(EL
,is_equal
)(pw
->p
[i
].FIELD
, pw
->p
[j
].FIELD
))
455 pw
->p
[j
].set
= isl_set_union(pw
->p
[j
].set
,
457 FN(EL
,free
)(pw
->p
[i
].FIELD
);
459 pw
->p
[i
] = pw
->p
[pw
->n
- 1];
465 pw
->p
[i
].set
= isl_set_coalesce(pw
->p
[i
].set
);
476 isl_ctx
*FN(PW
,get_ctx
)(__isl_keep PW
*pw
)
478 return pw
? pw
->dim
->ctx
: NULL
;
481 int FN(PW
,involves_dims
)(__isl_keep PW
*pw
, enum isl_dim_type type
,
482 unsigned first
, unsigned n
)
488 if (pw
->n
== 0 || n
== 0)
490 for (i
= 0; i
< pw
->n
; ++i
) {
491 int involves
= FN(EL
,involves_dims
)(pw
->p
[i
].FIELD
,
493 if (involves
< 0 || involves
)
499 __isl_give PW
*FN(PW
,set_dim_name
)(__isl_take PW
*pw
,
500 enum isl_dim_type type
, unsigned pos
, const char *s
)
508 pw
->dim
= isl_dim_set_name(pw
->dim
, type
, pos
, s
);
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
);
516 pw
->p
[i
].FIELD
= FN(EL
,set_dim_name
)(pw
->p
[i
].FIELD
, type
, pos
, s
);
527 __isl_give PW
*FN(PW
,drop_dims
)(__isl_take PW
*pw
,
528 enum isl_dim_type type
, unsigned first
, unsigned n
)
534 if (n
== 0 && !isl_dim_get_tuple_name(pw
->dim
, type
))
540 pw
->dim
= isl_dim_drop(pw
->dim
, type
, first
, n
);
543 for (i
= 0; i
< pw
->n
; ++i
) {
544 pw
->p
[i
].set
= isl_set_drop(pw
->p
[i
].set
, type
, first
, n
);
547 pw
->p
[i
].FIELD
= FN(EL
,drop_dims
)(pw
->p
[i
].FIELD
, type
, first
, n
);
558 __isl_give PW
*FN(PW
,insert_dims
)(__isl_take PW
*pw
, enum isl_dim_type type
,
559 unsigned first
, unsigned n
)
565 if (n
== 0 && !isl_dim_is_named_or_nested(pw
->dim
, type
))
572 pw
->dim
= isl_dim_insert(pw
->dim
, type
, first
, n
);
576 for (i
= 0; i
< pw
->n
; ++i
) {
577 pw
->p
[i
].set
= isl_set_insert(pw
->p
[i
].set
, type
, first
, n
);
580 pw
->p
[i
].FIELD
= FN(EL
,insert_dims
)(pw
->p
[i
].FIELD
,
592 __isl_give PW
*FN(PW
,fix_dim
)(__isl_take PW
*pw
,
593 enum isl_dim_type type
, unsigned pos
, isl_int v
)
603 for (i
= 0; i
< pw
->n
; ++i
) {
604 pw
->p
[i
].set
= isl_set_fix(pw
->p
[i
].set
, type
, pos
, v
);
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
)
635 for (i
= 0; i
< pw
->n
; ++i
) {
636 pw
->p
[i
].set
= isl_set_split_dims(pw
->p
[i
].set
, type
, first
, n
);
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
)
655 isl_qpolynomial
*opt
;
661 isl_dim
*dim
= isl_dim_copy(pw
->dim
);
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
);
673 opt
= isl_qpolynomial_max_cst(opt
, opt_i
);
675 opt
= isl_qpolynomial_min_cst(opt
, opt_i
);
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
)
705 for (i
= 0; i
< pw
->n
; ++i
) {
706 pw
->p
[i
].set
= isl_set_reset_dim(pw
->p
[i
].set
,
710 pw
->p
[i
].FIELD
= FN(EL
,reset_dim
)(pw
->p
[i
].FIELD
,
715 isl_dim_free(pw
->dim
);
725 int FN(PW
,has_equal_dim
)(__isl_keep PW
*pw1
, __isl_keep PW
*pw2
)
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
)
740 isl_assert(pw
->dim
->ctx
, isl_dim_equal(pw
->dim
, morph
->dom
->dim
),
746 isl_dim_free(pw
->dim
);
747 pw
->dim
= isl_dim_copy(morph
->ran
->dim
);
751 for (i
= 0; i
< pw
->n
; ++i
) {
752 pw
->p
[i
].set
= isl_morph_set(isl_morph_copy(morph
), pw
->p
[i
].set
);
755 pw
->p
[i
].FIELD
= FN(EL
,morph
)(pw
->p
[i
].FIELD
,
756 isl_morph_copy(morph
));
761 isl_morph_free(morph
);
766 isl_morph_free(morph
);
770 int FN(PW
,foreach_piece
)(__isl_keep PW
*pw
,
771 int (*fn
)(__isl_take isl_set
*set
, __isl_take EL
*el
, void *user
),
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)
787 static int any_divs(__isl_keep isl_set
*set
)
794 for (i
= 0; i
< set
->n
; ++i
)
795 if (set
->p
[i
]->n_div
> 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
)
810 for (i
= 0; i
< set
->n
; ++i
) {
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)
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
)
843 for (i
= 0; i
< pw
->n
; ++i
) {
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)
854 if (foreach_lifted_subset(set
, el
, fn
, user
) < 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
)
871 pw
->dim
= isl_dim_move(pw
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
875 for (i
= 0; i
< pw
->n
; ++i
) {
876 pw
->p
[i
].set
= isl_set_move_dims(pw
->p
[i
].set
,
878 src_type
, src_pos
, n
);
881 pw
->p
[i
].FIELD
= FN(EL
,move_dims
)(pw
->p
[i
].FIELD
,
882 dst_type
, dst_pos
, src_type
, src_pos
, n
);
893 __isl_give PW
*FN(PW
,realign
)(__isl_take PW
*pw
, __isl_take isl_reordering
*exp
)
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
));
906 pw
->p
[i
].FIELD
= FN(EL
,realign
)(pw
->p
[i
].FIELD
,
907 isl_reordering_copy(exp
));
912 pw
= FN(PW
,reset_dim
)(pw
, isl_dim_copy(exp
->dim
));
914 isl_reordering_free(exp
);
917 isl_reordering_free(exp
);
922 __isl_give PW
*FN(PW
,mul_isl_int
)(__isl_take PW
*pw
, isl_int v
)
926 if (isl_int_is_one(v
))
928 if (pw
&& isl_int_is_zero(v
)) {
930 isl_dim
*dim
= FN(PW
,get_dim
)(pw
);
932 zero
= FN(PW
,zero
)(dim
, pw
->type
);
934 zero
= FN(PW
,zero
)(dim
);
946 if (isl_int_is_neg(v
))
947 pw
->type
= isl_fold_type_negate(pw
->type
);
949 for (i
= 0; i
< pw
->n
; ++i
) {
950 pw
->p
[i
].FIELD
= FN(EL
,mul_isl_int
)(pw
->p
[i
].FIELD
, v
);