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 /* Given a dimension type for an isl_qpolynomial_fold,
173 * return the corresponding type for the domain.
175 static enum isl_dim_type
domain_type(enum isl_dim_type type
)
177 if (type
== isl_dim_in
)
182 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_drop_dims(
183 __isl_take isl_qpolynomial_fold
*fold
,
184 enum isl_dim_type type
, unsigned first
, unsigned n
)
187 enum isl_dim_type set_type
;
194 set_type
= domain_type(type
);
196 fold
= isl_qpolynomial_fold_cow(fold
);
199 fold
->dim
= isl_space_drop_dims(fold
->dim
, set_type
, first
, n
);
203 for (i
= 0; i
< fold
->n
; ++i
) {
204 fold
->qp
[i
] = isl_qpolynomial_drop_dims(fold
->qp
[i
],
212 isl_qpolynomial_fold_free(fold
);
216 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_insert_dims(
217 __isl_take isl_qpolynomial_fold
*fold
,
218 enum isl_dim_type type
, unsigned first
, unsigned n
)
224 if (n
== 0 && !isl_space_is_named_or_nested(fold
->dim
, type
))
227 fold
= isl_qpolynomial_fold_cow(fold
);
230 fold
->dim
= isl_space_insert_dims(fold
->dim
, type
, first
, n
);
234 for (i
= 0; i
< fold
->n
; ++i
) {
235 fold
->qp
[i
] = isl_qpolynomial_insert_dims(fold
->qp
[i
],
243 isl_qpolynomial_fold_free(fold
);
247 /* Determine the sign of the constant quasipolynomial "qp".
254 * For qp == 0, we can return either -1 or 1. In practice, we return 1.
255 * For qp == NaN, the sign is undefined, so we return 0.
257 static int isl_qpolynomial_cst_sign(__isl_keep isl_qpolynomial
*qp
)
259 struct isl_upoly_cst
*cst
;
261 if (isl_qpolynomial_is_nan(qp
))
264 cst
= isl_upoly_as_cst(qp
->upoly
);
268 return isl_int_sgn(cst
->n
) < 0 ? -1 : 1;
271 static int isl_qpolynomial_aff_sign(__isl_keep isl_set
*set
,
272 __isl_keep isl_qpolynomial
*qp
)
274 enum isl_lp_result res
;
279 aff
= isl_qpolynomial_extract_affine(qp
);
285 res
= isl_set_solve_lp(set
, 0, aff
->el
+ 1, aff
->el
[0],
287 if (res
== isl_lp_error
)
289 if (res
== isl_lp_empty
||
290 (res
== isl_lp_ok
&& !isl_int_is_neg(opt
))) {
295 res
= isl_set_solve_lp(set
, 1, aff
->el
+ 1, aff
->el
[0],
297 if (res
== isl_lp_ok
&& !isl_int_is_pos(opt
))
306 /* Determine, if possible, the sign of the quasipolynomial "qp" on
309 * If qp is a constant, then the problem is trivial.
310 * If qp is linear, then we check if the minimum of the corresponding
311 * affine constraint is non-negative or if the maximum is non-positive.
313 * Otherwise, we check if the outermost variable "v" has a lower bound "l"
314 * in "set". If so, we write qp(v,v') as
316 * q(v,v') * (v - l) + r(v')
318 * if q(v,v') and r(v') have the same known sign, then the original
319 * quasipolynomial has the same sign as well.
326 static int isl_qpolynomial_sign(__isl_keep isl_set
*set
,
327 __isl_keep isl_qpolynomial
*qp
)
332 struct isl_upoly_rec
*rec
;
335 enum isl_lp_result res
;
338 is
= isl_qpolynomial_is_cst(qp
, NULL
, NULL
);
342 return isl_qpolynomial_cst_sign(qp
);
344 is
= isl_qpolynomial_is_affine(qp
);
348 return isl_qpolynomial_aff_sign(set
, qp
);
350 if (qp
->div
->n_row
> 0)
353 rec
= isl_upoly_as_rec(qp
->upoly
);
357 d
= isl_space_dim(qp
->dim
, isl_dim_all
);
358 v
= isl_vec_alloc(set
->ctx
, 2 + d
);
362 isl_seq_clr(v
->el
+ 1, 1 + d
);
363 isl_int_set_si(v
->el
[0], 1);
364 isl_int_set_si(v
->el
[2 + qp
->upoly
->var
], 1);
368 res
= isl_set_solve_lp(set
, 0, v
->el
+ 1, v
->el
[0], &l
, NULL
, NULL
);
369 if (res
== isl_lp_ok
) {
370 isl_qpolynomial
*min
;
371 isl_qpolynomial
*base
;
372 isl_qpolynomial
*r
, *q
;
375 min
= isl_qpolynomial_cst_on_domain(isl_space_copy(qp
->dim
), l
);
376 base
= isl_qpolynomial_var_pow_on_domain(isl_space_copy(qp
->dim
),
379 r
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), 0,
380 isl_upoly_copy(rec
->p
[rec
->n
- 1]));
381 q
= isl_qpolynomial_copy(r
);
383 for (i
= rec
->n
- 2; i
>= 0; --i
) {
384 r
= isl_qpolynomial_mul(r
, isl_qpolynomial_copy(min
));
385 t
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), 0,
386 isl_upoly_copy(rec
->p
[i
]));
387 r
= isl_qpolynomial_add(r
, t
);
390 q
= isl_qpolynomial_mul(q
, isl_qpolynomial_copy(base
));
391 q
= isl_qpolynomial_add(q
, isl_qpolynomial_copy(r
));
394 if (isl_qpolynomial_is_zero(q
))
395 sgn
= isl_qpolynomial_sign(set
, r
);
396 else if (isl_qpolynomial_is_zero(r
))
397 sgn
= isl_qpolynomial_sign(set
, q
);
400 sgn_r
= isl_qpolynomial_sign(set
, r
);
401 sgn_q
= isl_qpolynomial_sign(set
, q
);
406 isl_qpolynomial_free(min
);
407 isl_qpolynomial_free(base
);
408 isl_qpolynomial_free(q
);
409 isl_qpolynomial_free(r
);
419 /* Combine "fold1" and "fold2" into a single reduction, eliminating
420 * those elements of one reduction that are already covered by the other
421 * reduction on "set".
423 * If "fold1" or "fold2" is an empty reduction, then return
424 * the other reduction.
425 * If "fold1" or "fold2" is a NaN, then return this NaN.
427 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_fold_on_domain(
428 __isl_keep isl_set
*set
,
429 __isl_take isl_qpolynomial_fold
*fold1
,
430 __isl_take isl_qpolynomial_fold
*fold2
)
434 struct isl_qpolynomial_fold
*res
= NULL
;
437 if (!fold1
|| !fold2
)
440 isl_assert(fold1
->dim
->ctx
, fold1
->type
== fold2
->type
, goto error
);
441 isl_assert(fold1
->dim
->ctx
, isl_space_is_equal(fold1
->dim
, fold2
->dim
),
444 better
= fold1
->type
== isl_fold_max
? -1 : 1;
446 if (isl_qpolynomial_fold_is_empty(fold1
) ||
447 isl_qpolynomial_fold_is_nan(fold2
)) {
448 isl_qpolynomial_fold_free(fold1
);
452 if (isl_qpolynomial_fold_is_empty(fold2
) ||
453 isl_qpolynomial_fold_is_nan(fold1
)) {
454 isl_qpolynomial_fold_free(fold2
);
458 res
= qpolynomial_fold_alloc(fold1
->type
, isl_space_copy(fold1
->dim
),
459 fold1
->n
+ fold2
->n
);
463 for (i
= 0; i
< fold1
->n
; ++i
) {
464 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold1
->qp
[i
]);
465 if (!res
->qp
[res
->n
])
471 for (i
= 0; i
< fold2
->n
; ++i
) {
472 for (j
= n1
- 1; j
>= 0; --j
) {
475 equal
= isl_qpolynomial_plain_is_equal(res
->qp
[j
],
481 d
= isl_qpolynomial_sub(
482 isl_qpolynomial_copy(res
->qp
[j
]),
483 isl_qpolynomial_copy(fold2
->qp
[i
]));
484 sgn
= isl_qpolynomial_sign(set
, d
);
485 isl_qpolynomial_free(d
);
490 isl_qpolynomial_free(res
->qp
[j
]);
492 res
->qp
[j
] = res
->qp
[n1
- 1];
494 if (n1
!= res
->n
- 1)
495 res
->qp
[n1
] = res
->qp
[res
->n
- 1];
500 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold2
->qp
[i
]);
501 if (!res
->qp
[res
->n
])
506 isl_qpolynomial_fold_free(fold1
);
507 isl_qpolynomial_fold_free(fold2
);
511 isl_qpolynomial_fold_free(res
);
512 isl_qpolynomial_fold_free(fold1
);
513 isl_qpolynomial_fold_free(fold2
);
517 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_add_qpolynomial(
518 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_qpolynomial
*qp
)
525 if (isl_qpolynomial_is_zero(qp
)) {
526 isl_qpolynomial_free(qp
);
530 fold
= isl_qpolynomial_fold_cow(fold
);
534 for (i
= 0; i
< fold
->n
; ++i
) {
535 fold
->qp
[i
] = isl_qpolynomial_add(fold
->qp
[i
],
536 isl_qpolynomial_copy(qp
));
541 isl_qpolynomial_free(qp
);
544 isl_qpolynomial_fold_free(fold
);
545 isl_qpolynomial_free(qp
);
549 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_add_on_domain(
550 __isl_keep isl_set
*dom
,
551 __isl_take isl_qpolynomial_fold
*fold1
,
552 __isl_take isl_qpolynomial_fold
*fold2
)
555 isl_qpolynomial_fold
*res
= NULL
;
557 if (!fold1
|| !fold2
)
560 if (isl_qpolynomial_fold_is_empty(fold1
)) {
561 isl_qpolynomial_fold_free(fold1
);
565 if (isl_qpolynomial_fold_is_empty(fold2
)) {
566 isl_qpolynomial_fold_free(fold2
);
570 if (fold1
->n
== 1 && fold2
->n
!= 1)
571 return isl_qpolynomial_fold_add_on_domain(dom
, fold2
, fold1
);
574 res
= isl_qpolynomial_fold_add_qpolynomial(fold1
,
575 isl_qpolynomial_copy(fold2
->qp
[0]));
576 isl_qpolynomial_fold_free(fold2
);
580 res
= isl_qpolynomial_fold_add_qpolynomial(
581 isl_qpolynomial_fold_copy(fold1
),
582 isl_qpolynomial_copy(fold2
->qp
[0]));
584 for (i
= 1; i
< fold2
->n
; ++i
) {
585 isl_qpolynomial_fold
*res_i
;
586 res_i
= isl_qpolynomial_fold_add_qpolynomial(
587 isl_qpolynomial_fold_copy(fold1
),
588 isl_qpolynomial_copy(fold2
->qp
[i
]));
589 res
= isl_qpolynomial_fold_fold_on_domain(dom
, res
, res_i
);
592 isl_qpolynomial_fold_free(fold1
);
593 isl_qpolynomial_fold_free(fold2
);
596 isl_qpolynomial_fold_free(res
);
597 isl_qpolynomial_fold_free(fold1
);
598 isl_qpolynomial_fold_free(fold2
);
602 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_substitute_equalities(
603 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_basic_set
*eq
)
610 fold
= isl_qpolynomial_fold_cow(fold
);
614 for (i
= 0; i
< fold
->n
; ++i
) {
615 fold
->qp
[i
] = isl_qpolynomial_substitute_equalities(fold
->qp
[i
],
616 isl_basic_set_copy(eq
));
621 isl_basic_set_free(eq
);
624 isl_basic_set_free(eq
);
625 isl_qpolynomial_fold_free(fold
);
629 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_gist(
630 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_set
*context
)
634 if (!fold
|| !context
)
637 fold
= isl_qpolynomial_fold_cow(fold
);
641 for (i
= 0; i
< fold
->n
; ++i
) {
642 fold
->qp
[i
] = isl_qpolynomial_gist(fold
->qp
[i
],
643 isl_set_copy(context
));
648 isl_set_free(context
);
651 isl_set_free(context
);
652 isl_qpolynomial_fold_free(fold
);
656 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_gist_params(
657 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_set
*context
)
659 isl_space
*space
= isl_qpolynomial_fold_get_domain_space(fold
);
660 isl_set
*dom_context
= isl_set_universe(space
);
661 dom_context
= isl_set_intersect_params(dom_context
, context
);
662 return isl_qpolynomial_fold_gist(fold
, dom_context
);
665 #define isl_qpolynomial_fold_involves_nan isl_qpolynomial_fold_is_nan
670 #define PW isl_pw_qpolynomial_fold
672 #define EL isl_qpolynomial_fold
674 #define EL_IS_ZERO is_empty
678 #define IS_ZERO is_zero
681 #undef DEFAULT_IS_ZERO
682 #define DEFAULT_IS_ZERO 1
688 #include <isl_pw_templ.c>
691 #define UNION isl_union_pw_qpolynomial_fold
693 #define PART isl_pw_qpolynomial_fold
695 #define PARTS pw_qpolynomial_fold
699 #include <isl_union_single.c>
700 #include <isl_union_eval.c>
702 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_empty(enum isl_fold type
,
703 __isl_take isl_space
*dim
)
705 return qpolynomial_fold_alloc(type
, dim
, 0);
708 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_alloc(
709 enum isl_fold type
, __isl_take isl_qpolynomial
*qp
)
711 isl_qpolynomial_fold
*fold
;
716 fold
= qpolynomial_fold_alloc(type
, isl_space_copy(qp
->dim
), 1);
725 isl_qpolynomial_fold_free(fold
);
726 isl_qpolynomial_free(qp
);
730 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_copy(
731 __isl_keep isl_qpolynomial_fold
*fold
)
740 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_dup(
741 __isl_keep isl_qpolynomial_fold
*fold
)
744 isl_qpolynomial_fold
*dup
;
748 dup
= qpolynomial_fold_alloc(fold
->type
,
749 isl_space_copy(fold
->dim
), fold
->n
);
754 for (i
= 0; i
< fold
->n
; ++i
) {
755 dup
->qp
[i
] = isl_qpolynomial_copy(fold
->qp
[i
]);
762 isl_qpolynomial_fold_free(dup
);
766 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_cow(
767 __isl_take isl_qpolynomial_fold
*fold
)
775 return isl_qpolynomial_fold_dup(fold
);
778 void isl_qpolynomial_fold_free(__isl_take isl_qpolynomial_fold
*fold
)
787 for (i
= 0; i
< fold
->n
; ++i
)
788 isl_qpolynomial_free(fold
->qp
[i
]);
789 isl_space_free(fold
->dim
);
793 int isl_qpolynomial_fold_is_empty(__isl_keep isl_qpolynomial_fold
*fold
)
801 /* Does "fold" represent max(NaN) or min(NaN)?
803 isl_bool
isl_qpolynomial_fold_is_nan(__isl_keep isl_qpolynomial_fold
*fold
)
806 return isl_bool_error
;
808 return isl_bool_false
;
809 return isl_qpolynomial_is_nan(fold
->qp
[0]);
812 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_fold(
813 __isl_take isl_qpolynomial_fold
*fold1
,
814 __isl_take isl_qpolynomial_fold
*fold2
)
817 struct isl_qpolynomial_fold
*res
= NULL
;
819 if (!fold1
|| !fold2
)
822 isl_assert(fold1
->dim
->ctx
, fold1
->type
== fold2
->type
, goto error
);
823 isl_assert(fold1
->dim
->ctx
, isl_space_is_equal(fold1
->dim
, fold2
->dim
),
826 if (isl_qpolynomial_fold_is_empty(fold1
)) {
827 isl_qpolynomial_fold_free(fold1
);
831 if (isl_qpolynomial_fold_is_empty(fold2
)) {
832 isl_qpolynomial_fold_free(fold2
);
836 res
= qpolynomial_fold_alloc(fold1
->type
, isl_space_copy(fold1
->dim
),
837 fold1
->n
+ fold2
->n
);
841 for (i
= 0; i
< fold1
->n
; ++i
) {
842 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold1
->qp
[i
]);
843 if (!res
->qp
[res
->n
])
848 for (i
= 0; i
< fold2
->n
; ++i
) {
849 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold2
->qp
[i
]);
850 if (!res
->qp
[res
->n
])
855 isl_qpolynomial_fold_free(fold1
);
856 isl_qpolynomial_fold_free(fold2
);
860 isl_qpolynomial_fold_free(res
);
861 isl_qpolynomial_fold_free(fold1
);
862 isl_qpolynomial_fold_free(fold2
);
866 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_fold_fold(
867 __isl_take isl_pw_qpolynomial_fold
*pw1
,
868 __isl_take isl_pw_qpolynomial_fold
*pw2
)
871 struct isl_pw_qpolynomial_fold
*res
;
877 isl_assert(pw1
->dim
->ctx
, isl_space_is_equal(pw1
->dim
, pw2
->dim
), goto error
);
879 if (isl_pw_qpolynomial_fold_is_zero(pw1
)) {
880 isl_pw_qpolynomial_fold_free(pw1
);
884 if (isl_pw_qpolynomial_fold_is_zero(pw2
)) {
885 isl_pw_qpolynomial_fold_free(pw2
);
889 if (pw1
->type
!= pw2
->type
)
890 isl_die(pw1
->dim
->ctx
, isl_error_invalid
,
891 "fold types don't match", goto error
);
893 n
= (pw1
->n
+ 1) * (pw2
->n
+ 1);
894 res
= isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pw1
->dim
),
897 for (i
= 0; i
< pw1
->n
; ++i
) {
898 set
= isl_set_copy(pw1
->p
[i
].set
);
899 for (j
= 0; j
< pw2
->n
; ++j
) {
900 struct isl_set
*common
;
901 isl_qpolynomial_fold
*sum
;
902 set
= isl_set_subtract(set
,
903 isl_set_copy(pw2
->p
[j
].set
));
904 common
= isl_set_intersect(isl_set_copy(pw1
->p
[i
].set
),
905 isl_set_copy(pw2
->p
[j
].set
));
906 if (isl_set_plain_is_empty(common
)) {
907 isl_set_free(common
);
911 sum
= isl_qpolynomial_fold_fold_on_domain(common
,
912 isl_qpolynomial_fold_copy(pw1
->p
[i
].fold
),
913 isl_qpolynomial_fold_copy(pw2
->p
[j
].fold
));
915 res
= isl_pw_qpolynomial_fold_add_piece(res
, common
, sum
);
917 res
= isl_pw_qpolynomial_fold_add_piece(res
, set
,
918 isl_qpolynomial_fold_copy(pw1
->p
[i
].fold
));
921 for (j
= 0; j
< pw2
->n
; ++j
) {
922 set
= isl_set_copy(pw2
->p
[j
].set
);
923 for (i
= 0; i
< pw1
->n
; ++i
)
924 set
= isl_set_subtract(set
, isl_set_copy(pw1
->p
[i
].set
));
925 res
= isl_pw_qpolynomial_fold_add_piece(res
, set
,
926 isl_qpolynomial_fold_copy(pw2
->p
[j
].fold
));
929 isl_pw_qpolynomial_fold_free(pw1
);
930 isl_pw_qpolynomial_fold_free(pw2
);
934 isl_pw_qpolynomial_fold_free(pw1
);
935 isl_pw_qpolynomial_fold_free(pw2
);
939 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
940 __isl_take isl_union_pw_qpolynomial_fold
*u
,
941 __isl_take isl_pw_qpolynomial_fold
*part
)
943 struct isl_hash_table_entry
*entry
;
945 u
= isl_union_pw_qpolynomial_fold_cow(u
);
950 isl_assert(u
->space
->ctx
,
951 isl_space_match(part
->dim
, isl_dim_param
, u
->space
, isl_dim_param
),
954 entry
= isl_union_pw_qpolynomial_fold_find_part_entry(u
, part
->dim
, 1);
961 entry
->data
= isl_pw_qpolynomial_fold_fold(entry
->data
,
962 isl_pw_qpolynomial_fold_copy(part
));
965 isl_pw_qpolynomial_fold_free(part
);
970 isl_pw_qpolynomial_fold_free(part
);
971 isl_union_pw_qpolynomial_fold_free(u
);
975 static isl_stat
fold_part(__isl_take isl_pw_qpolynomial_fold
*part
, void *user
)
977 isl_union_pw_qpolynomial_fold
**u
;
978 u
= (isl_union_pw_qpolynomial_fold
**)user
;
980 *u
= isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(*u
, part
);
985 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_pw_qpolynomial_fold_fold(
986 __isl_take isl_union_pw_qpolynomial_fold
*u1
,
987 __isl_take isl_union_pw_qpolynomial_fold
*u2
)
989 u1
= isl_union_pw_qpolynomial_fold_cow(u1
);
994 if (isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(u2
,
995 &fold_part
, &u1
) < 0)
998 isl_union_pw_qpolynomial_fold_free(u2
);
1002 isl_union_pw_qpolynomial_fold_free(u1
);
1003 isl_union_pw_qpolynomial_fold_free(u2
);
1007 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_fold_from_pw_qpolynomial(
1008 enum isl_fold type
, __isl_take isl_pw_qpolynomial
*pwqp
)
1011 isl_pw_qpolynomial_fold
*pwf
;
1016 pwf
= isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pwqp
->dim
),
1019 for (i
= 0; i
< pwqp
->n
; ++i
)
1020 pwf
= isl_pw_qpolynomial_fold_add_piece(pwf
,
1021 isl_set_copy(pwqp
->p
[i
].set
),
1022 isl_qpolynomial_fold_alloc(type
,
1023 isl_qpolynomial_copy(pwqp
->p
[i
].qp
)));
1025 isl_pw_qpolynomial_free(pwqp
);
1030 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_fold_add(
1031 __isl_take isl_pw_qpolynomial_fold
*pwf1
,
1032 __isl_take isl_pw_qpolynomial_fold
*pwf2
)
1034 return isl_pw_qpolynomial_fold_union_add_(pwf1
, pwf2
);
1037 /* Compare two quasi-polynomial reductions.
1039 * Return -1 if "fold1" is "smaller" than "fold2", 1 if "fold1" is "greater"
1040 * than "fold2" and 0 if they are equal.
1042 int isl_qpolynomial_fold_plain_cmp(__isl_keep isl_qpolynomial_fold
*fold1
,
1043 __isl_keep isl_qpolynomial_fold
*fold2
)
1054 if (fold1
->n
!= fold2
->n
)
1055 return fold1
->n
- fold2
->n
;
1057 for (i
= 0; i
< fold1
->n
; ++i
) {
1060 cmp
= isl_qpolynomial_plain_cmp(fold1
->qp
[i
], fold2
->qp
[i
]);
1068 int isl_qpolynomial_fold_plain_is_equal(__isl_keep isl_qpolynomial_fold
*fold1
,
1069 __isl_keep isl_qpolynomial_fold
*fold2
)
1073 if (!fold1
|| !fold2
)
1076 if (fold1
->n
!= fold2
->n
)
1079 /* We probably want to sort the qps first... */
1080 for (i
= 0; i
< fold1
->n
; ++i
) {
1081 int eq
= isl_qpolynomial_plain_is_equal(fold1
->qp
[i
], fold2
->qp
[i
]);
1089 __isl_give isl_val
*isl_qpolynomial_fold_eval(
1090 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_point
*pnt
)
1097 ctx
= isl_point_get_ctx(pnt
);
1098 isl_assert(pnt
->dim
->ctx
, isl_space_is_equal(pnt
->dim
, fold
->dim
), goto error
);
1099 isl_assert(pnt
->dim
->ctx
,
1100 fold
->type
== isl_fold_max
|| fold
->type
== isl_fold_min
,
1104 v
= isl_val_zero(ctx
);
1107 v
= isl_qpolynomial_eval(isl_qpolynomial_copy(fold
->qp
[0]),
1108 isl_point_copy(pnt
));
1109 for (i
= 1; i
< fold
->n
; ++i
) {
1111 v_i
= isl_qpolynomial_eval(
1112 isl_qpolynomial_copy(fold
->qp
[i
]),
1113 isl_point_copy(pnt
));
1114 if (fold
->type
== isl_fold_max
)
1115 v
= isl_val_max(v
, v_i
);
1117 v
= isl_val_min(v
, v_i
);
1120 isl_qpolynomial_fold_free(fold
);
1121 isl_point_free(pnt
);
1125 isl_qpolynomial_fold_free(fold
);
1126 isl_point_free(pnt
);
1130 size_t isl_pw_qpolynomial_fold_size(__isl_keep isl_pw_qpolynomial_fold
*pwf
)
1135 for (i
= 0; i
< pwf
->n
; ++i
)
1136 n
+= pwf
->p
[i
].fold
->n
;
1141 __isl_give isl_val
*isl_qpolynomial_fold_opt_on_domain(
1142 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_set
*set
, int max
)
1151 opt
= isl_val_zero(isl_set_get_ctx(set
));
1153 isl_qpolynomial_fold_free(fold
);
1157 opt
= isl_qpolynomial_opt_on_domain(isl_qpolynomial_copy(fold
->qp
[0]),
1158 isl_set_copy(set
), max
);
1159 for (i
= 1; i
< fold
->n
; ++i
) {
1161 opt_i
= isl_qpolynomial_opt_on_domain(
1162 isl_qpolynomial_copy(fold
->qp
[i
]),
1163 isl_set_copy(set
), max
);
1165 opt
= isl_val_max(opt
, opt_i
);
1167 opt
= isl_val_min(opt
, opt_i
);
1171 isl_qpolynomial_fold_free(fold
);
1176 isl_qpolynomial_fold_free(fold
);
1180 /* Check whether for each quasi-polynomial in "fold2" there is
1181 * a quasi-polynomial in "fold1" that dominates it on "set".
1183 static int qpolynomial_fold_covers_on_domain(__isl_keep isl_set
*set
,
1184 __isl_keep isl_qpolynomial_fold
*fold1
,
1185 __isl_keep isl_qpolynomial_fold
*fold2
)
1190 if (!set
|| !fold1
|| !fold2
)
1193 covers
= fold1
->type
== isl_fold_max
? 1 : -1;
1195 for (i
= 0; i
< fold2
->n
; ++i
) {
1196 for (j
= 0; j
< fold1
->n
; ++j
) {
1200 d
= isl_qpolynomial_sub(
1201 isl_qpolynomial_copy(fold1
->qp
[j
]),
1202 isl_qpolynomial_copy(fold2
->qp
[i
]));
1203 sgn
= isl_qpolynomial_sign(set
, d
);
1204 isl_qpolynomial_free(d
);
1215 /* Check whether "pwf1" dominated "pwf2", i.e., the domain of "pwf1" contains
1216 * that of "pwf2" and on each cell, the corresponding fold from pwf1 dominates
1219 int isl_pw_qpolynomial_fold_covers(__isl_keep isl_pw_qpolynomial_fold
*pwf1
,
1220 __isl_keep isl_pw_qpolynomial_fold
*pwf2
)
1223 isl_set
*dom1
, *dom2
;
1234 dom1
= isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf1
));
1235 dom2
= isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf2
));
1236 is_subset
= isl_set_is_subset(dom2
, dom1
);
1240 if (is_subset
< 0 || !is_subset
)
1243 for (i
= 0; i
< pwf2
->n
; ++i
) {
1244 for (j
= 0; j
< pwf1
->n
; ++j
) {
1249 common
= isl_set_intersect(isl_set_copy(pwf1
->p
[j
].set
),
1250 isl_set_copy(pwf2
->p
[i
].set
));
1251 is_empty
= isl_set_is_empty(common
);
1252 if (is_empty
< 0 || is_empty
) {
1253 isl_set_free(common
);
1258 covers
= qpolynomial_fold_covers_on_domain(common
,
1259 pwf1
->p
[j
].fold
, pwf2
->p
[i
].fold
);
1260 isl_set_free(common
);
1261 if (covers
< 0 || !covers
)
1269 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_morph_domain(
1270 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_morph
*morph
)
1275 if (!fold
|| !morph
)
1278 ctx
= fold
->dim
->ctx
;
1279 isl_assert(ctx
, isl_space_is_equal(fold
->dim
, morph
->dom
->dim
), goto error
);
1281 fold
= isl_qpolynomial_fold_cow(fold
);
1285 isl_space_free(fold
->dim
);
1286 fold
->dim
= isl_space_copy(morph
->ran
->dim
);
1290 for (i
= 0; i
< fold
->n
; ++i
) {
1291 fold
->qp
[i
] = isl_qpolynomial_morph_domain(fold
->qp
[i
],
1292 isl_morph_copy(morph
));
1297 isl_morph_free(morph
);
1301 isl_qpolynomial_fold_free(fold
);
1302 isl_morph_free(morph
);
1306 enum isl_fold
isl_qpolynomial_fold_get_type(__isl_keep isl_qpolynomial_fold
*fold
)
1309 return isl_fold_list
;
1313 enum isl_fold
isl_union_pw_qpolynomial_fold_get_type(
1314 __isl_keep isl_union_pw_qpolynomial_fold
*upwf
)
1317 return isl_fold_list
;
1321 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_lift(
1322 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_space
*dim
)
1329 if (isl_space_is_equal(fold
->dim
, dim
)) {
1330 isl_space_free(dim
);
1334 fold
= isl_qpolynomial_fold_cow(fold
);
1338 isl_space_free(fold
->dim
);
1339 fold
->dim
= isl_space_copy(dim
);
1343 for (i
= 0; i
< fold
->n
; ++i
) {
1344 fold
->qp
[i
] = isl_qpolynomial_lift(fold
->qp
[i
],
1345 isl_space_copy(dim
));
1350 isl_space_free(dim
);
1354 isl_qpolynomial_fold_free(fold
);
1355 isl_space_free(dim
);
1359 isl_stat
isl_qpolynomial_fold_foreach_qpolynomial(
1360 __isl_keep isl_qpolynomial_fold
*fold
,
1361 isl_stat (*fn
)(__isl_take isl_qpolynomial
*qp
, void *user
), void *user
)
1366 return isl_stat_error
;
1368 for (i
= 0; i
< fold
->n
; ++i
)
1369 if (fn(isl_qpolynomial_copy(fold
->qp
[i
]), user
) < 0)
1370 return isl_stat_error
;
1375 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_move_dims(
1376 __isl_take isl_qpolynomial_fold
*fold
,
1377 enum isl_dim_type dst_type
, unsigned dst_pos
,
1378 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1381 enum isl_dim_type set_src_type
, set_dst_type
;
1386 fold
= isl_qpolynomial_fold_cow(fold
);
1390 set_src_type
= domain_type(src_type
);
1391 set_dst_type
= domain_type(dst_type
);
1393 fold
->dim
= isl_space_move_dims(fold
->dim
, set_dst_type
, dst_pos
,
1394 set_src_type
, src_pos
, n
);
1398 for (i
= 0; i
< fold
->n
; ++i
) {
1399 fold
->qp
[i
] = isl_qpolynomial_move_dims(fold
->qp
[i
],
1400 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1407 isl_qpolynomial_fold_free(fold
);
1411 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
1412 * in fold->qp[k] by subs[i].
1414 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_substitute(
1415 __isl_take isl_qpolynomial_fold
*fold
,
1416 enum isl_dim_type type
, unsigned first
, unsigned n
,
1417 __isl_keep isl_qpolynomial
**subs
)
1424 fold
= isl_qpolynomial_fold_cow(fold
);
1428 for (i
= 0; i
< fold
->n
; ++i
) {
1429 fold
->qp
[i
] = isl_qpolynomial_substitute(fold
->qp
[i
],
1430 type
, first
, n
, subs
);
1437 isl_qpolynomial_fold_free(fold
);
1441 static isl_stat
add_pwqp(__isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
1443 isl_pw_qpolynomial_fold
*pwf
;
1444 isl_union_pw_qpolynomial_fold
**upwf
;
1445 struct isl_hash_table_entry
*entry
;
1447 upwf
= (isl_union_pw_qpolynomial_fold
**)user
;
1449 entry
= isl_union_pw_qpolynomial_fold_find_part_entry(*upwf
,
1454 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial((*upwf
)->type
, pwqp
);
1458 entry
->data
= isl_pw_qpolynomial_fold_add(entry
->data
, pwf
);
1460 return isl_stat_error
;
1461 if (isl_pw_qpolynomial_fold_is_zero(entry
->data
))
1462 *upwf
= isl_union_pw_qpolynomial_fold_remove_part_entry(
1468 isl_pw_qpolynomial_free(pwqp
);
1469 return isl_stat_error
;
1472 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(
1473 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
1474 __isl_take isl_union_pw_qpolynomial
*upwqp
)
1476 upwf
= isl_union_pw_qpolynomial_fold_align_params(upwf
,
1477 isl_union_pw_qpolynomial_get_space(upwqp
));
1478 upwqp
= isl_union_pw_qpolynomial_align_params(upwqp
,
1479 isl_union_pw_qpolynomial_fold_get_space(upwf
));
1481 upwf
= isl_union_pw_qpolynomial_fold_cow(upwf
);
1482 if (!upwf
|| !upwqp
)
1485 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp
, &add_pwqp
,
1489 isl_union_pw_qpolynomial_free(upwqp
);
1493 isl_union_pw_qpolynomial_fold_free(upwf
);
1494 isl_union_pw_qpolynomial_free(upwqp
);
1498 static isl_bool
join_compatible(__isl_keep isl_space
*space1
,
1499 __isl_keep isl_space
*space2
)
1502 m
= isl_space_match(space1
, isl_dim_param
, space2
, isl_dim_param
);
1505 return isl_space_tuple_is_equal(space1
, isl_dim_out
,
1506 space2
, isl_dim_in
);
1509 /* Compute the intersection of the range of the map and the domain
1510 * of the piecewise quasipolynomial reduction and then compute a bound
1511 * on the associated quasipolynomial reduction over all elements
1512 * in this intersection.
1514 * We first introduce some unconstrained dimensions in the
1515 * piecewise quasipolynomial, intersect the resulting domain
1516 * with the wrapped map and the compute the sum.
1518 __isl_give isl_pw_qpolynomial_fold
*isl_map_apply_pw_qpolynomial_fold(
1519 __isl_take isl_map
*map
, __isl_take isl_pw_qpolynomial_fold
*pwf
,
1529 ctx
= isl_map_get_ctx(map
);
1533 map_dim
= isl_map_get_space(map
);
1534 pwf_dim
= isl_pw_qpolynomial_fold_get_space(pwf
);
1535 ok
= join_compatible(map_dim
, pwf_dim
);
1536 isl_space_free(map_dim
);
1537 isl_space_free(pwf_dim
);
1541 isl_die(ctx
, isl_error_invalid
, "incompatible dimensions",
1544 n_in
= isl_map_dim(map
, isl_dim_in
);
1545 pwf
= isl_pw_qpolynomial_fold_insert_dims(pwf
, isl_dim_in
, 0, n_in
);
1547 dom
= isl_map_wrap(map
);
1548 pwf
= isl_pw_qpolynomial_fold_reset_domain_space(pwf
,
1549 isl_set_get_space(dom
));
1551 pwf
= isl_pw_qpolynomial_fold_intersect_domain(pwf
, dom
);
1552 pwf
= isl_pw_qpolynomial_fold_bound(pwf
, tight
);
1557 isl_pw_qpolynomial_fold_free(pwf
);
1561 __isl_give isl_pw_qpolynomial_fold
*isl_set_apply_pw_qpolynomial_fold(
1562 __isl_take isl_set
*set
, __isl_take isl_pw_qpolynomial_fold
*pwf
,
1565 return isl_map_apply_pw_qpolynomial_fold(set
, pwf
, tight
);
1568 struct isl_apply_fold_data
{
1569 isl_union_pw_qpolynomial_fold
*upwf
;
1570 isl_union_pw_qpolynomial_fold
*res
;
1575 static isl_stat
pw_qpolynomial_fold_apply(
1576 __isl_take isl_pw_qpolynomial_fold
*pwf
, void *user
)
1580 struct isl_apply_fold_data
*data
= user
;
1583 map_dim
= isl_map_get_space(data
->map
);
1584 pwf_dim
= isl_pw_qpolynomial_fold_get_space(pwf
);
1585 ok
= join_compatible(map_dim
, pwf_dim
);
1586 isl_space_free(map_dim
);
1587 isl_space_free(pwf_dim
);
1590 return isl_stat_error
;
1592 pwf
= isl_map_apply_pw_qpolynomial_fold(isl_map_copy(data
->map
),
1593 pwf
, data
->tight
? &data
->tight
: NULL
);
1594 data
->res
= isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
1597 isl_pw_qpolynomial_fold_free(pwf
);
1602 static isl_stat
map_apply(__isl_take isl_map
*map
, void *user
)
1604 struct isl_apply_fold_data
*data
= user
;
1608 r
= isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(
1609 data
->upwf
, &pw_qpolynomial_fold_apply
, data
);
1615 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_map_apply_union_pw_qpolynomial_fold(
1616 __isl_take isl_union_map
*umap
,
1617 __isl_take isl_union_pw_qpolynomial_fold
*upwf
, int *tight
)
1621 struct isl_apply_fold_data data
;
1623 upwf
= isl_union_pw_qpolynomial_fold_align_params(upwf
,
1624 isl_union_map_get_space(umap
));
1625 umap
= isl_union_map_align_params(umap
,
1626 isl_union_pw_qpolynomial_fold_get_space(upwf
));
1629 data
.tight
= tight
? 1 : 0;
1630 dim
= isl_union_pw_qpolynomial_fold_get_space(upwf
);
1631 type
= isl_union_pw_qpolynomial_fold_get_type(upwf
);
1632 data
.res
= isl_union_pw_qpolynomial_fold_zero(dim
, type
);
1633 if (isl_union_map_foreach_map(umap
, &map_apply
, &data
) < 0)
1636 isl_union_map_free(umap
);
1637 isl_union_pw_qpolynomial_fold_free(upwf
);
1640 *tight
= data
.tight
;
1644 isl_union_map_free(umap
);
1645 isl_union_pw_qpolynomial_fold_free(upwf
);
1646 isl_union_pw_qpolynomial_fold_free(data
.res
);
1650 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_set_apply_union_pw_qpolynomial_fold(
1651 __isl_take isl_union_set
*uset
,
1652 __isl_take isl_union_pw_qpolynomial_fold
*upwf
, int *tight
)
1654 return isl_union_map_apply_union_pw_qpolynomial_fold(uset
, upwf
, tight
);
1657 /* Reorder the dimension of "fold" according to the given reordering.
1659 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_realign_domain(
1660 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_reordering
*r
)
1664 fold
= isl_qpolynomial_fold_cow(fold
);
1668 for (i
= 0; i
< fold
->n
; ++i
) {
1669 fold
->qp
[i
] = isl_qpolynomial_realign_domain(fold
->qp
[i
],
1670 isl_reordering_copy(r
));
1675 fold
= isl_qpolynomial_fold_reset_domain_space(fold
,
1676 isl_space_copy(r
->dim
));
1678 isl_reordering_free(r
);
1682 isl_qpolynomial_fold_free(fold
);
1683 isl_reordering_free(r
);
1687 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_mul_isl_int(
1688 __isl_take isl_qpolynomial_fold
*fold
, isl_int v
)
1692 if (isl_int_is_one(v
))
1694 if (fold
&& isl_int_is_zero(v
)) {
1695 isl_qpolynomial_fold
*zero
;
1696 isl_space
*dim
= isl_space_copy(fold
->dim
);
1697 zero
= isl_qpolynomial_fold_empty(fold
->type
, dim
);
1698 isl_qpolynomial_fold_free(fold
);
1702 fold
= isl_qpolynomial_fold_cow(fold
);
1706 if (isl_int_is_neg(v
))
1707 fold
->type
= isl_fold_type_negate(fold
->type
);
1708 for (i
= 0; i
< fold
->n
; ++i
) {
1709 fold
->qp
[i
] = isl_qpolynomial_mul_isl_int(fold
->qp
[i
], v
);
1716 isl_qpolynomial_fold_free(fold
);
1720 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_scale(
1721 __isl_take isl_qpolynomial_fold
*fold
, isl_int v
)
1723 return isl_qpolynomial_fold_mul_isl_int(fold
, v
);
1726 /* Multiply "fold" by "v".
1728 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_scale_val(
1729 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_val
*v
)
1736 if (isl_val_is_one(v
)) {
1740 if (isl_val_is_zero(v
)) {
1741 isl_qpolynomial_fold
*zero
;
1742 isl_space
*space
= isl_qpolynomial_fold_get_domain_space(fold
);
1743 zero
= isl_qpolynomial_fold_empty(fold
->type
, space
);
1744 isl_qpolynomial_fold_free(fold
);
1748 if (!isl_val_is_rat(v
))
1749 isl_die(isl_qpolynomial_fold_get_ctx(fold
), isl_error_invalid
,
1750 "expecting rational factor", goto error
);
1752 fold
= isl_qpolynomial_fold_cow(fold
);
1756 if (isl_val_is_neg(v
))
1757 fold
->type
= isl_fold_type_negate(fold
->type
);
1758 for (i
= 0; i
< fold
->n
; ++i
) {
1759 fold
->qp
[i
] = isl_qpolynomial_scale_val(fold
->qp
[i
],
1769 isl_qpolynomial_fold_free(fold
);
1773 /* Divide "fold" by "v".
1775 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_scale_down_val(
1776 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_val
*v
)
1781 if (isl_val_is_one(v
)) {
1785 if (!isl_val_is_rat(v
))
1786 isl_die(isl_qpolynomial_fold_get_ctx(fold
), isl_error_invalid
,
1787 "expecting rational factor", goto error
);
1788 if (isl_val_is_zero(v
))
1789 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1790 "cannot scale down by zero", goto error
);
1792 return isl_qpolynomial_fold_scale_val(fold
, isl_val_inv(v
));
1795 isl_qpolynomial_fold_free(fold
);