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
17 #include <isl_val_private.h>
19 #include <isl_pw_macro.h>
23 __isl_give PW
*FN(PW
,alloc_size
)(__isl_take isl_space
*space
24 OPT_TYPE_PARAM
, int n
)
31 ctx
= isl_space_get_ctx(space
);
32 isl_assert(ctx
, n
>= 0, goto error
);
33 pw
= isl_alloc(ctx
, struct PW
,
34 sizeof(struct PW
) + (n
- 1) * sizeof(S(PW
,piece
)));
39 OPT_SET_TYPE(pw
->, type
);
45 isl_space_free(space
);
49 __isl_give PW
*FN(PW
,ZERO
)(__isl_take isl_space
*space OPT_TYPE_PARAM
)
51 return FN(PW
,alloc_size
)(space
OPT_TYPE_ARG(NO_LOC
), 0);
54 /* Add a piece with domain "set" and base expression "el"
55 * to the piecewise expression "pw".
57 * Do this independently of the values of "set" and "el",
58 * such that this function can be used by isl_pw_*_dup.
60 __isl_give PW
*FN(PW
,add_dup_piece
)(__isl_take PW
*pw
,
61 __isl_take isl_set
*set
, __isl_take EL
*el
)
64 isl_space
*el_dim
= NULL
;
66 if (!pw
|| !set
|| !el
)
69 ctx
= isl_set_get_ctx(set
);
70 if (!OPT_EQUAL_TYPES(pw
->, el
->))
71 isl_die(ctx
, isl_error_invalid
, "fold types don't match",
73 el_dim
= FN(EL
,get_space(el
));
74 isl_assert(ctx
, isl_space_is_equal(pw
->dim
, el_dim
), goto error
);
75 isl_assert(ctx
, pw
->n
< pw
->size
, goto error
);
77 pw
->p
[pw
->n
].set
= set
;
78 pw
->p
[pw
->n
].FIELD
= el
;
81 isl_space_free(el_dim
);
84 isl_space_free(el_dim
);
91 /* Add a piece with domain "set" and base expression "el"
92 * to the piecewise expression "pw", provided the domain
93 * is not obviously empty and the base expression
94 * is not equal to the default value.
96 __isl_give PW
*FN(PW
,add_piece
)(__isl_take PW
*pw
,
97 __isl_take isl_set
*set
, __isl_take EL
*el
)
101 skip
= isl_set_plain_is_empty(set
);
102 if (skip
>= 0 && !skip
)
103 skip
= FN(EL
,EL_IS_ZERO
)(el
);
104 if (skip
>= 0 && !skip
)
105 return FN(PW
,add_dup_piece
)(pw
, set
, el
);
110 return FN(PW
,free
)(pw
);
114 /* Does the space of "set" correspond to that of the domain of "el".
116 static isl_bool
FN(PW
,compatible_domain
)(__isl_keep EL
*el
,
117 __isl_keep isl_set
*set
)
120 isl_space
*el_space
, *set_space
;
123 return isl_bool_error
;
124 set_space
= isl_set_get_space(set
);
125 el_space
= FN(EL
,get_space
)(el
);
126 ok
= isl_space_is_domain_internal(set_space
, el_space
);
127 isl_space_free(el_space
);
128 isl_space_free(set_space
);
132 /* Check that the space of "set" corresponds to that of the domain of "el".
134 static isl_stat
FN(PW
,check_compatible_domain
)(__isl_keep EL
*el
,
135 __isl_keep isl_set
*set
)
139 ok
= FN(PW
,compatible_domain
)(el
, set
);
141 return isl_stat_error
;
143 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
144 "incompatible spaces", return isl_stat_error
);
149 __isl_give PW
*FN(PW
,alloc
)(OPT_TYPE_PARAM_FIRST
150 __isl_take isl_set
*set
, __isl_take EL
*el
)
154 if (FN(PW
,check_compatible_domain
)(el
, set
) < 0)
157 pw
= FN(PW
,alloc_size
)(FN(EL
,get_space
)(el
) OPT_TYPE_ARG(NO_LOC
), 1);
159 return FN(PW
,add_piece
)(pw
, set
, el
);
166 __isl_give PW
*FN(PW
,dup
)(__isl_keep PW
*pw
)
174 dup
= FN(PW
,alloc_size
)(isl_space_copy(pw
->dim
)
175 OPT_TYPE_ARG(pw
->), pw
->n
);
179 for (i
= 0; i
< pw
->n
; ++i
)
180 dup
= FN(PW
,add_dup_piece
)(dup
, isl_set_copy(pw
->p
[i
].set
),
181 FN(EL
,copy
)(pw
->p
[i
].FIELD
));
186 __isl_give PW
*FN(PW
,cow
)(__isl_take PW
*pw
)
194 return FN(PW
,dup
)(pw
);
197 __isl_give PW
*FN(PW
,copy
)(__isl_keep PW
*pw
)
206 __isl_null PW
*FN(PW
,free
)(__isl_take PW
*pw
)
215 for (i
= 0; i
< pw
->n
; ++i
) {
216 isl_set_free(pw
->p
[i
].set
);
217 FN(EL
,free
)(pw
->p
[i
].FIELD
);
219 isl_space_free(pw
->dim
);
225 /* Return the space of "pw".
227 __isl_keep isl_space
*FN(PW
,peek_space
)(__isl_keep PW
*pw
)
229 return pw
? pw
->dim
: NULL
;
232 __isl_give isl_space
*FN(PW
,get_space
)(__isl_keep PW
*pw
)
234 return isl_space_copy(FN(PW
,peek_space
)(pw
));
237 /* Return the space of "pw".
238 * This may be either a copy or the space itself
239 * if there is only one reference to "pw".
240 * This allows the space to be modified inplace
241 * if both the piecewise expression and its space have only a single reference.
242 * The caller is not allowed to modify "pw" between this call and
243 * a subsequent call to isl_pw_*_restore_*.
244 * The only exception is that isl_pw_*_free can be called instead.
246 __isl_give isl_space
*FN(PW
,take_space
)(__isl_keep PW
*pw
)
253 return FN(PW
,get_space
)(pw
);
259 /* Set the space of "pw" to "space", where the space of "pw" may be missing
260 * due to a preceding call to isl_pw_*_take_space.
261 * However, in this case, "pw" only has a single reference and
262 * then the call to isl_pw_*_cow has no effect.
264 __isl_give PW
*FN(PW
,restore_space
)(__isl_take PW
*pw
,
265 __isl_take isl_space
*space
)
270 if (pw
->dim
== space
) {
271 isl_space_free(space
);
278 isl_space_free(pw
->dim
);
284 isl_space_free(space
);
288 /* Check that "pos" is a valid position for a cell in "pw".
290 static isl_stat
FN(PW
,check_pos
)(__isl_keep PW
*pw
, int pos
)
293 return isl_stat_error
;
294 if (pos
< 0 || pos
>= pw
->n
)
295 isl_die(FN(PW
,get_ctx
)(pw
), isl_error_internal
,
296 "position out of bounds", return isl_stat_error
);
300 /* Return the cell at position "pos" in "pw".
302 static __isl_keep isl_set
*FN(PW
,peek_domain_at
)(__isl_keep PW
*pw
, int pos
)
304 if (FN(PW
,check_pos
)(pw
, pos
) < 0)
306 return pw
->p
[pos
].set
;
309 /* Return a copy of the cell at position "pos" in "pw".
311 __isl_give isl_set
*FN(PW
,get_domain_at
)(__isl_keep PW
*pw
, int pos
)
313 return isl_set_copy(FN(PW
,peek_domain_at
)(pw
, pos
));
316 /* Return the base expression associated to
317 * the cell at position "pos" in "pw".
319 static __isl_keep EL
*FN(PW
,peek_base_at
)(__isl_keep PW
*pw
, int pos
)
321 if (FN(PW
,check_pos
)(pw
, pos
) < 0)
323 return pw
->p
[pos
].FIELD
;
326 /* Return a copy of the base expression associated to
327 * the cell at position "pos" in "pw".
329 __isl_give EL
*FN(PW
,get_base_at
)(__isl_keep PW
*pw
, int pos
)
331 return FN(EL
,copy
)(FN(PW
,peek_base_at
)(pw
, pos
));
334 /* Return the base expression associated to
335 * the cell at position "pos" in "pw".
336 * This may be either a copy or the base expression itself
337 * if there is only one reference to "pw".
338 * This allows the base expression to be modified inplace
339 * if both the piecewise expression and this base expression
340 * have only a single reference.
341 * The caller is not allowed to modify "pw" between this call and
342 * a subsequent call to isl_pw_*_restore_*.
343 * The only exception is that isl_pw_*_free can be called instead.
345 __isl_give EL
*FN(PW
,take_base_at
)(__isl_keep PW
*pw
, int pos
)
352 return FN(PW
,get_base_at
)(pw
, pos
);
353 if (FN(PW
,check_pos
)(pw
, pos
) < 0)
355 el
= pw
->p
[pos
].FIELD
;
356 pw
->p
[pos
].FIELD
= NULL
;
360 /* Set the base expression associated to
361 * the cell at position "pos" in "pw" to "el",
362 * where this base expression may be missing
363 * due to a preceding call to isl_pw_*_take_base_at.
364 * However, in this case, "pw" only has a single reference and
365 * then the call to isl_pw_*_cow has no effect.
367 __isl_give PW
*FN(PW
,restore_base_at
)(__isl_take PW
*pw
, int pos
,
370 if (FN(PW
,check_pos
)(pw
, pos
) < 0 || !el
)
373 if (pw
->p
[pos
].FIELD
== el
) {
381 FN(EL
,free
)(pw
->p
[pos
].FIELD
);
382 pw
->p
[pos
].FIELD
= el
;
391 /* Create a piecewise expression with the given base expression on a universe
394 static __isl_give PW
*FN(FN(FN(PW
,from
),BASE
),type_base
)(__isl_take EL
*el
397 isl_set
*dom
= isl_set_universe(FN(EL
,get_domain_space
)(el
));
398 return FN(PW
,alloc
)(OPT_TYPE_ARG_FIRST(NO_LOC
) dom
, el
);
401 /* Create a piecewise expression with the given base expression on a universe
404 * If the default value of this piecewise type is zero and
405 * if "el" is effectively zero, then create an empty piecewise expression
408 static __isl_give PW
*FN(FN(FN(PW
,from
),BASE
),type
)(__isl_take EL
*el
414 if (!DEFAULT_IS_ZERO
)
415 return FN(FN(FN(PW
,from
),BASE
),type_base
)(el
416 OPT_TYPE_ARG(NO_LOC
));
417 is_zero
= FN(EL
,EL_IS_ZERO
)(el
);
421 return FN(FN(FN(PW
,from
),BASE
),type_base
)(el
422 OPT_TYPE_ARG(NO_LOC
));
423 space
= FN(EL
,get_space
)(el
);
425 return FN(PW
,ZERO
)(space
OPT_TYPE_ARG(NO_LOC
));
432 /* Create a piecewise expression with the given base expression on a universe
435 * Pass along the type as an extra argument for improved uniformity
436 * with piecewise types that do not have a fold type.
438 __isl_give PW
*FN(FN(PW
,from
),BASE
)(__isl_take EL
*el
)
440 enum isl_fold type
= FN(EL
,get_type
)(el
);
441 return FN(FN(FN(PW
,from
),BASE
),type
)(el
, type
);
444 __isl_give PW
*FN(FN(PW
,from
),BASE
)(__isl_take EL
*el
)
446 return FN(FN(FN(PW
,from
),BASE
),type
)(el
);
450 const char *FN(PW
,get_dim_name
)(__isl_keep PW
*pw
, enum isl_dim_type type
,
453 return pw
? isl_space_get_dim_name(pw
->dim
, type
, pos
) : NULL
;
456 isl_bool
FN(PW
,has_dim_id
)(__isl_keep PW
*pw
, enum isl_dim_type type
,
459 return pw
? isl_space_has_dim_id(pw
->dim
, type
, pos
) : isl_bool_error
;
462 __isl_give isl_id
*FN(PW
,get_dim_id
)(__isl_keep PW
*pw
, enum isl_dim_type type
,
465 return pw
? isl_space_get_dim_id(pw
->dim
, type
, pos
) : NULL
;
468 isl_bool
FN(PW
,has_tuple_name
)(__isl_keep PW
*pw
, enum isl_dim_type type
)
470 return pw
? isl_space_has_tuple_name(pw
->dim
, type
) : isl_bool_error
;
473 const char *FN(PW
,get_tuple_name
)(__isl_keep PW
*pw
, enum isl_dim_type type
)
475 return pw
? isl_space_get_tuple_name(pw
->dim
, type
) : NULL
;
478 isl_bool
FN(PW
,has_tuple_id
)(__isl_keep PW
*pw
, enum isl_dim_type type
)
480 return pw
? isl_space_has_tuple_id(pw
->dim
, type
) : isl_bool_error
;
483 __isl_give isl_id
*FN(PW
,get_tuple_id
)(__isl_keep PW
*pw
, enum isl_dim_type type
)
485 return pw
? isl_space_get_tuple_id(pw
->dim
, type
) : NULL
;
488 isl_bool
FN(PW
,IS_ZERO
)(__isl_keep PW
*pw
)
491 return isl_bool_error
;
493 return isl_bool_ok(pw
->n
== 0);
496 __isl_give PW
*FN(PW
,realign_domain
)(__isl_take PW
*pw
,
497 __isl_take isl_reordering
*exp
)
503 n
= FN(PW
,n_piece
)(pw
);
507 for (i
= 0; i
< n
; ++i
) {
510 pw
->p
[i
].set
= isl_set_realign(pw
->p
[i
].set
,
511 isl_reordering_copy(exp
));
514 el
= FN(PW
,take_base_at
)(pw
, i
);
515 el
= FN(EL
,realign_domain
)(el
, isl_reordering_copy(exp
));
516 pw
= FN(PW
,restore_base_at
)(pw
, i
, el
);
521 pw
= FN(PW
,reset_domain_space
)(pw
, isl_reordering_get_space(exp
));
523 isl_reordering_free(exp
);
526 isl_reordering_free(exp
);
534 #include "isl_check_named_params_templ.c"
536 /* Align the parameters of "pw" to those of "model".
538 __isl_give PW
*FN(PW
,align_params
)(__isl_take PW
*pw
, __isl_take isl_space
*model
)
541 isl_bool equal_params
;
546 ctx
= isl_space_get_ctx(model
);
547 if (!isl_space_has_named_params(model
))
548 isl_die(ctx
, isl_error_invalid
,
549 "model has unnamed parameters", goto error
);
550 if (FN(PW
,check_named_params
)(pw
) < 0)
552 equal_params
= isl_space_has_equal_params(pw
->dim
, model
);
553 if (equal_params
< 0)
558 exp
= isl_parameter_alignment_reordering(pw
->dim
, model
);
559 exp
= isl_reordering_extend_space(exp
,
560 FN(PW
,get_domain_space
)(pw
));
561 pw
= FN(PW
,realign_domain
)(pw
, exp
);
564 isl_space_free(model
);
567 isl_space_free(model
);
576 #include "isl_align_params_bin_templ.c"
586 #include "isl_align_params_templ.c"
591 #include "isl_type_has_equal_space_bin_templ.c"
592 #include "isl_type_check_equal_space_templ.c"
594 /* Private version of "union_add". For isl_pw_qpolynomial and
595 * isl_pw_qpolynomial_fold, we prefer to simply call it "add".
597 static __isl_give PW
*FN(PW
,union_add_
)(__isl_take PW
*pw1
, __isl_take PW
*pw2
)
604 if (FN(PW
,align_params_bin
)(&pw1
, &pw2
) < 0)
607 ctx
= isl_space_get_ctx(pw1
->dim
);
608 if (!OPT_EQUAL_TYPES(pw1
->, pw2
->))
609 isl_die(ctx
, isl_error_invalid
,
610 "fold types don't match", goto error
);
611 if (FN(PW
,check_equal_space
)(pw1
, pw2
) < 0)
614 if (FN(PW
,IS_ZERO
)(pw1
)) {
619 if (FN(PW
,IS_ZERO
)(pw2
)) {
624 n
= (pw1
->n
+ 1) * (pw2
->n
+ 1);
625 res
= FN(PW
,alloc_size
)(isl_space_copy(pw1
->dim
)
626 OPT_TYPE_ARG(pw1
->), n
);
628 for (i
= 0; i
< pw1
->n
; ++i
) {
629 set
= isl_set_copy(pw1
->p
[i
].set
);
630 for (j
= 0; j
< pw2
->n
; ++j
) {
631 struct isl_set
*common
;
633 common
= isl_set_intersect(isl_set_copy(pw1
->p
[i
].set
),
634 isl_set_copy(pw2
->p
[j
].set
));
635 if (isl_set_plain_is_empty(common
)) {
636 isl_set_free(common
);
639 set
= isl_set_subtract(set
,
640 isl_set_copy(pw2
->p
[j
].set
));
642 sum
= FN(EL
,add_on_domain
)(common
,
643 FN(EL
,copy
)(pw1
->p
[i
].FIELD
),
644 FN(EL
,copy
)(pw2
->p
[j
].FIELD
));
646 res
= FN(PW
,add_piece
)(res
, common
, sum
);
648 res
= FN(PW
,add_piece
)(res
, set
, FN(EL
,copy
)(pw1
->p
[i
].FIELD
));
651 for (j
= 0; j
< pw2
->n
; ++j
) {
652 set
= isl_set_copy(pw2
->p
[j
].set
);
653 for (i
= 0; i
< pw1
->n
; ++i
)
654 set
= isl_set_subtract(set
,
655 isl_set_copy(pw1
->p
[i
].set
));
656 res
= FN(PW
,add_piece
)(res
, set
, FN(EL
,copy
)(pw2
->p
[j
].FIELD
));
669 /* Make sure "pw" has room for at least "n" more pieces.
671 * If there is only one reference to pw, we extend it in place.
672 * Otherwise, we create a new PW and copy the pieces.
674 static __isl_give PW
*FN(PW
,grow
)(__isl_take PW
*pw
, int n
)
682 if (pw
->n
+ n
<= pw
->size
)
684 ctx
= FN(PW
,get_ctx
)(pw
);
687 res
= isl_realloc(ctx
, pw
, struct PW
,
688 sizeof(struct PW
) + (n
- 1) * sizeof(S(PW
,piece
)));
690 return FN(PW
,free
)(pw
);
694 res
= FN(PW
,alloc_size
)(isl_space_copy(pw
->dim
) OPT_TYPE_ARG(pw
->), n
);
696 return FN(PW
,free
)(pw
);
697 for (i
= 0; i
< pw
->n
; ++i
)
698 res
= FN(PW
,add_piece
)(res
, isl_set_copy(pw
->p
[i
].set
),
699 FN(EL
,copy
)(pw
->p
[i
].FIELD
));
704 __isl_give PW
*FN(PW
,add_disjoint
)(__isl_take PW
*pw1
, __isl_take PW
*pw2
)
709 if (FN(PW
,align_params_bin
)(&pw1
, &pw2
) < 0)
712 if (pw1
->size
< pw1
->n
+ pw2
->n
&& pw1
->n
< pw2
->n
)
713 return FN(PW
,add_disjoint
)(pw2
, pw1
);
715 ctx
= isl_space_get_ctx(pw1
->dim
);
716 if (!OPT_EQUAL_TYPES(pw1
->, pw2
->))
717 isl_die(ctx
, isl_error_invalid
,
718 "fold types don't match", goto error
);
719 if (FN(PW
,check_equal_space
)(pw1
, pw2
) < 0)
722 if (FN(PW
,IS_ZERO
)(pw1
)) {
727 if (FN(PW
,IS_ZERO
)(pw2
)) {
732 pw1
= FN(PW
,grow
)(pw1
, pw2
->n
);
736 for (i
= 0; i
< pw2
->n
; ++i
)
737 pw1
= FN(PW
,add_piece
)(pw1
,
738 isl_set_copy(pw2
->p
[i
].set
),
739 FN(EL
,copy
)(pw2
->p
[i
].FIELD
));
750 /* This function is currently only used from isl_aff.c
752 static __isl_give PW
*FN(PW
,on_shared_domain_in
)(__isl_take PW
*pw1
,
753 __isl_take PW
*pw2
, __isl_take isl_space
*space
,
754 __isl_give EL
*(*fn
)(__isl_take EL
*el1
, __isl_take EL
*el2
))
755 __attribute__ ((unused
));
757 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
758 * The result of "fn" (and therefore also of this function) lives in "space".
760 static __isl_give PW
*FN(PW
,on_shared_domain_in
)(__isl_take PW
*pw1
,
761 __isl_take PW
*pw2
, __isl_take isl_space
*space
,
762 __isl_give EL
*(*fn
)(__isl_take EL
*el1
, __isl_take EL
*el2
))
771 res
= FN(PW
,alloc_size
)(isl_space_copy(space
) OPT_TYPE_ARG(pw1
->), n
);
773 for (i
= 0; i
< pw1
->n
; ++i
) {
774 for (j
= 0; j
< pw2
->n
; ++j
) {
779 common
= isl_set_intersect(
780 isl_set_copy(pw1
->p
[i
].set
),
781 isl_set_copy(pw2
->p
[j
].set
));
782 empty
= isl_set_plain_is_empty(common
);
783 if (empty
< 0 || empty
) {
784 isl_set_free(common
);
790 res_ij
= fn(FN(EL
,copy
)(pw1
->p
[i
].FIELD
),
791 FN(EL
,copy
)(pw2
->p
[j
].FIELD
));
792 res_ij
= FN(EL
,gist
)(res_ij
, isl_set_copy(common
));
794 res
= FN(PW
,add_piece
)(res
, common
, res_ij
);
798 isl_space_free(space
);
803 isl_space_free(space
);
810 /* This function is currently only used from isl_aff.c
812 static __isl_give PW
*FN(PW
,on_shared_domain
)(__isl_take PW
*pw1
,
814 __isl_give EL
*(*fn
)(__isl_take EL
*el1
, __isl_take EL
*el2
))
815 __attribute__ ((unused
));
817 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
818 * The result of "fn" is assumed to live in the same space as "pw1" and "pw2".
820 static __isl_give PW
*FN(PW
,on_shared_domain
)(__isl_take PW
*pw1
,
822 __isl_give EL
*(*fn
)(__isl_take EL
*el1
, __isl_take EL
*el2
))
826 if (FN(PW
,check_equal_space
)(pw1
, pw2
) < 0)
829 space
= isl_space_copy(pw1
->dim
);
830 return FN(PW
,on_shared_domain_in
)(pw1
, pw2
, space
, fn
);
837 /* Return the parameter domain of "pw".
839 __isl_give isl_set
*FN(PW
,params
)(__isl_take PW
*pw
)
841 return isl_set_params(FN(PW
,domain
)(pw
));
844 __isl_give isl_set
*FN(PW
,domain
)(__isl_take PW
*pw
)
852 dom
= isl_set_empty(FN(PW
,get_domain_space
)(pw
));
853 for (i
= 0; i
< pw
->n
; ++i
)
854 dom
= isl_set_union_disjoint(dom
, isl_set_copy(pw
->p
[i
].set
));
861 /* Exploit the equalities in the domain of piece "i" of "pw"
862 * to simplify the associated function.
863 * If the domain of piece "i" is empty, then remove it entirely,
864 * replacing it with the final piece.
866 static int FN(PW
,exploit_equalities_and_remove_if_empty
)(__isl_keep PW
*pw
,
870 int empty
= isl_set_plain_is_empty(pw
->p
[i
].set
);
875 isl_set_free(pw
->p
[i
].set
);
876 FN(EL
,free
)(pw
->p
[i
].FIELD
);
878 pw
->p
[i
] = pw
->p
[pw
->n
- 1];
884 aff
= isl_set_affine_hull(isl_set_copy(pw
->p
[i
].set
));
885 pw
->p
[i
].FIELD
= FN(EL
,substitute_equalities
)(pw
->p
[i
].FIELD
, aff
);
892 /* Convert a piecewise expression defined over a parameter domain
893 * into one that is defined over a zero-dimensional set.
895 __isl_give PW
*FN(PW
,from_range
)(__isl_take PW
*pw
)
901 if (!isl_space_is_set(pw
->dim
))
902 isl_die(FN(PW
,get_ctx
)(pw
), isl_error_invalid
,
903 "not living in a set space", return FN(PW
,free
)(pw
));
905 space
= FN(PW
,get_space
)(pw
);
906 space
= isl_space_from_range(space
);
907 pw
= FN(PW
,reset_space
)(pw
, space
);
912 /* Fix the value of the given parameter or domain dimension of "pw"
913 * to be equal to "value".
915 __isl_give PW
*FN(PW
,fix_si
)(__isl_take PW
*pw
, enum isl_dim_type type
,
916 unsigned pos
, int value
)
921 n
= FN(PW
,n_piece
)(pw
);
923 return FN(PW
,free
)(pw
);
925 if (type
== isl_dim_out
)
926 isl_die(FN(PW
,get_ctx
)(pw
), isl_error_invalid
,
927 "cannot fix output dimension", return FN(PW
,free
)(pw
));
932 if (type
== isl_dim_in
)
937 return FN(PW
,free
)(pw
);
939 for (i
= n
- 1; i
>= 0; --i
) {
940 pw
->p
[i
].set
= isl_set_fix_si(pw
->p
[i
].set
, type
, pos
, value
);
941 if (FN(PW
,exploit_equalities_and_remove_if_empty
)(pw
, i
) < 0)
942 return FN(PW
,free
)(pw
);
948 /* Restrict the domain of "pw" by combining each cell
949 * with "set" through a call to "fn", where "fn" may be
950 * isl_set_intersect, isl_set_intersect_params, isl_set_intersect_factor_domain,
951 * isl_set_intersect_factor_range or isl_set_subtract.
953 static __isl_give PW
*FN(PW
,restrict_domain
)(__isl_take PW
*pw
,
954 __isl_take isl_set
*set
,
955 __isl_give isl_set
*(*fn
)(__isl_take isl_set
*set1
,
956 __isl_take isl_set
*set2
))
961 FN(PW
,align_params_set
)(&pw
, &set
);
962 n
= FN(PW
,n_piece
)(pw
);
975 for (i
= n
- 1; i
>= 0; --i
) {
976 pw
->p
[i
].set
= fn(pw
->p
[i
].set
, isl_set_copy(set
));
977 if (FN(PW
,exploit_equalities_and_remove_if_empty
)(pw
, i
) < 0)
989 __isl_give PW
*FN(PW
,intersect_domain
)(__isl_take PW
*pw
,
990 __isl_take isl_set
*context
)
992 return FN(PW
,restrict_domain
)(pw
, context
, &isl_set_intersect
);
995 /* Intersect the domain of "pw" with the parameter domain "context".
997 __isl_give PW
*FN(PW
,intersect_params
)(__isl_take PW
*pw
,
998 __isl_take isl_set
*context
)
1000 return FN(PW
,restrict_domain
)(pw
, context
, &isl_set_intersect_params
);
1003 /* Given a piecewise expression "pw" with domain in a space [A -> B] and
1004 * a set in the space A, intersect the domain with the set.
1006 __isl_give PW
*FN(PW
,intersect_domain_wrapped_domain
)(__isl_take PW
*pw
,
1007 __isl_take isl_set
*set
)
1009 return FN(PW
,restrict_domain
)(pw
, set
,
1010 &isl_set_intersect_factor_domain
);
1013 /* Given a piecewise expression "pw" with domain in a space [A -> B] and
1014 * a set in the space B, intersect the domain with the set.
1016 __isl_give PW
*FN(PW
,intersect_domain_wrapped_range
)(__isl_take PW
*pw
,
1017 __isl_take isl_set
*set
)
1019 return FN(PW
,restrict_domain
)(pw
, set
, &isl_set_intersect_factor_range
);
1022 /* Subtract "domain' from the domain of "pw".
1024 __isl_give PW
*FN(PW
,subtract_domain
)(__isl_take PW
*pw
,
1025 __isl_take isl_set
*domain
)
1027 return FN(PW
,restrict_domain
)(pw
, domain
, &isl_set_subtract
);
1030 /* Return -1 if the piece "p1" should be sorted before "p2"
1031 * and 1 if it should be sorted after "p2".
1032 * Return 0 if they do not need to be sorted in a specific order.
1034 * The two pieces are compared on the basis of their function value expressions.
1036 static int FN(PW
,sort_field_cmp
)(const void *p1
, const void *p2
, void *arg
)
1038 struct FN(PW
,piece
) const *pc1
= p1
;
1039 struct FN(PW
,piece
) const *pc2
= p2
;
1041 return FN(EL
,plain_cmp
)(pc1
->FIELD
, pc2
->FIELD
);
1044 /* Sort the pieces of "pw" according to their function value
1045 * expressions and then combine pairs of adjacent pieces with
1046 * the same such expression.
1048 * The sorting is performed in place because it does not
1049 * change the meaning of "pw", but care needs to be
1050 * taken not to change any possible other copies of "pw"
1051 * in case anything goes wrong.
1053 static __isl_give PW
*FN(PW
,sort_unique
)(__isl_take PW
*pw
)
1062 if (isl_sort(pw
->p
, pw
->n
, sizeof(pw
->p
[0]),
1063 &FN(PW
,sort_field_cmp
), NULL
) < 0)
1064 return FN(PW
,free
)(pw
);
1065 for (i
= pw
->n
- 1; i
>= 1; --i
) {
1070 el
= FN(PW
,peek_base_at
)(pw
, i
);
1071 el_prev
= FN(PW
,peek_base_at
)(pw
, i
- 1);
1072 equal
= FN(EL
,plain_is_equal
)(el
, el_prev
);
1074 return FN(PW
,free
)(pw
);
1077 set
= FN(PW
,get_domain_at
)(pw
, i
);
1078 set_prev
= FN(PW
,get_domain_at
)(pw
, i
- 1);
1079 set
= isl_set_union(set_prev
, set
);
1081 return FN(PW
,free
)(pw
);
1082 isl_set_free(pw
->p
[i
].set
);
1083 FN(EL
,free
)(pw
->p
[i
].FIELD
);
1084 isl_set_free(pw
->p
[i
- 1].set
);
1085 pw
->p
[i
- 1].set
= set
;
1086 for (j
= i
+ 1; j
< pw
->n
; ++j
)
1087 pw
->p
[j
- 1] = pw
->p
[j
];
1094 /* Compute the gist of "pw" with respect to the domain constraints
1095 * of "context" for the case where the domain of the last element
1096 * of "pw" is equal to "context".
1097 * Call "fn_el" to compute the gist of this element, replace
1098 * its domain by the universe and drop all other elements
1099 * as their domains are necessarily disjoint from "context".
1101 static __isl_give PW
*FN(PW
,gist_last
)(__isl_take PW
*pw
,
1102 __isl_take isl_set
*context
,
1103 __isl_give EL
*(*fn_el
)(__isl_take EL
*el
, __isl_take isl_set
*set
))
1109 for (i
= 0; i
< pw
->n
- 1; ++i
) {
1110 isl_set_free(pw
->p
[i
].set
);
1111 FN(EL
,free
)(pw
->p
[i
].FIELD
);
1113 pw
->p
[0].FIELD
= pw
->p
[pw
->n
- 1].FIELD
;
1114 pw
->p
[0].set
= pw
->p
[pw
->n
- 1].set
;
1117 space
= isl_set_get_space(context
);
1118 el
= FN(PW
,take_base_at
)(pw
, 0);
1119 el
= fn_el(el
, context
);
1120 pw
= FN(PW
,restore_base_at
)(pw
, 0, el
);
1123 context
= isl_set_universe(space
);
1124 isl_set_free(pw
->p
[0].set
);
1125 pw
->p
[0].set
= context
;
1128 return FN(PW
,free
)(pw
);
1132 isl_space_free(space
);
1133 return FN(PW
,free
)(pw
);
1136 /* Compute the gist of "pw" with respect to the domain constraints
1137 * of "context". Call "fn_el" to compute the gist of the elements
1138 * and "fn_dom" to compute the gist of the domains.
1140 * If the piecewise expression is empty or the context is the universe,
1141 * then nothing can be simplified.
1142 * If "pw" has a single domain and it is equal to "context",
1143 * then simply replace the domain by the universe.
1144 * Combine duplicate function value expressions first
1145 * to increase the chance of "pw" having a single domain.
1147 static __isl_give PW
*FN(PW
,gist_fn
)(__isl_take PW
*pw
,
1148 __isl_take isl_set
*context
,
1149 __isl_give EL
*(*fn_el
)(__isl_take EL
*el
,
1150 __isl_take isl_set
*set
),
1151 __isl_give isl_set
*(*fn_dom
)(__isl_take isl_set
*set
,
1152 __isl_take isl_basic_set
*bset
))
1156 isl_basic_set
*hull
= NULL
;
1158 pw
= FN(PW
,sort_unique
)(pw
);
1159 if (!pw
|| !context
)
1163 isl_set_free(context
);
1167 is_universe
= isl_set_plain_is_universe(context
);
1168 if (is_universe
< 0)
1171 isl_set_free(context
);
1175 FN(PW
,align_params_set
)(&pw
, &context
);
1177 pw
= FN(PW
,cow
)(pw
);
1184 equal
= isl_set_plain_is_equal(pw
->p
[0].set
, context
);
1188 return FN(PW
,gist_last
)(pw
, context
, fn_el
);
1191 context
= isl_set_compute_divs(context
);
1192 hull
= isl_set_simple_hull(isl_set_copy(context
));
1194 for (i
= pw
->n
- 1; i
>= 0; --i
) {
1199 if (i
== pw
->n
- 1) {
1201 equal
= isl_set_plain_is_equal(pw
->p
[i
].set
, context
);
1205 isl_basic_set_free(hull
);
1206 return FN(PW
,gist_last
)(pw
, context
, fn_el
);
1209 set_i
= FN(PW
,get_domain_at
)(pw
, i
);
1210 set_i
= isl_set_intersect(set_i
, isl_set_copy(context
));
1211 empty
= isl_set_plain_is_empty(set_i
);
1212 el
= FN(PW
,take_base_at
)(pw
, i
);
1213 el
= fn_el(el
, set_i
);
1214 pw
= FN(PW
,restore_base_at
)(pw
, i
, el
);
1217 pw
->p
[i
].set
= fn_dom(pw
->p
[i
].set
, isl_basic_set_copy(hull
));
1218 if (empty
< 0 || !pw
->p
[i
].set
)
1221 isl_set_free(pw
->p
[i
].set
);
1222 FN(EL
,free
)(pw
->p
[i
].FIELD
);
1224 pw
->p
[i
] = pw
->p
[pw
->n
- 1];
1229 isl_basic_set_free(hull
);
1230 isl_set_free(context
);
1235 isl_basic_set_free(hull
);
1236 isl_set_free(context
);
1240 __isl_give PW
*FN(PW
,gist
)(__isl_take PW
*pw
, __isl_take isl_set
*context
)
1242 return FN(PW
,gist_fn
)(pw
, context
, &FN(EL
,gist
),
1243 &isl_set_gist_basic_set
);
1246 __isl_give PW
*FN(PW
,gist_params
)(__isl_take PW
*pw
,
1247 __isl_take isl_set
*context
)
1249 return FN(PW
,gist_fn
)(pw
, context
, &FN(EL
,gist_params
),
1250 &isl_set_gist_params_basic_set
);
1253 /* Coalesce the domains of "pw".
1255 * Prior to the actual coalescing, first sort the pieces such that
1256 * pieces with the same function value expression are combined
1257 * into a single piece, the combined domain of which can then
1260 __isl_give PW
*FN(PW
,coalesce
)(__isl_take PW
*pw
)
1265 pw
= FN(PW
,sort_unique
)(pw
);
1266 n
= FN(PW
,n_piece
)(pw
);
1268 return FN(PW
,free
)(pw
);
1270 for (i
= 0; i
< n
; ++i
) {
1271 pw
->p
[i
].set
= isl_set_coalesce(pw
->p
[i
].set
);
1282 isl_ctx
*FN(PW
,get_ctx
)(__isl_keep PW
*pw
)
1284 return pw
? isl_space_get_ctx(pw
->dim
) : NULL
;
1287 isl_bool
FN(PW
,involves_dims
)(__isl_keep PW
*pw
, enum isl_dim_type type
,
1288 unsigned first
, unsigned n
)
1291 enum isl_dim_type set_type
;
1294 return isl_bool_error
;
1295 if (pw
->n
== 0 || n
== 0)
1296 return isl_bool_false
;
1298 set_type
= type
== isl_dim_in
? isl_dim_set
: type
;
1300 for (i
= 0; i
< pw
->n
; ++i
) {
1301 isl_bool involves
= FN(EL
,involves_dims
)(pw
->p
[i
].FIELD
,
1303 if (involves
< 0 || involves
)
1305 involves
= isl_set_involves_dims(pw
->p
[i
].set
,
1306 set_type
, first
, n
);
1307 if (involves
< 0 || involves
)
1310 return isl_bool_false
;
1313 __isl_give PW
*FN(PW
,set_dim_name
)(__isl_take PW
*pw
,
1314 enum isl_dim_type type
, unsigned pos
, const char *s
)
1318 space
= FN(PW
,get_space
)(pw
);
1319 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
1320 return FN(PW
,reset_space
)(pw
, space
);
1323 __isl_give PW
*FN(PW
,drop_dims
)(__isl_take PW
*pw
,
1324 enum isl_dim_type type
, unsigned first
, unsigned n
)
1328 enum isl_dim_type set_type
;
1331 n_piece
= FN(PW
,n_piece
)(pw
);
1333 return FN(PW
,free
)(pw
);
1334 if (n
== 0 && !isl_space_get_tuple_name(pw
->dim
, type
))
1337 set_type
= type
== isl_dim_in
? isl_dim_set
: type
;
1339 space
= FN(PW
,take_space
)(pw
);
1340 space
= isl_space_drop_dims(space
, type
, first
, n
);
1341 pw
= FN(PW
,restore_space
)(pw
, space
);
1342 pw
= FN(PW
,cow
)(pw
);
1343 for (i
= 0; i
< n_piece
; ++i
) {
1346 el
= FN(PW
,take_base_at
)(pw
, i
);
1347 el
= FN(EL
,drop_dims
)(el
, type
, first
, n
);
1348 pw
= FN(PW
,restore_base_at
)(pw
, i
, el
);
1351 if (type
== isl_dim_out
)
1353 pw
->p
[i
].set
= isl_set_drop(pw
->p
[i
].set
, set_type
, first
, n
);
1364 /* This function is very similar to drop_dims.
1365 * The only difference is that the cells may still involve
1366 * the specified dimensions. They are removed using
1367 * isl_set_project_out instead of isl_set_drop.
1369 __isl_give PW
*FN(PW
,project_out
)(__isl_take PW
*pw
,
1370 enum isl_dim_type type
, unsigned first
, unsigned n
)
1374 enum isl_dim_type set_type
;
1377 n_piece
= FN(PW
,n_piece
)(pw
);
1379 return FN(PW
,free
)(pw
);
1380 if (n
== 0 && !isl_space_get_tuple_name(pw
->dim
, type
))
1383 set_type
= type
== isl_dim_in
? isl_dim_set
: type
;
1385 space
= FN(PW
,take_space
)(pw
);
1386 space
= isl_space_drop_dims(space
, type
, first
, n
);
1387 pw
= FN(PW
,restore_space
)(pw
, space
);
1388 pw
= FN(PW
,cow
)(pw
);
1391 for (i
= 0; i
< n_piece
; ++i
) {
1394 pw
->p
[i
].set
= isl_set_project_out(pw
->p
[i
].set
,
1395 set_type
, first
, n
);
1398 el
= FN(PW
,take_base_at
)(pw
, i
);
1399 el
= FN(EL
,drop_dims
)(el
, type
, first
, n
);
1400 pw
= FN(PW
,restore_base_at
)(pw
, i
, el
);
1411 /* Project the domain of pw onto its parameter space.
1413 __isl_give PW
*FN(PW
,project_domain_on_params
)(__isl_take PW
*pw
)
1418 n
= FN(PW
,dim
)(pw
, isl_dim_in
);
1420 return FN(PW
,free
)(pw
);
1421 pw
= FN(PW
,project_out
)(pw
, isl_dim_in
, 0, n
);
1422 space
= FN(PW
,get_domain_space
)(pw
);
1423 space
= isl_space_params(space
);
1424 pw
= FN(PW
,reset_domain_space
)(pw
, space
);
1428 /* Drop all parameters not referenced by "pw".
1430 __isl_give PW
*FN(PW
,drop_unused_params
)(__isl_take PW
*pw
)
1435 if (FN(PW
,check_named_params
)(pw
) < 0)
1436 return FN(PW
,free
)(pw
);
1438 n
= FN(PW
,dim
)(pw
, isl_dim_param
);
1440 return FN(PW
,free
)(pw
);
1441 for (i
= n
- 1; i
>= 0; i
--) {
1444 involves
= FN(PW
,involves_dims
)(pw
, isl_dim_param
, i
, 1);
1446 return FN(PW
,free
)(pw
);
1448 pw
= FN(PW
,drop_dims
)(pw
, isl_dim_param
, i
, 1);
1454 __isl_give PW
*FN(PW
,fix_dim
)(__isl_take PW
*pw
,
1455 enum isl_dim_type type
, unsigned pos
, isl_int v
)
1460 n
= FN(PW
,n_piece
)(pw
);
1462 return FN(PW
,free
)(pw
);
1464 if (type
== isl_dim_in
)
1467 pw
= FN(PW
,cow
)(pw
);
1470 for (i
= 0; i
< n
; ++i
) {
1471 pw
->p
[i
].set
= isl_set_fix(pw
->p
[i
].set
, type
, pos
, v
);
1472 if (FN(PW
,exploit_equalities_and_remove_if_empty
)(pw
, i
) < 0)
1473 return FN(PW
,free
)(pw
);
1479 /* Fix the value of the variable at position "pos" of type "type" of "pw"
1480 * to be equal to "v".
1482 __isl_give PW
*FN(PW
,fix_val
)(__isl_take PW
*pw
,
1483 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
1486 return FN(PW
,free
)(pw
);
1487 if (!isl_val_is_int(v
))
1488 isl_die(FN(PW
,get_ctx
)(pw
), isl_error_invalid
,
1489 "expecting integer value", goto error
);
1491 pw
= FN(PW
,fix_dim
)(pw
, type
, pos
, v
->n
);
1497 return FN(PW
,free
)(pw
);
1500 isl_size
FN(PW
,dim
)(__isl_keep PW
*pw
, enum isl_dim_type type
)
1502 return isl_space_dim(FN(PW
,peek_space
)(pw
), type
);
1505 __isl_give PW
*FN(PW
,split_dims
)(__isl_take PW
*pw
,
1506 enum isl_dim_type type
, unsigned first
, unsigned n
)
1511 n_piece
= FN(PW
,n_piece
)(pw
);
1513 return FN(PW
,free
)(pw
);
1517 if (type
== isl_dim_in
)
1520 pw
= FN(PW
,cow
)(pw
);
1523 for (i
= 0; i
< n
; ++i
) {
1524 pw
->p
[i
].set
= isl_set_split_dims(pw
->p
[i
].set
, type
, first
, n
);
1535 __isl_give isl_space
*FN(PW
,get_domain_space
)(__isl_keep PW
*pw
)
1537 return pw
? isl_space_domain(isl_space_copy(pw
->dim
)) : NULL
;
1540 /* Return the position of the dimension of the given type and name
1542 * Return -1 if no such dimension can be found.
1544 int FN(PW
,find_dim_by_name
)(__isl_keep PW
*pw
,
1545 enum isl_dim_type type
, const char *name
)
1549 return isl_space_find_dim_by_name(pw
->dim
, type
, name
);
1552 /* Return the position of the dimension of the given type and identifier
1554 * Return -1 if no such dimension can be found.
1556 static int FN(PW
,find_dim_by_id
)(__isl_keep PW
*pw
,
1557 enum isl_dim_type type
, __isl_keep isl_id
*id
)
1561 space
= FN(PW
,peek_space
)(pw
);
1562 return isl_space_find_dim_by_id(space
, type
, id
);
1565 /* Does the piecewise expression "pw" depend in any way
1566 * on the parameter with identifier "id"?
1568 isl_bool
FN(PW
,involves_param_id
)(__isl_keep PW
*pw
, __isl_keep isl_id
*id
)
1573 return isl_bool_error
;
1575 return isl_bool_false
;
1577 pos
= FN(PW
,find_dim_by_id
)(pw
, isl_dim_param
, id
);
1579 return isl_bool_false
;
1580 return FN(PW
,involves_dims
)(pw
, isl_dim_param
, pos
, 1);
1583 /* Reset the space of "pw". Since we don't know if the elements
1584 * represent the spaces themselves or their domains, we pass along
1585 * both when we call their reset_space_and_domain.
1587 static __isl_give PW
*FN(PW
,reset_space_and_domain
)(__isl_take PW
*pw
,
1588 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
1593 pw
= FN(PW
,cow
)(pw
);
1594 n
= FN(PW
,n_piece
)(pw
);
1595 if (n
< 0 || !space
|| !domain
)
1598 for (i
= 0; i
< n
; ++i
) {
1601 pw
->p
[i
].set
= isl_set_reset_space(pw
->p
[i
].set
,
1602 isl_space_copy(domain
));
1605 el
= FN(PW
,take_base_at
)(pw
, i
);
1606 el
= FN(EL
,reset_space_and_domain
)(el
,
1607 isl_space_copy(space
), isl_space_copy(domain
));
1608 pw
= FN(PW
,restore_base_at
)(pw
, i
, el
);
1613 isl_space_free(domain
);
1615 pw
= FN(PW
,restore_space
)(pw
, space
);
1619 isl_space_free(domain
);
1620 isl_space_free(space
);
1625 __isl_give PW
*FN(PW
,reset_domain_space
)(__isl_take PW
*pw
,
1626 __isl_take isl_space
*domain
)
1630 space
= isl_space_extend_domain_with_range(isl_space_copy(domain
),
1631 FN(PW
,get_space
)(pw
));
1632 return FN(PW
,reset_space_and_domain
)(pw
, space
, domain
);
1635 __isl_give PW
*FN(PW
,reset_space
)(__isl_take PW
*pw
,
1636 __isl_take isl_space
*space
)
1640 domain
= isl_space_domain(isl_space_copy(space
));
1641 return FN(PW
,reset_space_and_domain
)(pw
, space
, domain
);
1644 __isl_give PW
*FN(PW
,set_tuple_id
)(__isl_take PW
*pw
, enum isl_dim_type type
,
1645 __isl_take isl_id
*id
)
1649 pw
= FN(PW
,cow
)(pw
);
1653 space
= FN(PW
,get_space
)(pw
);
1654 space
= isl_space_set_tuple_id(space
, type
, id
);
1656 return FN(PW
,reset_space
)(pw
, space
);
1659 return FN(PW
,free
)(pw
);
1662 /* Drop the id on the specified tuple.
1664 __isl_give PW
*FN(PW
,reset_tuple_id
)(__isl_take PW
*pw
, enum isl_dim_type type
)
1670 if (!FN(PW
,has_tuple_id
)(pw
, type
))
1673 pw
= FN(PW
,cow
)(pw
);
1677 space
= FN(PW
,get_space
)(pw
);
1678 space
= isl_space_reset_tuple_id(space
, type
);
1680 return FN(PW
,reset_space
)(pw
, space
);
1683 __isl_give PW
*FN(PW
,set_dim_id
)(__isl_take PW
*pw
,
1684 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1688 space
= FN(PW
,get_space
)(pw
);
1689 space
= isl_space_set_dim_id(space
, type
, pos
, id
);
1690 return FN(PW
,reset_space
)(pw
, space
);
1693 /* Reset the user pointer on all identifiers of parameters and tuples
1694 * of the space of "pw".
1696 __isl_give PW
*FN(PW
,reset_user
)(__isl_take PW
*pw
)
1700 space
= FN(PW
,get_space
)(pw
);
1701 space
= isl_space_reset_user(space
);
1703 return FN(PW
,reset_space
)(pw
, space
);
1706 isl_size
FN(PW
,n_piece
)(__isl_keep PW
*pw
)
1708 return pw
? pw
->n
: isl_size_error
;
1711 isl_stat
FN(PW
,foreach_piece
)(__isl_keep PW
*pw
,
1712 isl_stat (*fn
)(__isl_take isl_set
*set
, __isl_take EL
*el
, void *user
),
1718 return isl_stat_error
;
1720 for (i
= 0; i
< pw
->n
; ++i
)
1721 if (fn(isl_set_copy(pw
->p
[i
].set
),
1722 FN(EL
,copy
)(pw
->p
[i
].FIELD
), user
) < 0)
1723 return isl_stat_error
;
1728 /* Does "test" succeed on every cell of "pw"?
1730 isl_bool
FN(PW
,every_piece
)(__isl_keep PW
*pw
,
1731 isl_bool (*test
)(__isl_keep isl_set
*set
,
1732 __isl_keep EL
*el
, void *user
), void *user
)
1737 return isl_bool_error
;
1739 for (i
= 0; i
< pw
->n
; ++i
) {
1742 r
= test(pw
->p
[i
].set
, pw
->p
[i
].FIELD
, user
);
1747 return isl_bool_true
;
1750 /* Is "pw" defined over a single universe domain?
1752 * If the default value of this piecewise type is zero,
1753 * then a "pw" with a zero number of cells is also accepted
1754 * as it represents the default zero value.
1756 isl_bool
FN(FN(PW
,isa
),BASE
)(__isl_keep PW
*pw
)
1760 n
= FN(PW
,n_piece
)(pw
);
1762 return isl_bool_error
;
1763 if (DEFAULT_IS_ZERO
&& n
== 0)
1764 return isl_bool_true
;
1766 return isl_bool_false
;
1767 return isl_set_plain_is_universe(FN(PW
,peek_domain_at
)(pw
, 0));
1770 /* Return a zero base expression in the same space (and of the same type)
1773 static __isl_give EL
*FN(EL
,zero_like_type
)(__isl_take PW
*pw OPT_TYPE_PARAM
)
1777 space
= FN(PW
,get_space
)(pw
);
1779 return FN(EL
,zero_in_space
)(space
OPT_TYPE_ARG(NO_LOC
));
1783 /* Return a zero base expression in the same space as "pw".
1785 static __isl_give EL
*FN(EL
,zero_like
)(__isl_take PW
*pw
)
1787 return FN(EL
,zero_like_type
)(pw
);
1790 /* Return a zero base expression in the same space and of the same type
1793 * Pass along the type as an explicit argument for uniform handling
1794 * in isl_*_zero_like_type.
1796 static __isl_give EL
*FN(EL
,zero_like
)(__isl_take PW
*pw
)
1800 type
= FN(PW
,get_type
)(pw
);
1803 return FN(EL
,zero_like_type
)(pw
, type
);
1810 /* Given that "pw" is defined over a single universe domain,
1811 * return the base expression associated to this domain.
1813 * If the number of cells is zero, then "pw" is of a piecewise type
1814 * with a default zero value and effectively represents zero.
1815 * In this case, create a zero base expression in the same space
1816 * (and with the same type).
1817 * Otherwise, simply extract the associated base expression.
1819 __isl_give EL
*FN(FN(PW
,as
),BASE
)(__isl_take PW
*pw
)
1825 is_total
= FN(FN(PW
,isa
),BASE
)(pw
);
1829 isl_die(FN(PW
,get_ctx
)(pw
), isl_error_invalid
,
1830 "expecting single total function", goto error
);
1831 n
= FN(PW
,n_piece
)(pw
);
1835 return FN(EL
,zero_like
)(pw
);
1836 el
= FN(PW
,take_base_at
)(pw
, 0);
1845 /* Negate the type of "pw".
1847 static __isl_give PW
*FN(PW
,negate_type
)(__isl_take PW
*pw
)
1849 pw
= FN(PW
,cow
)(pw
);
1852 pw
->type
= isl_fold_type_negate(pw
->type
);
1856 /* Negate the type of "pw".
1857 * Since "pw" does not have a type, do nothing.
1859 static __isl_give PW
*FN(PW
,negate_type
)(__isl_take PW
*pw
)
1865 __isl_give PW
*FN(PW
,mul_isl_int
)(__isl_take PW
*pw
, isl_int v
)
1870 if (isl_int_is_one(v
))
1872 if (pw
&& DEFAULT_IS_ZERO
&& isl_int_is_zero(v
)) {
1874 isl_space
*space
= FN(PW
,get_space
)(pw
);
1875 zero
= FN(PW
,ZERO
)(space
OPT_TYPE_ARG(pw
->));
1879 if (isl_int_is_neg(v
))
1880 pw
= FN(PW
,negate_type
)(pw
);
1882 n
= FN(PW
,n_piece
)(pw
);
1884 return FN(PW
,free
)(pw
);
1885 for (i
= 0; i
< n
; ++i
) {
1888 el
= FN(PW
,take_base_at
)(pw
, i
);
1889 el
= FN(EL
,scale
)(el
, v
);
1890 pw
= FN(PW
,restore_base_at
)(pw
, i
, el
);
1896 /* Multiply the pieces of "pw" by "v" and return the result.
1898 __isl_give PW
*FN(PW
,scale_val
)(__isl_take PW
*pw
, __isl_take isl_val
*v
)
1906 if (isl_val_is_one(v
)) {
1910 if (pw
&& DEFAULT_IS_ZERO
&& isl_val_is_zero(v
)) {
1912 isl_space
*space
= FN(PW
,get_space
)(pw
);
1913 zero
= FN(PW
,ZERO
)(space
OPT_TYPE_ARG(pw
->));
1918 if (isl_val_is_neg(v
))
1919 pw
= FN(PW
,negate_type
)(pw
);
1920 n
= FN(PW
,n_piece
)(pw
);
1924 for (i
= 0; i
< n
; ++i
) {
1927 el
= FN(PW
,take_base_at
)(pw
, i
);
1928 el
= FN(EL
,scale_val
)(el
, isl_val_copy(v
));
1929 pw
= FN(PW
,restore_base_at
)(pw
, i
, el
);
1940 /* Divide the pieces of "pw" by "v" and return the result.
1942 __isl_give PW
*FN(PW
,scale_down_val
)(__isl_take PW
*pw
, __isl_take isl_val
*v
)
1950 if (isl_val_is_one(v
)) {
1955 if (!isl_val_is_rat(v
))
1956 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1957 "expecting rational factor", goto error
);
1958 if (isl_val_is_zero(v
))
1959 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1960 "cannot scale down by zero", goto error
);
1962 if (isl_val_is_neg(v
))
1963 pw
= FN(PW
,negate_type
)(pw
);
1964 n
= FN(PW
,n_piece
)(pw
);
1968 for (i
= 0; i
< n
; ++i
) {
1971 el
= FN(PW
,take_base_at
)(pw
, i
);
1972 el
= FN(EL
,scale_down_val
)(el
, isl_val_copy(v
));
1973 pw
= FN(PW
,restore_base_at
)(pw
, i
, el
);
1984 __isl_give PW
*FN(PW
,scale
)(__isl_take PW
*pw
, isl_int v
)
1986 return FN(PW
,mul_isl_int
)(pw
, v
);
1989 /* Apply some normalization to "pw".
1990 * In particular, sort the pieces according to their function value
1991 * expressions, combining pairs of adjacent pieces with
1992 * the same such expression, and then normalize the domains of the pieces.
1994 * We normalize in place, but if anything goes wrong we need
1995 * to return NULL, so we need to make sure we don't change the
1996 * meaning of any possible other copies of "pw".
1998 __isl_give PW
*FN(PW
,normalize
)(__isl_take PW
*pw
)
2003 pw
= FN(PW
,sort_unique
)(pw
);
2006 for (i
= 0; i
< pw
->n
; ++i
) {
2007 set
= isl_set_normalize(isl_set_copy(pw
->p
[i
].set
));
2009 return FN(PW
,free
)(pw
);
2010 isl_set_free(pw
->p
[i
].set
);
2017 /* Is pw1 obviously equal to pw2?
2018 * That is, do they have obviously identical cells and obviously identical
2019 * elements on each cell?
2021 * If "pw1" or "pw2" contain any NaNs, then they are considered
2022 * not to be the same. A NaN is not equal to anything, not even
2025 isl_bool
FN(PW
,plain_is_equal
)(__isl_keep PW
*pw1
, __isl_keep PW
*pw2
)
2028 isl_bool equal
, has_nan
;
2031 return isl_bool_error
;
2033 has_nan
= FN(PW
,involves_nan
)(pw1
);
2034 if (has_nan
>= 0 && !has_nan
)
2035 has_nan
= FN(PW
,involves_nan
)(pw2
);
2036 if (has_nan
< 0 || has_nan
)
2037 return isl_bool_not(has_nan
);
2040 return isl_bool_true
;
2041 equal
= FN(PW
,has_equal_space
)(pw1
, pw2
);
2042 if (equal
< 0 || !equal
)
2045 pw1
= FN(PW
,copy
)(pw1
);
2046 pw2
= FN(PW
,copy
)(pw2
);
2047 pw1
= FN(PW
,normalize
)(pw1
);
2048 pw2
= FN(PW
,normalize
)(pw2
);
2052 equal
= isl_bool_ok(pw1
->n
== pw2
->n
);
2053 for (i
= 0; equal
&& i
< pw1
->n
; ++i
) {
2054 equal
= isl_set_plain_is_equal(pw1
->p
[i
].set
, pw2
->p
[i
].set
);
2059 equal
= FN(EL
,plain_is_equal
)(pw1
->p
[i
].FIELD
, pw2
->p
[i
].FIELD
);
2070 return isl_bool_error
;
2073 /* Does "pw" involve any NaNs?
2075 isl_bool
FN(PW
,involves_nan
)(__isl_keep PW
*pw
)
2080 return isl_bool_error
;
2082 return isl_bool_false
;
2084 for (i
= 0; i
< pw
->n
; ++i
) {
2085 isl_bool has_nan
= FN(EL
,involves_nan
)(pw
->p
[i
].FIELD
);
2086 if (has_nan
< 0 || has_nan
)
2090 return isl_bool_false
;