2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 #include <isl_polynomial_private.h>
12 #include <isl_point_private.h>
14 #include <isl_map_private.h>
18 static __isl_give isl_qpolynomial_fold
*qpolynomial_fold_alloc(
19 enum isl_fold type
, __isl_take isl_dim
*dim
, int n
)
21 isl_qpolynomial_fold
*fold
;
26 isl_assert(dim
->ctx
, n
>= 0, goto error
);
27 fold
= isl_calloc(dim
->ctx
, struct isl_qpolynomial_fold
,
28 sizeof(struct isl_qpolynomial_fold
) +
29 (n
- 1) * sizeof(struct isl_qpolynomial
*));
45 int isl_qpolynomial_fold_involves_dims(__isl_keep isl_qpolynomial_fold
*fold
,
46 enum isl_dim_type type
, unsigned first
, unsigned n
)
52 if (fold
->n
== 0 || n
== 0)
55 for (i
= 0; i
< fold
->n
; ++i
) {
56 int involves
= isl_qpolynomial_involves_dims(fold
->qp
[i
],
58 if (involves
< 0 || involves
)
64 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_drop_dims(
65 __isl_take isl_qpolynomial_fold
*fold
,
66 enum isl_dim_type type
, unsigned first
, unsigned n
)
75 fold
= isl_qpolynomial_fold_cow(fold
);
78 fold
->dim
= isl_dim_drop(fold
->dim
, type
, first
, n
);
82 for (i
= 0; i
< fold
->n
; ++i
) {
83 fold
->qp
[i
] = isl_qpolynomial_drop_dims(fold
->qp
[i
],
91 isl_qpolynomial_fold_free(fold
);
95 static int isl_qpolynomial_cst_sign(__isl_keep isl_qpolynomial
*qp
)
97 struct isl_upoly_cst
*cst
;
99 cst
= isl_upoly_as_cst(qp
->upoly
);
103 return isl_int_sgn(cst
->n
) < 0 ? -1 : 1;
106 static int isl_qpolynomial_aff_sign(__isl_keep isl_set
*set
,
107 __isl_keep isl_qpolynomial
*qp
)
109 enum isl_lp_result res
;
114 aff
= isl_qpolynomial_extract_affine(qp
);
120 res
= isl_set_solve_lp(set
, 0, aff
->el
+ 1, aff
->el
[0],
122 if (res
== isl_lp_error
)
124 if (res
== isl_lp_empty
||
125 (res
== isl_lp_ok
&& !isl_int_is_neg(opt
))) {
130 res
= isl_set_solve_lp(set
, 1, aff
->el
+ 1, aff
->el
[0],
132 if (res
== isl_lp_ok
&& !isl_int_is_pos(opt
))
141 /* Determine, if possible, the sign of the quasipolynomial "qp" on
144 * If qp is a constant, then the problem is trivial.
145 * If qp is linear, then we check if the minimum of the corresponding
146 * affine constraint is non-negative or if the maximum is non-positive.
148 * Otherwise, we check if the outermost variable "v" has a lower bound "l"
149 * in "set". If so, we write qp(v,v') as
151 * q(v,v') * (v - l) + r(v')
153 * if q(v,v') and r(v') have the same known sign, then the original
154 * quasipolynomial has the same sign as well.
161 static int isl_qpolynomial_sign(__isl_keep isl_set
*set
,
162 __isl_keep isl_qpolynomial
*qp
)
167 struct isl_upoly_rec
*rec
;
170 enum isl_lp_result res
;
173 is
= isl_qpolynomial_is_cst(qp
, NULL
, NULL
);
177 return isl_qpolynomial_cst_sign(qp
);
179 is
= isl_qpolynomial_is_affine(qp
);
183 return isl_qpolynomial_aff_sign(set
, qp
);
185 if (qp
->div
->n_row
> 0)
188 rec
= isl_upoly_as_rec(qp
->upoly
);
192 d
= isl_dim_total(qp
->dim
);
193 v
= isl_vec_alloc(set
->ctx
, 2 + d
);
197 isl_seq_clr(v
->el
+ 1, 1 + d
);
198 isl_int_set_si(v
->el
[0], 1);
199 isl_int_set_si(v
->el
[2 + qp
->upoly
->var
], 1);
203 res
= isl_set_solve_lp(set
, 0, v
->el
+ 1, v
->el
[0], &l
, NULL
, NULL
);
204 if (res
== isl_lp_ok
) {
205 isl_qpolynomial
*min
;
206 isl_qpolynomial
*base
;
207 isl_qpolynomial
*r
, *q
;
210 min
= isl_qpolynomial_cst(isl_dim_copy(qp
->dim
), l
);
211 base
= isl_qpolynomial_pow(isl_dim_copy(qp
->dim
),
214 r
= isl_qpolynomial_alloc(isl_dim_copy(qp
->dim
), 0,
215 isl_upoly_copy(rec
->p
[rec
->n
- 1]));
216 q
= isl_qpolynomial_copy(r
);
218 for (i
= rec
->n
- 2; i
>= 0; --i
) {
219 r
= isl_qpolynomial_mul(r
, isl_qpolynomial_copy(min
));
220 t
= isl_qpolynomial_alloc(isl_dim_copy(qp
->dim
), 0,
221 isl_upoly_copy(rec
->p
[i
]));
222 r
= isl_qpolynomial_add(r
, t
);
225 q
= isl_qpolynomial_mul(q
, isl_qpolynomial_copy(base
));
226 q
= isl_qpolynomial_add(q
, isl_qpolynomial_copy(r
));
229 if (isl_qpolynomial_is_zero(q
))
230 sgn
= isl_qpolynomial_sign(set
, r
);
231 else if (isl_qpolynomial_is_zero(r
))
232 sgn
= isl_qpolynomial_sign(set
, q
);
235 sgn_r
= isl_qpolynomial_sign(set
, r
);
236 sgn_q
= isl_qpolynomial_sign(set
, q
);
241 isl_qpolynomial_free(min
);
242 isl_qpolynomial_free(base
);
243 isl_qpolynomial_free(q
);
244 isl_qpolynomial_free(r
);
254 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_fold_on_domain(
255 __isl_keep isl_set
*set
,
256 __isl_take isl_qpolynomial_fold
*fold1
,
257 __isl_take isl_qpolynomial_fold
*fold2
)
261 struct isl_qpolynomial_fold
*res
= NULL
;
264 if (!fold1
|| !fold2
)
267 isl_assert(fold1
->dim
->ctx
, fold1
->type
== fold2
->type
, goto error
);
268 isl_assert(fold1
->dim
->ctx
, isl_dim_equal(fold1
->dim
, fold2
->dim
),
271 better
= fold1
->type
== isl_fold_max
? -1 : 1;
273 if (isl_qpolynomial_fold_is_empty(fold1
)) {
274 isl_qpolynomial_fold_free(fold1
);
278 if (isl_qpolynomial_fold_is_empty(fold2
)) {
279 isl_qpolynomial_fold_free(fold2
);
283 res
= qpolynomial_fold_alloc(fold1
->type
, isl_dim_copy(fold1
->dim
),
284 fold1
->n
+ fold2
->n
);
288 for (i
= 0; i
< fold1
->n
; ++i
) {
289 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold1
->qp
[i
]);
290 if (!res
->qp
[res
->n
])
296 for (i
= 0; i
< fold2
->n
; ++i
) {
297 for (j
= n1
- 1; j
>= 0; --j
) {
300 d
= isl_qpolynomial_sub(
301 isl_qpolynomial_copy(res
->qp
[j
]),
302 isl_qpolynomial_copy(fold2
->qp
[i
]));
303 sgn
= isl_qpolynomial_sign(set
, d
);
304 isl_qpolynomial_free(d
);
309 isl_qpolynomial_free(res
->qp
[j
]);
311 res
->qp
[j
] = res
->qp
[n1
- 1];
313 if (n1
!= res
->n
- 1)
314 res
->qp
[n1
] = res
->qp
[res
->n
- 1];
319 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold2
->qp
[i
]);
320 if (!res
->qp
[res
->n
])
325 isl_qpolynomial_fold_free(fold1
);
326 isl_qpolynomial_fold_free(fold2
);
330 isl_qpolynomial_fold_free(res
);
331 isl_qpolynomial_fold_free(fold1
);
332 isl_qpolynomial_fold_free(fold2
);
337 #define PW isl_pw_qpolynomial_fold
339 #define EL isl_qpolynomial_fold
341 #define IS_ZERO is_empty
345 #define ADD(d,e1,e2) isl_qpolynomial_fold_fold_on_domain(d,e1,e2)
347 #include <isl_pw_templ.c>
349 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_empty(enum isl_fold type
,
350 __isl_take isl_dim
*dim
)
352 return qpolynomial_fold_alloc(type
, dim
, 0);
355 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_alloc(
356 enum isl_fold type
, __isl_take isl_qpolynomial
*qp
)
358 isl_qpolynomial_fold
*fold
;
363 fold
= qpolynomial_fold_alloc(type
, isl_dim_copy(qp
->dim
), 1);
372 isl_qpolynomial_fold_free(fold
);
373 isl_qpolynomial_free(qp
);
377 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_copy(
378 __isl_keep isl_qpolynomial_fold
*fold
)
387 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_dup(
388 __isl_keep isl_qpolynomial_fold
*fold
)
391 isl_qpolynomial_fold
*dup
;
395 dup
= qpolynomial_fold_alloc(fold
->type
,
396 isl_dim_copy(fold
->dim
), fold
->n
);
401 for (i
= 0; i
< fold
->n
; ++i
) {
402 dup
->qp
[i
] = isl_qpolynomial_copy(fold
->qp
[i
]);
409 isl_qpolynomial_fold_free(dup
);
413 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_cow(
414 __isl_take isl_qpolynomial_fold
*fold
)
422 return isl_qpolynomial_fold_dup(fold
);
425 void isl_qpolynomial_fold_free(__isl_take isl_qpolynomial_fold
*fold
)
434 for (i
= 0; i
< fold
->n
; ++i
)
435 isl_qpolynomial_free(fold
->qp
[i
]);
436 isl_dim_free(fold
->dim
);
440 int isl_qpolynomial_fold_is_empty(__isl_keep isl_qpolynomial_fold
*fold
)
448 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_fold(
449 __isl_take isl_qpolynomial_fold
*fold1
,
450 __isl_take isl_qpolynomial_fold
*fold2
)
453 struct isl_qpolynomial_fold
*res
= NULL
;
455 if (!fold1
|| !fold2
)
458 isl_assert(fold1
->dim
->ctx
, fold1
->type
== fold2
->type
, goto error
);
459 isl_assert(fold1
->dim
->ctx
, isl_dim_equal(fold1
->dim
, fold2
->dim
),
462 if (isl_qpolynomial_fold_is_empty(fold1
)) {
463 isl_qpolynomial_fold_free(fold1
);
467 if (isl_qpolynomial_fold_is_empty(fold2
)) {
468 isl_qpolynomial_fold_free(fold2
);
472 res
= qpolynomial_fold_alloc(fold1
->type
, isl_dim_copy(fold1
->dim
),
473 fold1
->n
+ fold2
->n
);
477 for (i
= 0; i
< fold1
->n
; ++i
) {
478 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold1
->qp
[i
]);
479 if (!res
->qp
[res
->n
])
484 for (i
= 0; i
< fold2
->n
; ++i
) {
485 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold2
->qp
[i
]);
486 if (!res
->qp
[res
->n
])
491 isl_qpolynomial_fold_free(fold1
);
492 isl_qpolynomial_fold_free(fold2
);
496 isl_qpolynomial_fold_free(res
);
497 isl_qpolynomial_fold_free(fold1
);
498 isl_qpolynomial_fold_free(fold2
);
502 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_fold_from_pw_qpolynomial(
503 enum isl_fold type
, __isl_take isl_pw_qpolynomial
*pwqp
)
506 isl_pw_qpolynomial_fold
*pwf
;
511 pwf
= isl_pw_qpolynomial_fold_alloc_(isl_dim_copy(pwqp
->dim
), pwqp
->n
);
513 for (i
= 0; i
< pwqp
->n
; ++i
)
514 pwf
= isl_pw_qpolynomial_fold_add_piece(pwf
,
515 isl_set_copy(pwqp
->p
[i
].set
),
516 isl_qpolynomial_fold_alloc(type
,
517 isl_qpolynomial_copy(pwqp
->p
[i
].qp
)));
519 isl_pw_qpolynomial_free(pwqp
);
524 int isl_qpolynomial_fold_is_equal(__isl_keep isl_qpolynomial_fold
*fold1
,
525 __isl_keep isl_qpolynomial_fold
*fold2
)
529 if (!fold1
|| !fold2
)
532 if (fold1
->n
!= fold2
->n
)
535 /* We probably want to sort the qps first... */
536 for (i
= 0; i
< fold1
->n
; ++i
) {
537 int eq
= isl_qpolynomial_is_equal(fold1
->qp
[i
], fold2
->qp
[i
]);
545 __isl_give isl_qpolynomial
*isl_qpolynomial_fold_eval(
546 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_point
*pnt
)
552 isl_assert(pnt
->dim
->ctx
, isl_dim_equal(pnt
->dim
, fold
->dim
), goto error
);
553 isl_assert(pnt
->dim
->ctx
,
554 fold
->type
== isl_fold_max
|| fold
->type
== isl_fold_min
,
558 qp
= isl_qpolynomial_zero(isl_dim_copy(fold
->dim
));
561 qp
= isl_qpolynomial_eval(isl_qpolynomial_copy(fold
->qp
[0]),
562 isl_point_copy(pnt
));
563 for (i
= 1; i
< fold
->n
; ++i
) {
564 isl_qpolynomial
*qp_i
;
565 qp_i
= isl_qpolynomial_eval(
566 isl_qpolynomial_copy(fold
->qp
[i
]),
567 isl_point_copy(pnt
));
568 if (fold
->type
== isl_fold_max
)
569 qp
= isl_qpolynomial_max_cst(qp
, qp_i
);
571 qp
= isl_qpolynomial_min_cst(qp
, qp_i
);
574 isl_qpolynomial_fold_free(fold
);
579 isl_qpolynomial_fold_free(fold
);
584 size_t isl_pw_qpolynomial_fold_size(__isl_keep isl_pw_qpolynomial_fold
*pwf
)
589 for (i
= 0; i
< pwf
->n
; ++i
)
590 n
+= pwf
->p
[i
].fold
->n
;
595 __isl_give isl_qpolynomial
*isl_qpolynomial_fold_opt_on_domain(
596 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_set
*set
, int max
)
599 isl_qpolynomial
*opt
;
605 isl_dim
*dim
= isl_dim_copy(fold
->dim
);
607 isl_qpolynomial_fold_free(fold
);
608 return isl_qpolynomial_zero(dim
);
611 opt
= isl_qpolynomial_opt_on_domain(isl_qpolynomial_copy(fold
->qp
[0]),
612 isl_set_copy(set
), max
);
613 for (i
= 1; i
< fold
->n
; ++i
) {
614 isl_qpolynomial
*opt_i
;
615 opt_i
= isl_qpolynomial_opt_on_domain(
616 isl_qpolynomial_copy(fold
->qp
[i
]),
617 isl_set_copy(set
), max
);
619 opt
= isl_qpolynomial_max_cst(opt
, opt_i
);
621 opt
= isl_qpolynomial_min_cst(opt
, opt_i
);
625 isl_qpolynomial_fold_free(fold
);
630 isl_qpolynomial_fold_free(fold
);
634 /* Check whether for each quasi-polynomial in "fold2" there is
635 * a quasi-polynomial in "fold1" that dominates it on "set".
637 static int qpolynomial_fold_covers_on_domain(__isl_keep isl_set
*set
,
638 __isl_keep isl_qpolynomial_fold
*fold1
,
639 __isl_keep isl_qpolynomial_fold
*fold2
)
644 if (!set
|| !fold1
|| !fold2
)
647 covers
= fold1
->type
== isl_fold_max
? 1 : -1;
649 for (i
= 0; i
< fold2
->n
; ++i
) {
650 for (j
= 0; j
< fold1
->n
; ++j
) {
654 d
= isl_qpolynomial_sub(
655 isl_qpolynomial_copy(fold1
->qp
[j
]),
656 isl_qpolynomial_copy(fold2
->qp
[i
]));
657 sgn
= isl_qpolynomial_sign(set
, d
);
658 isl_qpolynomial_free(d
);
669 /* Check whether "pwf1" dominated "pwf2", i.e., the domain of "pwf1" contains
670 * that of "pwf2" and on each cell, the corresponding fold from pwf1 dominates
673 int isl_pw_qpolynomial_fold_covers(__isl_keep isl_pw_qpolynomial_fold
*pwf1
,
674 __isl_keep isl_pw_qpolynomial_fold
*pwf2
)
677 isl_set
*dom1
, *dom2
;
688 dom1
= isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf1
));
689 dom2
= isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf2
));
690 is_subset
= isl_set_is_subset(dom2
, dom1
);
694 if (is_subset
< 0 || !is_subset
)
697 for (i
= 0; i
< pwf2
->n
; ++i
) {
698 for (j
= 0; j
< pwf1
->n
; ++j
) {
703 common
= isl_set_intersect(isl_set_copy(pwf1
->p
[j
].set
),
704 isl_set_copy(pwf2
->p
[i
].set
));
705 is_empty
= isl_set_is_empty(common
);
706 if (is_empty
< 0 || is_empty
) {
707 isl_set_free(common
);
712 covers
= qpolynomial_fold_covers_on_domain(common
,
713 pwf1
->p
[j
].fold
, pwf2
->p
[i
].fold
);
714 isl_set_free(common
);
715 if (covers
< 0 || !covers
)
723 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_morph(
724 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_morph
*morph
)
732 ctx
= fold
->dim
->ctx
;
733 isl_assert(ctx
, isl_dim_equal(fold
->dim
, morph
->dom
->dim
), goto error
);
735 fold
= isl_qpolynomial_fold_cow(fold
);
739 isl_dim_free(fold
->dim
);
740 fold
->dim
= isl_dim_copy(morph
->ran
->dim
);
744 for (i
= 0; i
< fold
->n
; ++i
) {
745 fold
->qp
[i
] = isl_qpolynomial_morph(fold
->qp
[i
],
746 isl_morph_copy(morph
));
751 isl_morph_free(morph
);
755 isl_qpolynomial_fold_free(fold
);
756 isl_morph_free(morph
);
760 enum isl_fold
isl_qpolynomial_fold_get_type(__isl_keep isl_qpolynomial_fold
*fold
)
763 return isl_fold_list
;
767 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_lift(
768 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_dim
*dim
)
776 if (isl_dim_equal(fold
->dim
, dim
)) {
781 fold
= isl_qpolynomial_fold_cow(fold
);
785 isl_dim_free(fold
->dim
);
786 fold
->dim
= isl_dim_copy(dim
);
790 for (i
= 0; i
< fold
->n
; ++i
) {
791 fold
->qp
[i
] = isl_qpolynomial_lift(fold
->qp
[i
],
801 isl_qpolynomial_fold_free(fold
);
806 int isl_qpolynomial_fold_foreach_qpolynomial(
807 __isl_keep isl_qpolynomial_fold
*fold
,
808 int (*fn
)(__isl_take isl_qpolynomial
*qp
, void *user
), void *user
)
815 for (i
= 0; i
< fold
->n
; ++i
)
816 if (fn(isl_qpolynomial_copy(fold
->qp
[i
]), user
) < 0)
822 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_move_dims(
823 __isl_take isl_qpolynomial_fold
*fold
,
824 enum isl_dim_type dst_type
, unsigned dst_pos
,
825 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
832 fold
= isl_qpolynomial_fold_cow(fold
);
836 fold
->dim
= isl_dim_move(fold
->dim
, dst_type
, dst_pos
,
837 src_type
, src_pos
, n
);
841 for (i
= 0; i
< fold
->n
; ++i
) {
842 fold
->qp
[i
] = isl_qpolynomial_move_dims(fold
->qp
[i
],
843 dst_type
, dst_pos
, src_type
, src_pos
, n
);
850 isl_qpolynomial_fold_free(fold
);