isl_schedule_node.c: collect_filter_prefix*: return isl_stat
[isl.git] / isl_output.c
blob1d3431c468f265ef5792c9c5a6a724dfc7d921e6
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 enum isl_dim_type pos2type(__isl_keep isl_space *space, unsigned *pos)
222 enum isl_dim_type type;
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 type;
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 type = pos2type(space, &pos);
278 print_div_def = type == isl_dim_div && can_print_div_expr(p, div, pos);
280 if (isl_int_is_one(c))
282 else if (isl_int_is_negone(c))
283 p = isl_printer_print_str(p, "-");
284 else {
285 p = isl_printer_print_isl_int(p, c);
286 if (p->output_format == ISL_FORMAT_C || print_div_def)
287 p = isl_printer_print_str(p, "*");
289 if (print_div_def)
290 p = print_div(space, div, pos, p);
291 else
292 p = print_name(space, p, type, pos, latex);
293 return p;
296 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_space *dim,
297 __isl_keep isl_mat *div,
298 __isl_take isl_printer *p, isl_int *c, int len)
300 int i;
301 int first;
303 for (i = 0, first = 1; i < len; ++i) {
304 int flip = 0;
305 if (isl_int_is_zero(c[i]))
306 continue;
307 if (!first) {
308 if (isl_int_is_neg(c[i])) {
309 flip = 1;
310 isl_int_neg(c[i], c[i]);
311 p = isl_printer_print_str(p, " - ");
312 } else
313 p = isl_printer_print_str(p, " + ");
315 first = 0;
316 p = print_term(dim, div, c[i], i, p, 0);
317 if (flip)
318 isl_int_neg(c[i], c[i]);
320 if (first)
321 p = isl_printer_print_str(p, "0");
322 return p;
325 /* Print an affine expression "c"
326 * to "p", with the variable names taken from "space" and
327 * the integer division definitions taken from "div".
329 static __isl_give isl_printer *print_affine(__isl_take isl_printer *p,
330 __isl_keep isl_space *space, __isl_keep isl_mat *div, isl_int *c)
332 unsigned n_div;
333 unsigned len;
335 if (!space || !div)
336 return isl_printer_free(p);
337 n_div = isl_mat_rows(div);
338 len = 1 + isl_space_dim(space, isl_dim_all) + n_div;
339 return print_affine_of_len(space, div, p, c, len);
342 /* offset is the offset of local_space inside data->type of data->space.
344 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
345 __isl_keep isl_space *local_space, enum isl_dim_type local_type,
346 struct isl_print_space_data *data, int offset)
348 int i;
350 if (data->space != local_space && local_type == isl_dim_out)
351 offset += local_space->n_in;
353 for (i = 0; i < isl_space_dim(local_space, local_type); ++i) {
354 if (i)
355 p = isl_printer_print_str(p, ", ");
356 if (data->print_dim)
357 p = data->print_dim(p, data, offset + i);
358 else
359 p = print_name(data->space, p, data->type, offset + i,
360 data->latex);
362 return p;
365 static __isl_give isl_printer *print_var_list(__isl_take isl_printer *p,
366 __isl_keep isl_space *space, enum isl_dim_type type)
368 struct isl_print_space_data data = { .space = space, .type = type };
370 return print_nested_var_list(p, space, type, &data, 0);
373 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
374 __isl_keep isl_space *local_dim,
375 struct isl_print_space_data *data, int offset);
377 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
378 __isl_keep isl_space *local_space, enum isl_dim_type local_type,
379 struct isl_print_space_data *data, int offset)
381 const char *name = NULL;
382 unsigned n = isl_space_dim(local_space, local_type);
383 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
384 name = isl_space_get_tuple_name(local_space, local_type);
385 if (name) {
386 if (data->latex)
387 p = isl_printer_print_str(p, "\\mathrm{");
388 p = isl_printer_print_str(p, name);
389 if (data->latex)
390 p = isl_printer_print_str(p, "}");
393 if (!data->latex || n != 1 || name)
394 p = isl_printer_print_str(p, s_open_list[data->latex]);
395 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
396 local_space->nested[local_type - isl_dim_in]) {
397 if (data->space != local_space && local_type == isl_dim_out)
398 offset += local_space->n_in;
399 p = print_nested_map_dim(p,
400 local_space->nested[local_type - isl_dim_in],
401 data, offset);
402 } else
403 p = print_nested_var_list(p, local_space, local_type, data,
404 offset);
405 if (!data->latex || n != 1 || name)
406 p = isl_printer_print_str(p, s_close_list[data->latex]);
407 return p;
410 static __isl_give isl_printer *print_tuple(__isl_keep isl_space *dim,
411 __isl_take isl_printer *p, enum isl_dim_type type,
412 struct isl_print_space_data *data)
414 data->space = dim;
415 data->type = type;
416 return print_nested_tuple(p, dim, type, data, 0);
419 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
420 __isl_keep isl_space *local_dim,
421 struct isl_print_space_data *data, int offset)
423 p = print_nested_tuple(p, local_dim, isl_dim_in, data, offset);
424 p = isl_printer_print_str(p, s_to[data->latex]);
425 p = print_nested_tuple(p, local_dim, isl_dim_out, data, offset);
427 return p;
430 __isl_give isl_printer *isl_print_space(__isl_keep isl_space *space,
431 __isl_take isl_printer *p, int rational,
432 struct isl_print_space_data *data)
434 if (rational && !data->latex)
435 p = isl_printer_print_str(p, "rat: ");
436 if (isl_space_is_params(space))
438 else if (isl_space_is_set(space))
439 p = print_tuple(space, p, isl_dim_set, data);
440 else {
441 p = print_tuple(space, p, isl_dim_in, data);
442 p = isl_printer_print_str(p, s_to[data->latex]);
443 p = print_tuple(space, p, isl_dim_out, data);
446 return p;
449 static __isl_give isl_printer *print_omega_parameters(
450 __isl_keep isl_space *space, __isl_take isl_printer *p)
452 if (isl_space_dim(space, isl_dim_param) == 0)
453 return p;
455 p = isl_printer_start_line(p);
456 p = isl_printer_print_str(p, "symbolic ");
457 p = print_var_list(p, space, isl_dim_param);
458 p = isl_printer_print_str(p, ";");
459 p = isl_printer_end_line(p);
460 return p;
463 /* Does the inequality constraint following "i" in "bmap"
464 * have an opposite value for the same last coefficient?
465 * "last" is the position of the last coefficient of inequality "i".
466 * If the next constraint is a div constraint, then it is ignored
467 * since div constraints are not printed.
469 static isl_bool next_is_opposite(__isl_keep isl_basic_map *bmap, int i,
470 int last)
472 unsigned total = isl_basic_map_total_dim(bmap);
473 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
475 if (i + 1 >= bmap->n_ineq)
476 return isl_bool_false;
477 if (isl_seq_last_non_zero(bmap->ineq[i + 1], 1 + total) != last)
478 return isl_bool_false;
479 if (last >= o_div) {
480 isl_bool is_div;
481 is_div = isl_basic_map_is_div_constraint(bmap,
482 bmap->ineq[i + 1], last - o_div);
483 if (is_div < 0)
484 return isl_bool_error;
485 if (is_div)
486 return isl_bool_false;
488 return isl_int_abs_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]) &&
489 !isl_int_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]);
492 /* Return a string representation of the operator used when
493 * printing a constraint where the LHS is greater than or equal to the LHS
494 * (sign > 0) or smaller than or equal to the LHS (sign < 0).
495 * If "strict" is set, then return the strict version of the comparison
496 * operator.
498 static const char *constraint_op(int sign, int strict, int latex)
500 if (strict)
501 return sign < 0 ? "<" : ">";
502 if (sign < 0)
503 return s_le[latex];
504 else
505 return s_ge[latex];
508 /* Print one side of a constraint "c" to "p", with
509 * the variable names taken from "space" and the integer division definitions
510 * taken from "div".
511 * "last" is the position of the last non-zero coefficient.
512 * Let c' be the result of zeroing out this coefficient, then
513 * the partial constraint
515 * c' op
517 * is printed.
519 static __isl_give isl_printer *print_half_constraint(__isl_take isl_printer *p,
520 __isl_keep isl_space *space, __isl_keep isl_mat *div,
521 isl_int *c, int last, const char *op, int latex)
523 isl_int_set_si(c[last], 0);
524 p = print_affine(p, space, div, c);
526 p = isl_printer_print_str(p, " ");
527 p = isl_printer_print_str(p, op);
528 p = isl_printer_print_str(p, " ");
530 return p;
533 /* Print a constraint "c" to "p", with the variable names
534 * taken from "space" and the integer division definitions taken from "div".
535 * "last" is the position of the last non-zero coefficient, which is
536 * moreover assumed to be negative.
537 * Let c' be the result of zeroing out this coefficient, then
538 * the constraint is printed in the form
540 * -c[last] op c'
542 static __isl_give isl_printer *print_constraint(__isl_take isl_printer *p,
543 __isl_keep isl_space *space, __isl_keep isl_mat *div,
544 isl_int *c, int last, const char *op, int latex)
546 isl_int_abs(c[last], c[last]);
548 p = print_term(space, div, c[last], last, p, latex);
550 p = isl_printer_print_str(p, " ");
551 p = isl_printer_print_str(p, op);
552 p = isl_printer_print_str(p, " ");
554 isl_int_set_si(c[last], 0);
555 p = print_affine(p, space, div, c);
557 return p;
560 /* Given an integer division
562 * floor(f/m)
564 * at position "pos" in "div", print the corresponding modulo expression
566 * (f) mod m
568 * to "p". The variable names are taken from "space", while any
569 * nested integer division definitions are taken from "div".
571 static __isl_give isl_printer *print_mod(__isl_take isl_printer *p,
572 __isl_keep isl_space *space, __isl_keep isl_mat *div, int pos,
573 int latex)
575 if (!p || !div)
576 return isl_printer_free(p);
578 p = isl_printer_print_str(p, "(");
579 p = print_affine_of_len(space, div, p,
580 div->row[pos] + 1, div->n_col - 1);
581 p = isl_printer_print_str(p, ") ");
582 p = isl_printer_print_str(p, s_mod[latex]);
583 p = isl_printer_print_str(p, " ");
584 p = isl_printer_print_isl_int(p, div->row[pos][0]);
585 return p;
588 /* Can the equality constraints "c" be printed as a modulo constraint?
589 * In particular, is of the form
591 * f - a m floor(g/m) = 0,
593 * with c = -a m the coefficient at position "pos"?
594 * Return the position of the corresponding integer division if so.
595 * Return the number of integer divisions if not.
596 * Return -1 on error.
598 * Modulo constraints are currently not printed in C format.
599 * Other than that, "pos" needs to correspond to an integer division
600 * with explicit representation and "c" needs to be a multiple
601 * of the denominator of the integer division.
603 static int print_as_modulo_pos(__isl_keep isl_printer *p,
604 __isl_keep isl_space *space, __isl_keep isl_mat *div, unsigned pos,
605 isl_int c)
607 isl_bool can_print;
608 unsigned n_div;
609 enum isl_dim_type type;
611 if (!p || !space)
612 return -1;
613 n_div = isl_mat_rows(div);
614 if (p->output_format == ISL_FORMAT_C)
615 return n_div;
616 type = pos2type(space, &pos);
617 if (type != isl_dim_div)
618 return n_div;
619 can_print = can_print_div_expr(p, div, pos);
620 if (can_print < 0)
621 return -1;
622 if (!can_print)
623 return n_div;
624 if (!isl_int_is_divisible_by(c, div->row[pos][0]))
625 return n_div;
626 return pos;
629 /* Print equality constraint "c" to "p" as a modulo constraint,
630 * with the variable names taken from "space" and
631 * the integer division definitions taken from "div".
632 * "last" is the position of the last non-zero coefficient, which is
633 * moreover assumed to be negative and a multiple of the denominator
634 * of the corresponding integer division. "div_pos" is the corresponding
635 * position in the sequence of integer divisions.
637 * The equality is of the form
639 * f - a m floor(g/m) = 0.
641 * Print it as
643 * a (g mod m) = -f + a g
645 static __isl_give isl_printer *print_eq_mod_constraint(
646 __isl_take isl_printer *p, __isl_keep isl_space *space,
647 __isl_keep isl_mat *div, unsigned div_pos,
648 isl_int *c, int last, int latex)
650 isl_ctx *ctx;
651 int multiple;
653 ctx = isl_printer_get_ctx(p);
654 isl_int_divexact(c[last], c[last], div->row[div_pos][0]);
655 isl_int_abs(c[last], c[last]);
656 multiple = !isl_int_is_one(c[last]);
657 if (multiple) {
658 p = isl_printer_print_isl_int(p, c[last]);
659 p = isl_printer_print_str(p, "*(");
661 p = print_mod(p, space, div, div_pos, latex);
662 if (multiple)
663 p = isl_printer_print_str(p, ")");
664 p = isl_printer_print_str(p, " = ");
665 isl_seq_combine(c, ctx->negone, c,
666 c[last], div->row[div_pos] + 1, last);
667 isl_int_set_si(c[last], 0);
668 p = print_affine(p, space, div, c);
669 return p;
672 /* Print equality constraint "c" to "p", with the variable names
673 * taken from "space" and the integer division definitions taken from "div".
674 * "last" is the position of the last non-zero coefficient, which is
675 * moreover assumed to be negative.
677 * If possible, print the equality constraint as a modulo constraint.
679 static __isl_give isl_printer *print_eq_constraint(__isl_take isl_printer *p,
680 __isl_keep isl_space *space, __isl_keep isl_mat *div, isl_int *c,
681 int last, int latex)
683 unsigned n_div;
684 int div_pos;
686 n_div = isl_mat_rows(div);
687 div_pos = print_as_modulo_pos(p, space, div, last, c[last]);
688 if (div_pos < 0)
689 return isl_printer_free(p);
690 if (div_pos < n_div)
691 return print_eq_mod_constraint(p, space, div, div_pos,
692 c, last, latex);
693 return print_constraint(p, space, div, c, last, "=", latex);
696 /* Print the constraints of "bmap" to "p".
697 * The names of the variables are taken from "space" and
698 * the integer division definitions are taken from "div".
699 * Div constraints are only printed in "dump" mode.
700 * The constraints are sorted prior to printing (except in "dump" mode).
702 * If x is the last variable with a non-zero coefficient,
703 * then a lower bound
705 * f - a x >= 0
707 * is printed as
709 * a x <= f
711 * while an upper bound
713 * f + a x >= 0
715 * is printed as
717 * a x >= -f
719 * If the next constraint has an opposite sign for the same last coefficient,
720 * then it is printed as
722 * f >= a x
724 * or
726 * -f <= a x
728 * instead. In fact, the "a x" part is not printed explicitly, but
729 * reused from the next constraint, which is therefore treated as
730 * a first constraint in the conjunction.
732 * If the constant term of "f" is -1, then "f" is replaced by "f + 1" and
733 * the comparison operator is replaced by the strict variant.
734 * Essentially, ">= 1" is replaced by "> 0".
736 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
737 __isl_keep isl_space *space, __isl_keep isl_mat *div,
738 __isl_take isl_printer *p, int latex)
740 int i;
741 isl_vec *c = NULL;
742 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
743 unsigned total = isl_basic_map_total_dim(bmap);
744 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
745 int first = 1;
746 int dump;
748 if (!p)
749 return NULL;
750 bmap = isl_basic_map_copy(bmap);
751 dump = p->dump;
752 if (!dump)
753 bmap = isl_basic_map_sort_constraints(bmap);
754 if (!bmap)
755 goto error;
757 c = isl_vec_alloc(bmap->ctx, 1 + total);
758 if (!c)
759 goto error;
761 for (i = bmap->n_eq - 1; i >= 0; --i) {
762 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
763 if (l < 0) {
764 if (i != bmap->n_eq - 1)
765 p = isl_printer_print_str(p, s_and[latex]);
766 p = isl_printer_print_str(p, "0 = 0");
767 continue;
769 if (!first)
770 p = isl_printer_print_str(p, s_and[latex]);
771 if (isl_int_is_neg(bmap->eq[i][l]))
772 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
773 else
774 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
775 p = print_eq_constraint(p, space, div, c->el, l, latex);
776 first = 0;
778 for (i = 0; i < bmap->n_ineq; ++i) {
779 isl_bool combine;
780 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
781 int strict;
782 int s;
783 const char *op;
784 if (l < 0)
785 continue;
786 if (!dump && l >= o_div &&
787 can_print_div_expr(p, div, l - o_div)) {
788 isl_bool is_div;
789 is_div = isl_basic_map_is_div_constraint(bmap,
790 bmap->ineq[i], l - o_div);
791 if (is_div < 0)
792 goto error;
793 if (is_div)
794 continue;
796 if (!first)
797 p = isl_printer_print_str(p, s_and[latex]);
798 s = isl_int_sgn(bmap->ineq[i][l]);
799 strict = !rational && isl_int_is_negone(bmap->ineq[i][0]);
800 if (s < 0)
801 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
802 else
803 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
804 if (strict)
805 isl_int_set_si(c->el[0], 0);
806 combine = dump ? isl_bool_false : next_is_opposite(bmap, i, l);
807 if (combine < 0)
808 goto error;
809 if (combine) {
810 op = constraint_op(-s, strict, latex);
811 p = print_half_constraint(p, space, div, c->el, l,
812 op, latex);
813 first = 1;
814 } else {
815 op = constraint_op(s, strict, latex);
816 p = print_constraint(p, space, div, c->el, l,
817 op, latex);
818 first = 0;
822 isl_basic_map_free(bmap);
823 isl_vec_free(c);
825 return p;
826 error:
827 isl_basic_map_free(bmap);
828 isl_vec_free(c);
829 isl_printer_free(p);
830 return NULL;
833 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
834 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
836 int c;
838 if (!p || !div)
839 return isl_printer_free(p);
841 c = p->output_format == ISL_FORMAT_C;
842 p = isl_printer_print_str(p, c ? "floord(" : "floor((");
843 p = print_affine_of_len(dim, div, p,
844 div->row[pos] + 1, div->n_col - 1);
845 p = isl_printer_print_str(p, c ? ", " : ")/");
846 p = isl_printer_print_isl_int(p, div->row[pos][0]);
847 p = isl_printer_print_str(p, ")");
848 return p;
851 /* Print a comma separated list of div names, except those that have
852 * a definition that can be printed.
853 * If "print_defined_divs" is set, then those div names are printed
854 * as well, along with their definitions.
856 static __isl_give isl_printer *print_div_list(__isl_take isl_printer *p,
857 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex,
858 int print_defined_divs)
860 int i;
861 int first = 1;
862 unsigned n_div;
864 if (!p || !space || !div)
865 return isl_printer_free(p);
867 n_div = isl_mat_rows(div);
869 for (i = 0; i < n_div; ++i) {
870 if (!print_defined_divs && can_print_div_expr(p, div, i))
871 continue;
872 if (!first)
873 p = isl_printer_print_str(p, ", ");
874 p = print_name(space, p, isl_dim_div, i, latex);
875 first = 0;
876 if (!can_print_div_expr(p, div, i))
877 continue;
878 p = isl_printer_print_str(p, " = ");
879 p = print_div(space, div, i, p);
882 return p;
885 /* Does printing an object with local variables described by "div"
886 * require an "exists" clause?
887 * That is, are there any local variables without an explicit representation?
888 * An exists clause is also needed in "dump" mode because
889 * explicit div representations are not printed inline in that case.
891 static isl_bool need_exists(__isl_keep isl_printer *p, __isl_keep isl_mat *div)
893 int i, n;
895 if (!p || !div)
896 return isl_bool_error;
897 n = isl_mat_rows(div);
898 if (n == 0)
899 return isl_bool_false;
900 if (p->dump)
901 return isl_bool_true;
902 for (i = 0; i < n; ++i)
903 if (!can_print_div_expr(p, div, i))
904 return isl_bool_true;
905 return isl_bool_false;
908 /* Print the start of an exists clause, i.e.,
910 * (exists variables:
912 * In dump mode, local variables with an explicit definition are printed
913 * as well because they will not be printed inline.
915 static __isl_give isl_printer *open_exists(__isl_take isl_printer *p,
916 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex)
918 int dump;
920 if (!p)
921 return NULL;
923 dump = p->dump;
924 p = isl_printer_print_str(p, s_open_exists[latex]);
925 p = print_div_list(p, space, div, latex, dump);
926 p = isl_printer_print_str(p, ": ");
928 return p;
931 /* Remove the explicit representations of all local variables in "div".
933 static __isl_give isl_mat *mark_all_unknown(__isl_take isl_mat *div)
935 int i, n_div;
937 if (!div)
938 return NULL;
940 n_div = isl_mat_rows(div);
941 for (i = 0; i < n_div; ++i)
942 div = isl_mat_set_element_si(div, i, 0, 0);
943 return div;
946 /* Print the constraints of "bmap" to "p".
947 * The names of the variables are taken from "space".
948 * "latex" is set if the constraints should be printed in LaTeX format.
949 * Do not print inline explicit div representations in "dump" mode.
951 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
952 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
954 int dump;
955 isl_mat *div;
956 isl_bool exists;
958 if (!p)
959 return NULL;
960 dump = p->dump;
961 div = isl_basic_map_get_divs(bmap);
962 exists = need_exists(p, div);
963 if (exists >= 0 && exists)
964 p = open_exists(p, space, div, latex);
966 if (dump)
967 div = mark_all_unknown(div);
968 p = print_constraints(bmap, space, div, p, latex);
969 isl_mat_free(div);
971 if (exists >= 0 && exists)
972 p = isl_printer_print_str(p, s_close_exists[latex]);
973 return p;
976 /* Print a colon followed by the constraints of "bmap"
977 * to "p", provided there are any constraints.
978 * The names of the variables are taken from "space".
979 * "latex" is set if the constraints should be printed in LaTeX format.
981 static __isl_give isl_printer *print_optional_disjunct(
982 __isl_keep isl_basic_map *bmap, __isl_keep isl_space *space,
983 __isl_take isl_printer *p, int latex)
985 if (isl_basic_map_plain_is_universe(bmap))
986 return p;
988 p = isl_printer_print_str(p, ": ");
989 p = print_disjunct(bmap, space, p, latex);
991 return p;
994 static __isl_give isl_printer *basic_map_print_omega(
995 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
997 p = isl_printer_print_str(p, "{ [");
998 p = print_var_list(p, bmap->dim, isl_dim_in);
999 p = isl_printer_print_str(p, "] -> [");
1000 p = print_var_list(p, bmap->dim, isl_dim_out);
1001 p = isl_printer_print_str(p, "] ");
1002 p = print_optional_disjunct(bmap, bmap->dim, p, 0);
1003 p = isl_printer_print_str(p, " }");
1004 return p;
1007 static __isl_give isl_printer *basic_set_print_omega(
1008 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
1010 p = isl_printer_print_str(p, "{ [");
1011 p = print_var_list(p, bset->dim, isl_dim_set);
1012 p = isl_printer_print_str(p, "] ");
1013 p = print_optional_disjunct(bset, bset->dim, p, 0);
1014 p = isl_printer_print_str(p, " }");
1015 return p;
1018 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
1019 __isl_take isl_printer *p)
1021 int i;
1023 for (i = 0; i < map->n; ++i) {
1024 if (i)
1025 p = isl_printer_print_str(p, " union ");
1026 p = basic_map_print_omega(map->p[i], p);
1028 return p;
1031 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
1032 __isl_take isl_printer *p)
1034 int i;
1036 for (i = 0; i < set->n; ++i) {
1037 if (i)
1038 p = isl_printer_print_str(p, " union ");
1039 p = basic_set_print_omega(set->p[i], p);
1041 return p;
1044 /* Print the list of parameters in "space", followed by an arrow, to "p",
1045 * if there are any parameters.
1047 static __isl_give isl_printer *print_param_tuple(__isl_take isl_printer *p,
1048 __isl_keep isl_space *space, struct isl_print_space_data *data)
1050 if (!p || !space)
1051 return isl_printer_free(p);
1052 if (isl_space_dim(space, isl_dim_param) == 0)
1053 return p;
1055 p = print_tuple(space, p, isl_dim_param, data);
1056 p = isl_printer_print_str(p, s_to[data->latex]);
1058 return p;
1061 static __isl_give isl_printer *isl_basic_map_print_isl(
1062 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
1063 int latex)
1065 struct isl_print_space_data data = { .latex = latex };
1066 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1068 p = print_param_tuple(p, bmap->dim, &data);
1069 p = isl_printer_print_str(p, "{ ");
1070 p = isl_print_space(bmap->dim, p, rational, &data);
1071 p = isl_printer_print_str(p, " : ");
1072 p = print_disjunct(bmap, bmap->dim, p, latex);
1073 p = isl_printer_print_str(p, " }");
1074 return p;
1077 /* Print the disjuncts of a map (or set) "map" to "p".
1078 * The names of the variables are taken from "space".
1079 * "latex" is set if the constraints should be printed in LaTeX format.
1081 static __isl_give isl_printer *print_disjuncts_core(__isl_keep isl_map *map,
1082 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1084 int i;
1086 if (map->n == 0)
1087 p = isl_printer_print_str(p, "false");
1088 for (i = 0; i < map->n; ++i) {
1089 if (i)
1090 p = isl_printer_print_str(p, s_or[latex]);
1091 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
1092 p = isl_printer_print_str(p, "(");
1093 p = print_disjunct(map->p[i], space, p, latex);
1094 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
1095 p = isl_printer_print_str(p, ")");
1097 return p;
1100 /* Print the disjuncts of a map (or set) "map" to "p".
1101 * The names of the variables are taken from "space".
1102 * "hull" describes constraints shared by all disjuncts of "map".
1103 * "latex" is set if the constraints should be printed in LaTeX format.
1105 * Print the disjuncts as a conjunction of "hull" and
1106 * the result of removing the constraints of "hull" from "map".
1107 * If this result turns out to be the universe, then simply print "hull".
1109 static __isl_give isl_printer *print_disjuncts_in_hull(__isl_keep isl_map *map,
1110 __isl_keep isl_space *space, __isl_take isl_basic_map *hull,
1111 __isl_take isl_printer *p, int latex)
1113 isl_bool is_universe;
1115 p = print_disjunct(hull, space, p, latex);
1116 map = isl_map_plain_gist_basic_map(isl_map_copy(map), hull);
1117 is_universe = isl_map_plain_is_universe(map);
1118 if (is_universe < 0)
1119 goto error;
1120 if (!is_universe) {
1121 p = isl_printer_print_str(p, s_and[latex]);
1122 p = isl_printer_print_str(p, "(");
1123 p = print_disjuncts_core(map, space, p, latex);
1124 p = isl_printer_print_str(p, ")");
1126 isl_map_free(map);
1128 return p;
1129 error:
1130 isl_map_free(map);
1131 isl_printer_free(p);
1132 return NULL;
1135 /* Print the disjuncts of a map (or set) "map" to "p".
1136 * The names of the variables are taken from "space".
1137 * "latex" is set if the constraints should be printed in LaTeX format.
1139 * If there are at least two disjuncts and "dump" mode is not turned out,
1140 * check for any shared constraints among all disjuncts.
1141 * If there are any, then print them separately in print_disjuncts_in_hull.
1143 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
1144 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1146 if (isl_map_plain_is_universe(map))
1147 return p;
1149 p = isl_printer_print_str(p, s_such_that[latex]);
1150 if (!p)
1151 return NULL;
1153 if (!p->dump && map->n >= 2) {
1154 isl_basic_map *hull;
1155 isl_bool is_universe;
1157 hull = isl_map_plain_unshifted_simple_hull(isl_map_copy(map));
1158 is_universe = isl_basic_map_plain_is_universe(hull);
1159 if (is_universe < 0)
1160 p = isl_printer_free(p);
1161 else if (!is_universe)
1162 return print_disjuncts_in_hull(map, space, hull,
1163 p, latex);
1164 isl_basic_map_free(hull);
1167 return print_disjuncts_core(map, space, p, latex);
1170 /* Print the disjuncts of a map (or set).
1171 * The names of the variables are taken from "space".
1172 * "latex" is set if the constraints should be printed in LaTeX format.
1174 * If the map turns out to be a universal parameter domain, then
1175 * we need to print the colon. Otherwise, the output looks identical
1176 * to the empty set.
1178 static __isl_give isl_printer *print_disjuncts_map(__isl_keep isl_map *map,
1179 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1181 if (isl_map_plain_is_universe(map) && isl_space_is_params(map->dim))
1182 return isl_printer_print_str(p, s_such_that[latex]);
1183 else
1184 return print_disjuncts(map, space, p, latex);
1187 /* Print the disjuncts of a set.
1188 * The names of the variables are taken from "space".
1189 * "latex" is set if the constraints should be printed in LaTeX format.
1191 static __isl_give isl_printer *print_disjuncts_set(__isl_keep isl_set *set,
1192 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1194 return print_disjuncts_map(set_to_map(set), space, p, latex);
1197 struct isl_aff_split {
1198 isl_basic_map *aff;
1199 isl_map *map;
1202 static void free_split(__isl_take struct isl_aff_split *split, int n)
1204 int i;
1206 if (!split)
1207 return;
1209 for (i = 0; i < n; ++i) {
1210 isl_basic_map_free(split[i].aff);
1211 isl_map_free(split[i].map);
1214 free(split);
1217 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
1219 int i, j;
1220 unsigned nparam, n_in, n_out, total;
1222 bmap = isl_basic_map_cow(bmap);
1223 if (!bmap)
1224 return NULL;
1225 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
1226 goto error;
1228 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1229 n_in = isl_basic_map_dim(bmap, isl_dim_in);
1230 n_out = isl_basic_map_dim(bmap, isl_dim_out);
1231 total = isl_basic_map_dim(bmap, isl_dim_all);
1232 for (i = bmap->n_eq - 1; i >= 0; --i) {
1233 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
1234 if (j >= nparam && j < nparam + n_in + n_out &&
1235 (isl_int_is_one(bmap->eq[i][1 + j]) ||
1236 isl_int_is_negone(bmap->eq[i][1 + j])))
1237 continue;
1238 if (isl_basic_map_drop_equality(bmap, i) < 0)
1239 goto error;
1242 bmap = isl_basic_map_finalize(bmap);
1244 return bmap;
1245 error:
1246 isl_basic_map_free(bmap);
1247 return NULL;
1250 static int aff_split_cmp(const void *p1, const void *p2, void *user)
1252 const struct isl_aff_split *s1, *s2;
1253 s1 = (const struct isl_aff_split *) p1;
1254 s2 = (const struct isl_aff_split *) p2;
1256 return isl_basic_map_plain_cmp(s1->aff, s2->aff);
1259 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
1260 __isl_keep isl_basic_map *aff)
1262 int i, j;
1263 unsigned total;
1265 if (!bmap || !aff)
1266 goto error;
1268 total = isl_space_dim(bmap->dim, isl_dim_all);
1270 for (i = bmap->n_eq - 1; i >= 0; --i) {
1271 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
1272 bmap->n_div) != -1)
1273 continue;
1274 for (j = 0; j < aff->n_eq; ++j) {
1275 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
1276 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
1277 continue;
1278 if (isl_basic_map_drop_equality(bmap, i) < 0)
1279 goto error;
1280 break;
1284 return bmap;
1285 error:
1286 isl_basic_map_free(bmap);
1287 return NULL;
1290 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
1292 int i, n;
1293 struct isl_aff_split *split;
1294 isl_ctx *ctx;
1296 ctx = isl_map_get_ctx(map);
1297 split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
1298 if (!split)
1299 return NULL;
1301 for (i = 0; i < map->n; ++i) {
1302 isl_basic_map *bmap;
1303 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
1304 bmap = isl_basic_map_copy(map->p[i]);
1305 bmap = isl_basic_map_cow(bmap);
1306 bmap = drop_aff(bmap, split[i].aff);
1307 split[i].map = isl_map_from_basic_map(bmap);
1308 if (!split[i].aff || !split[i].map)
1309 goto error;
1312 if (isl_sort(split, map->n, sizeof(struct isl_aff_split),
1313 &aff_split_cmp, NULL) < 0)
1314 goto error;
1316 n = map->n;
1317 for (i = n - 1; i >= 1; --i) {
1318 if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
1319 split[i].aff))
1320 continue;
1321 isl_basic_map_free(split[i].aff);
1322 split[i - 1].map = isl_map_union(split[i - 1].map,
1323 split[i].map);
1324 if (i != n - 1)
1325 split[i] = split[n - 1];
1326 split[n - 1].aff = NULL;
1327 split[n - 1].map = NULL;
1328 --n;
1331 return split;
1332 error:
1333 free_split(split, map->n);
1334 return NULL;
1337 static int defining_equality(__isl_keep isl_basic_map *eq,
1338 __isl_keep isl_space *dim, enum isl_dim_type type, int pos)
1340 int i;
1341 unsigned total;
1343 if (!eq)
1344 return -1;
1346 pos += isl_space_offset(dim, type);
1347 total = isl_basic_map_total_dim(eq);
1349 for (i = 0; i < eq->n_eq; ++i) {
1350 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
1351 continue;
1352 if (isl_int_is_one(eq->eq[i][1 + pos]))
1353 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
1354 return i;
1357 return -1;
1360 /* Print dimension "pos" of data->space to "p".
1362 * data->user is assumed to be an isl_basic_map keeping track of equalities.
1364 * If the current dimension is defined by these equalities, then print
1365 * the corresponding expression, assigned to the name of the dimension
1366 * if there is any. Otherwise, print the name of the dimension.
1368 static __isl_give isl_printer *print_dim_eq(__isl_take isl_printer *p,
1369 struct isl_print_space_data *data, unsigned pos)
1371 isl_basic_map *eq = data->user;
1372 int j;
1374 j = defining_equality(eq, data->space, data->type, pos);
1375 if (j >= 0) {
1376 if (isl_space_has_dim_name(data->space, data->type, pos)) {
1377 p = print_name(data->space, p, data->type, pos,
1378 data->latex);
1379 p = isl_printer_print_str(p, " = ");
1381 pos += 1 + isl_space_offset(data->space, data->type);
1382 p = print_affine_of_len(data->space, NULL, p, eq->eq[j], pos);
1383 } else {
1384 p = print_name(data->space, p, data->type, pos, data->latex);
1387 return p;
1390 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
1391 struct isl_aff_split *split, int n, __isl_keep isl_space *space)
1393 struct isl_print_space_data data = { 0 };
1394 int i;
1395 int rational;
1397 data.print_dim = &print_dim_eq;
1398 for (i = 0; i < n; ++i) {
1399 if (!split[i].map)
1400 break;
1401 rational = split[i].map->n > 0 &&
1402 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
1403 if (i)
1404 p = isl_printer_print_str(p, "; ");
1405 data.user = split[i].aff;
1406 p = isl_print_space(space, p, rational, &data);
1407 p = print_disjuncts_map(split[i].map, space, p, 0);
1410 return p;
1413 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
1414 __isl_take isl_printer *p)
1416 struct isl_print_space_data data = { 0 };
1417 struct isl_aff_split *split = NULL;
1418 int rational;
1420 if (!p || !map)
1421 return isl_printer_free(p);
1422 if (!p->dump && map->n > 0)
1423 split = split_aff(map);
1424 if (split) {
1425 p = print_split_map(p, split, map->n, map->dim);
1426 } else {
1427 rational = map->n > 0 &&
1428 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
1429 p = isl_print_space(map->dim, p, rational, &data);
1430 p = print_disjuncts_map(map, map->dim, p, 0);
1432 free_split(split, map->n);
1433 return p;
1436 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
1437 __isl_take isl_printer *p)
1439 struct isl_print_space_data data = { 0 };
1441 p = print_param_tuple(p, map->dim, &data);
1442 p = isl_printer_print_str(p, s_open_set[0]);
1443 p = isl_map_print_isl_body(map, p);
1444 p = isl_printer_print_str(p, s_close_set[0]);
1445 return p;
1448 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
1449 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff)
1451 struct isl_print_space_data data = { 0 };
1453 data.latex = 1;
1454 p = print_param_tuple(p, map->dim, &data);
1455 p = isl_printer_print_str(p, s_open_set[1]);
1456 data.print_dim = &print_dim_eq;
1457 data.user = aff;
1458 p = isl_print_space(map->dim, p, 0, &data);
1459 p = print_disjuncts_map(map, map->dim, p, 1);
1460 p = isl_printer_print_str(p, s_close_set[1]);
1462 return p;
1465 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
1466 __isl_take isl_printer *p)
1468 int i;
1469 struct isl_aff_split *split = NULL;
1471 if (map->n > 0)
1472 split = split_aff(map);
1474 if (!split)
1475 return print_latex_map(map, p, NULL);
1477 for (i = 0; i < map->n; ++i) {
1478 if (!split[i].map)
1479 break;
1480 if (i)
1481 p = isl_printer_print_str(p, " \\cup ");
1482 p = print_latex_map(split[i].map, p, split[i].aff);
1485 free_split(split, map->n);
1486 return p;
1489 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
1490 __isl_keep isl_basic_map *bmap)
1492 if (!p || !bmap)
1493 goto error;
1494 if (p->output_format == ISL_FORMAT_ISL)
1495 return isl_basic_map_print_isl(bmap, p, 0);
1496 else if (p->output_format == ISL_FORMAT_OMEGA)
1497 return basic_map_print_omega(bmap, p);
1498 isl_assert(bmap->ctx, 0, goto error);
1499 error:
1500 isl_printer_free(p);
1501 return NULL;
1504 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
1505 __isl_keep isl_basic_set *bset)
1507 if (!p || !bset)
1508 goto error;
1510 if (p->output_format == ISL_FORMAT_ISL)
1511 return isl_basic_map_print_isl(bset, p, 0);
1512 else if (p->output_format == ISL_FORMAT_POLYLIB)
1513 return isl_basic_set_print_polylib(bset, p, 0);
1514 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1515 return isl_basic_set_print_polylib(bset, p, 1);
1516 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
1517 return bset_print_constraints_polylib(bset, p);
1518 else if (p->output_format == ISL_FORMAT_OMEGA)
1519 return basic_set_print_omega(bset, p);
1520 isl_assert(p->ctx, 0, goto error);
1521 error:
1522 isl_printer_free(p);
1523 return NULL;
1526 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
1527 __isl_keep isl_set *set)
1529 if (!p || !set)
1530 goto error;
1531 if (p->output_format == ISL_FORMAT_ISL)
1532 return isl_map_print_isl(set_to_map(set), p);
1533 else if (p->output_format == ISL_FORMAT_POLYLIB)
1534 return isl_set_print_polylib(set, p, 0);
1535 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1536 return isl_set_print_polylib(set, p, 1);
1537 else if (p->output_format == ISL_FORMAT_OMEGA)
1538 return isl_set_print_omega(set, p);
1539 else if (p->output_format == ISL_FORMAT_LATEX)
1540 return isl_map_print_latex(set_to_map(set), p);
1541 isl_assert(set->ctx, 0, goto error);
1542 error:
1543 isl_printer_free(p);
1544 return NULL;
1547 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1548 __isl_keep isl_map *map)
1550 if (!p || !map)
1551 goto error;
1553 if (p->output_format == ISL_FORMAT_ISL)
1554 return isl_map_print_isl(map, p);
1555 else if (p->output_format == ISL_FORMAT_POLYLIB)
1556 return isl_map_print_polylib(map, p, 0);
1557 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1558 return isl_map_print_polylib(map, p, 1);
1559 else if (p->output_format == ISL_FORMAT_OMEGA)
1560 return isl_map_print_omega(map, p);
1561 else if (p->output_format == ISL_FORMAT_LATEX)
1562 return isl_map_print_latex(map, p);
1563 isl_assert(map->ctx, 0, goto error);
1564 error:
1565 isl_printer_free(p);
1566 return NULL;
1569 struct isl_union_print_data {
1570 isl_printer *p;
1571 int first;
1574 static isl_stat print_map_body(__isl_take isl_map *map, void *user)
1576 struct isl_union_print_data *data;
1577 data = (struct isl_union_print_data *)user;
1579 if (!data->first)
1580 data->p = isl_printer_print_str(data->p, "; ");
1581 data->first = 0;
1583 data->p = isl_map_print_isl_body(map, data->p);
1584 isl_map_free(map);
1586 return isl_stat_ok;
1589 /* Print the body of "umap" (everything except the parameter declarations)
1590 * to "p" in isl format.
1592 static __isl_give isl_printer *isl_printer_print_union_map_isl_body(
1593 __isl_take isl_printer *p, __isl_keep isl_union_map *umap)
1595 struct isl_union_print_data data;
1597 p = isl_printer_print_str(p, s_open_set[0]);
1598 data.p = p;
1599 data.first = 1;
1600 isl_union_map_foreach_map(umap, &print_map_body, &data);
1601 p = data.p;
1602 p = isl_printer_print_str(p, s_close_set[0]);
1603 return p;
1606 /* Print the body of "uset" (everything except the parameter declarations)
1607 * to "p" in isl format.
1609 static __isl_give isl_printer *isl_printer_print_union_set_isl_body(
1610 __isl_take isl_printer *p, __isl_keep isl_union_set *uset)
1612 return isl_printer_print_union_map_isl_body(p, uset_to_umap(uset));
1615 /* Print the isl_union_map "umap" to "p" in isl format.
1617 static __isl_give isl_printer *isl_union_map_print_isl(
1618 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1620 struct isl_print_space_data space_data = { 0 };
1621 isl_space *space;
1623 space = isl_union_map_get_space(umap);
1624 p = print_param_tuple(p, space, &space_data);
1625 isl_space_free(space);
1627 p = isl_printer_print_union_map_isl_body(p, umap);
1629 return p;
1632 static isl_stat print_latex_map_body(__isl_take isl_map *map, void *user)
1634 struct isl_union_print_data *data;
1635 data = (struct isl_union_print_data *)user;
1637 if (!data->first)
1638 data->p = isl_printer_print_str(data->p, " \\cup ");
1639 data->first = 0;
1641 data->p = isl_map_print_latex(map, data->p);
1642 isl_map_free(map);
1644 return isl_stat_ok;
1647 static __isl_give isl_printer *isl_union_map_print_latex(
1648 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1650 struct isl_union_print_data data = { p, 1 };
1651 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1652 p = data.p;
1653 return p;
1656 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1657 __isl_keep isl_union_map *umap)
1659 if (!p || !umap)
1660 goto error;
1662 if (p->output_format == ISL_FORMAT_ISL)
1663 return isl_union_map_print_isl(umap, p);
1664 if (p->output_format == ISL_FORMAT_LATEX)
1665 return isl_union_map_print_latex(umap, p);
1667 isl_die(p->ctx, isl_error_invalid,
1668 "invalid output format for isl_union_map", goto error);
1669 error:
1670 isl_printer_free(p);
1671 return NULL;
1674 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1675 __isl_keep isl_union_set *uset)
1677 if (!p || !uset)
1678 goto error;
1680 if (p->output_format == ISL_FORMAT_ISL)
1681 return isl_union_map_print_isl(uset_to_umap(uset), p);
1682 if (p->output_format == ISL_FORMAT_LATEX)
1683 return isl_union_map_print_latex(uset_to_umap(uset), p);
1685 isl_die(p->ctx, isl_error_invalid,
1686 "invalid output format for isl_union_set", goto error);
1687 error:
1688 isl_printer_free(p);
1689 return NULL;
1692 static int poly_rec_n_non_zero(__isl_keep isl_poly_rec *rec)
1694 int i;
1695 int n;
1697 if (!rec)
1698 return -1;
1700 for (i = 0, n = 0; i < rec->n; ++i) {
1701 isl_bool is_zero = isl_poly_is_zero(rec->p[i]);
1703 if (is_zero < 0)
1704 return -1;
1705 if (!is_zero)
1706 ++n;
1709 return n;
1712 static __isl_give isl_printer *poly_print_cst(__isl_keep isl_poly *poly,
1713 __isl_take isl_printer *p, int first)
1715 isl_poly_cst *cst;
1716 int neg;
1718 cst = isl_poly_as_cst(poly);
1719 if (!cst)
1720 goto error;
1721 neg = !first && isl_int_is_neg(cst->n);
1722 if (!first)
1723 p = isl_printer_print_str(p, neg ? " - " : " + ");
1724 if (neg)
1725 isl_int_neg(cst->n, cst->n);
1726 if (isl_int_is_zero(cst->d)) {
1727 int sgn = isl_int_sgn(cst->n);
1728 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1729 sgn == 0 ? "NaN" : "infty");
1730 } else
1731 p = isl_printer_print_isl_int(p, cst->n);
1732 if (neg)
1733 isl_int_neg(cst->n, cst->n);
1734 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1735 p = isl_printer_print_str(p, "/");
1736 p = isl_printer_print_isl_int(p, cst->d);
1738 return p;
1739 error:
1740 isl_printer_free(p);
1741 return NULL;
1744 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1745 __isl_keep isl_space *space, __isl_keep isl_mat *div, int var)
1747 unsigned total;
1749 total = isl_space_dim(space, isl_dim_all);
1750 if (var < total)
1751 p = print_term(space, NULL, space->ctx->one, 1 + var, p, 0);
1752 else
1753 p = print_div(space, div, var - total, p);
1754 return p;
1757 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1758 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var, int exp)
1760 p = print_base(p, dim, div, var);
1761 if (exp == 1)
1762 return p;
1763 if (p->output_format == ISL_FORMAT_C) {
1764 int i;
1765 for (i = 1; i < exp; ++i) {
1766 p = isl_printer_print_str(p, "*");
1767 p = print_base(p, dim, div, var);
1769 } else {
1770 p = isl_printer_print_str(p, "^");
1771 p = isl_printer_print_int(p, exp);
1773 return p;
1776 /* Print the polynomial "poly" defined over the domain space "space" and
1777 * local variables defined by "div" to "p".
1779 static __isl_give isl_printer *poly_print(__isl_keep isl_poly *poly,
1780 __isl_keep isl_space *space, __isl_keep isl_mat *div,
1781 __isl_take isl_printer *p)
1783 int i, n, first, print_parens;
1784 isl_bool is_cst;
1785 isl_poly_rec *rec;
1787 is_cst = isl_poly_is_cst(poly);
1788 if (!p || is_cst < 0 || !space || !div)
1789 goto error;
1791 if (is_cst)
1792 return poly_print_cst(poly, p, 1);
1794 rec = isl_poly_as_rec(poly);
1795 n = poly_rec_n_non_zero(rec);
1796 if (n < 0)
1797 return isl_printer_free(p);
1798 print_parens = n > 1;
1799 if (print_parens)
1800 p = isl_printer_print_str(p, "(");
1801 for (i = 0, first = 1; i < rec->n; ++i) {
1802 isl_bool is_zero = isl_poly_is_zero(rec->p[i]);
1803 isl_bool is_one = isl_poly_is_one(rec->p[i]);
1804 isl_bool is_negone = isl_poly_is_negone(rec->p[i]);
1805 isl_bool is_cst = isl_poly_is_cst(rec->p[i]);
1807 if (is_zero < 0 || is_one < 0 || is_negone < 0)
1808 return isl_printer_free(p);
1809 if (is_zero)
1810 continue;
1811 if (is_negone) {
1812 if (!i)
1813 p = isl_printer_print_str(p, "-1");
1814 else if (first)
1815 p = isl_printer_print_str(p, "-");
1816 else
1817 p = isl_printer_print_str(p, " - ");
1818 } else if (is_cst && !is_one)
1819 p = poly_print_cst(rec->p[i], p, first);
1820 else {
1821 if (!first)
1822 p = isl_printer_print_str(p, " + ");
1823 if (i == 0 || !is_one)
1824 p = poly_print(rec->p[i], space, div, p);
1826 first = 0;
1827 if (i == 0)
1828 continue;
1829 if (!is_one && !is_negone)
1830 p = isl_printer_print_str(p, " * ");
1831 p = print_pow(p, space, div, rec->poly.var, i);
1833 if (print_parens)
1834 p = isl_printer_print_str(p, ")");
1835 return p;
1836 error:
1837 isl_printer_free(p);
1838 return NULL;
1841 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1842 __isl_keep isl_qpolynomial *qp)
1844 if (!p || !qp)
1845 goto error;
1846 p = poly_print(qp->poly, qp->dim, qp->div, p);
1847 return p;
1848 error:
1849 isl_printer_free(p);
1850 return NULL;
1853 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1854 __isl_keep isl_qpolynomial *qp)
1856 struct isl_print_space_data data = { 0 };
1858 if (!p || !qp)
1859 goto error;
1861 p = print_param_tuple(p, qp->dim, &data);
1862 p = isl_printer_print_str(p, "{ ");
1863 if (!isl_space_is_params(qp->dim)) {
1864 p = isl_print_space(qp->dim, p, 0, &data);
1865 p = isl_printer_print_str(p, " -> ");
1867 p = print_qpolynomial(p, qp);
1868 p = isl_printer_print_str(p, " }");
1869 return p;
1870 error:
1871 isl_printer_free(p);
1872 return NULL;
1875 /* Print the quasi-polynomial "qp" to "p" in C format, with the variable names
1876 * taken from the domain space "space".
1878 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1879 __isl_keep isl_space *space, __isl_keep isl_qpolynomial *qp)
1881 isl_bool is_one;
1882 isl_val *den;
1884 den = isl_qpolynomial_get_den(qp);
1885 qp = isl_qpolynomial_copy(qp);
1886 qp = isl_qpolynomial_scale_val(qp, isl_val_copy(den));
1887 is_one = isl_val_is_one(den);
1888 if (is_one < 0)
1889 p = isl_printer_free(p);
1890 if (!is_one)
1891 p = isl_printer_print_str(p, "(");
1892 if (qp)
1893 p = poly_print(qp->poly, space, qp->div, p);
1894 else
1895 p = isl_printer_free(p);
1896 if (!is_one) {
1897 p = isl_printer_print_str(p, ")/");
1898 p = isl_printer_print_val(p, den);
1900 isl_qpolynomial_free(qp);
1901 isl_val_free(den);
1902 return p;
1905 __isl_give isl_printer *isl_printer_print_qpolynomial(
1906 __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1908 if (!p || !qp)
1909 goto error;
1911 if (p->output_format == ISL_FORMAT_ISL)
1912 return print_qpolynomial_isl(p, qp);
1913 else if (p->output_format == ISL_FORMAT_C)
1914 return print_qpolynomial_c(p, qp->dim, qp);
1915 else
1916 isl_die(qp->dim->ctx, isl_error_unsupported,
1917 "output format not supported for isl_qpolynomials",
1918 goto error);
1919 error:
1920 isl_printer_free(p);
1921 return NULL;
1924 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1925 unsigned output_format)
1927 isl_printer *p;
1929 if (!qp)
1930 return;
1932 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1933 p = isl_printer_to_file(qp->dim->ctx, out);
1934 p = isl_printer_print_qpolynomial(p, qp);
1935 isl_printer_free(p);
1938 static __isl_give isl_printer *qpolynomial_fold_print(
1939 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1941 int i;
1943 if (fold->type == isl_fold_min)
1944 p = isl_printer_print_str(p, "min");
1945 else if (fold->type == isl_fold_max)
1946 p = isl_printer_print_str(p, "max");
1947 p = isl_printer_print_str(p, "(");
1948 for (i = 0; i < fold->n; ++i) {
1949 if (i)
1950 p = isl_printer_print_str(p, ", ");
1951 p = print_qpolynomial(p, fold->qp[i]);
1953 p = isl_printer_print_str(p, ")");
1954 return p;
1957 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1958 FILE *out, unsigned output_format)
1960 isl_printer *p;
1962 if (!fold)
1963 return;
1965 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1967 p = isl_printer_to_file(fold->dim->ctx, out);
1968 p = isl_printer_print_qpolynomial_fold(p, fold);
1970 isl_printer_free(p);
1973 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1974 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1976 struct isl_print_space_data data = { 0 };
1977 int i = 0;
1979 for (i = 0; i < pwqp->n; ++i) {
1980 isl_space *space;
1982 if (i)
1983 p = isl_printer_print_str(p, "; ");
1984 space = isl_qpolynomial_get_domain_space(pwqp->p[i].qp);
1985 if (!isl_space_is_params(space)) {
1986 p = isl_print_space(space, p, 0, &data);
1987 p = isl_printer_print_str(p, " -> ");
1989 p = print_qpolynomial(p, pwqp->p[i].qp);
1990 p = print_disjuncts(set_to_map(pwqp->p[i].set), space, p, 0);
1991 isl_space_free(space);
1994 return p;
1997 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1998 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2000 struct isl_print_space_data data = { 0 };
2002 if (!p || !pwqp)
2003 goto error;
2005 p = print_param_tuple(p, pwqp->dim, &data);
2006 p = isl_printer_print_str(p, "{ ");
2007 if (pwqp->n == 0) {
2008 if (!isl_space_is_set(pwqp->dim)) {
2009 p = print_tuple(pwqp->dim, p, isl_dim_in, &data);
2010 p = isl_printer_print_str(p, " -> ");
2012 p = isl_printer_print_str(p, "0");
2014 p = isl_pwqp_print_isl_body(p, pwqp);
2015 p = isl_printer_print_str(p, " }");
2016 return p;
2017 error:
2018 isl_printer_free(p);
2019 return NULL;
2022 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
2023 unsigned output_format)
2025 isl_printer *p;
2027 if (!pwqp)
2028 return;
2030 p = isl_printer_to_file(pwqp->dim->ctx, out);
2031 p = isl_printer_set_output_format(p, output_format);
2032 p = isl_printer_print_pw_qpolynomial(p, pwqp);
2034 isl_printer_free(p);
2037 static __isl_give isl_printer *isl_pwf_print_isl_body(
2038 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2040 struct isl_print_space_data data = { 0 };
2041 int i = 0;
2043 for (i = 0; i < pwf->n; ++i) {
2044 isl_space *space;
2046 if (i)
2047 p = isl_printer_print_str(p, "; ");
2048 space = isl_qpolynomial_fold_get_domain_space(pwf->p[i].fold);
2049 if (!isl_space_is_params(space)) {
2050 p = isl_print_space(space, p, 0, &data);
2051 p = isl_printer_print_str(p, " -> ");
2053 p = qpolynomial_fold_print(pwf->p[i].fold, p);
2054 p = print_disjuncts(set_to_map(pwf->p[i].set), space, p, 0);
2055 isl_space_free(space);
2058 return p;
2061 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
2062 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2064 struct isl_print_space_data data = { 0 };
2066 p = print_param_tuple(p, pwf->dim, &data);
2067 p = isl_printer_print_str(p, "{ ");
2068 if (pwf->n == 0) {
2069 if (!isl_space_is_set(pwf->dim)) {
2070 p = print_tuple(pwf->dim, p, isl_dim_in, &data);
2071 p = isl_printer_print_str(p, " -> ");
2073 p = isl_printer_print_str(p, "0");
2075 p = isl_pwf_print_isl_body(p, pwf);
2076 p = isl_printer_print_str(p, " }");
2077 return p;
2080 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
2081 __isl_keep isl_space *space, __isl_keep isl_basic_set *bset,
2082 isl_int *c);
2084 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
2085 __isl_keep isl_space *dim,
2086 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
2088 if (type == isl_dim_div) {
2089 p = isl_printer_print_str(p, "floord(");
2090 p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
2091 p = isl_printer_print_str(p, ", ");
2092 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
2093 p = isl_printer_print_str(p, ")");
2094 } else {
2095 const char *name;
2097 name = isl_space_get_dim_name(dim, type, pos);
2098 if (!name)
2099 name = "UNNAMED";
2100 p = isl_printer_print_str(p, name);
2102 return p;
2105 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
2106 __isl_keep isl_space *space,
2107 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
2109 enum isl_dim_type type;
2111 if (!p || !space)
2112 return isl_printer_free(p);
2114 if (pos == 0)
2115 return isl_printer_print_isl_int(p, c);
2117 if (isl_int_is_one(c))
2119 else if (isl_int_is_negone(c))
2120 p = isl_printer_print_str(p, "-");
2121 else {
2122 p = isl_printer_print_isl_int(p, c);
2123 p = isl_printer_print_str(p, "*");
2125 type = pos2type(space, &pos);
2126 p = print_name_c(p, space, bset, type, pos);
2127 return p;
2130 static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
2131 __isl_keep isl_space *dim,
2132 __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
2134 int i;
2135 int first;
2137 for (i = 0, first = 1; i < len; ++i) {
2138 int flip = 0;
2139 if (isl_int_is_zero(c[i]))
2140 continue;
2141 if (!first) {
2142 if (isl_int_is_neg(c[i])) {
2143 flip = 1;
2144 isl_int_neg(c[i], c[i]);
2145 p = isl_printer_print_str(p, " - ");
2146 } else
2147 p = isl_printer_print_str(p, " + ");
2149 first = 0;
2150 p = print_term_c(p, dim, bset, c[i], i);
2151 if (flip)
2152 isl_int_neg(c[i], c[i]);
2154 if (first)
2155 p = isl_printer_print_str(p, "0");
2156 return p;
2159 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
2160 __isl_keep isl_space *space, __isl_keep isl_basic_set *bset, isl_int *c)
2162 unsigned len = 1 + isl_basic_set_total_dim(bset);
2163 return print_partial_affine_c(p, space, bset, c, len);
2166 /* We skip the constraint if it is implied by the div expression.
2168 * *first indicates whether this is the first constraint in the conjunction and
2169 * is updated if the constraint is actually printed.
2171 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
2172 __isl_keep isl_space *dim,
2173 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int *first)
2175 unsigned o_div;
2176 unsigned n_div;
2177 int div;
2179 o_div = isl_basic_set_offset(bset, isl_dim_div);
2180 n_div = isl_basic_set_dim(bset, isl_dim_div);
2181 div = isl_seq_last_non_zero(c + o_div, n_div);
2182 if (div >= 0) {
2183 isl_bool is_div = isl_basic_set_is_div_constraint(bset, c, div);
2184 if (is_div < 0)
2185 return isl_printer_free(p);
2186 if (is_div)
2187 return p;
2190 if (!*first)
2191 p = isl_printer_print_str(p, " && ");
2193 p = print_affine_c(p, dim, bset, c);
2194 p = isl_printer_print_str(p, " ");
2195 p = isl_printer_print_str(p, op);
2196 p = isl_printer_print_str(p, " 0");
2198 *first = 0;
2200 return p;
2203 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
2204 __isl_keep isl_space *space, __isl_keep isl_basic_set *bset)
2206 int i, j;
2207 int first = 1;
2208 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
2209 unsigned total = isl_basic_set_total_dim(bset) - n_div;
2211 for (i = 0; i < bset->n_eq; ++i) {
2212 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
2213 if (j < 0)
2214 p = print_constraint_c(p, space, bset,
2215 bset->eq[i], "==", &first);
2216 else {
2217 if (i)
2218 p = isl_printer_print_str(p, " && ");
2219 p = isl_printer_print_str(p, "(");
2220 p = print_partial_affine_c(p, space, bset, bset->eq[i],
2221 1 + total + j);
2222 p = isl_printer_print_str(p, ") % ");
2223 p = isl_printer_print_isl_int(p,
2224 bset->eq[i][1 + total + j]);
2225 p = isl_printer_print_str(p, " == 0");
2226 first = 0;
2229 for (i = 0; i < bset->n_ineq; ++i)
2230 p = print_constraint_c(p, space, bset, bset->ineq[i], ">=",
2231 &first);
2232 return p;
2235 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
2236 __isl_keep isl_space *space, __isl_keep isl_set *set)
2238 int i;
2240 if (!set)
2241 return isl_printer_free(p);
2243 if (set->n == 0)
2244 p = isl_printer_print_str(p, "0");
2246 for (i = 0; i < set->n; ++i) {
2247 if (i)
2248 p = isl_printer_print_str(p, " || ");
2249 if (set->n > 1)
2250 p = isl_printer_print_str(p, "(");
2251 p = print_basic_set_c(p, space, set->p[i]);
2252 if (set->n > 1)
2253 p = isl_printer_print_str(p, ")");
2255 return p;
2258 /* Print the piecewise quasi-polynomial "pwqp" to "p" in C format.
2260 static __isl_give isl_printer *print_pw_qpolynomial_c(
2261 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2263 int i;
2264 isl_space *space;
2266 space = isl_pw_qpolynomial_get_domain_space(pwqp);
2267 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set)) {
2268 p = print_qpolynomial_c(p, space, pwqp->p[0].qp);
2269 isl_space_free(space);
2270 return p;
2273 for (i = 0; i < pwqp->n; ++i) {
2274 p = isl_printer_print_str(p, "(");
2275 p = print_set_c(p, space, pwqp->p[i].set);
2276 p = isl_printer_print_str(p, ") ? (");
2277 p = print_qpolynomial_c(p, space, pwqp->p[i].qp);
2278 p = isl_printer_print_str(p, ") : ");
2281 isl_space_free(space);
2282 p = isl_printer_print_str(p, "0");
2283 return p;
2286 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
2287 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2289 if (!p || !pwqp)
2290 goto error;
2292 if (p->output_format == ISL_FORMAT_ISL)
2293 return print_pw_qpolynomial_isl(p, pwqp);
2294 else if (p->output_format == ISL_FORMAT_C)
2295 return print_pw_qpolynomial_c(p, pwqp);
2296 isl_assert(p->ctx, 0, goto error);
2297 error:
2298 isl_printer_free(p);
2299 return NULL;
2302 static isl_stat print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
2304 struct isl_union_print_data *data;
2305 data = (struct isl_union_print_data *)user;
2307 if (!data->first)
2308 data->p = isl_printer_print_str(data->p, "; ");
2309 data->first = 0;
2311 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
2312 isl_pw_qpolynomial_free(pwqp);
2314 return isl_stat_ok;
2317 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
2318 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2320 struct isl_union_print_data data;
2321 struct isl_print_space_data space_data = { 0 };
2322 isl_space *space;
2324 space = isl_union_pw_qpolynomial_get_space(upwqp);
2325 p = print_param_tuple(p, space, &space_data);
2326 isl_space_free(space);
2327 p = isl_printer_print_str(p, "{ ");
2328 data.p = p;
2329 data.first = 1;
2330 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
2331 &data);
2332 p = data.p;
2333 p = isl_printer_print_str(p, " }");
2334 return p;
2337 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
2338 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2340 if (!p || !upwqp)
2341 goto error;
2343 if (p->output_format == ISL_FORMAT_ISL)
2344 return print_union_pw_qpolynomial_isl(p, upwqp);
2345 isl_die(p->ctx, isl_error_invalid,
2346 "invalid output format for isl_union_pw_qpolynomial",
2347 goto error);
2348 error:
2349 isl_printer_free(p);
2350 return NULL;
2353 /* Print the quasi-polynomial reduction "fold" to "p" in C format,
2354 * with the variable names taken from the domain space "space".
2356 static __isl_give isl_printer *print_qpolynomial_fold_c(
2357 __isl_take isl_printer *p, __isl_keep isl_space *space,
2358 __isl_keep isl_qpolynomial_fold *fold)
2360 int i;
2362 for (i = 0; i < fold->n - 1; ++i)
2363 if (fold->type == isl_fold_min)
2364 p = isl_printer_print_str(p, "min(");
2365 else if (fold->type == isl_fold_max)
2366 p = isl_printer_print_str(p, "max(");
2368 for (i = 0; i < fold->n; ++i) {
2369 if (i)
2370 p = isl_printer_print_str(p, ", ");
2371 p = print_qpolynomial_c(p, space, fold->qp[i]);
2372 if (i)
2373 p = isl_printer_print_str(p, ")");
2375 return p;
2378 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
2379 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
2381 if (!p || !fold)
2382 goto error;
2383 if (p->output_format == ISL_FORMAT_ISL)
2384 return qpolynomial_fold_print(fold, p);
2385 else if (p->output_format == ISL_FORMAT_C)
2386 return print_qpolynomial_fold_c(p, fold->dim, fold);
2387 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2388 goto error);
2389 error:
2390 isl_printer_free(p);
2391 return NULL;
2394 /* Print the piecewise quasi-polynomial reduction "pwf" to "p" in C format.
2396 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
2397 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2399 int i;
2400 isl_space *space;
2402 space = isl_pw_qpolynomial_fold_get_domain_space(pwf);
2403 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set)) {
2404 p = print_qpolynomial_fold_c(p, space, pwf->p[0].fold);
2405 isl_space_free(space);
2406 return p;
2409 for (i = 0; i < pwf->n; ++i) {
2410 p = isl_printer_print_str(p, "(");
2411 p = print_set_c(p, space, pwf->p[i].set);
2412 p = isl_printer_print_str(p, ") ? (");
2413 p = print_qpolynomial_fold_c(p, space, pwf->p[i].fold);
2414 p = isl_printer_print_str(p, ") : ");
2417 isl_space_free(space);
2418 p = isl_printer_print_str(p, "0");
2419 return p;
2422 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
2423 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2425 if (!p || !pwf)
2426 goto error;
2428 if (p->output_format == ISL_FORMAT_ISL)
2429 return print_pw_qpolynomial_fold_isl(p, pwf);
2430 else if (p->output_format == ISL_FORMAT_C)
2431 return print_pw_qpolynomial_fold_c(p, pwf);
2432 isl_assert(p->ctx, 0, goto error);
2433 error:
2434 isl_printer_free(p);
2435 return NULL;
2438 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
2439 FILE *out, unsigned output_format)
2441 isl_printer *p;
2443 if (!pwf)
2444 return;
2446 p = isl_printer_to_file(pwf->dim->ctx, out);
2447 p = isl_printer_set_output_format(p, output_format);
2448 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
2450 isl_printer_free(p);
2453 static isl_stat print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf,
2454 void *user)
2456 struct isl_union_print_data *data;
2457 data = (struct isl_union_print_data *)user;
2459 if (!data->first)
2460 data->p = isl_printer_print_str(data->p, "; ");
2461 data->first = 0;
2463 data->p = isl_pwf_print_isl_body(data->p, pwf);
2464 isl_pw_qpolynomial_fold_free(pwf);
2466 return isl_stat_ok;
2469 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
2470 __isl_take isl_printer *p,
2471 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2473 struct isl_union_print_data data;
2474 struct isl_print_space_data space_data = { 0 };
2475 isl_space *space;
2477 space = isl_union_pw_qpolynomial_fold_get_space(upwf);
2478 p = print_param_tuple(p, space, &space_data);
2479 isl_space_free(space);
2480 p = isl_printer_print_str(p, "{ ");
2481 data.p = p;
2482 data.first = 1;
2483 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
2484 &print_pwf_body, &data);
2485 p = data.p;
2486 p = isl_printer_print_str(p, " }");
2487 return p;
2490 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2491 __isl_take isl_printer *p,
2492 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2494 if (!p || !upwf)
2495 goto error;
2497 if (p->output_format == ISL_FORMAT_ISL)
2498 return print_union_pw_qpolynomial_fold_isl(p, upwf);
2499 isl_die(p->ctx, isl_error_invalid,
2500 "invalid output format for isl_union_pw_qpolynomial_fold",
2501 goto error);
2502 error:
2503 isl_printer_free(p);
2504 return NULL;
2507 /* Print the isl_constraint "c" to "p".
2509 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
2510 __isl_keep isl_constraint *c)
2512 struct isl_print_space_data data = { 0 };
2513 isl_local_space *ls;
2514 isl_space *space;
2515 isl_bool exists;
2517 if (!p || !c)
2518 goto error;
2520 ls = isl_constraint_get_local_space(c);
2521 if (!ls)
2522 return isl_printer_free(p);
2523 space = isl_local_space_get_space(ls);
2524 p = print_param_tuple(p, space, &data);
2525 p = isl_printer_print_str(p, "{ ");
2526 p = isl_print_space(space, p, 0, &data);
2527 p = isl_printer_print_str(p, " : ");
2528 exists = need_exists(p, ls->div);
2529 if (exists < 0)
2530 p = isl_printer_free(p);
2531 if (exists >= 0 && exists)
2532 p = open_exists(p, space, ls->div, 0);
2533 p = print_affine_of_len(space, ls->div, p, c->v->el, c->v->size);
2534 if (isl_constraint_is_equality(c))
2535 p = isl_printer_print_str(p, " = 0");
2536 else
2537 p = isl_printer_print_str(p, " >= 0");
2538 if (exists >= 0 && exists)
2539 p = isl_printer_print_str(p, s_close_exists[0]);
2540 p = isl_printer_print_str(p, " }");
2541 isl_space_free(space);
2542 isl_local_space_free(ls);
2544 return p;
2545 error:
2546 isl_printer_free(p);
2547 return NULL;
2550 static __isl_give isl_printer *isl_printer_print_space_isl(
2551 __isl_take isl_printer *p, __isl_keep isl_space *space)
2553 struct isl_print_space_data data = { 0 };
2555 if (!space)
2556 goto error;
2558 p = print_param_tuple(p, space, &data);
2560 p = isl_printer_print_str(p, "{ ");
2561 if (isl_space_is_params(space))
2562 p = isl_printer_print_str(p, s_such_that[0]);
2563 else
2564 p = isl_print_space(space, p, 0, &data);
2565 p = isl_printer_print_str(p, " }");
2567 return p;
2568 error:
2569 isl_printer_free(p);
2570 return NULL;
2573 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
2574 __isl_keep isl_space *space)
2576 if (!p || !space)
2577 return isl_printer_free(p);
2578 if (p->output_format == ISL_FORMAT_ISL)
2579 return isl_printer_print_space_isl(p, space);
2580 else if (p->output_format == ISL_FORMAT_OMEGA)
2581 return print_omega_parameters(space, p);
2583 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
2584 "output format not supported for space",
2585 return isl_printer_free(p));
2588 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
2589 __isl_keep isl_local_space *ls)
2591 struct isl_print_space_data data = { 0 };
2592 unsigned n_div;
2594 if (!ls)
2595 goto error;
2597 p = print_param_tuple(p, ls->dim, &data);
2598 p = isl_printer_print_str(p, "{ ");
2599 p = isl_print_space(ls->dim, p, 0, &data);
2600 n_div = isl_local_space_dim(ls, isl_dim_div);
2601 if (n_div > 0) {
2602 p = isl_printer_print_str(p, " : ");
2603 p = isl_printer_print_str(p, s_open_exists[0]);
2604 p = print_div_list(p, ls->dim, ls->div, 0, 1);
2605 p = isl_printer_print_str(p, s_close_exists[0]);
2606 } else if (isl_space_is_params(ls->dim))
2607 p = isl_printer_print_str(p, s_such_that[0]);
2608 p = isl_printer_print_str(p, " }");
2609 return p;
2610 error:
2611 isl_printer_free(p);
2612 return NULL;
2615 /* Print the (potentially rational) affine expression "aff" to "p",
2616 * with the variable names taken from "space".
2618 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2619 __isl_keep isl_space *space, __isl_keep isl_aff *aff)
2621 unsigned total;
2623 if (isl_aff_is_nan(aff))
2624 return isl_printer_print_str(p, "NaN");
2626 total = isl_local_space_dim(aff->ls, isl_dim_all);
2627 p = isl_printer_print_str(p, "(");
2628 p = print_affine_of_len(space, aff->ls->div, p,
2629 aff->v->el + 1, 1 + total);
2630 if (isl_int_is_one(aff->v->el[0]))
2631 p = isl_printer_print_str(p, ")");
2632 else {
2633 p = isl_printer_print_str(p, ")/");
2634 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2637 return p;
2640 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2641 __isl_keep isl_aff *aff)
2643 struct isl_print_space_data data = { 0 };
2645 if (isl_space_is_params(aff->ls->dim))
2647 else {
2648 p = print_tuple(aff->ls->dim, p, isl_dim_set, &data);
2649 p = isl_printer_print_str(p, " -> ");
2651 p = isl_printer_print_str(p, "[");
2652 p = print_aff_body(p, aff->ls->dim, aff);
2653 p = isl_printer_print_str(p, "]");
2655 return p;
2658 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2659 __isl_keep isl_aff *aff)
2661 struct isl_print_space_data data = { 0 };
2663 if (!aff)
2664 goto error;
2666 p = print_param_tuple(p, aff->ls->dim, &data);
2667 p = isl_printer_print_str(p, "{ ");
2668 p = print_aff(p, aff);
2669 p = isl_printer_print_str(p, " }");
2670 return p;
2671 error:
2672 isl_printer_free(p);
2673 return NULL;
2676 /* Print the body of an isl_pw_aff, i.e., a semicolon delimited
2677 * sequence of affine expressions, each followed by constraints.
2679 static __isl_give isl_printer *print_pw_aff_body(
2680 __isl_take isl_printer *p, __isl_keep isl_pw_aff *pa)
2682 int i;
2684 if (!pa)
2685 return isl_printer_free(p);
2687 for (i = 0; i < pa->n; ++i) {
2688 isl_space *space;
2690 if (i)
2691 p = isl_printer_print_str(p, "; ");
2692 p = print_aff(p, pa->p[i].aff);
2693 space = isl_aff_get_domain_space(pa->p[i].aff);
2694 p = print_disjuncts(set_to_map(pa->p[i].set), space, p, 0);
2695 isl_space_free(space);
2698 return p;
2701 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2702 __isl_keep isl_pw_aff *pwaff)
2704 struct isl_print_space_data data = { 0 };
2706 if (!pwaff)
2707 goto error;
2709 p = print_param_tuple(p, pwaff->dim, &data);
2710 p = isl_printer_print_str(p, "{ ");
2711 p = print_pw_aff_body(p, pwaff);
2712 p = isl_printer_print_str(p, " }");
2713 return p;
2714 error:
2715 isl_printer_free(p);
2716 return NULL;
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 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2723 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2725 if (type == isl_dim_div) {
2726 p = isl_printer_print_str(p, "floord(");
2727 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2728 p = isl_printer_print_str(p, ", ");
2729 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2730 p = isl_printer_print_str(p, ")");
2731 } else {
2732 const char *name;
2734 name = isl_space_get_dim_name(ls->dim, type, pos);
2735 if (!name)
2736 name = "UNNAMED";
2737 p = isl_printer_print_str(p, name);
2739 return p;
2742 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2743 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2745 enum isl_dim_type type;
2747 if (!p || !ls)
2748 return isl_printer_free(p);
2750 if (pos == 0)
2751 return isl_printer_print_isl_int(p, c);
2753 if (isl_int_is_one(c))
2755 else if (isl_int_is_negone(c))
2756 p = isl_printer_print_str(p, "-");
2757 else {
2758 p = isl_printer_print_isl_int(p, c);
2759 p = isl_printer_print_str(p, "*");
2761 type = pos2type(ls->dim, &pos);
2762 p = print_ls_name_c(p, ls, type, pos);
2763 return p;
2766 static __isl_give isl_printer *print_ls_partial_affine_c(
2767 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2768 isl_int *c, unsigned len)
2770 int i;
2771 int first;
2773 for (i = 0, first = 1; i < len; ++i) {
2774 int flip = 0;
2775 if (isl_int_is_zero(c[i]))
2776 continue;
2777 if (!first) {
2778 if (isl_int_is_neg(c[i])) {
2779 flip = 1;
2780 isl_int_neg(c[i], c[i]);
2781 p = isl_printer_print_str(p, " - ");
2782 } else
2783 p = isl_printer_print_str(p, " + ");
2785 first = 0;
2786 p = print_ls_term_c(p, ls, c[i], i);
2787 if (flip)
2788 isl_int_neg(c[i], c[i]);
2790 if (first)
2791 p = isl_printer_print_str(p, "0");
2792 return p;
2795 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2796 __isl_keep isl_local_space *ls, isl_int *c)
2798 unsigned len = 1 + isl_local_space_dim(ls, isl_dim_all);
2799 return print_ls_partial_affine_c(p, ls, c, len);
2802 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2803 __isl_keep isl_aff *aff)
2805 unsigned total;
2807 total = isl_local_space_dim(aff->ls, isl_dim_all);
2808 if (!isl_int_is_one(aff->v->el[0]))
2809 p = isl_printer_print_str(p, "(");
2810 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2811 if (!isl_int_is_one(aff->v->el[0])) {
2812 p = isl_printer_print_str(p, ")/");
2813 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2815 return p;
2818 /* In the C format, we cannot express that "pwaff" may be undefined
2819 * on parts of the domain space. We therefore assume that the expression
2820 * will only be evaluated on its definition domain and compute the gist
2821 * of each cell with respect to this domain.
2823 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2824 __isl_keep isl_pw_aff *pwaff)
2826 isl_set *domain;
2827 isl_ast_build *build;
2828 isl_ast_expr *expr;
2830 if (pwaff->n < 1)
2831 isl_die(p->ctx, isl_error_unsupported,
2832 "cannot print empty isl_pw_aff in C format",
2833 return isl_printer_free(p));
2835 domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
2836 build = isl_ast_build_from_context(domain);
2837 expr = isl_ast_build_expr_from_pw_aff(build, isl_pw_aff_copy(pwaff));
2838 p = isl_printer_print_ast_expr(p, expr);
2839 isl_ast_expr_free(expr);
2840 isl_ast_build_free(build);
2842 return p;
2845 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2846 __isl_keep isl_aff *aff)
2848 if (!p || !aff)
2849 goto error;
2851 if (p->output_format == ISL_FORMAT_ISL)
2852 return print_aff_isl(p, aff);
2853 else if (p->output_format == ISL_FORMAT_C)
2854 return print_aff_c(p, aff);
2855 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2856 goto error);
2857 error:
2858 isl_printer_free(p);
2859 return NULL;
2862 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2863 __isl_keep isl_pw_aff *pwaff)
2865 if (!p || !pwaff)
2866 goto error;
2868 if (p->output_format == ISL_FORMAT_ISL)
2869 return print_pw_aff_isl(p, pwaff);
2870 else if (p->output_format == ISL_FORMAT_C)
2871 return print_pw_aff_c(p, pwaff);
2872 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2873 goto error);
2874 error:
2875 isl_printer_free(p);
2876 return NULL;
2879 /* Print "pa" in a sequence of isl_pw_affs delimited by semicolons.
2880 * Each isl_pw_aff itself is also printed as semicolon delimited
2881 * sequence of pieces.
2882 * If data->first = 1, then this is the first in the sequence.
2883 * Update data->first to tell the next element that it is not the first.
2885 static isl_stat print_pw_aff_body_wrap(__isl_take isl_pw_aff *pa,
2886 void *user)
2888 struct isl_union_print_data *data;
2889 data = (struct isl_union_print_data *) user;
2891 if (!data->first)
2892 data->p = isl_printer_print_str(data->p, "; ");
2893 data->first = 0;
2895 data->p = print_pw_aff_body(data->p, pa);
2896 isl_pw_aff_free(pa);
2898 return data->p ? isl_stat_ok : isl_stat_error;
2901 /* Print the body of an isl_union_pw_aff, i.e., a semicolon delimited
2902 * sequence of affine expressions, each followed by constraints,
2903 * with the sequence enclosed in braces.
2905 static __isl_give isl_printer *print_union_pw_aff_body(
2906 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2908 struct isl_union_print_data data = { p, 1 };
2910 p = isl_printer_print_str(p, s_open_set[0]);
2911 data.p = p;
2912 if (isl_union_pw_aff_foreach_pw_aff(upa,
2913 &print_pw_aff_body_wrap, &data) < 0)
2914 data.p = isl_printer_free(p);
2915 p = data.p;
2916 p = isl_printer_print_str(p, s_close_set[0]);
2918 return p;
2921 /* Print the isl_union_pw_aff "upa" to "p" in isl format.
2923 * The individual isl_pw_affs are delimited by a semicolon.
2925 static __isl_give isl_printer *print_union_pw_aff_isl(
2926 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2928 struct isl_print_space_data data = { 0 };
2929 isl_space *space;
2931 space = isl_union_pw_aff_get_space(upa);
2932 p = print_param_tuple(p, space, &data);
2933 isl_space_free(space);
2934 p = print_union_pw_aff_body(p, upa);
2935 return p;
2938 /* Print the isl_union_pw_aff "upa" to "p".
2940 * We currently only support an isl format.
2942 __isl_give isl_printer *isl_printer_print_union_pw_aff(
2943 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2945 if (!p || !upa)
2946 return isl_printer_free(p);
2948 if (p->output_format == ISL_FORMAT_ISL)
2949 return print_union_pw_aff_isl(p, upa);
2950 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2951 "unsupported output format", return isl_printer_free(p));
2954 /* Print dimension "pos" of data->space to "p".
2956 * data->user is assumed to be an isl_multi_aff.
2958 * If the current dimension is an output dimension, then print
2959 * the corresponding expression. Otherwise, print the name of the dimension.
2961 static __isl_give isl_printer *print_dim_ma(__isl_take isl_printer *p,
2962 struct isl_print_space_data *data, unsigned pos)
2964 isl_multi_aff *ma = data->user;
2966 if (data->type == isl_dim_out) {
2967 isl_space *space;
2969 space = isl_multi_aff_get_domain_space(ma);
2970 p = print_aff_body(p, space, ma->u.p[pos]);
2971 isl_space_free(space);
2972 } else {
2973 p = print_name(data->space, p, data->type, pos, data->latex);
2976 return p;
2979 static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
2980 __isl_keep isl_multi_aff *maff)
2982 struct isl_print_space_data data = { 0 };
2984 data.print_dim = &print_dim_ma;
2985 data.user = maff;
2986 return isl_print_space(maff->space, p, 0, &data);
2989 static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
2990 __isl_keep isl_multi_aff *maff)
2992 struct isl_print_space_data data = { 0 };
2994 if (!maff)
2995 goto error;
2997 p = print_param_tuple(p, maff->space, &data);
2998 p = isl_printer_print_str(p, "{ ");
2999 p = print_multi_aff(p, maff);
3000 p = isl_printer_print_str(p, " }");
3001 return p;
3002 error:
3003 isl_printer_free(p);
3004 return NULL;
3007 __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
3008 __isl_keep isl_multi_aff *maff)
3010 if (!p || !maff)
3011 goto error;
3013 if (p->output_format == ISL_FORMAT_ISL)
3014 return print_multi_aff_isl(p, maff);
3015 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3016 goto error);
3017 error:
3018 isl_printer_free(p);
3019 return NULL;
3022 static __isl_give isl_printer *print_pw_multi_aff_body(
3023 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3025 int i;
3027 if (!pma)
3028 goto error;
3030 for (i = 0; i < pma->n; ++i) {
3031 isl_space *space;
3033 if (i)
3034 p = isl_printer_print_str(p, "; ");
3035 p = print_multi_aff(p, pma->p[i].maff);
3036 space = isl_multi_aff_get_domain_space(pma->p[i].maff);
3037 p = print_disjuncts(set_to_map(pma->p[i].set), space, p, 0);
3038 isl_space_free(space);
3040 return p;
3041 error:
3042 isl_printer_free(p);
3043 return NULL;
3046 static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
3047 __isl_keep isl_pw_multi_aff *pma)
3049 struct isl_print_space_data data = { 0 };
3051 if (!pma)
3052 goto error;
3054 p = print_param_tuple(p, pma->dim, &data);
3055 p = isl_printer_print_str(p, "{ ");
3056 p = print_pw_multi_aff_body(p, pma);
3057 p = isl_printer_print_str(p, " }");
3058 return p;
3059 error:
3060 isl_printer_free(p);
3061 return NULL;
3064 /* Print the unnamed, single-dimensional piecewise multi affine expression "pma"
3065 * to "p".
3067 static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
3068 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3070 int i;
3071 isl_space *space;
3073 space = isl_pw_multi_aff_get_domain_space(pma);
3074 for (i = 0; i < pma->n - 1; ++i) {
3075 p = isl_printer_print_str(p, "(");
3076 p = print_set_c(p, space, pma->p[i].set);
3077 p = isl_printer_print_str(p, ") ? (");
3078 p = print_aff_c(p, pma->p[i].maff->u.p[0]);
3079 p = isl_printer_print_str(p, ") : ");
3081 isl_space_free(space);
3083 return print_aff_c(p, pma->p[pma->n - 1].maff->u.p[0]);
3086 static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
3087 __isl_keep isl_pw_multi_aff *pma)
3089 int n;
3090 const char *name;
3092 if (!pma)
3093 goto error;
3094 if (pma->n < 1)
3095 isl_die(p->ctx, isl_error_unsupported,
3096 "cannot print empty isl_pw_multi_aff in C format",
3097 goto error);
3098 name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
3099 if (!name && isl_pw_multi_aff_dim(pma, isl_dim_out) == 1)
3100 return print_unnamed_pw_multi_aff_c(p, pma);
3101 if (!name)
3102 isl_die(p->ctx, isl_error_unsupported,
3103 "cannot print unnamed isl_pw_multi_aff in C format",
3104 goto error);
3106 p = isl_printer_print_str(p, name);
3107 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
3108 if (n != 0)
3109 isl_die(p->ctx, isl_error_unsupported,
3110 "not supported yet", goto error);
3112 return p;
3113 error:
3114 isl_printer_free(p);
3115 return NULL;
3118 __isl_give isl_printer *isl_printer_print_pw_multi_aff(
3119 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3121 if (!p || !pma)
3122 goto error;
3124 if (p->output_format == ISL_FORMAT_ISL)
3125 return print_pw_multi_aff_isl(p, pma);
3126 if (p->output_format == ISL_FORMAT_C)
3127 return print_pw_multi_aff_c(p, pma);
3128 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3129 goto error);
3130 error:
3131 isl_printer_free(p);
3132 return NULL;
3135 static isl_stat print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
3136 void *user)
3138 struct isl_union_print_data *data;
3139 data = (struct isl_union_print_data *) user;
3141 if (!data->first)
3142 data->p = isl_printer_print_str(data->p, "; ");
3143 data->first = 0;
3145 data->p = print_pw_multi_aff_body(data->p, pma);
3146 isl_pw_multi_aff_free(pma);
3148 return isl_stat_ok;
3151 static __isl_give isl_printer *print_union_pw_multi_aff_isl(
3152 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
3154 struct isl_union_print_data data;
3155 struct isl_print_space_data space_data = { 0 };
3156 isl_space *space;
3158 space = isl_union_pw_multi_aff_get_space(upma);
3159 p = print_param_tuple(p, space, &space_data);
3160 isl_space_free(space);
3161 p = isl_printer_print_str(p, s_open_set[0]);
3162 data.p = p;
3163 data.first = 1;
3164 isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3165 &print_pw_multi_aff_body_wrap, &data);
3166 p = data.p;
3167 p = isl_printer_print_str(p, s_close_set[0]);
3168 return p;
3171 __isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
3172 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
3174 if (!p || !upma)
3175 goto error;
3177 if (p->output_format == ISL_FORMAT_ISL)
3178 return print_union_pw_multi_aff_isl(p, upma);
3179 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3180 goto error);
3181 error:
3182 isl_printer_free(p);
3183 return NULL;
3186 /* Print dimension "pos" of data->space to "p".
3188 * data->user is assumed to be an isl_multi_pw_aff.
3190 * If the current dimension is an output dimension, then print
3191 * the corresponding piecewise affine expression.
3192 * Otherwise, print the name of the dimension.
3194 static __isl_give isl_printer *print_dim_mpa(__isl_take isl_printer *p,
3195 struct isl_print_space_data *data, unsigned pos)
3197 int i;
3198 int need_parens;
3199 isl_space *space;
3200 isl_multi_pw_aff *mpa = data->user;
3201 isl_pw_aff *pa;
3203 if (data->type != isl_dim_out)
3204 return print_name(data->space, p, data->type, pos, data->latex);
3206 pa = mpa->u.p[pos];
3207 if (pa->n == 0)
3208 return isl_printer_print_str(p, "(0 : false)");
3210 need_parens = pa->n != 1 || !isl_set_plain_is_universe(pa->p[0].set);
3211 if (need_parens)
3212 p = isl_printer_print_str(p, "(");
3213 space = isl_multi_pw_aff_get_domain_space(mpa);
3214 for (i = 0; i < pa->n; ++i) {
3216 if (i)
3217 p = isl_printer_print_str(p, "; ");
3218 p = print_aff_body(p, space, pa->p[i].aff);
3219 p = print_disjuncts(pa->p[i].set, space, p, 0);
3221 isl_space_free(space);
3222 if (need_parens)
3223 p = isl_printer_print_str(p, ")");
3225 return p;
3228 /* Print "mpa" to "p" in isl format.
3230 * If "mpa" is zero-dimensional and has a non-trivial explicit domain,
3231 * then it is printed after the tuple of affine expressions.
3233 static __isl_give isl_printer *print_multi_pw_aff_isl(__isl_take isl_printer *p,
3234 __isl_keep isl_multi_pw_aff *mpa)
3236 struct isl_print_space_data data = { 0 };
3237 isl_bool has_domain;
3239 if (!mpa)
3240 return isl_printer_free(p);
3242 p = print_param_tuple(p, mpa->space, &data);
3243 p = isl_printer_print_str(p, "{ ");
3244 data.print_dim = &print_dim_mpa;
3245 data.user = mpa;
3246 p = isl_print_space(mpa->space, p, 0, &data);
3247 has_domain = isl_multi_pw_aff_has_non_trivial_domain(mpa);
3248 if (has_domain < 0)
3249 return isl_printer_free(p);
3250 if (has_domain) {
3251 isl_space *space;
3253 space = isl_space_domain(isl_space_copy(mpa->space));
3254 p = print_disjuncts_set(mpa->u.dom, space, p, 0);
3255 isl_space_free(space);
3257 p = isl_printer_print_str(p, " }");
3258 return p;
3261 __isl_give isl_printer *isl_printer_print_multi_pw_aff(
3262 __isl_take isl_printer *p, __isl_keep isl_multi_pw_aff *mpa)
3264 if (!p || !mpa)
3265 return isl_printer_free(p);
3267 if (p->output_format == ISL_FORMAT_ISL)
3268 return print_multi_pw_aff_isl(p, mpa);
3269 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3270 return isl_printer_free(p));
3273 /* Print dimension "pos" of data->space to "p".
3275 * data->user is assumed to be an isl_multi_val.
3277 * If the current dimension is an output dimension, then print
3278 * the corresponding value. Otherwise, print the name of the dimension.
3280 static __isl_give isl_printer *print_dim_mv(__isl_take isl_printer *p,
3281 struct isl_print_space_data *data, unsigned pos)
3283 isl_multi_val *mv = data->user;
3285 if (data->type == isl_dim_out)
3286 return isl_printer_print_val(p, mv->u.p[pos]);
3287 else
3288 return print_name(data->space, p, data->type, pos, data->latex);
3291 /* Print the isl_multi_val "mv" to "p" in isl format.
3293 static __isl_give isl_printer *print_multi_val_isl(__isl_take isl_printer *p,
3294 __isl_keep isl_multi_val *mv)
3296 struct isl_print_space_data data = { 0 };
3298 if (!mv)
3299 return isl_printer_free(p);
3301 p = print_param_tuple(p, mv->space, &data);
3302 p = isl_printer_print_str(p, "{ ");
3303 data.print_dim = &print_dim_mv;
3304 data.user = mv;
3305 p = isl_print_space(mv->space, p, 0, &data);
3306 p = isl_printer_print_str(p, " }");
3307 return p;
3310 /* Print the isl_multi_val "mv" to "p".
3312 * Currently only supported in isl format.
3314 __isl_give isl_printer *isl_printer_print_multi_val(
3315 __isl_take isl_printer *p, __isl_keep isl_multi_val *mv)
3317 if (!p || !mv)
3318 return isl_printer_free(p);
3320 if (p->output_format == ISL_FORMAT_ISL)
3321 return print_multi_val_isl(p, mv);
3322 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3323 return isl_printer_free(p));
3326 /* Print dimension "pos" of data->space to "p".
3328 * data->user is assumed to be an isl_multi_union_pw_aff.
3330 * The current dimension is necessarily a set dimension, so
3331 * we print the corresponding isl_union_pw_aff, including
3332 * the braces.
3334 static __isl_give isl_printer *print_union_pw_aff_dim(__isl_take isl_printer *p,
3335 struct isl_print_space_data *data, unsigned pos)
3337 isl_multi_union_pw_aff *mupa = data->user;
3338 isl_union_pw_aff *upa;
3340 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, pos);
3341 p = print_union_pw_aff_body(p, upa);
3342 isl_union_pw_aff_free(upa);
3344 return p;
3347 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3349 * If "mupa" is zero-dimensional and has a non-trivial explicit domain,
3350 * then it is printed after the tuple of affine expressions.
3351 * In order to clarify that this domain belongs to the expression,
3352 * the tuple along with the domain are placed inside parentheses.
3353 * If "mupa" has any parameters, then the opening parenthesis
3354 * appears after the parameter declarations.
3356 static __isl_give isl_printer *print_multi_union_pw_aff_isl(
3357 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3359 struct isl_print_space_data data = { 0 };
3360 isl_bool has_domain;
3361 isl_space *space;
3363 if (!mupa)
3364 return isl_printer_free(p);
3365 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
3366 if (has_domain < 0)
3367 return isl_printer_free(p);
3369 space = isl_multi_union_pw_aff_get_space(mupa);
3370 p = print_param_tuple(p, space, &data);
3372 if (has_domain)
3373 p = isl_printer_print_str(p, "(");
3375 data.print_dim = &print_union_pw_aff_dim;
3376 data.user = mupa;
3378 p = isl_print_space(space, p, 0, &data);
3379 isl_space_free(space);
3381 if (has_domain) {
3382 p = isl_printer_print_str(p, " : ");
3383 p = isl_printer_print_union_set_isl_body(p, mupa->u.dom);
3384 p = isl_printer_print_str(p, ")");
3387 return p;
3390 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3392 * We currently only support an isl format.
3394 __isl_give isl_printer *isl_printer_print_multi_union_pw_aff(
3395 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3397 if (!p || !mupa)
3398 return isl_printer_free(p);
3400 if (p->output_format == ISL_FORMAT_ISL)
3401 return print_multi_union_pw_aff_isl(p, mupa);
3402 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
3403 "unsupported output format", return isl_printer_free(p));