isl_map.c: remove_if_empty: fix type of variable
[isl.git] / isl_output.c
blob4888bae1ecb8ce4abaebc009a50b0e81ed0dfe6f
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <stdlib.h>
16 #include <string.h>
17 #include <isl_ctx_private.h>
18 #include <isl_map_private.h>
19 #include <isl/set.h>
20 #include <isl_seq.h>
21 #include <isl_polynomial_private.h>
22 #include <isl_printer_private.h>
23 #include <isl_space_private.h>
24 #include <isl_mat_private.h>
25 #include <isl_vec_private.h>
26 #include <isl/union_map.h>
27 #include <isl/constraint.h>
28 #include <isl_local_space_private.h>
29 #include <isl_aff_private.h>
30 #include <isl_val_private.h>
31 #include <isl_constraint_private.h>
32 #include <isl/ast_build.h>
33 #include <isl_sort.h>
34 #include <isl_output_private.h>
36 #include <bset_to_bmap.c>
37 #include <set_to_map.c>
38 #include <uset_to_umap.c>
40 static const char *s_to[2] = { " -> ", " \\to " };
41 static const char *s_and[2] = { " and ", " \\wedge " };
42 static const char *s_or[2] = { " or ", " \\vee " };
43 static const char *s_le[2] = { "<=", "\\le" };
44 static const char *s_ge[2] = { ">=", "\\ge" };
45 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
46 static const char *s_close_set[2] = { " }", " \\,\\}" };
47 static const char *s_open_list[2] = { "[", "(" };
48 static const char *s_close_list[2] = { "]", ")" };
49 static const char *s_such_that[2] = { " : ", " \\mid " };
50 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
51 static const char *s_close_exists[2] = { ")", "" };
52 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
53 static const char *s_mod[2] = { "mod", "\\bmod" };
54 static const char *s_param_prefix[2] = { "p", "p_" };
55 static const char *s_input_prefix[2] = { "i", "i_" };
56 static const char *s_output_prefix[2] = { "o", "o_" };
58 static __isl_give isl_printer *print_constraint_polylib(
59 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
61 int i;
62 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
63 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
64 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
65 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
67 p = isl_printer_start_line(p);
68 p = isl_printer_print_int(p, ineq);
69 for (i = 0; i < n_out; ++i) {
70 p = isl_printer_print_str(p, " ");
71 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
73 for (i = 0; i < n_in; ++i) {
74 p = isl_printer_print_str(p, " ");
75 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
77 for (i = 0; i < bmap->n_div; ++i) {
78 p = isl_printer_print_str(p, " ");
79 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
81 for (i = 0; i < nparam; ++i) {
82 p = isl_printer_print_str(p, " ");
83 p = isl_printer_print_isl_int(p, c[1+i]);
85 p = isl_printer_print_str(p, " ");
86 p = isl_printer_print_isl_int(p, c[0]);
87 p = isl_printer_end_line(p);
88 return p;
91 static __isl_give isl_printer *print_constraints_polylib(
92 struct isl_basic_map *bmap, __isl_take isl_printer *p)
94 int i;
96 p = isl_printer_set_isl_int_width(p, 5);
98 for (i = 0; i < bmap->n_eq; ++i)
99 p = print_constraint_polylib(bmap, 0, i, p);
100 for (i = 0; i < bmap->n_ineq; ++i)
101 p = print_constraint_polylib(bmap, 1, i, p);
103 return p;
106 static __isl_give isl_printer *bset_print_constraints_polylib(
107 struct isl_basic_set *bset, __isl_take isl_printer *p)
109 return print_constraints_polylib(bset_to_bmap(bset), p);
112 static __isl_give isl_printer *isl_basic_map_print_polylib(
113 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
115 unsigned total = isl_basic_map_total_dim(bmap);
116 p = isl_printer_start_line(p);
117 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
118 p = isl_printer_print_str(p, " ");
119 p = isl_printer_print_int(p, 1 + total + 1);
120 if (ext) {
121 p = isl_printer_print_str(p, " ");
122 p = isl_printer_print_int(p,
123 isl_basic_map_dim(bmap, isl_dim_out));
124 p = isl_printer_print_str(p, " ");
125 p = isl_printer_print_int(p,
126 isl_basic_map_dim(bmap, isl_dim_in));
127 p = isl_printer_print_str(p, " ");
128 p = isl_printer_print_int(p,
129 isl_basic_map_dim(bmap, isl_dim_div));
130 p = isl_printer_print_str(p, " ");
131 p = isl_printer_print_int(p,
132 isl_basic_map_dim(bmap, isl_dim_param));
134 p = isl_printer_end_line(p);
135 return print_constraints_polylib(bmap, p);
138 static __isl_give isl_printer *isl_basic_set_print_polylib(
139 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
141 return isl_basic_map_print_polylib(bset_to_bmap(bset), p, ext);
144 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
145 __isl_take isl_printer *p, int ext)
147 int i;
149 p = isl_printer_start_line(p);
150 p = isl_printer_print_int(p, map->n);
151 p = isl_printer_end_line(p);
152 for (i = 0; i < map->n; ++i) {
153 p = isl_printer_start_line(p);
154 p = isl_printer_end_line(p);
155 p = isl_basic_map_print_polylib(map->p[i], p, ext);
157 return p;
160 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
161 __isl_take isl_printer *p, int ext)
163 return isl_map_print_polylib(set_to_map(set), p, ext);
166 static int count_same_name(__isl_keep isl_space *dim,
167 enum isl_dim_type type, unsigned pos, const char *name)
169 enum isl_dim_type t;
170 unsigned p, s;
171 int count = 0;
173 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
174 s = t == type ? pos : isl_space_dim(dim, t);
175 for (p = 0; p < s; ++p) {
176 const char *n = isl_space_get_dim_name(dim, t, p);
177 if (n && !strcmp(n, name))
178 count++;
181 return count;
184 /* Print the name of the variable of type "type" and position "pos"
185 * in "space" to "p".
187 static __isl_give isl_printer *print_name(__isl_keep isl_space *space,
188 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
189 int latex)
191 const char *name;
192 char buffer[20];
193 int primes;
195 name = type == isl_dim_div ? NULL
196 : isl_space_get_dim_name(space, type, pos);
198 if (!name) {
199 const char *prefix;
200 if (type == isl_dim_param)
201 prefix = s_param_prefix[latex];
202 else if (type == isl_dim_div)
203 prefix = s_div_prefix[latex];
204 else if (isl_space_is_set(space) || type == isl_dim_in)
205 prefix = s_input_prefix[latex];
206 else
207 prefix = s_output_prefix[latex];
208 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
209 name = buffer;
211 primes = count_same_name(space, name == buffer ? isl_dim_div : type,
212 pos, name);
213 p = isl_printer_print_str(p, name);
214 while (primes-- > 0)
215 p = isl_printer_print_str(p, "'");
216 return p;
219 static enum isl_dim_type pos2type(__isl_keep isl_space *dim, unsigned *pos)
221 enum isl_dim_type type;
222 unsigned n_in = isl_space_dim(dim, isl_dim_in);
223 unsigned n_out = isl_space_dim(dim, isl_dim_out);
224 unsigned nparam = isl_space_dim(dim, isl_dim_param);
226 if (*pos < 1 + nparam) {
227 type = isl_dim_param;
228 *pos -= 1;
229 } else if (*pos < 1 + nparam + n_in) {
230 type = isl_dim_in;
231 *pos -= 1 + nparam;
232 } else if (*pos < 1 + nparam + n_in + n_out) {
233 type = isl_dim_out;
234 *pos -= 1 + nparam + n_in;
235 } else {
236 type = isl_dim_div;
237 *pos -= 1 + nparam + n_in + n_out;
240 return type;
243 /* Can the div expression of the integer division at position "row" of "div"
244 * be printed?
245 * In particular, are the div expressions available and does the selected
246 * variable have a known explicit representation?
247 * Furthermore, the Omega format does not allow any div expressions
248 * to be printed.
250 static isl_bool can_print_div_expr(__isl_keep isl_printer *p,
251 __isl_keep isl_mat *div, int pos)
253 if (p->output_format == ISL_FORMAT_OMEGA)
254 return isl_bool_false;
255 if (!div)
256 return isl_bool_false;
257 return !isl_int_is_zero(div->row[pos][0]);
260 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
261 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
263 static __isl_give isl_printer *print_term(__isl_keep isl_space *space,
264 __isl_keep isl_mat *div,
265 isl_int c, unsigned pos, __isl_take isl_printer *p, int latex)
267 enum isl_dim_type type;
268 int print_div_def;
270 if (pos == 0)
271 return isl_printer_print_isl_int(p, c);
273 type = pos2type(space, &pos);
274 print_div_def = type == isl_dim_div && can_print_div_expr(p, div, pos);
276 if (isl_int_is_one(c))
278 else if (isl_int_is_negone(c))
279 p = isl_printer_print_str(p, "-");
280 else {
281 p = isl_printer_print_isl_int(p, c);
282 if (p->output_format == ISL_FORMAT_C || print_div_def)
283 p = isl_printer_print_str(p, "*");
285 if (print_div_def)
286 p = print_div(space, div, pos, p);
287 else
288 p = print_name(space, p, type, pos, latex);
289 return p;
292 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_space *dim,
293 __isl_keep isl_mat *div,
294 __isl_take isl_printer *p, isl_int *c, int len)
296 int i;
297 int first;
299 for (i = 0, first = 1; i < len; ++i) {
300 int flip = 0;
301 if (isl_int_is_zero(c[i]))
302 continue;
303 if (!first) {
304 if (isl_int_is_neg(c[i])) {
305 flip = 1;
306 isl_int_neg(c[i], c[i]);
307 p = isl_printer_print_str(p, " - ");
308 } else
309 p = isl_printer_print_str(p, " + ");
311 first = 0;
312 p = print_term(dim, div, c[i], i, p, 0);
313 if (flip)
314 isl_int_neg(c[i], c[i]);
316 if (first)
317 p = isl_printer_print_str(p, "0");
318 return p;
321 /* Print an affine expression "c"
322 * to "p", with the variable names taken from "space" and
323 * the integer division definitions taken from "div".
325 static __isl_give isl_printer *print_affine(__isl_take isl_printer *p,
326 __isl_keep isl_space *space, __isl_keep isl_mat *div, isl_int *c)
328 unsigned n_div;
329 unsigned len;
331 if (!space || !div)
332 return isl_printer_free(p);
333 n_div = isl_mat_rows(div);
334 len = 1 + isl_space_dim(space, isl_dim_all) + n_div;
335 return print_affine_of_len(space, div, p, c, len);
338 /* offset is the offset of local_dim inside data->type of data->space.
340 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
341 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
342 struct isl_print_space_data *data, int offset)
344 int i;
346 if (data->space != local_dim && local_type == isl_dim_out)
347 offset += local_dim->n_in;
349 for (i = 0; i < isl_space_dim(local_dim, local_type); ++i) {
350 if (i)
351 p = isl_printer_print_str(p, ", ");
352 if (data->print_dim)
353 p = data->print_dim(p, data, offset + i);
354 else
355 p = print_name(data->space, p, data->type, offset + i,
356 data->latex);
358 return p;
361 static __isl_give isl_printer *print_var_list(__isl_take isl_printer *p,
362 __isl_keep isl_space *space, enum isl_dim_type type)
364 struct isl_print_space_data data = { .space = space, .type = type };
366 return print_nested_var_list(p, space, type, &data, 0);
369 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
370 __isl_keep isl_space *local_dim,
371 struct isl_print_space_data *data, int offset);
373 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
374 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
375 struct isl_print_space_data *data, int offset)
377 const char *name = NULL;
378 unsigned n = isl_space_dim(local_dim, local_type);
379 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
380 name = isl_space_get_tuple_name(local_dim, local_type);
381 if (name) {
382 if (data->latex)
383 p = isl_printer_print_str(p, "\\mathrm{");
384 p = isl_printer_print_str(p, name);
385 if (data->latex)
386 p = isl_printer_print_str(p, "}");
389 if (!data->latex || n != 1 || name)
390 p = isl_printer_print_str(p, s_open_list[data->latex]);
391 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
392 local_dim->nested[local_type - isl_dim_in]) {
393 if (data->space != local_dim && local_type == isl_dim_out)
394 offset += local_dim->n_in;
395 p = print_nested_map_dim(p,
396 local_dim->nested[local_type - isl_dim_in],
397 data, offset);
398 } else
399 p = print_nested_var_list(p, local_dim, local_type, data,
400 offset);
401 if (!data->latex || n != 1 || name)
402 p = isl_printer_print_str(p, s_close_list[data->latex]);
403 return p;
406 static __isl_give isl_printer *print_tuple(__isl_keep isl_space *dim,
407 __isl_take isl_printer *p, enum isl_dim_type type,
408 struct isl_print_space_data *data)
410 data->space = dim;
411 data->type = type;
412 return print_nested_tuple(p, dim, type, data, 0);
415 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
416 __isl_keep isl_space *local_dim,
417 struct isl_print_space_data *data, int offset)
419 p = print_nested_tuple(p, local_dim, isl_dim_in, data, offset);
420 p = isl_printer_print_str(p, s_to[data->latex]);
421 p = print_nested_tuple(p, local_dim, isl_dim_out, data, offset);
423 return p;
426 __isl_give isl_printer *isl_print_space(__isl_keep isl_space *space,
427 __isl_take isl_printer *p, int rational,
428 struct isl_print_space_data *data)
430 if (rational && !data->latex)
431 p = isl_printer_print_str(p, "rat: ");
432 if (isl_space_is_params(space))
434 else if (isl_space_is_set(space))
435 p = print_tuple(space, p, isl_dim_set, data);
436 else {
437 p = print_tuple(space, p, isl_dim_in, data);
438 p = isl_printer_print_str(p, s_to[data->latex]);
439 p = print_tuple(space, p, isl_dim_out, data);
442 return p;
445 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_space *dim,
446 __isl_take isl_printer *p)
448 if (isl_space_dim(dim, isl_dim_param) == 0)
449 return p;
451 p = isl_printer_start_line(p);
452 p = isl_printer_print_str(p, "symbolic ");
453 p = print_var_list(p, dim, isl_dim_param);
454 p = isl_printer_print_str(p, ";");
455 p = isl_printer_end_line(p);
456 return p;
459 /* Does the inequality constraint following "i" in "bmap"
460 * have an opposite value for the same last coefficient?
461 * "last" is the position of the last coefficient of inequality "i".
462 * If the next constraint is a div constraint, then it is ignored
463 * since div constraints are not printed.
465 static int next_is_opposite(__isl_keep isl_basic_map *bmap, int i, int last)
467 unsigned total = isl_basic_map_total_dim(bmap);
468 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
470 if (i + 1 >= bmap->n_ineq)
471 return 0;
472 if (isl_seq_last_non_zero(bmap->ineq[i + 1], 1 + total) != last)
473 return 0;
474 if (last >= o_div) {
475 isl_bool is_div;
476 is_div = isl_basic_map_is_div_constraint(bmap,
477 bmap->ineq[i + 1], last - o_div);
478 if (is_div < 0)
479 return -1;
480 if (is_div)
481 return 0;
483 return isl_int_abs_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]) &&
484 !isl_int_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]);
487 /* Return a string representation of the operator used when
488 * printing a constraint where the LHS is greater than or equal to the LHS
489 * (sign > 0) or smaller than or equal to the LHS (sign < 0).
490 * If "strict" is set, then return the strict version of the comparison
491 * operator.
493 static const char *constraint_op(int sign, int strict, int latex)
495 if (strict)
496 return sign < 0 ? "<" : ">";
497 if (sign < 0)
498 return s_le[latex];
499 else
500 return s_ge[latex];
503 /* Print one side of a constraint "c" to "p", with
504 * the variable names taken from "space" and the integer division definitions
505 * taken from "div".
506 * "last" is the position of the last non-zero coefficient.
507 * Let c' be the result of zeroing out this coefficient, then
508 * the partial constraint
510 * c' op
512 * is printed.
514 static __isl_give isl_printer *print_half_constraint(__isl_take isl_printer *p,
515 __isl_keep isl_space *space, __isl_keep isl_mat *div,
516 isl_int *c, int last, const char *op, int latex)
518 isl_int_set_si(c[last], 0);
519 p = print_affine(p, space, div, c);
521 p = isl_printer_print_str(p, " ");
522 p = isl_printer_print_str(p, op);
523 p = isl_printer_print_str(p, " ");
525 return p;
528 /* Print a constraint "c" to "p", with the variable names
529 * taken from "space" and the integer division definitions taken from "div".
530 * "last" is the position of the last non-zero coefficient, which is
531 * moreover assumed to be negative.
532 * Let c' be the result of zeroing out this coefficient, then
533 * the constraint is printed in the form
535 * -c[last] op c'
537 static __isl_give isl_printer *print_constraint(__isl_take isl_printer *p,
538 __isl_keep isl_space *space, __isl_keep isl_mat *div,
539 isl_int *c, int last, const char *op, int latex)
541 isl_int_abs(c[last], c[last]);
543 p = print_term(space, div, c[last], last, p, latex);
545 p = isl_printer_print_str(p, " ");
546 p = isl_printer_print_str(p, op);
547 p = isl_printer_print_str(p, " ");
549 isl_int_set_si(c[last], 0);
550 p = print_affine(p, space, div, c);
552 return p;
555 /* Given an integer division
557 * floor(f/m)
559 * at position "pos" in "div", print the corresponding modulo expression
561 * (f) mod m
563 * to "p". The variable names are taken from "space", while any
564 * nested integer division definitions are taken from "div".
566 static __isl_give isl_printer *print_mod(__isl_take isl_printer *p,
567 __isl_keep isl_space *space, __isl_keep isl_mat *div, int pos,
568 int latex)
570 if (!p || !div)
571 return isl_printer_free(p);
573 p = isl_printer_print_str(p, "(");
574 p = print_affine_of_len(space, div, p,
575 div->row[pos] + 1, div->n_col - 1);
576 p = isl_printer_print_str(p, ") ");
577 p = isl_printer_print_str(p, s_mod[latex]);
578 p = isl_printer_print_str(p, " ");
579 p = isl_printer_print_isl_int(p, div->row[pos][0]);
580 return p;
583 /* Can the equality constraints "c" be printed as a modulo constraint?
584 * In particular, is of the form
586 * f - a m floor(g/m) = 0,
588 * with c = -a m the coefficient at position "pos"?
589 * Return the position of the corresponding integer division if so.
590 * Return the number of integer divisions if not.
591 * Return -1 on error.
593 * Modulo constraints are currently not printed in C format.
594 * Other than that, "pos" needs to correspond to an integer division
595 * with explicit representation and "c" needs to be a multiple
596 * of the denominator of the integer division.
598 static int print_as_modulo_pos(__isl_keep isl_printer *p,
599 __isl_keep isl_space *space, __isl_keep isl_mat *div, unsigned pos,
600 isl_int c)
602 isl_bool can_print;
603 unsigned n_div;
604 enum isl_dim_type type;
606 if (!p)
607 return -1;
608 n_div = isl_mat_rows(div);
609 if (p->output_format == ISL_FORMAT_C)
610 return n_div;
611 type = pos2type(space, &pos);
612 if (type != isl_dim_div)
613 return n_div;
614 can_print = can_print_div_expr(p, div, pos);
615 if (can_print < 0)
616 return -1;
617 if (!can_print)
618 return n_div;
619 if (!isl_int_is_divisible_by(c, div->row[pos][0]))
620 return n_div;
621 return pos;
624 /* Print equality constraint "c" to "p" as a modulo constraint,
625 * with the variable names taken from "space" and
626 * the integer division definitions taken from "div".
627 * "last" is the position of the last non-zero coefficient, which is
628 * moreover assumed to be negative and a multiple of the denominator
629 * of the corresponding integer division. "div_pos" is the corresponding
630 * position in the sequence of integer divisions.
632 * The equality is of the form
634 * f - a m floor(g/m) = 0.
636 * Print it as
638 * a (g mod m) = -f + a g
640 static __isl_give isl_printer *print_eq_mod_constraint(
641 __isl_take isl_printer *p, __isl_keep isl_space *space,
642 __isl_keep isl_mat *div, unsigned div_pos,
643 isl_int *c, int last, int latex)
645 isl_ctx *ctx;
646 int multiple;
648 ctx = isl_printer_get_ctx(p);
649 isl_int_divexact(c[last], c[last], div->row[div_pos][0]);
650 isl_int_abs(c[last], c[last]);
651 multiple = !isl_int_is_one(c[last]);
652 if (multiple) {
653 p = isl_printer_print_isl_int(p, c[last]);
654 p = isl_printer_print_str(p, "*(");
656 p = print_mod(p, space, div, div_pos, latex);
657 if (multiple)
658 p = isl_printer_print_str(p, ")");
659 p = isl_printer_print_str(p, " = ");
660 isl_seq_combine(c, ctx->negone, c,
661 c[last], div->row[div_pos] + 1, last);
662 isl_int_set_si(c[last], 0);
663 p = print_affine(p, space, div, c);
664 return p;
667 /* Print equality constraint "c" to "p", with the variable names
668 * taken from "space" and the integer division definitions taken from "div".
669 * "last" is the position of the last non-zero coefficient, which is
670 * moreover assumed to be negative.
672 * If possible, print the equality constraint as a modulo constraint.
674 static __isl_give isl_printer *print_eq_constraint(__isl_take isl_printer *p,
675 __isl_keep isl_space *space, __isl_keep isl_mat *div, isl_int *c,
676 int last, int latex)
678 unsigned n_div;
679 int div_pos;
681 n_div = isl_mat_rows(div);
682 div_pos = print_as_modulo_pos(p, space, div, last, c[last]);
683 if (div_pos < 0)
684 return isl_printer_free(p);
685 if (div_pos < n_div)
686 return print_eq_mod_constraint(p, space, div, div_pos,
687 c, last, latex);
688 return print_constraint(p, space, div, c, last, "=", latex);
691 /* Print the constraints of "bmap" to "p".
692 * The names of the variables are taken from "space" and
693 * the integer division definitions are taken from "div".
694 * Div constraints are only printed in "dump" mode.
695 * The constraints are sorted prior to printing (except in "dump" mode).
697 * If x is the last variable with a non-zero coefficient,
698 * then a lower bound
700 * f - a x >= 0
702 * is printed as
704 * a x <= f
706 * while an upper bound
708 * f + a x >= 0
710 * is printed as
712 * a x >= -f
714 * If the next constraint has an opposite sign for the same last coefficient,
715 * then it is printed as
717 * f >= a x
719 * or
721 * -f <= a x
723 * instead. In fact, the "a x" part is not printed explicitly, but
724 * reused from the next constraint, which is therefore treated as
725 * a first constraint in the conjunction.
727 * If the constant term of "f" is -1, then "f" is replaced by "f + 1" and
728 * the comparison operator is replaced by the strict variant.
729 * Essentially, ">= 1" is replaced by "> 0".
731 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
732 __isl_keep isl_space *space, __isl_keep isl_mat *div,
733 __isl_take isl_printer *p, int latex)
735 int i;
736 isl_vec *c = NULL;
737 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
738 unsigned total = isl_basic_map_total_dim(bmap);
739 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
740 int first = 1;
741 int dump;
743 if (!p)
744 return NULL;
745 bmap = isl_basic_map_copy(bmap);
746 dump = p->dump;
747 if (!dump)
748 bmap = isl_basic_map_sort_constraints(bmap);
749 if (!bmap)
750 goto error;
752 c = isl_vec_alloc(bmap->ctx, 1 + total);
753 if (!c)
754 goto error;
756 for (i = bmap->n_eq - 1; i >= 0; --i) {
757 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
758 if (l < 0) {
759 if (i != bmap->n_eq - 1)
760 p = isl_printer_print_str(p, s_and[latex]);
761 p = isl_printer_print_str(p, "0 = 0");
762 continue;
764 if (!first)
765 p = isl_printer_print_str(p, s_and[latex]);
766 if (isl_int_is_neg(bmap->eq[i][l]))
767 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
768 else
769 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
770 p = print_eq_constraint(p, space, div, c->el, l, latex);
771 first = 0;
773 for (i = 0; i < bmap->n_ineq; ++i) {
774 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
775 int strict;
776 int s;
777 const char *op;
778 if (l < 0)
779 continue;
780 if (!dump && l >= o_div &&
781 can_print_div_expr(p, div, l - o_div)) {
782 isl_bool is_div;
783 is_div = isl_basic_map_is_div_constraint(bmap,
784 bmap->ineq[i], l - o_div);
785 if (is_div < 0)
786 goto error;
787 if (is_div)
788 continue;
790 if (!first)
791 p = isl_printer_print_str(p, s_and[latex]);
792 s = isl_int_sgn(bmap->ineq[i][l]);
793 strict = !rational && isl_int_is_negone(bmap->ineq[i][0]);
794 if (s < 0)
795 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
796 else
797 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
798 if (strict)
799 isl_int_set_si(c->el[0], 0);
800 if (!dump && next_is_opposite(bmap, i, l)) {
801 op = constraint_op(-s, strict, latex);
802 p = print_half_constraint(p, space, div, c->el, l,
803 op, latex);
804 first = 1;
805 } else {
806 op = constraint_op(s, strict, latex);
807 p = print_constraint(p, space, div, c->el, l,
808 op, latex);
809 first = 0;
813 isl_basic_map_free(bmap);
814 isl_vec_free(c);
816 return p;
817 error:
818 isl_basic_map_free(bmap);
819 isl_vec_free(c);
820 isl_printer_free(p);
821 return NULL;
824 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
825 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
827 int c;
829 if (!p || !div)
830 return isl_printer_free(p);
832 c = p->output_format == ISL_FORMAT_C;
833 p = isl_printer_print_str(p, c ? "floord(" : "floor((");
834 p = print_affine_of_len(dim, div, p,
835 div->row[pos] + 1, div->n_col - 1);
836 p = isl_printer_print_str(p, c ? ", " : ")/");
837 p = isl_printer_print_isl_int(p, div->row[pos][0]);
838 p = isl_printer_print_str(p, ")");
839 return p;
842 /* Print a comma separated list of div names, except those that have
843 * a definition that can be printed.
844 * If "print_defined_divs" is set, then those div names are printed
845 * as well, along with their definitions.
847 static __isl_give isl_printer *print_div_list(__isl_take isl_printer *p,
848 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex,
849 int print_defined_divs)
851 int i;
852 int first = 1;
853 unsigned n_div;
855 if (!p || !space || !div)
856 return isl_printer_free(p);
858 n_div = isl_mat_rows(div);
860 for (i = 0; i < n_div; ++i) {
861 if (!print_defined_divs && can_print_div_expr(p, div, i))
862 continue;
863 if (!first)
864 p = isl_printer_print_str(p, ", ");
865 p = print_name(space, p, isl_dim_div, i, latex);
866 first = 0;
867 if (!can_print_div_expr(p, div, i))
868 continue;
869 p = isl_printer_print_str(p, " = ");
870 p = print_div(space, div, i, p);
873 return p;
876 /* Does printing an object with local variables described by "div"
877 * require an "exists" clause?
878 * That is, are there any local variables without an explicit representation?
879 * An exists clause is also needed in "dump" mode because
880 * explicit div representations are not printed inline in that case.
882 static isl_bool need_exists(__isl_keep isl_printer *p, __isl_keep isl_mat *div)
884 int i, n;
886 if (!p || !div)
887 return isl_bool_error;
888 n = isl_mat_rows(div);
889 if (n == 0)
890 return isl_bool_false;
891 if (p->dump)
892 return isl_bool_true;
893 for (i = 0; i < n; ++i)
894 if (!can_print_div_expr(p, div, i))
895 return isl_bool_true;
896 return isl_bool_false;
899 /* Print the start of an exists clause, i.e.,
901 * (exists variables:
903 * In dump mode, local variables with an explicit definition are printed
904 * as well because they will not be printed inline.
906 static __isl_give isl_printer *open_exists(__isl_take isl_printer *p,
907 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex)
909 int dump;
911 if (!p)
912 return NULL;
914 dump = p->dump;
915 p = isl_printer_print_str(p, s_open_exists[latex]);
916 p = print_div_list(p, space, div, latex, dump);
917 p = isl_printer_print_str(p, ": ");
919 return p;
922 /* Remove the explicit representations of all local variables in "div".
924 static __isl_give isl_mat *mark_all_unknown(__isl_take isl_mat *div)
926 int i, n_div;
928 if (!div)
929 return NULL;
931 n_div = isl_mat_rows(div);
932 for (i = 0; i < n_div; ++i)
933 div = isl_mat_set_element_si(div, i, 0, 0);
934 return div;
937 /* Print the constraints of "bmap" to "p".
938 * The names of the variables are taken from "space".
939 * "latex" is set if the constraints should be printed in LaTeX format.
940 * Do not print inline explicit div representations in "dump" mode.
942 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
943 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
945 int dump;
946 isl_mat *div;
947 isl_bool exists;
949 if (!p)
950 return NULL;
951 dump = p->dump;
952 div = isl_basic_map_get_divs(bmap);
953 exists = need_exists(p, div);
954 if (exists >= 0 && exists)
955 p = open_exists(p, space, div, latex);
957 if (dump)
958 div = mark_all_unknown(div);
959 p = print_constraints(bmap, space, div, p, latex);
960 isl_mat_free(div);
962 if (exists >= 0 && exists)
963 p = isl_printer_print_str(p, s_close_exists[latex]);
964 return p;
967 /* Print a colon followed by the constraints of "bmap"
968 * to "p", provided there are any constraints.
969 * The names of the variables are taken from "space".
970 * "latex" is set if the constraints should be printed in LaTeX format.
972 static __isl_give isl_printer *print_optional_disjunct(
973 __isl_keep isl_basic_map *bmap, __isl_keep isl_space *space,
974 __isl_take isl_printer *p, int latex)
976 if (isl_basic_map_plain_is_universe(bmap))
977 return p;
979 p = isl_printer_print_str(p, ": ");
980 p = print_disjunct(bmap, space, p, latex);
982 return p;
985 static __isl_give isl_printer *basic_map_print_omega(
986 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
988 p = isl_printer_print_str(p, "{ [");
989 p = print_var_list(p, bmap->dim, isl_dim_in);
990 p = isl_printer_print_str(p, "] -> [");
991 p = print_var_list(p, bmap->dim, isl_dim_out);
992 p = isl_printer_print_str(p, "] ");
993 p = print_optional_disjunct(bmap, bmap->dim, p, 0);
994 p = isl_printer_print_str(p, " }");
995 return p;
998 static __isl_give isl_printer *basic_set_print_omega(
999 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
1001 p = isl_printer_print_str(p, "{ [");
1002 p = print_var_list(p, bset->dim, isl_dim_set);
1003 p = isl_printer_print_str(p, "] ");
1004 p = print_optional_disjunct(bset, bset->dim, p, 0);
1005 p = isl_printer_print_str(p, " }");
1006 return p;
1009 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
1010 __isl_take isl_printer *p)
1012 int i;
1014 for (i = 0; i < map->n; ++i) {
1015 if (i)
1016 p = isl_printer_print_str(p, " union ");
1017 p = basic_map_print_omega(map->p[i], p);
1019 return p;
1022 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
1023 __isl_take isl_printer *p)
1025 int i;
1027 for (i = 0; i < set->n; ++i) {
1028 if (i)
1029 p = isl_printer_print_str(p, " union ");
1030 p = basic_set_print_omega(set->p[i], p);
1032 return p;
1035 /* Print the list of parameters in "space", followed by an arrow, to "p",
1036 * if there are any parameters.
1038 static __isl_give isl_printer *print_param_tuple(__isl_take isl_printer *p,
1039 __isl_keep isl_space *space, struct isl_print_space_data *data)
1041 if (!p || !space)
1042 return isl_printer_free(p);
1043 if (isl_space_dim(space, isl_dim_param) == 0)
1044 return p;
1046 p = print_tuple(space, p, isl_dim_param, data);
1047 p = isl_printer_print_str(p, s_to[data->latex]);
1049 return p;
1052 static __isl_give isl_printer *isl_basic_map_print_isl(
1053 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
1054 int latex)
1056 struct isl_print_space_data data = { .latex = latex };
1057 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1059 p = print_param_tuple(p, bmap->dim, &data);
1060 p = isl_printer_print_str(p, "{ ");
1061 p = isl_print_space(bmap->dim, p, rational, &data);
1062 p = isl_printer_print_str(p, " : ");
1063 p = print_disjunct(bmap, bmap->dim, p, latex);
1064 p = isl_printer_print_str(p, " }");
1065 return p;
1068 /* Print the disjuncts of a map (or set) "map" to "p".
1069 * The names of the variables are taken from "space".
1070 * "latex" is set if the constraints should be printed in LaTeX format.
1072 static __isl_give isl_printer *print_disjuncts_core(__isl_keep isl_map *map,
1073 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1075 int i;
1077 if (map->n == 0)
1078 p = isl_printer_print_str(p, "false");
1079 for (i = 0; i < map->n; ++i) {
1080 if (i)
1081 p = isl_printer_print_str(p, s_or[latex]);
1082 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
1083 p = isl_printer_print_str(p, "(");
1084 p = print_disjunct(map->p[i], space, p, latex);
1085 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
1086 p = isl_printer_print_str(p, ")");
1088 return p;
1091 /* Print the disjuncts of a map (or set) "map" to "p".
1092 * The names of the variables are taken from "space".
1093 * "hull" describes constraints shared by all disjuncts of "map".
1094 * "latex" is set if the constraints should be printed in LaTeX format.
1096 * Print the disjuncts as a conjunction of "hull" and
1097 * the result of removing the constraints of "hull" from "map".
1098 * If this result turns out to be the universe, then simply print "hull".
1100 static __isl_give isl_printer *print_disjuncts_in_hull(__isl_keep isl_map *map,
1101 __isl_keep isl_space *space, __isl_take isl_basic_map *hull,
1102 __isl_take isl_printer *p, int latex)
1104 isl_bool is_universe;
1106 p = print_disjunct(hull, space, p, latex);
1107 map = isl_map_plain_gist_basic_map(isl_map_copy(map), hull);
1108 is_universe = isl_map_plain_is_universe(map);
1109 if (is_universe < 0)
1110 goto error;
1111 if (!is_universe) {
1112 p = isl_printer_print_str(p, s_and[latex]);
1113 p = isl_printer_print_str(p, "(");
1114 p = print_disjuncts_core(map, space, p, latex);
1115 p = isl_printer_print_str(p, ")");
1117 isl_map_free(map);
1119 return p;
1120 error:
1121 isl_map_free(map);
1122 isl_printer_free(p);
1123 return NULL;
1126 /* Print the disjuncts of a map (or set) "map" to "p".
1127 * The names of the variables are taken from "space".
1128 * "latex" is set if the constraints should be printed in LaTeX format.
1130 * If there are at least two disjuncts and "dump" mode is not turned out,
1131 * check for any shared constraints among all disjuncts.
1132 * If there are any, then print them separately in print_disjuncts_in_hull.
1134 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
1135 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1137 if (isl_map_plain_is_universe(map))
1138 return p;
1140 p = isl_printer_print_str(p, s_such_that[latex]);
1141 if (!p)
1142 return NULL;
1144 if (!p->dump && map->n >= 2) {
1145 isl_basic_map *hull;
1146 isl_bool is_universe;
1148 hull = isl_map_plain_unshifted_simple_hull(isl_map_copy(map));
1149 is_universe = isl_basic_map_plain_is_universe(hull);
1150 if (is_universe < 0)
1151 p = isl_printer_free(p);
1152 else if (!is_universe)
1153 return print_disjuncts_in_hull(map, space, hull,
1154 p, latex);
1155 isl_basic_map_free(hull);
1158 return print_disjuncts_core(map, space, p, latex);
1161 /* Print the disjuncts of a map (or set).
1162 * The names of the variables are taken from "space".
1163 * "latex" is set if the constraints should be printed in LaTeX format.
1165 * If the map turns out to be a universal parameter domain, then
1166 * we need to print the colon. Otherwise, the output looks identical
1167 * to the empty set.
1169 static __isl_give isl_printer *print_disjuncts_map(__isl_keep isl_map *map,
1170 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1172 if (isl_map_plain_is_universe(map) && isl_space_is_params(map->dim))
1173 return isl_printer_print_str(p, s_such_that[latex]);
1174 else
1175 return print_disjuncts(map, space, p, latex);
1178 struct isl_aff_split {
1179 isl_basic_map *aff;
1180 isl_map *map;
1183 static void free_split(__isl_take struct isl_aff_split *split, int n)
1185 int i;
1187 if (!split)
1188 return;
1190 for (i = 0; i < n; ++i) {
1191 isl_basic_map_free(split[i].aff);
1192 isl_map_free(split[i].map);
1195 free(split);
1198 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
1200 int i, j;
1201 unsigned nparam, n_in, n_out, total;
1203 bmap = isl_basic_map_cow(bmap);
1204 if (!bmap)
1205 return NULL;
1206 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
1207 goto error;
1209 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1210 n_in = isl_basic_map_dim(bmap, isl_dim_in);
1211 n_out = isl_basic_map_dim(bmap, isl_dim_out);
1212 total = isl_basic_map_dim(bmap, isl_dim_all);
1213 for (i = bmap->n_eq - 1; i >= 0; --i) {
1214 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
1215 if (j >= nparam && j < nparam + n_in + n_out &&
1216 (isl_int_is_one(bmap->eq[i][1 + j]) ||
1217 isl_int_is_negone(bmap->eq[i][1 + j])))
1218 continue;
1219 if (isl_basic_map_drop_equality(bmap, i) < 0)
1220 goto error;
1223 bmap = isl_basic_map_finalize(bmap);
1225 return bmap;
1226 error:
1227 isl_basic_map_free(bmap);
1228 return NULL;
1231 static int aff_split_cmp(const void *p1, const void *p2, void *user)
1233 const struct isl_aff_split *s1, *s2;
1234 s1 = (const struct isl_aff_split *) p1;
1235 s2 = (const struct isl_aff_split *) p2;
1237 return isl_basic_map_plain_cmp(s1->aff, s2->aff);
1240 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
1241 __isl_keep isl_basic_map *aff)
1243 int i, j;
1244 unsigned total;
1246 if (!bmap || !aff)
1247 goto error;
1249 total = isl_space_dim(bmap->dim, isl_dim_all);
1251 for (i = bmap->n_eq - 1; i >= 0; --i) {
1252 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
1253 bmap->n_div) != -1)
1254 continue;
1255 for (j = 0; j < aff->n_eq; ++j) {
1256 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
1257 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
1258 continue;
1259 if (isl_basic_map_drop_equality(bmap, i) < 0)
1260 goto error;
1261 break;
1265 return bmap;
1266 error:
1267 isl_basic_map_free(bmap);
1268 return NULL;
1271 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
1273 int i, n;
1274 struct isl_aff_split *split;
1275 isl_ctx *ctx;
1277 ctx = isl_map_get_ctx(map);
1278 split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
1279 if (!split)
1280 return NULL;
1282 for (i = 0; i < map->n; ++i) {
1283 isl_basic_map *bmap;
1284 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
1285 bmap = isl_basic_map_copy(map->p[i]);
1286 bmap = isl_basic_map_cow(bmap);
1287 bmap = drop_aff(bmap, split[i].aff);
1288 split[i].map = isl_map_from_basic_map(bmap);
1289 if (!split[i].aff || !split[i].map)
1290 goto error;
1293 if (isl_sort(split, map->n, sizeof(struct isl_aff_split),
1294 &aff_split_cmp, NULL) < 0)
1295 goto error;
1297 n = map->n;
1298 for (i = n - 1; i >= 1; --i) {
1299 if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
1300 split[i].aff))
1301 continue;
1302 isl_basic_map_free(split[i].aff);
1303 split[i - 1].map = isl_map_union(split[i - 1].map,
1304 split[i].map);
1305 if (i != n - 1)
1306 split[i] = split[n - 1];
1307 split[n - 1].aff = NULL;
1308 split[n - 1].map = NULL;
1309 --n;
1312 return split;
1313 error:
1314 free_split(split, map->n);
1315 return NULL;
1318 static int defining_equality(__isl_keep isl_basic_map *eq,
1319 __isl_keep isl_space *dim, enum isl_dim_type type, int pos)
1321 int i;
1322 unsigned total;
1324 if (!eq)
1325 return -1;
1327 pos += isl_space_offset(dim, type);
1328 total = isl_basic_map_total_dim(eq);
1330 for (i = 0; i < eq->n_eq; ++i) {
1331 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
1332 continue;
1333 if (isl_int_is_one(eq->eq[i][1 + pos]))
1334 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
1335 return i;
1338 return -1;
1341 /* Print dimension "pos" of data->space to "p".
1343 * data->user is assumed to be an isl_basic_map keeping track of equalities.
1345 * If the current dimension is defined by these equalities, then print
1346 * the corresponding expression, assigned to the name of the dimension
1347 * if there is any. Otherwise, print the name of the dimension.
1349 static __isl_give isl_printer *print_dim_eq(__isl_take isl_printer *p,
1350 struct isl_print_space_data *data, unsigned pos)
1352 isl_basic_map *eq = data->user;
1353 int j;
1355 j = defining_equality(eq, data->space, data->type, pos);
1356 if (j >= 0) {
1357 if (isl_space_has_dim_name(data->space, data->type, pos)) {
1358 p = print_name(data->space, p, data->type, pos,
1359 data->latex);
1360 p = isl_printer_print_str(p, " = ");
1362 pos += 1 + isl_space_offset(data->space, data->type);
1363 p = print_affine_of_len(data->space, NULL, p, eq->eq[j], pos);
1364 } else {
1365 p = print_name(data->space, p, data->type, pos, data->latex);
1368 return p;
1371 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
1372 struct isl_aff_split *split, int n, __isl_keep isl_space *space)
1374 struct isl_print_space_data data = { 0 };
1375 int i;
1376 int rational;
1378 data.print_dim = &print_dim_eq;
1379 for (i = 0; i < n; ++i) {
1380 if (!split[i].map)
1381 break;
1382 rational = split[i].map->n > 0 &&
1383 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
1384 if (i)
1385 p = isl_printer_print_str(p, "; ");
1386 data.user = split[i].aff;
1387 p = isl_print_space(space, p, rational, &data);
1388 p = print_disjuncts_map(split[i].map, space, p, 0);
1391 return p;
1394 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
1395 __isl_take isl_printer *p)
1397 struct isl_print_space_data data = { 0 };
1398 struct isl_aff_split *split = NULL;
1399 int rational;
1401 if (!p || !map)
1402 return isl_printer_free(p);
1403 if (!p->dump && map->n > 0)
1404 split = split_aff(map);
1405 if (split) {
1406 p = print_split_map(p, split, map->n, map->dim);
1407 } else {
1408 rational = map->n > 0 &&
1409 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
1410 p = isl_print_space(map->dim, p, rational, &data);
1411 p = print_disjuncts_map(map, map->dim, p, 0);
1413 free_split(split, map->n);
1414 return p;
1417 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
1418 __isl_take isl_printer *p)
1420 struct isl_print_space_data data = { 0 };
1422 p = print_param_tuple(p, map->dim, &data);
1423 p = isl_printer_print_str(p, s_open_set[0]);
1424 p = isl_map_print_isl_body(map, p);
1425 p = isl_printer_print_str(p, s_close_set[0]);
1426 return p;
1429 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
1430 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff)
1432 struct isl_print_space_data data = { 0 };
1434 data.latex = 1;
1435 p = print_param_tuple(p, map->dim, &data);
1436 p = isl_printer_print_str(p, s_open_set[1]);
1437 data.print_dim = &print_dim_eq;
1438 data.user = aff;
1439 p = isl_print_space(map->dim, p, 0, &data);
1440 p = print_disjuncts_map(map, map->dim, p, 1);
1441 p = isl_printer_print_str(p, s_close_set[1]);
1443 return p;
1446 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
1447 __isl_take isl_printer *p)
1449 int i;
1450 struct isl_aff_split *split = NULL;
1452 if (map->n > 0)
1453 split = split_aff(map);
1455 if (!split)
1456 return print_latex_map(map, p, NULL);
1458 for (i = 0; i < map->n; ++i) {
1459 if (!split[i].map)
1460 break;
1461 if (i)
1462 p = isl_printer_print_str(p, " \\cup ");
1463 p = print_latex_map(split[i].map, p, split[i].aff);
1466 free_split(split, map->n);
1467 return p;
1470 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
1471 __isl_keep isl_basic_map *bmap)
1473 if (!p || !bmap)
1474 goto error;
1475 if (p->output_format == ISL_FORMAT_ISL)
1476 return isl_basic_map_print_isl(bmap, p, 0);
1477 else if (p->output_format == ISL_FORMAT_OMEGA)
1478 return basic_map_print_omega(bmap, p);
1479 isl_assert(bmap->ctx, 0, goto error);
1480 error:
1481 isl_printer_free(p);
1482 return NULL;
1485 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
1486 __isl_keep isl_basic_set *bset)
1488 if (!p || !bset)
1489 goto error;
1491 if (p->output_format == ISL_FORMAT_ISL)
1492 return isl_basic_map_print_isl(bset, p, 0);
1493 else if (p->output_format == ISL_FORMAT_POLYLIB)
1494 return isl_basic_set_print_polylib(bset, p, 0);
1495 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1496 return isl_basic_set_print_polylib(bset, p, 1);
1497 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
1498 return bset_print_constraints_polylib(bset, p);
1499 else if (p->output_format == ISL_FORMAT_OMEGA)
1500 return basic_set_print_omega(bset, p);
1501 isl_assert(p->ctx, 0, goto error);
1502 error:
1503 isl_printer_free(p);
1504 return NULL;
1507 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
1508 __isl_keep isl_set *set)
1510 if (!p || !set)
1511 goto error;
1512 if (p->output_format == ISL_FORMAT_ISL)
1513 return isl_map_print_isl(set_to_map(set), p);
1514 else if (p->output_format == ISL_FORMAT_POLYLIB)
1515 return isl_set_print_polylib(set, p, 0);
1516 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1517 return isl_set_print_polylib(set, p, 1);
1518 else if (p->output_format == ISL_FORMAT_OMEGA)
1519 return isl_set_print_omega(set, p);
1520 else if (p->output_format == ISL_FORMAT_LATEX)
1521 return isl_map_print_latex(set_to_map(set), p);
1522 isl_assert(set->ctx, 0, goto error);
1523 error:
1524 isl_printer_free(p);
1525 return NULL;
1528 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1529 __isl_keep isl_map *map)
1531 if (!p || !map)
1532 goto error;
1534 if (p->output_format == ISL_FORMAT_ISL)
1535 return isl_map_print_isl(map, p);
1536 else if (p->output_format == ISL_FORMAT_POLYLIB)
1537 return isl_map_print_polylib(map, p, 0);
1538 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1539 return isl_map_print_polylib(map, p, 1);
1540 else if (p->output_format == ISL_FORMAT_OMEGA)
1541 return isl_map_print_omega(map, p);
1542 else if (p->output_format == ISL_FORMAT_LATEX)
1543 return isl_map_print_latex(map, p);
1544 isl_assert(map->ctx, 0, goto error);
1545 error:
1546 isl_printer_free(p);
1547 return NULL;
1550 struct isl_union_print_data {
1551 isl_printer *p;
1552 int first;
1555 static isl_stat print_map_body(__isl_take isl_map *map, void *user)
1557 struct isl_union_print_data *data;
1558 data = (struct isl_union_print_data *)user;
1560 if (!data->first)
1561 data->p = isl_printer_print_str(data->p, "; ");
1562 data->first = 0;
1564 data->p = isl_map_print_isl_body(map, data->p);
1565 isl_map_free(map);
1567 return isl_stat_ok;
1570 static __isl_give isl_printer *isl_union_map_print_isl(
1571 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1573 struct isl_union_print_data data;
1574 struct isl_print_space_data space_data = { 0 };
1575 isl_space *space;
1577 space = isl_union_map_get_space(umap);
1578 p = print_param_tuple(p, space, &space_data);
1579 isl_space_free(space);
1580 p = isl_printer_print_str(p, s_open_set[0]);
1581 data.p = p;
1582 data.first = 1;
1583 isl_union_map_foreach_map(umap, &print_map_body, &data);
1584 p = data.p;
1585 p = isl_printer_print_str(p, s_close_set[0]);
1586 return p;
1589 static isl_stat print_latex_map_body(__isl_take isl_map *map, void *user)
1591 struct isl_union_print_data *data;
1592 data = (struct isl_union_print_data *)user;
1594 if (!data->first)
1595 data->p = isl_printer_print_str(data->p, " \\cup ");
1596 data->first = 0;
1598 data->p = isl_map_print_latex(map, data->p);
1599 isl_map_free(map);
1601 return isl_stat_ok;
1604 static __isl_give isl_printer *isl_union_map_print_latex(
1605 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1607 struct isl_union_print_data data = { p, 1 };
1608 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1609 p = data.p;
1610 return p;
1613 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1614 __isl_keep isl_union_map *umap)
1616 if (!p || !umap)
1617 goto error;
1619 if (p->output_format == ISL_FORMAT_ISL)
1620 return isl_union_map_print_isl(umap, p);
1621 if (p->output_format == ISL_FORMAT_LATEX)
1622 return isl_union_map_print_latex(umap, p);
1624 isl_die(p->ctx, isl_error_invalid,
1625 "invalid output format for isl_union_map", goto error);
1626 error:
1627 isl_printer_free(p);
1628 return NULL;
1631 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1632 __isl_keep isl_union_set *uset)
1634 if (!p || !uset)
1635 goto error;
1637 if (p->output_format == ISL_FORMAT_ISL)
1638 return isl_union_map_print_isl(uset_to_umap(uset), p);
1639 if (p->output_format == ISL_FORMAT_LATEX)
1640 return isl_union_map_print_latex(uset_to_umap(uset), p);
1642 isl_die(p->ctx, isl_error_invalid,
1643 "invalid output format for isl_union_set", goto error);
1644 error:
1645 isl_printer_free(p);
1646 return NULL;
1649 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
1651 int i;
1652 int n;
1654 for (i = 0, n = 0; i < rec->n; ++i)
1655 if (!isl_upoly_is_zero(rec->p[i]))
1656 ++n;
1658 return n;
1661 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
1662 __isl_take isl_printer *p, int first)
1664 struct isl_upoly_cst *cst;
1665 int neg;
1667 cst = isl_upoly_as_cst(up);
1668 if (!cst)
1669 goto error;
1670 neg = !first && isl_int_is_neg(cst->n);
1671 if (!first)
1672 p = isl_printer_print_str(p, neg ? " - " : " + ");
1673 if (neg)
1674 isl_int_neg(cst->n, cst->n);
1675 if (isl_int_is_zero(cst->d)) {
1676 int sgn = isl_int_sgn(cst->n);
1677 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1678 sgn == 0 ? "NaN" : "infty");
1679 } else
1680 p = isl_printer_print_isl_int(p, cst->n);
1681 if (neg)
1682 isl_int_neg(cst->n, cst->n);
1683 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1684 p = isl_printer_print_str(p, "/");
1685 p = isl_printer_print_isl_int(p, cst->d);
1687 return p;
1688 error:
1689 isl_printer_free(p);
1690 return NULL;
1693 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1694 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var)
1696 unsigned total;
1698 total = isl_space_dim(dim, isl_dim_all);
1699 if (var < total)
1700 p = print_term(dim, NULL, dim->ctx->one, 1 + var, p, 0);
1701 else
1702 p = print_div(dim, div, var - total, p);
1703 return p;
1706 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1707 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var, int exp)
1709 p = print_base(p, dim, div, var);
1710 if (exp == 1)
1711 return p;
1712 if (p->output_format == ISL_FORMAT_C) {
1713 int i;
1714 for (i = 1; i < exp; ++i) {
1715 p = isl_printer_print_str(p, "*");
1716 p = print_base(p, dim, div, var);
1718 } else {
1719 p = isl_printer_print_str(p, "^");
1720 p = isl_printer_print_int(p, exp);
1722 return p;
1725 /* Print the polynomial "up" defined over the domain space "space" and
1726 * local variables defined by "div" to "p".
1728 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1729 __isl_keep isl_space *space, __isl_keep isl_mat *div,
1730 __isl_take isl_printer *p)
1732 int i, n, first, print_parens;
1733 struct isl_upoly_rec *rec;
1735 if (!p || !up || !space || !div)
1736 goto error;
1738 if (isl_upoly_is_cst(up))
1739 return upoly_print_cst(up, p, 1);
1741 rec = isl_upoly_as_rec(up);
1742 if (!rec)
1743 goto error;
1744 n = upoly_rec_n_non_zero(rec);
1745 print_parens = n > 1;
1746 if (print_parens)
1747 p = isl_printer_print_str(p, "(");
1748 for (i = 0, first = 1; i < rec->n; ++i) {
1749 if (isl_upoly_is_zero(rec->p[i]))
1750 continue;
1751 if (isl_upoly_is_negone(rec->p[i])) {
1752 if (!i)
1753 p = isl_printer_print_str(p, "-1");
1754 else if (first)
1755 p = isl_printer_print_str(p, "-");
1756 else
1757 p = isl_printer_print_str(p, " - ");
1758 } else if (isl_upoly_is_cst(rec->p[i]) &&
1759 !isl_upoly_is_one(rec->p[i]))
1760 p = upoly_print_cst(rec->p[i], p, first);
1761 else {
1762 if (!first)
1763 p = isl_printer_print_str(p, " + ");
1764 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1765 p = upoly_print(rec->p[i], space, div, p);
1767 first = 0;
1768 if (i == 0)
1769 continue;
1770 if (!isl_upoly_is_one(rec->p[i]) &&
1771 !isl_upoly_is_negone(rec->p[i]))
1772 p = isl_printer_print_str(p, " * ");
1773 p = print_pow(p, space, div, rec->up.var, i);
1775 if (print_parens)
1776 p = isl_printer_print_str(p, ")");
1777 return p;
1778 error:
1779 isl_printer_free(p);
1780 return NULL;
1783 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1784 __isl_keep isl_qpolynomial *qp)
1786 if (!p || !qp)
1787 goto error;
1788 p = upoly_print(qp->upoly, qp->dim, qp->div, p);
1789 return p;
1790 error:
1791 isl_printer_free(p);
1792 return NULL;
1795 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1796 __isl_keep isl_qpolynomial *qp)
1798 struct isl_print_space_data data = { 0 };
1800 if (!p || !qp)
1801 goto error;
1803 p = print_param_tuple(p, qp->dim, &data);
1804 p = isl_printer_print_str(p, "{ ");
1805 if (!isl_space_is_params(qp->dim)) {
1806 p = isl_print_space(qp->dim, p, 0, &data);
1807 p = isl_printer_print_str(p, " -> ");
1809 p = print_qpolynomial(p, qp);
1810 p = isl_printer_print_str(p, " }");
1811 return p;
1812 error:
1813 isl_printer_free(p);
1814 return NULL;
1817 /* Print the quasi-polynomial "qp" to "p" in C format, with the variable names
1818 * taken from the domain space "space".
1820 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1821 __isl_keep isl_space *space, __isl_keep isl_qpolynomial *qp)
1823 isl_int den;
1825 isl_int_init(den);
1826 isl_qpolynomial_get_den(qp, &den);
1827 if (!isl_int_is_one(den)) {
1828 isl_qpolynomial *f;
1829 p = isl_printer_print_str(p, "(");
1830 qp = isl_qpolynomial_copy(qp);
1831 f = isl_qpolynomial_rat_cst_on_domain(isl_space_copy(qp->dim),
1832 den, qp->dim->ctx->one);
1833 qp = isl_qpolynomial_mul(qp, f);
1835 if (qp)
1836 p = upoly_print(qp->upoly, space, qp->div, p);
1837 else
1838 p = isl_printer_free(p);
1839 if (!isl_int_is_one(den)) {
1840 p = isl_printer_print_str(p, ")/");
1841 p = isl_printer_print_isl_int(p, den);
1842 isl_qpolynomial_free(qp);
1844 isl_int_clear(den);
1845 return p;
1848 __isl_give isl_printer *isl_printer_print_qpolynomial(
1849 __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1851 if (!p || !qp)
1852 goto error;
1854 if (p->output_format == ISL_FORMAT_ISL)
1855 return print_qpolynomial_isl(p, qp);
1856 else if (p->output_format == ISL_FORMAT_C)
1857 return print_qpolynomial_c(p, qp->dim, qp);
1858 else
1859 isl_die(qp->dim->ctx, isl_error_unsupported,
1860 "output format not supported for isl_qpolynomials",
1861 goto error);
1862 error:
1863 isl_printer_free(p);
1864 return NULL;
1867 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1868 unsigned output_format)
1870 isl_printer *p;
1872 if (!qp)
1873 return;
1875 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1876 p = isl_printer_to_file(qp->dim->ctx, out);
1877 p = isl_printer_print_qpolynomial(p, qp);
1878 isl_printer_free(p);
1881 static __isl_give isl_printer *qpolynomial_fold_print(
1882 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1884 int i;
1886 if (fold->type == isl_fold_min)
1887 p = isl_printer_print_str(p, "min");
1888 else if (fold->type == isl_fold_max)
1889 p = isl_printer_print_str(p, "max");
1890 p = isl_printer_print_str(p, "(");
1891 for (i = 0; i < fold->n; ++i) {
1892 if (i)
1893 p = isl_printer_print_str(p, ", ");
1894 p = print_qpolynomial(p, fold->qp[i]);
1896 p = isl_printer_print_str(p, ")");
1897 return p;
1900 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1901 FILE *out, unsigned output_format)
1903 isl_printer *p;
1905 if (!fold)
1906 return;
1908 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1910 p = isl_printer_to_file(fold->dim->ctx, out);
1911 p = isl_printer_print_qpolynomial_fold(p, fold);
1913 isl_printer_free(p);
1916 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1917 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1919 struct isl_print_space_data data = { 0 };
1920 int i = 0;
1922 for (i = 0; i < pwqp->n; ++i) {
1923 isl_space *space;
1925 if (i)
1926 p = isl_printer_print_str(p, "; ");
1927 space = isl_qpolynomial_get_domain_space(pwqp->p[i].qp);
1928 if (!isl_space_is_params(space)) {
1929 p = isl_print_space(space, p, 0, &data);
1930 p = isl_printer_print_str(p, " -> ");
1932 p = print_qpolynomial(p, pwqp->p[i].qp);
1933 p = print_disjuncts(set_to_map(pwqp->p[i].set), space, p, 0);
1934 isl_space_free(space);
1937 return p;
1940 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1941 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1943 struct isl_print_space_data data = { 0 };
1945 if (!p || !pwqp)
1946 goto error;
1948 p = print_param_tuple(p, pwqp->dim, &data);
1949 p = isl_printer_print_str(p, "{ ");
1950 if (pwqp->n == 0) {
1951 if (!isl_space_is_set(pwqp->dim)) {
1952 p = print_tuple(pwqp->dim, p, isl_dim_in, &data);
1953 p = isl_printer_print_str(p, " -> ");
1955 p = isl_printer_print_str(p, "0");
1957 p = isl_pwqp_print_isl_body(p, pwqp);
1958 p = isl_printer_print_str(p, " }");
1959 return p;
1960 error:
1961 isl_printer_free(p);
1962 return NULL;
1965 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
1966 unsigned output_format)
1968 isl_printer *p;
1970 if (!pwqp)
1971 return;
1973 p = isl_printer_to_file(pwqp->dim->ctx, out);
1974 p = isl_printer_set_output_format(p, output_format);
1975 p = isl_printer_print_pw_qpolynomial(p, pwqp);
1977 isl_printer_free(p);
1980 static __isl_give isl_printer *isl_pwf_print_isl_body(
1981 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1983 struct isl_print_space_data data = { 0 };
1984 int i = 0;
1986 for (i = 0; i < pwf->n; ++i) {
1987 isl_space *space;
1989 if (i)
1990 p = isl_printer_print_str(p, "; ");
1991 space = isl_qpolynomial_fold_get_domain_space(pwf->p[i].fold);
1992 if (!isl_space_is_params(space)) {
1993 p = isl_print_space(space, p, 0, &data);
1994 p = isl_printer_print_str(p, " -> ");
1996 p = qpolynomial_fold_print(pwf->p[i].fold, p);
1997 p = print_disjuncts(set_to_map(pwf->p[i].set), space, p, 0);
1998 isl_space_free(space);
2001 return p;
2004 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
2005 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2007 struct isl_print_space_data data = { 0 };
2009 p = print_param_tuple(p, pwf->dim, &data);
2010 p = isl_printer_print_str(p, "{ ");
2011 if (pwf->n == 0) {
2012 if (!isl_space_is_set(pwf->dim)) {
2013 p = print_tuple(pwf->dim, p, isl_dim_in, &data);
2014 p = isl_printer_print_str(p, " -> ");
2016 p = isl_printer_print_str(p, "0");
2018 p = isl_pwf_print_isl_body(p, pwf);
2019 p = isl_printer_print_str(p, " }");
2020 return p;
2023 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
2024 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c);
2026 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
2027 __isl_keep isl_space *dim,
2028 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
2030 if (type == isl_dim_div) {
2031 p = isl_printer_print_str(p, "floord(");
2032 p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
2033 p = isl_printer_print_str(p, ", ");
2034 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
2035 p = isl_printer_print_str(p, ")");
2036 } else {
2037 const char *name;
2039 name = isl_space_get_dim_name(dim, type, pos);
2040 if (!name)
2041 name = "UNNAMED";
2042 p = isl_printer_print_str(p, name);
2044 return p;
2047 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
2048 __isl_keep isl_space *dim,
2049 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
2051 enum isl_dim_type type;
2053 if (pos == 0)
2054 return isl_printer_print_isl_int(p, c);
2056 if (isl_int_is_one(c))
2058 else if (isl_int_is_negone(c))
2059 p = isl_printer_print_str(p, "-");
2060 else {
2061 p = isl_printer_print_isl_int(p, c);
2062 p = isl_printer_print_str(p, "*");
2064 type = pos2type(dim, &pos);
2065 p = print_name_c(p, dim, bset, type, pos);
2066 return p;
2069 static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
2070 __isl_keep isl_space *dim,
2071 __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
2073 int i;
2074 int first;
2076 for (i = 0, first = 1; i < len; ++i) {
2077 int flip = 0;
2078 if (isl_int_is_zero(c[i]))
2079 continue;
2080 if (!first) {
2081 if (isl_int_is_neg(c[i])) {
2082 flip = 1;
2083 isl_int_neg(c[i], c[i]);
2084 p = isl_printer_print_str(p, " - ");
2085 } else
2086 p = isl_printer_print_str(p, " + ");
2088 first = 0;
2089 p = print_term_c(p, dim, bset, c[i], i);
2090 if (flip)
2091 isl_int_neg(c[i], c[i]);
2093 if (first)
2094 p = isl_printer_print_str(p, "0");
2095 return p;
2098 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
2099 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c)
2101 unsigned len = 1 + isl_basic_set_total_dim(bset);
2102 return print_partial_affine_c(p, dim, bset, c, len);
2105 /* We skip the constraint if it is implied by the div expression.
2107 * *first indicates whether this is the first constraint in the conjunction and
2108 * is updated if the constraint is actually printed.
2110 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
2111 __isl_keep isl_space *dim,
2112 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int *first)
2114 unsigned o_div;
2115 unsigned n_div;
2116 int div;
2118 o_div = isl_basic_set_offset(bset, isl_dim_div);
2119 n_div = isl_basic_set_dim(bset, isl_dim_div);
2120 div = isl_seq_last_non_zero(c + o_div, n_div);
2121 if (div >= 0) {
2122 isl_bool is_div = isl_basic_set_is_div_constraint(bset, c, div);
2123 if (is_div < 0)
2124 return isl_printer_free(p);
2125 if (is_div)
2126 return p;
2129 if (!*first)
2130 p = isl_printer_print_str(p, " && ");
2132 p = print_affine_c(p, dim, bset, c);
2133 p = isl_printer_print_str(p, " ");
2134 p = isl_printer_print_str(p, op);
2135 p = isl_printer_print_str(p, " 0");
2137 *first = 0;
2139 return p;
2142 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
2143 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset)
2145 int i, j;
2146 int first = 1;
2147 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
2148 unsigned total = isl_basic_set_total_dim(bset) - n_div;
2150 for (i = 0; i < bset->n_eq; ++i) {
2151 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
2152 if (j < 0)
2153 p = print_constraint_c(p, dim, bset,
2154 bset->eq[i], "==", &first);
2155 else {
2156 if (i)
2157 p = isl_printer_print_str(p, " && ");
2158 p = isl_printer_print_str(p, "(");
2159 p = print_partial_affine_c(p, dim, bset, bset->eq[i],
2160 1 + total + j);
2161 p = isl_printer_print_str(p, ") % ");
2162 p = isl_printer_print_isl_int(p,
2163 bset->eq[i][1 + total + j]);
2164 p = isl_printer_print_str(p, " == 0");
2165 first = 0;
2168 for (i = 0; i < bset->n_ineq; ++i)
2169 p = print_constraint_c(p, dim, bset, bset->ineq[i], ">=",
2170 &first);
2171 return p;
2174 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
2175 __isl_keep isl_space *dim, __isl_keep isl_set *set)
2177 int i;
2179 if (!set)
2180 return isl_printer_free(p);
2182 if (set->n == 0)
2183 p = isl_printer_print_str(p, "0");
2185 for (i = 0; i < set->n; ++i) {
2186 if (i)
2187 p = isl_printer_print_str(p, " || ");
2188 if (set->n > 1)
2189 p = isl_printer_print_str(p, "(");
2190 p = print_basic_set_c(p, dim, set->p[i]);
2191 if (set->n > 1)
2192 p = isl_printer_print_str(p, ")");
2194 return p;
2197 /* Print the piecewise quasi-polynomial "pwqp" to "p" in C format.
2199 static __isl_give isl_printer *print_pw_qpolynomial_c(
2200 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2202 int i;
2203 isl_space *space;
2205 space = isl_pw_qpolynomial_get_domain_space(pwqp);
2206 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set)) {
2207 p = print_qpolynomial_c(p, space, pwqp->p[0].qp);
2208 isl_space_free(space);
2209 return p;
2212 for (i = 0; i < pwqp->n; ++i) {
2213 p = isl_printer_print_str(p, "(");
2214 p = print_set_c(p, space, pwqp->p[i].set);
2215 p = isl_printer_print_str(p, ") ? (");
2216 p = print_qpolynomial_c(p, space, pwqp->p[i].qp);
2217 p = isl_printer_print_str(p, ") : ");
2220 isl_space_free(space);
2221 p = isl_printer_print_str(p, "0");
2222 return p;
2225 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
2226 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2228 if (!p || !pwqp)
2229 goto error;
2231 if (p->output_format == ISL_FORMAT_ISL)
2232 return print_pw_qpolynomial_isl(p, pwqp);
2233 else if (p->output_format == ISL_FORMAT_C)
2234 return print_pw_qpolynomial_c(p, pwqp);
2235 isl_assert(p->ctx, 0, goto error);
2236 error:
2237 isl_printer_free(p);
2238 return NULL;
2241 static isl_stat print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
2243 struct isl_union_print_data *data;
2244 data = (struct isl_union_print_data *)user;
2246 if (!data->first)
2247 data->p = isl_printer_print_str(data->p, "; ");
2248 data->first = 0;
2250 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
2251 isl_pw_qpolynomial_free(pwqp);
2253 return isl_stat_ok;
2256 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
2257 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2259 struct isl_union_print_data data;
2260 struct isl_print_space_data space_data = { 0 };
2261 isl_space *space;
2263 space = isl_union_pw_qpolynomial_get_space(upwqp);
2264 p = print_param_tuple(p, space, &space_data);
2265 isl_space_free(space);
2266 p = isl_printer_print_str(p, "{ ");
2267 data.p = p;
2268 data.first = 1;
2269 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
2270 &data);
2271 p = data.p;
2272 p = isl_printer_print_str(p, " }");
2273 return p;
2276 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
2277 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2279 if (!p || !upwqp)
2280 goto error;
2282 if (p->output_format == ISL_FORMAT_ISL)
2283 return print_union_pw_qpolynomial_isl(p, upwqp);
2284 isl_die(p->ctx, isl_error_invalid,
2285 "invalid output format for isl_union_pw_qpolynomial",
2286 goto error);
2287 error:
2288 isl_printer_free(p);
2289 return NULL;
2292 /* Print the quasi-polynomial reduction "fold" to "p" in C format,
2293 * with the variable names taken from the domain space "space".
2295 static __isl_give isl_printer *print_qpolynomial_fold_c(
2296 __isl_take isl_printer *p, __isl_keep isl_space *space,
2297 __isl_keep isl_qpolynomial_fold *fold)
2299 int i;
2301 for (i = 0; i < fold->n - 1; ++i)
2302 if (fold->type == isl_fold_min)
2303 p = isl_printer_print_str(p, "min(");
2304 else if (fold->type == isl_fold_max)
2305 p = isl_printer_print_str(p, "max(");
2307 for (i = 0; i < fold->n; ++i) {
2308 if (i)
2309 p = isl_printer_print_str(p, ", ");
2310 p = print_qpolynomial_c(p, space, fold->qp[i]);
2311 if (i)
2312 p = isl_printer_print_str(p, ")");
2314 return p;
2317 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
2318 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
2320 if (!p || !fold)
2321 goto error;
2322 if (p->output_format == ISL_FORMAT_ISL)
2323 return qpolynomial_fold_print(fold, p);
2324 else if (p->output_format == ISL_FORMAT_C)
2325 return print_qpolynomial_fold_c(p, fold->dim, fold);
2326 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2327 goto error);
2328 error:
2329 isl_printer_free(p);
2330 return NULL;
2333 /* Print the piecewise quasi-polynomial reduction "pwf" to "p" in C format.
2335 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
2336 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2338 int i;
2339 isl_space *space;
2341 space = isl_pw_qpolynomial_fold_get_domain_space(pwf);
2342 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set)) {
2343 p = print_qpolynomial_fold_c(p, space, pwf->p[0].fold);
2344 isl_space_free(space);
2345 return p;
2348 for (i = 0; i < pwf->n; ++i) {
2349 p = isl_printer_print_str(p, "(");
2350 p = print_set_c(p, space, pwf->p[i].set);
2351 p = isl_printer_print_str(p, ") ? (");
2352 p = print_qpolynomial_fold_c(p, space, pwf->p[i].fold);
2353 p = isl_printer_print_str(p, ") : ");
2356 isl_space_free(space);
2357 p = isl_printer_print_str(p, "0");
2358 return p;
2361 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
2362 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2364 if (!p || !pwf)
2365 goto error;
2367 if (p->output_format == ISL_FORMAT_ISL)
2368 return print_pw_qpolynomial_fold_isl(p, pwf);
2369 else if (p->output_format == ISL_FORMAT_C)
2370 return print_pw_qpolynomial_fold_c(p, pwf);
2371 isl_assert(p->ctx, 0, goto error);
2372 error:
2373 isl_printer_free(p);
2374 return NULL;
2377 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
2378 FILE *out, unsigned output_format)
2380 isl_printer *p;
2382 if (!pwf)
2383 return;
2385 p = isl_printer_to_file(pwf->dim->ctx, out);
2386 p = isl_printer_set_output_format(p, output_format);
2387 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
2389 isl_printer_free(p);
2392 static isl_stat print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf,
2393 void *user)
2395 struct isl_union_print_data *data;
2396 data = (struct isl_union_print_data *)user;
2398 if (!data->first)
2399 data->p = isl_printer_print_str(data->p, "; ");
2400 data->first = 0;
2402 data->p = isl_pwf_print_isl_body(data->p, pwf);
2403 isl_pw_qpolynomial_fold_free(pwf);
2405 return isl_stat_ok;
2408 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
2409 __isl_take isl_printer *p,
2410 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2412 struct isl_union_print_data data;
2413 struct isl_print_space_data space_data = { 0 };
2414 isl_space *space;
2416 space = isl_union_pw_qpolynomial_fold_get_space(upwf);
2417 p = print_param_tuple(p, space, &space_data);
2418 isl_space_free(space);
2419 p = isl_printer_print_str(p, "{ ");
2420 data.p = p;
2421 data.first = 1;
2422 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
2423 &print_pwf_body, &data);
2424 p = data.p;
2425 p = isl_printer_print_str(p, " }");
2426 return p;
2429 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2430 __isl_take isl_printer *p,
2431 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2433 if (!p || !upwf)
2434 goto error;
2436 if (p->output_format == ISL_FORMAT_ISL)
2437 return print_union_pw_qpolynomial_fold_isl(p, upwf);
2438 isl_die(p->ctx, isl_error_invalid,
2439 "invalid output format for isl_union_pw_qpolynomial_fold",
2440 goto error);
2441 error:
2442 isl_printer_free(p);
2443 return NULL;
2446 /* Print the isl_constraint "c" to "p".
2448 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
2449 __isl_keep isl_constraint *c)
2451 struct isl_print_space_data data = { 0 };
2452 isl_local_space *ls;
2453 isl_space *space;
2454 isl_bool exists;
2456 if (!p || !c)
2457 goto error;
2459 ls = isl_constraint_get_local_space(c);
2460 if (!ls)
2461 return isl_printer_free(p);
2462 space = isl_local_space_get_space(ls);
2463 p = print_param_tuple(p, space, &data);
2464 p = isl_printer_print_str(p, "{ ");
2465 p = isl_print_space(space, p, 0, &data);
2466 p = isl_printer_print_str(p, " : ");
2467 exists = need_exists(p, ls->div);
2468 if (exists < 0)
2469 p = isl_printer_free(p);
2470 if (exists >= 0 && exists)
2471 p = open_exists(p, space, ls->div, 0);
2472 p = print_affine_of_len(space, ls->div, p, c->v->el, c->v->size);
2473 if (isl_constraint_is_equality(c))
2474 p = isl_printer_print_str(p, " = 0");
2475 else
2476 p = isl_printer_print_str(p, " >= 0");
2477 if (exists >= 0 && exists)
2478 p = isl_printer_print_str(p, s_close_exists[0]);
2479 p = isl_printer_print_str(p, " }");
2480 isl_space_free(space);
2481 isl_local_space_free(ls);
2483 return p;
2484 error:
2485 isl_printer_free(p);
2486 return NULL;
2489 static __isl_give isl_printer *isl_printer_print_space_isl(
2490 __isl_take isl_printer *p, __isl_keep isl_space *space)
2492 struct isl_print_space_data data = { 0 };
2494 if (!space)
2495 goto error;
2497 p = print_param_tuple(p, space, &data);
2499 p = isl_printer_print_str(p, "{ ");
2500 if (isl_space_is_params(space))
2501 p = isl_printer_print_str(p, s_such_that[0]);
2502 else
2503 p = isl_print_space(space, p, 0, &data);
2504 p = isl_printer_print_str(p, " }");
2506 return p;
2507 error:
2508 isl_printer_free(p);
2509 return NULL;
2512 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
2513 __isl_keep isl_space *space)
2515 if (!p || !space)
2516 return isl_printer_free(p);
2517 if (p->output_format == ISL_FORMAT_ISL)
2518 return isl_printer_print_space_isl(p, space);
2519 else if (p->output_format == ISL_FORMAT_OMEGA)
2520 return print_omega_parameters(space, p);
2522 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
2523 "output format not supported for space",
2524 return isl_printer_free(p));
2527 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
2528 __isl_keep isl_local_space *ls)
2530 struct isl_print_space_data data = { 0 };
2531 unsigned n_div;
2533 if (!ls)
2534 goto error;
2536 p = print_param_tuple(p, ls->dim, &data);
2537 p = isl_printer_print_str(p, "{ ");
2538 p = isl_print_space(ls->dim, p, 0, &data);
2539 n_div = isl_local_space_dim(ls, isl_dim_div);
2540 if (n_div > 0) {
2541 p = isl_printer_print_str(p, " : ");
2542 p = isl_printer_print_str(p, s_open_exists[0]);
2543 p = print_div_list(p, ls->dim, ls->div, 0, 1);
2544 p = isl_printer_print_str(p, s_close_exists[0]);
2545 } else if (isl_space_is_params(ls->dim))
2546 p = isl_printer_print_str(p, s_such_that[0]);
2547 p = isl_printer_print_str(p, " }");
2548 return p;
2549 error:
2550 isl_printer_free(p);
2551 return NULL;
2554 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2555 __isl_keep isl_aff *aff)
2557 unsigned total;
2559 if (isl_aff_is_nan(aff))
2560 return isl_printer_print_str(p, "NaN");
2562 total = isl_local_space_dim(aff->ls, isl_dim_all);
2563 p = isl_printer_print_str(p, "(");
2564 p = print_affine_of_len(aff->ls->dim, aff->ls->div, p,
2565 aff->v->el + 1, 1 + total);
2566 if (isl_int_is_one(aff->v->el[0]))
2567 p = isl_printer_print_str(p, ")");
2568 else {
2569 p = isl_printer_print_str(p, ")/");
2570 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2573 return p;
2576 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2577 __isl_keep isl_aff *aff)
2579 struct isl_print_space_data data = { 0 };
2581 if (isl_space_is_params(aff->ls->dim))
2583 else {
2584 p = print_tuple(aff->ls->dim, p, isl_dim_set, &data);
2585 p = isl_printer_print_str(p, " -> ");
2587 p = isl_printer_print_str(p, "[");
2588 p = print_aff_body(p, aff);
2589 p = isl_printer_print_str(p, "]");
2591 return p;
2594 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2595 __isl_keep isl_aff *aff)
2597 struct isl_print_space_data data = { 0 };
2599 if (!aff)
2600 goto error;
2602 p = print_param_tuple(p, aff->ls->dim, &data);
2603 p = isl_printer_print_str(p, "{ ");
2604 p = print_aff(p, aff);
2605 p = isl_printer_print_str(p, " }");
2606 return p;
2607 error:
2608 isl_printer_free(p);
2609 return NULL;
2612 /* Print the body of an isl_pw_aff, i.e., a semicolon delimited
2613 * sequence of affine expressions, each followed by constraints.
2615 static __isl_give isl_printer *print_pw_aff_body(
2616 __isl_take isl_printer *p, __isl_keep isl_pw_aff *pa)
2618 int i;
2620 if (!pa)
2621 return isl_printer_free(p);
2623 for (i = 0; i < pa->n; ++i) {
2624 isl_space *space;
2626 if (i)
2627 p = isl_printer_print_str(p, "; ");
2628 p = print_aff(p, pa->p[i].aff);
2629 space = isl_aff_get_domain_space(pa->p[i].aff);
2630 p = print_disjuncts(set_to_map(pa->p[i].set), space, p, 0);
2631 isl_space_free(space);
2634 return p;
2637 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2638 __isl_keep isl_pw_aff *pwaff)
2640 struct isl_print_space_data data = { 0 };
2642 if (!pwaff)
2643 goto error;
2645 p = print_param_tuple(p, pwaff->dim, &data);
2646 p = isl_printer_print_str(p, "{ ");
2647 p = print_pw_aff_body(p, pwaff);
2648 p = isl_printer_print_str(p, " }");
2649 return p;
2650 error:
2651 isl_printer_free(p);
2652 return NULL;
2655 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2656 __isl_keep isl_local_space *ls, isl_int *c);
2658 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2659 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2661 if (type == isl_dim_div) {
2662 p = isl_printer_print_str(p, "floord(");
2663 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2664 p = isl_printer_print_str(p, ", ");
2665 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2666 p = isl_printer_print_str(p, ")");
2667 } else {
2668 const char *name;
2670 name = isl_space_get_dim_name(ls->dim, type, pos);
2671 if (!name)
2672 name = "UNNAMED";
2673 p = isl_printer_print_str(p, name);
2675 return p;
2678 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2679 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2681 enum isl_dim_type type;
2683 if (pos == 0)
2684 return isl_printer_print_isl_int(p, c);
2686 if (isl_int_is_one(c))
2688 else if (isl_int_is_negone(c))
2689 p = isl_printer_print_str(p, "-");
2690 else {
2691 p = isl_printer_print_isl_int(p, c);
2692 p = isl_printer_print_str(p, "*");
2694 type = pos2type(ls->dim, &pos);
2695 p = print_ls_name_c(p, ls, type, pos);
2696 return p;
2699 static __isl_give isl_printer *print_ls_partial_affine_c(
2700 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2701 isl_int *c, unsigned len)
2703 int i;
2704 int first;
2706 for (i = 0, first = 1; i < len; ++i) {
2707 int flip = 0;
2708 if (isl_int_is_zero(c[i]))
2709 continue;
2710 if (!first) {
2711 if (isl_int_is_neg(c[i])) {
2712 flip = 1;
2713 isl_int_neg(c[i], c[i]);
2714 p = isl_printer_print_str(p, " - ");
2715 } else
2716 p = isl_printer_print_str(p, " + ");
2718 first = 0;
2719 p = print_ls_term_c(p, ls, c[i], i);
2720 if (flip)
2721 isl_int_neg(c[i], c[i]);
2723 if (first)
2724 p = isl_printer_print_str(p, "0");
2725 return p;
2728 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2729 __isl_keep isl_local_space *ls, isl_int *c)
2731 unsigned len = 1 + isl_local_space_dim(ls, isl_dim_all);
2732 return print_ls_partial_affine_c(p, ls, c, len);
2735 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2736 __isl_keep isl_aff *aff)
2738 unsigned total;
2740 total = isl_local_space_dim(aff->ls, isl_dim_all);
2741 if (!isl_int_is_one(aff->v->el[0]))
2742 p = isl_printer_print_str(p, "(");
2743 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2744 if (!isl_int_is_one(aff->v->el[0])) {
2745 p = isl_printer_print_str(p, ")/");
2746 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2748 return p;
2751 /* In the C format, we cannot express that "pwaff" may be undefined
2752 * on parts of the domain space. We therefore assume that the expression
2753 * will only be evaluated on its definition domain and compute the gist
2754 * of each cell with respect to this domain.
2756 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2757 __isl_keep isl_pw_aff *pwaff)
2759 isl_set *domain;
2760 isl_ast_build *build;
2761 isl_ast_expr *expr;
2763 if (pwaff->n < 1)
2764 isl_die(p->ctx, isl_error_unsupported,
2765 "cannot print empty isl_pw_aff in C format",
2766 return isl_printer_free(p));
2768 domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
2769 build = isl_ast_build_from_context(domain);
2770 expr = isl_ast_build_expr_from_pw_aff(build, isl_pw_aff_copy(pwaff));
2771 p = isl_printer_print_ast_expr(p, expr);
2772 isl_ast_expr_free(expr);
2773 isl_ast_build_free(build);
2775 return p;
2778 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2779 __isl_keep isl_aff *aff)
2781 if (!p || !aff)
2782 goto error;
2784 if (p->output_format == ISL_FORMAT_ISL)
2785 return print_aff_isl(p, aff);
2786 else if (p->output_format == ISL_FORMAT_C)
2787 return print_aff_c(p, aff);
2788 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2789 goto error);
2790 error:
2791 isl_printer_free(p);
2792 return NULL;
2795 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2796 __isl_keep isl_pw_aff *pwaff)
2798 if (!p || !pwaff)
2799 goto error;
2801 if (p->output_format == ISL_FORMAT_ISL)
2802 return print_pw_aff_isl(p, pwaff);
2803 else if (p->output_format == ISL_FORMAT_C)
2804 return print_pw_aff_c(p, pwaff);
2805 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2806 goto error);
2807 error:
2808 isl_printer_free(p);
2809 return NULL;
2812 /* Print "pa" in a sequence of isl_pw_affs delimited by semicolons.
2813 * Each isl_pw_aff itself is also printed as semicolon delimited
2814 * sequence of pieces.
2815 * If data->first = 1, then this is the first in the sequence.
2816 * Update data->first to tell the next element that it is not the first.
2818 static isl_stat print_pw_aff_body_wrap(__isl_take isl_pw_aff *pa,
2819 void *user)
2821 struct isl_union_print_data *data;
2822 data = (struct isl_union_print_data *) user;
2824 if (!data->first)
2825 data->p = isl_printer_print_str(data->p, "; ");
2826 data->first = 0;
2828 data->p = print_pw_aff_body(data->p, pa);
2829 isl_pw_aff_free(pa);
2831 return data->p ? isl_stat_ok : isl_stat_error;
2834 /* Print the body of an isl_union_pw_aff, i.e., a semicolon delimited
2835 * sequence of affine expressions, each followed by constraints,
2836 * with the sequence enclosed in braces.
2838 static __isl_give isl_printer *print_union_pw_aff_body(
2839 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2841 struct isl_union_print_data data = { p, 1 };
2843 p = isl_printer_print_str(p, s_open_set[0]);
2844 data.p = p;
2845 if (isl_union_pw_aff_foreach_pw_aff(upa,
2846 &print_pw_aff_body_wrap, &data) < 0)
2847 data.p = isl_printer_free(p);
2848 p = data.p;
2849 p = isl_printer_print_str(p, s_close_set[0]);
2851 return p;
2854 /* Print the isl_union_pw_aff "upa" to "p" in isl format.
2856 * The individual isl_pw_affs are delimited by a semicolon.
2858 static __isl_give isl_printer *print_union_pw_aff_isl(
2859 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2861 struct isl_print_space_data data = { 0 };
2862 isl_space *space;
2864 space = isl_union_pw_aff_get_space(upa);
2865 p = print_param_tuple(p, space, &data);
2866 isl_space_free(space);
2867 p = print_union_pw_aff_body(p, upa);
2868 return p;
2871 /* Print the isl_union_pw_aff "upa" to "p".
2873 * We currently only support an isl format.
2875 __isl_give isl_printer *isl_printer_print_union_pw_aff(
2876 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2878 if (!p || !upa)
2879 return isl_printer_free(p);
2881 if (p->output_format == ISL_FORMAT_ISL)
2882 return print_union_pw_aff_isl(p, upa);
2883 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2884 "unsupported output format", return isl_printer_free(p));
2887 /* Print dimension "pos" of data->space to "p".
2889 * data->user is assumed to be an isl_multi_aff.
2891 * If the current dimension is an output dimension, then print
2892 * the corresponding expression. Otherwise, print the name of the dimension.
2894 static __isl_give isl_printer *print_dim_ma(__isl_take isl_printer *p,
2895 struct isl_print_space_data *data, unsigned pos)
2897 isl_multi_aff *ma = data->user;
2899 if (data->type == isl_dim_out)
2900 p = print_aff_body(p, ma->p[pos]);
2901 else
2902 p = print_name(data->space, p, data->type, pos, data->latex);
2904 return p;
2907 static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
2908 __isl_keep isl_multi_aff *maff)
2910 struct isl_print_space_data data = { 0 };
2912 data.print_dim = &print_dim_ma;
2913 data.user = maff;
2914 return isl_print_space(maff->space, p, 0, &data);
2917 static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
2918 __isl_keep isl_multi_aff *maff)
2920 struct isl_print_space_data data = { 0 };
2922 if (!maff)
2923 goto error;
2925 p = print_param_tuple(p, maff->space, &data);
2926 p = isl_printer_print_str(p, "{ ");
2927 p = print_multi_aff(p, maff);
2928 p = isl_printer_print_str(p, " }");
2929 return p;
2930 error:
2931 isl_printer_free(p);
2932 return NULL;
2935 __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
2936 __isl_keep isl_multi_aff *maff)
2938 if (!p || !maff)
2939 goto error;
2941 if (p->output_format == ISL_FORMAT_ISL)
2942 return print_multi_aff_isl(p, maff);
2943 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2944 goto error);
2945 error:
2946 isl_printer_free(p);
2947 return NULL;
2950 static __isl_give isl_printer *print_pw_multi_aff_body(
2951 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2953 int i;
2955 if (!pma)
2956 goto error;
2958 for (i = 0; i < pma->n; ++i) {
2959 isl_space *space;
2961 if (i)
2962 p = isl_printer_print_str(p, "; ");
2963 p = print_multi_aff(p, pma->p[i].maff);
2964 space = isl_multi_aff_get_domain_space(pma->p[i].maff);
2965 p = print_disjuncts(set_to_map(pma->p[i].set), space, p, 0);
2966 isl_space_free(space);
2968 return p;
2969 error:
2970 isl_printer_free(p);
2971 return NULL;
2974 static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
2975 __isl_keep isl_pw_multi_aff *pma)
2977 struct isl_print_space_data data = { 0 };
2979 if (!pma)
2980 goto error;
2982 p = print_param_tuple(p, pma->dim, &data);
2983 p = isl_printer_print_str(p, "{ ");
2984 p = print_pw_multi_aff_body(p, pma);
2985 p = isl_printer_print_str(p, " }");
2986 return p;
2987 error:
2988 isl_printer_free(p);
2989 return NULL;
2992 /* Print the unnamed, single-dimensional piecewise multi affine expression "pma"
2993 * to "p".
2995 static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
2996 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2998 int i;
2999 isl_space *space;
3001 space = isl_pw_multi_aff_get_domain_space(pma);
3002 for (i = 0; i < pma->n - 1; ++i) {
3003 p = isl_printer_print_str(p, "(");
3004 p = print_set_c(p, space, pma->p[i].set);
3005 p = isl_printer_print_str(p, ") ? (");
3006 p = print_aff_c(p, pma->p[i].maff->p[0]);
3007 p = isl_printer_print_str(p, ") : ");
3009 isl_space_free(space);
3011 return print_aff_c(p, pma->p[pma->n - 1].maff->p[0]);
3014 static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
3015 __isl_keep isl_pw_multi_aff *pma)
3017 int n;
3018 const char *name;
3020 if (!pma)
3021 goto error;
3022 if (pma->n < 1)
3023 isl_die(p->ctx, isl_error_unsupported,
3024 "cannot print empty isl_pw_multi_aff in C format",
3025 goto error);
3026 name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
3027 if (!name && isl_pw_multi_aff_dim(pma, isl_dim_out) == 1)
3028 return print_unnamed_pw_multi_aff_c(p, pma);
3029 if (!name)
3030 isl_die(p->ctx, isl_error_unsupported,
3031 "cannot print unnamed isl_pw_multi_aff in C format",
3032 goto error);
3034 p = isl_printer_print_str(p, name);
3035 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
3036 if (n != 0)
3037 isl_die(p->ctx, isl_error_unsupported,
3038 "not supported yet", goto error);
3040 return p;
3041 error:
3042 isl_printer_free(p);
3043 return NULL;
3046 __isl_give isl_printer *isl_printer_print_pw_multi_aff(
3047 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3049 if (!p || !pma)
3050 goto error;
3052 if (p->output_format == ISL_FORMAT_ISL)
3053 return print_pw_multi_aff_isl(p, pma);
3054 if (p->output_format == ISL_FORMAT_C)
3055 return print_pw_multi_aff_c(p, pma);
3056 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3057 goto error);
3058 error:
3059 isl_printer_free(p);
3060 return NULL;
3063 static isl_stat print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
3064 void *user)
3066 struct isl_union_print_data *data;
3067 data = (struct isl_union_print_data *) user;
3069 if (!data->first)
3070 data->p = isl_printer_print_str(data->p, "; ");
3071 data->first = 0;
3073 data->p = print_pw_multi_aff_body(data->p, pma);
3074 isl_pw_multi_aff_free(pma);
3076 return isl_stat_ok;
3079 static __isl_give isl_printer *print_union_pw_multi_aff_isl(
3080 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
3082 struct isl_union_print_data data;
3083 struct isl_print_space_data space_data = { 0 };
3084 isl_space *space;
3086 space = isl_union_pw_multi_aff_get_space(upma);
3087 p = print_param_tuple(p, space, &space_data);
3088 isl_space_free(space);
3089 p = isl_printer_print_str(p, s_open_set[0]);
3090 data.p = p;
3091 data.first = 1;
3092 isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3093 &print_pw_multi_aff_body_wrap, &data);
3094 p = data.p;
3095 p = isl_printer_print_str(p, s_close_set[0]);
3096 return p;
3099 __isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
3100 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
3102 if (!p || !upma)
3103 goto error;
3105 if (p->output_format == ISL_FORMAT_ISL)
3106 return print_union_pw_multi_aff_isl(p, upma);
3107 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3108 goto error);
3109 error:
3110 isl_printer_free(p);
3111 return NULL;
3114 /* Print dimension "pos" of data->space to "p".
3116 * data->user is assumed to be an isl_multi_pw_aff.
3118 * If the current dimension is an output dimension, then print
3119 * the corresponding piecewise affine expression.
3120 * Otherwise, print the name of the dimension.
3122 static __isl_give isl_printer *print_dim_mpa(__isl_take isl_printer *p,
3123 struct isl_print_space_data *data, unsigned pos)
3125 int i;
3126 int need_parens;
3127 isl_multi_pw_aff *mpa = data->user;
3128 isl_pw_aff *pa;
3130 if (data->type != isl_dim_out)
3131 return print_name(data->space, p, data->type, pos, data->latex);
3133 pa = mpa->p[pos];
3134 if (pa->n == 0)
3135 return isl_printer_print_str(p, "(0 : false)");
3137 need_parens = pa->n != 1 || !isl_set_plain_is_universe(pa->p[0].set);
3138 if (need_parens)
3139 p = isl_printer_print_str(p, "(");
3140 for (i = 0; i < pa->n; ++i) {
3141 isl_space *space;
3143 if (i)
3144 p = isl_printer_print_str(p, "; ");
3145 p = print_aff_body(p, pa->p[i].aff);
3146 space = isl_aff_get_domain_space(pa->p[i].aff);
3147 p = print_disjuncts(pa->p[i].set, space, p, 0);
3148 isl_space_free(space);
3150 if (need_parens)
3151 p = isl_printer_print_str(p, ")");
3153 return p;
3156 /* Print "mpa" to "p" in isl format.
3158 static __isl_give isl_printer *print_multi_pw_aff_isl(__isl_take isl_printer *p,
3159 __isl_keep isl_multi_pw_aff *mpa)
3161 struct isl_print_space_data data = { 0 };
3163 if (!mpa)
3164 return isl_printer_free(p);
3166 p = print_param_tuple(p, mpa->space, &data);
3167 p = isl_printer_print_str(p, "{ ");
3168 data.print_dim = &print_dim_mpa;
3169 data.user = mpa;
3170 p = isl_print_space(mpa->space, p, 0, &data);
3171 p = isl_printer_print_str(p, " }");
3172 return p;
3175 __isl_give isl_printer *isl_printer_print_multi_pw_aff(
3176 __isl_take isl_printer *p, __isl_keep isl_multi_pw_aff *mpa)
3178 if (!p || !mpa)
3179 return isl_printer_free(p);
3181 if (p->output_format == ISL_FORMAT_ISL)
3182 return print_multi_pw_aff_isl(p, mpa);
3183 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3184 return isl_printer_free(p));
3187 /* Print dimension "pos" of data->space to "p".
3189 * data->user is assumed to be an isl_multi_val.
3191 * If the current dimension is an output dimension, then print
3192 * the corresponding value. Otherwise, print the name of the dimension.
3194 static __isl_give isl_printer *print_dim_mv(__isl_take isl_printer *p,
3195 struct isl_print_space_data *data, unsigned pos)
3197 isl_multi_val *mv = data->user;
3199 if (data->type == isl_dim_out)
3200 return isl_printer_print_val(p, mv->p[pos]);
3201 else
3202 return print_name(data->space, p, data->type, pos, data->latex);
3205 /* Print the isl_multi_val "mv" to "p" in isl format.
3207 static __isl_give isl_printer *print_multi_val_isl(__isl_take isl_printer *p,
3208 __isl_keep isl_multi_val *mv)
3210 struct isl_print_space_data data = { 0 };
3212 if (!mv)
3213 return isl_printer_free(p);
3215 p = print_param_tuple(p, mv->space, &data);
3216 p = isl_printer_print_str(p, "{ ");
3217 data.print_dim = &print_dim_mv;
3218 data.user = mv;
3219 p = isl_print_space(mv->space, p, 0, &data);
3220 p = isl_printer_print_str(p, " }");
3221 return p;
3224 /* Print the isl_multi_val "mv" to "p".
3226 * Currently only supported in isl format.
3228 __isl_give isl_printer *isl_printer_print_multi_val(
3229 __isl_take isl_printer *p, __isl_keep isl_multi_val *mv)
3231 if (!p || !mv)
3232 return isl_printer_free(p);
3234 if (p->output_format == ISL_FORMAT_ISL)
3235 return print_multi_val_isl(p, mv);
3236 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3237 return isl_printer_free(p));
3240 /* Print dimension "pos" of data->space to "p".
3242 * data->user is assumed to be an isl_multi_union_pw_aff.
3244 * The current dimension is necessarily a set dimension, so
3245 * we print the corresponding isl_union_pw_aff, including
3246 * the braces.
3248 static __isl_give isl_printer *print_union_pw_aff_dim(__isl_take isl_printer *p,
3249 struct isl_print_space_data *data, unsigned pos)
3251 isl_multi_union_pw_aff *mupa = data->user;
3252 isl_union_pw_aff *upa;
3254 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, pos);
3255 p = print_union_pw_aff_body(p, upa);
3256 isl_union_pw_aff_free(upa);
3258 return p;
3261 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3263 static __isl_give isl_printer *print_multi_union_pw_aff_isl(
3264 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3266 struct isl_print_space_data data = { 0 };
3267 isl_space *space;
3269 space = isl_multi_union_pw_aff_get_space(mupa);
3270 p = print_param_tuple(p, space, &data);
3272 data.print_dim = &print_union_pw_aff_dim;
3273 data.user = mupa;
3275 p = isl_print_space(space, p, 0, &data);
3276 isl_space_free(space);
3278 return p;
3281 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3283 * We currently only support an isl format.
3285 __isl_give isl_printer *isl_printer_print_multi_union_pw_aff(
3286 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3288 if (!p || !mupa)
3289 return isl_printer_free(p);
3291 if (p->output_format == ISL_FORMAT_ISL)
3292 return print_multi_union_pw_aff_isl(p, mupa);
3293 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
3294 "unsupported output format", return isl_printer_free(p));