2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the MIT 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_ctx_private.h>
16 #include <isl_map_private.h>
19 #include <isl_polynomial_private.h>
20 #include <isl_printer_private.h>
21 #include <isl_space_private.h>
22 #include <isl_mat_private.h>
23 #include <isl/union_map.h>
24 #include <isl/constraint.h>
25 #include <isl_local_space_private.h>
26 #include <isl_aff_private.h>
27 #include <isl_ast_build_expr.h>
30 static const char *s_to[2] = { " -> ", " \\to " };
31 static const char *s_and[2] = { " and ", " \\wedge " };
32 static const char *s_or[2] = { " or ", " \\vee " };
33 static const char *s_le[2] = { "<=", "\\le" };
34 static const char *s_ge[2] = { ">=", "\\ge" };
35 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
36 static const char *s_close_set[2] = { " }", " \\,\\}" };
37 static const char *s_open_list[2] = { "[", "(" };
38 static const char *s_close_list[2] = { "]", ")" };
39 static const char *s_such_that[2] = { " : ", " \\mid " };
40 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
41 static const char *s_close_exists[2] = { ")", "" };
42 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
43 static const char *s_param_prefix[2] = { "p", "p_" };
44 static const char *s_input_prefix[2] = { "i", "i_" };
45 static const char *s_output_prefix[2] = { "o", "o_" };
47 static __isl_give isl_printer *print_constraint_polylib(
48 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
51 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
52 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
53 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
54 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
56 p = isl_printer_start_line(p);
57 p = isl_printer_print_int(p, ineq);
58 for (i = 0; i < n_out; ++i) {
59 p = isl_printer_print_str(p, " ");
60 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
62 for (i = 0; i < n_in; ++i) {
63 p = isl_printer_print_str(p, " ");
64 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
66 for (i = 0; i < bmap->n_div; ++i) {
67 p = isl_printer_print_str(p, " ");
68 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
70 for (i = 0; i < nparam; ++i) {
71 p = isl_printer_print_str(p, " ");
72 p = isl_printer_print_isl_int(p, c[1+i]);
74 p = isl_printer_print_str(p, " ");
75 p = isl_printer_print_isl_int(p, c[0]);
76 p = isl_printer_end_line(p);
80 static __isl_give isl_printer *print_constraints_polylib(
81 struct isl_basic_map *bmap, __isl_take isl_printer *p)
85 p = isl_printer_set_isl_int_width(p, 5);
87 for (i = 0; i < bmap->n_eq; ++i)
88 p = print_constraint_polylib(bmap, 0, i, p);
89 for (i = 0; i < bmap->n_ineq; ++i)
90 p = print_constraint_polylib(bmap, 1, i, p);
95 static __isl_give isl_printer *bset_print_constraints_polylib(
96 struct isl_basic_set *bset, __isl_take isl_printer *p)
98 return print_constraints_polylib((struct isl_basic_map *)bset, p);
101 static __isl_give isl_printer *isl_basic_map_print_polylib(
102 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
104 unsigned total = isl_basic_map_total_dim(bmap);
105 p = isl_printer_start_line(p);
106 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
107 p = isl_printer_print_str(p, " ");
108 p = isl_printer_print_int(p, 1 + total + 1);
110 p = isl_printer_print_str(p, " ");
111 p = isl_printer_print_int(p,
112 isl_basic_map_dim(bmap, isl_dim_out));
113 p = isl_printer_print_str(p, " ");
114 p = isl_printer_print_int(p,
115 isl_basic_map_dim(bmap, isl_dim_in));
116 p = isl_printer_print_str(p, " ");
117 p = isl_printer_print_int(p,
118 isl_basic_map_dim(bmap, isl_dim_div));
119 p = isl_printer_print_str(p, " ");
120 p = isl_printer_print_int(p,
121 isl_basic_map_dim(bmap, isl_dim_param));
123 p = isl_printer_end_line(p);
124 return print_constraints_polylib(bmap, p);
127 static __isl_give isl_printer *isl_basic_set_print_polylib(
128 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
130 return isl_basic_map_print_polylib((struct isl_basic_map *)bset, p, ext);
133 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
134 __isl_take isl_printer *p, int ext)
138 p = isl_printer_start_line(p);
139 p = isl_printer_print_int(p, map->n);
140 p = isl_printer_end_line(p);
141 for (i = 0; i < map->n; ++i) {
142 p = isl_printer_start_line(p);
143 p = isl_printer_end_line(p);
144 p = isl_basic_map_print_polylib(map->p[i], p, ext);
149 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
150 __isl_take isl_printer *p, int ext)
152 return isl_map_print_polylib((struct isl_map *)set, p, ext);
155 static int count_same_name(__isl_keep isl_space *dim,
156 enum isl_dim_type type, unsigned pos, const char *name)
162 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
163 s = t == type ? pos : isl_space_dim(dim, t);
164 for (p = 0; p < s; ++p) {
165 const char *n = isl_space_get_dim_name(dim, t, p);
166 if (n && !strcmp(n, name))
173 static __isl_give isl_printer *print_name(__isl_keep isl_space *dim,
174 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
181 name = type == isl_dim_div ? NULL : isl_space_get_dim_name(dim, type, pos);
185 if (type == isl_dim_param)
186 prefix = s_param_prefix[latex];
187 else if (type == isl_dim_div)
188 prefix = s_div_prefix[latex];
189 else if (isl_space_is_set(dim) || type == isl_dim_in)
190 prefix = s_input_prefix[latex];
192 prefix = s_output_prefix[latex];
193 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
196 primes = count_same_name(dim, name == buffer ? isl_dim_div : type,
198 p = isl_printer_print_str(p, name);
200 p = isl_printer_print_str(p, "'");
204 static enum isl_dim_type pos2type(__isl_keep isl_space *dim, unsigned *pos)
206 enum isl_dim_type type;
207 unsigned n_in = isl_space_dim(dim, isl_dim_in);
208 unsigned n_out = isl_space_dim(dim, isl_dim_out);
209 unsigned nparam = isl_space_dim(dim, isl_dim_param);
211 if (*pos < 1 + nparam) {
212 type = isl_dim_param;
214 } else if (*pos < 1 + nparam + n_in) {
217 } else if (*pos < 1 + nparam + n_in + n_out) {
219 *pos -= 1 + nparam + n_in;
222 *pos -= 1 + nparam + n_in + n_out;
228 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
229 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
231 static __isl_give isl_printer *print_term(__isl_keep isl_space *dim,
232 __isl_keep isl_mat *div,
233 isl_int c, unsigned pos, __isl_take isl_printer *p, int latex)
235 enum isl_dim_type type;
239 return isl_printer_print_isl_int(p, c);
241 type = pos2type(dim, &pos);
242 print_div_def = type == isl_dim_div && div &&
243 !isl_int_is_zero(div->row[pos][0]);
245 if (isl_int_is_one(c))
247 else if (isl_int_is_negone(c))
248 p = isl_printer_print_str(p, "-");
250 p = isl_printer_print_isl_int(p, c);
251 if (p->output_format == ISL_FORMAT_C || print_div_def)
252 p = isl_printer_print_str(p, "*");
255 p = print_div(dim, div, pos, p);
257 p = print_name(dim, p, type, pos, latex);
261 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_space *dim,
262 __isl_keep isl_mat *div,
263 __isl_take isl_printer *p, isl_int *c, int len)
268 for (i = 0, first = 1; i < len; ++i) {
270 if (isl_int_is_zero(c[i]))
273 if (isl_int_is_neg(c[i])) {
275 isl_int_neg(c[i], c[i]);
276 p = isl_printer_print_str(p, " - ");
278 p = isl_printer_print_str(p, " + ");
281 p = print_term(dim, div, c[i], i, p, 0);
283 isl_int_neg(c[i], c[i]);
286 p = isl_printer_print_str(p, "0");
290 static __isl_give isl_printer *print_affine(__isl_keep isl_basic_map *bmap,
291 __isl_keep isl_space *dim, __isl_take isl_printer *p, isl_int *c)
293 unsigned len = 1 + isl_basic_map_total_dim(bmap);
294 return print_affine_of_len(dim, NULL, p, c, len);
297 static int defining_equality(__isl_keep isl_basic_map *eq,
298 __isl_keep isl_space *dim, enum isl_dim_type type, int pos)
306 pos += isl_space_offset(dim, type);
307 total = isl_basic_map_total_dim(eq);
309 for (i = 0; i < eq->n_eq; ++i) {
310 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
312 if (isl_int_is_one(eq->eq[i][1 + pos]))
313 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
320 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
321 __isl_keep isl_aff *aff);
323 /* offset is the offset of local_dim inside global_type of global_dim.
325 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
326 __isl_keep isl_space *global_dim, enum isl_dim_type global_type,
327 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
328 int latex, __isl_keep isl_basic_map *eq,
329 __isl_keep isl_multi_aff *maff, int offset)
333 if (global_dim != local_dim && local_type == isl_dim_out)
334 offset += local_dim->n_in;
336 for (i = 0; i < isl_space_dim(local_dim, local_type); ++i) {
338 p = isl_printer_print_str(p, ", ");
339 if (maff && global_type == isl_dim_out) {
340 p = print_aff_body(p, maff->p[offset + i]);
343 j = defining_equality(eq, global_dim, global_type, offset + i);
345 int pos = 1 + isl_space_offset(global_dim, global_type)
347 p = print_affine_of_len(eq->dim, NULL,
350 p = print_name(global_dim, p, global_type, offset + i,
357 static __isl_give isl_printer *print_var_list(__isl_keep isl_space *dim,
358 __isl_take isl_printer *p, enum isl_dim_type type,
359 int latex, __isl_keep isl_basic_map *eq, __isl_keep isl_multi_aff *maff)
361 return print_nested_var_list(p, dim, type, dim, type, latex,
365 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
366 __isl_keep isl_space *global_dim, enum isl_dim_type global_type,
367 __isl_keep isl_space *local_dim,
368 int latex, __isl_keep isl_basic_map *eq,
369 __isl_keep isl_multi_aff *maff, int offset);
371 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
372 __isl_keep isl_space *global_dim, enum isl_dim_type global_type,
373 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
374 int latex, __isl_keep isl_basic_map *eq,
375 __isl_keep isl_multi_aff *maff, int offset)
377 const char *name = NULL;
378 unsigned n = isl_space_dim(local_dim, local_type);
379 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
380 name = isl_space_get_tuple_name(local_dim, local_type);
383 p = isl_printer_print_str(p, "\\mathrm{");
384 p = isl_printer_print_str(p, name);
386 p = isl_printer_print_str(p, "}");
389 if (!latex || n != 1 || name)
390 p = isl_printer_print_str(p, s_open_list[latex]);
391 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
392 local_dim->nested[local_type - isl_dim_in]) {
393 if (global_dim != local_dim && local_type == isl_dim_out)
394 offset += local_dim->n_in;
395 p = print_nested_map_dim(p, global_dim, global_type,
396 local_dim->nested[local_type - isl_dim_in],
397 latex, eq, maff, offset);
399 p = print_nested_var_list(p, global_dim, global_type,
400 local_dim, local_type, latex,
402 if (!latex || n != 1 || name)
403 p = isl_printer_print_str(p, s_close_list[latex]);
407 static __isl_give isl_printer *print_tuple(__isl_keep isl_space *dim,
408 __isl_take isl_printer *p, enum isl_dim_type type,
409 int latex, __isl_keep isl_basic_map *eq, __isl_keep isl_multi_aff *maff)
411 return print_nested_tuple(p, dim, type, dim, type, latex, eq, maff, 0);
414 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
415 __isl_keep isl_space *global_dim, enum isl_dim_type global_type,
416 __isl_keep isl_space *local_dim,
417 int latex, __isl_keep isl_basic_map *eq,
418 __isl_keep isl_multi_aff *maff, int offset)
420 p = print_nested_tuple(p, global_dim, global_type,
421 local_dim, isl_dim_in, latex, eq, maff, offset);
422 p = isl_printer_print_str(p, s_to[latex]);
423 p = print_nested_tuple(p, global_dim, global_type,
424 local_dim, isl_dim_out, latex, eq, maff, offset);
429 static __isl_give isl_printer *print_space(__isl_keep isl_space *dim,
430 __isl_take isl_printer *p, int latex, int rational,
431 __isl_keep isl_basic_map *eq, __isl_keep isl_multi_aff *maff)
433 if (rational && !latex)
434 p = isl_printer_print_str(p, "rat: ");
435 if (isl_space_is_params(dim))
437 else if (isl_space_is_set(dim))
438 p = print_tuple(dim, p, isl_dim_set, latex, eq, maff);
440 p = print_tuple(dim, p, isl_dim_in, latex, eq, maff);
441 p = isl_printer_print_str(p, s_to[latex]);
442 p = print_tuple(dim, p, isl_dim_out, latex, eq, maff);
448 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_space *dim,
449 __isl_take isl_printer *p)
451 if (isl_space_dim(dim, isl_dim_param) == 0)
454 p = isl_printer_start_line(p);
455 p = isl_printer_print_str(p, "symbolic ");
456 p = print_var_list(dim, p, isl_dim_param, 0, NULL, NULL);
457 p = isl_printer_print_str(p, ";");
458 p = isl_printer_end_line(p);
462 static __isl_give isl_printer *print_constraint(struct isl_basic_map *bmap,
463 __isl_keep isl_space *dim, __isl_take isl_printer *p,
464 isl_int *c, int last, const char *op, int first_constraint, int latex)
466 if (!first_constraint)
467 p = isl_printer_print_str(p, s_and[latex]);
469 isl_int_abs(c[last], c[last]);
471 p = print_term(dim, NULL, c[last], last, p, latex);
473 p = isl_printer_print_str(p, " ");
474 p = isl_printer_print_str(p, op);
475 p = isl_printer_print_str(p, " ");
477 isl_int_set_si(c[last], 0);
478 p = print_affine(bmap, dim, p, c);
483 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
484 __isl_keep isl_space *dim, __isl_take isl_printer *p, int latex)
488 unsigned total = isl_basic_map_total_dim(bmap);
490 c = isl_vec_alloc(bmap->ctx, 1 + total);
494 for (i = bmap->n_eq - 1; i >= 0; --i) {
495 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
497 if (i != bmap->n_eq - 1)
498 p = isl_printer_print_str(p, s_and[latex]);
499 p = isl_printer_print_str(p, "0 = 0");
502 if (isl_int_is_neg(bmap->eq[i][l]))
503 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
505 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
506 p = print_constraint(bmap, dim, p, c->el, l,
507 "=", i == bmap->n_eq - 1, latex);
509 for (i = 0; i < bmap->n_ineq; ++i) {
510 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
515 s = isl_int_sgn(bmap->ineq[i][l]);
517 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
519 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
520 op = s < 0 ? s_le[latex] : s_ge[latex];
521 p = print_constraint(bmap, dim, p, c->el, l,
522 op, !bmap->n_eq && !i, latex);
534 static __isl_give isl_printer *print_omega_constraints(
535 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
537 if (bmap->n_eq + bmap->n_ineq == 0)
540 p = isl_printer_print_str(p, ": ");
541 if (bmap->n_div > 0) {
543 p = isl_printer_print_str(p, "exists (");
544 for (i = 0; i < bmap->n_div; ++i) {
546 p = isl_printer_print_str(p, ", ");
547 p = print_name(bmap->dim, p, isl_dim_div, i, 0);
549 p = isl_printer_print_str(p, ": ");
551 p = print_constraints(bmap, bmap->dim, p, 0);
553 p = isl_printer_print_str(p, ")");
557 static __isl_give isl_printer *basic_map_print_omega(
558 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
560 p = isl_printer_print_str(p, "{ [");
561 p = print_var_list(bmap->dim, p, isl_dim_in, 0, NULL, NULL);
562 p = isl_printer_print_str(p, "] -> [");
563 p = print_var_list(bmap->dim, p, isl_dim_out, 0, NULL, NULL);
564 p = isl_printer_print_str(p, "] ");
565 p = print_omega_constraints(bmap, p);
566 p = isl_printer_print_str(p, " }");
570 static __isl_give isl_printer *basic_set_print_omega(
571 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
573 p = isl_printer_print_str(p, "{ [");
574 p = print_var_list(bset->dim, p, isl_dim_set, 0, NULL, NULL);
575 p = isl_printer_print_str(p, "] ");
576 p = print_omega_constraints((isl_basic_map *)bset, p);
577 p = isl_printer_print_str(p, " }");
581 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
582 __isl_take isl_printer *p)
586 for (i = 0; i < map->n; ++i) {
588 p = isl_printer_print_str(p, " union ");
589 p = basic_map_print_omega(map->p[i], p);
594 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
595 __isl_take isl_printer *p)
599 for (i = 0; i < set->n; ++i) {
601 p = isl_printer_print_str(p, " union ");
602 p = basic_set_print_omega(set->p[i], p);
607 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
608 __isl_keep isl_space *dim, __isl_take isl_printer *p, int latex)
610 if (bmap->n_div > 0) {
612 p = isl_printer_print_str(p, s_open_exists[latex]);
613 for (i = 0; i < bmap->n_div; ++i) {
615 p = isl_printer_print_str(p, ", ");
616 p = print_name(dim, p, isl_dim_div, i, latex);
617 if (latex || isl_int_is_zero(bmap->div[i][0]))
619 p = isl_printer_print_str(p, " = [(");
620 p = print_affine(bmap, dim, p, bmap->div[i] + 1);
621 p = isl_printer_print_str(p, ")/");
622 p = isl_printer_print_isl_int(p, bmap->div[i][0]);
623 p = isl_printer_print_str(p, "]");
625 p = isl_printer_print_str(p, ": ");
628 p = print_constraints(bmap, dim, p, latex);
631 p = isl_printer_print_str(p, s_close_exists[latex]);
635 static __isl_give isl_printer *isl_basic_map_print_isl(
636 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
639 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
640 if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
641 p = print_tuple(bmap->dim, p, isl_dim_param, latex, NULL, NULL);
642 p = isl_printer_print_str(p, " -> ");
644 p = isl_printer_print_str(p, "{ ");
645 p = print_space(bmap->dim, p, latex, rational, NULL, NULL);
646 p = isl_printer_print_str(p, " : ");
647 p = print_disjunct(bmap, bmap->dim, p, latex);
648 p = isl_printer_print_str(p, " }");
652 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
653 __isl_take isl_printer *p, int latex)
657 if (isl_map_plain_is_universe(map))
660 p = isl_printer_print_str(p, s_such_that[latex]);
662 p = isl_printer_print_str(p, "1 = 0");
663 for (i = 0; i < map->n; ++i) {
665 p = isl_printer_print_str(p, s_or[latex]);
666 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
667 p = isl_printer_print_str(p, "(");
668 p = print_disjunct(map->p[i], map->dim, p, latex);
669 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
670 p = isl_printer_print_str(p, ")");
675 /* Print the disjuncts of a map (or set).
676 * If the map turns out to be a universal parameter domain, then
677 * we need to print the colon. Otherwise, the output looks identical
680 static __isl_give isl_printer *print_disjuncts_map(__isl_keep isl_map *map,
681 __isl_take isl_printer *p, int latex)
683 if (isl_map_plain_is_universe(map) && isl_space_is_params(map->dim))
684 return isl_printer_print_str(p, s_such_that[latex]);
686 return print_disjuncts(map, p, latex);
689 struct isl_aff_split {
694 static void free_split(__isl_take struct isl_aff_split *split, int n)
701 for (i = 0; i < n; ++i) {
702 isl_basic_map_free(split[i].aff);
703 isl_map_free(split[i].map);
709 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
712 unsigned nparam, n_in, n_out, total;
714 bmap = isl_basic_map_cow(bmap);
717 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
720 nparam = isl_basic_map_dim(bmap, isl_dim_param);
721 n_in = isl_basic_map_dim(bmap, isl_dim_in);
722 n_out = isl_basic_map_dim(bmap, isl_dim_out);
723 total = isl_basic_map_dim(bmap, isl_dim_all);
724 for (i = bmap->n_eq - 1; i >= 0; --i) {
725 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
726 if (j >= nparam && j < nparam + n_in + n_out &&
727 (isl_int_is_one(bmap->eq[i][1 + j]) ||
728 isl_int_is_negone(bmap->eq[i][1 + j])))
730 if (isl_basic_map_drop_equality(bmap, i) < 0)
734 bmap = isl_basic_map_finalize(bmap);
738 isl_basic_map_free(bmap);
742 static int aff_split_cmp(const void *p1, const void *p2, void *user)
744 const struct isl_aff_split *s1, *s2;
745 s1 = (const struct isl_aff_split *) p1;
746 s2 = (const struct isl_aff_split *) p2;
748 return isl_basic_map_plain_cmp(s1->aff, s2->aff);
751 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
752 __isl_keep isl_basic_map *aff)
760 total = isl_space_dim(bmap->dim, isl_dim_all);
762 for (i = bmap->n_eq - 1; i >= 0; --i) {
763 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
766 for (j = 0; j < aff->n_eq; ++j) {
767 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
768 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
770 if (isl_basic_map_drop_equality(bmap, i) < 0)
778 isl_basic_map_free(bmap);
782 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
785 struct isl_aff_split *split;
788 ctx = isl_map_get_ctx(map);
789 split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
793 for (i = 0; i < map->n; ++i) {
795 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
796 bmap = isl_basic_map_copy(map->p[i]);
797 bmap = isl_basic_map_cow(bmap);
798 bmap = drop_aff(bmap, split[i].aff);
799 split[i].map = isl_map_from_basic_map(bmap);
800 if (!split[i].aff || !split[i].map)
804 if (isl_sort(split, map->n, sizeof(struct isl_aff_split),
805 &aff_split_cmp, NULL) < 0)
809 for (i = n - 1; i >= 1; --i) {
810 if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
813 isl_basic_map_free(split[i].aff);
814 split[i - 1].map = isl_map_union(split[i - 1].map,
817 split[i] = split[n - 1];
818 split[n - 1].aff = NULL;
819 split[n - 1].map = NULL;
825 free_split(split, map->n);
829 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
830 struct isl_aff_split *split, int n)
835 for (i = 0; i < n; ++i) {
840 dim = split[i].map->dim;
841 rational = split[i].map->n > 0 &&
842 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
844 p = isl_printer_print_str(p, "; ");
845 p = print_space(dim, p, 0, rational, split[i].aff, NULL);
846 p = print_disjuncts_map(split[i].map, p, 0);
852 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
853 __isl_take isl_printer *p)
855 struct isl_aff_split *split = NULL;
859 split = split_aff(map);
861 p = print_split_map(p, split, map->n);
863 rational = map->n > 0 &&
864 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
865 p = print_space(map->dim, p, 0, rational, NULL, NULL);
866 p = print_disjuncts_map(map, p, 0);
868 free_split(split, map->n);
872 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
873 __isl_take isl_printer *p)
875 if (isl_map_dim(map, isl_dim_param) > 0) {
876 p = print_tuple(map->dim, p, isl_dim_param, 0, NULL, NULL);
877 p = isl_printer_print_str(p, s_to[0]);
879 p = isl_printer_print_str(p, s_open_set[0]);
880 p = isl_map_print_isl_body(map, p);
881 p = isl_printer_print_str(p, s_close_set[0]);
885 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
886 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff)
888 if (isl_map_dim(map, isl_dim_param) > 0) {
889 p = print_tuple(map->dim, p, isl_dim_param, 1, NULL, NULL);
890 p = isl_printer_print_str(p, s_to[1]);
892 p = isl_printer_print_str(p, s_open_set[1]);
893 p = print_space(map->dim, p, 1, 0, aff, NULL);
894 p = print_disjuncts_map(map, p, 1);
895 p = isl_printer_print_str(p, s_close_set[1]);
900 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
901 __isl_take isl_printer *p)
904 struct isl_aff_split *split = NULL;
907 split = split_aff(map);
910 return print_latex_map(map, p, NULL);
912 for (i = 0; i < map->n; ++i) {
916 p = isl_printer_print_str(p, " \\cup ");
917 p = print_latex_map(split[i].map, p, split[i].aff);
920 free_split(split, map->n);
924 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
925 __isl_keep isl_basic_map *bmap)
929 if (p->output_format == ISL_FORMAT_ISL)
930 return isl_basic_map_print_isl(bmap, p, 0);
931 else if (p->output_format == ISL_FORMAT_OMEGA)
932 return basic_map_print_omega(bmap, p);
933 isl_assert(bmap->ctx, 0, goto error);
939 void isl_basic_map_print(__isl_keep isl_basic_map *bmap, FILE *out, int indent,
940 const char *prefix, const char *suffix, unsigned output_format)
942 isl_printer *printer;
947 printer = isl_printer_to_file(bmap->ctx, out);
948 printer = isl_printer_set_indent(printer, indent);
949 printer = isl_printer_set_prefix(printer, prefix);
950 printer = isl_printer_set_suffix(printer, suffix);
951 printer = isl_printer_set_output_format(printer, output_format);
952 isl_printer_print_basic_map(printer, bmap);
954 isl_printer_free(printer);
957 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
958 __isl_keep isl_basic_set *bset)
963 if (p->output_format == ISL_FORMAT_ISL)
964 return isl_basic_map_print_isl(bset, p, 0);
965 else if (p->output_format == ISL_FORMAT_POLYLIB)
966 return isl_basic_set_print_polylib(bset, p, 0);
967 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
968 return isl_basic_set_print_polylib(bset, p, 1);
969 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
970 return bset_print_constraints_polylib(bset, p);
971 else if (p->output_format == ISL_FORMAT_OMEGA)
972 return basic_set_print_omega(bset, p);
973 isl_assert(p->ctx, 0, goto error);
979 void isl_basic_set_print(struct isl_basic_set *bset, FILE *out, int indent,
980 const char *prefix, const char *suffix, unsigned output_format)
982 isl_printer *printer;
987 printer = isl_printer_to_file(bset->ctx, out);
988 printer = isl_printer_set_indent(printer, indent);
989 printer = isl_printer_set_prefix(printer, prefix);
990 printer = isl_printer_set_suffix(printer, suffix);
991 printer = isl_printer_set_output_format(printer, output_format);
992 isl_printer_print_basic_set(printer, bset);
994 isl_printer_free(printer);
997 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
998 __isl_keep isl_set *set)
1002 if (p->output_format == ISL_FORMAT_ISL)
1003 return isl_map_print_isl((isl_map *)set, p);
1004 else if (p->output_format == ISL_FORMAT_POLYLIB)
1005 return isl_set_print_polylib(set, p, 0);
1006 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1007 return isl_set_print_polylib(set, p, 1);
1008 else if (p->output_format == ISL_FORMAT_OMEGA)
1009 return isl_set_print_omega(set, p);
1010 else if (p->output_format == ISL_FORMAT_LATEX)
1011 return isl_map_print_latex((isl_map *)set, p);
1012 isl_assert(set->ctx, 0, goto error);
1014 isl_printer_free(p);
1018 void isl_set_print(struct isl_set *set, FILE *out, int indent,
1019 unsigned output_format)
1021 isl_printer *printer;
1026 printer = isl_printer_to_file(set->ctx, out);
1027 printer = isl_printer_set_indent(printer, indent);
1028 printer = isl_printer_set_output_format(printer, output_format);
1029 printer = isl_printer_print_set(printer, set);
1031 isl_printer_free(printer);
1034 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1035 __isl_keep isl_map *map)
1040 if (p->output_format == ISL_FORMAT_ISL)
1041 return isl_map_print_isl(map, p);
1042 else if (p->output_format == ISL_FORMAT_POLYLIB)
1043 return isl_map_print_polylib(map, p, 0);
1044 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1045 return isl_map_print_polylib(map, p, 1);
1046 else if (p->output_format == ISL_FORMAT_OMEGA)
1047 return isl_map_print_omega(map, p);
1048 else if (p->output_format == ISL_FORMAT_LATEX)
1049 return isl_map_print_latex(map, p);
1050 isl_assert(map->ctx, 0, goto error);
1052 isl_printer_free(p);
1056 struct isl_union_print_data {
1061 static int print_map_body(__isl_take isl_map *map, void *user)
1063 struct isl_union_print_data *data;
1064 data = (struct isl_union_print_data *)user;
1067 data->p = isl_printer_print_str(data->p, "; ");
1070 data->p = isl_map_print_isl_body(map, data->p);
1076 static __isl_give isl_printer *isl_union_map_print_isl(
1077 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1079 struct isl_union_print_data data = { p, 1 };
1081 dim = isl_union_map_get_space(umap);
1082 if (isl_space_dim(dim, isl_dim_param) > 0) {
1083 p = print_tuple(dim, p, isl_dim_param, 0, NULL, NULL);
1084 p = isl_printer_print_str(p, s_to[0]);
1086 isl_space_free(dim);
1087 p = isl_printer_print_str(p, s_open_set[0]);
1088 isl_union_map_foreach_map(umap, &print_map_body, &data);
1090 p = isl_printer_print_str(p, s_close_set[0]);
1094 static int print_latex_map_body(__isl_take isl_map *map, void *user)
1096 struct isl_union_print_data *data;
1097 data = (struct isl_union_print_data *)user;
1100 data->p = isl_printer_print_str(data->p, " \\cup ");
1103 data->p = isl_map_print_latex(map, data->p);
1109 static __isl_give isl_printer *isl_union_map_print_latex(
1110 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1112 struct isl_union_print_data data = { p, 1 };
1113 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1118 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1119 __isl_keep isl_union_map *umap)
1124 if (p->output_format == ISL_FORMAT_ISL)
1125 return isl_union_map_print_isl(umap, p);
1126 if (p->output_format == ISL_FORMAT_LATEX)
1127 return isl_union_map_print_latex(umap, p);
1129 isl_die(p->ctx, isl_error_invalid,
1130 "invalid output format for isl_union_map", goto error);
1132 isl_printer_free(p);
1136 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1137 __isl_keep isl_union_set *uset)
1142 if (p->output_format == ISL_FORMAT_ISL)
1143 return isl_union_map_print_isl((isl_union_map *)uset, p);
1144 if (p->output_format == ISL_FORMAT_LATEX)
1145 return isl_union_map_print_latex((isl_union_map *)uset, p);
1147 isl_die(p->ctx, isl_error_invalid,
1148 "invalid output format for isl_union_set", goto error);
1150 isl_printer_free(p);
1154 void isl_map_print(__isl_keep isl_map *map, FILE *out, int indent,
1155 unsigned output_format)
1157 isl_printer *printer;
1162 printer = isl_printer_to_file(map->ctx, out);
1163 printer = isl_printer_set_indent(printer, indent);
1164 printer = isl_printer_set_output_format(printer, output_format);
1165 printer = isl_printer_print_map(printer, map);
1167 isl_printer_free(printer);
1170 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
1175 for (i = 0, n = 0; i < rec->n; ++i)
1176 if (!isl_upoly_is_zero(rec->p[i]))
1182 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
1183 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
1185 int c = p->output_format == ISL_FORMAT_C;
1186 p = isl_printer_print_str(p, c ? "floord(" : "[(");
1187 p = print_affine_of_len(dim, div, p,
1188 div->row[pos] + 1, div->n_col - 1);
1189 p = isl_printer_print_str(p, c ? ", " : ")/");
1190 p = isl_printer_print_isl_int(p, div->row[pos][0]);
1191 p = isl_printer_print_str(p, c ? ")" : "]");
1195 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
1196 __isl_take isl_printer *p, int first)
1198 struct isl_upoly_cst *cst;
1201 cst = isl_upoly_as_cst(up);
1204 neg = !first && isl_int_is_neg(cst->n);
1206 p = isl_printer_print_str(p, neg ? " - " : " + ");
1208 isl_int_neg(cst->n, cst->n);
1209 if (isl_int_is_zero(cst->d)) {
1210 int sgn = isl_int_sgn(cst->n);
1211 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1212 sgn == 0 ? "NaN" : "infty");
1214 p = isl_printer_print_isl_int(p, cst->n);
1216 isl_int_neg(cst->n, cst->n);
1217 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1218 p = isl_printer_print_str(p, "/");
1219 p = isl_printer_print_isl_int(p, cst->d);
1223 isl_printer_free(p);
1227 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1228 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var)
1232 total = isl_space_dim(dim, isl_dim_all);
1234 p = print_term(dim, NULL, dim->ctx->one, 1 + var, p, 0);
1236 p = print_div(dim, div, var - total, p);
1240 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1241 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var, int exp)
1243 p = print_base(p, dim, div, var);
1246 if (p->output_format == ISL_FORMAT_C) {
1248 for (i = 1; i < exp; ++i) {
1249 p = isl_printer_print_str(p, "*");
1250 p = print_base(p, dim, div, var);
1253 p = isl_printer_print_str(p, "^");
1254 p = isl_printer_print_int(p, exp);
1259 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1260 __isl_keep isl_space *dim, __isl_keep isl_mat *div,
1261 __isl_take isl_printer *p, int outer)
1263 int i, n, first, print_parens;
1264 struct isl_upoly_rec *rec;
1266 if (!p || !up || !dim || !div)
1269 if (isl_upoly_is_cst(up))
1270 return upoly_print_cst(up, p, 1);
1272 rec = isl_upoly_as_rec(up);
1275 n = upoly_rec_n_non_zero(rec);
1276 print_parens = n > 1 ||
1277 (outer && rec->up.var >= isl_space_dim(dim, isl_dim_all));
1279 p = isl_printer_print_str(p, "(");
1280 for (i = 0, first = 1; i < rec->n; ++i) {
1281 if (isl_upoly_is_zero(rec->p[i]))
1283 if (isl_upoly_is_negone(rec->p[i])) {
1285 p = isl_printer_print_str(p, "-1");
1287 p = isl_printer_print_str(p, "-");
1289 p = isl_printer_print_str(p, " - ");
1290 } else if (isl_upoly_is_cst(rec->p[i]) &&
1291 !isl_upoly_is_one(rec->p[i]))
1292 p = upoly_print_cst(rec->p[i], p, first);
1295 p = isl_printer_print_str(p, " + ");
1296 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1297 p = upoly_print(rec->p[i], dim, div, p, 0);
1302 if (!isl_upoly_is_one(rec->p[i]) &&
1303 !isl_upoly_is_negone(rec->p[i]))
1304 p = isl_printer_print_str(p, " * ");
1305 p = print_pow(p, dim, div, rec->up.var, i);
1308 p = isl_printer_print_str(p, ")");
1311 isl_printer_free(p);
1315 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1316 __isl_keep isl_qpolynomial *qp)
1320 p = upoly_print(qp->upoly, qp->dim, qp->div, p, 1);
1323 isl_printer_free(p);
1327 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1328 __isl_keep isl_qpolynomial *qp)
1333 if (isl_space_dim(qp->dim, isl_dim_param) > 0) {
1334 p = print_tuple(qp->dim, p, isl_dim_param, 0, NULL, NULL);
1335 p = isl_printer_print_str(p, " -> ");
1337 p = isl_printer_print_str(p, "{ ");
1338 if (!isl_space_is_params(qp->dim)) {
1339 p = print_space(qp->dim, p, 0, 0, NULL, NULL);
1340 p = isl_printer_print_str(p, " -> ");
1342 p = print_qpolynomial(p, qp);
1343 p = isl_printer_print_str(p, " }");
1346 isl_printer_free(p);
1350 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1351 __isl_keep isl_space *dim, __isl_keep isl_qpolynomial *qp)
1356 isl_qpolynomial_get_den(qp, &den);
1357 if (!isl_int_is_one(den)) {
1359 p = isl_printer_print_str(p, "(");
1360 qp = isl_qpolynomial_copy(qp);
1361 f = isl_qpolynomial_rat_cst_on_domain(isl_space_copy(qp->dim),
1362 den, qp->dim->ctx->one);
1363 qp = isl_qpolynomial_mul(qp, f);
1366 p = upoly_print(qp->upoly, dim, qp->div, p, 0);
1368 p = isl_printer_free(p);
1369 if (!isl_int_is_one(den)) {
1370 p = isl_printer_print_str(p, ")/");
1371 p = isl_printer_print_isl_int(p, den);
1372 isl_qpolynomial_free(qp);
1378 __isl_give isl_printer *isl_printer_print_qpolynomial(
1379 __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1384 if (p->output_format == ISL_FORMAT_ISL)
1385 return print_qpolynomial_isl(p, qp);
1386 else if (p->output_format == ISL_FORMAT_C)
1387 return print_qpolynomial_c(p, qp->dim, qp);
1389 isl_die(qp->dim->ctx, isl_error_unsupported,
1390 "output format not supported for isl_qpolynomials",
1393 isl_printer_free(p);
1397 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1398 unsigned output_format)
1405 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1406 p = isl_printer_to_file(qp->dim->ctx, out);
1407 p = isl_printer_print_qpolynomial(p, qp);
1408 isl_printer_free(p);
1411 static __isl_give isl_printer *qpolynomial_fold_print(
1412 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1416 if (fold->type == isl_fold_min)
1417 p = isl_printer_print_str(p, "min");
1418 else if (fold->type == isl_fold_max)
1419 p = isl_printer_print_str(p, "max");
1420 p = isl_printer_print_str(p, "(");
1421 for (i = 0; i < fold->n; ++i) {
1423 p = isl_printer_print_str(p, ", ");
1424 p = print_qpolynomial(p, fold->qp[i]);
1426 p = isl_printer_print_str(p, ")");
1430 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1431 FILE *out, unsigned output_format)
1438 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1440 p = isl_printer_to_file(fold->dim->ctx, out);
1441 p = isl_printer_print_qpolynomial_fold(p, fold);
1443 isl_printer_free(p);
1446 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1447 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1451 for (i = 0; i < pwqp->n; ++i) {
1453 p = isl_printer_print_str(p, "; ");
1454 if (!isl_space_is_params(pwqp->p[i].set->dim)) {
1455 p = print_space(pwqp->p[i].set->dim, p, 0, 0, NULL, NULL);
1456 p = isl_printer_print_str(p, " -> ");
1458 p = print_qpolynomial(p, pwqp->p[i].qp);
1459 p = print_disjuncts((isl_map *)pwqp->p[i].set, p, 0);
1465 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1466 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1471 if (isl_space_dim(pwqp->dim, isl_dim_param) > 0) {
1472 p = print_tuple(pwqp->dim, p, isl_dim_param, 0, NULL, NULL);
1473 p = isl_printer_print_str(p, " -> ");
1475 p = isl_printer_print_str(p, "{ ");
1477 if (!isl_space_is_set(pwqp->dim)) {
1478 p = print_tuple(pwqp->dim, p, isl_dim_in, 0, NULL, NULL);
1479 p = isl_printer_print_str(p, " -> ");
1481 p = isl_printer_print_str(p, "0");
1483 p = isl_pwqp_print_isl_body(p, pwqp);
1484 p = isl_printer_print_str(p, " }");
1487 isl_printer_free(p);
1491 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
1492 unsigned output_format)
1499 p = isl_printer_to_file(pwqp->dim->ctx, out);
1500 p = isl_printer_set_output_format(p, output_format);
1501 p = isl_printer_print_pw_qpolynomial(p, pwqp);
1503 isl_printer_free(p);
1506 static __isl_give isl_printer *isl_pwf_print_isl_body(
1507 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1511 for (i = 0; i < pwf->n; ++i) {
1513 p = isl_printer_print_str(p, "; ");
1514 if (!isl_space_is_params(pwf->p[i].set->dim)) {
1515 p = print_space(pwf->p[i].set->dim, p, 0, 0, NULL, NULL);
1516 p = isl_printer_print_str(p, " -> ");
1518 p = qpolynomial_fold_print(pwf->p[i].fold, p);
1519 p = print_disjuncts((isl_map *)pwf->p[i].set, p, 0);
1525 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
1526 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1528 if (isl_space_dim(pwf->dim, isl_dim_param) > 0) {
1529 p = print_tuple(pwf->dim, p, isl_dim_param, 0, NULL, NULL);
1530 p = isl_printer_print_str(p, " -> ");
1532 p = isl_printer_print_str(p, "{ ");
1534 if (!isl_space_is_set(pwf->dim)) {
1535 p = print_tuple(pwf->dim, p, isl_dim_in, 0, NULL, NULL);
1536 p = isl_printer_print_str(p, " -> ");
1538 p = isl_printer_print_str(p, "0");
1540 p = isl_pwf_print_isl_body(p, pwf);
1541 p = isl_printer_print_str(p, " }");
1545 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1546 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c);
1548 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1549 __isl_keep isl_space *dim,
1550 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1552 if (type == isl_dim_div) {
1553 p = isl_printer_print_str(p, "floord(");
1554 p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
1555 p = isl_printer_print_str(p, ", ");
1556 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1557 p = isl_printer_print_str(p, ")");
1561 name = isl_space_get_dim_name(dim, type, pos);
1564 p = isl_printer_print_str(p, name);
1569 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1570 __isl_keep isl_space *dim,
1571 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1573 enum isl_dim_type type;
1576 return isl_printer_print_isl_int(p, c);
1578 if (isl_int_is_one(c))
1580 else if (isl_int_is_negone(c))
1581 p = isl_printer_print_str(p, "-");
1583 p = isl_printer_print_isl_int(p, c);
1584 p = isl_printer_print_str(p, "*");
1586 type = pos2type(dim, &pos);
1587 p = print_name_c(p, dim, bset, type, pos);
1591 static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
1592 __isl_keep isl_space *dim,
1593 __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
1598 for (i = 0, first = 1; i < len; ++i) {
1600 if (isl_int_is_zero(c[i]))
1603 if (isl_int_is_neg(c[i])) {
1605 isl_int_neg(c[i], c[i]);
1606 p = isl_printer_print_str(p, " - ");
1608 p = isl_printer_print_str(p, " + ");
1611 p = print_term_c(p, dim, bset, c[i], i);
1613 isl_int_neg(c[i], c[i]);
1616 p = isl_printer_print_str(p, "0");
1620 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1621 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c)
1623 unsigned len = 1 + isl_basic_set_total_dim(bset);
1624 return print_partial_affine_c(p, dim, bset, c, len);
1627 /* We skip the constraint if it is implied by the div expression.
1629 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1630 __isl_keep isl_space *dim,
1631 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int first)
1637 o_div = isl_basic_set_offset(bset, isl_dim_div);
1638 n_div = isl_basic_set_dim(bset, isl_dim_div);
1639 div = isl_seq_last_non_zero(c + o_div, n_div);
1640 if (div >= 0 && isl_basic_set_is_div_constraint(bset, c, div))
1644 p = isl_printer_print_str(p, " && ");
1646 p = print_affine_c(p, dim, bset, c);
1647 p = isl_printer_print_str(p, " ");
1648 p = isl_printer_print_str(p, op);
1649 p = isl_printer_print_str(p, " 0");
1653 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1654 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset)
1657 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
1658 unsigned total = isl_basic_set_total_dim(bset) - n_div;
1660 for (i = 0; i < bset->n_eq; ++i) {
1661 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
1663 p = print_constraint_c(p, dim, bset,
1664 bset->eq[i], "==", !i);
1667 p = isl_printer_print_str(p, " && ");
1668 p = isl_printer_print_str(p, "(");
1669 p = print_partial_affine_c(p, dim, bset, bset->eq[i],
1671 p = isl_printer_print_str(p, ") % ");
1672 p = isl_printer_print_isl_int(p,
1673 bset->eq[i][1 + total + j]);
1674 p = isl_printer_print_str(p, " == 0");
1677 for (i = 0; i < bset->n_ineq; ++i)
1678 p = print_constraint_c(p, dim, bset, bset->ineq[i], ">=",
1683 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
1684 __isl_keep isl_space *dim, __isl_keep isl_set *set)
1689 return isl_printer_free(p);
1692 p = isl_printer_print_str(p, "0");
1694 for (i = 0; i < set->n; ++i) {
1696 p = isl_printer_print_str(p, " || ");
1698 p = isl_printer_print_str(p, "(");
1699 p = print_basic_set_c(p, dim, set->p[i]);
1701 p = isl_printer_print_str(p, ")");
1706 static __isl_give isl_printer *print_pw_qpolynomial_c(
1707 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1711 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set))
1712 return print_qpolynomial_c(p, pwqp->dim, pwqp->p[0].qp);
1714 for (i = 0; i < pwqp->n; ++i) {
1715 p = isl_printer_print_str(p, "(");
1716 p = print_set_c(p, pwqp->dim, pwqp->p[i].set);
1717 p = isl_printer_print_str(p, ") ? (");
1718 p = print_qpolynomial_c(p, pwqp->dim, pwqp->p[i].qp);
1719 p = isl_printer_print_str(p, ") : ");
1722 p = isl_printer_print_str(p, "0");
1726 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
1727 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1732 if (p->output_format == ISL_FORMAT_ISL)
1733 return print_pw_qpolynomial_isl(p, pwqp);
1734 else if (p->output_format == ISL_FORMAT_C)
1735 return print_pw_qpolynomial_c(p, pwqp);
1736 isl_assert(p->ctx, 0, goto error);
1738 isl_printer_free(p);
1742 static int print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1744 struct isl_union_print_data *data;
1745 data = (struct isl_union_print_data *)user;
1748 data->p = isl_printer_print_str(data->p, "; ");
1751 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
1752 isl_pw_qpolynomial_free(pwqp);
1757 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
1758 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1760 struct isl_union_print_data data = { p, 1 };
1762 dim = isl_union_pw_qpolynomial_get_space(upwqp);
1763 if (isl_space_dim(dim, isl_dim_param) > 0) {
1764 p = print_tuple(dim, p, isl_dim_param, 0, NULL, NULL);
1765 p = isl_printer_print_str(p, " -> ");
1767 isl_space_free(dim);
1768 p = isl_printer_print_str(p, "{ ");
1769 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
1772 p = isl_printer_print_str(p, " }");
1776 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
1777 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1782 if (p->output_format == ISL_FORMAT_ISL)
1783 return print_union_pw_qpolynomial_isl(p, upwqp);
1784 isl_die(p->ctx, isl_error_invalid,
1785 "invalid output format for isl_union_pw_qpolynomial",
1788 isl_printer_free(p);
1792 static __isl_give isl_printer *print_qpolynomial_fold_c(
1793 __isl_take isl_printer *p, __isl_keep isl_space *dim,
1794 __isl_keep isl_qpolynomial_fold *fold)
1798 for (i = 0; i < fold->n - 1; ++i)
1799 if (fold->type == isl_fold_min)
1800 p = isl_printer_print_str(p, "min(");
1801 else if (fold->type == isl_fold_max)
1802 p = isl_printer_print_str(p, "max(");
1804 for (i = 0; i < fold->n; ++i) {
1806 p = isl_printer_print_str(p, ", ");
1807 p = print_qpolynomial_c(p, dim, fold->qp[i]);
1809 p = isl_printer_print_str(p, ")");
1814 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
1815 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
1819 if (p->output_format == ISL_FORMAT_ISL)
1820 return qpolynomial_fold_print(fold, p);
1821 else if (p->output_format == ISL_FORMAT_C)
1822 return print_qpolynomial_fold_c(p, fold->dim, fold);
1823 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
1826 isl_printer_free(p);
1830 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
1831 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1835 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set))
1836 return print_qpolynomial_fold_c(p, pwf->dim, pwf->p[0].fold);
1838 for (i = 0; i < pwf->n; ++i) {
1839 p = isl_printer_print_str(p, "(");
1840 p = print_set_c(p, pwf->dim, pwf->p[i].set);
1841 p = isl_printer_print_str(p, ") ? (");
1842 p = print_qpolynomial_fold_c(p, pwf->dim, pwf->p[i].fold);
1843 p = isl_printer_print_str(p, ") : ");
1846 p = isl_printer_print_str(p, "0");
1850 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
1851 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1856 if (p->output_format == ISL_FORMAT_ISL)
1857 return print_pw_qpolynomial_fold_isl(p, pwf);
1858 else if (p->output_format == ISL_FORMAT_C)
1859 return print_pw_qpolynomial_fold_c(p, pwf);
1860 isl_assert(p->ctx, 0, goto error);
1862 isl_printer_free(p);
1866 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
1867 FILE *out, unsigned output_format)
1874 p = isl_printer_to_file(pwf->dim->ctx, out);
1875 p = isl_printer_set_output_format(p, output_format);
1876 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
1878 isl_printer_free(p);
1881 static int print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf, void *user)
1883 struct isl_union_print_data *data;
1884 data = (struct isl_union_print_data *)user;
1887 data->p = isl_printer_print_str(data->p, "; ");
1890 data->p = isl_pwf_print_isl_body(data->p, pwf);
1891 isl_pw_qpolynomial_fold_free(pwf);
1896 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
1897 __isl_take isl_printer *p,
1898 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1900 struct isl_union_print_data data = { p, 1 };
1902 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
1903 if (isl_space_dim(dim, isl_dim_param) > 0) {
1904 p = print_tuple(dim, p, isl_dim_param, 0, NULL, NULL);
1905 p = isl_printer_print_str(p, " -> ");
1907 isl_space_free(dim);
1908 p = isl_printer_print_str(p, "{ ");
1909 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
1910 &print_pwf_body, &data);
1912 p = isl_printer_print_str(p, " }");
1916 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
1917 __isl_take isl_printer *p,
1918 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1923 if (p->output_format == ISL_FORMAT_ISL)
1924 return print_union_pw_qpolynomial_fold_isl(p, upwf);
1925 isl_die(p->ctx, isl_error_invalid,
1926 "invalid output format for isl_union_pw_qpolynomial_fold",
1929 isl_printer_free(p);
1933 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
1934 __isl_keep isl_constraint *c)
1936 isl_basic_map *bmap;
1941 bmap = isl_basic_map_from_constraint(isl_constraint_copy(c));
1942 p = isl_printer_print_basic_map(p, bmap);
1943 isl_basic_map_free(bmap);
1946 isl_printer_free(p);
1950 static __isl_give isl_printer *isl_printer_print_space_isl(
1951 __isl_take isl_printer *p, __isl_keep isl_space *dim)
1956 if (isl_space_dim(dim, isl_dim_param) > 0) {
1957 p = print_tuple(dim, p, isl_dim_param, 0, NULL, NULL);
1958 p = isl_printer_print_str(p, " -> ");
1961 p = isl_printer_print_str(p, "{ ");
1962 if (isl_space_is_params(dim))
1963 p = isl_printer_print_str(p, s_such_that[0]);
1965 p = print_space(dim, p, 0, 0, NULL, NULL);
1966 p = isl_printer_print_str(p, " }");
1970 isl_printer_free(p);
1974 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
1975 __isl_keep isl_space *space)
1978 return isl_printer_free(p);
1979 if (p->output_format == ISL_FORMAT_ISL)
1980 return isl_printer_print_space_isl(p, space);
1981 else if (p->output_format == ISL_FORMAT_OMEGA)
1982 return print_omega_parameters(space, p);
1984 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
1985 "output format not supported for space",
1986 return isl_printer_free(p));
1989 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
1990 __isl_keep isl_local_space *ls)
1998 total = isl_local_space_dim(ls, isl_dim_all);
1999 if (isl_local_space_dim(ls, isl_dim_param) > 0) {
2000 p = print_tuple(ls->dim, p, isl_dim_param, 0, NULL, NULL);
2001 p = isl_printer_print_str(p, " -> ");
2003 p = isl_printer_print_str(p, "{ ");
2004 p = print_space(ls->dim, p, 0, 0, NULL, NULL);
2005 n_div = isl_local_space_dim(ls, isl_dim_div);
2008 p = isl_printer_print_str(p, " : ");
2009 p = isl_printer_print_str(p, s_open_exists[0]);
2010 for (i = 0; i < n_div; ++i) {
2012 p = isl_printer_print_str(p, ", ");
2013 p = print_name(ls->dim, p, isl_dim_div, i, 0);
2014 if (isl_int_is_zero(ls->div->row[i][0]))
2016 p = isl_printer_print_str(p, " = [(");
2017 p = print_affine_of_len(ls->dim, ls->div, p,
2018 ls->div->row[i] + 1, 1 + total);
2019 p = isl_printer_print_str(p, ")/");
2020 p = isl_printer_print_isl_int(p, ls->div->row[i][0]);
2021 p = isl_printer_print_str(p, "]");
2023 } else if (isl_space_is_params(ls->dim))
2024 p = isl_printer_print_str(p, s_such_that[0]);
2025 p = isl_printer_print_str(p, " }");
2028 isl_printer_free(p);
2032 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2033 __isl_keep isl_aff *aff)
2037 total = isl_local_space_dim(aff->ls, isl_dim_all);
2038 p = isl_printer_print_str(p, "(");
2039 p = print_affine_of_len(aff->ls->dim, aff->ls->div, p,
2040 aff->v->el + 1, 1 + total);
2041 if (isl_int_is_one(aff->v->el[0]))
2042 p = isl_printer_print_str(p, ")");
2044 p = isl_printer_print_str(p, ")/");
2045 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2051 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2052 __isl_keep isl_aff *aff)
2054 if (isl_space_is_params(aff->ls->dim))
2057 p = print_tuple(aff->ls->dim, p, isl_dim_set, 0, NULL, NULL);
2058 p = isl_printer_print_str(p, " -> ");
2060 p = isl_printer_print_str(p, "[");
2061 p = print_aff_body(p, aff);
2062 p = isl_printer_print_str(p, "]");
2067 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2068 __isl_keep isl_aff *aff)
2073 if (isl_local_space_dim(aff->ls, isl_dim_param) > 0) {
2074 p = print_tuple(aff->ls->dim, p, isl_dim_param, 0, NULL, NULL);
2075 p = isl_printer_print_str(p, " -> ");
2077 p = isl_printer_print_str(p, "{ ");
2078 p = print_aff(p, aff);
2079 p = isl_printer_print_str(p, " }");
2082 isl_printer_free(p);
2086 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2087 __isl_keep isl_pw_aff *pwaff)
2094 if (isl_space_dim(pwaff->dim, isl_dim_param) > 0) {
2095 p = print_tuple(pwaff->dim, p, isl_dim_param, 0, NULL, NULL);
2096 p = isl_printer_print_str(p, " -> ");
2098 p = isl_printer_print_str(p, "{ ");
2099 for (i = 0; i < pwaff->n; ++i) {
2101 p = isl_printer_print_str(p, "; ");
2102 p = print_aff(p, pwaff->p[i].aff);
2103 p = print_disjuncts((isl_map *)pwaff->p[i].set, p, 0);
2105 p = isl_printer_print_str(p, " }");
2108 isl_printer_free(p);
2112 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2113 __isl_keep isl_local_space *ls, isl_int *c);
2115 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2116 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2118 if (type == isl_dim_div) {
2119 p = isl_printer_print_str(p, "floord(");
2120 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2121 p = isl_printer_print_str(p, ", ");
2122 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2123 p = isl_printer_print_str(p, ")");
2127 name = isl_space_get_dim_name(ls->dim, type, pos);
2130 p = isl_printer_print_str(p, name);
2135 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2136 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2138 enum isl_dim_type type;
2141 return isl_printer_print_isl_int(p, c);
2143 if (isl_int_is_one(c))
2145 else if (isl_int_is_negone(c))
2146 p = isl_printer_print_str(p, "-");
2148 p = isl_printer_print_isl_int(p, c);
2149 p = isl_printer_print_str(p, "*");
2151 type = pos2type(ls->dim, &pos);
2152 p = print_ls_name_c(p, ls, type, pos);
2156 static __isl_give isl_printer *print_ls_partial_affine_c(
2157 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2158 isl_int *c, unsigned len)
2163 for (i = 0, first = 1; i < len; ++i) {
2165 if (isl_int_is_zero(c[i]))
2168 if (isl_int_is_neg(c[i])) {
2170 isl_int_neg(c[i], c[i]);
2171 p = isl_printer_print_str(p, " - ");
2173 p = isl_printer_print_str(p, " + ");
2176 p = print_ls_term_c(p, ls, c[i], i);
2178 isl_int_neg(c[i], c[i]);
2181 p = isl_printer_print_str(p, "0");
2185 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2186 __isl_keep isl_local_space *ls, isl_int *c)
2188 unsigned len = 1 + isl_local_space_dim(ls, isl_dim_all);
2189 return print_ls_partial_affine_c(p, ls, c, len);
2192 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2193 __isl_keep isl_aff *aff)
2197 total = isl_local_space_dim(aff->ls, isl_dim_all);
2198 if (!isl_int_is_one(aff->v->el[0]))
2199 p = isl_printer_print_str(p, "(");
2200 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2201 if (!isl_int_is_one(aff->v->el[0])) {
2202 p = isl_printer_print_str(p, ")/");
2203 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2208 /* Print the affine expression "aff" in C format to "p".
2209 * The dimension names are taken from "space"
2210 * "set" represents the domain of the affine expression.
2212 static __isl_give isl_printer *print_aff_on_domain_c(__isl_take isl_printer *p,
2213 __isl_keep isl_space *space, __isl_keep isl_aff *aff,
2214 __isl_keep isl_set *set)
2217 isl_ast_build *build;
2220 u = isl_set_universe(isl_space_copy(space));
2221 build = isl_ast_build_from_context(u);
2222 build = isl_ast_build_restrict(build, isl_set_copy(set));
2223 expr = isl_ast_expr_from_aff(isl_aff_copy(aff), build);
2224 p = isl_printer_print_ast_expr(p, expr);
2225 isl_ast_expr_free(expr);
2226 isl_ast_build_free(build);
2231 /* In the C format, we cannot express that "pwaff" may be undefined
2232 * on parts of the domain space. We therefore assume that the expression
2233 * will only be evaluated on its definition domain and compute the gist
2234 * of each cell with respect to this domain.
2236 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2237 __isl_keep isl_pw_aff *pwaff)
2244 isl_die(p->ctx, isl_error_unsupported,
2245 "cannot print empty isl_pw_aff in C format", goto error);
2246 space = isl_pw_aff_get_domain_space(pwaff);
2250 domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
2252 for (i = 0; i < pwaff->n - 1; ++i) {
2255 p = isl_printer_print_str(p, "(");
2257 set_i = isl_set_copy(pwaff->p[i].set);
2258 set_i = isl_set_gist(set_i, isl_set_copy(domain));
2259 p = print_set_c(p, space, set_i);
2260 isl_set_free(set_i);
2262 p = isl_printer_print_str(p, ") ? (");
2263 p = print_aff_on_domain_c(p, space,
2264 pwaff->p[i].aff, pwaff->p[i].set);
2265 p = isl_printer_print_str(p, ") : ");
2268 isl_set_free(domain);
2270 p = print_aff_on_domain_c(p, space, pwaff->p[pwaff->n - 1].aff,
2271 pwaff->p[pwaff->n - 1].set);
2272 isl_space_free(space);
2275 isl_printer_free(p);
2279 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2280 __isl_keep isl_aff *aff)
2285 if (p->output_format == ISL_FORMAT_ISL)
2286 return print_aff_isl(p, aff);
2287 else if (p->output_format == ISL_FORMAT_C)
2288 return print_aff_c(p, aff);
2289 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2292 isl_printer_free(p);
2296 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2297 __isl_keep isl_pw_aff *pwaff)
2302 if (p->output_format == ISL_FORMAT_ISL)
2303 return print_pw_aff_isl(p, pwaff);
2304 else if (p->output_format == ISL_FORMAT_C)
2305 return print_pw_aff_c(p, pwaff);
2306 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2309 isl_printer_free(p);
2313 static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
2314 __isl_keep isl_multi_aff *maff)
2316 return print_space(maff->space, p, 0, 0, NULL, maff);
2319 static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
2320 __isl_keep isl_multi_aff *maff)
2325 if (isl_space_dim(maff->space, isl_dim_param) > 0) {
2326 p = print_tuple(maff->space, p, isl_dim_param, 0, NULL, NULL);
2327 p = isl_printer_print_str(p, " -> ");
2329 p = isl_printer_print_str(p, "{ ");
2330 p = print_multi_aff(p, maff);
2331 p = isl_printer_print_str(p, " }");
2334 isl_printer_free(p);
2338 __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
2339 __isl_keep isl_multi_aff *maff)
2344 if (p->output_format == ISL_FORMAT_ISL)
2345 return print_multi_aff_isl(p, maff);
2346 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2349 isl_printer_free(p);
2353 static __isl_give isl_printer *print_pw_multi_aff_body(
2354 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2361 for (i = 0; i < pma->n; ++i) {
2363 p = isl_printer_print_str(p, "; ");
2364 p = print_multi_aff(p, pma->p[i].maff);
2365 p = print_disjuncts((isl_map *)pma->p[i].set, p, 0);
2369 isl_printer_free(p);
2373 static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
2374 __isl_keep isl_pw_multi_aff *pma)
2379 if (isl_space_dim(pma->dim, isl_dim_param) > 0) {
2380 p = print_tuple(pma->dim, p, isl_dim_param, 0, NULL, NULL);
2381 p = isl_printer_print_str(p, " -> ");
2383 p = isl_printer_print_str(p, "{ ");
2384 p = print_pw_multi_aff_body(p, pma);
2385 p = isl_printer_print_str(p, " }");
2388 isl_printer_free(p);
2392 static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
2393 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2397 for (i = 0; i < pma->n - 1; ++i) {
2398 p = isl_printer_print_str(p, "(");
2399 p = print_set_c(p, pma->dim, pma->p[i].set);
2400 p = isl_printer_print_str(p, ") ? (");
2401 p = print_aff_c(p, pma->p[i].maff->p[0]);
2402 p = isl_printer_print_str(p, ") : ");
2405 return print_aff_c(p, pma->p[pma->n - 1].maff->p[0]);
2408 static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
2409 __isl_keep isl_pw_multi_aff *pma)
2417 isl_die(p->ctx, isl_error_unsupported,
2418 "cannot print empty isl_pw_multi_aff in C format",
2420 name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
2421 if (!name && isl_pw_multi_aff_dim(pma, isl_dim_out) == 1)
2422 return print_unnamed_pw_multi_aff_c(p, pma);
2424 isl_die(p->ctx, isl_error_unsupported,
2425 "cannot print unnamed isl_pw_multi_aff in C format",
2428 p = isl_printer_print_str(p, name);
2429 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
2431 isl_die(p->ctx, isl_error_unsupported,
2432 "not supported yet", goto error);
2436 isl_printer_free(p);
2440 __isl_give isl_printer *isl_printer_print_pw_multi_aff(
2441 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2446 if (p->output_format == ISL_FORMAT_ISL)
2447 return print_pw_multi_aff_isl(p, pma);
2448 if (p->output_format == ISL_FORMAT_C)
2449 return print_pw_multi_aff_c(p, pma);
2450 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2453 isl_printer_free(p);
2457 static int print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
2460 struct isl_union_print_data *data;
2461 data = (struct isl_union_print_data *) user;
2464 data->p = isl_printer_print_str(data->p, "; ");
2467 data->p = print_pw_multi_aff_body(data->p, pma);
2468 isl_pw_multi_aff_free(pma);
2473 static __isl_give isl_printer *print_union_pw_multi_aff_isl(
2474 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2476 struct isl_union_print_data data = { p, 1 };
2479 space = isl_union_pw_multi_aff_get_space(upma);
2480 if (isl_space_dim(space, isl_dim_param) > 0) {
2481 p = print_tuple(space, p, isl_dim_param, 0, NULL, NULL);
2482 p = isl_printer_print_str(p, s_to[0]);
2484 isl_space_free(space);
2485 p = isl_printer_print_str(p, s_open_set[0]);
2486 isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
2487 &print_pw_multi_aff_body_wrap, &data);
2489 p = isl_printer_print_str(p, s_close_set[0]);
2493 __isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
2494 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2499 if (p->output_format == ISL_FORMAT_ISL)
2500 return print_union_pw_multi_aff_isl(p, upma);
2501 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2504 isl_printer_free(p);
2508 static __isl_give isl_printer *print_multi_pw_aff_isl(__isl_take isl_printer *p,
2509 __isl_keep isl_multi_pw_aff *mpa)
2514 return isl_printer_free(p);
2516 p = isl_printer_print_str(p, "(");
2517 for (i = 0; i < mpa->n; ++i) {
2519 p = isl_printer_print_str(p, ",");
2520 p = isl_printer_print_pw_aff(p, mpa->p[i]);
2522 p = isl_printer_print_str(p, ")");
2526 __isl_give isl_printer *isl_printer_print_multi_pw_aff(
2527 __isl_take isl_printer *p, __isl_keep isl_multi_pw_aff *mpa)
2530 return isl_printer_free(p);
2532 if (p->output_format == ISL_FORMAT_ISL)
2533 return print_multi_pw_aff_isl(p, mpa);
2534 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2535 return isl_printer_free(p));