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 static __isl_give PW
*FN(PW
,alloc_
)(__isl_take isl_dim
*dim
, int n
)
12 isl_assert(dim
->ctx
, n
>= 0, goto error
);
13 pw
= isl_alloc(dim
->ctx
, struct PW
,
14 sizeof(struct PW
) + (n
- 1) * sizeof(S(PW
,piece
)));
28 __isl_give PW
*FN(PW
,zero
)(__isl_take isl_dim
*dim
)
30 return FN(PW
,alloc_
)(dim
, 0);
33 __isl_give PW
*FN(PW
,add_piece
)(__isl_take PW
*pw
,
34 __isl_take isl_set
*set
, __isl_take EL
*el
)
36 if (!pw
|| !set
|| !el
)
39 if (isl_set_fast_is_empty(set
) || FN(EL
,IS_ZERO
)(el
)) {
45 isl_assert(set
->ctx
, isl_dim_equal(pw
->dim
, el
->dim
), goto error
);
46 isl_assert(set
->ctx
, pw
->n
< pw
->size
, goto error
);
48 pw
->p
[pw
->n
].set
= set
;
49 pw
->p
[pw
->n
].FIELD
= el
;
60 __isl_give PW
*FN(PW
,alloc
)(__isl_take isl_set
*set
, __isl_take EL
*el
)
67 pw
= FN(PW
,alloc_
)(isl_set_get_dim(set
), 1);
69 return FN(PW
,add_piece
)(pw
, set
, el
);
76 __isl_give PW
*FN(PW
,dup
)(__isl_keep PW
*pw
)
84 dup
= FN(PW
,alloc_
)(isl_dim_copy(pw
->dim
), pw
->n
);
88 for (i
= 0; i
< pw
->n
; ++i
)
89 dup
= FN(PW
,add_piece
)(dup
, isl_set_copy(pw
->p
[i
].set
),
90 FN(EL
,copy
)(pw
->p
[i
].FIELD
));
98 __isl_give PW
*FN(PW
,cow
)(__isl_take PW
*pw
)
106 return FN(PW
,dup
)(pw
);
109 __isl_give PW
*FN(PW
,copy
)(__isl_keep PW
*pw
)
118 void FN(PW
,free
)(__isl_take PW
*pw
)
127 for (i
= 0; i
< pw
->n
; ++i
) {
128 isl_set_free(pw
->p
[i
].set
);
129 FN(EL
,free
)(pw
->p
[i
].FIELD
);
131 isl_dim_free(pw
->dim
);
135 int FN(PW
,is_zero
)(__isl_keep PW
*pw
)
143 __isl_give PW
*FN(PW
,add
)(__isl_take PW
*pw1
, __isl_take PW
*pw2
)
152 isl_assert(pw1
->dim
->ctx
, isl_dim_equal(pw1
->dim
, pw2
->dim
), goto error
);
154 if (FN(PW
,is_zero
)(pw1
)) {
159 if (FN(PW
,is_zero
)(pw2
)) {
164 n
= (pw1
->n
+ 1) * (pw2
->n
+ 1);
165 res
= FN(PW
,alloc_
)(isl_dim_copy(pw1
->dim
), n
);
167 for (i
= 0; i
< pw1
->n
; ++i
) {
168 set
= isl_set_copy(pw1
->p
[i
].set
);
169 for (j
= 0; j
< pw2
->n
; ++j
) {
170 struct isl_set
*common
;
172 set
= isl_set_subtract(set
,
173 isl_set_copy(pw2
->p
[j
].set
));
174 common
= isl_set_intersect(isl_set_copy(pw1
->p
[i
].set
),
175 isl_set_copy(pw2
->p
[j
].set
));
176 if (isl_set_fast_is_empty(common
)) {
177 isl_set_free(common
);
181 sum
= ADD(common
, FN(EL
,copy
)(pw1
->p
[i
].FIELD
),
182 FN(EL
,copy
)(pw2
->p
[j
].FIELD
));
184 res
= FN(PW
,add_piece
)(res
, common
, sum
);
186 res
= FN(PW
,add_piece
)(res
, set
, FN(EL
,copy
)(pw1
->p
[i
].FIELD
));
189 for (j
= 0; j
< pw2
->n
; ++j
) {
190 set
= isl_set_copy(pw2
->p
[j
].set
);
191 for (i
= 0; i
< pw1
->n
; ++i
)
192 set
= isl_set_subtract(set
,
193 isl_set_copy(pw1
->p
[i
].set
));
194 res
= FN(PW
,add_piece
)(res
, set
, FN(EL
,copy
)(pw2
->p
[j
].FIELD
));
207 __isl_give PW
*FN(PW
,add_disjoint
)(__isl_take PW
*pw1
, __isl_take PW
*pw2
)
215 isl_assert(pw1
->dim
->ctx
, isl_dim_equal(pw1
->dim
, pw2
->dim
), goto error
);
217 if (FN(PW
,is_zero
)(pw1
)) {
222 if (FN(PW
,is_zero
)(pw2
)) {
227 res
= FN(PW
,alloc_
)(isl_dim_copy(pw1
->dim
), pw1
->n
+ pw2
->n
);
229 for (i
= 0; i
< pw1
->n
; ++i
)
230 res
= FN(PW
,add_piece
)(res
,
231 isl_set_copy(pw1
->p
[i
].set
),
232 FN(EL
,copy
)(pw1
->p
[i
].FIELD
));
234 for (i
= 0; i
< pw2
->n
; ++i
)
235 res
= FN(PW
,add_piece
)(res
,
236 isl_set_copy(pw2
->p
[i
].set
),
237 FN(EL
,copy
)(pw2
->p
[i
].FIELD
));
249 __isl_give isl_qpolynomial
*FN(PW
,eval
)(__isl_take PW
*pw
,
250 __isl_take isl_point
*pnt
)
258 isl_assert(pnt
->dim
->ctx
, isl_dim_equal(pnt
->dim
, pw
->dim
), goto error
);
260 for (i
= 0; i
< pw
->n
; ++i
) {
261 found
= isl_set_contains_point(pw
->p
[i
].set
, pnt
);
268 qp
= FN(EL
,eval
)(FN(EL
,copy
)(pw
->p
[i
].FIELD
),
269 isl_point_copy(pnt
));
271 qp
= isl_qpolynomial_zero(isl_dim_copy(pw
->dim
));
281 __isl_give isl_set
*FN(PW
,domain
)(__isl_take PW
*pw
)
289 dom
= isl_set_empty(isl_dim_copy(pw
->dim
));
290 for (i
= 0; i
< pw
->n
; ++i
)
291 dom
= isl_set_union_disjoint(dom
, isl_set_copy(pw
->p
[i
].set
));
298 __isl_give PW
*FN(PW
,intersect_domain
)(__isl_take PW
*pw
, __isl_take isl_set
*set
)
314 for (i
= pw
->n
- 1; i
>= 0; --i
) {
315 pw
->p
[i
].set
= isl_set_intersect(pw
->p
[i
].set
, isl_set_copy(set
));
318 if (isl_set_fast_is_empty(pw
->p
[i
].set
)) {
319 isl_set_free(pw
->p
[i
].set
);
320 FN(EL
,free
)(pw
->p
[i
].FIELD
);
322 pw
->p
[i
] = pw
->p
[pw
->n
- 1];
335 __isl_give PW
*FN(PW
,gist
)(__isl_take PW
*pw
, __isl_take isl_set
*context
)
338 isl_basic_set
*hull
= NULL
;
344 isl_set_free(context
);
348 hull
= isl_set_simple_hull(isl_set_copy(context
));
354 for (i
= 0; i
< pw
->n
; ++i
) {
355 pw
->p
[i
].set
= isl_set_gist_basic_set(pw
->p
[i
].set
,
356 isl_basic_set_copy(hull
));
361 isl_basic_set_free(hull
);
362 isl_set_free(context
);
367 isl_basic_set_free(hull
);
368 isl_set_free(context
);
372 __isl_give PW
*FN(PW
,coalesce
)(__isl_take PW
*pw
)
381 for (i
= pw
->n
- 1; i
>= 0; --i
) {
382 for (j
= i
- 1; j
>= 0; --j
) {
383 if (!FN(EL
,is_equal
)(pw
->p
[i
].FIELD
, pw
->p
[j
].FIELD
))
385 pw
->p
[j
].set
= isl_set_union(pw
->p
[j
].set
,
387 FN(EL
,free
)(pw
->p
[i
].FIELD
);
389 pw
->p
[i
] = pw
->p
[pw
->n
- 1];
395 pw
->p
[i
].set
= isl_set_coalesce(pw
->p
[i
].set
);
406 isl_ctx
*FN(PW
,get_ctx
)(__isl_keep PW
*pw
)
408 return pw
? pw
->dim
->ctx
: NULL
;
411 int FN(PW
,involves_dims
)(__isl_keep PW
*pw
, enum isl_dim_type type
,
412 unsigned first
, unsigned n
)
418 if (pw
->n
== 0 || n
== 0)
420 for (i
= 0; i
< pw
->n
; ++i
) {
421 int involves
= FN(EL
,involves_dims
)(pw
->p
[i
].FIELD
,
423 if (involves
< 0 || involves
)
429 __isl_give PW
*FN(PW
,drop_dims
)(__isl_take PW
*pw
,
430 enum isl_dim_type type
, unsigned first
, unsigned n
)
436 if (n
== 0 && !isl_dim_get_tuple_name(pw
->dim
, type
))
442 pw
->dim
= isl_dim_drop(pw
->dim
, type
, first
, n
);
445 for (i
= 0; i
< pw
->n
; ++i
) {
446 pw
->p
[i
].set
= isl_set_drop(pw
->p
[i
].set
, type
, first
, n
);
449 pw
->p
[i
].FIELD
= FN(EL
,drop_dims
)(pw
->p
[i
].FIELD
, type
, first
, n
);
460 __isl_give PW
*FN(PW
,fix_dim
)(__isl_take PW
*pw
,
461 enum isl_dim_type type
, unsigned pos
, isl_int v
)
471 for (i
= 0; i
< pw
->n
; ++i
) {
472 pw
->p
[i
].set
= isl_set_fix(pw
->p
[i
].set
, type
, pos
, v
);
483 unsigned FN(PW
,dim
)(__isl_keep PW
*pw
, enum isl_dim_type type
)
485 return pw
? isl_dim_size(pw
->dim
, type
) : 0;
488 __isl_give PW
*FN(PW
,split_dims
)(__isl_take PW
*pw
,
489 enum isl_dim_type type
, unsigned first
, unsigned n
)
503 for (i
= 0; i
< pw
->n
; ++i
) {
504 pw
->p
[i
].set
= isl_set_split_dims(pw
->p
[i
].set
, type
, first
, n
);
515 /* Compute the maximal value attained by the piecewise quasipolynomial
516 * on its domain or zero if the domain is empty.
517 * In the worst case, the domain is scanned completely,
518 * so the domain is assumed to be bounded.
520 __isl_give isl_qpolynomial
*FN(PW
,opt
)(__isl_take PW
*pw
, int max
)
523 isl_qpolynomial
*opt
;
529 isl_dim
*dim
= isl_dim_copy(pw
->dim
);
531 return isl_qpolynomial_zero(dim
);
534 opt
= FN(EL
,opt_on_domain
)(FN(EL
,copy
)(pw
->p
[0].FIELD
),
535 isl_set_copy(pw
->p
[0].set
), max
);
536 for (i
= 1; i
< pw
->n
; ++i
) {
537 isl_qpolynomial
*opt_i
;
538 opt_i
= FN(EL
,opt_on_domain
)(FN(EL
,copy
)(pw
->p
[i
].FIELD
),
539 isl_set_copy(pw
->p
[i
].set
), max
);
541 opt
= isl_qpolynomial_max_cst(opt
, opt_i
);
543 opt
= isl_qpolynomial_min_cst(opt
, opt_i
);
550 __isl_give isl_qpolynomial
*FN(PW
,max
)(__isl_take PW
*pw
)
552 return FN(PW
,opt
)(pw
, 1);
555 __isl_give isl_qpolynomial
*FN(PW
,min
)(__isl_take PW
*pw
)
557 return FN(PW
,opt
)(pw
, 0);
560 __isl_give isl_dim
*FN(PW
,get_dim
)(__isl_keep PW
*pw
)
562 return pw
? isl_dim_copy(pw
->dim
) : NULL
;
565 __isl_give PW
*FN(PW
,reset_dim
)(__isl_take PW
*pw
, __isl_take isl_dim
*dim
)
573 for (i
= 0; i
< pw
->n
; ++i
) {
574 pw
->p
[i
].set
= isl_set_reset_dim(pw
->p
[i
].set
,
578 pw
->p
[i
].FIELD
= FN(EL
,reset_dim
)(pw
->p
[i
].FIELD
,
583 isl_dim_free(pw
->dim
);
593 int FN(PW
,has_equal_dim
)(__isl_keep PW
*pw1
, __isl_keep PW
*pw2
)
598 return isl_dim_equal(pw1
->dim
, pw2
->dim
);
601 __isl_give PW
*FN(PW
,morph
)(__isl_take PW
*pw
, __isl_take isl_morph
*morph
)
608 isl_assert(pw
->dim
->ctx
, isl_dim_equal(pw
->dim
, morph
->dom
->dim
),
614 isl_dim_free(pw
->dim
);
615 pw
->dim
= isl_dim_copy(morph
->ran
->dim
);
619 for (i
= 0; i
< pw
->n
; ++i
) {
620 pw
->p
[i
].set
= isl_morph_set(isl_morph_copy(morph
), pw
->p
[i
].set
);
623 pw
->p
[i
].FIELD
= FN(EL
,morph
)(pw
->p
[i
].FIELD
,
624 isl_morph_copy(morph
));
629 isl_morph_free(morph
);
634 isl_morph_free(morph
);
638 int FN(PW
,foreach_piece
)(__isl_keep PW
*pw
,
639 int (*fn
)(__isl_take isl_set
*set
, __isl_take EL
*el
, void *user
),
647 for (i
= 0; i
< pw
->n
; ++i
)
648 if (fn(isl_set_copy(pw
->p
[i
].set
),
649 FN(EL
,copy
)(pw
->p
[i
].FIELD
), user
) < 0)
655 static int any_divs(__isl_keep isl_set
*set
)
662 for (i
= 0; i
< set
->n
; ++i
)
663 if (set
->p
[i
]->n_div
> 0)
669 static int foreach_lifted_subset(__isl_take isl_set
*set
, __isl_take EL
*el
,
670 int (*fn
)(__isl_take isl_set
*set
, __isl_take EL
*el
,
671 void *user
), void *user
)
678 for (i
= 0; i
< set
->n
; ++i
) {
682 lift
= isl_set_from_basic_set(isl_basic_set_copy(set
->p
[i
]));
683 lift
= isl_set_lift(lift
);
685 copy
= FN(EL
,copy
)(el
);
686 copy
= FN(EL
,lift
)(copy
, isl_set_get_dim(lift
));
688 if (fn(lift
, copy
, user
) < 0)
702 int FN(PW
,foreach_lifted_piece
)(__isl_keep PW
*pw
,
703 int (*fn
)(__isl_take isl_set
*set
, __isl_take EL
*el
,
704 void *user
), void *user
)
711 for (i
= 0; i
< pw
->n
; ++i
) {
715 set
= isl_set_copy(pw
->p
[i
].set
);
716 el
= FN(EL
,copy
)(pw
->p
[i
].FIELD
);
717 if (!any_divs(set
)) {
718 if (fn(set
, el
, user
) < 0)
722 if (foreach_lifted_subset(set
, el
, fn
, user
) < 0)
729 __isl_give PW
*FN(PW
,move_dims
)(__isl_take PW
*pw
,
730 enum isl_dim_type dst_type
, unsigned dst_pos
,
731 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
739 pw
->dim
= isl_dim_move(pw
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
743 for (i
= 0; i
< pw
->n
; ++i
) {
744 pw
->p
[i
].set
= isl_set_move_dims(pw
->p
[i
].set
,
746 src_type
, src_pos
, n
);
749 pw
->p
[i
].FIELD
= FN(EL
,move_dims
)(pw
->p
[i
].FIELD
,
750 dst_type
, dst_pos
, src_type
, src_pos
, n
);