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 __isl_give PW
*FN(PW
,alloc_size
)(__isl_take isl_space
*dim
,
8 enum isl_fold type
, int n
)
10 __isl_give PW
*FN(PW
,alloc_size
)(__isl_take isl_space
*dim
, int n
)
18 ctx
= isl_space_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_space
*dim
, enum isl_fold type
)
41 return FN(PW
,alloc_size
)(dim
, type
, 0);
44 __isl_give PW
*FN(PW
,ZERO
)(__isl_take isl_space
*dim
)
46 return FN(PW
,alloc_size
)(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_space
*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_space(el
));
72 isl_assert(ctx
, isl_space_is_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
;
79 isl_space_free(el_dim
);
82 isl_space_free(el_dim
);
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_size
)(FN(EL
,get_space
)(el
), type
, 1);
104 pw
= FN(PW
,alloc_size
)(FN(EL
,get_space
)(el
), 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_size
)(isl_space_copy(pw
->dim
), pw
->type
, pw
->n
);
125 dup
= FN(PW
,alloc_size
)(isl_space_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_space_free(pw
->dim
);
176 const char *FN(PW
,get_dim_name
)(__isl_keep PW
*pw
, enum isl_dim_type type
,
179 return pw
? isl_space_get_dim_name(pw
->dim
, type
, pos
) : NULL
;
182 int FN(PW
,has_dim_id
)(__isl_keep PW
*pw
, enum isl_dim_type type
, unsigned pos
)
184 return pw
? isl_space_has_dim_id(pw
->dim
, type
, pos
) : -1;
187 __isl_give isl_id
*FN(PW
,get_dim_id
)(__isl_keep PW
*pw
, enum isl_dim_type type
,
190 return pw
? isl_space_get_dim_id(pw
->dim
, type
, pos
) : NULL
;
193 int FN(PW
,has_tuple_name
)(__isl_keep PW
*pw
, enum isl_dim_type type
)
195 return pw
? isl_space_has_tuple_name(pw
->dim
, type
) : -1;
198 const char *FN(PW
,get_tuple_name
)(__isl_keep PW
*pw
, enum isl_dim_type type
)
200 return pw
? isl_space_get_tuple_name(pw
->dim
, type
) : NULL
;
203 int FN(PW
,has_tuple_id
)(__isl_keep PW
*pw
, enum isl_dim_type type
)
205 return pw
? isl_space_has_tuple_id(pw
->dim
, type
) : -1;
208 __isl_give isl_id
*FN(PW
,get_tuple_id
)(__isl_keep PW
*pw
, enum isl_dim_type type
)
210 return pw
? isl_space_get_tuple_id(pw
->dim
, type
) : NULL
;
213 int FN(PW
,IS_ZERO
)(__isl_keep PW
*pw
)
222 __isl_give PW
*FN(PW
,realign_domain
)(__isl_take PW
*pw
,
223 __isl_take isl_reordering
*exp
)
231 for (i
= 0; i
< pw
->n
; ++i
) {
232 pw
->p
[i
].set
= isl_set_realign(pw
->p
[i
].set
,
233 isl_reordering_copy(exp
));
236 pw
->p
[i
].FIELD
= FN(EL
,realign_domain
)(pw
->p
[i
].FIELD
,
237 isl_reordering_copy(exp
));
242 pw
= FN(PW
,reset_domain_space
)(pw
, isl_space_copy(exp
->dim
));
244 isl_reordering_free(exp
);
247 isl_reordering_free(exp
);
252 /* Align the parameters of "pw" to those of "model".
254 __isl_give PW
*FN(PW
,align_params
)(__isl_take PW
*pw
, __isl_take isl_space
*model
)
261 ctx
= isl_space_get_ctx(model
);
262 if (!isl_space_has_named_params(model
))
263 isl_die(ctx
, isl_error_invalid
,
264 "model has unnamed parameters", goto error
);
265 if (!isl_space_has_named_params(pw
->dim
))
266 isl_die(ctx
, isl_error_invalid
,
267 "input has unnamed parameters", goto error
);
268 if (!isl_space_match(pw
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
271 model
= isl_space_drop_dims(model
, isl_dim_in
,
272 0, isl_space_dim(model
, isl_dim_in
));
273 model
= isl_space_drop_dims(model
, isl_dim_out
,
274 0, isl_space_dim(model
, isl_dim_out
));
275 exp
= isl_parameter_alignment_reordering(pw
->dim
, model
);
276 exp
= isl_reordering_extend_space(exp
,
277 FN(PW
,get_domain_space
)(pw
));
278 pw
= FN(PW
,realign_domain
)(pw
, exp
);
281 isl_space_free(model
);
284 isl_space_free(model
);
289 static __isl_give PW
*FN(PW
,align_params_pw_pw_and
)(__isl_take PW
*pw1
,
291 __isl_give PW
*(*fn
)(__isl_take PW
*pw1
, __isl_take PW
*pw2
))
297 if (isl_space_match(pw1
->dim
, isl_dim_param
, pw2
->dim
, isl_dim_param
))
299 ctx
= FN(PW
,get_ctx
)(pw1
);
300 if (!isl_space_has_named_params(pw1
->dim
) ||
301 !isl_space_has_named_params(pw2
->dim
))
302 isl_die(ctx
, isl_error_invalid
,
303 "unaligned unnamed parameters", goto error
);
304 pw1
= FN(PW
,align_params
)(pw1
, FN(PW
,get_space
)(pw2
));
305 pw2
= FN(PW
,align_params
)(pw2
, FN(PW
,get_space
)(pw1
));
313 static __isl_give PW
*FN(PW
,align_params_pw_set_and
)(__isl_take PW
*pw
,
314 __isl_take isl_set
*set
,
315 __isl_give PW
*(*fn
)(__isl_take PW
*pw
, __isl_take isl_set
*set
))
321 if (isl_space_match(pw
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
))
323 ctx
= FN(PW
,get_ctx
)(pw
);
324 if (!isl_space_has_named_params(pw
->dim
) ||
325 !isl_space_has_named_params(set
->dim
))
326 isl_die(ctx
, isl_error_invalid
,
327 "unaligned unnamed parameters", goto error
);
328 pw
= FN(PW
,align_params
)(pw
, isl_set_get_space(set
));
329 set
= isl_set_align_params(set
, FN(PW
,get_space
)(pw
));
338 static __isl_give PW
*FN(PW
,union_add_aligned
)(__isl_take PW
*pw1
,
349 ctx
= isl_space_get_ctx(pw1
->dim
);
351 if (pw1
->type
!= pw2
->type
)
352 isl_die(ctx
, isl_error_invalid
,
353 "fold types don't match", goto error
);
355 isl_assert(ctx
, isl_space_is_equal(pw1
->dim
, pw2
->dim
), goto error
);
357 if (FN(PW
,IS_ZERO
)(pw1
)) {
362 if (FN(PW
,IS_ZERO
)(pw2
)) {
367 n
= (pw1
->n
+ 1) * (pw2
->n
+ 1);
369 res
= FN(PW
,alloc_size
)(isl_space_copy(pw1
->dim
), pw1
->type
, n
);
371 res
= FN(PW
,alloc_size
)(isl_space_copy(pw1
->dim
), n
);
374 for (i
= 0; i
< pw1
->n
; ++i
) {
375 set
= isl_set_copy(pw1
->p
[i
].set
);
376 for (j
= 0; j
< pw2
->n
; ++j
) {
377 struct isl_set
*common
;
379 common
= isl_set_intersect(isl_set_copy(pw1
->p
[i
].set
),
380 isl_set_copy(pw2
->p
[j
].set
));
381 if (isl_set_plain_is_empty(common
)) {
382 isl_set_free(common
);
385 set
= isl_set_subtract(set
,
386 isl_set_copy(pw2
->p
[j
].set
));
388 sum
= FN(EL
,add_on_domain
)(common
,
389 FN(EL
,copy
)(pw1
->p
[i
].FIELD
),
390 FN(EL
,copy
)(pw2
->p
[j
].FIELD
));
392 res
= FN(PW
,add_piece
)(res
, common
, sum
);
394 res
= FN(PW
,add_piece
)(res
, set
, FN(EL
,copy
)(pw1
->p
[i
].FIELD
));
397 for (j
= 0; j
< pw2
->n
; ++j
) {
398 set
= isl_set_copy(pw2
->p
[j
].set
);
399 for (i
= 0; i
< pw1
->n
; ++i
)
400 set
= isl_set_subtract(set
,
401 isl_set_copy(pw1
->p
[i
].set
));
402 res
= FN(PW
,add_piece
)(res
, set
, FN(EL
,copy
)(pw2
->p
[j
].FIELD
));
415 /* Private version of "union_add". For isl_pw_qpolynomial and
416 * isl_pw_qpolynomial_fold, we prefer to simply call it "add".
418 static __isl_give PW
*FN(PW
,union_add_
)(__isl_take PW
*pw1
, __isl_take PW
*pw2
)
420 return FN(PW
,align_params_pw_pw_and
)(pw1
, pw2
,
421 &FN(PW
,union_add_aligned
));
424 /* Make sure "pw" has room for at least "n" more pieces.
426 * If there is only one reference to pw, we extend it in place.
427 * Otherwise, we create a new PW and copy the pieces.
429 static __isl_give PW
*FN(PW
,grow
)(__isl_take PW
*pw
, int n
)
437 if (pw
->n
+ n
<= pw
->size
)
439 ctx
= FN(PW
,get_ctx
)(pw
);
442 res
= isl_realloc(ctx
, pw
, struct PW
,
443 sizeof(struct PW
) + (n
- 1) * sizeof(S(PW
,piece
)));
445 return FN(PW
,free
)(pw
);
450 res
= FN(PW
,alloc_size
)(isl_space_copy(pw
->dim
), pw
->type
, n
);
452 res
= FN(PW
,alloc_size
)(isl_space_copy(pw
->dim
), n
);
455 return FN(PW
,free
)(pw
);
456 for (i
= 0; i
< pw
->n
; ++i
)
457 res
= FN(PW
,add_piece
)(res
, isl_set_copy(pw
->p
[i
].set
),
458 FN(EL
,copy
)(pw
->p
[i
].FIELD
));
463 static __isl_give PW
*FN(PW
,add_disjoint_aligned
)(__isl_take PW
*pw1
,
472 if (pw1
->size
< pw1
->n
+ pw2
->n
&& pw1
->n
< pw2
->n
)
473 return FN(PW
,add_disjoint_aligned
)(pw2
, pw1
);
475 ctx
= isl_space_get_ctx(pw1
->dim
);
477 if (pw1
->type
!= pw2
->type
)
478 isl_die(ctx
, isl_error_invalid
,
479 "fold types don't match", goto error
);
481 isl_assert(ctx
, isl_space_is_equal(pw1
->dim
, pw2
->dim
), goto error
);
483 if (FN(PW
,IS_ZERO
)(pw1
)) {
488 if (FN(PW
,IS_ZERO
)(pw2
)) {
493 pw1
= FN(PW
,grow
)(pw1
, pw2
->n
);
497 for (i
= 0; i
< pw2
->n
; ++i
)
498 pw1
= FN(PW
,add_piece
)(pw1
,
499 isl_set_copy(pw2
->p
[i
].set
),
500 FN(EL
,copy
)(pw2
->p
[i
].FIELD
));
511 __isl_give PW
*FN(PW
,add_disjoint
)(__isl_take PW
*pw1
, __isl_take PW
*pw2
)
513 return FN(PW
,align_params_pw_pw_and
)(pw1
, pw2
,
514 &FN(PW
,add_disjoint_aligned
));
517 /* This function is currently only used from isl_aff.c
519 static __isl_give PW
*FN(PW
,on_shared_domain_in
)(__isl_take PW
*pw1
,
520 __isl_take PW
*pw2
, __isl_take isl_space
*space
,
521 __isl_give EL
*(*fn
)(__isl_take EL
*el1
, __isl_take EL
*el2
))
522 __attribute__ ((unused
));
524 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
525 * The result of "fn" (and therefore also of this function) lives in "space".
527 static __isl_give PW
*FN(PW
,on_shared_domain_in
)(__isl_take PW
*pw1
,
528 __isl_take PW
*pw2
, __isl_take isl_space
*space
,
529 __isl_give EL
*(*fn
)(__isl_take EL
*el1
, __isl_take EL
*el2
))
539 res
= FN(PW
,alloc_size
)(space
, pw1
->type
, n
);
541 res
= FN(PW
,alloc_size
)(space
, n
);
544 for (i
= 0; i
< pw1
->n
; ++i
) {
545 for (j
= 0; j
< pw2
->n
; ++j
) {
550 common
= isl_set_intersect(
551 isl_set_copy(pw1
->p
[i
].set
),
552 isl_set_copy(pw2
->p
[j
].set
));
553 empty
= isl_set_plain_is_empty(common
);
554 if (empty
< 0 || empty
) {
555 isl_set_free(common
);
561 res_ij
= fn(FN(EL
,copy
)(pw1
->p
[i
].FIELD
),
562 FN(EL
,copy
)(pw2
->p
[j
].FIELD
));
563 res_ij
= FN(EL
,gist
)(res_ij
, isl_set_copy(common
));
565 res
= FN(PW
,add_piece
)(res
, common
, res_ij
);
573 isl_space_free(space
);
580 /* This function is currently only used from isl_aff.c
582 static __isl_give PW
*FN(PW
,on_shared_domain
)(__isl_take PW
*pw1
,
584 __isl_give EL
*(*fn
)(__isl_take EL
*el1
, __isl_take EL
*el2
))
585 __attribute__ ((unused
));
587 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
588 * The result of "fn" is assumed to live in the same space as "pw1" and "pw2".
590 static __isl_give PW
*FN(PW
,on_shared_domain
)(__isl_take PW
*pw1
,
592 __isl_give EL
*(*fn
)(__isl_take EL
*el1
, __isl_take EL
*el2
))
599 space
= isl_space_copy(pw1
->dim
);
600 return FN(PW
,on_shared_domain_in
)(pw1
, pw2
, space
, fn
);
608 __isl_give PW
*FN(PW
,neg
)(__isl_take PW
*pw
)
615 if (FN(PW
,IS_ZERO
)(pw
))
622 for (i
= 0; i
< pw
->n
; ++i
) {
623 pw
->p
[i
].FIELD
= FN(EL
,neg
)(pw
->p
[i
].FIELD
);
625 return FN(PW
,free
)(pw
);
631 __isl_give PW
*FN(PW
,sub
)(__isl_take PW
*pw1
, __isl_take PW
*pw2
)
633 return FN(PW
,add
)(pw1
, FN(PW
,neg
)(pw2
));
638 __isl_give isl_qpolynomial
*FN(PW
,eval
)(__isl_take PW
*pw
,
639 __isl_take isl_point
*pnt
)
644 isl_space
*pnt_dim
= NULL
;
649 ctx
= isl_point_get_ctx(pnt
);
650 pnt_dim
= isl_point_get_space(pnt
);
651 isl_assert(ctx
, isl_space_is_domain(pnt_dim
, pw
->dim
), goto error
);
653 for (i
= 0; i
< pw
->n
; ++i
) {
654 found
= isl_set_contains_point(pw
->p
[i
].set
, pnt
);
661 qp
= FN(EL
,eval
)(FN(EL
,copy
)(pw
->p
[i
].FIELD
),
662 isl_point_copy(pnt
));
664 qp
= isl_qpolynomial_zero_on_domain(FN(PW
,get_domain_space
)(pw
));
666 isl_space_free(pnt_dim
);
671 isl_space_free(pnt_dim
);
677 __isl_give isl_set
*FN(PW
,domain
)(__isl_take PW
*pw
)
685 dom
= isl_set_empty(FN(PW
,get_domain_space
)(pw
));
686 for (i
= 0; i
< pw
->n
; ++i
)
687 dom
= isl_set_union_disjoint(dom
, isl_set_copy(pw
->p
[i
].set
));
694 /* Restrict the domain of "pw" by combining each cell
695 * with "set" through a call to "fn", where "fn" may be
696 * isl_set_intersect or isl_set_intersect_params.
698 static __isl_give PW
*FN(PW
,intersect_aligned
)(__isl_take PW
*pw
,
699 __isl_take isl_set
*set
,
700 __isl_give isl_set
*(*fn
)(__isl_take isl_set
*set1
,
701 __isl_take isl_set
*set2
))
717 for (i
= pw
->n
- 1; i
>= 0; --i
) {
719 pw
->p
[i
].set
= fn(pw
->p
[i
].set
, isl_set_copy(set
));
722 aff
= isl_set_affine_hull(isl_set_copy(pw
->p
[i
].set
));
723 pw
->p
[i
].FIELD
= FN(EL
,substitute_equalities
)(pw
->p
[i
].FIELD
,
727 if (isl_set_plain_is_empty(pw
->p
[i
].set
)) {
728 isl_set_free(pw
->p
[i
].set
);
729 FN(EL
,free
)(pw
->p
[i
].FIELD
);
731 pw
->p
[i
] = pw
->p
[pw
->n
- 1];
744 static __isl_give PW
*FN(PW
,intersect_domain_aligned
)(__isl_take PW
*pw
,
745 __isl_take isl_set
*set
)
747 return FN(PW
,intersect_aligned
)(pw
, set
, &isl_set_intersect
);
750 __isl_give PW
*FN(PW
,intersect_domain
)(__isl_take PW
*pw
,
751 __isl_take isl_set
*context
)
753 return FN(PW
,align_params_pw_set_and
)(pw
, context
,
754 &FN(PW
,intersect_domain_aligned
));
757 static __isl_give PW
*FN(PW
,intersect_params_aligned
)(__isl_take PW
*pw
,
758 __isl_take isl_set
*set
)
760 return FN(PW
,intersect_aligned
)(pw
, set
, &isl_set_intersect_params
);
763 /* Intersect the domain of "pw" with the parameter domain "context".
765 __isl_give PW
*FN(PW
,intersect_params
)(__isl_take PW
*pw
,
766 __isl_take isl_set
*context
)
768 return FN(PW
,align_params_pw_set_and
)(pw
, context
,
769 &FN(PW
,intersect_params_aligned
));
772 static __isl_give PW
*FN(PW
,gist_aligned
)(__isl_take PW
*pw
,
773 __isl_take isl_set
*context
,
774 __isl_give EL
*(*fn_el
)(__isl_take EL
*el
,
775 __isl_take isl_set
*set
),
776 __isl_give isl_set
*(*fn_dom
)(__isl_take isl_set
*set
,
777 __isl_take isl_basic_set
*bset
))
780 isl_basic_set
*hull
= NULL
;
786 isl_set_free(context
);
790 if (!isl_space_match(pw
->dim
, isl_dim_param
,
791 context
->dim
, isl_dim_param
)) {
792 pw
= FN(PW
,align_params
)(pw
, isl_set_get_space(context
));
793 context
= isl_set_align_params(context
, FN(PW
,get_space
)(pw
));
796 context
= isl_set_compute_divs(context
);
797 hull
= isl_set_simple_hull(isl_set_copy(context
));
803 for (i
= pw
->n
- 1; i
>= 0; --i
) {
804 pw
->p
[i
].set
= isl_set_intersect(pw
->p
[i
].set
,
805 isl_set_copy(context
));
808 pw
->p
[i
].FIELD
= fn_el(pw
->p
[i
].FIELD
,
809 isl_set_copy(pw
->p
[i
].set
));
810 pw
->p
[i
].set
= fn_dom(pw
->p
[i
].set
, isl_basic_set_copy(hull
));
813 if (isl_set_plain_is_empty(pw
->p
[i
].set
)) {
814 isl_set_free(pw
->p
[i
].set
);
815 FN(EL
,free
)(pw
->p
[i
].FIELD
);
817 pw
->p
[i
] = pw
->p
[pw
->n
- 1];
822 isl_basic_set_free(hull
);
823 isl_set_free(context
);
828 isl_basic_set_free(hull
);
829 isl_set_free(context
);
833 static __isl_give PW
*FN(PW
,gist_domain_aligned
)(__isl_take PW
*pw
,
834 __isl_take isl_set
*set
)
836 return FN(PW
,gist_aligned
)(pw
, set
, &FN(EL
,gist
),
837 &isl_set_gist_basic_set
);
840 __isl_give PW
*FN(PW
,gist
)(__isl_take PW
*pw
, __isl_take isl_set
*context
)
842 return FN(PW
,align_params_pw_set_and
)(pw
, context
,
843 &FN(PW
,gist_domain_aligned
));
846 static __isl_give PW
*FN(PW
,gist_params_aligned
)(__isl_take PW
*pw
,
847 __isl_take isl_set
*set
)
849 return FN(PW
,gist_aligned
)(pw
, set
, &FN(EL
,gist_params
),
850 &isl_set_gist_params_basic_set
);
853 __isl_give PW
*FN(PW
,gist_params
)(__isl_take PW
*pw
,
854 __isl_take isl_set
*context
)
856 return FN(PW
,align_params_pw_set_and
)(pw
, context
,
857 &FN(PW
,gist_params_aligned
));
860 __isl_give PW
*FN(PW
,coalesce
)(__isl_take PW
*pw
)
869 for (i
= pw
->n
- 1; i
>= 0; --i
) {
870 for (j
= i
- 1; j
>= 0; --j
) {
871 if (!FN(EL
,plain_is_equal
)(pw
->p
[i
].FIELD
,
874 pw
->p
[j
].set
= isl_set_union(pw
->p
[j
].set
,
876 FN(EL
,free
)(pw
->p
[i
].FIELD
);
878 pw
->p
[i
] = pw
->p
[pw
->n
- 1];
884 pw
->p
[i
].set
= isl_set_coalesce(pw
->p
[i
].set
);
895 isl_ctx
*FN(PW
,get_ctx
)(__isl_keep PW
*pw
)
897 return pw
? isl_space_get_ctx(pw
->dim
) : NULL
;
900 #ifndef NO_INVOLVES_DIMS
901 int FN(PW
,involves_dims
)(__isl_keep PW
*pw
, enum isl_dim_type type
,
902 unsigned first
, unsigned n
)
905 enum isl_dim_type set_type
;
909 if (pw
->n
== 0 || n
== 0)
912 set_type
= type
== isl_dim_in
? isl_dim_set
: type
;
914 for (i
= 0; i
< pw
->n
; ++i
) {
915 int involves
= FN(EL
,involves_dims
)(pw
->p
[i
].FIELD
,
917 if (involves
< 0 || involves
)
919 involves
= isl_set_involves_dims(pw
->p
[i
].set
,
921 if (involves
< 0 || involves
)
928 __isl_give PW
*FN(PW
,set_dim_name
)(__isl_take PW
*pw
,
929 enum isl_dim_type type
, unsigned pos
, const char *s
)
932 enum isl_dim_type set_type
;
938 set_type
= type
== isl_dim_in
? isl_dim_set
: type
;
940 pw
->dim
= isl_space_set_dim_name(pw
->dim
, type
, pos
, s
);
944 for (i
= 0; i
< pw
->n
; ++i
) {
945 pw
->p
[i
].set
= isl_set_set_dim_name(pw
->p
[i
].set
,
949 pw
->p
[i
].FIELD
= FN(EL
,set_dim_name
)(pw
->p
[i
].FIELD
, type
, pos
, s
);
961 __isl_give PW
*FN(PW
,drop_dims
)(__isl_take PW
*pw
,
962 enum isl_dim_type type
, unsigned first
, unsigned n
)
965 enum isl_dim_type set_type
;
969 if (n
== 0 && !isl_space_get_tuple_name(pw
->dim
, type
))
972 set_type
= type
== isl_dim_in
? isl_dim_set
: type
;
977 pw
->dim
= isl_space_drop_dims(pw
->dim
, type
, first
, n
);
980 for (i
= 0; i
< pw
->n
; ++i
) {
981 pw
->p
[i
].set
= isl_set_drop(pw
->p
[i
].set
, set_type
, first
, n
);
984 pw
->p
[i
].FIELD
= FN(EL
,drop_dims
)(pw
->p
[i
].FIELD
, type
, first
, n
);
995 /* This function is very similar to drop_dims.
996 * The only difference is that the cells may still involve
997 * the specified dimensions. They are removed using
998 * isl_set_project_out instead of isl_set_drop.
1000 __isl_give PW
*FN(PW
,project_out
)(__isl_take PW
*pw
,
1001 enum isl_dim_type type
, unsigned first
, unsigned n
)
1004 enum isl_dim_type set_type
;
1008 if (n
== 0 && !isl_space_get_tuple_name(pw
->dim
, type
))
1011 set_type
= type
== isl_dim_in
? isl_dim_set
: type
;
1013 pw
= FN(PW
,cow
)(pw
);
1016 pw
->dim
= isl_space_drop_dims(pw
->dim
, type
, first
, n
);
1019 for (i
= 0; i
< pw
->n
; ++i
) {
1020 pw
->p
[i
].set
= isl_set_project_out(pw
->p
[i
].set
,
1021 set_type
, first
, n
);
1024 pw
->p
[i
].FIELD
= FN(EL
,drop_dims
)(pw
->p
[i
].FIELD
, type
, first
, n
);
1025 if (!pw
->p
[i
].FIELD
)
1035 /* Project the domain of pw onto its parameter space.
1037 __isl_give PW
*FN(PW
,project_domain_on_params
)(__isl_take PW
*pw
)
1042 n
= FN(PW
,dim
)(pw
, isl_dim_in
);
1043 pw
= FN(PW
,project_out
)(pw
, isl_dim_in
, 0, n
);
1044 space
= FN(PW
,get_domain_space
)(pw
);
1045 space
= isl_space_params(space
);
1046 pw
= FN(PW
,reset_domain_space
)(pw
, space
);
1051 #ifndef NO_INSERT_DIMS
1052 __isl_give PW
*FN(PW
,insert_dims
)(__isl_take PW
*pw
, enum isl_dim_type type
,
1053 unsigned first
, unsigned n
)
1056 enum isl_dim_type set_type
;
1060 if (n
== 0 && !isl_space_is_named_or_nested(pw
->dim
, type
))
1063 set_type
= type
== isl_dim_in
? isl_dim_set
: type
;
1065 pw
= FN(PW
,cow
)(pw
);
1069 pw
->dim
= isl_space_insert_dims(pw
->dim
, type
, first
, n
);
1073 for (i
= 0; i
< pw
->n
; ++i
) {
1074 pw
->p
[i
].set
= isl_set_insert_dims(pw
->p
[i
].set
,
1075 set_type
, first
, n
);
1078 pw
->p
[i
].FIELD
= FN(EL
,insert_dims
)(pw
->p
[i
].FIELD
,
1080 if (!pw
->p
[i
].FIELD
)
1091 __isl_give PW
*FN(PW
,fix_dim
)(__isl_take PW
*pw
,
1092 enum isl_dim_type type
, unsigned pos
, isl_int v
)
1099 if (type
== isl_dim_in
)
1102 pw
= FN(PW
,cow
)(pw
);
1105 for (i
= 0; i
< pw
->n
; ++i
) {
1106 pw
->p
[i
].set
= isl_set_fix(pw
->p
[i
].set
, type
, pos
, v
);
1117 unsigned FN(PW
,dim
)(__isl_keep PW
*pw
, enum isl_dim_type type
)
1119 return pw
? isl_space_dim(pw
->dim
, type
) : 0;
1122 __isl_give PW
*FN(PW
,split_dims
)(__isl_take PW
*pw
,
1123 enum isl_dim_type type
, unsigned first
, unsigned n
)
1132 if (type
== isl_dim_in
)
1135 pw
= FN(PW
,cow
)(pw
);
1140 for (i
= 0; i
< pw
->n
; ++i
) {
1141 pw
->p
[i
].set
= isl_set_split_dims(pw
->p
[i
].set
, type
, first
, n
);
1153 /* Compute the maximal value attained by the piecewise quasipolynomial
1154 * on its domain or zero if the domain is empty.
1155 * In the worst case, the domain is scanned completely,
1156 * so the domain is assumed to be bounded.
1158 __isl_give isl_qpolynomial
*FN(PW
,opt
)(__isl_take PW
*pw
, int max
)
1161 isl_qpolynomial
*opt
;
1167 isl_space
*dim
= isl_space_copy(pw
->dim
);
1169 return isl_qpolynomial_zero_on_domain(isl_space_domain(dim
));
1172 opt
= FN(EL
,opt_on_domain
)(FN(EL
,copy
)(pw
->p
[0].FIELD
),
1173 isl_set_copy(pw
->p
[0].set
), max
);
1174 for (i
= 1; i
< pw
->n
; ++i
) {
1175 isl_qpolynomial
*opt_i
;
1176 opt_i
= FN(EL
,opt_on_domain
)(FN(EL
,copy
)(pw
->p
[i
].FIELD
),
1177 isl_set_copy(pw
->p
[i
].set
), max
);
1179 opt
= isl_qpolynomial_max_cst(opt
, opt_i
);
1181 opt
= isl_qpolynomial_min_cst(opt
, opt_i
);
1188 __isl_give isl_qpolynomial
*FN(PW
,max
)(__isl_take PW
*pw
)
1190 return FN(PW
,opt
)(pw
, 1);
1193 __isl_give isl_qpolynomial
*FN(PW
,min
)(__isl_take PW
*pw
)
1195 return FN(PW
,opt
)(pw
, 0);
1199 __isl_give isl_space
*FN(PW
,get_space
)(__isl_keep PW
*pw
)
1201 return pw
? isl_space_copy(pw
->dim
) : NULL
;
1204 __isl_give isl_space
*FN(PW
,get_domain_space
)(__isl_keep PW
*pw
)
1206 return pw
? isl_space_domain(isl_space_copy(pw
->dim
)) : NULL
;
1209 #ifndef NO_RESET_DIM
1210 /* Reset the space of "pw". Since we don't know if the elements
1211 * represent the spaces themselves or their domains, we pass along
1212 * both when we call their reset_space_and_domain.
1214 static __isl_give PW
*FN(PW
,reset_space_and_domain
)(__isl_take PW
*pw
,
1215 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
1219 pw
= FN(PW
,cow
)(pw
);
1220 if (!pw
|| !space
|| !domain
)
1223 for (i
= 0; i
< pw
->n
; ++i
) {
1224 pw
->p
[i
].set
= isl_set_reset_space(pw
->p
[i
].set
,
1225 isl_space_copy(domain
));
1228 pw
->p
[i
].FIELD
= FN(EL
,reset_space_and_domain
)(pw
->p
[i
].FIELD
,
1229 isl_space_copy(space
), isl_space_copy(domain
));
1230 if (!pw
->p
[i
].FIELD
)
1234 isl_space_free(domain
);
1236 isl_space_free(pw
->dim
);
1241 isl_space_free(domain
);
1242 isl_space_free(space
);
1247 __isl_give PW
*FN(PW
,reset_domain_space
)(__isl_take PW
*pw
,
1248 __isl_take isl_space
*domain
)
1252 space
= isl_space_extend_domain_with_range(isl_space_copy(domain
),
1253 FN(PW
,get_space
)(pw
));
1254 return FN(PW
,reset_space_and_domain
)(pw
, space
, domain
);
1257 __isl_give PW
*FN(PW
,reset_space
)(__isl_take PW
*pw
, __isl_take isl_space
*dim
)
1261 domain
= isl_space_domain(isl_space_copy(dim
));
1262 return FN(PW
,reset_space_and_domain
)(pw
, dim
, domain
);
1265 __isl_give PW
*FN(PW
,set_tuple_id
)(__isl_keep PW
*pw
, enum isl_dim_type type
,
1266 __isl_take isl_id
*id
)
1270 pw
= FN(PW
,cow
)(pw
);
1272 return isl_id_free(id
);
1274 space
= FN(PW
,get_space
)(pw
);
1275 space
= isl_space_set_tuple_id(space
, type
, id
);
1277 return FN(PW
,reset_space
)(pw
, space
);
1280 __isl_give PW
*FN(PW
,set_dim_id
)(__isl_take PW
*pw
,
1281 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1283 pw
= FN(PW
,cow
)(pw
);
1285 return isl_id_free(id
);
1286 pw
->dim
= isl_space_set_dim_id(pw
->dim
, type
, pos
, id
);
1287 return FN(PW
,reset_space
)(pw
, isl_space_copy(pw
->dim
));
1291 int FN(PW
,has_equal_space
)(__isl_keep PW
*pw1
, __isl_keep PW
*pw2
)
1296 return isl_space_is_equal(pw1
->dim
, pw2
->dim
);
1300 __isl_give PW
*FN(PW
,morph_domain
)(__isl_take PW
*pw
,
1301 __isl_take isl_morph
*morph
)
1309 ctx
= isl_space_get_ctx(pw
->dim
);
1310 isl_assert(ctx
, isl_space_is_domain(morph
->dom
->dim
, pw
->dim
),
1313 pw
= FN(PW
,cow
)(pw
);
1316 pw
->dim
= isl_space_extend_domain_with_range(
1317 isl_space_copy(morph
->ran
->dim
), pw
->dim
);
1321 for (i
= 0; i
< pw
->n
; ++i
) {
1322 pw
->p
[i
].set
= isl_morph_set(isl_morph_copy(morph
), pw
->p
[i
].set
);
1325 pw
->p
[i
].FIELD
= FN(EL
,morph_domain
)(pw
->p
[i
].FIELD
,
1326 isl_morph_copy(morph
));
1327 if (!pw
->p
[i
].FIELD
)
1331 isl_morph_free(morph
);
1336 isl_morph_free(morph
);
1341 int FN(PW
,n_piece
)(__isl_keep PW
*pw
)
1343 return pw
? pw
->n
: 0;
1346 int FN(PW
,foreach_piece
)(__isl_keep PW
*pw
,
1347 int (*fn
)(__isl_take isl_set
*set
, __isl_take EL
*el
, void *user
),
1355 for (i
= 0; i
< pw
->n
; ++i
)
1356 if (fn(isl_set_copy(pw
->p
[i
].set
),
1357 FN(EL
,copy
)(pw
->p
[i
].FIELD
), user
) < 0)
1364 static int any_divs(__isl_keep isl_set
*set
)
1371 for (i
= 0; i
< set
->n
; ++i
)
1372 if (set
->p
[i
]->n_div
> 0)
1378 static int foreach_lifted_subset(__isl_take isl_set
*set
, __isl_take EL
*el
,
1379 int (*fn
)(__isl_take isl_set
*set
, __isl_take EL
*el
,
1380 void *user
), void *user
)
1387 for (i
= 0; i
< set
->n
; ++i
) {
1391 lift
= isl_set_from_basic_set(isl_basic_set_copy(set
->p
[i
]));
1392 lift
= isl_set_lift(lift
);
1394 copy
= FN(EL
,copy
)(el
);
1395 copy
= FN(EL
,lift
)(copy
, isl_set_get_space(lift
));
1397 if (fn(lift
, copy
, user
) < 0)
1411 int FN(PW
,foreach_lifted_piece
)(__isl_keep PW
*pw
,
1412 int (*fn
)(__isl_take isl_set
*set
, __isl_take EL
*el
,
1413 void *user
), void *user
)
1420 for (i
= 0; i
< pw
->n
; ++i
) {
1424 set
= isl_set_copy(pw
->p
[i
].set
);
1425 el
= FN(EL
,copy
)(pw
->p
[i
].FIELD
);
1426 if (!any_divs(set
)) {
1427 if (fn(set
, el
, user
) < 0)
1431 if (foreach_lifted_subset(set
, el
, fn
, user
) < 0)
1439 #ifndef NO_MOVE_DIMS
1440 __isl_give PW
*FN(PW
,move_dims
)(__isl_take PW
*pw
,
1441 enum isl_dim_type dst_type
, unsigned dst_pos
,
1442 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1446 pw
= FN(PW
,cow
)(pw
);
1450 pw
->dim
= isl_space_move_dims(pw
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
1454 for (i
= 0; i
< pw
->n
; ++i
) {
1455 pw
->p
[i
].FIELD
= FN(EL
,move_dims
)(pw
->p
[i
].FIELD
,
1456 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1457 if (!pw
->p
[i
].FIELD
)
1461 if (dst_type
== isl_dim_in
)
1462 dst_type
= isl_dim_set
;
1463 if (src_type
== isl_dim_in
)
1464 src_type
= isl_dim_set
;
1466 for (i
= 0; i
< pw
->n
; ++i
) {
1467 pw
->p
[i
].set
= isl_set_move_dims(pw
->p
[i
].set
,
1469 src_type
, src_pos
, n
);
1481 __isl_give PW
*FN(PW
,mul_isl_int
)(__isl_take PW
*pw
, isl_int v
)
1485 if (isl_int_is_one(v
))
1487 if (pw
&& DEFAULT_IS_ZERO
&& isl_int_is_zero(v
)) {
1489 isl_space
*dim
= FN(PW
,get_space
)(pw
);
1491 zero
= FN(PW
,ZERO
)(dim
, pw
->type
);
1493 zero
= FN(PW
,ZERO
)(dim
);
1498 pw
= FN(PW
,cow
)(pw
);
1505 if (isl_int_is_neg(v
))
1506 pw
->type
= isl_fold_type_negate(pw
->type
);
1508 for (i
= 0; i
< pw
->n
; ++i
) {
1509 pw
->p
[i
].FIELD
= FN(EL
,scale
)(pw
->p
[i
].FIELD
, v
);
1510 if (!pw
->p
[i
].FIELD
)
1520 __isl_give PW
*FN(PW
,scale
)(__isl_take PW
*pw
, isl_int v
)
1522 return FN(PW
,mul_isl_int
)(pw
, v
);
1525 static int FN(PW
,qsort_set_cmp
)(const void *p1
, const void *p2
)
1527 const isl_set
*set1
= *(const isl_set
**)p1
;
1528 const isl_set
*set2
= *(const isl_set
**)p2
;
1530 return isl_set_plain_cmp(set1
, set2
);
1533 /* We normalize in place, but if anything goes wrong we need
1534 * to return NULL, so we need to make sure we don't change the
1535 * meaning of any possible other copies of map.
1537 __isl_give PW
*FN(PW
,normalize
)(__isl_take PW
*pw
)
1544 for (i
= 0; i
< pw
->n
; ++i
) {
1545 set
= isl_set_normalize(isl_set_copy(pw
->p
[i
].set
));
1547 return FN(PW
,free
)(pw
);
1548 isl_set_free(pw
->p
[i
].set
);
1551 qsort(pw
->p
, pw
->n
, sizeof(pw
->p
[0]), &FN(PW
,qsort_set_cmp
));
1552 for (i
= pw
->n
- 1; i
>= 1; --i
) {
1553 if (!isl_set_plain_is_equal(pw
->p
[i
- 1].set
, pw
->p
[i
].set
))
1555 if (!FN(EL
,plain_is_equal
)(pw
->p
[i
- 1].FIELD
, pw
->p
[i
].FIELD
))
1557 set
= isl_set_union(isl_set_copy(pw
->p
[i
- 1].set
),
1558 isl_set_copy(pw
->p
[i
].set
));
1560 return FN(PW
,free
)(pw
);
1561 isl_set_free(pw
->p
[i
].set
);
1562 FN(EL
,free
)(pw
->p
[i
].FIELD
);
1563 isl_set_free(pw
->p
[i
- 1].set
);
1564 pw
->p
[i
- 1].set
= set
;
1565 for (j
= i
+ 1; j
< pw
->n
; ++j
)
1566 pw
->p
[j
- 1] = pw
->p
[j
];
1573 /* Is pw1 obviously equal to pw2?
1574 * That is, do they have obviously identical cells and obviously identical
1575 * elements on each cell?
1577 int FN(PW
,plain_is_equal
)(__isl_keep PW
*pw1
, __isl_keep PW
*pw2
)
1587 if (!isl_space_is_equal(pw1
->dim
, pw2
->dim
))
1590 pw1
= FN(PW
,copy
)(pw1
);
1591 pw2
= FN(PW
,copy
)(pw2
);
1592 pw1
= FN(PW
,normalize
)(pw1
);
1593 pw2
= FN(PW
,normalize
)(pw2
);
1597 equal
= pw1
->n
== pw2
->n
;
1598 for (i
= 0; equal
&& i
< pw1
->n
; ++i
) {
1599 equal
= isl_set_plain_is_equal(pw1
->p
[i
].set
, pw2
->p
[i
].set
);
1604 equal
= FN(EL
,plain_is_equal
)(pw1
->p
[i
].FIELD
, pw2
->p
[i
].FIELD
);