2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 #include <isl_map_private.h>
18 #include <isl_polynomial_private.h>
19 #include <isl_printer_private.h>
20 #include <isl_dim_private.h>
21 #include <isl_mat_private.h>
22 #include <isl/union_map.h>
24 static const char *s_to[2] = { " -> ", " \\to " };
25 static const char *s_and[2] = { " and ", " \\wedge " };
26 static const char *s_or[2] = { " or ", " \\vee " };
27 static const char *s_le[2] = { "<=", "\\le" };
28 static const char *s_ge[2] = { ">=", "\\ge" };
29 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
30 static const char *s_close_set[2] = { " }", " \\,\\}" };
31 static const char *s_open_list[2] = { "[", "(" };
32 static const char *s_close_list[2] = { "]", ")" };
33 static const char *s_such_that[2] = { " : ", " \\mid " };
34 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
35 static const char *s_close_exists[2] = { ")", "" };
36 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
37 static const char *s_param_prefix[2] = { "p", "p_" };
38 static const char *s_input_prefix[2] = { "i", "i_" };
39 static const char *s_output_prefix[2] = { "o", "o_" };
41 static __isl_give isl_printer *print_constraint_polylib(
42 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
45 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
46 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
47 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
48 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
50 p = isl_printer_start_line(p);
51 p = isl_printer_print_int(p, ineq);
52 for (i = 0; i < n_out; ++i) {
53 p = isl_printer_print_str(p, " ");
54 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
56 for (i = 0; i < n_in; ++i) {
57 p = isl_printer_print_str(p, " ");
58 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
60 for (i = 0; i < bmap->n_div; ++i) {
61 p = isl_printer_print_str(p, " ");
62 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
64 for (i = 0; i < nparam; ++i) {
65 p = isl_printer_print_str(p, " ");
66 p = isl_printer_print_isl_int(p, c[1+i]);
68 p = isl_printer_print_str(p, " ");
69 p = isl_printer_print_isl_int(p, c[0]);
70 p = isl_printer_end_line(p);
74 static __isl_give isl_printer *print_constraints_polylib(
75 struct isl_basic_map *bmap, __isl_take isl_printer *p)
79 p = isl_printer_set_isl_int_width(p, 5);
81 for (i = 0; i < bmap->n_eq; ++i)
82 p = print_constraint_polylib(bmap, 0, i, p);
83 for (i = 0; i < bmap->n_ineq; ++i)
84 p = print_constraint_polylib(bmap, 1, i, p);
89 static __isl_give isl_printer *bset_print_constraints_polylib(
90 struct isl_basic_set *bset, __isl_take isl_printer *p)
92 return print_constraints_polylib((struct isl_basic_map *)bset, p);
95 static __isl_give isl_printer *isl_basic_map_print_polylib(
96 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
98 unsigned total = isl_basic_map_total_dim(bmap);
99 p = isl_printer_start_line(p);
100 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
101 p = isl_printer_print_str(p, " ");
102 p = isl_printer_print_int(p, 1 + total + 1);
104 p = isl_printer_print_str(p, " ");
105 p = isl_printer_print_int(p,
106 isl_basic_map_dim(bmap, isl_dim_out));
107 p = isl_printer_print_str(p, " ");
108 p = isl_printer_print_int(p,
109 isl_basic_map_dim(bmap, isl_dim_in));
110 p = isl_printer_print_str(p, " ");
111 p = isl_printer_print_int(p,
112 isl_basic_map_dim(bmap, isl_dim_div));
113 p = isl_printer_print_str(p, " ");
114 p = isl_printer_print_int(p,
115 isl_basic_map_dim(bmap, isl_dim_param));
117 p = isl_printer_end_line(p);
118 return print_constraints_polylib(bmap, p);
121 static __isl_give isl_printer *isl_basic_set_print_polylib(
122 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
124 return isl_basic_map_print_polylib((struct isl_basic_map *)bset, p, ext);
127 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
128 __isl_take isl_printer *p, int ext)
132 p = isl_printer_start_line(p);
133 p = isl_printer_print_int(p, map->n);
134 p = isl_printer_end_line(p);
135 for (i = 0; i < map->n; ++i) {
136 p = isl_printer_start_line(p);
137 p = isl_printer_end_line(p);
138 p = isl_basic_map_print_polylib(map->p[i], p, ext);
143 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
144 __isl_take isl_printer *p, int ext)
146 return isl_map_print_polylib((struct isl_map *)set, p, ext);
149 static int count_same_name(__isl_keep isl_dim *dim,
150 enum isl_dim_type type, unsigned pos, const char *name)
156 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
157 s = t == type ? pos : isl_dim_size(dim, t);
158 for (p = 0; p < s; ++p) {
159 const char *n = isl_dim_get_name(dim, t, p);
160 if (n && !strcmp(n, name))
167 static __isl_give isl_printer *print_name(__isl_keep isl_dim *dim,
168 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
175 name = type == isl_dim_div ? NULL : isl_dim_get_name(dim, type, pos);
179 if (type == isl_dim_param)
180 prefix = s_param_prefix[latex];
181 else if (type == isl_dim_div)
182 prefix = s_div_prefix[latex];
183 else if (set || type == isl_dim_in)
184 prefix = s_input_prefix[latex];
186 prefix = s_output_prefix[latex];
187 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
190 primes = count_same_name(dim, name == buffer ? isl_dim_div : type,
192 p = isl_printer_print_str(p, name);
194 p = isl_printer_print_str(p, "'");
198 static enum isl_dim_type pos2type(__isl_keep isl_dim *dim, unsigned *pos)
200 enum isl_dim_type type;
201 unsigned n_in = isl_dim_size(dim, isl_dim_in);
202 unsigned n_out = isl_dim_size(dim, isl_dim_out);
203 unsigned nparam = isl_dim_size(dim, isl_dim_param);
205 if (*pos < 1 + nparam) {
206 type = isl_dim_param;
208 } else if (*pos < 1 + nparam + n_in) {
211 } else if (*pos < 1 + nparam + n_in + n_out) {
213 *pos -= 1 + nparam + n_in;
216 *pos -= 1 + nparam + n_in + n_out;
222 static __isl_give isl_printer *print_div(__isl_keep isl_dim *dim,
223 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
225 static __isl_give isl_printer *print_term(__isl_keep isl_dim *dim,
226 __isl_keep isl_mat *div,
227 isl_int c, unsigned pos, __isl_take isl_printer *p, int set, int latex)
229 enum isl_dim_type type;
233 return isl_printer_print_isl_int(p, c);
235 type = pos2type(dim, &pos);
236 print_div_def = type == isl_dim_div && div &&
237 !isl_int_is_zero(div->row[pos][0]);
239 if (isl_int_is_one(c))
241 else if (isl_int_is_negone(c))
242 p = isl_printer_print_str(p, "-");
244 p = isl_printer_print_isl_int(p, c);
245 if (p->output_format == ISL_FORMAT_C || print_div_def)
246 p = isl_printer_print_str(p, "*");
249 p = print_div(dim, div, pos, p);
251 p = print_name(dim, p, type, pos, set, latex);
255 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_dim *dim,
256 __isl_keep isl_mat *div,
257 __isl_take isl_printer *p, isl_int *c, int len, int set)
262 for (i = 0, first = 1; i < len; ++i) {
264 if (isl_int_is_zero(c[i]))
267 if (isl_int_is_neg(c[i])) {
269 isl_int_neg(c[i], c[i]);
270 p = isl_printer_print_str(p, " - ");
272 p = isl_printer_print_str(p, " + ");
275 p = print_term(dim, div, c[i], i, p, set, 0);
277 isl_int_neg(c[i], c[i]);
280 p = isl_printer_print_str(p, "0");
284 static __isl_give isl_printer *print_affine(__isl_keep isl_basic_map *bmap,
285 __isl_keep isl_dim *dim, __isl_take isl_printer *p, isl_int *c, int set)
287 unsigned len = 1 + isl_basic_map_total_dim(bmap);
288 return print_affine_of_len(dim, NULL, p, c, len, set);
291 static int defining_equality(__isl_keep isl_basic_map *eq,
292 __isl_keep isl_dim *dim, enum isl_dim_type type, int pos)
300 pos += isl_dim_offset(dim, type);
301 total = isl_basic_map_total_dim(eq);
303 for (i = 0; i < eq->n_eq; ++i) {
304 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
306 if (isl_int_is_one(eq->eq[i][1 + pos]))
307 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
314 /* offset is the offset of local_dim inside global_type of global_dim.
316 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
317 __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
318 __isl_keep isl_dim *local_dim, enum isl_dim_type local_type,
319 int set, int latex, __isl_keep isl_basic_map *eq, int offset)
323 if (global_dim != local_dim && local_type == isl_dim_out)
324 offset += local_dim->n_in;
326 for (i = 0; i < isl_dim_size(local_dim, local_type); ++i) {
328 p = isl_printer_print_str(p, ", ");
329 j = defining_equality(eq, global_dim, global_type, offset + i);
331 int pos = 1 + isl_dim_offset(global_dim, global_type)
333 p = print_affine_of_len(eq->dim, NULL,
334 p, eq->eq[j], pos, set);
336 p = print_name(global_dim, p, global_type, offset + i,
343 static __isl_give isl_printer *print_var_list(__isl_keep isl_dim *dim,
344 __isl_take isl_printer *p, enum isl_dim_type type,
345 int set, int latex, __isl_keep isl_basic_map *eq)
347 return print_nested_var_list(p, dim, type, dim, type, set, latex, eq, 0);
350 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
351 __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
352 __isl_keep isl_dim *local_dim,
353 int set, int latex, __isl_keep isl_basic_map *eq, int offset);
355 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
356 __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
357 __isl_keep isl_dim *local_dim, enum isl_dim_type local_type,
358 int set, int latex, __isl_keep isl_basic_map *eq, int offset)
360 const char *name = NULL;
361 unsigned n = isl_dim_size(local_dim, local_type);
362 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
363 name = isl_dim_get_tuple_name(local_dim, local_type);
366 p = isl_printer_print_str(p, "\\mathrm{");
367 p = isl_printer_print_str(p, name);
369 p = isl_printer_print_str(p, "}");
372 if (!latex || n != 1 || name)
373 p = isl_printer_print_str(p, s_open_list[latex]);
374 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
375 local_dim->nested[local_type - isl_dim_in]) {
376 if (global_dim != local_dim && local_type == isl_dim_out)
377 offset += local_dim->n_in;
378 p = print_nested_map_dim(p, global_dim, global_type,
379 local_dim->nested[local_type - isl_dim_in],
380 set, latex, eq, offset);
382 p = print_nested_var_list(p, global_dim, global_type,
383 local_dim, local_type, set, latex, eq, offset);
384 if (!latex || n != 1 || name)
385 p = isl_printer_print_str(p, s_close_list[latex]);
389 static __isl_give isl_printer *print_tuple(__isl_keep isl_dim *dim,
390 __isl_take isl_printer *p, enum isl_dim_type type,
391 int set, int latex, __isl_keep isl_basic_map *eq)
393 return print_nested_tuple(p, dim, type, dim, type, set, latex, eq, 0);
396 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
397 __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
398 __isl_keep isl_dim *local_dim,
399 int set, int latex, __isl_keep isl_basic_map *eq, int offset)
401 p = print_nested_tuple(p, global_dim, global_type,
402 local_dim, isl_dim_in, set, latex, eq, offset);
403 p = isl_printer_print_str(p, s_to[latex]);
404 p = print_nested_tuple(p, global_dim, global_type,
405 local_dim, isl_dim_out, set, latex, eq, offset);
410 static __isl_give isl_printer *print_dim(__isl_keep isl_dim *dim,
411 __isl_take isl_printer *p, int set, int latex,
412 __isl_keep isl_basic_map *eq)
415 p = print_tuple(dim, p, isl_dim_set, 1, latex, eq);
417 p = print_tuple(dim, p, isl_dim_in, 0, latex, eq);
418 p = isl_printer_print_str(p, s_to[latex]);
419 p = print_tuple(dim, p, isl_dim_out, 0, latex, eq);
425 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_dim *dim,
426 __isl_take isl_printer *p)
428 if (isl_dim_size(dim, isl_dim_param) == 0)
431 p = isl_printer_start_line(p);
432 p = isl_printer_print_str(p, "symbolic ");
433 p = print_var_list(dim, p, isl_dim_param, 0, 0, NULL);
434 p = isl_printer_print_str(p, ";");
435 p = isl_printer_end_line(p);
439 static __isl_give isl_printer *print_constraint(struct isl_basic_map *bmap,
440 __isl_keep isl_dim *dim, __isl_take isl_printer *p,
441 isl_int *c, int last, const char *op, int first_constraint, int set,
444 if (!first_constraint)
445 p = isl_printer_print_str(p, s_and[latex]);
447 isl_int_abs(c[last], c[last]);
449 p = print_term(dim, NULL, c[last], last, p, set, latex);
451 p = isl_printer_print_str(p, " ");
452 p = isl_printer_print_str(p, op);
453 p = isl_printer_print_str(p, " ");
455 isl_int_set_si(c[last], 0);
456 p = print_affine(bmap, dim, p, c, set);
461 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
462 __isl_keep isl_dim *dim, __isl_take isl_printer *p, int set, int latex)
466 unsigned total = isl_basic_map_total_dim(bmap);
468 c = isl_vec_alloc(bmap->ctx, 1 + total);
472 for (i = bmap->n_eq - 1; i >= 0; --i) {
473 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
474 isl_assert(bmap->ctx, l >= 0, goto error);
475 if (isl_int_is_neg(bmap->eq[i][l]))
476 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
478 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
479 p = print_constraint(bmap, dim, p, c->el, l,
480 "=", i == bmap->n_eq - 1, set, latex);
482 for (i = 0; i < bmap->n_ineq; ++i) {
483 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
488 s = isl_int_sgn(bmap->ineq[i][l]);
490 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
492 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
493 op = s < 0 ? s_le[latex] : s_ge[latex];
494 p = print_constraint(bmap, dim, p, c->el, l,
495 op, !bmap->n_eq && !i, set, latex);
507 static __isl_give isl_printer *print_omega_constraints(
508 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int set)
510 if (bmap->n_eq + bmap->n_ineq == 0)
513 p = isl_printer_print_str(p, ": ");
514 if (bmap->n_div > 0) {
516 p = isl_printer_print_str(p, "exists (");
517 for (i = 0; i < bmap->n_div; ++i) {
519 p = isl_printer_print_str(p, ", ");
520 p = print_name(bmap->dim, p, isl_dim_div, i, 0, 0);
522 p = isl_printer_print_str(p, ": ");
524 p = print_constraints(bmap, bmap->dim, p, set, 0);
526 p = isl_printer_print_str(p, ")");
530 static __isl_give isl_printer *basic_map_print_omega(
531 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
533 p = isl_printer_print_str(p, "{ [");
534 p = print_var_list(bmap->dim, p, isl_dim_in, 0, 0, NULL);
535 p = isl_printer_print_str(p, "] -> [");
536 p = print_var_list(bmap->dim, p, isl_dim_out, 0, 0, NULL);
537 p = isl_printer_print_str(p, "] ");
538 p = print_omega_constraints(bmap, p, 0);
539 p = isl_printer_print_str(p, " }");
543 static __isl_give isl_printer *isl_basic_map_print_omega(
544 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
546 p = print_omega_parameters(bmap->dim, p);
548 p = isl_printer_start_line(p);
549 p = basic_map_print_omega(bmap, p);
550 p = isl_printer_end_line(p);
554 static __isl_give isl_printer *basic_set_print_omega(
555 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
557 p = isl_printer_print_str(p, "{ [");
558 p = print_var_list(bset->dim, p, isl_dim_set, 1, 0, NULL);
559 p = isl_printer_print_str(p, "] ");
560 p = print_omega_constraints((isl_basic_map *)bset, p, 1);
561 p = isl_printer_print_str(p, " }");
565 static __isl_give isl_printer *isl_basic_set_print_omega(
566 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
568 p = print_omega_parameters(bset->dim, p);
570 p = isl_printer_start_line(p);
571 p = basic_set_print_omega(bset, p);
572 p = isl_printer_end_line(p);
576 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
577 __isl_take isl_printer *p)
581 p = print_omega_parameters(map->dim, p);
583 p = isl_printer_start_line(p);
584 for (i = 0; i < map->n; ++i) {
586 p = isl_printer_print_str(p, " union ");
587 p = basic_map_print_omega(map->p[i], p);
589 p = isl_printer_end_line(p);
593 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
594 __isl_take isl_printer *p)
598 p = print_omega_parameters(set->dim, p);
600 p = isl_printer_start_line(p);
601 for (i = 0; i < set->n; ++i) {
603 p = isl_printer_print_str(p, " union ");
604 p = basic_set_print_omega(set->p[i], p);
606 p = isl_printer_end_line(p);
610 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
611 __isl_keep isl_dim *dim, __isl_take isl_printer *p, int set, int latex)
613 if (bmap->n_div > 0) {
615 p = isl_printer_print_str(p, s_open_exists[latex]);
616 for (i = 0; i < bmap->n_div; ++i) {
618 p = isl_printer_print_str(p, ", ");
619 p = print_name(dim, p, isl_dim_div, i, 0, latex);
620 if (latex || isl_int_is_zero(bmap->div[i][0]))
622 p = isl_printer_print_str(p, " = [(");
623 p = print_affine(bmap, dim, p, bmap->div[i] + 1, set);
624 p = isl_printer_print_str(p, ")/");
625 p = isl_printer_print_isl_int(p, bmap->div[i][0]);
626 p = isl_printer_print_str(p, "]");
628 p = isl_printer_print_str(p, ": ");
631 p = print_constraints(bmap, dim, p, set, latex);
634 p = isl_printer_print_str(p, s_close_exists[latex]);
638 static __isl_give isl_printer *isl_basic_map_print_isl(
639 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
642 if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
643 p = print_tuple(bmap->dim, p, isl_dim_param, 0, latex, NULL);
644 p = isl_printer_print_str(p, " -> ");
646 p = isl_printer_print_str(p, "{ ");
647 p = print_dim(bmap->dim, p, set, latex, NULL);
648 p = isl_printer_print_str(p, " : ");
649 p = print_disjunct(bmap, bmap->dim, p, set, latex);
650 p = isl_printer_print_str(p, " }");
654 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
655 __isl_take isl_printer *p, int set, int latex)
659 if (isl_map_fast_is_universe(map))
662 p = isl_printer_print_str(p, s_such_that[latex]);
664 p = isl_printer_print_str(p, "1 = 0");
665 for (i = 0; i < map->n; ++i) {
667 p = isl_printer_print_str(p, s_or[latex]);
668 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
669 p = isl_printer_print_str(p, "(");
670 p = print_disjunct(map->p[i], map->dim, p, set, latex);
671 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
672 p = isl_printer_print_str(p, ")");
677 struct isl_aff_split {
682 static void free_split(__isl_take struct isl_aff_split *split, int n)
689 for (i = 0; i < n; ++i) {
690 isl_basic_map_free(split[i].aff);
691 isl_map_free(split[i].map);
697 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
700 unsigned nparam, n_in, n_out, total;
702 bmap = isl_basic_map_cow(bmap);
705 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
708 nparam = isl_basic_map_dim(bmap, isl_dim_param);
709 n_in = isl_basic_map_dim(bmap, isl_dim_in);
710 n_out = isl_basic_map_dim(bmap, isl_dim_out);
711 total = isl_basic_map_dim(bmap, isl_dim_all);
712 for (i = bmap->n_eq - 1; i >= 0; --i) {
713 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
714 if (j >= nparam && j < nparam + n_in + n_out &&
715 (isl_int_is_one(bmap->eq[i][1 + j]) ||
716 isl_int_is_negone(bmap->eq[i][1 + j])))
718 if (isl_basic_map_drop_equality(bmap, i) < 0)
722 bmap = isl_basic_map_finalize(bmap);
726 isl_basic_map_free(bmap);
730 static int aff_split_cmp(const void *p1, const void *p2)
732 const struct isl_aff_split *s1, *s2;
733 s1 = (const struct isl_aff_split *) p1;
734 s2 = (const struct isl_aff_split *) p2;
736 return isl_basic_map_fast_cmp(s1->aff, s2->aff);
739 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
740 __isl_keep isl_basic_map *aff)
748 total = isl_dim_total(bmap->dim);
750 for (i = bmap->n_eq - 1; i >= 0; --i) {
751 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
754 for (j = 0; j < aff->n_eq; ++j) {
755 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
756 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
758 if (isl_basic_map_drop_equality(bmap, i) < 0)
766 isl_basic_map_free(bmap);
770 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
773 struct isl_aff_split *split;
775 split = isl_calloc_array(map->isl, struct isl_aff_split, map->n);
779 for (i = 0; i < map->n; ++i) {
781 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
782 bmap = isl_basic_map_copy(map->p[i]);
783 bmap = isl_basic_map_cow(bmap);
784 bmap = drop_aff(bmap, split[i].aff);
785 split[i].map = isl_map_from_basic_map(bmap);
786 if (!split[i].aff || !split[i].map)
790 qsort(split, map->n, sizeof(struct isl_aff_split), &aff_split_cmp);
793 for (i = n - 1; i >= 1; --i) {
794 if (!isl_basic_map_fast_is_equal(split[i - 1].aff,
797 isl_basic_map_free(split[i].aff);
798 split[i - 1].map = isl_map_union(split[i - 1].map,
801 split[i] = split[n - 1];
802 split[n - 1].aff = NULL;
803 split[n - 1].map = NULL;
809 free_split(split, map->n);
813 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
814 struct isl_aff_split *split, int n, int set)
818 for (i = 0; i < n; ++i) {
823 dim = split[i].map->dim;
825 p = isl_printer_print_str(p, "; ");
826 p = print_dim(dim, p, set, 0, split[i].aff);
827 p = print_disjuncts(split[i].map, p, set, 0);
833 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
834 __isl_take isl_printer *p, int set)
836 struct isl_aff_split *split = NULL;
839 split = split_aff(map);
841 p = print_split_map(p, split, map->n, set);
843 p = print_dim(map->dim, p, set, 0, NULL);
844 p = print_disjuncts(map, p, set, 0);
846 free_split(split, map->n);
850 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
851 __isl_take isl_printer *p, int set)
853 if (isl_map_dim(map, isl_dim_param) > 0) {
854 p = print_tuple(map->dim, p, isl_dim_param, set, 0, NULL);
855 p = isl_printer_print_str(p, s_to[0]);
857 p = isl_printer_print_str(p, s_open_set[0]);
858 p = isl_map_print_isl_body(map, p, set);
859 p = isl_printer_print_str(p, s_close_set[0]);
863 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
864 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff, int set)
866 if (isl_map_dim(map, isl_dim_param) > 0) {
867 p = print_tuple(map->dim, p, isl_dim_param, set, 1, NULL);
868 p = isl_printer_print_str(p, s_to[1]);
870 p = isl_printer_print_str(p, s_open_set[1]);
871 p = print_dim(map->dim, p, set, 1, aff);
872 p = print_disjuncts(map, p, set, 1);
873 p = isl_printer_print_str(p, s_close_set[1]);
878 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
879 __isl_take isl_printer *p, int set)
882 struct isl_aff_split *split = NULL;
885 split = split_aff(map);
888 return print_latex_map(map, p, NULL, set);
890 for (i = 0; i < map->n; ++i) {
894 p = isl_printer_print_str(p, " \\cup ");
895 p = print_latex_map(split[i].map, p, split[i].aff, set);
898 free_split(split, map->n);
902 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
903 __isl_keep isl_basic_map *bmap)
907 if (p->output_format == ISL_FORMAT_ISL)
908 return isl_basic_map_print_isl(bmap, p, 0, 0);
909 else if (p->output_format == ISL_FORMAT_OMEGA)
910 return isl_basic_map_print_omega(bmap, p);
911 isl_assert(bmap->ctx, 0, goto error);
917 void isl_basic_map_print(__isl_keep isl_basic_map *bmap, FILE *out, int indent,
918 const char *prefix, const char *suffix, unsigned output_format)
920 isl_printer *printer;
925 printer = isl_printer_to_file(bmap->ctx, out);
926 printer = isl_printer_set_indent(printer, indent);
927 printer = isl_printer_set_prefix(printer, prefix);
928 printer = isl_printer_set_suffix(printer, suffix);
929 printer = isl_printer_set_output_format(printer, output_format);
930 isl_printer_print_basic_map(printer, bmap);
932 isl_printer_free(printer);
935 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
936 __isl_keep isl_basic_set *bset)
941 if (p->output_format == ISL_FORMAT_ISL)
942 return isl_basic_map_print_isl(bset, p, 1, 0);
943 else if (p->output_format == ISL_FORMAT_POLYLIB)
944 return isl_basic_set_print_polylib(bset, p, 0);
945 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
946 return isl_basic_set_print_polylib(bset, p, 1);
947 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
948 return bset_print_constraints_polylib(bset, p);
949 else if (p->output_format == ISL_FORMAT_OMEGA)
950 return isl_basic_set_print_omega(bset, p);
951 isl_assert(p->ctx, 0, goto error);
957 void isl_basic_set_print(struct isl_basic_set *bset, FILE *out, int indent,
958 const char *prefix, const char *suffix, unsigned output_format)
960 isl_printer *printer;
965 printer = isl_printer_to_file(bset->ctx, out);
966 printer = isl_printer_set_indent(printer, indent);
967 printer = isl_printer_set_prefix(printer, prefix);
968 printer = isl_printer_set_suffix(printer, suffix);
969 printer = isl_printer_set_output_format(printer, output_format);
970 isl_printer_print_basic_set(printer, bset);
972 isl_printer_free(printer);
975 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
976 __isl_keep isl_set *set)
980 if (p->output_format == ISL_FORMAT_ISL)
981 return isl_map_print_isl((isl_map *)set, p, 1);
982 else if (p->output_format == ISL_FORMAT_POLYLIB)
983 return isl_set_print_polylib(set, p, 0);
984 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
985 return isl_set_print_polylib(set, p, 1);
986 else if (p->output_format == ISL_FORMAT_OMEGA)
987 return isl_set_print_omega(set, p);
988 else if (p->output_format == ISL_FORMAT_LATEX)
989 return isl_map_print_latex((isl_map *)set, p, 1);
990 isl_assert(set->ctx, 0, goto error);
996 void isl_set_print(struct isl_set *set, FILE *out, int indent,
997 unsigned output_format)
999 isl_printer *printer;
1004 printer = isl_printer_to_file(set->ctx, out);
1005 printer = isl_printer_set_indent(printer, indent);
1006 printer = isl_printer_set_output_format(printer, output_format);
1007 printer = isl_printer_print_set(printer, set);
1009 isl_printer_free(printer);
1012 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1013 __isl_keep isl_map *map)
1018 if (p->output_format == ISL_FORMAT_ISL)
1019 return isl_map_print_isl(map, p, 0);
1020 else if (p->output_format == ISL_FORMAT_POLYLIB)
1021 return isl_map_print_polylib(map, p, 0);
1022 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1023 return isl_map_print_polylib(map, p, 1);
1024 else if (p->output_format == ISL_FORMAT_OMEGA)
1025 return isl_map_print_omega(map, p);
1026 else if (p->output_format == ISL_FORMAT_LATEX)
1027 return isl_map_print_latex(map, p, 0);
1028 isl_assert(map->ctx, 0, goto error);
1030 isl_printer_free(p);
1034 struct isl_union_print_data {
1040 static int print_map_body(__isl_take isl_map *map, void *user)
1042 struct isl_union_print_data *data;
1043 data = (struct isl_union_print_data *)user;
1046 data->p = isl_printer_print_str(data->p, "; ");
1049 data->p = isl_map_print_isl_body(map, data->p, data->set);
1055 static __isl_give isl_printer *isl_union_map_print_isl(
1056 __isl_keep isl_union_map *umap, __isl_take isl_printer *p, int set)
1058 struct isl_union_print_data data = { p, set, 1 };
1060 dim = isl_union_map_get_dim(umap);
1061 if (isl_dim_size(dim, isl_dim_param) > 0) {
1062 p = print_tuple(dim, p, isl_dim_param, set, 0, NULL);
1063 p = isl_printer_print_str(p, s_to[0]);
1066 p = isl_printer_print_str(p, s_open_set[0]);
1067 isl_union_map_foreach_map(umap, &print_map_body, &data);
1069 p = isl_printer_print_str(p, s_close_set[0]);
1073 static int print_latex_map_body(__isl_take isl_map *map, void *user)
1075 struct isl_union_print_data *data;
1076 data = (struct isl_union_print_data *)user;
1079 data->p = isl_printer_print_str(data->p, " \\cup ");
1082 data->p = isl_map_print_latex(map, data->p, data->set);
1088 static __isl_give isl_printer *isl_union_map_print_latex(
1089 __isl_keep isl_union_map *umap, __isl_take isl_printer *p, int set)
1091 struct isl_union_print_data data = { p, set, 1 };
1092 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1097 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1098 __isl_keep isl_union_map *umap)
1103 if (p->output_format == ISL_FORMAT_ISL)
1104 return isl_union_map_print_isl(umap, p, 0);
1105 if (p->output_format == ISL_FORMAT_LATEX)
1106 return isl_union_map_print_latex(umap, p, 0);
1108 isl_die(p->ctx, isl_error_invalid,
1109 "invalid output format for isl_union_map", goto error);
1111 isl_printer_free(p);
1115 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1116 __isl_keep isl_union_set *uset)
1121 if (p->output_format == ISL_FORMAT_ISL)
1122 return isl_union_map_print_isl((isl_union_map *)uset, p, 1);
1123 if (p->output_format == ISL_FORMAT_LATEX)
1124 return isl_union_map_print_latex((isl_union_map *)uset, p, 1);
1126 isl_die(p->ctx, isl_error_invalid,
1127 "invalid output format for isl_union_set", goto error);
1129 isl_printer_free(p);
1133 void isl_map_print(__isl_keep isl_map *map, FILE *out, int indent,
1134 unsigned output_format)
1136 isl_printer *printer;
1141 printer = isl_printer_to_file(map->ctx, out);
1142 printer = isl_printer_set_indent(printer, indent);
1143 printer = isl_printer_set_output_format(printer, output_format);
1144 printer = isl_printer_print_map(printer, map);
1146 isl_printer_free(printer);
1149 void isl_basic_map_dump(__isl_keep isl_basic_map *bmap)
1151 isl_printer *printer;
1156 printer = isl_printer_to_file(isl_basic_map_get_ctx(bmap), stderr);
1157 printer = isl_printer_print_basic_map(printer, bmap);
1158 printer = isl_printer_end_line(printer);
1160 isl_printer_free(printer);
1163 void isl_basic_set_dump(__isl_keep isl_basic_set *bset)
1165 isl_printer *printer;
1170 printer = isl_printer_to_file(isl_basic_set_get_ctx(bset), stderr);
1171 printer = isl_printer_print_basic_set(printer, bset);
1172 printer = isl_printer_end_line(printer);
1174 isl_printer_free(printer);
1177 void isl_map_dump(__isl_keep isl_map *map)
1179 isl_printer *printer;
1184 printer = isl_printer_to_file(isl_map_get_ctx(map), stderr);
1185 printer = isl_printer_print_map(printer, map);
1186 printer = isl_printer_end_line(printer);
1188 isl_printer_free(printer);
1191 void isl_set_dump(__isl_keep isl_set *set)
1193 isl_printer *printer;
1198 printer = isl_printer_to_file(isl_set_get_ctx(set), stderr);
1199 printer = isl_printer_print_set(printer, set);
1200 printer = isl_printer_end_line(printer);
1202 isl_printer_free(printer);
1205 void isl_union_map_dump(__isl_keep isl_union_map *umap)
1207 isl_printer *printer;
1212 printer = isl_printer_to_file(isl_union_map_get_ctx(umap), stderr);
1213 printer = isl_printer_print_union_map(printer, umap);
1214 printer = isl_printer_end_line(printer);
1216 isl_printer_free(printer);
1219 void isl_union_set_dump(__isl_keep isl_union_set *uset)
1221 isl_printer *printer;
1226 printer = isl_printer_to_file(isl_union_set_get_ctx(uset), stderr);
1227 printer = isl_printer_print_union_set(printer, uset);
1228 printer = isl_printer_end_line(printer);
1230 isl_printer_free(printer);
1233 void isl_union_pw_qpolynomial_dump(__isl_keep isl_union_pw_qpolynomial *upwqp)
1235 isl_printer *printer;
1240 printer = isl_printer_to_file(isl_union_pw_qpolynomial_get_ctx(upwqp),
1242 printer = isl_printer_print_union_pw_qpolynomial(printer, upwqp);
1243 printer = isl_printer_end_line(printer);
1245 isl_printer_free(printer);
1248 void isl_qpolynomial_dump(__isl_keep isl_qpolynomial *qp)
1250 isl_printer *printer;
1255 printer = isl_printer_to_file(isl_qpolynomial_get_ctx(qp), stderr);
1256 printer = isl_printer_print_qpolynomial(printer, qp);
1257 printer = isl_printer_end_line(printer);
1259 isl_printer_free(printer);
1262 void isl_qpolynomial_fold_dump(__isl_keep isl_qpolynomial_fold *fold)
1264 isl_printer *printer;
1269 printer = isl_printer_to_file(isl_qpolynomial_fold_get_ctx(fold),
1271 printer = isl_printer_print_qpolynomial_fold(printer, fold);
1272 printer = isl_printer_end_line(printer);
1274 isl_printer_free(printer);
1277 void isl_pw_qpolynomial_dump(__isl_keep isl_pw_qpolynomial *pwqp)
1279 isl_printer *printer;
1284 printer = isl_printer_to_file(isl_pw_qpolynomial_get_ctx(pwqp), stderr);
1285 printer = isl_printer_print_pw_qpolynomial(printer, pwqp);
1286 printer = isl_printer_end_line(printer);
1288 isl_printer_free(printer);
1291 void isl_pw_qpolynomial_fold_dump(__isl_keep isl_pw_qpolynomial_fold *pwf)
1293 isl_printer *printer;
1298 printer = isl_printer_to_file(
1299 isl_pw_qpolynomial_fold_get_ctx(pwf), stderr);
1300 printer = isl_printer_print_pw_qpolynomial_fold(printer, pwf);
1301 printer = isl_printer_end_line(printer);
1303 isl_printer_free(printer);
1306 void isl_union_pw_qpolynomial_fold_dump(
1307 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1309 isl_printer *printer;
1314 printer = isl_printer_to_file(
1315 isl_union_pw_qpolynomial_fold_get_ctx(upwf), stderr);
1316 printer = isl_printer_print_union_pw_qpolynomial_fold(printer, upwf);
1317 printer = isl_printer_end_line(printer);
1319 isl_printer_free(printer);
1322 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
1327 for (i = 0, n = 0; i < rec->n; ++i)
1328 if (!isl_upoly_is_zero(rec->p[i]))
1334 static __isl_give isl_printer *print_div(__isl_keep isl_dim *dim,
1335 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
1337 int c = p->output_format == ISL_FORMAT_C;
1338 p = isl_printer_print_str(p, c ? "floord(" : "[(");
1339 p = print_affine_of_len(dim, div, p,
1340 div->row[pos] + 1, div->n_col - 1, 1);
1341 p = isl_printer_print_str(p, c ? ", " : ")/");
1342 p = isl_printer_print_isl_int(p, div->row[pos][0]);
1343 p = isl_printer_print_str(p, c ? ")" : "]");
1347 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
1348 __isl_take isl_printer *p, int first)
1350 struct isl_upoly_cst *cst;
1353 cst = isl_upoly_as_cst(up);
1356 neg = !first && isl_int_is_neg(cst->n);
1358 p = isl_printer_print_str(p, neg ? " - " : " + ");
1360 isl_int_neg(cst->n, cst->n);
1361 if (isl_int_is_zero(cst->d)) {
1362 int sgn = isl_int_sgn(cst->n);
1363 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1364 sgn == 0 ? "NaN" : "infty");
1366 p = isl_printer_print_isl_int(p, cst->n);
1368 isl_int_neg(cst->n, cst->n);
1369 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1370 p = isl_printer_print_str(p, "/");
1371 p = isl_printer_print_isl_int(p, cst->d);
1375 isl_printer_free(p);
1379 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1380 __isl_keep isl_dim *dim, __isl_keep isl_mat *div, int var)
1384 total = isl_dim_total(dim);
1386 p = print_term(dim, NULL, dim->ctx->one, 1 + var, p, 1, 0);
1388 p = print_div(dim, div, var - total, p);
1392 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1393 __isl_keep isl_dim *dim, __isl_keep isl_mat *div, int var, int exp)
1395 p = print_base(p, dim, div, var);
1398 if (p->output_format == ISL_FORMAT_C) {
1400 for (i = 1; i < exp; ++i) {
1401 p = isl_printer_print_str(p, "*");
1402 p = print_base(p, dim, div, var);
1405 p = isl_printer_print_str(p, "^");
1406 p = isl_printer_print_int(p, exp);
1411 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1412 __isl_keep isl_dim *dim, __isl_keep isl_mat *div,
1413 __isl_take isl_printer *p, int outer)
1415 int i, n, first, print_parens;
1416 struct isl_upoly_rec *rec;
1418 if (!p || !up || !dim || !div)
1421 if (isl_upoly_is_cst(up))
1422 return upoly_print_cst(up, p, 1);
1424 rec = isl_upoly_as_rec(up);
1427 n = upoly_rec_n_non_zero(rec);
1428 print_parens = n > 1 || (outer && rec->up.var >= isl_dim_total(dim));
1430 p = isl_printer_print_str(p, "(");
1431 for (i = 0, first = 1; i < rec->n; ++i) {
1432 if (isl_upoly_is_zero(rec->p[i]))
1434 if (isl_upoly_is_negone(rec->p[i])) {
1436 p = isl_printer_print_str(p, "-1");
1438 p = isl_printer_print_str(p, "-");
1440 p = isl_printer_print_str(p, " - ");
1441 } else if (isl_upoly_is_cst(rec->p[i]) &&
1442 !isl_upoly_is_one(rec->p[i]))
1443 p = upoly_print_cst(rec->p[i], p, first);
1446 p = isl_printer_print_str(p, " + ");
1447 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1448 p = upoly_print(rec->p[i], dim, div, p, 0);
1453 if (!isl_upoly_is_one(rec->p[i]) &&
1454 !isl_upoly_is_negone(rec->p[i]))
1455 p = isl_printer_print_str(p, " * ");
1456 p = print_pow(p, dim, div, rec->up.var, i);
1459 p = isl_printer_print_str(p, ")");
1462 isl_printer_free(p);
1466 __isl_give isl_printer *isl_printer_print_qpolynomial(__isl_take isl_printer *p,
1467 __isl_keep isl_qpolynomial *qp)
1471 p = upoly_print(qp->upoly, qp->dim, qp->div, p, 1);
1474 isl_printer_free(p);
1478 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1479 unsigned output_format)
1486 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1487 p = isl_printer_to_file(qp->dim->ctx, out);
1488 p = isl_printer_print_qpolynomial(p, qp);
1489 isl_printer_free(p);
1492 static __isl_give isl_printer *qpolynomial_fold_print(
1493 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1497 if (fold->type == isl_fold_min)
1498 p = isl_printer_print_str(p, "min");
1499 else if (fold->type == isl_fold_max)
1500 p = isl_printer_print_str(p, "max");
1501 p = isl_printer_print_str(p, "(");
1502 for (i = 0; i < fold->n; ++i) {
1504 p = isl_printer_print_str(p, ", ");
1505 p = isl_printer_print_qpolynomial(p, fold->qp[i]);
1507 p = isl_printer_print_str(p, ")");
1511 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1512 FILE *out, unsigned output_format)
1519 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1521 p = isl_printer_to_file(fold->dim->ctx, out);
1522 p = isl_printer_print_qpolynomial_fold(p, fold);
1524 isl_printer_free(p);
1527 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1528 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1532 for (i = 0; i < pwqp->n; ++i) {
1534 p = isl_printer_print_str(p, "; ");
1535 if (isl_dim_size(pwqp->dim, isl_dim_set) > 0 ||
1536 isl_dim_is_named_or_nested(pwqp->dim, isl_dim_set)) {
1537 p = print_dim(pwqp->p[i].set->dim, p, 1, 0, NULL);
1538 p = isl_printer_print_str(p, " -> ");
1540 p = isl_printer_print_qpolynomial(p, pwqp->p[i].qp);
1541 p = print_disjuncts((isl_map *)pwqp->p[i].set, p, 1, 0);
1547 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1548 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1553 if (isl_dim_size(pwqp->dim, isl_dim_param) > 0) {
1554 p = print_tuple(pwqp->dim, p, isl_dim_param, 0, 0, NULL);
1555 p = isl_printer_print_str(p, " -> ");
1557 p = isl_printer_print_str(p, "{ ");
1559 if (isl_dim_size(pwqp->dim, isl_dim_set) > 0 ||
1560 isl_dim_is_named_or_nested(pwqp->dim, isl_dim_set)) {
1561 p = print_dim(pwqp->dim, p, 1, 0, NULL);
1562 p = isl_printer_print_str(p, " -> ");
1564 p = isl_printer_print_str(p, "0");
1566 p = isl_pwqp_print_isl_body(p, pwqp);
1567 p = isl_printer_print_str(p, " }");
1570 isl_printer_free(p);
1574 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
1575 unsigned output_format)
1582 p = isl_printer_to_file(pwqp->dim->ctx, out);
1583 p = isl_printer_set_output_format(p, output_format);
1584 p = isl_printer_print_pw_qpolynomial(p, pwqp);
1586 isl_printer_free(p);
1589 static __isl_give isl_printer *isl_pwf_print_isl_body(
1590 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1594 for (i = 0; i < pwf->n; ++i) {
1596 p = isl_printer_print_str(p, "; ");
1597 if (isl_dim_size(pwf->dim, isl_dim_set) > 0 ||
1598 isl_dim_is_named_or_nested(pwf->dim, isl_dim_set)) {
1599 p = print_dim(pwf->p[i].set->dim, p, 1, 0, NULL);
1600 p = isl_printer_print_str(p, " -> ");
1602 p = qpolynomial_fold_print(pwf->p[i].fold, p);
1603 p = print_disjuncts((isl_map *)pwf->p[i].set, p, 1, 0);
1609 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
1610 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1612 if (isl_dim_size(pwf->dim, isl_dim_param) > 0) {
1613 p = print_tuple(pwf->dim, p, isl_dim_param, 0, 0, NULL);
1614 p = isl_printer_print_str(p, " -> ");
1616 p = isl_printer_print_str(p, "{ ");
1618 if (isl_dim_size(pwf->dim, isl_dim_set) > 0 ||
1619 isl_dim_is_named_or_nested(pwf->dim, isl_dim_set)) {
1620 p = print_dim(pwf->dim, p, 1, 0, NULL);
1621 p = isl_printer_print_str(p, " -> ");
1623 p = isl_printer_print_str(p, "0");
1625 p = isl_pwf_print_isl_body(p, pwf);
1626 p = isl_printer_print_str(p, " }");
1630 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1631 __isl_keep isl_dim *dim, __isl_keep isl_basic_set *bset, isl_int *c);
1633 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1634 __isl_keep isl_dim *dim,
1635 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1637 if (type == isl_dim_div) {
1638 p = isl_printer_print_str(p, "floord(");
1639 p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
1640 p = isl_printer_print_str(p, ", ");
1641 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1642 p = isl_printer_print_str(p, ")");
1646 name = isl_dim_get_name(dim, type, pos);
1649 p = isl_printer_print_str(p, name);
1654 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1655 __isl_keep isl_dim *dim,
1656 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1658 enum isl_dim_type type;
1659 unsigned nparam = isl_basic_set_dim(bset, isl_dim_param);
1662 return isl_printer_print_isl_int(p, c);
1664 if (isl_int_is_one(c))
1666 else if (isl_int_is_negone(c))
1667 p = isl_printer_print_str(p, "-");
1669 p = isl_printer_print_isl_int(p, c);
1670 p = isl_printer_print_str(p, "*");
1672 type = pos2type(dim, &pos);
1673 p = print_name_c(p, dim, bset, type, pos);
1677 static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
1678 __isl_keep isl_dim *dim,
1679 __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
1684 for (i = 0, first = 1; i < len; ++i) {
1686 if (isl_int_is_zero(c[i]))
1689 if (isl_int_is_neg(c[i])) {
1691 isl_int_neg(c[i], c[i]);
1692 p = isl_printer_print_str(p, " - ");
1694 p = isl_printer_print_str(p, " + ");
1697 p = print_term_c(p, dim, bset, c[i], i);
1699 isl_int_neg(c[i], c[i]);
1702 p = isl_printer_print_str(p, "0");
1706 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1707 __isl_keep isl_dim *dim, __isl_keep isl_basic_set *bset, isl_int *c)
1709 unsigned len = 1 + isl_basic_set_total_dim(bset);
1710 return print_partial_affine_c(p, dim, bset, c, len);
1713 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1714 __isl_keep isl_dim *dim,
1715 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int first)
1718 p = isl_printer_print_str(p, " && ");
1720 p = print_affine_c(p, dim, bset, c);
1721 p = isl_printer_print_str(p, " ");
1722 p = isl_printer_print_str(p, op);
1723 p = isl_printer_print_str(p, " 0");
1727 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1728 __isl_keep isl_dim *dim, __isl_keep isl_basic_set *bset)
1731 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
1732 unsigned total = isl_basic_set_total_dim(bset) - n_div;
1734 for (i = 0; i < bset->n_eq; ++i) {
1735 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
1737 p = print_constraint_c(p, dim, bset,
1738 bset->eq[i], "==", !i);
1741 p = isl_printer_print_str(p, " && ");
1742 p = isl_printer_print_str(p, "(");
1743 p = print_partial_affine_c(p, dim, bset, bset->eq[i],
1745 p = isl_printer_print_str(p, ") % ");
1746 p = isl_printer_print_isl_int(p,
1747 bset->eq[i][1 + total + j]);
1748 p = isl_printer_print_str(p, " == 0");
1751 for (i = 0; i < bset->n_ineq; ++i)
1752 p = print_constraint_c(p, dim, bset, bset->ineq[i], ">=",
1757 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
1758 __isl_keep isl_dim *dim, __isl_keep isl_set *set)
1763 p = isl_printer_print_str(p, "0");
1765 for (i = 0; i < set->n; ++i) {
1767 p = isl_printer_print_str(p, " || ");
1769 p = isl_printer_print_str(p, "(");
1770 p = print_basic_set_c(p, dim, set->p[i]);
1772 p = isl_printer_print_str(p, ")");
1777 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1778 __isl_keep isl_dim *dim, __isl_keep isl_qpolynomial *qp)
1783 isl_qpolynomial_get_den(qp, &den);
1784 if (!isl_int_is_one(den)) {
1786 p = isl_printer_print_str(p, "(");
1787 qp = isl_qpolynomial_copy(qp);
1788 f = isl_qpolynomial_rat_cst(isl_dim_copy(qp->dim),
1789 den, qp->dim->ctx->one);
1790 qp = isl_qpolynomial_mul(qp, f);
1793 p = upoly_print(qp->upoly, dim, qp->div, p, 0);
1794 if (!isl_int_is_one(den)) {
1795 p = isl_printer_print_str(p, ")/");
1796 p = isl_printer_print_isl_int(p, den);
1797 isl_qpolynomial_free(qp);
1803 static __isl_give isl_printer *print_pw_qpolynomial_c(
1804 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1808 if (pwqp->n == 1 && isl_set_fast_is_universe(pwqp->p[0].set))
1809 return print_qpolynomial_c(p, pwqp->dim, pwqp->p[0].qp);
1811 for (i = 0; i < pwqp->n; ++i) {
1812 p = isl_printer_print_str(p, "(");
1813 p = print_set_c(p, pwqp->dim, pwqp->p[i].set);
1814 p = isl_printer_print_str(p, ") ? (");
1815 p = print_qpolynomial_c(p, pwqp->dim, pwqp->p[i].qp);
1816 p = isl_printer_print_str(p, ") : ");
1819 p = isl_printer_print_str(p, "0");
1823 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
1824 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1829 if (p->output_format == ISL_FORMAT_ISL)
1830 return print_pw_qpolynomial_isl(p, pwqp);
1831 else if (p->output_format == ISL_FORMAT_C)
1832 return print_pw_qpolynomial_c(p, pwqp);
1833 isl_assert(p->ctx, 0, goto error);
1835 isl_printer_free(p);
1839 static int print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1841 struct isl_union_print_data *data;
1842 data = (struct isl_union_print_data *)user;
1845 data->p = isl_printer_print_str(data->p, "; ");
1848 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
1849 isl_pw_qpolynomial_free(pwqp);
1854 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
1855 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1857 struct isl_union_print_data data = { p, 1, 1 };
1859 dim = isl_union_pw_qpolynomial_get_dim(upwqp);
1860 if (isl_dim_size(dim, isl_dim_param) > 0) {
1861 p = print_tuple(dim, p, isl_dim_param, 1, 0, NULL);
1862 p = isl_printer_print_str(p, " -> ");
1865 p = isl_printer_print_str(p, "{ ");
1866 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
1869 p = isl_printer_print_str(p, " }");
1873 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
1874 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1879 if (p->output_format == ISL_FORMAT_ISL)
1880 return print_union_pw_qpolynomial_isl(p, upwqp);
1881 isl_die(p->ctx, isl_error_invalid,
1882 "invalid output format for isl_union_pw_qpolynomial",
1885 isl_printer_free(p);
1889 static __isl_give isl_printer *print_qpolynomial_fold_c(
1890 __isl_take isl_printer *p, __isl_keep isl_dim *dim,
1891 __isl_keep isl_qpolynomial_fold *fold)
1895 for (i = 0; i < fold->n - 1; ++i)
1896 if (fold->type == isl_fold_min)
1897 p = isl_printer_print_str(p, "min(");
1898 else if (fold->type == isl_fold_max)
1899 p = isl_printer_print_str(p, "max(");
1901 for (i = 0; i < fold->n; ++i) {
1903 p = isl_printer_print_str(p, ", ");
1904 p = print_qpolynomial_c(p, dim, fold->qp[i]);
1906 p = isl_printer_print_str(p, ")");
1911 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
1912 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
1916 if (p->output_format == ISL_FORMAT_ISL)
1917 return qpolynomial_fold_print(fold, p);
1918 else if (p->output_format == ISL_FORMAT_C)
1919 return print_qpolynomial_fold_c(p, fold->dim, fold);
1920 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
1923 isl_printer_free(p);
1927 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
1928 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1932 if (pwf->n == 1 && isl_set_fast_is_universe(pwf->p[0].set))
1933 return print_qpolynomial_fold_c(p, pwf->dim, pwf->p[0].fold);
1935 for (i = 0; i < pwf->n; ++i) {
1936 p = isl_printer_print_str(p, "(");
1937 p = print_set_c(p, pwf->dim, pwf->p[i].set);
1938 p = isl_printer_print_str(p, ") ? (");
1939 p = print_qpolynomial_fold_c(p, pwf->dim, pwf->p[i].fold);
1940 p = isl_printer_print_str(p, ") : ");
1943 p = isl_printer_print_str(p, "0");
1947 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
1948 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1953 if (p->output_format == ISL_FORMAT_ISL)
1954 return print_pw_qpolynomial_fold_isl(p, pwf);
1955 else if (p->output_format == ISL_FORMAT_C)
1956 return print_pw_qpolynomial_fold_c(p, pwf);
1957 isl_assert(p->ctx, 0, goto error);
1959 isl_printer_free(p);
1963 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
1964 FILE *out, unsigned output_format)
1971 p = isl_printer_to_file(pwf->dim->ctx, out);
1972 p = isl_printer_set_output_format(p, output_format);
1973 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
1975 isl_printer_free(p);
1978 static int print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf, void *user)
1980 struct isl_union_print_data *data;
1981 data = (struct isl_union_print_data *)user;
1984 data->p = isl_printer_print_str(data->p, "; ");
1987 data->p = isl_pwf_print_isl_body(data->p, pwf);
1988 isl_pw_qpolynomial_fold_free(pwf);
1993 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
1994 __isl_take isl_printer *p,
1995 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1997 struct isl_union_print_data data = { p, 1, 1 };
1999 dim = isl_union_pw_qpolynomial_fold_get_dim(upwf);
2000 if (isl_dim_size(dim, isl_dim_param) > 0) {
2001 p = print_tuple(dim, p, isl_dim_param, 1, 0, NULL);
2002 p = isl_printer_print_str(p, " -> ");
2005 p = isl_printer_print_str(p, "{ ");
2006 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
2007 &print_pwf_body, &data);
2009 p = isl_printer_print_str(p, " }");
2013 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2014 __isl_take isl_printer *p,
2015 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2020 if (p->output_format == ISL_FORMAT_ISL)
2021 return print_union_pw_qpolynomial_fold_isl(p, upwf);
2022 isl_die(p->ctx, isl_error_invalid,
2023 "invalid output format for isl_union_pw_qpolynomial_fold",
2026 isl_printer_free(p);