2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 #include <isl_val_private.h>
18 #include <isl_pw_macro.h>
21 __isl_give PW
*FN(PW
,alloc_size
)(__isl_take isl_space
*dim
,
22 enum isl_fold type
, int n
)
24 __isl_give PW
*FN(PW
,alloc_size
)(__isl_take isl_space
*dim
, int n
)
32 ctx
= isl_space_get_ctx(dim
);
33 isl_assert(ctx
, n
>= 0, goto error
);
34 pw
= isl_alloc(ctx
, struct PW
,
35 sizeof(struct PW
) + (n
- 1) * sizeof(S(PW
,piece
)));
53 __isl_give PW
*FN(PW
,ZERO
)(__isl_take isl_space
*dim
, enum isl_fold type
)
55 return FN(PW
,alloc_size
)(dim
, type
, 0);
58 __isl_give PW
*FN(PW
,ZERO
)(__isl_take isl_space
*dim
)
60 return FN(PW
,alloc_size
)(dim
, 0);
64 __isl_give PW
*FN(PW
,add_piece
)(__isl_take PW
*pw
,
65 __isl_take isl_set
*set
, __isl_take EL
*el
)
68 isl_space
*el_dim
= NULL
;
70 if (!pw
|| !set
|| !el
)
73 if (isl_set_plain_is_empty(set
) || FN(EL
,EL_IS_ZERO
)(el
)) {
79 ctx
= isl_set_get_ctx(set
);
81 if (pw
->type
!= el
->type
)
82 isl_die(ctx
, isl_error_invalid
, "fold types don't match",
85 el_dim
= FN(EL
,get_space(el
));
86 isl_assert(ctx
, isl_space_is_equal(pw
->dim
, el_dim
), goto error
);
87 isl_assert(ctx
, pw
->n
< pw
->size
, goto error
);
89 pw
->p
[pw
->n
].set
= set
;
90 pw
->p
[pw
->n
].FIELD
= el
;
93 isl_space_free(el_dim
);
96 isl_space_free(el_dim
);
104 __isl_give PW
*FN(PW
,alloc
)(enum isl_fold type
,
105 __isl_take isl_set
*set
, __isl_take EL
*el
)
107 __isl_give PW
*FN(PW
,alloc
)(__isl_take isl_set
*set
, __isl_take EL
*el
)
116 pw
= FN(PW
,alloc_size
)(FN(EL
,get_space
)(el
), type
, 1);
118 pw
= FN(PW
,alloc_size
)(FN(EL
,get_space
)(el
), 1);
121 return FN(PW
,add_piece
)(pw
, set
, el
);
128 __isl_give PW
*FN(PW
,dup
)(__isl_keep PW
*pw
)
137 dup
= FN(PW
,alloc_size
)(isl_space_copy(pw
->dim
), pw
->type
, pw
->n
);
139 dup
= FN(PW
,alloc_size
)(isl_space_copy(pw
->dim
), pw
->n
);
144 for (i
= 0; i
< pw
->n
; ++i
)
145 dup
= FN(PW
,add_piece
)(dup
, isl_set_copy(pw
->p
[i
].set
),
146 FN(EL
,copy
)(pw
->p
[i
].FIELD
));
151 __isl_give PW
*FN(PW
,cow
)(__isl_take PW
*pw
)
159 return FN(PW
,dup
)(pw
);
162 __isl_give PW
*FN(PW
,copy
)(__isl_keep PW
*pw
)
171 __isl_null PW
*FN(PW
,free
)(__isl_take PW
*pw
)
180 for (i
= 0; i
< pw
->n
; ++i
) {
181 isl_set_free(pw
->p
[i
].set
);
182 FN(EL
,free
)(pw
->p
[i
].FIELD
);
184 isl_space_free(pw
->dim
);
190 const char *FN(PW
,get_dim_name
)(__isl_keep PW
*pw
, enum isl_dim_type type
,
193 return pw
? isl_space_get_dim_name(pw
->dim
, type
, pos
) : NULL
;
196 isl_bool
FN(PW
,has_dim_id
)(__isl_keep PW
*pw
, enum isl_dim_type type
,
199 return pw
? isl_space_has_dim_id(pw
->dim
, type
, pos
) : isl_bool_error
;
202 __isl_give isl_id
*FN(PW
,get_dim_id
)(__isl_keep PW
*pw
, enum isl_dim_type type
,
205 return pw
? isl_space_get_dim_id(pw
->dim
, type
, pos
) : NULL
;
208 isl_bool
FN(PW
,has_tuple_name
)(__isl_keep PW
*pw
, enum isl_dim_type type
)
210 return pw
? isl_space_has_tuple_name(pw
->dim
, type
) : isl_bool_error
;
213 const char *FN(PW
,get_tuple_name
)(__isl_keep PW
*pw
, enum isl_dim_type type
)
215 return pw
? isl_space_get_tuple_name(pw
->dim
, type
) : NULL
;
218 isl_bool
FN(PW
,has_tuple_id
)(__isl_keep PW
*pw
, enum isl_dim_type type
)
220 return pw
? isl_space_has_tuple_id(pw
->dim
, type
) : isl_bool_error
;
223 __isl_give isl_id
*FN(PW
,get_tuple_id
)(__isl_keep PW
*pw
, enum isl_dim_type type
)
225 return pw
? isl_space_get_tuple_id(pw
->dim
, type
) : NULL
;
228 isl_bool
FN(PW
,IS_ZERO
)(__isl_keep PW
*pw
)
231 return isl_bool_error
;
237 __isl_give PW
*FN(PW
,realign_domain
)(__isl_take PW
*pw
,
238 __isl_take isl_reordering
*exp
)
246 for (i
= 0; i
< pw
->n
; ++i
) {
247 pw
->p
[i
].set
= isl_set_realign(pw
->p
[i
].set
,
248 isl_reordering_copy(exp
));
251 pw
->p
[i
].FIELD
= FN(EL
,realign_domain
)(pw
->p
[i
].FIELD
,
252 isl_reordering_copy(exp
));
257 pw
= FN(PW
,reset_domain_space
)(pw
, isl_space_copy(exp
->dim
));
259 isl_reordering_free(exp
);
262 isl_reordering_free(exp
);
267 /* Align the parameters of "pw" to those of "model".
269 __isl_give PW
*FN(PW
,align_params
)(__isl_take PW
*pw
, __isl_take isl_space
*model
)
276 ctx
= isl_space_get_ctx(model
);
277 if (!isl_space_has_named_params(model
))
278 isl_die(ctx
, isl_error_invalid
,
279 "model has unnamed parameters", goto error
);
280 if (!isl_space_has_named_params(pw
->dim
))
281 isl_die(ctx
, isl_error_invalid
,
282 "input has unnamed parameters", goto error
);
283 if (!isl_space_match(pw
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
286 model
= isl_space_drop_dims(model
, isl_dim_in
,
287 0, isl_space_dim(model
, isl_dim_in
));
288 model
= isl_space_drop_dims(model
, isl_dim_out
,
289 0, isl_space_dim(model
, isl_dim_out
));
290 exp
= isl_parameter_alignment_reordering(pw
->dim
, model
);
291 exp
= isl_reordering_extend_space(exp
,
292 FN(PW
,get_domain_space
)(pw
));
293 pw
= FN(PW
,realign_domain
)(pw
, exp
);
296 isl_space_free(model
);
299 isl_space_free(model
);
304 static __isl_give PW
*FN(PW
,align_params_pw_pw_and
)(__isl_take PW
*pw1
,
306 __isl_give PW
*(*fn
)(__isl_take PW
*pw1
, __isl_take PW
*pw2
))
312 if (isl_space_match(pw1
->dim
, isl_dim_param
, pw2
->dim
, isl_dim_param
))
314 ctx
= FN(PW
,get_ctx
)(pw1
);
315 if (!isl_space_has_named_params(pw1
->dim
) ||
316 !isl_space_has_named_params(pw2
->dim
))
317 isl_die(ctx
, isl_error_invalid
,
318 "unaligned unnamed parameters", goto error
);
319 pw1
= FN(PW
,align_params
)(pw1
, FN(PW
,get_space
)(pw2
));
320 pw2
= FN(PW
,align_params
)(pw2
, FN(PW
,get_space
)(pw1
));
328 static __isl_give PW
*FN(PW
,align_params_pw_set_and
)(__isl_take PW
*pw
,
329 __isl_take isl_set
*set
,
330 __isl_give PW
*(*fn
)(__isl_take PW
*pw
, __isl_take isl_set
*set
))
337 aligned
= isl_set_space_has_equal_params(set
, pw
->dim
);
342 ctx
= FN(PW
,get_ctx
)(pw
);
343 if (!isl_space_has_named_params(pw
->dim
) ||
344 !isl_space_has_named_params(set
->dim
))
345 isl_die(ctx
, isl_error_invalid
,
346 "unaligned unnamed parameters", goto error
);
347 pw
= FN(PW
,align_params
)(pw
, isl_set_get_space(set
));
348 set
= isl_set_align_params(set
, FN(PW
,get_space
)(pw
));
357 static __isl_give PW
*FN(PW
,union_add_aligned
)(__isl_take PW
*pw1
,
368 ctx
= isl_space_get_ctx(pw1
->dim
);
370 if (pw1
->type
!= pw2
->type
)
371 isl_die(ctx
, isl_error_invalid
,
372 "fold types don't match", goto error
);
374 isl_assert(ctx
, isl_space_is_equal(pw1
->dim
, pw2
->dim
), goto error
);
376 if (FN(PW
,IS_ZERO
)(pw1
)) {
381 if (FN(PW
,IS_ZERO
)(pw2
)) {
386 n
= (pw1
->n
+ 1) * (pw2
->n
+ 1);
388 res
= FN(PW
,alloc_size
)(isl_space_copy(pw1
->dim
), pw1
->type
, n
);
390 res
= FN(PW
,alloc_size
)(isl_space_copy(pw1
->dim
), n
);
393 for (i
= 0; i
< pw1
->n
; ++i
) {
394 set
= isl_set_copy(pw1
->p
[i
].set
);
395 for (j
= 0; j
< pw2
->n
; ++j
) {
396 struct isl_set
*common
;
398 common
= isl_set_intersect(isl_set_copy(pw1
->p
[i
].set
),
399 isl_set_copy(pw2
->p
[j
].set
));
400 if (isl_set_plain_is_empty(common
)) {
401 isl_set_free(common
);
404 set
= isl_set_subtract(set
,
405 isl_set_copy(pw2
->p
[j
].set
));
407 sum
= FN(EL
,add_on_domain
)(common
,
408 FN(EL
,copy
)(pw1
->p
[i
].FIELD
),
409 FN(EL
,copy
)(pw2
->p
[j
].FIELD
));
411 res
= FN(PW
,add_piece
)(res
, common
, sum
);
413 res
= FN(PW
,add_piece
)(res
, set
, FN(EL
,copy
)(pw1
->p
[i
].FIELD
));
416 for (j
= 0; j
< pw2
->n
; ++j
) {
417 set
= isl_set_copy(pw2
->p
[j
].set
);
418 for (i
= 0; i
< pw1
->n
; ++i
)
419 set
= isl_set_subtract(set
,
420 isl_set_copy(pw1
->p
[i
].set
));
421 res
= FN(PW
,add_piece
)(res
, set
, FN(EL
,copy
)(pw2
->p
[j
].FIELD
));
434 /* Private version of "union_add". For isl_pw_qpolynomial and
435 * isl_pw_qpolynomial_fold, we prefer to simply call it "add".
437 static __isl_give PW
*FN(PW
,union_add_
)(__isl_take PW
*pw1
, __isl_take PW
*pw2
)
439 return FN(PW
,align_params_pw_pw_and
)(pw1
, pw2
,
440 &FN(PW
,union_add_aligned
));
443 /* Make sure "pw" has room for at least "n" more pieces.
445 * If there is only one reference to pw, we extend it in place.
446 * Otherwise, we create a new PW and copy the pieces.
448 static __isl_give PW
*FN(PW
,grow
)(__isl_take PW
*pw
, int n
)
456 if (pw
->n
+ n
<= pw
->size
)
458 ctx
= FN(PW
,get_ctx
)(pw
);
461 res
= isl_realloc(ctx
, pw
, struct PW
,
462 sizeof(struct PW
) + (n
- 1) * sizeof(S(PW
,piece
)));
464 return FN(PW
,free
)(pw
);
469 res
= FN(PW
,alloc_size
)(isl_space_copy(pw
->dim
), pw
->type
, n
);
471 res
= FN(PW
,alloc_size
)(isl_space_copy(pw
->dim
), n
);
474 return FN(PW
,free
)(pw
);
475 for (i
= 0; i
< pw
->n
; ++i
)
476 res
= FN(PW
,add_piece
)(res
, isl_set_copy(pw
->p
[i
].set
),
477 FN(EL
,copy
)(pw
->p
[i
].FIELD
));
482 static __isl_give PW
*FN(PW
,add_disjoint_aligned
)(__isl_take PW
*pw1
,
491 if (pw1
->size
< pw1
->n
+ pw2
->n
&& pw1
->n
< pw2
->n
)
492 return FN(PW
,add_disjoint_aligned
)(pw2
, pw1
);
494 ctx
= isl_space_get_ctx(pw1
->dim
);
496 if (pw1
->type
!= pw2
->type
)
497 isl_die(ctx
, isl_error_invalid
,
498 "fold types don't match", goto error
);
500 isl_assert(ctx
, isl_space_is_equal(pw1
->dim
, pw2
->dim
), goto error
);
502 if (FN(PW
,IS_ZERO
)(pw1
)) {
507 if (FN(PW
,IS_ZERO
)(pw2
)) {
512 pw1
= FN(PW
,grow
)(pw1
, pw2
->n
);
516 for (i
= 0; i
< pw2
->n
; ++i
)
517 pw1
= FN(PW
,add_piece
)(pw1
,
518 isl_set_copy(pw2
->p
[i
].set
),
519 FN(EL
,copy
)(pw2
->p
[i
].FIELD
));
530 __isl_give PW
*FN(PW
,add_disjoint
)(__isl_take PW
*pw1
, __isl_take PW
*pw2
)
532 return FN(PW
,align_params_pw_pw_and
)(pw1
, pw2
,
533 &FN(PW
,add_disjoint_aligned
));
536 /* This function is currently only used from isl_aff.c
538 static __isl_give PW
*FN(PW
,on_shared_domain_in
)(__isl_take PW
*pw1
,
539 __isl_take PW
*pw2
, __isl_take isl_space
*space
,
540 __isl_give EL
*(*fn
)(__isl_take EL
*el1
, __isl_take EL
*el2
))
541 __attribute__ ((unused
));
543 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
544 * The result of "fn" (and therefore also of this function) lives in "space".
546 static __isl_give PW
*FN(PW
,on_shared_domain_in
)(__isl_take PW
*pw1
,
547 __isl_take PW
*pw2
, __isl_take isl_space
*space
,
548 __isl_give EL
*(*fn
)(__isl_take EL
*el1
, __isl_take EL
*el2
))
558 res
= FN(PW
,alloc_size
)(isl_space_copy(space
), pw1
->type
, n
);
560 res
= FN(PW
,alloc_size
)(isl_space_copy(space
), n
);
563 for (i
= 0; i
< pw1
->n
; ++i
) {
564 for (j
= 0; j
< pw2
->n
; ++j
) {
569 common
= isl_set_intersect(
570 isl_set_copy(pw1
->p
[i
].set
),
571 isl_set_copy(pw2
->p
[j
].set
));
572 empty
= isl_set_plain_is_empty(common
);
573 if (empty
< 0 || empty
) {
574 isl_set_free(common
);
580 res_ij
= fn(FN(EL
,copy
)(pw1
->p
[i
].FIELD
),
581 FN(EL
,copy
)(pw2
->p
[j
].FIELD
));
582 res_ij
= FN(EL
,gist
)(res_ij
, isl_set_copy(common
));
584 res
= FN(PW
,add_piece
)(res
, common
, res_ij
);
588 isl_space_free(space
);
593 isl_space_free(space
);
600 /* This function is currently only used from isl_aff.c
602 static __isl_give PW
*FN(PW
,on_shared_domain
)(__isl_take PW
*pw1
,
604 __isl_give EL
*(*fn
)(__isl_take EL
*el1
, __isl_take EL
*el2
))
605 __attribute__ ((unused
));
607 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
608 * The result of "fn" is assumed to live in the same space as "pw1" and "pw2".
610 static __isl_give PW
*FN(PW
,on_shared_domain
)(__isl_take PW
*pw1
,
612 __isl_give EL
*(*fn
)(__isl_take EL
*el1
, __isl_take EL
*el2
))
619 space
= isl_space_copy(pw1
->dim
);
620 return FN(PW
,on_shared_domain_in
)(pw1
, pw2
, space
, fn
);
628 __isl_give PW
*FN(PW
,neg
)(__isl_take PW
*pw
)
635 if (FN(PW
,IS_ZERO
)(pw
))
642 for (i
= 0; i
< pw
->n
; ++i
) {
643 pw
->p
[i
].FIELD
= FN(EL
,neg
)(pw
->p
[i
].FIELD
);
645 return FN(PW
,free
)(pw
);
653 __isl_give PW
*FN(PW
,sub
)(__isl_take PW
*pw1
, __isl_take PW
*pw2
)
655 return FN(PW
,add
)(pw1
, FN(PW
,neg
)(pw2
));
660 __isl_give isl_val
*FN(PW
,eval
)(__isl_take PW
*pw
, __isl_take isl_point
*pnt
)
665 isl_space
*pnt_dim
= NULL
;
670 ctx
= isl_point_get_ctx(pnt
);
671 pnt_dim
= isl_point_get_space(pnt
);
672 isl_assert(ctx
, isl_space_is_domain_internal(pnt_dim
, pw
->dim
),
675 for (i
= 0; i
< pw
->n
; ++i
) {
676 found
= isl_set_contains_point(pw
->p
[i
].set
, pnt
);
683 v
= FN(EL
,eval
)(FN(EL
,copy
)(pw
->p
[i
].FIELD
),
684 isl_point_copy(pnt
));
686 v
= isl_val_zero(ctx
);
688 isl_space_free(pnt_dim
);
693 isl_space_free(pnt_dim
);
699 /* Return the parameter domain of "pw".
701 __isl_give isl_set
*FN(PW
,params
)(__isl_take PW
*pw
)
703 return isl_set_params(FN(PW
,domain
)(pw
));
706 __isl_give isl_set
*FN(PW
,domain
)(__isl_take PW
*pw
)
714 dom
= isl_set_empty(FN(PW
,get_domain_space
)(pw
));
715 for (i
= 0; i
< pw
->n
; ++i
)
716 dom
= isl_set_union_disjoint(dom
, isl_set_copy(pw
->p
[i
].set
));
723 /* Exploit the equalities in the domain of piece "i" of "pw"
724 * to simplify the associated function.
725 * If the domain of piece "i" is empty, then remove it entirely,
726 * replacing it with the final piece.
728 static int FN(PW
,exploit_equalities_and_remove_if_empty
)(__isl_keep PW
*pw
,
732 int empty
= isl_set_plain_is_empty(pw
->p
[i
].set
);
737 isl_set_free(pw
->p
[i
].set
);
738 FN(EL
,free
)(pw
->p
[i
].FIELD
);
740 pw
->p
[i
] = pw
->p
[pw
->n
- 1];
746 aff
= isl_set_affine_hull(isl_set_copy(pw
->p
[i
].set
));
747 pw
->p
[i
].FIELD
= FN(EL
,substitute_equalities
)(pw
->p
[i
].FIELD
, aff
);
754 /* Convert a piecewise expression defined over a parameter domain
755 * into one that is defined over a zero-dimensional set.
757 __isl_give PW
*FN(PW
,from_range
)(__isl_take PW
*pw
)
763 if (!isl_space_is_set(pw
->dim
))
764 isl_die(FN(PW
,get_ctx
)(pw
), isl_error_invalid
,
765 "not living in a set space", return FN(PW
,free
)(pw
));
767 space
= FN(PW
,get_space
)(pw
);
768 space
= isl_space_from_range(space
);
769 pw
= FN(PW
,reset_space
)(pw
, space
);
774 /* Fix the value of the given parameter or domain dimension of "pw"
775 * to be equal to "value".
777 __isl_give PW
*FN(PW
,fix_si
)(__isl_take PW
*pw
, enum isl_dim_type type
,
778 unsigned pos
, int value
)
785 if (type
== isl_dim_out
)
786 isl_die(FN(PW
,get_ctx
)(pw
), isl_error_invalid
,
787 "cannot fix output dimension", return FN(PW
,free
)(pw
));
792 if (type
== isl_dim_in
)
797 return FN(PW
,free
)(pw
);
799 for (i
= pw
->n
- 1; i
>= 0; --i
) {
800 pw
->p
[i
].set
= isl_set_fix_si(pw
->p
[i
].set
, type
, pos
, value
);
801 if (FN(PW
,exploit_equalities_and_remove_if_empty
)(pw
, i
) < 0)
802 return FN(PW
,free
)(pw
);
808 /* Restrict the domain of "pw" by combining each cell
809 * with "set" through a call to "fn", where "fn" may be
810 * isl_set_intersect, isl_set_intersect_params or isl_set_subtract.
812 static __isl_give PW
*FN(PW
,restrict_domain_aligned
)(__isl_take PW
*pw
,
813 __isl_take isl_set
*set
,
814 __isl_give isl_set
*(*fn
)(__isl_take isl_set
*set1
,
815 __isl_take isl_set
*set2
))
831 for (i
= pw
->n
- 1; i
>= 0; --i
) {
832 pw
->p
[i
].set
= fn(pw
->p
[i
].set
, isl_set_copy(set
));
833 if (FN(PW
,exploit_equalities_and_remove_if_empty
)(pw
, i
) < 0)
845 static __isl_give PW
*FN(PW
,intersect_domain_aligned
)(__isl_take PW
*pw
,
846 __isl_take isl_set
*set
)
848 return FN(PW
,restrict_domain_aligned
)(pw
, set
, &isl_set_intersect
);
851 __isl_give PW
*FN(PW
,intersect_domain
)(__isl_take PW
*pw
,
852 __isl_take isl_set
*context
)
854 return FN(PW
,align_params_pw_set_and
)(pw
, context
,
855 &FN(PW
,intersect_domain_aligned
));
858 static __isl_give PW
*FN(PW
,intersect_params_aligned
)(__isl_take PW
*pw
,
859 __isl_take isl_set
*set
)
861 return FN(PW
,restrict_domain_aligned
)(pw
, set
,
862 &isl_set_intersect_params
);
865 /* Intersect the domain of "pw" with the parameter domain "context".
867 __isl_give PW
*FN(PW
,intersect_params
)(__isl_take PW
*pw
,
868 __isl_take isl_set
*context
)
870 return FN(PW
,align_params_pw_set_and
)(pw
, context
,
871 &FN(PW
,intersect_params_aligned
));
874 /* Subtract "domain' from the domain of "pw", assuming their
875 * parameters have been aligned.
877 static __isl_give PW
*FN(PW
,subtract_domain_aligned
)(__isl_take PW
*pw
,
878 __isl_take isl_set
*domain
)
880 return FN(PW
,restrict_domain_aligned
)(pw
, domain
, &isl_set_subtract
);
883 /* Subtract "domain' from the domain of "pw".
885 __isl_give PW
*FN(PW
,subtract_domain
)(__isl_take PW
*pw
,
886 __isl_take isl_set
*domain
)
888 return FN(PW
,align_params_pw_set_and
)(pw
, domain
,
889 &FN(PW
,subtract_domain_aligned
));
892 /* Compute the gist of "pw" with respect to the domain constraints
893 * of "context" for the case where the domain of the last element
894 * of "pw" is equal to "context".
895 * Call "fn_el" to compute the gist of this element, replace
896 * its domain by the universe and drop all other elements
897 * as their domains are necessarily disjoint from "context".
899 static __isl_give PW
*FN(PW
,gist_last
)(__isl_take PW
*pw
,
900 __isl_take isl_set
*context
,
901 __isl_give EL
*(*fn_el
)(__isl_take EL
*el
, __isl_take isl_set
*set
))
906 for (i
= 0; i
< pw
->n
- 1; ++i
) {
907 isl_set_free(pw
->p
[i
].set
);
908 FN(EL
,free
)(pw
->p
[i
].FIELD
);
910 pw
->p
[0].FIELD
= pw
->p
[pw
->n
- 1].FIELD
;
911 pw
->p
[0].set
= pw
->p
[pw
->n
- 1].set
;
914 space
= isl_set_get_space(context
);
915 pw
->p
[0].FIELD
= fn_el(pw
->p
[0].FIELD
, context
);
916 context
= isl_set_universe(space
);
917 isl_set_free(pw
->p
[0].set
);
918 pw
->p
[0].set
= context
;
920 if (!pw
->p
[0].FIELD
|| !pw
->p
[0].set
)
921 return FN(PW
,free
)(pw
);
926 /* Compute the gist of "pw" with respect to the domain constraints
927 * of "context". Call "fn_el" to compute the gist of the elements
928 * and "fn_dom" to compute the gist of the domains.
930 * If the piecewise expression is empty or the context is the universe,
931 * then nothing can be simplified.
933 static __isl_give PW
*FN(PW
,gist_aligned
)(__isl_take PW
*pw
,
934 __isl_take isl_set
*context
,
935 __isl_give EL
*(*fn_el
)(__isl_take EL
*el
,
936 __isl_take isl_set
*set
),
937 __isl_give isl_set
*(*fn_dom
)(__isl_take isl_set
*set
,
938 __isl_take isl_basic_set
*bset
))
943 isl_basic_set
*hull
= NULL
;
949 isl_set_free(context
);
953 is_universe
= isl_set_plain_is_universe(context
);
957 isl_set_free(context
);
961 aligned
= isl_set_space_has_equal_params(context
, pw
->dim
);
965 pw
= FN(PW
,align_params
)(pw
, isl_set_get_space(context
));
966 context
= isl_set_align_params(context
, FN(PW
,get_space
)(pw
));
976 equal
= isl_set_plain_is_equal(pw
->p
[0].set
, context
);
980 return FN(PW
,gist_last
)(pw
, context
, fn_el
);
983 context
= isl_set_compute_divs(context
);
984 hull
= isl_set_simple_hull(isl_set_copy(context
));
986 for (i
= pw
->n
- 1; i
>= 0; --i
) {
990 if (i
== pw
->n
- 1) {
992 equal
= isl_set_plain_is_equal(pw
->p
[i
].set
, context
);
996 isl_basic_set_free(hull
);
997 return FN(PW
,gist_last
)(pw
, context
, fn_el
);
1000 set_i
= isl_set_intersect(isl_set_copy(pw
->p
[i
].set
),
1001 isl_set_copy(context
));
1002 empty
= isl_set_plain_is_empty(set_i
);
1003 pw
->p
[i
].FIELD
= fn_el(pw
->p
[i
].FIELD
, set_i
);
1004 pw
->p
[i
].set
= fn_dom(pw
->p
[i
].set
, isl_basic_set_copy(hull
));
1005 if (empty
< 0 || !pw
->p
[i
].FIELD
|| !pw
->p
[i
].set
)
1008 isl_set_free(pw
->p
[i
].set
);
1009 FN(EL
,free
)(pw
->p
[i
].FIELD
);
1011 pw
->p
[i
] = pw
->p
[pw
->n
- 1];
1016 isl_basic_set_free(hull
);
1017 isl_set_free(context
);
1022 isl_basic_set_free(hull
);
1023 isl_set_free(context
);
1027 static __isl_give PW
*FN(PW
,gist_domain_aligned
)(__isl_take PW
*pw
,
1028 __isl_take isl_set
*set
)
1030 return FN(PW
,gist_aligned
)(pw
, set
, &FN(EL
,gist
),
1031 &isl_set_gist_basic_set
);
1034 __isl_give PW
*FN(PW
,gist
)(__isl_take PW
*pw
, __isl_take isl_set
*context
)
1036 return FN(PW
,align_params_pw_set_and
)(pw
, context
,
1037 &FN(PW
,gist_domain_aligned
));
1040 static __isl_give PW
*FN(PW
,gist_params_aligned
)(__isl_take PW
*pw
,
1041 __isl_take isl_set
*set
)
1043 return FN(PW
,gist_aligned
)(pw
, set
, &FN(EL
,gist_params
),
1044 &isl_set_gist_params_basic_set
);
1047 __isl_give PW
*FN(PW
,gist_params
)(__isl_take PW
*pw
,
1048 __isl_take isl_set
*context
)
1050 return FN(PW
,align_params_pw_set_and
)(pw
, context
,
1051 &FN(PW
,gist_params_aligned
));
1054 /* Return -1 if the piece "p1" should be sorted before "p2"
1055 * and 1 if it should be sorted after "p2".
1056 * Return 0 if they do not need to be sorted in a specific order.
1058 * The two pieces are compared on the basis of their function value expressions.
1060 static int FN(PW
,sort_field_cmp
)(const void *p1
, const void *p2
, void *arg
)
1062 struct FN(PW
,piece
) const *pc1
= p1
;
1063 struct FN(PW
,piece
) const *pc2
= p2
;
1065 return FN(EL
,plain_cmp
)(pc1
->FIELD
, pc2
->FIELD
);
1068 /* Sort the pieces of "pw" according to their function value
1069 * expressions and then combine pairs of adjacent pieces with
1070 * the same such expression.
1072 * The sorting is performed in place because it does not
1073 * change the meaning of "pw", but care needs to be
1074 * taken not to change any possible other copies of "pw"
1075 * in case anything goes wrong.
1077 __isl_give PW
*FN(PW
,sort
)(__isl_take PW
*pw
)
1086 if (isl_sort(pw
->p
, pw
->n
, sizeof(pw
->p
[0]),
1087 &FN(PW
,sort_field_cmp
), NULL
) < 0)
1088 return FN(PW
,free
)(pw
);
1089 for (i
= pw
->n
- 1; i
>= 1; --i
) {
1090 if (!FN(EL
,plain_is_equal
)(pw
->p
[i
- 1].FIELD
, pw
->p
[i
].FIELD
))
1092 set
= isl_set_union(isl_set_copy(pw
->p
[i
- 1].set
),
1093 isl_set_copy(pw
->p
[i
].set
));
1095 return FN(PW
,free
)(pw
);
1096 isl_set_free(pw
->p
[i
].set
);
1097 FN(EL
,free
)(pw
->p
[i
].FIELD
);
1098 isl_set_free(pw
->p
[i
- 1].set
);
1099 pw
->p
[i
- 1].set
= set
;
1100 for (j
= i
+ 1; j
< pw
->n
; ++j
)
1101 pw
->p
[j
- 1] = pw
->p
[j
];
1108 /* Coalesce the domains of "pw".
1110 * Prior to the actual coalescing, first sort the pieces such that
1111 * pieces with the same function value expression are combined
1112 * into a single piece, the combined domain of which can then
1115 __isl_give PW
*FN(PW
,coalesce
)(__isl_take PW
*pw
)
1119 pw
= FN(PW
,sort
)(pw
);
1123 for (i
= 0; i
< pw
->n
; ++i
) {
1124 pw
->p
[i
].set
= isl_set_coalesce(pw
->p
[i
].set
);
1135 isl_ctx
*FN(PW
,get_ctx
)(__isl_keep PW
*pw
)
1137 return pw
? isl_space_get_ctx(pw
->dim
) : NULL
;
1140 #ifndef NO_INVOLVES_DIMS
1141 isl_bool
FN(PW
,involves_dims
)(__isl_keep PW
*pw
, enum isl_dim_type type
,
1142 unsigned first
, unsigned n
)
1145 enum isl_dim_type set_type
;
1148 return isl_bool_error
;
1149 if (pw
->n
== 0 || n
== 0)
1150 return isl_bool_false
;
1152 set_type
= type
== isl_dim_in
? isl_dim_set
: type
;
1154 for (i
= 0; i
< pw
->n
; ++i
) {
1155 isl_bool involves
= FN(EL
,involves_dims
)(pw
->p
[i
].FIELD
,
1157 if (involves
< 0 || involves
)
1159 involves
= isl_set_involves_dims(pw
->p
[i
].set
,
1160 set_type
, first
, n
);
1161 if (involves
< 0 || involves
)
1164 return isl_bool_false
;
1168 __isl_give PW
*FN(PW
,set_dim_name
)(__isl_take PW
*pw
,
1169 enum isl_dim_type type
, unsigned pos
, const char *s
)
1172 enum isl_dim_type set_type
;
1174 pw
= FN(PW
,cow
)(pw
);
1178 set_type
= type
== isl_dim_in
? isl_dim_set
: type
;
1180 pw
->dim
= isl_space_set_dim_name(pw
->dim
, type
, pos
, s
);
1184 for (i
= 0; i
< pw
->n
; ++i
) {
1185 pw
->p
[i
].set
= isl_set_set_dim_name(pw
->p
[i
].set
,
1189 pw
->p
[i
].FIELD
= FN(EL
,set_dim_name
)(pw
->p
[i
].FIELD
, type
, pos
, s
);
1190 if (!pw
->p
[i
].FIELD
)
1200 #ifndef NO_DROP_DIMS
1201 __isl_give PW
*FN(PW
,drop_dims
)(__isl_take PW
*pw
,
1202 enum isl_dim_type type
, unsigned first
, unsigned n
)
1205 enum isl_dim_type set_type
;
1209 if (n
== 0 && !isl_space_get_tuple_name(pw
->dim
, type
))
1212 set_type
= type
== isl_dim_in
? isl_dim_set
: type
;
1214 pw
= FN(PW
,cow
)(pw
);
1217 pw
->dim
= isl_space_drop_dims(pw
->dim
, type
, first
, n
);
1220 for (i
= 0; i
< pw
->n
; ++i
) {
1221 pw
->p
[i
].FIELD
= FN(EL
,drop_dims
)(pw
->p
[i
].FIELD
, type
, first
, n
);
1222 if (!pw
->p
[i
].FIELD
)
1224 if (type
== isl_dim_out
)
1226 pw
->p
[i
].set
= isl_set_drop(pw
->p
[i
].set
, set_type
, first
, n
);
1237 /* This function is very similar to drop_dims.
1238 * The only difference is that the cells may still involve
1239 * the specified dimensions. They are removed using
1240 * isl_set_project_out instead of isl_set_drop.
1242 __isl_give PW
*FN(PW
,project_out
)(__isl_take PW
*pw
,
1243 enum isl_dim_type type
, unsigned first
, unsigned n
)
1246 enum isl_dim_type set_type
;
1250 if (n
== 0 && !isl_space_get_tuple_name(pw
->dim
, type
))
1253 set_type
= type
== isl_dim_in
? isl_dim_set
: type
;
1255 pw
= FN(PW
,cow
)(pw
);
1258 pw
->dim
= isl_space_drop_dims(pw
->dim
, type
, first
, n
);
1261 for (i
= 0; i
< pw
->n
; ++i
) {
1262 pw
->p
[i
].set
= isl_set_project_out(pw
->p
[i
].set
,
1263 set_type
, first
, n
);
1266 pw
->p
[i
].FIELD
= FN(EL
,drop_dims
)(pw
->p
[i
].FIELD
, type
, first
, n
);
1267 if (!pw
->p
[i
].FIELD
)
1277 /* Project the domain of pw onto its parameter space.
1279 __isl_give PW
*FN(PW
,project_domain_on_params
)(__isl_take PW
*pw
)
1284 n
= FN(PW
,dim
)(pw
, isl_dim_in
);
1285 pw
= FN(PW
,project_out
)(pw
, isl_dim_in
, 0, n
);
1286 space
= FN(PW
,get_domain_space
)(pw
);
1287 space
= isl_space_params(space
);
1288 pw
= FN(PW
,reset_domain_space
)(pw
, space
);
1293 #ifndef NO_INSERT_DIMS
1294 __isl_give PW
*FN(PW
,insert_dims
)(__isl_take PW
*pw
, enum isl_dim_type type
,
1295 unsigned first
, unsigned n
)
1298 enum isl_dim_type set_type
;
1302 if (n
== 0 && !isl_space_is_named_or_nested(pw
->dim
, type
))
1305 set_type
= type
== isl_dim_in
? isl_dim_set
: type
;
1307 pw
= FN(PW
,cow
)(pw
);
1311 pw
->dim
= isl_space_insert_dims(pw
->dim
, type
, first
, n
);
1315 for (i
= 0; i
< pw
->n
; ++i
) {
1316 pw
->p
[i
].set
= isl_set_insert_dims(pw
->p
[i
].set
,
1317 set_type
, first
, n
);
1320 pw
->p
[i
].FIELD
= FN(EL
,insert_dims
)(pw
->p
[i
].FIELD
,
1322 if (!pw
->p
[i
].FIELD
)
1333 __isl_give PW
*FN(PW
,fix_dim
)(__isl_take PW
*pw
,
1334 enum isl_dim_type type
, unsigned pos
, isl_int v
)
1341 if (type
== isl_dim_in
)
1344 pw
= FN(PW
,cow
)(pw
);
1347 for (i
= 0; i
< pw
->n
; ++i
) {
1348 pw
->p
[i
].set
= isl_set_fix(pw
->p
[i
].set
, type
, pos
, v
);
1349 if (FN(PW
,exploit_equalities_and_remove_if_empty
)(pw
, i
) < 0)
1350 return FN(PW
,free
)(pw
);
1356 /* Fix the value of the variable at position "pos" of type "type" of "pw"
1357 * to be equal to "v".
1359 __isl_give PW
*FN(PW
,fix_val
)(__isl_take PW
*pw
,
1360 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
1363 return FN(PW
,free
)(pw
);
1364 if (!isl_val_is_int(v
))
1365 isl_die(FN(PW
,get_ctx
)(pw
), isl_error_invalid
,
1366 "expecting integer value", goto error
);
1368 pw
= FN(PW
,fix_dim
)(pw
, type
, pos
, v
->n
);
1374 return FN(PW
,free
)(pw
);
1377 unsigned FN(PW
,dim
)(__isl_keep PW
*pw
, enum isl_dim_type type
)
1379 return pw
? isl_space_dim(pw
->dim
, type
) : 0;
1382 __isl_give PW
*FN(PW
,split_dims
)(__isl_take PW
*pw
,
1383 enum isl_dim_type type
, unsigned first
, unsigned n
)
1392 if (type
== isl_dim_in
)
1395 pw
= FN(PW
,cow
)(pw
);
1400 for (i
= 0; i
< pw
->n
; ++i
) {
1401 pw
->p
[i
].set
= isl_set_split_dims(pw
->p
[i
].set
, type
, first
, n
);
1413 /* Compute the maximal value attained by the piecewise quasipolynomial
1414 * on its domain or zero if the domain is empty.
1415 * In the worst case, the domain is scanned completely,
1416 * so the domain is assumed to be bounded.
1418 __isl_give isl_val
*FN(PW
,opt
)(__isl_take PW
*pw
, int max
)
1427 opt
= isl_val_zero(FN(PW
,get_ctx
)(pw
));
1432 opt
= FN(EL
,opt_on_domain
)(FN(EL
,copy
)(pw
->p
[0].FIELD
),
1433 isl_set_copy(pw
->p
[0].set
), max
);
1434 for (i
= 1; i
< pw
->n
; ++i
) {
1436 opt_i
= FN(EL
,opt_on_domain
)(FN(EL
,copy
)(pw
->p
[i
].FIELD
),
1437 isl_set_copy(pw
->p
[i
].set
), max
);
1439 opt
= isl_val_max(opt
, opt_i
);
1441 opt
= isl_val_min(opt
, opt_i
);
1448 __isl_give isl_val
*FN(PW
,max
)(__isl_take PW
*pw
)
1450 return FN(PW
,opt
)(pw
, 1);
1453 __isl_give isl_val
*FN(PW
,min
)(__isl_take PW
*pw
)
1455 return FN(PW
,opt
)(pw
, 0);
1459 __isl_give isl_space
*FN(PW
,get_space
)(__isl_keep PW
*pw
)
1461 return pw
? isl_space_copy(pw
->dim
) : NULL
;
1464 __isl_give isl_space
*FN(PW
,get_domain_space
)(__isl_keep PW
*pw
)
1466 return pw
? isl_space_domain(isl_space_copy(pw
->dim
)) : NULL
;
1469 /* Return the position of the dimension of the given type and name
1471 * Return -1 if no such dimension can be found.
1473 int FN(PW
,find_dim_by_name
)(__isl_keep PW
*pw
,
1474 enum isl_dim_type type
, const char *name
)
1478 return isl_space_find_dim_by_name(pw
->dim
, type
, name
);
1481 #ifndef NO_RESET_DIM
1482 /* Reset the space of "pw". Since we don't know if the elements
1483 * represent the spaces themselves or their domains, we pass along
1484 * both when we call their reset_space_and_domain.
1486 static __isl_give PW
*FN(PW
,reset_space_and_domain
)(__isl_take PW
*pw
,
1487 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
1491 pw
= FN(PW
,cow
)(pw
);
1492 if (!pw
|| !space
|| !domain
)
1495 for (i
= 0; i
< pw
->n
; ++i
) {
1496 pw
->p
[i
].set
= isl_set_reset_space(pw
->p
[i
].set
,
1497 isl_space_copy(domain
));
1500 pw
->p
[i
].FIELD
= FN(EL
,reset_space_and_domain
)(pw
->p
[i
].FIELD
,
1501 isl_space_copy(space
), isl_space_copy(domain
));
1502 if (!pw
->p
[i
].FIELD
)
1506 isl_space_free(domain
);
1508 isl_space_free(pw
->dim
);
1513 isl_space_free(domain
);
1514 isl_space_free(space
);
1519 __isl_give PW
*FN(PW
,reset_domain_space
)(__isl_take PW
*pw
,
1520 __isl_take isl_space
*domain
)
1524 space
= isl_space_extend_domain_with_range(isl_space_copy(domain
),
1525 FN(PW
,get_space
)(pw
));
1526 return FN(PW
,reset_space_and_domain
)(pw
, space
, domain
);
1529 __isl_give PW
*FN(PW
,reset_space
)(__isl_take PW
*pw
, __isl_take isl_space
*dim
)
1533 domain
= isl_space_domain(isl_space_copy(dim
));
1534 return FN(PW
,reset_space_and_domain
)(pw
, dim
, domain
);
1537 __isl_give PW
*FN(PW
,set_tuple_id
)(__isl_take PW
*pw
, enum isl_dim_type type
,
1538 __isl_take isl_id
*id
)
1542 pw
= FN(PW
,cow
)(pw
);
1546 space
= FN(PW
,get_space
)(pw
);
1547 space
= isl_space_set_tuple_id(space
, type
, id
);
1549 return FN(PW
,reset_space
)(pw
, space
);
1552 return FN(PW
,free
)(pw
);
1555 /* Drop the id on the specified tuple.
1557 __isl_give PW
*FN(PW
,reset_tuple_id
)(__isl_take PW
*pw
, enum isl_dim_type type
)
1563 if (!FN(PW
,has_tuple_id
)(pw
, type
))
1566 pw
= FN(PW
,cow
)(pw
);
1570 space
= FN(PW
,get_space
)(pw
);
1571 space
= isl_space_reset_tuple_id(space
, type
);
1573 return FN(PW
,reset_space
)(pw
, space
);
1576 __isl_give PW
*FN(PW
,set_dim_id
)(__isl_take PW
*pw
,
1577 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1579 pw
= FN(PW
,cow
)(pw
);
1582 pw
->dim
= isl_space_set_dim_id(pw
->dim
, type
, pos
, id
);
1583 return FN(PW
,reset_space
)(pw
, isl_space_copy(pw
->dim
));
1586 return FN(PW
,free
)(pw
);
1590 /* Reset the user pointer on all identifiers of parameters and tuples
1591 * of the space of "pw".
1593 __isl_give PW
*FN(PW
,reset_user
)(__isl_take PW
*pw
)
1597 space
= FN(PW
,get_space
)(pw
);
1598 space
= isl_space_reset_user(space
);
1600 return FN(PW
,reset_space
)(pw
, space
);
1603 isl_bool
FN(PW
,has_equal_space
)(__isl_keep PW
*pw1
, __isl_keep PW
*pw2
)
1606 return isl_bool_error
;
1608 return isl_space_is_equal(pw1
->dim
, pw2
->dim
);
1612 __isl_give PW
*FN(PW
,morph_domain
)(__isl_take PW
*pw
,
1613 __isl_take isl_morph
*morph
)
1621 ctx
= isl_space_get_ctx(pw
->dim
);
1622 isl_assert(ctx
, isl_space_is_domain_internal(morph
->dom
->dim
, pw
->dim
),
1625 pw
= FN(PW
,cow
)(pw
);
1628 pw
->dim
= isl_space_extend_domain_with_range(
1629 isl_space_copy(morph
->ran
->dim
), pw
->dim
);
1633 for (i
= 0; i
< pw
->n
; ++i
) {
1634 pw
->p
[i
].set
= isl_morph_set(isl_morph_copy(morph
), pw
->p
[i
].set
);
1637 pw
->p
[i
].FIELD
= FN(EL
,morph_domain
)(pw
->p
[i
].FIELD
,
1638 isl_morph_copy(morph
));
1639 if (!pw
->p
[i
].FIELD
)
1643 isl_morph_free(morph
);
1648 isl_morph_free(morph
);
1653 int FN(PW
,n_piece
)(__isl_keep PW
*pw
)
1655 return pw
? pw
->n
: 0;
1658 isl_stat
FN(PW
,foreach_piece
)(__isl_keep PW
*pw
,
1659 isl_stat (*fn
)(__isl_take isl_set
*set
, __isl_take EL
*el
, void *user
),
1665 return isl_stat_error
;
1667 for (i
= 0; i
< pw
->n
; ++i
)
1668 if (fn(isl_set_copy(pw
->p
[i
].set
),
1669 FN(EL
,copy
)(pw
->p
[i
].FIELD
), user
) < 0)
1670 return isl_stat_error
;
1676 static isl_bool
any_divs(__isl_keep isl_set
*set
)
1681 return isl_bool_error
;
1683 for (i
= 0; i
< set
->n
; ++i
)
1684 if (set
->p
[i
]->n_div
> 0)
1685 return isl_bool_true
;
1687 return isl_bool_false
;
1690 static isl_stat
foreach_lifted_subset(__isl_take isl_set
*set
,
1692 isl_stat (*fn
)(__isl_take isl_set
*set
, __isl_take EL
*el
,
1693 void *user
), void *user
)
1700 for (i
= 0; i
< set
->n
; ++i
) {
1704 lift
= isl_set_from_basic_set(isl_basic_set_copy(set
->p
[i
]));
1705 lift
= isl_set_lift(lift
);
1707 copy
= FN(EL
,copy
)(el
);
1708 copy
= FN(EL
,lift
)(copy
, isl_set_get_space(lift
));
1710 if (fn(lift
, copy
, user
) < 0)
1721 return isl_stat_error
;
1724 isl_stat
FN(PW
,foreach_lifted_piece
)(__isl_keep PW
*pw
,
1725 isl_stat (*fn
)(__isl_take isl_set
*set
, __isl_take EL
*el
,
1726 void *user
), void *user
)
1731 return isl_stat_error
;
1733 for (i
= 0; i
< pw
->n
; ++i
) {
1738 any
= any_divs(pw
->p
[i
].set
);
1740 return isl_stat_error
;
1741 set
= isl_set_copy(pw
->p
[i
].set
);
1742 el
= FN(EL
,copy
)(pw
->p
[i
].FIELD
);
1744 if (fn(set
, el
, user
) < 0)
1745 return isl_stat_error
;
1748 if (foreach_lifted_subset(set
, el
, fn
, user
) < 0)
1749 return isl_stat_error
;
1756 #ifndef NO_MOVE_DIMS
1757 __isl_give PW
*FN(PW
,move_dims
)(__isl_take PW
*pw
,
1758 enum isl_dim_type dst_type
, unsigned dst_pos
,
1759 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1763 pw
= FN(PW
,cow
)(pw
);
1767 pw
->dim
= isl_space_move_dims(pw
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
1771 for (i
= 0; i
< pw
->n
; ++i
) {
1772 pw
->p
[i
].FIELD
= FN(EL
,move_dims
)(pw
->p
[i
].FIELD
,
1773 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1774 if (!pw
->p
[i
].FIELD
)
1778 if (dst_type
== isl_dim_in
)
1779 dst_type
= isl_dim_set
;
1780 if (src_type
== isl_dim_in
)
1781 src_type
= isl_dim_set
;
1783 for (i
= 0; i
< pw
->n
; ++i
) {
1784 pw
->p
[i
].set
= isl_set_move_dims(pw
->p
[i
].set
,
1786 src_type
, src_pos
, n
);
1798 __isl_give PW
*FN(PW
,mul_isl_int
)(__isl_take PW
*pw
, isl_int v
)
1802 if (isl_int_is_one(v
))
1804 if (pw
&& DEFAULT_IS_ZERO
&& isl_int_is_zero(v
)) {
1806 isl_space
*dim
= FN(PW
,get_space
)(pw
);
1808 zero
= FN(PW
,ZERO
)(dim
, pw
->type
);
1810 zero
= FN(PW
,ZERO
)(dim
);
1815 pw
= FN(PW
,cow
)(pw
);
1822 if (isl_int_is_neg(v
))
1823 pw
->type
= isl_fold_type_negate(pw
->type
);
1825 for (i
= 0; i
< pw
->n
; ++i
) {
1826 pw
->p
[i
].FIELD
= FN(EL
,scale
)(pw
->p
[i
].FIELD
, v
);
1827 if (!pw
->p
[i
].FIELD
)
1837 /* Multiply the pieces of "pw" by "v" and return the result.
1839 __isl_give PW
*FN(PW
,scale_val
)(__isl_take PW
*pw
, __isl_take isl_val
*v
)
1846 if (isl_val_is_one(v
)) {
1850 if (pw
&& DEFAULT_IS_ZERO
&& isl_val_is_zero(v
)) {
1852 isl_space
*space
= FN(PW
,get_space
)(pw
);
1854 zero
= FN(PW
,ZERO
)(space
, pw
->type
);
1856 zero
= FN(PW
,ZERO
)(space
);
1866 pw
= FN(PW
,cow
)(pw
);
1871 if (isl_val_is_neg(v
))
1872 pw
->type
= isl_fold_type_negate(pw
->type
);
1874 for (i
= 0; i
< pw
->n
; ++i
) {
1875 pw
->p
[i
].FIELD
= FN(EL
,scale_val
)(pw
->p
[i
].FIELD
,
1877 if (!pw
->p
[i
].FIELD
)
1889 /* Divide the pieces of "pw" by "v" and return the result.
1891 __isl_give PW
*FN(PW
,scale_down_val
)(__isl_take PW
*pw
, __isl_take isl_val
*v
)
1898 if (isl_val_is_one(v
)) {
1903 if (!isl_val_is_rat(v
))
1904 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1905 "expecting rational factor", goto error
);
1906 if (isl_val_is_zero(v
))
1907 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1908 "cannot scale down by zero", goto error
);
1914 pw
= FN(PW
,cow
)(pw
);
1919 if (isl_val_is_neg(v
))
1920 pw
->type
= isl_fold_type_negate(pw
->type
);
1922 for (i
= 0; i
< pw
->n
; ++i
) {
1923 pw
->p
[i
].FIELD
= FN(EL
,scale_down_val
)(pw
->p
[i
].FIELD
,
1925 if (!pw
->p
[i
].FIELD
)
1937 __isl_give PW
*FN(PW
,scale
)(__isl_take PW
*pw
, isl_int v
)
1939 return FN(PW
,mul_isl_int
)(pw
, v
);
1942 /* Apply some normalization to "pw".
1943 * In particular, sort the pieces according to their function value
1944 * expressions, combining pairs of adjacent pieces with
1945 * the same such expression, and then normalize the domains of the pieces.
1947 * We normalize in place, but if anything goes wrong we need
1948 * to return NULL, so we need to make sure we don't change the
1949 * meaning of any possible other copies of "pw".
1951 __isl_give PW
*FN(PW
,normalize
)(__isl_take PW
*pw
)
1956 pw
= FN(PW
,sort
)(pw
);
1959 for (i
= 0; i
< pw
->n
; ++i
) {
1960 set
= isl_set_normalize(isl_set_copy(pw
->p
[i
].set
));
1962 return FN(PW
,free
)(pw
);
1963 isl_set_free(pw
->p
[i
].set
);
1970 /* Is pw1 obviously equal to pw2?
1971 * That is, do they have obviously identical cells and obviously identical
1972 * elements on each cell?
1974 * If "pw1" or "pw2" contain any NaNs, then they are considered
1975 * not to be the same. A NaN is not equal to anything, not even
1978 isl_bool
FN(PW
,plain_is_equal
)(__isl_keep PW
*pw1
, __isl_keep PW
*pw2
)
1981 isl_bool equal
, has_nan
;
1984 return isl_bool_error
;
1986 has_nan
= FN(PW
,involves_nan
)(pw1
);
1987 if (has_nan
>= 0 && !has_nan
)
1988 has_nan
= FN(PW
,involves_nan
)(pw2
);
1989 if (has_nan
< 0 || has_nan
)
1990 return isl_bool_not(has_nan
);
1993 return isl_bool_true
;
1994 if (!isl_space_is_equal(pw1
->dim
, pw2
->dim
))
1995 return isl_bool_false
;
1997 pw1
= FN(PW
,copy
)(pw1
);
1998 pw2
= FN(PW
,copy
)(pw2
);
1999 pw1
= FN(PW
,normalize
)(pw1
);
2000 pw2
= FN(PW
,normalize
)(pw2
);
2004 equal
= pw1
->n
== pw2
->n
;
2005 for (i
= 0; equal
&& i
< pw1
->n
; ++i
) {
2006 equal
= isl_set_plain_is_equal(pw1
->p
[i
].set
, pw2
->p
[i
].set
);
2011 equal
= FN(EL
,plain_is_equal
)(pw1
->p
[i
].FIELD
, pw2
->p
[i
].FIELD
);
2022 return isl_bool_error
;
2025 /* Does "pw" involve any NaNs?
2027 isl_bool
FN(PW
,involves_nan
)(__isl_keep PW
*pw
)
2032 return isl_bool_error
;
2034 return isl_bool_false
;
2036 for (i
= 0; i
< pw
->n
; ++i
) {
2037 isl_bool has_nan
= FN(EL
,involves_nan
)(pw
->p
[i
].FIELD
);
2038 if (has_nan
< 0 || has_nan
)
2042 return isl_bool_false
;
2046 static __isl_give PW
*FN(PW
,align_params_pw_multi_aff_and
)(__isl_take PW
*pw
,
2047 __isl_take isl_multi_aff
*ma
,
2048 __isl_give PW
*(*fn
)(__isl_take PW
*pw
, __isl_take isl_multi_aff
*ma
))
2051 isl_space
*ma_space
;
2053 ma_space
= isl_multi_aff_get_space(ma
);
2054 if (!pw
|| !ma
|| !ma_space
)
2056 if (isl_space_match(pw
->dim
, isl_dim_param
, ma_space
, isl_dim_param
)) {
2057 isl_space_free(ma_space
);
2060 ctx
= FN(PW
,get_ctx
)(pw
);
2061 if (!isl_space_has_named_params(pw
->dim
) ||
2062 !isl_space_has_named_params(ma_space
))
2063 isl_die(ctx
, isl_error_invalid
,
2064 "unaligned unnamed parameters", goto error
);
2065 pw
= FN(PW
,align_params
)(pw
, ma_space
);
2066 ma
= isl_multi_aff_align_params(ma
, FN(PW
,get_space
)(pw
));
2069 isl_space_free(ma_space
);
2071 isl_multi_aff_free(ma
);
2075 static __isl_give PW
*FN(PW
,align_params_pw_pw_multi_aff_and
)(__isl_take PW
*pw
,
2076 __isl_take isl_pw_multi_aff
*pma
,
2077 __isl_give PW
*(*fn
)(__isl_take PW
*pw
,
2078 __isl_take isl_pw_multi_aff
*ma
))
2081 isl_space
*pma_space
;
2083 pma_space
= isl_pw_multi_aff_get_space(pma
);
2084 if (!pw
|| !pma
|| !pma_space
)
2086 if (isl_space_match(pw
->dim
, isl_dim_param
, pma_space
, isl_dim_param
)) {
2087 isl_space_free(pma_space
);
2090 ctx
= FN(PW
,get_ctx
)(pw
);
2091 if (!isl_space_has_named_params(pw
->dim
) ||
2092 !isl_space_has_named_params(pma_space
))
2093 isl_die(ctx
, isl_error_invalid
,
2094 "unaligned unnamed parameters", goto error
);
2095 pw
= FN(PW
,align_params
)(pw
, pma_space
);
2096 pma
= isl_pw_multi_aff_align_params(pma
, FN(PW
,get_space
)(pw
));
2099 isl_space_free(pma_space
);
2101 isl_pw_multi_aff_free(pma
);
2105 /* Compute the pullback of "pw" by the function represented by "ma".
2106 * In other words, plug in "ma" in "pw".
2108 static __isl_give PW
*FN(PW
,pullback_multi_aff_aligned
)(__isl_take PW
*pw
,
2109 __isl_take isl_multi_aff
*ma
)
2112 isl_space
*space
= NULL
;
2114 ma
= isl_multi_aff_align_divs(ma
);
2115 pw
= FN(PW
,cow
)(pw
);
2119 space
= isl_space_join(isl_multi_aff_get_space(ma
),
2120 FN(PW
,get_space
)(pw
));
2122 for (i
= 0; i
< pw
->n
; ++i
) {
2123 pw
->p
[i
].set
= isl_set_preimage_multi_aff(pw
->p
[i
].set
,
2124 isl_multi_aff_copy(ma
));
2127 pw
->p
[i
].FIELD
= FN(EL
,pullback_multi_aff
)(pw
->p
[i
].FIELD
,
2128 isl_multi_aff_copy(ma
));
2129 if (!pw
->p
[i
].FIELD
)
2133 pw
= FN(PW
,reset_space
)(pw
, space
);
2134 isl_multi_aff_free(ma
);
2137 isl_space_free(space
);
2138 isl_multi_aff_free(ma
);
2143 __isl_give PW
*FN(PW
,pullback_multi_aff
)(__isl_take PW
*pw
,
2144 __isl_take isl_multi_aff
*ma
)
2146 return FN(PW
,align_params_pw_multi_aff_and
)(pw
, ma
,
2147 &FN(PW
,pullback_multi_aff_aligned
));
2150 /* Compute the pullback of "pw" by the function represented by "pma".
2151 * In other words, plug in "pma" in "pw".
2153 static __isl_give PW
*FN(PW
,pullback_pw_multi_aff_aligned
)(__isl_take PW
*pw
,
2154 __isl_take isl_pw_multi_aff
*pma
)
2164 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
2165 FN(PW
,get_space
)(pw
));
2166 isl_pw_multi_aff_free(pma
);
2167 res
= FN(PW
,empty
)(space
);
2172 res
= FN(PW
,pullback_multi_aff
)(FN(PW
,copy
)(pw
),
2173 isl_multi_aff_copy(pma
->p
[0].maff
));
2174 res
= FN(PW
,intersect_domain
)(res
, isl_set_copy(pma
->p
[0].set
));
2176 for (i
= 1; i
< pma
->n
; ++i
) {
2179 res_i
= FN(PW
,pullback_multi_aff
)(FN(PW
,copy
)(pw
),
2180 isl_multi_aff_copy(pma
->p
[i
].maff
));
2181 res_i
= FN(PW
,intersect_domain
)(res_i
,
2182 isl_set_copy(pma
->p
[i
].set
));
2183 res
= FN(PW
,add_disjoint
)(res
, res_i
);
2186 isl_pw_multi_aff_free(pma
);
2190 isl_pw_multi_aff_free(pma
);
2195 __isl_give PW
*FN(PW
,pullback_pw_multi_aff
)(__isl_take PW
*pw
,
2196 __isl_take isl_pw_multi_aff
*pma
)
2198 return FN(PW
,align_params_pw_pw_multi_aff_and
)(pw
, pma
,
2199 &FN(PW
,pullback_pw_multi_aff_aligned
));