isl_map.c: move_last: use isl_basic_map_offset
[isl.git] / isl_output.c
blobdddbd0bca7d7a8a1bf573d9bcaedd1c8b1659a11
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_set.h>
27 #include <isl/union_map.h>
28 #include <isl/constraint.h>
29 #include <isl_local_space_private.h>
30 #include <isl_aff_private.h>
31 #include <isl_val_private.h>
32 #include <isl_constraint_private.h>
33 #include <isl/ast_build.h>
34 #include <isl_sort.h>
35 #include <isl_output_private.h>
37 #include <bset_to_bmap.c>
38 #include <set_to_map.c>
39 #include <uset_to_umap.c>
41 static const char *s_to[2] = { " -> ", " \\to " };
42 static const char *s_and[2] = { " and ", " \\wedge " };
43 static const char *s_or[2] = { " or ", " \\vee " };
44 static const char *s_le[2] = { "<=", "\\le" };
45 static const char *s_ge[2] = { ">=", "\\ge" };
46 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
47 static const char *s_close_set[2] = { " }", " \\,\\}" };
48 static const char *s_open_list[2] = { "[", "(" };
49 static const char *s_close_list[2] = { "]", ")" };
50 static const char *s_such_that[2] = { " : ", " \\mid " };
51 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
52 static const char *s_close_exists[2] = { ")", "" };
53 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
54 static const char *s_mod[2] = { "mod", "\\bmod" };
55 static const char *s_param_prefix[2] = { "p", "p_" };
56 static const char *s_input_prefix[2] = { "i", "i_" };
57 static const char *s_output_prefix[2] = { "o", "o_" };
59 static __isl_give isl_printer *print_constraint_polylib(
60 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
62 int i;
63 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
64 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
65 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
66 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
68 p = isl_printer_start_line(p);
69 p = isl_printer_print_int(p, ineq);
70 for (i = 0; i < n_out; ++i) {
71 p = isl_printer_print_str(p, " ");
72 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
74 for (i = 0; i < n_in; ++i) {
75 p = isl_printer_print_str(p, " ");
76 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
78 for (i = 0; i < bmap->n_div; ++i) {
79 p = isl_printer_print_str(p, " ");
80 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
82 for (i = 0; i < nparam; ++i) {
83 p = isl_printer_print_str(p, " ");
84 p = isl_printer_print_isl_int(p, c[1+i]);
86 p = isl_printer_print_str(p, " ");
87 p = isl_printer_print_isl_int(p, c[0]);
88 p = isl_printer_end_line(p);
89 return p;
92 static __isl_give isl_printer *print_constraints_polylib(
93 struct isl_basic_map *bmap, __isl_take isl_printer *p)
95 int i;
97 p = isl_printer_set_isl_int_width(p, 5);
99 for (i = 0; i < bmap->n_eq; ++i)
100 p = print_constraint_polylib(bmap, 0, i, p);
101 for (i = 0; i < bmap->n_ineq; ++i)
102 p = print_constraint_polylib(bmap, 1, i, p);
104 return p;
107 static __isl_give isl_printer *bset_print_constraints_polylib(
108 struct isl_basic_set *bset, __isl_take isl_printer *p)
110 return print_constraints_polylib(bset_to_bmap(bset), p);
113 static __isl_give isl_printer *isl_basic_map_print_polylib(
114 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
116 unsigned total = isl_basic_map_total_dim(bmap);
117 p = isl_printer_start_line(p);
118 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
119 p = isl_printer_print_str(p, " ");
120 p = isl_printer_print_int(p, 1 + total + 1);
121 if (ext) {
122 p = isl_printer_print_str(p, " ");
123 p = isl_printer_print_int(p,
124 isl_basic_map_dim(bmap, isl_dim_out));
125 p = isl_printer_print_str(p, " ");
126 p = isl_printer_print_int(p,
127 isl_basic_map_dim(bmap, isl_dim_in));
128 p = isl_printer_print_str(p, " ");
129 p = isl_printer_print_int(p,
130 isl_basic_map_dim(bmap, isl_dim_div));
131 p = isl_printer_print_str(p, " ");
132 p = isl_printer_print_int(p,
133 isl_basic_map_dim(bmap, isl_dim_param));
135 p = isl_printer_end_line(p);
136 return print_constraints_polylib(bmap, p);
139 static __isl_give isl_printer *isl_basic_set_print_polylib(
140 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
142 return isl_basic_map_print_polylib(bset_to_bmap(bset), p, ext);
145 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
146 __isl_take isl_printer *p, int ext)
148 int i;
150 p = isl_printer_start_line(p);
151 p = isl_printer_print_int(p, map->n);
152 p = isl_printer_end_line(p);
153 for (i = 0; i < map->n; ++i) {
154 p = isl_printer_start_line(p);
155 p = isl_printer_end_line(p);
156 p = isl_basic_map_print_polylib(map->p[i], p, ext);
158 return p;
161 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
162 __isl_take isl_printer *p, int ext)
164 return isl_map_print_polylib(set_to_map(set), p, ext);
167 static int count_same_name(__isl_keep isl_space *space,
168 enum isl_dim_type type, unsigned pos, const char *name)
170 enum isl_dim_type t;
171 unsigned p, s;
172 int count = 0;
174 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
175 s = t == type ? pos : isl_space_dim(space, t);
176 for (p = 0; p < s; ++p) {
177 const char *n = isl_space_get_dim_name(space, t, p);
178 if (n && !strcmp(n, name))
179 count++;
182 return count;
185 /* Print the name of the variable of type "type" and position "pos"
186 * in "space" to "p".
188 static __isl_give isl_printer *print_name(__isl_keep isl_space *space,
189 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
190 int latex)
192 const char *name;
193 char buffer[20];
194 int primes;
196 name = type == isl_dim_div ? NULL
197 : isl_space_get_dim_name(space, type, pos);
199 if (!name) {
200 const char *prefix;
201 if (type == isl_dim_param)
202 prefix = s_param_prefix[latex];
203 else if (type == isl_dim_div)
204 prefix = s_div_prefix[latex];
205 else if (isl_space_is_set(space) || type == isl_dim_in)
206 prefix = s_input_prefix[latex];
207 else
208 prefix = s_output_prefix[latex];
209 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
210 name = buffer;
212 primes = count_same_name(space, name == buffer ? isl_dim_div : type,
213 pos, name);
214 p = isl_printer_print_str(p, name);
215 while (primes-- > 0)
216 p = isl_printer_print_str(p, "'");
217 return p;
220 static isl_stat pos2type(__isl_keep isl_space *space,
221 enum isl_dim_type *type, unsigned *pos)
223 unsigned n_in = isl_space_dim(space, isl_dim_in);
224 unsigned n_out = isl_space_dim(space, isl_dim_out);
225 unsigned nparam = isl_space_dim(space, isl_dim_param);
227 if (*pos < 1 + nparam) {
228 *type = isl_dim_param;
229 *pos -= 1;
230 } else if (*pos < 1 + nparam + n_in) {
231 *type = isl_dim_in;
232 *pos -= 1 + nparam;
233 } else if (*pos < 1 + nparam + n_in + n_out) {
234 *type = isl_dim_out;
235 *pos -= 1 + nparam + n_in;
236 } else {
237 *type = isl_dim_div;
238 *pos -= 1 + nparam + n_in + n_out;
241 return isl_stat_ok;
244 /* Can the div expression of the integer division at position "row" of "div"
245 * be printed?
246 * In particular, are the div expressions available and does the selected
247 * variable have a known explicit representation?
248 * Furthermore, the Omega format does not allow any div expressions
249 * to be printed.
251 static isl_bool can_print_div_expr(__isl_keep isl_printer *p,
252 __isl_keep isl_mat *div, int pos)
254 if (p->output_format == ISL_FORMAT_OMEGA)
255 return isl_bool_false;
256 if (!div)
257 return isl_bool_false;
258 return !isl_int_is_zero(div->row[pos][0]);
261 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
262 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
264 static __isl_give isl_printer *print_term(__isl_keep isl_space *space,
265 __isl_keep isl_mat *div,
266 isl_int c, unsigned pos, __isl_take isl_printer *p, int latex)
268 enum isl_dim_type type;
269 int print_div_def;
271 if (!p || !space)
272 return isl_printer_free(p);
274 if (pos == 0)
275 return isl_printer_print_isl_int(p, c);
277 if (pos2type(space, &type, &pos) < 0)
278 return isl_printer_free(p);
279 print_div_def = type == isl_dim_div && can_print_div_expr(p, div, pos);
281 if (isl_int_is_one(c))
283 else if (isl_int_is_negone(c))
284 p = isl_printer_print_str(p, "-");
285 else {
286 p = isl_printer_print_isl_int(p, c);
287 if (p->output_format == ISL_FORMAT_C || print_div_def)
288 p = isl_printer_print_str(p, "*");
290 if (print_div_def)
291 p = print_div(space, div, pos, p);
292 else
293 p = print_name(space, p, type, pos, latex);
294 return p;
297 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_space *dim,
298 __isl_keep isl_mat *div,
299 __isl_take isl_printer *p, isl_int *c, int len)
301 int i;
302 int first;
304 for (i = 0, first = 1; i < len; ++i) {
305 int flip = 0;
306 if (isl_int_is_zero(c[i]))
307 continue;
308 if (!first) {
309 if (isl_int_is_neg(c[i])) {
310 flip = 1;
311 isl_int_neg(c[i], c[i]);
312 p = isl_printer_print_str(p, " - ");
313 } else
314 p = isl_printer_print_str(p, " + ");
316 first = 0;
317 p = print_term(dim, div, c[i], i, p, 0);
318 if (flip)
319 isl_int_neg(c[i], c[i]);
321 if (first)
322 p = isl_printer_print_str(p, "0");
323 return p;
326 /* Print an affine expression "c"
327 * to "p", with the variable names taken from "space" and
328 * the integer division definitions taken from "div".
330 static __isl_give isl_printer *print_affine(__isl_take isl_printer *p,
331 __isl_keep isl_space *space, __isl_keep isl_mat *div, isl_int *c)
333 unsigned n_div;
334 unsigned len;
336 if (!space || !div)
337 return isl_printer_free(p);
338 n_div = isl_mat_rows(div);
339 len = 1 + isl_space_dim(space, isl_dim_all) + n_div;
340 return print_affine_of_len(space, div, p, c, len);
343 /* offset is the offset of local_space inside data->type of data->space.
345 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
346 __isl_keep isl_space *local_space, enum isl_dim_type local_type,
347 struct isl_print_space_data *data, int offset)
349 int i;
351 if (data->space != local_space && local_type == isl_dim_out)
352 offset += local_space->n_in;
354 for (i = 0; i < isl_space_dim(local_space, local_type); ++i) {
355 if (i)
356 p = isl_printer_print_str(p, ", ");
357 if (data->print_dim)
358 p = data->print_dim(p, data, offset + i);
359 else
360 p = print_name(data->space, p, data->type, offset + i,
361 data->latex);
363 return p;
366 static __isl_give isl_printer *print_var_list(__isl_take isl_printer *p,
367 __isl_keep isl_space *space, enum isl_dim_type type)
369 struct isl_print_space_data data = { .space = space, .type = type };
371 return print_nested_var_list(p, space, type, &data, 0);
374 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
375 __isl_keep isl_space *local_dim,
376 struct isl_print_space_data *data, int offset);
378 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
379 __isl_keep isl_space *local_space, enum isl_dim_type local_type,
380 struct isl_print_space_data *data, int offset)
382 const char *name = NULL;
383 unsigned n = isl_space_dim(local_space, local_type);
384 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
385 name = isl_space_get_tuple_name(local_space, local_type);
386 if (name) {
387 if (data->latex)
388 p = isl_printer_print_str(p, "\\mathrm{");
389 p = isl_printer_print_str(p, name);
390 if (data->latex)
391 p = isl_printer_print_str(p, "}");
394 if (!data->latex || n != 1 || name)
395 p = isl_printer_print_str(p, s_open_list[data->latex]);
396 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
397 local_space->nested[local_type - isl_dim_in]) {
398 if (data->space != local_space && local_type == isl_dim_out)
399 offset += local_space->n_in;
400 p = print_nested_map_dim(p,
401 local_space->nested[local_type - isl_dim_in],
402 data, offset);
403 } else
404 p = print_nested_var_list(p, local_space, local_type, data,
405 offset);
406 if (!data->latex || n != 1 || name)
407 p = isl_printer_print_str(p, s_close_list[data->latex]);
408 return p;
411 static __isl_give isl_printer *print_tuple(__isl_keep isl_space *dim,
412 __isl_take isl_printer *p, enum isl_dim_type type,
413 struct isl_print_space_data *data)
415 data->space = dim;
416 data->type = type;
417 return print_nested_tuple(p, dim, type, data, 0);
420 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
421 __isl_keep isl_space *local_dim,
422 struct isl_print_space_data *data, int offset)
424 p = print_nested_tuple(p, local_dim, isl_dim_in, data, offset);
425 p = isl_printer_print_str(p, s_to[data->latex]);
426 p = print_nested_tuple(p, local_dim, isl_dim_out, data, offset);
428 return p;
431 __isl_give isl_printer *isl_print_space(__isl_keep isl_space *space,
432 __isl_take isl_printer *p, int rational,
433 struct isl_print_space_data *data)
435 if (rational && !data->latex)
436 p = isl_printer_print_str(p, "rat: ");
437 if (isl_space_is_params(space))
439 else if (isl_space_is_set(space))
440 p = print_tuple(space, p, isl_dim_set, data);
441 else {
442 p = print_tuple(space, p, isl_dim_in, data);
443 p = isl_printer_print_str(p, s_to[data->latex]);
444 p = print_tuple(space, p, isl_dim_out, data);
447 return p;
450 static __isl_give isl_printer *print_omega_parameters(
451 __isl_keep isl_space *space, __isl_take isl_printer *p)
453 if (isl_space_dim(space, isl_dim_param) == 0)
454 return p;
456 p = isl_printer_start_line(p);
457 p = isl_printer_print_str(p, "symbolic ");
458 p = print_var_list(p, space, isl_dim_param);
459 p = isl_printer_print_str(p, ";");
460 p = isl_printer_end_line(p);
461 return p;
464 /* Does the inequality constraint following "i" in "bmap"
465 * have an opposite value for the same last coefficient?
466 * "last" is the position of the last coefficient of inequality "i".
467 * If the next constraint is a div constraint, then it is ignored
468 * since div constraints are not printed.
470 static isl_bool next_is_opposite(__isl_keep isl_basic_map *bmap, int i,
471 int last)
473 unsigned total = isl_basic_map_total_dim(bmap);
474 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
476 if (i + 1 >= bmap->n_ineq)
477 return isl_bool_false;
478 if (isl_seq_last_non_zero(bmap->ineq[i + 1], 1 + total) != last)
479 return isl_bool_false;
480 if (last >= o_div) {
481 isl_bool is_div;
482 is_div = isl_basic_map_is_div_constraint(bmap,
483 bmap->ineq[i + 1], last - o_div);
484 if (is_div < 0)
485 return isl_bool_error;
486 if (is_div)
487 return isl_bool_false;
489 return isl_int_abs_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]) &&
490 !isl_int_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]);
493 /* Return a string representation of the operator used when
494 * printing a constraint where the LHS is greater than or equal to the LHS
495 * (sign > 0) or smaller than or equal to the LHS (sign < 0).
496 * If "strict" is set, then return the strict version of the comparison
497 * operator.
499 static const char *constraint_op(int sign, int strict, int latex)
501 if (strict)
502 return sign < 0 ? "<" : ">";
503 if (sign < 0)
504 return s_le[latex];
505 else
506 return s_ge[latex];
509 /* Print one side of a constraint "c" to "p", with
510 * the variable names taken from "space" and the integer division definitions
511 * taken from "div".
512 * "last" is the position of the last non-zero coefficient.
513 * Let c' be the result of zeroing out this coefficient, then
514 * the partial constraint
516 * c' op
518 * is printed.
520 static __isl_give isl_printer *print_half_constraint(__isl_take isl_printer *p,
521 __isl_keep isl_space *space, __isl_keep isl_mat *div,
522 isl_int *c, int last, const char *op, int latex)
524 isl_int_set_si(c[last], 0);
525 p = print_affine(p, space, div, c);
527 p = isl_printer_print_str(p, " ");
528 p = isl_printer_print_str(p, op);
529 p = isl_printer_print_str(p, " ");
531 return p;
534 /* Print a constraint "c" to "p", with the variable names
535 * taken from "space" and the integer division definitions taken from "div".
536 * "last" is the position of the last non-zero coefficient, which is
537 * moreover assumed to be negative.
538 * Let c' be the result of zeroing out this coefficient, then
539 * the constraint is printed in the form
541 * -c[last] op c'
543 static __isl_give isl_printer *print_constraint(__isl_take isl_printer *p,
544 __isl_keep isl_space *space, __isl_keep isl_mat *div,
545 isl_int *c, int last, const char *op, int latex)
547 isl_int_abs(c[last], c[last]);
549 p = print_term(space, div, c[last], last, p, latex);
551 p = isl_printer_print_str(p, " ");
552 p = isl_printer_print_str(p, op);
553 p = isl_printer_print_str(p, " ");
555 isl_int_set_si(c[last], 0);
556 p = print_affine(p, space, div, c);
558 return p;
561 /* Given an integer division
563 * floor(f/m)
565 * at position "pos" in "div", print the corresponding modulo expression
567 * (f) mod m
569 * to "p". The variable names are taken from "space", while any
570 * nested integer division definitions are taken from "div".
572 static __isl_give isl_printer *print_mod(__isl_take isl_printer *p,
573 __isl_keep isl_space *space, __isl_keep isl_mat *div, int pos,
574 int latex)
576 if (!p || !div)
577 return isl_printer_free(p);
579 p = isl_printer_print_str(p, "(");
580 p = print_affine_of_len(space, div, p,
581 div->row[pos] + 1, div->n_col - 1);
582 p = isl_printer_print_str(p, ") ");
583 p = isl_printer_print_str(p, s_mod[latex]);
584 p = isl_printer_print_str(p, " ");
585 p = isl_printer_print_isl_int(p, div->row[pos][0]);
586 return p;
589 /* Can the equality constraints "c" be printed as a modulo constraint?
590 * In particular, is of the form
592 * f - a m floor(g/m) = 0,
594 * with c = -a m the coefficient at position "pos"?
595 * Return the position of the corresponding integer division if so.
596 * Return the number of integer divisions if not.
597 * Return -1 on error.
599 * Modulo constraints are currently not printed in C format.
600 * Other than that, "pos" needs to correspond to an integer division
601 * with explicit representation and "c" needs to be a multiple
602 * of the denominator of the integer division.
604 static int print_as_modulo_pos(__isl_keep isl_printer *p,
605 __isl_keep isl_space *space, __isl_keep isl_mat *div, unsigned pos,
606 isl_int c)
608 isl_bool can_print;
609 unsigned n_div;
610 enum isl_dim_type type;
612 if (!p || !space)
613 return -1;
614 n_div = isl_mat_rows(div);
615 if (p->output_format == ISL_FORMAT_C)
616 return n_div;
617 if (pos2type(space, &type, &pos) < 0)
618 return -1;
619 if (type != isl_dim_div)
620 return n_div;
621 can_print = can_print_div_expr(p, div, pos);
622 if (can_print < 0)
623 return -1;
624 if (!can_print)
625 return n_div;
626 if (!isl_int_is_divisible_by(c, div->row[pos][0]))
627 return n_div;
628 return pos;
631 /* Print equality constraint "c" to "p" as a modulo constraint,
632 * with the variable names taken from "space" and
633 * the integer division definitions taken from "div".
634 * "last" is the position of the last non-zero coefficient, which is
635 * moreover assumed to be negative and a multiple of the denominator
636 * of the corresponding integer division. "div_pos" is the corresponding
637 * position in the sequence of integer divisions.
639 * The equality is of the form
641 * f - a m floor(g/m) = 0.
643 * Print it as
645 * a (g mod m) = -f + a g
647 static __isl_give isl_printer *print_eq_mod_constraint(
648 __isl_take isl_printer *p, __isl_keep isl_space *space,
649 __isl_keep isl_mat *div, unsigned div_pos,
650 isl_int *c, int last, int latex)
652 isl_ctx *ctx;
653 int multiple;
655 ctx = isl_printer_get_ctx(p);
656 isl_int_divexact(c[last], c[last], div->row[div_pos][0]);
657 isl_int_abs(c[last], c[last]);
658 multiple = !isl_int_is_one(c[last]);
659 if (multiple) {
660 p = isl_printer_print_isl_int(p, c[last]);
661 p = isl_printer_print_str(p, "*(");
663 p = print_mod(p, space, div, div_pos, latex);
664 if (multiple)
665 p = isl_printer_print_str(p, ")");
666 p = isl_printer_print_str(p, " = ");
667 isl_seq_combine(c, ctx->negone, c,
668 c[last], div->row[div_pos] + 1, last);
669 isl_int_set_si(c[last], 0);
670 p = print_affine(p, space, div, c);
671 return p;
674 /* Print equality constraint "c" to "p", with the variable names
675 * taken from "space" and the integer division definitions taken from "div".
676 * "last" is the position of the last non-zero coefficient, which is
677 * moreover assumed to be negative.
679 * If possible, print the equality constraint as a modulo constraint.
681 static __isl_give isl_printer *print_eq_constraint(__isl_take isl_printer *p,
682 __isl_keep isl_space *space, __isl_keep isl_mat *div, isl_int *c,
683 int last, int latex)
685 unsigned n_div;
686 int div_pos;
688 n_div = isl_mat_rows(div);
689 div_pos = print_as_modulo_pos(p, space, div, last, c[last]);
690 if (div_pos < 0)
691 return isl_printer_free(p);
692 if (div_pos < n_div)
693 return print_eq_mod_constraint(p, space, div, div_pos,
694 c, last, latex);
695 return print_constraint(p, space, div, c, last, "=", latex);
698 /* Print the constraints of "bmap" to "p".
699 * The names of the variables are taken from "space" and
700 * the integer division definitions are taken from "div".
701 * Div constraints are only printed in "dump" mode.
702 * The constraints are sorted prior to printing (except in "dump" mode).
704 * If x is the last variable with a non-zero coefficient,
705 * then a lower bound
707 * f - a x >= 0
709 * is printed as
711 * a x <= f
713 * while an upper bound
715 * f + a x >= 0
717 * is printed as
719 * a x >= -f
721 * If the next constraint has an opposite sign for the same last coefficient,
722 * then it is printed as
724 * f >= a x
726 * or
728 * -f <= a x
730 * instead. In fact, the "a x" part is not printed explicitly, but
731 * reused from the next constraint, which is therefore treated as
732 * a first constraint in the conjunction.
734 * If the constant term of "f" is -1, then "f" is replaced by "f + 1" and
735 * the comparison operator is replaced by the strict variant.
736 * Essentially, ">= 1" is replaced by "> 0".
738 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
739 __isl_keep isl_space *space, __isl_keep isl_mat *div,
740 __isl_take isl_printer *p, int latex)
742 int i;
743 isl_vec *c = NULL;
744 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
745 unsigned total = isl_basic_map_total_dim(bmap);
746 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
747 int first = 1;
748 int dump;
750 if (!p)
751 return NULL;
752 bmap = isl_basic_map_copy(bmap);
753 dump = p->dump;
754 if (!dump)
755 bmap = isl_basic_map_sort_constraints(bmap);
756 if (!bmap)
757 goto error;
759 c = isl_vec_alloc(bmap->ctx, 1 + total);
760 if (!c)
761 goto error;
763 for (i = bmap->n_eq - 1; i >= 0; --i) {
764 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
765 if (l < 0) {
766 if (i != bmap->n_eq - 1)
767 p = isl_printer_print_str(p, s_and[latex]);
768 p = isl_printer_print_str(p, "0 = 0");
769 continue;
771 if (!first)
772 p = isl_printer_print_str(p, s_and[latex]);
773 if (isl_int_is_neg(bmap->eq[i][l]))
774 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
775 else
776 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
777 p = print_eq_constraint(p, space, div, c->el, l, latex);
778 first = 0;
780 for (i = 0; i < bmap->n_ineq; ++i) {
781 isl_bool combine;
782 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
783 int strict;
784 int s;
785 const char *op;
786 if (l < 0)
787 continue;
788 if (!dump && l >= o_div &&
789 can_print_div_expr(p, div, l - o_div)) {
790 isl_bool is_div;
791 is_div = isl_basic_map_is_div_constraint(bmap,
792 bmap->ineq[i], l - o_div);
793 if (is_div < 0)
794 goto error;
795 if (is_div)
796 continue;
798 if (!first)
799 p = isl_printer_print_str(p, s_and[latex]);
800 s = isl_int_sgn(bmap->ineq[i][l]);
801 strict = !rational && isl_int_is_negone(bmap->ineq[i][0]);
802 if (s < 0)
803 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
804 else
805 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
806 if (strict)
807 isl_int_set_si(c->el[0], 0);
808 combine = dump ? isl_bool_false : next_is_opposite(bmap, i, l);
809 if (combine < 0)
810 goto error;
811 if (combine) {
812 op = constraint_op(-s, strict, latex);
813 p = print_half_constraint(p, space, div, c->el, l,
814 op, latex);
815 first = 1;
816 } else {
817 op = constraint_op(s, strict, latex);
818 p = print_constraint(p, space, div, c->el, l,
819 op, latex);
820 first = 0;
824 isl_basic_map_free(bmap);
825 isl_vec_free(c);
827 return p;
828 error:
829 isl_basic_map_free(bmap);
830 isl_vec_free(c);
831 isl_printer_free(p);
832 return NULL;
835 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
836 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
838 int c;
840 if (!p || !div)
841 return isl_printer_free(p);
843 c = p->output_format == ISL_FORMAT_C;
844 p = isl_printer_print_str(p, c ? "floord(" : "floor((");
845 p = print_affine_of_len(dim, div, p,
846 div->row[pos] + 1, div->n_col - 1);
847 p = isl_printer_print_str(p, c ? ", " : ")/");
848 p = isl_printer_print_isl_int(p, div->row[pos][0]);
849 p = isl_printer_print_str(p, ")");
850 return p;
853 /* Print a comma separated list of div names, except those that have
854 * a definition that can be printed.
855 * If "print_defined_divs" is set, then those div names are printed
856 * as well, along with their definitions.
858 static __isl_give isl_printer *print_div_list(__isl_take isl_printer *p,
859 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex,
860 int print_defined_divs)
862 int i;
863 int first = 1;
864 unsigned n_div;
866 if (!p || !space || !div)
867 return isl_printer_free(p);
869 n_div = isl_mat_rows(div);
871 for (i = 0; i < n_div; ++i) {
872 if (!print_defined_divs && can_print_div_expr(p, div, i))
873 continue;
874 if (!first)
875 p = isl_printer_print_str(p, ", ");
876 p = print_name(space, p, isl_dim_div, i, latex);
877 first = 0;
878 if (!can_print_div_expr(p, div, i))
879 continue;
880 p = isl_printer_print_str(p, " = ");
881 p = print_div(space, div, i, p);
884 return p;
887 /* Does printing an object with local variables described by "div"
888 * require an "exists" clause?
889 * That is, are there any local variables without an explicit representation?
890 * An exists clause is also needed in "dump" mode because
891 * explicit div representations are not printed inline in that case.
893 static isl_bool need_exists(__isl_keep isl_printer *p, __isl_keep isl_mat *div)
895 int i, n;
897 if (!p || !div)
898 return isl_bool_error;
899 n = isl_mat_rows(div);
900 if (n == 0)
901 return isl_bool_false;
902 if (p->dump)
903 return isl_bool_true;
904 for (i = 0; i < n; ++i)
905 if (!can_print_div_expr(p, div, i))
906 return isl_bool_true;
907 return isl_bool_false;
910 /* Print the start of an exists clause, i.e.,
912 * (exists variables:
914 * In dump mode, local variables with an explicit definition are printed
915 * as well because they will not be printed inline.
917 static __isl_give isl_printer *open_exists(__isl_take isl_printer *p,
918 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex)
920 int dump;
922 if (!p)
923 return NULL;
925 dump = p->dump;
926 p = isl_printer_print_str(p, s_open_exists[latex]);
927 p = print_div_list(p, space, div, latex, dump);
928 p = isl_printer_print_str(p, ": ");
930 return p;
933 /* Remove the explicit representations of all local variables in "div".
935 static __isl_give isl_mat *mark_all_unknown(__isl_take isl_mat *div)
937 int i, n_div;
939 if (!div)
940 return NULL;
942 n_div = isl_mat_rows(div);
943 for (i = 0; i < n_div; ++i)
944 div = isl_mat_set_element_si(div, i, 0, 0);
945 return div;
948 /* Print the constraints of "bmap" to "p".
949 * The names of the variables are taken from "space".
950 * "latex" is set if the constraints should be printed in LaTeX format.
951 * Do not print inline explicit div representations in "dump" mode.
953 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
954 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
956 int dump;
957 isl_mat *div;
958 isl_bool exists;
960 if (!p)
961 return NULL;
962 dump = p->dump;
963 div = isl_basic_map_get_divs(bmap);
964 exists = need_exists(p, div);
965 if (exists >= 0 && exists)
966 p = open_exists(p, space, div, latex);
968 if (dump)
969 div = mark_all_unknown(div);
970 p = print_constraints(bmap, space, div, p, latex);
971 isl_mat_free(div);
973 if (exists >= 0 && exists)
974 p = isl_printer_print_str(p, s_close_exists[latex]);
975 return p;
978 /* Print a colon followed by the constraints of "bmap"
979 * to "p", provided there are any constraints.
980 * The names of the variables are taken from "space".
981 * "latex" is set if the constraints should be printed in LaTeX format.
983 static __isl_give isl_printer *print_optional_disjunct(
984 __isl_keep isl_basic_map *bmap, __isl_keep isl_space *space,
985 __isl_take isl_printer *p, int latex)
987 if (isl_basic_map_plain_is_universe(bmap))
988 return p;
990 p = isl_printer_print_str(p, ": ");
991 p = print_disjunct(bmap, space, p, latex);
993 return p;
996 static __isl_give isl_printer *basic_map_print_omega(
997 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
999 p = isl_printer_print_str(p, "{ [");
1000 p = print_var_list(p, bmap->dim, isl_dim_in);
1001 p = isl_printer_print_str(p, "] -> [");
1002 p = print_var_list(p, bmap->dim, isl_dim_out);
1003 p = isl_printer_print_str(p, "] ");
1004 p = print_optional_disjunct(bmap, bmap->dim, p, 0);
1005 p = isl_printer_print_str(p, " }");
1006 return p;
1009 static __isl_give isl_printer *basic_set_print_omega(
1010 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
1012 p = isl_printer_print_str(p, "{ [");
1013 p = print_var_list(p, bset->dim, isl_dim_set);
1014 p = isl_printer_print_str(p, "] ");
1015 p = print_optional_disjunct(bset, bset->dim, p, 0);
1016 p = isl_printer_print_str(p, " }");
1017 return p;
1020 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
1021 __isl_take isl_printer *p)
1023 int i;
1025 for (i = 0; i < map->n; ++i) {
1026 if (i)
1027 p = isl_printer_print_str(p, " union ");
1028 p = basic_map_print_omega(map->p[i], p);
1030 return p;
1033 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
1034 __isl_take isl_printer *p)
1036 int i;
1038 for (i = 0; i < set->n; ++i) {
1039 if (i)
1040 p = isl_printer_print_str(p, " union ");
1041 p = basic_set_print_omega(set->p[i], p);
1043 return p;
1046 /* Print the list of parameters in "space", followed by an arrow, to "p",
1047 * if there are any parameters.
1049 static __isl_give isl_printer *print_param_tuple(__isl_take isl_printer *p,
1050 __isl_keep isl_space *space, struct isl_print_space_data *data)
1052 if (!p || !space)
1053 return isl_printer_free(p);
1054 if (isl_space_dim(space, isl_dim_param) == 0)
1055 return p;
1057 p = print_tuple(space, p, isl_dim_param, data);
1058 p = isl_printer_print_str(p, s_to[data->latex]);
1060 return p;
1063 static __isl_give isl_printer *isl_basic_map_print_isl(
1064 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
1065 int latex)
1067 struct isl_print_space_data data = { .latex = latex };
1068 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1070 p = print_param_tuple(p, bmap->dim, &data);
1071 p = isl_printer_print_str(p, "{ ");
1072 p = isl_print_space(bmap->dim, p, rational, &data);
1073 p = isl_printer_print_str(p, " : ");
1074 p = print_disjunct(bmap, bmap->dim, p, latex);
1075 p = isl_printer_print_str(p, " }");
1076 return p;
1079 /* Print the disjuncts of a map (or set) "map" to "p".
1080 * The names of the variables are taken from "space".
1081 * "latex" is set if the constraints should be printed in LaTeX format.
1083 static __isl_give isl_printer *print_disjuncts_core(__isl_keep isl_map *map,
1084 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1086 int i;
1088 if (map->n == 0)
1089 p = isl_printer_print_str(p, "false");
1090 for (i = 0; i < map->n; ++i) {
1091 if (i)
1092 p = isl_printer_print_str(p, s_or[latex]);
1093 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
1094 p = isl_printer_print_str(p, "(");
1095 p = print_disjunct(map->p[i], space, p, latex);
1096 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
1097 p = isl_printer_print_str(p, ")");
1099 return p;
1102 /* Print the disjuncts of a map (or set) "map" to "p".
1103 * The names of the variables are taken from "space".
1104 * "hull" describes constraints shared by all disjuncts of "map".
1105 * "latex" is set if the constraints should be printed in LaTeX format.
1107 * Print the disjuncts as a conjunction of "hull" and
1108 * the result of removing the constraints of "hull" from "map".
1109 * If this result turns out to be the universe, then simply print "hull".
1111 static __isl_give isl_printer *print_disjuncts_in_hull(__isl_keep isl_map *map,
1112 __isl_keep isl_space *space, __isl_take isl_basic_map *hull,
1113 __isl_take isl_printer *p, int latex)
1115 isl_bool is_universe;
1117 p = print_disjunct(hull, space, p, latex);
1118 map = isl_map_plain_gist_basic_map(isl_map_copy(map), hull);
1119 is_universe = isl_map_plain_is_universe(map);
1120 if (is_universe < 0)
1121 goto error;
1122 if (!is_universe) {
1123 p = isl_printer_print_str(p, s_and[latex]);
1124 p = isl_printer_print_str(p, "(");
1125 p = print_disjuncts_core(map, space, p, latex);
1126 p = isl_printer_print_str(p, ")");
1128 isl_map_free(map);
1130 return p;
1131 error:
1132 isl_map_free(map);
1133 isl_printer_free(p);
1134 return NULL;
1137 /* Print the disjuncts of a map (or set) "map" to "p".
1138 * The names of the variables are taken from "space".
1139 * "latex" is set if the constraints should be printed in LaTeX format.
1141 * If there are at least two disjuncts and "dump" mode is not turned out,
1142 * check for any shared constraints among all disjuncts.
1143 * If there are any, then print them separately in print_disjuncts_in_hull.
1145 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
1146 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1148 if (isl_map_plain_is_universe(map))
1149 return p;
1151 p = isl_printer_print_str(p, s_such_that[latex]);
1152 if (!p)
1153 return NULL;
1155 if (!p->dump && map->n >= 2) {
1156 isl_basic_map *hull;
1157 isl_bool is_universe;
1159 hull = isl_map_plain_unshifted_simple_hull(isl_map_copy(map));
1160 is_universe = isl_basic_map_plain_is_universe(hull);
1161 if (is_universe < 0)
1162 p = isl_printer_free(p);
1163 else if (!is_universe)
1164 return print_disjuncts_in_hull(map, space, hull,
1165 p, latex);
1166 isl_basic_map_free(hull);
1169 return print_disjuncts_core(map, space, p, latex);
1172 /* Print the disjuncts of a map (or set).
1173 * The names of the variables are taken from "space".
1174 * "latex" is set if the constraints should be printed in LaTeX format.
1176 * If the map turns out to be a universal parameter domain, then
1177 * we need to print the colon. Otherwise, the output looks identical
1178 * to the empty set.
1180 static __isl_give isl_printer *print_disjuncts_map(__isl_keep isl_map *map,
1181 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1183 if (isl_map_plain_is_universe(map) && isl_space_is_params(map->dim))
1184 return isl_printer_print_str(p, s_such_that[latex]);
1185 else
1186 return print_disjuncts(map, space, p, latex);
1189 /* Print the disjuncts of a set.
1190 * The names of the variables are taken from "space".
1191 * "latex" is set if the constraints should be printed in LaTeX format.
1193 static __isl_give isl_printer *print_disjuncts_set(__isl_keep isl_set *set,
1194 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1196 return print_disjuncts_map(set_to_map(set), space, p, latex);
1199 struct isl_aff_split {
1200 isl_basic_map *aff;
1201 isl_map *map;
1204 static void free_split(__isl_take struct isl_aff_split *split, int n)
1206 int i;
1208 if (!split)
1209 return;
1211 for (i = 0; i < n; ++i) {
1212 isl_basic_map_free(split[i].aff);
1213 isl_map_free(split[i].map);
1216 free(split);
1219 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
1221 int i, j;
1222 unsigned nparam, n_in, n_out, total;
1224 bmap = isl_basic_map_cow(bmap);
1225 if (!bmap)
1226 return NULL;
1227 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
1228 goto error;
1230 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1231 n_in = isl_basic_map_dim(bmap, isl_dim_in);
1232 n_out = isl_basic_map_dim(bmap, isl_dim_out);
1233 total = isl_basic_map_dim(bmap, isl_dim_all);
1234 for (i = bmap->n_eq - 1; i >= 0; --i) {
1235 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
1236 if (j >= nparam && j < nparam + n_in + n_out &&
1237 (isl_int_is_one(bmap->eq[i][1 + j]) ||
1238 isl_int_is_negone(bmap->eq[i][1 + j])))
1239 continue;
1240 if (isl_basic_map_drop_equality(bmap, i) < 0)
1241 goto error;
1244 bmap = isl_basic_map_finalize(bmap);
1246 return bmap;
1247 error:
1248 isl_basic_map_free(bmap);
1249 return NULL;
1252 static int aff_split_cmp(const void *p1, const void *p2, void *user)
1254 const struct isl_aff_split *s1, *s2;
1255 s1 = (const struct isl_aff_split *) p1;
1256 s2 = (const struct isl_aff_split *) p2;
1258 return isl_basic_map_plain_cmp(s1->aff, s2->aff);
1261 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
1262 __isl_keep isl_basic_map *aff)
1264 int i, j;
1265 int v_div;
1267 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1268 if (v_div < 0 || !aff)
1269 goto error;
1271 for (i = bmap->n_eq - 1; i >= 0; --i) {
1272 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + v_div,
1273 bmap->n_div) != -1)
1274 continue;
1275 for (j = 0; j < aff->n_eq; ++j) {
1276 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + v_div) &&
1277 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + v_div))
1278 continue;
1279 if (isl_basic_map_drop_equality(bmap, i) < 0)
1280 goto error;
1281 break;
1285 return bmap;
1286 error:
1287 isl_basic_map_free(bmap);
1288 return NULL;
1291 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
1293 int i, n;
1294 struct isl_aff_split *split;
1295 isl_ctx *ctx;
1297 ctx = isl_map_get_ctx(map);
1298 split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
1299 if (!split)
1300 return NULL;
1302 for (i = 0; i < map->n; ++i) {
1303 isl_basic_map *bmap;
1304 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
1305 bmap = isl_basic_map_copy(map->p[i]);
1306 bmap = isl_basic_map_cow(bmap);
1307 bmap = drop_aff(bmap, split[i].aff);
1308 split[i].map = isl_map_from_basic_map(bmap);
1309 if (!split[i].aff || !split[i].map)
1310 goto error;
1313 if (isl_sort(split, map->n, sizeof(struct isl_aff_split),
1314 &aff_split_cmp, NULL) < 0)
1315 goto error;
1317 n = map->n;
1318 for (i = n - 1; i >= 1; --i) {
1319 if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
1320 split[i].aff))
1321 continue;
1322 isl_basic_map_free(split[i].aff);
1323 split[i - 1].map = isl_map_union(split[i - 1].map,
1324 split[i].map);
1325 if (i != n - 1)
1326 split[i] = split[n - 1];
1327 split[n - 1].aff = NULL;
1328 split[n - 1].map = NULL;
1329 --n;
1332 return split;
1333 error:
1334 free_split(split, map->n);
1335 return NULL;
1338 static int defining_equality(__isl_keep isl_basic_map *eq,
1339 __isl_keep isl_space *dim, enum isl_dim_type type, int pos)
1341 int i;
1342 unsigned total;
1344 if (!eq)
1345 return -1;
1347 pos += isl_space_offset(dim, type);
1348 total = isl_basic_map_total_dim(eq);
1350 for (i = 0; i < eq->n_eq; ++i) {
1351 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
1352 continue;
1353 if (isl_int_is_one(eq->eq[i][1 + pos]))
1354 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
1355 return i;
1358 return -1;
1361 /* Print dimension "pos" of data->space to "p".
1363 * data->user is assumed to be an isl_basic_map keeping track of equalities.
1365 * If the current dimension is defined by these equalities, then print
1366 * the corresponding expression, assigned to the name of the dimension
1367 * if there is any. Otherwise, print the name of the dimension.
1369 static __isl_give isl_printer *print_dim_eq(__isl_take isl_printer *p,
1370 struct isl_print_space_data *data, unsigned pos)
1372 isl_basic_map *eq = data->user;
1373 int j;
1375 j = defining_equality(eq, data->space, data->type, pos);
1376 if (j >= 0) {
1377 if (isl_space_has_dim_name(data->space, data->type, pos)) {
1378 p = print_name(data->space, p, data->type, pos,
1379 data->latex);
1380 p = isl_printer_print_str(p, " = ");
1382 pos += 1 + isl_space_offset(data->space, data->type);
1383 p = print_affine_of_len(data->space, NULL, p, eq->eq[j], pos);
1384 } else {
1385 p = print_name(data->space, p, data->type, pos, data->latex);
1388 return p;
1391 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
1392 struct isl_aff_split *split, int n, __isl_keep isl_space *space)
1394 struct isl_print_space_data data = { 0 };
1395 int i;
1396 int rational;
1398 data.print_dim = &print_dim_eq;
1399 for (i = 0; i < n; ++i) {
1400 if (!split[i].map)
1401 break;
1402 rational = split[i].map->n > 0 &&
1403 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
1404 if (i)
1405 p = isl_printer_print_str(p, "; ");
1406 data.user = split[i].aff;
1407 p = isl_print_space(space, p, rational, &data);
1408 p = print_disjuncts_map(split[i].map, space, p, 0);
1411 return p;
1414 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
1415 __isl_take isl_printer *p)
1417 struct isl_print_space_data data = { 0 };
1418 struct isl_aff_split *split = NULL;
1419 int rational;
1421 if (!p || !map)
1422 return isl_printer_free(p);
1423 if (!p->dump && map->n > 0)
1424 split = split_aff(map);
1425 if (split) {
1426 p = print_split_map(p, split, map->n, map->dim);
1427 } else {
1428 rational = map->n > 0 &&
1429 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
1430 p = isl_print_space(map->dim, p, rational, &data);
1431 p = print_disjuncts_map(map, map->dim, p, 0);
1433 free_split(split, map->n);
1434 return p;
1437 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
1438 __isl_take isl_printer *p)
1440 struct isl_print_space_data data = { 0 };
1442 p = print_param_tuple(p, map->dim, &data);
1443 p = isl_printer_print_str(p, s_open_set[0]);
1444 p = isl_map_print_isl_body(map, p);
1445 p = isl_printer_print_str(p, s_close_set[0]);
1446 return p;
1449 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
1450 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff)
1452 struct isl_print_space_data data = { 0 };
1454 data.latex = 1;
1455 p = print_param_tuple(p, map->dim, &data);
1456 p = isl_printer_print_str(p, s_open_set[1]);
1457 data.print_dim = &print_dim_eq;
1458 data.user = aff;
1459 p = isl_print_space(map->dim, p, 0, &data);
1460 p = print_disjuncts_map(map, map->dim, p, 1);
1461 p = isl_printer_print_str(p, s_close_set[1]);
1463 return p;
1466 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
1467 __isl_take isl_printer *p)
1469 int i;
1470 struct isl_aff_split *split = NULL;
1472 if (map->n > 0)
1473 split = split_aff(map);
1475 if (!split)
1476 return print_latex_map(map, p, NULL);
1478 for (i = 0; i < map->n; ++i) {
1479 if (!split[i].map)
1480 break;
1481 if (i)
1482 p = isl_printer_print_str(p, " \\cup ");
1483 p = print_latex_map(split[i].map, p, split[i].aff);
1486 free_split(split, map->n);
1487 return p;
1490 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
1491 __isl_keep isl_basic_map *bmap)
1493 if (!p || !bmap)
1494 goto error;
1495 if (p->output_format == ISL_FORMAT_ISL)
1496 return isl_basic_map_print_isl(bmap, p, 0);
1497 else if (p->output_format == ISL_FORMAT_OMEGA)
1498 return basic_map_print_omega(bmap, p);
1499 isl_assert(bmap->ctx, 0, goto error);
1500 error:
1501 isl_printer_free(p);
1502 return NULL;
1505 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
1506 __isl_keep isl_basic_set *bset)
1508 if (!p || !bset)
1509 goto error;
1511 if (p->output_format == ISL_FORMAT_ISL)
1512 return isl_basic_map_print_isl(bset, p, 0);
1513 else if (p->output_format == ISL_FORMAT_POLYLIB)
1514 return isl_basic_set_print_polylib(bset, p, 0);
1515 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1516 return isl_basic_set_print_polylib(bset, p, 1);
1517 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
1518 return bset_print_constraints_polylib(bset, p);
1519 else if (p->output_format == ISL_FORMAT_OMEGA)
1520 return basic_set_print_omega(bset, p);
1521 isl_assert(p->ctx, 0, goto error);
1522 error:
1523 isl_printer_free(p);
1524 return NULL;
1527 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
1528 __isl_keep isl_set *set)
1530 if (!p || !set)
1531 goto error;
1532 if (p->output_format == ISL_FORMAT_ISL)
1533 return isl_map_print_isl(set_to_map(set), p);
1534 else if (p->output_format == ISL_FORMAT_POLYLIB)
1535 return isl_set_print_polylib(set, p, 0);
1536 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1537 return isl_set_print_polylib(set, p, 1);
1538 else if (p->output_format == ISL_FORMAT_OMEGA)
1539 return isl_set_print_omega(set, p);
1540 else if (p->output_format == ISL_FORMAT_LATEX)
1541 return isl_map_print_latex(set_to_map(set), p);
1542 isl_assert(set->ctx, 0, goto error);
1543 error:
1544 isl_printer_free(p);
1545 return NULL;
1548 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1549 __isl_keep isl_map *map)
1551 if (!p || !map)
1552 goto error;
1554 if (p->output_format == ISL_FORMAT_ISL)
1555 return isl_map_print_isl(map, p);
1556 else if (p->output_format == ISL_FORMAT_POLYLIB)
1557 return isl_map_print_polylib(map, p, 0);
1558 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1559 return isl_map_print_polylib(map, p, 1);
1560 else if (p->output_format == ISL_FORMAT_OMEGA)
1561 return isl_map_print_omega(map, p);
1562 else if (p->output_format == ISL_FORMAT_LATEX)
1563 return isl_map_print_latex(map, p);
1564 isl_assert(map->ctx, 0, goto error);
1565 error:
1566 isl_printer_free(p);
1567 return NULL;
1570 struct isl_union_print_data {
1571 isl_printer *p;
1572 int first;
1575 static isl_stat print_map_body(__isl_take isl_map *map, void *user)
1577 struct isl_union_print_data *data;
1578 data = (struct isl_union_print_data *)user;
1580 if (!data->first)
1581 data->p = isl_printer_print_str(data->p, "; ");
1582 data->first = 0;
1584 data->p = isl_map_print_isl_body(map, data->p);
1585 isl_map_free(map);
1587 return isl_stat_ok;
1590 /* Print the body of "umap" (everything except the parameter declarations)
1591 * to "p" in isl format.
1593 static __isl_give isl_printer *isl_printer_print_union_map_isl_body(
1594 __isl_take isl_printer *p, __isl_keep isl_union_map *umap)
1596 struct isl_union_print_data data;
1598 p = isl_printer_print_str(p, s_open_set[0]);
1599 data.p = p;
1600 data.first = 1;
1601 isl_union_map_foreach_map(umap, &print_map_body, &data);
1602 p = data.p;
1603 p = isl_printer_print_str(p, s_close_set[0]);
1604 return p;
1607 /* Print the body of "uset" (everything except the parameter declarations)
1608 * to "p" in isl format.
1610 static __isl_give isl_printer *isl_printer_print_union_set_isl_body(
1611 __isl_take isl_printer *p, __isl_keep isl_union_set *uset)
1613 return isl_printer_print_union_map_isl_body(p, uset_to_umap(uset));
1616 /* Print the isl_union_map "umap" to "p" in isl format.
1618 static __isl_give isl_printer *isl_union_map_print_isl(
1619 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1621 struct isl_print_space_data space_data = { 0 };
1622 isl_space *space;
1624 space = isl_union_map_get_space(umap);
1625 p = print_param_tuple(p, space, &space_data);
1626 isl_space_free(space);
1628 p = isl_printer_print_union_map_isl_body(p, umap);
1630 return p;
1633 static isl_stat print_latex_map_body(__isl_take isl_map *map, void *user)
1635 struct isl_union_print_data *data;
1636 data = (struct isl_union_print_data *)user;
1638 if (!data->first)
1639 data->p = isl_printer_print_str(data->p, " \\cup ");
1640 data->first = 0;
1642 data->p = isl_map_print_latex(map, data->p);
1643 isl_map_free(map);
1645 return isl_stat_ok;
1648 static __isl_give isl_printer *isl_union_map_print_latex(
1649 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1651 struct isl_union_print_data data = { p, 1 };
1652 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1653 p = data.p;
1654 return p;
1657 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1658 __isl_keep isl_union_map *umap)
1660 if (!p || !umap)
1661 goto error;
1663 if (p->output_format == ISL_FORMAT_ISL)
1664 return isl_union_map_print_isl(umap, p);
1665 if (p->output_format == ISL_FORMAT_LATEX)
1666 return isl_union_map_print_latex(umap, p);
1668 isl_die(p->ctx, isl_error_invalid,
1669 "invalid output format for isl_union_map", goto error);
1670 error:
1671 isl_printer_free(p);
1672 return NULL;
1675 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1676 __isl_keep isl_union_set *uset)
1678 if (!p || !uset)
1679 goto error;
1681 if (p->output_format == ISL_FORMAT_ISL)
1682 return isl_union_map_print_isl(uset_to_umap(uset), p);
1683 if (p->output_format == ISL_FORMAT_LATEX)
1684 return isl_union_map_print_latex(uset_to_umap(uset), p);
1686 isl_die(p->ctx, isl_error_invalid,
1687 "invalid output format for isl_union_set", goto error);
1688 error:
1689 isl_printer_free(p);
1690 return NULL;
1693 static int poly_rec_n_non_zero(__isl_keep isl_poly_rec *rec)
1695 int i;
1696 int n;
1698 if (!rec)
1699 return -1;
1701 for (i = 0, n = 0; i < rec->n; ++i) {
1702 isl_bool is_zero = isl_poly_is_zero(rec->p[i]);
1704 if (is_zero < 0)
1705 return -1;
1706 if (!is_zero)
1707 ++n;
1710 return n;
1713 static __isl_give isl_printer *poly_print_cst(__isl_keep isl_poly *poly,
1714 __isl_take isl_printer *p, int first)
1716 isl_poly_cst *cst;
1717 int neg;
1719 cst = isl_poly_as_cst(poly);
1720 if (!cst)
1721 goto error;
1722 neg = !first && isl_int_is_neg(cst->n);
1723 if (!first)
1724 p = isl_printer_print_str(p, neg ? " - " : " + ");
1725 if (neg)
1726 isl_int_neg(cst->n, cst->n);
1727 if (isl_int_is_zero(cst->d)) {
1728 int sgn = isl_int_sgn(cst->n);
1729 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1730 sgn == 0 ? "NaN" : "infty");
1731 } else
1732 p = isl_printer_print_isl_int(p, cst->n);
1733 if (neg)
1734 isl_int_neg(cst->n, cst->n);
1735 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1736 p = isl_printer_print_str(p, "/");
1737 p = isl_printer_print_isl_int(p, cst->d);
1739 return p;
1740 error:
1741 isl_printer_free(p);
1742 return NULL;
1745 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1746 __isl_keep isl_space *space, __isl_keep isl_mat *div, int var)
1748 unsigned total;
1750 total = isl_space_dim(space, isl_dim_all);
1751 if (var < total)
1752 p = print_term(space, NULL, space->ctx->one, 1 + var, p, 0);
1753 else
1754 p = print_div(space, div, var - total, p);
1755 return p;
1758 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1759 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var, int exp)
1761 p = print_base(p, dim, div, var);
1762 if (exp == 1)
1763 return p;
1764 if (p->output_format == ISL_FORMAT_C) {
1765 int i;
1766 for (i = 1; i < exp; ++i) {
1767 p = isl_printer_print_str(p, "*");
1768 p = print_base(p, dim, div, var);
1770 } else {
1771 p = isl_printer_print_str(p, "^");
1772 p = isl_printer_print_int(p, exp);
1774 return p;
1777 /* Print the polynomial "poly" defined over the domain space "space" and
1778 * local variables defined by "div" to "p".
1780 static __isl_give isl_printer *poly_print(__isl_keep isl_poly *poly,
1781 __isl_keep isl_space *space, __isl_keep isl_mat *div,
1782 __isl_take isl_printer *p)
1784 int i, n, first, print_parens;
1785 isl_bool is_cst;
1786 isl_poly_rec *rec;
1788 is_cst = isl_poly_is_cst(poly);
1789 if (!p || is_cst < 0 || !space || !div)
1790 goto error;
1792 if (is_cst)
1793 return poly_print_cst(poly, p, 1);
1795 rec = isl_poly_as_rec(poly);
1796 n = poly_rec_n_non_zero(rec);
1797 if (n < 0)
1798 return isl_printer_free(p);
1799 print_parens = n > 1;
1800 if (print_parens)
1801 p = isl_printer_print_str(p, "(");
1802 for (i = 0, first = 1; i < rec->n; ++i) {
1803 isl_bool is_zero = isl_poly_is_zero(rec->p[i]);
1804 isl_bool is_one = isl_poly_is_one(rec->p[i]);
1805 isl_bool is_negone = isl_poly_is_negone(rec->p[i]);
1806 isl_bool is_cst = isl_poly_is_cst(rec->p[i]);
1808 if (is_zero < 0 || is_one < 0 || is_negone < 0)
1809 return isl_printer_free(p);
1810 if (is_zero)
1811 continue;
1812 if (is_negone) {
1813 if (!i)
1814 p = isl_printer_print_str(p, "-1");
1815 else if (first)
1816 p = isl_printer_print_str(p, "-");
1817 else
1818 p = isl_printer_print_str(p, " - ");
1819 } else if (is_cst && !is_one)
1820 p = poly_print_cst(rec->p[i], p, first);
1821 else {
1822 if (!first)
1823 p = isl_printer_print_str(p, " + ");
1824 if (i == 0 || !is_one)
1825 p = poly_print(rec->p[i], space, div, p);
1827 first = 0;
1828 if (i == 0)
1829 continue;
1830 if (!is_one && !is_negone)
1831 p = isl_printer_print_str(p, " * ");
1832 p = print_pow(p, space, div, rec->poly.var, i);
1834 if (print_parens)
1835 p = isl_printer_print_str(p, ")");
1836 return p;
1837 error:
1838 isl_printer_free(p);
1839 return NULL;
1842 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1843 __isl_keep isl_qpolynomial *qp)
1845 if (!p || !qp)
1846 goto error;
1847 p = poly_print(qp->poly, qp->dim, qp->div, p);
1848 return p;
1849 error:
1850 isl_printer_free(p);
1851 return NULL;
1854 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1855 __isl_keep isl_qpolynomial *qp)
1857 struct isl_print_space_data data = { 0 };
1859 if (!p || !qp)
1860 goto error;
1862 p = print_param_tuple(p, qp->dim, &data);
1863 p = isl_printer_print_str(p, "{ ");
1864 if (!isl_space_is_params(qp->dim)) {
1865 p = isl_print_space(qp->dim, p, 0, &data);
1866 p = isl_printer_print_str(p, " -> ");
1868 p = print_qpolynomial(p, qp);
1869 p = isl_printer_print_str(p, " }");
1870 return p;
1871 error:
1872 isl_printer_free(p);
1873 return NULL;
1876 /* Print the quasi-polynomial "qp" to "p" in C format, with the variable names
1877 * taken from the domain space "space".
1879 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1880 __isl_keep isl_space *space, __isl_keep isl_qpolynomial *qp)
1882 isl_bool is_one;
1883 isl_val *den;
1885 den = isl_qpolynomial_get_den(qp);
1886 qp = isl_qpolynomial_copy(qp);
1887 qp = isl_qpolynomial_scale_val(qp, isl_val_copy(den));
1888 is_one = isl_val_is_one(den);
1889 if (is_one < 0)
1890 p = isl_printer_free(p);
1891 if (!is_one)
1892 p = isl_printer_print_str(p, "(");
1893 if (qp)
1894 p = poly_print(qp->poly, space, qp->div, p);
1895 else
1896 p = isl_printer_free(p);
1897 if (!is_one) {
1898 p = isl_printer_print_str(p, ")/");
1899 p = isl_printer_print_val(p, den);
1901 isl_qpolynomial_free(qp);
1902 isl_val_free(den);
1903 return p;
1906 __isl_give isl_printer *isl_printer_print_qpolynomial(
1907 __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1909 if (!p || !qp)
1910 goto error;
1912 if (p->output_format == ISL_FORMAT_ISL)
1913 return print_qpolynomial_isl(p, qp);
1914 else if (p->output_format == ISL_FORMAT_C)
1915 return print_qpolynomial_c(p, qp->dim, qp);
1916 else
1917 isl_die(qp->dim->ctx, isl_error_unsupported,
1918 "output format not supported for isl_qpolynomials",
1919 goto error);
1920 error:
1921 isl_printer_free(p);
1922 return NULL;
1925 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1926 unsigned output_format)
1928 isl_printer *p;
1930 if (!qp)
1931 return;
1933 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1934 p = isl_printer_to_file(qp->dim->ctx, out);
1935 p = isl_printer_print_qpolynomial(p, qp);
1936 isl_printer_free(p);
1939 static __isl_give isl_printer *qpolynomial_fold_print(
1940 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1942 int i;
1944 if (fold->type == isl_fold_min)
1945 p = isl_printer_print_str(p, "min");
1946 else if (fold->type == isl_fold_max)
1947 p = isl_printer_print_str(p, "max");
1948 p = isl_printer_print_str(p, "(");
1949 for (i = 0; i < fold->n; ++i) {
1950 if (i)
1951 p = isl_printer_print_str(p, ", ");
1952 p = print_qpolynomial(p, fold->qp[i]);
1954 p = isl_printer_print_str(p, ")");
1955 return p;
1958 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1959 FILE *out, unsigned output_format)
1961 isl_printer *p;
1963 if (!fold)
1964 return;
1966 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1968 p = isl_printer_to_file(fold->dim->ctx, out);
1969 p = isl_printer_print_qpolynomial_fold(p, fold);
1971 isl_printer_free(p);
1974 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1975 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1977 struct isl_print_space_data data = { 0 };
1978 int i = 0;
1980 for (i = 0; i < pwqp->n; ++i) {
1981 isl_space *space;
1983 if (i)
1984 p = isl_printer_print_str(p, "; ");
1985 space = isl_qpolynomial_get_domain_space(pwqp->p[i].qp);
1986 if (!isl_space_is_params(space)) {
1987 p = isl_print_space(space, p, 0, &data);
1988 p = isl_printer_print_str(p, " -> ");
1990 p = print_qpolynomial(p, pwqp->p[i].qp);
1991 p = print_disjuncts(set_to_map(pwqp->p[i].set), space, p, 0);
1992 isl_space_free(space);
1995 return p;
1998 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1999 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2001 struct isl_print_space_data data = { 0 };
2003 if (!p || !pwqp)
2004 goto error;
2006 p = print_param_tuple(p, pwqp->dim, &data);
2007 p = isl_printer_print_str(p, "{ ");
2008 if (pwqp->n == 0) {
2009 if (!isl_space_is_set(pwqp->dim)) {
2010 p = print_tuple(pwqp->dim, p, isl_dim_in, &data);
2011 p = isl_printer_print_str(p, " -> ");
2013 p = isl_printer_print_str(p, "0");
2015 p = isl_pwqp_print_isl_body(p, pwqp);
2016 p = isl_printer_print_str(p, " }");
2017 return p;
2018 error:
2019 isl_printer_free(p);
2020 return NULL;
2023 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
2024 unsigned output_format)
2026 isl_printer *p;
2028 if (!pwqp)
2029 return;
2031 p = isl_printer_to_file(pwqp->dim->ctx, out);
2032 p = isl_printer_set_output_format(p, output_format);
2033 p = isl_printer_print_pw_qpolynomial(p, pwqp);
2035 isl_printer_free(p);
2038 static __isl_give isl_printer *isl_pwf_print_isl_body(
2039 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2041 struct isl_print_space_data data = { 0 };
2042 int i = 0;
2044 for (i = 0; i < pwf->n; ++i) {
2045 isl_space *space;
2047 if (i)
2048 p = isl_printer_print_str(p, "; ");
2049 space = isl_qpolynomial_fold_get_domain_space(pwf->p[i].fold);
2050 if (!isl_space_is_params(space)) {
2051 p = isl_print_space(space, p, 0, &data);
2052 p = isl_printer_print_str(p, " -> ");
2054 p = qpolynomial_fold_print(pwf->p[i].fold, p);
2055 p = print_disjuncts(set_to_map(pwf->p[i].set), space, p, 0);
2056 isl_space_free(space);
2059 return p;
2062 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
2063 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2065 struct isl_print_space_data data = { 0 };
2067 p = print_param_tuple(p, pwf->dim, &data);
2068 p = isl_printer_print_str(p, "{ ");
2069 if (pwf->n == 0) {
2070 if (!isl_space_is_set(pwf->dim)) {
2071 p = print_tuple(pwf->dim, p, isl_dim_in, &data);
2072 p = isl_printer_print_str(p, " -> ");
2074 p = isl_printer_print_str(p, "0");
2076 p = isl_pwf_print_isl_body(p, pwf);
2077 p = isl_printer_print_str(p, " }");
2078 return p;
2081 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2082 __isl_keep isl_local_space *ls, isl_int *c);
2084 /* We skip the constraint if it is implied by the div expression.
2086 * *first indicates whether this is the first constraint in the conjunction and
2087 * is updated if the constraint is actually printed.
2089 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
2090 __isl_keep isl_local_space *ls, isl_int *c, const char *op, int *first)
2092 unsigned o_div;
2093 unsigned n_div;
2094 int div;
2096 o_div = isl_local_space_offset(ls, isl_dim_div);
2097 n_div = isl_local_space_dim(ls, isl_dim_div);
2098 div = isl_seq_last_non_zero(c + o_div, n_div);
2099 if (div >= 0) {
2100 isl_bool is_div = isl_local_space_is_div_constraint(ls, c, div);
2101 if (is_div < 0)
2102 return isl_printer_free(p);
2103 if (is_div)
2104 return p;
2107 if (!*first)
2108 p = isl_printer_print_str(p, " && ");
2110 p = print_ls_affine_c(p, ls, c);
2111 p = isl_printer_print_str(p, " ");
2112 p = isl_printer_print_str(p, op);
2113 p = isl_printer_print_str(p, " 0");
2115 *first = 0;
2117 return p;
2120 static __isl_give isl_printer *print_ls_partial_affine_c(
2121 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2122 isl_int *c, unsigned len);
2124 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
2125 __isl_keep isl_space *space, __isl_keep isl_basic_set *bset)
2127 int i, j;
2128 int first = 1;
2129 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
2130 unsigned total = isl_basic_set_total_dim(bset) - n_div;
2131 isl_mat *div;
2132 isl_local_space *ls;
2134 div = isl_basic_set_get_divs(bset);
2135 ls = isl_local_space_alloc_div(isl_space_copy(space), div);
2136 for (i = 0; i < bset->n_eq; ++i) {
2137 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
2138 if (j < 0)
2139 p = print_constraint_c(p, ls,
2140 bset->eq[i], "==", &first);
2141 else {
2142 if (i)
2143 p = isl_printer_print_str(p, " && ");
2144 p = isl_printer_print_str(p, "(");
2145 p = print_ls_partial_affine_c(p, ls, bset->eq[i],
2146 1 + total + j);
2147 p = isl_printer_print_str(p, ") % ");
2148 p = isl_printer_print_isl_int(p,
2149 bset->eq[i][1 + total + j]);
2150 p = isl_printer_print_str(p, " == 0");
2151 first = 0;
2154 for (i = 0; i < bset->n_ineq; ++i)
2155 p = print_constraint_c(p, ls, bset->ineq[i], ">=", &first);
2156 isl_local_space_free(ls);
2157 return p;
2160 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
2161 __isl_keep isl_space *space, __isl_keep isl_set *set)
2163 int i;
2165 if (!set)
2166 return isl_printer_free(p);
2168 if (set->n == 0)
2169 p = isl_printer_print_str(p, "0");
2171 for (i = 0; i < set->n; ++i) {
2172 if (i)
2173 p = isl_printer_print_str(p, " || ");
2174 if (set->n > 1)
2175 p = isl_printer_print_str(p, "(");
2176 p = print_basic_set_c(p, space, set->p[i]);
2177 if (set->n > 1)
2178 p = isl_printer_print_str(p, ")");
2180 return p;
2183 /* Print the piecewise quasi-polynomial "pwqp" to "p" in C format.
2185 static __isl_give isl_printer *print_pw_qpolynomial_c(
2186 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2188 int i;
2189 isl_space *space;
2191 space = isl_pw_qpolynomial_get_domain_space(pwqp);
2192 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set)) {
2193 p = print_qpolynomial_c(p, space, pwqp->p[0].qp);
2194 isl_space_free(space);
2195 return p;
2198 for (i = 0; i < pwqp->n; ++i) {
2199 p = isl_printer_print_str(p, "(");
2200 p = print_set_c(p, space, pwqp->p[i].set);
2201 p = isl_printer_print_str(p, ") ? (");
2202 p = print_qpolynomial_c(p, space, pwqp->p[i].qp);
2203 p = isl_printer_print_str(p, ") : ");
2206 isl_space_free(space);
2207 p = isl_printer_print_str(p, "0");
2208 return p;
2211 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
2212 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2214 if (!p || !pwqp)
2215 goto error;
2217 if (p->output_format == ISL_FORMAT_ISL)
2218 return print_pw_qpolynomial_isl(p, pwqp);
2219 else if (p->output_format == ISL_FORMAT_C)
2220 return print_pw_qpolynomial_c(p, pwqp);
2221 isl_assert(p->ctx, 0, goto error);
2222 error:
2223 isl_printer_free(p);
2224 return NULL;
2227 static isl_stat print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
2229 struct isl_union_print_data *data;
2230 data = (struct isl_union_print_data *)user;
2232 if (!data->first)
2233 data->p = isl_printer_print_str(data->p, "; ");
2234 data->first = 0;
2236 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
2237 isl_pw_qpolynomial_free(pwqp);
2239 return isl_stat_ok;
2242 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
2243 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2245 struct isl_union_print_data data;
2246 struct isl_print_space_data space_data = { 0 };
2247 isl_space *space;
2249 space = isl_union_pw_qpolynomial_get_space(upwqp);
2250 p = print_param_tuple(p, space, &space_data);
2251 isl_space_free(space);
2252 p = isl_printer_print_str(p, "{ ");
2253 data.p = p;
2254 data.first = 1;
2255 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
2256 &data);
2257 p = data.p;
2258 p = isl_printer_print_str(p, " }");
2259 return p;
2262 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
2263 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2265 if (!p || !upwqp)
2266 goto error;
2268 if (p->output_format == ISL_FORMAT_ISL)
2269 return print_union_pw_qpolynomial_isl(p, upwqp);
2270 isl_die(p->ctx, isl_error_invalid,
2271 "invalid output format for isl_union_pw_qpolynomial",
2272 goto error);
2273 error:
2274 isl_printer_free(p);
2275 return NULL;
2278 /* Print the quasi-polynomial reduction "fold" to "p" in C format,
2279 * with the variable names taken from the domain space "space".
2281 static __isl_give isl_printer *print_qpolynomial_fold_c(
2282 __isl_take isl_printer *p, __isl_keep isl_space *space,
2283 __isl_keep isl_qpolynomial_fold *fold)
2285 int i;
2287 for (i = 0; i < fold->n - 1; ++i)
2288 if (fold->type == isl_fold_min)
2289 p = isl_printer_print_str(p, "min(");
2290 else if (fold->type == isl_fold_max)
2291 p = isl_printer_print_str(p, "max(");
2293 for (i = 0; i < fold->n; ++i) {
2294 if (i)
2295 p = isl_printer_print_str(p, ", ");
2296 p = print_qpolynomial_c(p, space, fold->qp[i]);
2297 if (i)
2298 p = isl_printer_print_str(p, ")");
2300 return p;
2303 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
2304 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
2306 if (!p || !fold)
2307 goto error;
2308 if (p->output_format == ISL_FORMAT_ISL)
2309 return qpolynomial_fold_print(fold, p);
2310 else if (p->output_format == ISL_FORMAT_C)
2311 return print_qpolynomial_fold_c(p, fold->dim, fold);
2312 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2313 goto error);
2314 error:
2315 isl_printer_free(p);
2316 return NULL;
2319 /* Print the piecewise quasi-polynomial reduction "pwf" to "p" in C format.
2321 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
2322 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2324 int i;
2325 isl_space *space;
2327 space = isl_pw_qpolynomial_fold_get_domain_space(pwf);
2328 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set)) {
2329 p = print_qpolynomial_fold_c(p, space, pwf->p[0].fold);
2330 isl_space_free(space);
2331 return p;
2334 for (i = 0; i < pwf->n; ++i) {
2335 p = isl_printer_print_str(p, "(");
2336 p = print_set_c(p, space, pwf->p[i].set);
2337 p = isl_printer_print_str(p, ") ? (");
2338 p = print_qpolynomial_fold_c(p, space, pwf->p[i].fold);
2339 p = isl_printer_print_str(p, ") : ");
2342 isl_space_free(space);
2343 p = isl_printer_print_str(p, "0");
2344 return p;
2347 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
2348 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2350 if (!p || !pwf)
2351 goto error;
2353 if (p->output_format == ISL_FORMAT_ISL)
2354 return print_pw_qpolynomial_fold_isl(p, pwf);
2355 else if (p->output_format == ISL_FORMAT_C)
2356 return print_pw_qpolynomial_fold_c(p, pwf);
2357 isl_assert(p->ctx, 0, goto error);
2358 error:
2359 isl_printer_free(p);
2360 return NULL;
2363 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
2364 FILE *out, unsigned output_format)
2366 isl_printer *p;
2368 if (!pwf)
2369 return;
2371 p = isl_printer_to_file(pwf->dim->ctx, out);
2372 p = isl_printer_set_output_format(p, output_format);
2373 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
2375 isl_printer_free(p);
2378 static isl_stat print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf,
2379 void *user)
2381 struct isl_union_print_data *data;
2382 data = (struct isl_union_print_data *)user;
2384 if (!data->first)
2385 data->p = isl_printer_print_str(data->p, "; ");
2386 data->first = 0;
2388 data->p = isl_pwf_print_isl_body(data->p, pwf);
2389 isl_pw_qpolynomial_fold_free(pwf);
2391 return isl_stat_ok;
2394 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
2395 __isl_take isl_printer *p,
2396 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2398 struct isl_union_print_data data;
2399 struct isl_print_space_data space_data = { 0 };
2400 isl_space *space;
2402 space = isl_union_pw_qpolynomial_fold_get_space(upwf);
2403 p = print_param_tuple(p, space, &space_data);
2404 isl_space_free(space);
2405 p = isl_printer_print_str(p, "{ ");
2406 data.p = p;
2407 data.first = 1;
2408 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
2409 &print_pwf_body, &data);
2410 p = data.p;
2411 p = isl_printer_print_str(p, " }");
2412 return p;
2415 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2416 __isl_take isl_printer *p,
2417 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2419 if (!p || !upwf)
2420 goto error;
2422 if (p->output_format == ISL_FORMAT_ISL)
2423 return print_union_pw_qpolynomial_fold_isl(p, upwf);
2424 isl_die(p->ctx, isl_error_invalid,
2425 "invalid output format for isl_union_pw_qpolynomial_fold",
2426 goto error);
2427 error:
2428 isl_printer_free(p);
2429 return NULL;
2432 /* Print the isl_constraint "c" to "p".
2434 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
2435 __isl_keep isl_constraint *c)
2437 struct isl_print_space_data data = { 0 };
2438 isl_local_space *ls;
2439 isl_space *space;
2440 isl_bool exists;
2442 if (!p || !c)
2443 goto error;
2445 ls = isl_constraint_get_local_space(c);
2446 if (!ls)
2447 return isl_printer_free(p);
2448 space = isl_local_space_get_space(ls);
2449 p = print_param_tuple(p, space, &data);
2450 p = isl_printer_print_str(p, "{ ");
2451 p = isl_print_space(space, p, 0, &data);
2452 p = isl_printer_print_str(p, " : ");
2453 exists = need_exists(p, ls->div);
2454 if (exists < 0)
2455 p = isl_printer_free(p);
2456 if (exists >= 0 && exists)
2457 p = open_exists(p, space, ls->div, 0);
2458 p = print_affine_of_len(space, ls->div, p, c->v->el, c->v->size);
2459 if (isl_constraint_is_equality(c))
2460 p = isl_printer_print_str(p, " = 0");
2461 else
2462 p = isl_printer_print_str(p, " >= 0");
2463 if (exists >= 0 && exists)
2464 p = isl_printer_print_str(p, s_close_exists[0]);
2465 p = isl_printer_print_str(p, " }");
2466 isl_space_free(space);
2467 isl_local_space_free(ls);
2469 return p;
2470 error:
2471 isl_printer_free(p);
2472 return NULL;
2475 static __isl_give isl_printer *isl_printer_print_space_isl(
2476 __isl_take isl_printer *p, __isl_keep isl_space *space)
2478 struct isl_print_space_data data = { 0 };
2480 if (!space)
2481 goto error;
2483 p = print_param_tuple(p, space, &data);
2485 p = isl_printer_print_str(p, "{ ");
2486 if (isl_space_is_params(space))
2487 p = isl_printer_print_str(p, s_such_that[0]);
2488 else
2489 p = isl_print_space(space, p, 0, &data);
2490 p = isl_printer_print_str(p, " }");
2492 return p;
2493 error:
2494 isl_printer_free(p);
2495 return NULL;
2498 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
2499 __isl_keep isl_space *space)
2501 if (!p || !space)
2502 return isl_printer_free(p);
2503 if (p->output_format == ISL_FORMAT_ISL)
2504 return isl_printer_print_space_isl(p, space);
2505 else if (p->output_format == ISL_FORMAT_OMEGA)
2506 return print_omega_parameters(space, p);
2508 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
2509 "output format not supported for space",
2510 return isl_printer_free(p));
2513 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
2514 __isl_keep isl_local_space *ls)
2516 struct isl_print_space_data data = { 0 };
2517 unsigned n_div;
2519 if (!ls)
2520 goto error;
2522 p = print_param_tuple(p, ls->dim, &data);
2523 p = isl_printer_print_str(p, "{ ");
2524 p = isl_print_space(ls->dim, p, 0, &data);
2525 n_div = isl_local_space_dim(ls, isl_dim_div);
2526 if (n_div > 0) {
2527 p = isl_printer_print_str(p, " : ");
2528 p = isl_printer_print_str(p, s_open_exists[0]);
2529 p = print_div_list(p, ls->dim, ls->div, 0, 1);
2530 p = isl_printer_print_str(p, s_close_exists[0]);
2531 } else if (isl_space_is_params(ls->dim))
2532 p = isl_printer_print_str(p, s_such_that[0]);
2533 p = isl_printer_print_str(p, " }");
2534 return p;
2535 error:
2536 isl_printer_free(p);
2537 return NULL;
2540 /* Print the (potentially rational) affine expression "aff" to "p",
2541 * with the variable names taken from "space".
2543 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2544 __isl_keep isl_space *space, __isl_keep isl_aff *aff)
2546 unsigned total;
2548 if (isl_aff_is_nan(aff))
2549 return isl_printer_print_str(p, "NaN");
2551 total = isl_local_space_dim(aff->ls, isl_dim_all);
2552 p = isl_printer_print_str(p, "(");
2553 p = print_affine_of_len(space, aff->ls->div, p,
2554 aff->v->el + 1, 1 + total);
2555 if (isl_int_is_one(aff->v->el[0]))
2556 p = isl_printer_print_str(p, ")");
2557 else {
2558 p = isl_printer_print_str(p, ")/");
2559 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2562 return p;
2565 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2566 __isl_keep isl_aff *aff)
2568 struct isl_print_space_data data = { 0 };
2570 if (isl_space_is_params(aff->ls->dim))
2572 else {
2573 p = print_tuple(aff->ls->dim, p, isl_dim_set, &data);
2574 p = isl_printer_print_str(p, " -> ");
2576 p = isl_printer_print_str(p, "[");
2577 p = print_aff_body(p, aff->ls->dim, aff);
2578 p = isl_printer_print_str(p, "]");
2580 return p;
2583 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2584 __isl_keep isl_aff *aff)
2586 struct isl_print_space_data data = { 0 };
2588 if (!aff)
2589 goto error;
2591 p = print_param_tuple(p, aff->ls->dim, &data);
2592 p = isl_printer_print_str(p, "{ ");
2593 p = print_aff(p, aff);
2594 p = isl_printer_print_str(p, " }");
2595 return p;
2596 error:
2597 isl_printer_free(p);
2598 return NULL;
2601 /* Print the body of an isl_pw_aff, i.e., a semicolon delimited
2602 * sequence of affine expressions, each followed by constraints.
2604 static __isl_give isl_printer *print_pw_aff_body(
2605 __isl_take isl_printer *p, __isl_keep isl_pw_aff *pa)
2607 int i;
2609 if (!pa)
2610 return isl_printer_free(p);
2612 for (i = 0; i < pa->n; ++i) {
2613 isl_space *space;
2615 if (i)
2616 p = isl_printer_print_str(p, "; ");
2617 p = print_aff(p, pa->p[i].aff);
2618 space = isl_aff_get_domain_space(pa->p[i].aff);
2619 p = print_disjuncts(set_to_map(pa->p[i].set), space, p, 0);
2620 isl_space_free(space);
2623 return p;
2626 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2627 __isl_keep isl_pw_aff *pwaff)
2629 struct isl_print_space_data data = { 0 };
2631 if (!pwaff)
2632 goto error;
2634 p = print_param_tuple(p, pwaff->dim, &data);
2635 p = isl_printer_print_str(p, "{ ");
2636 p = print_pw_aff_body(p, pwaff);
2637 p = isl_printer_print_str(p, " }");
2638 return p;
2639 error:
2640 isl_printer_free(p);
2641 return NULL;
2644 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2645 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2647 if (type == isl_dim_div) {
2648 p = isl_printer_print_str(p, "floord(");
2649 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2650 p = isl_printer_print_str(p, ", ");
2651 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2652 p = isl_printer_print_str(p, ")");
2653 } else {
2654 const char *name;
2656 name = isl_space_get_dim_name(ls->dim, type, pos);
2657 if (!name)
2658 name = "UNNAMED";
2659 p = isl_printer_print_str(p, name);
2661 return p;
2664 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2665 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2667 enum isl_dim_type type;
2669 if (!p || !ls)
2670 return isl_printer_free(p);
2672 if (pos == 0)
2673 return isl_printer_print_isl_int(p, c);
2675 if (isl_int_is_one(c))
2677 else if (isl_int_is_negone(c))
2678 p = isl_printer_print_str(p, "-");
2679 else {
2680 p = isl_printer_print_isl_int(p, c);
2681 p = isl_printer_print_str(p, "*");
2683 if (pos2type(ls->dim, &type, &pos) < 0)
2684 return isl_printer_free(p);
2685 p = print_ls_name_c(p, ls, type, pos);
2686 return p;
2689 static __isl_give isl_printer *print_ls_partial_affine_c(
2690 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2691 isl_int *c, unsigned len)
2693 int i;
2694 int first;
2696 for (i = 0, first = 1; i < len; ++i) {
2697 int flip = 0;
2698 if (isl_int_is_zero(c[i]))
2699 continue;
2700 if (!first) {
2701 if (isl_int_is_neg(c[i])) {
2702 flip = 1;
2703 isl_int_neg(c[i], c[i]);
2704 p = isl_printer_print_str(p, " - ");
2705 } else
2706 p = isl_printer_print_str(p, " + ");
2708 first = 0;
2709 p = print_ls_term_c(p, ls, c[i], i);
2710 if (flip)
2711 isl_int_neg(c[i], c[i]);
2713 if (first)
2714 p = isl_printer_print_str(p, "0");
2715 return p;
2718 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2719 __isl_keep isl_local_space *ls, isl_int *c)
2721 unsigned len = 1 + isl_local_space_dim(ls, isl_dim_all);
2722 return print_ls_partial_affine_c(p, ls, c, len);
2725 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2726 __isl_keep isl_aff *aff)
2728 unsigned total;
2730 total = isl_local_space_dim(aff->ls, isl_dim_all);
2731 if (!isl_int_is_one(aff->v->el[0]))
2732 p = isl_printer_print_str(p, "(");
2733 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2734 if (!isl_int_is_one(aff->v->el[0])) {
2735 p = isl_printer_print_str(p, ")/");
2736 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2738 return p;
2741 /* In the C format, we cannot express that "pwaff" may be undefined
2742 * on parts of the domain space. We therefore assume that the expression
2743 * will only be evaluated on its definition domain and compute the gist
2744 * of each cell with respect to this domain.
2746 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2747 __isl_keep isl_pw_aff *pwaff)
2749 isl_set *domain;
2750 isl_ast_build *build;
2751 isl_ast_expr *expr;
2753 if (pwaff->n < 1)
2754 isl_die(p->ctx, isl_error_unsupported,
2755 "cannot print empty isl_pw_aff in C format",
2756 return isl_printer_free(p));
2758 domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
2759 build = isl_ast_build_from_context(domain);
2760 expr = isl_ast_build_expr_from_pw_aff(build, isl_pw_aff_copy(pwaff));
2761 p = isl_printer_print_ast_expr(p, expr);
2762 isl_ast_expr_free(expr);
2763 isl_ast_build_free(build);
2765 return p;
2768 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2769 __isl_keep isl_aff *aff)
2771 if (!p || !aff)
2772 goto error;
2774 if (p->output_format == ISL_FORMAT_ISL)
2775 return print_aff_isl(p, aff);
2776 else if (p->output_format == ISL_FORMAT_C)
2777 return print_aff_c(p, aff);
2778 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2779 goto error);
2780 error:
2781 isl_printer_free(p);
2782 return NULL;
2785 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2786 __isl_keep isl_pw_aff *pwaff)
2788 if (!p || !pwaff)
2789 goto error;
2791 if (p->output_format == ISL_FORMAT_ISL)
2792 return print_pw_aff_isl(p, pwaff);
2793 else if (p->output_format == ISL_FORMAT_C)
2794 return print_pw_aff_c(p, pwaff);
2795 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2796 goto error);
2797 error:
2798 isl_printer_free(p);
2799 return NULL;
2802 /* Print "pa" in a sequence of isl_pw_affs delimited by semicolons.
2803 * Each isl_pw_aff itself is also printed as semicolon delimited
2804 * sequence of pieces.
2805 * If data->first = 1, then this is the first in the sequence.
2806 * Update data->first to tell the next element that it is not the first.
2808 static isl_stat print_pw_aff_body_wrap(__isl_take isl_pw_aff *pa,
2809 void *user)
2811 struct isl_union_print_data *data;
2812 data = (struct isl_union_print_data *) user;
2814 if (!data->first)
2815 data->p = isl_printer_print_str(data->p, "; ");
2816 data->first = 0;
2818 data->p = print_pw_aff_body(data->p, pa);
2819 isl_pw_aff_free(pa);
2821 return data->p ? isl_stat_ok : isl_stat_error;
2824 /* Print the body of an isl_union_pw_aff, i.e., a semicolon delimited
2825 * sequence of affine expressions, each followed by constraints,
2826 * with the sequence enclosed in braces.
2828 static __isl_give isl_printer *print_union_pw_aff_body(
2829 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2831 struct isl_union_print_data data = { p, 1 };
2833 p = isl_printer_print_str(p, s_open_set[0]);
2834 data.p = p;
2835 if (isl_union_pw_aff_foreach_pw_aff(upa,
2836 &print_pw_aff_body_wrap, &data) < 0)
2837 data.p = isl_printer_free(p);
2838 p = data.p;
2839 p = isl_printer_print_str(p, s_close_set[0]);
2841 return p;
2844 /* Print the isl_union_pw_aff "upa" to "p" in isl format.
2846 * The individual isl_pw_affs are delimited by a semicolon.
2848 static __isl_give isl_printer *print_union_pw_aff_isl(
2849 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2851 struct isl_print_space_data data = { 0 };
2852 isl_space *space;
2854 space = isl_union_pw_aff_get_space(upa);
2855 p = print_param_tuple(p, space, &data);
2856 isl_space_free(space);
2857 p = print_union_pw_aff_body(p, upa);
2858 return p;
2861 /* Print the isl_union_pw_aff "upa" to "p".
2863 * We currently only support an isl format.
2865 __isl_give isl_printer *isl_printer_print_union_pw_aff(
2866 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2868 if (!p || !upa)
2869 return isl_printer_free(p);
2871 if (p->output_format == ISL_FORMAT_ISL)
2872 return print_union_pw_aff_isl(p, upa);
2873 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2874 "unsupported output format", return isl_printer_free(p));
2877 /* Print dimension "pos" of data->space to "p".
2879 * data->user is assumed to be an isl_multi_aff.
2881 * If the current dimension is an output dimension, then print
2882 * the corresponding expression. Otherwise, print the name of the dimension.
2884 static __isl_give isl_printer *print_dim_ma(__isl_take isl_printer *p,
2885 struct isl_print_space_data *data, unsigned pos)
2887 isl_multi_aff *ma = data->user;
2889 if (data->type == isl_dim_out) {
2890 isl_space *space;
2892 space = isl_multi_aff_get_domain_space(ma);
2893 p = print_aff_body(p, space, ma->u.p[pos]);
2894 isl_space_free(space);
2895 } else {
2896 p = print_name(data->space, p, data->type, pos, data->latex);
2899 return p;
2902 static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
2903 __isl_keep isl_multi_aff *maff)
2905 struct isl_print_space_data data = { 0 };
2907 data.print_dim = &print_dim_ma;
2908 data.user = maff;
2909 return isl_print_space(maff->space, p, 0, &data);
2912 static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
2913 __isl_keep isl_multi_aff *maff)
2915 struct isl_print_space_data data = { 0 };
2917 if (!maff)
2918 goto error;
2920 p = print_param_tuple(p, maff->space, &data);
2921 p = isl_printer_print_str(p, "{ ");
2922 p = print_multi_aff(p, maff);
2923 p = isl_printer_print_str(p, " }");
2924 return p;
2925 error:
2926 isl_printer_free(p);
2927 return NULL;
2930 __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
2931 __isl_keep isl_multi_aff *maff)
2933 if (!p || !maff)
2934 goto error;
2936 if (p->output_format == ISL_FORMAT_ISL)
2937 return print_multi_aff_isl(p, maff);
2938 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2939 goto error);
2940 error:
2941 isl_printer_free(p);
2942 return NULL;
2945 static __isl_give isl_printer *print_pw_multi_aff_body(
2946 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2948 int i;
2950 if (!pma)
2951 goto error;
2953 for (i = 0; i < pma->n; ++i) {
2954 isl_space *space;
2956 if (i)
2957 p = isl_printer_print_str(p, "; ");
2958 p = print_multi_aff(p, pma->p[i].maff);
2959 space = isl_multi_aff_get_domain_space(pma->p[i].maff);
2960 p = print_disjuncts(set_to_map(pma->p[i].set), space, p, 0);
2961 isl_space_free(space);
2963 return p;
2964 error:
2965 isl_printer_free(p);
2966 return NULL;
2969 static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
2970 __isl_keep isl_pw_multi_aff *pma)
2972 struct isl_print_space_data data = { 0 };
2974 if (!pma)
2975 goto error;
2977 p = print_param_tuple(p, pma->dim, &data);
2978 p = isl_printer_print_str(p, "{ ");
2979 p = print_pw_multi_aff_body(p, pma);
2980 p = isl_printer_print_str(p, " }");
2981 return p;
2982 error:
2983 isl_printer_free(p);
2984 return NULL;
2987 /* Print the unnamed, single-dimensional piecewise multi affine expression "pma"
2988 * to "p".
2990 static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
2991 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2993 int i;
2994 isl_space *space;
2996 space = isl_pw_multi_aff_get_domain_space(pma);
2997 for (i = 0; i < pma->n - 1; ++i) {
2998 p = isl_printer_print_str(p, "(");
2999 p = print_set_c(p, space, pma->p[i].set);
3000 p = isl_printer_print_str(p, ") ? (");
3001 p = print_aff_c(p, pma->p[i].maff->u.p[0]);
3002 p = isl_printer_print_str(p, ") : ");
3004 isl_space_free(space);
3006 return print_aff_c(p, pma->p[pma->n - 1].maff->u.p[0]);
3009 static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
3010 __isl_keep isl_pw_multi_aff *pma)
3012 int n;
3013 const char *name;
3015 if (!pma)
3016 goto error;
3017 if (pma->n < 1)
3018 isl_die(p->ctx, isl_error_unsupported,
3019 "cannot print empty isl_pw_multi_aff in C format",
3020 goto error);
3021 name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
3022 if (!name && isl_pw_multi_aff_dim(pma, isl_dim_out) == 1)
3023 return print_unnamed_pw_multi_aff_c(p, pma);
3024 if (!name)
3025 isl_die(p->ctx, isl_error_unsupported,
3026 "cannot print unnamed isl_pw_multi_aff in C format",
3027 goto error);
3029 p = isl_printer_print_str(p, name);
3030 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
3031 if (n != 0)
3032 isl_die(p->ctx, isl_error_unsupported,
3033 "not supported yet", goto error);
3035 return p;
3036 error:
3037 isl_printer_free(p);
3038 return NULL;
3041 __isl_give isl_printer *isl_printer_print_pw_multi_aff(
3042 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3044 if (!p || !pma)
3045 goto error;
3047 if (p->output_format == ISL_FORMAT_ISL)
3048 return print_pw_multi_aff_isl(p, pma);
3049 if (p->output_format == ISL_FORMAT_C)
3050 return print_pw_multi_aff_c(p, pma);
3051 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3052 goto error);
3053 error:
3054 isl_printer_free(p);
3055 return NULL;
3058 static isl_stat print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
3059 void *user)
3061 struct isl_union_print_data *data;
3062 data = (struct isl_union_print_data *) user;
3064 if (!data->first)
3065 data->p = isl_printer_print_str(data->p, "; ");
3066 data->first = 0;
3068 data->p = print_pw_multi_aff_body(data->p, pma);
3069 isl_pw_multi_aff_free(pma);
3071 return isl_stat_ok;
3074 static __isl_give isl_printer *print_union_pw_multi_aff_isl(
3075 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
3077 struct isl_union_print_data data;
3078 struct isl_print_space_data space_data = { 0 };
3079 isl_space *space;
3081 space = isl_union_pw_multi_aff_get_space(upma);
3082 p = print_param_tuple(p, space, &space_data);
3083 isl_space_free(space);
3084 p = isl_printer_print_str(p, s_open_set[0]);
3085 data.p = p;
3086 data.first = 1;
3087 isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3088 &print_pw_multi_aff_body_wrap, &data);
3089 p = data.p;
3090 p = isl_printer_print_str(p, s_close_set[0]);
3091 return p;
3094 __isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
3095 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
3097 if (!p || !upma)
3098 goto error;
3100 if (p->output_format == ISL_FORMAT_ISL)
3101 return print_union_pw_multi_aff_isl(p, upma);
3102 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3103 goto error);
3104 error:
3105 isl_printer_free(p);
3106 return NULL;
3109 /* Print dimension "pos" of data->space to "p".
3111 * data->user is assumed to be an isl_multi_pw_aff.
3113 * If the current dimension is an output dimension, then print
3114 * the corresponding piecewise affine expression.
3115 * Otherwise, print the name of the dimension.
3117 static __isl_give isl_printer *print_dim_mpa(__isl_take isl_printer *p,
3118 struct isl_print_space_data *data, unsigned pos)
3120 int i;
3121 int need_parens;
3122 isl_space *space;
3123 isl_multi_pw_aff *mpa = data->user;
3124 isl_pw_aff *pa;
3126 if (data->type != isl_dim_out)
3127 return print_name(data->space, p, data->type, pos, data->latex);
3129 pa = mpa->u.p[pos];
3130 if (pa->n == 0)
3131 return isl_printer_print_str(p, "(0 : false)");
3133 need_parens = pa->n != 1 || !isl_set_plain_is_universe(pa->p[0].set);
3134 if (need_parens)
3135 p = isl_printer_print_str(p, "(");
3136 space = isl_multi_pw_aff_get_domain_space(mpa);
3137 for (i = 0; i < pa->n; ++i) {
3139 if (i)
3140 p = isl_printer_print_str(p, "; ");
3141 p = print_aff_body(p, space, pa->p[i].aff);
3142 p = print_disjuncts(pa->p[i].set, space, p, 0);
3144 isl_space_free(space);
3145 if (need_parens)
3146 p = isl_printer_print_str(p, ")");
3148 return p;
3151 /* Print "mpa" to "p" in isl format.
3153 * If "mpa" is zero-dimensional and has a non-trivial explicit domain,
3154 * then it is printed after the tuple of affine expressions.
3156 static __isl_give isl_printer *print_multi_pw_aff_isl(__isl_take isl_printer *p,
3157 __isl_keep isl_multi_pw_aff *mpa)
3159 struct isl_print_space_data data = { 0 };
3160 isl_bool has_domain;
3162 if (!mpa)
3163 return isl_printer_free(p);
3165 p = print_param_tuple(p, mpa->space, &data);
3166 p = isl_printer_print_str(p, "{ ");
3167 data.print_dim = &print_dim_mpa;
3168 data.user = mpa;
3169 p = isl_print_space(mpa->space, p, 0, &data);
3170 has_domain = isl_multi_pw_aff_has_non_trivial_domain(mpa);
3171 if (has_domain < 0)
3172 return isl_printer_free(p);
3173 if (has_domain) {
3174 isl_space *space;
3176 space = isl_space_domain(isl_space_copy(mpa->space));
3177 p = print_disjuncts_set(mpa->u.dom, space, p, 0);
3178 isl_space_free(space);
3180 p = isl_printer_print_str(p, " }");
3181 return p;
3184 __isl_give isl_printer *isl_printer_print_multi_pw_aff(
3185 __isl_take isl_printer *p, __isl_keep isl_multi_pw_aff *mpa)
3187 if (!p || !mpa)
3188 return isl_printer_free(p);
3190 if (p->output_format == ISL_FORMAT_ISL)
3191 return print_multi_pw_aff_isl(p, mpa);
3192 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3193 return isl_printer_free(p));
3196 /* Print dimension "pos" of data->space to "p".
3198 * data->user is assumed to be an isl_multi_val.
3200 * If the current dimension is an output dimension, then print
3201 * the corresponding value. Otherwise, print the name of the dimension.
3203 static __isl_give isl_printer *print_dim_mv(__isl_take isl_printer *p,
3204 struct isl_print_space_data *data, unsigned pos)
3206 isl_multi_val *mv = data->user;
3208 if (data->type == isl_dim_out)
3209 return isl_printer_print_val(p, mv->u.p[pos]);
3210 else
3211 return print_name(data->space, p, data->type, pos, data->latex);
3214 /* Print the isl_multi_val "mv" to "p" in isl format.
3216 static __isl_give isl_printer *print_multi_val_isl(__isl_take isl_printer *p,
3217 __isl_keep isl_multi_val *mv)
3219 struct isl_print_space_data data = { 0 };
3221 if (!mv)
3222 return isl_printer_free(p);
3224 p = print_param_tuple(p, mv->space, &data);
3225 p = isl_printer_print_str(p, "{ ");
3226 data.print_dim = &print_dim_mv;
3227 data.user = mv;
3228 p = isl_print_space(mv->space, p, 0, &data);
3229 p = isl_printer_print_str(p, " }");
3230 return p;
3233 /* Print the isl_multi_val "mv" to "p".
3235 * Currently only supported in isl format.
3237 __isl_give isl_printer *isl_printer_print_multi_val(
3238 __isl_take isl_printer *p, __isl_keep isl_multi_val *mv)
3240 if (!p || !mv)
3241 return isl_printer_free(p);
3243 if (p->output_format == ISL_FORMAT_ISL)
3244 return print_multi_val_isl(p, mv);
3245 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3246 return isl_printer_free(p));
3249 /* Print dimension "pos" of data->space to "p".
3251 * data->user is assumed to be an isl_multi_union_pw_aff.
3253 * The current dimension is necessarily a set dimension, so
3254 * we print the corresponding isl_union_pw_aff, including
3255 * the braces.
3257 static __isl_give isl_printer *print_union_pw_aff_dim(__isl_take isl_printer *p,
3258 struct isl_print_space_data *data, unsigned pos)
3260 isl_multi_union_pw_aff *mupa = data->user;
3261 isl_union_pw_aff *upa;
3263 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, pos);
3264 p = print_union_pw_aff_body(p, upa);
3265 isl_union_pw_aff_free(upa);
3267 return p;
3270 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3272 * If "mupa" is zero-dimensional and has a non-trivial explicit domain,
3273 * then it is printed after the tuple of affine expressions.
3274 * In order to clarify that this domain belongs to the expression,
3275 * the tuple along with the domain are placed inside parentheses.
3276 * If "mupa" has any parameters, then the opening parenthesis
3277 * appears after the parameter declarations.
3279 static __isl_give isl_printer *print_multi_union_pw_aff_isl(
3280 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3282 struct isl_print_space_data data = { 0 };
3283 isl_bool has_domain;
3284 isl_space *space;
3286 if (!mupa)
3287 return isl_printer_free(p);
3288 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
3289 if (has_domain < 0)
3290 return isl_printer_free(p);
3292 space = isl_multi_union_pw_aff_get_space(mupa);
3293 p = print_param_tuple(p, space, &data);
3295 if (has_domain)
3296 p = isl_printer_print_str(p, "(");
3298 data.print_dim = &print_union_pw_aff_dim;
3299 data.user = mupa;
3301 p = isl_print_space(space, p, 0, &data);
3302 isl_space_free(space);
3304 if (has_domain) {
3305 p = isl_printer_print_str(p, " : ");
3306 p = isl_printer_print_union_set_isl_body(p, mupa->u.dom);
3307 p = isl_printer_print_str(p, ")");
3310 return p;
3313 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3315 * We currently only support an isl format.
3317 __isl_give isl_printer *isl_printer_print_multi_union_pw_aff(
3318 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3320 if (!p || !mupa)
3321 return isl_printer_free(p);
3323 if (p->output_format == ISL_FORMAT_ISL)
3324 return print_multi_union_pw_aff_isl(p, mupa);
3325 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
3326 "unsupported output format", return isl_printer_free(p));