isl_scheduler.c: extract out shared intra_dim_map
[isl.git] / isl_output.c
bloba894cf8d77531638624a611ab602428c38130dbb
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 and 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 isl_basic_map_is_div_constraint(bmap, bmap->ineq[i],
638 l - o_div))
639 continue;
640 s = isl_int_sgn(bmap->ineq[i][l]);
641 strict = !rational && isl_int_is_negone(bmap->ineq[i][0]);
642 if (s < 0)
643 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
644 else
645 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
646 if (strict)
647 isl_int_set_si(c->el[0], 0);
648 if (!p->dump && next_is_opposite(bmap, i, l)) {
649 op = constraint_op(-s, strict, latex);
650 p = print_half_constraint(bmap, space, div, p, c->el, l,
651 op, first, latex);
652 first = 1;
653 } else {
654 op = constraint_op(s, strict, latex);
655 p = print_constraint(bmap, space, div, p, c->el, l,
656 op, first, latex);
657 first = 0;
661 isl_basic_map_free(bmap);
662 isl_vec_free(c);
664 return p;
665 error:
666 isl_basic_map_free(bmap);
667 isl_vec_free(c);
668 isl_printer_free(p);
669 return NULL;
672 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
673 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
675 int c;
677 if (!p || !div)
678 return isl_printer_free(p);
680 c = p->output_format == ISL_FORMAT_C;
681 p = isl_printer_print_str(p, c ? "floord(" : "floor((");
682 p = print_affine_of_len(dim, div, p,
683 div->row[pos] + 1, div->n_col - 1);
684 p = isl_printer_print_str(p, c ? ", " : ")/");
685 p = isl_printer_print_isl_int(p, div->row[pos][0]);
686 p = isl_printer_print_str(p, ")");
687 return p;
690 /* Print a comma separated list of div names, except those that have
691 * a definition that can be printed.
692 * If "print_defined_divs" is set, then those div names are printed
693 * as well, along with their definitions.
695 static __isl_give isl_printer *print_div_list(__isl_take isl_printer *p,
696 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex,
697 int print_defined_divs)
699 int i;
700 int first = 1;
701 unsigned n_div;
703 if (!p || !space || !div)
704 return isl_printer_free(p);
706 n_div = isl_mat_rows(div);
708 for (i = 0; i < n_div; ++i) {
709 if (!print_defined_divs && can_print_div_expr(p, div, i))
710 continue;
711 if (!first)
712 p = isl_printer_print_str(p, ", ");
713 p = print_name(space, p, isl_dim_div, i, latex);
714 first = 0;
715 if (!can_print_div_expr(p, div, i))
716 continue;
717 p = isl_printer_print_str(p, " = ");
718 p = print_div(space, div, i, p);
721 return p;
724 /* Does printing "bmap" require an "exists" clause?
725 * That is, are there any local variables without an explicit representation?
727 static isl_bool need_exists(__isl_keep isl_printer *p,
728 __isl_keep isl_basic_map *bmap, __isl_keep isl_mat *div)
730 int i;
732 if (!p || !bmap)
733 return isl_bool_error;
734 if (bmap->n_div == 0)
735 return isl_bool_false;
736 for (i = 0; i < bmap->n_div; ++i)
737 if (!can_print_div_expr(p, div, i))
738 return isl_bool_true;
739 return isl_bool_false;
742 /* Print the constraints of "bmap" to "p".
743 * The names of the variables are taken from "space".
744 * "latex" is set if the constraints should be printed in LaTeX format.
745 * Do not print inline explicit div representations in "dump" mode.
747 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
748 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
750 isl_mat *div;
751 isl_bool exists;
753 div = isl_basic_map_get_divs(bmap);
754 if (p->dump)
755 exists = bmap->n_div > 0;
756 else
757 exists = need_exists(p, bmap, div);
758 if (exists >= 0 && exists) {
759 p = isl_printer_print_str(p, s_open_exists[latex]);
760 p = print_div_list(p, space, div, latex, p->dump);
761 p = isl_printer_print_str(p, ": ");
764 if (p->dump)
765 div = isl_mat_free(div);
766 p = print_constraints(bmap, space, div, p, latex);
767 isl_mat_free(div);
769 if (exists >= 0 && exists)
770 p = isl_printer_print_str(p, s_close_exists[latex]);
771 return p;
774 /* Print a colon followed by the constraints of "bmap"
775 * to "p", provided there are any constraints.
776 * The names of the variables are taken from "space".
777 * "latex" is set if the constraints should be printed in LaTeX format.
779 static __isl_give isl_printer *print_optional_disjunct(
780 __isl_keep isl_basic_map *bmap, __isl_keep isl_space *space,
781 __isl_take isl_printer *p, int latex)
783 if (isl_basic_map_plain_is_universe(bmap))
784 return p;
786 p = isl_printer_print_str(p, ": ");
787 p = print_disjunct(bmap, space, p, latex);
789 return p;
792 static __isl_give isl_printer *basic_map_print_omega(
793 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
795 p = isl_printer_print_str(p, "{ [");
796 p = print_var_list(p, bmap->dim, isl_dim_in);
797 p = isl_printer_print_str(p, "] -> [");
798 p = print_var_list(p, bmap->dim, isl_dim_out);
799 p = isl_printer_print_str(p, "] ");
800 p = print_optional_disjunct(bmap, bmap->dim, p, 0);
801 p = isl_printer_print_str(p, " }");
802 return p;
805 static __isl_give isl_printer *basic_set_print_omega(
806 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
808 p = isl_printer_print_str(p, "{ [");
809 p = print_var_list(p, bset->dim, isl_dim_set);
810 p = isl_printer_print_str(p, "] ");
811 p = print_optional_disjunct(bset, bset->dim, p, 0);
812 p = isl_printer_print_str(p, " }");
813 return p;
816 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
817 __isl_take isl_printer *p)
819 int i;
821 for (i = 0; i < map->n; ++i) {
822 if (i)
823 p = isl_printer_print_str(p, " union ");
824 p = basic_map_print_omega(map->p[i], p);
826 return p;
829 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
830 __isl_take isl_printer *p)
832 int i;
834 for (i = 0; i < set->n; ++i) {
835 if (i)
836 p = isl_printer_print_str(p, " union ");
837 p = basic_set_print_omega(set->p[i], p);
839 return p;
842 static __isl_give isl_printer *isl_basic_map_print_isl(
843 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
844 int latex)
846 struct isl_print_space_data data = { .latex = latex };
847 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
849 if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
850 p = print_tuple(bmap->dim, p, isl_dim_param, &data);
851 p = isl_printer_print_str(p, " -> ");
853 p = isl_printer_print_str(p, "{ ");
854 p = isl_print_space(bmap->dim, p, rational, &data);
855 p = isl_printer_print_str(p, " : ");
856 p = print_disjunct(bmap, bmap->dim, p, latex);
857 p = isl_printer_print_str(p, " }");
858 return p;
861 /* Print the disjuncts of a map (or set) "map" to "p".
862 * The names of the variables are taken from "space".
863 * "latex" is set if the constraints should be printed in LaTeX format.
865 static __isl_give isl_printer *print_disjuncts_core(__isl_keep isl_map *map,
866 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
868 int i;
870 if (map->n == 0)
871 p = isl_printer_print_str(p, "1 = 0");
872 for (i = 0; i < map->n; ++i) {
873 if (i)
874 p = isl_printer_print_str(p, s_or[latex]);
875 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
876 p = isl_printer_print_str(p, "(");
877 p = print_disjunct(map->p[i], space, p, latex);
878 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
879 p = isl_printer_print_str(p, ")");
881 return p;
884 /* Print the disjuncts of a map (or set) "map" to "p".
885 * The names of the variables are taken from "space".
886 * "hull" describes constraints shared by all disjuncts of "map".
887 * "latex" is set if the constraints should be printed in LaTeX format.
889 * Print the disjuncts as a conjunction of "hull" and
890 * the result of removing the constraints of "hull" from "map".
891 * If this result turns out to be the universe, then simply print "hull".
893 static __isl_give isl_printer *print_disjuncts_in_hull(__isl_keep isl_map *map,
894 __isl_keep isl_space *space, __isl_take isl_basic_map *hull,
895 __isl_take isl_printer *p, int latex)
897 isl_bool is_universe;
899 p = print_disjunct(hull, space, p, latex);
900 map = isl_map_plain_gist_basic_map(isl_map_copy(map), hull);
901 is_universe = isl_map_plain_is_universe(map);
902 if (is_universe < 0)
903 goto error;
904 if (!is_universe) {
905 p = isl_printer_print_str(p, s_and[latex]);
906 p = isl_printer_print_str(p, "(");
907 p = print_disjuncts_core(map, space, p, latex);
908 p = isl_printer_print_str(p, ")");
910 isl_map_free(map);
912 return p;
913 error:
914 isl_map_free(map);
915 isl_printer_free(p);
916 return NULL;
919 /* Print the disjuncts of a map (or set) "map" to "p".
920 * The names of the variables are taken from "space".
921 * "latex" is set if the constraints should be printed in LaTeX format.
923 * If there are at least two disjuncts and "dump" mode is not turned out,
924 * check for any shared constraints among all disjuncts.
925 * If there are any, then print them separately in print_disjuncts_in_hull.
927 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
928 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
930 if (isl_map_plain_is_universe(map))
931 return p;
933 p = isl_printer_print_str(p, s_such_that[latex]);
935 if (!p->dump && map->n >= 2) {
936 isl_basic_map *hull;
937 isl_bool is_universe;
939 hull = isl_map_plain_unshifted_simple_hull(isl_map_copy(map));
940 is_universe = isl_basic_map_plain_is_universe(hull);
941 if (is_universe < 0)
942 p = isl_printer_free(p);
943 else if (!is_universe)
944 return print_disjuncts_in_hull(map, space, hull,
945 p, latex);
946 isl_basic_map_free(hull);
949 return print_disjuncts_core(map, space, p, latex);
952 /* Print the disjuncts of a map (or set).
953 * The names of the variables are taken from "space".
954 * "latex" is set if the constraints should be printed in LaTeX format.
956 * If the map turns out to be a universal parameter domain, then
957 * we need to print the colon. Otherwise, the output looks identical
958 * to the empty set.
960 static __isl_give isl_printer *print_disjuncts_map(__isl_keep isl_map *map,
961 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
963 if (isl_map_plain_is_universe(map) && isl_space_is_params(map->dim))
964 return isl_printer_print_str(p, s_such_that[latex]);
965 else
966 return print_disjuncts(map, space, p, latex);
969 struct isl_aff_split {
970 isl_basic_map *aff;
971 isl_map *map;
974 static void free_split(__isl_take struct isl_aff_split *split, int n)
976 int i;
978 if (!split)
979 return;
981 for (i = 0; i < n; ++i) {
982 isl_basic_map_free(split[i].aff);
983 isl_map_free(split[i].map);
986 free(split);
989 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
991 int i, j;
992 unsigned nparam, n_in, n_out, total;
994 bmap = isl_basic_map_cow(bmap);
995 if (!bmap)
996 return NULL;
997 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
998 goto error;
1000 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1001 n_in = isl_basic_map_dim(bmap, isl_dim_in);
1002 n_out = isl_basic_map_dim(bmap, isl_dim_out);
1003 total = isl_basic_map_dim(bmap, isl_dim_all);
1004 for (i = bmap->n_eq - 1; i >= 0; --i) {
1005 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
1006 if (j >= nparam && j < nparam + n_in + n_out &&
1007 (isl_int_is_one(bmap->eq[i][1 + j]) ||
1008 isl_int_is_negone(bmap->eq[i][1 + j])))
1009 continue;
1010 if (isl_basic_map_drop_equality(bmap, i) < 0)
1011 goto error;
1014 bmap = isl_basic_map_finalize(bmap);
1016 return bmap;
1017 error:
1018 isl_basic_map_free(bmap);
1019 return NULL;
1022 static int aff_split_cmp(const void *p1, const void *p2, void *user)
1024 const struct isl_aff_split *s1, *s2;
1025 s1 = (const struct isl_aff_split *) p1;
1026 s2 = (const struct isl_aff_split *) p2;
1028 return isl_basic_map_plain_cmp(s1->aff, s2->aff);
1031 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
1032 __isl_keep isl_basic_map *aff)
1034 int i, j;
1035 unsigned total;
1037 if (!bmap || !aff)
1038 goto error;
1040 total = isl_space_dim(bmap->dim, isl_dim_all);
1042 for (i = bmap->n_eq - 1; i >= 0; --i) {
1043 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
1044 bmap->n_div) != -1)
1045 continue;
1046 for (j = 0; j < aff->n_eq; ++j) {
1047 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
1048 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
1049 continue;
1050 if (isl_basic_map_drop_equality(bmap, i) < 0)
1051 goto error;
1052 break;
1056 return bmap;
1057 error:
1058 isl_basic_map_free(bmap);
1059 return NULL;
1062 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
1064 int i, n;
1065 struct isl_aff_split *split;
1066 isl_ctx *ctx;
1068 ctx = isl_map_get_ctx(map);
1069 split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
1070 if (!split)
1071 return NULL;
1073 for (i = 0; i < map->n; ++i) {
1074 isl_basic_map *bmap;
1075 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
1076 bmap = isl_basic_map_copy(map->p[i]);
1077 bmap = isl_basic_map_cow(bmap);
1078 bmap = drop_aff(bmap, split[i].aff);
1079 split[i].map = isl_map_from_basic_map(bmap);
1080 if (!split[i].aff || !split[i].map)
1081 goto error;
1084 if (isl_sort(split, map->n, sizeof(struct isl_aff_split),
1085 &aff_split_cmp, NULL) < 0)
1086 goto error;
1088 n = map->n;
1089 for (i = n - 1; i >= 1; --i) {
1090 if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
1091 split[i].aff))
1092 continue;
1093 isl_basic_map_free(split[i].aff);
1094 split[i - 1].map = isl_map_union(split[i - 1].map,
1095 split[i].map);
1096 if (i != n - 1)
1097 split[i] = split[n - 1];
1098 split[n - 1].aff = NULL;
1099 split[n - 1].map = NULL;
1100 --n;
1103 return split;
1104 error:
1105 free_split(split, map->n);
1106 return NULL;
1109 static int defining_equality(__isl_keep isl_basic_map *eq,
1110 __isl_keep isl_space *dim, enum isl_dim_type type, int pos)
1112 int i;
1113 unsigned total;
1115 if (!eq)
1116 return -1;
1118 pos += isl_space_offset(dim, type);
1119 total = isl_basic_map_total_dim(eq);
1121 for (i = 0; i < eq->n_eq; ++i) {
1122 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
1123 continue;
1124 if (isl_int_is_one(eq->eq[i][1 + pos]))
1125 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
1126 return i;
1129 return -1;
1132 /* Print dimension "pos" of data->space to "p".
1134 * data->user is assumed to be an isl_basic_map keeping track of equalities.
1136 * If the current dimension is defined by these equalities, then print
1137 * the corresponding expression, assigned to the name of the dimension
1138 * if there is any. Otherwise, print the name of the dimension.
1140 static __isl_give isl_printer *print_dim_eq(__isl_take isl_printer *p,
1141 struct isl_print_space_data *data, unsigned pos)
1143 isl_basic_map *eq = data->user;
1144 int j;
1146 j = defining_equality(eq, data->space, data->type, pos);
1147 if (j >= 0) {
1148 if (isl_space_has_dim_name(data->space, data->type, pos)) {
1149 p = print_name(data->space, p, data->type, pos,
1150 data->latex);
1151 p = isl_printer_print_str(p, " = ");
1153 pos += 1 + isl_space_offset(data->space, data->type);
1154 p = print_affine_of_len(eq->dim, NULL, p, eq->eq[j], pos);
1155 } else {
1156 p = print_name(data->space, p, data->type, pos, data->latex);
1159 return p;
1162 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
1163 struct isl_aff_split *split, int n, __isl_keep isl_space *space)
1165 struct isl_print_space_data data = { 0 };
1166 int i;
1167 int rational;
1169 data.print_dim = &print_dim_eq;
1170 for (i = 0; i < n; ++i) {
1171 if (!split[i].map)
1172 break;
1173 rational = split[i].map->n > 0 &&
1174 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
1175 if (i)
1176 p = isl_printer_print_str(p, "; ");
1177 data.user = split[i].aff;
1178 p = isl_print_space(space, p, rational, &data);
1179 p = print_disjuncts_map(split[i].map, space, p, 0);
1182 return p;
1185 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
1186 __isl_take isl_printer *p)
1188 struct isl_print_space_data data = { 0 };
1189 struct isl_aff_split *split = NULL;
1190 int rational;
1192 if (!p->dump && map->n > 0)
1193 split = split_aff(map);
1194 if (split) {
1195 p = print_split_map(p, split, map->n, map->dim);
1196 } else {
1197 rational = map->n > 0 &&
1198 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
1199 p = isl_print_space(map->dim, p, rational, &data);
1200 p = print_disjuncts_map(map, map->dim, p, 0);
1202 free_split(split, map->n);
1203 return p;
1206 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
1207 __isl_take isl_printer *p)
1209 struct isl_print_space_data data = { 0 };
1211 if (isl_map_dim(map, isl_dim_param) > 0) {
1212 p = print_tuple(map->dim, p, isl_dim_param, &data);
1213 p = isl_printer_print_str(p, s_to[0]);
1215 p = isl_printer_print_str(p, s_open_set[0]);
1216 p = isl_map_print_isl_body(map, p);
1217 p = isl_printer_print_str(p, s_close_set[0]);
1218 return p;
1221 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
1222 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff)
1224 struct isl_print_space_data data = { 0 };
1226 data.latex = 1;
1227 if (isl_map_dim(map, isl_dim_param) > 0) {
1228 p = print_tuple(map->dim, p, isl_dim_param, &data);
1229 p = isl_printer_print_str(p, s_to[1]);
1231 p = isl_printer_print_str(p, s_open_set[1]);
1232 data.print_dim = &print_dim_eq;
1233 data.user = aff;
1234 p = isl_print_space(map->dim, p, 0, &data);
1235 p = print_disjuncts_map(map, map->dim, p, 1);
1236 p = isl_printer_print_str(p, s_close_set[1]);
1238 return p;
1241 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
1242 __isl_take isl_printer *p)
1244 int i;
1245 struct isl_aff_split *split = NULL;
1247 if (map->n > 0)
1248 split = split_aff(map);
1250 if (!split)
1251 return print_latex_map(map, p, NULL);
1253 for (i = 0; i < map->n; ++i) {
1254 if (!split[i].map)
1255 break;
1256 if (i)
1257 p = isl_printer_print_str(p, " \\cup ");
1258 p = print_latex_map(split[i].map, p, split[i].aff);
1261 free_split(split, map->n);
1262 return p;
1265 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
1266 __isl_keep isl_basic_map *bmap)
1268 if (!p || !bmap)
1269 goto error;
1270 if (p->output_format == ISL_FORMAT_ISL)
1271 return isl_basic_map_print_isl(bmap, p, 0);
1272 else if (p->output_format == ISL_FORMAT_OMEGA)
1273 return basic_map_print_omega(bmap, p);
1274 isl_assert(bmap->ctx, 0, goto error);
1275 error:
1276 isl_printer_free(p);
1277 return NULL;
1280 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
1281 __isl_keep isl_basic_set *bset)
1283 if (!p || !bset)
1284 goto error;
1286 if (p->output_format == ISL_FORMAT_ISL)
1287 return isl_basic_map_print_isl(bset, p, 0);
1288 else if (p->output_format == ISL_FORMAT_POLYLIB)
1289 return isl_basic_set_print_polylib(bset, p, 0);
1290 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1291 return isl_basic_set_print_polylib(bset, p, 1);
1292 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
1293 return bset_print_constraints_polylib(bset, p);
1294 else if (p->output_format == ISL_FORMAT_OMEGA)
1295 return basic_set_print_omega(bset, p);
1296 isl_assert(p->ctx, 0, goto error);
1297 error:
1298 isl_printer_free(p);
1299 return NULL;
1302 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
1303 __isl_keep isl_set *set)
1305 if (!p || !set)
1306 goto error;
1307 if (p->output_format == ISL_FORMAT_ISL)
1308 return isl_map_print_isl((isl_map *)set, p);
1309 else if (p->output_format == ISL_FORMAT_POLYLIB)
1310 return isl_set_print_polylib(set, p, 0);
1311 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1312 return isl_set_print_polylib(set, p, 1);
1313 else if (p->output_format == ISL_FORMAT_OMEGA)
1314 return isl_set_print_omega(set, p);
1315 else if (p->output_format == ISL_FORMAT_LATEX)
1316 return isl_map_print_latex((isl_map *)set, p);
1317 isl_assert(set->ctx, 0, goto error);
1318 error:
1319 isl_printer_free(p);
1320 return NULL;
1323 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1324 __isl_keep isl_map *map)
1326 if (!p || !map)
1327 goto error;
1329 if (p->output_format == ISL_FORMAT_ISL)
1330 return isl_map_print_isl(map, p);
1331 else if (p->output_format == ISL_FORMAT_POLYLIB)
1332 return isl_map_print_polylib(map, p, 0);
1333 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1334 return isl_map_print_polylib(map, p, 1);
1335 else if (p->output_format == ISL_FORMAT_OMEGA)
1336 return isl_map_print_omega(map, p);
1337 else if (p->output_format == ISL_FORMAT_LATEX)
1338 return isl_map_print_latex(map, p);
1339 isl_assert(map->ctx, 0, goto error);
1340 error:
1341 isl_printer_free(p);
1342 return NULL;
1345 struct isl_union_print_data {
1346 isl_printer *p;
1347 int first;
1350 static isl_stat print_map_body(__isl_take isl_map *map, void *user)
1352 struct isl_union_print_data *data;
1353 data = (struct isl_union_print_data *)user;
1355 if (!data->first)
1356 data->p = isl_printer_print_str(data->p, "; ");
1357 data->first = 0;
1359 data->p = isl_map_print_isl_body(map, data->p);
1360 isl_map_free(map);
1362 return isl_stat_ok;
1365 static __isl_give isl_printer *isl_union_map_print_isl(
1366 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1368 struct isl_union_print_data data;
1369 struct isl_print_space_data space_data = { 0 };
1370 isl_space *dim;
1372 dim = isl_union_map_get_space(umap);
1373 if (isl_space_dim(dim, isl_dim_param) > 0) {
1374 p = print_tuple(dim, p, isl_dim_param, &space_data);
1375 p = isl_printer_print_str(p, s_to[0]);
1377 isl_space_free(dim);
1378 p = isl_printer_print_str(p, s_open_set[0]);
1379 data.p = p;
1380 data.first = 1;
1381 isl_union_map_foreach_map(umap, &print_map_body, &data);
1382 p = data.p;
1383 p = isl_printer_print_str(p, s_close_set[0]);
1384 return p;
1387 static isl_stat print_latex_map_body(__isl_take isl_map *map, void *user)
1389 struct isl_union_print_data *data;
1390 data = (struct isl_union_print_data *)user;
1392 if (!data->first)
1393 data->p = isl_printer_print_str(data->p, " \\cup ");
1394 data->first = 0;
1396 data->p = isl_map_print_latex(map, data->p);
1397 isl_map_free(map);
1399 return isl_stat_ok;
1402 static __isl_give isl_printer *isl_union_map_print_latex(
1403 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1405 struct isl_union_print_data data = { p, 1 };
1406 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1407 p = data.p;
1408 return p;
1411 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1412 __isl_keep isl_union_map *umap)
1414 if (!p || !umap)
1415 goto error;
1417 if (p->output_format == ISL_FORMAT_ISL)
1418 return isl_union_map_print_isl(umap, p);
1419 if (p->output_format == ISL_FORMAT_LATEX)
1420 return isl_union_map_print_latex(umap, p);
1422 isl_die(p->ctx, isl_error_invalid,
1423 "invalid output format for isl_union_map", goto error);
1424 error:
1425 isl_printer_free(p);
1426 return NULL;
1429 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1430 __isl_keep isl_union_set *uset)
1432 if (!p || !uset)
1433 goto error;
1435 if (p->output_format == ISL_FORMAT_ISL)
1436 return isl_union_map_print_isl((isl_union_map *)uset, p);
1437 if (p->output_format == ISL_FORMAT_LATEX)
1438 return isl_union_map_print_latex((isl_union_map *)uset, p);
1440 isl_die(p->ctx, isl_error_invalid,
1441 "invalid output format for isl_union_set", goto error);
1442 error:
1443 isl_printer_free(p);
1444 return NULL;
1447 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
1449 int i;
1450 int n;
1452 for (i = 0, n = 0; i < rec->n; ++i)
1453 if (!isl_upoly_is_zero(rec->p[i]))
1454 ++n;
1456 return n;
1459 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
1460 __isl_take isl_printer *p, int first)
1462 struct isl_upoly_cst *cst;
1463 int neg;
1465 cst = isl_upoly_as_cst(up);
1466 if (!cst)
1467 goto error;
1468 neg = !first && isl_int_is_neg(cst->n);
1469 if (!first)
1470 p = isl_printer_print_str(p, neg ? " - " : " + ");
1471 if (neg)
1472 isl_int_neg(cst->n, cst->n);
1473 if (isl_int_is_zero(cst->d)) {
1474 int sgn = isl_int_sgn(cst->n);
1475 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1476 sgn == 0 ? "NaN" : "infty");
1477 } else
1478 p = isl_printer_print_isl_int(p, cst->n);
1479 if (neg)
1480 isl_int_neg(cst->n, cst->n);
1481 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1482 p = isl_printer_print_str(p, "/");
1483 p = isl_printer_print_isl_int(p, cst->d);
1485 return p;
1486 error:
1487 isl_printer_free(p);
1488 return NULL;
1491 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1492 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var)
1494 unsigned total;
1496 total = isl_space_dim(dim, isl_dim_all);
1497 if (var < total)
1498 p = print_term(dim, NULL, dim->ctx->one, 1 + var, p, 0);
1499 else
1500 p = print_div(dim, div, var - total, p);
1501 return p;
1504 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1505 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var, int exp)
1507 p = print_base(p, dim, div, var);
1508 if (exp == 1)
1509 return p;
1510 if (p->output_format == ISL_FORMAT_C) {
1511 int i;
1512 for (i = 1; i < exp; ++i) {
1513 p = isl_printer_print_str(p, "*");
1514 p = print_base(p, dim, div, var);
1516 } else {
1517 p = isl_printer_print_str(p, "^");
1518 p = isl_printer_print_int(p, exp);
1520 return p;
1523 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1524 __isl_keep isl_space *dim, __isl_keep isl_mat *div,
1525 __isl_take isl_printer *p, int outer)
1527 int i, n, first, print_parens;
1528 struct isl_upoly_rec *rec;
1530 if (!p || !up || !dim || !div)
1531 goto error;
1533 if (isl_upoly_is_cst(up))
1534 return upoly_print_cst(up, p, 1);
1536 rec = isl_upoly_as_rec(up);
1537 if (!rec)
1538 goto error;
1539 n = upoly_rec_n_non_zero(rec);
1540 print_parens = n > 1 ||
1541 (outer && rec->up.var >= isl_space_dim(dim, isl_dim_all));
1542 if (print_parens)
1543 p = isl_printer_print_str(p, "(");
1544 for (i = 0, first = 1; i < rec->n; ++i) {
1545 if (isl_upoly_is_zero(rec->p[i]))
1546 continue;
1547 if (isl_upoly_is_negone(rec->p[i])) {
1548 if (!i)
1549 p = isl_printer_print_str(p, "-1");
1550 else if (first)
1551 p = isl_printer_print_str(p, "-");
1552 else
1553 p = isl_printer_print_str(p, " - ");
1554 } else if (isl_upoly_is_cst(rec->p[i]) &&
1555 !isl_upoly_is_one(rec->p[i]))
1556 p = upoly_print_cst(rec->p[i], p, first);
1557 else {
1558 if (!first)
1559 p = isl_printer_print_str(p, " + ");
1560 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1561 p = upoly_print(rec->p[i], dim, div, p, 0);
1563 first = 0;
1564 if (i == 0)
1565 continue;
1566 if (!isl_upoly_is_one(rec->p[i]) &&
1567 !isl_upoly_is_negone(rec->p[i]))
1568 p = isl_printer_print_str(p, " * ");
1569 p = print_pow(p, dim, div, rec->up.var, i);
1571 if (print_parens)
1572 p = isl_printer_print_str(p, ")");
1573 return p;
1574 error:
1575 isl_printer_free(p);
1576 return NULL;
1579 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1580 __isl_keep isl_qpolynomial *qp)
1582 if (!p || !qp)
1583 goto error;
1584 p = upoly_print(qp->upoly, qp->dim, qp->div, p, 1);
1585 return p;
1586 error:
1587 isl_printer_free(p);
1588 return NULL;
1591 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1592 __isl_keep isl_qpolynomial *qp)
1594 struct isl_print_space_data data = { 0 };
1596 if (!p || !qp)
1597 goto error;
1599 if (isl_space_dim(qp->dim, isl_dim_param) > 0) {
1600 p = print_tuple(qp->dim, p, isl_dim_param, &data);
1601 p = isl_printer_print_str(p, " -> ");
1603 p = isl_printer_print_str(p, "{ ");
1604 if (!isl_space_is_params(qp->dim)) {
1605 p = isl_print_space(qp->dim, p, 0, &data);
1606 p = isl_printer_print_str(p, " -> ");
1608 p = print_qpolynomial(p, qp);
1609 p = isl_printer_print_str(p, " }");
1610 return p;
1611 error:
1612 isl_printer_free(p);
1613 return NULL;
1616 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1617 __isl_keep isl_space *dim, __isl_keep isl_qpolynomial *qp)
1619 isl_int den;
1621 isl_int_init(den);
1622 isl_qpolynomial_get_den(qp, &den);
1623 if (!isl_int_is_one(den)) {
1624 isl_qpolynomial *f;
1625 p = isl_printer_print_str(p, "(");
1626 qp = isl_qpolynomial_copy(qp);
1627 f = isl_qpolynomial_rat_cst_on_domain(isl_space_copy(qp->dim),
1628 den, qp->dim->ctx->one);
1629 qp = isl_qpolynomial_mul(qp, f);
1631 if (qp)
1632 p = upoly_print(qp->upoly, dim, qp->div, p, 0);
1633 else
1634 p = isl_printer_free(p);
1635 if (!isl_int_is_one(den)) {
1636 p = isl_printer_print_str(p, ")/");
1637 p = isl_printer_print_isl_int(p, den);
1638 isl_qpolynomial_free(qp);
1640 isl_int_clear(den);
1641 return p;
1644 __isl_give isl_printer *isl_printer_print_qpolynomial(
1645 __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1647 if (!p || !qp)
1648 goto error;
1650 if (p->output_format == ISL_FORMAT_ISL)
1651 return print_qpolynomial_isl(p, qp);
1652 else if (p->output_format == ISL_FORMAT_C)
1653 return print_qpolynomial_c(p, qp->dim, qp);
1654 else
1655 isl_die(qp->dim->ctx, isl_error_unsupported,
1656 "output format not supported for isl_qpolynomials",
1657 goto error);
1658 error:
1659 isl_printer_free(p);
1660 return NULL;
1663 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1664 unsigned output_format)
1666 isl_printer *p;
1668 if (!qp)
1669 return;
1671 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1672 p = isl_printer_to_file(qp->dim->ctx, out);
1673 p = isl_printer_print_qpolynomial(p, qp);
1674 isl_printer_free(p);
1677 static __isl_give isl_printer *qpolynomial_fold_print(
1678 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1680 int i;
1682 if (fold->type == isl_fold_min)
1683 p = isl_printer_print_str(p, "min");
1684 else if (fold->type == isl_fold_max)
1685 p = isl_printer_print_str(p, "max");
1686 p = isl_printer_print_str(p, "(");
1687 for (i = 0; i < fold->n; ++i) {
1688 if (i)
1689 p = isl_printer_print_str(p, ", ");
1690 p = print_qpolynomial(p, fold->qp[i]);
1692 p = isl_printer_print_str(p, ")");
1693 return p;
1696 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1697 FILE *out, unsigned output_format)
1699 isl_printer *p;
1701 if (!fold)
1702 return;
1704 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1706 p = isl_printer_to_file(fold->dim->ctx, out);
1707 p = isl_printer_print_qpolynomial_fold(p, fold);
1709 isl_printer_free(p);
1712 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1713 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1715 struct isl_print_space_data data = { 0 };
1716 int i = 0;
1718 for (i = 0; i < pwqp->n; ++i) {
1719 isl_space *space;
1721 if (i)
1722 p = isl_printer_print_str(p, "; ");
1723 space = isl_qpolynomial_get_domain_space(pwqp->p[i].qp);
1724 if (!isl_space_is_params(space)) {
1725 p = isl_print_space(space, p, 0, &data);
1726 p = isl_printer_print_str(p, " -> ");
1728 p = print_qpolynomial(p, pwqp->p[i].qp);
1729 p = print_disjuncts((isl_map *)pwqp->p[i].set, space, p, 0);
1730 isl_space_free(space);
1733 return p;
1736 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1737 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1739 struct isl_print_space_data data = { 0 };
1741 if (!p || !pwqp)
1742 goto error;
1744 if (isl_space_dim(pwqp->dim, isl_dim_param) > 0) {
1745 p = print_tuple(pwqp->dim, p, isl_dim_param, &data);
1746 p = isl_printer_print_str(p, " -> ");
1748 p = isl_printer_print_str(p, "{ ");
1749 if (pwqp->n == 0) {
1750 if (!isl_space_is_set(pwqp->dim)) {
1751 p = print_tuple(pwqp->dim, p, isl_dim_in, &data);
1752 p = isl_printer_print_str(p, " -> ");
1754 p = isl_printer_print_str(p, "0");
1756 p = isl_pwqp_print_isl_body(p, pwqp);
1757 p = isl_printer_print_str(p, " }");
1758 return p;
1759 error:
1760 isl_printer_free(p);
1761 return NULL;
1764 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
1765 unsigned output_format)
1767 isl_printer *p;
1769 if (!pwqp)
1770 return;
1772 p = isl_printer_to_file(pwqp->dim->ctx, out);
1773 p = isl_printer_set_output_format(p, output_format);
1774 p = isl_printer_print_pw_qpolynomial(p, pwqp);
1776 isl_printer_free(p);
1779 static __isl_give isl_printer *isl_pwf_print_isl_body(
1780 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1782 struct isl_print_space_data data = { 0 };
1783 int i = 0;
1785 for (i = 0; i < pwf->n; ++i) {
1786 isl_space *space;
1788 if (i)
1789 p = isl_printer_print_str(p, "; ");
1790 space = isl_qpolynomial_fold_get_domain_space(pwf->p[i].fold);
1791 if (!isl_space_is_params(space)) {
1792 p = isl_print_space(space, p, 0, &data);
1793 p = isl_printer_print_str(p, " -> ");
1795 p = qpolynomial_fold_print(pwf->p[i].fold, p);
1796 p = print_disjuncts((isl_map *)pwf->p[i].set, space, p, 0);
1797 isl_space_free(space);
1800 return p;
1803 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
1804 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1806 struct isl_print_space_data data = { 0 };
1808 if (isl_space_dim(pwf->dim, isl_dim_param) > 0) {
1809 p = print_tuple(pwf->dim, p, isl_dim_param, &data);
1810 p = isl_printer_print_str(p, " -> ");
1812 p = isl_printer_print_str(p, "{ ");
1813 if (pwf->n == 0) {
1814 if (!isl_space_is_set(pwf->dim)) {
1815 p = print_tuple(pwf->dim, p, isl_dim_in, &data);
1816 p = isl_printer_print_str(p, " -> ");
1818 p = isl_printer_print_str(p, "0");
1820 p = isl_pwf_print_isl_body(p, pwf);
1821 p = isl_printer_print_str(p, " }");
1822 return p;
1825 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1826 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c);
1828 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1829 __isl_keep isl_space *dim,
1830 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1832 if (type == isl_dim_div) {
1833 p = isl_printer_print_str(p, "floord(");
1834 p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
1835 p = isl_printer_print_str(p, ", ");
1836 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1837 p = isl_printer_print_str(p, ")");
1838 } else {
1839 const char *name;
1841 name = isl_space_get_dim_name(dim, type, pos);
1842 if (!name)
1843 name = "UNNAMED";
1844 p = isl_printer_print_str(p, name);
1846 return p;
1849 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1850 __isl_keep isl_space *dim,
1851 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1853 enum isl_dim_type type;
1855 if (pos == 0)
1856 return isl_printer_print_isl_int(p, c);
1858 if (isl_int_is_one(c))
1860 else if (isl_int_is_negone(c))
1861 p = isl_printer_print_str(p, "-");
1862 else {
1863 p = isl_printer_print_isl_int(p, c);
1864 p = isl_printer_print_str(p, "*");
1866 type = pos2type(dim, &pos);
1867 p = print_name_c(p, dim, bset, type, pos);
1868 return p;
1871 static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
1872 __isl_keep isl_space *dim,
1873 __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
1875 int i;
1876 int first;
1878 for (i = 0, first = 1; i < len; ++i) {
1879 int flip = 0;
1880 if (isl_int_is_zero(c[i]))
1881 continue;
1882 if (!first) {
1883 if (isl_int_is_neg(c[i])) {
1884 flip = 1;
1885 isl_int_neg(c[i], c[i]);
1886 p = isl_printer_print_str(p, " - ");
1887 } else
1888 p = isl_printer_print_str(p, " + ");
1890 first = 0;
1891 p = print_term_c(p, dim, bset, c[i], i);
1892 if (flip)
1893 isl_int_neg(c[i], c[i]);
1895 if (first)
1896 p = isl_printer_print_str(p, "0");
1897 return p;
1900 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1901 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c)
1903 unsigned len = 1 + isl_basic_set_total_dim(bset);
1904 return print_partial_affine_c(p, dim, bset, c, len);
1907 /* We skip the constraint if it is implied by the div expression.
1909 * *first indicates whether this is the first constraint in the conjunction and
1910 * is updated if the constraint is actually printed.
1912 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1913 __isl_keep isl_space *dim,
1914 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int *first)
1916 unsigned o_div;
1917 unsigned n_div;
1918 int div;
1920 o_div = isl_basic_set_offset(bset, isl_dim_div);
1921 n_div = isl_basic_set_dim(bset, isl_dim_div);
1922 div = isl_seq_last_non_zero(c + o_div, n_div);
1923 if (div >= 0 && isl_basic_set_is_div_constraint(bset, c, div))
1924 return p;
1926 if (!*first)
1927 p = isl_printer_print_str(p, " && ");
1929 p = print_affine_c(p, dim, bset, c);
1930 p = isl_printer_print_str(p, " ");
1931 p = isl_printer_print_str(p, op);
1932 p = isl_printer_print_str(p, " 0");
1934 *first = 0;
1936 return p;
1939 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1940 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset)
1942 int i, j;
1943 int first = 1;
1944 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
1945 unsigned total = isl_basic_set_total_dim(bset) - n_div;
1947 for (i = 0; i < bset->n_eq; ++i) {
1948 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
1949 if (j < 0)
1950 p = print_constraint_c(p, dim, bset,
1951 bset->eq[i], "==", &first);
1952 else {
1953 if (i)
1954 p = isl_printer_print_str(p, " && ");
1955 p = isl_printer_print_str(p, "(");
1956 p = print_partial_affine_c(p, dim, bset, bset->eq[i],
1957 1 + total + j);
1958 p = isl_printer_print_str(p, ") % ");
1959 p = isl_printer_print_isl_int(p,
1960 bset->eq[i][1 + total + j]);
1961 p = isl_printer_print_str(p, " == 0");
1962 first = 0;
1965 for (i = 0; i < bset->n_ineq; ++i)
1966 p = print_constraint_c(p, dim, bset, bset->ineq[i], ">=",
1967 &first);
1968 return p;
1971 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
1972 __isl_keep isl_space *dim, __isl_keep isl_set *set)
1974 int i;
1976 if (!set)
1977 return isl_printer_free(p);
1979 if (set->n == 0)
1980 p = isl_printer_print_str(p, "0");
1982 for (i = 0; i < set->n; ++i) {
1983 if (i)
1984 p = isl_printer_print_str(p, " || ");
1985 if (set->n > 1)
1986 p = isl_printer_print_str(p, "(");
1987 p = print_basic_set_c(p, dim, set->p[i]);
1988 if (set->n > 1)
1989 p = isl_printer_print_str(p, ")");
1991 return p;
1994 static __isl_give isl_printer *print_pw_qpolynomial_c(
1995 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1997 int i;
1999 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set))
2000 return print_qpolynomial_c(p, pwqp->dim, pwqp->p[0].qp);
2002 for (i = 0; i < pwqp->n; ++i) {
2003 p = isl_printer_print_str(p, "(");
2004 p = print_set_c(p, pwqp->dim, pwqp->p[i].set);
2005 p = isl_printer_print_str(p, ") ? (");
2006 p = print_qpolynomial_c(p, pwqp->dim, pwqp->p[i].qp);
2007 p = isl_printer_print_str(p, ") : ");
2010 p = isl_printer_print_str(p, "0");
2011 return p;
2014 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
2015 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2017 if (!p || !pwqp)
2018 goto error;
2020 if (p->output_format == ISL_FORMAT_ISL)
2021 return print_pw_qpolynomial_isl(p, pwqp);
2022 else if (p->output_format == ISL_FORMAT_C)
2023 return print_pw_qpolynomial_c(p, pwqp);
2024 isl_assert(p->ctx, 0, goto error);
2025 error:
2026 isl_printer_free(p);
2027 return NULL;
2030 static isl_stat print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
2032 struct isl_union_print_data *data;
2033 data = (struct isl_union_print_data *)user;
2035 if (!data->first)
2036 data->p = isl_printer_print_str(data->p, "; ");
2037 data->first = 0;
2039 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
2040 isl_pw_qpolynomial_free(pwqp);
2042 return isl_stat_ok;
2045 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
2046 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2048 struct isl_union_print_data data;
2049 struct isl_print_space_data space_data = { 0 };
2050 isl_space *dim;
2052 dim = isl_union_pw_qpolynomial_get_space(upwqp);
2053 if (isl_space_dim(dim, isl_dim_param) > 0) {
2054 p = print_tuple(dim, p, isl_dim_param, &space_data);
2055 p = isl_printer_print_str(p, " -> ");
2057 isl_space_free(dim);
2058 p = isl_printer_print_str(p, "{ ");
2059 data.p = p;
2060 data.first = 1;
2061 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
2062 &data);
2063 p = data.p;
2064 p = isl_printer_print_str(p, " }");
2065 return p;
2068 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
2069 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2071 if (!p || !upwqp)
2072 goto error;
2074 if (p->output_format == ISL_FORMAT_ISL)
2075 return print_union_pw_qpolynomial_isl(p, upwqp);
2076 isl_die(p->ctx, isl_error_invalid,
2077 "invalid output format for isl_union_pw_qpolynomial",
2078 goto error);
2079 error:
2080 isl_printer_free(p);
2081 return NULL;
2084 static __isl_give isl_printer *print_qpolynomial_fold_c(
2085 __isl_take isl_printer *p, __isl_keep isl_space *dim,
2086 __isl_keep isl_qpolynomial_fold *fold)
2088 int i;
2090 for (i = 0; i < fold->n - 1; ++i)
2091 if (fold->type == isl_fold_min)
2092 p = isl_printer_print_str(p, "min(");
2093 else if (fold->type == isl_fold_max)
2094 p = isl_printer_print_str(p, "max(");
2096 for (i = 0; i < fold->n; ++i) {
2097 if (i)
2098 p = isl_printer_print_str(p, ", ");
2099 p = print_qpolynomial_c(p, dim, fold->qp[i]);
2100 if (i)
2101 p = isl_printer_print_str(p, ")");
2103 return p;
2106 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
2107 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
2109 if (!p || !fold)
2110 goto error;
2111 if (p->output_format == ISL_FORMAT_ISL)
2112 return qpolynomial_fold_print(fold, p);
2113 else if (p->output_format == ISL_FORMAT_C)
2114 return print_qpolynomial_fold_c(p, fold->dim, fold);
2115 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2116 goto error);
2117 error:
2118 isl_printer_free(p);
2119 return NULL;
2122 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
2123 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2125 int i;
2127 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set))
2128 return print_qpolynomial_fold_c(p, pwf->dim, pwf->p[0].fold);
2130 for (i = 0; i < pwf->n; ++i) {
2131 p = isl_printer_print_str(p, "(");
2132 p = print_set_c(p, pwf->dim, pwf->p[i].set);
2133 p = isl_printer_print_str(p, ") ? (");
2134 p = print_qpolynomial_fold_c(p, pwf->dim, pwf->p[i].fold);
2135 p = isl_printer_print_str(p, ") : ");
2138 p = isl_printer_print_str(p, "0");
2139 return p;
2142 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
2143 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2145 if (!p || !pwf)
2146 goto error;
2148 if (p->output_format == ISL_FORMAT_ISL)
2149 return print_pw_qpolynomial_fold_isl(p, pwf);
2150 else if (p->output_format == ISL_FORMAT_C)
2151 return print_pw_qpolynomial_fold_c(p, pwf);
2152 isl_assert(p->ctx, 0, goto error);
2153 error:
2154 isl_printer_free(p);
2155 return NULL;
2158 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
2159 FILE *out, unsigned output_format)
2161 isl_printer *p;
2163 if (!pwf)
2164 return;
2166 p = isl_printer_to_file(pwf->dim->ctx, out);
2167 p = isl_printer_set_output_format(p, output_format);
2168 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
2170 isl_printer_free(p);
2173 static isl_stat print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf,
2174 void *user)
2176 struct isl_union_print_data *data;
2177 data = (struct isl_union_print_data *)user;
2179 if (!data->first)
2180 data->p = isl_printer_print_str(data->p, "; ");
2181 data->first = 0;
2183 data->p = isl_pwf_print_isl_body(data->p, pwf);
2184 isl_pw_qpolynomial_fold_free(pwf);
2186 return isl_stat_ok;
2189 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
2190 __isl_take isl_printer *p,
2191 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2193 struct isl_union_print_data data;
2194 struct isl_print_space_data space_data = { 0 };
2195 isl_space *dim;
2197 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
2198 if (isl_space_dim(dim, isl_dim_param) > 0) {
2199 p = print_tuple(dim, p, isl_dim_param, &space_data);
2200 p = isl_printer_print_str(p, " -> ");
2202 isl_space_free(dim);
2203 p = isl_printer_print_str(p, "{ ");
2204 data.p = p;
2205 data.first = 1;
2206 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
2207 &print_pwf_body, &data);
2208 p = data.p;
2209 p = isl_printer_print_str(p, " }");
2210 return p;
2213 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2214 __isl_take isl_printer *p,
2215 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2217 if (!p || !upwf)
2218 goto error;
2220 if (p->output_format == ISL_FORMAT_ISL)
2221 return print_union_pw_qpolynomial_fold_isl(p, upwf);
2222 isl_die(p->ctx, isl_error_invalid,
2223 "invalid output format for isl_union_pw_qpolynomial_fold",
2224 goto error);
2225 error:
2226 isl_printer_free(p);
2227 return NULL;
2230 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
2231 __isl_keep isl_constraint *c)
2233 isl_basic_map *bmap;
2235 if (!p || !c)
2236 goto error;
2238 bmap = isl_basic_map_from_constraint(isl_constraint_copy(c));
2239 p = isl_printer_print_basic_map(p, bmap);
2240 isl_basic_map_free(bmap);
2241 return p;
2242 error:
2243 isl_printer_free(p);
2244 return NULL;
2247 static __isl_give isl_printer *isl_printer_print_space_isl(
2248 __isl_take isl_printer *p, __isl_keep isl_space *space)
2250 struct isl_print_space_data data = { 0 };
2252 if (!space)
2253 goto error;
2255 if (isl_space_dim(space, isl_dim_param) > 0) {
2256 p = print_tuple(space, p, isl_dim_param, &data);
2257 p = isl_printer_print_str(p, " -> ");
2260 p = isl_printer_print_str(p, "{ ");
2261 if (isl_space_is_params(space))
2262 p = isl_printer_print_str(p, s_such_that[0]);
2263 else
2264 p = isl_print_space(space, p, 0, &data);
2265 p = isl_printer_print_str(p, " }");
2267 return p;
2268 error:
2269 isl_printer_free(p);
2270 return NULL;
2273 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
2274 __isl_keep isl_space *space)
2276 if (!p || !space)
2277 return isl_printer_free(p);
2278 if (p->output_format == ISL_FORMAT_ISL)
2279 return isl_printer_print_space_isl(p, space);
2280 else if (p->output_format == ISL_FORMAT_OMEGA)
2281 return print_omega_parameters(space, p);
2283 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
2284 "output format not supported for space",
2285 return isl_printer_free(p));
2288 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
2289 __isl_keep isl_local_space *ls)
2291 struct isl_print_space_data data = { 0 };
2292 unsigned n_div;
2294 if (!ls)
2295 goto error;
2297 if (isl_local_space_dim(ls, isl_dim_param) > 0) {
2298 p = print_tuple(ls->dim, p, isl_dim_param, &data);
2299 p = isl_printer_print_str(p, " -> ");
2301 p = isl_printer_print_str(p, "{ ");
2302 p = isl_print_space(ls->dim, p, 0, &data);
2303 n_div = isl_local_space_dim(ls, isl_dim_div);
2304 if (n_div > 0) {
2305 p = isl_printer_print_str(p, " : ");
2306 p = isl_printer_print_str(p, s_open_exists[0]);
2307 p = print_div_list(p, ls->dim, ls->div, 0, 1);
2308 p = isl_printer_print_str(p, s_close_exists[0]);
2309 } else if (isl_space_is_params(ls->dim))
2310 p = isl_printer_print_str(p, s_such_that[0]);
2311 p = isl_printer_print_str(p, " }");
2312 return p;
2313 error:
2314 isl_printer_free(p);
2315 return NULL;
2318 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2319 __isl_keep isl_aff *aff)
2321 unsigned total;
2323 if (isl_aff_is_nan(aff))
2324 return isl_printer_print_str(p, "NaN");
2326 total = isl_local_space_dim(aff->ls, isl_dim_all);
2327 p = isl_printer_print_str(p, "(");
2328 p = print_affine_of_len(aff->ls->dim, aff->ls->div, p,
2329 aff->v->el + 1, 1 + total);
2330 if (isl_int_is_one(aff->v->el[0]))
2331 p = isl_printer_print_str(p, ")");
2332 else {
2333 p = isl_printer_print_str(p, ")/");
2334 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2337 return p;
2340 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2341 __isl_keep isl_aff *aff)
2343 struct isl_print_space_data data = { 0 };
2345 if (isl_space_is_params(aff->ls->dim))
2347 else {
2348 p = print_tuple(aff->ls->dim, p, isl_dim_set, &data);
2349 p = isl_printer_print_str(p, " -> ");
2351 p = isl_printer_print_str(p, "[");
2352 p = print_aff_body(p, aff);
2353 p = isl_printer_print_str(p, "]");
2355 return p;
2358 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2359 __isl_keep isl_aff *aff)
2361 struct isl_print_space_data data = { 0 };
2363 if (!aff)
2364 goto error;
2366 if (isl_local_space_dim(aff->ls, isl_dim_param) > 0) {
2367 p = print_tuple(aff->ls->dim, p, isl_dim_param, &data);
2368 p = isl_printer_print_str(p, " -> ");
2370 p = isl_printer_print_str(p, "{ ");
2371 p = print_aff(p, aff);
2372 p = isl_printer_print_str(p, " }");
2373 return p;
2374 error:
2375 isl_printer_free(p);
2376 return NULL;
2379 /* Print the body of an isl_pw_aff, i.e., a semicolon delimited
2380 * sequence of affine expressions, each followed by constraints.
2382 static __isl_give isl_printer *print_pw_aff_body(
2383 __isl_take isl_printer *p, __isl_keep isl_pw_aff *pa)
2385 int i;
2387 if (!pa)
2388 return isl_printer_free(p);
2390 for (i = 0; i < pa->n; ++i) {
2391 isl_space *space;
2393 if (i)
2394 p = isl_printer_print_str(p, "; ");
2395 p = print_aff(p, pa->p[i].aff);
2396 space = isl_aff_get_domain_space(pa->p[i].aff);
2397 p = print_disjuncts((isl_map *)pa->p[i].set, space, p, 0);
2398 isl_space_free(space);
2401 return p;
2404 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2405 __isl_keep isl_pw_aff *pwaff)
2407 struct isl_print_space_data data = { 0 };
2409 if (!pwaff)
2410 goto error;
2412 if (isl_space_dim(pwaff->dim, isl_dim_param) > 0) {
2413 p = print_tuple(pwaff->dim, p, isl_dim_param, &data);
2414 p = isl_printer_print_str(p, " -> ");
2416 p = isl_printer_print_str(p, "{ ");
2417 p = print_pw_aff_body(p, pwaff);
2418 p = isl_printer_print_str(p, " }");
2419 return p;
2420 error:
2421 isl_printer_free(p);
2422 return NULL;
2425 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2426 __isl_keep isl_local_space *ls, isl_int *c);
2428 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2429 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2431 if (type == isl_dim_div) {
2432 p = isl_printer_print_str(p, "floord(");
2433 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2434 p = isl_printer_print_str(p, ", ");
2435 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2436 p = isl_printer_print_str(p, ")");
2437 } else {
2438 const char *name;
2440 name = isl_space_get_dim_name(ls->dim, type, pos);
2441 if (!name)
2442 name = "UNNAMED";
2443 p = isl_printer_print_str(p, name);
2445 return p;
2448 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2449 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2451 enum isl_dim_type type;
2453 if (pos == 0)
2454 return isl_printer_print_isl_int(p, c);
2456 if (isl_int_is_one(c))
2458 else if (isl_int_is_negone(c))
2459 p = isl_printer_print_str(p, "-");
2460 else {
2461 p = isl_printer_print_isl_int(p, c);
2462 p = isl_printer_print_str(p, "*");
2464 type = pos2type(ls->dim, &pos);
2465 p = print_ls_name_c(p, ls, type, pos);
2466 return p;
2469 static __isl_give isl_printer *print_ls_partial_affine_c(
2470 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2471 isl_int *c, unsigned len)
2473 int i;
2474 int first;
2476 for (i = 0, first = 1; i < len; ++i) {
2477 int flip = 0;
2478 if (isl_int_is_zero(c[i]))
2479 continue;
2480 if (!first) {
2481 if (isl_int_is_neg(c[i])) {
2482 flip = 1;
2483 isl_int_neg(c[i], c[i]);
2484 p = isl_printer_print_str(p, " - ");
2485 } else
2486 p = isl_printer_print_str(p, " + ");
2488 first = 0;
2489 p = print_ls_term_c(p, ls, c[i], i);
2490 if (flip)
2491 isl_int_neg(c[i], c[i]);
2493 if (first)
2494 p = isl_printer_print_str(p, "0");
2495 return p;
2498 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2499 __isl_keep isl_local_space *ls, isl_int *c)
2501 unsigned len = 1 + isl_local_space_dim(ls, isl_dim_all);
2502 return print_ls_partial_affine_c(p, ls, c, len);
2505 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2506 __isl_keep isl_aff *aff)
2508 unsigned total;
2510 total = isl_local_space_dim(aff->ls, isl_dim_all);
2511 if (!isl_int_is_one(aff->v->el[0]))
2512 p = isl_printer_print_str(p, "(");
2513 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2514 if (!isl_int_is_one(aff->v->el[0])) {
2515 p = isl_printer_print_str(p, ")/");
2516 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2518 return p;
2521 /* In the C format, we cannot express that "pwaff" may be undefined
2522 * on parts of the domain space. We therefore assume that the expression
2523 * will only be evaluated on its definition domain and compute the gist
2524 * of each cell with respect to this domain.
2526 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2527 __isl_keep isl_pw_aff *pwaff)
2529 isl_set *domain;
2530 isl_ast_build *build;
2531 isl_ast_expr *expr;
2533 if (pwaff->n < 1)
2534 isl_die(p->ctx, isl_error_unsupported,
2535 "cannot print empty isl_pw_aff in C format",
2536 return isl_printer_free(p));
2538 domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
2539 build = isl_ast_build_from_context(domain);
2540 expr = isl_ast_build_expr_from_pw_aff(build, isl_pw_aff_copy(pwaff));
2541 p = isl_printer_print_ast_expr(p, expr);
2542 isl_ast_expr_free(expr);
2543 isl_ast_build_free(build);
2545 return p;
2548 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2549 __isl_keep isl_aff *aff)
2551 if (!p || !aff)
2552 goto error;
2554 if (p->output_format == ISL_FORMAT_ISL)
2555 return print_aff_isl(p, aff);
2556 else if (p->output_format == ISL_FORMAT_C)
2557 return print_aff_c(p, aff);
2558 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2559 goto error);
2560 error:
2561 isl_printer_free(p);
2562 return NULL;
2565 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2566 __isl_keep isl_pw_aff *pwaff)
2568 if (!p || !pwaff)
2569 goto error;
2571 if (p->output_format == ISL_FORMAT_ISL)
2572 return print_pw_aff_isl(p, pwaff);
2573 else if (p->output_format == ISL_FORMAT_C)
2574 return print_pw_aff_c(p, pwaff);
2575 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2576 goto error);
2577 error:
2578 isl_printer_free(p);
2579 return NULL;
2582 /* Print "pa" in a sequence of isl_pw_affs delimited by semicolons.
2583 * Each isl_pw_aff itself is also printed as semicolon delimited
2584 * sequence of pieces.
2585 * If data->first = 1, then this is the first in the sequence.
2586 * Update data->first to tell the next element that it is not the first.
2588 static isl_stat print_pw_aff_body_wrap(__isl_take isl_pw_aff *pa,
2589 void *user)
2591 struct isl_union_print_data *data;
2592 data = (struct isl_union_print_data *) user;
2594 if (!data->first)
2595 data->p = isl_printer_print_str(data->p, "; ");
2596 data->first = 0;
2598 data->p = print_pw_aff_body(data->p, pa);
2599 isl_pw_aff_free(pa);
2601 return data->p ? isl_stat_ok : isl_stat_error;
2604 /* Print the body of an isl_union_pw_aff, i.e., a semicolon delimited
2605 * sequence of affine expressions, each followed by constraints,
2606 * with the sequence enclosed in braces.
2608 static __isl_give isl_printer *print_union_pw_aff_body(
2609 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2611 struct isl_union_print_data data = { p, 1 };
2613 p = isl_printer_print_str(p, s_open_set[0]);
2614 data.p = p;
2615 if (isl_union_pw_aff_foreach_pw_aff(upa,
2616 &print_pw_aff_body_wrap, &data) < 0)
2617 data.p = isl_printer_free(p);
2618 p = data.p;
2619 p = isl_printer_print_str(p, s_close_set[0]);
2621 return p;
2624 /* Print the isl_union_pw_aff "upa" to "p" in isl format.
2626 * The individual isl_pw_affs are delimited by a semicolon.
2628 static __isl_give isl_printer *print_union_pw_aff_isl(
2629 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2631 struct isl_print_space_data data = { 0 };
2632 isl_space *space;
2634 space = isl_union_pw_aff_get_space(upa);
2635 if (isl_space_dim(space, isl_dim_param) > 0) {
2636 p = print_tuple(space, p, isl_dim_param, &data);
2637 p = isl_printer_print_str(p, s_to[0]);
2639 isl_space_free(space);
2640 p = print_union_pw_aff_body(p, upa);
2641 return p;
2644 /* Print the isl_union_pw_aff "upa" to "p".
2646 * We currently only support an isl format.
2648 __isl_give isl_printer *isl_printer_print_union_pw_aff(
2649 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2651 if (!p || !upa)
2652 return isl_printer_free(p);
2654 if (p->output_format == ISL_FORMAT_ISL)
2655 return print_union_pw_aff_isl(p, upa);
2656 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2657 "unsupported output format", return isl_printer_free(p));
2660 /* Print dimension "pos" of data->space to "p".
2662 * data->user is assumed to be an isl_multi_aff.
2664 * If the current dimension is an output dimension, then print
2665 * the corresponding expression. Otherwise, print the name of the dimension.
2667 static __isl_give isl_printer *print_dim_ma(__isl_take isl_printer *p,
2668 struct isl_print_space_data *data, unsigned pos)
2670 isl_multi_aff *ma = data->user;
2672 if (data->type == isl_dim_out)
2673 p = print_aff_body(p, ma->p[pos]);
2674 else
2675 p = print_name(data->space, p, data->type, pos, data->latex);
2677 return p;
2680 static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
2681 __isl_keep isl_multi_aff *maff)
2683 struct isl_print_space_data data = { 0 };
2685 data.print_dim = &print_dim_ma;
2686 data.user = maff;
2687 return isl_print_space(maff->space, p, 0, &data);
2690 static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
2691 __isl_keep isl_multi_aff *maff)
2693 struct isl_print_space_data data = { 0 };
2695 if (!maff)
2696 goto error;
2698 if (isl_space_dim(maff->space, isl_dim_param) > 0) {
2699 p = print_tuple(maff->space, p, isl_dim_param, &data);
2700 p = isl_printer_print_str(p, " -> ");
2702 p = isl_printer_print_str(p, "{ ");
2703 p = print_multi_aff(p, maff);
2704 p = isl_printer_print_str(p, " }");
2705 return p;
2706 error:
2707 isl_printer_free(p);
2708 return NULL;
2711 __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
2712 __isl_keep isl_multi_aff *maff)
2714 if (!p || !maff)
2715 goto error;
2717 if (p->output_format == ISL_FORMAT_ISL)
2718 return print_multi_aff_isl(p, maff);
2719 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2720 goto error);
2721 error:
2722 isl_printer_free(p);
2723 return NULL;
2726 static __isl_give isl_printer *print_pw_multi_aff_body(
2727 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2729 int i;
2731 if (!pma)
2732 goto error;
2734 for (i = 0; i < pma->n; ++i) {
2735 isl_space *space;
2737 if (i)
2738 p = isl_printer_print_str(p, "; ");
2739 p = print_multi_aff(p, pma->p[i].maff);
2740 space = isl_multi_aff_get_domain_space(pma->p[i].maff);
2741 p = print_disjuncts((isl_map *)pma->p[i].set, space, p, 0);
2742 isl_space_free(space);
2744 return p;
2745 error:
2746 isl_printer_free(p);
2747 return NULL;
2750 static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
2751 __isl_keep isl_pw_multi_aff *pma)
2753 struct isl_print_space_data data = { 0 };
2755 if (!pma)
2756 goto error;
2758 if (isl_space_dim(pma->dim, isl_dim_param) > 0) {
2759 p = print_tuple(pma->dim, p, isl_dim_param, &data);
2760 p = isl_printer_print_str(p, " -> ");
2762 p = isl_printer_print_str(p, "{ ");
2763 p = print_pw_multi_aff_body(p, pma);
2764 p = isl_printer_print_str(p, " }");
2765 return p;
2766 error:
2767 isl_printer_free(p);
2768 return NULL;
2771 static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
2772 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2774 int i;
2776 for (i = 0; i < pma->n - 1; ++i) {
2777 p = isl_printer_print_str(p, "(");
2778 p = print_set_c(p, pma->dim, pma->p[i].set);
2779 p = isl_printer_print_str(p, ") ? (");
2780 p = print_aff_c(p, pma->p[i].maff->p[0]);
2781 p = isl_printer_print_str(p, ") : ");
2784 return print_aff_c(p, pma->p[pma->n - 1].maff->p[0]);
2787 static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
2788 __isl_keep isl_pw_multi_aff *pma)
2790 int n;
2791 const char *name;
2793 if (!pma)
2794 goto error;
2795 if (pma->n < 1)
2796 isl_die(p->ctx, isl_error_unsupported,
2797 "cannot print empty isl_pw_multi_aff in C format",
2798 goto error);
2799 name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
2800 if (!name && isl_pw_multi_aff_dim(pma, isl_dim_out) == 1)
2801 return print_unnamed_pw_multi_aff_c(p, pma);
2802 if (!name)
2803 isl_die(p->ctx, isl_error_unsupported,
2804 "cannot print unnamed isl_pw_multi_aff in C format",
2805 goto error);
2807 p = isl_printer_print_str(p, name);
2808 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
2809 if (n != 0)
2810 isl_die(p->ctx, isl_error_unsupported,
2811 "not supported yet", goto error);
2813 return p;
2814 error:
2815 isl_printer_free(p);
2816 return NULL;
2819 __isl_give isl_printer *isl_printer_print_pw_multi_aff(
2820 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2822 if (!p || !pma)
2823 goto error;
2825 if (p->output_format == ISL_FORMAT_ISL)
2826 return print_pw_multi_aff_isl(p, pma);
2827 if (p->output_format == ISL_FORMAT_C)
2828 return print_pw_multi_aff_c(p, pma);
2829 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2830 goto error);
2831 error:
2832 isl_printer_free(p);
2833 return NULL;
2836 static isl_stat print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
2837 void *user)
2839 struct isl_union_print_data *data;
2840 data = (struct isl_union_print_data *) user;
2842 if (!data->first)
2843 data->p = isl_printer_print_str(data->p, "; ");
2844 data->first = 0;
2846 data->p = print_pw_multi_aff_body(data->p, pma);
2847 isl_pw_multi_aff_free(pma);
2849 return isl_stat_ok;
2852 static __isl_give isl_printer *print_union_pw_multi_aff_isl(
2853 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2855 struct isl_union_print_data data;
2856 struct isl_print_space_data space_data = { 0 };
2857 isl_space *space;
2859 space = isl_union_pw_multi_aff_get_space(upma);
2860 if (isl_space_dim(space, isl_dim_param) > 0) {
2861 p = print_tuple(space, p, isl_dim_param, &space_data);
2862 p = isl_printer_print_str(p, s_to[0]);
2864 isl_space_free(space);
2865 p = isl_printer_print_str(p, s_open_set[0]);
2866 data.p = p;
2867 data.first = 1;
2868 isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
2869 &print_pw_multi_aff_body_wrap, &data);
2870 p = data.p;
2871 p = isl_printer_print_str(p, s_close_set[0]);
2872 return p;
2875 __isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
2876 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2878 if (!p || !upma)
2879 goto error;
2881 if (p->output_format == ISL_FORMAT_ISL)
2882 return print_union_pw_multi_aff_isl(p, upma);
2883 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2884 goto error);
2885 error:
2886 isl_printer_free(p);
2887 return NULL;
2890 /* Print dimension "pos" of data->space to "p".
2892 * data->user is assumed to be an isl_multi_pw_aff.
2894 * If the current dimension is an output dimension, then print
2895 * the corresponding piecewise affine expression.
2896 * Otherwise, print the name of the dimension.
2898 static __isl_give isl_printer *print_dim_mpa(__isl_take isl_printer *p,
2899 struct isl_print_space_data *data, unsigned pos)
2901 int i;
2902 int need_parens;
2903 isl_multi_pw_aff *mpa = data->user;
2904 isl_pw_aff *pa;
2906 if (data->type != isl_dim_out)
2907 return print_name(data->space, p, data->type, pos, data->latex);
2909 pa = mpa->p[pos];
2910 if (pa->n == 0)
2911 return isl_printer_print_str(p, "(0 : 1 = 0)");
2913 need_parens = pa->n != 1 || !isl_set_plain_is_universe(pa->p[0].set);
2914 if (need_parens)
2915 p = isl_printer_print_str(p, "(");
2916 for (i = 0; i < pa->n; ++i) {
2917 isl_space *space;
2919 if (i)
2920 p = isl_printer_print_str(p, "; ");
2921 p = print_aff_body(p, pa->p[i].aff);
2922 space = isl_aff_get_domain_space(pa->p[i].aff);
2923 p = print_disjuncts(pa->p[i].set, space, p, 0);
2924 isl_space_free(space);
2926 if (need_parens)
2927 p = isl_printer_print_str(p, ")");
2929 return p;
2932 /* Print "mpa" to "p" in isl format.
2934 static __isl_give isl_printer *print_multi_pw_aff_isl(__isl_take isl_printer *p,
2935 __isl_keep isl_multi_pw_aff *mpa)
2937 struct isl_print_space_data data = { 0 };
2939 if (!mpa)
2940 return isl_printer_free(p);
2942 if (isl_space_dim(mpa->space, isl_dim_param) > 0) {
2943 p = print_tuple(mpa->space, p, isl_dim_param, &data);
2944 p = isl_printer_print_str(p, " -> ");
2946 p = isl_printer_print_str(p, "{ ");
2947 data.print_dim = &print_dim_mpa;
2948 data.user = mpa;
2949 p = isl_print_space(mpa->space, p, 0, &data);
2950 p = isl_printer_print_str(p, " }");
2951 return p;
2954 __isl_give isl_printer *isl_printer_print_multi_pw_aff(
2955 __isl_take isl_printer *p, __isl_keep isl_multi_pw_aff *mpa)
2957 if (!p || !mpa)
2958 return isl_printer_free(p);
2960 if (p->output_format == ISL_FORMAT_ISL)
2961 return print_multi_pw_aff_isl(p, mpa);
2962 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2963 return isl_printer_free(p));
2966 /* Print dimension "pos" of data->space to "p".
2968 * data->user is assumed to be an isl_multi_val.
2970 * If the current dimension is an output dimension, then print
2971 * the corresponding value. Otherwise, print the name of the dimension.
2973 static __isl_give isl_printer *print_dim_mv(__isl_take isl_printer *p,
2974 struct isl_print_space_data *data, unsigned pos)
2976 isl_multi_val *mv = data->user;
2978 if (data->type == isl_dim_out)
2979 return isl_printer_print_val(p, mv->p[pos]);
2980 else
2981 return print_name(data->space, p, data->type, pos, data->latex);
2984 /* Print the isl_multi_val "mv" to "p" in isl format.
2986 static __isl_give isl_printer *print_multi_val_isl(__isl_take isl_printer *p,
2987 __isl_keep isl_multi_val *mv)
2989 struct isl_print_space_data data = { 0 };
2991 if (!mv)
2992 return isl_printer_free(p);
2994 if (isl_space_dim(mv->space, isl_dim_param) > 0) {
2995 p = print_tuple(mv->space, p, isl_dim_param, &data);
2996 p = isl_printer_print_str(p, " -> ");
2998 p = isl_printer_print_str(p, "{ ");
2999 data.print_dim = &print_dim_mv;
3000 data.user = mv;
3001 p = isl_print_space(mv->space, p, 0, &data);
3002 p = isl_printer_print_str(p, " }");
3003 return p;
3006 /* Print the isl_multi_val "mv" to "p".
3008 * Currently only supported in isl format.
3010 __isl_give isl_printer *isl_printer_print_multi_val(
3011 __isl_take isl_printer *p, __isl_keep isl_multi_val *mv)
3013 if (!p || !mv)
3014 return isl_printer_free(p);
3016 if (p->output_format == ISL_FORMAT_ISL)
3017 return print_multi_val_isl(p, mv);
3018 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3019 return isl_printer_free(p));
3022 /* Print dimension "pos" of data->space to "p".
3024 * data->user is assumed to be an isl_multi_union_pw_aff.
3026 * The current dimension is necessarily a set dimension, so
3027 * we print the corresponding isl_union_pw_aff, including
3028 * the braces.
3030 static __isl_give isl_printer *print_union_pw_aff_dim(__isl_take isl_printer *p,
3031 struct isl_print_space_data *data, unsigned pos)
3033 isl_multi_union_pw_aff *mupa = data->user;
3034 isl_union_pw_aff *upa;
3036 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, pos);
3037 p = print_union_pw_aff_body(p, upa);
3038 isl_union_pw_aff_free(upa);
3040 return p;
3043 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3045 static __isl_give isl_printer *print_multi_union_pw_aff_isl(
3046 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3048 struct isl_print_space_data data = { 0 };
3049 isl_space *space;
3051 space = isl_multi_union_pw_aff_get_space(mupa);
3052 if (isl_space_dim(space, isl_dim_param) > 0) {
3053 struct isl_print_space_data space_data = { 0 };
3054 p = print_tuple(space, p, isl_dim_param, &space_data);
3055 p = isl_printer_print_str(p, s_to[0]);
3058 data.print_dim = &print_union_pw_aff_dim;
3059 data.user = mupa;
3061 p = isl_print_space(space, p, 0, &data);
3062 isl_space_free(space);
3064 return p;
3067 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3069 * We currently only support an isl format.
3071 __isl_give isl_printer *isl_printer_print_multi_union_pw_aff(
3072 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3074 if (!p || !mupa)
3075 return isl_printer_free(p);
3077 if (p->output_format == ISL_FORMAT_ISL)
3078 return print_multi_union_pw_aff_isl(p, mupa);
3079 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
3080 "unsupported output format", return isl_printer_free(p));