isl_tab_basic_map_partial_lexopt: add memory management annotations
[isl.git] / isl_output.c
blobbe78aa6285015f94348b296cc65caa34174964b9
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <stdlib.h>
16 #include <string.h>
17 #include <isl_ctx_private.h>
18 #include <isl_map_private.h>
19 #include <isl/set.h>
20 #include <isl_seq.h>
21 #include <isl_polynomial_private.h>
22 #include <isl_printer_private.h>
23 #include <isl_space_private.h>
24 #include <isl_mat_private.h>
25 #include <isl_vec_private.h>
26 #include <isl/union_map.h>
27 #include <isl/constraint.h>
28 #include <isl_local_space_private.h>
29 #include <isl_aff_private.h>
30 #include <isl_val_private.h>
31 #include <isl/ast_build.h>
32 #include <isl_sort.h>
33 #include <isl_output_private.h>
35 static const char *s_to[2] = { " -> ", " \\to " };
36 static const char *s_and[2] = { " and ", " \\wedge " };
37 static const char *s_or[2] = { " or ", " \\vee " };
38 static const char *s_le[2] = { "<=", "\\le" };
39 static const char *s_ge[2] = { ">=", "\\ge" };
40 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
41 static const char *s_close_set[2] = { " }", " \\,\\}" };
42 static const char *s_open_list[2] = { "[", "(" };
43 static const char *s_close_list[2] = { "]", ")" };
44 static const char *s_such_that[2] = { " : ", " \\mid " };
45 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
46 static const char *s_close_exists[2] = { ")", "" };
47 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
48 static const char *s_param_prefix[2] = { "p", "p_" };
49 static const char *s_input_prefix[2] = { "i", "i_" };
50 static const char *s_output_prefix[2] = { "o", "o_" };
52 static __isl_give isl_printer *print_constraint_polylib(
53 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
55 int i;
56 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
57 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
58 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
59 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
61 p = isl_printer_start_line(p);
62 p = isl_printer_print_int(p, ineq);
63 for (i = 0; i < n_out; ++i) {
64 p = isl_printer_print_str(p, " ");
65 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
67 for (i = 0; i < n_in; ++i) {
68 p = isl_printer_print_str(p, " ");
69 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
71 for (i = 0; i < bmap->n_div; ++i) {
72 p = isl_printer_print_str(p, " ");
73 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
75 for (i = 0; i < nparam; ++i) {
76 p = isl_printer_print_str(p, " ");
77 p = isl_printer_print_isl_int(p, c[1+i]);
79 p = isl_printer_print_str(p, " ");
80 p = isl_printer_print_isl_int(p, c[0]);
81 p = isl_printer_end_line(p);
82 return p;
85 static __isl_give isl_printer *print_constraints_polylib(
86 struct isl_basic_map *bmap, __isl_take isl_printer *p)
88 int i;
90 p = isl_printer_set_isl_int_width(p, 5);
92 for (i = 0; i < bmap->n_eq; ++i)
93 p = print_constraint_polylib(bmap, 0, i, p);
94 for (i = 0; i < bmap->n_ineq; ++i)
95 p = print_constraint_polylib(bmap, 1, i, p);
97 return p;
100 static __isl_give isl_printer *bset_print_constraints_polylib(
101 struct isl_basic_set *bset, __isl_take isl_printer *p)
103 return print_constraints_polylib((struct isl_basic_map *)bset, p);
106 static __isl_give isl_printer *isl_basic_map_print_polylib(
107 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
109 unsigned total = isl_basic_map_total_dim(bmap);
110 p = isl_printer_start_line(p);
111 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
112 p = isl_printer_print_str(p, " ");
113 p = isl_printer_print_int(p, 1 + total + 1);
114 if (ext) {
115 p = isl_printer_print_str(p, " ");
116 p = isl_printer_print_int(p,
117 isl_basic_map_dim(bmap, isl_dim_out));
118 p = isl_printer_print_str(p, " ");
119 p = isl_printer_print_int(p,
120 isl_basic_map_dim(bmap, isl_dim_in));
121 p = isl_printer_print_str(p, " ");
122 p = isl_printer_print_int(p,
123 isl_basic_map_dim(bmap, isl_dim_div));
124 p = isl_printer_print_str(p, " ");
125 p = isl_printer_print_int(p,
126 isl_basic_map_dim(bmap, isl_dim_param));
128 p = isl_printer_end_line(p);
129 return print_constraints_polylib(bmap, p);
132 static __isl_give isl_printer *isl_basic_set_print_polylib(
133 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
135 return isl_basic_map_print_polylib((struct isl_basic_map *)bset, p, ext);
138 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
139 __isl_take isl_printer *p, int ext)
141 int i;
143 p = isl_printer_start_line(p);
144 p = isl_printer_print_int(p, map->n);
145 p = isl_printer_end_line(p);
146 for (i = 0; i < map->n; ++i) {
147 p = isl_printer_start_line(p);
148 p = isl_printer_end_line(p);
149 p = isl_basic_map_print_polylib(map->p[i], p, ext);
151 return p;
154 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
155 __isl_take isl_printer *p, int ext)
157 return isl_map_print_polylib((struct isl_map *)set, p, ext);
160 static int count_same_name(__isl_keep isl_space *dim,
161 enum isl_dim_type type, unsigned pos, const char *name)
163 enum isl_dim_type t;
164 unsigned p, s;
165 int count = 0;
167 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
168 s = t == type ? pos : isl_space_dim(dim, t);
169 for (p = 0; p < s; ++p) {
170 const char *n = isl_space_get_dim_name(dim, t, p);
171 if (n && !strcmp(n, name))
172 count++;
175 return count;
178 /* Print the name of the variable of type "type" and position "pos"
179 * in "space" to "p".
181 static __isl_give isl_printer *print_name(__isl_keep isl_space *space,
182 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
183 int latex)
185 const char *name;
186 char buffer[20];
187 int primes;
189 name = type == isl_dim_div ? NULL
190 : isl_space_get_dim_name(space, type, pos);
192 if (!name) {
193 const char *prefix;
194 if (type == isl_dim_param)
195 prefix = s_param_prefix[latex];
196 else if (type == isl_dim_div)
197 prefix = s_div_prefix[latex];
198 else if (isl_space_is_set(space) || type == isl_dim_in)
199 prefix = s_input_prefix[latex];
200 else
201 prefix = s_output_prefix[latex];
202 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
203 name = buffer;
205 primes = count_same_name(space, name == buffer ? isl_dim_div : type,
206 pos, name);
207 p = isl_printer_print_str(p, name);
208 while (primes-- > 0)
209 p = isl_printer_print_str(p, "'");
210 return p;
213 static enum isl_dim_type pos2type(__isl_keep isl_space *dim, unsigned *pos)
215 enum isl_dim_type type;
216 unsigned n_in = isl_space_dim(dim, isl_dim_in);
217 unsigned n_out = isl_space_dim(dim, isl_dim_out);
218 unsigned nparam = isl_space_dim(dim, isl_dim_param);
220 if (*pos < 1 + nparam) {
221 type = isl_dim_param;
222 *pos -= 1;
223 } else if (*pos < 1 + nparam + n_in) {
224 type = isl_dim_in;
225 *pos -= 1 + nparam;
226 } else if (*pos < 1 + nparam + n_in + n_out) {
227 type = isl_dim_out;
228 *pos -= 1 + nparam + n_in;
229 } else {
230 type = isl_dim_div;
231 *pos -= 1 + nparam + n_in + n_out;
234 return type;
237 /* Can the div expression of the integer division at position "row" of "div"
238 * be printed?
239 * In particular, are the div expressions available and does the selected
240 * variable have a known explicit representation?
241 * Furthermore, the Omega format does not allow any div expressions
242 * to be printed.
244 static isl_bool can_print_div_expr(__isl_keep isl_printer *p,
245 __isl_keep isl_mat *div, int pos)
247 if (p->output_format == ISL_FORMAT_OMEGA)
248 return isl_bool_false;
249 if (!div)
250 return isl_bool_false;
251 return !isl_int_is_zero(div->row[pos][0]);
254 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
255 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
257 static __isl_give isl_printer *print_term(__isl_keep isl_space *space,
258 __isl_keep isl_mat *div,
259 isl_int c, unsigned pos, __isl_take isl_printer *p, int latex)
261 enum isl_dim_type type;
262 int print_div_def;
264 if (pos == 0)
265 return isl_printer_print_isl_int(p, c);
267 type = pos2type(space, &pos);
268 print_div_def = type == isl_dim_div && can_print_div_expr(p, div, pos);
270 if (isl_int_is_one(c))
272 else if (isl_int_is_negone(c))
273 p = isl_printer_print_str(p, "-");
274 else {
275 p = isl_printer_print_isl_int(p, c);
276 if (p->output_format == ISL_FORMAT_C || print_div_def)
277 p = isl_printer_print_str(p, "*");
279 if (print_div_def)
280 p = print_div(space, div, pos, p);
281 else
282 p = print_name(space, p, type, pos, latex);
283 return p;
286 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_space *dim,
287 __isl_keep isl_mat *div,
288 __isl_take isl_printer *p, isl_int *c, int len)
290 int i;
291 int first;
293 for (i = 0, first = 1; i < len; ++i) {
294 int flip = 0;
295 if (isl_int_is_zero(c[i]))
296 continue;
297 if (!first) {
298 if (isl_int_is_neg(c[i])) {
299 flip = 1;
300 isl_int_neg(c[i], c[i]);
301 p = isl_printer_print_str(p, " - ");
302 } else
303 p = isl_printer_print_str(p, " + ");
305 first = 0;
306 p = print_term(dim, div, c[i], i, p, 0);
307 if (flip)
308 isl_int_neg(c[i], c[i]);
310 if (first)
311 p = isl_printer_print_str(p, "0");
312 return p;
315 /* Print an affine expression "c" corresponding to a constraint in "bmap"
316 * to "p", with the variable names taken from "space" and
317 * the integer division definitions taken from "div".
319 static __isl_give isl_printer *print_affine(__isl_keep isl_basic_map *bmap,
320 __isl_keep isl_space *space, __isl_keep isl_mat *div,
321 __isl_take isl_printer *p, isl_int *c)
323 unsigned len = 1 + isl_basic_map_total_dim(bmap);
324 return print_affine_of_len(space, div, p, c, len);
327 /* offset is the offset of local_dim inside data->type of data->space.
329 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
330 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
331 struct isl_print_space_data *data, int offset)
333 int i;
335 if (data->space != local_dim && local_type == isl_dim_out)
336 offset += local_dim->n_in;
338 for (i = 0; i < isl_space_dim(local_dim, local_type); ++i) {
339 if (i)
340 p = isl_printer_print_str(p, ", ");
341 if (data->print_dim)
342 p = data->print_dim(p, data, offset + i);
343 else
344 p = print_name(data->space, p, data->type, offset + i,
345 data->latex);
347 return p;
350 static __isl_give isl_printer *print_var_list(__isl_take isl_printer *p,
351 __isl_keep isl_space *space, enum isl_dim_type type)
353 struct isl_print_space_data data = { .space = space, .type = type };
355 return print_nested_var_list(p, space, type, &data, 0);
358 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
359 __isl_keep isl_space *local_dim,
360 struct isl_print_space_data *data, int offset);
362 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
363 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
364 struct isl_print_space_data *data, int offset)
366 const char *name = NULL;
367 unsigned n = isl_space_dim(local_dim, local_type);
368 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
369 name = isl_space_get_tuple_name(local_dim, local_type);
370 if (name) {
371 if (data->latex)
372 p = isl_printer_print_str(p, "\\mathrm{");
373 p = isl_printer_print_str(p, name);
374 if (data->latex)
375 p = isl_printer_print_str(p, "}");
378 if (!data->latex || n != 1 || name)
379 p = isl_printer_print_str(p, s_open_list[data->latex]);
380 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
381 local_dim->nested[local_type - isl_dim_in]) {
382 if (data->space != local_dim && local_type == isl_dim_out)
383 offset += local_dim->n_in;
384 p = print_nested_map_dim(p,
385 local_dim->nested[local_type - isl_dim_in],
386 data, offset);
387 } else
388 p = print_nested_var_list(p, local_dim, local_type, data,
389 offset);
390 if (!data->latex || n != 1 || name)
391 p = isl_printer_print_str(p, s_close_list[data->latex]);
392 return p;
395 static __isl_give isl_printer *print_tuple(__isl_keep isl_space *dim,
396 __isl_take isl_printer *p, enum isl_dim_type type,
397 struct isl_print_space_data *data)
399 data->space = dim;
400 data->type = type;
401 return print_nested_tuple(p, dim, type, data, 0);
404 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
405 __isl_keep isl_space *local_dim,
406 struct isl_print_space_data *data, int offset)
408 p = print_nested_tuple(p, local_dim, isl_dim_in, data, offset);
409 p = isl_printer_print_str(p, s_to[data->latex]);
410 p = print_nested_tuple(p, local_dim, isl_dim_out, data, offset);
412 return p;
415 __isl_give isl_printer *isl_print_space(__isl_keep isl_space *space,
416 __isl_take isl_printer *p, int rational,
417 struct isl_print_space_data *data)
419 if (rational && !data->latex)
420 p = isl_printer_print_str(p, "rat: ");
421 if (isl_space_is_params(space))
423 else if (isl_space_is_set(space))
424 p = print_tuple(space, p, isl_dim_set, data);
425 else {
426 p = print_tuple(space, p, isl_dim_in, data);
427 p = isl_printer_print_str(p, s_to[data->latex]);
428 p = print_tuple(space, p, isl_dim_out, data);
431 return p;
434 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_space *dim,
435 __isl_take isl_printer *p)
437 if (isl_space_dim(dim, isl_dim_param) == 0)
438 return p;
440 p = isl_printer_start_line(p);
441 p = isl_printer_print_str(p, "symbolic ");
442 p = print_var_list(p, dim, isl_dim_param);
443 p = isl_printer_print_str(p, ";");
444 p = isl_printer_end_line(p);
445 return p;
448 /* Does the inequality constraint following "i" in "bmap"
449 * have an opposite value for the same last coefficient?
450 * "last" is the position of the last coefficient of inequality "i".
451 * If the next constraint is a div constraint, then it is ignored
452 * since div constraints are not printed.
454 static int next_is_opposite(__isl_keep isl_basic_map *bmap, int i, int last)
456 unsigned total = isl_basic_map_total_dim(bmap);
457 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
459 if (i + 1 >= bmap->n_ineq)
460 return 0;
461 if (isl_seq_last_non_zero(bmap->ineq[i + 1], 1 + total) != last)
462 return 0;
463 if (last >= o_div &&
464 isl_basic_map_is_div_constraint(bmap, bmap->ineq[i + 1],
465 last - o_div))
466 return 0;
467 return isl_int_abs_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]) &&
468 !isl_int_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]);
471 /* Return a string representation of the operator used when
472 * printing a constraint where the LHS is greater than or equal to the LHS
473 * (sign > 0) or smaller than or equal to the LHS (sign < 0).
474 * If "strict" is set, then return the strict version of the comparison
475 * operator.
477 static const char *constraint_op(int sign, int strict, int latex)
479 if (strict)
480 return sign < 0 ? "<" : ">";
481 if (sign < 0)
482 return s_le[latex];
483 else
484 return s_ge[latex];
487 /* Print one side of a constraint "c" from "bmap" to "p", with
488 * the variable names taken from "space" and the integer division definitions
489 * taken from "div".
490 * "last" is the position of the last non-zero coefficient.
491 * Let c' be the result of zeroing out this coefficient, then
492 * the partial constraint
494 * c' op
496 * is printed.
497 * "first_constraint" is set if this is the first constraint
498 * in the conjunction.
500 static __isl_give isl_printer *print_half_constraint(struct isl_basic_map *bmap,
501 __isl_keep isl_space *space, __isl_keep isl_mat *div,
502 __isl_take isl_printer *p, isl_int *c, int last, const char *op,
503 int first_constraint, int latex)
505 if (!first_constraint)
506 p = isl_printer_print_str(p, s_and[latex]);
508 isl_int_set_si(c[last], 0);
509 p = print_affine(bmap, space, div, p, c);
511 p = isl_printer_print_str(p, " ");
512 p = isl_printer_print_str(p, op);
513 p = isl_printer_print_str(p, " ");
515 return p;
518 /* Print a constraint "c" from "bmap" to "p", with the variable names
519 * taken from "space" and the integer division definitions taken from "div".
520 * "last" is the position of the last non-zero coefficient, which is
521 * moreover assumed to be negative.
522 * Let c' be the result of zeroing out this coefficient, then
523 * the constraint is printed in the form
525 * -c[last] op c'
527 * "first_constraint" is set if this is the first constraint
528 * in the conjunction.
530 static __isl_give isl_printer *print_constraint(struct isl_basic_map *bmap,
531 __isl_keep isl_space *space, __isl_keep isl_mat *div,
532 __isl_take isl_printer *p,
533 isl_int *c, int last, const char *op, int first_constraint, int latex)
535 if (!first_constraint)
536 p = isl_printer_print_str(p, s_and[latex]);
538 isl_int_abs(c[last], c[last]);
540 p = print_term(space, div, c[last], last, p, latex);
542 p = isl_printer_print_str(p, " ");
543 p = isl_printer_print_str(p, op);
544 p = isl_printer_print_str(p, " ");
546 isl_int_set_si(c[last], 0);
547 p = print_affine(bmap, space, div, p, c);
549 return p;
552 /* Print the constraints of "bmap" to "p".
553 * The names of the variables are taken from "space" and
554 * the integer division definitions are taken from "div".
555 * Div constraints are only printed in "dump" mode.
556 * The constraints are sorted prior to printing (except in "dump" mode).
558 * If x is the last variable with a non-zero coefficient,
559 * then a lower bound
561 * f - a x >= 0
563 * is printed as
565 * a x <= f
567 * while an upper bound
569 * f + a x >= 0
571 * is printed as
573 * a x >= -f
575 * If the next constraint has an opposite sign for the same last coefficient,
576 * then it is printed as
578 * f >= a x
580 * or
582 * -f <= a x
584 * instead. In fact, the "a x" part is not printed explicitly, but
585 * reused from the next constraint, which is therefore treated as
586 * a first constraint in the conjunction.
588 * If the constant term of "f" is -1, then "f" is replaced by "f + 1" and
589 * the comparison operator is replaced by the strict variant.
590 * Essentially, ">= 1" is replaced by "> 0".
592 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
593 __isl_keep isl_space *space, __isl_keep isl_mat *div,
594 __isl_take isl_printer *p, int latex)
596 int i;
597 isl_vec *c = NULL;
598 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
599 unsigned total = isl_basic_map_total_dim(bmap);
600 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
601 int first = 1;
603 bmap = isl_basic_map_copy(bmap);
604 if (!p->dump)
605 bmap = isl_basic_map_sort_constraints(bmap);
606 if (!bmap)
607 goto error;
609 c = isl_vec_alloc(bmap->ctx, 1 + total);
610 if (!c)
611 goto error;
613 for (i = bmap->n_eq - 1; i >= 0; --i) {
614 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
615 if (l < 0) {
616 if (i != bmap->n_eq - 1)
617 p = isl_printer_print_str(p, s_and[latex]);
618 p = isl_printer_print_str(p, "0 = 0");
619 continue;
621 if (isl_int_is_neg(bmap->eq[i][l]))
622 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
623 else
624 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
625 p = print_constraint(bmap, space, div, p, c->el, l,
626 "=", first, latex);
627 first = 0;
629 for (i = 0; i < bmap->n_ineq; ++i) {
630 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
631 int strict;
632 int s;
633 const char *op;
634 if (l < 0)
635 continue;
636 if (!p->dump && l >= o_div &&
637 can_print_div_expr(p, div, l - o_div) &&
638 isl_basic_map_is_div_constraint(bmap, bmap->ineq[i],
639 l - o_div))
640 continue;
641 s = isl_int_sgn(bmap->ineq[i][l]);
642 strict = !rational && isl_int_is_negone(bmap->ineq[i][0]);
643 if (s < 0)
644 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
645 else
646 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
647 if (strict)
648 isl_int_set_si(c->el[0], 0);
649 if (!p->dump && next_is_opposite(bmap, i, l)) {
650 op = constraint_op(-s, strict, latex);
651 p = print_half_constraint(bmap, space, div, p, c->el, l,
652 op, first, latex);
653 first = 1;
654 } else {
655 op = constraint_op(s, strict, latex);
656 p = print_constraint(bmap, space, div, p, c->el, l,
657 op, first, latex);
658 first = 0;
662 isl_basic_map_free(bmap);
663 isl_vec_free(c);
665 return p;
666 error:
667 isl_basic_map_free(bmap);
668 isl_vec_free(c);
669 isl_printer_free(p);
670 return NULL;
673 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
674 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
676 int c;
678 if (!p || !div)
679 return isl_printer_free(p);
681 c = p->output_format == ISL_FORMAT_C;
682 p = isl_printer_print_str(p, c ? "floord(" : "floor((");
683 p = print_affine_of_len(dim, div, p,
684 div->row[pos] + 1, div->n_col - 1);
685 p = isl_printer_print_str(p, c ? ", " : ")/");
686 p = isl_printer_print_isl_int(p, div->row[pos][0]);
687 p = isl_printer_print_str(p, ")");
688 return p;
691 /* Print a comma separated list of div names, except those that have
692 * a definition that can be printed.
693 * If "print_defined_divs" is set, then those div names are printed
694 * as well, along with their definitions.
696 static __isl_give isl_printer *print_div_list(__isl_take isl_printer *p,
697 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex,
698 int print_defined_divs)
700 int i;
701 int first = 1;
702 unsigned n_div;
704 if (!p || !space || !div)
705 return isl_printer_free(p);
707 n_div = isl_mat_rows(div);
709 for (i = 0; i < n_div; ++i) {
710 if (!print_defined_divs && can_print_div_expr(p, div, i))
711 continue;
712 if (!first)
713 p = isl_printer_print_str(p, ", ");
714 p = print_name(space, p, isl_dim_div, i, latex);
715 first = 0;
716 if (!can_print_div_expr(p, div, i))
717 continue;
718 p = isl_printer_print_str(p, " = ");
719 p = print_div(space, div, i, p);
722 return p;
725 /* Does printing "bmap" require an "exists" clause?
726 * That is, are there any local variables without an explicit representation?
728 static isl_bool need_exists(__isl_keep isl_printer *p,
729 __isl_keep isl_basic_map *bmap, __isl_keep isl_mat *div)
731 int i;
733 if (!p || !bmap)
734 return isl_bool_error;
735 if (bmap->n_div == 0)
736 return isl_bool_false;
737 for (i = 0; i < bmap->n_div; ++i)
738 if (!can_print_div_expr(p, div, i))
739 return isl_bool_true;
740 return isl_bool_false;
743 /* Print the constraints of "bmap" to "p".
744 * The names of the variables are taken from "space".
745 * "latex" is set if the constraints should be printed in LaTeX format.
746 * Do not print inline explicit div representations in "dump" mode.
748 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
749 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
751 isl_mat *div;
752 isl_bool exists;
754 if (!p)
755 return NULL;
756 div = isl_basic_map_get_divs(bmap);
757 if (p->dump)
758 exists = bmap->n_div > 0;
759 else
760 exists = need_exists(p, bmap, div);
761 if (exists >= 0 && exists) {
762 p = isl_printer_print_str(p, s_open_exists[latex]);
763 p = print_div_list(p, space, div, latex, p->dump);
764 p = isl_printer_print_str(p, ": ");
767 if (p->dump)
768 div = isl_mat_free(div);
769 p = print_constraints(bmap, space, div, p, latex);
770 isl_mat_free(div);
772 if (exists >= 0 && exists)
773 p = isl_printer_print_str(p, s_close_exists[latex]);
774 return p;
777 /* Print a colon followed by the constraints of "bmap"
778 * to "p", provided there are any constraints.
779 * The names of the variables are taken from "space".
780 * "latex" is set if the constraints should be printed in LaTeX format.
782 static __isl_give isl_printer *print_optional_disjunct(
783 __isl_keep isl_basic_map *bmap, __isl_keep isl_space *space,
784 __isl_take isl_printer *p, int latex)
786 if (isl_basic_map_plain_is_universe(bmap))
787 return p;
789 p = isl_printer_print_str(p, ": ");
790 p = print_disjunct(bmap, space, p, latex);
792 return p;
795 static __isl_give isl_printer *basic_map_print_omega(
796 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
798 p = isl_printer_print_str(p, "{ [");
799 p = print_var_list(p, bmap->dim, isl_dim_in);
800 p = isl_printer_print_str(p, "] -> [");
801 p = print_var_list(p, bmap->dim, isl_dim_out);
802 p = isl_printer_print_str(p, "] ");
803 p = print_optional_disjunct(bmap, bmap->dim, p, 0);
804 p = isl_printer_print_str(p, " }");
805 return p;
808 static __isl_give isl_printer *basic_set_print_omega(
809 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
811 p = isl_printer_print_str(p, "{ [");
812 p = print_var_list(p, bset->dim, isl_dim_set);
813 p = isl_printer_print_str(p, "] ");
814 p = print_optional_disjunct(bset, bset->dim, p, 0);
815 p = isl_printer_print_str(p, " }");
816 return p;
819 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
820 __isl_take isl_printer *p)
822 int i;
824 for (i = 0; i < map->n; ++i) {
825 if (i)
826 p = isl_printer_print_str(p, " union ");
827 p = basic_map_print_omega(map->p[i], p);
829 return p;
832 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
833 __isl_take isl_printer *p)
835 int i;
837 for (i = 0; i < set->n; ++i) {
838 if (i)
839 p = isl_printer_print_str(p, " union ");
840 p = basic_set_print_omega(set->p[i], p);
842 return p;
845 static __isl_give isl_printer *isl_basic_map_print_isl(
846 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
847 int latex)
849 struct isl_print_space_data data = { .latex = latex };
850 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
852 if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
853 p = print_tuple(bmap->dim, p, isl_dim_param, &data);
854 p = isl_printer_print_str(p, " -> ");
856 p = isl_printer_print_str(p, "{ ");
857 p = isl_print_space(bmap->dim, p, rational, &data);
858 p = isl_printer_print_str(p, " : ");
859 p = print_disjunct(bmap, bmap->dim, p, latex);
860 p = isl_printer_print_str(p, " }");
861 return p;
864 /* Print the disjuncts of a map (or set) "map" to "p".
865 * The names of the variables are taken from "space".
866 * "latex" is set if the constraints should be printed in LaTeX format.
868 static __isl_give isl_printer *print_disjuncts_core(__isl_keep isl_map *map,
869 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
871 int i;
873 if (map->n == 0)
874 p = isl_printer_print_str(p, "1 = 0");
875 for (i = 0; i < map->n; ++i) {
876 if (i)
877 p = isl_printer_print_str(p, s_or[latex]);
878 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
879 p = isl_printer_print_str(p, "(");
880 p = print_disjunct(map->p[i], space, p, latex);
881 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
882 p = isl_printer_print_str(p, ")");
884 return p;
887 /* Print the disjuncts of a map (or set) "map" to "p".
888 * The names of the variables are taken from "space".
889 * "hull" describes constraints shared by all disjuncts of "map".
890 * "latex" is set if the constraints should be printed in LaTeX format.
892 * Print the disjuncts as a conjunction of "hull" and
893 * the result of removing the constraints of "hull" from "map".
894 * If this result turns out to be the universe, then simply print "hull".
896 static __isl_give isl_printer *print_disjuncts_in_hull(__isl_keep isl_map *map,
897 __isl_keep isl_space *space, __isl_take isl_basic_map *hull,
898 __isl_take isl_printer *p, int latex)
900 isl_bool is_universe;
902 p = print_disjunct(hull, space, p, latex);
903 map = isl_map_plain_gist_basic_map(isl_map_copy(map), hull);
904 is_universe = isl_map_plain_is_universe(map);
905 if (is_universe < 0)
906 goto error;
907 if (!is_universe) {
908 p = isl_printer_print_str(p, s_and[latex]);
909 p = isl_printer_print_str(p, "(");
910 p = print_disjuncts_core(map, space, p, latex);
911 p = isl_printer_print_str(p, ")");
913 isl_map_free(map);
915 return p;
916 error:
917 isl_map_free(map);
918 isl_printer_free(p);
919 return NULL;
922 /* Print the disjuncts of a map (or set) "map" to "p".
923 * The names of the variables are taken from "space".
924 * "latex" is set if the constraints should be printed in LaTeX format.
926 * If there are at least two disjuncts and "dump" mode is not turned out,
927 * check for any shared constraints among all disjuncts.
928 * If there are any, then print them separately in print_disjuncts_in_hull.
930 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
931 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
933 if (isl_map_plain_is_universe(map))
934 return p;
936 p = isl_printer_print_str(p, s_such_that[latex]);
937 if (!p)
938 return NULL;
940 if (!p->dump && map->n >= 2) {
941 isl_basic_map *hull;
942 isl_bool is_universe;
944 hull = isl_map_plain_unshifted_simple_hull(isl_map_copy(map));
945 is_universe = isl_basic_map_plain_is_universe(hull);
946 if (is_universe < 0)
947 p = isl_printer_free(p);
948 else if (!is_universe)
949 return print_disjuncts_in_hull(map, space, hull,
950 p, latex);
951 isl_basic_map_free(hull);
954 return print_disjuncts_core(map, space, p, latex);
957 /* Print the disjuncts of a map (or set).
958 * The names of the variables are taken from "space".
959 * "latex" is set if the constraints should be printed in LaTeX format.
961 * If the map turns out to be a universal parameter domain, then
962 * we need to print the colon. Otherwise, the output looks identical
963 * to the empty set.
965 static __isl_give isl_printer *print_disjuncts_map(__isl_keep isl_map *map,
966 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
968 if (isl_map_plain_is_universe(map) && isl_space_is_params(map->dim))
969 return isl_printer_print_str(p, s_such_that[latex]);
970 else
971 return print_disjuncts(map, space, p, latex);
974 struct isl_aff_split {
975 isl_basic_map *aff;
976 isl_map *map;
979 static void free_split(__isl_take struct isl_aff_split *split, int n)
981 int i;
983 if (!split)
984 return;
986 for (i = 0; i < n; ++i) {
987 isl_basic_map_free(split[i].aff);
988 isl_map_free(split[i].map);
991 free(split);
994 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
996 int i, j;
997 unsigned nparam, n_in, n_out, total;
999 bmap = isl_basic_map_cow(bmap);
1000 if (!bmap)
1001 return NULL;
1002 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
1003 goto error;
1005 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1006 n_in = isl_basic_map_dim(bmap, isl_dim_in);
1007 n_out = isl_basic_map_dim(bmap, isl_dim_out);
1008 total = isl_basic_map_dim(bmap, isl_dim_all);
1009 for (i = bmap->n_eq - 1; i >= 0; --i) {
1010 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
1011 if (j >= nparam && j < nparam + n_in + n_out &&
1012 (isl_int_is_one(bmap->eq[i][1 + j]) ||
1013 isl_int_is_negone(bmap->eq[i][1 + j])))
1014 continue;
1015 if (isl_basic_map_drop_equality(bmap, i) < 0)
1016 goto error;
1019 bmap = isl_basic_map_finalize(bmap);
1021 return bmap;
1022 error:
1023 isl_basic_map_free(bmap);
1024 return NULL;
1027 static int aff_split_cmp(const void *p1, const void *p2, void *user)
1029 const struct isl_aff_split *s1, *s2;
1030 s1 = (const struct isl_aff_split *) p1;
1031 s2 = (const struct isl_aff_split *) p2;
1033 return isl_basic_map_plain_cmp(s1->aff, s2->aff);
1036 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
1037 __isl_keep isl_basic_map *aff)
1039 int i, j;
1040 unsigned total;
1042 if (!bmap || !aff)
1043 goto error;
1045 total = isl_space_dim(bmap->dim, isl_dim_all);
1047 for (i = bmap->n_eq - 1; i >= 0; --i) {
1048 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
1049 bmap->n_div) != -1)
1050 continue;
1051 for (j = 0; j < aff->n_eq; ++j) {
1052 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
1053 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
1054 continue;
1055 if (isl_basic_map_drop_equality(bmap, i) < 0)
1056 goto error;
1057 break;
1061 return bmap;
1062 error:
1063 isl_basic_map_free(bmap);
1064 return NULL;
1067 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
1069 int i, n;
1070 struct isl_aff_split *split;
1071 isl_ctx *ctx;
1073 ctx = isl_map_get_ctx(map);
1074 split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
1075 if (!split)
1076 return NULL;
1078 for (i = 0; i < map->n; ++i) {
1079 isl_basic_map *bmap;
1080 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
1081 bmap = isl_basic_map_copy(map->p[i]);
1082 bmap = isl_basic_map_cow(bmap);
1083 bmap = drop_aff(bmap, split[i].aff);
1084 split[i].map = isl_map_from_basic_map(bmap);
1085 if (!split[i].aff || !split[i].map)
1086 goto error;
1089 if (isl_sort(split, map->n, sizeof(struct isl_aff_split),
1090 &aff_split_cmp, NULL) < 0)
1091 goto error;
1093 n = map->n;
1094 for (i = n - 1; i >= 1; --i) {
1095 if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
1096 split[i].aff))
1097 continue;
1098 isl_basic_map_free(split[i].aff);
1099 split[i - 1].map = isl_map_union(split[i - 1].map,
1100 split[i].map);
1101 if (i != n - 1)
1102 split[i] = split[n - 1];
1103 split[n - 1].aff = NULL;
1104 split[n - 1].map = NULL;
1105 --n;
1108 return split;
1109 error:
1110 free_split(split, map->n);
1111 return NULL;
1114 static int defining_equality(__isl_keep isl_basic_map *eq,
1115 __isl_keep isl_space *dim, enum isl_dim_type type, int pos)
1117 int i;
1118 unsigned total;
1120 if (!eq)
1121 return -1;
1123 pos += isl_space_offset(dim, type);
1124 total = isl_basic_map_total_dim(eq);
1126 for (i = 0; i < eq->n_eq; ++i) {
1127 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
1128 continue;
1129 if (isl_int_is_one(eq->eq[i][1 + pos]))
1130 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
1131 return i;
1134 return -1;
1137 /* Print dimension "pos" of data->space to "p".
1139 * data->user is assumed to be an isl_basic_map keeping track of equalities.
1141 * If the current dimension is defined by these equalities, then print
1142 * the corresponding expression, assigned to the name of the dimension
1143 * if there is any. Otherwise, print the name of the dimension.
1145 static __isl_give isl_printer *print_dim_eq(__isl_take isl_printer *p,
1146 struct isl_print_space_data *data, unsigned pos)
1148 isl_basic_map *eq = data->user;
1149 int j;
1151 j = defining_equality(eq, data->space, data->type, pos);
1152 if (j >= 0) {
1153 if (isl_space_has_dim_name(data->space, data->type, pos)) {
1154 p = print_name(data->space, p, data->type, pos,
1155 data->latex);
1156 p = isl_printer_print_str(p, " = ");
1158 pos += 1 + isl_space_offset(data->space, data->type);
1159 p = print_affine_of_len(data->space, NULL, p, eq->eq[j], pos);
1160 } else {
1161 p = print_name(data->space, p, data->type, pos, data->latex);
1164 return p;
1167 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
1168 struct isl_aff_split *split, int n, __isl_keep isl_space *space)
1170 struct isl_print_space_data data = { 0 };
1171 int i;
1172 int rational;
1174 data.print_dim = &print_dim_eq;
1175 for (i = 0; i < n; ++i) {
1176 if (!split[i].map)
1177 break;
1178 rational = split[i].map->n > 0 &&
1179 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
1180 if (i)
1181 p = isl_printer_print_str(p, "; ");
1182 data.user = split[i].aff;
1183 p = isl_print_space(space, p, rational, &data);
1184 p = print_disjuncts_map(split[i].map, space, p, 0);
1187 return p;
1190 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
1191 __isl_take isl_printer *p)
1193 struct isl_print_space_data data = { 0 };
1194 struct isl_aff_split *split = NULL;
1195 int rational;
1197 if (!p->dump && map->n > 0)
1198 split = split_aff(map);
1199 if (split) {
1200 p = print_split_map(p, split, map->n, map->dim);
1201 } else {
1202 rational = map->n > 0 &&
1203 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
1204 p = isl_print_space(map->dim, p, rational, &data);
1205 p = print_disjuncts_map(map, map->dim, p, 0);
1207 free_split(split, map->n);
1208 return p;
1211 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
1212 __isl_take isl_printer *p)
1214 struct isl_print_space_data data = { 0 };
1216 if (isl_map_dim(map, isl_dim_param) > 0) {
1217 p = print_tuple(map->dim, p, isl_dim_param, &data);
1218 p = isl_printer_print_str(p, s_to[0]);
1220 p = isl_printer_print_str(p, s_open_set[0]);
1221 p = isl_map_print_isl_body(map, p);
1222 p = isl_printer_print_str(p, s_close_set[0]);
1223 return p;
1226 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
1227 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff)
1229 struct isl_print_space_data data = { 0 };
1231 data.latex = 1;
1232 if (isl_map_dim(map, isl_dim_param) > 0) {
1233 p = print_tuple(map->dim, p, isl_dim_param, &data);
1234 p = isl_printer_print_str(p, s_to[1]);
1236 p = isl_printer_print_str(p, s_open_set[1]);
1237 data.print_dim = &print_dim_eq;
1238 data.user = aff;
1239 p = isl_print_space(map->dim, p, 0, &data);
1240 p = print_disjuncts_map(map, map->dim, p, 1);
1241 p = isl_printer_print_str(p, s_close_set[1]);
1243 return p;
1246 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
1247 __isl_take isl_printer *p)
1249 int i;
1250 struct isl_aff_split *split = NULL;
1252 if (map->n > 0)
1253 split = split_aff(map);
1255 if (!split)
1256 return print_latex_map(map, p, NULL);
1258 for (i = 0; i < map->n; ++i) {
1259 if (!split[i].map)
1260 break;
1261 if (i)
1262 p = isl_printer_print_str(p, " \\cup ");
1263 p = print_latex_map(split[i].map, p, split[i].aff);
1266 free_split(split, map->n);
1267 return p;
1270 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
1271 __isl_keep isl_basic_map *bmap)
1273 if (!p || !bmap)
1274 goto error;
1275 if (p->output_format == ISL_FORMAT_ISL)
1276 return isl_basic_map_print_isl(bmap, p, 0);
1277 else if (p->output_format == ISL_FORMAT_OMEGA)
1278 return basic_map_print_omega(bmap, p);
1279 isl_assert(bmap->ctx, 0, goto error);
1280 error:
1281 isl_printer_free(p);
1282 return NULL;
1285 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
1286 __isl_keep isl_basic_set *bset)
1288 if (!p || !bset)
1289 goto error;
1291 if (p->output_format == ISL_FORMAT_ISL)
1292 return isl_basic_map_print_isl(bset, p, 0);
1293 else if (p->output_format == ISL_FORMAT_POLYLIB)
1294 return isl_basic_set_print_polylib(bset, p, 0);
1295 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1296 return isl_basic_set_print_polylib(bset, p, 1);
1297 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
1298 return bset_print_constraints_polylib(bset, p);
1299 else if (p->output_format == ISL_FORMAT_OMEGA)
1300 return basic_set_print_omega(bset, p);
1301 isl_assert(p->ctx, 0, goto error);
1302 error:
1303 isl_printer_free(p);
1304 return NULL;
1307 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
1308 __isl_keep isl_set *set)
1310 if (!p || !set)
1311 goto error;
1312 if (p->output_format == ISL_FORMAT_ISL)
1313 return isl_map_print_isl((isl_map *)set, p);
1314 else if (p->output_format == ISL_FORMAT_POLYLIB)
1315 return isl_set_print_polylib(set, p, 0);
1316 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1317 return isl_set_print_polylib(set, p, 1);
1318 else if (p->output_format == ISL_FORMAT_OMEGA)
1319 return isl_set_print_omega(set, p);
1320 else if (p->output_format == ISL_FORMAT_LATEX)
1321 return isl_map_print_latex((isl_map *)set, p);
1322 isl_assert(set->ctx, 0, goto error);
1323 error:
1324 isl_printer_free(p);
1325 return NULL;
1328 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1329 __isl_keep isl_map *map)
1331 if (!p || !map)
1332 goto error;
1334 if (p->output_format == ISL_FORMAT_ISL)
1335 return isl_map_print_isl(map, p);
1336 else if (p->output_format == ISL_FORMAT_POLYLIB)
1337 return isl_map_print_polylib(map, p, 0);
1338 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1339 return isl_map_print_polylib(map, p, 1);
1340 else if (p->output_format == ISL_FORMAT_OMEGA)
1341 return isl_map_print_omega(map, p);
1342 else if (p->output_format == ISL_FORMAT_LATEX)
1343 return isl_map_print_latex(map, p);
1344 isl_assert(map->ctx, 0, goto error);
1345 error:
1346 isl_printer_free(p);
1347 return NULL;
1350 struct isl_union_print_data {
1351 isl_printer *p;
1352 int first;
1355 static isl_stat print_map_body(__isl_take isl_map *map, void *user)
1357 struct isl_union_print_data *data;
1358 data = (struct isl_union_print_data *)user;
1360 if (!data->first)
1361 data->p = isl_printer_print_str(data->p, "; ");
1362 data->first = 0;
1364 data->p = isl_map_print_isl_body(map, data->p);
1365 isl_map_free(map);
1367 return isl_stat_ok;
1370 static __isl_give isl_printer *isl_union_map_print_isl(
1371 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1373 struct isl_union_print_data data;
1374 struct isl_print_space_data space_data = { 0 };
1375 isl_space *dim;
1377 dim = isl_union_map_get_space(umap);
1378 if (isl_space_dim(dim, isl_dim_param) > 0) {
1379 p = print_tuple(dim, p, isl_dim_param, &space_data);
1380 p = isl_printer_print_str(p, s_to[0]);
1382 isl_space_free(dim);
1383 p = isl_printer_print_str(p, s_open_set[0]);
1384 data.p = p;
1385 data.first = 1;
1386 isl_union_map_foreach_map(umap, &print_map_body, &data);
1387 p = data.p;
1388 p = isl_printer_print_str(p, s_close_set[0]);
1389 return p;
1392 static isl_stat print_latex_map_body(__isl_take isl_map *map, void *user)
1394 struct isl_union_print_data *data;
1395 data = (struct isl_union_print_data *)user;
1397 if (!data->first)
1398 data->p = isl_printer_print_str(data->p, " \\cup ");
1399 data->first = 0;
1401 data->p = isl_map_print_latex(map, data->p);
1402 isl_map_free(map);
1404 return isl_stat_ok;
1407 static __isl_give isl_printer *isl_union_map_print_latex(
1408 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1410 struct isl_union_print_data data = { p, 1 };
1411 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1412 p = data.p;
1413 return p;
1416 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1417 __isl_keep isl_union_map *umap)
1419 if (!p || !umap)
1420 goto error;
1422 if (p->output_format == ISL_FORMAT_ISL)
1423 return isl_union_map_print_isl(umap, p);
1424 if (p->output_format == ISL_FORMAT_LATEX)
1425 return isl_union_map_print_latex(umap, p);
1427 isl_die(p->ctx, isl_error_invalid,
1428 "invalid output format for isl_union_map", goto error);
1429 error:
1430 isl_printer_free(p);
1431 return NULL;
1434 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1435 __isl_keep isl_union_set *uset)
1437 if (!p || !uset)
1438 goto error;
1440 if (p->output_format == ISL_FORMAT_ISL)
1441 return isl_union_map_print_isl((isl_union_map *)uset, p);
1442 if (p->output_format == ISL_FORMAT_LATEX)
1443 return isl_union_map_print_latex((isl_union_map *)uset, p);
1445 isl_die(p->ctx, isl_error_invalid,
1446 "invalid output format for isl_union_set", goto error);
1447 error:
1448 isl_printer_free(p);
1449 return NULL;
1452 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
1454 int i;
1455 int n;
1457 for (i = 0, n = 0; i < rec->n; ++i)
1458 if (!isl_upoly_is_zero(rec->p[i]))
1459 ++n;
1461 return n;
1464 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
1465 __isl_take isl_printer *p, int first)
1467 struct isl_upoly_cst *cst;
1468 int neg;
1470 cst = isl_upoly_as_cst(up);
1471 if (!cst)
1472 goto error;
1473 neg = !first && isl_int_is_neg(cst->n);
1474 if (!first)
1475 p = isl_printer_print_str(p, neg ? " - " : " + ");
1476 if (neg)
1477 isl_int_neg(cst->n, cst->n);
1478 if (isl_int_is_zero(cst->d)) {
1479 int sgn = isl_int_sgn(cst->n);
1480 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1481 sgn == 0 ? "NaN" : "infty");
1482 } else
1483 p = isl_printer_print_isl_int(p, cst->n);
1484 if (neg)
1485 isl_int_neg(cst->n, cst->n);
1486 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1487 p = isl_printer_print_str(p, "/");
1488 p = isl_printer_print_isl_int(p, cst->d);
1490 return p;
1491 error:
1492 isl_printer_free(p);
1493 return NULL;
1496 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1497 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var)
1499 unsigned total;
1501 total = isl_space_dim(dim, isl_dim_all);
1502 if (var < total)
1503 p = print_term(dim, NULL, dim->ctx->one, 1 + var, p, 0);
1504 else
1505 p = print_div(dim, div, var - total, p);
1506 return p;
1509 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1510 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var, int exp)
1512 p = print_base(p, dim, div, var);
1513 if (exp == 1)
1514 return p;
1515 if (p->output_format == ISL_FORMAT_C) {
1516 int i;
1517 for (i = 1; i < exp; ++i) {
1518 p = isl_printer_print_str(p, "*");
1519 p = print_base(p, dim, div, var);
1521 } else {
1522 p = isl_printer_print_str(p, "^");
1523 p = isl_printer_print_int(p, exp);
1525 return p;
1528 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1529 __isl_keep isl_space *dim, __isl_keep isl_mat *div,
1530 __isl_take isl_printer *p, int outer)
1532 int i, n, first, print_parens;
1533 struct isl_upoly_rec *rec;
1535 if (!p || !up || !dim || !div)
1536 goto error;
1538 if (isl_upoly_is_cst(up))
1539 return upoly_print_cst(up, p, 1);
1541 rec = isl_upoly_as_rec(up);
1542 if (!rec)
1543 goto error;
1544 n = upoly_rec_n_non_zero(rec);
1545 print_parens = n > 1 ||
1546 (outer && rec->up.var >= isl_space_dim(dim, isl_dim_all));
1547 if (print_parens)
1548 p = isl_printer_print_str(p, "(");
1549 for (i = 0, first = 1; i < rec->n; ++i) {
1550 if (isl_upoly_is_zero(rec->p[i]))
1551 continue;
1552 if (isl_upoly_is_negone(rec->p[i])) {
1553 if (!i)
1554 p = isl_printer_print_str(p, "-1");
1555 else if (first)
1556 p = isl_printer_print_str(p, "-");
1557 else
1558 p = isl_printer_print_str(p, " - ");
1559 } else if (isl_upoly_is_cst(rec->p[i]) &&
1560 !isl_upoly_is_one(rec->p[i]))
1561 p = upoly_print_cst(rec->p[i], p, first);
1562 else {
1563 if (!first)
1564 p = isl_printer_print_str(p, " + ");
1565 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1566 p = upoly_print(rec->p[i], dim, div, p, 0);
1568 first = 0;
1569 if (i == 0)
1570 continue;
1571 if (!isl_upoly_is_one(rec->p[i]) &&
1572 !isl_upoly_is_negone(rec->p[i]))
1573 p = isl_printer_print_str(p, " * ");
1574 p = print_pow(p, dim, div, rec->up.var, i);
1576 if (print_parens)
1577 p = isl_printer_print_str(p, ")");
1578 return p;
1579 error:
1580 isl_printer_free(p);
1581 return NULL;
1584 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1585 __isl_keep isl_qpolynomial *qp)
1587 if (!p || !qp)
1588 goto error;
1589 p = upoly_print(qp->upoly, qp->dim, qp->div, p, 1);
1590 return p;
1591 error:
1592 isl_printer_free(p);
1593 return NULL;
1596 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1597 __isl_keep isl_qpolynomial *qp)
1599 struct isl_print_space_data data = { 0 };
1601 if (!p || !qp)
1602 goto error;
1604 if (isl_space_dim(qp->dim, isl_dim_param) > 0) {
1605 p = print_tuple(qp->dim, p, isl_dim_param, &data);
1606 p = isl_printer_print_str(p, " -> ");
1608 p = isl_printer_print_str(p, "{ ");
1609 if (!isl_space_is_params(qp->dim)) {
1610 p = isl_print_space(qp->dim, p, 0, &data);
1611 p = isl_printer_print_str(p, " -> ");
1613 p = print_qpolynomial(p, qp);
1614 p = isl_printer_print_str(p, " }");
1615 return p;
1616 error:
1617 isl_printer_free(p);
1618 return NULL;
1621 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1622 __isl_keep isl_space *dim, __isl_keep isl_qpolynomial *qp)
1624 isl_int den;
1626 isl_int_init(den);
1627 isl_qpolynomial_get_den(qp, &den);
1628 if (!isl_int_is_one(den)) {
1629 isl_qpolynomial *f;
1630 p = isl_printer_print_str(p, "(");
1631 qp = isl_qpolynomial_copy(qp);
1632 f = isl_qpolynomial_rat_cst_on_domain(isl_space_copy(qp->dim),
1633 den, qp->dim->ctx->one);
1634 qp = isl_qpolynomial_mul(qp, f);
1636 if (qp)
1637 p = upoly_print(qp->upoly, dim, qp->div, p, 0);
1638 else
1639 p = isl_printer_free(p);
1640 if (!isl_int_is_one(den)) {
1641 p = isl_printer_print_str(p, ")/");
1642 p = isl_printer_print_isl_int(p, den);
1643 isl_qpolynomial_free(qp);
1645 isl_int_clear(den);
1646 return p;
1649 __isl_give isl_printer *isl_printer_print_qpolynomial(
1650 __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1652 if (!p || !qp)
1653 goto error;
1655 if (p->output_format == ISL_FORMAT_ISL)
1656 return print_qpolynomial_isl(p, qp);
1657 else if (p->output_format == ISL_FORMAT_C)
1658 return print_qpolynomial_c(p, qp->dim, qp);
1659 else
1660 isl_die(qp->dim->ctx, isl_error_unsupported,
1661 "output format not supported for isl_qpolynomials",
1662 goto error);
1663 error:
1664 isl_printer_free(p);
1665 return NULL;
1668 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1669 unsigned output_format)
1671 isl_printer *p;
1673 if (!qp)
1674 return;
1676 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1677 p = isl_printer_to_file(qp->dim->ctx, out);
1678 p = isl_printer_print_qpolynomial(p, qp);
1679 isl_printer_free(p);
1682 static __isl_give isl_printer *qpolynomial_fold_print(
1683 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1685 int i;
1687 if (fold->type == isl_fold_min)
1688 p = isl_printer_print_str(p, "min");
1689 else if (fold->type == isl_fold_max)
1690 p = isl_printer_print_str(p, "max");
1691 p = isl_printer_print_str(p, "(");
1692 for (i = 0; i < fold->n; ++i) {
1693 if (i)
1694 p = isl_printer_print_str(p, ", ");
1695 p = print_qpolynomial(p, fold->qp[i]);
1697 p = isl_printer_print_str(p, ")");
1698 return p;
1701 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1702 FILE *out, unsigned output_format)
1704 isl_printer *p;
1706 if (!fold)
1707 return;
1709 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1711 p = isl_printer_to_file(fold->dim->ctx, out);
1712 p = isl_printer_print_qpolynomial_fold(p, fold);
1714 isl_printer_free(p);
1717 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1718 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1720 struct isl_print_space_data data = { 0 };
1721 int i = 0;
1723 for (i = 0; i < pwqp->n; ++i) {
1724 isl_space *space;
1726 if (i)
1727 p = isl_printer_print_str(p, "; ");
1728 space = isl_qpolynomial_get_domain_space(pwqp->p[i].qp);
1729 if (!isl_space_is_params(space)) {
1730 p = isl_print_space(space, p, 0, &data);
1731 p = isl_printer_print_str(p, " -> ");
1733 p = print_qpolynomial(p, pwqp->p[i].qp);
1734 p = print_disjuncts((isl_map *)pwqp->p[i].set, space, p, 0);
1735 isl_space_free(space);
1738 return p;
1741 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1742 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1744 struct isl_print_space_data data = { 0 };
1746 if (!p || !pwqp)
1747 goto error;
1749 if (isl_space_dim(pwqp->dim, isl_dim_param) > 0) {
1750 p = print_tuple(pwqp->dim, p, isl_dim_param, &data);
1751 p = isl_printer_print_str(p, " -> ");
1753 p = isl_printer_print_str(p, "{ ");
1754 if (pwqp->n == 0) {
1755 if (!isl_space_is_set(pwqp->dim)) {
1756 p = print_tuple(pwqp->dim, p, isl_dim_in, &data);
1757 p = isl_printer_print_str(p, " -> ");
1759 p = isl_printer_print_str(p, "0");
1761 p = isl_pwqp_print_isl_body(p, pwqp);
1762 p = isl_printer_print_str(p, " }");
1763 return p;
1764 error:
1765 isl_printer_free(p);
1766 return NULL;
1769 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
1770 unsigned output_format)
1772 isl_printer *p;
1774 if (!pwqp)
1775 return;
1777 p = isl_printer_to_file(pwqp->dim->ctx, out);
1778 p = isl_printer_set_output_format(p, output_format);
1779 p = isl_printer_print_pw_qpolynomial(p, pwqp);
1781 isl_printer_free(p);
1784 static __isl_give isl_printer *isl_pwf_print_isl_body(
1785 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1787 struct isl_print_space_data data = { 0 };
1788 int i = 0;
1790 for (i = 0; i < pwf->n; ++i) {
1791 isl_space *space;
1793 if (i)
1794 p = isl_printer_print_str(p, "; ");
1795 space = isl_qpolynomial_fold_get_domain_space(pwf->p[i].fold);
1796 if (!isl_space_is_params(space)) {
1797 p = isl_print_space(space, p, 0, &data);
1798 p = isl_printer_print_str(p, " -> ");
1800 p = qpolynomial_fold_print(pwf->p[i].fold, p);
1801 p = print_disjuncts((isl_map *)pwf->p[i].set, space, p, 0);
1802 isl_space_free(space);
1805 return p;
1808 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
1809 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1811 struct isl_print_space_data data = { 0 };
1813 if (isl_space_dim(pwf->dim, isl_dim_param) > 0) {
1814 p = print_tuple(pwf->dim, p, isl_dim_param, &data);
1815 p = isl_printer_print_str(p, " -> ");
1817 p = isl_printer_print_str(p, "{ ");
1818 if (pwf->n == 0) {
1819 if (!isl_space_is_set(pwf->dim)) {
1820 p = print_tuple(pwf->dim, p, isl_dim_in, &data);
1821 p = isl_printer_print_str(p, " -> ");
1823 p = isl_printer_print_str(p, "0");
1825 p = isl_pwf_print_isl_body(p, pwf);
1826 p = isl_printer_print_str(p, " }");
1827 return p;
1830 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1831 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c);
1833 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1834 __isl_keep isl_space *dim,
1835 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1837 if (type == isl_dim_div) {
1838 p = isl_printer_print_str(p, "floord(");
1839 p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
1840 p = isl_printer_print_str(p, ", ");
1841 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1842 p = isl_printer_print_str(p, ")");
1843 } else {
1844 const char *name;
1846 name = isl_space_get_dim_name(dim, type, pos);
1847 if (!name)
1848 name = "UNNAMED";
1849 p = isl_printer_print_str(p, name);
1851 return p;
1854 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1855 __isl_keep isl_space *dim,
1856 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1858 enum isl_dim_type type;
1860 if (pos == 0)
1861 return isl_printer_print_isl_int(p, c);
1863 if (isl_int_is_one(c))
1865 else if (isl_int_is_negone(c))
1866 p = isl_printer_print_str(p, "-");
1867 else {
1868 p = isl_printer_print_isl_int(p, c);
1869 p = isl_printer_print_str(p, "*");
1871 type = pos2type(dim, &pos);
1872 p = print_name_c(p, dim, bset, type, pos);
1873 return p;
1876 static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
1877 __isl_keep isl_space *dim,
1878 __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
1880 int i;
1881 int first;
1883 for (i = 0, first = 1; i < len; ++i) {
1884 int flip = 0;
1885 if (isl_int_is_zero(c[i]))
1886 continue;
1887 if (!first) {
1888 if (isl_int_is_neg(c[i])) {
1889 flip = 1;
1890 isl_int_neg(c[i], c[i]);
1891 p = isl_printer_print_str(p, " - ");
1892 } else
1893 p = isl_printer_print_str(p, " + ");
1895 first = 0;
1896 p = print_term_c(p, dim, bset, c[i], i);
1897 if (flip)
1898 isl_int_neg(c[i], c[i]);
1900 if (first)
1901 p = isl_printer_print_str(p, "0");
1902 return p;
1905 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1906 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c)
1908 unsigned len = 1 + isl_basic_set_total_dim(bset);
1909 return print_partial_affine_c(p, dim, bset, c, len);
1912 /* We skip the constraint if it is implied by the div expression.
1914 * *first indicates whether this is the first constraint in the conjunction and
1915 * is updated if the constraint is actually printed.
1917 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1918 __isl_keep isl_space *dim,
1919 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int *first)
1921 unsigned o_div;
1922 unsigned n_div;
1923 int div;
1925 o_div = isl_basic_set_offset(bset, isl_dim_div);
1926 n_div = isl_basic_set_dim(bset, isl_dim_div);
1927 div = isl_seq_last_non_zero(c + o_div, n_div);
1928 if (div >= 0 && isl_basic_set_is_div_constraint(bset, c, div))
1929 return p;
1931 if (!*first)
1932 p = isl_printer_print_str(p, " && ");
1934 p = print_affine_c(p, dim, bset, c);
1935 p = isl_printer_print_str(p, " ");
1936 p = isl_printer_print_str(p, op);
1937 p = isl_printer_print_str(p, " 0");
1939 *first = 0;
1941 return p;
1944 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1945 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset)
1947 int i, j;
1948 int first = 1;
1949 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
1950 unsigned total = isl_basic_set_total_dim(bset) - n_div;
1952 for (i = 0; i < bset->n_eq; ++i) {
1953 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
1954 if (j < 0)
1955 p = print_constraint_c(p, dim, bset,
1956 bset->eq[i], "==", &first);
1957 else {
1958 if (i)
1959 p = isl_printer_print_str(p, " && ");
1960 p = isl_printer_print_str(p, "(");
1961 p = print_partial_affine_c(p, dim, bset, bset->eq[i],
1962 1 + total + j);
1963 p = isl_printer_print_str(p, ") % ");
1964 p = isl_printer_print_isl_int(p,
1965 bset->eq[i][1 + total + j]);
1966 p = isl_printer_print_str(p, " == 0");
1967 first = 0;
1970 for (i = 0; i < bset->n_ineq; ++i)
1971 p = print_constraint_c(p, dim, bset, bset->ineq[i], ">=",
1972 &first);
1973 return p;
1976 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
1977 __isl_keep isl_space *dim, __isl_keep isl_set *set)
1979 int i;
1981 if (!set)
1982 return isl_printer_free(p);
1984 if (set->n == 0)
1985 p = isl_printer_print_str(p, "0");
1987 for (i = 0; i < set->n; ++i) {
1988 if (i)
1989 p = isl_printer_print_str(p, " || ");
1990 if (set->n > 1)
1991 p = isl_printer_print_str(p, "(");
1992 p = print_basic_set_c(p, dim, set->p[i]);
1993 if (set->n > 1)
1994 p = isl_printer_print_str(p, ")");
1996 return p;
1999 static __isl_give isl_printer *print_pw_qpolynomial_c(
2000 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2002 int i;
2004 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set))
2005 return print_qpolynomial_c(p, pwqp->dim, pwqp->p[0].qp);
2007 for (i = 0; i < pwqp->n; ++i) {
2008 p = isl_printer_print_str(p, "(");
2009 p = print_set_c(p, pwqp->dim, pwqp->p[i].set);
2010 p = isl_printer_print_str(p, ") ? (");
2011 p = print_qpolynomial_c(p, pwqp->dim, pwqp->p[i].qp);
2012 p = isl_printer_print_str(p, ") : ");
2015 p = isl_printer_print_str(p, "0");
2016 return p;
2019 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
2020 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2022 if (!p || !pwqp)
2023 goto error;
2025 if (p->output_format == ISL_FORMAT_ISL)
2026 return print_pw_qpolynomial_isl(p, pwqp);
2027 else if (p->output_format == ISL_FORMAT_C)
2028 return print_pw_qpolynomial_c(p, pwqp);
2029 isl_assert(p->ctx, 0, goto error);
2030 error:
2031 isl_printer_free(p);
2032 return NULL;
2035 static isl_stat print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
2037 struct isl_union_print_data *data;
2038 data = (struct isl_union_print_data *)user;
2040 if (!data->first)
2041 data->p = isl_printer_print_str(data->p, "; ");
2042 data->first = 0;
2044 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
2045 isl_pw_qpolynomial_free(pwqp);
2047 return isl_stat_ok;
2050 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
2051 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2053 struct isl_union_print_data data;
2054 struct isl_print_space_data space_data = { 0 };
2055 isl_space *dim;
2057 dim = isl_union_pw_qpolynomial_get_space(upwqp);
2058 if (isl_space_dim(dim, isl_dim_param) > 0) {
2059 p = print_tuple(dim, p, isl_dim_param, &space_data);
2060 p = isl_printer_print_str(p, " -> ");
2062 isl_space_free(dim);
2063 p = isl_printer_print_str(p, "{ ");
2064 data.p = p;
2065 data.first = 1;
2066 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
2067 &data);
2068 p = data.p;
2069 p = isl_printer_print_str(p, " }");
2070 return p;
2073 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
2074 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2076 if (!p || !upwqp)
2077 goto error;
2079 if (p->output_format == ISL_FORMAT_ISL)
2080 return print_union_pw_qpolynomial_isl(p, upwqp);
2081 isl_die(p->ctx, isl_error_invalid,
2082 "invalid output format for isl_union_pw_qpolynomial",
2083 goto error);
2084 error:
2085 isl_printer_free(p);
2086 return NULL;
2089 static __isl_give isl_printer *print_qpolynomial_fold_c(
2090 __isl_take isl_printer *p, __isl_keep isl_space *dim,
2091 __isl_keep isl_qpolynomial_fold *fold)
2093 int i;
2095 for (i = 0; i < fold->n - 1; ++i)
2096 if (fold->type == isl_fold_min)
2097 p = isl_printer_print_str(p, "min(");
2098 else if (fold->type == isl_fold_max)
2099 p = isl_printer_print_str(p, "max(");
2101 for (i = 0; i < fold->n; ++i) {
2102 if (i)
2103 p = isl_printer_print_str(p, ", ");
2104 p = print_qpolynomial_c(p, dim, fold->qp[i]);
2105 if (i)
2106 p = isl_printer_print_str(p, ")");
2108 return p;
2111 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
2112 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
2114 if (!p || !fold)
2115 goto error;
2116 if (p->output_format == ISL_FORMAT_ISL)
2117 return qpolynomial_fold_print(fold, p);
2118 else if (p->output_format == ISL_FORMAT_C)
2119 return print_qpolynomial_fold_c(p, fold->dim, fold);
2120 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2121 goto error);
2122 error:
2123 isl_printer_free(p);
2124 return NULL;
2127 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
2128 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2130 int i;
2132 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set))
2133 return print_qpolynomial_fold_c(p, pwf->dim, pwf->p[0].fold);
2135 for (i = 0; i < pwf->n; ++i) {
2136 p = isl_printer_print_str(p, "(");
2137 p = print_set_c(p, pwf->dim, pwf->p[i].set);
2138 p = isl_printer_print_str(p, ") ? (");
2139 p = print_qpolynomial_fold_c(p, pwf->dim, pwf->p[i].fold);
2140 p = isl_printer_print_str(p, ") : ");
2143 p = isl_printer_print_str(p, "0");
2144 return p;
2147 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
2148 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2150 if (!p || !pwf)
2151 goto error;
2153 if (p->output_format == ISL_FORMAT_ISL)
2154 return print_pw_qpolynomial_fold_isl(p, pwf);
2155 else if (p->output_format == ISL_FORMAT_C)
2156 return print_pw_qpolynomial_fold_c(p, pwf);
2157 isl_assert(p->ctx, 0, goto error);
2158 error:
2159 isl_printer_free(p);
2160 return NULL;
2163 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
2164 FILE *out, unsigned output_format)
2166 isl_printer *p;
2168 if (!pwf)
2169 return;
2171 p = isl_printer_to_file(pwf->dim->ctx, out);
2172 p = isl_printer_set_output_format(p, output_format);
2173 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
2175 isl_printer_free(p);
2178 static isl_stat print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf,
2179 void *user)
2181 struct isl_union_print_data *data;
2182 data = (struct isl_union_print_data *)user;
2184 if (!data->first)
2185 data->p = isl_printer_print_str(data->p, "; ");
2186 data->first = 0;
2188 data->p = isl_pwf_print_isl_body(data->p, pwf);
2189 isl_pw_qpolynomial_fold_free(pwf);
2191 return isl_stat_ok;
2194 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
2195 __isl_take isl_printer *p,
2196 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2198 struct isl_union_print_data data;
2199 struct isl_print_space_data space_data = { 0 };
2200 isl_space *dim;
2202 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
2203 if (isl_space_dim(dim, isl_dim_param) > 0) {
2204 p = print_tuple(dim, p, isl_dim_param, &space_data);
2205 p = isl_printer_print_str(p, " -> ");
2207 isl_space_free(dim);
2208 p = isl_printer_print_str(p, "{ ");
2209 data.p = p;
2210 data.first = 1;
2211 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
2212 &print_pwf_body, &data);
2213 p = data.p;
2214 p = isl_printer_print_str(p, " }");
2215 return p;
2218 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2219 __isl_take isl_printer *p,
2220 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2222 if (!p || !upwf)
2223 goto error;
2225 if (p->output_format == ISL_FORMAT_ISL)
2226 return print_union_pw_qpolynomial_fold_isl(p, upwf);
2227 isl_die(p->ctx, isl_error_invalid,
2228 "invalid output format for isl_union_pw_qpolynomial_fold",
2229 goto error);
2230 error:
2231 isl_printer_free(p);
2232 return NULL;
2235 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
2236 __isl_keep isl_constraint *c)
2238 isl_basic_map *bmap;
2240 if (!p || !c)
2241 goto error;
2243 bmap = isl_basic_map_from_constraint(isl_constraint_copy(c));
2244 p = isl_printer_print_basic_map(p, bmap);
2245 isl_basic_map_free(bmap);
2246 return p;
2247 error:
2248 isl_printer_free(p);
2249 return NULL;
2252 static __isl_give isl_printer *isl_printer_print_space_isl(
2253 __isl_take isl_printer *p, __isl_keep isl_space *space)
2255 struct isl_print_space_data data = { 0 };
2257 if (!space)
2258 goto error;
2260 if (isl_space_dim(space, isl_dim_param) > 0) {
2261 p = print_tuple(space, p, isl_dim_param, &data);
2262 p = isl_printer_print_str(p, " -> ");
2265 p = isl_printer_print_str(p, "{ ");
2266 if (isl_space_is_params(space))
2267 p = isl_printer_print_str(p, s_such_that[0]);
2268 else
2269 p = isl_print_space(space, p, 0, &data);
2270 p = isl_printer_print_str(p, " }");
2272 return p;
2273 error:
2274 isl_printer_free(p);
2275 return NULL;
2278 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
2279 __isl_keep isl_space *space)
2281 if (!p || !space)
2282 return isl_printer_free(p);
2283 if (p->output_format == ISL_FORMAT_ISL)
2284 return isl_printer_print_space_isl(p, space);
2285 else if (p->output_format == ISL_FORMAT_OMEGA)
2286 return print_omega_parameters(space, p);
2288 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
2289 "output format not supported for space",
2290 return isl_printer_free(p));
2293 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
2294 __isl_keep isl_local_space *ls)
2296 struct isl_print_space_data data = { 0 };
2297 unsigned n_div;
2299 if (!ls)
2300 goto error;
2302 if (isl_local_space_dim(ls, isl_dim_param) > 0) {
2303 p = print_tuple(ls->dim, p, isl_dim_param, &data);
2304 p = isl_printer_print_str(p, " -> ");
2306 p = isl_printer_print_str(p, "{ ");
2307 p = isl_print_space(ls->dim, p, 0, &data);
2308 n_div = isl_local_space_dim(ls, isl_dim_div);
2309 if (n_div > 0) {
2310 p = isl_printer_print_str(p, " : ");
2311 p = isl_printer_print_str(p, s_open_exists[0]);
2312 p = print_div_list(p, ls->dim, ls->div, 0, 1);
2313 p = isl_printer_print_str(p, s_close_exists[0]);
2314 } else if (isl_space_is_params(ls->dim))
2315 p = isl_printer_print_str(p, s_such_that[0]);
2316 p = isl_printer_print_str(p, " }");
2317 return p;
2318 error:
2319 isl_printer_free(p);
2320 return NULL;
2323 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2324 __isl_keep isl_aff *aff)
2326 unsigned total;
2328 if (isl_aff_is_nan(aff))
2329 return isl_printer_print_str(p, "NaN");
2331 total = isl_local_space_dim(aff->ls, isl_dim_all);
2332 p = isl_printer_print_str(p, "(");
2333 p = print_affine_of_len(aff->ls->dim, aff->ls->div, p,
2334 aff->v->el + 1, 1 + total);
2335 if (isl_int_is_one(aff->v->el[0]))
2336 p = isl_printer_print_str(p, ")");
2337 else {
2338 p = isl_printer_print_str(p, ")/");
2339 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2342 return p;
2345 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2346 __isl_keep isl_aff *aff)
2348 struct isl_print_space_data data = { 0 };
2350 if (isl_space_is_params(aff->ls->dim))
2352 else {
2353 p = print_tuple(aff->ls->dim, p, isl_dim_set, &data);
2354 p = isl_printer_print_str(p, " -> ");
2356 p = isl_printer_print_str(p, "[");
2357 p = print_aff_body(p, aff);
2358 p = isl_printer_print_str(p, "]");
2360 return p;
2363 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2364 __isl_keep isl_aff *aff)
2366 struct isl_print_space_data data = { 0 };
2368 if (!aff)
2369 goto error;
2371 if (isl_local_space_dim(aff->ls, isl_dim_param) > 0) {
2372 p = print_tuple(aff->ls->dim, p, isl_dim_param, &data);
2373 p = isl_printer_print_str(p, " -> ");
2375 p = isl_printer_print_str(p, "{ ");
2376 p = print_aff(p, aff);
2377 p = isl_printer_print_str(p, " }");
2378 return p;
2379 error:
2380 isl_printer_free(p);
2381 return NULL;
2384 /* Print the body of an isl_pw_aff, i.e., a semicolon delimited
2385 * sequence of affine expressions, each followed by constraints.
2387 static __isl_give isl_printer *print_pw_aff_body(
2388 __isl_take isl_printer *p, __isl_keep isl_pw_aff *pa)
2390 int i;
2392 if (!pa)
2393 return isl_printer_free(p);
2395 for (i = 0; i < pa->n; ++i) {
2396 isl_space *space;
2398 if (i)
2399 p = isl_printer_print_str(p, "; ");
2400 p = print_aff(p, pa->p[i].aff);
2401 space = isl_aff_get_domain_space(pa->p[i].aff);
2402 p = print_disjuncts((isl_map *)pa->p[i].set, space, p, 0);
2403 isl_space_free(space);
2406 return p;
2409 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2410 __isl_keep isl_pw_aff *pwaff)
2412 struct isl_print_space_data data = { 0 };
2414 if (!pwaff)
2415 goto error;
2417 if (isl_space_dim(pwaff->dim, isl_dim_param) > 0) {
2418 p = print_tuple(pwaff->dim, p, isl_dim_param, &data);
2419 p = isl_printer_print_str(p, " -> ");
2421 p = isl_printer_print_str(p, "{ ");
2422 p = print_pw_aff_body(p, pwaff);
2423 p = isl_printer_print_str(p, " }");
2424 return p;
2425 error:
2426 isl_printer_free(p);
2427 return NULL;
2430 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2431 __isl_keep isl_local_space *ls, isl_int *c);
2433 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2434 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2436 if (type == isl_dim_div) {
2437 p = isl_printer_print_str(p, "floord(");
2438 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2439 p = isl_printer_print_str(p, ", ");
2440 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2441 p = isl_printer_print_str(p, ")");
2442 } else {
2443 const char *name;
2445 name = isl_space_get_dim_name(ls->dim, type, pos);
2446 if (!name)
2447 name = "UNNAMED";
2448 p = isl_printer_print_str(p, name);
2450 return p;
2453 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2454 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2456 enum isl_dim_type type;
2458 if (pos == 0)
2459 return isl_printer_print_isl_int(p, c);
2461 if (isl_int_is_one(c))
2463 else if (isl_int_is_negone(c))
2464 p = isl_printer_print_str(p, "-");
2465 else {
2466 p = isl_printer_print_isl_int(p, c);
2467 p = isl_printer_print_str(p, "*");
2469 type = pos2type(ls->dim, &pos);
2470 p = print_ls_name_c(p, ls, type, pos);
2471 return p;
2474 static __isl_give isl_printer *print_ls_partial_affine_c(
2475 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2476 isl_int *c, unsigned len)
2478 int i;
2479 int first;
2481 for (i = 0, first = 1; i < len; ++i) {
2482 int flip = 0;
2483 if (isl_int_is_zero(c[i]))
2484 continue;
2485 if (!first) {
2486 if (isl_int_is_neg(c[i])) {
2487 flip = 1;
2488 isl_int_neg(c[i], c[i]);
2489 p = isl_printer_print_str(p, " - ");
2490 } else
2491 p = isl_printer_print_str(p, " + ");
2493 first = 0;
2494 p = print_ls_term_c(p, ls, c[i], i);
2495 if (flip)
2496 isl_int_neg(c[i], c[i]);
2498 if (first)
2499 p = isl_printer_print_str(p, "0");
2500 return p;
2503 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2504 __isl_keep isl_local_space *ls, isl_int *c)
2506 unsigned len = 1 + isl_local_space_dim(ls, isl_dim_all);
2507 return print_ls_partial_affine_c(p, ls, c, len);
2510 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2511 __isl_keep isl_aff *aff)
2513 unsigned total;
2515 total = isl_local_space_dim(aff->ls, isl_dim_all);
2516 if (!isl_int_is_one(aff->v->el[0]))
2517 p = isl_printer_print_str(p, "(");
2518 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2519 if (!isl_int_is_one(aff->v->el[0])) {
2520 p = isl_printer_print_str(p, ")/");
2521 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2523 return p;
2526 /* In the C format, we cannot express that "pwaff" may be undefined
2527 * on parts of the domain space. We therefore assume that the expression
2528 * will only be evaluated on its definition domain and compute the gist
2529 * of each cell with respect to this domain.
2531 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2532 __isl_keep isl_pw_aff *pwaff)
2534 isl_set *domain;
2535 isl_ast_build *build;
2536 isl_ast_expr *expr;
2538 if (pwaff->n < 1)
2539 isl_die(p->ctx, isl_error_unsupported,
2540 "cannot print empty isl_pw_aff in C format",
2541 return isl_printer_free(p));
2543 domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
2544 build = isl_ast_build_from_context(domain);
2545 expr = isl_ast_build_expr_from_pw_aff(build, isl_pw_aff_copy(pwaff));
2546 p = isl_printer_print_ast_expr(p, expr);
2547 isl_ast_expr_free(expr);
2548 isl_ast_build_free(build);
2550 return p;
2553 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2554 __isl_keep isl_aff *aff)
2556 if (!p || !aff)
2557 goto error;
2559 if (p->output_format == ISL_FORMAT_ISL)
2560 return print_aff_isl(p, aff);
2561 else if (p->output_format == ISL_FORMAT_C)
2562 return print_aff_c(p, aff);
2563 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2564 goto error);
2565 error:
2566 isl_printer_free(p);
2567 return NULL;
2570 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2571 __isl_keep isl_pw_aff *pwaff)
2573 if (!p || !pwaff)
2574 goto error;
2576 if (p->output_format == ISL_FORMAT_ISL)
2577 return print_pw_aff_isl(p, pwaff);
2578 else if (p->output_format == ISL_FORMAT_C)
2579 return print_pw_aff_c(p, pwaff);
2580 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2581 goto error);
2582 error:
2583 isl_printer_free(p);
2584 return NULL;
2587 /* Print "pa" in a sequence of isl_pw_affs delimited by semicolons.
2588 * Each isl_pw_aff itself is also printed as semicolon delimited
2589 * sequence of pieces.
2590 * If data->first = 1, then this is the first in the sequence.
2591 * Update data->first to tell the next element that it is not the first.
2593 static isl_stat print_pw_aff_body_wrap(__isl_take isl_pw_aff *pa,
2594 void *user)
2596 struct isl_union_print_data *data;
2597 data = (struct isl_union_print_data *) user;
2599 if (!data->first)
2600 data->p = isl_printer_print_str(data->p, "; ");
2601 data->first = 0;
2603 data->p = print_pw_aff_body(data->p, pa);
2604 isl_pw_aff_free(pa);
2606 return data->p ? isl_stat_ok : isl_stat_error;
2609 /* Print the body of an isl_union_pw_aff, i.e., a semicolon delimited
2610 * sequence of affine expressions, each followed by constraints,
2611 * with the sequence enclosed in braces.
2613 static __isl_give isl_printer *print_union_pw_aff_body(
2614 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2616 struct isl_union_print_data data = { p, 1 };
2618 p = isl_printer_print_str(p, s_open_set[0]);
2619 data.p = p;
2620 if (isl_union_pw_aff_foreach_pw_aff(upa,
2621 &print_pw_aff_body_wrap, &data) < 0)
2622 data.p = isl_printer_free(p);
2623 p = data.p;
2624 p = isl_printer_print_str(p, s_close_set[0]);
2626 return p;
2629 /* Print the isl_union_pw_aff "upa" to "p" in isl format.
2631 * The individual isl_pw_affs are delimited by a semicolon.
2633 static __isl_give isl_printer *print_union_pw_aff_isl(
2634 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2636 struct isl_print_space_data data = { 0 };
2637 isl_space *space;
2639 space = isl_union_pw_aff_get_space(upa);
2640 if (isl_space_dim(space, isl_dim_param) > 0) {
2641 p = print_tuple(space, p, isl_dim_param, &data);
2642 p = isl_printer_print_str(p, s_to[0]);
2644 isl_space_free(space);
2645 p = print_union_pw_aff_body(p, upa);
2646 return p;
2649 /* Print the isl_union_pw_aff "upa" to "p".
2651 * We currently only support an isl format.
2653 __isl_give isl_printer *isl_printer_print_union_pw_aff(
2654 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2656 if (!p || !upa)
2657 return isl_printer_free(p);
2659 if (p->output_format == ISL_FORMAT_ISL)
2660 return print_union_pw_aff_isl(p, upa);
2661 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2662 "unsupported output format", return isl_printer_free(p));
2665 /* Print dimension "pos" of data->space to "p".
2667 * data->user is assumed to be an isl_multi_aff.
2669 * If the current dimension is an output dimension, then print
2670 * the corresponding expression. Otherwise, print the name of the dimension.
2672 static __isl_give isl_printer *print_dim_ma(__isl_take isl_printer *p,
2673 struct isl_print_space_data *data, unsigned pos)
2675 isl_multi_aff *ma = data->user;
2677 if (data->type == isl_dim_out)
2678 p = print_aff_body(p, ma->p[pos]);
2679 else
2680 p = print_name(data->space, p, data->type, pos, data->latex);
2682 return p;
2685 static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
2686 __isl_keep isl_multi_aff *maff)
2688 struct isl_print_space_data data = { 0 };
2690 data.print_dim = &print_dim_ma;
2691 data.user = maff;
2692 return isl_print_space(maff->space, p, 0, &data);
2695 static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
2696 __isl_keep isl_multi_aff *maff)
2698 struct isl_print_space_data data = { 0 };
2700 if (!maff)
2701 goto error;
2703 if (isl_space_dim(maff->space, isl_dim_param) > 0) {
2704 p = print_tuple(maff->space, p, isl_dim_param, &data);
2705 p = isl_printer_print_str(p, " -> ");
2707 p = isl_printer_print_str(p, "{ ");
2708 p = print_multi_aff(p, maff);
2709 p = isl_printer_print_str(p, " }");
2710 return p;
2711 error:
2712 isl_printer_free(p);
2713 return NULL;
2716 __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
2717 __isl_keep isl_multi_aff *maff)
2719 if (!p || !maff)
2720 goto error;
2722 if (p->output_format == ISL_FORMAT_ISL)
2723 return print_multi_aff_isl(p, maff);
2724 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2725 goto error);
2726 error:
2727 isl_printer_free(p);
2728 return NULL;
2731 static __isl_give isl_printer *print_pw_multi_aff_body(
2732 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2734 int i;
2736 if (!pma)
2737 goto error;
2739 for (i = 0; i < pma->n; ++i) {
2740 isl_space *space;
2742 if (i)
2743 p = isl_printer_print_str(p, "; ");
2744 p = print_multi_aff(p, pma->p[i].maff);
2745 space = isl_multi_aff_get_domain_space(pma->p[i].maff);
2746 p = print_disjuncts((isl_map *)pma->p[i].set, space, p, 0);
2747 isl_space_free(space);
2749 return p;
2750 error:
2751 isl_printer_free(p);
2752 return NULL;
2755 static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
2756 __isl_keep isl_pw_multi_aff *pma)
2758 struct isl_print_space_data data = { 0 };
2760 if (!pma)
2761 goto error;
2763 if (isl_space_dim(pma->dim, isl_dim_param) > 0) {
2764 p = print_tuple(pma->dim, p, isl_dim_param, &data);
2765 p = isl_printer_print_str(p, " -> ");
2767 p = isl_printer_print_str(p, "{ ");
2768 p = print_pw_multi_aff_body(p, pma);
2769 p = isl_printer_print_str(p, " }");
2770 return p;
2771 error:
2772 isl_printer_free(p);
2773 return NULL;
2776 static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
2777 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2779 int i;
2781 for (i = 0; i < pma->n - 1; ++i) {
2782 p = isl_printer_print_str(p, "(");
2783 p = print_set_c(p, pma->dim, pma->p[i].set);
2784 p = isl_printer_print_str(p, ") ? (");
2785 p = print_aff_c(p, pma->p[i].maff->p[0]);
2786 p = isl_printer_print_str(p, ") : ");
2789 return print_aff_c(p, pma->p[pma->n - 1].maff->p[0]);
2792 static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
2793 __isl_keep isl_pw_multi_aff *pma)
2795 int n;
2796 const char *name;
2798 if (!pma)
2799 goto error;
2800 if (pma->n < 1)
2801 isl_die(p->ctx, isl_error_unsupported,
2802 "cannot print empty isl_pw_multi_aff in C format",
2803 goto error);
2804 name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
2805 if (!name && isl_pw_multi_aff_dim(pma, isl_dim_out) == 1)
2806 return print_unnamed_pw_multi_aff_c(p, pma);
2807 if (!name)
2808 isl_die(p->ctx, isl_error_unsupported,
2809 "cannot print unnamed isl_pw_multi_aff in C format",
2810 goto error);
2812 p = isl_printer_print_str(p, name);
2813 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
2814 if (n != 0)
2815 isl_die(p->ctx, isl_error_unsupported,
2816 "not supported yet", goto error);
2818 return p;
2819 error:
2820 isl_printer_free(p);
2821 return NULL;
2824 __isl_give isl_printer *isl_printer_print_pw_multi_aff(
2825 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2827 if (!p || !pma)
2828 goto error;
2830 if (p->output_format == ISL_FORMAT_ISL)
2831 return print_pw_multi_aff_isl(p, pma);
2832 if (p->output_format == ISL_FORMAT_C)
2833 return print_pw_multi_aff_c(p, pma);
2834 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2835 goto error);
2836 error:
2837 isl_printer_free(p);
2838 return NULL;
2841 static isl_stat print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
2842 void *user)
2844 struct isl_union_print_data *data;
2845 data = (struct isl_union_print_data *) user;
2847 if (!data->first)
2848 data->p = isl_printer_print_str(data->p, "; ");
2849 data->first = 0;
2851 data->p = print_pw_multi_aff_body(data->p, pma);
2852 isl_pw_multi_aff_free(pma);
2854 return isl_stat_ok;
2857 static __isl_give isl_printer *print_union_pw_multi_aff_isl(
2858 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2860 struct isl_union_print_data data;
2861 struct isl_print_space_data space_data = { 0 };
2862 isl_space *space;
2864 space = isl_union_pw_multi_aff_get_space(upma);
2865 if (isl_space_dim(space, isl_dim_param) > 0) {
2866 p = print_tuple(space, p, isl_dim_param, &space_data);
2867 p = isl_printer_print_str(p, s_to[0]);
2869 isl_space_free(space);
2870 p = isl_printer_print_str(p, s_open_set[0]);
2871 data.p = p;
2872 data.first = 1;
2873 isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
2874 &print_pw_multi_aff_body_wrap, &data);
2875 p = data.p;
2876 p = isl_printer_print_str(p, s_close_set[0]);
2877 return p;
2880 __isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
2881 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2883 if (!p || !upma)
2884 goto error;
2886 if (p->output_format == ISL_FORMAT_ISL)
2887 return print_union_pw_multi_aff_isl(p, upma);
2888 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2889 goto error);
2890 error:
2891 isl_printer_free(p);
2892 return NULL;
2895 /* Print dimension "pos" of data->space to "p".
2897 * data->user is assumed to be an isl_multi_pw_aff.
2899 * If the current dimension is an output dimension, then print
2900 * the corresponding piecewise affine expression.
2901 * Otherwise, print the name of the dimension.
2903 static __isl_give isl_printer *print_dim_mpa(__isl_take isl_printer *p,
2904 struct isl_print_space_data *data, unsigned pos)
2906 int i;
2907 int need_parens;
2908 isl_multi_pw_aff *mpa = data->user;
2909 isl_pw_aff *pa;
2911 if (data->type != isl_dim_out)
2912 return print_name(data->space, p, data->type, pos, data->latex);
2914 pa = mpa->p[pos];
2915 if (pa->n == 0)
2916 return isl_printer_print_str(p, "(0 : 1 = 0)");
2918 need_parens = pa->n != 1 || !isl_set_plain_is_universe(pa->p[0].set);
2919 if (need_parens)
2920 p = isl_printer_print_str(p, "(");
2921 for (i = 0; i < pa->n; ++i) {
2922 isl_space *space;
2924 if (i)
2925 p = isl_printer_print_str(p, "; ");
2926 p = print_aff_body(p, pa->p[i].aff);
2927 space = isl_aff_get_domain_space(pa->p[i].aff);
2928 p = print_disjuncts(pa->p[i].set, space, p, 0);
2929 isl_space_free(space);
2931 if (need_parens)
2932 p = isl_printer_print_str(p, ")");
2934 return p;
2937 /* Print "mpa" to "p" in isl format.
2939 static __isl_give isl_printer *print_multi_pw_aff_isl(__isl_take isl_printer *p,
2940 __isl_keep isl_multi_pw_aff *mpa)
2942 struct isl_print_space_data data = { 0 };
2944 if (!mpa)
2945 return isl_printer_free(p);
2947 if (isl_space_dim(mpa->space, isl_dim_param) > 0) {
2948 p = print_tuple(mpa->space, p, isl_dim_param, &data);
2949 p = isl_printer_print_str(p, " -> ");
2951 p = isl_printer_print_str(p, "{ ");
2952 data.print_dim = &print_dim_mpa;
2953 data.user = mpa;
2954 p = isl_print_space(mpa->space, p, 0, &data);
2955 p = isl_printer_print_str(p, " }");
2956 return p;
2959 __isl_give isl_printer *isl_printer_print_multi_pw_aff(
2960 __isl_take isl_printer *p, __isl_keep isl_multi_pw_aff *mpa)
2962 if (!p || !mpa)
2963 return isl_printer_free(p);
2965 if (p->output_format == ISL_FORMAT_ISL)
2966 return print_multi_pw_aff_isl(p, mpa);
2967 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2968 return isl_printer_free(p));
2971 /* Print dimension "pos" of data->space to "p".
2973 * data->user is assumed to be an isl_multi_val.
2975 * If the current dimension is an output dimension, then print
2976 * the corresponding value. Otherwise, print the name of the dimension.
2978 static __isl_give isl_printer *print_dim_mv(__isl_take isl_printer *p,
2979 struct isl_print_space_data *data, unsigned pos)
2981 isl_multi_val *mv = data->user;
2983 if (data->type == isl_dim_out)
2984 return isl_printer_print_val(p, mv->p[pos]);
2985 else
2986 return print_name(data->space, p, data->type, pos, data->latex);
2989 /* Print the isl_multi_val "mv" to "p" in isl format.
2991 static __isl_give isl_printer *print_multi_val_isl(__isl_take isl_printer *p,
2992 __isl_keep isl_multi_val *mv)
2994 struct isl_print_space_data data = { 0 };
2996 if (!mv)
2997 return isl_printer_free(p);
2999 if (isl_space_dim(mv->space, isl_dim_param) > 0) {
3000 p = print_tuple(mv->space, p, isl_dim_param, &data);
3001 p = isl_printer_print_str(p, " -> ");
3003 p = isl_printer_print_str(p, "{ ");
3004 data.print_dim = &print_dim_mv;
3005 data.user = mv;
3006 p = isl_print_space(mv->space, p, 0, &data);
3007 p = isl_printer_print_str(p, " }");
3008 return p;
3011 /* Print the isl_multi_val "mv" to "p".
3013 * Currently only supported in isl format.
3015 __isl_give isl_printer *isl_printer_print_multi_val(
3016 __isl_take isl_printer *p, __isl_keep isl_multi_val *mv)
3018 if (!p || !mv)
3019 return isl_printer_free(p);
3021 if (p->output_format == ISL_FORMAT_ISL)
3022 return print_multi_val_isl(p, mv);
3023 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3024 return isl_printer_free(p));
3027 /* Print dimension "pos" of data->space to "p".
3029 * data->user is assumed to be an isl_multi_union_pw_aff.
3031 * The current dimension is necessarily a set dimension, so
3032 * we print the corresponding isl_union_pw_aff, including
3033 * the braces.
3035 static __isl_give isl_printer *print_union_pw_aff_dim(__isl_take isl_printer *p,
3036 struct isl_print_space_data *data, unsigned pos)
3038 isl_multi_union_pw_aff *mupa = data->user;
3039 isl_union_pw_aff *upa;
3041 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, pos);
3042 p = print_union_pw_aff_body(p, upa);
3043 isl_union_pw_aff_free(upa);
3045 return p;
3048 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3050 static __isl_give isl_printer *print_multi_union_pw_aff_isl(
3051 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3053 struct isl_print_space_data data = { 0 };
3054 isl_space *space;
3056 space = isl_multi_union_pw_aff_get_space(mupa);
3057 if (isl_space_dim(space, isl_dim_param) > 0) {
3058 struct isl_print_space_data space_data = { 0 };
3059 p = print_tuple(space, p, isl_dim_param, &space_data);
3060 p = isl_printer_print_str(p, s_to[0]);
3063 data.print_dim = &print_union_pw_aff_dim;
3064 data.user = mupa;
3066 p = isl_print_space(space, p, 0, &data);
3067 isl_space_free(space);
3069 return p;
3072 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3074 * We currently only support an isl format.
3076 __isl_give isl_printer *isl_printer_print_multi_union_pw_aff(
3077 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3079 if (!p || !mupa)
3080 return isl_printer_free(p);
3082 if (p->output_format == ISL_FORMAT_ISL)
3083 return print_multi_union_pw_aff_isl(p, mupa);
3084 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
3085 "unsupported output format", return isl_printer_free(p));