2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
12 #include <isl_map_private.h>
13 #include <isl_union_map_private.h>
14 #include <isl_polynomial_private.h>
15 #include <isl_point_private.h>
16 #include <isl_space_private.h>
17 #include <isl_lp_private.h>
19 #include <isl_mat_private.h>
20 #include <isl_val_private.h>
21 #include <isl_vec_private.h>
22 #include <isl_config.h>
23 #include <isl/deprecated/polynomial_int.h>
25 enum isl_fold
isl_fold_type_negate(enum isl_fold type
)
36 isl_die(NULL
, isl_error_internal
, "unhandled isl_fold type", abort());
39 static __isl_give isl_qpolynomial_fold
*qpolynomial_fold_alloc(
40 enum isl_fold type
, __isl_take isl_space
*dim
, int n
)
42 isl_qpolynomial_fold
*fold
;
47 isl_assert(dim
->ctx
, n
>= 0, goto error
);
48 fold
= isl_calloc(dim
->ctx
, struct isl_qpolynomial_fold
,
49 sizeof(struct isl_qpolynomial_fold
) +
50 (n
- 1) * sizeof(struct isl_qpolynomial
*));
66 isl_ctx
*isl_qpolynomial_fold_get_ctx(__isl_keep isl_qpolynomial_fold
*fold
)
68 return fold
? fold
->dim
->ctx
: NULL
;
71 __isl_give isl_space
*isl_qpolynomial_fold_get_domain_space(
72 __isl_keep isl_qpolynomial_fold
*fold
)
74 return fold
? isl_space_copy(fold
->dim
) : NULL
;
77 __isl_give isl_space
*isl_qpolynomial_fold_get_space(
78 __isl_keep isl_qpolynomial_fold
*fold
)
83 space
= isl_space_copy(fold
->dim
);
84 space
= isl_space_from_domain(space
);
85 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
89 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_reset_domain_space(
90 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_space
*dim
)
94 fold
= isl_qpolynomial_fold_cow(fold
);
98 for (i
= 0; i
< fold
->n
; ++i
) {
99 fold
->qp
[i
] = isl_qpolynomial_reset_domain_space(fold
->qp
[i
],
100 isl_space_copy(dim
));
105 isl_space_free(fold
->dim
);
110 isl_qpolynomial_fold_free(fold
);
115 /* Reset the space of "fold". This function is called from isl_pw_templ.c
116 * and doesn't know if the space of an element object is represented
117 * directly or through its domain. It therefore passes along both.
119 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_reset_space_and_domain(
120 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_space
*space
,
121 __isl_take isl_space
*domain
)
123 isl_space_free(space
);
124 return isl_qpolynomial_fold_reset_domain_space(fold
, domain
);
127 int isl_qpolynomial_fold_involves_dims(__isl_keep isl_qpolynomial_fold
*fold
,
128 enum isl_dim_type type
, unsigned first
, unsigned n
)
134 if (fold
->n
== 0 || n
== 0)
137 for (i
= 0; i
< fold
->n
; ++i
) {
138 int involves
= isl_qpolynomial_involves_dims(fold
->qp
[i
],
140 if (involves
< 0 || involves
)
146 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_set_dim_name(
147 __isl_take isl_qpolynomial_fold
*fold
,
148 enum isl_dim_type type
, unsigned pos
, const char *s
)
152 fold
= isl_qpolynomial_fold_cow(fold
);
155 fold
->dim
= isl_space_set_dim_name(fold
->dim
, type
, pos
, s
);
159 for (i
= 0; i
< fold
->n
; ++i
) {
160 fold
->qp
[i
] = isl_qpolynomial_set_dim_name(fold
->qp
[i
],
168 isl_qpolynomial_fold_free(fold
);
172 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_drop_dims(
173 __isl_take isl_qpolynomial_fold
*fold
,
174 enum isl_dim_type type
, unsigned first
, unsigned n
)
177 enum isl_dim_type set_type
;
184 set_type
= type
== isl_dim_in
? isl_dim_set
: type
;
186 fold
= isl_qpolynomial_fold_cow(fold
);
189 fold
->dim
= isl_space_drop_dims(fold
->dim
, set_type
, first
, n
);
193 for (i
= 0; i
< fold
->n
; ++i
) {
194 fold
->qp
[i
] = isl_qpolynomial_drop_dims(fold
->qp
[i
],
202 isl_qpolynomial_fold_free(fold
);
206 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_insert_dims(
207 __isl_take isl_qpolynomial_fold
*fold
,
208 enum isl_dim_type type
, unsigned first
, unsigned n
)
214 if (n
== 0 && !isl_space_is_named_or_nested(fold
->dim
, type
))
217 fold
= isl_qpolynomial_fold_cow(fold
);
220 fold
->dim
= isl_space_insert_dims(fold
->dim
, type
, first
, n
);
224 for (i
= 0; i
< fold
->n
; ++i
) {
225 fold
->qp
[i
] = isl_qpolynomial_insert_dims(fold
->qp
[i
],
233 isl_qpolynomial_fold_free(fold
);
237 static int isl_qpolynomial_cst_sign(__isl_keep isl_qpolynomial
*qp
)
239 struct isl_upoly_cst
*cst
;
241 cst
= isl_upoly_as_cst(qp
->upoly
);
245 return isl_int_sgn(cst
->n
) < 0 ? -1 : 1;
248 static int isl_qpolynomial_aff_sign(__isl_keep isl_set
*set
,
249 __isl_keep isl_qpolynomial
*qp
)
251 enum isl_lp_result res
;
256 aff
= isl_qpolynomial_extract_affine(qp
);
262 res
= isl_set_solve_lp(set
, 0, aff
->el
+ 1, aff
->el
[0],
264 if (res
== isl_lp_error
)
266 if (res
== isl_lp_empty
||
267 (res
== isl_lp_ok
&& !isl_int_is_neg(opt
))) {
272 res
= isl_set_solve_lp(set
, 1, aff
->el
+ 1, aff
->el
[0],
274 if (res
== isl_lp_ok
&& !isl_int_is_pos(opt
))
283 /* Determine, if possible, the sign of the quasipolynomial "qp" on
286 * If qp is a constant, then the problem is trivial.
287 * If qp is linear, then we check if the minimum of the corresponding
288 * affine constraint is non-negative or if the maximum is non-positive.
290 * Otherwise, we check if the outermost variable "v" has a lower bound "l"
291 * in "set". If so, we write qp(v,v') as
293 * q(v,v') * (v - l) + r(v')
295 * if q(v,v') and r(v') have the same known sign, then the original
296 * quasipolynomial has the same sign as well.
303 static int isl_qpolynomial_sign(__isl_keep isl_set
*set
,
304 __isl_keep isl_qpolynomial
*qp
)
309 struct isl_upoly_rec
*rec
;
312 enum isl_lp_result res
;
315 is
= isl_qpolynomial_is_cst(qp
, NULL
, NULL
);
319 return isl_qpolynomial_cst_sign(qp
);
321 is
= isl_qpolynomial_is_affine(qp
);
325 return isl_qpolynomial_aff_sign(set
, qp
);
327 if (qp
->div
->n_row
> 0)
330 rec
= isl_upoly_as_rec(qp
->upoly
);
334 d
= isl_space_dim(qp
->dim
, isl_dim_all
);
335 v
= isl_vec_alloc(set
->ctx
, 2 + d
);
339 isl_seq_clr(v
->el
+ 1, 1 + d
);
340 isl_int_set_si(v
->el
[0], 1);
341 isl_int_set_si(v
->el
[2 + qp
->upoly
->var
], 1);
345 res
= isl_set_solve_lp(set
, 0, v
->el
+ 1, v
->el
[0], &l
, NULL
, NULL
);
346 if (res
== isl_lp_ok
) {
347 isl_qpolynomial
*min
;
348 isl_qpolynomial
*base
;
349 isl_qpolynomial
*r
, *q
;
352 min
= isl_qpolynomial_cst_on_domain(isl_space_copy(qp
->dim
), l
);
353 base
= isl_qpolynomial_var_pow_on_domain(isl_space_copy(qp
->dim
),
356 r
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), 0,
357 isl_upoly_copy(rec
->p
[rec
->n
- 1]));
358 q
= isl_qpolynomial_copy(r
);
360 for (i
= rec
->n
- 2; i
>= 0; --i
) {
361 r
= isl_qpolynomial_mul(r
, isl_qpolynomial_copy(min
));
362 t
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), 0,
363 isl_upoly_copy(rec
->p
[i
]));
364 r
= isl_qpolynomial_add(r
, t
);
367 q
= isl_qpolynomial_mul(q
, isl_qpolynomial_copy(base
));
368 q
= isl_qpolynomial_add(q
, isl_qpolynomial_copy(r
));
371 if (isl_qpolynomial_is_zero(q
))
372 sgn
= isl_qpolynomial_sign(set
, r
);
373 else if (isl_qpolynomial_is_zero(r
))
374 sgn
= isl_qpolynomial_sign(set
, q
);
377 sgn_r
= isl_qpolynomial_sign(set
, r
);
378 sgn_q
= isl_qpolynomial_sign(set
, q
);
383 isl_qpolynomial_free(min
);
384 isl_qpolynomial_free(base
);
385 isl_qpolynomial_free(q
);
386 isl_qpolynomial_free(r
);
396 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_fold_on_domain(
397 __isl_keep isl_set
*set
,
398 __isl_take isl_qpolynomial_fold
*fold1
,
399 __isl_take isl_qpolynomial_fold
*fold2
)
403 struct isl_qpolynomial_fold
*res
= NULL
;
406 if (!fold1
|| !fold2
)
409 isl_assert(fold1
->dim
->ctx
, fold1
->type
== fold2
->type
, goto error
);
410 isl_assert(fold1
->dim
->ctx
, isl_space_is_equal(fold1
->dim
, fold2
->dim
),
413 better
= fold1
->type
== isl_fold_max
? -1 : 1;
415 if (isl_qpolynomial_fold_is_empty(fold1
)) {
416 isl_qpolynomial_fold_free(fold1
);
420 if (isl_qpolynomial_fold_is_empty(fold2
)) {
421 isl_qpolynomial_fold_free(fold2
);
425 res
= qpolynomial_fold_alloc(fold1
->type
, isl_space_copy(fold1
->dim
),
426 fold1
->n
+ fold2
->n
);
430 for (i
= 0; i
< fold1
->n
; ++i
) {
431 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold1
->qp
[i
]);
432 if (!res
->qp
[res
->n
])
438 for (i
= 0; i
< fold2
->n
; ++i
) {
439 for (j
= n1
- 1; j
>= 0; --j
) {
442 d
= isl_qpolynomial_sub(
443 isl_qpolynomial_copy(res
->qp
[j
]),
444 isl_qpolynomial_copy(fold2
->qp
[i
]));
445 sgn
= isl_qpolynomial_sign(set
, d
);
446 isl_qpolynomial_free(d
);
451 isl_qpolynomial_free(res
->qp
[j
]);
453 res
->qp
[j
] = res
->qp
[n1
- 1];
455 if (n1
!= res
->n
- 1)
456 res
->qp
[n1
] = res
->qp
[res
->n
- 1];
461 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold2
->qp
[i
]);
462 if (!res
->qp
[res
->n
])
467 isl_qpolynomial_fold_free(fold1
);
468 isl_qpolynomial_fold_free(fold2
);
472 isl_qpolynomial_fold_free(res
);
473 isl_qpolynomial_fold_free(fold1
);
474 isl_qpolynomial_fold_free(fold2
);
478 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_add_qpolynomial(
479 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_qpolynomial
*qp
)
486 if (isl_qpolynomial_is_zero(qp
)) {
487 isl_qpolynomial_free(qp
);
491 fold
= isl_qpolynomial_fold_cow(fold
);
495 for (i
= 0; i
< fold
->n
; ++i
) {
496 fold
->qp
[i
] = isl_qpolynomial_add(fold
->qp
[i
],
497 isl_qpolynomial_copy(qp
));
502 isl_qpolynomial_free(qp
);
505 isl_qpolynomial_fold_free(fold
);
506 isl_qpolynomial_free(qp
);
510 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_add_on_domain(
511 __isl_keep isl_set
*dom
,
512 __isl_take isl_qpolynomial_fold
*fold1
,
513 __isl_take isl_qpolynomial_fold
*fold2
)
516 isl_qpolynomial_fold
*res
= NULL
;
518 if (!fold1
|| !fold2
)
521 if (isl_qpolynomial_fold_is_empty(fold1
)) {
522 isl_qpolynomial_fold_free(fold1
);
526 if (isl_qpolynomial_fold_is_empty(fold2
)) {
527 isl_qpolynomial_fold_free(fold2
);
531 if (fold1
->n
== 1 && fold2
->n
!= 1)
532 return isl_qpolynomial_fold_add_on_domain(dom
, fold2
, fold1
);
535 res
= isl_qpolynomial_fold_add_qpolynomial(fold1
,
536 isl_qpolynomial_copy(fold2
->qp
[0]));
537 isl_qpolynomial_fold_free(fold2
);
541 res
= isl_qpolynomial_fold_add_qpolynomial(
542 isl_qpolynomial_fold_copy(fold1
),
543 isl_qpolynomial_copy(fold2
->qp
[0]));
545 for (i
= 1; i
< fold2
->n
; ++i
) {
546 isl_qpolynomial_fold
*res_i
;
547 res_i
= isl_qpolynomial_fold_add_qpolynomial(
548 isl_qpolynomial_fold_copy(fold1
),
549 isl_qpolynomial_copy(fold2
->qp
[i
]));
550 res
= isl_qpolynomial_fold_fold_on_domain(dom
, res
, res_i
);
553 isl_qpolynomial_fold_free(fold1
);
554 isl_qpolynomial_fold_free(fold2
);
557 isl_qpolynomial_fold_free(res
);
558 isl_qpolynomial_fold_free(fold1
);
559 isl_qpolynomial_fold_free(fold2
);
563 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_substitute_equalities(
564 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_basic_set
*eq
)
571 fold
= isl_qpolynomial_fold_cow(fold
);
575 for (i
= 0; i
< fold
->n
; ++i
) {
576 fold
->qp
[i
] = isl_qpolynomial_substitute_equalities(fold
->qp
[i
],
577 isl_basic_set_copy(eq
));
582 isl_basic_set_free(eq
);
585 isl_basic_set_free(eq
);
586 isl_qpolynomial_fold_free(fold
);
590 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_gist(
591 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_set
*context
)
595 if (!fold
|| !context
)
598 fold
= isl_qpolynomial_fold_cow(fold
);
602 for (i
= 0; i
< fold
->n
; ++i
) {
603 fold
->qp
[i
] = isl_qpolynomial_gist(fold
->qp
[i
],
604 isl_set_copy(context
));
609 isl_set_free(context
);
612 isl_set_free(context
);
613 isl_qpolynomial_fold_free(fold
);
617 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_gist_params(
618 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_set
*context
)
620 isl_space
*space
= isl_qpolynomial_fold_get_domain_space(fold
);
621 isl_set
*dom_context
= isl_set_universe(space
);
622 dom_context
= isl_set_intersect_params(dom_context
, context
);
623 return isl_qpolynomial_fold_gist(fold
, dom_context
);
629 #define PW isl_pw_qpolynomial_fold
631 #define EL isl_qpolynomial_fold
633 #define EL_IS_ZERO is_empty
637 #define IS_ZERO is_zero
640 #undef DEFAULT_IS_ZERO
641 #define DEFAULT_IS_ZERO 1
647 #include <isl_pw_templ.c>
650 #define UNION isl_union_pw_qpolynomial_fold
652 #define PART isl_pw_qpolynomial_fold
654 #define PARTS pw_qpolynomial_fold
659 #include <isl_union_templ.c>
661 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_empty(enum isl_fold type
,
662 __isl_take isl_space
*dim
)
664 return qpolynomial_fold_alloc(type
, dim
, 0);
667 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_alloc(
668 enum isl_fold type
, __isl_take isl_qpolynomial
*qp
)
670 isl_qpolynomial_fold
*fold
;
675 fold
= qpolynomial_fold_alloc(type
, isl_space_copy(qp
->dim
), 1);
684 isl_qpolynomial_fold_free(fold
);
685 isl_qpolynomial_free(qp
);
689 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_copy(
690 __isl_keep isl_qpolynomial_fold
*fold
)
699 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_dup(
700 __isl_keep isl_qpolynomial_fold
*fold
)
703 isl_qpolynomial_fold
*dup
;
707 dup
= qpolynomial_fold_alloc(fold
->type
,
708 isl_space_copy(fold
->dim
), fold
->n
);
713 for (i
= 0; i
< fold
->n
; ++i
) {
714 dup
->qp
[i
] = isl_qpolynomial_copy(fold
->qp
[i
]);
721 isl_qpolynomial_fold_free(dup
);
725 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_cow(
726 __isl_take isl_qpolynomial_fold
*fold
)
734 return isl_qpolynomial_fold_dup(fold
);
737 void isl_qpolynomial_fold_free(__isl_take isl_qpolynomial_fold
*fold
)
746 for (i
= 0; i
< fold
->n
; ++i
)
747 isl_qpolynomial_free(fold
->qp
[i
]);
748 isl_space_free(fold
->dim
);
752 int isl_qpolynomial_fold_is_empty(__isl_keep isl_qpolynomial_fold
*fold
)
760 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_fold(
761 __isl_take isl_qpolynomial_fold
*fold1
,
762 __isl_take isl_qpolynomial_fold
*fold2
)
765 struct isl_qpolynomial_fold
*res
= NULL
;
767 if (!fold1
|| !fold2
)
770 isl_assert(fold1
->dim
->ctx
, fold1
->type
== fold2
->type
, goto error
);
771 isl_assert(fold1
->dim
->ctx
, isl_space_is_equal(fold1
->dim
, fold2
->dim
),
774 if (isl_qpolynomial_fold_is_empty(fold1
)) {
775 isl_qpolynomial_fold_free(fold1
);
779 if (isl_qpolynomial_fold_is_empty(fold2
)) {
780 isl_qpolynomial_fold_free(fold2
);
784 res
= qpolynomial_fold_alloc(fold1
->type
, isl_space_copy(fold1
->dim
),
785 fold1
->n
+ fold2
->n
);
789 for (i
= 0; i
< fold1
->n
; ++i
) {
790 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold1
->qp
[i
]);
791 if (!res
->qp
[res
->n
])
796 for (i
= 0; i
< fold2
->n
; ++i
) {
797 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold2
->qp
[i
]);
798 if (!res
->qp
[res
->n
])
803 isl_qpolynomial_fold_free(fold1
);
804 isl_qpolynomial_fold_free(fold2
);
808 isl_qpolynomial_fold_free(res
);
809 isl_qpolynomial_fold_free(fold1
);
810 isl_qpolynomial_fold_free(fold2
);
814 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_fold_fold(
815 __isl_take isl_pw_qpolynomial_fold
*pw1
,
816 __isl_take isl_pw_qpolynomial_fold
*pw2
)
819 struct isl_pw_qpolynomial_fold
*res
;
825 isl_assert(pw1
->dim
->ctx
, isl_space_is_equal(pw1
->dim
, pw2
->dim
), goto error
);
827 if (isl_pw_qpolynomial_fold_is_zero(pw1
)) {
828 isl_pw_qpolynomial_fold_free(pw1
);
832 if (isl_pw_qpolynomial_fold_is_zero(pw2
)) {
833 isl_pw_qpolynomial_fold_free(pw2
);
837 if (pw1
->type
!= pw2
->type
)
838 isl_die(pw1
->dim
->ctx
, isl_error_invalid
,
839 "fold types don't match", goto error
);
841 n
= (pw1
->n
+ 1) * (pw2
->n
+ 1);
842 res
= isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pw1
->dim
),
845 for (i
= 0; i
< pw1
->n
; ++i
) {
846 set
= isl_set_copy(pw1
->p
[i
].set
);
847 for (j
= 0; j
< pw2
->n
; ++j
) {
848 struct isl_set
*common
;
849 isl_qpolynomial_fold
*sum
;
850 set
= isl_set_subtract(set
,
851 isl_set_copy(pw2
->p
[j
].set
));
852 common
= isl_set_intersect(isl_set_copy(pw1
->p
[i
].set
),
853 isl_set_copy(pw2
->p
[j
].set
));
854 if (isl_set_plain_is_empty(common
)) {
855 isl_set_free(common
);
859 sum
= isl_qpolynomial_fold_fold_on_domain(common
,
860 isl_qpolynomial_fold_copy(pw1
->p
[i
].fold
),
861 isl_qpolynomial_fold_copy(pw2
->p
[j
].fold
));
863 res
= isl_pw_qpolynomial_fold_add_piece(res
, common
, sum
);
865 res
= isl_pw_qpolynomial_fold_add_piece(res
, set
,
866 isl_qpolynomial_fold_copy(pw1
->p
[i
].fold
));
869 for (j
= 0; j
< pw2
->n
; ++j
) {
870 set
= isl_set_copy(pw2
->p
[j
].set
);
871 for (i
= 0; i
< pw1
->n
; ++i
)
872 set
= isl_set_subtract(set
, isl_set_copy(pw1
->p
[i
].set
));
873 res
= isl_pw_qpolynomial_fold_add_piece(res
, set
,
874 isl_qpolynomial_fold_copy(pw2
->p
[j
].fold
));
877 isl_pw_qpolynomial_fold_free(pw1
);
878 isl_pw_qpolynomial_fold_free(pw2
);
882 isl_pw_qpolynomial_fold_free(pw1
);
883 isl_pw_qpolynomial_fold_free(pw2
);
887 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
888 __isl_take isl_union_pw_qpolynomial_fold
*u
,
889 __isl_take isl_pw_qpolynomial_fold
*part
)
892 struct isl_hash_table_entry
*entry
;
894 u
= isl_union_pw_qpolynomial_fold_cow(u
);
899 isl_assert(u
->space
->ctx
,
900 isl_space_match(part
->dim
, isl_dim_param
, u
->space
, isl_dim_param
),
903 hash
= isl_space_get_hash(part
->dim
);
904 entry
= isl_hash_table_find(u
->space
->ctx
, &u
->table
, hash
,
905 &isl_union_pw_qpolynomial_fold_has_same_domain_space
,
913 entry
->data
= isl_pw_qpolynomial_fold_fold(entry
->data
,
914 isl_pw_qpolynomial_fold_copy(part
));
917 isl_pw_qpolynomial_fold_free(part
);
922 isl_pw_qpolynomial_fold_free(part
);
923 isl_union_pw_qpolynomial_fold_free(u
);
927 static int fold_part(__isl_take isl_pw_qpolynomial_fold
*part
, void *user
)
929 isl_union_pw_qpolynomial_fold
**u
;
930 u
= (isl_union_pw_qpolynomial_fold
**)user
;
932 *u
= isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(*u
, part
);
937 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_pw_qpolynomial_fold_fold(
938 __isl_take isl_union_pw_qpolynomial_fold
*u1
,
939 __isl_take isl_union_pw_qpolynomial_fold
*u2
)
941 u1
= isl_union_pw_qpolynomial_fold_cow(u1
);
946 if (isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(u2
,
947 &fold_part
, &u1
) < 0)
950 isl_union_pw_qpolynomial_fold_free(u2
);
954 isl_union_pw_qpolynomial_fold_free(u1
);
955 isl_union_pw_qpolynomial_fold_free(u2
);
959 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_fold_from_pw_qpolynomial(
960 enum isl_fold type
, __isl_take isl_pw_qpolynomial
*pwqp
)
963 isl_pw_qpolynomial_fold
*pwf
;
968 pwf
= isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pwqp
->dim
),
971 for (i
= 0; i
< pwqp
->n
; ++i
)
972 pwf
= isl_pw_qpolynomial_fold_add_piece(pwf
,
973 isl_set_copy(pwqp
->p
[i
].set
),
974 isl_qpolynomial_fold_alloc(type
,
975 isl_qpolynomial_copy(pwqp
->p
[i
].qp
)));
977 isl_pw_qpolynomial_free(pwqp
);
982 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_fold_add(
983 __isl_take isl_pw_qpolynomial_fold
*pwf1
,
984 __isl_take isl_pw_qpolynomial_fold
*pwf2
)
986 return isl_pw_qpolynomial_fold_union_add_(pwf1
, pwf2
);
989 int isl_qpolynomial_fold_plain_is_equal(__isl_keep isl_qpolynomial_fold
*fold1
,
990 __isl_keep isl_qpolynomial_fold
*fold2
)
994 if (!fold1
|| !fold2
)
997 if (fold1
->n
!= fold2
->n
)
1000 /* We probably want to sort the qps first... */
1001 for (i
= 0; i
< fold1
->n
; ++i
) {
1002 int eq
= isl_qpolynomial_plain_is_equal(fold1
->qp
[i
], fold2
->qp
[i
]);
1010 __isl_give isl_val
*isl_qpolynomial_fold_eval(
1011 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_point
*pnt
)
1018 ctx
= isl_point_get_ctx(pnt
);
1019 isl_assert(pnt
->dim
->ctx
, isl_space_is_equal(pnt
->dim
, fold
->dim
), goto error
);
1020 isl_assert(pnt
->dim
->ctx
,
1021 fold
->type
== isl_fold_max
|| fold
->type
== isl_fold_min
,
1025 v
= isl_val_zero(ctx
);
1028 v
= isl_qpolynomial_eval(isl_qpolynomial_copy(fold
->qp
[0]),
1029 isl_point_copy(pnt
));
1030 for (i
= 1; i
< fold
->n
; ++i
) {
1032 v_i
= isl_qpolynomial_eval(
1033 isl_qpolynomial_copy(fold
->qp
[i
]),
1034 isl_point_copy(pnt
));
1035 if (fold
->type
== isl_fold_max
)
1036 v
= isl_val_max(v
, v_i
);
1038 v
= isl_val_min(v
, v_i
);
1041 isl_qpolynomial_fold_free(fold
);
1042 isl_point_free(pnt
);
1046 isl_qpolynomial_fold_free(fold
);
1047 isl_point_free(pnt
);
1051 size_t isl_pw_qpolynomial_fold_size(__isl_keep isl_pw_qpolynomial_fold
*pwf
)
1056 for (i
= 0; i
< pwf
->n
; ++i
)
1057 n
+= pwf
->p
[i
].fold
->n
;
1062 __isl_give isl_val
*isl_qpolynomial_fold_opt_on_domain(
1063 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_set
*set
, int max
)
1072 opt
= isl_val_zero(isl_set_get_ctx(set
));
1074 isl_qpolynomial_fold_free(fold
);
1078 opt
= isl_qpolynomial_opt_on_domain(isl_qpolynomial_copy(fold
->qp
[0]),
1079 isl_set_copy(set
), max
);
1080 for (i
= 1; i
< fold
->n
; ++i
) {
1082 opt_i
= isl_qpolynomial_opt_on_domain(
1083 isl_qpolynomial_copy(fold
->qp
[i
]),
1084 isl_set_copy(set
), max
);
1086 opt
= isl_val_max(opt
, opt_i
);
1088 opt
= isl_val_min(opt
, opt_i
);
1092 isl_qpolynomial_fold_free(fold
);
1097 isl_qpolynomial_fold_free(fold
);
1101 /* Check whether for each quasi-polynomial in "fold2" there is
1102 * a quasi-polynomial in "fold1" that dominates it on "set".
1104 static int qpolynomial_fold_covers_on_domain(__isl_keep isl_set
*set
,
1105 __isl_keep isl_qpolynomial_fold
*fold1
,
1106 __isl_keep isl_qpolynomial_fold
*fold2
)
1111 if (!set
|| !fold1
|| !fold2
)
1114 covers
= fold1
->type
== isl_fold_max
? 1 : -1;
1116 for (i
= 0; i
< fold2
->n
; ++i
) {
1117 for (j
= 0; j
< fold1
->n
; ++j
) {
1121 d
= isl_qpolynomial_sub(
1122 isl_qpolynomial_copy(fold1
->qp
[j
]),
1123 isl_qpolynomial_copy(fold2
->qp
[i
]));
1124 sgn
= isl_qpolynomial_sign(set
, d
);
1125 isl_qpolynomial_free(d
);
1136 /* Check whether "pwf1" dominated "pwf2", i.e., the domain of "pwf1" contains
1137 * that of "pwf2" and on each cell, the corresponding fold from pwf1 dominates
1140 int isl_pw_qpolynomial_fold_covers(__isl_keep isl_pw_qpolynomial_fold
*pwf1
,
1141 __isl_keep isl_pw_qpolynomial_fold
*pwf2
)
1144 isl_set
*dom1
, *dom2
;
1155 dom1
= isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf1
));
1156 dom2
= isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf2
));
1157 is_subset
= isl_set_is_subset(dom2
, dom1
);
1161 if (is_subset
< 0 || !is_subset
)
1164 for (i
= 0; i
< pwf2
->n
; ++i
) {
1165 for (j
= 0; j
< pwf1
->n
; ++j
) {
1170 common
= isl_set_intersect(isl_set_copy(pwf1
->p
[j
].set
),
1171 isl_set_copy(pwf2
->p
[i
].set
));
1172 is_empty
= isl_set_is_empty(common
);
1173 if (is_empty
< 0 || is_empty
) {
1174 isl_set_free(common
);
1179 covers
= qpolynomial_fold_covers_on_domain(common
,
1180 pwf1
->p
[j
].fold
, pwf2
->p
[i
].fold
);
1181 isl_set_free(common
);
1182 if (covers
< 0 || !covers
)
1190 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_morph_domain(
1191 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_morph
*morph
)
1196 if (!fold
|| !morph
)
1199 ctx
= fold
->dim
->ctx
;
1200 isl_assert(ctx
, isl_space_is_equal(fold
->dim
, morph
->dom
->dim
), goto error
);
1202 fold
= isl_qpolynomial_fold_cow(fold
);
1206 isl_space_free(fold
->dim
);
1207 fold
->dim
= isl_space_copy(morph
->ran
->dim
);
1211 for (i
= 0; i
< fold
->n
; ++i
) {
1212 fold
->qp
[i
] = isl_qpolynomial_morph_domain(fold
->qp
[i
],
1213 isl_morph_copy(morph
));
1218 isl_morph_free(morph
);
1222 isl_qpolynomial_fold_free(fold
);
1223 isl_morph_free(morph
);
1227 enum isl_fold
isl_qpolynomial_fold_get_type(__isl_keep isl_qpolynomial_fold
*fold
)
1230 return isl_fold_list
;
1234 enum isl_fold
isl_union_pw_qpolynomial_fold_get_type(
1235 __isl_keep isl_union_pw_qpolynomial_fold
*upwf
)
1238 return isl_fold_list
;
1242 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_lift(
1243 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_space
*dim
)
1250 if (isl_space_is_equal(fold
->dim
, dim
)) {
1251 isl_space_free(dim
);
1255 fold
= isl_qpolynomial_fold_cow(fold
);
1259 isl_space_free(fold
->dim
);
1260 fold
->dim
= isl_space_copy(dim
);
1264 for (i
= 0; i
< fold
->n
; ++i
) {
1265 fold
->qp
[i
] = isl_qpolynomial_lift(fold
->qp
[i
],
1266 isl_space_copy(dim
));
1271 isl_space_free(dim
);
1275 isl_qpolynomial_fold_free(fold
);
1276 isl_space_free(dim
);
1280 int isl_qpolynomial_fold_foreach_qpolynomial(
1281 __isl_keep isl_qpolynomial_fold
*fold
,
1282 int (*fn
)(__isl_take isl_qpolynomial
*qp
, void *user
), void *user
)
1289 for (i
= 0; i
< fold
->n
; ++i
)
1290 if (fn(isl_qpolynomial_copy(fold
->qp
[i
]), user
) < 0)
1296 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_move_dims(
1297 __isl_take isl_qpolynomial_fold
*fold
,
1298 enum isl_dim_type dst_type
, unsigned dst_pos
,
1299 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1306 fold
= isl_qpolynomial_fold_cow(fold
);
1310 fold
->dim
= isl_space_move_dims(fold
->dim
, dst_type
, dst_pos
,
1311 src_type
, src_pos
, n
);
1315 for (i
= 0; i
< fold
->n
; ++i
) {
1316 fold
->qp
[i
] = isl_qpolynomial_move_dims(fold
->qp
[i
],
1317 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1324 isl_qpolynomial_fold_free(fold
);
1328 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
1329 * in fold->qp[k] by subs[i].
1331 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_substitute(
1332 __isl_take isl_qpolynomial_fold
*fold
,
1333 enum isl_dim_type type
, unsigned first
, unsigned n
,
1334 __isl_keep isl_qpolynomial
**subs
)
1341 fold
= isl_qpolynomial_fold_cow(fold
);
1345 for (i
= 0; i
< fold
->n
; ++i
) {
1346 fold
->qp
[i
] = isl_qpolynomial_substitute(fold
->qp
[i
],
1347 type
, first
, n
, subs
);
1354 isl_qpolynomial_fold_free(fold
);
1358 static int add_pwqp(__isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
1361 isl_pw_qpolynomial_fold
*pwf
;
1362 isl_union_pw_qpolynomial_fold
**upwf
;
1364 struct isl_hash_table_entry
*entry
;
1366 upwf
= (isl_union_pw_qpolynomial_fold
**)user
;
1368 ctx
= pwqp
->dim
->ctx
;
1369 hash
= isl_space_get_hash(pwqp
->dim
);
1370 entry
= isl_hash_table_find(ctx
, &(*upwf
)->table
, hash
,
1371 &isl_union_pw_qpolynomial_fold_has_same_domain_space
,
1376 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial((*upwf
)->type
, pwqp
);
1380 entry
->data
= isl_pw_qpolynomial_fold_add(entry
->data
, pwf
);
1383 if (isl_pw_qpolynomial_fold_is_zero(entry
->data
)) {
1384 isl_pw_qpolynomial_fold_free(entry
->data
);
1385 isl_hash_table_remove(ctx
, &(*upwf
)->table
, entry
);
1391 isl_pw_qpolynomial_free(pwqp
);
1395 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(
1396 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
1397 __isl_take isl_union_pw_qpolynomial
*upwqp
)
1399 upwf
= isl_union_pw_qpolynomial_fold_align_params(upwf
,
1400 isl_union_pw_qpolynomial_get_space(upwqp
));
1401 upwqp
= isl_union_pw_qpolynomial_align_params(upwqp
,
1402 isl_union_pw_qpolynomial_fold_get_space(upwf
));
1404 upwf
= isl_union_pw_qpolynomial_fold_cow(upwf
);
1405 if (!upwf
|| !upwqp
)
1408 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp
, &add_pwqp
,
1412 isl_union_pw_qpolynomial_free(upwqp
);
1416 isl_union_pw_qpolynomial_fold_free(upwf
);
1417 isl_union_pw_qpolynomial_free(upwqp
);
1421 static int join_compatible(__isl_keep isl_space
*dim1
, __isl_keep isl_space
*dim2
)
1424 m
= isl_space_match(dim1
, isl_dim_param
, dim2
, isl_dim_param
);
1427 return isl_space_tuple_is_equal(dim1
, isl_dim_out
, dim2
, isl_dim_in
);
1430 /* Compute the intersection of the range of the map and the domain
1431 * of the piecewise quasipolynomial reduction and then compute a bound
1432 * on the associated quasipolynomial reduction over all elements
1433 * in this intersection.
1435 * We first introduce some unconstrained dimensions in the
1436 * piecewise quasipolynomial, intersect the resulting domain
1437 * with the wrapped map and the compute the sum.
1439 __isl_give isl_pw_qpolynomial_fold
*isl_map_apply_pw_qpolynomial_fold(
1440 __isl_take isl_map
*map
, __isl_take isl_pw_qpolynomial_fold
*pwf
,
1450 ctx
= isl_map_get_ctx(map
);
1454 map_dim
= isl_map_get_space(map
);
1455 pwf_dim
= isl_pw_qpolynomial_fold_get_space(pwf
);
1456 ok
= join_compatible(map_dim
, pwf_dim
);
1457 isl_space_free(map_dim
);
1458 isl_space_free(pwf_dim
);
1460 isl_die(ctx
, isl_error_invalid
, "incompatible dimensions",
1463 n_in
= isl_map_dim(map
, isl_dim_in
);
1464 pwf
= isl_pw_qpolynomial_fold_insert_dims(pwf
, isl_dim_in
, 0, n_in
);
1466 dom
= isl_map_wrap(map
);
1467 pwf
= isl_pw_qpolynomial_fold_reset_domain_space(pwf
,
1468 isl_set_get_space(dom
));
1470 pwf
= isl_pw_qpolynomial_fold_intersect_domain(pwf
, dom
);
1471 pwf
= isl_pw_qpolynomial_fold_bound(pwf
, tight
);
1476 isl_pw_qpolynomial_fold_free(pwf
);
1480 __isl_give isl_pw_qpolynomial_fold
*isl_set_apply_pw_qpolynomial_fold(
1481 __isl_take isl_set
*set
, __isl_take isl_pw_qpolynomial_fold
*pwf
,
1484 return isl_map_apply_pw_qpolynomial_fold(set
, pwf
, tight
);
1487 struct isl_apply_fold_data
{
1488 isl_union_pw_qpolynomial_fold
*upwf
;
1489 isl_union_pw_qpolynomial_fold
*res
;
1494 static int pw_qpolynomial_fold_apply(__isl_take isl_pw_qpolynomial_fold
*pwf
,
1499 struct isl_apply_fold_data
*data
= user
;
1502 map_dim
= isl_map_get_space(data
->map
);
1503 pwf_dim
= isl_pw_qpolynomial_fold_get_space(pwf
);
1504 ok
= join_compatible(map_dim
, pwf_dim
);
1505 isl_space_free(map_dim
);
1506 isl_space_free(pwf_dim
);
1509 pwf
= isl_map_apply_pw_qpolynomial_fold(isl_map_copy(data
->map
),
1510 pwf
, data
->tight
? &data
->tight
: NULL
);
1511 data
->res
= isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
1514 isl_pw_qpolynomial_fold_free(pwf
);
1519 static int map_apply(__isl_take isl_map
*map
, void *user
)
1521 struct isl_apply_fold_data
*data
= user
;
1525 r
= isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(
1526 data
->upwf
, &pw_qpolynomial_fold_apply
, data
);
1532 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_map_apply_union_pw_qpolynomial_fold(
1533 __isl_take isl_union_map
*umap
,
1534 __isl_take isl_union_pw_qpolynomial_fold
*upwf
, int *tight
)
1538 struct isl_apply_fold_data data
;
1540 upwf
= isl_union_pw_qpolynomial_fold_align_params(upwf
,
1541 isl_union_map_get_space(umap
));
1542 umap
= isl_union_map_align_params(umap
,
1543 isl_union_pw_qpolynomial_fold_get_space(upwf
));
1546 data
.tight
= tight
? 1 : 0;
1547 dim
= isl_union_pw_qpolynomial_fold_get_space(upwf
);
1548 type
= isl_union_pw_qpolynomial_fold_get_type(upwf
);
1549 data
.res
= isl_union_pw_qpolynomial_fold_zero(dim
, type
);
1550 if (isl_union_map_foreach_map(umap
, &map_apply
, &data
) < 0)
1553 isl_union_map_free(umap
);
1554 isl_union_pw_qpolynomial_fold_free(upwf
);
1557 *tight
= data
.tight
;
1561 isl_union_map_free(umap
);
1562 isl_union_pw_qpolynomial_fold_free(upwf
);
1563 isl_union_pw_qpolynomial_fold_free(data
.res
);
1567 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_set_apply_union_pw_qpolynomial_fold(
1568 __isl_take isl_union_set
*uset
,
1569 __isl_take isl_union_pw_qpolynomial_fold
*upwf
, int *tight
)
1571 return isl_union_map_apply_union_pw_qpolynomial_fold(uset
, upwf
, tight
);
1574 /* Reorder the dimension of "fold" according to the given reordering.
1576 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_realign_domain(
1577 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_reordering
*r
)
1581 fold
= isl_qpolynomial_fold_cow(fold
);
1585 for (i
= 0; i
< fold
->n
; ++i
) {
1586 fold
->qp
[i
] = isl_qpolynomial_realign_domain(fold
->qp
[i
],
1587 isl_reordering_copy(r
));
1592 fold
= isl_qpolynomial_fold_reset_domain_space(fold
,
1593 isl_space_copy(r
->dim
));
1595 isl_reordering_free(r
);
1599 isl_qpolynomial_fold_free(fold
);
1600 isl_reordering_free(r
);
1604 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_mul_isl_int(
1605 __isl_take isl_qpolynomial_fold
*fold
, isl_int v
)
1609 if (isl_int_is_one(v
))
1611 if (fold
&& isl_int_is_zero(v
)) {
1612 isl_qpolynomial_fold
*zero
;
1613 isl_space
*dim
= isl_space_copy(fold
->dim
);
1614 zero
= isl_qpolynomial_fold_empty(fold
->type
, dim
);
1615 isl_qpolynomial_fold_free(fold
);
1619 fold
= isl_qpolynomial_fold_cow(fold
);
1623 if (isl_int_is_neg(v
))
1624 fold
->type
= isl_fold_type_negate(fold
->type
);
1625 for (i
= 0; i
< fold
->n
; ++i
) {
1626 fold
->qp
[i
] = isl_qpolynomial_mul_isl_int(fold
->qp
[i
], v
);
1633 isl_qpolynomial_fold_free(fold
);
1637 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_scale(
1638 __isl_take isl_qpolynomial_fold
*fold
, isl_int v
)
1640 return isl_qpolynomial_fold_mul_isl_int(fold
, v
);
1643 /* Multiply "fold" by "v".
1645 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_scale_val(
1646 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_val
*v
)
1653 if (isl_val_is_one(v
)) {
1657 if (isl_val_is_zero(v
)) {
1658 isl_qpolynomial_fold
*zero
;
1659 isl_space
*space
= isl_qpolynomial_fold_get_domain_space(fold
);
1660 zero
= isl_qpolynomial_fold_empty(fold
->type
, space
);
1661 isl_qpolynomial_fold_free(fold
);
1665 if (!isl_val_is_rat(v
))
1666 isl_die(isl_qpolynomial_fold_get_ctx(fold
), isl_error_invalid
,
1667 "expecting rational factor", goto error
);
1669 fold
= isl_qpolynomial_fold_cow(fold
);
1673 if (isl_val_is_neg(v
))
1674 fold
->type
= isl_fold_type_negate(fold
->type
);
1675 for (i
= 0; i
< fold
->n
; ++i
) {
1676 fold
->qp
[i
] = isl_qpolynomial_scale_val(fold
->qp
[i
],
1686 isl_qpolynomial_fold_free(fold
);
1690 /* Divide "fold" by "v".
1692 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_scale_down_val(
1693 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_val
*v
)
1698 if (isl_val_is_one(v
)) {
1702 if (!isl_val_is_rat(v
))
1703 isl_die(isl_qpolynomial_fold_get_ctx(fold
), isl_error_invalid
,
1704 "expecting rational factor", goto error
);
1705 if (isl_val_is_zero(v
))
1706 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1707 "cannot scale down by zero", goto error
);
1709 return isl_qpolynomial_fold_scale_val(fold
, isl_val_inv(v
));
1712 isl_qpolynomial_fold_free(fold
);