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 __isl_give isl_printer *print_constraint_polylib(
20 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
23 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
24 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
25 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
26 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
28 p = isl_printer_start_line(p);
29 p = isl_printer_print_int(p, ineq);
30 for (i = 0; i < n_out; ++i) {
31 p = isl_printer_print_str(p, " ");
32 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
34 for (i = 0; i < n_in; ++i) {
35 p = isl_printer_print_str(p, " ");
36 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
38 for (i = 0; i < bmap->n_div; ++i) {
39 p = isl_printer_print_str(p, " ");
40 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
42 for (i = 0; i < nparam; ++i) {
43 p = isl_printer_print_str(p, " ");
44 p = isl_printer_print_isl_int(p, c[1+i]);
46 p = isl_printer_print_str(p, " ");
47 p = isl_printer_print_isl_int(p, c[0]);
48 p = isl_printer_end_line(p);
52 static __isl_give isl_printer *print_constraints_polylib(
53 struct isl_basic_map *bmap, __isl_take isl_printer *p)
57 p = isl_printer_set_isl_int_width(p, 5);
59 for (i = 0; i < bmap->n_eq; ++i)
60 p = print_constraint_polylib(bmap, 0, i, p);
61 for (i = 0; i < bmap->n_ineq; ++i)
62 p = print_constraint_polylib(bmap, 1, i, p);
67 static __isl_give isl_printer *bset_print_constraints_polylib(
68 struct isl_basic_set *bset, __isl_take isl_printer *p)
70 return print_constraints_polylib((struct isl_basic_map *)bset, p);
73 static __isl_give isl_printer *isl_basic_map_print_polylib(
74 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
76 unsigned total = isl_basic_map_total_dim(bmap);
77 p = isl_printer_start_line(p);
78 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
79 p = isl_printer_print_str(p, " ");
80 p = isl_printer_print_int(p, 1 + total + 1);
81 p = isl_printer_end_line(p);
82 return print_constraints_polylib(bmap, p);
85 static __isl_give isl_printer *isl_basic_set_print_polylib(
86 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
88 return isl_basic_map_print_polylib((struct isl_basic_map *)bset, p);
91 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
92 __isl_take isl_printer *p)
96 p = isl_printer_start_line(p);
97 p = isl_printer_print_int(p, map->n);
98 p = isl_printer_end_line(p);
99 for (i = 0; i < map->n; ++i) {
100 p = isl_printer_start_line(p);
101 p = isl_printer_end_line(p);
102 p = isl_basic_map_print_polylib(map->p[i], p);
107 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
108 __isl_take isl_printer *p)
110 return isl_map_print_polylib((struct isl_map *)set, p);
113 static int count_same_name(__isl_keep isl_dim *dim,
114 enum isl_dim_type type, unsigned pos, const char *name)
120 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
121 s = t == type ? pos : isl_dim_size(dim, t);
122 for (p = 0; p < s; ++p) {
123 const char *n = isl_dim_get_name(dim, t, p);
124 if (n && !strcmp(n, name))
131 static __isl_give isl_printer *print_name(__isl_keep isl_dim *dim,
132 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos, int set)
138 name = type == isl_dim_div ? NULL : isl_dim_get_name(dim, type, pos);
142 if (type == isl_dim_param)
144 else if (type == isl_dim_div)
146 else if (set || type == isl_dim_in)
150 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
153 primes = count_same_name(dim, name == buffer ? isl_dim_div : type,
155 p = isl_printer_print_str(p, name);
157 p = isl_printer_print_str(p, "'");
161 static __isl_give isl_printer *print_var_list(__isl_keep isl_dim *dim,
162 __isl_take isl_printer *p, enum isl_dim_type type, int set)
166 for (i = 0; i < isl_dim_size(dim, type); ++i) {
168 p = isl_printer_print_str(p, ", ");
169 p = print_name(dim, p, type, i, set);
174 static __isl_give isl_printer *print_tuple(__isl_keep isl_dim *dim,
175 __isl_take isl_printer *p, enum isl_dim_type type, int set)
177 p = isl_printer_print_str(p, "[");
178 p = print_var_list(dim, p, type, set);
179 p = isl_printer_print_str(p, "]");
183 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_dim *dim,
184 __isl_take isl_printer *p)
186 if (isl_dim_size(dim, isl_dim_param) == 0)
189 p = isl_printer_start_line(p);
190 p = isl_printer_print_str(p, "symbolic ");
191 p = print_var_list(dim, p, isl_dim_param, 0);
192 p = isl_printer_print_str(p, ";");
193 p = isl_printer_end_line(p);
197 static enum isl_dim_type pos2type(__isl_keep isl_dim *dim, unsigned *pos)
199 enum isl_dim_type type;
200 unsigned n_in = isl_dim_size(dim, isl_dim_in);
201 unsigned n_out = isl_dim_size(dim, isl_dim_out);
202 unsigned nparam = isl_dim_size(dim, isl_dim_param);
204 if (*pos < 1 + nparam) {
205 type = isl_dim_param;
207 } else if (*pos < 1 + nparam + n_in) {
210 } else if (*pos < 1 + nparam + n_in + n_out) {
212 *pos -= 1 + nparam + n_in;
215 *pos -= 1 + nparam + n_in + n_out;
221 static __isl_give isl_printer *print_term(__isl_keep isl_dim *dim,
222 isl_int c, unsigned pos, __isl_take isl_printer *p, int set)
224 enum isl_dim_type type;
227 return isl_printer_print_isl_int(p, c);
229 if (isl_int_is_one(c))
231 else if (isl_int_is_negone(c))
232 p = isl_printer_print_str(p, "-");
234 p = isl_printer_print_isl_int(p, c);
235 type = pos2type(dim, &pos);
236 p = print_name(dim, p, type, pos, set);
240 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_dim *dim,
241 __isl_take isl_printer *p, isl_int *c, int len, int set)
246 for (i = 0, first = 1; i < len; ++i) {
248 if (isl_int_is_zero(c[i]))
251 if (isl_int_is_neg(c[i])) {
253 isl_int_neg(c[i], c[i]);
254 p = isl_printer_print_str(p, " - ");
256 p = isl_printer_print_str(p, " + ");
259 p = print_term(dim, c[i], i, p, set);
261 isl_int_neg(c[i], c[i]);
264 p = isl_printer_print_str(p, "0");
268 static __isl_give isl_printer *print_affine(__isl_keep isl_basic_map *bmap,
269 __isl_keep isl_dim *dim, __isl_take isl_printer *p, isl_int *c, int set)
271 unsigned len = 1 + isl_basic_map_total_dim(bmap);
272 return print_affine_of_len(dim, p, c, len, set);
275 static __isl_give isl_printer *print_constraint(struct isl_basic_map *bmap,
276 __isl_keep isl_dim *dim, __isl_take isl_printer *p,
277 isl_int *c, int last, const char *op, int first_constraint, int set)
279 if (!first_constraint)
280 p = isl_printer_print_str(p, " and ");
282 isl_int_abs(c[last], c[last]);
284 p = print_term(dim, c[last], last, p, set);
286 p = isl_printer_print_str(p, " ");
287 p = isl_printer_print_str(p, op);
288 p = isl_printer_print_str(p, " ");
290 isl_int_set_si(c[last], 0);
291 p = print_affine(bmap, dim, p, c, set);
296 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
297 __isl_keep isl_dim *dim, __isl_take isl_printer *p, int set)
301 unsigned total = isl_basic_map_total_dim(bmap);
303 c = isl_vec_alloc(bmap->ctx, 1 + total);
307 for (i = bmap->n_eq - 1; i >= 0; --i) {
308 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
309 isl_assert(bmap->ctx, l >= 0, goto error);
310 if (isl_int_is_neg(bmap->eq[i][l]))
311 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
313 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
314 p = print_constraint(bmap, dim, p, c->el, l,
315 "=", i == bmap->n_eq - 1, set);
317 for (i = 0; i < bmap->n_ineq; ++i) {
318 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
320 isl_assert(bmap->ctx, l >= 0, goto error);
321 s = isl_int_sgn(bmap->ineq[i][l]);
323 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
325 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
326 p = print_constraint(bmap, dim, p, c->el, l,
327 s < 0 ? "<=" : ">=", !bmap->n_eq && !i, set);
338 static __isl_give isl_printer *print_omega_constraints(
339 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int set)
341 if (bmap->n_eq + bmap->n_ineq == 0)
344 p = isl_printer_print_str(p, ": ");
345 if (bmap->n_div > 0) {
347 p = isl_printer_print_str(p, "exists (");
348 for (i = 0; i < bmap->n_div; ++i) {
350 p = isl_printer_print_str(p, ", ");
351 p = print_name(bmap->dim, p, isl_dim_div, i, 0);
353 p = isl_printer_print_str(p, ": ");
355 p = print_constraints(bmap, bmap->dim, p, set);
357 p = isl_printer_print_str(p, ")");
361 static __isl_give isl_printer *basic_map_print_omega(
362 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
364 p = isl_printer_print_str(p, "{ [");
365 p = print_var_list(bmap->dim, p, isl_dim_in, 0);
366 p = isl_printer_print_str(p, "] -> [");
367 p = print_var_list(bmap->dim, p, isl_dim_out, 0);
368 p = isl_printer_print_str(p, "] ");
369 p = print_omega_constraints(bmap, p, 0);
370 p = isl_printer_print_str(p, " }");
374 static __isl_give isl_printer *isl_basic_map_print_omega(
375 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
377 p = print_omega_parameters(bmap->dim, p);
379 p = isl_printer_start_line(p);
380 p = basic_map_print_omega(bmap, p);
381 p = isl_printer_end_line(p);
385 static __isl_give isl_printer *basic_set_print_omega(
386 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
388 p = isl_printer_print_str(p, "{ [");
389 p = print_var_list(bset->dim, p, isl_dim_set, 1);
390 p = isl_printer_print_str(p, "] ");
391 p = print_omega_constraints((isl_basic_map *)bset, p, 1);
392 p = isl_printer_print_str(p, " }");
396 static __isl_give isl_printer *isl_basic_set_print_omega(
397 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
399 p = print_omega_parameters(bset->dim, p);
401 p = isl_printer_start_line(p);
402 p = basic_set_print_omega(bset, p);
403 p = isl_printer_end_line(p);
407 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
408 __isl_take isl_printer *p)
412 p = print_omega_parameters(map->dim, p);
414 p = isl_printer_start_line(p);
415 for (i = 0; i < map->n; ++i) {
417 p = isl_printer_print_str(p, " union ");
418 p = basic_map_print_omega(map->p[i], p);
420 p = isl_printer_end_line(p);
424 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
425 __isl_take isl_printer *p)
429 p = print_omega_parameters(set->dim, p);
431 p = isl_printer_start_line(p);
432 for (i = 0; i < set->n; ++i) {
434 p = isl_printer_print_str(p, " union ");
435 p = basic_set_print_omega(set->p[i], p);
437 p = isl_printer_end_line(p);
441 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
442 __isl_keep isl_dim *dim, __isl_take isl_printer *p, int set)
444 if (bmap->n_div > 0) {
446 p = isl_printer_print_str(p, "exists (");
447 for (i = 0; i < bmap->n_div; ++i) {
449 p = isl_printer_print_str(p, ", ");
450 p = print_name(dim, p, isl_dim_div, i, 0);
451 if (isl_int_is_zero(bmap->div[i][0]))
453 p = isl_printer_print_str(p, " = [(");
454 p = print_affine(bmap, dim, p, bmap->div[i] + 1, set);
455 p = isl_printer_print_str(p, ")/");
456 p = isl_printer_print_isl_int(p, bmap->div[i][0]);
457 p = isl_printer_print_str(p, "]");
459 p = isl_printer_print_str(p, ": ");
462 p = print_constraints(bmap, dim, p, set);
465 p = isl_printer_print_str(p, ")");
469 static __isl_give isl_printer *isl_basic_map_print_isl(
470 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
474 p = isl_printer_start_line(p);
475 if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
476 p = print_tuple(bmap->dim, p, isl_dim_param, 0);
477 p = isl_printer_print_str(p, " -> ");
479 p = isl_printer_print_str(p, "{ ");
480 p = print_tuple(bmap->dim, p, isl_dim_in, 0);
481 p = isl_printer_print_str(p, " -> ");
482 p = print_tuple(bmap->dim, p, isl_dim_out, 0);
483 p = isl_printer_print_str(p, " : ");
484 p = print_disjunct(bmap, bmap->dim, p, 0);
485 p = isl_printer_end_line(p);
489 static __isl_give isl_printer *isl_basic_set_print_isl(
490 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
494 p = isl_printer_start_line(p);
495 if (isl_basic_set_dim(bset, isl_dim_param) > 0) {
496 p = print_tuple(bset->dim, p, isl_dim_param, 0);
497 p = isl_printer_print_str(p, " -> ");
499 p = isl_printer_print_str(p, "{ ");
500 p = print_tuple(bset->dim, p, isl_dim_set, 1);
501 p = isl_printer_print_str(p, " : ");
502 p = print_disjunct((isl_basic_map *)bset, bset->dim, p, 1);
503 p = isl_printer_end_line(p);
507 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
508 __isl_take isl_printer *p, int set)
512 if (isl_map_fast_is_universe(map))
515 p = isl_printer_print_str(p, " : ");
517 p = isl_printer_print_str(p, "1 = 0");
518 for (i = 0; i < map->n; ++i) {
520 p = isl_printer_print_str(p, " or ");
521 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
522 p = isl_printer_print_str(p, "(");
523 p = print_disjunct(map->p[i], map->dim, p, set);
524 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
525 p = isl_printer_print_str(p, ")");
530 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
531 __isl_take isl_printer *p)
533 if (isl_map_dim(map, isl_dim_param) > 0) {
534 p = print_tuple(map->dim, p, isl_dim_param, 0);
535 p = isl_printer_print_str(p, " -> ");
537 p = isl_printer_print_str(p, "{ ");
538 p = print_tuple(map->dim, p, isl_dim_in, 0);
539 p = isl_printer_print_str(p, " -> ");
540 p = print_tuple(map->dim, p, isl_dim_out, 0);
541 p = print_disjuncts(map, p, 0);
542 p = isl_printer_print_str(p, " }");
546 static __isl_give isl_printer *isl_set_print_isl(__isl_keep isl_set *set,
547 __isl_take isl_printer *p)
551 if (isl_set_dim(set, isl_dim_param) > 0) {
552 p = print_tuple(set->dim, p, isl_dim_param, 0);
553 p = isl_printer_print_str(p, " -> ");
555 p = isl_printer_print_str(p, "{ ");
556 p = print_tuple(set->dim, p, isl_dim_set, 1);
557 p = print_disjuncts((isl_map *)set, p, 1);
558 p = isl_printer_print_str(p, " }");
562 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
563 __isl_keep isl_basic_map *bmap)
567 if (p->output_format == ISL_FORMAT_ISL)
568 return isl_basic_map_print_isl(bmap, p);
569 else if (p->output_format == ISL_FORMAT_OMEGA)
570 return isl_basic_map_print_omega(bmap, p);
571 isl_assert(bmap->ctx, 0, goto error);
577 void isl_basic_map_print(__isl_keep isl_basic_map *bmap, FILE *out, int indent,
578 const char *prefix, const char *suffix, unsigned output_format)
580 isl_printer *printer;
585 printer = isl_printer_to_file(bmap->ctx, out);
586 printer = isl_printer_set_indent(printer, indent);
587 printer = isl_printer_set_prefix(printer, prefix);
588 printer = isl_printer_set_suffix(printer, suffix);
589 printer = isl_printer_set_output_format(printer, output_format);
590 isl_printer_print_basic_map(printer, bmap);
592 isl_printer_free(printer);
595 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
596 __isl_keep isl_basic_set *bset)
601 if (p->output_format == ISL_FORMAT_ISL)
602 return isl_basic_set_print_isl(bset, p);
603 else if (p->output_format == ISL_FORMAT_POLYLIB)
604 return isl_basic_set_print_polylib(bset, p);
605 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
606 return bset_print_constraints_polylib(bset, p);
607 else if (p->output_format == ISL_FORMAT_OMEGA)
608 return isl_basic_set_print_omega(bset, p);
609 isl_assert(p->ctx, 0, goto error);
615 void isl_basic_set_print(struct isl_basic_set *bset, FILE *out, int indent,
616 const char *prefix, const char *suffix, unsigned output_format)
618 isl_printer *printer;
623 printer = isl_printer_to_file(bset->ctx, out);
624 printer = isl_printer_set_indent(printer, indent);
625 printer = isl_printer_set_prefix(printer, prefix);
626 printer = isl_printer_set_suffix(printer, suffix);
627 printer = isl_printer_set_output_format(printer, output_format);
628 isl_printer_print_basic_set(printer, bset);
630 isl_printer_free(printer);
633 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
634 __isl_keep isl_set *set)
638 if (p->output_format == ISL_FORMAT_ISL)
639 return isl_set_print_isl(set, p);
640 else if (p->output_format == ISL_FORMAT_POLYLIB)
641 return isl_set_print_polylib(set, p);
642 else if (p->output_format == ISL_FORMAT_OMEGA)
643 return isl_set_print_omega(set, p);
644 isl_assert(set->ctx, 0, goto error);
650 void isl_set_print(struct isl_set *set, FILE *out, int indent,
651 unsigned output_format)
653 isl_printer *printer;
658 printer = isl_printer_to_file(set->ctx, out);
659 printer = isl_printer_set_indent(printer, indent);
660 printer = isl_printer_set_output_format(printer, output_format);
661 printer = isl_printer_print_set(printer, set);
663 isl_printer_free(printer);
666 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
667 __isl_keep isl_map *map)
672 if (p->output_format == ISL_FORMAT_ISL)
673 return isl_map_print_isl(map, p);
674 else if (p->output_format == ISL_FORMAT_POLYLIB)
675 return isl_map_print_polylib(map, p);
676 else if (p->output_format == ISL_FORMAT_OMEGA)
677 return isl_map_print_omega(map, p);
678 isl_assert(map->ctx, 0, goto error);
684 void isl_map_print(__isl_keep isl_map *map, FILE *out, int indent,
685 unsigned output_format)
687 isl_printer *printer;
692 printer = isl_printer_to_file(map->ctx, out);
693 printer = isl_printer_set_indent(printer, indent);
694 printer = isl_printer_set_output_format(printer, output_format);
695 printer = isl_printer_print_map(printer, map);
697 isl_printer_free(printer);
700 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
705 for (i = 0, n = 0; i < rec->n; ++i)
706 if (!isl_upoly_is_zero(rec->p[i]))
712 static __isl_give isl_printer *print_div(__isl_keep isl_dim *dim,
713 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
715 int c = p->output_format == ISL_FORMAT_C;
716 p = isl_printer_print_str(p, c ? "floord(" : "[(");
717 p = print_affine_of_len(dim, p, div->row[pos] + 1, div->n_col - 1, 1);
718 p = isl_printer_print_str(p, c ? ", " : ")/");
719 p = isl_printer_print_isl_int(p, div->row[pos][0]);
720 p = isl_printer_print_str(p, c ? ")" : "]");
724 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
725 __isl_take isl_printer *p, int first)
727 struct isl_upoly_cst *cst;
730 cst = isl_upoly_as_cst(up);
733 neg = !first && isl_int_is_neg(cst->n);
735 p = isl_printer_print_str(p, neg ? " - " : " + ");
737 isl_int_neg(cst->n, cst->n);
738 if (isl_int_is_zero(cst->d)) {
739 int sgn = isl_int_sgn(cst->n);
740 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
741 sgn == 0 ? "NaN" : "infty");
743 p = isl_printer_print_isl_int(p, cst->n);
745 isl_int_neg(cst->n, cst->n);
746 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
747 p = isl_printer_print_str(p, "/");
748 p = isl_printer_print_isl_int(p, cst->d);
756 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
757 __isl_keep isl_dim *dim, __isl_keep isl_mat *div, int var)
761 total = isl_dim_total(dim);
763 p = print_term(dim, dim->ctx->one, 1 + var, p, 1);
765 p = print_div(dim, div, var - total, p);
769 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
770 __isl_keep isl_dim *dim, __isl_keep isl_mat *div, int var, int exp)
772 p = print_base(p, dim, div, var);
775 if (p->output_format == ISL_FORMAT_C) {
777 for (i = 1; i < exp; ++i) {
778 p = isl_printer_print_str(p, "*");
779 p = print_base(p, dim, div, var);
782 p = isl_printer_print_str(p, "^");
783 p = isl_printer_print_int(p, exp);
788 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
789 __isl_keep isl_dim *dim, __isl_keep isl_mat *div,
790 __isl_take isl_printer *p)
793 struct isl_upoly_rec *rec;
795 if (!p || !up || !dim || !div)
798 if (isl_upoly_is_cst(up))
799 return upoly_print_cst(up, p, 1);
801 rec = isl_upoly_as_rec(up);
804 n = upoly_rec_n_non_zero(rec);
806 p = isl_printer_print_str(p, "(");
807 for (i = 0, first = 1; i < rec->n; ++i) {
808 if (isl_upoly_is_zero(rec->p[i]))
810 if (isl_upoly_is_negone(rec->p[i])) {
812 p = isl_printer_print_str(p, "-1");
814 p = isl_printer_print_str(p, "-");
816 p = isl_printer_print_str(p, " - ");
817 } else if (isl_upoly_is_cst(rec->p[i]) &&
818 !isl_upoly_is_one(rec->p[i]))
819 p = upoly_print_cst(rec->p[i], p, first);
822 p = isl_printer_print_str(p, " + ");
823 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
824 p = upoly_print(rec->p[i], dim, div, p);
829 if (!isl_upoly_is_one(rec->p[i]) &&
830 !isl_upoly_is_negone(rec->p[i]))
831 p = isl_printer_print_str(p, " * ");
832 p = print_pow(p, dim, div, rec->up.var, i);
835 p = isl_printer_print_str(p, ")");
842 __isl_give isl_printer *isl_printer_print_qpolynomial(__isl_take isl_printer *p,
843 __isl_keep isl_qpolynomial *qp)
847 p = upoly_print(qp->upoly, qp->dim, qp->div, p);
854 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
855 unsigned output_format)
862 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
863 p = isl_printer_to_file(qp->dim->ctx, out);
864 p = isl_printer_print_qpolynomial(p, qp);
868 static __isl_give isl_printer *qpolynomial_fold_print(
869 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
873 if (fold->type == isl_fold_min)
874 p = isl_printer_print_str(p, "min");
875 else if (fold->type == isl_fold_max)
876 p = isl_printer_print_str(p, "max");
877 p = isl_printer_print_str(p, "(");
878 for (i = 0; i < fold->n; ++i) {
880 p = isl_printer_print_str(p, ", ");
881 p = isl_printer_print_qpolynomial(p, fold->qp[i]);
883 p = isl_printer_print_str(p, ")");
887 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
888 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
892 p = qpolynomial_fold_print(fold, p);
899 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
900 FILE *out, unsigned output_format)
907 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
909 p = isl_printer_to_file(fold->dim->ctx, out);
910 p = isl_printer_print_qpolynomial_fold(p, fold);
915 static __isl_give isl_printer *print_pw_qpolynomial_isl(
916 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
923 if (isl_dim_size(pwqp->dim, isl_dim_param) > 0) {
924 p = print_tuple(pwqp->dim, p, isl_dim_param, 0);
925 p = isl_printer_print_str(p, " -> ");
927 p = isl_printer_print_str(p, "{ ");
929 if (isl_dim_size(pwqp->dim, isl_dim_set) > 0) {
930 p = print_tuple(pwqp->dim, p, isl_dim_set, 1);
931 p = isl_printer_print_str(p, " -> ");
933 p = isl_printer_print_str(p, "0");
935 for (i = 0; i < pwqp->n; ++i) {
937 p = isl_printer_print_str(p, "; ");
938 if (isl_dim_size(pwqp->p[i].set->dim, isl_dim_set) > 0) {
939 p = print_tuple(pwqp->p[i].set->dim, p, isl_dim_set, 1);
940 p = isl_printer_print_str(p, " -> ");
942 p = isl_printer_print_qpolynomial(p, pwqp->p[i].qp);
943 p = print_disjuncts((isl_map *)pwqp->p[i].set, p, 1);
945 p = isl_printer_print_str(p, " }");
952 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
953 unsigned output_format)
960 p = isl_printer_to_file(pwqp->dim->ctx, out);
961 p = isl_printer_set_output_format(p, output_format);
962 p = isl_printer_print_pw_qpolynomial(p, pwqp);
967 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
968 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
972 if (isl_dim_size(pwf->dim, isl_dim_param) > 0) {
973 p = print_tuple(pwf->dim, p, isl_dim_param, 0);
974 p = isl_printer_print_str(p, " -> ");
976 p = isl_printer_print_str(p, "{ ");
978 if (isl_dim_size(pwf->dim, isl_dim_set) > 0) {
979 p = print_tuple(pwf->dim, p, isl_dim_set, 0);
980 p = isl_printer_print_str(p, " -> ");
982 p = isl_printer_print_str(p, "0");
984 for (i = 0; i < pwf->n; ++i) {
986 p = isl_printer_print_str(p, "; ");
987 if (isl_dim_size(pwf->p[i].set->dim, isl_dim_set) > 0) {
988 p = print_tuple(pwf->p[i].set->dim, p, isl_dim_set, 0);
989 p = isl_printer_print_str(p, " -> ");
991 p = qpolynomial_fold_print(pwf->p[i].fold, p);
992 p = print_disjuncts((isl_map *)pwf->p[i].set, p, 1);
994 p = isl_printer_print_str(p, " }");
998 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
999 __isl_keep isl_basic_set *bset, isl_int *c);
1001 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1002 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1004 if (type == isl_dim_div) {
1005 p = isl_printer_print_str(p, "floord(");
1006 p = print_affine_c(p, bset, bset->div[pos] + 1);
1007 p = isl_printer_print_str(p, ", ");
1008 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1009 p = isl_printer_print_str(p, ")");
1013 name = isl_dim_get_name(bset->dim, type, pos);
1016 p = isl_printer_print_str(p, name);
1021 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1022 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1024 enum isl_dim_type type;
1025 unsigned nparam = isl_basic_set_dim(bset, isl_dim_param);
1028 return isl_printer_print_isl_int(p, c);
1030 if (isl_int_is_one(c))
1032 else if (isl_int_is_negone(c))
1033 p = isl_printer_print_str(p, "-");
1035 p = isl_printer_print_isl_int(p, c);
1036 p = isl_printer_print_str(p, "*");
1038 type = pos2type(bset->dim, &pos);
1039 p = print_name_c(p, bset, type, pos);
1043 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1044 __isl_keep isl_basic_set *bset, isl_int *c)
1048 unsigned len = 1 + isl_basic_set_total_dim(bset);
1050 for (i = 0, first = 1; i < len; ++i) {
1052 if (isl_int_is_zero(c[i]))
1055 if (isl_int_is_neg(c[i])) {
1057 isl_int_neg(c[i], c[i]);
1058 p = isl_printer_print_str(p, " - ");
1060 p = isl_printer_print_str(p, " + ");
1063 p = print_term(bset->dim, c[i], i, p, 1);
1065 isl_int_neg(c[i], c[i]);
1068 p = isl_printer_print_str(p, "0");
1072 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1073 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int first)
1076 p = isl_printer_print_str(p, " && ");
1078 p = print_affine_c(p, bset, c);
1079 p = isl_printer_print_str(p, " ");
1080 p = isl_printer_print_str(p, op);
1081 p = isl_printer_print_str(p, " 0");
1085 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1086 __isl_keep isl_basic_set *bset)
1090 for (i = 0; i < bset->n_eq; ++i)
1091 p = print_constraint_c(p, bset, bset->eq[i], "==", !i);
1092 for (i = 0; i < bset->n_ineq; ++i)
1093 p = print_constraint_c(p, bset, bset->ineq[i], ">=",
1098 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
1099 __isl_keep isl_set *set)
1104 p = isl_printer_print_str(p, "0");
1106 for (i = 0; i < set->n; ++i) {
1108 p = isl_printer_print_str(p, " || ");
1110 p = isl_printer_print_str(p, "(");
1111 p = print_basic_set_c(p, set->p[i]);
1113 p = isl_printer_print_str(p, ")");
1118 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1119 __isl_keep isl_qpolynomial *qp)
1124 isl_qpolynomial_get_den(qp, &den);
1125 if (!isl_int_is_one(den)) {
1127 p = isl_printer_print_str(p, "(");
1128 qp = isl_qpolynomial_copy(qp);
1129 f = isl_qpolynomial_rat_cst(isl_dim_copy(qp->dim),
1130 den, qp->dim->ctx->one);
1131 qp = isl_qpolynomial_mul(qp, f);
1134 p = upoly_print(qp->upoly, qp->dim, qp->div, p);
1135 if (!isl_int_is_one(den)) {
1136 p = isl_printer_print_str(p, ")/");
1137 p = isl_printer_print_isl_int(p, den);
1138 isl_qpolynomial_free(qp);
1144 static __isl_give isl_printer *print_pw_qpolynomial_c(
1145 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwpq)
1149 if (pwpq->n == 1 && isl_set_fast_is_universe(pwpq->p[0].set))
1150 return print_qpolynomial_c(p, pwpq->p[0].qp);
1152 for (i = 0; i < pwpq->n; ++i) {
1153 p = isl_printer_print_str(p, "(");
1154 p = print_set_c(p, pwpq->p[i].set);
1155 p = isl_printer_print_str(p, ") ? (");
1156 p = print_qpolynomial_c(p, pwpq->p[i].qp);
1157 p = isl_printer_print_str(p, ") : ");
1160 p = isl_printer_print_str(p, "0");
1164 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
1165 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1170 if (p->output_format == ISL_FORMAT_ISL)
1171 return print_pw_qpolynomial_isl(p, pwqp);
1172 else if (p->output_format == ISL_FORMAT_C)
1173 return print_pw_qpolynomial_c(p, pwqp);
1174 isl_assert(p->ctx, 0, goto error);
1176 isl_printer_free(p);
1180 static __isl_give isl_printer *print_qpolynomial_fold_c(
1181 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
1185 for (i = 0; i < fold->n - 1; ++i)
1186 if (fold->type == isl_fold_min)
1187 p = isl_printer_print_str(p, "min(");
1188 else if (fold->type == isl_fold_max)
1189 p = isl_printer_print_str(p, "max(");
1191 for (i = 0; i < fold->n; ++i) {
1193 p = isl_printer_print_str(p, ", ");
1194 p = print_qpolynomial_c(p, fold->qp[i]);
1196 p = isl_printer_print_str(p, ")");
1201 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
1202 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1206 if (pwf->n == 1 && isl_set_fast_is_universe(pwf->p[0].set))
1207 return print_qpolynomial_fold_c(p, pwf->p[0].fold);
1209 for (i = 0; i < pwf->n; ++i) {
1210 p = isl_printer_print_str(p, "(");
1211 p = print_set_c(p, pwf->p[i].set);
1212 p = isl_printer_print_str(p, ") ? (");
1213 p = print_qpolynomial_fold_c(p, pwf->p[i].fold);
1214 p = isl_printer_print_str(p, ") : ");
1217 p = isl_printer_print_str(p, "0");
1221 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
1222 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1227 if (p->output_format == ISL_FORMAT_ISL)
1228 return print_pw_qpolynomial_fold_isl(p, pwf);
1229 else if (p->output_format == ISL_FORMAT_C)
1230 return print_pw_qpolynomial_fold_c(p, pwf);
1231 isl_assert(p->ctx, 0, goto error);
1233 isl_printer_free(p);
1237 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
1238 FILE *out, unsigned output_format)
1245 p = isl_printer_to_file(pwf->dim->ctx, out);
1246 p = isl_printer_set_output_format(p, output_format);
1247 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
1249 isl_printer_free(p);