isl_mat_inverse_product: add memory management annotations
[isl.git] / isl_output.c
blob93442a95201103cc67df6a3ac7719f2395541056
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_constraint_private.h>
32 #include <isl/ast_build.h>
33 #include <isl_sort.h>
34 #include <isl_output_private.h>
36 #include <bset_to_bmap.c>
37 #include <set_to_map.c>
39 static const char *s_to[2] = { " -> ", " \\to " };
40 static const char *s_and[2] = { " and ", " \\wedge " };
41 static const char *s_or[2] = { " or ", " \\vee " };
42 static const char *s_le[2] = { "<=", "\\le" };
43 static const char *s_ge[2] = { ">=", "\\ge" };
44 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
45 static const char *s_close_set[2] = { " }", " \\,\\}" };
46 static const char *s_open_list[2] = { "[", "(" };
47 static const char *s_close_list[2] = { "]", ")" };
48 static const char *s_such_that[2] = { " : ", " \\mid " };
49 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
50 static const char *s_close_exists[2] = { ")", "" };
51 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
52 static const char *s_param_prefix[2] = { "p", "p_" };
53 static const char *s_input_prefix[2] = { "i", "i_" };
54 static const char *s_output_prefix[2] = { "o", "o_" };
56 static __isl_give isl_printer *print_constraint_polylib(
57 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
59 int i;
60 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
61 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
62 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
63 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
65 p = isl_printer_start_line(p);
66 p = isl_printer_print_int(p, ineq);
67 for (i = 0; i < n_out; ++i) {
68 p = isl_printer_print_str(p, " ");
69 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
71 for (i = 0; i < n_in; ++i) {
72 p = isl_printer_print_str(p, " ");
73 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
75 for (i = 0; i < bmap->n_div; ++i) {
76 p = isl_printer_print_str(p, " ");
77 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
79 for (i = 0; i < nparam; ++i) {
80 p = isl_printer_print_str(p, " ");
81 p = isl_printer_print_isl_int(p, c[1+i]);
83 p = isl_printer_print_str(p, " ");
84 p = isl_printer_print_isl_int(p, c[0]);
85 p = isl_printer_end_line(p);
86 return p;
89 static __isl_give isl_printer *print_constraints_polylib(
90 struct isl_basic_map *bmap, __isl_take isl_printer *p)
92 int i;
94 p = isl_printer_set_isl_int_width(p, 5);
96 for (i = 0; i < bmap->n_eq; ++i)
97 p = print_constraint_polylib(bmap, 0, i, p);
98 for (i = 0; i < bmap->n_ineq; ++i)
99 p = print_constraint_polylib(bmap, 1, i, p);
101 return p;
104 static __isl_give isl_printer *bset_print_constraints_polylib(
105 struct isl_basic_set *bset, __isl_take isl_printer *p)
107 return print_constraints_polylib(bset_to_bmap(bset), p);
110 static __isl_give isl_printer *isl_basic_map_print_polylib(
111 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
113 unsigned total = isl_basic_map_total_dim(bmap);
114 p = isl_printer_start_line(p);
115 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
116 p = isl_printer_print_str(p, " ");
117 p = isl_printer_print_int(p, 1 + total + 1);
118 if (ext) {
119 p = isl_printer_print_str(p, " ");
120 p = isl_printer_print_int(p,
121 isl_basic_map_dim(bmap, isl_dim_out));
122 p = isl_printer_print_str(p, " ");
123 p = isl_printer_print_int(p,
124 isl_basic_map_dim(bmap, isl_dim_in));
125 p = isl_printer_print_str(p, " ");
126 p = isl_printer_print_int(p,
127 isl_basic_map_dim(bmap, isl_dim_div));
128 p = isl_printer_print_str(p, " ");
129 p = isl_printer_print_int(p,
130 isl_basic_map_dim(bmap, isl_dim_param));
132 p = isl_printer_end_line(p);
133 return print_constraints_polylib(bmap, p);
136 static __isl_give isl_printer *isl_basic_set_print_polylib(
137 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
139 return isl_basic_map_print_polylib(bset_to_bmap(bset), p, ext);
142 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
143 __isl_take isl_printer *p, int ext)
145 int i;
147 p = isl_printer_start_line(p);
148 p = isl_printer_print_int(p, map->n);
149 p = isl_printer_end_line(p);
150 for (i = 0; i < map->n; ++i) {
151 p = isl_printer_start_line(p);
152 p = isl_printer_end_line(p);
153 p = isl_basic_map_print_polylib(map->p[i], p, ext);
155 return p;
158 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
159 __isl_take isl_printer *p, int ext)
161 return isl_map_print_polylib(set_to_map(set), p, ext);
164 static int count_same_name(__isl_keep isl_space *dim,
165 enum isl_dim_type type, unsigned pos, const char *name)
167 enum isl_dim_type t;
168 unsigned p, s;
169 int count = 0;
171 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
172 s = t == type ? pos : isl_space_dim(dim, t);
173 for (p = 0; p < s; ++p) {
174 const char *n = isl_space_get_dim_name(dim, t, p);
175 if (n && !strcmp(n, name))
176 count++;
179 return count;
182 /* Print the name of the variable of type "type" and position "pos"
183 * in "space" to "p".
185 static __isl_give isl_printer *print_name(__isl_keep isl_space *space,
186 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
187 int latex)
189 const char *name;
190 char buffer[20];
191 int primes;
193 name = type == isl_dim_div ? NULL
194 : isl_space_get_dim_name(space, type, pos);
196 if (!name) {
197 const char *prefix;
198 if (type == isl_dim_param)
199 prefix = s_param_prefix[latex];
200 else if (type == isl_dim_div)
201 prefix = s_div_prefix[latex];
202 else if (isl_space_is_set(space) || type == isl_dim_in)
203 prefix = s_input_prefix[latex];
204 else
205 prefix = s_output_prefix[latex];
206 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
207 name = buffer;
209 primes = count_same_name(space, name == buffer ? isl_dim_div : type,
210 pos, name);
211 p = isl_printer_print_str(p, name);
212 while (primes-- > 0)
213 p = isl_printer_print_str(p, "'");
214 return p;
217 static enum isl_dim_type pos2type(__isl_keep isl_space *dim, unsigned *pos)
219 enum isl_dim_type type;
220 unsigned n_in = isl_space_dim(dim, isl_dim_in);
221 unsigned n_out = isl_space_dim(dim, isl_dim_out);
222 unsigned nparam = isl_space_dim(dim, isl_dim_param);
224 if (*pos < 1 + nparam) {
225 type = isl_dim_param;
226 *pos -= 1;
227 } else if (*pos < 1 + nparam + n_in) {
228 type = isl_dim_in;
229 *pos -= 1 + nparam;
230 } else if (*pos < 1 + nparam + n_in + n_out) {
231 type = isl_dim_out;
232 *pos -= 1 + nparam + n_in;
233 } else {
234 type = isl_dim_div;
235 *pos -= 1 + nparam + n_in + n_out;
238 return type;
241 /* Can the div expression of the integer division at position "row" of "div"
242 * be printed?
243 * In particular, are the div expressions available and does the selected
244 * variable have a known explicit representation?
245 * Furthermore, the Omega format does not allow any div expressions
246 * to be printed.
248 static isl_bool can_print_div_expr(__isl_keep isl_printer *p,
249 __isl_keep isl_mat *div, int pos)
251 if (p->output_format == ISL_FORMAT_OMEGA)
252 return isl_bool_false;
253 if (!div)
254 return isl_bool_false;
255 return !isl_int_is_zero(div->row[pos][0]);
258 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
259 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
261 static __isl_give isl_printer *print_term(__isl_keep isl_space *space,
262 __isl_keep isl_mat *div,
263 isl_int c, unsigned pos, __isl_take isl_printer *p, int latex)
265 enum isl_dim_type type;
266 int print_div_def;
268 if (pos == 0)
269 return isl_printer_print_isl_int(p, c);
271 type = pos2type(space, &pos);
272 print_div_def = type == isl_dim_div && can_print_div_expr(p, div, pos);
274 if (isl_int_is_one(c))
276 else if (isl_int_is_negone(c))
277 p = isl_printer_print_str(p, "-");
278 else {
279 p = isl_printer_print_isl_int(p, c);
280 if (p->output_format == ISL_FORMAT_C || print_div_def)
281 p = isl_printer_print_str(p, "*");
283 if (print_div_def)
284 p = print_div(space, div, pos, p);
285 else
286 p = print_name(space, p, type, pos, latex);
287 return p;
290 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_space *dim,
291 __isl_keep isl_mat *div,
292 __isl_take isl_printer *p, isl_int *c, int len)
294 int i;
295 int first;
297 for (i = 0, first = 1; i < len; ++i) {
298 int flip = 0;
299 if (isl_int_is_zero(c[i]))
300 continue;
301 if (!first) {
302 if (isl_int_is_neg(c[i])) {
303 flip = 1;
304 isl_int_neg(c[i], c[i]);
305 p = isl_printer_print_str(p, " - ");
306 } else
307 p = isl_printer_print_str(p, " + ");
309 first = 0;
310 p = print_term(dim, div, c[i], i, p, 0);
311 if (flip)
312 isl_int_neg(c[i], c[i]);
314 if (first)
315 p = isl_printer_print_str(p, "0");
316 return p;
319 /* Print an affine expression "c" corresponding to a constraint in "bmap"
320 * to "p", with the variable names taken from "space" and
321 * the integer division definitions taken from "div".
323 static __isl_give isl_printer *print_affine(__isl_keep isl_basic_map *bmap,
324 __isl_keep isl_space *space, __isl_keep isl_mat *div,
325 __isl_take isl_printer *p, isl_int *c)
327 unsigned len = 1 + isl_basic_map_total_dim(bmap);
328 return print_affine_of_len(space, div, p, c, len);
331 /* offset is the offset of local_dim inside data->type of data->space.
333 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
334 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
335 struct isl_print_space_data *data, int offset)
337 int i;
339 if (data->space != local_dim && local_type == isl_dim_out)
340 offset += local_dim->n_in;
342 for (i = 0; i < isl_space_dim(local_dim, local_type); ++i) {
343 if (i)
344 p = isl_printer_print_str(p, ", ");
345 if (data->print_dim)
346 p = data->print_dim(p, data, offset + i);
347 else
348 p = print_name(data->space, p, data->type, offset + i,
349 data->latex);
351 return p;
354 static __isl_give isl_printer *print_var_list(__isl_take isl_printer *p,
355 __isl_keep isl_space *space, enum isl_dim_type type)
357 struct isl_print_space_data data = { .space = space, .type = type };
359 return print_nested_var_list(p, space, type, &data, 0);
362 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
363 __isl_keep isl_space *local_dim,
364 struct isl_print_space_data *data, int offset);
366 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
367 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
368 struct isl_print_space_data *data, int offset)
370 const char *name = NULL;
371 unsigned n = isl_space_dim(local_dim, local_type);
372 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
373 name = isl_space_get_tuple_name(local_dim, local_type);
374 if (name) {
375 if (data->latex)
376 p = isl_printer_print_str(p, "\\mathrm{");
377 p = isl_printer_print_str(p, name);
378 if (data->latex)
379 p = isl_printer_print_str(p, "}");
382 if (!data->latex || n != 1 || name)
383 p = isl_printer_print_str(p, s_open_list[data->latex]);
384 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
385 local_dim->nested[local_type - isl_dim_in]) {
386 if (data->space != local_dim && local_type == isl_dim_out)
387 offset += local_dim->n_in;
388 p = print_nested_map_dim(p,
389 local_dim->nested[local_type - isl_dim_in],
390 data, offset);
391 } else
392 p = print_nested_var_list(p, local_dim, local_type, data,
393 offset);
394 if (!data->latex || n != 1 || name)
395 p = isl_printer_print_str(p, s_close_list[data->latex]);
396 return p;
399 static __isl_give isl_printer *print_tuple(__isl_keep isl_space *dim,
400 __isl_take isl_printer *p, enum isl_dim_type type,
401 struct isl_print_space_data *data)
403 data->space = dim;
404 data->type = type;
405 return print_nested_tuple(p, dim, type, data, 0);
408 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
409 __isl_keep isl_space *local_dim,
410 struct isl_print_space_data *data, int offset)
412 p = print_nested_tuple(p, local_dim, isl_dim_in, data, offset);
413 p = isl_printer_print_str(p, s_to[data->latex]);
414 p = print_nested_tuple(p, local_dim, isl_dim_out, data, offset);
416 return p;
419 __isl_give isl_printer *isl_print_space(__isl_keep isl_space *space,
420 __isl_take isl_printer *p, int rational,
421 struct isl_print_space_data *data)
423 if (rational && !data->latex)
424 p = isl_printer_print_str(p, "rat: ");
425 if (isl_space_is_params(space))
427 else if (isl_space_is_set(space))
428 p = print_tuple(space, p, isl_dim_set, data);
429 else {
430 p = print_tuple(space, p, isl_dim_in, data);
431 p = isl_printer_print_str(p, s_to[data->latex]);
432 p = print_tuple(space, p, isl_dim_out, data);
435 return p;
438 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_space *dim,
439 __isl_take isl_printer *p)
441 if (isl_space_dim(dim, isl_dim_param) == 0)
442 return p;
444 p = isl_printer_start_line(p);
445 p = isl_printer_print_str(p, "symbolic ");
446 p = print_var_list(p, dim, isl_dim_param);
447 p = isl_printer_print_str(p, ";");
448 p = isl_printer_end_line(p);
449 return p;
452 /* Does the inequality constraint following "i" in "bmap"
453 * have an opposite value for the same last coefficient?
454 * "last" is the position of the last coefficient of inequality "i".
455 * If the next constraint is a div constraint, then it is ignored
456 * since div constraints are not printed.
458 static int next_is_opposite(__isl_keep isl_basic_map *bmap, int i, int last)
460 unsigned total = isl_basic_map_total_dim(bmap);
461 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
463 if (i + 1 >= bmap->n_ineq)
464 return 0;
465 if (isl_seq_last_non_zero(bmap->ineq[i + 1], 1 + total) != last)
466 return 0;
467 if (last >= o_div) {
468 isl_bool is_div;
469 is_div = isl_basic_map_is_div_constraint(bmap,
470 bmap->ineq[i + 1], last - o_div);
471 if (is_div < 0)
472 return -1;
473 if (is_div)
474 return 0;
476 return isl_int_abs_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]) &&
477 !isl_int_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]);
480 /* Return a string representation of the operator used when
481 * printing a constraint where the LHS is greater than or equal to the LHS
482 * (sign > 0) or smaller than or equal to the LHS (sign < 0).
483 * If "strict" is set, then return the strict version of the comparison
484 * operator.
486 static const char *constraint_op(int sign, int strict, int latex)
488 if (strict)
489 return sign < 0 ? "<" : ">";
490 if (sign < 0)
491 return s_le[latex];
492 else
493 return s_ge[latex];
496 /* Print one side of a constraint "c" from "bmap" to "p", with
497 * the variable names taken from "space" and the integer division definitions
498 * taken from "div".
499 * "last" is the position of the last non-zero coefficient.
500 * Let c' be the result of zeroing out this coefficient, then
501 * the partial constraint
503 * c' op
505 * is printed.
506 * "first_constraint" is set if this is the first constraint
507 * in the conjunction.
509 static __isl_give isl_printer *print_half_constraint(
510 __isl_keep isl_basic_map *bmap,
511 __isl_keep isl_space *space, __isl_keep isl_mat *div,
512 __isl_take isl_printer *p, isl_int *c, int last, const char *op,
513 int first_constraint, int latex)
515 if (!first_constraint)
516 p = isl_printer_print_str(p, s_and[latex]);
518 isl_int_set_si(c[last], 0);
519 p = print_affine(bmap, space, div, p, c);
521 p = isl_printer_print_str(p, " ");
522 p = isl_printer_print_str(p, op);
523 p = isl_printer_print_str(p, " ");
525 return p;
528 /* Print a constraint "c" from "bmap" to "p", with the variable names
529 * taken from "space" and the integer division definitions taken from "div".
530 * "last" is the position of the last non-zero coefficient, which is
531 * moreover assumed to be negative.
532 * Let c' be the result of zeroing out this coefficient, then
533 * the constraint is printed in the form
535 * -c[last] op c'
537 * "first_constraint" is set if this is the first constraint
538 * in the conjunction.
540 static __isl_give isl_printer *print_constraint(__isl_keep isl_basic_map *bmap,
541 __isl_keep isl_space *space, __isl_keep isl_mat *div,
542 __isl_take isl_printer *p,
543 isl_int *c, int last, const char *op, int first_constraint, int latex)
545 if (!first_constraint)
546 p = isl_printer_print_str(p, s_and[latex]);
548 isl_int_abs(c[last], c[last]);
550 p = print_term(space, div, c[last], last, p, latex);
552 p = isl_printer_print_str(p, " ");
553 p = isl_printer_print_str(p, op);
554 p = isl_printer_print_str(p, " ");
556 isl_int_set_si(c[last], 0);
557 p = print_affine(bmap, space, div, p, c);
559 return p;
562 /* Print the constraints of "bmap" to "p".
563 * The names of the variables are taken from "space" and
564 * the integer division definitions are taken from "div".
565 * Div constraints are only printed in "dump" mode.
566 * The constraints are sorted prior to printing (except in "dump" mode).
568 * If x is the last variable with a non-zero coefficient,
569 * then a lower bound
571 * f - a x >= 0
573 * is printed as
575 * a x <= f
577 * while an upper bound
579 * f + a x >= 0
581 * is printed as
583 * a x >= -f
585 * If the next constraint has an opposite sign for the same last coefficient,
586 * then it is printed as
588 * f >= a x
590 * or
592 * -f <= a x
594 * instead. In fact, the "a x" part is not printed explicitly, but
595 * reused from the next constraint, which is therefore treated as
596 * a first constraint in the conjunction.
598 * If the constant term of "f" is -1, then "f" is replaced by "f + 1" and
599 * the comparison operator is replaced by the strict variant.
600 * Essentially, ">= 1" is replaced by "> 0".
602 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
603 __isl_keep isl_space *space, __isl_keep isl_mat *div,
604 __isl_take isl_printer *p, int latex)
606 int i;
607 isl_vec *c = NULL;
608 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
609 unsigned total = isl_basic_map_total_dim(bmap);
610 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
611 int first = 1;
612 int dump;
614 if (!p)
615 return NULL;
616 bmap = isl_basic_map_copy(bmap);
617 dump = p->dump;
618 if (!dump)
619 bmap = isl_basic_map_sort_constraints(bmap);
620 if (!bmap)
621 goto error;
623 c = isl_vec_alloc(bmap->ctx, 1 + total);
624 if (!c)
625 goto error;
627 for (i = bmap->n_eq - 1; i >= 0; --i) {
628 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
629 if (l < 0) {
630 if (i != bmap->n_eq - 1)
631 p = isl_printer_print_str(p, s_and[latex]);
632 p = isl_printer_print_str(p, "0 = 0");
633 continue;
635 if (isl_int_is_neg(bmap->eq[i][l]))
636 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
637 else
638 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
639 p = print_constraint(bmap, space, div, p, c->el, l,
640 "=", first, latex);
641 first = 0;
643 for (i = 0; i < bmap->n_ineq; ++i) {
644 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
645 int strict;
646 int s;
647 const char *op;
648 if (l < 0)
649 continue;
650 if (!dump && l >= o_div &&
651 can_print_div_expr(p, div, l - o_div)) {
652 isl_bool is_div;
653 is_div = isl_basic_map_is_div_constraint(bmap,
654 bmap->ineq[i], l - o_div);
655 if (is_div < 0)
656 goto error;
657 if (is_div)
658 continue;
660 s = isl_int_sgn(bmap->ineq[i][l]);
661 strict = !rational && isl_int_is_negone(bmap->ineq[i][0]);
662 if (s < 0)
663 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
664 else
665 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
666 if (strict)
667 isl_int_set_si(c->el[0], 0);
668 if (!dump && next_is_opposite(bmap, i, l)) {
669 op = constraint_op(-s, strict, latex);
670 p = print_half_constraint(bmap, space, div, p, c->el, l,
671 op, first, latex);
672 first = 1;
673 } else {
674 op = constraint_op(s, strict, latex);
675 p = print_constraint(bmap, space, div, p, c->el, l,
676 op, first, latex);
677 first = 0;
681 isl_basic_map_free(bmap);
682 isl_vec_free(c);
684 return p;
685 error:
686 isl_basic_map_free(bmap);
687 isl_vec_free(c);
688 isl_printer_free(p);
689 return NULL;
692 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
693 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
695 int c;
697 if (!p || !div)
698 return isl_printer_free(p);
700 c = p->output_format == ISL_FORMAT_C;
701 p = isl_printer_print_str(p, c ? "floord(" : "floor((");
702 p = print_affine_of_len(dim, div, p,
703 div->row[pos] + 1, div->n_col - 1);
704 p = isl_printer_print_str(p, c ? ", " : ")/");
705 p = isl_printer_print_isl_int(p, div->row[pos][0]);
706 p = isl_printer_print_str(p, ")");
707 return p;
710 /* Print a comma separated list of div names, except those that have
711 * a definition that can be printed.
712 * If "print_defined_divs" is set, then those div names are printed
713 * as well, along with their definitions.
715 static __isl_give isl_printer *print_div_list(__isl_take isl_printer *p,
716 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex,
717 int print_defined_divs)
719 int i;
720 int first = 1;
721 unsigned n_div;
723 if (!p || !space || !div)
724 return isl_printer_free(p);
726 n_div = isl_mat_rows(div);
728 for (i = 0; i < n_div; ++i) {
729 if (!print_defined_divs && can_print_div_expr(p, div, i))
730 continue;
731 if (!first)
732 p = isl_printer_print_str(p, ", ");
733 p = print_name(space, p, isl_dim_div, i, latex);
734 first = 0;
735 if (!can_print_div_expr(p, div, i))
736 continue;
737 p = isl_printer_print_str(p, " = ");
738 p = print_div(space, div, i, p);
741 return p;
744 /* Does printing an object with local variables described by "div"
745 * require an "exists" clause?
746 * That is, are there any local variables without an explicit representation?
747 * An exists clause is also needed in "dump" mode because
748 * explicit div representations are not printed inline in that case.
750 static isl_bool need_exists(__isl_keep isl_printer *p, __isl_keep isl_mat *div)
752 int i, n;
754 if (!p || !div)
755 return isl_bool_error;
756 n = isl_mat_rows(div);
757 if (n == 0)
758 return isl_bool_false;
759 if (p->dump)
760 return isl_bool_true;
761 for (i = 0; i < n; ++i)
762 if (!can_print_div_expr(p, div, i))
763 return isl_bool_true;
764 return isl_bool_false;
767 /* Print the start of an exists clause, i.e.,
769 * (exists variables:
771 * In dump mode, local variables with an explicit definition are printed
772 * as well because they will not be printed inline.
774 static __isl_give isl_printer *open_exists(__isl_take isl_printer *p,
775 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex)
777 int dump;
779 if (!p)
780 return NULL;
782 dump = p->dump;
783 p = isl_printer_print_str(p, s_open_exists[latex]);
784 p = print_div_list(p, space, div, latex, dump);
785 p = isl_printer_print_str(p, ": ");
787 return p;
790 /* Print the constraints of "bmap" to "p".
791 * The names of the variables are taken from "space".
792 * "latex" is set if the constraints should be printed in LaTeX format.
793 * Do not print inline explicit div representations in "dump" mode.
795 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
796 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
798 int dump;
799 isl_mat *div;
800 isl_bool exists;
802 if (!p)
803 return NULL;
804 dump = p->dump;
805 div = isl_basic_map_get_divs(bmap);
806 exists = need_exists(p, div);
807 if (exists >= 0 && exists)
808 p = open_exists(p, space, div, latex);
810 if (dump)
811 div = isl_mat_free(div);
812 p = print_constraints(bmap, space, div, p, latex);
813 isl_mat_free(div);
815 if (exists >= 0 && exists)
816 p = isl_printer_print_str(p, s_close_exists[latex]);
817 return p;
820 /* Print a colon followed by the constraints of "bmap"
821 * to "p", provided there are any constraints.
822 * The names of the variables are taken from "space".
823 * "latex" is set if the constraints should be printed in LaTeX format.
825 static __isl_give isl_printer *print_optional_disjunct(
826 __isl_keep isl_basic_map *bmap, __isl_keep isl_space *space,
827 __isl_take isl_printer *p, int latex)
829 if (isl_basic_map_plain_is_universe(bmap))
830 return p;
832 p = isl_printer_print_str(p, ": ");
833 p = print_disjunct(bmap, space, p, latex);
835 return p;
838 static __isl_give isl_printer *basic_map_print_omega(
839 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
841 p = isl_printer_print_str(p, "{ [");
842 p = print_var_list(p, bmap->dim, isl_dim_in);
843 p = isl_printer_print_str(p, "] -> [");
844 p = print_var_list(p, bmap->dim, isl_dim_out);
845 p = isl_printer_print_str(p, "] ");
846 p = print_optional_disjunct(bmap, bmap->dim, p, 0);
847 p = isl_printer_print_str(p, " }");
848 return p;
851 static __isl_give isl_printer *basic_set_print_omega(
852 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
854 p = isl_printer_print_str(p, "{ [");
855 p = print_var_list(p, bset->dim, isl_dim_set);
856 p = isl_printer_print_str(p, "] ");
857 p = print_optional_disjunct(bset, bset->dim, p, 0);
858 p = isl_printer_print_str(p, " }");
859 return p;
862 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
863 __isl_take isl_printer *p)
865 int i;
867 for (i = 0; i < map->n; ++i) {
868 if (i)
869 p = isl_printer_print_str(p, " union ");
870 p = basic_map_print_omega(map->p[i], p);
872 return p;
875 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
876 __isl_take isl_printer *p)
878 int i;
880 for (i = 0; i < set->n; ++i) {
881 if (i)
882 p = isl_printer_print_str(p, " union ");
883 p = basic_set_print_omega(set->p[i], p);
885 return p;
888 /* Print the list of parameters in "space", followed by an arrow, to "p",
889 * if there are any parameters.
891 static __isl_give isl_printer *print_param_tuple(__isl_take isl_printer *p,
892 __isl_keep isl_space *space, struct isl_print_space_data *data)
894 if (!p || !space)
895 return isl_printer_free(p);
896 if (isl_space_dim(space, isl_dim_param) == 0)
897 return p;
899 p = print_tuple(space, p, isl_dim_param, data);
900 p = isl_printer_print_str(p, s_to[data->latex]);
902 return p;
905 static __isl_give isl_printer *isl_basic_map_print_isl(
906 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
907 int latex)
909 struct isl_print_space_data data = { .latex = latex };
910 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
912 p = print_param_tuple(p, bmap->dim, &data);
913 p = isl_printer_print_str(p, "{ ");
914 p = isl_print_space(bmap->dim, p, rational, &data);
915 p = isl_printer_print_str(p, " : ");
916 p = print_disjunct(bmap, bmap->dim, p, latex);
917 p = isl_printer_print_str(p, " }");
918 return p;
921 /* Print the disjuncts of a map (or set) "map" to "p".
922 * The names of the variables are taken from "space".
923 * "latex" is set if the constraints should be printed in LaTeX format.
925 static __isl_give isl_printer *print_disjuncts_core(__isl_keep isl_map *map,
926 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
928 int i;
930 if (map->n == 0)
931 p = isl_printer_print_str(p, "1 = 0");
932 for (i = 0; i < map->n; ++i) {
933 if (i)
934 p = isl_printer_print_str(p, s_or[latex]);
935 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
936 p = isl_printer_print_str(p, "(");
937 p = print_disjunct(map->p[i], space, p, latex);
938 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
939 p = isl_printer_print_str(p, ")");
941 return p;
944 /* Print the disjuncts of a map (or set) "map" to "p".
945 * The names of the variables are taken from "space".
946 * "hull" describes constraints shared by all disjuncts of "map".
947 * "latex" is set if the constraints should be printed in LaTeX format.
949 * Print the disjuncts as a conjunction of "hull" and
950 * the result of removing the constraints of "hull" from "map".
951 * If this result turns out to be the universe, then simply print "hull".
953 static __isl_give isl_printer *print_disjuncts_in_hull(__isl_keep isl_map *map,
954 __isl_keep isl_space *space, __isl_take isl_basic_map *hull,
955 __isl_take isl_printer *p, int latex)
957 isl_bool is_universe;
959 p = print_disjunct(hull, space, p, latex);
960 map = isl_map_plain_gist_basic_map(isl_map_copy(map), hull);
961 is_universe = isl_map_plain_is_universe(map);
962 if (is_universe < 0)
963 goto error;
964 if (!is_universe) {
965 p = isl_printer_print_str(p, s_and[latex]);
966 p = isl_printer_print_str(p, "(");
967 p = print_disjuncts_core(map, space, p, latex);
968 p = isl_printer_print_str(p, ")");
970 isl_map_free(map);
972 return p;
973 error:
974 isl_map_free(map);
975 isl_printer_free(p);
976 return NULL;
979 /* Print the disjuncts of a map (or set) "map" to "p".
980 * The names of the variables are taken from "space".
981 * "latex" is set if the constraints should be printed in LaTeX format.
983 * If there are at least two disjuncts and "dump" mode is not turned out,
984 * check for any shared constraints among all disjuncts.
985 * If there are any, then print them separately in print_disjuncts_in_hull.
987 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
988 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
990 if (isl_map_plain_is_universe(map))
991 return p;
993 p = isl_printer_print_str(p, s_such_that[latex]);
994 if (!p)
995 return NULL;
997 if (!p->dump && map->n >= 2) {
998 isl_basic_map *hull;
999 isl_bool is_universe;
1001 hull = isl_map_plain_unshifted_simple_hull(isl_map_copy(map));
1002 is_universe = isl_basic_map_plain_is_universe(hull);
1003 if (is_universe < 0)
1004 p = isl_printer_free(p);
1005 else if (!is_universe)
1006 return print_disjuncts_in_hull(map, space, hull,
1007 p, latex);
1008 isl_basic_map_free(hull);
1011 return print_disjuncts_core(map, space, p, latex);
1014 /* Print the disjuncts of a map (or set).
1015 * The names of the variables are taken from "space".
1016 * "latex" is set if the constraints should be printed in LaTeX format.
1018 * If the map turns out to be a universal parameter domain, then
1019 * we need to print the colon. Otherwise, the output looks identical
1020 * to the empty set.
1022 static __isl_give isl_printer *print_disjuncts_map(__isl_keep isl_map *map,
1023 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1025 if (isl_map_plain_is_universe(map) && isl_space_is_params(map->dim))
1026 return isl_printer_print_str(p, s_such_that[latex]);
1027 else
1028 return print_disjuncts(map, space, p, latex);
1031 struct isl_aff_split {
1032 isl_basic_map *aff;
1033 isl_map *map;
1036 static void free_split(__isl_take struct isl_aff_split *split, int n)
1038 int i;
1040 if (!split)
1041 return;
1043 for (i = 0; i < n; ++i) {
1044 isl_basic_map_free(split[i].aff);
1045 isl_map_free(split[i].map);
1048 free(split);
1051 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
1053 int i, j;
1054 unsigned nparam, n_in, n_out, total;
1056 bmap = isl_basic_map_cow(bmap);
1057 if (!bmap)
1058 return NULL;
1059 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
1060 goto error;
1062 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1063 n_in = isl_basic_map_dim(bmap, isl_dim_in);
1064 n_out = isl_basic_map_dim(bmap, isl_dim_out);
1065 total = isl_basic_map_dim(bmap, isl_dim_all);
1066 for (i = bmap->n_eq - 1; i >= 0; --i) {
1067 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
1068 if (j >= nparam && j < nparam + n_in + n_out &&
1069 (isl_int_is_one(bmap->eq[i][1 + j]) ||
1070 isl_int_is_negone(bmap->eq[i][1 + j])))
1071 continue;
1072 if (isl_basic_map_drop_equality(bmap, i) < 0)
1073 goto error;
1076 bmap = isl_basic_map_finalize(bmap);
1078 return bmap;
1079 error:
1080 isl_basic_map_free(bmap);
1081 return NULL;
1084 static int aff_split_cmp(const void *p1, const void *p2, void *user)
1086 const struct isl_aff_split *s1, *s2;
1087 s1 = (const struct isl_aff_split *) p1;
1088 s2 = (const struct isl_aff_split *) p2;
1090 return isl_basic_map_plain_cmp(s1->aff, s2->aff);
1093 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
1094 __isl_keep isl_basic_map *aff)
1096 int i, j;
1097 unsigned total;
1099 if (!bmap || !aff)
1100 goto error;
1102 total = isl_space_dim(bmap->dim, isl_dim_all);
1104 for (i = bmap->n_eq - 1; i >= 0; --i) {
1105 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
1106 bmap->n_div) != -1)
1107 continue;
1108 for (j = 0; j < aff->n_eq; ++j) {
1109 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
1110 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
1111 continue;
1112 if (isl_basic_map_drop_equality(bmap, i) < 0)
1113 goto error;
1114 break;
1118 return bmap;
1119 error:
1120 isl_basic_map_free(bmap);
1121 return NULL;
1124 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
1126 int i, n;
1127 struct isl_aff_split *split;
1128 isl_ctx *ctx;
1130 ctx = isl_map_get_ctx(map);
1131 split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
1132 if (!split)
1133 return NULL;
1135 for (i = 0; i < map->n; ++i) {
1136 isl_basic_map *bmap;
1137 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
1138 bmap = isl_basic_map_copy(map->p[i]);
1139 bmap = isl_basic_map_cow(bmap);
1140 bmap = drop_aff(bmap, split[i].aff);
1141 split[i].map = isl_map_from_basic_map(bmap);
1142 if (!split[i].aff || !split[i].map)
1143 goto error;
1146 if (isl_sort(split, map->n, sizeof(struct isl_aff_split),
1147 &aff_split_cmp, NULL) < 0)
1148 goto error;
1150 n = map->n;
1151 for (i = n - 1; i >= 1; --i) {
1152 if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
1153 split[i].aff))
1154 continue;
1155 isl_basic_map_free(split[i].aff);
1156 split[i - 1].map = isl_map_union(split[i - 1].map,
1157 split[i].map);
1158 if (i != n - 1)
1159 split[i] = split[n - 1];
1160 split[n - 1].aff = NULL;
1161 split[n - 1].map = NULL;
1162 --n;
1165 return split;
1166 error:
1167 free_split(split, map->n);
1168 return NULL;
1171 static int defining_equality(__isl_keep isl_basic_map *eq,
1172 __isl_keep isl_space *dim, enum isl_dim_type type, int pos)
1174 int i;
1175 unsigned total;
1177 if (!eq)
1178 return -1;
1180 pos += isl_space_offset(dim, type);
1181 total = isl_basic_map_total_dim(eq);
1183 for (i = 0; i < eq->n_eq; ++i) {
1184 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
1185 continue;
1186 if (isl_int_is_one(eq->eq[i][1 + pos]))
1187 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
1188 return i;
1191 return -1;
1194 /* Print dimension "pos" of data->space to "p".
1196 * data->user is assumed to be an isl_basic_map keeping track of equalities.
1198 * If the current dimension is defined by these equalities, then print
1199 * the corresponding expression, assigned to the name of the dimension
1200 * if there is any. Otherwise, print the name of the dimension.
1202 static __isl_give isl_printer *print_dim_eq(__isl_take isl_printer *p,
1203 struct isl_print_space_data *data, unsigned pos)
1205 isl_basic_map *eq = data->user;
1206 int j;
1208 j = defining_equality(eq, data->space, data->type, pos);
1209 if (j >= 0) {
1210 if (isl_space_has_dim_name(data->space, data->type, pos)) {
1211 p = print_name(data->space, p, data->type, pos,
1212 data->latex);
1213 p = isl_printer_print_str(p, " = ");
1215 pos += 1 + isl_space_offset(data->space, data->type);
1216 p = print_affine_of_len(data->space, NULL, p, eq->eq[j], pos);
1217 } else {
1218 p = print_name(data->space, p, data->type, pos, data->latex);
1221 return p;
1224 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
1225 struct isl_aff_split *split, int n, __isl_keep isl_space *space)
1227 struct isl_print_space_data data = { 0 };
1228 int i;
1229 int rational;
1231 data.print_dim = &print_dim_eq;
1232 for (i = 0; i < n; ++i) {
1233 if (!split[i].map)
1234 break;
1235 rational = split[i].map->n > 0 &&
1236 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
1237 if (i)
1238 p = isl_printer_print_str(p, "; ");
1239 data.user = split[i].aff;
1240 p = isl_print_space(space, p, rational, &data);
1241 p = print_disjuncts_map(split[i].map, space, p, 0);
1244 return p;
1247 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
1248 __isl_take isl_printer *p)
1250 struct isl_print_space_data data = { 0 };
1251 struct isl_aff_split *split = NULL;
1252 int rational;
1254 if (!p || !map)
1255 return isl_printer_free(p);
1256 if (!p->dump && map->n > 0)
1257 split = split_aff(map);
1258 if (split) {
1259 p = print_split_map(p, split, map->n, map->dim);
1260 } else {
1261 rational = map->n > 0 &&
1262 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
1263 p = isl_print_space(map->dim, p, rational, &data);
1264 p = print_disjuncts_map(map, map->dim, p, 0);
1266 free_split(split, map->n);
1267 return p;
1270 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
1271 __isl_take isl_printer *p)
1273 struct isl_print_space_data data = { 0 };
1275 p = print_param_tuple(p, map->dim, &data);
1276 p = isl_printer_print_str(p, s_open_set[0]);
1277 p = isl_map_print_isl_body(map, p);
1278 p = isl_printer_print_str(p, s_close_set[0]);
1279 return p;
1282 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
1283 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff)
1285 struct isl_print_space_data data = { 0 };
1287 data.latex = 1;
1288 p = print_param_tuple(p, map->dim, &data);
1289 p = isl_printer_print_str(p, s_open_set[1]);
1290 data.print_dim = &print_dim_eq;
1291 data.user = aff;
1292 p = isl_print_space(map->dim, p, 0, &data);
1293 p = print_disjuncts_map(map, map->dim, p, 1);
1294 p = isl_printer_print_str(p, s_close_set[1]);
1296 return p;
1299 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
1300 __isl_take isl_printer *p)
1302 int i;
1303 struct isl_aff_split *split = NULL;
1305 if (map->n > 0)
1306 split = split_aff(map);
1308 if (!split)
1309 return print_latex_map(map, p, NULL);
1311 for (i = 0; i < map->n; ++i) {
1312 if (!split[i].map)
1313 break;
1314 if (i)
1315 p = isl_printer_print_str(p, " \\cup ");
1316 p = print_latex_map(split[i].map, p, split[i].aff);
1319 free_split(split, map->n);
1320 return p;
1323 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
1324 __isl_keep isl_basic_map *bmap)
1326 if (!p || !bmap)
1327 goto error;
1328 if (p->output_format == ISL_FORMAT_ISL)
1329 return isl_basic_map_print_isl(bmap, p, 0);
1330 else if (p->output_format == ISL_FORMAT_OMEGA)
1331 return basic_map_print_omega(bmap, p);
1332 isl_assert(bmap->ctx, 0, goto error);
1333 error:
1334 isl_printer_free(p);
1335 return NULL;
1338 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
1339 __isl_keep isl_basic_set *bset)
1341 if (!p || !bset)
1342 goto error;
1344 if (p->output_format == ISL_FORMAT_ISL)
1345 return isl_basic_map_print_isl(bset, p, 0);
1346 else if (p->output_format == ISL_FORMAT_POLYLIB)
1347 return isl_basic_set_print_polylib(bset, p, 0);
1348 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1349 return isl_basic_set_print_polylib(bset, p, 1);
1350 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
1351 return bset_print_constraints_polylib(bset, p);
1352 else if (p->output_format == ISL_FORMAT_OMEGA)
1353 return basic_set_print_omega(bset, p);
1354 isl_assert(p->ctx, 0, goto error);
1355 error:
1356 isl_printer_free(p);
1357 return NULL;
1360 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
1361 __isl_keep isl_set *set)
1363 if (!p || !set)
1364 goto error;
1365 if (p->output_format == ISL_FORMAT_ISL)
1366 return isl_map_print_isl(set_to_map(set), p);
1367 else if (p->output_format == ISL_FORMAT_POLYLIB)
1368 return isl_set_print_polylib(set, p, 0);
1369 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1370 return isl_set_print_polylib(set, p, 1);
1371 else if (p->output_format == ISL_FORMAT_OMEGA)
1372 return isl_set_print_omega(set, p);
1373 else if (p->output_format == ISL_FORMAT_LATEX)
1374 return isl_map_print_latex(set_to_map(set), p);
1375 isl_assert(set->ctx, 0, goto error);
1376 error:
1377 isl_printer_free(p);
1378 return NULL;
1381 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1382 __isl_keep isl_map *map)
1384 if (!p || !map)
1385 goto error;
1387 if (p->output_format == ISL_FORMAT_ISL)
1388 return isl_map_print_isl(map, p);
1389 else if (p->output_format == ISL_FORMAT_POLYLIB)
1390 return isl_map_print_polylib(map, p, 0);
1391 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1392 return isl_map_print_polylib(map, p, 1);
1393 else if (p->output_format == ISL_FORMAT_OMEGA)
1394 return isl_map_print_omega(map, p);
1395 else if (p->output_format == ISL_FORMAT_LATEX)
1396 return isl_map_print_latex(map, p);
1397 isl_assert(map->ctx, 0, goto error);
1398 error:
1399 isl_printer_free(p);
1400 return NULL;
1403 struct isl_union_print_data {
1404 isl_printer *p;
1405 int first;
1408 static isl_stat print_map_body(__isl_take isl_map *map, void *user)
1410 struct isl_union_print_data *data;
1411 data = (struct isl_union_print_data *)user;
1413 if (!data->first)
1414 data->p = isl_printer_print_str(data->p, "; ");
1415 data->first = 0;
1417 data->p = isl_map_print_isl_body(map, data->p);
1418 isl_map_free(map);
1420 return isl_stat_ok;
1423 static __isl_give isl_printer *isl_union_map_print_isl(
1424 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1426 struct isl_union_print_data data;
1427 struct isl_print_space_data space_data = { 0 };
1428 isl_space *space;
1430 space = isl_union_map_get_space(umap);
1431 p = print_param_tuple(p, space, &space_data);
1432 isl_space_free(space);
1433 p = isl_printer_print_str(p, s_open_set[0]);
1434 data.p = p;
1435 data.first = 1;
1436 isl_union_map_foreach_map(umap, &print_map_body, &data);
1437 p = data.p;
1438 p = isl_printer_print_str(p, s_close_set[0]);
1439 return p;
1442 static isl_stat print_latex_map_body(__isl_take isl_map *map, void *user)
1444 struct isl_union_print_data *data;
1445 data = (struct isl_union_print_data *)user;
1447 if (!data->first)
1448 data->p = isl_printer_print_str(data->p, " \\cup ");
1449 data->first = 0;
1451 data->p = isl_map_print_latex(map, data->p);
1452 isl_map_free(map);
1454 return isl_stat_ok;
1457 static __isl_give isl_printer *isl_union_map_print_latex(
1458 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1460 struct isl_union_print_data data = { p, 1 };
1461 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1462 p = data.p;
1463 return p;
1466 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1467 __isl_keep isl_union_map *umap)
1469 if (!p || !umap)
1470 goto error;
1472 if (p->output_format == ISL_FORMAT_ISL)
1473 return isl_union_map_print_isl(umap, p);
1474 if (p->output_format == ISL_FORMAT_LATEX)
1475 return isl_union_map_print_latex(umap, p);
1477 isl_die(p->ctx, isl_error_invalid,
1478 "invalid output format for isl_union_map", goto error);
1479 error:
1480 isl_printer_free(p);
1481 return NULL;
1484 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1485 __isl_keep isl_union_set *uset)
1487 if (!p || !uset)
1488 goto error;
1490 if (p->output_format == ISL_FORMAT_ISL)
1491 return isl_union_map_print_isl((isl_union_map *)uset, p);
1492 if (p->output_format == ISL_FORMAT_LATEX)
1493 return isl_union_map_print_latex((isl_union_map *)uset, p);
1495 isl_die(p->ctx, isl_error_invalid,
1496 "invalid output format for isl_union_set", goto error);
1497 error:
1498 isl_printer_free(p);
1499 return NULL;
1502 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
1504 int i;
1505 int n;
1507 for (i = 0, n = 0; i < rec->n; ++i)
1508 if (!isl_upoly_is_zero(rec->p[i]))
1509 ++n;
1511 return n;
1514 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
1515 __isl_take isl_printer *p, int first)
1517 struct isl_upoly_cst *cst;
1518 int neg;
1520 cst = isl_upoly_as_cst(up);
1521 if (!cst)
1522 goto error;
1523 neg = !first && isl_int_is_neg(cst->n);
1524 if (!first)
1525 p = isl_printer_print_str(p, neg ? " - " : " + ");
1526 if (neg)
1527 isl_int_neg(cst->n, cst->n);
1528 if (isl_int_is_zero(cst->d)) {
1529 int sgn = isl_int_sgn(cst->n);
1530 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1531 sgn == 0 ? "NaN" : "infty");
1532 } else
1533 p = isl_printer_print_isl_int(p, cst->n);
1534 if (neg)
1535 isl_int_neg(cst->n, cst->n);
1536 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1537 p = isl_printer_print_str(p, "/");
1538 p = isl_printer_print_isl_int(p, cst->d);
1540 return p;
1541 error:
1542 isl_printer_free(p);
1543 return NULL;
1546 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1547 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var)
1549 unsigned total;
1551 total = isl_space_dim(dim, isl_dim_all);
1552 if (var < total)
1553 p = print_term(dim, NULL, dim->ctx->one, 1 + var, p, 0);
1554 else
1555 p = print_div(dim, div, var - total, p);
1556 return p;
1559 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1560 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var, int exp)
1562 p = print_base(p, dim, div, var);
1563 if (exp == 1)
1564 return p;
1565 if (p->output_format == ISL_FORMAT_C) {
1566 int i;
1567 for (i = 1; i < exp; ++i) {
1568 p = isl_printer_print_str(p, "*");
1569 p = print_base(p, dim, div, var);
1571 } else {
1572 p = isl_printer_print_str(p, "^");
1573 p = isl_printer_print_int(p, exp);
1575 return p;
1578 /* Print the polynomial "up" defined over the domain space "space" and
1579 * local variables defined by "div" to "p".
1581 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1582 __isl_keep isl_space *space, __isl_keep isl_mat *div,
1583 __isl_take isl_printer *p)
1585 int i, n, first, print_parens;
1586 struct isl_upoly_rec *rec;
1588 if (!p || !up || !space || !div)
1589 goto error;
1591 if (isl_upoly_is_cst(up))
1592 return upoly_print_cst(up, p, 1);
1594 rec = isl_upoly_as_rec(up);
1595 if (!rec)
1596 goto error;
1597 n = upoly_rec_n_non_zero(rec);
1598 print_parens = n > 1;
1599 if (print_parens)
1600 p = isl_printer_print_str(p, "(");
1601 for (i = 0, first = 1; i < rec->n; ++i) {
1602 if (isl_upoly_is_zero(rec->p[i]))
1603 continue;
1604 if (isl_upoly_is_negone(rec->p[i])) {
1605 if (!i)
1606 p = isl_printer_print_str(p, "-1");
1607 else if (first)
1608 p = isl_printer_print_str(p, "-");
1609 else
1610 p = isl_printer_print_str(p, " - ");
1611 } else if (isl_upoly_is_cst(rec->p[i]) &&
1612 !isl_upoly_is_one(rec->p[i]))
1613 p = upoly_print_cst(rec->p[i], p, first);
1614 else {
1615 if (!first)
1616 p = isl_printer_print_str(p, " + ");
1617 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1618 p = upoly_print(rec->p[i], space, div, p);
1620 first = 0;
1621 if (i == 0)
1622 continue;
1623 if (!isl_upoly_is_one(rec->p[i]) &&
1624 !isl_upoly_is_negone(rec->p[i]))
1625 p = isl_printer_print_str(p, " * ");
1626 p = print_pow(p, space, div, rec->up.var, i);
1628 if (print_parens)
1629 p = isl_printer_print_str(p, ")");
1630 return p;
1631 error:
1632 isl_printer_free(p);
1633 return NULL;
1636 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1637 __isl_keep isl_qpolynomial *qp)
1639 if (!p || !qp)
1640 goto error;
1641 p = upoly_print(qp->upoly, qp->dim, qp->div, p);
1642 return p;
1643 error:
1644 isl_printer_free(p);
1645 return NULL;
1648 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1649 __isl_keep isl_qpolynomial *qp)
1651 struct isl_print_space_data data = { 0 };
1653 if (!p || !qp)
1654 goto error;
1656 p = print_param_tuple(p, qp->dim, &data);
1657 p = isl_printer_print_str(p, "{ ");
1658 if (!isl_space_is_params(qp->dim)) {
1659 p = isl_print_space(qp->dim, p, 0, &data);
1660 p = isl_printer_print_str(p, " -> ");
1662 p = print_qpolynomial(p, qp);
1663 p = isl_printer_print_str(p, " }");
1664 return p;
1665 error:
1666 isl_printer_free(p);
1667 return NULL;
1670 /* Print the quasi-polynomial "qp" to "p" in C format, with the variable names
1671 * taken from the domain space "space".
1673 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1674 __isl_keep isl_space *space, __isl_keep isl_qpolynomial *qp)
1676 isl_int den;
1678 isl_int_init(den);
1679 isl_qpolynomial_get_den(qp, &den);
1680 if (!isl_int_is_one(den)) {
1681 isl_qpolynomial *f;
1682 p = isl_printer_print_str(p, "(");
1683 qp = isl_qpolynomial_copy(qp);
1684 f = isl_qpolynomial_rat_cst_on_domain(isl_space_copy(qp->dim),
1685 den, qp->dim->ctx->one);
1686 qp = isl_qpolynomial_mul(qp, f);
1688 if (qp)
1689 p = upoly_print(qp->upoly, space, qp->div, p);
1690 else
1691 p = isl_printer_free(p);
1692 if (!isl_int_is_one(den)) {
1693 p = isl_printer_print_str(p, ")/");
1694 p = isl_printer_print_isl_int(p, den);
1695 isl_qpolynomial_free(qp);
1697 isl_int_clear(den);
1698 return p;
1701 __isl_give isl_printer *isl_printer_print_qpolynomial(
1702 __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1704 if (!p || !qp)
1705 goto error;
1707 if (p->output_format == ISL_FORMAT_ISL)
1708 return print_qpolynomial_isl(p, qp);
1709 else if (p->output_format == ISL_FORMAT_C)
1710 return print_qpolynomial_c(p, qp->dim, qp);
1711 else
1712 isl_die(qp->dim->ctx, isl_error_unsupported,
1713 "output format not supported for isl_qpolynomials",
1714 goto error);
1715 error:
1716 isl_printer_free(p);
1717 return NULL;
1720 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1721 unsigned output_format)
1723 isl_printer *p;
1725 if (!qp)
1726 return;
1728 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1729 p = isl_printer_to_file(qp->dim->ctx, out);
1730 p = isl_printer_print_qpolynomial(p, qp);
1731 isl_printer_free(p);
1734 static __isl_give isl_printer *qpolynomial_fold_print(
1735 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1737 int i;
1739 if (fold->type == isl_fold_min)
1740 p = isl_printer_print_str(p, "min");
1741 else if (fold->type == isl_fold_max)
1742 p = isl_printer_print_str(p, "max");
1743 p = isl_printer_print_str(p, "(");
1744 for (i = 0; i < fold->n; ++i) {
1745 if (i)
1746 p = isl_printer_print_str(p, ", ");
1747 p = print_qpolynomial(p, fold->qp[i]);
1749 p = isl_printer_print_str(p, ")");
1750 return p;
1753 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1754 FILE *out, unsigned output_format)
1756 isl_printer *p;
1758 if (!fold)
1759 return;
1761 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1763 p = isl_printer_to_file(fold->dim->ctx, out);
1764 p = isl_printer_print_qpolynomial_fold(p, fold);
1766 isl_printer_free(p);
1769 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1770 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1772 struct isl_print_space_data data = { 0 };
1773 int i = 0;
1775 for (i = 0; i < pwqp->n; ++i) {
1776 isl_space *space;
1778 if (i)
1779 p = isl_printer_print_str(p, "; ");
1780 space = isl_qpolynomial_get_domain_space(pwqp->p[i].qp);
1781 if (!isl_space_is_params(space)) {
1782 p = isl_print_space(space, p, 0, &data);
1783 p = isl_printer_print_str(p, " -> ");
1785 p = print_qpolynomial(p, pwqp->p[i].qp);
1786 p = print_disjuncts(set_to_map(pwqp->p[i].set), space, p, 0);
1787 isl_space_free(space);
1790 return p;
1793 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1794 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1796 struct isl_print_space_data data = { 0 };
1798 if (!p || !pwqp)
1799 goto error;
1801 p = print_param_tuple(p, pwqp->dim, &data);
1802 p = isl_printer_print_str(p, "{ ");
1803 if (pwqp->n == 0) {
1804 if (!isl_space_is_set(pwqp->dim)) {
1805 p = print_tuple(pwqp->dim, p, isl_dim_in, &data);
1806 p = isl_printer_print_str(p, " -> ");
1808 p = isl_printer_print_str(p, "0");
1810 p = isl_pwqp_print_isl_body(p, pwqp);
1811 p = isl_printer_print_str(p, " }");
1812 return p;
1813 error:
1814 isl_printer_free(p);
1815 return NULL;
1818 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
1819 unsigned output_format)
1821 isl_printer *p;
1823 if (!pwqp)
1824 return;
1826 p = isl_printer_to_file(pwqp->dim->ctx, out);
1827 p = isl_printer_set_output_format(p, output_format);
1828 p = isl_printer_print_pw_qpolynomial(p, pwqp);
1830 isl_printer_free(p);
1833 static __isl_give isl_printer *isl_pwf_print_isl_body(
1834 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1836 struct isl_print_space_data data = { 0 };
1837 int i = 0;
1839 for (i = 0; i < pwf->n; ++i) {
1840 isl_space *space;
1842 if (i)
1843 p = isl_printer_print_str(p, "; ");
1844 space = isl_qpolynomial_fold_get_domain_space(pwf->p[i].fold);
1845 if (!isl_space_is_params(space)) {
1846 p = isl_print_space(space, p, 0, &data);
1847 p = isl_printer_print_str(p, " -> ");
1849 p = qpolynomial_fold_print(pwf->p[i].fold, p);
1850 p = print_disjuncts(set_to_map(pwf->p[i].set), space, p, 0);
1851 isl_space_free(space);
1854 return p;
1857 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
1858 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1860 struct isl_print_space_data data = { 0 };
1862 p = print_param_tuple(p, pwf->dim, &data);
1863 p = isl_printer_print_str(p, "{ ");
1864 if (pwf->n == 0) {
1865 if (!isl_space_is_set(pwf->dim)) {
1866 p = print_tuple(pwf->dim, p, isl_dim_in, &data);
1867 p = isl_printer_print_str(p, " -> ");
1869 p = isl_printer_print_str(p, "0");
1871 p = isl_pwf_print_isl_body(p, pwf);
1872 p = isl_printer_print_str(p, " }");
1873 return p;
1876 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1877 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c);
1879 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1880 __isl_keep isl_space *dim,
1881 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1883 if (type == isl_dim_div) {
1884 p = isl_printer_print_str(p, "floord(");
1885 p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
1886 p = isl_printer_print_str(p, ", ");
1887 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1888 p = isl_printer_print_str(p, ")");
1889 } else {
1890 const char *name;
1892 name = isl_space_get_dim_name(dim, type, pos);
1893 if (!name)
1894 name = "UNNAMED";
1895 p = isl_printer_print_str(p, name);
1897 return p;
1900 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1901 __isl_keep isl_space *dim,
1902 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1904 enum isl_dim_type type;
1906 if (pos == 0)
1907 return isl_printer_print_isl_int(p, c);
1909 if (isl_int_is_one(c))
1911 else if (isl_int_is_negone(c))
1912 p = isl_printer_print_str(p, "-");
1913 else {
1914 p = isl_printer_print_isl_int(p, c);
1915 p = isl_printer_print_str(p, "*");
1917 type = pos2type(dim, &pos);
1918 p = print_name_c(p, dim, bset, type, pos);
1919 return p;
1922 static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
1923 __isl_keep isl_space *dim,
1924 __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
1926 int i;
1927 int first;
1929 for (i = 0, first = 1; i < len; ++i) {
1930 int flip = 0;
1931 if (isl_int_is_zero(c[i]))
1932 continue;
1933 if (!first) {
1934 if (isl_int_is_neg(c[i])) {
1935 flip = 1;
1936 isl_int_neg(c[i], c[i]);
1937 p = isl_printer_print_str(p, " - ");
1938 } else
1939 p = isl_printer_print_str(p, " + ");
1941 first = 0;
1942 p = print_term_c(p, dim, bset, c[i], i);
1943 if (flip)
1944 isl_int_neg(c[i], c[i]);
1946 if (first)
1947 p = isl_printer_print_str(p, "0");
1948 return p;
1951 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1952 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c)
1954 unsigned len = 1 + isl_basic_set_total_dim(bset);
1955 return print_partial_affine_c(p, dim, bset, c, len);
1958 /* We skip the constraint if it is implied by the div expression.
1960 * *first indicates whether this is the first constraint in the conjunction and
1961 * is updated if the constraint is actually printed.
1963 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1964 __isl_keep isl_space *dim,
1965 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int *first)
1967 unsigned o_div;
1968 unsigned n_div;
1969 int div;
1971 o_div = isl_basic_set_offset(bset, isl_dim_div);
1972 n_div = isl_basic_set_dim(bset, isl_dim_div);
1973 div = isl_seq_last_non_zero(c + o_div, n_div);
1974 if (div >= 0) {
1975 isl_bool is_div = isl_basic_set_is_div_constraint(bset, c, div);
1976 if (is_div < 0)
1977 return isl_printer_free(p);
1978 if (is_div)
1979 return p;
1982 if (!*first)
1983 p = isl_printer_print_str(p, " && ");
1985 p = print_affine_c(p, dim, bset, c);
1986 p = isl_printer_print_str(p, " ");
1987 p = isl_printer_print_str(p, op);
1988 p = isl_printer_print_str(p, " 0");
1990 *first = 0;
1992 return p;
1995 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1996 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset)
1998 int i, j;
1999 int first = 1;
2000 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
2001 unsigned total = isl_basic_set_total_dim(bset) - n_div;
2003 for (i = 0; i < bset->n_eq; ++i) {
2004 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
2005 if (j < 0)
2006 p = print_constraint_c(p, dim, bset,
2007 bset->eq[i], "==", &first);
2008 else {
2009 if (i)
2010 p = isl_printer_print_str(p, " && ");
2011 p = isl_printer_print_str(p, "(");
2012 p = print_partial_affine_c(p, dim, bset, bset->eq[i],
2013 1 + total + j);
2014 p = isl_printer_print_str(p, ") % ");
2015 p = isl_printer_print_isl_int(p,
2016 bset->eq[i][1 + total + j]);
2017 p = isl_printer_print_str(p, " == 0");
2018 first = 0;
2021 for (i = 0; i < bset->n_ineq; ++i)
2022 p = print_constraint_c(p, dim, bset, bset->ineq[i], ">=",
2023 &first);
2024 return p;
2027 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
2028 __isl_keep isl_space *dim, __isl_keep isl_set *set)
2030 int i;
2032 if (!set)
2033 return isl_printer_free(p);
2035 if (set->n == 0)
2036 p = isl_printer_print_str(p, "0");
2038 for (i = 0; i < set->n; ++i) {
2039 if (i)
2040 p = isl_printer_print_str(p, " || ");
2041 if (set->n > 1)
2042 p = isl_printer_print_str(p, "(");
2043 p = print_basic_set_c(p, dim, set->p[i]);
2044 if (set->n > 1)
2045 p = isl_printer_print_str(p, ")");
2047 return p;
2050 /* Print the piecewise quasi-polynomial "pwqp" to "p" in C format.
2052 static __isl_give isl_printer *print_pw_qpolynomial_c(
2053 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2055 int i;
2056 isl_space *space;
2058 space = isl_pw_qpolynomial_get_domain_space(pwqp);
2059 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set)) {
2060 p = print_qpolynomial_c(p, space, pwqp->p[0].qp);
2061 isl_space_free(space);
2062 return p;
2065 for (i = 0; i < pwqp->n; ++i) {
2066 p = isl_printer_print_str(p, "(");
2067 p = print_set_c(p, space, pwqp->p[i].set);
2068 p = isl_printer_print_str(p, ") ? (");
2069 p = print_qpolynomial_c(p, space, pwqp->p[i].qp);
2070 p = isl_printer_print_str(p, ") : ");
2073 isl_space_free(space);
2074 p = isl_printer_print_str(p, "0");
2075 return p;
2078 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
2079 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2081 if (!p || !pwqp)
2082 goto error;
2084 if (p->output_format == ISL_FORMAT_ISL)
2085 return print_pw_qpolynomial_isl(p, pwqp);
2086 else if (p->output_format == ISL_FORMAT_C)
2087 return print_pw_qpolynomial_c(p, pwqp);
2088 isl_assert(p->ctx, 0, goto error);
2089 error:
2090 isl_printer_free(p);
2091 return NULL;
2094 static isl_stat print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
2096 struct isl_union_print_data *data;
2097 data = (struct isl_union_print_data *)user;
2099 if (!data->first)
2100 data->p = isl_printer_print_str(data->p, "; ");
2101 data->first = 0;
2103 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
2104 isl_pw_qpolynomial_free(pwqp);
2106 return isl_stat_ok;
2109 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
2110 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2112 struct isl_union_print_data data;
2113 struct isl_print_space_data space_data = { 0 };
2114 isl_space *space;
2116 space = isl_union_pw_qpolynomial_get_space(upwqp);
2117 p = print_param_tuple(p, space, &space_data);
2118 isl_space_free(space);
2119 p = isl_printer_print_str(p, "{ ");
2120 data.p = p;
2121 data.first = 1;
2122 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
2123 &data);
2124 p = data.p;
2125 p = isl_printer_print_str(p, " }");
2126 return p;
2129 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
2130 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2132 if (!p || !upwqp)
2133 goto error;
2135 if (p->output_format == ISL_FORMAT_ISL)
2136 return print_union_pw_qpolynomial_isl(p, upwqp);
2137 isl_die(p->ctx, isl_error_invalid,
2138 "invalid output format for isl_union_pw_qpolynomial",
2139 goto error);
2140 error:
2141 isl_printer_free(p);
2142 return NULL;
2145 /* Print the quasi-polynomial reduction "fold" to "p" in C format,
2146 * with the variable names taken from the domain space "space".
2148 static __isl_give isl_printer *print_qpolynomial_fold_c(
2149 __isl_take isl_printer *p, __isl_keep isl_space *space,
2150 __isl_keep isl_qpolynomial_fold *fold)
2152 int i;
2154 for (i = 0; i < fold->n - 1; ++i)
2155 if (fold->type == isl_fold_min)
2156 p = isl_printer_print_str(p, "min(");
2157 else if (fold->type == isl_fold_max)
2158 p = isl_printer_print_str(p, "max(");
2160 for (i = 0; i < fold->n; ++i) {
2161 if (i)
2162 p = isl_printer_print_str(p, ", ");
2163 p = print_qpolynomial_c(p, space, fold->qp[i]);
2164 if (i)
2165 p = isl_printer_print_str(p, ")");
2167 return p;
2170 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
2171 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
2173 if (!p || !fold)
2174 goto error;
2175 if (p->output_format == ISL_FORMAT_ISL)
2176 return qpolynomial_fold_print(fold, p);
2177 else if (p->output_format == ISL_FORMAT_C)
2178 return print_qpolynomial_fold_c(p, fold->dim, fold);
2179 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2180 goto error);
2181 error:
2182 isl_printer_free(p);
2183 return NULL;
2186 /* Print the piecewise quasi-polynomial reduction "pwf" to "p" in C format.
2188 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
2189 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2191 int i;
2192 isl_space *space;
2194 space = isl_pw_qpolynomial_fold_get_domain_space(pwf);
2195 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set)) {
2196 p = print_qpolynomial_fold_c(p, space, pwf->p[0].fold);
2197 isl_space_free(space);
2198 return p;
2201 for (i = 0; i < pwf->n; ++i) {
2202 p = isl_printer_print_str(p, "(");
2203 p = print_set_c(p, space, pwf->p[i].set);
2204 p = isl_printer_print_str(p, ") ? (");
2205 p = print_qpolynomial_fold_c(p, space, pwf->p[i].fold);
2206 p = isl_printer_print_str(p, ") : ");
2209 isl_space_free(space);
2210 p = isl_printer_print_str(p, "0");
2211 return p;
2214 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
2215 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2217 if (!p || !pwf)
2218 goto error;
2220 if (p->output_format == ISL_FORMAT_ISL)
2221 return print_pw_qpolynomial_fold_isl(p, pwf);
2222 else if (p->output_format == ISL_FORMAT_C)
2223 return print_pw_qpolynomial_fold_c(p, pwf);
2224 isl_assert(p->ctx, 0, goto error);
2225 error:
2226 isl_printer_free(p);
2227 return NULL;
2230 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
2231 FILE *out, unsigned output_format)
2233 isl_printer *p;
2235 if (!pwf)
2236 return;
2238 p = isl_printer_to_file(pwf->dim->ctx, out);
2239 p = isl_printer_set_output_format(p, output_format);
2240 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
2242 isl_printer_free(p);
2245 static isl_stat print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf,
2246 void *user)
2248 struct isl_union_print_data *data;
2249 data = (struct isl_union_print_data *)user;
2251 if (!data->first)
2252 data->p = isl_printer_print_str(data->p, "; ");
2253 data->first = 0;
2255 data->p = isl_pwf_print_isl_body(data->p, pwf);
2256 isl_pw_qpolynomial_fold_free(pwf);
2258 return isl_stat_ok;
2261 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
2262 __isl_take isl_printer *p,
2263 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2265 struct isl_union_print_data data;
2266 struct isl_print_space_data space_data = { 0 };
2267 isl_space *space;
2269 space = isl_union_pw_qpolynomial_fold_get_space(upwf);
2270 p = print_param_tuple(p, space, &space_data);
2271 isl_space_free(space);
2272 p = isl_printer_print_str(p, "{ ");
2273 data.p = p;
2274 data.first = 1;
2275 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
2276 &print_pwf_body, &data);
2277 p = data.p;
2278 p = isl_printer_print_str(p, " }");
2279 return p;
2282 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2283 __isl_take isl_printer *p,
2284 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2286 if (!p || !upwf)
2287 goto error;
2289 if (p->output_format == ISL_FORMAT_ISL)
2290 return print_union_pw_qpolynomial_fold_isl(p, upwf);
2291 isl_die(p->ctx, isl_error_invalid,
2292 "invalid output format for isl_union_pw_qpolynomial_fold",
2293 goto error);
2294 error:
2295 isl_printer_free(p);
2296 return NULL;
2299 /* Print the isl_constraint "c" to "p".
2301 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
2302 __isl_keep isl_constraint *c)
2304 struct isl_print_space_data data = { 0 };
2305 isl_local_space *ls;
2306 isl_space *space;
2307 isl_bool exists;
2309 if (!p || !c)
2310 goto error;
2312 ls = isl_constraint_get_local_space(c);
2313 if (!ls)
2314 return isl_printer_free(p);
2315 space = isl_local_space_get_space(ls);
2316 p = print_param_tuple(p, space, &data);
2317 p = isl_printer_print_str(p, "{ ");
2318 p = isl_print_space(space, p, 0, &data);
2319 p = isl_printer_print_str(p, " : ");
2320 exists = need_exists(p, ls->div);
2321 if (exists < 0)
2322 p = isl_printer_free(p);
2323 if (exists >= 0 && exists)
2324 p = open_exists(p, space, ls->div, 0);
2325 p = print_affine_of_len(space, ls->div, p, c->v->el, c->v->size);
2326 if (isl_constraint_is_equality(c))
2327 p = isl_printer_print_str(p, " = 0");
2328 else
2329 p = isl_printer_print_str(p, " >= 0");
2330 if (exists >= 0 && exists)
2331 p = isl_printer_print_str(p, s_close_exists[0]);
2332 p = isl_printer_print_str(p, " }");
2333 isl_space_free(space);
2334 isl_local_space_free(ls);
2336 return p;
2337 error:
2338 isl_printer_free(p);
2339 return NULL;
2342 static __isl_give isl_printer *isl_printer_print_space_isl(
2343 __isl_take isl_printer *p, __isl_keep isl_space *space)
2345 struct isl_print_space_data data = { 0 };
2347 if (!space)
2348 goto error;
2350 p = print_param_tuple(p, space, &data);
2352 p = isl_printer_print_str(p, "{ ");
2353 if (isl_space_is_params(space))
2354 p = isl_printer_print_str(p, s_such_that[0]);
2355 else
2356 p = isl_print_space(space, p, 0, &data);
2357 p = isl_printer_print_str(p, " }");
2359 return p;
2360 error:
2361 isl_printer_free(p);
2362 return NULL;
2365 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
2366 __isl_keep isl_space *space)
2368 if (!p || !space)
2369 return isl_printer_free(p);
2370 if (p->output_format == ISL_FORMAT_ISL)
2371 return isl_printer_print_space_isl(p, space);
2372 else if (p->output_format == ISL_FORMAT_OMEGA)
2373 return print_omega_parameters(space, p);
2375 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
2376 "output format not supported for space",
2377 return isl_printer_free(p));
2380 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
2381 __isl_keep isl_local_space *ls)
2383 struct isl_print_space_data data = { 0 };
2384 unsigned n_div;
2386 if (!ls)
2387 goto error;
2389 p = print_param_tuple(p, ls->dim, &data);
2390 p = isl_printer_print_str(p, "{ ");
2391 p = isl_print_space(ls->dim, p, 0, &data);
2392 n_div = isl_local_space_dim(ls, isl_dim_div);
2393 if (n_div > 0) {
2394 p = isl_printer_print_str(p, " : ");
2395 p = isl_printer_print_str(p, s_open_exists[0]);
2396 p = print_div_list(p, ls->dim, ls->div, 0, 1);
2397 p = isl_printer_print_str(p, s_close_exists[0]);
2398 } else if (isl_space_is_params(ls->dim))
2399 p = isl_printer_print_str(p, s_such_that[0]);
2400 p = isl_printer_print_str(p, " }");
2401 return p;
2402 error:
2403 isl_printer_free(p);
2404 return NULL;
2407 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2408 __isl_keep isl_aff *aff)
2410 unsigned total;
2412 if (isl_aff_is_nan(aff))
2413 return isl_printer_print_str(p, "NaN");
2415 total = isl_local_space_dim(aff->ls, isl_dim_all);
2416 p = isl_printer_print_str(p, "(");
2417 p = print_affine_of_len(aff->ls->dim, aff->ls->div, p,
2418 aff->v->el + 1, 1 + total);
2419 if (isl_int_is_one(aff->v->el[0]))
2420 p = isl_printer_print_str(p, ")");
2421 else {
2422 p = isl_printer_print_str(p, ")/");
2423 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2426 return p;
2429 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2430 __isl_keep isl_aff *aff)
2432 struct isl_print_space_data data = { 0 };
2434 if (isl_space_is_params(aff->ls->dim))
2436 else {
2437 p = print_tuple(aff->ls->dim, p, isl_dim_set, &data);
2438 p = isl_printer_print_str(p, " -> ");
2440 p = isl_printer_print_str(p, "[");
2441 p = print_aff_body(p, aff);
2442 p = isl_printer_print_str(p, "]");
2444 return p;
2447 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2448 __isl_keep isl_aff *aff)
2450 struct isl_print_space_data data = { 0 };
2452 if (!aff)
2453 goto error;
2455 p = print_param_tuple(p, aff->ls->dim, &data);
2456 p = isl_printer_print_str(p, "{ ");
2457 p = print_aff(p, aff);
2458 p = isl_printer_print_str(p, " }");
2459 return p;
2460 error:
2461 isl_printer_free(p);
2462 return NULL;
2465 /* Print the body of an isl_pw_aff, i.e., a semicolon delimited
2466 * sequence of affine expressions, each followed by constraints.
2468 static __isl_give isl_printer *print_pw_aff_body(
2469 __isl_take isl_printer *p, __isl_keep isl_pw_aff *pa)
2471 int i;
2473 if (!pa)
2474 return isl_printer_free(p);
2476 for (i = 0; i < pa->n; ++i) {
2477 isl_space *space;
2479 if (i)
2480 p = isl_printer_print_str(p, "; ");
2481 p = print_aff(p, pa->p[i].aff);
2482 space = isl_aff_get_domain_space(pa->p[i].aff);
2483 p = print_disjuncts(set_to_map(pa->p[i].set), space, p, 0);
2484 isl_space_free(space);
2487 return p;
2490 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2491 __isl_keep isl_pw_aff *pwaff)
2493 struct isl_print_space_data data = { 0 };
2495 if (!pwaff)
2496 goto error;
2498 p = print_param_tuple(p, pwaff->dim, &data);
2499 p = isl_printer_print_str(p, "{ ");
2500 p = print_pw_aff_body(p, pwaff);
2501 p = isl_printer_print_str(p, " }");
2502 return p;
2503 error:
2504 isl_printer_free(p);
2505 return NULL;
2508 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2509 __isl_keep isl_local_space *ls, isl_int *c);
2511 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2512 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2514 if (type == isl_dim_div) {
2515 p = isl_printer_print_str(p, "floord(");
2516 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2517 p = isl_printer_print_str(p, ", ");
2518 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2519 p = isl_printer_print_str(p, ")");
2520 } else {
2521 const char *name;
2523 name = isl_space_get_dim_name(ls->dim, type, pos);
2524 if (!name)
2525 name = "UNNAMED";
2526 p = isl_printer_print_str(p, name);
2528 return p;
2531 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2532 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2534 enum isl_dim_type type;
2536 if (pos == 0)
2537 return isl_printer_print_isl_int(p, c);
2539 if (isl_int_is_one(c))
2541 else if (isl_int_is_negone(c))
2542 p = isl_printer_print_str(p, "-");
2543 else {
2544 p = isl_printer_print_isl_int(p, c);
2545 p = isl_printer_print_str(p, "*");
2547 type = pos2type(ls->dim, &pos);
2548 p = print_ls_name_c(p, ls, type, pos);
2549 return p;
2552 static __isl_give isl_printer *print_ls_partial_affine_c(
2553 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2554 isl_int *c, unsigned len)
2556 int i;
2557 int first;
2559 for (i = 0, first = 1; i < len; ++i) {
2560 int flip = 0;
2561 if (isl_int_is_zero(c[i]))
2562 continue;
2563 if (!first) {
2564 if (isl_int_is_neg(c[i])) {
2565 flip = 1;
2566 isl_int_neg(c[i], c[i]);
2567 p = isl_printer_print_str(p, " - ");
2568 } else
2569 p = isl_printer_print_str(p, " + ");
2571 first = 0;
2572 p = print_ls_term_c(p, ls, c[i], i);
2573 if (flip)
2574 isl_int_neg(c[i], c[i]);
2576 if (first)
2577 p = isl_printer_print_str(p, "0");
2578 return p;
2581 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2582 __isl_keep isl_local_space *ls, isl_int *c)
2584 unsigned len = 1 + isl_local_space_dim(ls, isl_dim_all);
2585 return print_ls_partial_affine_c(p, ls, c, len);
2588 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2589 __isl_keep isl_aff *aff)
2591 unsigned total;
2593 total = isl_local_space_dim(aff->ls, isl_dim_all);
2594 if (!isl_int_is_one(aff->v->el[0]))
2595 p = isl_printer_print_str(p, "(");
2596 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2597 if (!isl_int_is_one(aff->v->el[0])) {
2598 p = isl_printer_print_str(p, ")/");
2599 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2601 return p;
2604 /* In the C format, we cannot express that "pwaff" may be undefined
2605 * on parts of the domain space. We therefore assume that the expression
2606 * will only be evaluated on its definition domain and compute the gist
2607 * of each cell with respect to this domain.
2609 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2610 __isl_keep isl_pw_aff *pwaff)
2612 isl_set *domain;
2613 isl_ast_build *build;
2614 isl_ast_expr *expr;
2616 if (pwaff->n < 1)
2617 isl_die(p->ctx, isl_error_unsupported,
2618 "cannot print empty isl_pw_aff in C format",
2619 return isl_printer_free(p));
2621 domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
2622 build = isl_ast_build_from_context(domain);
2623 expr = isl_ast_build_expr_from_pw_aff(build, isl_pw_aff_copy(pwaff));
2624 p = isl_printer_print_ast_expr(p, expr);
2625 isl_ast_expr_free(expr);
2626 isl_ast_build_free(build);
2628 return p;
2631 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2632 __isl_keep isl_aff *aff)
2634 if (!p || !aff)
2635 goto error;
2637 if (p->output_format == ISL_FORMAT_ISL)
2638 return print_aff_isl(p, aff);
2639 else if (p->output_format == ISL_FORMAT_C)
2640 return print_aff_c(p, aff);
2641 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2642 goto error);
2643 error:
2644 isl_printer_free(p);
2645 return NULL;
2648 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2649 __isl_keep isl_pw_aff *pwaff)
2651 if (!p || !pwaff)
2652 goto error;
2654 if (p->output_format == ISL_FORMAT_ISL)
2655 return print_pw_aff_isl(p, pwaff);
2656 else if (p->output_format == ISL_FORMAT_C)
2657 return print_pw_aff_c(p, pwaff);
2658 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2659 goto error);
2660 error:
2661 isl_printer_free(p);
2662 return NULL;
2665 /* Print "pa" in a sequence of isl_pw_affs delimited by semicolons.
2666 * Each isl_pw_aff itself is also printed as semicolon delimited
2667 * sequence of pieces.
2668 * If data->first = 1, then this is the first in the sequence.
2669 * Update data->first to tell the next element that it is not the first.
2671 static isl_stat print_pw_aff_body_wrap(__isl_take isl_pw_aff *pa,
2672 void *user)
2674 struct isl_union_print_data *data;
2675 data = (struct isl_union_print_data *) user;
2677 if (!data->first)
2678 data->p = isl_printer_print_str(data->p, "; ");
2679 data->first = 0;
2681 data->p = print_pw_aff_body(data->p, pa);
2682 isl_pw_aff_free(pa);
2684 return data->p ? isl_stat_ok : isl_stat_error;
2687 /* Print the body of an isl_union_pw_aff, i.e., a semicolon delimited
2688 * sequence of affine expressions, each followed by constraints,
2689 * with the sequence enclosed in braces.
2691 static __isl_give isl_printer *print_union_pw_aff_body(
2692 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2694 struct isl_union_print_data data = { p, 1 };
2696 p = isl_printer_print_str(p, s_open_set[0]);
2697 data.p = p;
2698 if (isl_union_pw_aff_foreach_pw_aff(upa,
2699 &print_pw_aff_body_wrap, &data) < 0)
2700 data.p = isl_printer_free(p);
2701 p = data.p;
2702 p = isl_printer_print_str(p, s_close_set[0]);
2704 return p;
2707 /* Print the isl_union_pw_aff "upa" to "p" in isl format.
2709 * The individual isl_pw_affs are delimited by a semicolon.
2711 static __isl_give isl_printer *print_union_pw_aff_isl(
2712 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2714 struct isl_print_space_data data = { 0 };
2715 isl_space *space;
2717 space = isl_union_pw_aff_get_space(upa);
2718 p = print_param_tuple(p, space, &data);
2719 isl_space_free(space);
2720 p = print_union_pw_aff_body(p, upa);
2721 return p;
2724 /* Print the isl_union_pw_aff "upa" to "p".
2726 * We currently only support an isl format.
2728 __isl_give isl_printer *isl_printer_print_union_pw_aff(
2729 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2731 if (!p || !upa)
2732 return isl_printer_free(p);
2734 if (p->output_format == ISL_FORMAT_ISL)
2735 return print_union_pw_aff_isl(p, upa);
2736 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2737 "unsupported output format", return isl_printer_free(p));
2740 /* Print dimension "pos" of data->space to "p".
2742 * data->user is assumed to be an isl_multi_aff.
2744 * If the current dimension is an output dimension, then print
2745 * the corresponding expression. Otherwise, print the name of the dimension.
2747 static __isl_give isl_printer *print_dim_ma(__isl_take isl_printer *p,
2748 struct isl_print_space_data *data, unsigned pos)
2750 isl_multi_aff *ma = data->user;
2752 if (data->type == isl_dim_out)
2753 p = print_aff_body(p, ma->p[pos]);
2754 else
2755 p = print_name(data->space, p, data->type, pos, data->latex);
2757 return p;
2760 static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
2761 __isl_keep isl_multi_aff *maff)
2763 struct isl_print_space_data data = { 0 };
2765 data.print_dim = &print_dim_ma;
2766 data.user = maff;
2767 return isl_print_space(maff->space, p, 0, &data);
2770 static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
2771 __isl_keep isl_multi_aff *maff)
2773 struct isl_print_space_data data = { 0 };
2775 if (!maff)
2776 goto error;
2778 p = print_param_tuple(p, maff->space, &data);
2779 p = isl_printer_print_str(p, "{ ");
2780 p = print_multi_aff(p, maff);
2781 p = isl_printer_print_str(p, " }");
2782 return p;
2783 error:
2784 isl_printer_free(p);
2785 return NULL;
2788 __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
2789 __isl_keep isl_multi_aff *maff)
2791 if (!p || !maff)
2792 goto error;
2794 if (p->output_format == ISL_FORMAT_ISL)
2795 return print_multi_aff_isl(p, maff);
2796 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2797 goto error);
2798 error:
2799 isl_printer_free(p);
2800 return NULL;
2803 static __isl_give isl_printer *print_pw_multi_aff_body(
2804 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2806 int i;
2808 if (!pma)
2809 goto error;
2811 for (i = 0; i < pma->n; ++i) {
2812 isl_space *space;
2814 if (i)
2815 p = isl_printer_print_str(p, "; ");
2816 p = print_multi_aff(p, pma->p[i].maff);
2817 space = isl_multi_aff_get_domain_space(pma->p[i].maff);
2818 p = print_disjuncts(set_to_map(pma->p[i].set), space, p, 0);
2819 isl_space_free(space);
2821 return p;
2822 error:
2823 isl_printer_free(p);
2824 return NULL;
2827 static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
2828 __isl_keep isl_pw_multi_aff *pma)
2830 struct isl_print_space_data data = { 0 };
2832 if (!pma)
2833 goto error;
2835 p = print_param_tuple(p, pma->dim, &data);
2836 p = isl_printer_print_str(p, "{ ");
2837 p = print_pw_multi_aff_body(p, pma);
2838 p = isl_printer_print_str(p, " }");
2839 return p;
2840 error:
2841 isl_printer_free(p);
2842 return NULL;
2845 /* Print the unnamed, single-dimensional piecewise multi affine expression "pma"
2846 * to "p".
2848 static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
2849 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2851 int i;
2852 isl_space *space;
2854 space = isl_pw_multi_aff_get_domain_space(pma);
2855 for (i = 0; i < pma->n - 1; ++i) {
2856 p = isl_printer_print_str(p, "(");
2857 p = print_set_c(p, space, pma->p[i].set);
2858 p = isl_printer_print_str(p, ") ? (");
2859 p = print_aff_c(p, pma->p[i].maff->p[0]);
2860 p = isl_printer_print_str(p, ") : ");
2862 isl_space_free(space);
2864 return print_aff_c(p, pma->p[pma->n - 1].maff->p[0]);
2867 static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
2868 __isl_keep isl_pw_multi_aff *pma)
2870 int n;
2871 const char *name;
2873 if (!pma)
2874 goto error;
2875 if (pma->n < 1)
2876 isl_die(p->ctx, isl_error_unsupported,
2877 "cannot print empty isl_pw_multi_aff in C format",
2878 goto error);
2879 name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
2880 if (!name && isl_pw_multi_aff_dim(pma, isl_dim_out) == 1)
2881 return print_unnamed_pw_multi_aff_c(p, pma);
2882 if (!name)
2883 isl_die(p->ctx, isl_error_unsupported,
2884 "cannot print unnamed isl_pw_multi_aff in C format",
2885 goto error);
2887 p = isl_printer_print_str(p, name);
2888 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
2889 if (n != 0)
2890 isl_die(p->ctx, isl_error_unsupported,
2891 "not supported yet", goto error);
2893 return p;
2894 error:
2895 isl_printer_free(p);
2896 return NULL;
2899 __isl_give isl_printer *isl_printer_print_pw_multi_aff(
2900 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2902 if (!p || !pma)
2903 goto error;
2905 if (p->output_format == ISL_FORMAT_ISL)
2906 return print_pw_multi_aff_isl(p, pma);
2907 if (p->output_format == ISL_FORMAT_C)
2908 return print_pw_multi_aff_c(p, pma);
2909 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2910 goto error);
2911 error:
2912 isl_printer_free(p);
2913 return NULL;
2916 static isl_stat print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
2917 void *user)
2919 struct isl_union_print_data *data;
2920 data = (struct isl_union_print_data *) user;
2922 if (!data->first)
2923 data->p = isl_printer_print_str(data->p, "; ");
2924 data->first = 0;
2926 data->p = print_pw_multi_aff_body(data->p, pma);
2927 isl_pw_multi_aff_free(pma);
2929 return isl_stat_ok;
2932 static __isl_give isl_printer *print_union_pw_multi_aff_isl(
2933 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2935 struct isl_union_print_data data;
2936 struct isl_print_space_data space_data = { 0 };
2937 isl_space *space;
2939 space = isl_union_pw_multi_aff_get_space(upma);
2940 p = print_param_tuple(p, space, &space_data);
2941 isl_space_free(space);
2942 p = isl_printer_print_str(p, s_open_set[0]);
2943 data.p = p;
2944 data.first = 1;
2945 isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
2946 &print_pw_multi_aff_body_wrap, &data);
2947 p = data.p;
2948 p = isl_printer_print_str(p, s_close_set[0]);
2949 return p;
2952 __isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
2953 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2955 if (!p || !upma)
2956 goto error;
2958 if (p->output_format == ISL_FORMAT_ISL)
2959 return print_union_pw_multi_aff_isl(p, upma);
2960 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2961 goto error);
2962 error:
2963 isl_printer_free(p);
2964 return NULL;
2967 /* Print dimension "pos" of data->space to "p".
2969 * data->user is assumed to be an isl_multi_pw_aff.
2971 * If the current dimension is an output dimension, then print
2972 * the corresponding piecewise affine expression.
2973 * Otherwise, print the name of the dimension.
2975 static __isl_give isl_printer *print_dim_mpa(__isl_take isl_printer *p,
2976 struct isl_print_space_data *data, unsigned pos)
2978 int i;
2979 int need_parens;
2980 isl_multi_pw_aff *mpa = data->user;
2981 isl_pw_aff *pa;
2983 if (data->type != isl_dim_out)
2984 return print_name(data->space, p, data->type, pos, data->latex);
2986 pa = mpa->p[pos];
2987 if (pa->n == 0)
2988 return isl_printer_print_str(p, "(0 : 1 = 0)");
2990 need_parens = pa->n != 1 || !isl_set_plain_is_universe(pa->p[0].set);
2991 if (need_parens)
2992 p = isl_printer_print_str(p, "(");
2993 for (i = 0; i < pa->n; ++i) {
2994 isl_space *space;
2996 if (i)
2997 p = isl_printer_print_str(p, "; ");
2998 p = print_aff_body(p, pa->p[i].aff);
2999 space = isl_aff_get_domain_space(pa->p[i].aff);
3000 p = print_disjuncts(pa->p[i].set, space, p, 0);
3001 isl_space_free(space);
3003 if (need_parens)
3004 p = isl_printer_print_str(p, ")");
3006 return p;
3009 /* Print "mpa" to "p" in isl format.
3011 static __isl_give isl_printer *print_multi_pw_aff_isl(__isl_take isl_printer *p,
3012 __isl_keep isl_multi_pw_aff *mpa)
3014 struct isl_print_space_data data = { 0 };
3016 if (!mpa)
3017 return isl_printer_free(p);
3019 p = print_param_tuple(p, mpa->space, &data);
3020 p = isl_printer_print_str(p, "{ ");
3021 data.print_dim = &print_dim_mpa;
3022 data.user = mpa;
3023 p = isl_print_space(mpa->space, p, 0, &data);
3024 p = isl_printer_print_str(p, " }");
3025 return p;
3028 __isl_give isl_printer *isl_printer_print_multi_pw_aff(
3029 __isl_take isl_printer *p, __isl_keep isl_multi_pw_aff *mpa)
3031 if (!p || !mpa)
3032 return isl_printer_free(p);
3034 if (p->output_format == ISL_FORMAT_ISL)
3035 return print_multi_pw_aff_isl(p, mpa);
3036 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3037 return isl_printer_free(p));
3040 /* Print dimension "pos" of data->space to "p".
3042 * data->user is assumed to be an isl_multi_val.
3044 * If the current dimension is an output dimension, then print
3045 * the corresponding value. Otherwise, print the name of the dimension.
3047 static __isl_give isl_printer *print_dim_mv(__isl_take isl_printer *p,
3048 struct isl_print_space_data *data, unsigned pos)
3050 isl_multi_val *mv = data->user;
3052 if (data->type == isl_dim_out)
3053 return isl_printer_print_val(p, mv->p[pos]);
3054 else
3055 return print_name(data->space, p, data->type, pos, data->latex);
3058 /* Print the isl_multi_val "mv" to "p" in isl format.
3060 static __isl_give isl_printer *print_multi_val_isl(__isl_take isl_printer *p,
3061 __isl_keep isl_multi_val *mv)
3063 struct isl_print_space_data data = { 0 };
3065 if (!mv)
3066 return isl_printer_free(p);
3068 p = print_param_tuple(p, mv->space, &data);
3069 p = isl_printer_print_str(p, "{ ");
3070 data.print_dim = &print_dim_mv;
3071 data.user = mv;
3072 p = isl_print_space(mv->space, p, 0, &data);
3073 p = isl_printer_print_str(p, " }");
3074 return p;
3077 /* Print the isl_multi_val "mv" to "p".
3079 * Currently only supported in isl format.
3081 __isl_give isl_printer *isl_printer_print_multi_val(
3082 __isl_take isl_printer *p, __isl_keep isl_multi_val *mv)
3084 if (!p || !mv)
3085 return isl_printer_free(p);
3087 if (p->output_format == ISL_FORMAT_ISL)
3088 return print_multi_val_isl(p, mv);
3089 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3090 return isl_printer_free(p));
3093 /* Print dimension "pos" of data->space to "p".
3095 * data->user is assumed to be an isl_multi_union_pw_aff.
3097 * The current dimension is necessarily a set dimension, so
3098 * we print the corresponding isl_union_pw_aff, including
3099 * the braces.
3101 static __isl_give isl_printer *print_union_pw_aff_dim(__isl_take isl_printer *p,
3102 struct isl_print_space_data *data, unsigned pos)
3104 isl_multi_union_pw_aff *mupa = data->user;
3105 isl_union_pw_aff *upa;
3107 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, pos);
3108 p = print_union_pw_aff_body(p, upa);
3109 isl_union_pw_aff_free(upa);
3111 return p;
3114 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3116 static __isl_give isl_printer *print_multi_union_pw_aff_isl(
3117 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3119 struct isl_print_space_data data = { 0 };
3120 isl_space *space;
3122 space = isl_multi_union_pw_aff_get_space(mupa);
3123 p = print_param_tuple(p, space, &data);
3125 data.print_dim = &print_union_pw_aff_dim;
3126 data.user = mupa;
3128 p = isl_print_space(space, p, 0, &data);
3129 isl_space_free(space);
3131 return p;
3134 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3136 * We currently only support an isl format.
3138 __isl_give isl_printer *isl_printer_print_multi_union_pw_aff(
3139 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3141 if (!p || !mupa)
3142 return isl_printer_free(p);
3144 if (p->output_format == ISL_FORMAT_ISL)
3145 return print_multi_union_pw_aff_isl(p, mupa);
3146 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
3147 "unsupported output format", return isl_printer_free(p));