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,
11 #include <isl_map_private.h>
12 #include <isl_union_map_private.h>
13 #include <isl_polynomial_private.h>
14 #include <isl_point_private.h>
15 #include <isl_space_private.h>
16 #include <isl_lp_private.h>
18 #include <isl_mat_private.h>
19 #include <isl_val_private.h>
20 #include <isl_vec_private.h>
21 #include <isl_config.h>
24 #define BASE pw_qpolynomial_fold
26 #include <isl_list_templ.c>
28 enum isl_fold
isl_fold_type_negate(enum isl_fold type
)
39 isl_die(NULL
, isl_error_internal
, "unhandled isl_fold type", abort());
42 static __isl_give isl_qpolynomial_fold
*qpolynomial_fold_alloc(
43 enum isl_fold type
, __isl_take isl_space
*space
, int n
)
45 isl_qpolynomial_fold
*fold
;
50 isl_assert(space
->ctx
, n
>= 0, goto error
);
51 fold
= isl_calloc(space
->ctx
, struct isl_qpolynomial_fold
,
52 sizeof(struct isl_qpolynomial_fold
) +
53 (n
- 1) * sizeof(struct isl_qpolynomial
*));
65 isl_space_free(space
);
69 isl_ctx
*isl_qpolynomial_fold_get_ctx(__isl_keep isl_qpolynomial_fold
*fold
)
71 return fold
? fold
->dim
->ctx
: NULL
;
74 __isl_give isl_space
*isl_qpolynomial_fold_get_domain_space(
75 __isl_keep isl_qpolynomial_fold
*fold
)
77 return fold
? isl_space_copy(fold
->dim
) : NULL
;
80 __isl_give isl_space
*isl_qpolynomial_fold_get_space(
81 __isl_keep isl_qpolynomial_fold
*fold
)
86 space
= isl_space_copy(fold
->dim
);
87 space
= isl_space_from_domain(space
);
88 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
92 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_reset_domain_space(
93 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_space
*dim
)
97 fold
= isl_qpolynomial_fold_cow(fold
);
101 for (i
= 0; i
< fold
->n
; ++i
) {
102 fold
->qp
[i
] = isl_qpolynomial_reset_domain_space(fold
->qp
[i
],
103 isl_space_copy(dim
));
108 isl_space_free(fold
->dim
);
113 isl_qpolynomial_fold_free(fold
);
118 /* Reset the space of "fold". This function is called from isl_pw_templ.c
119 * and doesn't know if the space of an element object is represented
120 * directly or through its domain. It therefore passes along both.
122 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_reset_space_and_domain(
123 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_space
*space
,
124 __isl_take isl_space
*domain
)
126 isl_space_free(space
);
127 return isl_qpolynomial_fold_reset_domain_space(fold
, domain
);
130 int isl_qpolynomial_fold_involves_dims(__isl_keep isl_qpolynomial_fold
*fold
,
131 enum isl_dim_type type
, unsigned first
, unsigned n
)
137 if (fold
->n
== 0 || n
== 0)
140 for (i
= 0; i
< fold
->n
; ++i
) {
141 int involves
= isl_qpolynomial_involves_dims(fold
->qp
[i
],
143 if (involves
< 0 || involves
)
149 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_set_dim_name(
150 __isl_take isl_qpolynomial_fold
*fold
,
151 enum isl_dim_type type
, unsigned pos
, const char *s
)
155 fold
= isl_qpolynomial_fold_cow(fold
);
158 fold
->dim
= isl_space_set_dim_name(fold
->dim
, type
, pos
, s
);
162 for (i
= 0; i
< fold
->n
; ++i
) {
163 fold
->qp
[i
] = isl_qpolynomial_set_dim_name(fold
->qp
[i
],
171 isl_qpolynomial_fold_free(fold
);
175 /* Given a dimension type for an isl_qpolynomial_fold,
176 * return the corresponding type for the domain.
178 static enum isl_dim_type
domain_type(enum isl_dim_type type
)
180 if (type
== isl_dim_in
)
185 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_drop_dims(
186 __isl_take isl_qpolynomial_fold
*fold
,
187 enum isl_dim_type type
, unsigned first
, unsigned n
)
190 enum isl_dim_type set_type
;
197 set_type
= domain_type(type
);
199 fold
= isl_qpolynomial_fold_cow(fold
);
202 fold
->dim
= isl_space_drop_dims(fold
->dim
, set_type
, first
, n
);
206 for (i
= 0; i
< fold
->n
; ++i
) {
207 fold
->qp
[i
] = isl_qpolynomial_drop_dims(fold
->qp
[i
],
215 isl_qpolynomial_fold_free(fold
);
219 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_insert_dims(
220 __isl_take isl_qpolynomial_fold
*fold
,
221 enum isl_dim_type type
, unsigned first
, unsigned n
)
227 if (n
== 0 && !isl_space_is_named_or_nested(fold
->dim
, type
))
230 fold
= isl_qpolynomial_fold_cow(fold
);
233 fold
->dim
= isl_space_insert_dims(fold
->dim
, type
, first
, n
);
237 for (i
= 0; i
< fold
->n
; ++i
) {
238 fold
->qp
[i
] = isl_qpolynomial_insert_dims(fold
->qp
[i
],
246 isl_qpolynomial_fold_free(fold
);
250 /* Determine the sign of the constant quasipolynomial "qp".
257 * For qp == 0, we can return either -1 or 1. In practice, we return 1.
258 * For qp == NaN, the sign is undefined, so we return 0.
260 static int isl_qpolynomial_cst_sign(__isl_keep isl_qpolynomial
*qp
)
264 if (isl_qpolynomial_is_nan(qp
))
267 cst
= isl_poly_as_cst(qp
->poly
);
271 return isl_int_sgn(cst
->n
) < 0 ? -1 : 1;
274 static int isl_qpolynomial_aff_sign(__isl_keep isl_set
*set
,
275 __isl_keep isl_qpolynomial
*qp
)
277 enum isl_lp_result res
;
282 aff
= isl_qpolynomial_extract_affine(qp
);
288 res
= isl_set_solve_lp(set
, 0, aff
->el
+ 1, aff
->el
[0],
290 if (res
== isl_lp_error
)
292 if (res
== isl_lp_empty
||
293 (res
== isl_lp_ok
&& !isl_int_is_neg(opt
))) {
298 res
= isl_set_solve_lp(set
, 1, aff
->el
+ 1, aff
->el
[0],
300 if (res
== isl_lp_ok
&& !isl_int_is_pos(opt
))
309 /* Determine, if possible, the sign of the quasipolynomial "qp" on
312 * If qp is a constant, then the problem is trivial.
313 * If qp is linear, then we check if the minimum of the corresponding
314 * affine constraint is non-negative or if the maximum is non-positive.
316 * Otherwise, we check if the outermost variable "v" has a lower bound "l"
317 * in "set". If so, we write qp(v,v') as
319 * q(v,v') * (v - l) + r(v')
321 * if q(v,v') and r(v') have the same known sign, then the original
322 * quasipolynomial has the same sign as well.
329 static int isl_qpolynomial_sign(__isl_keep isl_set
*set
,
330 __isl_keep isl_qpolynomial
*qp
)
338 enum isl_lp_result res
;
341 is
= isl_qpolynomial_is_cst(qp
, NULL
, NULL
);
345 return isl_qpolynomial_cst_sign(qp
);
347 is
= isl_qpolynomial_is_affine(qp
);
351 return isl_qpolynomial_aff_sign(set
, qp
);
353 if (qp
->div
->n_row
> 0)
356 rec
= isl_poly_as_rec(qp
->poly
);
360 d
= isl_space_dim(qp
->dim
, isl_dim_all
);
361 v
= isl_vec_alloc(set
->ctx
, 2 + d
);
365 isl_seq_clr(v
->el
+ 1, 1 + d
);
366 isl_int_set_si(v
->el
[0], 1);
367 isl_int_set_si(v
->el
[2 + qp
->poly
->var
], 1);
371 res
= isl_set_solve_lp(set
, 0, v
->el
+ 1, v
->el
[0], &l
, NULL
, NULL
);
372 if (res
== isl_lp_ok
) {
373 isl_qpolynomial
*min
;
374 isl_qpolynomial
*base
;
375 isl_qpolynomial
*r
, *q
;
378 min
= isl_qpolynomial_cst_on_domain(isl_space_copy(qp
->dim
), l
);
379 base
= isl_qpolynomial_var_pow_on_domain(isl_space_copy(qp
->dim
),
382 r
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), 0,
383 isl_poly_copy(rec
->p
[rec
->n
- 1]));
384 q
= isl_qpolynomial_copy(r
);
386 for (i
= rec
->n
- 2; i
>= 0; --i
) {
387 r
= isl_qpolynomial_mul(r
, isl_qpolynomial_copy(min
));
388 t
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), 0,
389 isl_poly_copy(rec
->p
[i
]));
390 r
= isl_qpolynomial_add(r
, t
);
393 q
= isl_qpolynomial_mul(q
, isl_qpolynomial_copy(base
));
394 q
= isl_qpolynomial_add(q
, isl_qpolynomial_copy(r
));
397 if (isl_qpolynomial_is_zero(q
))
398 sgn
= isl_qpolynomial_sign(set
, r
);
399 else if (isl_qpolynomial_is_zero(r
))
400 sgn
= isl_qpolynomial_sign(set
, q
);
403 sgn_r
= isl_qpolynomial_sign(set
, r
);
404 sgn_q
= isl_qpolynomial_sign(set
, q
);
409 isl_qpolynomial_free(min
);
410 isl_qpolynomial_free(base
);
411 isl_qpolynomial_free(q
);
412 isl_qpolynomial_free(r
);
422 /* Combine "fold1" and "fold2" into a single reduction, eliminating
423 * those elements of one reduction that are already covered by the other
424 * reduction on "set".
426 * If "fold1" or "fold2" is an empty reduction, then return
427 * the other reduction.
428 * If "fold1" or "fold2" is a NaN, then return this NaN.
430 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_fold_on_domain(
431 __isl_keep isl_set
*set
,
432 __isl_take isl_qpolynomial_fold
*fold1
,
433 __isl_take isl_qpolynomial_fold
*fold2
)
437 struct isl_qpolynomial_fold
*res
= NULL
;
440 if (!fold1
|| !fold2
)
443 isl_assert(fold1
->dim
->ctx
, fold1
->type
== fold2
->type
, goto error
);
444 isl_assert(fold1
->dim
->ctx
, isl_space_is_equal(fold1
->dim
, fold2
->dim
),
447 better
= fold1
->type
== isl_fold_max
? -1 : 1;
449 if (isl_qpolynomial_fold_is_empty(fold1
) ||
450 isl_qpolynomial_fold_is_nan(fold2
)) {
451 isl_qpolynomial_fold_free(fold1
);
455 if (isl_qpolynomial_fold_is_empty(fold2
) ||
456 isl_qpolynomial_fold_is_nan(fold1
)) {
457 isl_qpolynomial_fold_free(fold2
);
461 res
= qpolynomial_fold_alloc(fold1
->type
, isl_space_copy(fold1
->dim
),
462 fold1
->n
+ fold2
->n
);
466 for (i
= 0; i
< fold1
->n
; ++i
) {
467 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold1
->qp
[i
]);
468 if (!res
->qp
[res
->n
])
474 for (i
= 0; i
< fold2
->n
; ++i
) {
475 for (j
= n1
- 1; j
>= 0; --j
) {
478 equal
= isl_qpolynomial_plain_is_equal(res
->qp
[j
],
484 d
= isl_qpolynomial_sub(
485 isl_qpolynomial_copy(res
->qp
[j
]),
486 isl_qpolynomial_copy(fold2
->qp
[i
]));
487 sgn
= isl_qpolynomial_sign(set
, d
);
488 isl_qpolynomial_free(d
);
493 isl_qpolynomial_free(res
->qp
[j
]);
495 res
->qp
[j
] = res
->qp
[n1
- 1];
497 if (n1
!= res
->n
- 1)
498 res
->qp
[n1
] = res
->qp
[res
->n
- 1];
503 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold2
->qp
[i
]);
504 if (!res
->qp
[res
->n
])
509 isl_qpolynomial_fold_free(fold1
);
510 isl_qpolynomial_fold_free(fold2
);
514 isl_qpolynomial_fold_free(res
);
515 isl_qpolynomial_fold_free(fold1
);
516 isl_qpolynomial_fold_free(fold2
);
520 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_add_qpolynomial(
521 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_qpolynomial
*qp
)
528 if (isl_qpolynomial_is_zero(qp
)) {
529 isl_qpolynomial_free(qp
);
533 fold
= isl_qpolynomial_fold_cow(fold
);
537 for (i
= 0; i
< fold
->n
; ++i
) {
538 fold
->qp
[i
] = isl_qpolynomial_add(fold
->qp
[i
],
539 isl_qpolynomial_copy(qp
));
544 isl_qpolynomial_free(qp
);
547 isl_qpolynomial_fold_free(fold
);
548 isl_qpolynomial_free(qp
);
552 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_add_on_domain(
553 __isl_keep isl_set
*dom
,
554 __isl_take isl_qpolynomial_fold
*fold1
,
555 __isl_take isl_qpolynomial_fold
*fold2
)
558 isl_qpolynomial_fold
*res
= NULL
;
560 if (!fold1
|| !fold2
)
563 if (isl_qpolynomial_fold_is_empty(fold1
)) {
564 isl_qpolynomial_fold_free(fold1
);
568 if (isl_qpolynomial_fold_is_empty(fold2
)) {
569 isl_qpolynomial_fold_free(fold2
);
573 if (fold1
->n
== 1 && fold2
->n
!= 1)
574 return isl_qpolynomial_fold_add_on_domain(dom
, fold2
, fold1
);
577 res
= isl_qpolynomial_fold_add_qpolynomial(fold1
,
578 isl_qpolynomial_copy(fold2
->qp
[0]));
579 isl_qpolynomial_fold_free(fold2
);
583 res
= isl_qpolynomial_fold_add_qpolynomial(
584 isl_qpolynomial_fold_copy(fold1
),
585 isl_qpolynomial_copy(fold2
->qp
[0]));
587 for (i
= 1; i
< fold2
->n
; ++i
) {
588 isl_qpolynomial_fold
*res_i
;
589 res_i
= isl_qpolynomial_fold_add_qpolynomial(
590 isl_qpolynomial_fold_copy(fold1
),
591 isl_qpolynomial_copy(fold2
->qp
[i
]));
592 res
= isl_qpolynomial_fold_fold_on_domain(dom
, res
, res_i
);
595 isl_qpolynomial_fold_free(fold1
);
596 isl_qpolynomial_fold_free(fold2
);
599 isl_qpolynomial_fold_free(res
);
600 isl_qpolynomial_fold_free(fold1
);
601 isl_qpolynomial_fold_free(fold2
);
605 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_substitute_equalities(
606 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_basic_set
*eq
)
613 fold
= isl_qpolynomial_fold_cow(fold
);
617 for (i
= 0; i
< fold
->n
; ++i
) {
618 fold
->qp
[i
] = isl_qpolynomial_substitute_equalities(fold
->qp
[i
],
619 isl_basic_set_copy(eq
));
624 isl_basic_set_free(eq
);
627 isl_basic_set_free(eq
);
628 isl_qpolynomial_fold_free(fold
);
632 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_gist(
633 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_set
*context
)
637 if (!fold
|| !context
)
640 fold
= isl_qpolynomial_fold_cow(fold
);
644 for (i
= 0; i
< fold
->n
; ++i
) {
645 fold
->qp
[i
] = isl_qpolynomial_gist(fold
->qp
[i
],
646 isl_set_copy(context
));
651 isl_set_free(context
);
654 isl_set_free(context
);
655 isl_qpolynomial_fold_free(fold
);
659 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_gist_params(
660 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_set
*context
)
662 isl_space
*space
= isl_qpolynomial_fold_get_domain_space(fold
);
663 isl_set
*dom_context
= isl_set_universe(space
);
664 dom_context
= isl_set_intersect_params(dom_context
, context
);
665 return isl_qpolynomial_fold_gist(fold
, dom_context
);
668 #define isl_qpolynomial_fold_involves_nan isl_qpolynomial_fold_is_nan
673 #define PW isl_pw_qpolynomial_fold
675 #define EL isl_qpolynomial_fold
677 #define EL_IS_ZERO is_empty
681 #define IS_ZERO is_zero
684 #undef DEFAULT_IS_ZERO
685 #define DEFAULT_IS_ZERO 1
691 #include <isl_pw_templ.c>
692 #include <isl_pw_eval.c>
695 #define BASE 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 __isl_null isl_qpolynomial_fold
*isl_qpolynomial_fold_free(
779 __isl_take isl_qpolynomial_fold
*fold
)
788 for (i
= 0; i
< fold
->n
; ++i
)
789 isl_qpolynomial_free(fold
->qp
[i
]);
790 isl_space_free(fold
->dim
);
796 int isl_qpolynomial_fold_is_empty(__isl_keep isl_qpolynomial_fold
*fold
)
804 /* Does "fold" represent max(NaN) or min(NaN)?
806 isl_bool
isl_qpolynomial_fold_is_nan(__isl_keep isl_qpolynomial_fold
*fold
)
809 return isl_bool_error
;
811 return isl_bool_false
;
812 return isl_qpolynomial_is_nan(fold
->qp
[0]);
815 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_fold(
816 __isl_take isl_qpolynomial_fold
*fold1
,
817 __isl_take isl_qpolynomial_fold
*fold2
)
820 struct isl_qpolynomial_fold
*res
= NULL
;
822 if (!fold1
|| !fold2
)
825 isl_assert(fold1
->dim
->ctx
, fold1
->type
== fold2
->type
, goto error
);
826 isl_assert(fold1
->dim
->ctx
, isl_space_is_equal(fold1
->dim
, fold2
->dim
),
829 if (isl_qpolynomial_fold_is_empty(fold1
)) {
830 isl_qpolynomial_fold_free(fold1
);
834 if (isl_qpolynomial_fold_is_empty(fold2
)) {
835 isl_qpolynomial_fold_free(fold2
);
839 res
= qpolynomial_fold_alloc(fold1
->type
, isl_space_copy(fold1
->dim
),
840 fold1
->n
+ fold2
->n
);
844 for (i
= 0; i
< fold1
->n
; ++i
) {
845 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold1
->qp
[i
]);
846 if (!res
->qp
[res
->n
])
851 for (i
= 0; i
< fold2
->n
; ++i
) {
852 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold2
->qp
[i
]);
853 if (!res
->qp
[res
->n
])
858 isl_qpolynomial_fold_free(fold1
);
859 isl_qpolynomial_fold_free(fold2
);
863 isl_qpolynomial_fold_free(res
);
864 isl_qpolynomial_fold_free(fold1
);
865 isl_qpolynomial_fold_free(fold2
);
869 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_fold_fold(
870 __isl_take isl_pw_qpolynomial_fold
*pw1
,
871 __isl_take isl_pw_qpolynomial_fold
*pw2
)
874 struct isl_pw_qpolynomial_fold
*res
;
880 isl_assert(pw1
->dim
->ctx
, isl_space_is_equal(pw1
->dim
, pw2
->dim
), goto error
);
882 if (isl_pw_qpolynomial_fold_is_zero(pw1
)) {
883 isl_pw_qpolynomial_fold_free(pw1
);
887 if (isl_pw_qpolynomial_fold_is_zero(pw2
)) {
888 isl_pw_qpolynomial_fold_free(pw2
);
892 if (pw1
->type
!= pw2
->type
)
893 isl_die(pw1
->dim
->ctx
, isl_error_invalid
,
894 "fold types don't match", goto error
);
896 n
= (pw1
->n
+ 1) * (pw2
->n
+ 1);
897 res
= isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pw1
->dim
),
900 for (i
= 0; i
< pw1
->n
; ++i
) {
901 set
= isl_set_copy(pw1
->p
[i
].set
);
902 for (j
= 0; j
< pw2
->n
; ++j
) {
903 struct isl_set
*common
;
904 isl_qpolynomial_fold
*sum
;
905 set
= isl_set_subtract(set
,
906 isl_set_copy(pw2
->p
[j
].set
));
907 common
= isl_set_intersect(isl_set_copy(pw1
->p
[i
].set
),
908 isl_set_copy(pw2
->p
[j
].set
));
909 if (isl_set_plain_is_empty(common
)) {
910 isl_set_free(common
);
914 sum
= isl_qpolynomial_fold_fold_on_domain(common
,
915 isl_qpolynomial_fold_copy(pw1
->p
[i
].fold
),
916 isl_qpolynomial_fold_copy(pw2
->p
[j
].fold
));
918 res
= isl_pw_qpolynomial_fold_add_piece(res
, common
, sum
);
920 res
= isl_pw_qpolynomial_fold_add_piece(res
, set
,
921 isl_qpolynomial_fold_copy(pw1
->p
[i
].fold
));
924 for (j
= 0; j
< pw2
->n
; ++j
) {
925 set
= isl_set_copy(pw2
->p
[j
].set
);
926 for (i
= 0; i
< pw1
->n
; ++i
)
927 set
= isl_set_subtract(set
, isl_set_copy(pw1
->p
[i
].set
));
928 res
= isl_pw_qpolynomial_fold_add_piece(res
, set
,
929 isl_qpolynomial_fold_copy(pw2
->p
[j
].fold
));
932 isl_pw_qpolynomial_fold_free(pw1
);
933 isl_pw_qpolynomial_fold_free(pw2
);
937 isl_pw_qpolynomial_fold_free(pw1
);
938 isl_pw_qpolynomial_fold_free(pw2
);
942 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
943 __isl_take isl_union_pw_qpolynomial_fold
*u
,
944 __isl_take isl_pw_qpolynomial_fold
*part
)
946 struct isl_hash_table_entry
*entry
;
948 u
= isl_union_pw_qpolynomial_fold_cow(u
);
952 if (isl_space_check_equal_params(part
->dim
, u
->space
) < 0)
955 entry
= isl_union_pw_qpolynomial_fold_find_part_entry(u
, part
->dim
, 1);
962 entry
->data
= isl_pw_qpolynomial_fold_fold(entry
->data
,
963 isl_pw_qpolynomial_fold_copy(part
));
966 isl_pw_qpolynomial_fold_free(part
);
971 isl_pw_qpolynomial_fold_free(part
);
972 isl_union_pw_qpolynomial_fold_free(u
);
976 static isl_stat
fold_part(__isl_take isl_pw_qpolynomial_fold
*part
, void *user
)
978 isl_union_pw_qpolynomial_fold
**u
;
979 u
= (isl_union_pw_qpolynomial_fold
**)user
;
981 *u
= isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(*u
, part
);
986 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_pw_qpolynomial_fold_fold(
987 __isl_take isl_union_pw_qpolynomial_fold
*u1
,
988 __isl_take isl_union_pw_qpolynomial_fold
*u2
)
990 u1
= isl_union_pw_qpolynomial_fold_cow(u1
);
995 if (isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(u2
,
996 &fold_part
, &u1
) < 0)
999 isl_union_pw_qpolynomial_fold_free(u2
);
1003 isl_union_pw_qpolynomial_fold_free(u1
);
1004 isl_union_pw_qpolynomial_fold_free(u2
);
1008 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_fold_from_pw_qpolynomial(
1009 enum isl_fold type
, __isl_take isl_pw_qpolynomial
*pwqp
)
1012 isl_pw_qpolynomial_fold
*pwf
;
1017 pwf
= isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pwqp
->dim
),
1020 for (i
= 0; i
< pwqp
->n
; ++i
)
1021 pwf
= isl_pw_qpolynomial_fold_add_piece(pwf
,
1022 isl_set_copy(pwqp
->p
[i
].set
),
1023 isl_qpolynomial_fold_alloc(type
,
1024 isl_qpolynomial_copy(pwqp
->p
[i
].qp
)));
1026 isl_pw_qpolynomial_free(pwqp
);
1031 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_fold_add(
1032 __isl_take isl_pw_qpolynomial_fold
*pwf1
,
1033 __isl_take isl_pw_qpolynomial_fold
*pwf2
)
1035 return isl_pw_qpolynomial_fold_union_add_(pwf1
, pwf2
);
1038 /* Compare two quasi-polynomial reductions.
1040 * Return -1 if "fold1" is "smaller" than "fold2", 1 if "fold1" is "greater"
1041 * than "fold2" and 0 if they are equal.
1043 int isl_qpolynomial_fold_plain_cmp(__isl_keep isl_qpolynomial_fold
*fold1
,
1044 __isl_keep isl_qpolynomial_fold
*fold2
)
1055 if (fold1
->n
!= fold2
->n
)
1056 return fold1
->n
- fold2
->n
;
1058 for (i
= 0; i
< fold1
->n
; ++i
) {
1061 cmp
= isl_qpolynomial_plain_cmp(fold1
->qp
[i
], fold2
->qp
[i
]);
1069 int isl_qpolynomial_fold_plain_is_equal(__isl_keep isl_qpolynomial_fold
*fold1
,
1070 __isl_keep isl_qpolynomial_fold
*fold2
)
1074 if (!fold1
|| !fold2
)
1077 if (fold1
->n
!= fold2
->n
)
1080 /* We probably want to sort the qps first... */
1081 for (i
= 0; i
< fold1
->n
; ++i
) {
1082 int eq
= isl_qpolynomial_plain_is_equal(fold1
->qp
[i
], fold2
->qp
[i
]);
1090 __isl_give isl_val
*isl_qpolynomial_fold_eval(
1091 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_point
*pnt
)
1098 ctx
= isl_point_get_ctx(pnt
);
1099 isl_assert(pnt
->dim
->ctx
, isl_space_is_equal(pnt
->dim
, fold
->dim
), goto error
);
1100 isl_assert(pnt
->dim
->ctx
,
1101 fold
->type
== isl_fold_max
|| fold
->type
== isl_fold_min
,
1105 v
= isl_val_zero(ctx
);
1108 v
= isl_qpolynomial_eval(isl_qpolynomial_copy(fold
->qp
[0]),
1109 isl_point_copy(pnt
));
1110 for (i
= 1; i
< fold
->n
; ++i
) {
1112 v_i
= isl_qpolynomial_eval(
1113 isl_qpolynomial_copy(fold
->qp
[i
]),
1114 isl_point_copy(pnt
));
1115 if (fold
->type
== isl_fold_max
)
1116 v
= isl_val_max(v
, v_i
);
1118 v
= isl_val_min(v
, v_i
);
1121 isl_qpolynomial_fold_free(fold
);
1122 isl_point_free(pnt
);
1126 isl_qpolynomial_fold_free(fold
);
1127 isl_point_free(pnt
);
1131 size_t isl_pw_qpolynomial_fold_size(__isl_keep isl_pw_qpolynomial_fold
*pwf
)
1136 for (i
= 0; i
< pwf
->n
; ++i
)
1137 n
+= pwf
->p
[i
].fold
->n
;
1142 __isl_give isl_val
*isl_qpolynomial_fold_opt_on_domain(
1143 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_set
*set
, int max
)
1152 opt
= isl_val_zero(isl_set_get_ctx(set
));
1154 isl_qpolynomial_fold_free(fold
);
1158 opt
= isl_qpolynomial_opt_on_domain(isl_qpolynomial_copy(fold
->qp
[0]),
1159 isl_set_copy(set
), max
);
1160 for (i
= 1; i
< fold
->n
; ++i
) {
1162 opt_i
= isl_qpolynomial_opt_on_domain(
1163 isl_qpolynomial_copy(fold
->qp
[i
]),
1164 isl_set_copy(set
), max
);
1166 opt
= isl_val_max(opt
, opt_i
);
1168 opt
= isl_val_min(opt
, opt_i
);
1172 isl_qpolynomial_fold_free(fold
);
1177 isl_qpolynomial_fold_free(fold
);
1181 /* Check whether for each quasi-polynomial in "fold2" there is
1182 * a quasi-polynomial in "fold1" that dominates it on "set".
1184 static isl_bool
qpolynomial_fold_covers_on_domain(__isl_keep isl_set
*set
,
1185 __isl_keep isl_qpolynomial_fold
*fold1
,
1186 __isl_keep isl_qpolynomial_fold
*fold2
)
1191 if (!set
|| !fold1
|| !fold2
)
1192 return isl_bool_error
;
1194 covers
= fold1
->type
== isl_fold_max
? 1 : -1;
1196 for (i
= 0; i
< fold2
->n
; ++i
) {
1197 for (j
= 0; j
< fold1
->n
; ++j
) {
1201 d
= isl_qpolynomial_sub(
1202 isl_qpolynomial_copy(fold1
->qp
[j
]),
1203 isl_qpolynomial_copy(fold2
->qp
[i
]));
1204 sgn
= isl_qpolynomial_sign(set
, d
);
1205 isl_qpolynomial_free(d
);
1210 return isl_bool_false
;
1213 return isl_bool_true
;
1216 /* Check whether "pwf1" dominated "pwf2", i.e., the domain of "pwf1" contains
1217 * that of "pwf2" and on each cell, the corresponding fold from pwf1 dominates
1220 isl_bool
isl_pw_qpolynomial_fold_covers(
1221 __isl_keep isl_pw_qpolynomial_fold
*pwf1
,
1222 __isl_keep isl_pw_qpolynomial_fold
*pwf2
)
1225 isl_set
*dom1
, *dom2
;
1229 return isl_bool_error
;
1232 return isl_bool_true
;
1234 return isl_bool_false
;
1236 dom1
= isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf1
));
1237 dom2
= isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf2
));
1238 is_subset
= isl_set_is_subset(dom2
, dom1
);
1242 if (is_subset
< 0 || !is_subset
)
1245 for (i
= 0; i
< pwf2
->n
; ++i
) {
1246 for (j
= 0; j
< pwf1
->n
; ++j
) {
1251 common
= isl_set_intersect(isl_set_copy(pwf1
->p
[j
].set
),
1252 isl_set_copy(pwf2
->p
[i
].set
));
1253 is_empty
= isl_set_is_empty(common
);
1254 if (is_empty
< 0 || is_empty
) {
1255 isl_set_free(common
);
1257 return isl_bool_error
;
1260 covers
= qpolynomial_fold_covers_on_domain(common
,
1261 pwf1
->p
[j
].fold
, pwf2
->p
[i
].fold
);
1262 isl_set_free(common
);
1263 if (covers
< 0 || !covers
)
1268 return isl_bool_true
;
1271 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_morph_domain(
1272 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_morph
*morph
)
1277 if (!fold
|| !morph
)
1280 ctx
= fold
->dim
->ctx
;
1281 isl_assert(ctx
, isl_space_is_equal(fold
->dim
, morph
->dom
->dim
), goto error
);
1283 fold
= isl_qpolynomial_fold_cow(fold
);
1287 isl_space_free(fold
->dim
);
1288 fold
->dim
= isl_space_copy(morph
->ran
->dim
);
1292 for (i
= 0; i
< fold
->n
; ++i
) {
1293 fold
->qp
[i
] = isl_qpolynomial_morph_domain(fold
->qp
[i
],
1294 isl_morph_copy(morph
));
1299 isl_morph_free(morph
);
1303 isl_qpolynomial_fold_free(fold
);
1304 isl_morph_free(morph
);
1308 enum isl_fold
isl_qpolynomial_fold_get_type(__isl_keep isl_qpolynomial_fold
*fold
)
1311 return isl_fold_list
;
1315 enum isl_fold
isl_union_pw_qpolynomial_fold_get_type(
1316 __isl_keep isl_union_pw_qpolynomial_fold
*upwf
)
1319 return isl_fold_list
;
1323 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_lift(
1324 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_space
*dim
)
1331 if (isl_space_is_equal(fold
->dim
, dim
)) {
1332 isl_space_free(dim
);
1336 fold
= isl_qpolynomial_fold_cow(fold
);
1340 isl_space_free(fold
->dim
);
1341 fold
->dim
= isl_space_copy(dim
);
1345 for (i
= 0; i
< fold
->n
; ++i
) {
1346 fold
->qp
[i
] = isl_qpolynomial_lift(fold
->qp
[i
],
1347 isl_space_copy(dim
));
1352 isl_space_free(dim
);
1356 isl_qpolynomial_fold_free(fold
);
1357 isl_space_free(dim
);
1361 isl_stat
isl_qpolynomial_fold_foreach_qpolynomial(
1362 __isl_keep isl_qpolynomial_fold
*fold
,
1363 isl_stat (*fn
)(__isl_take isl_qpolynomial
*qp
, void *user
), void *user
)
1368 return isl_stat_error
;
1370 for (i
= 0; i
< fold
->n
; ++i
)
1371 if (fn(isl_qpolynomial_copy(fold
->qp
[i
]), user
) < 0)
1372 return isl_stat_error
;
1377 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_move_dims(
1378 __isl_take isl_qpolynomial_fold
*fold
,
1379 enum isl_dim_type dst_type
, unsigned dst_pos
,
1380 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1383 enum isl_dim_type set_src_type
, set_dst_type
;
1388 fold
= isl_qpolynomial_fold_cow(fold
);
1392 set_src_type
= domain_type(src_type
);
1393 set_dst_type
= domain_type(dst_type
);
1395 fold
->dim
= isl_space_move_dims(fold
->dim
, set_dst_type
, dst_pos
,
1396 set_src_type
, src_pos
, n
);
1400 for (i
= 0; i
< fold
->n
; ++i
) {
1401 fold
->qp
[i
] = isl_qpolynomial_move_dims(fold
->qp
[i
],
1402 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1409 isl_qpolynomial_fold_free(fold
);
1413 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
1414 * in fold->qp[k] by subs[i].
1416 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_substitute(
1417 __isl_take isl_qpolynomial_fold
*fold
,
1418 enum isl_dim_type type
, unsigned first
, unsigned n
,
1419 __isl_keep isl_qpolynomial
**subs
)
1426 fold
= isl_qpolynomial_fold_cow(fold
);
1430 for (i
= 0; i
< fold
->n
; ++i
) {
1431 fold
->qp
[i
] = isl_qpolynomial_substitute(fold
->qp
[i
],
1432 type
, first
, n
, subs
);
1439 isl_qpolynomial_fold_free(fold
);
1443 static isl_stat
add_pwqp(__isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
1445 isl_pw_qpolynomial_fold
*pwf
;
1446 isl_union_pw_qpolynomial_fold
**upwf
;
1447 struct isl_hash_table_entry
*entry
;
1449 upwf
= (isl_union_pw_qpolynomial_fold
**)user
;
1451 entry
= isl_union_pw_qpolynomial_fold_find_part_entry(*upwf
,
1456 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial((*upwf
)->type
, pwqp
);
1460 entry
->data
= isl_pw_qpolynomial_fold_add(entry
->data
, pwf
);
1462 return isl_stat_error
;
1463 if (isl_pw_qpolynomial_fold_is_zero(entry
->data
))
1464 *upwf
= isl_union_pw_qpolynomial_fold_remove_part_entry(
1470 isl_pw_qpolynomial_free(pwqp
);
1471 return isl_stat_error
;
1474 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(
1475 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
1476 __isl_take isl_union_pw_qpolynomial
*upwqp
)
1478 upwf
= isl_union_pw_qpolynomial_fold_align_params(upwf
,
1479 isl_union_pw_qpolynomial_get_space(upwqp
));
1480 upwqp
= isl_union_pw_qpolynomial_align_params(upwqp
,
1481 isl_union_pw_qpolynomial_fold_get_space(upwf
));
1483 upwf
= isl_union_pw_qpolynomial_fold_cow(upwf
);
1484 if (!upwf
|| !upwqp
)
1487 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp
, &add_pwqp
,
1491 isl_union_pw_qpolynomial_free(upwqp
);
1495 isl_union_pw_qpolynomial_fold_free(upwf
);
1496 isl_union_pw_qpolynomial_free(upwqp
);
1500 static isl_bool
join_compatible(__isl_keep isl_space
*space1
,
1501 __isl_keep isl_space
*space2
)
1504 m
= isl_space_has_equal_params(space1
, space2
);
1507 return isl_space_tuple_is_equal(space1
, isl_dim_out
,
1508 space2
, isl_dim_in
);
1511 /* Compute the intersection of the range of the map and the domain
1512 * of the piecewise quasipolynomial reduction and then compute a bound
1513 * on the associated quasipolynomial reduction over all elements
1514 * in this intersection.
1516 * We first introduce some unconstrained dimensions in the
1517 * piecewise quasipolynomial, intersect the resulting domain
1518 * with the wrapped map and the compute the sum.
1520 __isl_give isl_pw_qpolynomial_fold
*isl_map_apply_pw_qpolynomial_fold(
1521 __isl_take isl_map
*map
, __isl_take isl_pw_qpolynomial_fold
*pwf
,
1526 isl_space
*map_space
;
1527 isl_space
*pwf_space
;
1531 ctx
= isl_map_get_ctx(map
);
1535 map_space
= isl_map_get_space(map
);
1536 pwf_space
= isl_pw_qpolynomial_fold_get_space(pwf
);
1537 ok
= join_compatible(map_space
, pwf_space
);
1538 isl_space_free(map_space
);
1539 isl_space_free(pwf_space
);
1543 isl_die(ctx
, isl_error_invalid
, "incompatible dimensions",
1546 n_in
= isl_map_dim(map
, isl_dim_in
);
1547 pwf
= isl_pw_qpolynomial_fold_insert_dims(pwf
, isl_dim_in
, 0, n_in
);
1549 dom
= isl_map_wrap(map
);
1550 pwf
= isl_pw_qpolynomial_fold_reset_domain_space(pwf
,
1551 isl_set_get_space(dom
));
1553 pwf
= isl_pw_qpolynomial_fold_intersect_domain(pwf
, dom
);
1554 pwf
= isl_pw_qpolynomial_fold_bound(pwf
, tight
);
1559 isl_pw_qpolynomial_fold_free(pwf
);
1563 __isl_give isl_pw_qpolynomial_fold
*isl_set_apply_pw_qpolynomial_fold(
1564 __isl_take isl_set
*set
, __isl_take isl_pw_qpolynomial_fold
*pwf
,
1567 return isl_map_apply_pw_qpolynomial_fold(set
, pwf
, tight
);
1570 struct isl_apply_fold_data
{
1571 isl_union_pw_qpolynomial_fold
*upwf
;
1572 isl_union_pw_qpolynomial_fold
*res
;
1577 static isl_stat
pw_qpolynomial_fold_apply(
1578 __isl_take isl_pw_qpolynomial_fold
*pwf
, void *user
)
1582 struct isl_apply_fold_data
*data
= user
;
1585 map_dim
= isl_map_get_space(data
->map
);
1586 pwf_dim
= isl_pw_qpolynomial_fold_get_space(pwf
);
1587 ok
= join_compatible(map_dim
, pwf_dim
);
1588 isl_space_free(map_dim
);
1589 isl_space_free(pwf_dim
);
1592 return isl_stat_error
;
1594 pwf
= isl_map_apply_pw_qpolynomial_fold(isl_map_copy(data
->map
),
1595 pwf
, data
->tight
? &data
->tight
: NULL
);
1596 data
->res
= isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
1599 isl_pw_qpolynomial_fold_free(pwf
);
1604 static isl_stat
map_apply(__isl_take isl_map
*map
, void *user
)
1606 struct isl_apply_fold_data
*data
= user
;
1610 r
= isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(
1611 data
->upwf
, &pw_qpolynomial_fold_apply
, data
);
1617 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_map_apply_union_pw_qpolynomial_fold(
1618 __isl_take isl_union_map
*umap
,
1619 __isl_take isl_union_pw_qpolynomial_fold
*upwf
, int *tight
)
1623 struct isl_apply_fold_data data
;
1625 upwf
= isl_union_pw_qpolynomial_fold_align_params(upwf
,
1626 isl_union_map_get_space(umap
));
1627 umap
= isl_union_map_align_params(umap
,
1628 isl_union_pw_qpolynomial_fold_get_space(upwf
));
1631 data
.tight
= tight
? 1 : 0;
1632 dim
= isl_union_pw_qpolynomial_fold_get_space(upwf
);
1633 type
= isl_union_pw_qpolynomial_fold_get_type(upwf
);
1634 data
.res
= isl_union_pw_qpolynomial_fold_zero(dim
, type
);
1635 if (isl_union_map_foreach_map(umap
, &map_apply
, &data
) < 0)
1638 isl_union_map_free(umap
);
1639 isl_union_pw_qpolynomial_fold_free(upwf
);
1642 *tight
= data
.tight
;
1646 isl_union_map_free(umap
);
1647 isl_union_pw_qpolynomial_fold_free(upwf
);
1648 isl_union_pw_qpolynomial_fold_free(data
.res
);
1652 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_set_apply_union_pw_qpolynomial_fold(
1653 __isl_take isl_union_set
*uset
,
1654 __isl_take isl_union_pw_qpolynomial_fold
*upwf
, int *tight
)
1656 return isl_union_map_apply_union_pw_qpolynomial_fold(uset
, upwf
, tight
);
1659 /* Reorder the dimension of "fold" according to the given reordering.
1661 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_realign_domain(
1662 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_reordering
*r
)
1667 fold
= isl_qpolynomial_fold_cow(fold
);
1671 for (i
= 0; i
< fold
->n
; ++i
) {
1672 fold
->qp
[i
] = isl_qpolynomial_realign_domain(fold
->qp
[i
],
1673 isl_reordering_copy(r
));
1678 space
= isl_reordering_get_space(r
);
1679 fold
= isl_qpolynomial_fold_reset_domain_space(fold
, space
);
1681 isl_reordering_free(r
);
1685 isl_qpolynomial_fold_free(fold
);
1686 isl_reordering_free(r
);
1690 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_mul_isl_int(
1691 __isl_take isl_qpolynomial_fold
*fold
, isl_int v
)
1695 if (isl_int_is_one(v
))
1697 if (fold
&& isl_int_is_zero(v
)) {
1698 isl_qpolynomial_fold
*zero
;
1699 isl_space
*dim
= isl_space_copy(fold
->dim
);
1700 zero
= isl_qpolynomial_fold_empty(fold
->type
, dim
);
1701 isl_qpolynomial_fold_free(fold
);
1705 fold
= isl_qpolynomial_fold_cow(fold
);
1709 if (isl_int_is_neg(v
))
1710 fold
->type
= isl_fold_type_negate(fold
->type
);
1711 for (i
= 0; i
< fold
->n
; ++i
) {
1712 fold
->qp
[i
] = isl_qpolynomial_mul_isl_int(fold
->qp
[i
], v
);
1719 isl_qpolynomial_fold_free(fold
);
1723 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_scale(
1724 __isl_take isl_qpolynomial_fold
*fold
, isl_int v
)
1726 return isl_qpolynomial_fold_mul_isl_int(fold
, v
);
1729 /* Multiply "fold" by "v".
1731 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_scale_val(
1732 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_val
*v
)
1739 if (isl_val_is_one(v
)) {
1743 if (isl_val_is_zero(v
)) {
1744 isl_qpolynomial_fold
*zero
;
1745 isl_space
*space
= isl_qpolynomial_fold_get_domain_space(fold
);
1746 zero
= isl_qpolynomial_fold_empty(fold
->type
, space
);
1747 isl_qpolynomial_fold_free(fold
);
1751 if (!isl_val_is_rat(v
))
1752 isl_die(isl_qpolynomial_fold_get_ctx(fold
), isl_error_invalid
,
1753 "expecting rational factor", goto error
);
1755 fold
= isl_qpolynomial_fold_cow(fold
);
1759 if (isl_val_is_neg(v
))
1760 fold
->type
= isl_fold_type_negate(fold
->type
);
1761 for (i
= 0; i
< fold
->n
; ++i
) {
1762 fold
->qp
[i
] = isl_qpolynomial_scale_val(fold
->qp
[i
],
1772 isl_qpolynomial_fold_free(fold
);
1776 /* Divide "fold" by "v".
1778 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_scale_down_val(
1779 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_val
*v
)
1784 if (isl_val_is_one(v
)) {
1788 if (!isl_val_is_rat(v
))
1789 isl_die(isl_qpolynomial_fold_get_ctx(fold
), isl_error_invalid
,
1790 "expecting rational factor", goto error
);
1791 if (isl_val_is_zero(v
))
1792 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1793 "cannot scale down by zero", goto error
);
1795 return isl_qpolynomial_fold_scale_val(fold
, isl_val_inv(v
));
1798 isl_qpolynomial_fold_free(fold
);