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
)
18 ctx
= isl_dim_get_ctx(dim
);
19 isl_assert(ctx
, n
>= 0, goto error
);
20 pw
= isl_alloc(ctx
, struct PW
,
21 sizeof(struct PW
) + (n
- 1) * sizeof(S(PW
,piece
)));
39 __isl_give PW
*FN(PW
,zero
)(__isl_take isl_dim
*dim
, enum isl_fold type
)
41 return FN(PW
,alloc_
)(dim
, type
, 0);
44 __isl_give PW
*FN(PW
,zero
)(__isl_take isl_dim
*dim
)
46 return FN(PW
,alloc_
)(dim
, 0);
50 __isl_give PW
*FN(PW
,add_piece
)(__isl_take PW
*pw
,
51 __isl_take isl_set
*set
, __isl_take EL
*el
)
54 isl_dim
*el_dim
= NULL
;
56 if (!pw
|| !set
|| !el
)
59 if (isl_set_plain_is_empty(set
) || FN(EL
,IS_ZERO
)(el
)) {
65 ctx
= isl_set_get_ctx(set
);
67 if (pw
->type
!= el
->type
)
68 isl_die(ctx
, isl_error_invalid
, "fold types don't match",
71 el_dim
= FN(EL
,get_dim(el
));
72 isl_assert(ctx
, isl_dim_equal(pw
->dim
, el_dim
), goto error
);
73 isl_assert(ctx
, pw
->n
< pw
->size
, goto error
);
75 pw
->p
[pw
->n
].set
= set
;
76 pw
->p
[pw
->n
].FIELD
= el
;
90 __isl_give PW
*FN(PW
,alloc
)(enum isl_fold type
,
91 __isl_take isl_set
*set
, __isl_take EL
*el
)
93 __isl_give PW
*FN(PW
,alloc
)(__isl_take isl_set
*set
, __isl_take EL
*el
)
102 pw
= FN(PW
,alloc_
)(isl_set_get_dim(set
), type
, 1);
104 pw
= FN(PW
,alloc_
)(isl_set_get_dim(set
), 1);
107 return FN(PW
,add_piece
)(pw
, set
, el
);
114 __isl_give PW
*FN(PW
,dup
)(__isl_keep PW
*pw
)
123 dup
= FN(PW
,alloc_
)(isl_dim_copy(pw
->dim
), pw
->type
, pw
->n
);
125 dup
= FN(PW
,alloc_
)(isl_dim_copy(pw
->dim
), pw
->n
);
130 for (i
= 0; i
< pw
->n
; ++i
)
131 dup
= FN(PW
,add_piece
)(dup
, isl_set_copy(pw
->p
[i
].set
),
132 FN(EL
,copy
)(pw
->p
[i
].FIELD
));
137 __isl_give PW
*FN(PW
,cow
)(__isl_take PW
*pw
)
145 return FN(PW
,dup
)(pw
);
148 __isl_give PW
*FN(PW
,copy
)(__isl_keep PW
*pw
)
157 void *FN(PW
,free
)(__isl_take PW
*pw
)
166 for (i
= 0; i
< pw
->n
; ++i
) {
167 isl_set_free(pw
->p
[i
].set
);
168 FN(EL
,free
)(pw
->p
[i
].FIELD
);
170 isl_dim_free(pw
->dim
);
176 int FN(PW
,is_zero
)(__isl_keep PW
*pw
)
184 __isl_give PW
*FN(PW
,add
)(__isl_take PW
*pw1
, __isl_take PW
*pw2
)
194 ctx
= isl_dim_get_ctx(pw1
->dim
);
196 if (pw1
->type
!= pw2
->type
)
197 isl_die(ctx
, isl_error_invalid
,
198 "fold types don't match", goto error
);
200 isl_assert(ctx
, isl_dim_equal(pw1
->dim
, pw2
->dim
), goto error
);
202 if (FN(PW
,is_zero
)(pw1
)) {
207 if (FN(PW
,is_zero
)(pw2
)) {
212 n
= (pw1
->n
+ 1) * (pw2
->n
+ 1);
214 res
= FN(PW
,alloc_
)(isl_dim_copy(pw1
->dim
), pw1
->type
, n
);
216 res
= FN(PW
,alloc_
)(isl_dim_copy(pw1
->dim
), n
);
219 for (i
= 0; i
< pw1
->n
; ++i
) {
220 set
= isl_set_copy(pw1
->p
[i
].set
);
221 for (j
= 0; j
< pw2
->n
; ++j
) {
222 struct isl_set
*common
;
224 set
= isl_set_subtract(set
,
225 isl_set_copy(pw2
->p
[j
].set
));
226 common
= isl_set_intersect(isl_set_copy(pw1
->p
[i
].set
),
227 isl_set_copy(pw2
->p
[j
].set
));
228 if (isl_set_plain_is_empty(common
)) {
229 isl_set_free(common
);
233 sum
= FN(EL
,add_on_domain
)(common
,
234 FN(EL
,copy
)(pw1
->p
[i
].FIELD
),
235 FN(EL
,copy
)(pw2
->p
[j
].FIELD
));
237 res
= FN(PW
,add_piece
)(res
, common
, sum
);
239 res
= FN(PW
,add_piece
)(res
, set
, FN(EL
,copy
)(pw1
->p
[i
].FIELD
));
242 for (j
= 0; j
< pw2
->n
; ++j
) {
243 set
= isl_set_copy(pw2
->p
[j
].set
);
244 for (i
= 0; i
< pw1
->n
; ++i
)
245 set
= isl_set_subtract(set
,
246 isl_set_copy(pw1
->p
[i
].set
));
247 res
= FN(PW
,add_piece
)(res
, set
, FN(EL
,copy
)(pw2
->p
[j
].FIELD
));
260 __isl_give PW
*FN(PW
,add_disjoint
)(__isl_take PW
*pw1
, __isl_take PW
*pw2
)
269 ctx
= isl_dim_get_ctx(pw1
->dim
);
271 if (pw1
->type
!= pw2
->type
)
272 isl_die(ctx
, isl_error_invalid
,
273 "fold types don't match", goto error
);
275 isl_assert(ctx
, isl_dim_equal(pw1
->dim
, pw2
->dim
), goto error
);
277 if (FN(PW
,is_zero
)(pw1
)) {
282 if (FN(PW
,is_zero
)(pw2
)) {
288 res
= FN(PW
,alloc_
)(isl_dim_copy(pw1
->dim
), pw1
->type
, pw1
->n
+ pw2
->n
);
290 res
= FN(PW
,alloc_
)(isl_dim_copy(pw1
->dim
), pw1
->n
+ pw2
->n
);
293 for (i
= 0; i
< pw1
->n
; ++i
)
294 res
= FN(PW
,add_piece
)(res
,
295 isl_set_copy(pw1
->p
[i
].set
),
296 FN(EL
,copy
)(pw1
->p
[i
].FIELD
));
298 for (i
= 0; i
< pw2
->n
; ++i
)
299 res
= FN(PW
,add_piece
)(res
,
300 isl_set_copy(pw2
->p
[i
].set
),
301 FN(EL
,copy
)(pw2
->p
[i
].FIELD
));
313 __isl_give isl_qpolynomial
*FN(PW
,eval
)(__isl_take PW
*pw
,
314 __isl_take isl_point
*pnt
)
319 isl_dim
*pnt_dim
= NULL
;
324 ctx
= isl_point_get_ctx(pnt
);
325 pnt_dim
= isl_point_get_dim(pnt
);
326 isl_assert(ctx
, isl_dim_equal(pnt_dim
, pw
->dim
), goto error
);
328 for (i
= 0; i
< pw
->n
; ++i
) {
329 found
= isl_set_contains_point(pw
->p
[i
].set
, pnt
);
336 qp
= FN(EL
,eval
)(FN(EL
,copy
)(pw
->p
[i
].FIELD
),
337 isl_point_copy(pnt
));
339 qp
= isl_qpolynomial_zero(isl_dim_copy(pw
->dim
));
341 isl_dim_free(pnt_dim
);
346 isl_dim_free(pnt_dim
);
351 __isl_give isl_set
*FN(PW
,domain
)(__isl_take PW
*pw
)
359 dom
= isl_set_empty(isl_dim_copy(pw
->dim
));
360 for (i
= 0; i
< pw
->n
; ++i
)
361 dom
= isl_set_union_disjoint(dom
, isl_set_copy(pw
->p
[i
].set
));
368 __isl_give PW
*FN(PW
,intersect_domain
)(__isl_take PW
*pw
, __isl_take isl_set
*set
)
384 for (i
= pw
->n
- 1; i
>= 0; --i
) {
386 pw
->p
[i
].set
= isl_set_intersect(pw
->p
[i
].set
, isl_set_copy(set
));
389 aff
= isl_set_affine_hull(isl_set_copy(pw
->p
[i
].set
));
390 pw
->p
[i
].FIELD
= FN(EL
,substitute_equalities
)(pw
->p
[i
].FIELD
,
392 if (isl_set_plain_is_empty(pw
->p
[i
].set
)) {
393 isl_set_free(pw
->p
[i
].set
);
394 FN(EL
,free
)(pw
->p
[i
].FIELD
);
396 pw
->p
[i
] = pw
->p
[pw
->n
- 1];
409 __isl_give PW
*FN(PW
,gist
)(__isl_take PW
*pw
, __isl_take isl_set
*context
)
412 isl_basic_set
*hull
= NULL
;
418 isl_set_free(context
);
422 context
= isl_set_compute_divs(context
);
423 hull
= isl_set_simple_hull(isl_set_copy(context
));
429 for (i
= pw
->n
- 1; i
>= 0; --i
) {
430 pw
->p
[i
].set
= isl_set_intersect(pw
->p
[i
].set
,
431 isl_set_copy(context
));
434 pw
->p
[i
].FIELD
= FN(EL
,gist
)(pw
->p
[i
].FIELD
,
435 isl_set_copy(pw
->p
[i
].set
));
436 pw
->p
[i
].set
= isl_set_gist_basic_set(pw
->p
[i
].set
,
437 isl_basic_set_copy(hull
));
440 if (isl_set_plain_is_empty(pw
->p
[i
].set
)) {
441 isl_set_free(pw
->p
[i
].set
);
442 FN(EL
,free
)(pw
->p
[i
].FIELD
);
444 pw
->p
[i
] = pw
->p
[pw
->n
- 1];
449 isl_basic_set_free(hull
);
450 isl_set_free(context
);
455 isl_basic_set_free(hull
);
456 isl_set_free(context
);
460 __isl_give PW
*FN(PW
,coalesce
)(__isl_take PW
*pw
)
469 for (i
= pw
->n
- 1; i
>= 0; --i
) {
470 for (j
= i
- 1; j
>= 0; --j
) {
471 if (!FN(EL
,plain_is_equal
)(pw
->p
[i
].FIELD
,
474 pw
->p
[j
].set
= isl_set_union(pw
->p
[j
].set
,
476 FN(EL
,free
)(pw
->p
[i
].FIELD
);
478 pw
->p
[i
] = pw
->p
[pw
->n
- 1];
484 pw
->p
[i
].set
= isl_set_coalesce(pw
->p
[i
].set
);
495 isl_ctx
*FN(PW
,get_ctx
)(__isl_keep PW
*pw
)
497 return pw
? isl_dim_get_ctx(pw
->dim
) : NULL
;
500 int FN(PW
,involves_dims
)(__isl_keep PW
*pw
, enum isl_dim_type type
,
501 unsigned first
, unsigned n
)
507 if (pw
->n
== 0 || n
== 0)
509 for (i
= 0; i
< pw
->n
; ++i
) {
510 int involves
= FN(EL
,involves_dims
)(pw
->p
[i
].FIELD
,
512 if (involves
< 0 || involves
)
514 involves
= isl_set_involves_dims(pw
->p
[i
].set
, type
, first
, n
);
515 if (involves
< 0 || involves
)
521 __isl_give PW
*FN(PW
,set_dim_name
)(__isl_take PW
*pw
,
522 enum isl_dim_type type
, unsigned pos
, const char *s
)
530 pw
->dim
= isl_dim_set_name(pw
->dim
, type
, pos
, s
);
534 for (i
= 0; i
< pw
->n
; ++i
) {
535 pw
->p
[i
].set
= isl_set_set_dim_name(pw
->p
[i
].set
, type
, pos
, s
);
538 pw
->p
[i
].FIELD
= FN(EL
,set_dim_name
)(pw
->p
[i
].FIELD
, type
, pos
, s
);
549 __isl_give PW
*FN(PW
,drop_dims
)(__isl_take PW
*pw
,
550 enum isl_dim_type type
, unsigned first
, unsigned n
)
556 if (n
== 0 && !isl_dim_get_tuple_name(pw
->dim
, type
))
562 pw
->dim
= isl_dim_drop(pw
->dim
, type
, first
, n
);
565 for (i
= 0; i
< pw
->n
; ++i
) {
566 pw
->p
[i
].set
= isl_set_drop(pw
->p
[i
].set
, type
, first
, n
);
569 pw
->p
[i
].FIELD
= FN(EL
,drop_dims
)(pw
->p
[i
].FIELD
, type
, first
, n
);
580 __isl_give PW
*FN(PW
,insert_dims
)(__isl_take PW
*pw
, enum isl_dim_type type
,
581 unsigned first
, unsigned n
)
587 if (n
== 0 && !isl_dim_is_named_or_nested(pw
->dim
, type
))
594 pw
->dim
= isl_dim_insert(pw
->dim
, type
, first
, n
);
598 for (i
= 0; i
< pw
->n
; ++i
) {
599 pw
->p
[i
].set
= isl_set_insert(pw
->p
[i
].set
, type
, first
, n
);
602 pw
->p
[i
].FIELD
= FN(EL
,insert_dims
)(pw
->p
[i
].FIELD
,
614 __isl_give PW
*FN(PW
,fix_dim
)(__isl_take PW
*pw
,
615 enum isl_dim_type type
, unsigned pos
, isl_int v
)
625 for (i
= 0; i
< pw
->n
; ++i
) {
626 pw
->p
[i
].set
= isl_set_fix(pw
->p
[i
].set
, type
, pos
, v
);
637 unsigned FN(PW
,dim
)(__isl_keep PW
*pw
, enum isl_dim_type type
)
639 return pw
? isl_dim_size(pw
->dim
, type
) : 0;
642 __isl_give PW
*FN(PW
,split_dims
)(__isl_take PW
*pw
,
643 enum isl_dim_type type
, unsigned first
, unsigned n
)
657 for (i
= 0; i
< pw
->n
; ++i
) {
658 pw
->p
[i
].set
= isl_set_split_dims(pw
->p
[i
].set
, type
, first
, n
);
669 /* Compute the maximal value attained by the piecewise quasipolynomial
670 * on its domain or zero if the domain is empty.
671 * In the worst case, the domain is scanned completely,
672 * so the domain is assumed to be bounded.
674 __isl_give isl_qpolynomial
*FN(PW
,opt
)(__isl_take PW
*pw
, int max
)
677 isl_qpolynomial
*opt
;
683 isl_dim
*dim
= isl_dim_copy(pw
->dim
);
685 return isl_qpolynomial_zero(dim
);
688 opt
= FN(EL
,opt_on_domain
)(FN(EL
,copy
)(pw
->p
[0].FIELD
),
689 isl_set_copy(pw
->p
[0].set
), max
);
690 for (i
= 1; i
< pw
->n
; ++i
) {
691 isl_qpolynomial
*opt_i
;
692 opt_i
= FN(EL
,opt_on_domain
)(FN(EL
,copy
)(pw
->p
[i
].FIELD
),
693 isl_set_copy(pw
->p
[i
].set
), max
);
695 opt
= isl_qpolynomial_max_cst(opt
, opt_i
);
697 opt
= isl_qpolynomial_min_cst(opt
, opt_i
);
704 __isl_give isl_qpolynomial
*FN(PW
,max
)(__isl_take PW
*pw
)
706 return FN(PW
,opt
)(pw
, 1);
709 __isl_give isl_qpolynomial
*FN(PW
,min
)(__isl_take PW
*pw
)
711 return FN(PW
,opt
)(pw
, 0);
714 __isl_give isl_dim
*FN(PW
,get_dim
)(__isl_keep PW
*pw
)
716 return pw
? isl_dim_copy(pw
->dim
) : NULL
;
719 __isl_give PW
*FN(PW
,reset_dim
)(__isl_take PW
*pw
, __isl_take isl_dim
*dim
)
727 for (i
= 0; i
< pw
->n
; ++i
) {
728 pw
->p
[i
].set
= isl_set_reset_dim(pw
->p
[i
].set
,
732 pw
->p
[i
].FIELD
= FN(EL
,reset_dim
)(pw
->p
[i
].FIELD
,
737 isl_dim_free(pw
->dim
);
747 int FN(PW
,has_equal_dim
)(__isl_keep PW
*pw1
, __isl_keep PW
*pw2
)
752 return isl_dim_equal(pw1
->dim
, pw2
->dim
);
755 __isl_give PW
*FN(PW
,morph
)(__isl_take PW
*pw
, __isl_take isl_morph
*morph
)
763 ctx
= isl_dim_get_ctx(pw
->dim
);
764 isl_assert(ctx
, isl_dim_equal(pw
->dim
, morph
->dom
->dim
),
770 isl_dim_free(pw
->dim
);
771 pw
->dim
= isl_dim_copy(morph
->ran
->dim
);
775 for (i
= 0; i
< pw
->n
; ++i
) {
776 pw
->p
[i
].set
= isl_morph_set(isl_morph_copy(morph
), pw
->p
[i
].set
);
779 pw
->p
[i
].FIELD
= FN(EL
,morph
)(pw
->p
[i
].FIELD
,
780 isl_morph_copy(morph
));
785 isl_morph_free(morph
);
790 isl_morph_free(morph
);
794 int FN(PW
,foreach_piece
)(__isl_keep PW
*pw
,
795 int (*fn
)(__isl_take isl_set
*set
, __isl_take EL
*el
, void *user
),
803 for (i
= 0; i
< pw
->n
; ++i
)
804 if (fn(isl_set_copy(pw
->p
[i
].set
),
805 FN(EL
,copy
)(pw
->p
[i
].FIELD
), user
) < 0)
811 static int any_divs(__isl_keep isl_set
*set
)
818 for (i
= 0; i
< set
->n
; ++i
)
819 if (set
->p
[i
]->n_div
> 0)
825 static int foreach_lifted_subset(__isl_take isl_set
*set
, __isl_take EL
*el
,
826 int (*fn
)(__isl_take isl_set
*set
, __isl_take EL
*el
,
827 void *user
), void *user
)
834 for (i
= 0; i
< set
->n
; ++i
) {
838 lift
= isl_set_from_basic_set(isl_basic_set_copy(set
->p
[i
]));
839 lift
= isl_set_lift(lift
);
841 copy
= FN(EL
,copy
)(el
);
842 copy
= FN(EL
,lift
)(copy
, isl_set_get_dim(lift
));
844 if (fn(lift
, copy
, user
) < 0)
858 int FN(PW
,foreach_lifted_piece
)(__isl_keep PW
*pw
,
859 int (*fn
)(__isl_take isl_set
*set
, __isl_take EL
*el
,
860 void *user
), void *user
)
867 for (i
= 0; i
< pw
->n
; ++i
) {
871 set
= isl_set_copy(pw
->p
[i
].set
);
872 el
= FN(EL
,copy
)(pw
->p
[i
].FIELD
);
873 if (!any_divs(set
)) {
874 if (fn(set
, el
, user
) < 0)
878 if (foreach_lifted_subset(set
, el
, fn
, user
) < 0)
885 __isl_give PW
*FN(PW
,move_dims
)(__isl_take PW
*pw
,
886 enum isl_dim_type dst_type
, unsigned dst_pos
,
887 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
895 pw
->dim
= isl_dim_move(pw
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
899 for (i
= 0; i
< pw
->n
; ++i
) {
900 pw
->p
[i
].set
= isl_set_move_dims(pw
->p
[i
].set
,
902 src_type
, src_pos
, n
);
905 pw
->p
[i
].FIELD
= FN(EL
,move_dims
)(pw
->p
[i
].FIELD
,
906 dst_type
, dst_pos
, src_type
, src_pos
, n
);
917 __isl_give PW
*FN(PW
,realign
)(__isl_take PW
*pw
, __isl_take isl_reordering
*exp
)
925 for (i
= 0; i
< pw
->n
; ++i
) {
926 pw
->p
[i
].set
= isl_set_realign(pw
->p
[i
].set
,
927 isl_reordering_copy(exp
));
930 pw
->p
[i
].FIELD
= FN(EL
,realign
)(pw
->p
[i
].FIELD
,
931 isl_reordering_copy(exp
));
936 pw
= FN(PW
,reset_dim
)(pw
, isl_dim_copy(exp
->dim
));
938 isl_reordering_free(exp
);
941 isl_reordering_free(exp
);
946 __isl_give PW
*FN(PW
,mul_isl_int
)(__isl_take PW
*pw
, isl_int v
)
950 if (isl_int_is_one(v
))
952 if (pw
&& isl_int_is_zero(v
)) {
954 isl_dim
*dim
= FN(PW
,get_dim
)(pw
);
956 zero
= FN(PW
,zero
)(dim
, pw
->type
);
958 zero
= FN(PW
,zero
)(dim
);
970 if (isl_int_is_neg(v
))
971 pw
->type
= isl_fold_type_negate(pw
->type
);
973 for (i
= 0; i
< pw
->n
; ++i
) {
974 pw
->p
[i
].FIELD
= FN(EL
,scale
)(pw
->p
[i
].FIELD
, v
);