1 /*
2  * Copyright 2010      INRIA Saclay
3  *
4  * Use of this software is governed by the MIT license
5  *
6  * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7  * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8  * 91893 Orsay, France
9  */
10 
11 #include <stdlib.h>
12 #include <isl_ctx_private.h>
13 #include <isl_map_private.h>
14 #include <isl_factorization.h>
15 #include <isl_lp_private.h>
16 #include <isl_seq.h>
17 #include <isl_union_map_private.h>
18 #include <isl_constraint_private.h>
19 #include <isl_polynomial_private.h>
20 #include <isl_point_private.h>
21 #include <isl_space_private.h>
22 #include <isl_mat_private.h>
23 #include <isl_vec_private.h>
24 #include <isl_range.h>
25 #include <isl_local.h>
26 #include <isl_local_space_private.h>
27 #include <isl_aff_private.h>
28 #include <isl_val_private.h>
29 #include <isl_config.h>
30 
31 #undef EL_BASE
32 #define EL_BASE pw_qpolynomial
33 
34 #include <isl_list_templ.c>
35 
pos(__isl_keep isl_space * space,enum isl_dim_type type)36 static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
37 {
38 	switch (type) {
39 	case isl_dim_param:	return 0;
40 	case isl_dim_in:	return space->nparam;
41 	case isl_dim_out:	return space->nparam + space->n_in;
42 	default:		return 0;
43 	}
44 }
45 
isl_poly_is_cst(__isl_keep isl_poly * poly)46 isl_bool isl_poly_is_cst(__isl_keep isl_poly *poly)
47 {
48 	if (!poly)
49 		return isl_bool_error;
50 
51 	return isl_bool_ok(poly->var < 0);
52 }
53 
isl_poly_as_cst(__isl_keep isl_poly * poly)54 __isl_keep isl_poly_cst *isl_poly_as_cst(__isl_keep isl_poly *poly)
55 {
56 	if (!poly)
57 		return NULL;
58 
59 	isl_assert(poly->ctx, poly->var < 0, return NULL);
60 
61 	return (isl_poly_cst *) poly;
62 }
63 
isl_poly_as_rec(__isl_keep isl_poly * poly)64 __isl_keep isl_poly_rec *isl_poly_as_rec(__isl_keep isl_poly *poly)
65 {
66 	if (!poly)
67 		return NULL;
68 
69 	isl_assert(poly->ctx, poly->var >= 0, return NULL);
70 
71 	return (isl_poly_rec *) poly;
72 }
73 
74 /* Compare two polynomials.
75  *
76  * Return -1 if "poly1" is "smaller" than "poly2", 1 if "poly1" is "greater"
77  * than "poly2" and 0 if they are equal.
78  */
isl_poly_plain_cmp(__isl_keep isl_poly * poly1,__isl_keep isl_poly * poly2)79 static int isl_poly_plain_cmp(__isl_keep isl_poly *poly1,
80 	__isl_keep isl_poly *poly2)
81 {
82 	int i;
83 	isl_bool is_cst1;
84 	isl_poly_rec *rec1, *rec2;
85 
86 	if (poly1 == poly2)
87 		return 0;
88 	is_cst1 = isl_poly_is_cst(poly1);
89 	if (is_cst1 < 0)
90 		return -1;
91 	if (!poly2)
92 		return 1;
93 	if (poly1->var != poly2->var)
94 		return poly1->var - poly2->var;
95 
96 	if (is_cst1) {
97 		isl_poly_cst *cst1, *cst2;
98 		int cmp;
99 
100 		cst1 = isl_poly_as_cst(poly1);
101 		cst2 = isl_poly_as_cst(poly2);
102 		if (!cst1 || !cst2)
103 			return 0;
104 		cmp = isl_int_cmp(cst1->n, cst2->n);
105 		if (cmp != 0)
106 			return cmp;
107 		return isl_int_cmp(cst1->d, cst2->d);
108 	}
109 
110 	rec1 = isl_poly_as_rec(poly1);
111 	rec2 = isl_poly_as_rec(poly2);
112 	if (!rec1 || !rec2)
113 		return 0;
114 
115 	if (rec1->n != rec2->n)
116 		return rec1->n - rec2->n;
117 
118 	for (i = 0; i < rec1->n; ++i) {
119 		int cmp = isl_poly_plain_cmp(rec1->p[i], rec2->p[i]);
120 		if (cmp != 0)
121 			return cmp;
122 	}
123 
124 	return 0;
125 }
126 
isl_poly_is_equal(__isl_keep isl_poly * poly1,__isl_keep isl_poly * poly2)127 isl_bool isl_poly_is_equal(__isl_keep isl_poly *poly1,
128 	__isl_keep isl_poly *poly2)
129 {
130 	int i;
131 	isl_bool is_cst1;
132 	isl_poly_rec *rec1, *rec2;
133 
134 	is_cst1 = isl_poly_is_cst(poly1);
135 	if (is_cst1 < 0 || !poly2)
136 		return isl_bool_error;
137 	if (poly1 == poly2)
138 		return isl_bool_true;
139 	if (poly1->var != poly2->var)
140 		return isl_bool_false;
141 	if (is_cst1) {
142 		isl_poly_cst *cst1, *cst2;
143 		int r;
144 		cst1 = isl_poly_as_cst(poly1);
145 		cst2 = isl_poly_as_cst(poly2);
146 		if (!cst1 || !cst2)
147 			return isl_bool_error;
148 		r = isl_int_eq(cst1->n, cst2->n) &&
149 		    isl_int_eq(cst1->d, cst2->d);
150 		return isl_bool_ok(r);
151 	}
152 
153 	rec1 = isl_poly_as_rec(poly1);
154 	rec2 = isl_poly_as_rec(poly2);
155 	if (!rec1 || !rec2)
156 		return isl_bool_error;
157 
158 	if (rec1->n != rec2->n)
159 		return isl_bool_false;
160 
161 	for (i = 0; i < rec1->n; ++i) {
162 		isl_bool eq = isl_poly_is_equal(rec1->p[i], rec2->p[i]);
163 		if (eq < 0 || !eq)
164 			return eq;
165 	}
166 
167 	return isl_bool_true;
168 }
169 
isl_poly_is_zero(__isl_keep isl_poly * poly)170 isl_bool isl_poly_is_zero(__isl_keep isl_poly *poly)
171 {
172 	isl_bool is_cst;
173 	isl_poly_cst *cst;
174 
175 	is_cst = isl_poly_is_cst(poly);
176 	if (is_cst < 0 || !is_cst)
177 		return is_cst;
178 
179 	cst = isl_poly_as_cst(poly);
180 	if (!cst)
181 		return isl_bool_error;
182 
183 	return isl_bool_ok(isl_int_is_zero(cst->n) && isl_int_is_pos(cst->d));
184 }
185 
isl_poly_sgn(__isl_keep isl_poly * poly)186 int isl_poly_sgn(__isl_keep isl_poly *poly)
187 {
188 	isl_bool is_cst;
189 	isl_poly_cst *cst;
190 
191 	is_cst = isl_poly_is_cst(poly);
192 	if (is_cst < 0 || !is_cst)
193 		return 0;
194 
195 	cst = isl_poly_as_cst(poly);
196 	if (!cst)
197 		return 0;
198 
199 	return isl_int_sgn(cst->n);
200 }
201 
isl_poly_is_nan(__isl_keep isl_poly * poly)202 isl_bool isl_poly_is_nan(__isl_keep isl_poly *poly)
203 {
204 	isl_bool is_cst;
205 	isl_poly_cst *cst;
206 
207 	is_cst = isl_poly_is_cst(poly);
208 	if (is_cst < 0 || !is_cst)
209 		return is_cst;
210 
211 	cst = isl_poly_as_cst(poly);
212 	if (!cst)
213 		return isl_bool_error;
214 
215 	return isl_bool_ok(isl_int_is_zero(cst->n) && isl_int_is_zero(cst->d));
216 }
217 
isl_poly_is_infty(__isl_keep isl_poly * poly)218 isl_bool isl_poly_is_infty(__isl_keep isl_poly *poly)
219 {
220 	isl_bool is_cst;
221 	isl_poly_cst *cst;
222 
223 	is_cst = isl_poly_is_cst(poly);
224 	if (is_cst < 0 || !is_cst)
225 		return is_cst;
226 
227 	cst = isl_poly_as_cst(poly);
228 	if (!cst)
229 		return isl_bool_error;
230 
231 	return isl_bool_ok(isl_int_is_pos(cst->n) && isl_int_is_zero(cst->d));
232 }
233 
isl_poly_is_neginfty(__isl_keep isl_poly * poly)234 isl_bool isl_poly_is_neginfty(__isl_keep isl_poly *poly)
235 {
236 	isl_bool is_cst;
237 	isl_poly_cst *cst;
238 
239 	is_cst = isl_poly_is_cst(poly);
240 	if (is_cst < 0 || !is_cst)
241 		return is_cst;
242 
243 	cst = isl_poly_as_cst(poly);
244 	if (!cst)
245 		return isl_bool_error;
246 
247 	return isl_bool_ok(isl_int_is_neg(cst->n) && isl_int_is_zero(cst->d));
248 }
249 
isl_poly_is_one(__isl_keep isl_poly * poly)250 isl_bool isl_poly_is_one(__isl_keep isl_poly *poly)
251 {
252 	isl_bool is_cst;
253 	isl_poly_cst *cst;
254 	int r;
255 
256 	is_cst = isl_poly_is_cst(poly);
257 	if (is_cst < 0 || !is_cst)
258 		return is_cst;
259 
260 	cst = isl_poly_as_cst(poly);
261 	if (!cst)
262 		return isl_bool_error;
263 
264 	r = isl_int_eq(cst->n, cst->d) && isl_int_is_pos(cst->d);
265 	return isl_bool_ok(r);
266 }
267 
isl_poly_is_negone(__isl_keep isl_poly * poly)268 isl_bool isl_poly_is_negone(__isl_keep isl_poly *poly)
269 {
270 	isl_bool is_cst;
271 	isl_poly_cst *cst;
272 
273 	is_cst = isl_poly_is_cst(poly);
274 	if (is_cst < 0 || !is_cst)
275 		return is_cst;
276 
277 	cst = isl_poly_as_cst(poly);
278 	if (!cst)
279 		return isl_bool_error;
280 
281 	return isl_bool_ok(isl_int_is_negone(cst->n) && isl_int_is_one(cst->d));
282 }
283 
isl_poly_cst_alloc(isl_ctx * ctx)284 __isl_give isl_poly_cst *isl_poly_cst_alloc(isl_ctx *ctx)
285 {
286 	isl_poly_cst *cst;
287 
288 	cst = isl_alloc_type(ctx, struct isl_poly_cst);
289 	if (!cst)
290 		return NULL;
291 
292 	cst->poly.ref = 1;
293 	cst->poly.ctx = ctx;
294 	isl_ctx_ref(ctx);
295 	cst->poly.var = -1;
296 
297 	isl_int_init(cst->n);
298 	isl_int_init(cst->d);
299 
300 	return cst;
301 }
302 
isl_poly_zero(isl_ctx * ctx)303 __isl_give isl_poly *isl_poly_zero(isl_ctx *ctx)
304 {
305 	isl_poly_cst *cst;
306 
307 	cst = isl_poly_cst_alloc(ctx);
308 	if (!cst)
309 		return NULL;
310 
311 	isl_int_set_si(cst->n, 0);
312 	isl_int_set_si(cst->d, 1);
313 
314 	return &cst->poly;
315 }
316 
isl_poly_one(isl_ctx * ctx)317 __isl_give isl_poly *isl_poly_one(isl_ctx *ctx)
318 {
319 	isl_poly_cst *cst;
320 
321 	cst = isl_poly_cst_alloc(ctx);
322 	if (!cst)
323 		return NULL;
324 
325 	isl_int_set_si(cst->n, 1);
326 	isl_int_set_si(cst->d, 1);
327 
328 	return &cst->poly;
329 }
330 
isl_poly_infty(isl_ctx * ctx)331 __isl_give isl_poly *isl_poly_infty(isl_ctx *ctx)
332 {
333 	isl_poly_cst *cst;
334 
335 	cst = isl_poly_cst_alloc(ctx);
336 	if (!cst)
337 		return NULL;
338 
339 	isl_int_set_si(cst->n, 1);
340 	isl_int_set_si(cst->d, 0);
341 
342 	return &cst->poly;
343 }
344 
isl_poly_neginfty(isl_ctx * ctx)345 __isl_give isl_poly *isl_poly_neginfty(isl_ctx *ctx)
346 {
347 	isl_poly_cst *cst;
348 
349 	cst = isl_poly_cst_alloc(ctx);
350 	if (!cst)
351 		return NULL;
352 
353 	isl_int_set_si(cst->n, -1);
354 	isl_int_set_si(cst->d, 0);
355 
356 	return &cst->poly;
357 }
358 
isl_poly_nan(isl_ctx * ctx)359 __isl_give isl_poly *isl_poly_nan(isl_ctx *ctx)
360 {
361 	isl_poly_cst *cst;
362 
363 	cst = isl_poly_cst_alloc(ctx);
364 	if (!cst)
365 		return NULL;
366 
367 	isl_int_set_si(cst->n, 0);
368 	isl_int_set_si(cst->d, 0);
369 
370 	return &cst->poly;
371 }
372 
isl_poly_rat_cst(isl_ctx * ctx,isl_int n,isl_int d)373 __isl_give isl_poly *isl_poly_rat_cst(isl_ctx *ctx, isl_int n, isl_int d)
374 {
375 	isl_poly_cst *cst;
376 
377 	cst = isl_poly_cst_alloc(ctx);
378 	if (!cst)
379 		return NULL;
380 
381 	isl_int_set(cst->n, n);
382 	isl_int_set(cst->d, d);
383 
384 	return &cst->poly;
385 }
386 
isl_poly_alloc_rec(isl_ctx * ctx,int var,int size)387 __isl_give isl_poly_rec *isl_poly_alloc_rec(isl_ctx *ctx, int var, int size)
388 {
389 	isl_poly_rec *rec;
390 
391 	isl_assert(ctx, var >= 0, return NULL);
392 	isl_assert(ctx, size >= 0, return NULL);
393 	rec = isl_calloc(ctx, struct isl_poly_rec,
394 			sizeof(struct isl_poly_rec) +
395 			size * sizeof(struct isl_poly *));
396 	if (!rec)
397 		return NULL;
398 
399 	rec->poly.ref = 1;
400 	rec->poly.ctx = ctx;
401 	isl_ctx_ref(ctx);
402 	rec->poly.var = var;
403 
404 	rec->n = 0;
405 	rec->size = size;
406 
407 	return rec;
408 }
409 
isl_qpolynomial_reset_domain_space(__isl_take isl_qpolynomial * qp,__isl_take isl_space * space)410 __isl_give isl_qpolynomial *isl_qpolynomial_reset_domain_space(
411 	__isl_take isl_qpolynomial *qp, __isl_take isl_space *space)
412 {
413 	qp = isl_qpolynomial_cow(qp);
414 	if (!qp || !space)
415 		goto error;
416 
417 	isl_space_free(qp->dim);
418 	qp->dim = space;
419 
420 	return qp;
421 error:
422 	isl_qpolynomial_free(qp);
423 	isl_space_free(space);
424 	return NULL;
425 }
426 
427 /* Reset the space of "qp".  This function is called from isl_pw_templ.c
428  * and doesn't know if the space of an element object is represented
429  * directly or through its domain.  It therefore passes along both.
430  */
isl_qpolynomial_reset_space_and_domain(__isl_take isl_qpolynomial * qp,__isl_take isl_space * space,__isl_take isl_space * domain)431 __isl_give isl_qpolynomial *isl_qpolynomial_reset_space_and_domain(
432 	__isl_take isl_qpolynomial *qp, __isl_take isl_space *space,
433 	__isl_take isl_space *domain)
434 {
435 	isl_space_free(space);
436 	return isl_qpolynomial_reset_domain_space(qp, domain);
437 }
438 
isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial * qp)439 isl_ctx *isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial *qp)
440 {
441 	return qp ? qp->dim->ctx : NULL;
442 }
443 
444 /* Return the domain space of "qp".
445  */
isl_qpolynomial_peek_domain_space(__isl_keep isl_qpolynomial * qp)446 static __isl_keep isl_space *isl_qpolynomial_peek_domain_space(
447 	__isl_keep isl_qpolynomial *qp)
448 {
449 	return qp ? qp->dim : NULL;
450 }
451 
452 /* Return a copy of the domain space of "qp".
453  */
isl_qpolynomial_get_domain_space(__isl_keep isl_qpolynomial * qp)454 __isl_give isl_space *isl_qpolynomial_get_domain_space(
455 	__isl_keep isl_qpolynomial *qp)
456 {
457 	return isl_space_copy(isl_qpolynomial_peek_domain_space(qp));
458 }
459 
460 #undef TYPE
461 #define TYPE		isl_qpolynomial
462 #undef PEEK_SPACE
463 #define PEEK_SPACE	peek_domain_space
464 
465 static
466 #include "isl_type_has_equal_space_bin_templ.c"
467 static
468 #include "isl_type_check_equal_space_templ.c"
469 
470 #undef PEEK_SPACE
471 
472 /* Return a copy of the local space on which "qp" is defined.
473  */
isl_qpolynomial_get_domain_local_space(__isl_keep isl_qpolynomial * qp)474 static __isl_give isl_local_space *isl_qpolynomial_get_domain_local_space(
475 	__isl_keep isl_qpolynomial *qp)
476 {
477 	isl_space *space;
478 
479 	if (!qp)
480 		return NULL;
481 
482 	space = isl_qpolynomial_get_domain_space(qp);
483 	return isl_local_space_alloc_div(space, isl_mat_copy(qp->div));
484 }
485 
isl_qpolynomial_get_space(__isl_keep isl_qpolynomial * qp)486 __isl_give isl_space *isl_qpolynomial_get_space(__isl_keep isl_qpolynomial *qp)
487 {
488 	isl_space *space;
489 	if (!qp)
490 		return NULL;
491 	space = isl_space_copy(qp->dim);
492 	space = isl_space_from_domain(space);
493 	space = isl_space_add_dims(space, isl_dim_out, 1);
494 	return space;
495 }
496 
497 /* Return the number of variables of the given type in the domain of "qp".
498  */
isl_qpolynomial_domain_dim(__isl_keep isl_qpolynomial * qp,enum isl_dim_type type)499 isl_size isl_qpolynomial_domain_dim(__isl_keep isl_qpolynomial *qp,
500 	enum isl_dim_type type)
501 {
502 	isl_space *space;
503 	isl_size dim;
504 
505 	space = isl_qpolynomial_peek_domain_space(qp);
506 
507 	if (!space)
508 		return isl_size_error;
509 	if (type == isl_dim_div)
510 		return qp->div->n_row;
511 	dim = isl_space_dim(space, type);
512 	if (dim < 0)
513 		return isl_size_error;
514 	if (type == isl_dim_all) {
515 		isl_size n_div;
516 
517 		n_div = isl_qpolynomial_domain_dim(qp, isl_dim_div);
518 		if (n_div < 0)
519 			return isl_size_error;
520 		dim += n_div;
521 	}
522 	return dim;
523 }
524 
525 /* Given the type of a dimension of an isl_qpolynomial,
526  * return the type of the corresponding dimension in its domain.
527  * This function is only called for "type" equal to isl_dim_in or
528  * isl_dim_param.
529  */
domain_type(enum isl_dim_type type)530 static enum isl_dim_type domain_type(enum isl_dim_type type)
531 {
532 	return type == isl_dim_in ? isl_dim_set : type;
533 }
534 
535 /* Externally, an isl_qpolynomial has a map space, but internally, the
536  * ls field corresponds to the domain of that space.
537  */
isl_qpolynomial_dim(__isl_keep isl_qpolynomial * qp,enum isl_dim_type type)538 isl_size isl_qpolynomial_dim(__isl_keep isl_qpolynomial *qp,
539 	enum isl_dim_type type)
540 {
541 	if (!qp)
542 		return isl_size_error;
543 	if (type == isl_dim_out)
544 		return 1;
545 	type = domain_type(type);
546 	return isl_qpolynomial_domain_dim(qp, type);
547 }
548 
549 /* Return the offset of the first variable of type "type" within
550  * the variables of the domain of "qp".
551  */
isl_qpolynomial_domain_var_offset(__isl_keep isl_qpolynomial * qp,enum isl_dim_type type)552 static isl_size isl_qpolynomial_domain_var_offset(
553 	__isl_keep isl_qpolynomial *qp, enum isl_dim_type type)
554 {
555 	isl_space *space;
556 
557 	space = isl_qpolynomial_peek_domain_space(qp);
558 	if (!space)
559 		return isl_size_error;
560 
561 	switch (type) {
562 	case isl_dim_param:
563 	case isl_dim_set:	return isl_space_offset(space, type);
564 	case isl_dim_div:	return isl_space_dim(space, isl_dim_all);
565 	case isl_dim_cst:
566 	default:
567 		isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
568 			"invalid dimension type", return isl_size_error);
569 	}
570 }
571 
572 /* Return the offset of the first coefficient of type "type" in
573  * the domain of "qp".
574  */
isl_qpolynomial_domain_offset(__isl_keep isl_qpolynomial * qp,enum isl_dim_type type)575 unsigned isl_qpolynomial_domain_offset(__isl_keep isl_qpolynomial *qp,
576 	enum isl_dim_type type)
577 {
578 	switch (type) {
579 	case isl_dim_cst:
580 		return 0;
581 	case isl_dim_param:
582 	case isl_dim_set:
583 	case isl_dim_div:
584 		return 1 + isl_qpolynomial_domain_var_offset(qp, type);
585 	default:
586 		return 0;
587 	}
588 }
589 
isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial * qp)590 isl_bool isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
591 {
592 	return qp ? isl_poly_is_zero(qp->poly) : isl_bool_error;
593 }
594 
isl_qpolynomial_is_one(__isl_keep isl_qpolynomial * qp)595 isl_bool isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
596 {
597 	return qp ? isl_poly_is_one(qp->poly) : isl_bool_error;
598 }
599 
isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial * qp)600 isl_bool isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial *qp)
601 {
602 	return qp ? isl_poly_is_nan(qp->poly) : isl_bool_error;
603 }
604 
isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial * qp)605 isl_bool isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial *qp)
606 {
607 	return qp ? isl_poly_is_infty(qp->poly) : isl_bool_error;
608 }
609 
isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial * qp)610 isl_bool isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial *qp)
611 {
612 	return qp ? isl_poly_is_neginfty(qp->poly) : isl_bool_error;
613 }
614 
isl_qpolynomial_sgn(__isl_keep isl_qpolynomial * qp)615 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial *qp)
616 {
617 	return qp ? isl_poly_sgn(qp->poly) : 0;
618 }
619 
poly_free_cst(__isl_take isl_poly_cst * cst)620 static void poly_free_cst(__isl_take isl_poly_cst *cst)
621 {
622 	isl_int_clear(cst->n);
623 	isl_int_clear(cst->d);
624 }
625 
poly_free_rec(__isl_take isl_poly_rec * rec)626 static void poly_free_rec(__isl_take isl_poly_rec *rec)
627 {
628 	int i;
629 
630 	for (i = 0; i < rec->n; ++i)
631 		isl_poly_free(rec->p[i]);
632 }
633 
isl_poly_copy(__isl_keep isl_poly * poly)634 __isl_give isl_poly *isl_poly_copy(__isl_keep isl_poly *poly)
635 {
636 	if (!poly)
637 		return NULL;
638 
639 	poly->ref++;
640 	return poly;
641 }
642 
isl_poly_dup_cst(__isl_keep isl_poly * poly)643 __isl_give isl_poly *isl_poly_dup_cst(__isl_keep isl_poly *poly)
644 {
645 	isl_poly_cst *cst;
646 	isl_poly_cst *dup;
647 
648 	cst = isl_poly_as_cst(poly);
649 	if (!cst)
650 		return NULL;
651 
652 	dup = isl_poly_as_cst(isl_poly_zero(poly->ctx));
653 	if (!dup)
654 		return NULL;
655 	isl_int_set(dup->n, cst->n);
656 	isl_int_set(dup->d, cst->d);
657 
658 	return &dup->poly;
659 }
660 
isl_poly_dup_rec(__isl_keep isl_poly * poly)661 __isl_give isl_poly *isl_poly_dup_rec(__isl_keep isl_poly *poly)
662 {
663 	int i;
664 	isl_poly_rec *rec;
665 	isl_poly_rec *dup;
666 
667 	rec = isl_poly_as_rec(poly);
668 	if (!rec)
669 		return NULL;
670 
671 	dup = isl_poly_alloc_rec(poly->ctx, poly->var, rec->n);
672 	if (!dup)
673 		return NULL;
674 
675 	for (i = 0; i < rec->n; ++i) {
676 		dup->p[i] = isl_poly_copy(rec->p[i]);
677 		if (!dup->p[i])
678 			goto error;
679 		dup->n++;
680 	}
681 
682 	return &dup->poly;
683 error:
684 	isl_poly_free(&dup->poly);
685 	return NULL;
686 }
687 
isl_poly_dup(__isl_keep isl_poly * poly)688 __isl_give isl_poly *isl_poly_dup(__isl_keep isl_poly *poly)
689 {
690 	isl_bool is_cst;
691 
692 	is_cst = isl_poly_is_cst(poly);
693 	if (is_cst < 0)
694 		return NULL;
695 	if (is_cst)
696 		return isl_poly_dup_cst(poly);
697 	else
698 		return isl_poly_dup_rec(poly);
699 }
700 
isl_poly_cow(__isl_take isl_poly * poly)701 __isl_give isl_poly *isl_poly_cow(__isl_take isl_poly *poly)
702 {
703 	if (!poly)
704 		return NULL;
705 
706 	if (poly->ref == 1)
707 		return poly;
708 	poly->ref--;
709 	return isl_poly_dup(poly);
710 }
711 
isl_poly_free(__isl_take isl_poly * poly)712 __isl_null isl_poly *isl_poly_free(__isl_take isl_poly *poly)
713 {
714 	if (!poly)
715 		return NULL;
716 
717 	if (--poly->ref > 0)
718 		return NULL;
719 
720 	if (poly->var < 0)
721 		poly_free_cst((isl_poly_cst *) poly);
722 	else
723 		poly_free_rec((isl_poly_rec *) poly);
724 
725 	isl_ctx_deref(poly->ctx);
726 	free(poly);
727 	return NULL;
728 }
729 
isl_poly_cst_reduce(__isl_keep isl_poly_cst * cst)730 static void isl_poly_cst_reduce(__isl_keep isl_poly_cst *cst)
731 {
732 	isl_int gcd;
733 
734 	isl_int_init(gcd);
735 	isl_int_gcd(gcd, cst->n, cst->d);
736 	if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd)) {
737 		isl_int_divexact(cst->n, cst->n, gcd);
738 		isl_int_divexact(cst->d, cst->d, gcd);
739 	}
740 	isl_int_clear(gcd);
741 }
742 
isl_poly_sum_cst(__isl_take isl_poly * poly1,__isl_take isl_poly * poly2)743 __isl_give isl_poly *isl_poly_sum_cst(__isl_take isl_poly *poly1,
744 	__isl_take isl_poly *poly2)
745 {
746 	isl_poly_cst *cst1;
747 	isl_poly_cst *cst2;
748 
749 	poly1 = isl_poly_cow(poly1);
750 	if (!poly1 || !poly2)
751 		goto error;
752 
753 	cst1 = isl_poly_as_cst(poly1);
754 	cst2 = isl_poly_as_cst(poly2);
755 
756 	if (isl_int_eq(cst1->d, cst2->d))
757 		isl_int_add(cst1->n, cst1->n, cst2->n);
758 	else {
759 		isl_int_mul(cst1->n, cst1->n, cst2->d);
760 		isl_int_addmul(cst1->n, cst2->n, cst1->d);
761 		isl_int_mul(cst1->d, cst1->d, cst2->d);
762 	}
763 
764 	isl_poly_cst_reduce(cst1);
765 
766 	isl_poly_free(poly2);
767 	return poly1;
768 error:
769 	isl_poly_free(poly1);
770 	isl_poly_free(poly2);
771 	return NULL;
772 }
773 
replace_by_zero(__isl_take isl_poly * poly)774 static __isl_give isl_poly *replace_by_zero(__isl_take isl_poly *poly)
775 {
776 	struct isl_ctx *ctx;
777 
778 	if (!poly)
779 		return NULL;
780 	ctx = poly->ctx;
781 	isl_poly_free(poly);
782 	return isl_poly_zero(ctx);
783 }
784 
replace_by_constant_term(__isl_take isl_poly * poly)785 static __isl_give isl_poly *replace_by_constant_term(__isl_take isl_poly *poly)
786 {
787 	isl_poly_rec *rec;
788 	isl_poly *cst;
789 
790 	if (!poly)
791 		return NULL;
792 
793 	rec = isl_poly_as_rec(poly);
794 	if (!rec)
795 		goto error;
796 	cst = isl_poly_copy(rec->p[0]);
797 	isl_poly_free(poly);
798 	return cst;
799 error:
800 	isl_poly_free(poly);
801 	return NULL;
802 }
803 
isl_poly_sum(__isl_take isl_poly * poly1,__isl_take isl_poly * poly2)804 __isl_give isl_poly *isl_poly_sum(__isl_take isl_poly *poly1,
805 	__isl_take isl_poly *poly2)
806 {
807 	int i;
808 	isl_bool is_zero, is_nan, is_cst;
809 	isl_poly_rec *rec1, *rec2;
810 
811 	if (!poly1 || !poly2)
812 		goto error;
813 
814 	is_nan = isl_poly_is_nan(poly1);
815 	if (is_nan < 0)
816 		goto error;
817 	if (is_nan) {
818 		isl_poly_free(poly2);
819 		return poly1;
820 	}
821 
822 	is_nan = isl_poly_is_nan(poly2);
823 	if (is_nan < 0)
824 		goto error;
825 	if (is_nan) {
826 		isl_poly_free(poly1);
827 		return poly2;
828 	}
829 
830 	is_zero = isl_poly_is_zero(poly1);
831 	if (is_zero < 0)
832 		goto error;
833 	if (is_zero) {
834 		isl_poly_free(poly1);
835 		return poly2;
836 	}
837 
838 	is_zero = isl_poly_is_zero(poly2);
839 	if (is_zero < 0)
840 		goto error;
841 	if (is_zero) {
842 		isl_poly_free(poly2);
843 		return poly1;
844 	}
845 
846 	if (poly1->var < poly2->var)
847 		return isl_poly_sum(poly2, poly1);
848 
849 	if (poly2->var < poly1->var) {
850 		isl_poly_rec *rec;
851 		isl_bool is_infty;
852 
853 		is_infty = isl_poly_is_infty(poly2);
854 		if (is_infty >= 0 && !is_infty)
855 			is_infty = isl_poly_is_neginfty(poly2);
856 		if (is_infty < 0)
857 			goto error;
858 		if (is_infty) {
859 			isl_poly_free(poly1);
860 			return poly2;
861 		}
862 		poly1 = isl_poly_cow(poly1);
863 		rec = isl_poly_as_rec(poly1);
864 		if (!rec)
865 			goto error;
866 		rec->p[0] = isl_poly_sum(rec->p[0], poly2);
867 		if (rec->n == 1)
868 			poly1 = replace_by_constant_term(poly1);
869 		return poly1;
870 	}
871 
872 	is_cst = isl_poly_is_cst(poly1);
873 	if (is_cst < 0)
874 		goto error;
875 	if (is_cst)
876 		return isl_poly_sum_cst(poly1, poly2);
877 
878 	rec1 = isl_poly_as_rec(poly1);
879 	rec2 = isl_poly_as_rec(poly2);
880 	if (!rec1 || !rec2)
881 		goto error;
882 
883 	if (rec1->n < rec2->n)
884 		return isl_poly_sum(poly2, poly1);
885 
886 	poly1 = isl_poly_cow(poly1);
887 	rec1 = isl_poly_as_rec(poly1);
888 	if (!rec1)
889 		goto error;
890 
891 	for (i = rec2->n - 1; i >= 0; --i) {
892 		isl_bool is_zero;
893 
894 		rec1->p[i] = isl_poly_sum(rec1->p[i],
895 					    isl_poly_copy(rec2->p[i]));
896 		if (!rec1->p[i])
897 			goto error;
898 		if (i != rec1->n - 1)
899 			continue;
900 		is_zero = isl_poly_is_zero(rec1->p[i]);
901 		if (is_zero < 0)
902 			goto error;
903 		if (is_zero) {
904 			isl_poly_free(rec1->p[i]);
905 			rec1->n--;
906 		}
907 	}
908 
909 	if (rec1->n == 0)
910 		poly1 = replace_by_zero(poly1);
911 	else if (rec1->n == 1)
912 		poly1 = replace_by_constant_term(poly1);
913 
914 	isl_poly_free(poly2);
915 
916 	return poly1;
917 error:
918 	isl_poly_free(poly1);
919 	isl_poly_free(poly2);
920 	return NULL;
921 }
922 
isl_poly_cst_add_isl_int(__isl_take isl_poly * poly,isl_int v)923 __isl_give isl_poly *isl_poly_cst_add_isl_int(__isl_take isl_poly *poly,
924 	isl_int v)
925 {
926 	isl_poly_cst *cst;
927 
928 	poly = isl_poly_cow(poly);
929 	if (!poly)
930 		return NULL;
931 
932 	cst = isl_poly_as_cst(poly);
933 
934 	isl_int_addmul(cst->n, cst->d, v);
935 
936 	return poly;
937 }
938 
isl_poly_add_isl_int(__isl_take isl_poly * poly,isl_int v)939 __isl_give isl_poly *isl_poly_add_isl_int(__isl_take isl_poly *poly, isl_int v)
940 {
941 	isl_bool is_cst;
942 	isl_poly_rec *rec;
943 
944 	is_cst = isl_poly_is_cst(poly);
945 	if (is_cst < 0)
946 		return isl_poly_free(poly);
947 	if (is_cst)
948 		return isl_poly_cst_add_isl_int(poly, v);
949 
950 	poly = isl_poly_cow(poly);
951 	rec = isl_poly_as_rec(poly);
952 	if (!rec)
953 		goto error;
954 
955 	rec->p[0] = isl_poly_add_isl_int(rec->p[0], v);
956 	if (!rec->p[0])
957 		goto error;
958 
959 	return poly;
960 error:
961 	isl_poly_free(poly);
962 	return NULL;
963 }
964 
isl_poly_cst_mul_isl_int(__isl_take isl_poly * poly,isl_int v)965 __isl_give isl_poly *isl_poly_cst_mul_isl_int(__isl_take isl_poly *poly,
966 	isl_int v)
967 {
968 	isl_bool is_zero;
969 	isl_poly_cst *cst;
970 
971 	is_zero = isl_poly_is_zero(poly);
972 	if (is_zero < 0)
973 		return isl_poly_free(poly);
974 	if (is_zero)
975 		return poly;
976 
977 	poly = isl_poly_cow(poly);
978 	if (!poly)
979 		return NULL;
980 
981 	cst = isl_poly_as_cst(poly);
982 
983 	isl_int_mul(cst->n, cst->n, v);
984 
985 	return poly;
986 }
987 
isl_poly_mul_isl_int(__isl_take isl_poly * poly,isl_int v)988 __isl_give isl_poly *isl_poly_mul_isl_int(__isl_take isl_poly *poly, isl_int v)
989 {
990 	int i;
991 	isl_bool is_cst;
992 	isl_poly_rec *rec;
993 
994 	is_cst = isl_poly_is_cst(poly);
995 	if (is_cst < 0)
996 		return isl_poly_free(poly);
997 	if (is_cst)
998 		return isl_poly_cst_mul_isl_int(poly, v);
999 
1000 	poly = isl_poly_cow(poly);
1001 	rec = isl_poly_as_rec(poly);
1002 	if (!rec)
1003 		goto error;
1004 
1005 	for (i = 0; i < rec->n; ++i) {
1006 		rec->p[i] = isl_poly_mul_isl_int(rec->p[i], v);
1007 		if (!rec->p[i])
1008 			goto error;
1009 	}
1010 
1011 	return poly;
1012 error:
1013 	isl_poly_free(poly);
1014 	return NULL;
1015 }
1016 
1017 /* Multiply the constant polynomial "poly" by "v".
1018  */
isl_poly_cst_scale_val(__isl_take isl_poly * poly,__isl_keep isl_val * v)1019 static __isl_give isl_poly *isl_poly_cst_scale_val(__isl_take isl_poly *poly,
1020 	__isl_keep isl_val *v)
1021 {
1022 	isl_bool is_zero;
1023 	isl_poly_cst *cst;
1024 
1025 	is_zero = isl_poly_is_zero(poly);
1026 	if (is_zero < 0)
1027 		return isl_poly_free(poly);
1028 	if (is_zero)
1029 		return poly;
1030 
1031 	poly = isl_poly_cow(poly);
1032 	if (!poly)
1033 		return NULL;
1034 
1035 	cst = isl_poly_as_cst(poly);
1036 
1037 	isl_int_mul(cst->n, cst->n, v->n);
1038 	isl_int_mul(cst->d, cst->d, v->d);
1039 	isl_poly_cst_reduce(cst);
1040 
1041 	return poly;
1042 }
1043 
1044 /* Multiply the polynomial "poly" by "v".
1045  */
isl_poly_scale_val(__isl_take isl_poly * poly,__isl_keep isl_val * v)1046 static __isl_give isl_poly *isl_poly_scale_val(__isl_take isl_poly *poly,
1047 	__isl_keep isl_val *v)
1048 {
1049 	int i;
1050 	isl_bool is_cst;
1051 	isl_poly_rec *rec;
1052 
1053 	is_cst = isl_poly_is_cst(poly);
1054 	if (is_cst < 0)
1055 		return isl_poly_free(poly);
1056 	if (is_cst)
1057 		return isl_poly_cst_scale_val(poly, v);
1058 
1059 	poly = isl_poly_cow(poly);
1060 	rec = isl_poly_as_rec(poly);
1061 	if (!rec)
1062 		goto error;
1063 
1064 	for (i = 0; i < rec->n; ++i) {
1065 		rec->p[i] = isl_poly_scale_val(rec->p[i], v);
1066 		if (!rec->p[i])
1067 			goto error;
1068 	}
1069 
1070 	return poly;
1071 error:
1072 	isl_poly_free(poly);
1073 	return NULL;
1074 }
1075 
isl_poly_mul_cst(__isl_take isl_poly * poly1,__isl_take isl_poly * poly2)1076 __isl_give isl_poly *isl_poly_mul_cst(__isl_take isl_poly *poly1,
1077 	__isl_take isl_poly *poly2)
1078 {
1079 	isl_poly_cst *cst1;
1080 	isl_poly_cst *cst2;
1081 
1082 	poly1 = isl_poly_cow(poly1);
1083 	if (!poly1 || !poly2)
1084 		goto error;
1085 
1086 	cst1 = isl_poly_as_cst(poly1);
1087 	cst2 = isl_poly_as_cst(poly2);
1088 
1089 	isl_int_mul(cst1->n, cst1->n, cst2->n);
1090 	isl_int_mul(cst1->d, cst1->d, cst2->d);
1091 
1092 	isl_poly_cst_reduce(cst1);
1093 
1094 	isl_poly_free(poly2);
1095 	return poly1;
1096 error:
1097 	isl_poly_free(poly1);
1098 	isl_poly_free(poly2);
1099 	return NULL;
1100 }
1101 
isl_poly_mul_rec(__isl_take isl_poly * poly1,__isl_take isl_poly * poly2)1102 __isl_give isl_poly *isl_poly_mul_rec(__isl_take isl_poly *poly1,
1103 	__isl_take isl_poly *poly2)
1104 {
1105 	isl_poly_rec *rec1;
1106 	isl_poly_rec *rec2;
1107 	isl_poly_rec *res = NULL;
1108 	int i, j;
1109 	int size;
1110 
1111 	rec1 = isl_poly_as_rec(poly1);
1112 	rec2 = isl_poly_as_rec(poly2);
1113 	if (!rec1 || !rec2)
1114 		goto error;
1115 	size = rec1->n + rec2->n - 1;
1116 	res = isl_poly_alloc_rec(poly1->ctx, poly1->var, size);
1117 	if (!res)
1118 		goto error;
1119 
1120 	for (i = 0; i < rec1->n; ++i) {
1121 		res->p[i] = isl_poly_mul(isl_poly_copy(rec2->p[0]),
1122 					    isl_poly_copy(rec1->p[i]));
1123 		if (!res->p[i])
1124 			goto error;
1125 		res->n++;
1126 	}
1127 	for (; i < size; ++i) {
1128 		res->p[i] = isl_poly_zero(poly1->ctx);
1129 		if (!res->p[i])
1130 			goto error;
1131 		res->n++;
1132 	}
1133 	for (i = 0; i < rec1->n; ++i) {
1134 		for (j = 1; j < rec2->n; ++j) {
1135 			isl_poly *poly;
1136 			poly = isl_poly_mul(isl_poly_copy(rec2->p[j]),
1137 					    isl_poly_copy(rec1->p[i]));
1138 			res->p[i + j] = isl_poly_sum(res->p[i + j], poly);
1139 			if (!res->p[i + j])
1140 				goto error;
1141 		}
1142 	}
1143 
1144 	isl_poly_free(poly1);
1145 	isl_poly_free(poly2);
1146 
1147 	return &res->poly;
1148 error:
1149 	isl_poly_free(poly1);
1150 	isl_poly_free(poly2);
1151 	isl_poly_free(&res->poly);
1152 	return NULL;
1153 }
1154 
isl_poly_mul(__isl_take isl_poly * poly1,__isl_take isl_poly * poly2)1155 __isl_give isl_poly *isl_poly_mul(__isl_take isl_poly *poly1,
1156 	__isl_take isl_poly *poly2)
1157 {
1158 	isl_bool is_zero, is_nan, is_one, is_cst;
1159 
1160 	if (!poly1 || !poly2)
1161 		goto error;
1162 
1163 	is_nan = isl_poly_is_nan(poly1);
1164 	if (is_nan < 0)
1165 		goto error;
1166 	if (is_nan) {
1167 		isl_poly_free(poly2);
1168 		return poly1;
1169 	}
1170 
1171 	is_nan = isl_poly_is_nan(poly2);
1172 	if (is_nan < 0)
1173 		goto error;
1174 	if (is_nan) {
1175 		isl_poly_free(poly1);
1176 		return poly2;
1177 	}
1178 
1179 	is_zero = isl_poly_is_zero(poly1);
1180 	if (is_zero < 0)
1181 		goto error;
1182 	if (is_zero) {
1183 		isl_poly_free(poly2);
1184 		return poly1;
1185 	}
1186 
1187 	is_zero = isl_poly_is_zero(poly2);
1188 	if (is_zero < 0)
1189 		goto error;
1190 	if (is_zero) {
1191 		isl_poly_free(poly1);
1192 		return poly2;
1193 	}
1194 
1195 	is_one = isl_poly_is_one(poly1);
1196 	if (is_one < 0)
1197 		goto error;
1198 	if (is_one) {
1199 		isl_poly_free(poly1);
1200 		return poly2;
1201 	}
1202 
1203 	is_one = isl_poly_is_one(poly2);
1204 	if (is_one < 0)
1205 		goto error;
1206 	if (is_one) {
1207 		isl_poly_free(poly2);
1208 		return poly1;
1209 	}
1210 
1211 	if (poly1->var < poly2->var)
1212 		return isl_poly_mul(poly2, poly1);
1213 
1214 	if (poly2->var < poly1->var) {
1215 		int i;
1216 		isl_poly_rec *rec;
1217 		isl_bool is_infty;
1218 
1219 		is_infty = isl_poly_is_infty(poly2);
1220 		if (is_infty >= 0 && !is_infty)
1221 			is_infty = isl_poly_is_neginfty(poly2);
1222 		if (is_infty < 0)
1223 			goto error;
1224 		if (is_infty) {
1225 			isl_ctx *ctx = poly1->ctx;
1226 			isl_poly_free(poly1);
1227 			isl_poly_free(poly2);
1228 			return isl_poly_nan(ctx);
1229 		}
1230 		poly1 = isl_poly_cow(poly1);
1231 		rec = isl_poly_as_rec(poly1);
1232 		if (!rec)
1233 			goto error;
1234 
1235 		for (i = 0; i < rec->n; ++i) {
1236 			rec->p[i] = isl_poly_mul(rec->p[i],
1237 						isl_poly_copy(poly2));
1238 			if (!rec->p[i])
1239 				goto error;
1240 		}
1241 		isl_poly_free(poly2);
1242 		return poly1;
1243 	}
1244 
1245 	is_cst = isl_poly_is_cst(poly1);
1246 	if (is_cst < 0)
1247 		goto error;
1248 	if (is_cst)
1249 		return isl_poly_mul_cst(poly1, poly2);
1250 
1251 	return isl_poly_mul_rec(poly1, poly2);
1252 error:
1253 	isl_poly_free(poly1);
1254 	isl_poly_free(poly2);
1255 	return NULL;
1256 }
1257 
isl_poly_pow(__isl_take isl_poly * poly,unsigned power)1258 __isl_give isl_poly *isl_poly_pow(__isl_take isl_poly *poly, unsigned power)
1259 {
1260 	isl_poly *res;
1261 
1262 	if (!poly)
1263 		return NULL;
1264 	if (power == 1)
1265 		return poly;
1266 
1267 	if (power % 2)
1268 		res = isl_poly_copy(poly);
1269 	else
1270 		res = isl_poly_one(poly->ctx);
1271 
1272 	while (power >>= 1) {
1273 		poly = isl_poly_mul(poly, isl_poly_copy(poly));
1274 		if (power % 2)
1275 			res = isl_poly_mul(res, isl_poly_copy(poly));
1276 	}
1277 
1278 	isl_poly_free(poly);
1279 	return res;
1280 }
1281 
isl_qpolynomial_alloc(__isl_take isl_space * space,unsigned n_div,__isl_take isl_poly * poly)1282 __isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_space *space,
1283 	unsigned n_div, __isl_take isl_poly *poly)
1284 {
1285 	struct isl_qpolynomial *qp = NULL;
1286 	isl_size total;
1287 
1288 	total = isl_space_dim(space, isl_dim_all);
1289 	if (total < 0 || !poly)
1290 		goto error;
1291 
1292 	if (!isl_space_is_set(space))
1293 		isl_die(isl_space_get_ctx(space), isl_error_invalid,
1294 			"domain of polynomial should be a set", goto error);
1295 
1296 	qp = isl_calloc_type(space->ctx, struct isl_qpolynomial);
1297 	if (!qp)
1298 		goto error;
1299 
1300 	qp->ref = 1;
1301 	qp->div = isl_mat_alloc(space->ctx, n_div, 1 + 1 + total + n_div);
1302 	if (!qp->div)
1303 		goto error;
1304 
1305 	qp->dim = space;
1306 	qp->poly = poly;
1307 
1308 	return qp;
1309 error:
1310 	isl_space_free(space);
1311 	isl_poly_free(poly);
1312 	isl_qpolynomial_free(qp);
1313 	return NULL;
1314 }
1315 
isl_qpolynomial_copy(__isl_keep isl_qpolynomial * qp)1316 __isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
1317 {
1318 	if (!qp)
1319 		return NULL;
1320 
1321 	qp->ref++;
1322 	return qp;
1323 }
1324 
isl_qpolynomial_dup(__isl_keep isl_qpolynomial * qp)1325 __isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
1326 {
1327 	struct isl_qpolynomial *dup;
1328 
1329 	if (!qp)
1330 		return NULL;
1331 
1332 	dup = isl_qpolynomial_alloc(isl_space_copy(qp->dim), qp->div->n_row,
1333 				    isl_poly_copy(qp->poly));
1334 	if (!dup)
1335 		return NULL;
1336 	isl_mat_free(dup->div);
1337 	dup->div = isl_mat_copy(qp->div);
1338 	if (!dup->div)
1339 		goto error;
1340 
1341 	return dup;
1342 error:
1343 	isl_qpolynomial_free(dup);
1344 	return NULL;
1345 }
1346 
isl_qpolynomial_cow(__isl_take isl_qpolynomial * qp)1347 __isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
1348 {
1349 	if (!qp)
1350 		return NULL;
1351 
1352 	if (qp->ref == 1)
1353 		return qp;
1354 	qp->ref--;
1355 	return isl_qpolynomial_dup(qp);
1356 }
1357 
isl_qpolynomial_free(__isl_take isl_qpolynomial * qp)1358 __isl_null isl_qpolynomial *isl_qpolynomial_free(
1359 	__isl_take isl_qpolynomial *qp)
1360 {
1361 	if (!qp)
1362 		return NULL;
1363 
1364 	if (--qp->ref > 0)
1365 		return NULL;
1366 
1367 	isl_space_free(qp->dim);
1368 	isl_mat_free(qp->div);
1369 	isl_poly_free(qp->poly);
1370 
1371 	free(qp);
1372 	return NULL;
1373 }
1374 
isl_poly_var_pow(isl_ctx * ctx,int pos,int power)1375 __isl_give isl_poly *isl_poly_var_pow(isl_ctx *ctx, int pos, int power)
1376 {
1377 	int i;
1378 	isl_poly_rec *rec;
1379 	isl_poly_cst *cst;
1380 
1381 	rec = isl_poly_alloc_rec(ctx, pos, 1 + power);
1382 	if (!rec)
1383 		return NULL;
1384 	for (i = 0; i < 1 + power; ++i) {
1385 		rec->p[i] = isl_poly_zero(ctx);
1386 		if (!rec->p[i])
1387 			goto error;
1388 		rec->n++;
1389 	}
1390 	cst = isl_poly_as_cst(rec->p[power]);
1391 	isl_int_set_si(cst->n, 1);
1392 
1393 	return &rec->poly;
1394 error:
1395 	isl_poly_free(&rec->poly);
1396 	return NULL;
1397 }
1398 
1399 /* r array maps original positions to new positions.
1400  */
reorder(__isl_take isl_poly * poly,int * r)1401 static __isl_give isl_poly *reorder(__isl_take isl_poly *poly, int *r)
1402 {
1403 	int i;
1404 	isl_bool is_cst;
1405 	isl_poly_rec *rec;
1406 	isl_poly *base;
1407 	isl_poly *res;
1408 
1409 	is_cst = isl_poly_is_cst(poly);
1410 	if (is_cst < 0)
1411 		return isl_poly_free(poly);
1412 	if (is_cst)
1413 		return poly;
1414 
1415 	rec = isl_poly_as_rec(poly);
1416 	if (!rec)
1417 		goto error;
1418 
1419 	isl_assert(poly->ctx, rec->n >= 1, goto error);
1420 
1421 	base = isl_poly_var_pow(poly->ctx, r[poly->var], 1);
1422 	res = reorder(isl_poly_copy(rec->p[rec->n - 1]), r);
1423 
1424 	for (i = rec->n - 2; i >= 0; --i) {
1425 		res = isl_poly_mul(res, isl_poly_copy(base));
1426 		res = isl_poly_sum(res, reorder(isl_poly_copy(rec->p[i]), r));
1427 	}
1428 
1429 	isl_poly_free(base);
1430 	isl_poly_free(poly);
1431 
1432 	return res;
1433 error:
1434 	isl_poly_free(poly);
1435 	return NULL;
1436 }
1437 
compatible_divs(__isl_keep isl_mat * div1,__isl_keep isl_mat * div2)1438 static isl_bool compatible_divs(__isl_keep isl_mat *div1,
1439 	__isl_keep isl_mat *div2)
1440 {
1441 	int n_row, n_col;
1442 	isl_bool equal;
1443 
1444 	isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
1445 				div1->n_col >= div2->n_col,
1446 		    return isl_bool_error);
1447 
1448 	if (div1->n_row == div2->n_row)
1449 		return isl_mat_is_equal(div1, div2);
1450 
1451 	n_row = div1->n_row;
1452 	n_col = div1->n_col;
1453 	div1->n_row = div2->n_row;
1454 	div1->n_col = div2->n_col;
1455 
1456 	equal = isl_mat_is_equal(div1, div2);
1457 
1458 	div1->n_row = n_row;
1459 	div1->n_col = n_col;
1460 
1461 	return equal;
1462 }
1463 
cmp_row(__isl_keep isl_mat * div,int i,int j)1464 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
1465 {
1466 	int li, lj;
1467 
1468 	li = isl_seq_last_non_zero(div->row[i], div->n_col);
1469 	lj = isl_seq_last_non_zero(div->row[j], div->n_col);
1470 
1471 	if (li != lj)
1472 		return li - lj;
1473 
1474 	return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
1475 }
1476 
1477 struct isl_div_sort_info {
1478 	isl_mat	*div;
1479 	int	 row;
1480 };
1481 
div_sort_cmp(const void * p1,const void * p2)1482 static int div_sort_cmp(const void *p1, const void *p2)
1483 {
1484 	const struct isl_div_sort_info *i1, *i2;
1485 	i1 = (const struct isl_div_sort_info *) p1;
1486 	i2 = (const struct isl_div_sort_info *) p2;
1487 
1488 	return cmp_row(i1->div, i1->row, i2->row);
1489 }
1490 
1491 /* Sort divs and remove duplicates.
1492  */
sort_divs(__isl_take isl_qpolynomial * qp)1493 static __isl_give isl_qpolynomial *sort_divs(__isl_take isl_qpolynomial *qp)
1494 {
1495 	int i;
1496 	int skip;
1497 	int len;
1498 	struct isl_div_sort_info *array = NULL;
1499 	int *pos = NULL, *at = NULL;
1500 	int *reordering = NULL;
1501 	isl_size div_pos;
1502 
1503 	if (!qp)
1504 		return NULL;
1505 	if (qp->div->n_row <= 1)
1506 		return qp;
1507 
1508 	div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
1509 	if (div_pos < 0)
1510 		return isl_qpolynomial_free(qp);
1511 
1512 	array = isl_alloc_array(qp->div->ctx, struct isl_div_sort_info,
1513 				qp->div->n_row);
1514 	pos = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1515 	at = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1516 	len = qp->div->n_col - 2;
1517 	reordering = isl_alloc_array(qp->div->ctx, int, len);
1518 	if (!array || !pos || !at || !reordering)
1519 		goto error;
1520 
1521 	for (i = 0; i < qp->div->n_row; ++i) {
1522 		array[i].div = qp->div;
1523 		array[i].row = i;
1524 		pos[i] = i;
1525 		at[i] = i;
1526 	}
1527 
1528 	qsort(array, qp->div->n_row, sizeof(struct isl_div_sort_info),
1529 		div_sort_cmp);
1530 
1531 	for (i = 0; i < div_pos; ++i)
1532 		reordering[i] = i;
1533 
1534 	for (i = 0; i < qp->div->n_row; ++i) {
1535 		if (pos[array[i].row] == i)
1536 			continue;
1537 		qp->div = isl_mat_swap_rows(qp->div, i, pos[array[i].row]);
1538 		pos[at[i]] = pos[array[i].row];
1539 		at[pos[array[i].row]] = at[i];
1540 		at[i] = array[i].row;
1541 		pos[array[i].row] = i;
1542 	}
1543 
1544 	skip = 0;
1545 	for (i = 0; i < len - div_pos; ++i) {
1546 		if (i > 0 &&
1547 		    isl_seq_eq(qp->div->row[i - skip - 1],
1548 			       qp->div->row[i - skip], qp->div->n_col)) {
1549 			qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
1550 			isl_mat_col_add(qp->div, 2 + div_pos + i - skip - 1,
1551 						 2 + div_pos + i - skip);
1552 			qp->div = isl_mat_drop_cols(qp->div,
1553 						    2 + div_pos + i - skip, 1);
1554 			skip++;
1555 		}
1556 		reordering[div_pos + array[i].row] = div_pos + i - skip;
1557 	}
1558 
1559 	qp->poly = reorder(qp->poly, reordering);
1560 
1561 	if (!qp->poly || !qp->div)
1562 		goto error;
1563 
1564 	free(at);
1565 	free(pos);
1566 	free(array);
1567 	free(reordering);
1568 
1569 	return qp;
1570 error:
1571 	free(at);
1572 	free(pos);
1573 	free(array);
1574 	free(reordering);
1575 	isl_qpolynomial_free(qp);
1576 	return NULL;
1577 }
1578 
expand(__isl_take isl_poly * poly,int * exp,int first)1579 static __isl_give isl_poly *expand(__isl_take isl_poly *poly, int *exp,
1580 	int first)
1581 {
1582 	int i;
1583 	isl_bool is_cst;
1584 	isl_poly_rec *rec;
1585 
1586 	is_cst = isl_poly_is_cst(poly);
1587 	if (is_cst < 0)
1588 		return isl_poly_free(poly);
1589 	if (is_cst)
1590 		return poly;
1591 
1592 	if (poly->var < first)
1593 		return poly;
1594 
1595 	if (exp[poly->var - first] == poly->var - first)
1596 		return poly;
1597 
1598 	poly = isl_poly_cow(poly);
1599 	if (!poly)
1600 		goto error;
1601 
1602 	poly->var = exp[poly->var - first] + first;
1603 
1604 	rec = isl_poly_as_rec(poly);
1605 	if (!rec)
1606 		goto error;
1607 
1608 	for (i = 0; i < rec->n; ++i) {
1609 		rec->p[i] = expand(rec->p[i], exp, first);
1610 		if (!rec->p[i])
1611 			goto error;
1612 	}
1613 
1614 	return poly;
1615 error:
1616 	isl_poly_free(poly);
1617 	return NULL;
1618 }
1619 
with_merged_divs(__isl_give isl_qpolynomial * (* fn)(__isl_take isl_qpolynomial * qp1,__isl_take isl_qpolynomial * qp2),__isl_take isl_qpolynomial * qp1,__isl_take isl_qpolynomial * qp2)1620 static __isl_give isl_qpolynomial *with_merged_divs(
1621 	__isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
1622 					  __isl_take isl_qpolynomial *qp2),
1623 	__isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1624 {
1625 	int *exp1 = NULL;
1626 	int *exp2 = NULL;
1627 	isl_mat *div = NULL;
1628 	int n_div1, n_div2;
1629 
1630 	qp1 = isl_qpolynomial_cow(qp1);
1631 	qp2 = isl_qpolynomial_cow(qp2);
1632 
1633 	if (!qp1 || !qp2)
1634 		goto error;
1635 
1636 	isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
1637 				qp1->div->n_col >= qp2->div->n_col, goto error);
1638 
1639 	n_div1 = qp1->div->n_row;
1640 	n_div2 = qp2->div->n_row;
1641 	exp1 = isl_alloc_array(qp1->div->ctx, int, n_div1);
1642 	exp2 = isl_alloc_array(qp2->div->ctx, int, n_div2);
1643 	if ((n_div1 && !exp1) || (n_div2 && !exp2))
1644 		goto error;
1645 
1646 	div = isl_merge_divs(qp1->div, qp2->div, exp1, exp2);
1647 	if (!div)
1648 		goto error;
1649 
1650 	isl_mat_free(qp1->div);
1651 	qp1->div = isl_mat_copy(div);
1652 	isl_mat_free(qp2->div);
1653 	qp2->div = isl_mat_copy(div);
1654 
1655 	qp1->poly = expand(qp1->poly, exp1, div->n_col - div->n_row - 2);
1656 	qp2->poly = expand(qp2->poly, exp2, div->n_col - div->n_row - 2);
1657 
1658 	if (!qp1->poly || !qp2->poly)
1659 		goto error;
1660 
1661 	isl_mat_free(div);
1662 	free(exp1);
1663 	free(exp2);
1664 
1665 	return fn(qp1, qp2);
1666 error:
1667 	isl_mat_free(div);
1668 	free(exp1);
1669 	free(exp2);
1670 	isl_qpolynomial_free(qp1);
1671 	isl_qpolynomial_free(qp2);
1672 	return NULL;
1673 }
1674 
isl_qpolynomial_add(__isl_take isl_qpolynomial * qp1,__isl_take isl_qpolynomial * qp2)1675 __isl_give isl_qpolynomial *isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1,
1676 	__isl_take isl_qpolynomial *qp2)
1677 {
1678 	isl_bool compatible;
1679 
1680 	qp1 = isl_qpolynomial_cow(qp1);
1681 
1682 	if (isl_qpolynomial_check_equal_space(qp1, qp2) < 0)
1683 		goto error;
1684 
1685 	if (qp1->div->n_row < qp2->div->n_row)
1686 		return isl_qpolynomial_add(qp2, qp1);
1687 
1688 	compatible = compatible_divs(qp1->div, qp2->div);
1689 	if (compatible < 0)
1690 		goto error;
1691 	if (!compatible)
1692 		return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1693 
1694 	qp1->poly = isl_poly_sum(qp1->poly, isl_poly_copy(qp2->poly));
1695 	if (!qp1->poly)
1696 		goto error;
1697 
1698 	isl_qpolynomial_free(qp2);
1699 
1700 	return qp1;
1701 error:
1702 	isl_qpolynomial_free(qp1);
1703 	isl_qpolynomial_free(qp2);
1704 	return NULL;
1705 }
1706 
isl_qpolynomial_add_on_domain(__isl_keep isl_set * dom,__isl_take isl_qpolynomial * qp1,__isl_take isl_qpolynomial * qp2)1707 __isl_give isl_qpolynomial *isl_qpolynomial_add_on_domain(
1708 	__isl_keep isl_set *dom,
1709 	__isl_take isl_qpolynomial *qp1,
1710 	__isl_take isl_qpolynomial *qp2)
1711 {
1712 	qp1 = isl_qpolynomial_add(qp1, qp2);
1713 	qp1 = isl_qpolynomial_gist(qp1, isl_set_copy(dom));
1714 	return qp1;
1715 }
1716 
isl_qpolynomial_sub(__isl_take isl_qpolynomial * qp1,__isl_take isl_qpolynomial * qp2)1717 __isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
1718 	__isl_take isl_qpolynomial *qp2)
1719 {
1720 	return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
1721 }
1722 
isl_qpolynomial_add_isl_int(__isl_take isl_qpolynomial * qp,isl_int v)1723 __isl_give isl_qpolynomial *isl_qpolynomial_add_isl_int(
1724 	__isl_take isl_qpolynomial *qp, isl_int v)
1725 {
1726 	if (isl_int_is_zero(v))
1727 		return qp;
1728 
1729 	qp = isl_qpolynomial_cow(qp);
1730 	if (!qp)
1731 		return NULL;
1732 
1733 	qp->poly = isl_poly_add_isl_int(qp->poly, v);
1734 	if (!qp->poly)
1735 		goto error;
1736 
1737 	return qp;
1738 error:
1739 	isl_qpolynomial_free(qp);
1740 	return NULL;
1741 
1742 }
1743 
isl_qpolynomial_neg(__isl_take isl_qpolynomial * qp)1744 __isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
1745 {
1746 	if (!qp)
1747 		return NULL;
1748 
1749 	return isl_qpolynomial_mul_isl_int(qp, qp->dim->ctx->negone);
1750 }
1751 
isl_qpolynomial_mul_isl_int(__isl_take isl_qpolynomial * qp,isl_int v)1752 __isl_give isl_qpolynomial *isl_qpolynomial_mul_isl_int(
1753 	__isl_take isl_qpolynomial *qp, isl_int v)
1754 {
1755 	if (isl_int_is_one(v))
1756 		return qp;
1757 
1758 	if (qp && isl_int_is_zero(v)) {
1759 		isl_qpolynomial *zero;
1760 		zero = isl_qpolynomial_zero_on_domain(isl_space_copy(qp->dim));
1761 		isl_qpolynomial_free(qp);
1762 		return zero;
1763 	}
1764 
1765 	qp = isl_qpolynomial_cow(qp);
1766 	if (!qp)
1767 		return NULL;
1768 
1769 	qp->poly = isl_poly_mul_isl_int(qp->poly, v);
1770 	if (!qp->poly)
1771 		goto error;
1772 
1773 	return qp;
1774 error:
1775 	isl_qpolynomial_free(qp);
1776 	return NULL;
1777 }
1778 
isl_qpolynomial_scale(__isl_take isl_qpolynomial * qp,isl_int v)1779 __isl_give isl_qpolynomial *isl_qpolynomial_scale(
1780 	__isl_take isl_qpolynomial *qp, isl_int v)
1781 {
1782 	return isl_qpolynomial_mul_isl_int(qp, v);
1783 }
1784 
1785 /* Multiply "qp" by "v".
1786  */
isl_qpolynomial_scale_val(__isl_take isl_qpolynomial * qp,__isl_take isl_val * v)1787 __isl_give isl_qpolynomial *isl_qpolynomial_scale_val(
1788 	__isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
1789 {
1790 	if (!qp || !v)
1791 		goto error;
1792 
1793 	if (!isl_val_is_rat(v))
1794 		isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
1795 			"expecting rational factor", goto error);
1796 
1797 	if (isl_val_is_one(v)) {
1798 		isl_val_free(v);
1799 		return qp;
1800 	}
1801 
1802 	if (isl_val_is_zero(v)) {
1803 		isl_space *space;
1804 
1805 		space = isl_qpolynomial_get_domain_space(qp);
1806 		isl_qpolynomial_free(qp);
1807 		isl_val_free(v);
1808 		return isl_qpolynomial_zero_on_domain(space);
1809 	}
1810 
1811 	qp = isl_qpolynomial_cow(qp);
1812 	if (!qp)
1813 		goto error;
1814 
1815 	qp->poly = isl_poly_scale_val(qp->poly, v);
1816 	if (!qp->poly)
1817 		qp = isl_qpolynomial_free(qp);
1818 
1819 	isl_val_free(v);
1820 	return qp;
1821 error:
1822 	isl_val_free(v);
1823 	isl_qpolynomial_free(qp);
1824 	return NULL;
1825 }
1826 
1827 /* Divide "qp" by "v".
1828  */
isl_qpolynomial_scale_down_val(__isl_take isl_qpolynomial * qp,__isl_take isl_val * v)1829 __isl_give isl_qpolynomial *isl_qpolynomial_scale_down_val(
1830 	__isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
1831 {
1832 	if (!qp || !v)
1833 		goto error;
1834 
1835 	if (!isl_val_is_rat(v))
1836 		isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
1837 			"expecting rational factor", goto error);
1838 	if (isl_val_is_zero(v))
1839 		isl_die(isl_val_get_ctx(v), isl_error_invalid,
1840 			"cannot scale down by zero", goto error);
1841 
1842 	return isl_qpolynomial_scale_val(qp, isl_val_inv(v));
1843 error:
1844 	isl_val_free(v);
1845 	isl_qpolynomial_free(qp);
1846 	return NULL;
1847 }
1848 
isl_qpolynomial_mul(__isl_take isl_qpolynomial * qp1,__isl_take isl_qpolynomial * qp2)1849 __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
1850 	__isl_take isl_qpolynomial *qp2)
1851 {
1852 	isl_bool compatible;
1853 
1854 	qp1 = isl_qpolynomial_cow(qp1);
1855 
1856 	if (isl_qpolynomial_check_equal_space(qp1, qp2) < 0)
1857 		goto error;
1858 
1859 	if (qp1->div->n_row < qp2->div->n_row)
1860 		return isl_qpolynomial_mul(qp2, qp1);
1861 
1862 	compatible = compatible_divs(qp1->div, qp2->div);
1863 	if (compatible < 0)
1864 		goto error;
1865 	if (!compatible)
1866 		return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
1867 
1868 	qp1->poly = isl_poly_mul(qp1->poly, isl_poly_copy(qp2->poly));
1869 	if (!qp1->poly)
1870 		goto error;
1871 
1872 	isl_qpolynomial_free(qp2);
1873 
1874 	return qp1;
1875 error:
1876 	isl_qpolynomial_free(qp1);
1877 	isl_qpolynomial_free(qp2);
1878 	return NULL;
1879 }
1880 
isl_qpolynomial_pow(__isl_take isl_qpolynomial * qp,unsigned power)1881 __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_qpolynomial *qp,
1882 	unsigned power)
1883 {
1884 	qp = isl_qpolynomial_cow(qp);
1885 
1886 	if (!qp)
1887 		return NULL;
1888 
1889 	qp->poly = isl_poly_pow(qp->poly, power);
1890 	if (!qp->poly)
1891 		goto error;
1892 
1893 	return qp;
1894 error:
1895 	isl_qpolynomial_free(qp);
1896 	return NULL;
1897 }
1898 
isl_pw_qpolynomial_pow(__isl_take isl_pw_qpolynomial * pwqp,unsigned power)1899 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_pow(
1900 	__isl_take isl_pw_qpolynomial *pwqp, unsigned power)
1901 {
1902 	int i;
1903 
1904 	if (power == 1)
1905 		return pwqp;
1906 
1907 	pwqp = isl_pw_qpolynomial_cow(pwqp);
1908 	if (!pwqp)
1909 		return NULL;
1910 
1911 	for (i = 0; i < pwqp->n; ++i) {
1912 		pwqp->p[i].qp = isl_qpolynomial_pow(pwqp->p[i].qp, power);
1913 		if (!pwqp->p[i].qp)
1914 			return isl_pw_qpolynomial_free(pwqp);
1915 	}
1916 
1917 	return pwqp;
1918 }
1919 
isl_qpolynomial_zero_on_domain(__isl_take isl_space * domain)1920 __isl_give isl_qpolynomial *isl_qpolynomial_zero_on_domain(
1921 	__isl_take isl_space *domain)
1922 {
1923 	if (!domain)
1924 		return NULL;
1925 	return isl_qpolynomial_alloc(domain, 0, isl_poly_zero(domain->ctx));
1926 }
1927 
isl_qpolynomial_one_on_domain(__isl_take isl_space * domain)1928 __isl_give isl_qpolynomial *isl_qpolynomial_one_on_domain(
1929 	__isl_take isl_space *domain)
1930 {
1931 	if (!domain)
1932 		return NULL;
1933 	return isl_qpolynomial_alloc(domain, 0, isl_poly_one(domain->ctx));
1934 }
1935 
isl_qpolynomial_infty_on_domain(__isl_take isl_space * domain)1936 __isl_give isl_qpolynomial *isl_qpolynomial_infty_on_domain(
1937 	__isl_take isl_space *domain)
1938 {
1939 	if (!domain)
1940 		return NULL;
1941 	return isl_qpolynomial_alloc(domain, 0, isl_poly_infty(domain->ctx));
1942 }
1943 
isl_qpolynomial_neginfty_on_domain(__isl_take isl_space * domain)1944 __isl_give isl_qpolynomial *isl_qpolynomial_neginfty_on_domain(
1945 	__isl_take isl_space *domain)
1946 {
1947 	if (!domain)
1948 		return NULL;
1949 	return isl_qpolynomial_alloc(domain, 0, isl_poly_neginfty(domain->ctx));
1950 }
1951 
isl_qpolynomial_nan_on_domain(__isl_take isl_space * domain)1952 __isl_give isl_qpolynomial *isl_qpolynomial_nan_on_domain(
1953 	__isl_take isl_space *domain)
1954 {
1955 	if (!domain)
1956 		return NULL;
1957 	return isl_qpolynomial_alloc(domain, 0, isl_poly_nan(domain->ctx));
1958 }
1959 
isl_qpolynomial_cst_on_domain(__isl_take isl_space * domain,isl_int v)1960 __isl_give isl_qpolynomial *isl_qpolynomial_cst_on_domain(
1961 	__isl_take isl_space *domain,
1962 	isl_int v)
1963 {
1964 	struct isl_qpolynomial *qp;
1965 	isl_poly_cst *cst;
1966 
1967 	qp = isl_qpolynomial_zero_on_domain(domain);
1968 	if (!qp)
1969 		return NULL;
1970 
1971 	cst = isl_poly_as_cst(qp->poly);
1972 	isl_int_set(cst->n, v);
1973 
1974 	return qp;
1975 }
1976 
isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial * qp,isl_int * n,isl_int * d)1977 isl_bool isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
1978 	isl_int *n, isl_int *d)
1979 {
1980 	isl_bool is_cst;
1981 	isl_poly_cst *cst;
1982 
1983 	if (!qp)
1984 		return isl_bool_error;
1985 
1986 	is_cst = isl_poly_is_cst(qp->poly);
1987 	if (is_cst < 0 || !is_cst)
1988 		return is_cst;
1989 
1990 	cst = isl_poly_as_cst(qp->poly);
1991 	if (!cst)
1992 		return isl_bool_error;
1993 
1994 	if (n)
1995 		isl_int_set(*n, cst->n);
1996 	if (d)
1997 		isl_int_set(*d, cst->d);
1998 
1999 	return isl_bool_true;
2000 }
2001 
2002 /* Return the constant term of "poly".
2003  */
isl_poly_get_constant_val(__isl_keep isl_poly * poly)2004 static __isl_give isl_val *isl_poly_get_constant_val(__isl_keep isl_poly *poly)
2005 {
2006 	isl_bool is_cst;
2007 	isl_poly_cst *cst;
2008 
2009 	if (!poly)
2010 		return NULL;
2011 
2012 	while ((is_cst = isl_poly_is_cst(poly)) == isl_bool_false) {
2013 		isl_poly_rec *rec;
2014 
2015 		rec = isl_poly_as_rec(poly);
2016 		if (!rec)
2017 			return NULL;
2018 		poly = rec->p[0];
2019 	}
2020 	if (is_cst < 0)
2021 		return NULL;
2022 
2023 	cst = isl_poly_as_cst(poly);
2024 	if (!cst)
2025 		return NULL;
2026 	return isl_val_rat_from_isl_int(cst->poly.ctx, cst->n, cst->d);
2027 }
2028 
2029 /* Return the constant term of "qp".
2030  */
isl_qpolynomial_get_constant_val(__isl_keep isl_qpolynomial * qp)2031 __isl_give isl_val *isl_qpolynomial_get_constant_val(
2032 	__isl_keep isl_qpolynomial *qp)
2033 {
2034 	if (!qp)
2035 		return NULL;
2036 
2037 	return isl_poly_get_constant_val(qp->poly);
2038 }
2039 
isl_poly_is_affine(__isl_keep isl_poly * poly)2040 isl_bool isl_poly_is_affine(__isl_keep isl_poly *poly)
2041 {
2042 	isl_bool is_cst;
2043 	isl_poly_rec *rec;
2044 
2045 	if (!poly)
2046 		return isl_bool_error;
2047 
2048 	if (poly->var < 0)
2049 		return isl_bool_true;
2050 
2051 	rec = isl_poly_as_rec(poly);
2052 	if (!rec)
2053 		return isl_bool_error;
2054 
2055 	if (rec->n > 2)
2056 		return isl_bool_false;
2057 
2058 	isl_assert(poly->ctx, rec->n > 1, return isl_bool_error);
2059 
2060 	is_cst = isl_poly_is_cst(rec->p[1]);
2061 	if (is_cst < 0 || !is_cst)
2062 		return is_cst;
2063 
2064 	return isl_poly_is_affine(rec->p[0]);
2065 }
2066 
isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial * qp)2067 isl_bool isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
2068 {
2069 	if (!qp)
2070 		return isl_bool_error;
2071 
2072 	if (qp->div->n_row > 0)
2073 		return isl_bool_false;
2074 
2075 	return isl_poly_is_affine(qp->poly);
2076 }
2077 
update_coeff(__isl_keep isl_vec * aff,__isl_keep isl_poly_cst * cst,int pos)2078 static void update_coeff(__isl_keep isl_vec *aff,
2079 	__isl_keep isl_poly_cst *cst, int pos)
2080 {
2081 	isl_int gcd;
2082 	isl_int f;
2083 
2084 	if (isl_int_is_zero(cst->n))
2085 		return;
2086 
2087 	isl_int_init(gcd);
2088 	isl_int_init(f);
2089 	isl_int_gcd(gcd, cst->d, aff->el[0]);
2090 	isl_int_divexact(f, cst->d, gcd);
2091 	isl_int_divexact(gcd, aff->el[0], gcd);
2092 	isl_seq_scale(aff->el, aff->el, f, aff->size);
2093 	isl_int_mul(aff->el[1 + pos], gcd, cst->n);
2094 	isl_int_clear(gcd);
2095 	isl_int_clear(f);
2096 }
2097 
isl_poly_update_affine(__isl_keep isl_poly * poly,__isl_keep isl_vec * aff)2098 int isl_poly_update_affine(__isl_keep isl_poly *poly, __isl_keep isl_vec *aff)
2099 {
2100 	isl_poly_cst *cst;
2101 	isl_poly_rec *rec;
2102 
2103 	if (!poly || !aff)
2104 		return -1;
2105 
2106 	if (poly->var < 0) {
2107 		isl_poly_cst *cst;
2108 
2109 		cst = isl_poly_as_cst(poly);
2110 		if (!cst)
2111 			return -1;
2112 		update_coeff(aff, cst, 0);
2113 		return 0;
2114 	}
2115 
2116 	rec = isl_poly_as_rec(poly);
2117 	if (!rec)
2118 		return -1;
2119 	isl_assert(poly->ctx, rec->n == 2, return -1);
2120 
2121 	cst = isl_poly_as_cst(rec->p[1]);
2122 	if (!cst)
2123 		return -1;
2124 	update_coeff(aff, cst, 1 + poly->var);
2125 
2126 	return isl_poly_update_affine(rec->p[0], aff);
2127 }
2128 
isl_qpolynomial_extract_affine(__isl_keep isl_qpolynomial * qp)2129 __isl_give isl_vec *isl_qpolynomial_extract_affine(
2130 	__isl_keep isl_qpolynomial *qp)
2131 {
2132 	isl_vec *aff;
2133 	isl_size d;
2134 
2135 	d = isl_qpolynomial_domain_dim(qp, isl_dim_all);
2136 	if (d < 0)
2137 		return NULL;
2138 
2139 	aff = isl_vec_alloc(qp->div->ctx, 2 + d);
2140 	if (!aff)
2141 		return NULL;
2142 
2143 	isl_seq_clr(aff->el + 1, 1 + d);
2144 	isl_int_set_si(aff->el[0], 1);
2145 
2146 	if (isl_poly_update_affine(qp->poly, aff) < 0)
2147 		goto error;
2148 
2149 	return aff;
2150 error:
2151 	isl_vec_free(aff);
2152 	return NULL;
2153 }
2154 
2155 /* Compare two quasi-polynomials.
2156  *
2157  * Return -1 if "qp1" is "smaller" than "qp2", 1 if "qp1" is "greater"
2158  * than "qp2" and 0 if they are equal.
2159  */
isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial * qp1,__isl_keep isl_qpolynomial * qp2)2160 int isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial *qp1,
2161 	__isl_keep isl_qpolynomial *qp2)
2162 {
2163 	int cmp;
2164 
2165 	if (qp1 == qp2)
2166 		return 0;
2167 	if (!qp1)
2168 		return -1;
2169 	if (!qp2)
2170 		return 1;
2171 
2172 	cmp = isl_space_cmp(qp1->dim, qp2->dim);
2173 	if (cmp != 0)
2174 		return cmp;
2175 
2176 	cmp = isl_local_cmp(qp1->div, qp2->div);
2177 	if (cmp != 0)
2178 		return cmp;
2179 
2180 	return isl_poly_plain_cmp(qp1->poly, qp2->poly);
2181 }
2182 
2183 /* Is "qp1" obviously equal to "qp2"?
2184  *
2185  * NaN is not equal to anything, not even to another NaN.
2186  */
isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial * qp1,__isl_keep isl_qpolynomial * qp2)2187 isl_bool isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial *qp1,
2188 	__isl_keep isl_qpolynomial *qp2)
2189 {
2190 	isl_bool equal;
2191 
2192 	if (!qp1 || !qp2)
2193 		return isl_bool_error;
2194 
2195 	if (isl_qpolynomial_is_nan(qp1) || isl_qpolynomial_is_nan(qp2))
2196 		return isl_bool_false;
2197 
2198 	equal = isl_space_is_equal(qp1->dim, qp2->dim);
2199 	if (equal < 0 || !equal)
2200 		return equal;
2201 
2202 	equal = isl_mat_is_equal(qp1->div, qp2->div);
2203 	if (equal < 0 || !equal)
2204 		return equal;
2205 
2206 	return isl_poly_is_equal(qp1->poly, qp2->poly);
2207 }
2208 
poly_update_den(__isl_keep isl_poly * poly,isl_int * d)2209 static isl_stat poly_update_den(__isl_keep isl_poly *poly, isl_int *d)
2210 {
2211 	int i;
2212 	isl_bool is_cst;
2213 	isl_poly_rec *rec;
2214 
2215 	is_cst = isl_poly_is_cst(poly);
2216 	if (is_cst < 0)
2217 		return isl_stat_error;
2218 	if (is_cst) {
2219 		isl_poly_cst *cst;
2220 		cst = isl_poly_as_cst(poly);
2221 		if (!cst)
2222 			return isl_stat_error;
2223 		isl_int_lcm(*d, *d, cst->d);
2224 		return isl_stat_ok;
2225 	}
2226 
2227 	rec = isl_poly_as_rec(poly);
2228 	if (!rec)
2229 		return isl_stat_error;
2230 
2231 	for (i = 0; i < rec->n; ++i)
2232 		poly_update_den(rec->p[i], d);
2233 
2234 	return isl_stat_ok;
2235 }
2236 
isl_qpolynomial_get_den(__isl_keep isl_qpolynomial * qp)2237 __isl_give isl_val *isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp)
2238 {
2239 	isl_val *d;
2240 
2241 	if (!qp)
2242 		return NULL;
2243 	d = isl_val_one(isl_qpolynomial_get_ctx(qp));
2244 	if (!d)
2245 		return NULL;
2246 	if (poly_update_den(qp->poly, &d->n) < 0)
2247 		return isl_val_free(d);
2248 	return d;
2249 }
2250 
isl_qpolynomial_var_pow_on_domain(__isl_take isl_space * domain,int pos,int power)2251 __isl_give isl_qpolynomial *isl_qpolynomial_var_pow_on_domain(
2252 	__isl_take isl_space *domain, int pos, int power)
2253 {
2254 	struct isl_ctx *ctx;
2255 
2256 	if (!domain)
2257 		return NULL;
2258 
2259 	ctx = domain->ctx;
2260 
2261 	return isl_qpolynomial_alloc(domain, 0,
2262 					isl_poly_var_pow(ctx, pos, power));
2263 }
2264 
isl_qpolynomial_var_on_domain(__isl_take isl_space * domain,enum isl_dim_type type,unsigned pos)2265 __isl_give isl_qpolynomial *isl_qpolynomial_var_on_domain(
2266 	__isl_take isl_space *domain, enum isl_dim_type type, unsigned pos)
2267 {
2268 	if (isl_space_check_is_set(domain ) < 0)
2269 		goto error;
2270 	if (isl_space_check_range(domain, type, pos, 1) < 0)
2271 		goto error;
2272 
2273 	pos += isl_space_offset(domain, type);
2274 
2275 	return isl_qpolynomial_var_pow_on_domain(domain, pos, 1);
2276 error:
2277 	isl_space_free(domain);
2278 	return NULL;
2279 }
2280 
isl_poly_subs(__isl_take isl_poly * poly,unsigned first,unsigned n,__isl_keep isl_poly ** subs)2281 __isl_give isl_poly *isl_poly_subs(__isl_take isl_poly *poly,
2282 	unsigned first, unsigned n, __isl_keep isl_poly **subs)
2283 {
2284 	int i;
2285 	isl_bool is_cst;
2286 	isl_poly_rec *rec;
2287 	isl_poly *base, *res;
2288 
2289 	is_cst = isl_poly_is_cst(poly);
2290 	if (is_cst < 0)
2291 		return isl_poly_free(poly);
2292 	if (is_cst)
2293 		return poly;
2294 
2295 	if (poly->var < first)
2296 		return poly;
2297 
2298 	rec = isl_poly_as_rec(poly);
2299 	if (!rec)
2300 		goto error;
2301 
2302 	isl_assert(poly->ctx, rec->n >= 1, goto error);
2303 
2304 	if (poly->var >= first + n)
2305 		base = isl_poly_var_pow(poly->ctx, poly->var, 1);
2306 	else
2307 		base = isl_poly_copy(subs[poly->var - first]);
2308 
2309 	res = isl_poly_subs(isl_poly_copy(rec->p[rec->n - 1]), first, n, subs);
2310 	for (i = rec->n - 2; i >= 0; --i) {
2311 		isl_poly *t;
2312 		t = isl_poly_subs(isl_poly_copy(rec->p[i]), first, n, subs);
2313 		res = isl_poly_mul(res, isl_poly_copy(base));
2314 		res = isl_poly_sum(res, t);
2315 	}
2316 
2317 	isl_poly_free(base);
2318 	isl_poly_free(poly);
2319 
2320 	return res;
2321 error:
2322 	isl_poly_free(poly);
2323 	return NULL;
2324 }
2325 
isl_poly_from_affine(isl_ctx * ctx,isl_int * f,isl_int denom,unsigned len)2326 __isl_give isl_poly *isl_poly_from_affine(isl_ctx *ctx, isl_int *f,
2327 	isl_int denom, unsigned len)
2328 {
2329 	int i;
2330 	isl_poly *poly;
2331 
2332 	isl_assert(ctx, len >= 1, return NULL);
2333 
2334 	poly = isl_poly_rat_cst(ctx, f[0], denom);
2335 	for (i = 0; i < len - 1; ++i) {
2336 		isl_poly *t;
2337 		isl_poly *c;
2338 
2339 		if (isl_int_is_zero(f[1 + i]))
2340 			continue;
2341 
2342 		c = isl_poly_rat_cst(ctx, f[1 + i], denom);
2343 		t = isl_poly_var_pow(ctx, i, 1);
2344 		t = isl_poly_mul(c, t);
2345 		poly = isl_poly_sum(poly, t);
2346 	}
2347 
2348 	return poly;
2349 }
2350 
2351 /* Remove common factor of non-constant terms and denominator.
2352  */
normalize_div(__isl_keep isl_qpolynomial * qp,int div)2353 static void normalize_div(__isl_keep isl_qpolynomial *qp, int div)
2354 {
2355 	isl_ctx *ctx = qp->div->ctx;
2356 	unsigned total = qp->div->n_col - 2;
2357 
2358 	isl_seq_gcd(qp->div->row[div] + 2, total, &ctx->normalize_gcd);
2359 	isl_int_gcd(ctx->normalize_gcd,
2360 		    ctx->normalize_gcd, qp->div->row[div][0]);
2361 	if (isl_int_is_one(ctx->normalize_gcd))
2362 		return;
2363 
2364 	isl_seq_scale_down(qp->div->row[div] + 2, qp->div->row[div] + 2,
2365 			    ctx->normalize_gcd, total);
2366 	isl_int_divexact(qp->div->row[div][0], qp->div->row[div][0],
2367 			    ctx->normalize_gcd);
2368 	isl_int_fdiv_q(qp->div->row[div][1], qp->div->row[div][1],
2369 			    ctx->normalize_gcd);
2370 }
2371 
2372 /* Replace the integer division identified by "div" by the polynomial "s".
2373  * The integer division is assumed not to appear in the definition
2374  * of any other integer divisions.
2375  */
substitute_div(__isl_take isl_qpolynomial * qp,int div,__isl_take isl_poly * s)2376 static __isl_give isl_qpolynomial *substitute_div(
2377 	__isl_take isl_qpolynomial *qp, int div, __isl_take isl_poly *s)
2378 {
2379 	int i;
2380 	isl_size div_pos;
2381 	int *reordering;
2382 	isl_ctx *ctx;
2383 
2384 	if (!qp || !s)
2385 		goto error;
2386 
2387 	qp = isl_qpolynomial_cow(qp);
2388 	if (!qp)
2389 		goto error;
2390 
2391 	div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
2392 	if (div_pos < 0)
2393 		goto error;
2394 	qp->poly = isl_poly_subs(qp->poly, div_pos + div, 1, &s);
2395 	if (!qp->poly)
2396 		goto error;
2397 
2398 	ctx = isl_qpolynomial_get_ctx(qp);
2399 	reordering = isl_alloc_array(ctx, int, div_pos + qp->div->n_row);
2400 	if (!reordering)
2401 		goto error;
2402 	for (i = 0; i < div_pos + div; ++i)
2403 		reordering[i] = i;
2404 	for (i = div_pos + div + 1; i < div_pos + qp->div->n_row; ++i)
2405 		reordering[i] = i - 1;
2406 	qp->div = isl_mat_drop_rows(qp->div, div, 1);
2407 	qp->div = isl_mat_drop_cols(qp->div, 2 + div_pos + div, 1);
2408 	qp->poly = reorder(qp->poly, reordering);
2409 	free(reordering);
2410 
2411 	if (!qp->poly || !qp->div)
2412 		goto error;
2413 
2414 	isl_poly_free(s);
2415 	return qp;
2416 error:
2417 	isl_qpolynomial_free(qp);
2418 	isl_poly_free(s);
2419 	return NULL;
2420 }
2421 
2422 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2423  * divisions because d is equal to 1 by their definition, i.e., e.
2424  */
substitute_non_divs(__isl_take isl_qpolynomial * qp)2425 static __isl_give isl_qpolynomial *substitute_non_divs(
2426 	__isl_take isl_qpolynomial *qp)
2427 {
2428 	int i, j;
2429 	isl_size div_pos;
2430 	isl_poly *s;
2431 
2432 	div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
2433 	if (div_pos < 0)
2434 		return isl_qpolynomial_free(qp);
2435 
2436 	for (i = 0; qp && i < qp->div->n_row; ++i) {
2437 		if (!isl_int_is_one(qp->div->row[i][0]))
2438 			continue;
2439 		for (j = i + 1; j < qp->div->n_row; ++j) {
2440 			if (isl_int_is_zero(qp->div->row[j][2 + div_pos + i]))
2441 				continue;
2442 			isl_seq_combine(qp->div->row[j] + 1,
2443 				qp->div->ctx->one, qp->div->row[j] + 1,
2444 				qp->div->row[j][2 + div_pos + i],
2445 				qp->div->row[i] + 1, 1 + div_pos + i);
2446 			isl_int_set_si(qp->div->row[j][2 + div_pos + i], 0);
2447 			normalize_div(qp, j);
2448 		}
2449 		s = isl_poly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
2450 					qp->div->row[i][0], qp->div->n_col - 1);
2451 		qp = substitute_div(qp, i, s);
2452 		--i;
2453 	}
2454 
2455 	return qp;
2456 }
2457 
2458 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2459  * with d the denominator.  When replacing the coefficient e of x by
2460  * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2461  * inside the division, so we need to add floor(e/d) * x outside.
2462  * That is, we replace q by q' + floor(e/d) * x and we therefore need
2463  * to adjust the coefficient of x in each later div that depends on the
2464  * current div "div" and also in the affine expressions in the rows of "mat"
2465  * (if they too depend on "div").
2466  */
reduce_div(__isl_keep isl_qpolynomial * qp,int div,__isl_keep isl_mat ** mat)2467 static void reduce_div(__isl_keep isl_qpolynomial *qp, int div,
2468 	__isl_keep isl_mat **mat)
2469 {
2470 	int i, j;
2471 	isl_int v;
2472 	unsigned total = qp->div->n_col - qp->div->n_row - 2;
2473 
2474 	isl_int_init(v);
2475 	for (i = 0; i < 1 + total + div; ++i) {
2476 		if (isl_int_is_nonneg(qp->div->row[div][1 + i]) &&
2477 		    isl_int_lt(qp->div->row[div][1 + i], qp->div->row[div][0]))
2478 			continue;
2479 		isl_int_fdiv_q(v, qp->div->row[div][1 + i], qp->div->row[div][0]);
2480 		isl_int_fdiv_r(qp->div->row[div][1 + i],
2481 				qp->div->row[div][1 + i], qp->div->row[div][0]);
2482 		*mat = isl_mat_col_addmul(*mat, i, v, 1 + total + div);
2483 		for (j = div + 1; j < qp->div->n_row; ++j) {
2484 			if (isl_int_is_zero(qp->div->row[j][2 + total + div]))
2485 				continue;
2486 			isl_int_addmul(qp->div->row[j][1 + i],
2487 					v, qp->div->row[j][2 + total + div]);
2488 		}
2489 	}
2490 	isl_int_clear(v);
2491 }
2492 
2493 /* Check if the last non-zero coefficient is bigger that half of the
2494  * denominator.  If so, we will invert the div to further reduce the number
2495  * of distinct divs that may appear.
2496  * If the last non-zero coefficient is exactly half the denominator,
2497  * then we continue looking for earlier coefficients that are bigger
2498  * than half the denominator.
2499  */
needs_invert(__isl_keep isl_mat * div,int row)2500 static int needs_invert(__isl_keep isl_mat *div, int row)
2501 {
2502 	int i;
2503 	int cmp;
2504 
2505 	for (i = div->n_col - 1; i >= 1; --i) {
2506 		if (isl_int_is_zero(div->row[row][i]))
2507 			continue;
2508 		isl_int_mul_ui(div->row[row][i], div->row[row][i], 2);
2509 		cmp = isl_int_cmp(div->row[row][i], div->row[row][0]);
2510 		isl_int_divexact_ui(div->row[row][i], div->row[row][i], 2);
2511 		if (cmp)
2512 			return cmp > 0;
2513 		if (i == 1)
2514 			return 1;
2515 	}
2516 
2517 	return 0;
2518 }
2519 
2520 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2521  * We only invert the coefficients of e (and the coefficient of q in
2522  * later divs and in the rows of "mat").  After calling this function, the
2523  * coefficients of e should be reduced again.
2524  */
invert_div(__isl_keep isl_qpolynomial * qp,int div,__isl_keep isl_mat ** mat)2525 static void invert_div(__isl_keep isl_qpolynomial *qp, int div,
2526 	__isl_keep isl_mat **mat)
2527 {
2528 	unsigned total = qp->div->n_col - qp->div->n_row - 2;
2529 
2530 	isl_seq_neg(qp->div->row[div] + 1,
2531 		    qp->div->row[div] + 1, qp->div->n_col - 1);
2532 	isl_int_sub_ui(qp->div->row[div][1], qp->div->row[div][1], 1);
2533 	isl_int_add(qp->div->row[div][1],
2534 		    qp->div->row[div][1], qp->div->row[div][0]);
2535 	*mat = isl_mat_col_neg(*mat, 1 + total + div);
2536 	isl_mat_col_mul(qp->div, 2 + total + div,
2537 			qp->div->ctx->negone, 2 + total + div);
2538 }
2539 
2540 /* Reduce all divs of "qp" to have coefficients
2541  * in the interval [0, d-1], with d the denominator and such that the
2542  * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2543  * The modifications to the integer divisions need to be reflected
2544  * in the factors of the polynomial that refer to the original
2545  * integer divisions.  To this end, the modifications are collected
2546  * as a set of affine expressions and then plugged into the polynomial.
2547  *
2548  * After the reduction, some divs may have become redundant or identical,
2549  * so we call substitute_non_divs and sort_divs.  If these functions
2550  * eliminate divs or merge two or more divs into one, the coefficients
2551  * of the enclosing divs may have to be reduced again, so we call
2552  * ourselves recursively if the number of divs decreases.
2553  */
reduce_divs(__isl_take isl_qpolynomial * qp)2554 static __isl_give isl_qpolynomial *reduce_divs(__isl_take isl_qpolynomial *qp)
2555 {
2556 	int i;
2557 	isl_ctx *ctx;
2558 	isl_mat *mat;
2559 	isl_poly **s;
2560 	unsigned o_div;
2561 	isl_size n_div, total, new_n_div;
2562 
2563 	total = isl_qpolynomial_domain_dim(qp, isl_dim_all);
2564 	n_div = isl_qpolynomial_domain_dim(qp, isl_dim_div);
2565 	o_div = isl_qpolynomial_domain_offset(qp, isl_dim_div);
2566 	if (total < 0 || n_div < 0)
2567 		return isl_qpolynomial_free(qp);
2568 	ctx = isl_qpolynomial_get_ctx(qp);
2569 	mat = isl_mat_zero(ctx, n_div, 1 + total);
2570 
2571 	for (i = 0; i < n_div; ++i)
2572 		mat = isl_mat_set_element_si(mat, i, o_div + i, 1);
2573 
2574 	for (i = 0; i < qp->div->n_row; ++i) {
2575 		normalize_div(qp, i);
2576 		reduce_div(qp, i, &mat);
2577 		if (needs_invert(qp->div, i)) {
2578 			invert_div(qp, i, &mat);
2579 			reduce_div(qp, i, &mat);
2580 		}
2581 	}
2582 	if (!mat)
2583 		goto error;
2584 
2585 	s = isl_alloc_array(ctx, struct isl_poly *, n_div);
2586 	if (n_div && !s)
2587 		goto error;
2588 	for (i = 0; i < n_div; ++i)
2589 		s[i] = isl_poly_from_affine(ctx, mat->row[i], ctx->one,
2590 					    1 + total);
2591 	qp->poly = isl_poly_subs(qp->poly, o_div - 1, n_div, s);
2592 	for (i = 0; i < n_div; ++i)
2593 		isl_poly_free(s[i]);
2594 	free(s);
2595 	if (!qp->poly)
2596 		goto error;
2597 
2598 	isl_mat_free(mat);
2599 
2600 	qp = substitute_non_divs(qp);
2601 	qp = sort_divs(qp);
2602 	new_n_div = isl_qpolynomial_domain_dim(qp, isl_dim_div);
2603 	if (new_n_div < 0)
2604 		return isl_qpolynomial_free(qp);
2605 	if (new_n_div < n_div)
2606 		return reduce_divs(qp);
2607 
2608 	return qp;
2609 error:
2610 	isl_qpolynomial_free(qp);
2611 	isl_mat_free(mat);
2612 	return NULL;
2613 }
2614 
isl_qpolynomial_rat_cst_on_domain(__isl_take isl_space * domain,const isl_int n,const isl_int d)2615 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst_on_domain(
2616 	__isl_take isl_space *domain, const isl_int n, const isl_int d)
2617 {
2618 	struct isl_qpolynomial *qp;
2619 	isl_poly_cst *cst;
2620 
2621 	qp = isl_qpolynomial_zero_on_domain(domain);
2622 	if (!qp)
2623 		return NULL;
2624 
2625 	cst = isl_poly_as_cst(qp->poly);
2626 	isl_int_set(cst->n, n);
2627 	isl_int_set(cst->d, d);
2628 
2629 	return qp;
2630 }
2631 
2632 /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2633  */
isl_qpolynomial_val_on_domain(__isl_take isl_space * domain,__isl_take isl_val * val)2634 __isl_give isl_qpolynomial *isl_qpolynomial_val_on_domain(
2635 	__isl_take isl_space *domain, __isl_take isl_val *val)
2636 {
2637 	isl_qpolynomial *qp;
2638 	isl_poly_cst *cst;
2639 
2640 	qp = isl_qpolynomial_zero_on_domain(domain);
2641 	if (!qp || !val)
2642 		goto error;
2643 
2644 	cst = isl_poly_as_cst(qp->poly);
2645 	isl_int_set(cst->n, val->n);
2646 	isl_int_set(cst->d, val->d);
2647 
2648 	isl_val_free(val);
2649 	return qp;
2650 error:
2651 	isl_val_free(val);
2652 	isl_qpolynomial_free(qp);
2653 	return NULL;
2654 }
2655 
poly_set_active(__isl_keep isl_poly * poly,int * active,int d)2656 static isl_stat poly_set_active(__isl_keep isl_poly *poly, int *active, int d)
2657 {
2658 	isl_bool is_cst;
2659 	isl_poly_rec *rec;
2660 	int i;
2661 
2662 	is_cst = isl_poly_is_cst(poly);
2663 	if (is_cst < 0)
2664 		return isl_stat_error;
2665 	if (is_cst)
2666 		return isl_stat_ok;
2667 
2668 	if (poly->var < d)
2669 		active[poly->var] = 1;
2670 
2671 	rec = isl_poly_as_rec(poly);
2672 	for (i = 0; i < rec->n; ++i)
2673 		if (poly_set_active(rec->p[i], active, d) < 0)
2674 			return isl_stat_error;
2675 
2676 	return isl_stat_ok;
2677 }
2678 
set_active(__isl_keep isl_qpolynomial * qp,int * active)2679 static isl_stat set_active(__isl_keep isl_qpolynomial *qp, int *active)
2680 {
2681 	int i, j;
2682 	isl_size d;
2683 	isl_space *space;
2684 
2685 	space = isl_qpolynomial_peek_domain_space(qp);
2686 	d = isl_space_dim(space, isl_dim_all);
2687 	if (d < 0 || !active)
2688 		return isl_stat_error;
2689 
2690 	for (i = 0; i < d; ++i)
2691 		for (j = 0; j < qp->div->n_row; ++j) {
2692 			if (isl_int_is_zero(qp->div->row[j][2 + i]))
2693 				continue;
2694 			active[i] = 1;
2695 			break;
2696 		}
2697 
2698 	return poly_set_active(qp->poly, active, d);
2699 }
2700 
2701 #undef TYPE
2702 #define TYPE	isl_qpolynomial
2703 static
2704 #include "check_type_range_templ.c"
2705 
isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial * qp,enum isl_dim_type type,unsigned first,unsigned n)2706 isl_bool isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
2707 	enum isl_dim_type type, unsigned first, unsigned n)
2708 {
2709 	int i;
2710 	int *active = NULL;
2711 	isl_bool involves = isl_bool_false;
2712 	isl_size offset;
2713 	isl_size d;
2714 	isl_space *space;
2715 
2716 	if (!qp)
2717 		return isl_bool_error;
2718 	if (n == 0)
2719 		return isl_bool_false;
2720 
2721 	if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
2722 		return isl_bool_error;
2723 	isl_assert(qp->dim->ctx, type == isl_dim_param ||
2724 				 type == isl_dim_in, return isl_bool_error);
2725 
2726 	space = isl_qpolynomial_peek_domain_space(qp);
2727 	d = isl_space_dim(space, isl_dim_all);
2728 	if (d < 0)
2729 		return isl_bool_error;
2730 	active = isl_calloc_array(qp->dim->ctx, int, d);
2731 	if (set_active(qp, active) < 0)
2732 		goto error;
2733 
2734 	offset = isl_qpolynomial_domain_var_offset(qp, domain_type(type));
2735 	if (offset < 0)
2736 		goto error;
2737 	first += offset;
2738 	for (i = 0; i < n; ++i)
2739 		if (active[first + i]) {
2740 			involves = isl_bool_true;
2741 			break;
2742 		}
2743 
2744 	free(active);
2745 
2746 	return involves;
2747 error:
2748 	free(active);
2749 	return isl_bool_error;
2750 }
2751 
2752 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2753  * of the divs that do appear in the quasi-polynomial.
2754  */
remove_redundant_divs(__isl_take isl_qpolynomial * qp)2755 static __isl_give isl_qpolynomial *remove_redundant_divs(
2756 	__isl_take isl_qpolynomial *qp)
2757 {
2758 	int i, j;
2759 	isl_size div_pos;
2760 	int len;
2761 	int skip;
2762 	int *active = NULL;
2763 	int *reordering = NULL;
2764 	int redundant = 0;
2765 	int n_div;
2766 	isl_ctx *ctx;
2767 
2768 	if (!qp)
2769 		return NULL;
2770 	if (qp->div->n_row == 0)
2771 		return qp;
2772 
2773 	div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
2774 	if (div_pos < 0)
2775 		return isl_qpolynomial_free(qp);
2776 	len = qp->div->n_col - 2;
2777 	ctx = isl_qpolynomial_get_ctx(qp);
2778 	active = isl_calloc_array(ctx, int, len);
2779 	if (!active)
2780 		goto error;
2781 
2782 	if (poly_set_active(qp->poly, active, len) < 0)
2783 		goto error;
2784 
2785 	for (i = qp->div->n_row - 1; i >= 0; --i) {
2786 		if (!active[div_pos + i]) {
2787 			redundant = 1;
2788 			continue;
2789 		}
2790 		for (j = 0; j < i; ++j) {
2791 			if (isl_int_is_zero(qp->div->row[i][2 + div_pos + j]))
2792 				continue;
2793 			active[div_pos + j] = 1;
2794 			break;
2795 		}
2796 	}
2797 
2798 	if (!redundant) {
2799 		free(active);
2800 		return qp;
2801 	}
2802 
2803 	reordering = isl_alloc_array(qp->div->ctx, int, len);
2804 	if (!reordering)
2805 		goto error;
2806 
2807 	for (i = 0; i < div_pos; ++i)
2808 		reordering[i] = i;
2809 
2810 	skip = 0;
2811 	n_div = qp->div->n_row;
2812 	for (i = 0; i < n_div; ++i) {
2813 		if (!active[div_pos + i]) {
2814 			qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
2815 			qp->div = isl_mat_drop_cols(qp->div,
2816 						    2 + div_pos + i - skip, 1);
2817 			skip++;
2818 		}
2819 		reordering[div_pos + i] = div_pos + i - skip;
2820 	}
2821 
2822 	qp->poly = reorder(qp->poly, reordering);
2823 
2824 	if (!qp->poly || !qp->div)
2825 		goto error;
2826 
2827 	free(active);
2828 	free(reordering);
2829 
2830 	return qp;
2831 error:
2832 	free(active);
2833 	free(reordering);
2834 	isl_qpolynomial_free(qp);
2835 	return NULL;
2836 }
2837 
isl_poly_drop(__isl_take isl_poly * poly,unsigned first,unsigned n)2838 __isl_give isl_poly *isl_poly_drop(__isl_take isl_poly *poly,
2839 	unsigned first, unsigned n)
2840 {
2841 	int i;
2842 	isl_poly_rec *rec;
2843 
2844 	if (!poly)
2845 		return NULL;
2846 	if (n == 0 || poly->var < 0 || poly->var < first)
2847 		return poly;
2848 	if (poly->var < first + n) {
2849 		poly = replace_by_constant_term(poly);
2850 		return isl_poly_drop(poly, first, n);
2851 	}
2852 	poly = isl_poly_cow(poly);
2853 	if (!poly)
2854 		return NULL;
2855 	poly->var -= n;
2856 	rec = isl_poly_as_rec(poly);
2857 	if (!rec)
2858 		goto error;
2859 
2860 	for (i = 0; i < rec->n; ++i) {
2861 		rec->p[i] = isl_poly_drop(rec->p[i], first, n);
2862 		if (!rec->p[i])
2863 			goto error;
2864 	}
2865 
2866 	return poly;
2867 error:
2868 	isl_poly_free(poly);
2869 	return NULL;
2870 }
2871 
isl_qpolynomial_set_dim_name(__isl_take isl_qpolynomial * qp,enum isl_dim_type type,unsigned pos,const char * s)2872 __isl_give isl_qpolynomial *isl_qpolynomial_set_dim_name(
2873 	__isl_take isl_qpolynomial *qp,
2874 	enum isl_dim_type type, unsigned pos, const char *s)
2875 {
2876 	qp = isl_qpolynomial_cow(qp);
2877 	if (!qp)
2878 		return NULL;
2879 	if (type == isl_dim_out)
2880 		isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
2881 			"cannot set name of output/set dimension",
2882 			return isl_qpolynomial_free(qp));
2883 	type = domain_type(type);
2884 	qp->dim = isl_space_set_dim_name(qp->dim, type, pos, s);
2885 	if (!qp->dim)
2886 		goto error;
2887 	return qp;
2888 error:
2889 	isl_qpolynomial_free(qp);
2890 	return NULL;
2891 }
2892 
isl_qpolynomial_drop_dims(__isl_take isl_qpolynomial * qp,enum isl_dim_type type,unsigned first,unsigned n)2893 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
2894 	__isl_take isl_qpolynomial *qp,
2895 	enum isl_dim_type type, unsigned first, unsigned n)
2896 {
2897 	isl_size offset;
2898 
2899 	if (!qp)
2900 		return NULL;
2901 	if (type == isl_dim_out)
2902 		isl_die(qp->dim->ctx, isl_error_invalid,
2903 			"cannot drop output/set dimension",
2904 			goto error);
2905 	if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
2906 		return isl_qpolynomial_free(qp);
2907 	type = domain_type(type);
2908 	if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
2909 		return qp;
2910 
2911 	qp = isl_qpolynomial_cow(qp);
2912 	if (!qp)
2913 		return NULL;
2914 
2915 	isl_assert(qp->dim->ctx, type == isl_dim_param ||
2916 				 type == isl_dim_set, goto error);
2917 
2918 	qp->dim = isl_space_drop_dims(qp->dim, type, first, n);
2919 	if (!qp->dim)
2920 		goto error;
2921 
2922 	offset = isl_qpolynomial_domain_var_offset(qp, type);
2923 	if (offset < 0)
2924 		goto error;
2925 	first += offset;
2926 
2927 	qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
2928 	if (!qp->div)
2929 		goto error;
2930 
2931 	qp->poly = isl_poly_drop(qp->poly, first, n);
2932 	if (!qp->poly)
2933 		goto error;
2934 
2935 	return qp;
2936 error:
2937 	isl_qpolynomial_free(qp);
2938 	return NULL;
2939 }
2940 
2941 /* Project the domain of the quasi-polynomial onto its parameter space.
2942  * The quasi-polynomial may not involve any of the domain dimensions.
2943  */
isl_qpolynomial_project_domain_on_params(__isl_take isl_qpolynomial * qp)2944 __isl_give isl_qpolynomial *isl_qpolynomial_project_domain_on_params(
2945 	__isl_take isl_qpolynomial *qp)
2946 {
2947 	isl_space *space;
2948 	isl_size n;
2949 	isl_bool involves;
2950 
2951 	n = isl_qpolynomial_dim(qp, isl_dim_in);
2952 	if (n < 0)
2953 		return isl_qpolynomial_free(qp);
2954 	involves = isl_qpolynomial_involves_dims(qp, isl_dim_in, 0, n);
2955 	if (involves < 0)
2956 		return isl_qpolynomial_free(qp);
2957 	if (involves)
2958 		isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
2959 			"polynomial involves some of the domain dimensions",
2960 			return isl_qpolynomial_free(qp));
2961 	qp = isl_qpolynomial_drop_dims(qp, isl_dim_in, 0, n);
2962 	space = isl_qpolynomial_get_domain_space(qp);
2963 	space = isl_space_params(space);
2964 	qp = isl_qpolynomial_reset_domain_space(qp, space);
2965 	return qp;
2966 }
2967 
isl_qpolynomial_substitute_equalities_lifted(__isl_take isl_qpolynomial * qp,__isl_take isl_basic_set * eq)2968 static __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities_lifted(
2969 	__isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2970 {
2971 	int i, j, k;
2972 	isl_int denom;
2973 	unsigned total;
2974 	unsigned n_div;
2975 	isl_poly *poly;
2976 
2977 	if (!eq)
2978 		goto error;
2979 	if (eq->n_eq == 0) {
2980 		isl_basic_set_free(eq);
2981 		return qp;
2982 	}
2983 
2984 	qp = isl_qpolynomial_cow(qp);
2985 	if (!qp)
2986 		goto error;
2987 	qp->div = isl_mat_cow(qp->div);
2988 	if (!qp->div)
2989 		goto error;
2990 
2991 	total = isl_basic_set_offset(eq, isl_dim_div);
2992 	n_div = eq->n_div;
2993 	isl_int_init(denom);
2994 	for (i = 0; i < eq->n_eq; ++i) {
2995 		j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2996 		if (j < 0 || j == 0 || j >= total)
2997 			continue;
2998 
2999 		for (k = 0; k < qp->div->n_row; ++k) {
3000 			if (isl_int_is_zero(qp->div->row[k][1 + j]))
3001 				continue;
3002 			isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
3003 					&qp->div->row[k][0]);
3004 			normalize_div(qp, k);
3005 		}
3006 
3007 		if (isl_int_is_pos(eq->eq[i][j]))
3008 			isl_seq_neg(eq->eq[i], eq->eq[i], total);
3009 		isl_int_abs(denom, eq->eq[i][j]);
3010 		isl_int_set_si(eq->eq[i][j], 0);
3011 
3012 		poly = isl_poly_from_affine(qp->dim->ctx,
3013 						   eq->eq[i], denom, total);
3014 		qp->poly = isl_poly_subs(qp->poly, j - 1, 1, &poly);
3015 		isl_poly_free(poly);
3016 	}
3017 	isl_int_clear(denom);
3018 
3019 	if (!qp->poly)
3020 		goto error;
3021 
3022 	isl_basic_set_free(eq);
3023 
3024 	qp = substitute_non_divs(qp);
3025 	qp = sort_divs(qp);
3026 
3027 	return qp;
3028 error:
3029 	isl_basic_set_free(eq);
3030 	isl_qpolynomial_free(qp);
3031 	return NULL;
3032 }
3033 
3034 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
3035  */
isl_qpolynomial_substitute_equalities(__isl_take isl_qpolynomial * qp,__isl_take isl_basic_set * eq)3036 __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
3037 	__isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
3038 {
3039 	if (!qp || !eq)
3040 		goto error;
3041 	if (qp->div->n_row > 0)
3042 		eq = isl_basic_set_add_dims(eq, isl_dim_set, qp->div->n_row);
3043 	return isl_qpolynomial_substitute_equalities_lifted(qp, eq);
3044 error:
3045 	isl_basic_set_free(eq);
3046 	isl_qpolynomial_free(qp);
3047 	return NULL;
3048 }
3049 
3050 /* Look for equalities among the variables shared by context and qp
3051  * and the integer divisions of qp, if any.
3052  * The equalities are then used to eliminate variables and/or integer
3053  * divisions from qp.
3054  */
isl_qpolynomial_gist(__isl_take isl_qpolynomial * qp,__isl_take isl_set * context)3055 __isl_give isl_qpolynomial *isl_qpolynomial_gist(
3056 	__isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
3057 {
3058 	isl_local_space *ls;
3059 	isl_basic_set *aff;
3060 
3061 	ls = isl_qpolynomial_get_domain_local_space(qp);
3062 	context = isl_local_space_lift_set(ls, context);
3063 
3064 	aff = isl_set_affine_hull(context);
3065 	return isl_qpolynomial_substitute_equalities_lifted(qp, aff);
3066 }
3067 
isl_qpolynomial_gist_params(__isl_take isl_qpolynomial * qp,__isl_take isl_set * context)3068 __isl_give isl_qpolynomial *isl_qpolynomial_gist_params(
3069 	__isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
3070 {
3071 	isl_space *space = isl_qpolynomial_get_domain_space(qp);
3072 	isl_set *dom_context = isl_set_universe(space);
3073 	dom_context = isl_set_intersect_params(dom_context, context);
3074 	return isl_qpolynomial_gist(qp, dom_context);
3075 }
3076 
3077 /* Return a zero isl_qpolynomial in the given space.
3078  *
3079  * This is a helper function for isl_pw_*_as_* that ensures a uniform
3080  * interface over all piecewise types.
3081  */
isl_qpolynomial_zero_in_space(__isl_take isl_space * space)3082 static __isl_give isl_qpolynomial *isl_qpolynomial_zero_in_space(
3083 	__isl_take isl_space *space)
3084 {
3085 	return isl_qpolynomial_zero_on_domain(isl_space_domain(space));
3086 }
3087 
3088 #define isl_qpolynomial_involves_nan isl_qpolynomial_is_nan
3089 
3090 #undef PW
3091 #define PW isl_pw_qpolynomial
3092 #undef BASE
3093 #define BASE qpolynomial
3094 #undef EL_IS_ZERO
3095 #define EL_IS_ZERO is_zero
3096 #undef ZERO
3097 #define ZERO zero
3098 #undef IS_ZERO
3099 #define IS_ZERO is_zero
3100 #undef FIELD
3101 #define FIELD qp
3102 #undef DEFAULT_IS_ZERO
3103 #define DEFAULT_IS_ZERO 1
3104 
3105 #include <isl_pw_templ.c>
3106 #include <isl_pw_eval.c>
3107 #include <isl_pw_insert_dims_templ.c>
3108 #include <isl_pw_lift_templ.c>
3109 #include <isl_pw_morph_templ.c>
3110 #include <isl_pw_move_dims_templ.c>
3111 #include <isl_pw_neg_templ.c>
3112 #include <isl_pw_opt_templ.c>
3113 #include <isl_pw_sub_templ.c>
3114 
3115 #undef BASE
3116 #define BASE pw_qpolynomial
3117 
3118 #include <isl_union_single.c>
3119 #include <isl_union_eval.c>
3120 #include <isl_union_neg.c>
3121 
isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial * pwqp)3122 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
3123 {
3124 	if (!pwqp)
3125 		return -1;
3126 
3127 	if (pwqp->n != -1)
3128 		return 0;
3129 
3130 	if (!isl_set_plain_is_universe(pwqp->p[0].set))
3131 		return 0;
3132 
3133 	return isl_qpolynomial_is_one(pwqp->p[0].qp);
3134 }
3135 
isl_pw_qpolynomial_add(__isl_take isl_pw_qpolynomial * pwqp1,__isl_take isl_pw_qpolynomial * pwqp2)3136 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add(
3137 	__isl_take isl_pw_qpolynomial *pwqp1,
3138 	__isl_take isl_pw_qpolynomial *pwqp2)
3139 {
3140 	return isl_pw_qpolynomial_union_add_(pwqp1, pwqp2);
3141 }
3142 
isl_pw_qpolynomial_mul(__isl_take isl_pw_qpolynomial * pwqp1,__isl_take isl_pw_qpolynomial * pwqp2)3143 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
3144 	__isl_take isl_pw_qpolynomial *pwqp1,
3145 	__isl_take isl_pw_qpolynomial *pwqp2)
3146 {
3147 	int i, j, n;
3148 	struct isl_pw_qpolynomial *res;
3149 
3150 	if (!pwqp1 || !pwqp2)
3151 		goto error;
3152 
3153 	isl_assert(pwqp1->dim->ctx, isl_space_is_equal(pwqp1->dim, pwqp2->dim),
3154 			goto error);
3155 
3156 	if (isl_pw_qpolynomial_is_zero(pwqp1)) {
3157 		isl_pw_qpolynomial_free(pwqp2);
3158 		return pwqp1;
3159 	}
3160 
3161 	if (isl_pw_qpolynomial_is_zero(pwqp2)) {
3162 		isl_pw_qpolynomial_free(pwqp1);
3163 		return pwqp2;
3164 	}
3165 
3166 	if (isl_pw_qpolynomial_is_one(pwqp1)) {
3167 		isl_pw_qpolynomial_free(pwqp1);
3168 		return pwqp2;
3169 	}
3170 
3171 	if (isl_pw_qpolynomial_is_one(pwqp2)) {
3172 		isl_pw_qpolynomial_free(pwqp2);
3173 		return pwqp1;
3174 	}
3175 
3176 	n = pwqp1->n * pwqp2->n;
3177 	res = isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1->dim), n);
3178 
3179 	for (i = 0; i < pwqp1->n; ++i) {
3180 		for (j = 0; j < pwqp2->n; ++j) {
3181 			struct isl_set *common;
3182 			struct isl_qpolynomial *prod;
3183 			common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
3184 						isl_set_copy(pwqp2->p[j].set));
3185 			if (isl_set_plain_is_empty(common)) {
3186 				isl_set_free(common);
3187 				continue;
3188 			}
3189 
3190 			prod = isl_qpolynomial_mul(
3191 				isl_qpolynomial_copy(pwqp1->p[i].qp),
3192 				isl_qpolynomial_copy(pwqp2->p[j].qp));
3193 
3194 			res = isl_pw_qpolynomial_add_piece(res, common, prod);
3195 		}
3196 	}
3197 
3198 	isl_pw_qpolynomial_free(pwqp1);
3199 	isl_pw_qpolynomial_free(pwqp2);
3200 
3201 	return res;
3202 error:
3203 	isl_pw_qpolynomial_free(pwqp1);
3204 	isl_pw_qpolynomial_free(pwqp2);
3205 	return NULL;
3206 }
3207 
isl_poly_eval(__isl_take isl_poly * poly,__isl_take isl_vec * vec)3208 __isl_give isl_val *isl_poly_eval(__isl_take isl_poly *poly,
3209 	__isl_take isl_vec *vec)
3210 {
3211 	int i;
3212 	isl_bool is_cst;
3213 	isl_poly_rec *rec;
3214 	isl_val *res;
3215 	isl_val *base;
3216 
3217 	is_cst = isl_poly_is_cst(poly);
3218 	if (is_cst < 0)
3219 		goto error;
3220 	if (is_cst) {
3221 		isl_vec_free(vec);
3222 		res = isl_poly_get_constant_val(poly);
3223 		isl_poly_free(poly);
3224 		return res;
3225 	}
3226 
3227 	rec = isl_poly_as_rec(poly);
3228 	if (!rec || !vec)
3229 		goto error;
3230 
3231 	isl_assert(poly->ctx, rec->n >= 1, goto error);
3232 
3233 	base = isl_val_rat_from_isl_int(poly->ctx,
3234 					vec->el[1 + poly->var], vec->el[0]);
3235 
3236 	res = isl_poly_eval(isl_poly_copy(rec->p[rec->n - 1]),
3237 				isl_vec_copy(vec));
3238 
3239 	for (i = rec->n - 2; i >= 0; --i) {
3240 		res = isl_val_mul(res, isl_val_copy(base));
3241 		res = isl_val_add(res, isl_poly_eval(isl_poly_copy(rec->p[i]),
3242 							    isl_vec_copy(vec)));
3243 	}
3244 
3245 	isl_val_free(base);
3246 	isl_poly_free(poly);
3247 	isl_vec_free(vec);
3248 	return res;
3249 error:
3250 	isl_poly_free(poly);
3251 	isl_vec_free(vec);
3252 	return NULL;
3253 }
3254 
3255 /* Evaluate "qp" in the void point "pnt".
3256  * In particular, return the value NaN.
3257  */
eval_void(__isl_take isl_qpolynomial * qp,__isl_take isl_point * pnt)3258 static __isl_give isl_val *eval_void(__isl_take isl_qpolynomial *qp,
3259 	__isl_take isl_point *pnt)
3260 {
3261 	isl_ctx *ctx;
3262 
3263 	ctx = isl_point_get_ctx(pnt);
3264 	isl_qpolynomial_free(qp);
3265 	isl_point_free(pnt);
3266 	return isl_val_nan(ctx);
3267 }
3268 
isl_qpolynomial_eval(__isl_take isl_qpolynomial * qp,__isl_take isl_point * pnt)3269 __isl_give isl_val *isl_qpolynomial_eval(__isl_take isl_qpolynomial *qp,
3270 	__isl_take isl_point *pnt)
3271 {
3272 	isl_bool is_void;
3273 	isl_vec *ext;
3274 	isl_val *v;
3275 
3276 	if (!qp || !pnt)
3277 		goto error;
3278 	isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, qp->dim), goto error);
3279 	is_void = isl_point_is_void(pnt);
3280 	if (is_void < 0)
3281 		goto error;
3282 	if (is_void)
3283 		return eval_void(qp, pnt);
3284 
3285 	ext = isl_local_extend_point_vec(qp->div, isl_vec_copy(pnt->vec));
3286 
3287 	v = isl_poly_eval(isl_poly_copy(qp->poly), ext);
3288 
3289 	isl_qpolynomial_free(qp);
3290 	isl_point_free(pnt);
3291 
3292 	return v;
3293 error:
3294 	isl_qpolynomial_free(qp);
3295 	isl_point_free(pnt);
3296 	return NULL;
3297 }
3298 
isl_poly_cmp(__isl_keep isl_poly_cst * cst1,__isl_keep isl_poly_cst * cst2)3299 int isl_poly_cmp(__isl_keep isl_poly_cst *cst1, __isl_keep isl_poly_cst *cst2)
3300 {
3301 	int cmp;
3302 	isl_int t;
3303 	isl_int_init(t);
3304 	isl_int_mul(t, cst1->n, cst2->d);
3305 	isl_int_submul(t, cst2->n, cst1->d);
3306 	cmp = isl_int_sgn(t);
3307 	isl_int_clear(t);
3308 	return cmp;
3309 }
3310 
isl_qpolynomial_insert_dims(__isl_take isl_qpolynomial * qp,enum isl_dim_type type,unsigned first,unsigned n)3311 __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
3312 	__isl_take isl_qpolynomial *qp, enum isl_dim_type type,
3313 	unsigned first, unsigned n)
3314 {
3315 	unsigned total;
3316 	unsigned g_pos;
3317 	int *exp;
3318 
3319 	if (!qp)
3320 		return NULL;
3321 	if (type == isl_dim_out)
3322 		isl_die(qp->div->ctx, isl_error_invalid,
3323 			"cannot insert output/set dimensions",
3324 			goto error);
3325 	if (isl_qpolynomial_check_range(qp, type, first, 0) < 0)
3326 		return isl_qpolynomial_free(qp);
3327 	type = domain_type(type);
3328 	if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
3329 		return qp;
3330 
3331 	qp = isl_qpolynomial_cow(qp);
3332 	if (!qp)
3333 		return NULL;
3334 
3335 	g_pos = pos(qp->dim, type) + first;
3336 
3337 	qp->div = isl_mat_insert_zero_cols(qp->div, 2 + g_pos, n);
3338 	if (!qp->div)
3339 		goto error;
3340 
3341 	total = qp->div->n_col - 2;
3342 	if (total > g_pos) {
3343 		int i;
3344 		exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
3345 		if (!exp)
3346 			goto error;
3347 		for (i = 0; i < total - g_pos; ++i)
3348 			exp[i] = i + n;
3349 		qp->poly = expand(qp->poly, exp, g_pos);
3350 		free(exp);
3351 		if (!qp->poly)
3352 			goto error;
3353 	}
3354 
3355 	qp->dim = isl_space_insert_dims(qp->dim, type, first, n);
3356 	if (!qp->dim)
3357 		goto error;
3358 
3359 	return qp;
3360 error:
3361 	isl_qpolynomial_free(qp);
3362 	return NULL;
3363 }
3364 
isl_qpolynomial_add_dims(__isl_take isl_qpolynomial * qp,enum isl_dim_type type,unsigned n)3365 __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
3366 	__isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
3367 {
3368 	isl_size pos;
3369 
3370 	pos = isl_qpolynomial_dim(qp, type);
3371 	if (pos < 0)
3372 		return isl_qpolynomial_free(qp);
3373 
3374 	return isl_qpolynomial_insert_dims(qp, type, pos, n);
3375 }
3376 
reordering_move(isl_ctx * ctx,unsigned len,unsigned dst,unsigned src,unsigned n)3377 static int *reordering_move(isl_ctx *ctx,
3378 	unsigned len, unsigned dst, unsigned src, unsigned n)
3379 {
3380 	int i;
3381 	int *reordering;
3382 
3383 	reordering = isl_alloc_array(ctx, int, len);
3384 	if (!reordering)
3385 		return NULL;
3386 
3387 	if (dst <= src) {
3388 		for (i = 0; i < dst; ++i)
3389 			reordering[i] = i;
3390 		for (i = 0; i < n; ++i)
3391 			reordering[src + i] = dst + i;
3392 		for (i = 0; i < src - dst; ++i)
3393 			reordering[dst + i] = dst + n + i;
3394 		for (i = 0; i < len - src - n; ++i)
3395 			reordering[src + n + i] = src + n + i;
3396 	} else {
3397 		for (i = 0; i < src; ++i)
3398 			reordering[i] = i;
3399 		for (i = 0; i < n; ++i)
3400 			reordering[src + i] = dst + i;
3401 		for (i = 0; i < dst - src; ++i)
3402 			reordering[src + n + i] = src + i;
3403 		for (i = 0; i < len - dst - n; ++i)
3404 			reordering[dst + n + i] = dst + n + i;
3405 	}
3406 
3407 	return reordering;
3408 }
3409 
isl_qpolynomial_move_dims(__isl_take isl_qpolynomial * qp,enum isl_dim_type dst_type,unsigned dst_pos,enum isl_dim_type src_type,unsigned src_pos,unsigned n)3410 __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
3411 	__isl_take isl_qpolynomial *qp,
3412 	enum isl_dim_type dst_type, unsigned dst_pos,
3413 	enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3414 {
3415 	unsigned g_dst_pos;
3416 	unsigned g_src_pos;
3417 	int *reordering;
3418 
3419 	if (!qp)
3420 		return NULL;
3421 
3422 	if (dst_type == isl_dim_out || src_type == isl_dim_out)
3423 		isl_die(qp->dim->ctx, isl_error_invalid,
3424 			"cannot move output/set dimension",
3425 			goto error);
3426 	if (isl_qpolynomial_check_range(qp, src_type, src_pos, n) < 0)
3427 		return isl_qpolynomial_free(qp);
3428 	if (dst_type == isl_dim_in)
3429 		dst_type = isl_dim_set;
3430 	if (src_type == isl_dim_in)
3431 		src_type = isl_dim_set;
3432 
3433 	if (n == 0 &&
3434 	    !isl_space_is_named_or_nested(qp->dim, src_type) &&
3435 	    !isl_space_is_named_or_nested(qp->dim, dst_type))
3436 		return qp;
3437 
3438 	qp = isl_qpolynomial_cow(qp);
3439 	if (!qp)
3440 		return NULL;
3441 
3442 	g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
3443 	g_src_pos = pos(qp->dim, src_type) + src_pos;
3444 	if (dst_type > src_type)
3445 		g_dst_pos -= n;
3446 
3447 	qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
3448 	if (!qp->div)
3449 		goto error;
3450 	qp = sort_divs(qp);
3451 	if (!qp)
3452 		goto error;
3453 
3454 	reordering = reordering_move(qp->dim->ctx,
3455 				qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
3456 	if (!reordering)
3457 		goto error;
3458 
3459 	qp->poly = reorder(qp->poly, reordering);
3460 	free(reordering);
3461 	if (!qp->poly)
3462 		goto error;
3463 
3464 	qp->dim = isl_space_move_dims(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
3465 	if (!qp->dim)
3466 		goto error;
3467 
3468 	return qp;
3469 error:
3470 	isl_qpolynomial_free(qp);
3471 	return NULL;
3472 }
3473 
isl_qpolynomial_from_affine(__isl_take isl_space * space,isl_int * f,isl_int denom)3474 __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(
3475 	__isl_take isl_space *space, isl_int *f, isl_int denom)
3476 {
3477 	isl_size d;
3478 	isl_poly *poly;
3479 
3480 	space = isl_space_domain(space);
3481 	if (!space)
3482 		return NULL;
3483 
3484 	d = isl_space_dim(space, isl_dim_all);
3485 	poly = d < 0 ? NULL : isl_poly_from_affine(space->ctx, f, denom, 1 + d);
3486 
3487 	return isl_qpolynomial_alloc(space, 0, poly);
3488 }
3489 
isl_qpolynomial_from_aff(__isl_take isl_aff * aff)3490 __isl_give isl_qpolynomial *isl_qpolynomial_from_aff(__isl_take isl_aff *aff)
3491 {
3492 	isl_ctx *ctx;
3493 	isl_poly *poly;
3494 	isl_qpolynomial *qp;
3495 
3496 	if (!aff)
3497 		return NULL;
3498 
3499 	ctx = isl_aff_get_ctx(aff);
3500 	poly = isl_poly_from_affine(ctx, aff->v->el + 1, aff->v->el[0],
3501 				    aff->v->size - 1);
3502 
3503 	qp = isl_qpolynomial_alloc(isl_aff_get_domain_space(aff),
3504 				    aff->ls->div->n_row, poly);
3505 	if (!qp)
3506 		goto error;
3507 
3508 	isl_mat_free(qp->div);
3509 	qp->div = isl_mat_copy(aff->ls->div);
3510 	qp->div = isl_mat_cow(qp->div);
3511 	if (!qp->div)
3512 		goto error;
3513 
3514 	isl_aff_free(aff);
3515 	qp = reduce_divs(qp);
3516 	qp = remove_redundant_divs(qp);
3517 	return qp;
3518 error:
3519 	isl_aff_free(aff);
3520 	return isl_qpolynomial_free(qp);
3521 }
3522 
isl_pw_qpolynomial_from_pw_aff(__isl_take isl_pw_aff * pwaff)3523 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_pw_aff(
3524 	__isl_take isl_pw_aff *pwaff)
3525 {
3526 	int i;
3527 	isl_pw_qpolynomial *pwqp;
3528 
3529 	if (!pwaff)
3530 		return NULL;
3531 
3532 	pwqp = isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff),
3533 						pwaff->n);
3534 
3535 	for (i = 0; i < pwaff->n; ++i) {
3536 		isl_set *dom;
3537 		isl_qpolynomial *qp;
3538 
3539 		dom = isl_set_copy(pwaff->p[i].set);
3540 		qp = isl_qpolynomial_from_aff(isl_aff_copy(pwaff->p[i].aff));
3541 		pwqp = isl_pw_qpolynomial_add_piece(pwqp,  dom, qp);
3542 	}
3543 
3544 	isl_pw_aff_free(pwaff);
3545 	return pwqp;
3546 }
3547 
isl_qpolynomial_from_constraint(__isl_take isl_constraint * c,enum isl_dim_type type,unsigned pos)3548 __isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
3549 	__isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
3550 {
3551 	isl_aff *aff;
3552 
3553 	aff = isl_constraint_get_bound(c, type, pos);
3554 	isl_constraint_free(c);
3555 	return isl_qpolynomial_from_aff(aff);
3556 }
3557 
3558 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3559  * in "qp" by subs[i].
3560  */
isl_qpolynomial_substitute(__isl_take isl_qpolynomial * qp,enum isl_dim_type type,unsigned first,unsigned n,__isl_keep isl_qpolynomial ** subs)3561 __isl_give isl_qpolynomial *isl_qpolynomial_substitute(
3562 	__isl_take isl_qpolynomial *qp,
3563 	enum isl_dim_type type, unsigned first, unsigned n,
3564 	__isl_keep isl_qpolynomial **subs)
3565 {
3566 	int i;
3567 	isl_poly **polys;
3568 
3569 	if (n == 0)
3570 		return qp;
3571 
3572 	qp = isl_qpolynomial_cow(qp);
3573 	if (!qp)
3574 		return NULL;
3575 
3576 	if (type == isl_dim_out)
3577 		isl_die(qp->dim->ctx, isl_error_invalid,
3578 			"cannot substitute output/set dimension",
3579 			goto error);
3580 	if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
3581 		return isl_qpolynomial_free(qp);
3582 	type = domain_type(type);
3583 
3584 	for (i = 0; i < n; ++i)
3585 		if (!subs[i])
3586 			goto error;
3587 
3588 	for (i = 0; i < n; ++i)
3589 		if (isl_qpolynomial_check_equal_space(qp, subs[i]) < 0)
3590 			goto error;
3591 
3592 	isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
3593 	for (i = 0; i < n; ++i)
3594 		isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
3595 
3596 	first += pos(qp->dim, type);
3597 
3598 	polys = isl_alloc_array(qp->dim->ctx, struct isl_poly *, n);
3599 	if (!polys)
3600 		goto error;
3601 	for (i = 0; i < n; ++i)
3602 		polys[i] = subs[i]->poly;
3603 
3604 	qp->poly = isl_poly_subs(qp->poly, first, n, polys);
3605 
3606 	free(polys);
3607 
3608 	if (!qp->poly)
3609 		goto error;
3610 
3611 	return qp;
3612 error:
3613 	isl_qpolynomial_free(qp);
3614 	return NULL;
3615 }
3616 
3617 /* Extend "bset" with extra set dimensions for each integer division
3618  * in "qp" and then call "fn" with the extended bset and the polynomial
3619  * that results from replacing each of the integer divisions by the
3620  * corresponding extra set dimension.
3621  */
isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial * qp,__isl_keep isl_basic_set * bset,isl_stat (* fn)(__isl_take isl_basic_set * bset,__isl_take isl_qpolynomial * poly,void * user),void * user)3622 isl_stat isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
3623 	__isl_keep isl_basic_set *bset,
3624 	isl_stat (*fn)(__isl_take isl_basic_set *bset,
3625 		  __isl_take isl_qpolynomial *poly, void *user), void *user)
3626 {
3627 	isl_space *space;
3628 	isl_local_space *ls;
3629 	isl_qpolynomial *poly;
3630 
3631 	if (!qp || !bset)
3632 		return isl_stat_error;
3633 	if (qp->div->n_row == 0)
3634 		return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
3635 			  user);
3636 
3637 	space = isl_space_copy(qp->dim);
3638 	space = isl_space_add_dims(space, isl_dim_set, qp->div->n_row);
3639 	poly = isl_qpolynomial_alloc(space, 0, isl_poly_copy(qp->poly));
3640 	bset = isl_basic_set_copy(bset);
3641 	ls = isl_qpolynomial_get_domain_local_space(qp);
3642 	bset = isl_local_space_lift_basic_set(ls, bset);
3643 
3644 	return fn(bset, poly, user);
3645 }
3646 
3647 /* Return total degree in variables first (inclusive) up to last (exclusive).
3648  */
isl_poly_degree(__isl_keep isl_poly * poly,int first,int last)3649 int isl_poly_degree(__isl_keep isl_poly *poly, int first, int last)
3650 {
3651 	int deg = -1;
3652 	int i;
3653 	isl_bool is_zero, is_cst;
3654 	isl_poly_rec *rec;
3655 
3656 	is_zero = isl_poly_is_zero(poly);
3657 	if (is_zero < 0)
3658 		return -2;
3659 	if (is_zero)
3660 		return -1;
3661 	is_cst = isl_poly_is_cst(poly);
3662 	if (is_cst < 0)
3663 		return -2;
3664 	if (is_cst || poly->var < first)
3665 		return 0;
3666 
3667 	rec = isl_poly_as_rec(poly);
3668 	if (!rec)
3669 		return -2;
3670 
3671 	for (i = 0; i < rec->n; ++i) {
3672 		int d;
3673 
3674 		is_zero = isl_poly_is_zero(rec->p[i]);
3675 		if (is_zero < 0)
3676 			return -2;
3677 		if (is_zero)
3678 			continue;
3679 		d = isl_poly_degree(rec->p[i], first, last);
3680 		if (poly->var < last)
3681 			d += i;
3682 		if (d > deg)
3683 			deg = d;
3684 	}
3685 
3686 	return deg;
3687 }
3688 
3689 /* Return total degree in set variables.
3690  */
isl_qpolynomial_degree(__isl_keep isl_qpolynomial * poly)3691 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
3692 {
3693 	unsigned ovar;
3694 	isl_size nvar;
3695 
3696 	if (!poly)
3697 		return -2;
3698 
3699 	ovar = isl_space_offset(poly->dim, isl_dim_set);
3700 	nvar = isl_space_dim(poly->dim, isl_dim_set);
3701 	if (nvar < 0)
3702 		return -2;
3703 	return isl_poly_degree(poly->poly, ovar, ovar + nvar);
3704 }
3705 
isl_poly_coeff(__isl_keep isl_poly * poly,unsigned pos,int deg)3706 __isl_give isl_poly *isl_poly_coeff(__isl_keep isl_poly *poly,
3707 	unsigned pos, int deg)
3708 {
3709 	int i;
3710 	isl_bool is_cst;
3711 	isl_poly_rec *rec;
3712 
3713 	is_cst = isl_poly_is_cst(poly);
3714 	if (is_cst < 0)
3715 		return NULL;
3716 	if (is_cst || poly->var < pos) {
3717 		if (deg == 0)
3718 			return isl_poly_copy(poly);
3719 		else
3720 			return isl_poly_zero(poly->ctx);
3721 	}
3722 
3723 	rec = isl_poly_as_rec(poly);
3724 	if (!rec)
3725 		return NULL;
3726 
3727 	if (poly->var == pos) {
3728 		if (deg < rec->n)
3729 			return isl_poly_copy(rec->p[deg]);
3730 		else
3731 			return isl_poly_zero(poly->ctx);
3732 	}
3733 
3734 	poly = isl_poly_copy(poly);
3735 	poly = isl_poly_cow(poly);
3736 	rec = isl_poly_as_rec(poly);
3737 	if (!rec)
3738 		goto error;
3739 
3740 	for (i = 0; i < rec->n; ++i) {
3741 		isl_poly *t;
3742 		t = isl_poly_coeff(rec->p[i], pos, deg);
3743 		if (!t)
3744 			goto error;
3745 		isl_poly_free(rec->p[i]);
3746 		rec->p[i] = t;
3747 	}
3748 
3749 	return poly;
3750 error:
3751 	isl_poly_free(poly);
3752 	return NULL;
3753 }
3754 
3755 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3756  */
isl_qpolynomial_coeff(__isl_keep isl_qpolynomial * qp,enum isl_dim_type type,unsigned t_pos,int deg)3757 __isl_give isl_qpolynomial *isl_qpolynomial_coeff(
3758 	__isl_keep isl_qpolynomial *qp,
3759 	enum isl_dim_type type, unsigned t_pos, int deg)
3760 {
3761 	unsigned g_pos;
3762 	isl_poly *poly;
3763 	isl_qpolynomial *c;
3764 
3765 	if (!qp)
3766 		return NULL;
3767 
3768 	if (type == isl_dim_out)
3769 		isl_die(qp->div->ctx, isl_error_invalid,
3770 			"output/set dimension does not have a coefficient",
3771 			return NULL);
3772 	if (isl_qpolynomial_check_range(qp, type, t_pos, 1) < 0)
3773 		return NULL;
3774 	type = domain_type(type);
3775 
3776 	g_pos = pos(qp->dim, type) + t_pos;
3777 	poly = isl_poly_coeff(qp->poly, g_pos, deg);
3778 
3779 	c = isl_qpolynomial_alloc(isl_space_copy(qp->dim),
3780 				qp->div->n_row, poly);
3781 	if (!c)
3782 		return NULL;
3783 	isl_mat_free(c->div);
3784 	c->div = isl_mat_copy(qp->div);
3785 	if (!c->div)
3786 		goto error;
3787 	return c;
3788 error:
3789 	isl_qpolynomial_free(c);
3790 	return NULL;
3791 }
3792 
3793 /* Homogenize the polynomial in the variables first (inclusive) up to
3794  * last (exclusive) by inserting powers of variable first.
3795  * Variable first is assumed not to appear in the input.
3796  */
isl_poly_homogenize(__isl_take isl_poly * poly,int deg,int target,int first,int last)3797 __isl_give isl_poly *isl_poly_homogenize(__isl_take isl_poly *poly, int deg,
3798 	int target, int first, int last)
3799 {
3800 	int i;
3801 	isl_bool is_zero, is_cst;
3802 	isl_poly_rec *rec;
3803 
3804 	is_zero = isl_poly_is_zero(poly);
3805 	if (is_zero < 0)
3806 		return isl_poly_free(poly);
3807 	if (is_zero)
3808 		return poly;
3809 	if (deg == target)
3810 		return poly;
3811 	is_cst = isl_poly_is_cst(poly);
3812 	if (is_cst < 0)
3813 		return isl_poly_free(poly);
3814 	if (is_cst || poly->var < first) {
3815 		isl_poly *hom;
3816 
3817 		hom = isl_poly_var_pow(poly->ctx, first, target - deg);
3818 		if (!hom)
3819 			goto error;
3820 		rec = isl_poly_as_rec(hom);
3821 		rec->p[target - deg] = isl_poly_mul(rec->p[target - deg], poly);
3822 
3823 		return hom;
3824 	}
3825 
3826 	poly = isl_poly_cow(poly);
3827 	rec = isl_poly_as_rec(poly);
3828 	if (!rec)
3829 		goto error;
3830 
3831 	for (i = 0; i < rec->n; ++i) {
3832 		is_zero = isl_poly_is_zero(rec->p[i]);
3833 		if (is_zero < 0)
3834 			return isl_poly_free(poly);
3835 		if (is_zero)
3836 			continue;
3837 		rec->p[i] = isl_poly_homogenize(rec->p[i],
3838 				poly->var < last ? deg + i : i, target,
3839 				first, last);
3840 		if (!rec->p[i])
3841 			goto error;
3842 	}
3843 
3844 	return poly;
3845 error:
3846 	isl_poly_free(poly);
3847 	return NULL;
3848 }
3849 
3850 /* Homogenize the polynomial in the set variables by introducing
3851  * powers of an extra set variable at position 0.
3852  */
isl_qpolynomial_homogenize(__isl_take isl_qpolynomial * poly)3853 __isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
3854 	__isl_take isl_qpolynomial *poly)
3855 {
3856 	unsigned ovar;
3857 	isl_size nvar;
3858 	int deg = isl_qpolynomial_degree(poly);
3859 
3860 	if (deg < -1)
3861 		goto error;
3862 
3863 	poly = isl_qpolynomial_insert_dims(poly, isl_dim_in, 0, 1);
3864 	poly = isl_qpolynomial_cow(poly);
3865 	if (!poly)
3866 		goto error;
3867 
3868 	ovar = isl_space_offset(poly->dim, isl_dim_set);
3869 	nvar = isl_space_dim(poly->dim, isl_dim_set);
3870 	if (nvar < 0)
3871 		return isl_qpolynomial_free(poly);
3872 	poly->poly = isl_poly_homogenize(poly->poly, 0, deg, ovar, ovar + nvar);
3873 	if (!poly->poly)
3874 		goto error;
3875 
3876 	return poly;
3877 error:
3878 	isl_qpolynomial_free(poly);
3879 	return NULL;
3880 }
3881 
isl_term_alloc(__isl_take isl_space * space,__isl_take isl_mat * div)3882 __isl_give isl_term *isl_term_alloc(__isl_take isl_space *space,
3883 	__isl_take isl_mat *div)
3884 {
3885 	isl_term *term;
3886 	isl_size d;
3887 	int n;
3888 
3889 	d = isl_space_dim(space, isl_dim_all);
3890 	if (d < 0 || !div)
3891 		goto error;
3892 
3893 	n = d + div->n_row;
3894 
3895 	term = isl_calloc(space->ctx, struct isl_term,
3896 			sizeof(struct isl_term) + (n - 1) * sizeof(int));
3897 	if (!term)
3898 		goto error;
3899 
3900 	term->ref = 1;
3901 	term->dim = space;
3902 	term->div = div;
3903 	isl_int_init(term->n);
3904 	isl_int_init(term->d);
3905 
3906 	return term;
3907 error:
3908 	isl_space_free(space);
3909 	isl_mat_free(div);
3910 	return NULL;
3911 }
3912 
isl_term_copy(__isl_keep isl_term * term)3913 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
3914 {
3915 	if (!term)
3916 		return NULL;
3917 
3918 	term->ref++;
3919 	return term;
3920 }
3921 
isl_term_dup(__isl_keep isl_term * term)3922 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
3923 {
3924 	int i;
3925 	isl_term *dup;
3926 	isl_size total;
3927 
3928 	total = isl_term_dim(term, isl_dim_all);
3929 	if (total < 0)
3930 		return NULL;
3931 
3932 	dup = isl_term_alloc(isl_space_copy(term->dim), isl_mat_copy(term->div));
3933 	if (!dup)
3934 		return NULL;
3935 
3936 	isl_int_set(dup->n, term->n);
3937 	isl_int_set(dup->d, term->d);
3938 
3939 	for (i = 0; i < total; ++i)
3940 		dup->pow[i] = term->pow[i];
3941 
3942 	return dup;
3943 }
3944 
isl_term_cow(__isl_take isl_term * term)3945 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
3946 {
3947 	if (!term)
3948 		return NULL;
3949 
3950 	if (term->ref == 1)
3951 		return term;
3952 	term->ref--;
3953 	return isl_term_dup(term);
3954 }
3955 
isl_term_free(__isl_take isl_term * term)3956 __isl_null isl_term *isl_term_free(__isl_take isl_term *term)
3957 {
3958 	if (!term)
3959 		return NULL;
3960 
3961 	if (--term->ref > 0)
3962 		return NULL;
3963 
3964 	isl_space_free(term->dim);
3965 	isl_mat_free(term->div);
3966 	isl_int_clear(term->n);
3967 	isl_int_clear(term->d);
3968 	free(term);
3969 
3970 	return NULL;
3971 }
3972 
isl_term_dim(__isl_keep isl_term * term,enum isl_dim_type type)3973 isl_size isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
3974 {
3975 	isl_size dim;
3976 
3977 	if (!term)
3978 		return isl_size_error;
3979 
3980 	switch (type) {
3981 	case isl_dim_param:
3982 	case isl_dim_in:
3983 	case isl_dim_out:	return isl_space_dim(term->dim, type);
3984 	case isl_dim_div:	return term->div->n_row;
3985 	case isl_dim_all:	dim = isl_space_dim(term->dim, isl_dim_all);
3986 				if (dim < 0)
3987 					return isl_size_error;
3988 				return dim + term->div->n_row;
3989 	default:		return isl_size_error;
3990 	}
3991 }
3992 
3993 /* Return the space of "term".
3994  */
isl_term_peek_space(__isl_keep isl_term * term)3995 static __isl_keep isl_space *isl_term_peek_space(__isl_keep isl_term *term)
3996 {
3997 	return term ? term->dim : NULL;
3998 }
3999 
4000 /* Return the offset of the first variable of type "type" within
4001  * the variables of "term".
4002  */
isl_term_offset(__isl_keep isl_term * term,enum isl_dim_type type)4003 static isl_size isl_term_offset(__isl_keep isl_term *term,
4004 	enum isl_dim_type type)
4005 {
4006 	isl_space *space;
4007 
4008 	space = isl_term_peek_space(term);
4009 	if (!space)
4010 		return isl_size_error;
4011 
4012 	switch (type) {
4013 	case isl_dim_param:
4014 	case isl_dim_set:	return isl_space_offset(space, type);
4015 	case isl_dim_div:	return isl_space_dim(space, isl_dim_all);
4016 	default:
4017 		isl_die(isl_term_get_ctx(term), isl_error_invalid,
4018 			"invalid dimension type", return isl_size_error);
4019 	}
4020 }
4021 
isl_term_get_ctx(__isl_keep isl_term * term)4022 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
4023 {
4024 	return term ? term->dim->ctx : NULL;
4025 }
4026 
isl_term_get_num(__isl_keep isl_term * term,isl_int * n)4027 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
4028 {
4029 	if (!term)
4030 		return;
4031 	isl_int_set(*n, term->n);
4032 }
4033 
4034 /* Return the coefficient of the term "term".
4035  */
isl_term_get_coefficient_val(__isl_keep isl_term * term)4036 __isl_give isl_val *isl_term_get_coefficient_val(__isl_keep isl_term *term)
4037 {
4038 	if (!term)
4039 		return NULL;
4040 
4041 	return isl_val_rat_from_isl_int(isl_term_get_ctx(term),
4042 					term->n, term->d);
4043 }
4044 
4045 #undef TYPE
4046 #define TYPE	isl_term
4047 static
4048 #include "check_type_range_templ.c"
4049 
isl_term_get_exp(__isl_keep isl_term * term,enum isl_dim_type type,unsigned pos)4050 isl_size isl_term_get_exp(__isl_keep isl_term *term,
4051 	enum isl_dim_type type, unsigned pos)
4052 {
4053 	isl_size offset;
4054 
4055 	if (isl_term_check_range(term, type, pos, 1) < 0)
4056 		return isl_size_error;
4057 	offset = isl_term_offset(term, type);
4058 	if (offset < 0)
4059 		return isl_size_error;
4060 
4061 	return term->pow[offset + pos];
4062 }
4063 
isl_term_get_div(__isl_keep isl_term * term,unsigned pos)4064 __isl_give isl_aff *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
4065 {
4066 	isl_local_space *ls;
4067 	isl_aff *aff;
4068 
4069 	if (isl_term_check_range(term, isl_dim_div, pos, 1) < 0)
4070 		return NULL;
4071 
4072 	ls = isl_local_space_alloc_div(isl_space_copy(term->dim),
4073 					isl_mat_copy(term->div));
4074 	aff = isl_aff_alloc(ls);
4075 	if (!aff)
4076 		return NULL;
4077 
4078 	isl_seq_cpy(aff->v->el, term->div->row[pos], aff->v->size);
4079 
4080 	aff = isl_aff_normalize(aff);
4081 
4082 	return aff;
4083 }
4084 
isl_poly_foreach_term(__isl_keep isl_poly * poly,isl_stat (* fn)(__isl_take isl_term * term,void * user),__isl_take isl_term * term,void * user)4085 __isl_give isl_term *isl_poly_foreach_term(__isl_keep isl_poly *poly,
4086 	isl_stat (*fn)(__isl_take isl_term *term, void *user),
4087 	__isl_take isl_term *term, void *user)
4088 {
4089 	int i;
4090 	isl_bool is_zero, is_bad, is_cst;
4091 	isl_poly_rec *rec;
4092 
4093 	is_zero = isl_poly_is_zero(poly);
4094 	if (is_zero < 0 || !term)
4095 		goto error;
4096 
4097 	if (is_zero)
4098 		return term;
4099 
4100 	is_cst = isl_poly_is_cst(poly);
4101 	is_bad = isl_poly_is_nan(poly);
4102 	if (is_bad >= 0 && !is_bad)
4103 		is_bad = isl_poly_is_infty(poly);
4104 	if (is_bad >= 0 && !is_bad)
4105 		is_bad = isl_poly_is_neginfty(poly);
4106 	if (is_cst < 0 || is_bad < 0)
4107 		return isl_term_free(term);
4108 	if (is_bad)
4109 		isl_die(isl_term_get_ctx(term), isl_error_invalid,
4110 			"cannot handle NaN/infty polynomial",
4111 			return isl_term_free(term));
4112 
4113 	if (is_cst) {
4114 		isl_poly_cst *cst;
4115 		cst = isl_poly_as_cst(poly);
4116 		if (!cst)
4117 			goto error;
4118 		term = isl_term_cow(term);
4119 		if (!term)
4120 			goto error;
4121 		isl_int_set(term->n, cst->n);
4122 		isl_int_set(term->d, cst->d);
4123 		if (fn(isl_term_copy(term), user) < 0)
4124 			goto error;
4125 		return term;
4126 	}
4127 
4128 	rec = isl_poly_as_rec(poly);
4129 	if (!rec)
4130 		goto error;
4131 
4132 	for (i = 0; i < rec->n; ++i) {
4133 		term = isl_term_cow(term);
4134 		if (!term)
4135 			goto error;
4136 		term->pow[poly->var] = i;
4137 		term = isl_poly_foreach_term(rec->p[i], fn, term, user);
4138 		if (!term)
4139 			goto error;
4140 	}
4141 	term = isl_term_cow(term);
4142 	if (!term)
4143 		return NULL;
4144 	term->pow[poly->var] = 0;
4145 
4146 	return term;
4147 error:
4148 	isl_term_free(term);
4149 	return NULL;
4150 }
4151 
isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial * qp,isl_stat (* fn)(__isl_take isl_term * term,void * user),void * user)4152 isl_stat isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
4153 	isl_stat (*fn)(__isl_take isl_term *term, void *user), void *user)
4154 {
4155 	isl_term *term;
4156 
4157 	if (!qp)
4158 		return isl_stat_error;
4159 
4160 	term = isl_term_alloc(isl_space_copy(qp->dim), isl_mat_copy(qp->div));
4161 	if (!term)
4162 		return isl_stat_error;
4163 
4164 	term = isl_poly_foreach_term(qp->poly, fn, term, user);
4165 
4166 	isl_term_free(term);
4167 
4168 	return term ? isl_stat_ok : isl_stat_error;
4169 }
4170 
isl_qpolynomial_from_term(__isl_take isl_term * term)4171 __isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
4172 {
4173 	isl_poly *poly;
4174 	isl_qpolynomial *qp;
4175 	int i;
4176 	isl_size n;
4177 
4178 	n = isl_term_dim(term, isl_dim_all);
4179 	if (n < 0)
4180 		term = isl_term_free(term);
4181 	if (!term)
4182 		return NULL;
4183 
4184 	poly = isl_poly_rat_cst(term->dim->ctx, term->n, term->d);
4185 	for (i = 0; i < n; ++i) {
4186 		if (!term->pow[i])
4187 			continue;
4188 		poly = isl_poly_mul(poly,
4189 			    isl_poly_var_pow(term->dim->ctx, i, term->pow[i]));
4190 	}
4191 
4192 	qp = isl_qpolynomial_alloc(isl_space_copy(term->dim),
4193 				    term->div->n_row, poly);
4194 	if (!qp)
4195 		goto error;
4196 	isl_mat_free(qp->div);
4197 	qp->div = isl_mat_copy(term->div);
4198 	if (!qp->div)
4199 		goto error;
4200 
4201 	isl_term_free(term);
4202 	return qp;
4203 error:
4204 	isl_qpolynomial_free(qp);
4205 	isl_term_free(term);
4206 	return NULL;
4207 }
4208 
isl_qpolynomial_lift(__isl_take isl_qpolynomial * qp,__isl_take isl_space * space)4209 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
4210 	__isl_take isl_space *space)
4211 {
4212 	int i;
4213 	int extra;
4214 	isl_size total, d_set, d_qp;
4215 
4216 	if (!qp || !space)
4217 		goto error;
4218 
4219 	if (isl_space_is_equal(qp->dim, space)) {
4220 		isl_space_free(space);
4221 		return qp;
4222 	}
4223 
4224 	qp = isl_qpolynomial_cow(qp);
4225 	if (!qp)
4226 		goto error;
4227 
4228 	d_set = isl_space_dim(space, isl_dim_set);
4229 	d_qp = isl_qpolynomial_domain_dim(qp, isl_dim_set);
4230 	extra = d_set - d_qp;
4231 	total = isl_space_dim(qp->dim, isl_dim_all);
4232 	if (d_set < 0 || d_qp < 0 || total < 0)
4233 		goto error;
4234 	if (qp->div->n_row) {
4235 		int *exp;
4236 
4237 		exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
4238 		if (!exp)
4239 			goto error;
4240 		for (i = 0; i < qp->div->n_row; ++i)
4241 			exp[i] = extra + i;
4242 		qp->poly = expand(qp->poly, exp, total);
4243 		free(exp);
4244 		if (!qp->poly)
4245 			goto error;
4246 	}
4247 	qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
4248 	if (!qp->div)
4249 		goto error;
4250 	for (i = 0; i < qp->div->n_row; ++i)
4251 		isl_seq_clr(qp->div->row[i] + 2 + total, extra);
4252 
4253 	isl_space_free(qp->dim);
4254 	qp->dim = space;
4255 
4256 	return qp;
4257 error:
4258 	isl_space_free(space);
4259 	isl_qpolynomial_free(qp);
4260 	return NULL;
4261 }
4262 
4263 /* For each parameter or variable that does not appear in qp,
4264  * first eliminate the variable from all constraints and then set it to zero.
4265  */
fix_inactive(__isl_take isl_set * set,__isl_keep isl_qpolynomial * qp)4266 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
4267 	__isl_keep isl_qpolynomial *qp)
4268 {
4269 	int *active = NULL;
4270 	int i;
4271 	isl_size d;
4272 	isl_size nparam;
4273 	isl_size nvar;
4274 
4275 	d = isl_set_dim(set, isl_dim_all);
4276 	if (d < 0 || !qp)
4277 		goto error;
4278 
4279 	active = isl_calloc_array(set->ctx, int, d);
4280 	if (set_active(qp, active) < 0)
4281 		goto error;
4282 
4283 	for (i = 0; i < d; ++i)
4284 		if (!active[i])
4285 			break;
4286 
4287 	if (i == d) {
4288 		free(active);
4289 		return set;
4290 	}
4291 
4292 	nparam = isl_set_dim(set, isl_dim_param);
4293 	nvar = isl_set_dim(set, isl_dim_set);
4294 	if (nparam < 0 || nvar < 0)
4295 		goto error;
4296 	for (i = 0; i < nparam; ++i) {
4297 		if (active[i])
4298 			continue;
4299 		set = isl_set_eliminate(set, isl_dim_param, i, 1);
4300 		set = isl_set_fix_si(set, isl_dim_param, i, 0);
4301 	}
4302 	for (i = 0; i < nvar; ++i) {
4303 		if (active[nparam + i])
4304 			continue;
4305 		set = isl_set_eliminate(set, isl_dim_set, i, 1);
4306 		set = isl_set_fix_si(set, isl_dim_set, i, 0);
4307 	}
4308 
4309 	free(active);
4310 
4311 	return set;
4312 error:
4313 	free(active);
4314 	isl_set_free(set);
4315 	return NULL;
4316 }
4317 
4318 struct isl_opt_data {
4319 	isl_qpolynomial *qp;
4320 	int first;
4321 	isl_val *opt;
4322 	int max;
4323 };
4324 
opt_fn(__isl_take isl_point * pnt,void * user)4325 static isl_stat opt_fn(__isl_take isl_point *pnt, void *user)
4326 {
4327 	struct isl_opt_data *data = (struct isl_opt_data *)user;
4328 	isl_val *val;
4329 
4330 	val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
4331 	if (data->first) {
4332 		data->first = 0;
4333 		data->opt = val;
4334 	} else if (data->max) {
4335 		data->opt = isl_val_max(data->opt, val);
4336 	} else {
4337 		data->opt = isl_val_min(data->opt, val);
4338 	}
4339 
4340 	return isl_stat_ok;
4341 }
4342 
isl_qpolynomial_opt_on_domain(__isl_take isl_qpolynomial * qp,__isl_take isl_set * set,int max)4343 __isl_give isl_val *isl_qpolynomial_opt_on_domain(
4344 	__isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
4345 {
4346 	struct isl_opt_data data = { NULL, 1, NULL, max };
4347 	isl_bool is_cst;
4348 
4349 	if (!set || !qp)
4350 		goto error;
4351 
4352 	is_cst = isl_poly_is_cst(qp->poly);
4353 	if (is_cst < 0)
4354 		goto error;
4355 	if (is_cst) {
4356 		isl_set_free(set);
4357 		data.opt = isl_qpolynomial_get_constant_val(qp);
4358 		isl_qpolynomial_free(qp);
4359 		return data.opt;
4360 	}
4361 
4362 	set = fix_inactive(set, qp);
4363 
4364 	data.qp = qp;
4365 	if (isl_set_foreach_point(set, opt_fn, &data) < 0)
4366 		goto error;
4367 
4368 	if (data.first)
4369 		data.opt = isl_val_zero(isl_set_get_ctx(set));
4370 
4371 	isl_set_free(set);
4372 	isl_qpolynomial_free(qp);
4373 	return data.opt;
4374 error:
4375 	isl_set_free(set);
4376 	isl_qpolynomial_free(qp);
4377 	isl_val_free(data.opt);
4378 	return NULL;
4379 }
4380 
isl_qpolynomial_morph_domain(__isl_take isl_qpolynomial * qp,__isl_take isl_morph * morph)4381 __isl_give isl_qpolynomial *isl_qpolynomial_morph_domain(
4382 	__isl_take isl_qpolynomial *qp, __isl_take isl_morph *morph)
4383 {
4384 	int i;
4385 	int n_sub;
4386 	isl_ctx *ctx;
4387 	isl_poly **subs;
4388 	isl_mat *mat, *diag;
4389 
4390 	qp = isl_qpolynomial_cow(qp);
4391 	if (!qp || !morph)
4392 		goto error;
4393 
4394 	ctx = qp->dim->ctx;
4395 	isl_assert(ctx, isl_space_is_equal(qp->dim, morph->dom->dim), goto error);
4396 
4397 	n_sub = morph->inv->n_row - 1;
4398 	if (morph->inv->n_row != morph->inv->n_col)
4399 		n_sub += qp->div->n_row;
4400 	subs = isl_calloc_array(ctx, struct isl_poly *, n_sub);
4401 	if (n_sub && !subs)
4402 		goto error;
4403 
4404 	for (i = 0; 1 + i < morph->inv->n_row; ++i)
4405 		subs[i] = isl_poly_from_affine(ctx, morph->inv->row[1 + i],
4406 					morph->inv->row[0][0], morph->inv->n_col);
4407 	if (morph->inv->n_row != morph->inv->n_col)
4408 		for (i = 0; i < qp->div->n_row; ++i)
4409 			subs[morph->inv->n_row - 1 + i] =
4410 			    isl_poly_var_pow(ctx, morph->inv->n_col - 1 + i, 1);
4411 
4412 	qp->poly = isl_poly_subs(qp->poly, 0, n_sub, subs);
4413 
4414 	for (i = 0; i < n_sub; ++i)
4415 		isl_poly_free(subs[i]);
4416 	free(subs);
4417 
4418 	diag = isl_mat_diag(ctx, 1, morph->inv->row[0][0]);
4419 	mat = isl_mat_diagonal(diag, isl_mat_copy(morph->inv));
4420 	diag = isl_mat_diag(ctx, qp->div->n_row, morph->inv->row[0][0]);
4421 	mat = isl_mat_diagonal(mat, diag);
4422 	qp->div = isl_mat_product(qp->div, mat);
4423 	isl_space_free(qp->dim);
4424 	qp->dim = isl_space_copy(morph->ran->dim);
4425 
4426 	if (!qp->poly || !qp->div || !qp->dim)
4427 		goto error;
4428 
4429 	isl_morph_free(morph);
4430 
4431 	return qp;
4432 error:
4433 	isl_qpolynomial_free(qp);
4434 	isl_morph_free(morph);
4435 	return NULL;
4436 }
4437 
isl_union_pw_qpolynomial_mul(__isl_take isl_union_pw_qpolynomial * upwqp1,__isl_take isl_union_pw_qpolynomial * upwqp2)4438 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
4439 	__isl_take isl_union_pw_qpolynomial *upwqp1,
4440 	__isl_take isl_union_pw_qpolynomial *upwqp2)
4441 {
4442 	return isl_union_pw_qpolynomial_match_bin_op(upwqp1, upwqp2,
4443 						&isl_pw_qpolynomial_mul);
4444 }
4445 
4446 /* Reorder the dimension of "qp" according to the given reordering.
4447  */
isl_qpolynomial_realign_domain(__isl_take isl_qpolynomial * qp,__isl_take isl_reordering * r)4448 __isl_give isl_qpolynomial *isl_qpolynomial_realign_domain(
4449 	__isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
4450 {
4451 	isl_space *space;
4452 
4453 	qp = isl_qpolynomial_cow(qp);
4454 	if (!qp)
4455 		goto error;
4456 
4457 	r = isl_reordering_extend(r, qp->div->n_row);
4458 	if (!r)
4459 		goto error;
4460 
4461 	qp->div = isl_local_reorder(qp->div, isl_reordering_copy(r));
4462 	if (!qp->div)
4463 		goto error;
4464 
4465 	qp->poly = reorder(qp->poly, r->pos);
4466 	if (!qp->poly)
4467 		goto error;
4468 
4469 	space = isl_reordering_get_space(r);
4470 	qp = isl_qpolynomial_reset_domain_space(qp, space);
4471 
4472 	isl_reordering_free(r);
4473 	return qp;
4474 error:
4475 	isl_qpolynomial_free(qp);
4476 	isl_reordering_free(r);
4477 	return NULL;
4478 }
4479 
isl_qpolynomial_align_params(__isl_take isl_qpolynomial * qp,__isl_take isl_space * model)4480 __isl_give isl_qpolynomial *isl_qpolynomial_align_params(
4481 	__isl_take isl_qpolynomial *qp, __isl_take isl_space *model)
4482 {
4483 	isl_bool equal_params;
4484 
4485 	if (!qp || !model)
4486 		goto error;
4487 
4488 	equal_params = isl_space_has_equal_params(qp->dim, model);
4489 	if (equal_params < 0)
4490 		goto error;
4491 	if (!equal_params) {
4492 		isl_reordering *exp;
4493 
4494 		exp = isl_parameter_alignment_reordering(qp->dim, model);
4495 		exp = isl_reordering_extend_space(exp,
4496 					isl_qpolynomial_get_domain_space(qp));
4497 		qp = isl_qpolynomial_realign_domain(qp, exp);
4498 	}
4499 
4500 	isl_space_free(model);
4501 	return qp;
4502 error:
4503 	isl_space_free(model);
4504 	isl_qpolynomial_free(qp);
4505 	return NULL;
4506 }
4507 
4508 struct isl_split_periods_data {
4509 	int max_periods;
4510 	isl_pw_qpolynomial *res;
4511 };
4512 
4513 /* Create a slice where the integer division "div" has the fixed value "v".
4514  * In particular, if "div" refers to floor(f/m), then create a slice
4515  *
4516  *	m v <= f <= m v + (m - 1)
4517  *
4518  * or
4519  *
4520  *	f - m v >= 0
4521  *	-f + m v + (m - 1) >= 0
4522  */
set_div_slice(__isl_take isl_space * space,__isl_keep isl_qpolynomial * qp,int div,isl_int v)4523 static __isl_give isl_set *set_div_slice(__isl_take isl_space *space,
4524 	__isl_keep isl_qpolynomial *qp, int div, isl_int v)
4525 {
4526 	isl_size total;
4527 	isl_basic_set *bset = NULL;
4528 	int k;
4529 
4530 	total = isl_space_dim(space, isl_dim_all);
4531 	if (total < 0 || !qp)
4532 		goto error;
4533 
4534 	bset = isl_basic_set_alloc_space(isl_space_copy(space), 0, 0, 2);
4535 
4536 	k = isl_basic_set_alloc_inequality(bset);
4537 	if (k < 0)
4538 		goto error;
4539 	isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4540 	isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
4541 
4542 	k = isl_basic_set_alloc_inequality(bset);
4543 	if (k < 0)
4544 		goto error;
4545 	isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4546 	isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
4547 	isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
4548 	isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4549 
4550 	isl_space_free(space);
4551 	return isl_set_from_basic_set(bset);
4552 error:
4553 	isl_basic_set_free(bset);
4554 	isl_space_free(space);
4555 	return NULL;
4556 }
4557 
4558 static isl_stat split_periods(__isl_take isl_set *set,
4559 	__isl_take isl_qpolynomial *qp, void *user);
4560 
4561 /* Create a slice of the domain "set" such that integer division "div"
4562  * has the fixed value "v" and add the results to data->res,
4563  * replacing the integer division by "v" in "qp".
4564  */
set_div(__isl_take isl_set * set,__isl_take isl_qpolynomial * qp,int div,isl_int v,struct isl_split_periods_data * data)4565 static isl_stat set_div(__isl_take isl_set *set,
4566 	__isl_take isl_qpolynomial *qp, int div, isl_int v,
4567 	struct isl_split_periods_data *data)
4568 {
4569 	int i;
4570 	isl_size div_pos;
4571 	isl_set *slice;
4572 	isl_poly *cst;
4573 
4574 	slice = set_div_slice(isl_set_get_space(set), qp, div, v);
4575 	set = isl_set_intersect(set, slice);
4576 
4577 	div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
4578 	if (div_pos < 0)
4579 		goto error;
4580 
4581 	for (i = div + 1; i < qp->div->n_row; ++i) {
4582 		if (isl_int_is_zero(qp->div->row[i][2 + div_pos + div]))
4583 			continue;
4584 		isl_int_addmul(qp->div->row[i][1],
4585 				qp->div->row[i][2 + div_pos + div], v);
4586 		isl_int_set_si(qp->div->row[i][2 + div_pos + div], 0);
4587 	}
4588 
4589 	cst = isl_poly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
4590 	qp = substitute_div(qp, div, cst);
4591 
4592 	return split_periods(set, qp, data);
4593 error:
4594 	isl_set_free(set);
4595 	isl_qpolynomial_free(qp);
4596 	return isl_stat_error;
4597 }
4598 
4599 /* Split the domain "set" such that integer division "div"
4600  * has a fixed value (ranging from "min" to "max") on each slice
4601  * and add the results to data->res.
4602  */
split_div(__isl_take isl_set * set,__isl_take isl_qpolynomial * qp,int div,isl_int min,isl_int max,struct isl_split_periods_data * data)4603 static isl_stat split_div(__isl_take isl_set *set,
4604 	__isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
4605 	struct isl_split_periods_data *data)
4606 {
4607 	for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
4608 		isl_set *set_i = isl_set_copy(set);
4609 		isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
4610 
4611 		if (set_div(set_i, qp_i, div, min, data) < 0)
4612 			goto error;
4613 	}
4614 	isl_set_free(set);
4615 	isl_qpolynomial_free(qp);
4616 	return isl_stat_ok;
4617 error:
4618 	isl_set_free(set);
4619 	isl_qpolynomial_free(qp);
4620 	return isl_stat_error;
4621 }
4622 
4623 /* If "qp" refers to any integer division
4624  * that can only attain "max_periods" distinct values on "set"
4625  * then split the domain along those distinct values.
4626  * Add the results (or the original if no splitting occurs)
4627  * to data->res.
4628  */
split_periods(__isl_take isl_set * set,__isl_take isl_qpolynomial * qp,void * user)4629 static isl_stat split_periods(__isl_take isl_set *set,
4630 	__isl_take isl_qpolynomial *qp, void *user)
4631 {
4632 	int i;
4633 	isl_pw_qpolynomial *pwqp;
4634 	struct isl_split_periods_data *data;
4635 	isl_int min, max;
4636 	isl_size div_pos;
4637 	isl_stat r = isl_stat_ok;
4638 
4639 	data = (struct isl_split_periods_data *)user;
4640 
4641 	if (!set || !qp)
4642 		goto error;
4643 
4644 	if (qp->div->n_row == 0) {
4645 		pwqp = isl_pw_qpolynomial_alloc(set, qp);
4646 		data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4647 		return isl_stat_ok;
4648 	}
4649 
4650 	div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
4651 	if (div_pos < 0)
4652 		goto error;
4653 
4654 	isl_int_init(min);
4655 	isl_int_init(max);
4656 	for (i = 0; i < qp->div->n_row; ++i) {
4657 		enum isl_lp_result lp_res;
4658 
4659 		if (isl_seq_first_non_zero(qp->div->row[i] + 2 + div_pos,
4660 						qp->div->n_row) != -1)
4661 			continue;
4662 
4663 		lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
4664 					  set->ctx->one, &min, NULL, NULL);
4665 		if (lp_res == isl_lp_error)
4666 			goto error2;
4667 		if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4668 			continue;
4669 		isl_int_fdiv_q(min, min, qp->div->row[i][0]);
4670 
4671 		lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
4672 					  set->ctx->one, &max, NULL, NULL);
4673 		if (lp_res == isl_lp_error)
4674 			goto error2;
4675 		if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4676 			continue;
4677 		isl_int_fdiv_q(max, max, qp->div->row[i][0]);
4678 
4679 		isl_int_sub(max, max, min);
4680 		if (isl_int_cmp_si(max, data->max_periods) < 0) {
4681 			isl_int_add(max, max, min);
4682 			break;
4683 		}
4684 	}
4685 
4686 	if (i < qp->div->n_row) {
4687 		r = split_div(set, qp, i, min, max, data);
4688 	} else {
4689 		pwqp = isl_pw_qpolynomial_alloc(set, qp);
4690 		data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4691 	}
4692 
4693 	isl_int_clear(max);
4694 	isl_int_clear(min);
4695 
4696 	return r;
4697 error2:
4698 	isl_int_clear(max);
4699 	isl_int_clear(min);
4700 error:
4701 	isl_set_free(set);
4702 	isl_qpolynomial_free(qp);
4703 	return isl_stat_error;
4704 }
4705 
4706 /* If any quasi-polynomial in pwqp refers to any integer division
4707  * that can only attain "max_periods" distinct values on its domain
4708  * then split the domain along those distinct values.
4709  */
isl_pw_qpolynomial_split_periods(__isl_take isl_pw_qpolynomial * pwqp,int max_periods)4710 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
4711 	__isl_take isl_pw_qpolynomial *pwqp, int max_periods)
4712 {
4713 	struct isl_split_periods_data data;
4714 
4715 	data.max_periods = max_periods;
4716 	data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
4717 
4718 	if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
4719 		goto error;
4720 
4721 	isl_pw_qpolynomial_free(pwqp);
4722 
4723 	return data.res;
4724 error:
4725 	isl_pw_qpolynomial_free(data.res);
4726 	isl_pw_qpolynomial_free(pwqp);
4727 	return NULL;
4728 }
4729 
4730 /* Construct a piecewise quasipolynomial that is constant on the given
4731  * domain.  In particular, it is
4732  *	0	if cst == 0
4733  *	1	if cst == 1
4734  *  infinity	if cst == -1
4735  *
4736  * If cst == -1, then explicitly check whether the domain is empty and,
4737  * if so, return 0 instead.
4738  */
constant_on_domain(__isl_take isl_basic_set * bset,int cst)4739 static __isl_give isl_pw_qpolynomial *constant_on_domain(
4740 	__isl_take isl_basic_set *bset, int cst)
4741 {
4742 	isl_space *space;
4743 	isl_qpolynomial *qp;
4744 
4745 	if (cst < 0 && isl_basic_set_is_empty(bset) == isl_bool_true)
4746 		cst = 0;
4747 	if (!bset)
4748 		return NULL;
4749 
4750 	bset = isl_basic_set_params(bset);
4751 	space = isl_basic_set_get_space(bset);
4752 	if (cst < 0)
4753 		qp = isl_qpolynomial_infty_on_domain(space);
4754 	else if (cst == 0)
4755 		qp = isl_qpolynomial_zero_on_domain(space);
4756 	else
4757 		qp = isl_qpolynomial_one_on_domain(space);
4758 	return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset), qp);
4759 }
4760 
4761 /* Internal data structure for multiplicative_call_factor_pw_qpolynomial.
4762  * "fn" is the function that is called on each factor.
4763  * "pwpq" collects the results.
4764  */
4765 struct isl_multiplicative_call_data_pw_qpolynomial {
4766 	__isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset);
4767 	isl_pw_qpolynomial *pwqp;
4768 };
4769 
4770 /* isl_factorizer_every_factor_basic_set callback that applies
4771  * data->fn to the factor "bset" and multiplies in the result
4772  * in data->pwqp.
4773  */
multiplicative_call_factor_pw_qpolynomial(__isl_keep isl_basic_set * bset,void * user)4774 static isl_bool multiplicative_call_factor_pw_qpolynomial(
4775 	__isl_keep isl_basic_set *bset, void *user)
4776 {
4777 	struct isl_multiplicative_call_data_pw_qpolynomial *data = user;
4778 
4779 	bset = isl_basic_set_copy(bset);
4780 	data->pwqp = isl_pw_qpolynomial_mul(data->pwqp, data->fn(bset));
4781 	if (!data->pwqp)
4782 		return isl_bool_error;
4783 
4784 	return isl_bool_true;
4785 }
4786 
4787 /* Factor bset, call fn on each of the factors and return the product.
4788  *
4789  * If no factors can be found, simply call fn on the input.
4790  * Otherwise, construct the factors based on the factorizer,
4791  * call fn on each factor and compute the product.
4792  */
compressed_multiplicative_call(__isl_take isl_basic_set * bset,__isl_give isl_pw_qpolynomial * (* fn)(__isl_take isl_basic_set * bset))4793 static __isl_give isl_pw_qpolynomial *compressed_multiplicative_call(
4794 	__isl_take isl_basic_set *bset,
4795 	__isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4796 {
4797 	struct isl_multiplicative_call_data_pw_qpolynomial data = { fn };
4798 	isl_space *space;
4799 	isl_set *set;
4800 	isl_factorizer *f;
4801 	isl_qpolynomial *qp;
4802 	isl_bool every;
4803 
4804 	f = isl_basic_set_factorizer(bset);
4805 	if (!f)
4806 		goto error;
4807 	if (f->n_group == 0) {
4808 		isl_factorizer_free(f);
4809 		return fn(bset);
4810 	}
4811 
4812 	space = isl_basic_set_get_space(bset);
4813 	space = isl_space_params(space);
4814 	set = isl_set_universe(isl_space_copy(space));
4815 	qp = isl_qpolynomial_one_on_domain(space);
4816 	data.pwqp = isl_pw_qpolynomial_alloc(set, qp);
4817 
4818 	every = isl_factorizer_every_factor_basic_set(f,
4819 			&multiplicative_call_factor_pw_qpolynomial, &data);
4820 	if (every < 0)
4821 		data.pwqp = isl_pw_qpolynomial_free(data.pwqp);
4822 
4823 	isl_basic_set_free(bset);
4824 	isl_factorizer_free(f);
4825 
4826 	return data.pwqp;
4827 error:
4828 	isl_basic_set_free(bset);
4829 	return NULL;
4830 }
4831 
4832 /* Factor bset, call fn on each of the factors and return the product.
4833  * The function is assumed to evaluate to zero on empty domains,
4834  * to one on zero-dimensional domains and to infinity on unbounded domains
4835  * and will not be called explicitly on zero-dimensional or unbounded domains.
4836  *
4837  * We first check for some special cases and remove all equalities.
4838  * Then we hand over control to compressed_multiplicative_call.
4839  */
isl_basic_set_multiplicative_call(__isl_take isl_basic_set * bset,__isl_give isl_pw_qpolynomial * (* fn)(__isl_take isl_basic_set * bset))4840 __isl_give isl_pw_qpolynomial *isl_basic_set_multiplicative_call(
4841 	__isl_take isl_basic_set *bset,
4842 	__isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4843 {
4844 	isl_bool bounded;
4845 	isl_size dim;
4846 	isl_morph *morph;
4847 	isl_pw_qpolynomial *pwqp;
4848 
4849 	if (!bset)
4850 		return NULL;
4851 
4852 	if (isl_basic_set_plain_is_empty(bset))
4853 		return constant_on_domain(bset, 0);
4854 
4855 	dim = isl_basic_set_dim(bset, isl_dim_set);
4856 	if (dim < 0)
4857 		goto error;
4858 	if (dim == 0)
4859 		return constant_on_domain(bset, 1);
4860 
4861 	bounded = isl_basic_set_is_bounded(bset);
4862 	if (bounded < 0)
4863 		goto error;
4864 	if (!bounded)
4865 		return constant_on_domain(bset, -1);
4866 
4867 	if (bset->n_eq == 0)
4868 		return compressed_multiplicative_call(bset, fn);
4869 
4870 	morph = isl_basic_set_full_compression(bset);
4871 	bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
4872 
4873 	pwqp = compressed_multiplicative_call(bset, fn);
4874 
4875 	morph = isl_morph_dom_params(morph);
4876 	morph = isl_morph_ran_params(morph);
4877 	morph = isl_morph_inverse(morph);
4878 
4879 	pwqp = isl_pw_qpolynomial_morph_domain(pwqp, morph);
4880 
4881 	return pwqp;
4882 error:
4883 	isl_basic_set_free(bset);
4884 	return NULL;
4885 }
4886 
4887 /* Drop all floors in "qp", turning each integer division [a/m] into
4888  * a rational division a/m.  If "down" is set, then the integer division
4889  * is replaced by (a-(m-1))/m instead.
4890  */
qp_drop_floors(__isl_take isl_qpolynomial * qp,int down)4891 static __isl_give isl_qpolynomial *qp_drop_floors(
4892 	__isl_take isl_qpolynomial *qp, int down)
4893 {
4894 	int i;
4895 	isl_poly *s;
4896 
4897 	if (!qp)
4898 		return NULL;
4899 	if (qp->div->n_row == 0)
4900 		return qp;
4901 
4902 	qp = isl_qpolynomial_cow(qp);
4903 	if (!qp)
4904 		return NULL;
4905 
4906 	for (i = qp->div->n_row - 1; i >= 0; --i) {
4907 		if (down) {
4908 			isl_int_sub(qp->div->row[i][1],
4909 				    qp->div->row[i][1], qp->div->row[i][0]);
4910 			isl_int_add_ui(qp->div->row[i][1],
4911 				       qp->div->row[i][1], 1);
4912 		}
4913 		s = isl_poly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
4914 					qp->div->row[i][0], qp->div->n_col - 1);
4915 		qp = substitute_div(qp, i, s);
4916 		if (!qp)
4917 			return NULL;
4918 	}
4919 
4920 	return qp;
4921 }
4922 
4923 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4924  * a rational division a/m.
4925  */
pwqp_drop_floors(__isl_take isl_pw_qpolynomial * pwqp)4926 static __isl_give isl_pw_qpolynomial *pwqp_drop_floors(
4927 	__isl_take isl_pw_qpolynomial *pwqp)
4928 {
4929 	int i;
4930 
4931 	if (!pwqp)
4932 		return NULL;
4933 
4934 	if (isl_pw_qpolynomial_is_zero(pwqp))
4935 		return pwqp;
4936 
4937 	pwqp = isl_pw_qpolynomial_cow(pwqp);
4938 	if (!pwqp)
4939 		return NULL;
4940 
4941 	for (i = 0; i < pwqp->n; ++i) {
4942 		pwqp->p[i].qp = qp_drop_floors(pwqp->p[i].qp, 0);
4943 		if (!pwqp->p[i].qp)
4944 			goto error;
4945 	}
4946 
4947 	return pwqp;
4948 error:
4949 	isl_pw_qpolynomial_free(pwqp);
4950 	return NULL;
4951 }
4952 
4953 /* Adjust all the integer divisions in "qp" such that they are at least
4954  * one over the given orthant (identified by "signs").  This ensures
4955  * that they will still be non-negative even after subtracting (m-1)/m.
4956  *
4957  * In particular, f is replaced by f' + v, changing f = [a/m]
4958  * to f' = [(a - m v)/m].
4959  * If the constant term k in a is smaller than m,
4960  * the constant term of v is set to floor(k/m) - 1.
4961  * For any other term, if the coefficient c and the variable x have
4962  * the same sign, then no changes are needed.
4963  * Otherwise, if the variable is positive (and c is negative),
4964  * then the coefficient of x in v is set to floor(c/m).
4965  * If the variable is negative (and c is positive),
4966  * then the coefficient of x in v is set to ceil(c/m).
4967  */
make_divs_pos(__isl_take isl_qpolynomial * qp,int * signs)4968 static __isl_give isl_qpolynomial *make_divs_pos(__isl_take isl_qpolynomial *qp,
4969 	int *signs)
4970 {
4971 	int i, j;
4972 	isl_size div_pos;
4973 	isl_vec *v = NULL;
4974 	isl_poly *s;
4975 
4976 	qp = isl_qpolynomial_cow(qp);
4977 	div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
4978 	if (div_pos < 0)
4979 		return isl_qpolynomial_free(qp);
4980 	qp->div = isl_mat_cow(qp->div);
4981 	if (!qp->div)
4982 		goto error;
4983 
4984 	v = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
4985 
4986 	for (i = 0; i < qp->div->n_row; ++i) {
4987 		isl_int *row = qp->div->row[i];
4988 		v = isl_vec_clr(v);
4989 		if (!v)
4990 			goto error;
4991 		if (isl_int_lt(row[1], row[0])) {
4992 			isl_int_fdiv_q(v->el[0], row[1], row[0]);
4993 			isl_int_sub_ui(v->el[0], v->el[0], 1);
4994 			isl_int_submul(row[1], row[0], v->el[0]);
4995 		}
4996 		for (j = 0; j < div_pos; ++j) {
4997 			if (isl_int_sgn(row[2 + j]) * signs[j] >= 0)
4998 				continue;
4999 			if (signs[j] < 0)
5000 				isl_int_cdiv_q(v->el[1 + j], row[2 + j], row[0]);
5001 			else
5002 				isl_int_fdiv_q(v->el[1 + j], row[2 + j], row[0]);
5003 			isl_int_submul(row[2 + j], row[0], v->el[1 + j]);
5004 		}
5005 		for (j = 0; j < i; ++j) {
5006 			if (isl_int_sgn(row[2 + div_pos + j]) >= 0)
5007 				continue;
5008 			isl_int_fdiv_q(v->el[1 + div_pos + j],
5009 					row[2 + div_pos + j], row[0]);
5010 			isl_int_submul(row[2 + div_pos + j],
5011 					row[0], v->el[1 + div_pos + j]);
5012 		}
5013 		for (j = i + 1; j < qp->div->n_row; ++j) {
5014 			if (isl_int_is_zero(qp->div->row[j][2 + div_pos + i]))
5015 				continue;
5016 			isl_seq_combine(qp->div->row[j] + 1,
5017 				qp->div->ctx->one, qp->div->row[j] + 1,
5018 				qp->div->row[j][2 + div_pos + i], v->el,
5019 				v->size);
5020 		}
5021 		isl_int_set_si(v->el[1 + div_pos + i], 1);
5022 		s = isl_poly_from_affine(qp->dim->ctx, v->el,
5023 					qp->div->ctx->one, v->size);
5024 		qp->poly = isl_poly_subs(qp->poly, div_pos + i, 1, &s);
5025 		isl_poly_free(s);
5026 		if (!qp->poly)
5027 			goto error;
5028 	}
5029 
5030 	isl_vec_free(v);
5031 	return qp;
5032 error:
5033 	isl_vec_free(v);
5034 	isl_qpolynomial_free(qp);
5035 	return NULL;
5036 }
5037 
5038 struct isl_to_poly_data {
5039 	int sign;
5040 	isl_pw_qpolynomial *res;
5041 	isl_qpolynomial *qp;
5042 };
5043 
5044 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
5045  * We first make all integer divisions positive and then split the
5046  * quasipolynomials into terms with sign data->sign (the direction
5047  * of the requested approximation) and terms with the opposite sign.
5048  * In the first set of terms, each integer division [a/m] is
5049  * overapproximated by a/m, while in the second it is underapproximated
5050  * by (a-(m-1))/m.
5051  */
to_polynomial_on_orthant(__isl_take isl_set * orthant,int * signs,void * user)5052 static isl_stat to_polynomial_on_orthant(__isl_take isl_set *orthant,
5053 	int *signs, void *user)
5054 {
5055 	struct isl_to_poly_data *data = user;
5056 	isl_pw_qpolynomial *t;
5057 	isl_qpolynomial *qp, *up, *down;
5058 
5059 	qp = isl_qpolynomial_copy(data->qp);
5060 	qp = make_divs_pos(qp, signs);
5061 
5062 	up = isl_qpolynomial_terms_of_sign(qp, signs, data->sign);
5063 	up = qp_drop_floors(up, 0);
5064 	down = isl_qpolynomial_terms_of_sign(qp, signs, -data->sign);
5065 	down = qp_drop_floors(down, 1);
5066 
5067 	isl_qpolynomial_free(qp);
5068 	qp = isl_qpolynomial_add(up, down);
5069 
5070 	t = isl_pw_qpolynomial_alloc(orthant, qp);
5071 	data->res = isl_pw_qpolynomial_add_disjoint(data->res, t);
5072 
5073 	return isl_stat_ok;
5074 }
5075 
5076 /* Approximate each quasipolynomial by a polynomial.  If "sign" is positive,
5077  * the polynomial will be an overapproximation.  If "sign" is negative,
5078  * it will be an underapproximation.  If "sign" is zero, the approximation
5079  * will lie somewhere in between.
5080  *
5081  * In particular, is sign == 0, we simply drop the floors, turning
5082  * the integer divisions into rational divisions.
5083  * Otherwise, we split the domains into orthants, make all integer divisions
5084  * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
5085  * depending on the requested sign and the sign of the term in which
5086  * the integer division appears.
5087  */
isl_pw_qpolynomial_to_polynomial(__isl_take isl_pw_qpolynomial * pwqp,int sign)5088 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_to_polynomial(
5089 	__isl_take isl_pw_qpolynomial *pwqp, int sign)
5090 {
5091 	int i;
5092 	struct isl_to_poly_data data;
5093 
5094 	if (sign == 0)
5095 		return pwqp_drop_floors(pwqp);
5096 
5097 	if (!pwqp)
5098 		return NULL;
5099 
5100 	data.sign = sign;
5101 	data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
5102 
5103 	for (i = 0; i < pwqp->n; ++i) {
5104 		if (pwqp->p[i].qp->div->n_row == 0) {
5105 			isl_pw_qpolynomial *t;
5106 			t = isl_pw_qpolynomial_alloc(
5107 					isl_set_copy(pwqp->p[i].set),
5108 					isl_qpolynomial_copy(pwqp->p[i].qp));
5109 			data.res = isl_pw_qpolynomial_add_disjoint(data.res, t);
5110 			continue;
5111 		}
5112 		data.qp = pwqp->p[i].qp;
5113 		if (isl_set_foreach_orthant(pwqp->p[i].set,
5114 					&to_polynomial_on_orthant, &data) < 0)
5115 			goto error;
5116 	}
5117 
5118 	isl_pw_qpolynomial_free(pwqp);
5119 
5120 	return data.res;
5121 error:
5122 	isl_pw_qpolynomial_free(pwqp);
5123 	isl_pw_qpolynomial_free(data.res);
5124 	return NULL;
5125 }
5126 
poly_entry(__isl_take isl_pw_qpolynomial * pwqp,void * user)5127 static __isl_give isl_pw_qpolynomial *poly_entry(
5128 	__isl_take isl_pw_qpolynomial *pwqp, void *user)
5129 {
5130 	int *sign = user;
5131 
5132 	return isl_pw_qpolynomial_to_polynomial(pwqp, *sign);
5133 }
5134 
isl_union_pw_qpolynomial_to_polynomial(__isl_take isl_union_pw_qpolynomial * upwqp,int sign)5135 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_to_polynomial(
5136 	__isl_take isl_union_pw_qpolynomial *upwqp, int sign)
5137 {
5138 	return isl_union_pw_qpolynomial_transform_inplace(upwqp,
5139 				   &poly_entry, &sign);
5140 }
5141 
isl_basic_map_from_qpolynomial(__isl_take isl_qpolynomial * qp)5142 __isl_give isl_basic_map *isl_basic_map_from_qpolynomial(
5143 	__isl_take isl_qpolynomial *qp)
5144 {
5145 	int i, k;
5146 	isl_space *space;
5147 	isl_vec *aff = NULL;
5148 	isl_basic_map *bmap = NULL;
5149 	isl_bool is_affine;
5150 	unsigned pos;
5151 	unsigned n_div;
5152 
5153 	if (!qp)
5154 		return NULL;
5155 	is_affine = isl_poly_is_affine(qp->poly);
5156 	if (is_affine < 0)
5157 		goto error;
5158 	if (!is_affine)
5159 		isl_die(qp->dim->ctx, isl_error_invalid,
5160 			"input quasi-polynomial not affine", goto error);
5161 	aff = isl_qpolynomial_extract_affine(qp);
5162 	if (!aff)
5163 		goto error;
5164 	space = isl_qpolynomial_get_space(qp);
5165 	pos = 1 + isl_space_offset(space, isl_dim_out);
5166 	n_div = qp->div->n_row;
5167 	bmap = isl_basic_map_alloc_space(space, n_div, 1, 2 * n_div);
5168 
5169 	for (i = 0; i < n_div; ++i) {
5170 		k = isl_basic_map_alloc_div(bmap);
5171 		if (k < 0)
5172 			goto error;
5173 		isl_seq_cpy(bmap->div[k], qp->div->row[i], qp->div->n_col);
5174 		isl_int_set_si(bmap->div[k][qp->div->n_col], 0);
5175 		bmap = isl_basic_map_add_div_constraints(bmap, k);
5176 	}
5177 	k = isl_basic_map_alloc_equality(bmap);
5178 	if (k < 0)
5179 		goto error;
5180 	isl_int_neg(bmap->eq[k][pos], aff->el[0]);
5181 	isl_seq_cpy(bmap->eq[k], aff->el + 1, pos);
5182 	isl_seq_cpy(bmap->eq[k] + pos + 1, aff->el + 1 + pos, n_div);
5183 
5184 	isl_vec_free(aff);
5185 	isl_qpolynomial_free(qp);
5186 	bmap = isl_basic_map_finalize(bmap);
5187 	return bmap;
5188 error:
5189 	isl_vec_free(aff);
5190 	isl_qpolynomial_free(qp);
5191 	isl_basic_map_free(bmap);
5192 	return NULL;
5193 }
5194