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
16 #include <isl_polynomial_private.h>
17 #include <isl_printer_private.h>
19 static const char *s_to[2] = { " -> ", " \\to " };
20 static const char *s_and[2] = { " and ", " \\wedge " };
21 static const char *s_or[2] = { " or ", " \\vee " };
22 static const char *s_le[2] = { "<=", "\\le" };
23 static const char *s_ge[2] = { ">=", "\\ge" };
24 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
25 static const char *s_close_set[2] = { " }", " \\,\\}" };
26 static const char *s_open_list[2] = { "[", "(" };
27 static const char *s_close_list[2] = { "]", ")" };
28 static const char *s_such_that[2] = { " : ", " \\mid " };
29 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
30 static const char *s_close_exists[2] = { ")", "" };
31 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
32 static const char *s_param_prefix[2] = { "p", "p_" };
33 static const char *s_input_prefix[2] = { "i", "i_" };
34 static const char *s_output_prefix[2] = { "o", "o_" };
36 static __isl_give isl_printer *print_constraint_polylib(
37 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
40 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
41 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
42 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
43 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
45 p = isl_printer_start_line(p);
46 p = isl_printer_print_int(p, ineq);
47 for (i = 0; i < n_out; ++i) {
48 p = isl_printer_print_str(p, " ");
49 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
51 for (i = 0; i < n_in; ++i) {
52 p = isl_printer_print_str(p, " ");
53 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
55 for (i = 0; i < bmap->n_div; ++i) {
56 p = isl_printer_print_str(p, " ");
57 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
59 for (i = 0; i < nparam; ++i) {
60 p = isl_printer_print_str(p, " ");
61 p = isl_printer_print_isl_int(p, c[1+i]);
63 p = isl_printer_print_str(p, " ");
64 p = isl_printer_print_isl_int(p, c[0]);
65 p = isl_printer_end_line(p);
69 static __isl_give isl_printer *print_constraints_polylib(
70 struct isl_basic_map *bmap, __isl_take isl_printer *p)
74 p = isl_printer_set_isl_int_width(p, 5);
76 for (i = 0; i < bmap->n_eq; ++i)
77 p = print_constraint_polylib(bmap, 0, i, p);
78 for (i = 0; i < bmap->n_ineq; ++i)
79 p = print_constraint_polylib(bmap, 1, i, p);
84 static __isl_give isl_printer *bset_print_constraints_polylib(
85 struct isl_basic_set *bset, __isl_take isl_printer *p)
87 return print_constraints_polylib((struct isl_basic_map *)bset, p);
90 static __isl_give isl_printer *isl_basic_map_print_polylib(
91 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
93 unsigned total = isl_basic_map_total_dim(bmap);
94 p = isl_printer_start_line(p);
95 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
96 p = isl_printer_print_str(p, " ");
97 p = isl_printer_print_int(p, 1 + total + 1);
98 p = isl_printer_end_line(p);
99 return print_constraints_polylib(bmap, p);
102 static __isl_give isl_printer *isl_basic_set_print_polylib(
103 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
105 return isl_basic_map_print_polylib((struct isl_basic_map *)bset, p);
108 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
109 __isl_take isl_printer *p)
113 p = isl_printer_start_line(p);
114 p = isl_printer_print_int(p, map->n);
115 p = isl_printer_end_line(p);
116 for (i = 0; i < map->n; ++i) {
117 p = isl_printer_start_line(p);
118 p = isl_printer_end_line(p);
119 p = isl_basic_map_print_polylib(map->p[i], p);
124 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
125 __isl_take isl_printer *p)
127 return isl_map_print_polylib((struct isl_map *)set, p);
130 static int count_same_name(__isl_keep isl_dim *dim,
131 enum isl_dim_type type, unsigned pos, const char *name)
137 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
138 s = t == type ? pos : isl_dim_size(dim, t);
139 for (p = 0; p < s; ++p) {
140 const char *n = isl_dim_get_name(dim, t, p);
141 if (n && !strcmp(n, name))
148 static __isl_give isl_printer *print_name(__isl_keep isl_dim *dim,
149 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
156 name = type == isl_dim_div ? NULL : isl_dim_get_name(dim, type, pos);
160 if (type == isl_dim_param)
161 prefix = s_param_prefix[latex];
162 else if (type == isl_dim_div)
163 prefix = s_div_prefix[latex];
164 else if (set || type == isl_dim_in)
165 prefix = s_input_prefix[latex];
167 prefix = s_output_prefix[latex];
168 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
171 primes = count_same_name(dim, name == buffer ? isl_dim_div : type,
173 p = isl_printer_print_str(p, name);
175 p = isl_printer_print_str(p, "'");
179 static __isl_give isl_printer *print_var_list(__isl_keep isl_dim *dim,
180 __isl_take isl_printer *p, enum isl_dim_type type, int set, int latex)
184 for (i = 0; i < isl_dim_size(dim, type); ++i) {
186 p = isl_printer_print_str(p, ", ");
187 p = print_name(dim, p, type, i, set, latex);
192 static __isl_give isl_printer *print_tuple(__isl_keep isl_dim *dim,
193 __isl_take isl_printer *p, enum isl_dim_type type, int set, int latex)
195 unsigned n = isl_dim_size(dim, type);
196 if (!latex || n != 1)
197 p = isl_printer_print_str(p, s_open_list[latex]);
198 p = print_var_list(dim, p, type, set, latex);
199 if (!latex || n != 1)
200 p = isl_printer_print_str(p, s_close_list[latex]);
204 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_dim *dim,
205 __isl_take isl_printer *p)
207 if (isl_dim_size(dim, isl_dim_param) == 0)
210 p = isl_printer_start_line(p);
211 p = isl_printer_print_str(p, "symbolic ");
212 p = print_var_list(dim, p, isl_dim_param, 0, 0);
213 p = isl_printer_print_str(p, ";");
214 p = isl_printer_end_line(p);
218 static enum isl_dim_type pos2type(__isl_keep isl_dim *dim, unsigned *pos)
220 enum isl_dim_type type;
221 unsigned n_in = isl_dim_size(dim, isl_dim_in);
222 unsigned n_out = isl_dim_size(dim, isl_dim_out);
223 unsigned nparam = isl_dim_size(dim, isl_dim_param);
225 if (*pos < 1 + nparam) {
226 type = isl_dim_param;
228 } else if (*pos < 1 + nparam + n_in) {
231 } else if (*pos < 1 + nparam + n_in + n_out) {
233 *pos -= 1 + nparam + n_in;
236 *pos -= 1 + nparam + n_in + n_out;
242 static __isl_give isl_printer *print_term(__isl_keep isl_dim *dim,
243 isl_int c, unsigned pos, __isl_take isl_printer *p, int set, int latex)
245 enum isl_dim_type type;
248 return isl_printer_print_isl_int(p, c);
250 if (isl_int_is_one(c))
252 else if (isl_int_is_negone(c))
253 p = isl_printer_print_str(p, "-");
255 p = isl_printer_print_isl_int(p, c);
256 type = pos2type(dim, &pos);
257 p = print_name(dim, p, type, pos, set, latex);
261 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_dim *dim,
262 __isl_take isl_printer *p, isl_int *c, int len, int set)
267 for (i = 0, first = 1; i < len; ++i) {
269 if (isl_int_is_zero(c[i]))
272 if (isl_int_is_neg(c[i])) {
274 isl_int_neg(c[i], c[i]);
275 p = isl_printer_print_str(p, " - ");
277 p = isl_printer_print_str(p, " + ");
280 p = print_term(dim, c[i], i, p, set, 0);
282 isl_int_neg(c[i], c[i]);
285 p = isl_printer_print_str(p, "0");
289 static __isl_give isl_printer *print_affine(__isl_keep isl_basic_map *bmap,
290 __isl_keep isl_dim *dim, __isl_take isl_printer *p, isl_int *c, int set)
292 unsigned len = 1 + isl_basic_map_total_dim(bmap);
293 return print_affine_of_len(dim, p, c, len, set);
296 static __isl_give isl_printer *print_constraint(struct isl_basic_map *bmap,
297 __isl_keep isl_dim *dim, __isl_take isl_printer *p,
298 isl_int *c, int last, const char *op, int first_constraint, int set,
301 if (!first_constraint)
302 p = isl_printer_print_str(p, s_and[latex]);
304 isl_int_abs(c[last], c[last]);
306 p = print_term(dim, c[last], last, p, set, latex);
308 p = isl_printer_print_str(p, " ");
309 p = isl_printer_print_str(p, op);
310 p = isl_printer_print_str(p, " ");
312 isl_int_set_si(c[last], 0);
313 p = print_affine(bmap, dim, p, c, set);
318 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
319 __isl_keep isl_dim *dim, __isl_take isl_printer *p, int set, int latex)
323 unsigned total = isl_basic_map_total_dim(bmap);
325 c = isl_vec_alloc(bmap->ctx, 1 + total);
329 for (i = bmap->n_eq - 1; i >= 0; --i) {
330 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
331 isl_assert(bmap->ctx, l >= 0, goto error);
332 if (isl_int_is_neg(bmap->eq[i][l]))
333 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
335 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
336 p = print_constraint(bmap, dim, p, c->el, l,
337 "=", i == bmap->n_eq - 1, set, latex);
339 for (i = 0; i < bmap->n_ineq; ++i) {
340 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
343 isl_assert(bmap->ctx, l >= 0, goto error);
344 s = isl_int_sgn(bmap->ineq[i][l]);
346 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
348 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
349 op = s < 0 ? s_le[latex] : s_ge[latex];
350 p = print_constraint(bmap, dim, p, c->el, l,
351 op, !bmap->n_eq && !i, set, latex);
362 static __isl_give isl_printer *print_omega_constraints(
363 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int set)
365 if (bmap->n_eq + bmap->n_ineq == 0)
368 p = isl_printer_print_str(p, ": ");
369 if (bmap->n_div > 0) {
371 p = isl_printer_print_str(p, "exists (");
372 for (i = 0; i < bmap->n_div; ++i) {
374 p = isl_printer_print_str(p, ", ");
375 p = print_name(bmap->dim, p, isl_dim_div, i, 0, 0);
377 p = isl_printer_print_str(p, ": ");
379 p = print_constraints(bmap, bmap->dim, p, set, 0);
381 p = isl_printer_print_str(p, ")");
385 static __isl_give isl_printer *basic_map_print_omega(
386 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
388 p = isl_printer_print_str(p, "{ [");
389 p = print_var_list(bmap->dim, p, isl_dim_in, 0, 0);
390 p = isl_printer_print_str(p, "] -> [");
391 p = print_var_list(bmap->dim, p, isl_dim_out, 0, 0);
392 p = isl_printer_print_str(p, "] ");
393 p = print_omega_constraints(bmap, p, 0);
394 p = isl_printer_print_str(p, " }");
398 static __isl_give isl_printer *isl_basic_map_print_omega(
399 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
401 p = print_omega_parameters(bmap->dim, p);
403 p = isl_printer_start_line(p);
404 p = basic_map_print_omega(bmap, p);
405 p = isl_printer_end_line(p);
409 static __isl_give isl_printer *basic_set_print_omega(
410 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
412 p = isl_printer_print_str(p, "{ [");
413 p = print_var_list(bset->dim, p, isl_dim_set, 1, 0);
414 p = isl_printer_print_str(p, "] ");
415 p = print_omega_constraints((isl_basic_map *)bset, p, 1);
416 p = isl_printer_print_str(p, " }");
420 static __isl_give isl_printer *isl_basic_set_print_omega(
421 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
423 p = print_omega_parameters(bset->dim, p);
425 p = isl_printer_start_line(p);
426 p = basic_set_print_omega(bset, p);
427 p = isl_printer_end_line(p);
431 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
432 __isl_take isl_printer *p)
436 p = print_omega_parameters(map->dim, p);
438 p = isl_printer_start_line(p);
439 for (i = 0; i < map->n; ++i) {
441 p = isl_printer_print_str(p, " union ");
442 p = basic_map_print_omega(map->p[i], p);
444 p = isl_printer_end_line(p);
448 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
449 __isl_take isl_printer *p)
453 p = print_omega_parameters(set->dim, p);
455 p = isl_printer_start_line(p);
456 for (i = 0; i < set->n; ++i) {
458 p = isl_printer_print_str(p, " union ");
459 p = basic_set_print_omega(set->p[i], p);
461 p = isl_printer_end_line(p);
465 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
466 __isl_keep isl_dim *dim, __isl_take isl_printer *p, int set, int latex)
468 if (bmap->n_div > 0) {
470 p = isl_printer_print_str(p, s_open_exists[latex]);
471 for (i = 0; i < bmap->n_div; ++i) {
473 p = isl_printer_print_str(p, ", ");
474 p = print_name(dim, p, isl_dim_div, i, 0, latex);
475 if (latex || isl_int_is_zero(bmap->div[i][0]))
477 p = isl_printer_print_str(p, " = [(");
478 p = print_affine(bmap, dim, p, bmap->div[i] + 1, set);
479 p = isl_printer_print_str(p, ")/");
480 p = isl_printer_print_isl_int(p, bmap->div[i][0]);
481 p = isl_printer_print_str(p, "]");
483 p = isl_printer_print_str(p, ": ");
486 p = print_constraints(bmap, dim, p, set, latex);
489 p = isl_printer_print_str(p, s_close_exists[latex]);
493 static __isl_give isl_printer *isl_basic_map_print_isl(
494 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int latex)
498 p = isl_printer_start_line(p);
499 if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
500 p = print_tuple(bmap->dim, p, isl_dim_param, 0, latex);
501 p = isl_printer_print_str(p, " -> ");
503 p = isl_printer_print_str(p, "{ ");
504 p = print_tuple(bmap->dim, p, isl_dim_in, 0, latex);
505 p = isl_printer_print_str(p, " -> ");
506 p = print_tuple(bmap->dim, p, isl_dim_out, 0, latex);
507 p = isl_printer_print_str(p, " : ");
508 p = print_disjunct(bmap, bmap->dim, p, 0, latex);
509 p = isl_printer_print_str(p, " }");
510 p = isl_printer_end_line(p);
514 static __isl_give isl_printer *isl_basic_set_print_isl(
515 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int latex)
519 p = isl_printer_start_line(p);
520 if (isl_basic_set_dim(bset, isl_dim_param) > 0) {
521 p = print_tuple(bset->dim, p, isl_dim_param, 0, latex);
522 p = isl_printer_print_str(p, " -> ");
524 p = isl_printer_print_str(p, "{ ");
525 p = print_tuple(bset->dim, p, isl_dim_set, 1, latex);
526 p = isl_printer_print_str(p, " : ");
527 p = print_disjunct((isl_basic_map *)bset, bset->dim, p, 1, latex);
528 p = isl_printer_print_str(p, " }");
529 p = isl_printer_end_line(p);
533 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
534 __isl_take isl_printer *p, int set, int latex)
538 if (isl_map_fast_is_universe(map))
541 p = isl_printer_print_str(p, s_such_that[latex]);
543 p = isl_printer_print_str(p, "1 = 0");
544 for (i = 0; i < map->n; ++i) {
546 p = isl_printer_print_str(p, s_or[latex]);
547 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
548 p = isl_printer_print_str(p, "(");
549 p = print_disjunct(map->p[i], map->dim, p, set, latex);
550 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
551 p = isl_printer_print_str(p, ")");
556 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
557 __isl_take isl_printer *p, int latex)
559 if (isl_map_dim(map, isl_dim_param) > 0) {
560 p = print_tuple(map->dim, p, isl_dim_param, 0, latex);
561 p = isl_printer_print_str(p, s_to[latex]);
563 p = isl_printer_print_str(p, s_open_set[latex]);
564 p = print_tuple(map->dim, p, isl_dim_in, 0, latex);
565 p = isl_printer_print_str(p, s_to[latex]);
566 p = print_tuple(map->dim, p, isl_dim_out, 0, latex);
567 p = print_disjuncts(map, p, 0, latex);
568 p = isl_printer_print_str(p, s_close_set[latex]);
572 static __isl_give isl_printer *isl_set_print_isl(__isl_keep isl_set *set,
573 __isl_take isl_printer *p, int latex)
577 if (isl_set_dim(set, isl_dim_param) > 0) {
578 p = print_tuple(set->dim, p, isl_dim_param, 0, latex);
579 p = isl_printer_print_str(p, " -> ");
581 p = isl_printer_print_str(p, "{ ");
582 p = print_tuple(set->dim, p, isl_dim_set, 1, latex);
583 p = print_disjuncts((isl_map *)set, p, 1, latex);
584 p = isl_printer_print_str(p, " }");
588 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
589 __isl_keep isl_basic_map *bmap)
593 if (p->output_format == ISL_FORMAT_ISL)
594 return isl_basic_map_print_isl(bmap, p, 0);
595 else if (p->output_format == ISL_FORMAT_OMEGA)
596 return isl_basic_map_print_omega(bmap, p);
597 isl_assert(bmap->ctx, 0, goto error);
603 void isl_basic_map_print(__isl_keep isl_basic_map *bmap, FILE *out, int indent,
604 const char *prefix, const char *suffix, unsigned output_format)
606 isl_printer *printer;
611 printer = isl_printer_to_file(bmap->ctx, out);
612 printer = isl_printer_set_indent(printer, indent);
613 printer = isl_printer_set_prefix(printer, prefix);
614 printer = isl_printer_set_suffix(printer, suffix);
615 printer = isl_printer_set_output_format(printer, output_format);
616 isl_printer_print_basic_map(printer, bmap);
618 isl_printer_free(printer);
621 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
622 __isl_keep isl_basic_set *bset)
627 if (p->output_format == ISL_FORMAT_ISL)
628 return isl_basic_set_print_isl(bset, p, 0);
629 else if (p->output_format == ISL_FORMAT_POLYLIB)
630 return isl_basic_set_print_polylib(bset, p);
631 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
632 return bset_print_constraints_polylib(bset, p);
633 else if (p->output_format == ISL_FORMAT_OMEGA)
634 return isl_basic_set_print_omega(bset, p);
635 isl_assert(p->ctx, 0, goto error);
641 void isl_basic_set_print(struct isl_basic_set *bset, FILE *out, int indent,
642 const char *prefix, const char *suffix, unsigned output_format)
644 isl_printer *printer;
649 printer = isl_printer_to_file(bset->ctx, out);
650 printer = isl_printer_set_indent(printer, indent);
651 printer = isl_printer_set_prefix(printer, prefix);
652 printer = isl_printer_set_suffix(printer, suffix);
653 printer = isl_printer_set_output_format(printer, output_format);
654 isl_printer_print_basic_set(printer, bset);
656 isl_printer_free(printer);
659 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
660 __isl_keep isl_set *set)
664 if (p->output_format == ISL_FORMAT_ISL)
665 return isl_set_print_isl(set, p, 0);
666 else if (p->output_format == ISL_FORMAT_POLYLIB)
667 return isl_set_print_polylib(set, p);
668 else if (p->output_format == ISL_FORMAT_OMEGA)
669 return isl_set_print_omega(set, p);
670 isl_assert(set->ctx, 0, goto error);
676 void isl_set_print(struct isl_set *set, FILE *out, int indent,
677 unsigned output_format)
679 isl_printer *printer;
684 printer = isl_printer_to_file(set->ctx, out);
685 printer = isl_printer_set_indent(printer, indent);
686 printer = isl_printer_set_output_format(printer, output_format);
687 printer = isl_printer_print_set(printer, set);
689 isl_printer_free(printer);
692 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
693 __isl_keep isl_map *map)
698 if (p->output_format == ISL_FORMAT_ISL)
699 return isl_map_print_isl(map, p, 0);
700 else if (p->output_format == ISL_FORMAT_POLYLIB)
701 return isl_map_print_polylib(map, p);
702 else if (p->output_format == ISL_FORMAT_OMEGA)
703 return isl_map_print_omega(map, p);
704 else if (p->output_format == ISL_FORMAT_LATEX)
705 return isl_map_print_isl(map, p, 1);
706 isl_assert(map->ctx, 0, goto error);
712 void isl_map_print(__isl_keep isl_map *map, FILE *out, int indent,
713 unsigned output_format)
715 isl_printer *printer;
720 printer = isl_printer_to_file(map->ctx, out);
721 printer = isl_printer_set_indent(printer, indent);
722 printer = isl_printer_set_output_format(printer, output_format);
723 printer = isl_printer_print_map(printer, map);
725 isl_printer_free(printer);
728 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
733 for (i = 0, n = 0; i < rec->n; ++i)
734 if (!isl_upoly_is_zero(rec->p[i]))
740 static __isl_give isl_printer *print_div(__isl_keep isl_dim *dim,
741 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
743 int c = p->output_format == ISL_FORMAT_C;
744 p = isl_printer_print_str(p, c ? "floord(" : "[(");
745 p = print_affine_of_len(dim, p, div->row[pos] + 1, div->n_col - 1, 1);
746 p = isl_printer_print_str(p, c ? ", " : ")/");
747 p = isl_printer_print_isl_int(p, div->row[pos][0]);
748 p = isl_printer_print_str(p, c ? ")" : "]");
752 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
753 __isl_take isl_printer *p, int first)
755 struct isl_upoly_cst *cst;
758 cst = isl_upoly_as_cst(up);
761 neg = !first && isl_int_is_neg(cst->n);
763 p = isl_printer_print_str(p, neg ? " - " : " + ");
765 isl_int_neg(cst->n, cst->n);
766 if (isl_int_is_zero(cst->d)) {
767 int sgn = isl_int_sgn(cst->n);
768 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
769 sgn == 0 ? "NaN" : "infty");
771 p = isl_printer_print_isl_int(p, cst->n);
773 isl_int_neg(cst->n, cst->n);
774 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
775 p = isl_printer_print_str(p, "/");
776 p = isl_printer_print_isl_int(p, cst->d);
784 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
785 __isl_keep isl_dim *dim, __isl_keep isl_mat *div, int var)
789 total = isl_dim_total(dim);
791 p = print_term(dim, dim->ctx->one, 1 + var, p, 1, 0);
793 p = print_div(dim, div, var - total, p);
797 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
798 __isl_keep isl_dim *dim, __isl_keep isl_mat *div, int var, int exp)
800 p = print_base(p, dim, div, var);
803 if (p->output_format == ISL_FORMAT_C) {
805 for (i = 1; i < exp; ++i) {
806 p = isl_printer_print_str(p, "*");
807 p = print_base(p, dim, div, var);
810 p = isl_printer_print_str(p, "^");
811 p = isl_printer_print_int(p, exp);
816 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
817 __isl_keep isl_dim *dim, __isl_keep isl_mat *div,
818 __isl_take isl_printer *p)
821 struct isl_upoly_rec *rec;
823 if (!p || !up || !dim || !div)
826 if (isl_upoly_is_cst(up))
827 return upoly_print_cst(up, p, 1);
829 rec = isl_upoly_as_rec(up);
832 n = upoly_rec_n_non_zero(rec);
834 p = isl_printer_print_str(p, "(");
835 for (i = 0, first = 1; i < rec->n; ++i) {
836 if (isl_upoly_is_zero(rec->p[i]))
838 if (isl_upoly_is_negone(rec->p[i])) {
840 p = isl_printer_print_str(p, "-1");
842 p = isl_printer_print_str(p, "-");
844 p = isl_printer_print_str(p, " - ");
845 } else if (isl_upoly_is_cst(rec->p[i]) &&
846 !isl_upoly_is_one(rec->p[i]))
847 p = upoly_print_cst(rec->p[i], p, first);
850 p = isl_printer_print_str(p, " + ");
851 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
852 p = upoly_print(rec->p[i], dim, div, p);
857 if (!isl_upoly_is_one(rec->p[i]) &&
858 !isl_upoly_is_negone(rec->p[i]))
859 p = isl_printer_print_str(p, " * ");
860 p = print_pow(p, dim, div, rec->up.var, i);
863 p = isl_printer_print_str(p, ")");
870 __isl_give isl_printer *isl_printer_print_qpolynomial(__isl_take isl_printer *p,
871 __isl_keep isl_qpolynomial *qp)
875 p = upoly_print(qp->upoly, qp->dim, qp->div, p);
882 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
883 unsigned output_format)
890 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
891 p = isl_printer_to_file(qp->dim->ctx, out);
892 p = isl_printer_print_qpolynomial(p, qp);
896 static __isl_give isl_printer *qpolynomial_fold_print(
897 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
901 if (fold->type == isl_fold_min)
902 p = isl_printer_print_str(p, "min");
903 else if (fold->type == isl_fold_max)
904 p = isl_printer_print_str(p, "max");
905 p = isl_printer_print_str(p, "(");
906 for (i = 0; i < fold->n; ++i) {
908 p = isl_printer_print_str(p, ", ");
909 p = isl_printer_print_qpolynomial(p, fold->qp[i]);
911 p = isl_printer_print_str(p, ")");
915 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
916 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
920 p = qpolynomial_fold_print(fold, p);
927 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
928 FILE *out, unsigned output_format)
935 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
937 p = isl_printer_to_file(fold->dim->ctx, out);
938 p = isl_printer_print_qpolynomial_fold(p, fold);
943 static __isl_give isl_printer *print_pw_qpolynomial_isl(
944 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
951 if (isl_dim_size(pwqp->dim, isl_dim_param) > 0) {
952 p = print_tuple(pwqp->dim, p, isl_dim_param, 0, 0);
953 p = isl_printer_print_str(p, " -> ");
955 p = isl_printer_print_str(p, "{ ");
957 if (isl_dim_size(pwqp->dim, isl_dim_set) > 0) {
958 p = print_tuple(pwqp->dim, p, isl_dim_set, 1, 0);
959 p = isl_printer_print_str(p, " -> ");
961 p = isl_printer_print_str(p, "0");
963 for (i = 0; i < pwqp->n; ++i) {
965 p = isl_printer_print_str(p, "; ");
966 if (isl_dim_size(pwqp->p[i].set->dim, isl_dim_set) > 0) {
967 p = print_tuple(pwqp->p[i].set->dim, p, isl_dim_set, 1, 0);
968 p = isl_printer_print_str(p, " -> ");
970 p = isl_printer_print_qpolynomial(p, pwqp->p[i].qp);
971 p = print_disjuncts((isl_map *)pwqp->p[i].set, p, 1, 0);
973 p = isl_printer_print_str(p, " }");
980 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
981 unsigned output_format)
988 p = isl_printer_to_file(pwqp->dim->ctx, out);
989 p = isl_printer_set_output_format(p, output_format);
990 p = isl_printer_print_pw_qpolynomial(p, pwqp);
995 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
996 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1000 if (isl_dim_size(pwf->dim, isl_dim_param) > 0) {
1001 p = print_tuple(pwf->dim, p, isl_dim_param, 0, 0);
1002 p = isl_printer_print_str(p, " -> ");
1004 p = isl_printer_print_str(p, "{ ");
1006 if (isl_dim_size(pwf->dim, isl_dim_set) > 0) {
1007 p = print_tuple(pwf->dim, p, isl_dim_set, 0, 0);
1008 p = isl_printer_print_str(p, " -> ");
1010 p = isl_printer_print_str(p, "0");
1012 for (i = 0; i < pwf->n; ++i) {
1014 p = isl_printer_print_str(p, "; ");
1015 if (isl_dim_size(pwf->p[i].set->dim, isl_dim_set) > 0) {
1016 p = print_tuple(pwf->p[i].set->dim, p, isl_dim_set, 0, 0);
1017 p = isl_printer_print_str(p, " -> ");
1019 p = qpolynomial_fold_print(pwf->p[i].fold, p);
1020 p = print_disjuncts((isl_map *)pwf->p[i].set, p, 1, 0);
1022 p = isl_printer_print_str(p, " }");
1026 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1027 __isl_keep isl_basic_set *bset, isl_int *c);
1029 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1030 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1032 if (type == isl_dim_div) {
1033 p = isl_printer_print_str(p, "floord(");
1034 p = print_affine_c(p, bset, bset->div[pos] + 1);
1035 p = isl_printer_print_str(p, ", ");
1036 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1037 p = isl_printer_print_str(p, ")");
1041 name = isl_dim_get_name(bset->dim, type, pos);
1044 p = isl_printer_print_str(p, name);
1049 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1050 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1052 enum isl_dim_type type;
1053 unsigned nparam = isl_basic_set_dim(bset, isl_dim_param);
1056 return isl_printer_print_isl_int(p, c);
1058 if (isl_int_is_one(c))
1060 else if (isl_int_is_negone(c))
1061 p = isl_printer_print_str(p, "-");
1063 p = isl_printer_print_isl_int(p, c);
1064 p = isl_printer_print_str(p, "*");
1066 type = pos2type(bset->dim, &pos);
1067 p = print_name_c(p, bset, type, pos);
1071 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1072 __isl_keep isl_basic_set *bset, isl_int *c)
1076 unsigned len = 1 + isl_basic_set_total_dim(bset);
1078 for (i = 0, first = 1; i < len; ++i) {
1080 if (isl_int_is_zero(c[i]))
1083 if (isl_int_is_neg(c[i])) {
1085 isl_int_neg(c[i], c[i]);
1086 p = isl_printer_print_str(p, " - ");
1088 p = isl_printer_print_str(p, " + ");
1091 p = print_term(bset->dim, c[i], i, p, 1, 0);
1093 isl_int_neg(c[i], c[i]);
1096 p = isl_printer_print_str(p, "0");
1100 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1101 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int first)
1104 p = isl_printer_print_str(p, " && ");
1106 p = print_affine_c(p, bset, c);
1107 p = isl_printer_print_str(p, " ");
1108 p = isl_printer_print_str(p, op);
1109 p = isl_printer_print_str(p, " 0");
1113 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1114 __isl_keep isl_basic_set *bset)
1118 for (i = 0; i < bset->n_eq; ++i)
1119 p = print_constraint_c(p, bset, bset->eq[i], "==", !i);
1120 for (i = 0; i < bset->n_ineq; ++i)
1121 p = print_constraint_c(p, bset, bset->ineq[i], ">=",
1126 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
1127 __isl_keep isl_set *set)
1132 p = isl_printer_print_str(p, "0");
1134 for (i = 0; i < set->n; ++i) {
1136 p = isl_printer_print_str(p, " || ");
1138 p = isl_printer_print_str(p, "(");
1139 p = print_basic_set_c(p, set->p[i]);
1141 p = isl_printer_print_str(p, ")");
1146 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1147 __isl_keep isl_qpolynomial *qp)
1152 isl_qpolynomial_get_den(qp, &den);
1153 if (!isl_int_is_one(den)) {
1155 p = isl_printer_print_str(p, "(");
1156 qp = isl_qpolynomial_copy(qp);
1157 f = isl_qpolynomial_rat_cst(isl_dim_copy(qp->dim),
1158 den, qp->dim->ctx->one);
1159 qp = isl_qpolynomial_mul(qp, f);
1162 p = upoly_print(qp->upoly, qp->dim, qp->div, p);
1163 if (!isl_int_is_one(den)) {
1164 p = isl_printer_print_str(p, ")/");
1165 p = isl_printer_print_isl_int(p, den);
1166 isl_qpolynomial_free(qp);
1172 static __isl_give isl_printer *print_pw_qpolynomial_c(
1173 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwpq)
1177 if (pwpq->n == 1 && isl_set_fast_is_universe(pwpq->p[0].set))
1178 return print_qpolynomial_c(p, pwpq->p[0].qp);
1180 for (i = 0; i < pwpq->n; ++i) {
1181 p = isl_printer_print_str(p, "(");
1182 p = print_set_c(p, pwpq->p[i].set);
1183 p = isl_printer_print_str(p, ") ? (");
1184 p = print_qpolynomial_c(p, pwpq->p[i].qp);
1185 p = isl_printer_print_str(p, ") : ");
1188 p = isl_printer_print_str(p, "0");
1192 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
1193 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1198 if (p->output_format == ISL_FORMAT_ISL)
1199 return print_pw_qpolynomial_isl(p, pwqp);
1200 else if (p->output_format == ISL_FORMAT_C)
1201 return print_pw_qpolynomial_c(p, pwqp);
1202 isl_assert(p->ctx, 0, goto error);
1204 isl_printer_free(p);
1208 static __isl_give isl_printer *print_qpolynomial_fold_c(
1209 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
1213 for (i = 0; i < fold->n - 1; ++i)
1214 if (fold->type == isl_fold_min)
1215 p = isl_printer_print_str(p, "min(");
1216 else if (fold->type == isl_fold_max)
1217 p = isl_printer_print_str(p, "max(");
1219 for (i = 0; i < fold->n; ++i) {
1221 p = isl_printer_print_str(p, ", ");
1222 p = print_qpolynomial_c(p, fold->qp[i]);
1224 p = isl_printer_print_str(p, ")");
1229 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
1230 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1234 if (pwf->n == 1 && isl_set_fast_is_universe(pwf->p[0].set))
1235 return print_qpolynomial_fold_c(p, pwf->p[0].fold);
1237 for (i = 0; i < pwf->n; ++i) {
1238 p = isl_printer_print_str(p, "(");
1239 p = print_set_c(p, pwf->p[i].set);
1240 p = isl_printer_print_str(p, ") ? (");
1241 p = print_qpolynomial_fold_c(p, pwf->p[i].fold);
1242 p = isl_printer_print_str(p, ") : ");
1245 p = isl_printer_print_str(p, "0");
1249 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
1250 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1255 if (p->output_format == ISL_FORMAT_ISL)
1256 return print_pw_qpolynomial_fold_isl(p, pwf);
1257 else if (p->output_format == ISL_FORMAT_C)
1258 return print_pw_qpolynomial_fold_c(p, pwf);
1259 isl_assert(p->ctx, 0, goto error);
1261 isl_printer_free(p);
1265 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
1266 FILE *out, unsigned output_format)
1273 p = isl_printer_to_file(pwf->dim->ctx, out);
1274 p = isl_printer_set_output_format(p, output_format);
1275 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
1277 isl_printer_free(p);