isl_{union_},pw_*_eval: handle void input
[isl.git] / isl_output.c
blob8f8ee113bb4ece7e235b1e177dcf89f8409b7599
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 #include <bset_to_bmap.c>
36 #include <set_to_map.c>
38 static const char *s_to[2] = { " -> ", " \\to " };
39 static const char *s_and[2] = { " and ", " \\wedge " };
40 static const char *s_or[2] = { " or ", " \\vee " };
41 static const char *s_le[2] = { "<=", "\\le" };
42 static const char *s_ge[2] = { ">=", "\\ge" };
43 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
44 static const char *s_close_set[2] = { " }", " \\,\\}" };
45 static const char *s_open_list[2] = { "[", "(" };
46 static const char *s_close_list[2] = { "]", ")" };
47 static const char *s_such_that[2] = { " : ", " \\mid " };
48 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
49 static const char *s_close_exists[2] = { ")", "" };
50 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
51 static const char *s_param_prefix[2] = { "p", "p_" };
52 static const char *s_input_prefix[2] = { "i", "i_" };
53 static const char *s_output_prefix[2] = { "o", "o_" };
55 static __isl_give isl_printer *print_constraint_polylib(
56 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
58 int i;
59 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
60 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
61 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
62 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
64 p = isl_printer_start_line(p);
65 p = isl_printer_print_int(p, ineq);
66 for (i = 0; i < n_out; ++i) {
67 p = isl_printer_print_str(p, " ");
68 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
70 for (i = 0; i < n_in; ++i) {
71 p = isl_printer_print_str(p, " ");
72 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
74 for (i = 0; i < bmap->n_div; ++i) {
75 p = isl_printer_print_str(p, " ");
76 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
78 for (i = 0; i < nparam; ++i) {
79 p = isl_printer_print_str(p, " ");
80 p = isl_printer_print_isl_int(p, c[1+i]);
82 p = isl_printer_print_str(p, " ");
83 p = isl_printer_print_isl_int(p, c[0]);
84 p = isl_printer_end_line(p);
85 return p;
88 static __isl_give isl_printer *print_constraints_polylib(
89 struct isl_basic_map *bmap, __isl_take isl_printer *p)
91 int i;
93 p = isl_printer_set_isl_int_width(p, 5);
95 for (i = 0; i < bmap->n_eq; ++i)
96 p = print_constraint_polylib(bmap, 0, i, p);
97 for (i = 0; i < bmap->n_ineq; ++i)
98 p = print_constraint_polylib(bmap, 1, i, p);
100 return p;
103 static __isl_give isl_printer *bset_print_constraints_polylib(
104 struct isl_basic_set *bset, __isl_take isl_printer *p)
106 return print_constraints_polylib(bset_to_bmap(bset), p);
109 static __isl_give isl_printer *isl_basic_map_print_polylib(
110 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
112 unsigned total = isl_basic_map_total_dim(bmap);
113 p = isl_printer_start_line(p);
114 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
115 p = isl_printer_print_str(p, " ");
116 p = isl_printer_print_int(p, 1 + total + 1);
117 if (ext) {
118 p = isl_printer_print_str(p, " ");
119 p = isl_printer_print_int(p,
120 isl_basic_map_dim(bmap, isl_dim_out));
121 p = isl_printer_print_str(p, " ");
122 p = isl_printer_print_int(p,
123 isl_basic_map_dim(bmap, isl_dim_in));
124 p = isl_printer_print_str(p, " ");
125 p = isl_printer_print_int(p,
126 isl_basic_map_dim(bmap, isl_dim_div));
127 p = isl_printer_print_str(p, " ");
128 p = isl_printer_print_int(p,
129 isl_basic_map_dim(bmap, isl_dim_param));
131 p = isl_printer_end_line(p);
132 return print_constraints_polylib(bmap, p);
135 static __isl_give isl_printer *isl_basic_set_print_polylib(
136 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
138 return isl_basic_map_print_polylib(bset_to_bmap(bset), p, ext);
141 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
142 __isl_take isl_printer *p, int ext)
144 int i;
146 p = isl_printer_start_line(p);
147 p = isl_printer_print_int(p, map->n);
148 p = isl_printer_end_line(p);
149 for (i = 0; i < map->n; ++i) {
150 p = isl_printer_start_line(p);
151 p = isl_printer_end_line(p);
152 p = isl_basic_map_print_polylib(map->p[i], p, ext);
154 return p;
157 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
158 __isl_take isl_printer *p, int ext)
160 return isl_map_print_polylib(set_to_map(set), p, ext);
163 static int count_same_name(__isl_keep isl_space *dim,
164 enum isl_dim_type type, unsigned pos, const char *name)
166 enum isl_dim_type t;
167 unsigned p, s;
168 int count = 0;
170 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
171 s = t == type ? pos : isl_space_dim(dim, t);
172 for (p = 0; p < s; ++p) {
173 const char *n = isl_space_get_dim_name(dim, t, p);
174 if (n && !strcmp(n, name))
175 count++;
178 return count;
181 /* Print the name of the variable of type "type" and position "pos"
182 * in "space" to "p".
184 static __isl_give isl_printer *print_name(__isl_keep isl_space *space,
185 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
186 int latex)
188 const char *name;
189 char buffer[20];
190 int primes;
192 name = type == isl_dim_div ? NULL
193 : isl_space_get_dim_name(space, type, pos);
195 if (!name) {
196 const char *prefix;
197 if (type == isl_dim_param)
198 prefix = s_param_prefix[latex];
199 else if (type == isl_dim_div)
200 prefix = s_div_prefix[latex];
201 else if (isl_space_is_set(space) || type == isl_dim_in)
202 prefix = s_input_prefix[latex];
203 else
204 prefix = s_output_prefix[latex];
205 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
206 name = buffer;
208 primes = count_same_name(space, name == buffer ? isl_dim_div : type,
209 pos, name);
210 p = isl_printer_print_str(p, name);
211 while (primes-- > 0)
212 p = isl_printer_print_str(p, "'");
213 return p;
216 static enum isl_dim_type pos2type(__isl_keep isl_space *dim, unsigned *pos)
218 enum isl_dim_type type;
219 unsigned n_in = isl_space_dim(dim, isl_dim_in);
220 unsigned n_out = isl_space_dim(dim, isl_dim_out);
221 unsigned nparam = isl_space_dim(dim, isl_dim_param);
223 if (*pos < 1 + nparam) {
224 type = isl_dim_param;
225 *pos -= 1;
226 } else if (*pos < 1 + nparam + n_in) {
227 type = isl_dim_in;
228 *pos -= 1 + nparam;
229 } else if (*pos < 1 + nparam + n_in + n_out) {
230 type = isl_dim_out;
231 *pos -= 1 + nparam + n_in;
232 } else {
233 type = isl_dim_div;
234 *pos -= 1 + nparam + n_in + n_out;
237 return type;
240 /* Can the div expression of the integer division at position "row" of "div"
241 * be printed?
242 * In particular, are the div expressions available and does the selected
243 * variable have a known explicit representation?
244 * Furthermore, the Omega format does not allow any div expressions
245 * to be printed.
247 static isl_bool can_print_div_expr(__isl_keep isl_printer *p,
248 __isl_keep isl_mat *div, int pos)
250 if (p->output_format == ISL_FORMAT_OMEGA)
251 return isl_bool_false;
252 if (!div)
253 return isl_bool_false;
254 return !isl_int_is_zero(div->row[pos][0]);
257 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
258 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
260 static __isl_give isl_printer *print_term(__isl_keep isl_space *space,
261 __isl_keep isl_mat *div,
262 isl_int c, unsigned pos, __isl_take isl_printer *p, int latex)
264 enum isl_dim_type type;
265 int print_div_def;
267 if (pos == 0)
268 return isl_printer_print_isl_int(p, c);
270 type = pos2type(space, &pos);
271 print_div_def = type == isl_dim_div && can_print_div_expr(p, div, pos);
273 if (isl_int_is_one(c))
275 else if (isl_int_is_negone(c))
276 p = isl_printer_print_str(p, "-");
277 else {
278 p = isl_printer_print_isl_int(p, c);
279 if (p->output_format == ISL_FORMAT_C || print_div_def)
280 p = isl_printer_print_str(p, "*");
282 if (print_div_def)
283 p = print_div(space, div, pos, p);
284 else
285 p = print_name(space, p, type, pos, latex);
286 return p;
289 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_space *dim,
290 __isl_keep isl_mat *div,
291 __isl_take isl_printer *p, isl_int *c, int len)
293 int i;
294 int first;
296 for (i = 0, first = 1; i < len; ++i) {
297 int flip = 0;
298 if (isl_int_is_zero(c[i]))
299 continue;
300 if (!first) {
301 if (isl_int_is_neg(c[i])) {
302 flip = 1;
303 isl_int_neg(c[i], c[i]);
304 p = isl_printer_print_str(p, " - ");
305 } else
306 p = isl_printer_print_str(p, " + ");
308 first = 0;
309 p = print_term(dim, div, c[i], i, p, 0);
310 if (flip)
311 isl_int_neg(c[i], c[i]);
313 if (first)
314 p = isl_printer_print_str(p, "0");
315 return p;
318 /* Print an affine expression "c" corresponding to a constraint in "bmap"
319 * to "p", with the variable names taken from "space" and
320 * the integer division definitions taken from "div".
322 static __isl_give isl_printer *print_affine(__isl_keep isl_basic_map *bmap,
323 __isl_keep isl_space *space, __isl_keep isl_mat *div,
324 __isl_take isl_printer *p, isl_int *c)
326 unsigned len = 1 + isl_basic_map_total_dim(bmap);
327 return print_affine_of_len(space, div, p, c, len);
330 /* offset is the offset of local_dim inside data->type of data->space.
332 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
333 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
334 struct isl_print_space_data *data, int offset)
336 int i;
338 if (data->space != local_dim && local_type == isl_dim_out)
339 offset += local_dim->n_in;
341 for (i = 0; i < isl_space_dim(local_dim, local_type); ++i) {
342 if (i)
343 p = isl_printer_print_str(p, ", ");
344 if (data->print_dim)
345 p = data->print_dim(p, data, offset + i);
346 else
347 p = print_name(data->space, p, data->type, offset + i,
348 data->latex);
350 return p;
353 static __isl_give isl_printer *print_var_list(__isl_take isl_printer *p,
354 __isl_keep isl_space *space, enum isl_dim_type type)
356 struct isl_print_space_data data = { .space = space, .type = type };
358 return print_nested_var_list(p, space, type, &data, 0);
361 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
362 __isl_keep isl_space *local_dim,
363 struct isl_print_space_data *data, int offset);
365 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
366 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
367 struct isl_print_space_data *data, int offset)
369 const char *name = NULL;
370 unsigned n = isl_space_dim(local_dim, local_type);
371 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
372 name = isl_space_get_tuple_name(local_dim, local_type);
373 if (name) {
374 if (data->latex)
375 p = isl_printer_print_str(p, "\\mathrm{");
376 p = isl_printer_print_str(p, name);
377 if (data->latex)
378 p = isl_printer_print_str(p, "}");
381 if (!data->latex || n != 1 || name)
382 p = isl_printer_print_str(p, s_open_list[data->latex]);
383 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
384 local_dim->nested[local_type - isl_dim_in]) {
385 if (data->space != local_dim && local_type == isl_dim_out)
386 offset += local_dim->n_in;
387 p = print_nested_map_dim(p,
388 local_dim->nested[local_type - isl_dim_in],
389 data, offset);
390 } else
391 p = print_nested_var_list(p, local_dim, local_type, data,
392 offset);
393 if (!data->latex || n != 1 || name)
394 p = isl_printer_print_str(p, s_close_list[data->latex]);
395 return p;
398 static __isl_give isl_printer *print_tuple(__isl_keep isl_space *dim,
399 __isl_take isl_printer *p, enum isl_dim_type type,
400 struct isl_print_space_data *data)
402 data->space = dim;
403 data->type = type;
404 return print_nested_tuple(p, dim, type, data, 0);
407 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
408 __isl_keep isl_space *local_dim,
409 struct isl_print_space_data *data, int offset)
411 p = print_nested_tuple(p, local_dim, isl_dim_in, data, offset);
412 p = isl_printer_print_str(p, s_to[data->latex]);
413 p = print_nested_tuple(p, local_dim, isl_dim_out, data, offset);
415 return p;
418 __isl_give isl_printer *isl_print_space(__isl_keep isl_space *space,
419 __isl_take isl_printer *p, int rational,
420 struct isl_print_space_data *data)
422 if (rational && !data->latex)
423 p = isl_printer_print_str(p, "rat: ");
424 if (isl_space_is_params(space))
426 else if (isl_space_is_set(space))
427 p = print_tuple(space, p, isl_dim_set, data);
428 else {
429 p = print_tuple(space, p, isl_dim_in, data);
430 p = isl_printer_print_str(p, s_to[data->latex]);
431 p = print_tuple(space, p, isl_dim_out, data);
434 return p;
437 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_space *dim,
438 __isl_take isl_printer *p)
440 if (isl_space_dim(dim, isl_dim_param) == 0)
441 return p;
443 p = isl_printer_start_line(p);
444 p = isl_printer_print_str(p, "symbolic ");
445 p = print_var_list(p, dim, isl_dim_param);
446 p = isl_printer_print_str(p, ";");
447 p = isl_printer_end_line(p);
448 return p;
451 /* Does the inequality constraint following "i" in "bmap"
452 * have an opposite value for the same last coefficient?
453 * "last" is the position of the last coefficient of inequality "i".
454 * If the next constraint is a div constraint, then it is ignored
455 * since div constraints are not printed.
457 static int next_is_opposite(__isl_keep isl_basic_map *bmap, int i, int last)
459 unsigned total = isl_basic_map_total_dim(bmap);
460 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
462 if (i + 1 >= bmap->n_ineq)
463 return 0;
464 if (isl_seq_last_non_zero(bmap->ineq[i + 1], 1 + total) != last)
465 return 0;
466 if (last >= o_div &&
467 isl_basic_map_is_div_constraint(bmap, bmap->ineq[i + 1],
468 last - o_div))
469 return 0;
470 return isl_int_abs_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]) &&
471 !isl_int_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]);
474 /* Return a string representation of the operator used when
475 * printing a constraint where the LHS is greater than or equal to the LHS
476 * (sign > 0) or smaller than or equal to the LHS (sign < 0).
477 * If "strict" is set, then return the strict version of the comparison
478 * operator.
480 static const char *constraint_op(int sign, int strict, int latex)
482 if (strict)
483 return sign < 0 ? "<" : ">";
484 if (sign < 0)
485 return s_le[latex];
486 else
487 return s_ge[latex];
490 /* Print one side of a constraint "c" from "bmap" to "p", with
491 * the variable names taken from "space" and the integer division definitions
492 * taken from "div".
493 * "last" is the position of the last non-zero coefficient.
494 * Let c' be the result of zeroing out this coefficient, then
495 * the partial constraint
497 * c' op
499 * is printed.
500 * "first_constraint" is set if this is the first constraint
501 * in the conjunction.
503 static __isl_give isl_printer *print_half_constraint(struct isl_basic_map *bmap,
504 __isl_keep isl_space *space, __isl_keep isl_mat *div,
505 __isl_take isl_printer *p, isl_int *c, int last, const char *op,
506 int first_constraint, int latex)
508 if (!first_constraint)
509 p = isl_printer_print_str(p, s_and[latex]);
511 isl_int_set_si(c[last], 0);
512 p = print_affine(bmap, space, div, p, c);
514 p = isl_printer_print_str(p, " ");
515 p = isl_printer_print_str(p, op);
516 p = isl_printer_print_str(p, " ");
518 return p;
521 /* Print a constraint "c" from "bmap" to "p", with the variable names
522 * taken from "space" and the integer division definitions taken from "div".
523 * "last" is the position of the last non-zero coefficient, which is
524 * moreover assumed to be negative.
525 * Let c' be the result of zeroing out this coefficient, then
526 * the constraint is printed in the form
528 * -c[last] op c'
530 * "first_constraint" is set if this is the first constraint
531 * in the conjunction.
533 static __isl_give isl_printer *print_constraint(struct isl_basic_map *bmap,
534 __isl_keep isl_space *space, __isl_keep isl_mat *div,
535 __isl_take isl_printer *p,
536 isl_int *c, int last, const char *op, int first_constraint, int latex)
538 if (!first_constraint)
539 p = isl_printer_print_str(p, s_and[latex]);
541 isl_int_abs(c[last], c[last]);
543 p = print_term(space, div, c[last], last, p, latex);
545 p = isl_printer_print_str(p, " ");
546 p = isl_printer_print_str(p, op);
547 p = isl_printer_print_str(p, " ");
549 isl_int_set_si(c[last], 0);
550 p = print_affine(bmap, space, div, p, c);
552 return p;
555 /* Print the constraints of "bmap" to "p".
556 * The names of the variables are taken from "space" and
557 * the integer division definitions are taken from "div".
558 * Div constraints are only printed in "dump" mode.
559 * The constraints are sorted prior to printing (except in "dump" mode).
561 * If x is the last variable with a non-zero coefficient,
562 * then a lower bound
564 * f - a x >= 0
566 * is printed as
568 * a x <= f
570 * while an upper bound
572 * f + a x >= 0
574 * is printed as
576 * a x >= -f
578 * If the next constraint has an opposite sign for the same last coefficient,
579 * then it is printed as
581 * f >= a x
583 * or
585 * -f <= a x
587 * instead. In fact, the "a x" part is not printed explicitly, but
588 * reused from the next constraint, which is therefore treated as
589 * a first constraint in the conjunction.
591 * If the constant term of "f" is -1, then "f" is replaced by "f + 1" and
592 * the comparison operator is replaced by the strict variant.
593 * Essentially, ">= 1" is replaced by "> 0".
595 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
596 __isl_keep isl_space *space, __isl_keep isl_mat *div,
597 __isl_take isl_printer *p, int latex)
599 int i;
600 isl_vec *c = NULL;
601 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
602 unsigned total = isl_basic_map_total_dim(bmap);
603 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
604 int first = 1;
605 int dump;
607 if (!p)
608 return NULL;
609 bmap = isl_basic_map_copy(bmap);
610 dump = p->dump;
611 if (!dump)
612 bmap = isl_basic_map_sort_constraints(bmap);
613 if (!bmap)
614 goto error;
616 c = isl_vec_alloc(bmap->ctx, 1 + total);
617 if (!c)
618 goto error;
620 for (i = bmap->n_eq - 1; i >= 0; --i) {
621 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
622 if (l < 0) {
623 if (i != bmap->n_eq - 1)
624 p = isl_printer_print_str(p, s_and[latex]);
625 p = isl_printer_print_str(p, "0 = 0");
626 continue;
628 if (isl_int_is_neg(bmap->eq[i][l]))
629 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
630 else
631 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
632 p = print_constraint(bmap, space, div, p, c->el, l,
633 "=", first, latex);
634 first = 0;
636 for (i = 0; i < bmap->n_ineq; ++i) {
637 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
638 int strict;
639 int s;
640 const char *op;
641 if (l < 0)
642 continue;
643 if (!dump && l >= o_div &&
644 can_print_div_expr(p, div, l - o_div) &&
645 isl_basic_map_is_div_constraint(bmap, bmap->ineq[i],
646 l - o_div))
647 continue;
648 s = isl_int_sgn(bmap->ineq[i][l]);
649 strict = !rational && isl_int_is_negone(bmap->ineq[i][0]);
650 if (s < 0)
651 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
652 else
653 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
654 if (strict)
655 isl_int_set_si(c->el[0], 0);
656 if (!dump && next_is_opposite(bmap, i, l)) {
657 op = constraint_op(-s, strict, latex);
658 p = print_half_constraint(bmap, space, div, p, c->el, l,
659 op, first, latex);
660 first = 1;
661 } else {
662 op = constraint_op(s, strict, latex);
663 p = print_constraint(bmap, space, div, p, c->el, l,
664 op, first, latex);
665 first = 0;
669 isl_basic_map_free(bmap);
670 isl_vec_free(c);
672 return p;
673 error:
674 isl_basic_map_free(bmap);
675 isl_vec_free(c);
676 isl_printer_free(p);
677 return NULL;
680 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
681 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
683 int c;
685 if (!p || !div)
686 return isl_printer_free(p);
688 c = p->output_format == ISL_FORMAT_C;
689 p = isl_printer_print_str(p, c ? "floord(" : "floor((");
690 p = print_affine_of_len(dim, div, p,
691 div->row[pos] + 1, div->n_col - 1);
692 p = isl_printer_print_str(p, c ? ", " : ")/");
693 p = isl_printer_print_isl_int(p, div->row[pos][0]);
694 p = isl_printer_print_str(p, ")");
695 return p;
698 /* Print a comma separated list of div names, except those that have
699 * a definition that can be printed.
700 * If "print_defined_divs" is set, then those div names are printed
701 * as well, along with their definitions.
703 static __isl_give isl_printer *print_div_list(__isl_take isl_printer *p,
704 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex,
705 int print_defined_divs)
707 int i;
708 int first = 1;
709 unsigned n_div;
711 if (!p || !space || !div)
712 return isl_printer_free(p);
714 n_div = isl_mat_rows(div);
716 for (i = 0; i < n_div; ++i) {
717 if (!print_defined_divs && can_print_div_expr(p, div, i))
718 continue;
719 if (!first)
720 p = isl_printer_print_str(p, ", ");
721 p = print_name(space, p, isl_dim_div, i, latex);
722 first = 0;
723 if (!can_print_div_expr(p, div, i))
724 continue;
725 p = isl_printer_print_str(p, " = ");
726 p = print_div(space, div, i, p);
729 return p;
732 /* Does printing "bmap" require an "exists" clause?
733 * That is, are there any local variables without an explicit representation?
735 static isl_bool need_exists(__isl_keep isl_printer *p,
736 __isl_keep isl_basic_map *bmap, __isl_keep isl_mat *div)
738 int i;
740 if (!p || !bmap)
741 return isl_bool_error;
742 if (bmap->n_div == 0)
743 return isl_bool_false;
744 for (i = 0; i < bmap->n_div; ++i)
745 if (!can_print_div_expr(p, div, i))
746 return isl_bool_true;
747 return isl_bool_false;
750 /* Print the constraints of "bmap" to "p".
751 * The names of the variables are taken from "space".
752 * "latex" is set if the constraints should be printed in LaTeX format.
753 * Do not print inline explicit div representations in "dump" mode.
755 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
756 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
758 int dump;
759 isl_mat *div;
760 isl_bool exists;
762 if (!p)
763 return NULL;
764 dump = p->dump;
765 div = isl_basic_map_get_divs(bmap);
766 if (dump)
767 exists = bmap->n_div > 0;
768 else
769 exists = need_exists(p, bmap, div);
770 if (exists >= 0 && exists) {
771 p = isl_printer_print_str(p, s_open_exists[latex]);
772 p = print_div_list(p, space, div, latex, dump);
773 p = isl_printer_print_str(p, ": ");
776 if (dump)
777 div = isl_mat_free(div);
778 p = print_constraints(bmap, space, div, p, latex);
779 isl_mat_free(div);
781 if (exists >= 0 && exists)
782 p = isl_printer_print_str(p, s_close_exists[latex]);
783 return p;
786 /* Print a colon followed by the constraints of "bmap"
787 * to "p", provided there are any constraints.
788 * The names of the variables are taken from "space".
789 * "latex" is set if the constraints should be printed in LaTeX format.
791 static __isl_give isl_printer *print_optional_disjunct(
792 __isl_keep isl_basic_map *bmap, __isl_keep isl_space *space,
793 __isl_take isl_printer *p, int latex)
795 if (isl_basic_map_plain_is_universe(bmap))
796 return p;
798 p = isl_printer_print_str(p, ": ");
799 p = print_disjunct(bmap, space, p, latex);
801 return p;
804 static __isl_give isl_printer *basic_map_print_omega(
805 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
807 p = isl_printer_print_str(p, "{ [");
808 p = print_var_list(p, bmap->dim, isl_dim_in);
809 p = isl_printer_print_str(p, "] -> [");
810 p = print_var_list(p, bmap->dim, isl_dim_out);
811 p = isl_printer_print_str(p, "] ");
812 p = print_optional_disjunct(bmap, bmap->dim, p, 0);
813 p = isl_printer_print_str(p, " }");
814 return p;
817 static __isl_give isl_printer *basic_set_print_omega(
818 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
820 p = isl_printer_print_str(p, "{ [");
821 p = print_var_list(p, bset->dim, isl_dim_set);
822 p = isl_printer_print_str(p, "] ");
823 p = print_optional_disjunct(bset, bset->dim, p, 0);
824 p = isl_printer_print_str(p, " }");
825 return p;
828 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
829 __isl_take isl_printer *p)
831 int i;
833 for (i = 0; i < map->n; ++i) {
834 if (i)
835 p = isl_printer_print_str(p, " union ");
836 p = basic_map_print_omega(map->p[i], p);
838 return p;
841 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
842 __isl_take isl_printer *p)
844 int i;
846 for (i = 0; i < set->n; ++i) {
847 if (i)
848 p = isl_printer_print_str(p, " union ");
849 p = basic_set_print_omega(set->p[i], p);
851 return p;
854 static __isl_give isl_printer *isl_basic_map_print_isl(
855 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
856 int latex)
858 struct isl_print_space_data data = { .latex = latex };
859 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
861 if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
862 p = print_tuple(bmap->dim, p, isl_dim_param, &data);
863 p = isl_printer_print_str(p, " -> ");
865 p = isl_printer_print_str(p, "{ ");
866 p = isl_print_space(bmap->dim, p, rational, &data);
867 p = isl_printer_print_str(p, " : ");
868 p = print_disjunct(bmap, bmap->dim, p, latex);
869 p = isl_printer_print_str(p, " }");
870 return p;
873 /* Print the disjuncts of a map (or set) "map" to "p".
874 * The names of the variables are taken from "space".
875 * "latex" is set if the constraints should be printed in LaTeX format.
877 static __isl_give isl_printer *print_disjuncts_core(__isl_keep isl_map *map,
878 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
880 int i;
882 if (map->n == 0)
883 p = isl_printer_print_str(p, "1 = 0");
884 for (i = 0; i < map->n; ++i) {
885 if (i)
886 p = isl_printer_print_str(p, s_or[latex]);
887 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
888 p = isl_printer_print_str(p, "(");
889 p = print_disjunct(map->p[i], space, p, latex);
890 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
891 p = isl_printer_print_str(p, ")");
893 return p;
896 /* Print the disjuncts of a map (or set) "map" to "p".
897 * The names of the variables are taken from "space".
898 * "hull" describes constraints shared by all disjuncts of "map".
899 * "latex" is set if the constraints should be printed in LaTeX format.
901 * Print the disjuncts as a conjunction of "hull" and
902 * the result of removing the constraints of "hull" from "map".
903 * If this result turns out to be the universe, then simply print "hull".
905 static __isl_give isl_printer *print_disjuncts_in_hull(__isl_keep isl_map *map,
906 __isl_keep isl_space *space, __isl_take isl_basic_map *hull,
907 __isl_take isl_printer *p, int latex)
909 isl_bool is_universe;
911 p = print_disjunct(hull, space, p, latex);
912 map = isl_map_plain_gist_basic_map(isl_map_copy(map), hull);
913 is_universe = isl_map_plain_is_universe(map);
914 if (is_universe < 0)
915 goto error;
916 if (!is_universe) {
917 p = isl_printer_print_str(p, s_and[latex]);
918 p = isl_printer_print_str(p, "(");
919 p = print_disjuncts_core(map, space, p, latex);
920 p = isl_printer_print_str(p, ")");
922 isl_map_free(map);
924 return p;
925 error:
926 isl_map_free(map);
927 isl_printer_free(p);
928 return NULL;
931 /* Print the disjuncts of a map (or set) "map" to "p".
932 * The names of the variables are taken from "space".
933 * "latex" is set if the constraints should be printed in LaTeX format.
935 * If there are at least two disjuncts and "dump" mode is not turned out,
936 * check for any shared constraints among all disjuncts.
937 * If there are any, then print them separately in print_disjuncts_in_hull.
939 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
940 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
942 if (isl_map_plain_is_universe(map))
943 return p;
945 p = isl_printer_print_str(p, s_such_that[latex]);
946 if (!p)
947 return NULL;
949 if (!p->dump && map->n >= 2) {
950 isl_basic_map *hull;
951 isl_bool is_universe;
953 hull = isl_map_plain_unshifted_simple_hull(isl_map_copy(map));
954 is_universe = isl_basic_map_plain_is_universe(hull);
955 if (is_universe < 0)
956 p = isl_printer_free(p);
957 else if (!is_universe)
958 return print_disjuncts_in_hull(map, space, hull,
959 p, latex);
960 isl_basic_map_free(hull);
963 return print_disjuncts_core(map, space, p, latex);
966 /* Print the disjuncts of a map (or set).
967 * The names of the variables are taken from "space".
968 * "latex" is set if the constraints should be printed in LaTeX format.
970 * If the map turns out to be a universal parameter domain, then
971 * we need to print the colon. Otherwise, the output looks identical
972 * to the empty set.
974 static __isl_give isl_printer *print_disjuncts_map(__isl_keep isl_map *map,
975 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
977 if (isl_map_plain_is_universe(map) && isl_space_is_params(map->dim))
978 return isl_printer_print_str(p, s_such_that[latex]);
979 else
980 return print_disjuncts(map, space, p, latex);
983 struct isl_aff_split {
984 isl_basic_map *aff;
985 isl_map *map;
988 static void free_split(__isl_take struct isl_aff_split *split, int n)
990 int i;
992 if (!split)
993 return;
995 for (i = 0; i < n; ++i) {
996 isl_basic_map_free(split[i].aff);
997 isl_map_free(split[i].map);
1000 free(split);
1003 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
1005 int i, j;
1006 unsigned nparam, n_in, n_out, total;
1008 bmap = isl_basic_map_cow(bmap);
1009 if (!bmap)
1010 return NULL;
1011 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
1012 goto error;
1014 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1015 n_in = isl_basic_map_dim(bmap, isl_dim_in);
1016 n_out = isl_basic_map_dim(bmap, isl_dim_out);
1017 total = isl_basic_map_dim(bmap, isl_dim_all);
1018 for (i = bmap->n_eq - 1; i >= 0; --i) {
1019 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
1020 if (j >= nparam && j < nparam + n_in + n_out &&
1021 (isl_int_is_one(bmap->eq[i][1 + j]) ||
1022 isl_int_is_negone(bmap->eq[i][1 + j])))
1023 continue;
1024 if (isl_basic_map_drop_equality(bmap, i) < 0)
1025 goto error;
1028 bmap = isl_basic_map_finalize(bmap);
1030 return bmap;
1031 error:
1032 isl_basic_map_free(bmap);
1033 return NULL;
1036 static int aff_split_cmp(const void *p1, const void *p2, void *user)
1038 const struct isl_aff_split *s1, *s2;
1039 s1 = (const struct isl_aff_split *) p1;
1040 s2 = (const struct isl_aff_split *) p2;
1042 return isl_basic_map_plain_cmp(s1->aff, s2->aff);
1045 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
1046 __isl_keep isl_basic_map *aff)
1048 int i, j;
1049 unsigned total;
1051 if (!bmap || !aff)
1052 goto error;
1054 total = isl_space_dim(bmap->dim, isl_dim_all);
1056 for (i = bmap->n_eq - 1; i >= 0; --i) {
1057 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
1058 bmap->n_div) != -1)
1059 continue;
1060 for (j = 0; j < aff->n_eq; ++j) {
1061 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
1062 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
1063 continue;
1064 if (isl_basic_map_drop_equality(bmap, i) < 0)
1065 goto error;
1066 break;
1070 return bmap;
1071 error:
1072 isl_basic_map_free(bmap);
1073 return NULL;
1076 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
1078 int i, n;
1079 struct isl_aff_split *split;
1080 isl_ctx *ctx;
1082 ctx = isl_map_get_ctx(map);
1083 split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
1084 if (!split)
1085 return NULL;
1087 for (i = 0; i < map->n; ++i) {
1088 isl_basic_map *bmap;
1089 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
1090 bmap = isl_basic_map_copy(map->p[i]);
1091 bmap = isl_basic_map_cow(bmap);
1092 bmap = drop_aff(bmap, split[i].aff);
1093 split[i].map = isl_map_from_basic_map(bmap);
1094 if (!split[i].aff || !split[i].map)
1095 goto error;
1098 if (isl_sort(split, map->n, sizeof(struct isl_aff_split),
1099 &aff_split_cmp, NULL) < 0)
1100 goto error;
1102 n = map->n;
1103 for (i = n - 1; i >= 1; --i) {
1104 if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
1105 split[i].aff))
1106 continue;
1107 isl_basic_map_free(split[i].aff);
1108 split[i - 1].map = isl_map_union(split[i - 1].map,
1109 split[i].map);
1110 if (i != n - 1)
1111 split[i] = split[n - 1];
1112 split[n - 1].aff = NULL;
1113 split[n - 1].map = NULL;
1114 --n;
1117 return split;
1118 error:
1119 free_split(split, map->n);
1120 return NULL;
1123 static int defining_equality(__isl_keep isl_basic_map *eq,
1124 __isl_keep isl_space *dim, enum isl_dim_type type, int pos)
1126 int i;
1127 unsigned total;
1129 if (!eq)
1130 return -1;
1132 pos += isl_space_offset(dim, type);
1133 total = isl_basic_map_total_dim(eq);
1135 for (i = 0; i < eq->n_eq; ++i) {
1136 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
1137 continue;
1138 if (isl_int_is_one(eq->eq[i][1 + pos]))
1139 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
1140 return i;
1143 return -1;
1146 /* Print dimension "pos" of data->space to "p".
1148 * data->user is assumed to be an isl_basic_map keeping track of equalities.
1150 * If the current dimension is defined by these equalities, then print
1151 * the corresponding expression, assigned to the name of the dimension
1152 * if there is any. Otherwise, print the name of the dimension.
1154 static __isl_give isl_printer *print_dim_eq(__isl_take isl_printer *p,
1155 struct isl_print_space_data *data, unsigned pos)
1157 isl_basic_map *eq = data->user;
1158 int j;
1160 j = defining_equality(eq, data->space, data->type, pos);
1161 if (j >= 0) {
1162 if (isl_space_has_dim_name(data->space, data->type, pos)) {
1163 p = print_name(data->space, p, data->type, pos,
1164 data->latex);
1165 p = isl_printer_print_str(p, " = ");
1167 pos += 1 + isl_space_offset(data->space, data->type);
1168 p = print_affine_of_len(data->space, NULL, p, eq->eq[j], pos);
1169 } else {
1170 p = print_name(data->space, p, data->type, pos, data->latex);
1173 return p;
1176 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
1177 struct isl_aff_split *split, int n, __isl_keep isl_space *space)
1179 struct isl_print_space_data data = { 0 };
1180 int i;
1181 int rational;
1183 data.print_dim = &print_dim_eq;
1184 for (i = 0; i < n; ++i) {
1185 if (!split[i].map)
1186 break;
1187 rational = split[i].map->n > 0 &&
1188 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
1189 if (i)
1190 p = isl_printer_print_str(p, "; ");
1191 data.user = split[i].aff;
1192 p = isl_print_space(space, p, rational, &data);
1193 p = print_disjuncts_map(split[i].map, space, p, 0);
1196 return p;
1199 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
1200 __isl_take isl_printer *p)
1202 struct isl_print_space_data data = { 0 };
1203 struct isl_aff_split *split = NULL;
1204 int rational;
1206 if (!p || !map)
1207 return isl_printer_free(p);
1208 if (!p->dump && map->n > 0)
1209 split = split_aff(map);
1210 if (split) {
1211 p = print_split_map(p, split, map->n, map->dim);
1212 } else {
1213 rational = map->n > 0 &&
1214 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
1215 p = isl_print_space(map->dim, p, rational, &data);
1216 p = print_disjuncts_map(map, map->dim, p, 0);
1218 free_split(split, map->n);
1219 return p;
1222 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
1223 __isl_take isl_printer *p)
1225 struct isl_print_space_data data = { 0 };
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[0]);
1231 p = isl_printer_print_str(p, s_open_set[0]);
1232 p = isl_map_print_isl_body(map, p);
1233 p = isl_printer_print_str(p, s_close_set[0]);
1234 return p;
1237 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
1238 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff)
1240 struct isl_print_space_data data = { 0 };
1242 data.latex = 1;
1243 if (isl_map_dim(map, isl_dim_param) > 0) {
1244 p = print_tuple(map->dim, p, isl_dim_param, &data);
1245 p = isl_printer_print_str(p, s_to[1]);
1247 p = isl_printer_print_str(p, s_open_set[1]);
1248 data.print_dim = &print_dim_eq;
1249 data.user = aff;
1250 p = isl_print_space(map->dim, p, 0, &data);
1251 p = print_disjuncts_map(map, map->dim, p, 1);
1252 p = isl_printer_print_str(p, s_close_set[1]);
1254 return p;
1257 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
1258 __isl_take isl_printer *p)
1260 int i;
1261 struct isl_aff_split *split = NULL;
1263 if (map->n > 0)
1264 split = split_aff(map);
1266 if (!split)
1267 return print_latex_map(map, p, NULL);
1269 for (i = 0; i < map->n; ++i) {
1270 if (!split[i].map)
1271 break;
1272 if (i)
1273 p = isl_printer_print_str(p, " \\cup ");
1274 p = print_latex_map(split[i].map, p, split[i].aff);
1277 free_split(split, map->n);
1278 return p;
1281 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
1282 __isl_keep isl_basic_map *bmap)
1284 if (!p || !bmap)
1285 goto error;
1286 if (p->output_format == ISL_FORMAT_ISL)
1287 return isl_basic_map_print_isl(bmap, p, 0);
1288 else if (p->output_format == ISL_FORMAT_OMEGA)
1289 return basic_map_print_omega(bmap, p);
1290 isl_assert(bmap->ctx, 0, goto error);
1291 error:
1292 isl_printer_free(p);
1293 return NULL;
1296 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
1297 __isl_keep isl_basic_set *bset)
1299 if (!p || !bset)
1300 goto error;
1302 if (p->output_format == ISL_FORMAT_ISL)
1303 return isl_basic_map_print_isl(bset, p, 0);
1304 else if (p->output_format == ISL_FORMAT_POLYLIB)
1305 return isl_basic_set_print_polylib(bset, p, 0);
1306 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1307 return isl_basic_set_print_polylib(bset, p, 1);
1308 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
1309 return bset_print_constraints_polylib(bset, p);
1310 else if (p->output_format == ISL_FORMAT_OMEGA)
1311 return basic_set_print_omega(bset, p);
1312 isl_assert(p->ctx, 0, goto error);
1313 error:
1314 isl_printer_free(p);
1315 return NULL;
1318 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
1319 __isl_keep isl_set *set)
1321 if (!p || !set)
1322 goto error;
1323 if (p->output_format == ISL_FORMAT_ISL)
1324 return isl_map_print_isl(set_to_map(set), p);
1325 else if (p->output_format == ISL_FORMAT_POLYLIB)
1326 return isl_set_print_polylib(set, p, 0);
1327 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1328 return isl_set_print_polylib(set, p, 1);
1329 else if (p->output_format == ISL_FORMAT_OMEGA)
1330 return isl_set_print_omega(set, p);
1331 else if (p->output_format == ISL_FORMAT_LATEX)
1332 return isl_map_print_latex(set_to_map(set), p);
1333 isl_assert(set->ctx, 0, goto error);
1334 error:
1335 isl_printer_free(p);
1336 return NULL;
1339 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1340 __isl_keep isl_map *map)
1342 if (!p || !map)
1343 goto error;
1345 if (p->output_format == ISL_FORMAT_ISL)
1346 return isl_map_print_isl(map, p);
1347 else if (p->output_format == ISL_FORMAT_POLYLIB)
1348 return isl_map_print_polylib(map, p, 0);
1349 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1350 return isl_map_print_polylib(map, p, 1);
1351 else if (p->output_format == ISL_FORMAT_OMEGA)
1352 return isl_map_print_omega(map, p);
1353 else if (p->output_format == ISL_FORMAT_LATEX)
1354 return isl_map_print_latex(map, p);
1355 isl_assert(map->ctx, 0, goto error);
1356 error:
1357 isl_printer_free(p);
1358 return NULL;
1361 struct isl_union_print_data {
1362 isl_printer *p;
1363 int first;
1366 static isl_stat print_map_body(__isl_take isl_map *map, void *user)
1368 struct isl_union_print_data *data;
1369 data = (struct isl_union_print_data *)user;
1371 if (!data->first)
1372 data->p = isl_printer_print_str(data->p, "; ");
1373 data->first = 0;
1375 data->p = isl_map_print_isl_body(map, data->p);
1376 isl_map_free(map);
1378 return isl_stat_ok;
1381 static __isl_give isl_printer *isl_union_map_print_isl(
1382 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1384 struct isl_union_print_data data;
1385 struct isl_print_space_data space_data = { 0 };
1386 isl_space *dim;
1388 dim = isl_union_map_get_space(umap);
1389 if (isl_space_dim(dim, isl_dim_param) > 0) {
1390 p = print_tuple(dim, p, isl_dim_param, &space_data);
1391 p = isl_printer_print_str(p, s_to[0]);
1393 isl_space_free(dim);
1394 p = isl_printer_print_str(p, s_open_set[0]);
1395 data.p = p;
1396 data.first = 1;
1397 isl_union_map_foreach_map(umap, &print_map_body, &data);
1398 p = data.p;
1399 p = isl_printer_print_str(p, s_close_set[0]);
1400 return p;
1403 static isl_stat print_latex_map_body(__isl_take isl_map *map, void *user)
1405 struct isl_union_print_data *data;
1406 data = (struct isl_union_print_data *)user;
1408 if (!data->first)
1409 data->p = isl_printer_print_str(data->p, " \\cup ");
1410 data->first = 0;
1412 data->p = isl_map_print_latex(map, data->p);
1413 isl_map_free(map);
1415 return isl_stat_ok;
1418 static __isl_give isl_printer *isl_union_map_print_latex(
1419 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1421 struct isl_union_print_data data = { p, 1 };
1422 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1423 p = data.p;
1424 return p;
1427 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1428 __isl_keep isl_union_map *umap)
1430 if (!p || !umap)
1431 goto error;
1433 if (p->output_format == ISL_FORMAT_ISL)
1434 return isl_union_map_print_isl(umap, p);
1435 if (p->output_format == ISL_FORMAT_LATEX)
1436 return isl_union_map_print_latex(umap, p);
1438 isl_die(p->ctx, isl_error_invalid,
1439 "invalid output format for isl_union_map", goto error);
1440 error:
1441 isl_printer_free(p);
1442 return NULL;
1445 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1446 __isl_keep isl_union_set *uset)
1448 if (!p || !uset)
1449 goto error;
1451 if (p->output_format == ISL_FORMAT_ISL)
1452 return isl_union_map_print_isl((isl_union_map *)uset, p);
1453 if (p->output_format == ISL_FORMAT_LATEX)
1454 return isl_union_map_print_latex((isl_union_map *)uset, p);
1456 isl_die(p->ctx, isl_error_invalid,
1457 "invalid output format for isl_union_set", goto error);
1458 error:
1459 isl_printer_free(p);
1460 return NULL;
1463 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
1465 int i;
1466 int n;
1468 for (i = 0, n = 0; i < rec->n; ++i)
1469 if (!isl_upoly_is_zero(rec->p[i]))
1470 ++n;
1472 return n;
1475 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
1476 __isl_take isl_printer *p, int first)
1478 struct isl_upoly_cst *cst;
1479 int neg;
1481 cst = isl_upoly_as_cst(up);
1482 if (!cst)
1483 goto error;
1484 neg = !first && isl_int_is_neg(cst->n);
1485 if (!first)
1486 p = isl_printer_print_str(p, neg ? " - " : " + ");
1487 if (neg)
1488 isl_int_neg(cst->n, cst->n);
1489 if (isl_int_is_zero(cst->d)) {
1490 int sgn = isl_int_sgn(cst->n);
1491 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1492 sgn == 0 ? "NaN" : "infty");
1493 } else
1494 p = isl_printer_print_isl_int(p, cst->n);
1495 if (neg)
1496 isl_int_neg(cst->n, cst->n);
1497 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1498 p = isl_printer_print_str(p, "/");
1499 p = isl_printer_print_isl_int(p, cst->d);
1501 return p;
1502 error:
1503 isl_printer_free(p);
1504 return NULL;
1507 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1508 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var)
1510 unsigned total;
1512 total = isl_space_dim(dim, isl_dim_all);
1513 if (var < total)
1514 p = print_term(dim, NULL, dim->ctx->one, 1 + var, p, 0);
1515 else
1516 p = print_div(dim, div, var - total, p);
1517 return p;
1520 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1521 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var, int exp)
1523 p = print_base(p, dim, div, var);
1524 if (exp == 1)
1525 return p;
1526 if (p->output_format == ISL_FORMAT_C) {
1527 int i;
1528 for (i = 1; i < exp; ++i) {
1529 p = isl_printer_print_str(p, "*");
1530 p = print_base(p, dim, div, var);
1532 } else {
1533 p = isl_printer_print_str(p, "^");
1534 p = isl_printer_print_int(p, exp);
1536 return p;
1539 /* Print the polynomial "up" defined over the domain space "space" and
1540 * local variables defined by "div" to "p".
1541 * If "outer" is set, then "up" is not nested inside another polynomial.
1543 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1544 __isl_keep isl_space *space, __isl_keep isl_mat *div,
1545 __isl_take isl_printer *p, int outer)
1547 int i, n, first, print_parens;
1548 struct isl_upoly_rec *rec;
1550 if (!p || !up || !space || !div)
1551 goto error;
1553 if (isl_upoly_is_cst(up))
1554 return upoly_print_cst(up, p, 1);
1556 rec = isl_upoly_as_rec(up);
1557 if (!rec)
1558 goto error;
1559 n = upoly_rec_n_non_zero(rec);
1560 print_parens = n > 1 ||
1561 (outer && rec->up.var >= isl_space_dim(space, isl_dim_all));
1562 if (print_parens)
1563 p = isl_printer_print_str(p, "(");
1564 for (i = 0, first = 1; i < rec->n; ++i) {
1565 if (isl_upoly_is_zero(rec->p[i]))
1566 continue;
1567 if (isl_upoly_is_negone(rec->p[i])) {
1568 if (!i)
1569 p = isl_printer_print_str(p, "-1");
1570 else if (first)
1571 p = isl_printer_print_str(p, "-");
1572 else
1573 p = isl_printer_print_str(p, " - ");
1574 } else if (isl_upoly_is_cst(rec->p[i]) &&
1575 !isl_upoly_is_one(rec->p[i]))
1576 p = upoly_print_cst(rec->p[i], p, first);
1577 else {
1578 if (!first)
1579 p = isl_printer_print_str(p, " + ");
1580 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1581 p = upoly_print(rec->p[i], space, div, p, 0);
1583 first = 0;
1584 if (i == 0)
1585 continue;
1586 if (!isl_upoly_is_one(rec->p[i]) &&
1587 !isl_upoly_is_negone(rec->p[i]))
1588 p = isl_printer_print_str(p, " * ");
1589 p = print_pow(p, space, div, rec->up.var, i);
1591 if (print_parens)
1592 p = isl_printer_print_str(p, ")");
1593 return p;
1594 error:
1595 isl_printer_free(p);
1596 return NULL;
1599 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1600 __isl_keep isl_qpolynomial *qp)
1602 if (!p || !qp)
1603 goto error;
1604 p = upoly_print(qp->upoly, qp->dim, qp->div, p, 1);
1605 return p;
1606 error:
1607 isl_printer_free(p);
1608 return NULL;
1611 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1612 __isl_keep isl_qpolynomial *qp)
1614 struct isl_print_space_data data = { 0 };
1616 if (!p || !qp)
1617 goto error;
1619 if (isl_space_dim(qp->dim, isl_dim_param) > 0) {
1620 p = print_tuple(qp->dim, p, isl_dim_param, &data);
1621 p = isl_printer_print_str(p, " -> ");
1623 p = isl_printer_print_str(p, "{ ");
1624 if (!isl_space_is_params(qp->dim)) {
1625 p = isl_print_space(qp->dim, p, 0, &data);
1626 p = isl_printer_print_str(p, " -> ");
1628 p = print_qpolynomial(p, qp);
1629 p = isl_printer_print_str(p, " }");
1630 return p;
1631 error:
1632 isl_printer_free(p);
1633 return NULL;
1636 /* Print the quasi-polynomial "qp" to "p" in C format, with the variable names
1637 * taken from the domain space "space".
1639 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1640 __isl_keep isl_space *space, __isl_keep isl_qpolynomial *qp)
1642 isl_int den;
1644 isl_int_init(den);
1645 isl_qpolynomial_get_den(qp, &den);
1646 if (!isl_int_is_one(den)) {
1647 isl_qpolynomial *f;
1648 p = isl_printer_print_str(p, "(");
1649 qp = isl_qpolynomial_copy(qp);
1650 f = isl_qpolynomial_rat_cst_on_domain(isl_space_copy(qp->dim),
1651 den, qp->dim->ctx->one);
1652 qp = isl_qpolynomial_mul(qp, f);
1654 if (qp)
1655 p = upoly_print(qp->upoly, space, qp->div, p, 0);
1656 else
1657 p = isl_printer_free(p);
1658 if (!isl_int_is_one(den)) {
1659 p = isl_printer_print_str(p, ")/");
1660 p = isl_printer_print_isl_int(p, den);
1661 isl_qpolynomial_free(qp);
1663 isl_int_clear(den);
1664 return p;
1667 __isl_give isl_printer *isl_printer_print_qpolynomial(
1668 __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1670 if (!p || !qp)
1671 goto error;
1673 if (p->output_format == ISL_FORMAT_ISL)
1674 return print_qpolynomial_isl(p, qp);
1675 else if (p->output_format == ISL_FORMAT_C)
1676 return print_qpolynomial_c(p, qp->dim, qp);
1677 else
1678 isl_die(qp->dim->ctx, isl_error_unsupported,
1679 "output format not supported for isl_qpolynomials",
1680 goto error);
1681 error:
1682 isl_printer_free(p);
1683 return NULL;
1686 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1687 unsigned output_format)
1689 isl_printer *p;
1691 if (!qp)
1692 return;
1694 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1695 p = isl_printer_to_file(qp->dim->ctx, out);
1696 p = isl_printer_print_qpolynomial(p, qp);
1697 isl_printer_free(p);
1700 static __isl_give isl_printer *qpolynomial_fold_print(
1701 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1703 int i;
1705 if (fold->type == isl_fold_min)
1706 p = isl_printer_print_str(p, "min");
1707 else if (fold->type == isl_fold_max)
1708 p = isl_printer_print_str(p, "max");
1709 p = isl_printer_print_str(p, "(");
1710 for (i = 0; i < fold->n; ++i) {
1711 if (i)
1712 p = isl_printer_print_str(p, ", ");
1713 p = print_qpolynomial(p, fold->qp[i]);
1715 p = isl_printer_print_str(p, ")");
1716 return p;
1719 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1720 FILE *out, unsigned output_format)
1722 isl_printer *p;
1724 if (!fold)
1725 return;
1727 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1729 p = isl_printer_to_file(fold->dim->ctx, out);
1730 p = isl_printer_print_qpolynomial_fold(p, fold);
1732 isl_printer_free(p);
1735 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1736 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1738 struct isl_print_space_data data = { 0 };
1739 int i = 0;
1741 for (i = 0; i < pwqp->n; ++i) {
1742 isl_space *space;
1744 if (i)
1745 p = isl_printer_print_str(p, "; ");
1746 space = isl_qpolynomial_get_domain_space(pwqp->p[i].qp);
1747 if (!isl_space_is_params(space)) {
1748 p = isl_print_space(space, p, 0, &data);
1749 p = isl_printer_print_str(p, " -> ");
1751 p = print_qpolynomial(p, pwqp->p[i].qp);
1752 p = print_disjuncts(set_to_map(pwqp->p[i].set), space, p, 0);
1753 isl_space_free(space);
1756 return p;
1759 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1760 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1762 struct isl_print_space_data data = { 0 };
1764 if (!p || !pwqp)
1765 goto error;
1767 if (isl_space_dim(pwqp->dim, isl_dim_param) > 0) {
1768 p = print_tuple(pwqp->dim, p, isl_dim_param, &data);
1769 p = isl_printer_print_str(p, " -> ");
1771 p = isl_printer_print_str(p, "{ ");
1772 if (pwqp->n == 0) {
1773 if (!isl_space_is_set(pwqp->dim)) {
1774 p = print_tuple(pwqp->dim, p, isl_dim_in, &data);
1775 p = isl_printer_print_str(p, " -> ");
1777 p = isl_printer_print_str(p, "0");
1779 p = isl_pwqp_print_isl_body(p, pwqp);
1780 p = isl_printer_print_str(p, " }");
1781 return p;
1782 error:
1783 isl_printer_free(p);
1784 return NULL;
1787 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
1788 unsigned output_format)
1790 isl_printer *p;
1792 if (!pwqp)
1793 return;
1795 p = isl_printer_to_file(pwqp->dim->ctx, out);
1796 p = isl_printer_set_output_format(p, output_format);
1797 p = isl_printer_print_pw_qpolynomial(p, pwqp);
1799 isl_printer_free(p);
1802 static __isl_give isl_printer *isl_pwf_print_isl_body(
1803 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1805 struct isl_print_space_data data = { 0 };
1806 int i = 0;
1808 for (i = 0; i < pwf->n; ++i) {
1809 isl_space *space;
1811 if (i)
1812 p = isl_printer_print_str(p, "; ");
1813 space = isl_qpolynomial_fold_get_domain_space(pwf->p[i].fold);
1814 if (!isl_space_is_params(space)) {
1815 p = isl_print_space(space, p, 0, &data);
1816 p = isl_printer_print_str(p, " -> ");
1818 p = qpolynomial_fold_print(pwf->p[i].fold, p);
1819 p = print_disjuncts(set_to_map(pwf->p[i].set), space, p, 0);
1820 isl_space_free(space);
1823 return p;
1826 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
1827 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1829 struct isl_print_space_data data = { 0 };
1831 if (isl_space_dim(pwf->dim, isl_dim_param) > 0) {
1832 p = print_tuple(pwf->dim, p, isl_dim_param, &data);
1833 p = isl_printer_print_str(p, " -> ");
1835 p = isl_printer_print_str(p, "{ ");
1836 if (pwf->n == 0) {
1837 if (!isl_space_is_set(pwf->dim)) {
1838 p = print_tuple(pwf->dim, p, isl_dim_in, &data);
1839 p = isl_printer_print_str(p, " -> ");
1841 p = isl_printer_print_str(p, "0");
1843 p = isl_pwf_print_isl_body(p, pwf);
1844 p = isl_printer_print_str(p, " }");
1845 return p;
1848 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1849 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c);
1851 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1852 __isl_keep isl_space *dim,
1853 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1855 if (type == isl_dim_div) {
1856 p = isl_printer_print_str(p, "floord(");
1857 p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
1858 p = isl_printer_print_str(p, ", ");
1859 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1860 p = isl_printer_print_str(p, ")");
1861 } else {
1862 const char *name;
1864 name = isl_space_get_dim_name(dim, type, pos);
1865 if (!name)
1866 name = "UNNAMED";
1867 p = isl_printer_print_str(p, name);
1869 return p;
1872 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1873 __isl_keep isl_space *dim,
1874 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1876 enum isl_dim_type type;
1878 if (pos == 0)
1879 return isl_printer_print_isl_int(p, c);
1881 if (isl_int_is_one(c))
1883 else if (isl_int_is_negone(c))
1884 p = isl_printer_print_str(p, "-");
1885 else {
1886 p = isl_printer_print_isl_int(p, c);
1887 p = isl_printer_print_str(p, "*");
1889 type = pos2type(dim, &pos);
1890 p = print_name_c(p, dim, bset, type, pos);
1891 return p;
1894 static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
1895 __isl_keep isl_space *dim,
1896 __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
1898 int i;
1899 int first;
1901 for (i = 0, first = 1; i < len; ++i) {
1902 int flip = 0;
1903 if (isl_int_is_zero(c[i]))
1904 continue;
1905 if (!first) {
1906 if (isl_int_is_neg(c[i])) {
1907 flip = 1;
1908 isl_int_neg(c[i], c[i]);
1909 p = isl_printer_print_str(p, " - ");
1910 } else
1911 p = isl_printer_print_str(p, " + ");
1913 first = 0;
1914 p = print_term_c(p, dim, bset, c[i], i);
1915 if (flip)
1916 isl_int_neg(c[i], c[i]);
1918 if (first)
1919 p = isl_printer_print_str(p, "0");
1920 return p;
1923 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1924 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c)
1926 unsigned len = 1 + isl_basic_set_total_dim(bset);
1927 return print_partial_affine_c(p, dim, bset, c, len);
1930 /* We skip the constraint if it is implied by the div expression.
1932 * *first indicates whether this is the first constraint in the conjunction and
1933 * is updated if the constraint is actually printed.
1935 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1936 __isl_keep isl_space *dim,
1937 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int *first)
1939 unsigned o_div;
1940 unsigned n_div;
1941 int div;
1943 o_div = isl_basic_set_offset(bset, isl_dim_div);
1944 n_div = isl_basic_set_dim(bset, isl_dim_div);
1945 div = isl_seq_last_non_zero(c + o_div, n_div);
1946 if (div >= 0 && isl_basic_set_is_div_constraint(bset, c, div))
1947 return p;
1949 if (!*first)
1950 p = isl_printer_print_str(p, " && ");
1952 p = print_affine_c(p, dim, bset, c);
1953 p = isl_printer_print_str(p, " ");
1954 p = isl_printer_print_str(p, op);
1955 p = isl_printer_print_str(p, " 0");
1957 *first = 0;
1959 return p;
1962 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1963 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset)
1965 int i, j;
1966 int first = 1;
1967 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
1968 unsigned total = isl_basic_set_total_dim(bset) - n_div;
1970 for (i = 0; i < bset->n_eq; ++i) {
1971 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
1972 if (j < 0)
1973 p = print_constraint_c(p, dim, bset,
1974 bset->eq[i], "==", &first);
1975 else {
1976 if (i)
1977 p = isl_printer_print_str(p, " && ");
1978 p = isl_printer_print_str(p, "(");
1979 p = print_partial_affine_c(p, dim, bset, bset->eq[i],
1980 1 + total + j);
1981 p = isl_printer_print_str(p, ") % ");
1982 p = isl_printer_print_isl_int(p,
1983 bset->eq[i][1 + total + j]);
1984 p = isl_printer_print_str(p, " == 0");
1985 first = 0;
1988 for (i = 0; i < bset->n_ineq; ++i)
1989 p = print_constraint_c(p, dim, bset, bset->ineq[i], ">=",
1990 &first);
1991 return p;
1994 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
1995 __isl_keep isl_space *dim, __isl_keep isl_set *set)
1997 int i;
1999 if (!set)
2000 return isl_printer_free(p);
2002 if (set->n == 0)
2003 p = isl_printer_print_str(p, "0");
2005 for (i = 0; i < set->n; ++i) {
2006 if (i)
2007 p = isl_printer_print_str(p, " || ");
2008 if (set->n > 1)
2009 p = isl_printer_print_str(p, "(");
2010 p = print_basic_set_c(p, dim, set->p[i]);
2011 if (set->n > 1)
2012 p = isl_printer_print_str(p, ")");
2014 return p;
2017 /* Print the piecewise quasi-polynomial "pwqp" to "p" in C format.
2019 static __isl_give isl_printer *print_pw_qpolynomial_c(
2020 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2022 int i;
2023 isl_space *space;
2025 space = isl_pw_qpolynomial_get_domain_space(pwqp);
2026 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set)) {
2027 p = print_qpolynomial_c(p, space, pwqp->p[0].qp);
2028 isl_space_free(space);
2029 return p;
2032 for (i = 0; i < pwqp->n; ++i) {
2033 p = isl_printer_print_str(p, "(");
2034 p = print_set_c(p, space, pwqp->p[i].set);
2035 p = isl_printer_print_str(p, ") ? (");
2036 p = print_qpolynomial_c(p, space, pwqp->p[i].qp);
2037 p = isl_printer_print_str(p, ") : ");
2040 isl_space_free(space);
2041 p = isl_printer_print_str(p, "0");
2042 return p;
2045 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
2046 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2048 if (!p || !pwqp)
2049 goto error;
2051 if (p->output_format == ISL_FORMAT_ISL)
2052 return print_pw_qpolynomial_isl(p, pwqp);
2053 else if (p->output_format == ISL_FORMAT_C)
2054 return print_pw_qpolynomial_c(p, pwqp);
2055 isl_assert(p->ctx, 0, goto error);
2056 error:
2057 isl_printer_free(p);
2058 return NULL;
2061 static isl_stat print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
2063 struct isl_union_print_data *data;
2064 data = (struct isl_union_print_data *)user;
2066 if (!data->first)
2067 data->p = isl_printer_print_str(data->p, "; ");
2068 data->first = 0;
2070 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
2071 isl_pw_qpolynomial_free(pwqp);
2073 return isl_stat_ok;
2076 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
2077 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2079 struct isl_union_print_data data;
2080 struct isl_print_space_data space_data = { 0 };
2081 isl_space *dim;
2083 dim = isl_union_pw_qpolynomial_get_space(upwqp);
2084 if (isl_space_dim(dim, isl_dim_param) > 0) {
2085 p = print_tuple(dim, p, isl_dim_param, &space_data);
2086 p = isl_printer_print_str(p, " -> ");
2088 isl_space_free(dim);
2089 p = isl_printer_print_str(p, "{ ");
2090 data.p = p;
2091 data.first = 1;
2092 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
2093 &data);
2094 p = data.p;
2095 p = isl_printer_print_str(p, " }");
2096 return p;
2099 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
2100 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2102 if (!p || !upwqp)
2103 goto error;
2105 if (p->output_format == ISL_FORMAT_ISL)
2106 return print_union_pw_qpolynomial_isl(p, upwqp);
2107 isl_die(p->ctx, isl_error_invalid,
2108 "invalid output format for isl_union_pw_qpolynomial",
2109 goto error);
2110 error:
2111 isl_printer_free(p);
2112 return NULL;
2115 /* Print the quasi-polynomial reduction "fold" to "p" in C format,
2116 * with the variable names taken from the domain space "space".
2118 static __isl_give isl_printer *print_qpolynomial_fold_c(
2119 __isl_take isl_printer *p, __isl_keep isl_space *space,
2120 __isl_keep isl_qpolynomial_fold *fold)
2122 int i;
2124 for (i = 0; i < fold->n - 1; ++i)
2125 if (fold->type == isl_fold_min)
2126 p = isl_printer_print_str(p, "min(");
2127 else if (fold->type == isl_fold_max)
2128 p = isl_printer_print_str(p, "max(");
2130 for (i = 0; i < fold->n; ++i) {
2131 if (i)
2132 p = isl_printer_print_str(p, ", ");
2133 p = print_qpolynomial_c(p, space, fold->qp[i]);
2134 if (i)
2135 p = isl_printer_print_str(p, ")");
2137 return p;
2140 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
2141 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
2143 if (!p || !fold)
2144 goto error;
2145 if (p->output_format == ISL_FORMAT_ISL)
2146 return qpolynomial_fold_print(fold, p);
2147 else if (p->output_format == ISL_FORMAT_C)
2148 return print_qpolynomial_fold_c(p, fold->dim, fold);
2149 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2150 goto error);
2151 error:
2152 isl_printer_free(p);
2153 return NULL;
2156 /* Print the piecewise quasi-polynomial reduction "pwf" to "p" in C format.
2158 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
2159 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2161 int i;
2162 isl_space *space;
2164 space = isl_pw_qpolynomial_fold_get_domain_space(pwf);
2165 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set)) {
2166 p = print_qpolynomial_fold_c(p, space, pwf->p[0].fold);
2167 isl_space_free(space);
2168 return p;
2171 for (i = 0; i < pwf->n; ++i) {
2172 p = isl_printer_print_str(p, "(");
2173 p = print_set_c(p, space, pwf->p[i].set);
2174 p = isl_printer_print_str(p, ") ? (");
2175 p = print_qpolynomial_fold_c(p, space, pwf->p[i].fold);
2176 p = isl_printer_print_str(p, ") : ");
2179 isl_space_free(space);
2180 p = isl_printer_print_str(p, "0");
2181 return p;
2184 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
2185 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2187 if (!p || !pwf)
2188 goto error;
2190 if (p->output_format == ISL_FORMAT_ISL)
2191 return print_pw_qpolynomial_fold_isl(p, pwf);
2192 else if (p->output_format == ISL_FORMAT_C)
2193 return print_pw_qpolynomial_fold_c(p, pwf);
2194 isl_assert(p->ctx, 0, goto error);
2195 error:
2196 isl_printer_free(p);
2197 return NULL;
2200 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
2201 FILE *out, unsigned output_format)
2203 isl_printer *p;
2205 if (!pwf)
2206 return;
2208 p = isl_printer_to_file(pwf->dim->ctx, out);
2209 p = isl_printer_set_output_format(p, output_format);
2210 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
2212 isl_printer_free(p);
2215 static isl_stat print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf,
2216 void *user)
2218 struct isl_union_print_data *data;
2219 data = (struct isl_union_print_data *)user;
2221 if (!data->first)
2222 data->p = isl_printer_print_str(data->p, "; ");
2223 data->first = 0;
2225 data->p = isl_pwf_print_isl_body(data->p, pwf);
2226 isl_pw_qpolynomial_fold_free(pwf);
2228 return isl_stat_ok;
2231 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
2232 __isl_take isl_printer *p,
2233 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2235 struct isl_union_print_data data;
2236 struct isl_print_space_data space_data = { 0 };
2237 isl_space *dim;
2239 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
2240 if (isl_space_dim(dim, isl_dim_param) > 0) {
2241 p = print_tuple(dim, p, isl_dim_param, &space_data);
2242 p = isl_printer_print_str(p, " -> ");
2244 isl_space_free(dim);
2245 p = isl_printer_print_str(p, "{ ");
2246 data.p = p;
2247 data.first = 1;
2248 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
2249 &print_pwf_body, &data);
2250 p = data.p;
2251 p = isl_printer_print_str(p, " }");
2252 return p;
2255 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2256 __isl_take isl_printer *p,
2257 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2259 if (!p || !upwf)
2260 goto error;
2262 if (p->output_format == ISL_FORMAT_ISL)
2263 return print_union_pw_qpolynomial_fold_isl(p, upwf);
2264 isl_die(p->ctx, isl_error_invalid,
2265 "invalid output format for isl_union_pw_qpolynomial_fold",
2266 goto error);
2267 error:
2268 isl_printer_free(p);
2269 return NULL;
2272 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
2273 __isl_keep isl_constraint *c)
2275 isl_basic_map *bmap;
2277 if (!p || !c)
2278 goto error;
2280 bmap = isl_basic_map_from_constraint(isl_constraint_copy(c));
2281 p = isl_printer_print_basic_map(p, bmap);
2282 isl_basic_map_free(bmap);
2283 return p;
2284 error:
2285 isl_printer_free(p);
2286 return NULL;
2289 static __isl_give isl_printer *isl_printer_print_space_isl(
2290 __isl_take isl_printer *p, __isl_keep isl_space *space)
2292 struct isl_print_space_data data = { 0 };
2294 if (!space)
2295 goto error;
2297 if (isl_space_dim(space, isl_dim_param) > 0) {
2298 p = print_tuple(space, p, isl_dim_param, &data);
2299 p = isl_printer_print_str(p, " -> ");
2302 p = isl_printer_print_str(p, "{ ");
2303 if (isl_space_is_params(space))
2304 p = isl_printer_print_str(p, s_such_that[0]);
2305 else
2306 p = isl_print_space(space, p, 0, &data);
2307 p = isl_printer_print_str(p, " }");
2309 return p;
2310 error:
2311 isl_printer_free(p);
2312 return NULL;
2315 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
2316 __isl_keep isl_space *space)
2318 if (!p || !space)
2319 return isl_printer_free(p);
2320 if (p->output_format == ISL_FORMAT_ISL)
2321 return isl_printer_print_space_isl(p, space);
2322 else if (p->output_format == ISL_FORMAT_OMEGA)
2323 return print_omega_parameters(space, p);
2325 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
2326 "output format not supported for space",
2327 return isl_printer_free(p));
2330 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
2331 __isl_keep isl_local_space *ls)
2333 struct isl_print_space_data data = { 0 };
2334 unsigned n_div;
2336 if (!ls)
2337 goto error;
2339 if (isl_local_space_dim(ls, isl_dim_param) > 0) {
2340 p = print_tuple(ls->dim, p, isl_dim_param, &data);
2341 p = isl_printer_print_str(p, " -> ");
2343 p = isl_printer_print_str(p, "{ ");
2344 p = isl_print_space(ls->dim, p, 0, &data);
2345 n_div = isl_local_space_dim(ls, isl_dim_div);
2346 if (n_div > 0) {
2347 p = isl_printer_print_str(p, " : ");
2348 p = isl_printer_print_str(p, s_open_exists[0]);
2349 p = print_div_list(p, ls->dim, ls->div, 0, 1);
2350 p = isl_printer_print_str(p, s_close_exists[0]);
2351 } else if (isl_space_is_params(ls->dim))
2352 p = isl_printer_print_str(p, s_such_that[0]);
2353 p = isl_printer_print_str(p, " }");
2354 return p;
2355 error:
2356 isl_printer_free(p);
2357 return NULL;
2360 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2361 __isl_keep isl_aff *aff)
2363 unsigned total;
2365 if (isl_aff_is_nan(aff))
2366 return isl_printer_print_str(p, "NaN");
2368 total = isl_local_space_dim(aff->ls, isl_dim_all);
2369 p = isl_printer_print_str(p, "(");
2370 p = print_affine_of_len(aff->ls->dim, aff->ls->div, p,
2371 aff->v->el + 1, 1 + total);
2372 if (isl_int_is_one(aff->v->el[0]))
2373 p = isl_printer_print_str(p, ")");
2374 else {
2375 p = isl_printer_print_str(p, ")/");
2376 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2379 return p;
2382 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2383 __isl_keep isl_aff *aff)
2385 struct isl_print_space_data data = { 0 };
2387 if (isl_space_is_params(aff->ls->dim))
2389 else {
2390 p = print_tuple(aff->ls->dim, p, isl_dim_set, &data);
2391 p = isl_printer_print_str(p, " -> ");
2393 p = isl_printer_print_str(p, "[");
2394 p = print_aff_body(p, aff);
2395 p = isl_printer_print_str(p, "]");
2397 return p;
2400 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2401 __isl_keep isl_aff *aff)
2403 struct isl_print_space_data data = { 0 };
2405 if (!aff)
2406 goto error;
2408 if (isl_local_space_dim(aff->ls, isl_dim_param) > 0) {
2409 p = print_tuple(aff->ls->dim, p, isl_dim_param, &data);
2410 p = isl_printer_print_str(p, " -> ");
2412 p = isl_printer_print_str(p, "{ ");
2413 p = print_aff(p, aff);
2414 p = isl_printer_print_str(p, " }");
2415 return p;
2416 error:
2417 isl_printer_free(p);
2418 return NULL;
2421 /* Print the body of an isl_pw_aff, i.e., a semicolon delimited
2422 * sequence of affine expressions, each followed by constraints.
2424 static __isl_give isl_printer *print_pw_aff_body(
2425 __isl_take isl_printer *p, __isl_keep isl_pw_aff *pa)
2427 int i;
2429 if (!pa)
2430 return isl_printer_free(p);
2432 for (i = 0; i < pa->n; ++i) {
2433 isl_space *space;
2435 if (i)
2436 p = isl_printer_print_str(p, "; ");
2437 p = print_aff(p, pa->p[i].aff);
2438 space = isl_aff_get_domain_space(pa->p[i].aff);
2439 p = print_disjuncts(set_to_map(pa->p[i].set), space, p, 0);
2440 isl_space_free(space);
2443 return p;
2446 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2447 __isl_keep isl_pw_aff *pwaff)
2449 struct isl_print_space_data data = { 0 };
2451 if (!pwaff)
2452 goto error;
2454 if (isl_space_dim(pwaff->dim, isl_dim_param) > 0) {
2455 p = print_tuple(pwaff->dim, p, isl_dim_param, &data);
2456 p = isl_printer_print_str(p, " -> ");
2458 p = isl_printer_print_str(p, "{ ");
2459 p = print_pw_aff_body(p, pwaff);
2460 p = isl_printer_print_str(p, " }");
2461 return p;
2462 error:
2463 isl_printer_free(p);
2464 return NULL;
2467 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2468 __isl_keep isl_local_space *ls, isl_int *c);
2470 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2471 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2473 if (type == isl_dim_div) {
2474 p = isl_printer_print_str(p, "floord(");
2475 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2476 p = isl_printer_print_str(p, ", ");
2477 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2478 p = isl_printer_print_str(p, ")");
2479 } else {
2480 const char *name;
2482 name = isl_space_get_dim_name(ls->dim, type, pos);
2483 if (!name)
2484 name = "UNNAMED";
2485 p = isl_printer_print_str(p, name);
2487 return p;
2490 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2491 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2493 enum isl_dim_type type;
2495 if (pos == 0)
2496 return isl_printer_print_isl_int(p, c);
2498 if (isl_int_is_one(c))
2500 else if (isl_int_is_negone(c))
2501 p = isl_printer_print_str(p, "-");
2502 else {
2503 p = isl_printer_print_isl_int(p, c);
2504 p = isl_printer_print_str(p, "*");
2506 type = pos2type(ls->dim, &pos);
2507 p = print_ls_name_c(p, ls, type, pos);
2508 return p;
2511 static __isl_give isl_printer *print_ls_partial_affine_c(
2512 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2513 isl_int *c, unsigned len)
2515 int i;
2516 int first;
2518 for (i = 0, first = 1; i < len; ++i) {
2519 int flip = 0;
2520 if (isl_int_is_zero(c[i]))
2521 continue;
2522 if (!first) {
2523 if (isl_int_is_neg(c[i])) {
2524 flip = 1;
2525 isl_int_neg(c[i], c[i]);
2526 p = isl_printer_print_str(p, " - ");
2527 } else
2528 p = isl_printer_print_str(p, " + ");
2530 first = 0;
2531 p = print_ls_term_c(p, ls, c[i], i);
2532 if (flip)
2533 isl_int_neg(c[i], c[i]);
2535 if (first)
2536 p = isl_printer_print_str(p, "0");
2537 return p;
2540 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2541 __isl_keep isl_local_space *ls, isl_int *c)
2543 unsigned len = 1 + isl_local_space_dim(ls, isl_dim_all);
2544 return print_ls_partial_affine_c(p, ls, c, len);
2547 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2548 __isl_keep isl_aff *aff)
2550 unsigned total;
2552 total = isl_local_space_dim(aff->ls, isl_dim_all);
2553 if (!isl_int_is_one(aff->v->el[0]))
2554 p = isl_printer_print_str(p, "(");
2555 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2556 if (!isl_int_is_one(aff->v->el[0])) {
2557 p = isl_printer_print_str(p, ")/");
2558 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2560 return p;
2563 /* In the C format, we cannot express that "pwaff" may be undefined
2564 * on parts of the domain space. We therefore assume that the expression
2565 * will only be evaluated on its definition domain and compute the gist
2566 * of each cell with respect to this domain.
2568 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2569 __isl_keep isl_pw_aff *pwaff)
2571 isl_set *domain;
2572 isl_ast_build *build;
2573 isl_ast_expr *expr;
2575 if (pwaff->n < 1)
2576 isl_die(p->ctx, isl_error_unsupported,
2577 "cannot print empty isl_pw_aff in C format",
2578 return isl_printer_free(p));
2580 domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
2581 build = isl_ast_build_from_context(domain);
2582 expr = isl_ast_build_expr_from_pw_aff(build, isl_pw_aff_copy(pwaff));
2583 p = isl_printer_print_ast_expr(p, expr);
2584 isl_ast_expr_free(expr);
2585 isl_ast_build_free(build);
2587 return p;
2590 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2591 __isl_keep isl_aff *aff)
2593 if (!p || !aff)
2594 goto error;
2596 if (p->output_format == ISL_FORMAT_ISL)
2597 return print_aff_isl(p, aff);
2598 else if (p->output_format == ISL_FORMAT_C)
2599 return print_aff_c(p, aff);
2600 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2601 goto error);
2602 error:
2603 isl_printer_free(p);
2604 return NULL;
2607 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2608 __isl_keep isl_pw_aff *pwaff)
2610 if (!p || !pwaff)
2611 goto error;
2613 if (p->output_format == ISL_FORMAT_ISL)
2614 return print_pw_aff_isl(p, pwaff);
2615 else if (p->output_format == ISL_FORMAT_C)
2616 return print_pw_aff_c(p, pwaff);
2617 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2618 goto error);
2619 error:
2620 isl_printer_free(p);
2621 return NULL;
2624 /* Print "pa" in a sequence of isl_pw_affs delimited by semicolons.
2625 * Each isl_pw_aff itself is also printed as semicolon delimited
2626 * sequence of pieces.
2627 * If data->first = 1, then this is the first in the sequence.
2628 * Update data->first to tell the next element that it is not the first.
2630 static isl_stat print_pw_aff_body_wrap(__isl_take isl_pw_aff *pa,
2631 void *user)
2633 struct isl_union_print_data *data;
2634 data = (struct isl_union_print_data *) user;
2636 if (!data->first)
2637 data->p = isl_printer_print_str(data->p, "; ");
2638 data->first = 0;
2640 data->p = print_pw_aff_body(data->p, pa);
2641 isl_pw_aff_free(pa);
2643 return data->p ? isl_stat_ok : isl_stat_error;
2646 /* Print the body of an isl_union_pw_aff, i.e., a semicolon delimited
2647 * sequence of affine expressions, each followed by constraints,
2648 * with the sequence enclosed in braces.
2650 static __isl_give isl_printer *print_union_pw_aff_body(
2651 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2653 struct isl_union_print_data data = { p, 1 };
2655 p = isl_printer_print_str(p, s_open_set[0]);
2656 data.p = p;
2657 if (isl_union_pw_aff_foreach_pw_aff(upa,
2658 &print_pw_aff_body_wrap, &data) < 0)
2659 data.p = isl_printer_free(p);
2660 p = data.p;
2661 p = isl_printer_print_str(p, s_close_set[0]);
2663 return p;
2666 /* Print the isl_union_pw_aff "upa" to "p" in isl format.
2668 * The individual isl_pw_affs are delimited by a semicolon.
2670 static __isl_give isl_printer *print_union_pw_aff_isl(
2671 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2673 struct isl_print_space_data data = { 0 };
2674 isl_space *space;
2676 space = isl_union_pw_aff_get_space(upa);
2677 if (isl_space_dim(space, isl_dim_param) > 0) {
2678 p = print_tuple(space, p, isl_dim_param, &data);
2679 p = isl_printer_print_str(p, s_to[0]);
2681 isl_space_free(space);
2682 p = print_union_pw_aff_body(p, upa);
2683 return p;
2686 /* Print the isl_union_pw_aff "upa" to "p".
2688 * We currently only support an isl format.
2690 __isl_give isl_printer *isl_printer_print_union_pw_aff(
2691 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2693 if (!p || !upa)
2694 return isl_printer_free(p);
2696 if (p->output_format == ISL_FORMAT_ISL)
2697 return print_union_pw_aff_isl(p, upa);
2698 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2699 "unsupported output format", return isl_printer_free(p));
2702 /* Print dimension "pos" of data->space to "p".
2704 * data->user is assumed to be an isl_multi_aff.
2706 * If the current dimension is an output dimension, then print
2707 * the corresponding expression. Otherwise, print the name of the dimension.
2709 static __isl_give isl_printer *print_dim_ma(__isl_take isl_printer *p,
2710 struct isl_print_space_data *data, unsigned pos)
2712 isl_multi_aff *ma = data->user;
2714 if (data->type == isl_dim_out)
2715 p = print_aff_body(p, ma->p[pos]);
2716 else
2717 p = print_name(data->space, p, data->type, pos, data->latex);
2719 return p;
2722 static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
2723 __isl_keep isl_multi_aff *maff)
2725 struct isl_print_space_data data = { 0 };
2727 data.print_dim = &print_dim_ma;
2728 data.user = maff;
2729 return isl_print_space(maff->space, p, 0, &data);
2732 static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
2733 __isl_keep isl_multi_aff *maff)
2735 struct isl_print_space_data data = { 0 };
2737 if (!maff)
2738 goto error;
2740 if (isl_space_dim(maff->space, isl_dim_param) > 0) {
2741 p = print_tuple(maff->space, p, isl_dim_param, &data);
2742 p = isl_printer_print_str(p, " -> ");
2744 p = isl_printer_print_str(p, "{ ");
2745 p = print_multi_aff(p, maff);
2746 p = isl_printer_print_str(p, " }");
2747 return p;
2748 error:
2749 isl_printer_free(p);
2750 return NULL;
2753 __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
2754 __isl_keep isl_multi_aff *maff)
2756 if (!p || !maff)
2757 goto error;
2759 if (p->output_format == ISL_FORMAT_ISL)
2760 return print_multi_aff_isl(p, maff);
2761 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2762 goto error);
2763 error:
2764 isl_printer_free(p);
2765 return NULL;
2768 static __isl_give isl_printer *print_pw_multi_aff_body(
2769 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2771 int i;
2773 if (!pma)
2774 goto error;
2776 for (i = 0; i < pma->n; ++i) {
2777 isl_space *space;
2779 if (i)
2780 p = isl_printer_print_str(p, "; ");
2781 p = print_multi_aff(p, pma->p[i].maff);
2782 space = isl_multi_aff_get_domain_space(pma->p[i].maff);
2783 p = print_disjuncts(set_to_map(pma->p[i].set), space, p, 0);
2784 isl_space_free(space);
2786 return p;
2787 error:
2788 isl_printer_free(p);
2789 return NULL;
2792 static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
2793 __isl_keep isl_pw_multi_aff *pma)
2795 struct isl_print_space_data data = { 0 };
2797 if (!pma)
2798 goto error;
2800 if (isl_space_dim(pma->dim, isl_dim_param) > 0) {
2801 p = print_tuple(pma->dim, p, isl_dim_param, &data);
2802 p = isl_printer_print_str(p, " -> ");
2804 p = isl_printer_print_str(p, "{ ");
2805 p = print_pw_multi_aff_body(p, pma);
2806 p = isl_printer_print_str(p, " }");
2807 return p;
2808 error:
2809 isl_printer_free(p);
2810 return NULL;
2813 /* Print the unnamed, single-dimensional piecewise multi affine expression "pma"
2814 * to "p".
2816 static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
2817 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2819 int i;
2820 isl_space *space;
2822 space = isl_pw_multi_aff_get_domain_space(pma);
2823 for (i = 0; i < pma->n - 1; ++i) {
2824 p = isl_printer_print_str(p, "(");
2825 p = print_set_c(p, space, pma->p[i].set);
2826 p = isl_printer_print_str(p, ") ? (");
2827 p = print_aff_c(p, pma->p[i].maff->p[0]);
2828 p = isl_printer_print_str(p, ") : ");
2830 isl_space_free(space);
2832 return print_aff_c(p, pma->p[pma->n - 1].maff->p[0]);
2835 static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
2836 __isl_keep isl_pw_multi_aff *pma)
2838 int n;
2839 const char *name;
2841 if (!pma)
2842 goto error;
2843 if (pma->n < 1)
2844 isl_die(p->ctx, isl_error_unsupported,
2845 "cannot print empty isl_pw_multi_aff in C format",
2846 goto error);
2847 name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
2848 if (!name && isl_pw_multi_aff_dim(pma, isl_dim_out) == 1)
2849 return print_unnamed_pw_multi_aff_c(p, pma);
2850 if (!name)
2851 isl_die(p->ctx, isl_error_unsupported,
2852 "cannot print unnamed isl_pw_multi_aff in C format",
2853 goto error);
2855 p = isl_printer_print_str(p, name);
2856 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
2857 if (n != 0)
2858 isl_die(p->ctx, isl_error_unsupported,
2859 "not supported yet", goto error);
2861 return p;
2862 error:
2863 isl_printer_free(p);
2864 return NULL;
2867 __isl_give isl_printer *isl_printer_print_pw_multi_aff(
2868 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2870 if (!p || !pma)
2871 goto error;
2873 if (p->output_format == ISL_FORMAT_ISL)
2874 return print_pw_multi_aff_isl(p, pma);
2875 if (p->output_format == ISL_FORMAT_C)
2876 return print_pw_multi_aff_c(p, pma);
2877 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2878 goto error);
2879 error:
2880 isl_printer_free(p);
2881 return NULL;
2884 static isl_stat print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
2885 void *user)
2887 struct isl_union_print_data *data;
2888 data = (struct isl_union_print_data *) user;
2890 if (!data->first)
2891 data->p = isl_printer_print_str(data->p, "; ");
2892 data->first = 0;
2894 data->p = print_pw_multi_aff_body(data->p, pma);
2895 isl_pw_multi_aff_free(pma);
2897 return isl_stat_ok;
2900 static __isl_give isl_printer *print_union_pw_multi_aff_isl(
2901 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2903 struct isl_union_print_data data;
2904 struct isl_print_space_data space_data = { 0 };
2905 isl_space *space;
2907 space = isl_union_pw_multi_aff_get_space(upma);
2908 if (isl_space_dim(space, isl_dim_param) > 0) {
2909 p = print_tuple(space, p, isl_dim_param, &space_data);
2910 p = isl_printer_print_str(p, s_to[0]);
2912 isl_space_free(space);
2913 p = isl_printer_print_str(p, s_open_set[0]);
2914 data.p = p;
2915 data.first = 1;
2916 isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
2917 &print_pw_multi_aff_body_wrap, &data);
2918 p = data.p;
2919 p = isl_printer_print_str(p, s_close_set[0]);
2920 return p;
2923 __isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
2924 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2926 if (!p || !upma)
2927 goto error;
2929 if (p->output_format == ISL_FORMAT_ISL)
2930 return print_union_pw_multi_aff_isl(p, upma);
2931 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2932 goto error);
2933 error:
2934 isl_printer_free(p);
2935 return NULL;
2938 /* Print dimension "pos" of data->space to "p".
2940 * data->user is assumed to be an isl_multi_pw_aff.
2942 * If the current dimension is an output dimension, then print
2943 * the corresponding piecewise affine expression.
2944 * Otherwise, print the name of the dimension.
2946 static __isl_give isl_printer *print_dim_mpa(__isl_take isl_printer *p,
2947 struct isl_print_space_data *data, unsigned pos)
2949 int i;
2950 int need_parens;
2951 isl_multi_pw_aff *mpa = data->user;
2952 isl_pw_aff *pa;
2954 if (data->type != isl_dim_out)
2955 return print_name(data->space, p, data->type, pos, data->latex);
2957 pa = mpa->p[pos];
2958 if (pa->n == 0)
2959 return isl_printer_print_str(p, "(0 : 1 = 0)");
2961 need_parens = pa->n != 1 || !isl_set_plain_is_universe(pa->p[0].set);
2962 if (need_parens)
2963 p = isl_printer_print_str(p, "(");
2964 for (i = 0; i < pa->n; ++i) {
2965 isl_space *space;
2967 if (i)
2968 p = isl_printer_print_str(p, "; ");
2969 p = print_aff_body(p, pa->p[i].aff);
2970 space = isl_aff_get_domain_space(pa->p[i].aff);
2971 p = print_disjuncts(pa->p[i].set, space, p, 0);
2972 isl_space_free(space);
2974 if (need_parens)
2975 p = isl_printer_print_str(p, ")");
2977 return p;
2980 /* Print "mpa" to "p" in isl format.
2982 static __isl_give isl_printer *print_multi_pw_aff_isl(__isl_take isl_printer *p,
2983 __isl_keep isl_multi_pw_aff *mpa)
2985 struct isl_print_space_data data = { 0 };
2987 if (!mpa)
2988 return isl_printer_free(p);
2990 if (isl_space_dim(mpa->space, isl_dim_param) > 0) {
2991 p = print_tuple(mpa->space, p, isl_dim_param, &data);
2992 p = isl_printer_print_str(p, " -> ");
2994 p = isl_printer_print_str(p, "{ ");
2995 data.print_dim = &print_dim_mpa;
2996 data.user = mpa;
2997 p = isl_print_space(mpa->space, p, 0, &data);
2998 p = isl_printer_print_str(p, " }");
2999 return p;
3002 __isl_give isl_printer *isl_printer_print_multi_pw_aff(
3003 __isl_take isl_printer *p, __isl_keep isl_multi_pw_aff *mpa)
3005 if (!p || !mpa)
3006 return isl_printer_free(p);
3008 if (p->output_format == ISL_FORMAT_ISL)
3009 return print_multi_pw_aff_isl(p, mpa);
3010 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3011 return isl_printer_free(p));
3014 /* Print dimension "pos" of data->space to "p".
3016 * data->user is assumed to be an isl_multi_val.
3018 * If the current dimension is an output dimension, then print
3019 * the corresponding value. Otherwise, print the name of the dimension.
3021 static __isl_give isl_printer *print_dim_mv(__isl_take isl_printer *p,
3022 struct isl_print_space_data *data, unsigned pos)
3024 isl_multi_val *mv = data->user;
3026 if (data->type == isl_dim_out)
3027 return isl_printer_print_val(p, mv->p[pos]);
3028 else
3029 return print_name(data->space, p, data->type, pos, data->latex);
3032 /* Print the isl_multi_val "mv" to "p" in isl format.
3034 static __isl_give isl_printer *print_multi_val_isl(__isl_take isl_printer *p,
3035 __isl_keep isl_multi_val *mv)
3037 struct isl_print_space_data data = { 0 };
3039 if (!mv)
3040 return isl_printer_free(p);
3042 if (isl_space_dim(mv->space, isl_dim_param) > 0) {
3043 p = print_tuple(mv->space, p, isl_dim_param, &data);
3044 p = isl_printer_print_str(p, " -> ");
3046 p = isl_printer_print_str(p, "{ ");
3047 data.print_dim = &print_dim_mv;
3048 data.user = mv;
3049 p = isl_print_space(mv->space, p, 0, &data);
3050 p = isl_printer_print_str(p, " }");
3051 return p;
3054 /* Print the isl_multi_val "mv" to "p".
3056 * Currently only supported in isl format.
3058 __isl_give isl_printer *isl_printer_print_multi_val(
3059 __isl_take isl_printer *p, __isl_keep isl_multi_val *mv)
3061 if (!p || !mv)
3062 return isl_printer_free(p);
3064 if (p->output_format == ISL_FORMAT_ISL)
3065 return print_multi_val_isl(p, mv);
3066 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3067 return isl_printer_free(p));
3070 /* Print dimension "pos" of data->space to "p".
3072 * data->user is assumed to be an isl_multi_union_pw_aff.
3074 * The current dimension is necessarily a set dimension, so
3075 * we print the corresponding isl_union_pw_aff, including
3076 * the braces.
3078 static __isl_give isl_printer *print_union_pw_aff_dim(__isl_take isl_printer *p,
3079 struct isl_print_space_data *data, unsigned pos)
3081 isl_multi_union_pw_aff *mupa = data->user;
3082 isl_union_pw_aff *upa;
3084 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, pos);
3085 p = print_union_pw_aff_body(p, upa);
3086 isl_union_pw_aff_free(upa);
3088 return p;
3091 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3093 static __isl_give isl_printer *print_multi_union_pw_aff_isl(
3094 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3096 struct isl_print_space_data data = { 0 };
3097 isl_space *space;
3099 space = isl_multi_union_pw_aff_get_space(mupa);
3100 if (isl_space_dim(space, isl_dim_param) > 0) {
3101 struct isl_print_space_data space_data = { 0 };
3102 p = print_tuple(space, p, isl_dim_param, &space_data);
3103 p = isl_printer_print_str(p, s_to[0]);
3106 data.print_dim = &print_union_pw_aff_dim;
3107 data.user = mupa;
3109 p = isl_print_space(space, p, 0, &data);
3110 isl_space_free(space);
3112 return p;
3115 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3117 * We currently only support an isl format.
3119 __isl_give isl_printer *isl_printer_print_multi_union_pw_aff(
3120 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3122 if (!p || !mupa)
3123 return isl_printer_free(p);
3125 if (p->output_format == ISL_FORMAT_ISL)
3126 return print_multi_union_pw_aff_isl(p, mupa);
3127 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
3128 "unsupported output format", return isl_printer_free(p));