isl_vertices.c: tab_for_shifted_cone: allocate room for equality constraints
[isl.git] / isl_output.c
blob62883ed6cc288539e5fcf6f648c77769c0209e78
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <stdlib.h>
16 #include <string.h>
17 #include <isl_ctx_private.h>
18 #include <isl_map_private.h>
19 #include <isl/set.h>
20 #include <isl_seq.h>
21 #include <isl_polynomial_private.h>
22 #include <isl_printer_private.h>
23 #include <isl_space_private.h>
24 #include <isl_mat_private.h>
25 #include <isl_vec_private.h>
26 #include <isl/union_map.h>
27 #include <isl/constraint.h>
28 #include <isl_local_space_private.h>
29 #include <isl_aff_private.h>
30 #include <isl_val_private.h>
31 #include <isl/ast_build.h>
32 #include <isl_sort.h>
33 #include <isl_output_private.h>
35 static const char *s_to[2] = { " -> ", " \\to " };
36 static const char *s_and[2] = { " and ", " \\wedge " };
37 static const char *s_or[2] = { " or ", " \\vee " };
38 static const char *s_le[2] = { "<=", "\\le" };
39 static const char *s_ge[2] = { ">=", "\\ge" };
40 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
41 static const char *s_close_set[2] = { " }", " \\,\\}" };
42 static const char *s_open_list[2] = { "[", "(" };
43 static const char *s_close_list[2] = { "]", ")" };
44 static const char *s_such_that[2] = { " : ", " \\mid " };
45 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
46 static const char *s_close_exists[2] = { ")", "" };
47 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
48 static const char *s_param_prefix[2] = { "p", "p_" };
49 static const char *s_input_prefix[2] = { "i", "i_" };
50 static const char *s_output_prefix[2] = { "o", "o_" };
52 static __isl_give isl_printer *print_constraint_polylib(
53 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
55 int i;
56 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
57 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
58 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
59 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
61 p = isl_printer_start_line(p);
62 p = isl_printer_print_int(p, ineq);
63 for (i = 0; i < n_out; ++i) {
64 p = isl_printer_print_str(p, " ");
65 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
67 for (i = 0; i < n_in; ++i) {
68 p = isl_printer_print_str(p, " ");
69 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
71 for (i = 0; i < bmap->n_div; ++i) {
72 p = isl_printer_print_str(p, " ");
73 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
75 for (i = 0; i < nparam; ++i) {
76 p = isl_printer_print_str(p, " ");
77 p = isl_printer_print_isl_int(p, c[1+i]);
79 p = isl_printer_print_str(p, " ");
80 p = isl_printer_print_isl_int(p, c[0]);
81 p = isl_printer_end_line(p);
82 return p;
85 static __isl_give isl_printer *print_constraints_polylib(
86 struct isl_basic_map *bmap, __isl_take isl_printer *p)
88 int i;
90 p = isl_printer_set_isl_int_width(p, 5);
92 for (i = 0; i < bmap->n_eq; ++i)
93 p = print_constraint_polylib(bmap, 0, i, p);
94 for (i = 0; i < bmap->n_ineq; ++i)
95 p = print_constraint_polylib(bmap, 1, i, p);
97 return p;
100 static __isl_give isl_printer *bset_print_constraints_polylib(
101 struct isl_basic_set *bset, __isl_take isl_printer *p)
103 return print_constraints_polylib((struct isl_basic_map *)bset, p);
106 static __isl_give isl_printer *isl_basic_map_print_polylib(
107 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
109 unsigned total = isl_basic_map_total_dim(bmap);
110 p = isl_printer_start_line(p);
111 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
112 p = isl_printer_print_str(p, " ");
113 p = isl_printer_print_int(p, 1 + total + 1);
114 if (ext) {
115 p = isl_printer_print_str(p, " ");
116 p = isl_printer_print_int(p,
117 isl_basic_map_dim(bmap, isl_dim_out));
118 p = isl_printer_print_str(p, " ");
119 p = isl_printer_print_int(p,
120 isl_basic_map_dim(bmap, isl_dim_in));
121 p = isl_printer_print_str(p, " ");
122 p = isl_printer_print_int(p,
123 isl_basic_map_dim(bmap, isl_dim_div));
124 p = isl_printer_print_str(p, " ");
125 p = isl_printer_print_int(p,
126 isl_basic_map_dim(bmap, isl_dim_param));
128 p = isl_printer_end_line(p);
129 return print_constraints_polylib(bmap, p);
132 static __isl_give isl_printer *isl_basic_set_print_polylib(
133 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
135 return isl_basic_map_print_polylib((struct isl_basic_map *)bset, p, ext);
138 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
139 __isl_take isl_printer *p, int ext)
141 int i;
143 p = isl_printer_start_line(p);
144 p = isl_printer_print_int(p, map->n);
145 p = isl_printer_end_line(p);
146 for (i = 0; i < map->n; ++i) {
147 p = isl_printer_start_line(p);
148 p = isl_printer_end_line(p);
149 p = isl_basic_map_print_polylib(map->p[i], p, ext);
151 return p;
154 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
155 __isl_take isl_printer *p, int ext)
157 return isl_map_print_polylib((struct isl_map *)set, p, ext);
160 static int count_same_name(__isl_keep isl_space *dim,
161 enum isl_dim_type type, unsigned pos, const char *name)
163 enum isl_dim_type t;
164 unsigned p, s;
165 int count = 0;
167 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
168 s = t == type ? pos : isl_space_dim(dim, t);
169 for (p = 0; p < s; ++p) {
170 const char *n = isl_space_get_dim_name(dim, t, p);
171 if (n && !strcmp(n, name))
172 count++;
175 return count;
178 /* Print the name of the variable of type "type" and position "pos"
179 * in "space" to "p".
181 static __isl_give isl_printer *print_name(__isl_keep isl_space *space,
182 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
183 int latex)
185 const char *name;
186 char buffer[20];
187 int primes;
189 name = type == isl_dim_div ? NULL
190 : isl_space_get_dim_name(space, type, pos);
192 if (!name) {
193 const char *prefix;
194 if (type == isl_dim_param)
195 prefix = s_param_prefix[latex];
196 else if (type == isl_dim_div)
197 prefix = s_div_prefix[latex];
198 else if (isl_space_is_set(space) || type == isl_dim_in)
199 prefix = s_input_prefix[latex];
200 else
201 prefix = s_output_prefix[latex];
202 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
203 name = buffer;
205 primes = count_same_name(space, name == buffer ? isl_dim_div : type,
206 pos, name);
207 p = isl_printer_print_str(p, name);
208 while (primes-- > 0)
209 p = isl_printer_print_str(p, "'");
210 return p;
213 static enum isl_dim_type pos2type(__isl_keep isl_space *dim, unsigned *pos)
215 enum isl_dim_type type;
216 unsigned n_in = isl_space_dim(dim, isl_dim_in);
217 unsigned n_out = isl_space_dim(dim, isl_dim_out);
218 unsigned nparam = isl_space_dim(dim, isl_dim_param);
220 if (*pos < 1 + nparam) {
221 type = isl_dim_param;
222 *pos -= 1;
223 } else if (*pos < 1 + nparam + n_in) {
224 type = isl_dim_in;
225 *pos -= 1 + nparam;
226 } else if (*pos < 1 + nparam + n_in + n_out) {
227 type = isl_dim_out;
228 *pos -= 1 + nparam + n_in;
229 } else {
230 type = isl_dim_div;
231 *pos -= 1 + nparam + n_in + n_out;
234 return type;
237 /* Can the div expression of the integer division at position "row" of "div"
238 * be printed?
239 * In particular, are the div expressions available and does the selected
240 * variable have a known explicit representation?
241 * Furthermore, the Omega format does not allow and div expressions
242 * to be printed.
244 static isl_bool can_print_div_expr(__isl_keep isl_printer *p,
245 __isl_keep isl_mat *div, int pos)
247 if (p->output_format == ISL_FORMAT_OMEGA)
248 return isl_bool_false;
249 if (!div)
250 return isl_bool_false;
251 return !isl_int_is_zero(div->row[pos][0]);
254 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
255 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
257 static __isl_give isl_printer *print_term(__isl_keep isl_space *space,
258 __isl_keep isl_mat *div,
259 isl_int c, unsigned pos, __isl_take isl_printer *p, int latex)
261 enum isl_dim_type type;
262 int print_div_def;
264 if (pos == 0)
265 return isl_printer_print_isl_int(p, c);
267 type = pos2type(space, &pos);
268 print_div_def = type == isl_dim_div && can_print_div_expr(p, div, pos);
270 if (isl_int_is_one(c))
272 else if (isl_int_is_negone(c))
273 p = isl_printer_print_str(p, "-");
274 else {
275 p = isl_printer_print_isl_int(p, c);
276 if (p->output_format == ISL_FORMAT_C || print_div_def)
277 p = isl_printer_print_str(p, "*");
279 if (print_div_def)
280 p = print_div(space, div, pos, p);
281 else
282 p = print_name(space, p, type, pos, latex);
283 return p;
286 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_space *dim,
287 __isl_keep isl_mat *div,
288 __isl_take isl_printer *p, isl_int *c, int len)
290 int i;
291 int first;
293 for (i = 0, first = 1; i < len; ++i) {
294 int flip = 0;
295 if (isl_int_is_zero(c[i]))
296 continue;
297 if (!first) {
298 if (isl_int_is_neg(c[i])) {
299 flip = 1;
300 isl_int_neg(c[i], c[i]);
301 p = isl_printer_print_str(p, " - ");
302 } else
303 p = isl_printer_print_str(p, " + ");
305 first = 0;
306 p = print_term(dim, div, c[i], i, p, 0);
307 if (flip)
308 isl_int_neg(c[i], c[i]);
310 if (first)
311 p = isl_printer_print_str(p, "0");
312 return p;
315 /* Print an affine expression "c" corresponding to a constraint in "bmap"
316 * to "p", with the variable names taken from "space" and
317 * the integer division definitions taken from "div".
319 static __isl_give isl_printer *print_affine(__isl_keep isl_basic_map *bmap,
320 __isl_keep isl_space *space, __isl_keep isl_mat *div,
321 __isl_take isl_printer *p, isl_int *c)
323 unsigned len = 1 + isl_basic_map_total_dim(bmap);
324 return print_affine_of_len(space, div, p, c, len);
327 /* offset is the offset of local_dim inside data->type of data->space.
329 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
330 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
331 struct isl_print_space_data *data, int offset)
333 int i;
335 if (data->space != local_dim && local_type == isl_dim_out)
336 offset += local_dim->n_in;
338 for (i = 0; i < isl_space_dim(local_dim, local_type); ++i) {
339 if (i)
340 p = isl_printer_print_str(p, ", ");
341 if (data->print_dim)
342 p = data->print_dim(p, data, offset + i);
343 else
344 p = print_name(data->space, p, data->type, offset + i,
345 data->latex);
347 return p;
350 static __isl_give isl_printer *print_var_list(__isl_take isl_printer *p,
351 __isl_keep isl_space *space, enum isl_dim_type type)
353 struct isl_print_space_data data = { .space = space, .type = type };
355 return print_nested_var_list(p, space, type, &data, 0);
358 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
359 __isl_keep isl_space *local_dim,
360 struct isl_print_space_data *data, int offset);
362 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
363 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
364 struct isl_print_space_data *data, int offset)
366 const char *name = NULL;
367 unsigned n = isl_space_dim(local_dim, local_type);
368 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
369 name = isl_space_get_tuple_name(local_dim, local_type);
370 if (name) {
371 if (data->latex)
372 p = isl_printer_print_str(p, "\\mathrm{");
373 p = isl_printer_print_str(p, name);
374 if (data->latex)
375 p = isl_printer_print_str(p, "}");
378 if (!data->latex || n != 1 || name)
379 p = isl_printer_print_str(p, s_open_list[data->latex]);
380 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
381 local_dim->nested[local_type - isl_dim_in]) {
382 if (data->space != local_dim && local_type == isl_dim_out)
383 offset += local_dim->n_in;
384 p = print_nested_map_dim(p,
385 local_dim->nested[local_type - isl_dim_in],
386 data, offset);
387 } else
388 p = print_nested_var_list(p, local_dim, local_type, data,
389 offset);
390 if (!data->latex || n != 1 || name)
391 p = isl_printer_print_str(p, s_close_list[data->latex]);
392 return p;
395 static __isl_give isl_printer *print_tuple(__isl_keep isl_space *dim,
396 __isl_take isl_printer *p, enum isl_dim_type type,
397 struct isl_print_space_data *data)
399 data->space = dim;
400 data->type = type;
401 return print_nested_tuple(p, dim, type, data, 0);
404 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
405 __isl_keep isl_space *local_dim,
406 struct isl_print_space_data *data, int offset)
408 p = print_nested_tuple(p, local_dim, isl_dim_in, data, offset);
409 p = isl_printer_print_str(p, s_to[data->latex]);
410 p = print_nested_tuple(p, local_dim, isl_dim_out, data, offset);
412 return p;
415 __isl_give isl_printer *isl_print_space(__isl_keep isl_space *space,
416 __isl_take isl_printer *p, int rational,
417 struct isl_print_space_data *data)
419 if (rational && !data->latex)
420 p = isl_printer_print_str(p, "rat: ");
421 if (isl_space_is_params(space))
423 else if (isl_space_is_set(space))
424 p = print_tuple(space, p, isl_dim_set, data);
425 else {
426 p = print_tuple(space, p, isl_dim_in, data);
427 p = isl_printer_print_str(p, s_to[data->latex]);
428 p = print_tuple(space, p, isl_dim_out, data);
431 return p;
434 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_space *dim,
435 __isl_take isl_printer *p)
437 if (isl_space_dim(dim, isl_dim_param) == 0)
438 return p;
440 p = isl_printer_start_line(p);
441 p = isl_printer_print_str(p, "symbolic ");
442 p = print_var_list(p, dim, isl_dim_param);
443 p = isl_printer_print_str(p, ";");
444 p = isl_printer_end_line(p);
445 return p;
448 /* Does the inequality constraint following "i" in "bmap"
449 * have an opposite value for the same last coefficient?
450 * "last" is the position of the last coefficient of inequality "i".
451 * If the next constraint is a div constraint, then it is ignored
452 * since div constraints are not printed.
454 static int next_is_opposite(__isl_keep isl_basic_map *bmap, int i, int last)
456 unsigned total = isl_basic_map_total_dim(bmap);
457 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
459 if (i + 1 >= bmap->n_ineq)
460 return 0;
461 if (isl_seq_last_non_zero(bmap->ineq[i + 1], 1 + total) != last)
462 return 0;
463 if (last >= o_div &&
464 isl_basic_map_is_div_constraint(bmap, bmap->ineq[i + 1],
465 last - o_div))
466 return 0;
467 return isl_int_abs_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]) &&
468 !isl_int_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]);
471 /* Return a string representation of the operator used when
472 * printing a constraint where the LHS is greater than or equal to the LHS
473 * (sign > 0) or smaller than or equal to the LHS (sign < 0).
474 * If "strict" is set, then return the strict version of the comparison
475 * operator.
477 static const char *constraint_op(int sign, int strict, int latex)
479 if (strict)
480 return sign < 0 ? "<" : ">";
481 if (sign < 0)
482 return s_le[latex];
483 else
484 return s_ge[latex];
487 /* Print one side of a constraint "c" from "bmap" to "p", with
488 * the variable names taken from "space" and the integer division definitions
489 * taken from "div".
490 * "last" is the position of the last non-zero coefficient.
491 * Let c' be the result of zeroing out this coefficient, then
492 * the partial constraint
494 * c' op
496 * is printed.
497 * "first_constraint" is set if this is the first constraint
498 * in the conjunction.
500 static __isl_give isl_printer *print_half_constraint(struct isl_basic_map *bmap,
501 __isl_keep isl_space *space, __isl_keep isl_mat *div,
502 __isl_take isl_printer *p, isl_int *c, int last, const char *op,
503 int first_constraint, int latex)
505 if (!first_constraint)
506 p = isl_printer_print_str(p, s_and[latex]);
508 isl_int_set_si(c[last], 0);
509 p = print_affine(bmap, space, div, p, c);
511 p = isl_printer_print_str(p, " ");
512 p = isl_printer_print_str(p, op);
513 p = isl_printer_print_str(p, " ");
515 return p;
518 /* Print a constraint "c" from "bmap" to "p", with the variable names
519 * taken from "space" and the integer division definitions taken from "div".
520 * "last" is the position of the last non-zero coefficient, which is
521 * moreover assumed to be negative.
522 * Let c' be the result of zeroing out this coefficient, then
523 * the constraint is printed in the form
525 * -c[last] op c'
527 * "first_constraint" is set if this is the first constraint
528 * in the conjunction.
530 static __isl_give isl_printer *print_constraint(struct isl_basic_map *bmap,
531 __isl_keep isl_space *space, __isl_keep isl_mat *div,
532 __isl_take isl_printer *p,
533 isl_int *c, int last, const char *op, int first_constraint, int latex)
535 if (!first_constraint)
536 p = isl_printer_print_str(p, s_and[latex]);
538 isl_int_abs(c[last], c[last]);
540 p = print_term(space, div, c[last], last, p, latex);
542 p = isl_printer_print_str(p, " ");
543 p = isl_printer_print_str(p, op);
544 p = isl_printer_print_str(p, " ");
546 isl_int_set_si(c[last], 0);
547 p = print_affine(bmap, space, div, p, c);
549 return p;
552 /* Print the constraints of "bmap" to "p".
553 * The names of the variables are taken from "space" and
554 * the integer division definitions are taken from "div".
555 * Div constraints are only printed in "dump" mode.
556 * The constraints are sorted prior to printing (except in "dump" mode).
558 * If x is the last variable with a non-zero coefficient,
559 * then a lower bound
561 * f - a x >= 0
563 * is printed as
565 * a x <= f
567 * while an upper bound
569 * f + a x >= 0
571 * is printed as
573 * a x >= -f
575 * If the next constraint has an opposite sign for the same last coefficient,
576 * then it is printed as
578 * f >= a x
580 * or
582 * -f <= a x
584 * instead. In fact, the "a x" part is not printed explicitly, but
585 * reused from the next constraint, which is therefore treated as
586 * a first constraint in the conjunction.
588 * If the constant term of "f" is -1, then "f" is replaced by "f + 1" and
589 * the comparison operator is replaced by the strict variant.
590 * Essentially, ">= 1" is replaced by "> 0".
592 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
593 __isl_keep isl_space *space, __isl_keep isl_mat *div,
594 __isl_take isl_printer *p, int latex)
596 int i;
597 isl_vec *c = NULL;
598 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
599 unsigned total = isl_basic_map_total_dim(bmap);
600 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
601 int first = 1;
602 int dump;
604 if (!p)
605 return NULL;
606 bmap = isl_basic_map_copy(bmap);
607 dump = p->dump;
608 if (!dump)
609 bmap = isl_basic_map_sort_constraints(bmap);
610 if (!bmap)
611 goto error;
613 c = isl_vec_alloc(bmap->ctx, 1 + total);
614 if (!c)
615 goto error;
617 for (i = bmap->n_eq - 1; i >= 0; --i) {
618 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
619 if (l < 0) {
620 if (i != bmap->n_eq - 1)
621 p = isl_printer_print_str(p, s_and[latex]);
622 p = isl_printer_print_str(p, "0 = 0");
623 continue;
625 if (isl_int_is_neg(bmap->eq[i][l]))
626 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
627 else
628 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
629 p = print_constraint(bmap, space, div, p, c->el, l,
630 "=", first, latex);
631 first = 0;
633 for (i = 0; i < bmap->n_ineq; ++i) {
634 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
635 int strict;
636 int s;
637 const char *op;
638 if (l < 0)
639 continue;
640 if (!dump && l >= o_div &&
641 can_print_div_expr(p, div, l - o_div) &&
642 isl_basic_map_is_div_constraint(bmap, bmap->ineq[i],
643 l - o_div))
644 continue;
645 s = isl_int_sgn(bmap->ineq[i][l]);
646 strict = !rational && isl_int_is_negone(bmap->ineq[i][0]);
647 if (s < 0)
648 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
649 else
650 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
651 if (strict)
652 isl_int_set_si(c->el[0], 0);
653 if (!dump && next_is_opposite(bmap, i, l)) {
654 op = constraint_op(-s, strict, latex);
655 p = print_half_constraint(bmap, space, div, p, c->el, l,
656 op, first, latex);
657 first = 1;
658 } else {
659 op = constraint_op(s, strict, latex);
660 p = print_constraint(bmap, space, div, p, c->el, l,
661 op, first, latex);
662 first = 0;
666 isl_basic_map_free(bmap);
667 isl_vec_free(c);
669 return p;
670 error:
671 isl_basic_map_free(bmap);
672 isl_vec_free(c);
673 isl_printer_free(p);
674 return NULL;
677 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
678 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
680 int c;
682 if (!p || !div)
683 return isl_printer_free(p);
685 c = p->output_format == ISL_FORMAT_C;
686 p = isl_printer_print_str(p, c ? "floord(" : "floor((");
687 p = print_affine_of_len(dim, div, p,
688 div->row[pos] + 1, div->n_col - 1);
689 p = isl_printer_print_str(p, c ? ", " : ")/");
690 p = isl_printer_print_isl_int(p, div->row[pos][0]);
691 p = isl_printer_print_str(p, ")");
692 return p;
695 /* Print a comma separated list of div names, except those that have
696 * a definition that can be printed.
697 * If "print_defined_divs" is set, then those div names are printed
698 * as well, along with their definitions.
700 static __isl_give isl_printer *print_div_list(__isl_take isl_printer *p,
701 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex,
702 int print_defined_divs)
704 int i;
705 int first = 1;
706 unsigned n_div;
708 if (!p || !space || !div)
709 return isl_printer_free(p);
711 n_div = isl_mat_rows(div);
713 for (i = 0; i < n_div; ++i) {
714 if (!print_defined_divs && can_print_div_expr(p, div, i))
715 continue;
716 if (!first)
717 p = isl_printer_print_str(p, ", ");
718 p = print_name(space, p, isl_dim_div, i, latex);
719 first = 0;
720 if (!can_print_div_expr(p, div, i))
721 continue;
722 p = isl_printer_print_str(p, " = ");
723 p = print_div(space, div, i, p);
726 return p;
729 /* Does printing "bmap" require an "exists" clause?
730 * That is, are there any local variables without an explicit representation?
732 static isl_bool need_exists(__isl_keep isl_printer *p,
733 __isl_keep isl_basic_map *bmap, __isl_keep isl_mat *div)
735 int i;
737 if (!p || !bmap)
738 return isl_bool_error;
739 if (bmap->n_div == 0)
740 return isl_bool_false;
741 for (i = 0; i < bmap->n_div; ++i)
742 if (!can_print_div_expr(p, div, i))
743 return isl_bool_true;
744 return isl_bool_false;
747 /* Print the constraints of "bmap" to "p".
748 * The names of the variables are taken from "space".
749 * "latex" is set if the constraints should be printed in LaTeX format.
750 * Do not print inline explicit div representations in "dump" mode.
752 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
753 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
755 int dump;
756 isl_mat *div;
757 isl_bool exists;
759 if (!p)
760 return NULL;
761 dump = p->dump;
762 div = isl_basic_map_get_divs(bmap);
763 if (dump)
764 exists = bmap->n_div > 0;
765 else
766 exists = need_exists(p, bmap, div);
767 if (exists >= 0 && exists) {
768 p = isl_printer_print_str(p, s_open_exists[latex]);
769 p = print_div_list(p, space, div, latex, dump);
770 p = isl_printer_print_str(p, ": ");
773 if (dump)
774 div = isl_mat_free(div);
775 p = print_constraints(bmap, space, div, p, latex);
776 isl_mat_free(div);
778 if (exists >= 0 && exists)
779 p = isl_printer_print_str(p, s_close_exists[latex]);
780 return p;
783 /* Print a colon followed by the constraints of "bmap"
784 * to "p", provided there are any constraints.
785 * The names of the variables are taken from "space".
786 * "latex" is set if the constraints should be printed in LaTeX format.
788 static __isl_give isl_printer *print_optional_disjunct(
789 __isl_keep isl_basic_map *bmap, __isl_keep isl_space *space,
790 __isl_take isl_printer *p, int latex)
792 if (isl_basic_map_plain_is_universe(bmap))
793 return p;
795 p = isl_printer_print_str(p, ": ");
796 p = print_disjunct(bmap, space, p, latex);
798 return p;
801 static __isl_give isl_printer *basic_map_print_omega(
802 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
804 p = isl_printer_print_str(p, "{ [");
805 p = print_var_list(p, bmap->dim, isl_dim_in);
806 p = isl_printer_print_str(p, "] -> [");
807 p = print_var_list(p, bmap->dim, isl_dim_out);
808 p = isl_printer_print_str(p, "] ");
809 p = print_optional_disjunct(bmap, bmap->dim, p, 0);
810 p = isl_printer_print_str(p, " }");
811 return p;
814 static __isl_give isl_printer *basic_set_print_omega(
815 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
817 p = isl_printer_print_str(p, "{ [");
818 p = print_var_list(p, bset->dim, isl_dim_set);
819 p = isl_printer_print_str(p, "] ");
820 p = print_optional_disjunct(bset, bset->dim, p, 0);
821 p = isl_printer_print_str(p, " }");
822 return p;
825 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
826 __isl_take isl_printer *p)
828 int i;
830 for (i = 0; i < map->n; ++i) {
831 if (i)
832 p = isl_printer_print_str(p, " union ");
833 p = basic_map_print_omega(map->p[i], p);
835 return p;
838 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
839 __isl_take isl_printer *p)
841 int i;
843 for (i = 0; i < set->n; ++i) {
844 if (i)
845 p = isl_printer_print_str(p, " union ");
846 p = basic_set_print_omega(set->p[i], p);
848 return p;
851 static __isl_give isl_printer *isl_basic_map_print_isl(
852 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
853 int latex)
855 struct isl_print_space_data data = { .latex = latex };
856 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
858 if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
859 p = print_tuple(bmap->dim, p, isl_dim_param, &data);
860 p = isl_printer_print_str(p, " -> ");
862 p = isl_printer_print_str(p, "{ ");
863 p = isl_print_space(bmap->dim, p, rational, &data);
864 p = isl_printer_print_str(p, " : ");
865 p = print_disjunct(bmap, bmap->dim, p, latex);
866 p = isl_printer_print_str(p, " }");
867 return p;
870 /* Print the disjuncts of a map (or set) "map" to "p".
871 * The names of the variables are taken from "space".
872 * "latex" is set if the constraints should be printed in LaTeX format.
874 static __isl_give isl_printer *print_disjuncts_core(__isl_keep isl_map *map,
875 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
877 int i;
879 if (map->n == 0)
880 p = isl_printer_print_str(p, "1 = 0");
881 for (i = 0; i < map->n; ++i) {
882 if (i)
883 p = isl_printer_print_str(p, s_or[latex]);
884 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
885 p = isl_printer_print_str(p, "(");
886 p = print_disjunct(map->p[i], space, p, latex);
887 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
888 p = isl_printer_print_str(p, ")");
890 return p;
893 /* Print the disjuncts of a map (or set) "map" to "p".
894 * The names of the variables are taken from "space".
895 * "hull" describes constraints shared by all disjuncts of "map".
896 * "latex" is set if the constraints should be printed in LaTeX format.
898 * Print the disjuncts as a conjunction of "hull" and
899 * the result of removing the constraints of "hull" from "map".
900 * If this result turns out to be the universe, then simply print "hull".
902 static __isl_give isl_printer *print_disjuncts_in_hull(__isl_keep isl_map *map,
903 __isl_keep isl_space *space, __isl_take isl_basic_map *hull,
904 __isl_take isl_printer *p, int latex)
906 isl_bool is_universe;
908 p = print_disjunct(hull, space, p, latex);
909 map = isl_map_plain_gist_basic_map(isl_map_copy(map), hull);
910 is_universe = isl_map_plain_is_universe(map);
911 if (is_universe < 0)
912 goto error;
913 if (!is_universe) {
914 p = isl_printer_print_str(p, s_and[latex]);
915 p = isl_printer_print_str(p, "(");
916 p = print_disjuncts_core(map, space, p, latex);
917 p = isl_printer_print_str(p, ")");
919 isl_map_free(map);
921 return p;
922 error:
923 isl_map_free(map);
924 isl_printer_free(p);
925 return NULL;
928 /* Print the disjuncts of a map (or set) "map" to "p".
929 * The names of the variables are taken from "space".
930 * "latex" is set if the constraints should be printed in LaTeX format.
932 * If there are at least two disjuncts and "dump" mode is not turned out,
933 * check for any shared constraints among all disjuncts.
934 * If there are any, then print them separately in print_disjuncts_in_hull.
936 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
937 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
939 if (isl_map_plain_is_universe(map))
940 return p;
942 p = isl_printer_print_str(p, s_such_that[latex]);
943 if (!p)
944 return NULL;
946 if (!p->dump && map->n >= 2) {
947 isl_basic_map *hull;
948 isl_bool is_universe;
950 hull = isl_map_plain_unshifted_simple_hull(isl_map_copy(map));
951 is_universe = isl_basic_map_plain_is_universe(hull);
952 if (is_universe < 0)
953 p = isl_printer_free(p);
954 else if (!is_universe)
955 return print_disjuncts_in_hull(map, space, hull,
956 p, latex);
957 isl_basic_map_free(hull);
960 return print_disjuncts_core(map, space, p, latex);
963 /* Print the disjuncts of a map (or set).
964 * The names of the variables are taken from "space".
965 * "latex" is set if the constraints should be printed in LaTeX format.
967 * If the map turns out to be a universal parameter domain, then
968 * we need to print the colon. Otherwise, the output looks identical
969 * to the empty set.
971 static __isl_give isl_printer *print_disjuncts_map(__isl_keep isl_map *map,
972 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
974 if (isl_map_plain_is_universe(map) && isl_space_is_params(map->dim))
975 return isl_printer_print_str(p, s_such_that[latex]);
976 else
977 return print_disjuncts(map, space, p, latex);
980 struct isl_aff_split {
981 isl_basic_map *aff;
982 isl_map *map;
985 static void free_split(__isl_take struct isl_aff_split *split, int n)
987 int i;
989 if (!split)
990 return;
992 for (i = 0; i < n; ++i) {
993 isl_basic_map_free(split[i].aff);
994 isl_map_free(split[i].map);
997 free(split);
1000 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
1002 int i, j;
1003 unsigned nparam, n_in, n_out, total;
1005 bmap = isl_basic_map_cow(bmap);
1006 if (!bmap)
1007 return NULL;
1008 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
1009 goto error;
1011 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1012 n_in = isl_basic_map_dim(bmap, isl_dim_in);
1013 n_out = isl_basic_map_dim(bmap, isl_dim_out);
1014 total = isl_basic_map_dim(bmap, isl_dim_all);
1015 for (i = bmap->n_eq - 1; i >= 0; --i) {
1016 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
1017 if (j >= nparam && j < nparam + n_in + n_out &&
1018 (isl_int_is_one(bmap->eq[i][1 + j]) ||
1019 isl_int_is_negone(bmap->eq[i][1 + j])))
1020 continue;
1021 if (isl_basic_map_drop_equality(bmap, i) < 0)
1022 goto error;
1025 bmap = isl_basic_map_finalize(bmap);
1027 return bmap;
1028 error:
1029 isl_basic_map_free(bmap);
1030 return NULL;
1033 static int aff_split_cmp(const void *p1, const void *p2, void *user)
1035 const struct isl_aff_split *s1, *s2;
1036 s1 = (const struct isl_aff_split *) p1;
1037 s2 = (const struct isl_aff_split *) p2;
1039 return isl_basic_map_plain_cmp(s1->aff, s2->aff);
1042 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
1043 __isl_keep isl_basic_map *aff)
1045 int i, j;
1046 unsigned total;
1048 if (!bmap || !aff)
1049 goto error;
1051 total = isl_space_dim(bmap->dim, isl_dim_all);
1053 for (i = bmap->n_eq - 1; i >= 0; --i) {
1054 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
1055 bmap->n_div) != -1)
1056 continue;
1057 for (j = 0; j < aff->n_eq; ++j) {
1058 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
1059 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
1060 continue;
1061 if (isl_basic_map_drop_equality(bmap, i) < 0)
1062 goto error;
1063 break;
1067 return bmap;
1068 error:
1069 isl_basic_map_free(bmap);
1070 return NULL;
1073 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
1075 int i, n;
1076 struct isl_aff_split *split;
1077 isl_ctx *ctx;
1079 ctx = isl_map_get_ctx(map);
1080 split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
1081 if (!split)
1082 return NULL;
1084 for (i = 0; i < map->n; ++i) {
1085 isl_basic_map *bmap;
1086 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
1087 bmap = isl_basic_map_copy(map->p[i]);
1088 bmap = isl_basic_map_cow(bmap);
1089 bmap = drop_aff(bmap, split[i].aff);
1090 split[i].map = isl_map_from_basic_map(bmap);
1091 if (!split[i].aff || !split[i].map)
1092 goto error;
1095 if (isl_sort(split, map->n, sizeof(struct isl_aff_split),
1096 &aff_split_cmp, NULL) < 0)
1097 goto error;
1099 n = map->n;
1100 for (i = n - 1; i >= 1; --i) {
1101 if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
1102 split[i].aff))
1103 continue;
1104 isl_basic_map_free(split[i].aff);
1105 split[i - 1].map = isl_map_union(split[i - 1].map,
1106 split[i].map);
1107 if (i != n - 1)
1108 split[i] = split[n - 1];
1109 split[n - 1].aff = NULL;
1110 split[n - 1].map = NULL;
1111 --n;
1114 return split;
1115 error:
1116 free_split(split, map->n);
1117 return NULL;
1120 static int defining_equality(__isl_keep isl_basic_map *eq,
1121 __isl_keep isl_space *dim, enum isl_dim_type type, int pos)
1123 int i;
1124 unsigned total;
1126 if (!eq)
1127 return -1;
1129 pos += isl_space_offset(dim, type);
1130 total = isl_basic_map_total_dim(eq);
1132 for (i = 0; i < eq->n_eq; ++i) {
1133 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
1134 continue;
1135 if (isl_int_is_one(eq->eq[i][1 + pos]))
1136 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
1137 return i;
1140 return -1;
1143 /* Print dimension "pos" of data->space to "p".
1145 * data->user is assumed to be an isl_basic_map keeping track of equalities.
1147 * If the current dimension is defined by these equalities, then print
1148 * the corresponding expression, assigned to the name of the dimension
1149 * if there is any. Otherwise, print the name of the dimension.
1151 static __isl_give isl_printer *print_dim_eq(__isl_take isl_printer *p,
1152 struct isl_print_space_data *data, unsigned pos)
1154 isl_basic_map *eq = data->user;
1155 int j;
1157 j = defining_equality(eq, data->space, data->type, pos);
1158 if (j >= 0) {
1159 if (isl_space_has_dim_name(data->space, data->type, pos)) {
1160 p = print_name(data->space, p, data->type, pos,
1161 data->latex);
1162 p = isl_printer_print_str(p, " = ");
1164 pos += 1 + isl_space_offset(data->space, data->type);
1165 p = print_affine_of_len(data->space, NULL, p, eq->eq[j], pos);
1166 } else {
1167 p = print_name(data->space, p, data->type, pos, data->latex);
1170 return p;
1173 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
1174 struct isl_aff_split *split, int n, __isl_keep isl_space *space)
1176 struct isl_print_space_data data = { 0 };
1177 int i;
1178 int rational;
1180 data.print_dim = &print_dim_eq;
1181 for (i = 0; i < n; ++i) {
1182 if (!split[i].map)
1183 break;
1184 rational = split[i].map->n > 0 &&
1185 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
1186 if (i)
1187 p = isl_printer_print_str(p, "; ");
1188 data.user = split[i].aff;
1189 p = isl_print_space(space, p, rational, &data);
1190 p = print_disjuncts_map(split[i].map, space, p, 0);
1193 return p;
1196 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
1197 __isl_take isl_printer *p)
1199 struct isl_print_space_data data = { 0 };
1200 struct isl_aff_split *split = NULL;
1201 int rational;
1203 if (!p || !map)
1204 return isl_printer_free(p);
1205 if (!p->dump && map->n > 0)
1206 split = split_aff(map);
1207 if (split) {
1208 p = print_split_map(p, split, map->n, map->dim);
1209 } else {
1210 rational = map->n > 0 &&
1211 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
1212 p = isl_print_space(map->dim, p, rational, &data);
1213 p = print_disjuncts_map(map, map->dim, p, 0);
1215 free_split(split, map->n);
1216 return p;
1219 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
1220 __isl_take isl_printer *p)
1222 struct isl_print_space_data data = { 0 };
1224 if (isl_map_dim(map, isl_dim_param) > 0) {
1225 p = print_tuple(map->dim, p, isl_dim_param, &data);
1226 p = isl_printer_print_str(p, s_to[0]);
1228 p = isl_printer_print_str(p, s_open_set[0]);
1229 p = isl_map_print_isl_body(map, p);
1230 p = isl_printer_print_str(p, s_close_set[0]);
1231 return p;
1234 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
1235 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff)
1237 struct isl_print_space_data data = { 0 };
1239 data.latex = 1;
1240 if (isl_map_dim(map, isl_dim_param) > 0) {
1241 p = print_tuple(map->dim, p, isl_dim_param, &data);
1242 p = isl_printer_print_str(p, s_to[1]);
1244 p = isl_printer_print_str(p, s_open_set[1]);
1245 data.print_dim = &print_dim_eq;
1246 data.user = aff;
1247 p = isl_print_space(map->dim, p, 0, &data);
1248 p = print_disjuncts_map(map, map->dim, p, 1);
1249 p = isl_printer_print_str(p, s_close_set[1]);
1251 return p;
1254 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
1255 __isl_take isl_printer *p)
1257 int i;
1258 struct isl_aff_split *split = NULL;
1260 if (map->n > 0)
1261 split = split_aff(map);
1263 if (!split)
1264 return print_latex_map(map, p, NULL);
1266 for (i = 0; i < map->n; ++i) {
1267 if (!split[i].map)
1268 break;
1269 if (i)
1270 p = isl_printer_print_str(p, " \\cup ");
1271 p = print_latex_map(split[i].map, p, split[i].aff);
1274 free_split(split, map->n);
1275 return p;
1278 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
1279 __isl_keep isl_basic_map *bmap)
1281 if (!p || !bmap)
1282 goto error;
1283 if (p->output_format == ISL_FORMAT_ISL)
1284 return isl_basic_map_print_isl(bmap, p, 0);
1285 else if (p->output_format == ISL_FORMAT_OMEGA)
1286 return basic_map_print_omega(bmap, p);
1287 isl_assert(bmap->ctx, 0, goto error);
1288 error:
1289 isl_printer_free(p);
1290 return NULL;
1293 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
1294 __isl_keep isl_basic_set *bset)
1296 if (!p || !bset)
1297 goto error;
1299 if (p->output_format == ISL_FORMAT_ISL)
1300 return isl_basic_map_print_isl(bset, p, 0);
1301 else if (p->output_format == ISL_FORMAT_POLYLIB)
1302 return isl_basic_set_print_polylib(bset, p, 0);
1303 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1304 return isl_basic_set_print_polylib(bset, p, 1);
1305 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
1306 return bset_print_constraints_polylib(bset, p);
1307 else if (p->output_format == ISL_FORMAT_OMEGA)
1308 return basic_set_print_omega(bset, p);
1309 isl_assert(p->ctx, 0, goto error);
1310 error:
1311 isl_printer_free(p);
1312 return NULL;
1315 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
1316 __isl_keep isl_set *set)
1318 if (!p || !set)
1319 goto error;
1320 if (p->output_format == ISL_FORMAT_ISL)
1321 return isl_map_print_isl((isl_map *)set, p);
1322 else if (p->output_format == ISL_FORMAT_POLYLIB)
1323 return isl_set_print_polylib(set, p, 0);
1324 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1325 return isl_set_print_polylib(set, p, 1);
1326 else if (p->output_format == ISL_FORMAT_OMEGA)
1327 return isl_set_print_omega(set, p);
1328 else if (p->output_format == ISL_FORMAT_LATEX)
1329 return isl_map_print_latex((isl_map *)set, p);
1330 isl_assert(set->ctx, 0, goto error);
1331 error:
1332 isl_printer_free(p);
1333 return NULL;
1336 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1337 __isl_keep isl_map *map)
1339 if (!p || !map)
1340 goto error;
1342 if (p->output_format == ISL_FORMAT_ISL)
1343 return isl_map_print_isl(map, p);
1344 else if (p->output_format == ISL_FORMAT_POLYLIB)
1345 return isl_map_print_polylib(map, p, 0);
1346 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1347 return isl_map_print_polylib(map, p, 1);
1348 else if (p->output_format == ISL_FORMAT_OMEGA)
1349 return isl_map_print_omega(map, p);
1350 else if (p->output_format == ISL_FORMAT_LATEX)
1351 return isl_map_print_latex(map, p);
1352 isl_assert(map->ctx, 0, goto error);
1353 error:
1354 isl_printer_free(p);
1355 return NULL;
1358 struct isl_union_print_data {
1359 isl_printer *p;
1360 int first;
1363 static isl_stat print_map_body(__isl_take isl_map *map, void *user)
1365 struct isl_union_print_data *data;
1366 data = (struct isl_union_print_data *)user;
1368 if (!data->first)
1369 data->p = isl_printer_print_str(data->p, "; ");
1370 data->first = 0;
1372 data->p = isl_map_print_isl_body(map, data->p);
1373 isl_map_free(map);
1375 return isl_stat_ok;
1378 static __isl_give isl_printer *isl_union_map_print_isl(
1379 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1381 struct isl_union_print_data data;
1382 struct isl_print_space_data space_data = { 0 };
1383 isl_space *dim;
1385 dim = isl_union_map_get_space(umap);
1386 if (isl_space_dim(dim, isl_dim_param) > 0) {
1387 p = print_tuple(dim, p, isl_dim_param, &space_data);
1388 p = isl_printer_print_str(p, s_to[0]);
1390 isl_space_free(dim);
1391 p = isl_printer_print_str(p, s_open_set[0]);
1392 data.p = p;
1393 data.first = 1;
1394 isl_union_map_foreach_map(umap, &print_map_body, &data);
1395 p = data.p;
1396 p = isl_printer_print_str(p, s_close_set[0]);
1397 return p;
1400 static isl_stat print_latex_map_body(__isl_take isl_map *map, void *user)
1402 struct isl_union_print_data *data;
1403 data = (struct isl_union_print_data *)user;
1405 if (!data->first)
1406 data->p = isl_printer_print_str(data->p, " \\cup ");
1407 data->first = 0;
1409 data->p = isl_map_print_latex(map, data->p);
1410 isl_map_free(map);
1412 return isl_stat_ok;
1415 static __isl_give isl_printer *isl_union_map_print_latex(
1416 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1418 struct isl_union_print_data data = { p, 1 };
1419 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1420 p = data.p;
1421 return p;
1424 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1425 __isl_keep isl_union_map *umap)
1427 if (!p || !umap)
1428 goto error;
1430 if (p->output_format == ISL_FORMAT_ISL)
1431 return isl_union_map_print_isl(umap, p);
1432 if (p->output_format == ISL_FORMAT_LATEX)
1433 return isl_union_map_print_latex(umap, p);
1435 isl_die(p->ctx, isl_error_invalid,
1436 "invalid output format for isl_union_map", goto error);
1437 error:
1438 isl_printer_free(p);
1439 return NULL;
1442 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1443 __isl_keep isl_union_set *uset)
1445 if (!p || !uset)
1446 goto error;
1448 if (p->output_format == ISL_FORMAT_ISL)
1449 return isl_union_map_print_isl((isl_union_map *)uset, p);
1450 if (p->output_format == ISL_FORMAT_LATEX)
1451 return isl_union_map_print_latex((isl_union_map *)uset, p);
1453 isl_die(p->ctx, isl_error_invalid,
1454 "invalid output format for isl_union_set", goto error);
1455 error:
1456 isl_printer_free(p);
1457 return NULL;
1460 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
1462 int i;
1463 int n;
1465 for (i = 0, n = 0; i < rec->n; ++i)
1466 if (!isl_upoly_is_zero(rec->p[i]))
1467 ++n;
1469 return n;
1472 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
1473 __isl_take isl_printer *p, int first)
1475 struct isl_upoly_cst *cst;
1476 int neg;
1478 cst = isl_upoly_as_cst(up);
1479 if (!cst)
1480 goto error;
1481 neg = !first && isl_int_is_neg(cst->n);
1482 if (!first)
1483 p = isl_printer_print_str(p, neg ? " - " : " + ");
1484 if (neg)
1485 isl_int_neg(cst->n, cst->n);
1486 if (isl_int_is_zero(cst->d)) {
1487 int sgn = isl_int_sgn(cst->n);
1488 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1489 sgn == 0 ? "NaN" : "infty");
1490 } else
1491 p = isl_printer_print_isl_int(p, cst->n);
1492 if (neg)
1493 isl_int_neg(cst->n, cst->n);
1494 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1495 p = isl_printer_print_str(p, "/");
1496 p = isl_printer_print_isl_int(p, cst->d);
1498 return p;
1499 error:
1500 isl_printer_free(p);
1501 return NULL;
1504 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1505 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var)
1507 unsigned total;
1509 total = isl_space_dim(dim, isl_dim_all);
1510 if (var < total)
1511 p = print_term(dim, NULL, dim->ctx->one, 1 + var, p, 0);
1512 else
1513 p = print_div(dim, div, var - total, p);
1514 return p;
1517 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1518 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var, int exp)
1520 p = print_base(p, dim, div, var);
1521 if (exp == 1)
1522 return p;
1523 if (p->output_format == ISL_FORMAT_C) {
1524 int i;
1525 for (i = 1; i < exp; ++i) {
1526 p = isl_printer_print_str(p, "*");
1527 p = print_base(p, dim, div, var);
1529 } else {
1530 p = isl_printer_print_str(p, "^");
1531 p = isl_printer_print_int(p, exp);
1533 return p;
1536 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1537 __isl_keep isl_space *dim, __isl_keep isl_mat *div,
1538 __isl_take isl_printer *p, int outer)
1540 int i, n, first, print_parens;
1541 struct isl_upoly_rec *rec;
1543 if (!p || !up || !dim || !div)
1544 goto error;
1546 if (isl_upoly_is_cst(up))
1547 return upoly_print_cst(up, p, 1);
1549 rec = isl_upoly_as_rec(up);
1550 if (!rec)
1551 goto error;
1552 n = upoly_rec_n_non_zero(rec);
1553 print_parens = n > 1 ||
1554 (outer && rec->up.var >= isl_space_dim(dim, isl_dim_all));
1555 if (print_parens)
1556 p = isl_printer_print_str(p, "(");
1557 for (i = 0, first = 1; i < rec->n; ++i) {
1558 if (isl_upoly_is_zero(rec->p[i]))
1559 continue;
1560 if (isl_upoly_is_negone(rec->p[i])) {
1561 if (!i)
1562 p = isl_printer_print_str(p, "-1");
1563 else if (first)
1564 p = isl_printer_print_str(p, "-");
1565 else
1566 p = isl_printer_print_str(p, " - ");
1567 } else if (isl_upoly_is_cst(rec->p[i]) &&
1568 !isl_upoly_is_one(rec->p[i]))
1569 p = upoly_print_cst(rec->p[i], p, first);
1570 else {
1571 if (!first)
1572 p = isl_printer_print_str(p, " + ");
1573 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1574 p = upoly_print(rec->p[i], dim, div, p, 0);
1576 first = 0;
1577 if (i == 0)
1578 continue;
1579 if (!isl_upoly_is_one(rec->p[i]) &&
1580 !isl_upoly_is_negone(rec->p[i]))
1581 p = isl_printer_print_str(p, " * ");
1582 p = print_pow(p, dim, div, rec->up.var, i);
1584 if (print_parens)
1585 p = isl_printer_print_str(p, ")");
1586 return p;
1587 error:
1588 isl_printer_free(p);
1589 return NULL;
1592 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1593 __isl_keep isl_qpolynomial *qp)
1595 if (!p || !qp)
1596 goto error;
1597 p = upoly_print(qp->upoly, qp->dim, qp->div, p, 1);
1598 return p;
1599 error:
1600 isl_printer_free(p);
1601 return NULL;
1604 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1605 __isl_keep isl_qpolynomial *qp)
1607 struct isl_print_space_data data = { 0 };
1609 if (!p || !qp)
1610 goto error;
1612 if (isl_space_dim(qp->dim, isl_dim_param) > 0) {
1613 p = print_tuple(qp->dim, p, isl_dim_param, &data);
1614 p = isl_printer_print_str(p, " -> ");
1616 p = isl_printer_print_str(p, "{ ");
1617 if (!isl_space_is_params(qp->dim)) {
1618 p = isl_print_space(qp->dim, p, 0, &data);
1619 p = isl_printer_print_str(p, " -> ");
1621 p = print_qpolynomial(p, qp);
1622 p = isl_printer_print_str(p, " }");
1623 return p;
1624 error:
1625 isl_printer_free(p);
1626 return NULL;
1629 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1630 __isl_keep isl_space *dim, __isl_keep isl_qpolynomial *qp)
1632 isl_int den;
1634 isl_int_init(den);
1635 isl_qpolynomial_get_den(qp, &den);
1636 if (!isl_int_is_one(den)) {
1637 isl_qpolynomial *f;
1638 p = isl_printer_print_str(p, "(");
1639 qp = isl_qpolynomial_copy(qp);
1640 f = isl_qpolynomial_rat_cst_on_domain(isl_space_copy(qp->dim),
1641 den, qp->dim->ctx->one);
1642 qp = isl_qpolynomial_mul(qp, f);
1644 if (qp)
1645 p = upoly_print(qp->upoly, dim, qp->div, p, 0);
1646 else
1647 p = isl_printer_free(p);
1648 if (!isl_int_is_one(den)) {
1649 p = isl_printer_print_str(p, ")/");
1650 p = isl_printer_print_isl_int(p, den);
1651 isl_qpolynomial_free(qp);
1653 isl_int_clear(den);
1654 return p;
1657 __isl_give isl_printer *isl_printer_print_qpolynomial(
1658 __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1660 if (!p || !qp)
1661 goto error;
1663 if (p->output_format == ISL_FORMAT_ISL)
1664 return print_qpolynomial_isl(p, qp);
1665 else if (p->output_format == ISL_FORMAT_C)
1666 return print_qpolynomial_c(p, qp->dim, qp);
1667 else
1668 isl_die(qp->dim->ctx, isl_error_unsupported,
1669 "output format not supported for isl_qpolynomials",
1670 goto error);
1671 error:
1672 isl_printer_free(p);
1673 return NULL;
1676 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1677 unsigned output_format)
1679 isl_printer *p;
1681 if (!qp)
1682 return;
1684 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1685 p = isl_printer_to_file(qp->dim->ctx, out);
1686 p = isl_printer_print_qpolynomial(p, qp);
1687 isl_printer_free(p);
1690 static __isl_give isl_printer *qpolynomial_fold_print(
1691 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1693 int i;
1695 if (fold->type == isl_fold_min)
1696 p = isl_printer_print_str(p, "min");
1697 else if (fold->type == isl_fold_max)
1698 p = isl_printer_print_str(p, "max");
1699 p = isl_printer_print_str(p, "(");
1700 for (i = 0; i < fold->n; ++i) {
1701 if (i)
1702 p = isl_printer_print_str(p, ", ");
1703 p = print_qpolynomial(p, fold->qp[i]);
1705 p = isl_printer_print_str(p, ")");
1706 return p;
1709 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1710 FILE *out, unsigned output_format)
1712 isl_printer *p;
1714 if (!fold)
1715 return;
1717 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1719 p = isl_printer_to_file(fold->dim->ctx, out);
1720 p = isl_printer_print_qpolynomial_fold(p, fold);
1722 isl_printer_free(p);
1725 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1726 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1728 struct isl_print_space_data data = { 0 };
1729 int i = 0;
1731 for (i = 0; i < pwqp->n; ++i) {
1732 isl_space *space;
1734 if (i)
1735 p = isl_printer_print_str(p, "; ");
1736 space = isl_qpolynomial_get_domain_space(pwqp->p[i].qp);
1737 if (!isl_space_is_params(space)) {
1738 p = isl_print_space(space, p, 0, &data);
1739 p = isl_printer_print_str(p, " -> ");
1741 p = print_qpolynomial(p, pwqp->p[i].qp);
1742 p = print_disjuncts((isl_map *)pwqp->p[i].set, space, p, 0);
1743 isl_space_free(space);
1746 return p;
1749 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1750 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1752 struct isl_print_space_data data = { 0 };
1754 if (!p || !pwqp)
1755 goto error;
1757 if (isl_space_dim(pwqp->dim, isl_dim_param) > 0) {
1758 p = print_tuple(pwqp->dim, p, isl_dim_param, &data);
1759 p = isl_printer_print_str(p, " -> ");
1761 p = isl_printer_print_str(p, "{ ");
1762 if (pwqp->n == 0) {
1763 if (!isl_space_is_set(pwqp->dim)) {
1764 p = print_tuple(pwqp->dim, p, isl_dim_in, &data);
1765 p = isl_printer_print_str(p, " -> ");
1767 p = isl_printer_print_str(p, "0");
1769 p = isl_pwqp_print_isl_body(p, pwqp);
1770 p = isl_printer_print_str(p, " }");
1771 return p;
1772 error:
1773 isl_printer_free(p);
1774 return NULL;
1777 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
1778 unsigned output_format)
1780 isl_printer *p;
1782 if (!pwqp)
1783 return;
1785 p = isl_printer_to_file(pwqp->dim->ctx, out);
1786 p = isl_printer_set_output_format(p, output_format);
1787 p = isl_printer_print_pw_qpolynomial(p, pwqp);
1789 isl_printer_free(p);
1792 static __isl_give isl_printer *isl_pwf_print_isl_body(
1793 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1795 struct isl_print_space_data data = { 0 };
1796 int i = 0;
1798 for (i = 0; i < pwf->n; ++i) {
1799 isl_space *space;
1801 if (i)
1802 p = isl_printer_print_str(p, "; ");
1803 space = isl_qpolynomial_fold_get_domain_space(pwf->p[i].fold);
1804 if (!isl_space_is_params(space)) {
1805 p = isl_print_space(space, p, 0, &data);
1806 p = isl_printer_print_str(p, " -> ");
1808 p = qpolynomial_fold_print(pwf->p[i].fold, p);
1809 p = print_disjuncts((isl_map *)pwf->p[i].set, space, p, 0);
1810 isl_space_free(space);
1813 return p;
1816 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
1817 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1819 struct isl_print_space_data data = { 0 };
1821 if (isl_space_dim(pwf->dim, isl_dim_param) > 0) {
1822 p = print_tuple(pwf->dim, p, isl_dim_param, &data);
1823 p = isl_printer_print_str(p, " -> ");
1825 p = isl_printer_print_str(p, "{ ");
1826 if (pwf->n == 0) {
1827 if (!isl_space_is_set(pwf->dim)) {
1828 p = print_tuple(pwf->dim, p, isl_dim_in, &data);
1829 p = isl_printer_print_str(p, " -> ");
1831 p = isl_printer_print_str(p, "0");
1833 p = isl_pwf_print_isl_body(p, pwf);
1834 p = isl_printer_print_str(p, " }");
1835 return p;
1838 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1839 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c);
1841 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1842 __isl_keep isl_space *dim,
1843 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1845 if (type == isl_dim_div) {
1846 p = isl_printer_print_str(p, "floord(");
1847 p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
1848 p = isl_printer_print_str(p, ", ");
1849 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1850 p = isl_printer_print_str(p, ")");
1851 } else {
1852 const char *name;
1854 name = isl_space_get_dim_name(dim, type, pos);
1855 if (!name)
1856 name = "UNNAMED";
1857 p = isl_printer_print_str(p, name);
1859 return p;
1862 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1863 __isl_keep isl_space *dim,
1864 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1866 enum isl_dim_type type;
1868 if (pos == 0)
1869 return isl_printer_print_isl_int(p, c);
1871 if (isl_int_is_one(c))
1873 else if (isl_int_is_negone(c))
1874 p = isl_printer_print_str(p, "-");
1875 else {
1876 p = isl_printer_print_isl_int(p, c);
1877 p = isl_printer_print_str(p, "*");
1879 type = pos2type(dim, &pos);
1880 p = print_name_c(p, dim, bset, type, pos);
1881 return p;
1884 static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
1885 __isl_keep isl_space *dim,
1886 __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
1888 int i;
1889 int first;
1891 for (i = 0, first = 1; i < len; ++i) {
1892 int flip = 0;
1893 if (isl_int_is_zero(c[i]))
1894 continue;
1895 if (!first) {
1896 if (isl_int_is_neg(c[i])) {
1897 flip = 1;
1898 isl_int_neg(c[i], c[i]);
1899 p = isl_printer_print_str(p, " - ");
1900 } else
1901 p = isl_printer_print_str(p, " + ");
1903 first = 0;
1904 p = print_term_c(p, dim, bset, c[i], i);
1905 if (flip)
1906 isl_int_neg(c[i], c[i]);
1908 if (first)
1909 p = isl_printer_print_str(p, "0");
1910 return p;
1913 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1914 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c)
1916 unsigned len = 1 + isl_basic_set_total_dim(bset);
1917 return print_partial_affine_c(p, dim, bset, c, len);
1920 /* We skip the constraint if it is implied by the div expression.
1922 * *first indicates whether this is the first constraint in the conjunction and
1923 * is updated if the constraint is actually printed.
1925 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1926 __isl_keep isl_space *dim,
1927 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int *first)
1929 unsigned o_div;
1930 unsigned n_div;
1931 int div;
1933 o_div = isl_basic_set_offset(bset, isl_dim_div);
1934 n_div = isl_basic_set_dim(bset, isl_dim_div);
1935 div = isl_seq_last_non_zero(c + o_div, n_div);
1936 if (div >= 0 && isl_basic_set_is_div_constraint(bset, c, div))
1937 return p;
1939 if (!*first)
1940 p = isl_printer_print_str(p, " && ");
1942 p = print_affine_c(p, dim, bset, c);
1943 p = isl_printer_print_str(p, " ");
1944 p = isl_printer_print_str(p, op);
1945 p = isl_printer_print_str(p, " 0");
1947 *first = 0;
1949 return p;
1952 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1953 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset)
1955 int i, j;
1956 int first = 1;
1957 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
1958 unsigned total = isl_basic_set_total_dim(bset) - n_div;
1960 for (i = 0; i < bset->n_eq; ++i) {
1961 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
1962 if (j < 0)
1963 p = print_constraint_c(p, dim, bset,
1964 bset->eq[i], "==", &first);
1965 else {
1966 if (i)
1967 p = isl_printer_print_str(p, " && ");
1968 p = isl_printer_print_str(p, "(");
1969 p = print_partial_affine_c(p, dim, bset, bset->eq[i],
1970 1 + total + j);
1971 p = isl_printer_print_str(p, ") % ");
1972 p = isl_printer_print_isl_int(p,
1973 bset->eq[i][1 + total + j]);
1974 p = isl_printer_print_str(p, " == 0");
1975 first = 0;
1978 for (i = 0; i < bset->n_ineq; ++i)
1979 p = print_constraint_c(p, dim, bset, bset->ineq[i], ">=",
1980 &first);
1981 return p;
1984 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
1985 __isl_keep isl_space *dim, __isl_keep isl_set *set)
1987 int i;
1989 if (!set)
1990 return isl_printer_free(p);
1992 if (set->n == 0)
1993 p = isl_printer_print_str(p, "0");
1995 for (i = 0; i < set->n; ++i) {
1996 if (i)
1997 p = isl_printer_print_str(p, " || ");
1998 if (set->n > 1)
1999 p = isl_printer_print_str(p, "(");
2000 p = print_basic_set_c(p, dim, set->p[i]);
2001 if (set->n > 1)
2002 p = isl_printer_print_str(p, ")");
2004 return p;
2007 static __isl_give isl_printer *print_pw_qpolynomial_c(
2008 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2010 int i;
2012 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set))
2013 return print_qpolynomial_c(p, pwqp->dim, pwqp->p[0].qp);
2015 for (i = 0; i < pwqp->n; ++i) {
2016 p = isl_printer_print_str(p, "(");
2017 p = print_set_c(p, pwqp->dim, pwqp->p[i].set);
2018 p = isl_printer_print_str(p, ") ? (");
2019 p = print_qpolynomial_c(p, pwqp->dim, pwqp->p[i].qp);
2020 p = isl_printer_print_str(p, ") : ");
2023 p = isl_printer_print_str(p, "0");
2024 return p;
2027 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
2028 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2030 if (!p || !pwqp)
2031 goto error;
2033 if (p->output_format == ISL_FORMAT_ISL)
2034 return print_pw_qpolynomial_isl(p, pwqp);
2035 else if (p->output_format == ISL_FORMAT_C)
2036 return print_pw_qpolynomial_c(p, pwqp);
2037 isl_assert(p->ctx, 0, goto error);
2038 error:
2039 isl_printer_free(p);
2040 return NULL;
2043 static isl_stat print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
2045 struct isl_union_print_data *data;
2046 data = (struct isl_union_print_data *)user;
2048 if (!data->first)
2049 data->p = isl_printer_print_str(data->p, "; ");
2050 data->first = 0;
2052 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
2053 isl_pw_qpolynomial_free(pwqp);
2055 return isl_stat_ok;
2058 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
2059 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2061 struct isl_union_print_data data;
2062 struct isl_print_space_data space_data = { 0 };
2063 isl_space *dim;
2065 dim = isl_union_pw_qpolynomial_get_space(upwqp);
2066 if (isl_space_dim(dim, isl_dim_param) > 0) {
2067 p = print_tuple(dim, p, isl_dim_param, &space_data);
2068 p = isl_printer_print_str(p, " -> ");
2070 isl_space_free(dim);
2071 p = isl_printer_print_str(p, "{ ");
2072 data.p = p;
2073 data.first = 1;
2074 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
2075 &data);
2076 p = data.p;
2077 p = isl_printer_print_str(p, " }");
2078 return p;
2081 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
2082 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2084 if (!p || !upwqp)
2085 goto error;
2087 if (p->output_format == ISL_FORMAT_ISL)
2088 return print_union_pw_qpolynomial_isl(p, upwqp);
2089 isl_die(p->ctx, isl_error_invalid,
2090 "invalid output format for isl_union_pw_qpolynomial",
2091 goto error);
2092 error:
2093 isl_printer_free(p);
2094 return NULL;
2097 static __isl_give isl_printer *print_qpolynomial_fold_c(
2098 __isl_take isl_printer *p, __isl_keep isl_space *dim,
2099 __isl_keep isl_qpolynomial_fold *fold)
2101 int i;
2103 for (i = 0; i < fold->n - 1; ++i)
2104 if (fold->type == isl_fold_min)
2105 p = isl_printer_print_str(p, "min(");
2106 else if (fold->type == isl_fold_max)
2107 p = isl_printer_print_str(p, "max(");
2109 for (i = 0; i < fold->n; ++i) {
2110 if (i)
2111 p = isl_printer_print_str(p, ", ");
2112 p = print_qpolynomial_c(p, dim, fold->qp[i]);
2113 if (i)
2114 p = isl_printer_print_str(p, ")");
2116 return p;
2119 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
2120 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
2122 if (!p || !fold)
2123 goto error;
2124 if (p->output_format == ISL_FORMAT_ISL)
2125 return qpolynomial_fold_print(fold, p);
2126 else if (p->output_format == ISL_FORMAT_C)
2127 return print_qpolynomial_fold_c(p, fold->dim, fold);
2128 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2129 goto error);
2130 error:
2131 isl_printer_free(p);
2132 return NULL;
2135 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
2136 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2138 int i;
2140 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set))
2141 return print_qpolynomial_fold_c(p, pwf->dim, pwf->p[0].fold);
2143 for (i = 0; i < pwf->n; ++i) {
2144 p = isl_printer_print_str(p, "(");
2145 p = print_set_c(p, pwf->dim, pwf->p[i].set);
2146 p = isl_printer_print_str(p, ") ? (");
2147 p = print_qpolynomial_fold_c(p, pwf->dim, pwf->p[i].fold);
2148 p = isl_printer_print_str(p, ") : ");
2151 p = isl_printer_print_str(p, "0");
2152 return p;
2155 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
2156 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2158 if (!p || !pwf)
2159 goto error;
2161 if (p->output_format == ISL_FORMAT_ISL)
2162 return print_pw_qpolynomial_fold_isl(p, pwf);
2163 else if (p->output_format == ISL_FORMAT_C)
2164 return print_pw_qpolynomial_fold_c(p, pwf);
2165 isl_assert(p->ctx, 0, goto error);
2166 error:
2167 isl_printer_free(p);
2168 return NULL;
2171 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
2172 FILE *out, unsigned output_format)
2174 isl_printer *p;
2176 if (!pwf)
2177 return;
2179 p = isl_printer_to_file(pwf->dim->ctx, out);
2180 p = isl_printer_set_output_format(p, output_format);
2181 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
2183 isl_printer_free(p);
2186 static isl_stat print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf,
2187 void *user)
2189 struct isl_union_print_data *data;
2190 data = (struct isl_union_print_data *)user;
2192 if (!data->first)
2193 data->p = isl_printer_print_str(data->p, "; ");
2194 data->first = 0;
2196 data->p = isl_pwf_print_isl_body(data->p, pwf);
2197 isl_pw_qpolynomial_fold_free(pwf);
2199 return isl_stat_ok;
2202 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
2203 __isl_take isl_printer *p,
2204 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2206 struct isl_union_print_data data;
2207 struct isl_print_space_data space_data = { 0 };
2208 isl_space *dim;
2210 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
2211 if (isl_space_dim(dim, isl_dim_param) > 0) {
2212 p = print_tuple(dim, p, isl_dim_param, &space_data);
2213 p = isl_printer_print_str(p, " -> ");
2215 isl_space_free(dim);
2216 p = isl_printer_print_str(p, "{ ");
2217 data.p = p;
2218 data.first = 1;
2219 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
2220 &print_pwf_body, &data);
2221 p = data.p;
2222 p = isl_printer_print_str(p, " }");
2223 return p;
2226 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2227 __isl_take isl_printer *p,
2228 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2230 if (!p || !upwf)
2231 goto error;
2233 if (p->output_format == ISL_FORMAT_ISL)
2234 return print_union_pw_qpolynomial_fold_isl(p, upwf);
2235 isl_die(p->ctx, isl_error_invalid,
2236 "invalid output format for isl_union_pw_qpolynomial_fold",
2237 goto error);
2238 error:
2239 isl_printer_free(p);
2240 return NULL;
2243 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
2244 __isl_keep isl_constraint *c)
2246 isl_basic_map *bmap;
2248 if (!p || !c)
2249 goto error;
2251 bmap = isl_basic_map_from_constraint(isl_constraint_copy(c));
2252 p = isl_printer_print_basic_map(p, bmap);
2253 isl_basic_map_free(bmap);
2254 return p;
2255 error:
2256 isl_printer_free(p);
2257 return NULL;
2260 static __isl_give isl_printer *isl_printer_print_space_isl(
2261 __isl_take isl_printer *p, __isl_keep isl_space *space)
2263 struct isl_print_space_data data = { 0 };
2265 if (!space)
2266 goto error;
2268 if (isl_space_dim(space, isl_dim_param) > 0) {
2269 p = print_tuple(space, p, isl_dim_param, &data);
2270 p = isl_printer_print_str(p, " -> ");
2273 p = isl_printer_print_str(p, "{ ");
2274 if (isl_space_is_params(space))
2275 p = isl_printer_print_str(p, s_such_that[0]);
2276 else
2277 p = isl_print_space(space, p, 0, &data);
2278 p = isl_printer_print_str(p, " }");
2280 return p;
2281 error:
2282 isl_printer_free(p);
2283 return NULL;
2286 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
2287 __isl_keep isl_space *space)
2289 if (!p || !space)
2290 return isl_printer_free(p);
2291 if (p->output_format == ISL_FORMAT_ISL)
2292 return isl_printer_print_space_isl(p, space);
2293 else if (p->output_format == ISL_FORMAT_OMEGA)
2294 return print_omega_parameters(space, p);
2296 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
2297 "output format not supported for space",
2298 return isl_printer_free(p));
2301 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
2302 __isl_keep isl_local_space *ls)
2304 struct isl_print_space_data data = { 0 };
2305 unsigned n_div;
2307 if (!ls)
2308 goto error;
2310 if (isl_local_space_dim(ls, isl_dim_param) > 0) {
2311 p = print_tuple(ls->dim, p, isl_dim_param, &data);
2312 p = isl_printer_print_str(p, " -> ");
2314 p = isl_printer_print_str(p, "{ ");
2315 p = isl_print_space(ls->dim, p, 0, &data);
2316 n_div = isl_local_space_dim(ls, isl_dim_div);
2317 if (n_div > 0) {
2318 p = isl_printer_print_str(p, " : ");
2319 p = isl_printer_print_str(p, s_open_exists[0]);
2320 p = print_div_list(p, ls->dim, ls->div, 0, 1);
2321 p = isl_printer_print_str(p, s_close_exists[0]);
2322 } else if (isl_space_is_params(ls->dim))
2323 p = isl_printer_print_str(p, s_such_that[0]);
2324 p = isl_printer_print_str(p, " }");
2325 return p;
2326 error:
2327 isl_printer_free(p);
2328 return NULL;
2331 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2332 __isl_keep isl_aff *aff)
2334 unsigned total;
2336 if (isl_aff_is_nan(aff))
2337 return isl_printer_print_str(p, "NaN");
2339 total = isl_local_space_dim(aff->ls, isl_dim_all);
2340 p = isl_printer_print_str(p, "(");
2341 p = print_affine_of_len(aff->ls->dim, aff->ls->div, p,
2342 aff->v->el + 1, 1 + total);
2343 if (isl_int_is_one(aff->v->el[0]))
2344 p = isl_printer_print_str(p, ")");
2345 else {
2346 p = isl_printer_print_str(p, ")/");
2347 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2350 return p;
2353 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2354 __isl_keep isl_aff *aff)
2356 struct isl_print_space_data data = { 0 };
2358 if (isl_space_is_params(aff->ls->dim))
2360 else {
2361 p = print_tuple(aff->ls->dim, p, isl_dim_set, &data);
2362 p = isl_printer_print_str(p, " -> ");
2364 p = isl_printer_print_str(p, "[");
2365 p = print_aff_body(p, aff);
2366 p = isl_printer_print_str(p, "]");
2368 return p;
2371 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2372 __isl_keep isl_aff *aff)
2374 struct isl_print_space_data data = { 0 };
2376 if (!aff)
2377 goto error;
2379 if (isl_local_space_dim(aff->ls, isl_dim_param) > 0) {
2380 p = print_tuple(aff->ls->dim, p, isl_dim_param, &data);
2381 p = isl_printer_print_str(p, " -> ");
2383 p = isl_printer_print_str(p, "{ ");
2384 p = print_aff(p, aff);
2385 p = isl_printer_print_str(p, " }");
2386 return p;
2387 error:
2388 isl_printer_free(p);
2389 return NULL;
2392 /* Print the body of an isl_pw_aff, i.e., a semicolon delimited
2393 * sequence of affine expressions, each followed by constraints.
2395 static __isl_give isl_printer *print_pw_aff_body(
2396 __isl_take isl_printer *p, __isl_keep isl_pw_aff *pa)
2398 int i;
2400 if (!pa)
2401 return isl_printer_free(p);
2403 for (i = 0; i < pa->n; ++i) {
2404 isl_space *space;
2406 if (i)
2407 p = isl_printer_print_str(p, "; ");
2408 p = print_aff(p, pa->p[i].aff);
2409 space = isl_aff_get_domain_space(pa->p[i].aff);
2410 p = print_disjuncts((isl_map *)pa->p[i].set, space, p, 0);
2411 isl_space_free(space);
2414 return p;
2417 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2418 __isl_keep isl_pw_aff *pwaff)
2420 struct isl_print_space_data data = { 0 };
2422 if (!pwaff)
2423 goto error;
2425 if (isl_space_dim(pwaff->dim, isl_dim_param) > 0) {
2426 p = print_tuple(pwaff->dim, p, isl_dim_param, &data);
2427 p = isl_printer_print_str(p, " -> ");
2429 p = isl_printer_print_str(p, "{ ");
2430 p = print_pw_aff_body(p, pwaff);
2431 p = isl_printer_print_str(p, " }");
2432 return p;
2433 error:
2434 isl_printer_free(p);
2435 return NULL;
2438 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2439 __isl_keep isl_local_space *ls, isl_int *c);
2441 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2442 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2444 if (type == isl_dim_div) {
2445 p = isl_printer_print_str(p, "floord(");
2446 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2447 p = isl_printer_print_str(p, ", ");
2448 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2449 p = isl_printer_print_str(p, ")");
2450 } else {
2451 const char *name;
2453 name = isl_space_get_dim_name(ls->dim, type, pos);
2454 if (!name)
2455 name = "UNNAMED";
2456 p = isl_printer_print_str(p, name);
2458 return p;
2461 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2462 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2464 enum isl_dim_type type;
2466 if (pos == 0)
2467 return isl_printer_print_isl_int(p, c);
2469 if (isl_int_is_one(c))
2471 else if (isl_int_is_negone(c))
2472 p = isl_printer_print_str(p, "-");
2473 else {
2474 p = isl_printer_print_isl_int(p, c);
2475 p = isl_printer_print_str(p, "*");
2477 type = pos2type(ls->dim, &pos);
2478 p = print_ls_name_c(p, ls, type, pos);
2479 return p;
2482 static __isl_give isl_printer *print_ls_partial_affine_c(
2483 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2484 isl_int *c, unsigned len)
2486 int i;
2487 int first;
2489 for (i = 0, first = 1; i < len; ++i) {
2490 int flip = 0;
2491 if (isl_int_is_zero(c[i]))
2492 continue;
2493 if (!first) {
2494 if (isl_int_is_neg(c[i])) {
2495 flip = 1;
2496 isl_int_neg(c[i], c[i]);
2497 p = isl_printer_print_str(p, " - ");
2498 } else
2499 p = isl_printer_print_str(p, " + ");
2501 first = 0;
2502 p = print_ls_term_c(p, ls, c[i], i);
2503 if (flip)
2504 isl_int_neg(c[i], c[i]);
2506 if (first)
2507 p = isl_printer_print_str(p, "0");
2508 return p;
2511 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2512 __isl_keep isl_local_space *ls, isl_int *c)
2514 unsigned len = 1 + isl_local_space_dim(ls, isl_dim_all);
2515 return print_ls_partial_affine_c(p, ls, c, len);
2518 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2519 __isl_keep isl_aff *aff)
2521 unsigned total;
2523 total = isl_local_space_dim(aff->ls, isl_dim_all);
2524 if (!isl_int_is_one(aff->v->el[0]))
2525 p = isl_printer_print_str(p, "(");
2526 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2527 if (!isl_int_is_one(aff->v->el[0])) {
2528 p = isl_printer_print_str(p, ")/");
2529 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2531 return p;
2534 /* In the C format, we cannot express that "pwaff" may be undefined
2535 * on parts of the domain space. We therefore assume that the expression
2536 * will only be evaluated on its definition domain and compute the gist
2537 * of each cell with respect to this domain.
2539 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2540 __isl_keep isl_pw_aff *pwaff)
2542 isl_set *domain;
2543 isl_ast_build *build;
2544 isl_ast_expr *expr;
2546 if (pwaff->n < 1)
2547 isl_die(p->ctx, isl_error_unsupported,
2548 "cannot print empty isl_pw_aff in C format",
2549 return isl_printer_free(p));
2551 domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
2552 build = isl_ast_build_from_context(domain);
2553 expr = isl_ast_build_expr_from_pw_aff(build, isl_pw_aff_copy(pwaff));
2554 p = isl_printer_print_ast_expr(p, expr);
2555 isl_ast_expr_free(expr);
2556 isl_ast_build_free(build);
2558 return p;
2561 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2562 __isl_keep isl_aff *aff)
2564 if (!p || !aff)
2565 goto error;
2567 if (p->output_format == ISL_FORMAT_ISL)
2568 return print_aff_isl(p, aff);
2569 else if (p->output_format == ISL_FORMAT_C)
2570 return print_aff_c(p, aff);
2571 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2572 goto error);
2573 error:
2574 isl_printer_free(p);
2575 return NULL;
2578 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2579 __isl_keep isl_pw_aff *pwaff)
2581 if (!p || !pwaff)
2582 goto error;
2584 if (p->output_format == ISL_FORMAT_ISL)
2585 return print_pw_aff_isl(p, pwaff);
2586 else if (p->output_format == ISL_FORMAT_C)
2587 return print_pw_aff_c(p, pwaff);
2588 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2589 goto error);
2590 error:
2591 isl_printer_free(p);
2592 return NULL;
2595 /* Print "pa" in a sequence of isl_pw_affs delimited by semicolons.
2596 * Each isl_pw_aff itself is also printed as semicolon delimited
2597 * sequence of pieces.
2598 * If data->first = 1, then this is the first in the sequence.
2599 * Update data->first to tell the next element that it is not the first.
2601 static isl_stat print_pw_aff_body_wrap(__isl_take isl_pw_aff *pa,
2602 void *user)
2604 struct isl_union_print_data *data;
2605 data = (struct isl_union_print_data *) user;
2607 if (!data->first)
2608 data->p = isl_printer_print_str(data->p, "; ");
2609 data->first = 0;
2611 data->p = print_pw_aff_body(data->p, pa);
2612 isl_pw_aff_free(pa);
2614 return data->p ? isl_stat_ok : isl_stat_error;
2617 /* Print the body of an isl_union_pw_aff, i.e., a semicolon delimited
2618 * sequence of affine expressions, each followed by constraints,
2619 * with the sequence enclosed in braces.
2621 static __isl_give isl_printer *print_union_pw_aff_body(
2622 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2624 struct isl_union_print_data data = { p, 1 };
2626 p = isl_printer_print_str(p, s_open_set[0]);
2627 data.p = p;
2628 if (isl_union_pw_aff_foreach_pw_aff(upa,
2629 &print_pw_aff_body_wrap, &data) < 0)
2630 data.p = isl_printer_free(p);
2631 p = data.p;
2632 p = isl_printer_print_str(p, s_close_set[0]);
2634 return p;
2637 /* Print the isl_union_pw_aff "upa" to "p" in isl format.
2639 * The individual isl_pw_affs are delimited by a semicolon.
2641 static __isl_give isl_printer *print_union_pw_aff_isl(
2642 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2644 struct isl_print_space_data data = { 0 };
2645 isl_space *space;
2647 space = isl_union_pw_aff_get_space(upa);
2648 if (isl_space_dim(space, isl_dim_param) > 0) {
2649 p = print_tuple(space, p, isl_dim_param, &data);
2650 p = isl_printer_print_str(p, s_to[0]);
2652 isl_space_free(space);
2653 p = print_union_pw_aff_body(p, upa);
2654 return p;
2657 /* Print the isl_union_pw_aff "upa" to "p".
2659 * We currently only support an isl format.
2661 __isl_give isl_printer *isl_printer_print_union_pw_aff(
2662 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2664 if (!p || !upa)
2665 return isl_printer_free(p);
2667 if (p->output_format == ISL_FORMAT_ISL)
2668 return print_union_pw_aff_isl(p, upa);
2669 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2670 "unsupported output format", return isl_printer_free(p));
2673 /* Print dimension "pos" of data->space to "p".
2675 * data->user is assumed to be an isl_multi_aff.
2677 * If the current dimension is an output dimension, then print
2678 * the corresponding expression. Otherwise, print the name of the dimension.
2680 static __isl_give isl_printer *print_dim_ma(__isl_take isl_printer *p,
2681 struct isl_print_space_data *data, unsigned pos)
2683 isl_multi_aff *ma = data->user;
2685 if (data->type == isl_dim_out)
2686 p = print_aff_body(p, ma->p[pos]);
2687 else
2688 p = print_name(data->space, p, data->type, pos, data->latex);
2690 return p;
2693 static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
2694 __isl_keep isl_multi_aff *maff)
2696 struct isl_print_space_data data = { 0 };
2698 data.print_dim = &print_dim_ma;
2699 data.user = maff;
2700 return isl_print_space(maff->space, p, 0, &data);
2703 static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
2704 __isl_keep isl_multi_aff *maff)
2706 struct isl_print_space_data data = { 0 };
2708 if (!maff)
2709 goto error;
2711 if (isl_space_dim(maff->space, isl_dim_param) > 0) {
2712 p = print_tuple(maff->space, p, isl_dim_param, &data);
2713 p = isl_printer_print_str(p, " -> ");
2715 p = isl_printer_print_str(p, "{ ");
2716 p = print_multi_aff(p, maff);
2717 p = isl_printer_print_str(p, " }");
2718 return p;
2719 error:
2720 isl_printer_free(p);
2721 return NULL;
2724 __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
2725 __isl_keep isl_multi_aff *maff)
2727 if (!p || !maff)
2728 goto error;
2730 if (p->output_format == ISL_FORMAT_ISL)
2731 return print_multi_aff_isl(p, maff);
2732 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2733 goto error);
2734 error:
2735 isl_printer_free(p);
2736 return NULL;
2739 static __isl_give isl_printer *print_pw_multi_aff_body(
2740 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2742 int i;
2744 if (!pma)
2745 goto error;
2747 for (i = 0; i < pma->n; ++i) {
2748 isl_space *space;
2750 if (i)
2751 p = isl_printer_print_str(p, "; ");
2752 p = print_multi_aff(p, pma->p[i].maff);
2753 space = isl_multi_aff_get_domain_space(pma->p[i].maff);
2754 p = print_disjuncts((isl_map *)pma->p[i].set, space, p, 0);
2755 isl_space_free(space);
2757 return p;
2758 error:
2759 isl_printer_free(p);
2760 return NULL;
2763 static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
2764 __isl_keep isl_pw_multi_aff *pma)
2766 struct isl_print_space_data data = { 0 };
2768 if (!pma)
2769 goto error;
2771 if (isl_space_dim(pma->dim, isl_dim_param) > 0) {
2772 p = print_tuple(pma->dim, p, isl_dim_param, &data);
2773 p = isl_printer_print_str(p, " -> ");
2775 p = isl_printer_print_str(p, "{ ");
2776 p = print_pw_multi_aff_body(p, pma);
2777 p = isl_printer_print_str(p, " }");
2778 return p;
2779 error:
2780 isl_printer_free(p);
2781 return NULL;
2784 static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
2785 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2787 int i;
2789 for (i = 0; i < pma->n - 1; ++i) {
2790 p = isl_printer_print_str(p, "(");
2791 p = print_set_c(p, pma->dim, pma->p[i].set);
2792 p = isl_printer_print_str(p, ") ? (");
2793 p = print_aff_c(p, pma->p[i].maff->p[0]);
2794 p = isl_printer_print_str(p, ") : ");
2797 return print_aff_c(p, pma->p[pma->n - 1].maff->p[0]);
2800 static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
2801 __isl_keep isl_pw_multi_aff *pma)
2803 int n;
2804 const char *name;
2806 if (!pma)
2807 goto error;
2808 if (pma->n < 1)
2809 isl_die(p->ctx, isl_error_unsupported,
2810 "cannot print empty isl_pw_multi_aff in C format",
2811 goto error);
2812 name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
2813 if (!name && isl_pw_multi_aff_dim(pma, isl_dim_out) == 1)
2814 return print_unnamed_pw_multi_aff_c(p, pma);
2815 if (!name)
2816 isl_die(p->ctx, isl_error_unsupported,
2817 "cannot print unnamed isl_pw_multi_aff in C format",
2818 goto error);
2820 p = isl_printer_print_str(p, name);
2821 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
2822 if (n != 0)
2823 isl_die(p->ctx, isl_error_unsupported,
2824 "not supported yet", goto error);
2826 return p;
2827 error:
2828 isl_printer_free(p);
2829 return NULL;
2832 __isl_give isl_printer *isl_printer_print_pw_multi_aff(
2833 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2835 if (!p || !pma)
2836 goto error;
2838 if (p->output_format == ISL_FORMAT_ISL)
2839 return print_pw_multi_aff_isl(p, pma);
2840 if (p->output_format == ISL_FORMAT_C)
2841 return print_pw_multi_aff_c(p, pma);
2842 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2843 goto error);
2844 error:
2845 isl_printer_free(p);
2846 return NULL;
2849 static isl_stat print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
2850 void *user)
2852 struct isl_union_print_data *data;
2853 data = (struct isl_union_print_data *) user;
2855 if (!data->first)
2856 data->p = isl_printer_print_str(data->p, "; ");
2857 data->first = 0;
2859 data->p = print_pw_multi_aff_body(data->p, pma);
2860 isl_pw_multi_aff_free(pma);
2862 return isl_stat_ok;
2865 static __isl_give isl_printer *print_union_pw_multi_aff_isl(
2866 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2868 struct isl_union_print_data data;
2869 struct isl_print_space_data space_data = { 0 };
2870 isl_space *space;
2872 space = isl_union_pw_multi_aff_get_space(upma);
2873 if (isl_space_dim(space, isl_dim_param) > 0) {
2874 p = print_tuple(space, p, isl_dim_param, &space_data);
2875 p = isl_printer_print_str(p, s_to[0]);
2877 isl_space_free(space);
2878 p = isl_printer_print_str(p, s_open_set[0]);
2879 data.p = p;
2880 data.first = 1;
2881 isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
2882 &print_pw_multi_aff_body_wrap, &data);
2883 p = data.p;
2884 p = isl_printer_print_str(p, s_close_set[0]);
2885 return p;
2888 __isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
2889 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2891 if (!p || !upma)
2892 goto error;
2894 if (p->output_format == ISL_FORMAT_ISL)
2895 return print_union_pw_multi_aff_isl(p, upma);
2896 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2897 goto error);
2898 error:
2899 isl_printer_free(p);
2900 return NULL;
2903 /* Print dimension "pos" of data->space to "p".
2905 * data->user is assumed to be an isl_multi_pw_aff.
2907 * If the current dimension is an output dimension, then print
2908 * the corresponding piecewise affine expression.
2909 * Otherwise, print the name of the dimension.
2911 static __isl_give isl_printer *print_dim_mpa(__isl_take isl_printer *p,
2912 struct isl_print_space_data *data, unsigned pos)
2914 int i;
2915 int need_parens;
2916 isl_multi_pw_aff *mpa = data->user;
2917 isl_pw_aff *pa;
2919 if (data->type != isl_dim_out)
2920 return print_name(data->space, p, data->type, pos, data->latex);
2922 pa = mpa->p[pos];
2923 if (pa->n == 0)
2924 return isl_printer_print_str(p, "(0 : 1 = 0)");
2926 need_parens = pa->n != 1 || !isl_set_plain_is_universe(pa->p[0].set);
2927 if (need_parens)
2928 p = isl_printer_print_str(p, "(");
2929 for (i = 0; i < pa->n; ++i) {
2930 isl_space *space;
2932 if (i)
2933 p = isl_printer_print_str(p, "; ");
2934 p = print_aff_body(p, pa->p[i].aff);
2935 space = isl_aff_get_domain_space(pa->p[i].aff);
2936 p = print_disjuncts(pa->p[i].set, space, p, 0);
2937 isl_space_free(space);
2939 if (need_parens)
2940 p = isl_printer_print_str(p, ")");
2942 return p;
2945 /* Print "mpa" to "p" in isl format.
2947 static __isl_give isl_printer *print_multi_pw_aff_isl(__isl_take isl_printer *p,
2948 __isl_keep isl_multi_pw_aff *mpa)
2950 struct isl_print_space_data data = { 0 };
2952 if (!mpa)
2953 return isl_printer_free(p);
2955 if (isl_space_dim(mpa->space, isl_dim_param) > 0) {
2956 p = print_tuple(mpa->space, p, isl_dim_param, &data);
2957 p = isl_printer_print_str(p, " -> ");
2959 p = isl_printer_print_str(p, "{ ");
2960 data.print_dim = &print_dim_mpa;
2961 data.user = mpa;
2962 p = isl_print_space(mpa->space, p, 0, &data);
2963 p = isl_printer_print_str(p, " }");
2964 return p;
2967 __isl_give isl_printer *isl_printer_print_multi_pw_aff(
2968 __isl_take isl_printer *p, __isl_keep isl_multi_pw_aff *mpa)
2970 if (!p || !mpa)
2971 return isl_printer_free(p);
2973 if (p->output_format == ISL_FORMAT_ISL)
2974 return print_multi_pw_aff_isl(p, mpa);
2975 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2976 return isl_printer_free(p));
2979 /* Print dimension "pos" of data->space to "p".
2981 * data->user is assumed to be an isl_multi_val.
2983 * If the current dimension is an output dimension, then print
2984 * the corresponding value. Otherwise, print the name of the dimension.
2986 static __isl_give isl_printer *print_dim_mv(__isl_take isl_printer *p,
2987 struct isl_print_space_data *data, unsigned pos)
2989 isl_multi_val *mv = data->user;
2991 if (data->type == isl_dim_out)
2992 return isl_printer_print_val(p, mv->p[pos]);
2993 else
2994 return print_name(data->space, p, data->type, pos, data->latex);
2997 /* Print the isl_multi_val "mv" to "p" in isl format.
2999 static __isl_give isl_printer *print_multi_val_isl(__isl_take isl_printer *p,
3000 __isl_keep isl_multi_val *mv)
3002 struct isl_print_space_data data = { 0 };
3004 if (!mv)
3005 return isl_printer_free(p);
3007 if (isl_space_dim(mv->space, isl_dim_param) > 0) {
3008 p = print_tuple(mv->space, p, isl_dim_param, &data);
3009 p = isl_printer_print_str(p, " -> ");
3011 p = isl_printer_print_str(p, "{ ");
3012 data.print_dim = &print_dim_mv;
3013 data.user = mv;
3014 p = isl_print_space(mv->space, p, 0, &data);
3015 p = isl_printer_print_str(p, " }");
3016 return p;
3019 /* Print the isl_multi_val "mv" to "p".
3021 * Currently only supported in isl format.
3023 __isl_give isl_printer *isl_printer_print_multi_val(
3024 __isl_take isl_printer *p, __isl_keep isl_multi_val *mv)
3026 if (!p || !mv)
3027 return isl_printer_free(p);
3029 if (p->output_format == ISL_FORMAT_ISL)
3030 return print_multi_val_isl(p, mv);
3031 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3032 return isl_printer_free(p));
3035 /* Print dimension "pos" of data->space to "p".
3037 * data->user is assumed to be an isl_multi_union_pw_aff.
3039 * The current dimension is necessarily a set dimension, so
3040 * we print the corresponding isl_union_pw_aff, including
3041 * the braces.
3043 static __isl_give isl_printer *print_union_pw_aff_dim(__isl_take isl_printer *p,
3044 struct isl_print_space_data *data, unsigned pos)
3046 isl_multi_union_pw_aff *mupa = data->user;
3047 isl_union_pw_aff *upa;
3049 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, pos);
3050 p = print_union_pw_aff_body(p, upa);
3051 isl_union_pw_aff_free(upa);
3053 return p;
3056 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3058 static __isl_give isl_printer *print_multi_union_pw_aff_isl(
3059 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3061 struct isl_print_space_data data = { 0 };
3062 isl_space *space;
3064 space = isl_multi_union_pw_aff_get_space(mupa);
3065 if (isl_space_dim(space, isl_dim_param) > 0) {
3066 struct isl_print_space_data space_data = { 0 };
3067 p = print_tuple(space, p, isl_dim_param, &space_data);
3068 p = isl_printer_print_str(p, s_to[0]);
3071 data.print_dim = &print_union_pw_aff_dim;
3072 data.user = mupa;
3074 p = isl_print_space(space, p, 0, &data);
3075 isl_space_free(space);
3077 return p;
3080 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3082 * We currently only support an isl format.
3084 __isl_give isl_printer *isl_printer_print_multi_union_pw_aff(
3085 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3087 if (!p || !mupa)
3088 return isl_printer_free(p);
3090 if (p->output_format == ISL_FORMAT_ISL)
3091 return print_multi_union_pw_aff_isl(p, mupa);
3092 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
3093 "unsupported output format", return isl_printer_free(p));