isl_map_simplify.c: coalesce_divs: use isl_basic_map_var_offset
[isl.git] / isl_output.c
blobcdfccc7441e90f55f3d1a0a29073ff249188db02
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 unsigned total;
1267 if (!bmap || !aff)
1268 goto error;
1270 total = isl_space_dim(bmap->dim, isl_dim_all);
1272 for (i = bmap->n_eq - 1; i >= 0; --i) {
1273 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
1274 bmap->n_div) != -1)
1275 continue;
1276 for (j = 0; j < aff->n_eq; ++j) {
1277 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
1278 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
1279 continue;
1280 if (isl_basic_map_drop_equality(bmap, i) < 0)
1281 goto error;
1282 break;
1286 return bmap;
1287 error:
1288 isl_basic_map_free(bmap);
1289 return NULL;
1292 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
1294 int i, n;
1295 struct isl_aff_split *split;
1296 isl_ctx *ctx;
1298 ctx = isl_map_get_ctx(map);
1299 split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
1300 if (!split)
1301 return NULL;
1303 for (i = 0; i < map->n; ++i) {
1304 isl_basic_map *bmap;
1305 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
1306 bmap = isl_basic_map_copy(map->p[i]);
1307 bmap = isl_basic_map_cow(bmap);
1308 bmap = drop_aff(bmap, split[i].aff);
1309 split[i].map = isl_map_from_basic_map(bmap);
1310 if (!split[i].aff || !split[i].map)
1311 goto error;
1314 if (isl_sort(split, map->n, sizeof(struct isl_aff_split),
1315 &aff_split_cmp, NULL) < 0)
1316 goto error;
1318 n = map->n;
1319 for (i = n - 1; i >= 1; --i) {
1320 if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
1321 split[i].aff))
1322 continue;
1323 isl_basic_map_free(split[i].aff);
1324 split[i - 1].map = isl_map_union(split[i - 1].map,
1325 split[i].map);
1326 if (i != n - 1)
1327 split[i] = split[n - 1];
1328 split[n - 1].aff = NULL;
1329 split[n - 1].map = NULL;
1330 --n;
1333 return split;
1334 error:
1335 free_split(split, map->n);
1336 return NULL;
1339 static int defining_equality(__isl_keep isl_basic_map *eq,
1340 __isl_keep isl_space *dim, enum isl_dim_type type, int pos)
1342 int i;
1343 unsigned total;
1345 if (!eq)
1346 return -1;
1348 pos += isl_space_offset(dim, type);
1349 total = isl_basic_map_total_dim(eq);
1351 for (i = 0; i < eq->n_eq; ++i) {
1352 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
1353 continue;
1354 if (isl_int_is_one(eq->eq[i][1 + pos]))
1355 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
1356 return i;
1359 return -1;
1362 /* Print dimension "pos" of data->space to "p".
1364 * data->user is assumed to be an isl_basic_map keeping track of equalities.
1366 * If the current dimension is defined by these equalities, then print
1367 * the corresponding expression, assigned to the name of the dimension
1368 * if there is any. Otherwise, print the name of the dimension.
1370 static __isl_give isl_printer *print_dim_eq(__isl_take isl_printer *p,
1371 struct isl_print_space_data *data, unsigned pos)
1373 isl_basic_map *eq = data->user;
1374 int j;
1376 j = defining_equality(eq, data->space, data->type, pos);
1377 if (j >= 0) {
1378 if (isl_space_has_dim_name(data->space, data->type, pos)) {
1379 p = print_name(data->space, p, data->type, pos,
1380 data->latex);
1381 p = isl_printer_print_str(p, " = ");
1383 pos += 1 + isl_space_offset(data->space, data->type);
1384 p = print_affine_of_len(data->space, NULL, p, eq->eq[j], pos);
1385 } else {
1386 p = print_name(data->space, p, data->type, pos, data->latex);
1389 return p;
1392 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
1393 struct isl_aff_split *split, int n, __isl_keep isl_space *space)
1395 struct isl_print_space_data data = { 0 };
1396 int i;
1397 int rational;
1399 data.print_dim = &print_dim_eq;
1400 for (i = 0; i < n; ++i) {
1401 if (!split[i].map)
1402 break;
1403 rational = split[i].map->n > 0 &&
1404 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
1405 if (i)
1406 p = isl_printer_print_str(p, "; ");
1407 data.user = split[i].aff;
1408 p = isl_print_space(space, p, rational, &data);
1409 p = print_disjuncts_map(split[i].map, space, p, 0);
1412 return p;
1415 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
1416 __isl_take isl_printer *p)
1418 struct isl_print_space_data data = { 0 };
1419 struct isl_aff_split *split = NULL;
1420 int rational;
1422 if (!p || !map)
1423 return isl_printer_free(p);
1424 if (!p->dump && map->n > 0)
1425 split = split_aff(map);
1426 if (split) {
1427 p = print_split_map(p, split, map->n, map->dim);
1428 } else {
1429 rational = map->n > 0 &&
1430 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
1431 p = isl_print_space(map->dim, p, rational, &data);
1432 p = print_disjuncts_map(map, map->dim, p, 0);
1434 free_split(split, map->n);
1435 return p;
1438 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
1439 __isl_take isl_printer *p)
1441 struct isl_print_space_data data = { 0 };
1443 p = print_param_tuple(p, map->dim, &data);
1444 p = isl_printer_print_str(p, s_open_set[0]);
1445 p = isl_map_print_isl_body(map, p);
1446 p = isl_printer_print_str(p, s_close_set[0]);
1447 return p;
1450 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
1451 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff)
1453 struct isl_print_space_data data = { 0 };
1455 data.latex = 1;
1456 p = print_param_tuple(p, map->dim, &data);
1457 p = isl_printer_print_str(p, s_open_set[1]);
1458 data.print_dim = &print_dim_eq;
1459 data.user = aff;
1460 p = isl_print_space(map->dim, p, 0, &data);
1461 p = print_disjuncts_map(map, map->dim, p, 1);
1462 p = isl_printer_print_str(p, s_close_set[1]);
1464 return p;
1467 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
1468 __isl_take isl_printer *p)
1470 int i;
1471 struct isl_aff_split *split = NULL;
1473 if (map->n > 0)
1474 split = split_aff(map);
1476 if (!split)
1477 return print_latex_map(map, p, NULL);
1479 for (i = 0; i < map->n; ++i) {
1480 if (!split[i].map)
1481 break;
1482 if (i)
1483 p = isl_printer_print_str(p, " \\cup ");
1484 p = print_latex_map(split[i].map, p, split[i].aff);
1487 free_split(split, map->n);
1488 return p;
1491 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
1492 __isl_keep isl_basic_map *bmap)
1494 if (!p || !bmap)
1495 goto error;
1496 if (p->output_format == ISL_FORMAT_ISL)
1497 return isl_basic_map_print_isl(bmap, p, 0);
1498 else if (p->output_format == ISL_FORMAT_OMEGA)
1499 return basic_map_print_omega(bmap, p);
1500 isl_assert(bmap->ctx, 0, goto error);
1501 error:
1502 isl_printer_free(p);
1503 return NULL;
1506 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
1507 __isl_keep isl_basic_set *bset)
1509 if (!p || !bset)
1510 goto error;
1512 if (p->output_format == ISL_FORMAT_ISL)
1513 return isl_basic_map_print_isl(bset, p, 0);
1514 else if (p->output_format == ISL_FORMAT_POLYLIB)
1515 return isl_basic_set_print_polylib(bset, p, 0);
1516 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1517 return isl_basic_set_print_polylib(bset, p, 1);
1518 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
1519 return bset_print_constraints_polylib(bset, p);
1520 else if (p->output_format == ISL_FORMAT_OMEGA)
1521 return basic_set_print_omega(bset, p);
1522 isl_assert(p->ctx, 0, goto error);
1523 error:
1524 isl_printer_free(p);
1525 return NULL;
1528 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
1529 __isl_keep isl_set *set)
1531 if (!p || !set)
1532 goto error;
1533 if (p->output_format == ISL_FORMAT_ISL)
1534 return isl_map_print_isl(set_to_map(set), p);
1535 else if (p->output_format == ISL_FORMAT_POLYLIB)
1536 return isl_set_print_polylib(set, p, 0);
1537 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1538 return isl_set_print_polylib(set, p, 1);
1539 else if (p->output_format == ISL_FORMAT_OMEGA)
1540 return isl_set_print_omega(set, p);
1541 else if (p->output_format == ISL_FORMAT_LATEX)
1542 return isl_map_print_latex(set_to_map(set), p);
1543 isl_assert(set->ctx, 0, goto error);
1544 error:
1545 isl_printer_free(p);
1546 return NULL;
1549 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1550 __isl_keep isl_map *map)
1552 if (!p || !map)
1553 goto error;
1555 if (p->output_format == ISL_FORMAT_ISL)
1556 return isl_map_print_isl(map, p);
1557 else if (p->output_format == ISL_FORMAT_POLYLIB)
1558 return isl_map_print_polylib(map, p, 0);
1559 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1560 return isl_map_print_polylib(map, p, 1);
1561 else if (p->output_format == ISL_FORMAT_OMEGA)
1562 return isl_map_print_omega(map, p);
1563 else if (p->output_format == ISL_FORMAT_LATEX)
1564 return isl_map_print_latex(map, p);
1565 isl_assert(map->ctx, 0, goto error);
1566 error:
1567 isl_printer_free(p);
1568 return NULL;
1571 struct isl_union_print_data {
1572 isl_printer *p;
1573 int first;
1576 static isl_stat print_map_body(__isl_take isl_map *map, void *user)
1578 struct isl_union_print_data *data;
1579 data = (struct isl_union_print_data *)user;
1581 if (!data->first)
1582 data->p = isl_printer_print_str(data->p, "; ");
1583 data->first = 0;
1585 data->p = isl_map_print_isl_body(map, data->p);
1586 isl_map_free(map);
1588 return isl_stat_ok;
1591 /* Print the body of "umap" (everything except the parameter declarations)
1592 * to "p" in isl format.
1594 static __isl_give isl_printer *isl_printer_print_union_map_isl_body(
1595 __isl_take isl_printer *p, __isl_keep isl_union_map *umap)
1597 struct isl_union_print_data data;
1599 p = isl_printer_print_str(p, s_open_set[0]);
1600 data.p = p;
1601 data.first = 1;
1602 isl_union_map_foreach_map(umap, &print_map_body, &data);
1603 p = data.p;
1604 p = isl_printer_print_str(p, s_close_set[0]);
1605 return p;
1608 /* Print the body of "uset" (everything except the parameter declarations)
1609 * to "p" in isl format.
1611 static __isl_give isl_printer *isl_printer_print_union_set_isl_body(
1612 __isl_take isl_printer *p, __isl_keep isl_union_set *uset)
1614 return isl_printer_print_union_map_isl_body(p, uset_to_umap(uset));
1617 /* Print the isl_union_map "umap" to "p" in isl format.
1619 static __isl_give isl_printer *isl_union_map_print_isl(
1620 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1622 struct isl_print_space_data space_data = { 0 };
1623 isl_space *space;
1625 space = isl_union_map_get_space(umap);
1626 p = print_param_tuple(p, space, &space_data);
1627 isl_space_free(space);
1629 p = isl_printer_print_union_map_isl_body(p, umap);
1631 return p;
1634 static isl_stat print_latex_map_body(__isl_take isl_map *map, void *user)
1636 struct isl_union_print_data *data;
1637 data = (struct isl_union_print_data *)user;
1639 if (!data->first)
1640 data->p = isl_printer_print_str(data->p, " \\cup ");
1641 data->first = 0;
1643 data->p = isl_map_print_latex(map, data->p);
1644 isl_map_free(map);
1646 return isl_stat_ok;
1649 static __isl_give isl_printer *isl_union_map_print_latex(
1650 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1652 struct isl_union_print_data data = { p, 1 };
1653 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1654 p = data.p;
1655 return p;
1658 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1659 __isl_keep isl_union_map *umap)
1661 if (!p || !umap)
1662 goto error;
1664 if (p->output_format == ISL_FORMAT_ISL)
1665 return isl_union_map_print_isl(umap, p);
1666 if (p->output_format == ISL_FORMAT_LATEX)
1667 return isl_union_map_print_latex(umap, p);
1669 isl_die(p->ctx, isl_error_invalid,
1670 "invalid output format for isl_union_map", goto error);
1671 error:
1672 isl_printer_free(p);
1673 return NULL;
1676 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1677 __isl_keep isl_union_set *uset)
1679 if (!p || !uset)
1680 goto error;
1682 if (p->output_format == ISL_FORMAT_ISL)
1683 return isl_union_map_print_isl(uset_to_umap(uset), p);
1684 if (p->output_format == ISL_FORMAT_LATEX)
1685 return isl_union_map_print_latex(uset_to_umap(uset), p);
1687 isl_die(p->ctx, isl_error_invalid,
1688 "invalid output format for isl_union_set", goto error);
1689 error:
1690 isl_printer_free(p);
1691 return NULL;
1694 static int poly_rec_n_non_zero(__isl_keep isl_poly_rec *rec)
1696 int i;
1697 int n;
1699 if (!rec)
1700 return -1;
1702 for (i = 0, n = 0; i < rec->n; ++i) {
1703 isl_bool is_zero = isl_poly_is_zero(rec->p[i]);
1705 if (is_zero < 0)
1706 return -1;
1707 if (!is_zero)
1708 ++n;
1711 return n;
1714 static __isl_give isl_printer *poly_print_cst(__isl_keep isl_poly *poly,
1715 __isl_take isl_printer *p, int first)
1717 isl_poly_cst *cst;
1718 int neg;
1720 cst = isl_poly_as_cst(poly);
1721 if (!cst)
1722 goto error;
1723 neg = !first && isl_int_is_neg(cst->n);
1724 if (!first)
1725 p = isl_printer_print_str(p, neg ? " - " : " + ");
1726 if (neg)
1727 isl_int_neg(cst->n, cst->n);
1728 if (isl_int_is_zero(cst->d)) {
1729 int sgn = isl_int_sgn(cst->n);
1730 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1731 sgn == 0 ? "NaN" : "infty");
1732 } else
1733 p = isl_printer_print_isl_int(p, cst->n);
1734 if (neg)
1735 isl_int_neg(cst->n, cst->n);
1736 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1737 p = isl_printer_print_str(p, "/");
1738 p = isl_printer_print_isl_int(p, cst->d);
1740 return p;
1741 error:
1742 isl_printer_free(p);
1743 return NULL;
1746 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1747 __isl_keep isl_space *space, __isl_keep isl_mat *div, int var)
1749 unsigned total;
1751 total = isl_space_dim(space, isl_dim_all);
1752 if (var < total)
1753 p = print_term(space, NULL, space->ctx->one, 1 + var, p, 0);
1754 else
1755 p = print_div(space, div, var - total, p);
1756 return p;
1759 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1760 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var, int exp)
1762 p = print_base(p, dim, div, var);
1763 if (exp == 1)
1764 return p;
1765 if (p->output_format == ISL_FORMAT_C) {
1766 int i;
1767 for (i = 1; i < exp; ++i) {
1768 p = isl_printer_print_str(p, "*");
1769 p = print_base(p, dim, div, var);
1771 } else {
1772 p = isl_printer_print_str(p, "^");
1773 p = isl_printer_print_int(p, exp);
1775 return p;
1778 /* Print the polynomial "poly" defined over the domain space "space" and
1779 * local variables defined by "div" to "p".
1781 static __isl_give isl_printer *poly_print(__isl_keep isl_poly *poly,
1782 __isl_keep isl_space *space, __isl_keep isl_mat *div,
1783 __isl_take isl_printer *p)
1785 int i, n, first, print_parens;
1786 isl_bool is_cst;
1787 isl_poly_rec *rec;
1789 is_cst = isl_poly_is_cst(poly);
1790 if (!p || is_cst < 0 || !space || !div)
1791 goto error;
1793 if (is_cst)
1794 return poly_print_cst(poly, p, 1);
1796 rec = isl_poly_as_rec(poly);
1797 n = poly_rec_n_non_zero(rec);
1798 if (n < 0)
1799 return isl_printer_free(p);
1800 print_parens = n > 1;
1801 if (print_parens)
1802 p = isl_printer_print_str(p, "(");
1803 for (i = 0, first = 1; i < rec->n; ++i) {
1804 isl_bool is_zero = isl_poly_is_zero(rec->p[i]);
1805 isl_bool is_one = isl_poly_is_one(rec->p[i]);
1806 isl_bool is_negone = isl_poly_is_negone(rec->p[i]);
1807 isl_bool is_cst = isl_poly_is_cst(rec->p[i]);
1809 if (is_zero < 0 || is_one < 0 || is_negone < 0)
1810 return isl_printer_free(p);
1811 if (is_zero)
1812 continue;
1813 if (is_negone) {
1814 if (!i)
1815 p = isl_printer_print_str(p, "-1");
1816 else if (first)
1817 p = isl_printer_print_str(p, "-");
1818 else
1819 p = isl_printer_print_str(p, " - ");
1820 } else if (is_cst && !is_one)
1821 p = poly_print_cst(rec->p[i], p, first);
1822 else {
1823 if (!first)
1824 p = isl_printer_print_str(p, " + ");
1825 if (i == 0 || !is_one)
1826 p = poly_print(rec->p[i], space, div, p);
1828 first = 0;
1829 if (i == 0)
1830 continue;
1831 if (!is_one && !is_negone)
1832 p = isl_printer_print_str(p, " * ");
1833 p = print_pow(p, space, div, rec->poly.var, i);
1835 if (print_parens)
1836 p = isl_printer_print_str(p, ")");
1837 return p;
1838 error:
1839 isl_printer_free(p);
1840 return NULL;
1843 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1844 __isl_keep isl_qpolynomial *qp)
1846 if (!p || !qp)
1847 goto error;
1848 p = poly_print(qp->poly, qp->dim, qp->div, p);
1849 return p;
1850 error:
1851 isl_printer_free(p);
1852 return NULL;
1855 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1856 __isl_keep isl_qpolynomial *qp)
1858 struct isl_print_space_data data = { 0 };
1860 if (!p || !qp)
1861 goto error;
1863 p = print_param_tuple(p, qp->dim, &data);
1864 p = isl_printer_print_str(p, "{ ");
1865 if (!isl_space_is_params(qp->dim)) {
1866 p = isl_print_space(qp->dim, p, 0, &data);
1867 p = isl_printer_print_str(p, " -> ");
1869 p = print_qpolynomial(p, qp);
1870 p = isl_printer_print_str(p, " }");
1871 return p;
1872 error:
1873 isl_printer_free(p);
1874 return NULL;
1877 /* Print the quasi-polynomial "qp" to "p" in C format, with the variable names
1878 * taken from the domain space "space".
1880 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1881 __isl_keep isl_space *space, __isl_keep isl_qpolynomial *qp)
1883 isl_bool is_one;
1884 isl_val *den;
1886 den = isl_qpolynomial_get_den(qp);
1887 qp = isl_qpolynomial_copy(qp);
1888 qp = isl_qpolynomial_scale_val(qp, isl_val_copy(den));
1889 is_one = isl_val_is_one(den);
1890 if (is_one < 0)
1891 p = isl_printer_free(p);
1892 if (!is_one)
1893 p = isl_printer_print_str(p, "(");
1894 if (qp)
1895 p = poly_print(qp->poly, space, qp->div, p);
1896 else
1897 p = isl_printer_free(p);
1898 if (!is_one) {
1899 p = isl_printer_print_str(p, ")/");
1900 p = isl_printer_print_val(p, den);
1902 isl_qpolynomial_free(qp);
1903 isl_val_free(den);
1904 return p;
1907 __isl_give isl_printer *isl_printer_print_qpolynomial(
1908 __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1910 if (!p || !qp)
1911 goto error;
1913 if (p->output_format == ISL_FORMAT_ISL)
1914 return print_qpolynomial_isl(p, qp);
1915 else if (p->output_format == ISL_FORMAT_C)
1916 return print_qpolynomial_c(p, qp->dim, qp);
1917 else
1918 isl_die(qp->dim->ctx, isl_error_unsupported,
1919 "output format not supported for isl_qpolynomials",
1920 goto error);
1921 error:
1922 isl_printer_free(p);
1923 return NULL;
1926 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1927 unsigned output_format)
1929 isl_printer *p;
1931 if (!qp)
1932 return;
1934 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1935 p = isl_printer_to_file(qp->dim->ctx, out);
1936 p = isl_printer_print_qpolynomial(p, qp);
1937 isl_printer_free(p);
1940 static __isl_give isl_printer *qpolynomial_fold_print(
1941 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1943 int i;
1945 if (fold->type == isl_fold_min)
1946 p = isl_printer_print_str(p, "min");
1947 else if (fold->type == isl_fold_max)
1948 p = isl_printer_print_str(p, "max");
1949 p = isl_printer_print_str(p, "(");
1950 for (i = 0; i < fold->n; ++i) {
1951 if (i)
1952 p = isl_printer_print_str(p, ", ");
1953 p = print_qpolynomial(p, fold->qp[i]);
1955 p = isl_printer_print_str(p, ")");
1956 return p;
1959 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1960 FILE *out, unsigned output_format)
1962 isl_printer *p;
1964 if (!fold)
1965 return;
1967 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1969 p = isl_printer_to_file(fold->dim->ctx, out);
1970 p = isl_printer_print_qpolynomial_fold(p, fold);
1972 isl_printer_free(p);
1975 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1976 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1978 struct isl_print_space_data data = { 0 };
1979 int i = 0;
1981 for (i = 0; i < pwqp->n; ++i) {
1982 isl_space *space;
1984 if (i)
1985 p = isl_printer_print_str(p, "; ");
1986 space = isl_qpolynomial_get_domain_space(pwqp->p[i].qp);
1987 if (!isl_space_is_params(space)) {
1988 p = isl_print_space(space, p, 0, &data);
1989 p = isl_printer_print_str(p, " -> ");
1991 p = print_qpolynomial(p, pwqp->p[i].qp);
1992 p = print_disjuncts(set_to_map(pwqp->p[i].set), space, p, 0);
1993 isl_space_free(space);
1996 return p;
1999 static __isl_give isl_printer *print_pw_qpolynomial_isl(
2000 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2002 struct isl_print_space_data data = { 0 };
2004 if (!p || !pwqp)
2005 goto error;
2007 p = print_param_tuple(p, pwqp->dim, &data);
2008 p = isl_printer_print_str(p, "{ ");
2009 if (pwqp->n == 0) {
2010 if (!isl_space_is_set(pwqp->dim)) {
2011 p = print_tuple(pwqp->dim, p, isl_dim_in, &data);
2012 p = isl_printer_print_str(p, " -> ");
2014 p = isl_printer_print_str(p, "0");
2016 p = isl_pwqp_print_isl_body(p, pwqp);
2017 p = isl_printer_print_str(p, " }");
2018 return p;
2019 error:
2020 isl_printer_free(p);
2021 return NULL;
2024 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
2025 unsigned output_format)
2027 isl_printer *p;
2029 if (!pwqp)
2030 return;
2032 p = isl_printer_to_file(pwqp->dim->ctx, out);
2033 p = isl_printer_set_output_format(p, output_format);
2034 p = isl_printer_print_pw_qpolynomial(p, pwqp);
2036 isl_printer_free(p);
2039 static __isl_give isl_printer *isl_pwf_print_isl_body(
2040 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2042 struct isl_print_space_data data = { 0 };
2043 int i = 0;
2045 for (i = 0; i < pwf->n; ++i) {
2046 isl_space *space;
2048 if (i)
2049 p = isl_printer_print_str(p, "; ");
2050 space = isl_qpolynomial_fold_get_domain_space(pwf->p[i].fold);
2051 if (!isl_space_is_params(space)) {
2052 p = isl_print_space(space, p, 0, &data);
2053 p = isl_printer_print_str(p, " -> ");
2055 p = qpolynomial_fold_print(pwf->p[i].fold, p);
2056 p = print_disjuncts(set_to_map(pwf->p[i].set), space, p, 0);
2057 isl_space_free(space);
2060 return p;
2063 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
2064 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2066 struct isl_print_space_data data = { 0 };
2068 p = print_param_tuple(p, pwf->dim, &data);
2069 p = isl_printer_print_str(p, "{ ");
2070 if (pwf->n == 0) {
2071 if (!isl_space_is_set(pwf->dim)) {
2072 p = print_tuple(pwf->dim, p, isl_dim_in, &data);
2073 p = isl_printer_print_str(p, " -> ");
2075 p = isl_printer_print_str(p, "0");
2077 p = isl_pwf_print_isl_body(p, pwf);
2078 p = isl_printer_print_str(p, " }");
2079 return p;
2082 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2083 __isl_keep isl_local_space *ls, isl_int *c);
2085 /* We skip the constraint if it is implied by the div expression.
2087 * *first indicates whether this is the first constraint in the conjunction and
2088 * is updated if the constraint is actually printed.
2090 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
2091 __isl_keep isl_local_space *ls, isl_int *c, const char *op, int *first)
2093 unsigned o_div;
2094 unsigned n_div;
2095 int div;
2097 o_div = isl_local_space_offset(ls, isl_dim_div);
2098 n_div = isl_local_space_dim(ls, isl_dim_div);
2099 div = isl_seq_last_non_zero(c + o_div, n_div);
2100 if (div >= 0) {
2101 isl_bool is_div = isl_local_space_is_div_constraint(ls, c, div);
2102 if (is_div < 0)
2103 return isl_printer_free(p);
2104 if (is_div)
2105 return p;
2108 if (!*first)
2109 p = isl_printer_print_str(p, " && ");
2111 p = print_ls_affine_c(p, ls, c);
2112 p = isl_printer_print_str(p, " ");
2113 p = isl_printer_print_str(p, op);
2114 p = isl_printer_print_str(p, " 0");
2116 *first = 0;
2118 return p;
2121 static __isl_give isl_printer *print_ls_partial_affine_c(
2122 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2123 isl_int *c, unsigned len);
2125 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
2126 __isl_keep isl_space *space, __isl_keep isl_basic_set *bset)
2128 int i, j;
2129 int first = 1;
2130 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
2131 unsigned total = isl_basic_set_total_dim(bset) - n_div;
2132 isl_mat *div;
2133 isl_local_space *ls;
2135 div = isl_basic_set_get_divs(bset);
2136 ls = isl_local_space_alloc_div(isl_space_copy(space), div);
2137 for (i = 0; i < bset->n_eq; ++i) {
2138 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
2139 if (j < 0)
2140 p = print_constraint_c(p, ls,
2141 bset->eq[i], "==", &first);
2142 else {
2143 if (i)
2144 p = isl_printer_print_str(p, " && ");
2145 p = isl_printer_print_str(p, "(");
2146 p = print_ls_partial_affine_c(p, ls, bset->eq[i],
2147 1 + total + j);
2148 p = isl_printer_print_str(p, ") % ");
2149 p = isl_printer_print_isl_int(p,
2150 bset->eq[i][1 + total + j]);
2151 p = isl_printer_print_str(p, " == 0");
2152 first = 0;
2155 for (i = 0; i < bset->n_ineq; ++i)
2156 p = print_constraint_c(p, ls, bset->ineq[i], ">=", &first);
2157 isl_local_space_free(ls);
2158 return p;
2161 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
2162 __isl_keep isl_space *space, __isl_keep isl_set *set)
2164 int i;
2166 if (!set)
2167 return isl_printer_free(p);
2169 if (set->n == 0)
2170 p = isl_printer_print_str(p, "0");
2172 for (i = 0; i < set->n; ++i) {
2173 if (i)
2174 p = isl_printer_print_str(p, " || ");
2175 if (set->n > 1)
2176 p = isl_printer_print_str(p, "(");
2177 p = print_basic_set_c(p, space, set->p[i]);
2178 if (set->n > 1)
2179 p = isl_printer_print_str(p, ")");
2181 return p;
2184 /* Print the piecewise quasi-polynomial "pwqp" to "p" in C format.
2186 static __isl_give isl_printer *print_pw_qpolynomial_c(
2187 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2189 int i;
2190 isl_space *space;
2192 space = isl_pw_qpolynomial_get_domain_space(pwqp);
2193 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set)) {
2194 p = print_qpolynomial_c(p, space, pwqp->p[0].qp);
2195 isl_space_free(space);
2196 return p;
2199 for (i = 0; i < pwqp->n; ++i) {
2200 p = isl_printer_print_str(p, "(");
2201 p = print_set_c(p, space, pwqp->p[i].set);
2202 p = isl_printer_print_str(p, ") ? (");
2203 p = print_qpolynomial_c(p, space, pwqp->p[i].qp);
2204 p = isl_printer_print_str(p, ") : ");
2207 isl_space_free(space);
2208 p = isl_printer_print_str(p, "0");
2209 return p;
2212 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
2213 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2215 if (!p || !pwqp)
2216 goto error;
2218 if (p->output_format == ISL_FORMAT_ISL)
2219 return print_pw_qpolynomial_isl(p, pwqp);
2220 else if (p->output_format == ISL_FORMAT_C)
2221 return print_pw_qpolynomial_c(p, pwqp);
2222 isl_assert(p->ctx, 0, goto error);
2223 error:
2224 isl_printer_free(p);
2225 return NULL;
2228 static isl_stat print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
2230 struct isl_union_print_data *data;
2231 data = (struct isl_union_print_data *)user;
2233 if (!data->first)
2234 data->p = isl_printer_print_str(data->p, "; ");
2235 data->first = 0;
2237 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
2238 isl_pw_qpolynomial_free(pwqp);
2240 return isl_stat_ok;
2243 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
2244 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2246 struct isl_union_print_data data;
2247 struct isl_print_space_data space_data = { 0 };
2248 isl_space *space;
2250 space = isl_union_pw_qpolynomial_get_space(upwqp);
2251 p = print_param_tuple(p, space, &space_data);
2252 isl_space_free(space);
2253 p = isl_printer_print_str(p, "{ ");
2254 data.p = p;
2255 data.first = 1;
2256 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
2257 &data);
2258 p = data.p;
2259 p = isl_printer_print_str(p, " }");
2260 return p;
2263 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
2264 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2266 if (!p || !upwqp)
2267 goto error;
2269 if (p->output_format == ISL_FORMAT_ISL)
2270 return print_union_pw_qpolynomial_isl(p, upwqp);
2271 isl_die(p->ctx, isl_error_invalid,
2272 "invalid output format for isl_union_pw_qpolynomial",
2273 goto error);
2274 error:
2275 isl_printer_free(p);
2276 return NULL;
2279 /* Print the quasi-polynomial reduction "fold" to "p" in C format,
2280 * with the variable names taken from the domain space "space".
2282 static __isl_give isl_printer *print_qpolynomial_fold_c(
2283 __isl_take isl_printer *p, __isl_keep isl_space *space,
2284 __isl_keep isl_qpolynomial_fold *fold)
2286 int i;
2288 for (i = 0; i < fold->n - 1; ++i)
2289 if (fold->type == isl_fold_min)
2290 p = isl_printer_print_str(p, "min(");
2291 else if (fold->type == isl_fold_max)
2292 p = isl_printer_print_str(p, "max(");
2294 for (i = 0; i < fold->n; ++i) {
2295 if (i)
2296 p = isl_printer_print_str(p, ", ");
2297 p = print_qpolynomial_c(p, space, fold->qp[i]);
2298 if (i)
2299 p = isl_printer_print_str(p, ")");
2301 return p;
2304 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
2305 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
2307 if (!p || !fold)
2308 goto error;
2309 if (p->output_format == ISL_FORMAT_ISL)
2310 return qpolynomial_fold_print(fold, p);
2311 else if (p->output_format == ISL_FORMAT_C)
2312 return print_qpolynomial_fold_c(p, fold->dim, fold);
2313 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2314 goto error);
2315 error:
2316 isl_printer_free(p);
2317 return NULL;
2320 /* Print the piecewise quasi-polynomial reduction "pwf" to "p" in C format.
2322 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
2323 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2325 int i;
2326 isl_space *space;
2328 space = isl_pw_qpolynomial_fold_get_domain_space(pwf);
2329 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set)) {
2330 p = print_qpolynomial_fold_c(p, space, pwf->p[0].fold);
2331 isl_space_free(space);
2332 return p;
2335 for (i = 0; i < pwf->n; ++i) {
2336 p = isl_printer_print_str(p, "(");
2337 p = print_set_c(p, space, pwf->p[i].set);
2338 p = isl_printer_print_str(p, ") ? (");
2339 p = print_qpolynomial_fold_c(p, space, pwf->p[i].fold);
2340 p = isl_printer_print_str(p, ") : ");
2343 isl_space_free(space);
2344 p = isl_printer_print_str(p, "0");
2345 return p;
2348 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
2349 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2351 if (!p || !pwf)
2352 goto error;
2354 if (p->output_format == ISL_FORMAT_ISL)
2355 return print_pw_qpolynomial_fold_isl(p, pwf);
2356 else if (p->output_format == ISL_FORMAT_C)
2357 return print_pw_qpolynomial_fold_c(p, pwf);
2358 isl_assert(p->ctx, 0, goto error);
2359 error:
2360 isl_printer_free(p);
2361 return NULL;
2364 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
2365 FILE *out, unsigned output_format)
2367 isl_printer *p;
2369 if (!pwf)
2370 return;
2372 p = isl_printer_to_file(pwf->dim->ctx, out);
2373 p = isl_printer_set_output_format(p, output_format);
2374 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
2376 isl_printer_free(p);
2379 static isl_stat print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf,
2380 void *user)
2382 struct isl_union_print_data *data;
2383 data = (struct isl_union_print_data *)user;
2385 if (!data->first)
2386 data->p = isl_printer_print_str(data->p, "; ");
2387 data->first = 0;
2389 data->p = isl_pwf_print_isl_body(data->p, pwf);
2390 isl_pw_qpolynomial_fold_free(pwf);
2392 return isl_stat_ok;
2395 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
2396 __isl_take isl_printer *p,
2397 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2399 struct isl_union_print_data data;
2400 struct isl_print_space_data space_data = { 0 };
2401 isl_space *space;
2403 space = isl_union_pw_qpolynomial_fold_get_space(upwf);
2404 p = print_param_tuple(p, space, &space_data);
2405 isl_space_free(space);
2406 p = isl_printer_print_str(p, "{ ");
2407 data.p = p;
2408 data.first = 1;
2409 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
2410 &print_pwf_body, &data);
2411 p = data.p;
2412 p = isl_printer_print_str(p, " }");
2413 return p;
2416 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2417 __isl_take isl_printer *p,
2418 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2420 if (!p || !upwf)
2421 goto error;
2423 if (p->output_format == ISL_FORMAT_ISL)
2424 return print_union_pw_qpolynomial_fold_isl(p, upwf);
2425 isl_die(p->ctx, isl_error_invalid,
2426 "invalid output format for isl_union_pw_qpolynomial_fold",
2427 goto error);
2428 error:
2429 isl_printer_free(p);
2430 return NULL;
2433 /* Print the isl_constraint "c" to "p".
2435 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
2436 __isl_keep isl_constraint *c)
2438 struct isl_print_space_data data = { 0 };
2439 isl_local_space *ls;
2440 isl_space *space;
2441 isl_bool exists;
2443 if (!p || !c)
2444 goto error;
2446 ls = isl_constraint_get_local_space(c);
2447 if (!ls)
2448 return isl_printer_free(p);
2449 space = isl_local_space_get_space(ls);
2450 p = print_param_tuple(p, space, &data);
2451 p = isl_printer_print_str(p, "{ ");
2452 p = isl_print_space(space, p, 0, &data);
2453 p = isl_printer_print_str(p, " : ");
2454 exists = need_exists(p, ls->div);
2455 if (exists < 0)
2456 p = isl_printer_free(p);
2457 if (exists >= 0 && exists)
2458 p = open_exists(p, space, ls->div, 0);
2459 p = print_affine_of_len(space, ls->div, p, c->v->el, c->v->size);
2460 if (isl_constraint_is_equality(c))
2461 p = isl_printer_print_str(p, " = 0");
2462 else
2463 p = isl_printer_print_str(p, " >= 0");
2464 if (exists >= 0 && exists)
2465 p = isl_printer_print_str(p, s_close_exists[0]);
2466 p = isl_printer_print_str(p, " }");
2467 isl_space_free(space);
2468 isl_local_space_free(ls);
2470 return p;
2471 error:
2472 isl_printer_free(p);
2473 return NULL;
2476 static __isl_give isl_printer *isl_printer_print_space_isl(
2477 __isl_take isl_printer *p, __isl_keep isl_space *space)
2479 struct isl_print_space_data data = { 0 };
2481 if (!space)
2482 goto error;
2484 p = print_param_tuple(p, space, &data);
2486 p = isl_printer_print_str(p, "{ ");
2487 if (isl_space_is_params(space))
2488 p = isl_printer_print_str(p, s_such_that[0]);
2489 else
2490 p = isl_print_space(space, p, 0, &data);
2491 p = isl_printer_print_str(p, " }");
2493 return p;
2494 error:
2495 isl_printer_free(p);
2496 return NULL;
2499 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
2500 __isl_keep isl_space *space)
2502 if (!p || !space)
2503 return isl_printer_free(p);
2504 if (p->output_format == ISL_FORMAT_ISL)
2505 return isl_printer_print_space_isl(p, space);
2506 else if (p->output_format == ISL_FORMAT_OMEGA)
2507 return print_omega_parameters(space, p);
2509 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
2510 "output format not supported for space",
2511 return isl_printer_free(p));
2514 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
2515 __isl_keep isl_local_space *ls)
2517 struct isl_print_space_data data = { 0 };
2518 unsigned n_div;
2520 if (!ls)
2521 goto error;
2523 p = print_param_tuple(p, ls->dim, &data);
2524 p = isl_printer_print_str(p, "{ ");
2525 p = isl_print_space(ls->dim, p, 0, &data);
2526 n_div = isl_local_space_dim(ls, isl_dim_div);
2527 if (n_div > 0) {
2528 p = isl_printer_print_str(p, " : ");
2529 p = isl_printer_print_str(p, s_open_exists[0]);
2530 p = print_div_list(p, ls->dim, ls->div, 0, 1);
2531 p = isl_printer_print_str(p, s_close_exists[0]);
2532 } else if (isl_space_is_params(ls->dim))
2533 p = isl_printer_print_str(p, s_such_that[0]);
2534 p = isl_printer_print_str(p, " }");
2535 return p;
2536 error:
2537 isl_printer_free(p);
2538 return NULL;
2541 /* Print the (potentially rational) affine expression "aff" to "p",
2542 * with the variable names taken from "space".
2544 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2545 __isl_keep isl_space *space, __isl_keep isl_aff *aff)
2547 unsigned total;
2549 if (isl_aff_is_nan(aff))
2550 return isl_printer_print_str(p, "NaN");
2552 total = isl_local_space_dim(aff->ls, isl_dim_all);
2553 p = isl_printer_print_str(p, "(");
2554 p = print_affine_of_len(space, aff->ls->div, p,
2555 aff->v->el + 1, 1 + total);
2556 if (isl_int_is_one(aff->v->el[0]))
2557 p = isl_printer_print_str(p, ")");
2558 else {
2559 p = isl_printer_print_str(p, ")/");
2560 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2563 return p;
2566 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2567 __isl_keep isl_aff *aff)
2569 struct isl_print_space_data data = { 0 };
2571 if (isl_space_is_params(aff->ls->dim))
2573 else {
2574 p = print_tuple(aff->ls->dim, p, isl_dim_set, &data);
2575 p = isl_printer_print_str(p, " -> ");
2577 p = isl_printer_print_str(p, "[");
2578 p = print_aff_body(p, aff->ls->dim, aff);
2579 p = isl_printer_print_str(p, "]");
2581 return p;
2584 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2585 __isl_keep isl_aff *aff)
2587 struct isl_print_space_data data = { 0 };
2589 if (!aff)
2590 goto error;
2592 p = print_param_tuple(p, aff->ls->dim, &data);
2593 p = isl_printer_print_str(p, "{ ");
2594 p = print_aff(p, aff);
2595 p = isl_printer_print_str(p, " }");
2596 return p;
2597 error:
2598 isl_printer_free(p);
2599 return NULL;
2602 /* Print the body of an isl_pw_aff, i.e., a semicolon delimited
2603 * sequence of affine expressions, each followed by constraints.
2605 static __isl_give isl_printer *print_pw_aff_body(
2606 __isl_take isl_printer *p, __isl_keep isl_pw_aff *pa)
2608 int i;
2610 if (!pa)
2611 return isl_printer_free(p);
2613 for (i = 0; i < pa->n; ++i) {
2614 isl_space *space;
2616 if (i)
2617 p = isl_printer_print_str(p, "; ");
2618 p = print_aff(p, pa->p[i].aff);
2619 space = isl_aff_get_domain_space(pa->p[i].aff);
2620 p = print_disjuncts(set_to_map(pa->p[i].set), space, p, 0);
2621 isl_space_free(space);
2624 return p;
2627 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2628 __isl_keep isl_pw_aff *pwaff)
2630 struct isl_print_space_data data = { 0 };
2632 if (!pwaff)
2633 goto error;
2635 p = print_param_tuple(p, pwaff->dim, &data);
2636 p = isl_printer_print_str(p, "{ ");
2637 p = print_pw_aff_body(p, pwaff);
2638 p = isl_printer_print_str(p, " }");
2639 return p;
2640 error:
2641 isl_printer_free(p);
2642 return NULL;
2645 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2646 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2648 if (type == isl_dim_div) {
2649 p = isl_printer_print_str(p, "floord(");
2650 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2651 p = isl_printer_print_str(p, ", ");
2652 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2653 p = isl_printer_print_str(p, ")");
2654 } else {
2655 const char *name;
2657 name = isl_space_get_dim_name(ls->dim, type, pos);
2658 if (!name)
2659 name = "UNNAMED";
2660 p = isl_printer_print_str(p, name);
2662 return p;
2665 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2666 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2668 enum isl_dim_type type;
2670 if (!p || !ls)
2671 return isl_printer_free(p);
2673 if (pos == 0)
2674 return isl_printer_print_isl_int(p, c);
2676 if (isl_int_is_one(c))
2678 else if (isl_int_is_negone(c))
2679 p = isl_printer_print_str(p, "-");
2680 else {
2681 p = isl_printer_print_isl_int(p, c);
2682 p = isl_printer_print_str(p, "*");
2684 if (pos2type(ls->dim, &type, &pos) < 0)
2685 return isl_printer_free(p);
2686 p = print_ls_name_c(p, ls, type, pos);
2687 return p;
2690 static __isl_give isl_printer *print_ls_partial_affine_c(
2691 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2692 isl_int *c, unsigned len)
2694 int i;
2695 int first;
2697 for (i = 0, first = 1; i < len; ++i) {
2698 int flip = 0;
2699 if (isl_int_is_zero(c[i]))
2700 continue;
2701 if (!first) {
2702 if (isl_int_is_neg(c[i])) {
2703 flip = 1;
2704 isl_int_neg(c[i], c[i]);
2705 p = isl_printer_print_str(p, " - ");
2706 } else
2707 p = isl_printer_print_str(p, " + ");
2709 first = 0;
2710 p = print_ls_term_c(p, ls, c[i], i);
2711 if (flip)
2712 isl_int_neg(c[i], c[i]);
2714 if (first)
2715 p = isl_printer_print_str(p, "0");
2716 return p;
2719 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2720 __isl_keep isl_local_space *ls, isl_int *c)
2722 unsigned len = 1 + isl_local_space_dim(ls, isl_dim_all);
2723 return print_ls_partial_affine_c(p, ls, c, len);
2726 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2727 __isl_keep isl_aff *aff)
2729 unsigned total;
2731 total = isl_local_space_dim(aff->ls, isl_dim_all);
2732 if (!isl_int_is_one(aff->v->el[0]))
2733 p = isl_printer_print_str(p, "(");
2734 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2735 if (!isl_int_is_one(aff->v->el[0])) {
2736 p = isl_printer_print_str(p, ")/");
2737 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2739 return p;
2742 /* In the C format, we cannot express that "pwaff" may be undefined
2743 * on parts of the domain space. We therefore assume that the expression
2744 * will only be evaluated on its definition domain and compute the gist
2745 * of each cell with respect to this domain.
2747 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2748 __isl_keep isl_pw_aff *pwaff)
2750 isl_set *domain;
2751 isl_ast_build *build;
2752 isl_ast_expr *expr;
2754 if (pwaff->n < 1)
2755 isl_die(p->ctx, isl_error_unsupported,
2756 "cannot print empty isl_pw_aff in C format",
2757 return isl_printer_free(p));
2759 domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
2760 build = isl_ast_build_from_context(domain);
2761 expr = isl_ast_build_expr_from_pw_aff(build, isl_pw_aff_copy(pwaff));
2762 p = isl_printer_print_ast_expr(p, expr);
2763 isl_ast_expr_free(expr);
2764 isl_ast_build_free(build);
2766 return p;
2769 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2770 __isl_keep isl_aff *aff)
2772 if (!p || !aff)
2773 goto error;
2775 if (p->output_format == ISL_FORMAT_ISL)
2776 return print_aff_isl(p, aff);
2777 else if (p->output_format == ISL_FORMAT_C)
2778 return print_aff_c(p, aff);
2779 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2780 goto error);
2781 error:
2782 isl_printer_free(p);
2783 return NULL;
2786 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2787 __isl_keep isl_pw_aff *pwaff)
2789 if (!p || !pwaff)
2790 goto error;
2792 if (p->output_format == ISL_FORMAT_ISL)
2793 return print_pw_aff_isl(p, pwaff);
2794 else if (p->output_format == ISL_FORMAT_C)
2795 return print_pw_aff_c(p, pwaff);
2796 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2797 goto error);
2798 error:
2799 isl_printer_free(p);
2800 return NULL;
2803 /* Print "pa" in a sequence of isl_pw_affs delimited by semicolons.
2804 * Each isl_pw_aff itself is also printed as semicolon delimited
2805 * sequence of pieces.
2806 * If data->first = 1, then this is the first in the sequence.
2807 * Update data->first to tell the next element that it is not the first.
2809 static isl_stat print_pw_aff_body_wrap(__isl_take isl_pw_aff *pa,
2810 void *user)
2812 struct isl_union_print_data *data;
2813 data = (struct isl_union_print_data *) user;
2815 if (!data->first)
2816 data->p = isl_printer_print_str(data->p, "; ");
2817 data->first = 0;
2819 data->p = print_pw_aff_body(data->p, pa);
2820 isl_pw_aff_free(pa);
2822 return data->p ? isl_stat_ok : isl_stat_error;
2825 /* Print the body of an isl_union_pw_aff, i.e., a semicolon delimited
2826 * sequence of affine expressions, each followed by constraints,
2827 * with the sequence enclosed in braces.
2829 static __isl_give isl_printer *print_union_pw_aff_body(
2830 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2832 struct isl_union_print_data data = { p, 1 };
2834 p = isl_printer_print_str(p, s_open_set[0]);
2835 data.p = p;
2836 if (isl_union_pw_aff_foreach_pw_aff(upa,
2837 &print_pw_aff_body_wrap, &data) < 0)
2838 data.p = isl_printer_free(p);
2839 p = data.p;
2840 p = isl_printer_print_str(p, s_close_set[0]);
2842 return p;
2845 /* Print the isl_union_pw_aff "upa" to "p" in isl format.
2847 * The individual isl_pw_affs are delimited by a semicolon.
2849 static __isl_give isl_printer *print_union_pw_aff_isl(
2850 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2852 struct isl_print_space_data data = { 0 };
2853 isl_space *space;
2855 space = isl_union_pw_aff_get_space(upa);
2856 p = print_param_tuple(p, space, &data);
2857 isl_space_free(space);
2858 p = print_union_pw_aff_body(p, upa);
2859 return p;
2862 /* Print the isl_union_pw_aff "upa" to "p".
2864 * We currently only support an isl format.
2866 __isl_give isl_printer *isl_printer_print_union_pw_aff(
2867 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2869 if (!p || !upa)
2870 return isl_printer_free(p);
2872 if (p->output_format == ISL_FORMAT_ISL)
2873 return print_union_pw_aff_isl(p, upa);
2874 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2875 "unsupported output format", return isl_printer_free(p));
2878 /* Print dimension "pos" of data->space to "p".
2880 * data->user is assumed to be an isl_multi_aff.
2882 * If the current dimension is an output dimension, then print
2883 * the corresponding expression. Otherwise, print the name of the dimension.
2885 static __isl_give isl_printer *print_dim_ma(__isl_take isl_printer *p,
2886 struct isl_print_space_data *data, unsigned pos)
2888 isl_multi_aff *ma = data->user;
2890 if (data->type == isl_dim_out) {
2891 isl_space *space;
2893 space = isl_multi_aff_get_domain_space(ma);
2894 p = print_aff_body(p, space, ma->u.p[pos]);
2895 isl_space_free(space);
2896 } else {
2897 p = print_name(data->space, p, data->type, pos, data->latex);
2900 return p;
2903 static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
2904 __isl_keep isl_multi_aff *maff)
2906 struct isl_print_space_data data = { 0 };
2908 data.print_dim = &print_dim_ma;
2909 data.user = maff;
2910 return isl_print_space(maff->space, p, 0, &data);
2913 static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
2914 __isl_keep isl_multi_aff *maff)
2916 struct isl_print_space_data data = { 0 };
2918 if (!maff)
2919 goto error;
2921 p = print_param_tuple(p, maff->space, &data);
2922 p = isl_printer_print_str(p, "{ ");
2923 p = print_multi_aff(p, maff);
2924 p = isl_printer_print_str(p, " }");
2925 return p;
2926 error:
2927 isl_printer_free(p);
2928 return NULL;
2931 __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
2932 __isl_keep isl_multi_aff *maff)
2934 if (!p || !maff)
2935 goto error;
2937 if (p->output_format == ISL_FORMAT_ISL)
2938 return print_multi_aff_isl(p, maff);
2939 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2940 goto error);
2941 error:
2942 isl_printer_free(p);
2943 return NULL;
2946 static __isl_give isl_printer *print_pw_multi_aff_body(
2947 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2949 int i;
2951 if (!pma)
2952 goto error;
2954 for (i = 0; i < pma->n; ++i) {
2955 isl_space *space;
2957 if (i)
2958 p = isl_printer_print_str(p, "; ");
2959 p = print_multi_aff(p, pma->p[i].maff);
2960 space = isl_multi_aff_get_domain_space(pma->p[i].maff);
2961 p = print_disjuncts(set_to_map(pma->p[i].set), space, p, 0);
2962 isl_space_free(space);
2964 return p;
2965 error:
2966 isl_printer_free(p);
2967 return NULL;
2970 static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
2971 __isl_keep isl_pw_multi_aff *pma)
2973 struct isl_print_space_data data = { 0 };
2975 if (!pma)
2976 goto error;
2978 p = print_param_tuple(p, pma->dim, &data);
2979 p = isl_printer_print_str(p, "{ ");
2980 p = print_pw_multi_aff_body(p, pma);
2981 p = isl_printer_print_str(p, " }");
2982 return p;
2983 error:
2984 isl_printer_free(p);
2985 return NULL;
2988 /* Print the unnamed, single-dimensional piecewise multi affine expression "pma"
2989 * to "p".
2991 static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
2992 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2994 int i;
2995 isl_space *space;
2997 space = isl_pw_multi_aff_get_domain_space(pma);
2998 for (i = 0; i < pma->n - 1; ++i) {
2999 p = isl_printer_print_str(p, "(");
3000 p = print_set_c(p, space, pma->p[i].set);
3001 p = isl_printer_print_str(p, ") ? (");
3002 p = print_aff_c(p, pma->p[i].maff->u.p[0]);
3003 p = isl_printer_print_str(p, ") : ");
3005 isl_space_free(space);
3007 return print_aff_c(p, pma->p[pma->n - 1].maff->u.p[0]);
3010 static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
3011 __isl_keep isl_pw_multi_aff *pma)
3013 int n;
3014 const char *name;
3016 if (!pma)
3017 goto error;
3018 if (pma->n < 1)
3019 isl_die(p->ctx, isl_error_unsupported,
3020 "cannot print empty isl_pw_multi_aff in C format",
3021 goto error);
3022 name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
3023 if (!name && isl_pw_multi_aff_dim(pma, isl_dim_out) == 1)
3024 return print_unnamed_pw_multi_aff_c(p, pma);
3025 if (!name)
3026 isl_die(p->ctx, isl_error_unsupported,
3027 "cannot print unnamed isl_pw_multi_aff in C format",
3028 goto error);
3030 p = isl_printer_print_str(p, name);
3031 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
3032 if (n != 0)
3033 isl_die(p->ctx, isl_error_unsupported,
3034 "not supported yet", goto error);
3036 return p;
3037 error:
3038 isl_printer_free(p);
3039 return NULL;
3042 __isl_give isl_printer *isl_printer_print_pw_multi_aff(
3043 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3045 if (!p || !pma)
3046 goto error;
3048 if (p->output_format == ISL_FORMAT_ISL)
3049 return print_pw_multi_aff_isl(p, pma);
3050 if (p->output_format == ISL_FORMAT_C)
3051 return print_pw_multi_aff_c(p, pma);
3052 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3053 goto error);
3054 error:
3055 isl_printer_free(p);
3056 return NULL;
3059 static isl_stat print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
3060 void *user)
3062 struct isl_union_print_data *data;
3063 data = (struct isl_union_print_data *) user;
3065 if (!data->first)
3066 data->p = isl_printer_print_str(data->p, "; ");
3067 data->first = 0;
3069 data->p = print_pw_multi_aff_body(data->p, pma);
3070 isl_pw_multi_aff_free(pma);
3072 return isl_stat_ok;
3075 static __isl_give isl_printer *print_union_pw_multi_aff_isl(
3076 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
3078 struct isl_union_print_data data;
3079 struct isl_print_space_data space_data = { 0 };
3080 isl_space *space;
3082 space = isl_union_pw_multi_aff_get_space(upma);
3083 p = print_param_tuple(p, space, &space_data);
3084 isl_space_free(space);
3085 p = isl_printer_print_str(p, s_open_set[0]);
3086 data.p = p;
3087 data.first = 1;
3088 isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3089 &print_pw_multi_aff_body_wrap, &data);
3090 p = data.p;
3091 p = isl_printer_print_str(p, s_close_set[0]);
3092 return p;
3095 __isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
3096 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
3098 if (!p || !upma)
3099 goto error;
3101 if (p->output_format == ISL_FORMAT_ISL)
3102 return print_union_pw_multi_aff_isl(p, upma);
3103 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3104 goto error);
3105 error:
3106 isl_printer_free(p);
3107 return NULL;
3110 /* Print dimension "pos" of data->space to "p".
3112 * data->user is assumed to be an isl_multi_pw_aff.
3114 * If the current dimension is an output dimension, then print
3115 * the corresponding piecewise affine expression.
3116 * Otherwise, print the name of the dimension.
3118 static __isl_give isl_printer *print_dim_mpa(__isl_take isl_printer *p,
3119 struct isl_print_space_data *data, unsigned pos)
3121 int i;
3122 int need_parens;
3123 isl_space *space;
3124 isl_multi_pw_aff *mpa = data->user;
3125 isl_pw_aff *pa;
3127 if (data->type != isl_dim_out)
3128 return print_name(data->space, p, data->type, pos, data->latex);
3130 pa = mpa->u.p[pos];
3131 if (pa->n == 0)
3132 return isl_printer_print_str(p, "(0 : false)");
3134 need_parens = pa->n != 1 || !isl_set_plain_is_universe(pa->p[0].set);
3135 if (need_parens)
3136 p = isl_printer_print_str(p, "(");
3137 space = isl_multi_pw_aff_get_domain_space(mpa);
3138 for (i = 0; i < pa->n; ++i) {
3140 if (i)
3141 p = isl_printer_print_str(p, "; ");
3142 p = print_aff_body(p, space, pa->p[i].aff);
3143 p = print_disjuncts(pa->p[i].set, space, p, 0);
3145 isl_space_free(space);
3146 if (need_parens)
3147 p = isl_printer_print_str(p, ")");
3149 return p;
3152 /* Print "mpa" to "p" in isl format.
3154 * If "mpa" is zero-dimensional and has a non-trivial explicit domain,
3155 * then it is printed after the tuple of affine expressions.
3157 static __isl_give isl_printer *print_multi_pw_aff_isl(__isl_take isl_printer *p,
3158 __isl_keep isl_multi_pw_aff *mpa)
3160 struct isl_print_space_data data = { 0 };
3161 isl_bool has_domain;
3163 if (!mpa)
3164 return isl_printer_free(p);
3166 p = print_param_tuple(p, mpa->space, &data);
3167 p = isl_printer_print_str(p, "{ ");
3168 data.print_dim = &print_dim_mpa;
3169 data.user = mpa;
3170 p = isl_print_space(mpa->space, p, 0, &data);
3171 has_domain = isl_multi_pw_aff_has_non_trivial_domain(mpa);
3172 if (has_domain < 0)
3173 return isl_printer_free(p);
3174 if (has_domain) {
3175 isl_space *space;
3177 space = isl_space_domain(isl_space_copy(mpa->space));
3178 p = print_disjuncts_set(mpa->u.dom, space, p, 0);
3179 isl_space_free(space);
3181 p = isl_printer_print_str(p, " }");
3182 return p;
3185 __isl_give isl_printer *isl_printer_print_multi_pw_aff(
3186 __isl_take isl_printer *p, __isl_keep isl_multi_pw_aff *mpa)
3188 if (!p || !mpa)
3189 return isl_printer_free(p);
3191 if (p->output_format == ISL_FORMAT_ISL)
3192 return print_multi_pw_aff_isl(p, mpa);
3193 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3194 return isl_printer_free(p));
3197 /* Print dimension "pos" of data->space to "p".
3199 * data->user is assumed to be an isl_multi_val.
3201 * If the current dimension is an output dimension, then print
3202 * the corresponding value. Otherwise, print the name of the dimension.
3204 static __isl_give isl_printer *print_dim_mv(__isl_take isl_printer *p,
3205 struct isl_print_space_data *data, unsigned pos)
3207 isl_multi_val *mv = data->user;
3209 if (data->type == isl_dim_out)
3210 return isl_printer_print_val(p, mv->u.p[pos]);
3211 else
3212 return print_name(data->space, p, data->type, pos, data->latex);
3215 /* Print the isl_multi_val "mv" to "p" in isl format.
3217 static __isl_give isl_printer *print_multi_val_isl(__isl_take isl_printer *p,
3218 __isl_keep isl_multi_val *mv)
3220 struct isl_print_space_data data = { 0 };
3222 if (!mv)
3223 return isl_printer_free(p);
3225 p = print_param_tuple(p, mv->space, &data);
3226 p = isl_printer_print_str(p, "{ ");
3227 data.print_dim = &print_dim_mv;
3228 data.user = mv;
3229 p = isl_print_space(mv->space, p, 0, &data);
3230 p = isl_printer_print_str(p, " }");
3231 return p;
3234 /* Print the isl_multi_val "mv" to "p".
3236 * Currently only supported in isl format.
3238 __isl_give isl_printer *isl_printer_print_multi_val(
3239 __isl_take isl_printer *p, __isl_keep isl_multi_val *mv)
3241 if (!p || !mv)
3242 return isl_printer_free(p);
3244 if (p->output_format == ISL_FORMAT_ISL)
3245 return print_multi_val_isl(p, mv);
3246 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3247 return isl_printer_free(p));
3250 /* Print dimension "pos" of data->space to "p".
3252 * data->user is assumed to be an isl_multi_union_pw_aff.
3254 * The current dimension is necessarily a set dimension, so
3255 * we print the corresponding isl_union_pw_aff, including
3256 * the braces.
3258 static __isl_give isl_printer *print_union_pw_aff_dim(__isl_take isl_printer *p,
3259 struct isl_print_space_data *data, unsigned pos)
3261 isl_multi_union_pw_aff *mupa = data->user;
3262 isl_union_pw_aff *upa;
3264 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, pos);
3265 p = print_union_pw_aff_body(p, upa);
3266 isl_union_pw_aff_free(upa);
3268 return p;
3271 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3273 * If "mupa" is zero-dimensional and has a non-trivial explicit domain,
3274 * then it is printed after the tuple of affine expressions.
3275 * In order to clarify that this domain belongs to the expression,
3276 * the tuple along with the domain are placed inside parentheses.
3277 * If "mupa" has any parameters, then the opening parenthesis
3278 * appears after the parameter declarations.
3280 static __isl_give isl_printer *print_multi_union_pw_aff_isl(
3281 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3283 struct isl_print_space_data data = { 0 };
3284 isl_bool has_domain;
3285 isl_space *space;
3287 if (!mupa)
3288 return isl_printer_free(p);
3289 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
3290 if (has_domain < 0)
3291 return isl_printer_free(p);
3293 space = isl_multi_union_pw_aff_get_space(mupa);
3294 p = print_param_tuple(p, space, &data);
3296 if (has_domain)
3297 p = isl_printer_print_str(p, "(");
3299 data.print_dim = &print_union_pw_aff_dim;
3300 data.user = mupa;
3302 p = isl_print_space(space, p, 0, &data);
3303 isl_space_free(space);
3305 if (has_domain) {
3306 p = isl_printer_print_str(p, " : ");
3307 p = isl_printer_print_union_set_isl_body(p, mupa->u.dom);
3308 p = isl_printer_print_str(p, ")");
3311 return p;
3314 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3316 * We currently only support an isl format.
3318 __isl_give isl_printer *isl_printer_print_multi_union_pw_aff(
3319 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3321 if (!p || !mupa)
3322 return isl_printer_free(p);
3324 if (p->output_format == ISL_FORMAT_ISL)
3325 return print_multi_union_pw_aff_isl(p, mupa);
3326 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
3327 "unsupported output format", return isl_printer_free(p));