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
,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
));
314 __isl_give PW
*FN(PW
,neg
)(__isl_take PW
*pw
)
321 if (FN(PW
,IS_ZERO
)(pw
))
328 for (i
= 0; i
< pw
->n
; ++i
) {
329 pw
->p
[i
].FIELD
= FN(EL
,neg
)(pw
->p
[i
].FIELD
);
331 return FN(PW
,free
)(pw
);
337 __isl_give PW
*FN(PW
,sub
)(__isl_take PW
*pw1
, __isl_take PW
*pw2
)
339 return FN(PW
,add
)(pw1
, FN(PW
,neg
)(pw2
));
344 __isl_give isl_qpolynomial
*FN(PW
,eval
)(__isl_take PW
*pw
,
345 __isl_take isl_point
*pnt
)
350 isl_dim
*pnt_dim
= NULL
;
355 ctx
= isl_point_get_ctx(pnt
);
356 pnt_dim
= isl_point_get_dim(pnt
);
357 isl_assert(ctx
, isl_dim_equal(pnt_dim
, pw
->dim
), goto error
);
359 for (i
= 0; i
< pw
->n
; ++i
) {
360 found
= isl_set_contains_point(pw
->p
[i
].set
, pnt
);
367 qp
= FN(EL
,eval
)(FN(EL
,copy
)(pw
->p
[i
].FIELD
),
368 isl_point_copy(pnt
));
370 qp
= isl_qpolynomial_zero(isl_dim_copy(pw
->dim
));
372 isl_dim_free(pnt_dim
);
377 isl_dim_free(pnt_dim
);
383 __isl_give isl_set
*FN(PW
,domain
)(__isl_take PW
*pw
)
391 dom
= isl_set_empty(isl_dim_copy(pw
->dim
));
392 for (i
= 0; i
< pw
->n
; ++i
)
393 dom
= isl_set_union_disjoint(dom
, isl_set_copy(pw
->p
[i
].set
));
400 __isl_give PW
*FN(PW
,intersect_domain
)(__isl_take PW
*pw
, __isl_take isl_set
*set
)
416 for (i
= pw
->n
- 1; i
>= 0; --i
) {
418 pw
->p
[i
].set
= isl_set_intersect(pw
->p
[i
].set
, isl_set_copy(set
));
421 aff
= isl_set_affine_hull(isl_set_copy(pw
->p
[i
].set
));
422 pw
->p
[i
].FIELD
= FN(EL
,substitute_equalities
)(pw
->p
[i
].FIELD
,
424 if (isl_set_plain_is_empty(pw
->p
[i
].set
)) {
425 isl_set_free(pw
->p
[i
].set
);
426 FN(EL
,free
)(pw
->p
[i
].FIELD
);
428 pw
->p
[i
] = pw
->p
[pw
->n
- 1];
441 __isl_give PW
*FN(PW
,gist
)(__isl_take PW
*pw
, __isl_take isl_set
*context
)
444 isl_basic_set
*hull
= NULL
;
450 isl_set_free(context
);
454 context
= isl_set_compute_divs(context
);
455 hull
= isl_set_simple_hull(isl_set_copy(context
));
461 for (i
= pw
->n
- 1; i
>= 0; --i
) {
462 pw
->p
[i
].set
= isl_set_intersect(pw
->p
[i
].set
,
463 isl_set_copy(context
));
466 pw
->p
[i
].FIELD
= FN(EL
,gist
)(pw
->p
[i
].FIELD
,
467 isl_set_copy(pw
->p
[i
].set
));
468 pw
->p
[i
].set
= isl_set_gist_basic_set(pw
->p
[i
].set
,
469 isl_basic_set_copy(hull
));
472 if (isl_set_plain_is_empty(pw
->p
[i
].set
)) {
473 isl_set_free(pw
->p
[i
].set
);
474 FN(EL
,free
)(pw
->p
[i
].FIELD
);
476 pw
->p
[i
] = pw
->p
[pw
->n
- 1];
481 isl_basic_set_free(hull
);
482 isl_set_free(context
);
487 isl_basic_set_free(hull
);
488 isl_set_free(context
);
492 __isl_give PW
*FN(PW
,coalesce
)(__isl_take PW
*pw
)
501 for (i
= pw
->n
- 1; i
>= 0; --i
) {
502 for (j
= i
- 1; j
>= 0; --j
) {
503 if (!FN(EL
,plain_is_equal
)(pw
->p
[i
].FIELD
,
506 pw
->p
[j
].set
= isl_set_union(pw
->p
[j
].set
,
508 FN(EL
,free
)(pw
->p
[i
].FIELD
);
510 pw
->p
[i
] = pw
->p
[pw
->n
- 1];
516 pw
->p
[i
].set
= isl_set_coalesce(pw
->p
[i
].set
);
527 isl_ctx
*FN(PW
,get_ctx
)(__isl_keep PW
*pw
)
529 return pw
? isl_dim_get_ctx(pw
->dim
) : NULL
;
532 #ifndef NO_INVOLVES_DIMS
533 int FN(PW
,involves_dims
)(__isl_keep PW
*pw
, enum isl_dim_type type
,
534 unsigned first
, unsigned n
)
540 if (pw
->n
== 0 || n
== 0)
542 for (i
= 0; i
< pw
->n
; ++i
) {
543 int involves
= FN(EL
,involves_dims
)(pw
->p
[i
].FIELD
,
545 if (involves
< 0 || involves
)
547 involves
= isl_set_involves_dims(pw
->p
[i
].set
, type
, first
, n
);
548 if (involves
< 0 || involves
)
555 __isl_give PW
*FN(PW
,set_dim_name
)(__isl_take PW
*pw
,
556 enum isl_dim_type type
, unsigned pos
, const char *s
)
564 pw
->dim
= isl_dim_set_name(pw
->dim
, type
, pos
, s
);
568 for (i
= 0; i
< pw
->n
; ++i
) {
569 pw
->p
[i
].set
= isl_set_set_dim_name(pw
->p
[i
].set
, type
, pos
, s
);
572 pw
->p
[i
].FIELD
= FN(EL
,set_dim_name
)(pw
->p
[i
].FIELD
, type
, pos
, s
);
584 __isl_give PW
*FN(PW
,drop_dims
)(__isl_take PW
*pw
,
585 enum isl_dim_type type
, unsigned first
, unsigned n
)
591 if (n
== 0 && !isl_dim_get_tuple_name(pw
->dim
, type
))
597 pw
->dim
= isl_dim_drop(pw
->dim
, type
, first
, n
);
600 for (i
= 0; i
< pw
->n
; ++i
) {
601 pw
->p
[i
].set
= isl_set_drop(pw
->p
[i
].set
, type
, first
, n
);
604 pw
->p
[i
].FIELD
= FN(EL
,drop_dims
)(pw
->p
[i
].FIELD
, type
, first
, n
);
616 #ifndef NO_INSERT_DIMS
617 __isl_give PW
*FN(PW
,insert_dims
)(__isl_take PW
*pw
, enum isl_dim_type type
,
618 unsigned first
, unsigned n
)
624 if (n
== 0 && !isl_dim_is_named_or_nested(pw
->dim
, type
))
631 pw
->dim
= isl_dim_insert(pw
->dim
, type
, first
, n
);
635 for (i
= 0; i
< pw
->n
; ++i
) {
636 pw
->p
[i
].set
= isl_set_insert(pw
->p
[i
].set
, type
, first
, n
);
639 pw
->p
[i
].FIELD
= FN(EL
,insert_dims
)(pw
->p
[i
].FIELD
,
652 __isl_give PW
*FN(PW
,fix_dim
)(__isl_take PW
*pw
,
653 enum isl_dim_type type
, unsigned pos
, isl_int v
)
663 for (i
= 0; i
< pw
->n
; ++i
) {
664 pw
->p
[i
].set
= isl_set_fix(pw
->p
[i
].set
, type
, pos
, v
);
675 unsigned FN(PW
,dim
)(__isl_keep PW
*pw
, enum isl_dim_type type
)
677 return pw
? isl_dim_size(pw
->dim
, type
) : 0;
680 __isl_give PW
*FN(PW
,split_dims
)(__isl_take PW
*pw
,
681 enum isl_dim_type type
, unsigned first
, unsigned n
)
695 for (i
= 0; i
< pw
->n
; ++i
) {
696 pw
->p
[i
].set
= isl_set_split_dims(pw
->p
[i
].set
, type
, first
, n
);
708 /* Compute the maximal value attained by the piecewise quasipolynomial
709 * on its domain or zero if the domain is empty.
710 * In the worst case, the domain is scanned completely,
711 * so the domain is assumed to be bounded.
713 __isl_give isl_qpolynomial
*FN(PW
,opt
)(__isl_take PW
*pw
, int max
)
716 isl_qpolynomial
*opt
;
722 isl_dim
*dim
= isl_dim_copy(pw
->dim
);
724 return isl_qpolynomial_zero(dim
);
727 opt
= FN(EL
,opt_on_domain
)(FN(EL
,copy
)(pw
->p
[0].FIELD
),
728 isl_set_copy(pw
->p
[0].set
), max
);
729 for (i
= 1; i
< pw
->n
; ++i
) {
730 isl_qpolynomial
*opt_i
;
731 opt_i
= FN(EL
,opt_on_domain
)(FN(EL
,copy
)(pw
->p
[i
].FIELD
),
732 isl_set_copy(pw
->p
[i
].set
), max
);
734 opt
= isl_qpolynomial_max_cst(opt
, opt_i
);
736 opt
= isl_qpolynomial_min_cst(opt
, opt_i
);
743 __isl_give isl_qpolynomial
*FN(PW
,max
)(__isl_take PW
*pw
)
745 return FN(PW
,opt
)(pw
, 1);
748 __isl_give isl_qpolynomial
*FN(PW
,min
)(__isl_take PW
*pw
)
750 return FN(PW
,opt
)(pw
, 0);
754 __isl_give isl_dim
*FN(PW
,get_dim
)(__isl_keep PW
*pw
)
756 return pw
? isl_dim_copy(pw
->dim
) : NULL
;
760 __isl_give PW
*FN(PW
,reset_dim
)(__isl_take PW
*pw
, __isl_take isl_dim
*dim
)
768 for (i
= 0; i
< pw
->n
; ++i
) {
769 pw
->p
[i
].set
= isl_set_reset_dim(pw
->p
[i
].set
,
773 pw
->p
[i
].FIELD
= FN(EL
,reset_dim
)(pw
->p
[i
].FIELD
,
778 isl_dim_free(pw
->dim
);
789 int FN(PW
,has_equal_dim
)(__isl_keep PW
*pw1
, __isl_keep PW
*pw2
)
794 return isl_dim_equal(pw1
->dim
, pw2
->dim
);
798 __isl_give PW
*FN(PW
,morph
)(__isl_take PW
*pw
, __isl_take isl_morph
*morph
)
806 ctx
= isl_dim_get_ctx(pw
->dim
);
807 isl_assert(ctx
, isl_dim_equal(pw
->dim
, morph
->dom
->dim
),
813 isl_dim_free(pw
->dim
);
814 pw
->dim
= isl_dim_copy(morph
->ran
->dim
);
818 for (i
= 0; i
< pw
->n
; ++i
) {
819 pw
->p
[i
].set
= isl_morph_set(isl_morph_copy(morph
), pw
->p
[i
].set
);
822 pw
->p
[i
].FIELD
= FN(EL
,morph
)(pw
->p
[i
].FIELD
,
823 isl_morph_copy(morph
));
828 isl_morph_free(morph
);
833 isl_morph_free(morph
);
838 int FN(PW
,foreach_piece
)(__isl_keep PW
*pw
,
839 int (*fn
)(__isl_take isl_set
*set
, __isl_take EL
*el
, void *user
),
847 for (i
= 0; i
< pw
->n
; ++i
)
848 if (fn(isl_set_copy(pw
->p
[i
].set
),
849 FN(EL
,copy
)(pw
->p
[i
].FIELD
), user
) < 0)
856 static int any_divs(__isl_keep isl_set
*set
)
863 for (i
= 0; i
< set
->n
; ++i
)
864 if (set
->p
[i
]->n_div
> 0)
870 static int foreach_lifted_subset(__isl_take isl_set
*set
, __isl_take EL
*el
,
871 int (*fn
)(__isl_take isl_set
*set
, __isl_take EL
*el
,
872 void *user
), void *user
)
879 for (i
= 0; i
< set
->n
; ++i
) {
883 lift
= isl_set_from_basic_set(isl_basic_set_copy(set
->p
[i
]));
884 lift
= isl_set_lift(lift
);
886 copy
= FN(EL
,copy
)(el
);
887 copy
= FN(EL
,lift
)(copy
, isl_set_get_dim(lift
));
889 if (fn(lift
, copy
, user
) < 0)
903 int FN(PW
,foreach_lifted_piece
)(__isl_keep PW
*pw
,
904 int (*fn
)(__isl_take isl_set
*set
, __isl_take EL
*el
,
905 void *user
), void *user
)
912 for (i
= 0; i
< pw
->n
; ++i
) {
916 set
= isl_set_copy(pw
->p
[i
].set
);
917 el
= FN(EL
,copy
)(pw
->p
[i
].FIELD
);
918 if (!any_divs(set
)) {
919 if (fn(set
, el
, user
) < 0)
923 if (foreach_lifted_subset(set
, el
, fn
, user
) < 0)
932 __isl_give PW
*FN(PW
,move_dims
)(__isl_take PW
*pw
,
933 enum isl_dim_type dst_type
, unsigned dst_pos
,
934 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
942 pw
->dim
= isl_dim_move(pw
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
946 for (i
= 0; i
< pw
->n
; ++i
) {
947 pw
->p
[i
].set
= isl_set_move_dims(pw
->p
[i
].set
,
949 src_type
, src_pos
, n
);
952 pw
->p
[i
].FIELD
= FN(EL
,move_dims
)(pw
->p
[i
].FIELD
,
953 dst_type
, dst_pos
, src_type
, src_pos
, n
);
966 __isl_give PW
*FN(PW
,realign
)(__isl_take PW
*pw
, __isl_take isl_reordering
*exp
)
974 for (i
= 0; i
< pw
->n
; ++i
) {
975 pw
->p
[i
].set
= isl_set_realign(pw
->p
[i
].set
,
976 isl_reordering_copy(exp
));
979 pw
->p
[i
].FIELD
= FN(EL
,realign
)(pw
->p
[i
].FIELD
,
980 isl_reordering_copy(exp
));
985 pw
= FN(PW
,reset_dim
)(pw
, isl_dim_copy(exp
->dim
));
987 isl_reordering_free(exp
);
990 isl_reordering_free(exp
);
995 /* Align the parameters of "pw" to those of "model".
997 __isl_give PW
*FN(PW
,align_params
)(__isl_take PW
*pw
, __isl_take isl_dim
*model
)
1004 ctx
= isl_dim_get_ctx(model
);
1005 if (!isl_dim_has_named_params(model
))
1006 isl_die(ctx
, isl_error_invalid
,
1007 "model has unnamed parameters", goto error
);
1008 if (!isl_dim_has_named_params(pw
->dim
))
1009 isl_die(ctx
, isl_error_invalid
,
1010 "input has unnamed parameters", goto error
);
1011 if (!isl_dim_match(pw
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
1012 isl_reordering
*exp
;
1014 model
= isl_dim_drop(model
, isl_dim_in
,
1015 0, isl_dim_size(model
, isl_dim_in
));
1016 model
= isl_dim_drop(model
, isl_dim_out
,
1017 0, isl_dim_size(model
, isl_dim_out
));
1018 exp
= isl_parameter_alignment_reordering(pw
->dim
, model
);
1019 exp
= isl_reordering_extend_dim(exp
, FN(PW
,get_dim
)(pw
));
1020 pw
= FN(PW
,realign
)(pw
, exp
);
1023 isl_dim_free(model
);
1026 isl_dim_free(model
);
1032 __isl_give PW
*FN(PW
,mul_isl_int
)(__isl_take PW
*pw
, isl_int v
)
1036 if (isl_int_is_one(v
))
1038 if (pw
&& isl_int_is_zero(v
)) {
1040 isl_dim
*dim
= FN(PW
,get_dim
)(pw
);
1042 zero
= FN(PW
,ZERO
)(dim
, pw
->type
);
1044 zero
= FN(PW
,ZERO
)(dim
);
1049 pw
= FN(PW
,cow
)(pw
);
1056 if (isl_int_is_neg(v
))
1057 pw
->type
= isl_fold_type_negate(pw
->type
);
1059 for (i
= 0; i
< pw
->n
; ++i
) {
1060 pw
->p
[i
].FIELD
= FN(EL
,scale
)(pw
->p
[i
].FIELD
, v
);
1061 if (!pw
->p
[i
].FIELD
)
1071 __isl_give PW
*FN(PW
,scale
)(__isl_take PW
*pw
, isl_int v
)
1073 return FN(PW
,mul_isl_int
)(pw
, v
);