python: use python 3 exception syntax
[isl.git] / isl_output.c
blobc9c0265c63035f8ef2efedf25d12df6a55477050
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 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1540 __isl_keep isl_space *dim, __isl_keep isl_mat *div,
1541 __isl_take isl_printer *p, int outer)
1543 int i, n, first, print_parens;
1544 struct isl_upoly_rec *rec;
1546 if (!p || !up || !dim || !div)
1547 goto error;
1549 if (isl_upoly_is_cst(up))
1550 return upoly_print_cst(up, p, 1);
1552 rec = isl_upoly_as_rec(up);
1553 if (!rec)
1554 goto error;
1555 n = upoly_rec_n_non_zero(rec);
1556 print_parens = n > 1 ||
1557 (outer && rec->up.var >= isl_space_dim(dim, isl_dim_all));
1558 if (print_parens)
1559 p = isl_printer_print_str(p, "(");
1560 for (i = 0, first = 1; i < rec->n; ++i) {
1561 if (isl_upoly_is_zero(rec->p[i]))
1562 continue;
1563 if (isl_upoly_is_negone(rec->p[i])) {
1564 if (!i)
1565 p = isl_printer_print_str(p, "-1");
1566 else if (first)
1567 p = isl_printer_print_str(p, "-");
1568 else
1569 p = isl_printer_print_str(p, " - ");
1570 } else if (isl_upoly_is_cst(rec->p[i]) &&
1571 !isl_upoly_is_one(rec->p[i]))
1572 p = upoly_print_cst(rec->p[i], p, first);
1573 else {
1574 if (!first)
1575 p = isl_printer_print_str(p, " + ");
1576 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1577 p = upoly_print(rec->p[i], dim, div, p, 0);
1579 first = 0;
1580 if (i == 0)
1581 continue;
1582 if (!isl_upoly_is_one(rec->p[i]) &&
1583 !isl_upoly_is_negone(rec->p[i]))
1584 p = isl_printer_print_str(p, " * ");
1585 p = print_pow(p, dim, div, rec->up.var, i);
1587 if (print_parens)
1588 p = isl_printer_print_str(p, ")");
1589 return p;
1590 error:
1591 isl_printer_free(p);
1592 return NULL;
1595 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1596 __isl_keep isl_qpolynomial *qp)
1598 if (!p || !qp)
1599 goto error;
1600 p = upoly_print(qp->upoly, qp->dim, qp->div, p, 1);
1601 return p;
1602 error:
1603 isl_printer_free(p);
1604 return NULL;
1607 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1608 __isl_keep isl_qpolynomial *qp)
1610 struct isl_print_space_data data = { 0 };
1612 if (!p || !qp)
1613 goto error;
1615 if (isl_space_dim(qp->dim, isl_dim_param) > 0) {
1616 p = print_tuple(qp->dim, p, isl_dim_param, &data);
1617 p = isl_printer_print_str(p, " -> ");
1619 p = isl_printer_print_str(p, "{ ");
1620 if (!isl_space_is_params(qp->dim)) {
1621 p = isl_print_space(qp->dim, p, 0, &data);
1622 p = isl_printer_print_str(p, " -> ");
1624 p = print_qpolynomial(p, qp);
1625 p = isl_printer_print_str(p, " }");
1626 return p;
1627 error:
1628 isl_printer_free(p);
1629 return NULL;
1632 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1633 __isl_keep isl_space *dim, __isl_keep isl_qpolynomial *qp)
1635 isl_int den;
1637 isl_int_init(den);
1638 isl_qpolynomial_get_den(qp, &den);
1639 if (!isl_int_is_one(den)) {
1640 isl_qpolynomial *f;
1641 p = isl_printer_print_str(p, "(");
1642 qp = isl_qpolynomial_copy(qp);
1643 f = isl_qpolynomial_rat_cst_on_domain(isl_space_copy(qp->dim),
1644 den, qp->dim->ctx->one);
1645 qp = isl_qpolynomial_mul(qp, f);
1647 if (qp)
1648 p = upoly_print(qp->upoly, dim, qp->div, p, 0);
1649 else
1650 p = isl_printer_free(p);
1651 if (!isl_int_is_one(den)) {
1652 p = isl_printer_print_str(p, ")/");
1653 p = isl_printer_print_isl_int(p, den);
1654 isl_qpolynomial_free(qp);
1656 isl_int_clear(den);
1657 return p;
1660 __isl_give isl_printer *isl_printer_print_qpolynomial(
1661 __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1663 if (!p || !qp)
1664 goto error;
1666 if (p->output_format == ISL_FORMAT_ISL)
1667 return print_qpolynomial_isl(p, qp);
1668 else if (p->output_format == ISL_FORMAT_C)
1669 return print_qpolynomial_c(p, qp->dim, qp);
1670 else
1671 isl_die(qp->dim->ctx, isl_error_unsupported,
1672 "output format not supported for isl_qpolynomials",
1673 goto error);
1674 error:
1675 isl_printer_free(p);
1676 return NULL;
1679 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1680 unsigned output_format)
1682 isl_printer *p;
1684 if (!qp)
1685 return;
1687 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1688 p = isl_printer_to_file(qp->dim->ctx, out);
1689 p = isl_printer_print_qpolynomial(p, qp);
1690 isl_printer_free(p);
1693 static __isl_give isl_printer *qpolynomial_fold_print(
1694 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1696 int i;
1698 if (fold->type == isl_fold_min)
1699 p = isl_printer_print_str(p, "min");
1700 else if (fold->type == isl_fold_max)
1701 p = isl_printer_print_str(p, "max");
1702 p = isl_printer_print_str(p, "(");
1703 for (i = 0; i < fold->n; ++i) {
1704 if (i)
1705 p = isl_printer_print_str(p, ", ");
1706 p = print_qpolynomial(p, fold->qp[i]);
1708 p = isl_printer_print_str(p, ")");
1709 return p;
1712 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1713 FILE *out, unsigned output_format)
1715 isl_printer *p;
1717 if (!fold)
1718 return;
1720 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1722 p = isl_printer_to_file(fold->dim->ctx, out);
1723 p = isl_printer_print_qpolynomial_fold(p, fold);
1725 isl_printer_free(p);
1728 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1729 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1731 struct isl_print_space_data data = { 0 };
1732 int i = 0;
1734 for (i = 0; i < pwqp->n; ++i) {
1735 isl_space *space;
1737 if (i)
1738 p = isl_printer_print_str(p, "; ");
1739 space = isl_qpolynomial_get_domain_space(pwqp->p[i].qp);
1740 if (!isl_space_is_params(space)) {
1741 p = isl_print_space(space, p, 0, &data);
1742 p = isl_printer_print_str(p, " -> ");
1744 p = print_qpolynomial(p, pwqp->p[i].qp);
1745 p = print_disjuncts(set_to_map(pwqp->p[i].set), space, p, 0);
1746 isl_space_free(space);
1749 return p;
1752 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1753 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1755 struct isl_print_space_data data = { 0 };
1757 if (!p || !pwqp)
1758 goto error;
1760 if (isl_space_dim(pwqp->dim, isl_dim_param) > 0) {
1761 p = print_tuple(pwqp->dim, p, isl_dim_param, &data);
1762 p = isl_printer_print_str(p, " -> ");
1764 p = isl_printer_print_str(p, "{ ");
1765 if (pwqp->n == 0) {
1766 if (!isl_space_is_set(pwqp->dim)) {
1767 p = print_tuple(pwqp->dim, p, isl_dim_in, &data);
1768 p = isl_printer_print_str(p, " -> ");
1770 p = isl_printer_print_str(p, "0");
1772 p = isl_pwqp_print_isl_body(p, pwqp);
1773 p = isl_printer_print_str(p, " }");
1774 return p;
1775 error:
1776 isl_printer_free(p);
1777 return NULL;
1780 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
1781 unsigned output_format)
1783 isl_printer *p;
1785 if (!pwqp)
1786 return;
1788 p = isl_printer_to_file(pwqp->dim->ctx, out);
1789 p = isl_printer_set_output_format(p, output_format);
1790 p = isl_printer_print_pw_qpolynomial(p, pwqp);
1792 isl_printer_free(p);
1795 static __isl_give isl_printer *isl_pwf_print_isl_body(
1796 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1798 struct isl_print_space_data data = { 0 };
1799 int i = 0;
1801 for (i = 0; i < pwf->n; ++i) {
1802 isl_space *space;
1804 if (i)
1805 p = isl_printer_print_str(p, "; ");
1806 space = isl_qpolynomial_fold_get_domain_space(pwf->p[i].fold);
1807 if (!isl_space_is_params(space)) {
1808 p = isl_print_space(space, p, 0, &data);
1809 p = isl_printer_print_str(p, " -> ");
1811 p = qpolynomial_fold_print(pwf->p[i].fold, p);
1812 p = print_disjuncts(set_to_map(pwf->p[i].set), space, p, 0);
1813 isl_space_free(space);
1816 return p;
1819 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
1820 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1822 struct isl_print_space_data data = { 0 };
1824 if (isl_space_dim(pwf->dim, isl_dim_param) > 0) {
1825 p = print_tuple(pwf->dim, p, isl_dim_param, &data);
1826 p = isl_printer_print_str(p, " -> ");
1828 p = isl_printer_print_str(p, "{ ");
1829 if (pwf->n == 0) {
1830 if (!isl_space_is_set(pwf->dim)) {
1831 p = print_tuple(pwf->dim, p, isl_dim_in, &data);
1832 p = isl_printer_print_str(p, " -> ");
1834 p = isl_printer_print_str(p, "0");
1836 p = isl_pwf_print_isl_body(p, pwf);
1837 p = isl_printer_print_str(p, " }");
1838 return p;
1841 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1842 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c);
1844 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1845 __isl_keep isl_space *dim,
1846 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1848 if (type == isl_dim_div) {
1849 p = isl_printer_print_str(p, "floord(");
1850 p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
1851 p = isl_printer_print_str(p, ", ");
1852 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1853 p = isl_printer_print_str(p, ")");
1854 } else {
1855 const char *name;
1857 name = isl_space_get_dim_name(dim, type, pos);
1858 if (!name)
1859 name = "UNNAMED";
1860 p = isl_printer_print_str(p, name);
1862 return p;
1865 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1866 __isl_keep isl_space *dim,
1867 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1869 enum isl_dim_type type;
1871 if (pos == 0)
1872 return isl_printer_print_isl_int(p, c);
1874 if (isl_int_is_one(c))
1876 else if (isl_int_is_negone(c))
1877 p = isl_printer_print_str(p, "-");
1878 else {
1879 p = isl_printer_print_isl_int(p, c);
1880 p = isl_printer_print_str(p, "*");
1882 type = pos2type(dim, &pos);
1883 p = print_name_c(p, dim, bset, type, pos);
1884 return p;
1887 static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
1888 __isl_keep isl_space *dim,
1889 __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
1891 int i;
1892 int first;
1894 for (i = 0, first = 1; i < len; ++i) {
1895 int flip = 0;
1896 if (isl_int_is_zero(c[i]))
1897 continue;
1898 if (!first) {
1899 if (isl_int_is_neg(c[i])) {
1900 flip = 1;
1901 isl_int_neg(c[i], c[i]);
1902 p = isl_printer_print_str(p, " - ");
1903 } else
1904 p = isl_printer_print_str(p, " + ");
1906 first = 0;
1907 p = print_term_c(p, dim, bset, c[i], i);
1908 if (flip)
1909 isl_int_neg(c[i], c[i]);
1911 if (first)
1912 p = isl_printer_print_str(p, "0");
1913 return p;
1916 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1917 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c)
1919 unsigned len = 1 + isl_basic_set_total_dim(bset);
1920 return print_partial_affine_c(p, dim, bset, c, len);
1923 /* We skip the constraint if it is implied by the div expression.
1925 * *first indicates whether this is the first constraint in the conjunction and
1926 * is updated if the constraint is actually printed.
1928 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1929 __isl_keep isl_space *dim,
1930 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int *first)
1932 unsigned o_div;
1933 unsigned n_div;
1934 int div;
1936 o_div = isl_basic_set_offset(bset, isl_dim_div);
1937 n_div = isl_basic_set_dim(bset, isl_dim_div);
1938 div = isl_seq_last_non_zero(c + o_div, n_div);
1939 if (div >= 0 && isl_basic_set_is_div_constraint(bset, c, div))
1940 return p;
1942 if (!*first)
1943 p = isl_printer_print_str(p, " && ");
1945 p = print_affine_c(p, dim, bset, c);
1946 p = isl_printer_print_str(p, " ");
1947 p = isl_printer_print_str(p, op);
1948 p = isl_printer_print_str(p, " 0");
1950 *first = 0;
1952 return p;
1955 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1956 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset)
1958 int i, j;
1959 int first = 1;
1960 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
1961 unsigned total = isl_basic_set_total_dim(bset) - n_div;
1963 for (i = 0; i < bset->n_eq; ++i) {
1964 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
1965 if (j < 0)
1966 p = print_constraint_c(p, dim, bset,
1967 bset->eq[i], "==", &first);
1968 else {
1969 if (i)
1970 p = isl_printer_print_str(p, " && ");
1971 p = isl_printer_print_str(p, "(");
1972 p = print_partial_affine_c(p, dim, bset, bset->eq[i],
1973 1 + total + j);
1974 p = isl_printer_print_str(p, ") % ");
1975 p = isl_printer_print_isl_int(p,
1976 bset->eq[i][1 + total + j]);
1977 p = isl_printer_print_str(p, " == 0");
1978 first = 0;
1981 for (i = 0; i < bset->n_ineq; ++i)
1982 p = print_constraint_c(p, dim, bset, bset->ineq[i], ">=",
1983 &first);
1984 return p;
1987 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
1988 __isl_keep isl_space *dim, __isl_keep isl_set *set)
1990 int i;
1992 if (!set)
1993 return isl_printer_free(p);
1995 if (set->n == 0)
1996 p = isl_printer_print_str(p, "0");
1998 for (i = 0; i < set->n; ++i) {
1999 if (i)
2000 p = isl_printer_print_str(p, " || ");
2001 if (set->n > 1)
2002 p = isl_printer_print_str(p, "(");
2003 p = print_basic_set_c(p, dim, set->p[i]);
2004 if (set->n > 1)
2005 p = isl_printer_print_str(p, ")");
2007 return p;
2010 static __isl_give isl_printer *print_pw_qpolynomial_c(
2011 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2013 int i;
2015 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set))
2016 return print_qpolynomial_c(p, pwqp->dim, pwqp->p[0].qp);
2018 for (i = 0; i < pwqp->n; ++i) {
2019 p = isl_printer_print_str(p, "(");
2020 p = print_set_c(p, pwqp->dim, pwqp->p[i].set);
2021 p = isl_printer_print_str(p, ") ? (");
2022 p = print_qpolynomial_c(p, pwqp->dim, pwqp->p[i].qp);
2023 p = isl_printer_print_str(p, ") : ");
2026 p = isl_printer_print_str(p, "0");
2027 return p;
2030 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
2031 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2033 if (!p || !pwqp)
2034 goto error;
2036 if (p->output_format == ISL_FORMAT_ISL)
2037 return print_pw_qpolynomial_isl(p, pwqp);
2038 else if (p->output_format == ISL_FORMAT_C)
2039 return print_pw_qpolynomial_c(p, pwqp);
2040 isl_assert(p->ctx, 0, goto error);
2041 error:
2042 isl_printer_free(p);
2043 return NULL;
2046 static isl_stat print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
2048 struct isl_union_print_data *data;
2049 data = (struct isl_union_print_data *)user;
2051 if (!data->first)
2052 data->p = isl_printer_print_str(data->p, "; ");
2053 data->first = 0;
2055 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
2056 isl_pw_qpolynomial_free(pwqp);
2058 return isl_stat_ok;
2061 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
2062 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2064 struct isl_union_print_data data;
2065 struct isl_print_space_data space_data = { 0 };
2066 isl_space *dim;
2068 dim = isl_union_pw_qpolynomial_get_space(upwqp);
2069 if (isl_space_dim(dim, isl_dim_param) > 0) {
2070 p = print_tuple(dim, p, isl_dim_param, &space_data);
2071 p = isl_printer_print_str(p, " -> ");
2073 isl_space_free(dim);
2074 p = isl_printer_print_str(p, "{ ");
2075 data.p = p;
2076 data.first = 1;
2077 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
2078 &data);
2079 p = data.p;
2080 p = isl_printer_print_str(p, " }");
2081 return p;
2084 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
2085 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2087 if (!p || !upwqp)
2088 goto error;
2090 if (p->output_format == ISL_FORMAT_ISL)
2091 return print_union_pw_qpolynomial_isl(p, upwqp);
2092 isl_die(p->ctx, isl_error_invalid,
2093 "invalid output format for isl_union_pw_qpolynomial",
2094 goto error);
2095 error:
2096 isl_printer_free(p);
2097 return NULL;
2100 static __isl_give isl_printer *print_qpolynomial_fold_c(
2101 __isl_take isl_printer *p, __isl_keep isl_space *dim,
2102 __isl_keep isl_qpolynomial_fold *fold)
2104 int i;
2106 for (i = 0; i < fold->n - 1; ++i)
2107 if (fold->type == isl_fold_min)
2108 p = isl_printer_print_str(p, "min(");
2109 else if (fold->type == isl_fold_max)
2110 p = isl_printer_print_str(p, "max(");
2112 for (i = 0; i < fold->n; ++i) {
2113 if (i)
2114 p = isl_printer_print_str(p, ", ");
2115 p = print_qpolynomial_c(p, dim, fold->qp[i]);
2116 if (i)
2117 p = isl_printer_print_str(p, ")");
2119 return p;
2122 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
2123 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
2125 if (!p || !fold)
2126 goto error;
2127 if (p->output_format == ISL_FORMAT_ISL)
2128 return qpolynomial_fold_print(fold, p);
2129 else if (p->output_format == ISL_FORMAT_C)
2130 return print_qpolynomial_fold_c(p, fold->dim, fold);
2131 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2132 goto error);
2133 error:
2134 isl_printer_free(p);
2135 return NULL;
2138 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
2139 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2141 int i;
2143 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set))
2144 return print_qpolynomial_fold_c(p, pwf->dim, pwf->p[0].fold);
2146 for (i = 0; i < pwf->n; ++i) {
2147 p = isl_printer_print_str(p, "(");
2148 p = print_set_c(p, pwf->dim, pwf->p[i].set);
2149 p = isl_printer_print_str(p, ") ? (");
2150 p = print_qpolynomial_fold_c(p, pwf->dim, pwf->p[i].fold);
2151 p = isl_printer_print_str(p, ") : ");
2154 p = isl_printer_print_str(p, "0");
2155 return p;
2158 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
2159 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2161 if (!p || !pwf)
2162 goto error;
2164 if (p->output_format == ISL_FORMAT_ISL)
2165 return print_pw_qpolynomial_fold_isl(p, pwf);
2166 else if (p->output_format == ISL_FORMAT_C)
2167 return print_pw_qpolynomial_fold_c(p, pwf);
2168 isl_assert(p->ctx, 0, goto error);
2169 error:
2170 isl_printer_free(p);
2171 return NULL;
2174 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
2175 FILE *out, unsigned output_format)
2177 isl_printer *p;
2179 if (!pwf)
2180 return;
2182 p = isl_printer_to_file(pwf->dim->ctx, out);
2183 p = isl_printer_set_output_format(p, output_format);
2184 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
2186 isl_printer_free(p);
2189 static isl_stat print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf,
2190 void *user)
2192 struct isl_union_print_data *data;
2193 data = (struct isl_union_print_data *)user;
2195 if (!data->first)
2196 data->p = isl_printer_print_str(data->p, "; ");
2197 data->first = 0;
2199 data->p = isl_pwf_print_isl_body(data->p, pwf);
2200 isl_pw_qpolynomial_fold_free(pwf);
2202 return isl_stat_ok;
2205 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
2206 __isl_take isl_printer *p,
2207 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2209 struct isl_union_print_data data;
2210 struct isl_print_space_data space_data = { 0 };
2211 isl_space *dim;
2213 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
2214 if (isl_space_dim(dim, isl_dim_param) > 0) {
2215 p = print_tuple(dim, p, isl_dim_param, &space_data);
2216 p = isl_printer_print_str(p, " -> ");
2218 isl_space_free(dim);
2219 p = isl_printer_print_str(p, "{ ");
2220 data.p = p;
2221 data.first = 1;
2222 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
2223 &print_pwf_body, &data);
2224 p = data.p;
2225 p = isl_printer_print_str(p, " }");
2226 return p;
2229 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2230 __isl_take isl_printer *p,
2231 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2233 if (!p || !upwf)
2234 goto error;
2236 if (p->output_format == ISL_FORMAT_ISL)
2237 return print_union_pw_qpolynomial_fold_isl(p, upwf);
2238 isl_die(p->ctx, isl_error_invalid,
2239 "invalid output format for isl_union_pw_qpolynomial_fold",
2240 goto error);
2241 error:
2242 isl_printer_free(p);
2243 return NULL;
2246 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
2247 __isl_keep isl_constraint *c)
2249 isl_basic_map *bmap;
2251 if (!p || !c)
2252 goto error;
2254 bmap = isl_basic_map_from_constraint(isl_constraint_copy(c));
2255 p = isl_printer_print_basic_map(p, bmap);
2256 isl_basic_map_free(bmap);
2257 return p;
2258 error:
2259 isl_printer_free(p);
2260 return NULL;
2263 static __isl_give isl_printer *isl_printer_print_space_isl(
2264 __isl_take isl_printer *p, __isl_keep isl_space *space)
2266 struct isl_print_space_data data = { 0 };
2268 if (!space)
2269 goto error;
2271 if (isl_space_dim(space, isl_dim_param) > 0) {
2272 p = print_tuple(space, p, isl_dim_param, &data);
2273 p = isl_printer_print_str(p, " -> ");
2276 p = isl_printer_print_str(p, "{ ");
2277 if (isl_space_is_params(space))
2278 p = isl_printer_print_str(p, s_such_that[0]);
2279 else
2280 p = isl_print_space(space, p, 0, &data);
2281 p = isl_printer_print_str(p, " }");
2283 return p;
2284 error:
2285 isl_printer_free(p);
2286 return NULL;
2289 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
2290 __isl_keep isl_space *space)
2292 if (!p || !space)
2293 return isl_printer_free(p);
2294 if (p->output_format == ISL_FORMAT_ISL)
2295 return isl_printer_print_space_isl(p, space);
2296 else if (p->output_format == ISL_FORMAT_OMEGA)
2297 return print_omega_parameters(space, p);
2299 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
2300 "output format not supported for space",
2301 return isl_printer_free(p));
2304 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
2305 __isl_keep isl_local_space *ls)
2307 struct isl_print_space_data data = { 0 };
2308 unsigned n_div;
2310 if (!ls)
2311 goto error;
2313 if (isl_local_space_dim(ls, isl_dim_param) > 0) {
2314 p = print_tuple(ls->dim, p, isl_dim_param, &data);
2315 p = isl_printer_print_str(p, " -> ");
2317 p = isl_printer_print_str(p, "{ ");
2318 p = isl_print_space(ls->dim, p, 0, &data);
2319 n_div = isl_local_space_dim(ls, isl_dim_div);
2320 if (n_div > 0) {
2321 p = isl_printer_print_str(p, " : ");
2322 p = isl_printer_print_str(p, s_open_exists[0]);
2323 p = print_div_list(p, ls->dim, ls->div, 0, 1);
2324 p = isl_printer_print_str(p, s_close_exists[0]);
2325 } else if (isl_space_is_params(ls->dim))
2326 p = isl_printer_print_str(p, s_such_that[0]);
2327 p = isl_printer_print_str(p, " }");
2328 return p;
2329 error:
2330 isl_printer_free(p);
2331 return NULL;
2334 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2335 __isl_keep isl_aff *aff)
2337 unsigned total;
2339 if (isl_aff_is_nan(aff))
2340 return isl_printer_print_str(p, "NaN");
2342 total = isl_local_space_dim(aff->ls, isl_dim_all);
2343 p = isl_printer_print_str(p, "(");
2344 p = print_affine_of_len(aff->ls->dim, aff->ls->div, p,
2345 aff->v->el + 1, 1 + total);
2346 if (isl_int_is_one(aff->v->el[0]))
2347 p = isl_printer_print_str(p, ")");
2348 else {
2349 p = isl_printer_print_str(p, ")/");
2350 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2353 return p;
2356 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2357 __isl_keep isl_aff *aff)
2359 struct isl_print_space_data data = { 0 };
2361 if (isl_space_is_params(aff->ls->dim))
2363 else {
2364 p = print_tuple(aff->ls->dim, p, isl_dim_set, &data);
2365 p = isl_printer_print_str(p, " -> ");
2367 p = isl_printer_print_str(p, "[");
2368 p = print_aff_body(p, aff);
2369 p = isl_printer_print_str(p, "]");
2371 return p;
2374 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2375 __isl_keep isl_aff *aff)
2377 struct isl_print_space_data data = { 0 };
2379 if (!aff)
2380 goto error;
2382 if (isl_local_space_dim(aff->ls, isl_dim_param) > 0) {
2383 p = print_tuple(aff->ls->dim, p, isl_dim_param, &data);
2384 p = isl_printer_print_str(p, " -> ");
2386 p = isl_printer_print_str(p, "{ ");
2387 p = print_aff(p, aff);
2388 p = isl_printer_print_str(p, " }");
2389 return p;
2390 error:
2391 isl_printer_free(p);
2392 return NULL;
2395 /* Print the body of an isl_pw_aff, i.e., a semicolon delimited
2396 * sequence of affine expressions, each followed by constraints.
2398 static __isl_give isl_printer *print_pw_aff_body(
2399 __isl_take isl_printer *p, __isl_keep isl_pw_aff *pa)
2401 int i;
2403 if (!pa)
2404 return isl_printer_free(p);
2406 for (i = 0; i < pa->n; ++i) {
2407 isl_space *space;
2409 if (i)
2410 p = isl_printer_print_str(p, "; ");
2411 p = print_aff(p, pa->p[i].aff);
2412 space = isl_aff_get_domain_space(pa->p[i].aff);
2413 p = print_disjuncts(set_to_map(pa->p[i].set), space, p, 0);
2414 isl_space_free(space);
2417 return p;
2420 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2421 __isl_keep isl_pw_aff *pwaff)
2423 struct isl_print_space_data data = { 0 };
2425 if (!pwaff)
2426 goto error;
2428 if (isl_space_dim(pwaff->dim, isl_dim_param) > 0) {
2429 p = print_tuple(pwaff->dim, p, isl_dim_param, &data);
2430 p = isl_printer_print_str(p, " -> ");
2432 p = isl_printer_print_str(p, "{ ");
2433 p = print_pw_aff_body(p, pwaff);
2434 p = isl_printer_print_str(p, " }");
2435 return p;
2436 error:
2437 isl_printer_free(p);
2438 return NULL;
2441 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2442 __isl_keep isl_local_space *ls, isl_int *c);
2444 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2445 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2447 if (type == isl_dim_div) {
2448 p = isl_printer_print_str(p, "floord(");
2449 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2450 p = isl_printer_print_str(p, ", ");
2451 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2452 p = isl_printer_print_str(p, ")");
2453 } else {
2454 const char *name;
2456 name = isl_space_get_dim_name(ls->dim, type, pos);
2457 if (!name)
2458 name = "UNNAMED";
2459 p = isl_printer_print_str(p, name);
2461 return p;
2464 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2465 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2467 enum isl_dim_type type;
2469 if (pos == 0)
2470 return isl_printer_print_isl_int(p, c);
2472 if (isl_int_is_one(c))
2474 else if (isl_int_is_negone(c))
2475 p = isl_printer_print_str(p, "-");
2476 else {
2477 p = isl_printer_print_isl_int(p, c);
2478 p = isl_printer_print_str(p, "*");
2480 type = pos2type(ls->dim, &pos);
2481 p = print_ls_name_c(p, ls, type, pos);
2482 return p;
2485 static __isl_give isl_printer *print_ls_partial_affine_c(
2486 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2487 isl_int *c, unsigned len)
2489 int i;
2490 int first;
2492 for (i = 0, first = 1; i < len; ++i) {
2493 int flip = 0;
2494 if (isl_int_is_zero(c[i]))
2495 continue;
2496 if (!first) {
2497 if (isl_int_is_neg(c[i])) {
2498 flip = 1;
2499 isl_int_neg(c[i], c[i]);
2500 p = isl_printer_print_str(p, " - ");
2501 } else
2502 p = isl_printer_print_str(p, " + ");
2504 first = 0;
2505 p = print_ls_term_c(p, ls, c[i], i);
2506 if (flip)
2507 isl_int_neg(c[i], c[i]);
2509 if (first)
2510 p = isl_printer_print_str(p, "0");
2511 return p;
2514 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2515 __isl_keep isl_local_space *ls, isl_int *c)
2517 unsigned len = 1 + isl_local_space_dim(ls, isl_dim_all);
2518 return print_ls_partial_affine_c(p, ls, c, len);
2521 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2522 __isl_keep isl_aff *aff)
2524 unsigned total;
2526 total = isl_local_space_dim(aff->ls, isl_dim_all);
2527 if (!isl_int_is_one(aff->v->el[0]))
2528 p = isl_printer_print_str(p, "(");
2529 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2530 if (!isl_int_is_one(aff->v->el[0])) {
2531 p = isl_printer_print_str(p, ")/");
2532 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2534 return p;
2537 /* In the C format, we cannot express that "pwaff" may be undefined
2538 * on parts of the domain space. We therefore assume that the expression
2539 * will only be evaluated on its definition domain and compute the gist
2540 * of each cell with respect to this domain.
2542 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2543 __isl_keep isl_pw_aff *pwaff)
2545 isl_set *domain;
2546 isl_ast_build *build;
2547 isl_ast_expr *expr;
2549 if (pwaff->n < 1)
2550 isl_die(p->ctx, isl_error_unsupported,
2551 "cannot print empty isl_pw_aff in C format",
2552 return isl_printer_free(p));
2554 domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
2555 build = isl_ast_build_from_context(domain);
2556 expr = isl_ast_build_expr_from_pw_aff(build, isl_pw_aff_copy(pwaff));
2557 p = isl_printer_print_ast_expr(p, expr);
2558 isl_ast_expr_free(expr);
2559 isl_ast_build_free(build);
2561 return p;
2564 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2565 __isl_keep isl_aff *aff)
2567 if (!p || !aff)
2568 goto error;
2570 if (p->output_format == ISL_FORMAT_ISL)
2571 return print_aff_isl(p, aff);
2572 else if (p->output_format == ISL_FORMAT_C)
2573 return print_aff_c(p, aff);
2574 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2575 goto error);
2576 error:
2577 isl_printer_free(p);
2578 return NULL;
2581 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2582 __isl_keep isl_pw_aff *pwaff)
2584 if (!p || !pwaff)
2585 goto error;
2587 if (p->output_format == ISL_FORMAT_ISL)
2588 return print_pw_aff_isl(p, pwaff);
2589 else if (p->output_format == ISL_FORMAT_C)
2590 return print_pw_aff_c(p, pwaff);
2591 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2592 goto error);
2593 error:
2594 isl_printer_free(p);
2595 return NULL;
2598 /* Print "pa" in a sequence of isl_pw_affs delimited by semicolons.
2599 * Each isl_pw_aff itself is also printed as semicolon delimited
2600 * sequence of pieces.
2601 * If data->first = 1, then this is the first in the sequence.
2602 * Update data->first to tell the next element that it is not the first.
2604 static isl_stat print_pw_aff_body_wrap(__isl_take isl_pw_aff *pa,
2605 void *user)
2607 struct isl_union_print_data *data;
2608 data = (struct isl_union_print_data *) user;
2610 if (!data->first)
2611 data->p = isl_printer_print_str(data->p, "; ");
2612 data->first = 0;
2614 data->p = print_pw_aff_body(data->p, pa);
2615 isl_pw_aff_free(pa);
2617 return data->p ? isl_stat_ok : isl_stat_error;
2620 /* Print the body of an isl_union_pw_aff, i.e., a semicolon delimited
2621 * sequence of affine expressions, each followed by constraints,
2622 * with the sequence enclosed in braces.
2624 static __isl_give isl_printer *print_union_pw_aff_body(
2625 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2627 struct isl_union_print_data data = { p, 1 };
2629 p = isl_printer_print_str(p, s_open_set[0]);
2630 data.p = p;
2631 if (isl_union_pw_aff_foreach_pw_aff(upa,
2632 &print_pw_aff_body_wrap, &data) < 0)
2633 data.p = isl_printer_free(p);
2634 p = data.p;
2635 p = isl_printer_print_str(p, s_close_set[0]);
2637 return p;
2640 /* Print the isl_union_pw_aff "upa" to "p" in isl format.
2642 * The individual isl_pw_affs are delimited by a semicolon.
2644 static __isl_give isl_printer *print_union_pw_aff_isl(
2645 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2647 struct isl_print_space_data data = { 0 };
2648 isl_space *space;
2650 space = isl_union_pw_aff_get_space(upa);
2651 if (isl_space_dim(space, isl_dim_param) > 0) {
2652 p = print_tuple(space, p, isl_dim_param, &data);
2653 p = isl_printer_print_str(p, s_to[0]);
2655 isl_space_free(space);
2656 p = print_union_pw_aff_body(p, upa);
2657 return p;
2660 /* Print the isl_union_pw_aff "upa" to "p".
2662 * We currently only support an isl format.
2664 __isl_give isl_printer *isl_printer_print_union_pw_aff(
2665 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2667 if (!p || !upa)
2668 return isl_printer_free(p);
2670 if (p->output_format == ISL_FORMAT_ISL)
2671 return print_union_pw_aff_isl(p, upa);
2672 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2673 "unsupported output format", return isl_printer_free(p));
2676 /* Print dimension "pos" of data->space to "p".
2678 * data->user is assumed to be an isl_multi_aff.
2680 * If the current dimension is an output dimension, then print
2681 * the corresponding expression. Otherwise, print the name of the dimension.
2683 static __isl_give isl_printer *print_dim_ma(__isl_take isl_printer *p,
2684 struct isl_print_space_data *data, unsigned pos)
2686 isl_multi_aff *ma = data->user;
2688 if (data->type == isl_dim_out)
2689 p = print_aff_body(p, ma->p[pos]);
2690 else
2691 p = print_name(data->space, p, data->type, pos, data->latex);
2693 return p;
2696 static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
2697 __isl_keep isl_multi_aff *maff)
2699 struct isl_print_space_data data = { 0 };
2701 data.print_dim = &print_dim_ma;
2702 data.user = maff;
2703 return isl_print_space(maff->space, p, 0, &data);
2706 static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
2707 __isl_keep isl_multi_aff *maff)
2709 struct isl_print_space_data data = { 0 };
2711 if (!maff)
2712 goto error;
2714 if (isl_space_dim(maff->space, isl_dim_param) > 0) {
2715 p = print_tuple(maff->space, p, isl_dim_param, &data);
2716 p = isl_printer_print_str(p, " -> ");
2718 p = isl_printer_print_str(p, "{ ");
2719 p = print_multi_aff(p, maff);
2720 p = isl_printer_print_str(p, " }");
2721 return p;
2722 error:
2723 isl_printer_free(p);
2724 return NULL;
2727 __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
2728 __isl_keep isl_multi_aff *maff)
2730 if (!p || !maff)
2731 goto error;
2733 if (p->output_format == ISL_FORMAT_ISL)
2734 return print_multi_aff_isl(p, maff);
2735 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2736 goto error);
2737 error:
2738 isl_printer_free(p);
2739 return NULL;
2742 static __isl_give isl_printer *print_pw_multi_aff_body(
2743 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2745 int i;
2747 if (!pma)
2748 goto error;
2750 for (i = 0; i < pma->n; ++i) {
2751 isl_space *space;
2753 if (i)
2754 p = isl_printer_print_str(p, "; ");
2755 p = print_multi_aff(p, pma->p[i].maff);
2756 space = isl_multi_aff_get_domain_space(pma->p[i].maff);
2757 p = print_disjuncts(set_to_map(pma->p[i].set), space, p, 0);
2758 isl_space_free(space);
2760 return p;
2761 error:
2762 isl_printer_free(p);
2763 return NULL;
2766 static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
2767 __isl_keep isl_pw_multi_aff *pma)
2769 struct isl_print_space_data data = { 0 };
2771 if (!pma)
2772 goto error;
2774 if (isl_space_dim(pma->dim, isl_dim_param) > 0) {
2775 p = print_tuple(pma->dim, p, isl_dim_param, &data);
2776 p = isl_printer_print_str(p, " -> ");
2778 p = isl_printer_print_str(p, "{ ");
2779 p = print_pw_multi_aff_body(p, pma);
2780 p = isl_printer_print_str(p, " }");
2781 return p;
2782 error:
2783 isl_printer_free(p);
2784 return NULL;
2787 static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
2788 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2790 int i;
2792 for (i = 0; i < pma->n - 1; ++i) {
2793 p = isl_printer_print_str(p, "(");
2794 p = print_set_c(p, pma->dim, pma->p[i].set);
2795 p = isl_printer_print_str(p, ") ? (");
2796 p = print_aff_c(p, pma->p[i].maff->p[0]);
2797 p = isl_printer_print_str(p, ") : ");
2800 return print_aff_c(p, pma->p[pma->n - 1].maff->p[0]);
2803 static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
2804 __isl_keep isl_pw_multi_aff *pma)
2806 int n;
2807 const char *name;
2809 if (!pma)
2810 goto error;
2811 if (pma->n < 1)
2812 isl_die(p->ctx, isl_error_unsupported,
2813 "cannot print empty isl_pw_multi_aff in C format",
2814 goto error);
2815 name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
2816 if (!name && isl_pw_multi_aff_dim(pma, isl_dim_out) == 1)
2817 return print_unnamed_pw_multi_aff_c(p, pma);
2818 if (!name)
2819 isl_die(p->ctx, isl_error_unsupported,
2820 "cannot print unnamed isl_pw_multi_aff in C format",
2821 goto error);
2823 p = isl_printer_print_str(p, name);
2824 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
2825 if (n != 0)
2826 isl_die(p->ctx, isl_error_unsupported,
2827 "not supported yet", goto error);
2829 return p;
2830 error:
2831 isl_printer_free(p);
2832 return NULL;
2835 __isl_give isl_printer *isl_printer_print_pw_multi_aff(
2836 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2838 if (!p || !pma)
2839 goto error;
2841 if (p->output_format == ISL_FORMAT_ISL)
2842 return print_pw_multi_aff_isl(p, pma);
2843 if (p->output_format == ISL_FORMAT_C)
2844 return print_pw_multi_aff_c(p, pma);
2845 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2846 goto error);
2847 error:
2848 isl_printer_free(p);
2849 return NULL;
2852 static isl_stat print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
2853 void *user)
2855 struct isl_union_print_data *data;
2856 data = (struct isl_union_print_data *) user;
2858 if (!data->first)
2859 data->p = isl_printer_print_str(data->p, "; ");
2860 data->first = 0;
2862 data->p = print_pw_multi_aff_body(data->p, pma);
2863 isl_pw_multi_aff_free(pma);
2865 return isl_stat_ok;
2868 static __isl_give isl_printer *print_union_pw_multi_aff_isl(
2869 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2871 struct isl_union_print_data data;
2872 struct isl_print_space_data space_data = { 0 };
2873 isl_space *space;
2875 space = isl_union_pw_multi_aff_get_space(upma);
2876 if (isl_space_dim(space, isl_dim_param) > 0) {
2877 p = print_tuple(space, p, isl_dim_param, &space_data);
2878 p = isl_printer_print_str(p, s_to[0]);
2880 isl_space_free(space);
2881 p = isl_printer_print_str(p, s_open_set[0]);
2882 data.p = p;
2883 data.first = 1;
2884 isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
2885 &print_pw_multi_aff_body_wrap, &data);
2886 p = data.p;
2887 p = isl_printer_print_str(p, s_close_set[0]);
2888 return p;
2891 __isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
2892 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2894 if (!p || !upma)
2895 goto error;
2897 if (p->output_format == ISL_FORMAT_ISL)
2898 return print_union_pw_multi_aff_isl(p, upma);
2899 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2900 goto error);
2901 error:
2902 isl_printer_free(p);
2903 return NULL;
2906 /* Print dimension "pos" of data->space to "p".
2908 * data->user is assumed to be an isl_multi_pw_aff.
2910 * If the current dimension is an output dimension, then print
2911 * the corresponding piecewise affine expression.
2912 * Otherwise, print the name of the dimension.
2914 static __isl_give isl_printer *print_dim_mpa(__isl_take isl_printer *p,
2915 struct isl_print_space_data *data, unsigned pos)
2917 int i;
2918 int need_parens;
2919 isl_multi_pw_aff *mpa = data->user;
2920 isl_pw_aff *pa;
2922 if (data->type != isl_dim_out)
2923 return print_name(data->space, p, data->type, pos, data->latex);
2925 pa = mpa->p[pos];
2926 if (pa->n == 0)
2927 return isl_printer_print_str(p, "(0 : 1 = 0)");
2929 need_parens = pa->n != 1 || !isl_set_plain_is_universe(pa->p[0].set);
2930 if (need_parens)
2931 p = isl_printer_print_str(p, "(");
2932 for (i = 0; i < pa->n; ++i) {
2933 isl_space *space;
2935 if (i)
2936 p = isl_printer_print_str(p, "; ");
2937 p = print_aff_body(p, pa->p[i].aff);
2938 space = isl_aff_get_domain_space(pa->p[i].aff);
2939 p = print_disjuncts(pa->p[i].set, space, p, 0);
2940 isl_space_free(space);
2942 if (need_parens)
2943 p = isl_printer_print_str(p, ")");
2945 return p;
2948 /* Print "mpa" to "p" in isl format.
2950 static __isl_give isl_printer *print_multi_pw_aff_isl(__isl_take isl_printer *p,
2951 __isl_keep isl_multi_pw_aff *mpa)
2953 struct isl_print_space_data data = { 0 };
2955 if (!mpa)
2956 return isl_printer_free(p);
2958 if (isl_space_dim(mpa->space, isl_dim_param) > 0) {
2959 p = print_tuple(mpa->space, p, isl_dim_param, &data);
2960 p = isl_printer_print_str(p, " -> ");
2962 p = isl_printer_print_str(p, "{ ");
2963 data.print_dim = &print_dim_mpa;
2964 data.user = mpa;
2965 p = isl_print_space(mpa->space, p, 0, &data);
2966 p = isl_printer_print_str(p, " }");
2967 return p;
2970 __isl_give isl_printer *isl_printer_print_multi_pw_aff(
2971 __isl_take isl_printer *p, __isl_keep isl_multi_pw_aff *mpa)
2973 if (!p || !mpa)
2974 return isl_printer_free(p);
2976 if (p->output_format == ISL_FORMAT_ISL)
2977 return print_multi_pw_aff_isl(p, mpa);
2978 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2979 return isl_printer_free(p));
2982 /* Print dimension "pos" of data->space to "p".
2984 * data->user is assumed to be an isl_multi_val.
2986 * If the current dimension is an output dimension, then print
2987 * the corresponding value. Otherwise, print the name of the dimension.
2989 static __isl_give isl_printer *print_dim_mv(__isl_take isl_printer *p,
2990 struct isl_print_space_data *data, unsigned pos)
2992 isl_multi_val *mv = data->user;
2994 if (data->type == isl_dim_out)
2995 return isl_printer_print_val(p, mv->p[pos]);
2996 else
2997 return print_name(data->space, p, data->type, pos, data->latex);
3000 /* Print the isl_multi_val "mv" to "p" in isl format.
3002 static __isl_give isl_printer *print_multi_val_isl(__isl_take isl_printer *p,
3003 __isl_keep isl_multi_val *mv)
3005 struct isl_print_space_data data = { 0 };
3007 if (!mv)
3008 return isl_printer_free(p);
3010 if (isl_space_dim(mv->space, isl_dim_param) > 0) {
3011 p = print_tuple(mv->space, p, isl_dim_param, &data);
3012 p = isl_printer_print_str(p, " -> ");
3014 p = isl_printer_print_str(p, "{ ");
3015 data.print_dim = &print_dim_mv;
3016 data.user = mv;
3017 p = isl_print_space(mv->space, p, 0, &data);
3018 p = isl_printer_print_str(p, " }");
3019 return p;
3022 /* Print the isl_multi_val "mv" to "p".
3024 * Currently only supported in isl format.
3026 __isl_give isl_printer *isl_printer_print_multi_val(
3027 __isl_take isl_printer *p, __isl_keep isl_multi_val *mv)
3029 if (!p || !mv)
3030 return isl_printer_free(p);
3032 if (p->output_format == ISL_FORMAT_ISL)
3033 return print_multi_val_isl(p, mv);
3034 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3035 return isl_printer_free(p));
3038 /* Print dimension "pos" of data->space to "p".
3040 * data->user is assumed to be an isl_multi_union_pw_aff.
3042 * The current dimension is necessarily a set dimension, so
3043 * we print the corresponding isl_union_pw_aff, including
3044 * the braces.
3046 static __isl_give isl_printer *print_union_pw_aff_dim(__isl_take isl_printer *p,
3047 struct isl_print_space_data *data, unsigned pos)
3049 isl_multi_union_pw_aff *mupa = data->user;
3050 isl_union_pw_aff *upa;
3052 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, pos);
3053 p = print_union_pw_aff_body(p, upa);
3054 isl_union_pw_aff_free(upa);
3056 return p;
3059 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3061 static __isl_give isl_printer *print_multi_union_pw_aff_isl(
3062 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3064 struct isl_print_space_data data = { 0 };
3065 isl_space *space;
3067 space = isl_multi_union_pw_aff_get_space(mupa);
3068 if (isl_space_dim(space, isl_dim_param) > 0) {
3069 struct isl_print_space_data space_data = { 0 };
3070 p = print_tuple(space, p, isl_dim_param, &space_data);
3071 p = isl_printer_print_str(p, s_to[0]);
3074 data.print_dim = &print_union_pw_aff_dim;
3075 data.user = mupa;
3077 p = isl_print_space(space, p, 0, &data);
3078 isl_space_free(space);
3080 return p;
3083 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3085 * We currently only support an isl format.
3087 __isl_give isl_printer *isl_printer_print_multi_union_pw_aff(
3088 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3090 if (!p || !mupa)
3091 return isl_printer_free(p);
3093 if (p->output_format == ISL_FORMAT_ISL)
3094 return print_multi_union_pw_aff_isl(p, mupa);
3095 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
3096 "unsupported output format", return isl_printer_free(p));