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
= FN(EL
,ADD
)(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
= 0; i
< pw
->n
; ++i
) {
315 pw
->p
[i
].set
= isl_set_intersect(pw
->p
[i
].set
, isl_set_copy(set
));