isl_transitive_closure.c: has_spurious_elements: return isl_bool
[isl.git] / isl_output.c
blob0bd8bdf64700824c8dc9fdb958e42eff28f93df5
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <stdlib.h>
16 #include <string.h>
17 #include <isl_ctx_private.h>
18 #include <isl_map_private.h>
19 #include <isl/set.h>
20 #include <isl_seq.h>
21 #include <isl_polynomial_private.h>
22 #include <isl_printer_private.h>
23 #include <isl_space_private.h>
24 #include <isl_mat_private.h>
25 #include <isl_vec_private.h>
26 #include <isl/union_set.h>
27 #include <isl/union_map.h>
28 #include <isl/constraint.h>
29 #include <isl_local_space_private.h>
30 #include <isl_aff_private.h>
31 #include <isl_val_private.h>
32 #include <isl_constraint_private.h>
33 #include <isl/ast_build.h>
34 #include <isl_sort.h>
35 #include <isl_output_private.h>
37 #include <bset_to_bmap.c>
38 #include <set_to_map.c>
39 #include <uset_to_umap.c>
41 static const char *s_to[2] = { " -> ", " \\to " };
42 static const char *s_and[2] = { " and ", " \\wedge " };
43 static const char *s_or[2] = { " or ", " \\vee " };
44 static const char *s_le[2] = { "<=", "\\le" };
45 static const char *s_ge[2] = { ">=", "\\ge" };
46 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
47 static const char *s_close_set[2] = { " }", " \\,\\}" };
48 static const char *s_open_list[2] = { "[", "(" };
49 static const char *s_close_list[2] = { "]", ")" };
50 static const char *s_such_that[2] = { " : ", " \\mid " };
51 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
52 static const char *s_close_exists[2] = { ")", "" };
53 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
54 static const char *s_mod[2] = { "mod", "\\bmod" };
55 static const char *s_param_prefix[2] = { "p", "p_" };
56 static const char *s_input_prefix[2] = { "i", "i_" };
57 static const char *s_output_prefix[2] = { "o", "o_" };
59 static __isl_give isl_printer *print_constraint_polylib(
60 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
62 int i;
63 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
64 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
65 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
66 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
68 p = isl_printer_start_line(p);
69 p = isl_printer_print_int(p, ineq);
70 for (i = 0; i < n_out; ++i) {
71 p = isl_printer_print_str(p, " ");
72 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
74 for (i = 0; i < n_in; ++i) {
75 p = isl_printer_print_str(p, " ");
76 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
78 for (i = 0; i < bmap->n_div; ++i) {
79 p = isl_printer_print_str(p, " ");
80 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
82 for (i = 0; i < nparam; ++i) {
83 p = isl_printer_print_str(p, " ");
84 p = isl_printer_print_isl_int(p, c[1+i]);
86 p = isl_printer_print_str(p, " ");
87 p = isl_printer_print_isl_int(p, c[0]);
88 p = isl_printer_end_line(p);
89 return p;
92 static __isl_give isl_printer *print_constraints_polylib(
93 struct isl_basic_map *bmap, __isl_take isl_printer *p)
95 int i;
97 p = isl_printer_set_isl_int_width(p, 5);
99 for (i = 0; i < bmap->n_eq; ++i)
100 p = print_constraint_polylib(bmap, 0, i, p);
101 for (i = 0; i < bmap->n_ineq; ++i)
102 p = print_constraint_polylib(bmap, 1, i, p);
104 return p;
107 static __isl_give isl_printer *bset_print_constraints_polylib(
108 struct isl_basic_set *bset, __isl_take isl_printer *p)
110 return print_constraints_polylib(bset_to_bmap(bset), p);
113 static __isl_give isl_printer *isl_basic_map_print_polylib(
114 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
116 unsigned total = isl_basic_map_total_dim(bmap);
117 p = isl_printer_start_line(p);
118 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
119 p = isl_printer_print_str(p, " ");
120 p = isl_printer_print_int(p, 1 + total + 1);
121 if (ext) {
122 p = isl_printer_print_str(p, " ");
123 p = isl_printer_print_int(p,
124 isl_basic_map_dim(bmap, isl_dim_out));
125 p = isl_printer_print_str(p, " ");
126 p = isl_printer_print_int(p,
127 isl_basic_map_dim(bmap, isl_dim_in));
128 p = isl_printer_print_str(p, " ");
129 p = isl_printer_print_int(p,
130 isl_basic_map_dim(bmap, isl_dim_div));
131 p = isl_printer_print_str(p, " ");
132 p = isl_printer_print_int(p,
133 isl_basic_map_dim(bmap, isl_dim_param));
135 p = isl_printer_end_line(p);
136 return print_constraints_polylib(bmap, p);
139 static __isl_give isl_printer *isl_basic_set_print_polylib(
140 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
142 return isl_basic_map_print_polylib(bset_to_bmap(bset), p, ext);
145 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
146 __isl_take isl_printer *p, int ext)
148 int i;
150 p = isl_printer_start_line(p);
151 p = isl_printer_print_int(p, map->n);
152 p = isl_printer_end_line(p);
153 for (i = 0; i < map->n; ++i) {
154 p = isl_printer_start_line(p);
155 p = isl_printer_end_line(p);
156 p = isl_basic_map_print_polylib(map->p[i], p, ext);
158 return p;
161 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
162 __isl_take isl_printer *p, int ext)
164 return isl_map_print_polylib(set_to_map(set), p, ext);
167 static int count_same_name(__isl_keep isl_space *space,
168 enum isl_dim_type type, unsigned pos, const char *name)
170 enum isl_dim_type t;
171 unsigned p, s;
172 int count = 0;
174 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
175 s = t == type ? pos : isl_space_dim(space, t);
176 for (p = 0; p < s; ++p) {
177 const char *n = isl_space_get_dim_name(space, t, p);
178 if (n && !strcmp(n, name))
179 count++;
182 return count;
185 /* Print the name of the variable of type "type" and position "pos"
186 * in "space" to "p".
188 static __isl_give isl_printer *print_name(__isl_keep isl_space *space,
189 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
190 int latex)
192 const char *name;
193 char buffer[20];
194 int primes;
196 name = type == isl_dim_div ? NULL
197 : isl_space_get_dim_name(space, type, pos);
199 if (!name) {
200 const char *prefix;
201 if (type == isl_dim_param)
202 prefix = s_param_prefix[latex];
203 else if (type == isl_dim_div)
204 prefix = s_div_prefix[latex];
205 else if (isl_space_is_set(space) || type == isl_dim_in)
206 prefix = s_input_prefix[latex];
207 else
208 prefix = s_output_prefix[latex];
209 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
210 name = buffer;
212 primes = count_same_name(space, name == buffer ? isl_dim_div : type,
213 pos, name);
214 p = isl_printer_print_str(p, name);
215 while (primes-- > 0)
216 p = isl_printer_print_str(p, "'");
217 return p;
220 static enum isl_dim_type pos2type(__isl_keep isl_space *space, unsigned *pos)
222 enum isl_dim_type type;
223 unsigned n_in = isl_space_dim(space, isl_dim_in);
224 unsigned n_out = isl_space_dim(space, isl_dim_out);
225 unsigned nparam = isl_space_dim(space, isl_dim_param);
227 if (*pos < 1 + nparam) {
228 type = isl_dim_param;
229 *pos -= 1;
230 } else if (*pos < 1 + nparam + n_in) {
231 type = isl_dim_in;
232 *pos -= 1 + nparam;
233 } else if (*pos < 1 + nparam + n_in + n_out) {
234 type = isl_dim_out;
235 *pos -= 1 + nparam + n_in;
236 } else {
237 type = isl_dim_div;
238 *pos -= 1 + nparam + n_in + n_out;
241 return type;
244 /* Can the div expression of the integer division at position "row" of "div"
245 * be printed?
246 * In particular, are the div expressions available and does the selected
247 * variable have a known explicit representation?
248 * Furthermore, the Omega format does not allow any div expressions
249 * to be printed.
251 static isl_bool can_print_div_expr(__isl_keep isl_printer *p,
252 __isl_keep isl_mat *div, int pos)
254 if (p->output_format == ISL_FORMAT_OMEGA)
255 return isl_bool_false;
256 if (!div)
257 return isl_bool_false;
258 return !isl_int_is_zero(div->row[pos][0]);
261 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
262 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
264 static __isl_give isl_printer *print_term(__isl_keep isl_space *space,
265 __isl_keep isl_mat *div,
266 isl_int c, unsigned pos, __isl_take isl_printer *p, int latex)
268 enum isl_dim_type type;
269 int print_div_def;
271 if (!p || !space)
272 return isl_printer_free(p);
274 if (pos == 0)
275 return isl_printer_print_isl_int(p, c);
277 type = pos2type(space, &pos);
278 print_div_def = type == isl_dim_div && can_print_div_expr(p, div, pos);
280 if (isl_int_is_one(c))
282 else if (isl_int_is_negone(c))
283 p = isl_printer_print_str(p, "-");
284 else {
285 p = isl_printer_print_isl_int(p, c);
286 if (p->output_format == ISL_FORMAT_C || print_div_def)
287 p = isl_printer_print_str(p, "*");
289 if (print_div_def)
290 p = print_div(space, div, pos, p);
291 else
292 p = print_name(space, p, type, pos, latex);
293 return p;
296 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_space *dim,
297 __isl_keep isl_mat *div,
298 __isl_take isl_printer *p, isl_int *c, int len)
300 int i;
301 int first;
303 for (i = 0, first = 1; i < len; ++i) {
304 int flip = 0;
305 if (isl_int_is_zero(c[i]))
306 continue;
307 if (!first) {
308 if (isl_int_is_neg(c[i])) {
309 flip = 1;
310 isl_int_neg(c[i], c[i]);
311 p = isl_printer_print_str(p, " - ");
312 } else
313 p = isl_printer_print_str(p, " + ");
315 first = 0;
316 p = print_term(dim, div, c[i], i, p, 0);
317 if (flip)
318 isl_int_neg(c[i], c[i]);
320 if (first)
321 p = isl_printer_print_str(p, "0");
322 return p;
325 /* Print an affine expression "c"
326 * to "p", with the variable names taken from "space" and
327 * the integer division definitions taken from "div".
329 static __isl_give isl_printer *print_affine(__isl_take isl_printer *p,
330 __isl_keep isl_space *space, __isl_keep isl_mat *div, isl_int *c)
332 unsigned n_div;
333 unsigned len;
335 if (!space || !div)
336 return isl_printer_free(p);
337 n_div = isl_mat_rows(div);
338 len = 1 + isl_space_dim(space, isl_dim_all) + n_div;
339 return print_affine_of_len(space, div, p, c, len);
342 /* offset is the offset of local_space inside data->type of data->space.
344 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
345 __isl_keep isl_space *local_space, enum isl_dim_type local_type,
346 struct isl_print_space_data *data, int offset)
348 int i;
350 if (data->space != local_space && local_type == isl_dim_out)
351 offset += local_space->n_in;
353 for (i = 0; i < isl_space_dim(local_space, local_type); ++i) {
354 if (i)
355 p = isl_printer_print_str(p, ", ");
356 if (data->print_dim)
357 p = data->print_dim(p, data, offset + i);
358 else
359 p = print_name(data->space, p, data->type, offset + i,
360 data->latex);
362 return p;
365 static __isl_give isl_printer *print_var_list(__isl_take isl_printer *p,
366 __isl_keep isl_space *space, enum isl_dim_type type)
368 struct isl_print_space_data data = { .space = space, .type = type };
370 return print_nested_var_list(p, space, type, &data, 0);
373 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
374 __isl_keep isl_space *local_dim,
375 struct isl_print_space_data *data, int offset);
377 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
378 __isl_keep isl_space *local_space, enum isl_dim_type local_type,
379 struct isl_print_space_data *data, int offset)
381 const char *name = NULL;
382 unsigned n = isl_space_dim(local_space, local_type);
383 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
384 name = isl_space_get_tuple_name(local_space, local_type);
385 if (name) {
386 if (data->latex)
387 p = isl_printer_print_str(p, "\\mathrm{");
388 p = isl_printer_print_str(p, name);
389 if (data->latex)
390 p = isl_printer_print_str(p, "}");
393 if (!data->latex || n != 1 || name)
394 p = isl_printer_print_str(p, s_open_list[data->latex]);
395 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
396 local_space->nested[local_type - isl_dim_in]) {
397 if (data->space != local_space && local_type == isl_dim_out)
398 offset += local_space->n_in;
399 p = print_nested_map_dim(p,
400 local_space->nested[local_type - isl_dim_in],
401 data, offset);
402 } else
403 p = print_nested_var_list(p, local_space, local_type, data,
404 offset);
405 if (!data->latex || n != 1 || name)
406 p = isl_printer_print_str(p, s_close_list[data->latex]);
407 return p;
410 static __isl_give isl_printer *print_tuple(__isl_keep isl_space *dim,
411 __isl_take isl_printer *p, enum isl_dim_type type,
412 struct isl_print_space_data *data)
414 data->space = dim;
415 data->type = type;
416 return print_nested_tuple(p, dim, type, data, 0);
419 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
420 __isl_keep isl_space *local_dim,
421 struct isl_print_space_data *data, int offset)
423 p = print_nested_tuple(p, local_dim, isl_dim_in, data, offset);
424 p = isl_printer_print_str(p, s_to[data->latex]);
425 p = print_nested_tuple(p, local_dim, isl_dim_out, data, offset);
427 return p;
430 __isl_give isl_printer *isl_print_space(__isl_keep isl_space *space,
431 __isl_take isl_printer *p, int rational,
432 struct isl_print_space_data *data)
434 if (rational && !data->latex)
435 p = isl_printer_print_str(p, "rat: ");
436 if (isl_space_is_params(space))
438 else if (isl_space_is_set(space))
439 p = print_tuple(space, p, isl_dim_set, data);
440 else {
441 p = print_tuple(space, p, isl_dim_in, data);
442 p = isl_printer_print_str(p, s_to[data->latex]);
443 p = print_tuple(space, p, isl_dim_out, data);
446 return p;
449 static __isl_give isl_printer *print_omega_parameters(
450 __isl_keep isl_space *space, __isl_take isl_printer *p)
452 if (isl_space_dim(space, isl_dim_param) == 0)
453 return p;
455 p = isl_printer_start_line(p);
456 p = isl_printer_print_str(p, "symbolic ");
457 p = print_var_list(p, space, isl_dim_param);
458 p = isl_printer_print_str(p, ";");
459 p = isl_printer_end_line(p);
460 return p;
463 /* Does the inequality constraint following "i" in "bmap"
464 * have an opposite value for the same last coefficient?
465 * "last" is the position of the last coefficient of inequality "i".
466 * If the next constraint is a div constraint, then it is ignored
467 * since div constraints are not printed.
469 static int next_is_opposite(__isl_keep isl_basic_map *bmap, int i, int last)
471 unsigned total = isl_basic_map_total_dim(bmap);
472 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
474 if (i + 1 >= bmap->n_ineq)
475 return 0;
476 if (isl_seq_last_non_zero(bmap->ineq[i + 1], 1 + total) != last)
477 return 0;
478 if (last >= o_div) {
479 isl_bool is_div;
480 is_div = isl_basic_map_is_div_constraint(bmap,
481 bmap->ineq[i + 1], last - o_div);
482 if (is_div < 0)
483 return -1;
484 if (is_div)
485 return 0;
487 return isl_int_abs_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]) &&
488 !isl_int_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]);
491 /* Return a string representation of the operator used when
492 * printing a constraint where the LHS is greater than or equal to the LHS
493 * (sign > 0) or smaller than or equal to the LHS (sign < 0).
494 * If "strict" is set, then return the strict version of the comparison
495 * operator.
497 static const char *constraint_op(int sign, int strict, int latex)
499 if (strict)
500 return sign < 0 ? "<" : ">";
501 if (sign < 0)
502 return s_le[latex];
503 else
504 return s_ge[latex];
507 /* Print one side of a constraint "c" to "p", with
508 * the variable names taken from "space" and the integer division definitions
509 * taken from "div".
510 * "last" is the position of the last non-zero coefficient.
511 * Let c' be the result of zeroing out this coefficient, then
512 * the partial constraint
514 * c' op
516 * is printed.
518 static __isl_give isl_printer *print_half_constraint(__isl_take isl_printer *p,
519 __isl_keep isl_space *space, __isl_keep isl_mat *div,
520 isl_int *c, int last, const char *op, int latex)
522 isl_int_set_si(c[last], 0);
523 p = print_affine(p, space, div, c);
525 p = isl_printer_print_str(p, " ");
526 p = isl_printer_print_str(p, op);
527 p = isl_printer_print_str(p, " ");
529 return p;
532 /* Print a constraint "c" to "p", with the variable names
533 * taken from "space" and the integer division definitions taken from "div".
534 * "last" is the position of the last non-zero coefficient, which is
535 * moreover assumed to be negative.
536 * Let c' be the result of zeroing out this coefficient, then
537 * the constraint is printed in the form
539 * -c[last] op c'
541 static __isl_give isl_printer *print_constraint(__isl_take isl_printer *p,
542 __isl_keep isl_space *space, __isl_keep isl_mat *div,
543 isl_int *c, int last, const char *op, int latex)
545 isl_int_abs(c[last], c[last]);
547 p = print_term(space, div, c[last], last, p, latex);
549 p = isl_printer_print_str(p, " ");
550 p = isl_printer_print_str(p, op);
551 p = isl_printer_print_str(p, " ");
553 isl_int_set_si(c[last], 0);
554 p = print_affine(p, space, div, c);
556 return p;
559 /* Given an integer division
561 * floor(f/m)
563 * at position "pos" in "div", print the corresponding modulo expression
565 * (f) mod m
567 * to "p". The variable names are taken from "space", while any
568 * nested integer division definitions are taken from "div".
570 static __isl_give isl_printer *print_mod(__isl_take isl_printer *p,
571 __isl_keep isl_space *space, __isl_keep isl_mat *div, int pos,
572 int latex)
574 if (!p || !div)
575 return isl_printer_free(p);
577 p = isl_printer_print_str(p, "(");
578 p = print_affine_of_len(space, div, p,
579 div->row[pos] + 1, div->n_col - 1);
580 p = isl_printer_print_str(p, ") ");
581 p = isl_printer_print_str(p, s_mod[latex]);
582 p = isl_printer_print_str(p, " ");
583 p = isl_printer_print_isl_int(p, div->row[pos][0]);
584 return p;
587 /* Can the equality constraints "c" be printed as a modulo constraint?
588 * In particular, is of the form
590 * f - a m floor(g/m) = 0,
592 * with c = -a m the coefficient at position "pos"?
593 * Return the position of the corresponding integer division if so.
594 * Return the number of integer divisions if not.
595 * Return -1 on error.
597 * Modulo constraints are currently not printed in C format.
598 * Other than that, "pos" needs to correspond to an integer division
599 * with explicit representation and "c" needs to be a multiple
600 * of the denominator of the integer division.
602 static int print_as_modulo_pos(__isl_keep isl_printer *p,
603 __isl_keep isl_space *space, __isl_keep isl_mat *div, unsigned pos,
604 isl_int c)
606 isl_bool can_print;
607 unsigned n_div;
608 enum isl_dim_type type;
610 if (!p || !space)
611 return -1;
612 n_div = isl_mat_rows(div);
613 if (p->output_format == ISL_FORMAT_C)
614 return n_div;
615 type = pos2type(space, &pos);
616 if (type != isl_dim_div)
617 return n_div;
618 can_print = can_print_div_expr(p, div, pos);
619 if (can_print < 0)
620 return -1;
621 if (!can_print)
622 return n_div;
623 if (!isl_int_is_divisible_by(c, div->row[pos][0]))
624 return n_div;
625 return pos;
628 /* Print equality constraint "c" to "p" as a modulo constraint,
629 * with the variable names taken from "space" and
630 * the integer division definitions taken from "div".
631 * "last" is the position of the last non-zero coefficient, which is
632 * moreover assumed to be negative and a multiple of the denominator
633 * of the corresponding integer division. "div_pos" is the corresponding
634 * position in the sequence of integer divisions.
636 * The equality is of the form
638 * f - a m floor(g/m) = 0.
640 * Print it as
642 * a (g mod m) = -f + a g
644 static __isl_give isl_printer *print_eq_mod_constraint(
645 __isl_take isl_printer *p, __isl_keep isl_space *space,
646 __isl_keep isl_mat *div, unsigned div_pos,
647 isl_int *c, int last, int latex)
649 isl_ctx *ctx;
650 int multiple;
652 ctx = isl_printer_get_ctx(p);
653 isl_int_divexact(c[last], c[last], div->row[div_pos][0]);
654 isl_int_abs(c[last], c[last]);
655 multiple = !isl_int_is_one(c[last]);
656 if (multiple) {
657 p = isl_printer_print_isl_int(p, c[last]);
658 p = isl_printer_print_str(p, "*(");
660 p = print_mod(p, space, div, div_pos, latex);
661 if (multiple)
662 p = isl_printer_print_str(p, ")");
663 p = isl_printer_print_str(p, " = ");
664 isl_seq_combine(c, ctx->negone, c,
665 c[last], div->row[div_pos] + 1, last);
666 isl_int_set_si(c[last], 0);
667 p = print_affine(p, space, div, c);
668 return p;
671 /* Print equality constraint "c" to "p", with the variable names
672 * taken from "space" and the integer division definitions taken from "div".
673 * "last" is the position of the last non-zero coefficient, which is
674 * moreover assumed to be negative.
676 * If possible, print the equality constraint as a modulo constraint.
678 static __isl_give isl_printer *print_eq_constraint(__isl_take isl_printer *p,
679 __isl_keep isl_space *space, __isl_keep isl_mat *div, isl_int *c,
680 int last, int latex)
682 unsigned n_div;
683 int div_pos;
685 n_div = isl_mat_rows(div);
686 div_pos = print_as_modulo_pos(p, space, div, last, c[last]);
687 if (div_pos < 0)
688 return isl_printer_free(p);
689 if (div_pos < n_div)
690 return print_eq_mod_constraint(p, space, div, div_pos,
691 c, last, latex);
692 return print_constraint(p, space, div, c, last, "=", latex);
695 /* Print the constraints of "bmap" to "p".
696 * The names of the variables are taken from "space" and
697 * the integer division definitions are taken from "div".
698 * Div constraints are only printed in "dump" mode.
699 * The constraints are sorted prior to printing (except in "dump" mode).
701 * If x is the last variable with a non-zero coefficient,
702 * then a lower bound
704 * f - a x >= 0
706 * is printed as
708 * a x <= f
710 * while an upper bound
712 * f + a x >= 0
714 * is printed as
716 * a x >= -f
718 * If the next constraint has an opposite sign for the same last coefficient,
719 * then it is printed as
721 * f >= a x
723 * or
725 * -f <= a x
727 * instead. In fact, the "a x" part is not printed explicitly, but
728 * reused from the next constraint, which is therefore treated as
729 * a first constraint in the conjunction.
731 * If the constant term of "f" is -1, then "f" is replaced by "f + 1" and
732 * the comparison operator is replaced by the strict variant.
733 * Essentially, ">= 1" is replaced by "> 0".
735 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
736 __isl_keep isl_space *space, __isl_keep isl_mat *div,
737 __isl_take isl_printer *p, int latex)
739 int i;
740 isl_vec *c = NULL;
741 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
742 unsigned total = isl_basic_map_total_dim(bmap);
743 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
744 int first = 1;
745 int dump;
747 if (!p)
748 return NULL;
749 bmap = isl_basic_map_copy(bmap);
750 dump = p->dump;
751 if (!dump)
752 bmap = isl_basic_map_sort_constraints(bmap);
753 if (!bmap)
754 goto error;
756 c = isl_vec_alloc(bmap->ctx, 1 + total);
757 if (!c)
758 goto error;
760 for (i = bmap->n_eq - 1; i >= 0; --i) {
761 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
762 if (l < 0) {
763 if (i != bmap->n_eq - 1)
764 p = isl_printer_print_str(p, s_and[latex]);
765 p = isl_printer_print_str(p, "0 = 0");
766 continue;
768 if (!first)
769 p = isl_printer_print_str(p, s_and[latex]);
770 if (isl_int_is_neg(bmap->eq[i][l]))
771 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
772 else
773 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
774 p = print_eq_constraint(p, space, div, c->el, l, latex);
775 first = 0;
777 for (i = 0; i < bmap->n_ineq; ++i) {
778 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
779 int strict;
780 int s;
781 const char *op;
782 if (l < 0)
783 continue;
784 if (!dump && l >= o_div &&
785 can_print_div_expr(p, div, l - o_div)) {
786 isl_bool is_div;
787 is_div = isl_basic_map_is_div_constraint(bmap,
788 bmap->ineq[i], l - o_div);
789 if (is_div < 0)
790 goto error;
791 if (is_div)
792 continue;
794 if (!first)
795 p = isl_printer_print_str(p, s_and[latex]);
796 s = isl_int_sgn(bmap->ineq[i][l]);
797 strict = !rational && isl_int_is_negone(bmap->ineq[i][0]);
798 if (s < 0)
799 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
800 else
801 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
802 if (strict)
803 isl_int_set_si(c->el[0], 0);
804 if (!dump && next_is_opposite(bmap, i, l)) {
805 op = constraint_op(-s, strict, latex);
806 p = print_half_constraint(p, space, div, c->el, l,
807 op, latex);
808 first = 1;
809 } else {
810 op = constraint_op(s, strict, latex);
811 p = print_constraint(p, space, div, c->el, l,
812 op, latex);
813 first = 0;
817 isl_basic_map_free(bmap);
818 isl_vec_free(c);
820 return p;
821 error:
822 isl_basic_map_free(bmap);
823 isl_vec_free(c);
824 isl_printer_free(p);
825 return NULL;
828 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
829 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
831 int c;
833 if (!p || !div)
834 return isl_printer_free(p);
836 c = p->output_format == ISL_FORMAT_C;
837 p = isl_printer_print_str(p, c ? "floord(" : "floor((");
838 p = print_affine_of_len(dim, div, p,
839 div->row[pos] + 1, div->n_col - 1);
840 p = isl_printer_print_str(p, c ? ", " : ")/");
841 p = isl_printer_print_isl_int(p, div->row[pos][0]);
842 p = isl_printer_print_str(p, ")");
843 return p;
846 /* Print a comma separated list of div names, except those that have
847 * a definition that can be printed.
848 * If "print_defined_divs" is set, then those div names are printed
849 * as well, along with their definitions.
851 static __isl_give isl_printer *print_div_list(__isl_take isl_printer *p,
852 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex,
853 int print_defined_divs)
855 int i;
856 int first = 1;
857 unsigned n_div;
859 if (!p || !space || !div)
860 return isl_printer_free(p);
862 n_div = isl_mat_rows(div);
864 for (i = 0; i < n_div; ++i) {
865 if (!print_defined_divs && can_print_div_expr(p, div, i))
866 continue;
867 if (!first)
868 p = isl_printer_print_str(p, ", ");
869 p = print_name(space, p, isl_dim_div, i, latex);
870 first = 0;
871 if (!can_print_div_expr(p, div, i))
872 continue;
873 p = isl_printer_print_str(p, " = ");
874 p = print_div(space, div, i, p);
877 return p;
880 /* Does printing an object with local variables described by "div"
881 * require an "exists" clause?
882 * That is, are there any local variables without an explicit representation?
883 * An exists clause is also needed in "dump" mode because
884 * explicit div representations are not printed inline in that case.
886 static isl_bool need_exists(__isl_keep isl_printer *p, __isl_keep isl_mat *div)
888 int i, n;
890 if (!p || !div)
891 return isl_bool_error;
892 n = isl_mat_rows(div);
893 if (n == 0)
894 return isl_bool_false;
895 if (p->dump)
896 return isl_bool_true;
897 for (i = 0; i < n; ++i)
898 if (!can_print_div_expr(p, div, i))
899 return isl_bool_true;
900 return isl_bool_false;
903 /* Print the start of an exists clause, i.e.,
905 * (exists variables:
907 * In dump mode, local variables with an explicit definition are printed
908 * as well because they will not be printed inline.
910 static __isl_give isl_printer *open_exists(__isl_take isl_printer *p,
911 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex)
913 int dump;
915 if (!p)
916 return NULL;
918 dump = p->dump;
919 p = isl_printer_print_str(p, s_open_exists[latex]);
920 p = print_div_list(p, space, div, latex, dump);
921 p = isl_printer_print_str(p, ": ");
923 return p;
926 /* Remove the explicit representations of all local variables in "div".
928 static __isl_give isl_mat *mark_all_unknown(__isl_take isl_mat *div)
930 int i, n_div;
932 if (!div)
933 return NULL;
935 n_div = isl_mat_rows(div);
936 for (i = 0; i < n_div; ++i)
937 div = isl_mat_set_element_si(div, i, 0, 0);
938 return div;
941 /* Print the constraints of "bmap" to "p".
942 * The names of the variables are taken from "space".
943 * "latex" is set if the constraints should be printed in LaTeX format.
944 * Do not print inline explicit div representations in "dump" mode.
946 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
947 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
949 int dump;
950 isl_mat *div;
951 isl_bool exists;
953 if (!p)
954 return NULL;
955 dump = p->dump;
956 div = isl_basic_map_get_divs(bmap);
957 exists = need_exists(p, div);
958 if (exists >= 0 && exists)
959 p = open_exists(p, space, div, latex);
961 if (dump)
962 div = mark_all_unknown(div);
963 p = print_constraints(bmap, space, div, p, latex);
964 isl_mat_free(div);
966 if (exists >= 0 && exists)
967 p = isl_printer_print_str(p, s_close_exists[latex]);
968 return p;
971 /* Print a colon followed by the constraints of "bmap"
972 * to "p", provided there are any constraints.
973 * The names of the variables are taken from "space".
974 * "latex" is set if the constraints should be printed in LaTeX format.
976 static __isl_give isl_printer *print_optional_disjunct(
977 __isl_keep isl_basic_map *bmap, __isl_keep isl_space *space,
978 __isl_take isl_printer *p, int latex)
980 if (isl_basic_map_plain_is_universe(bmap))
981 return p;
983 p = isl_printer_print_str(p, ": ");
984 p = print_disjunct(bmap, space, p, latex);
986 return p;
989 static __isl_give isl_printer *basic_map_print_omega(
990 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
992 p = isl_printer_print_str(p, "{ [");
993 p = print_var_list(p, bmap->dim, isl_dim_in);
994 p = isl_printer_print_str(p, "] -> [");
995 p = print_var_list(p, bmap->dim, isl_dim_out);
996 p = isl_printer_print_str(p, "] ");
997 p = print_optional_disjunct(bmap, bmap->dim, p, 0);
998 p = isl_printer_print_str(p, " }");
999 return p;
1002 static __isl_give isl_printer *basic_set_print_omega(
1003 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
1005 p = isl_printer_print_str(p, "{ [");
1006 p = print_var_list(p, bset->dim, isl_dim_set);
1007 p = isl_printer_print_str(p, "] ");
1008 p = print_optional_disjunct(bset, bset->dim, p, 0);
1009 p = isl_printer_print_str(p, " }");
1010 return p;
1013 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
1014 __isl_take isl_printer *p)
1016 int i;
1018 for (i = 0; i < map->n; ++i) {
1019 if (i)
1020 p = isl_printer_print_str(p, " union ");
1021 p = basic_map_print_omega(map->p[i], p);
1023 return p;
1026 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
1027 __isl_take isl_printer *p)
1029 int i;
1031 for (i = 0; i < set->n; ++i) {
1032 if (i)
1033 p = isl_printer_print_str(p, " union ");
1034 p = basic_set_print_omega(set->p[i], p);
1036 return p;
1039 /* Print the list of parameters in "space", followed by an arrow, to "p",
1040 * if there are any parameters.
1042 static __isl_give isl_printer *print_param_tuple(__isl_take isl_printer *p,
1043 __isl_keep isl_space *space, struct isl_print_space_data *data)
1045 if (!p || !space)
1046 return isl_printer_free(p);
1047 if (isl_space_dim(space, isl_dim_param) == 0)
1048 return p;
1050 p = print_tuple(space, p, isl_dim_param, data);
1051 p = isl_printer_print_str(p, s_to[data->latex]);
1053 return p;
1056 static __isl_give isl_printer *isl_basic_map_print_isl(
1057 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
1058 int latex)
1060 struct isl_print_space_data data = { .latex = latex };
1061 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1063 p = print_param_tuple(p, bmap->dim, &data);
1064 p = isl_printer_print_str(p, "{ ");
1065 p = isl_print_space(bmap->dim, p, rational, &data);
1066 p = isl_printer_print_str(p, " : ");
1067 p = print_disjunct(bmap, bmap->dim, p, latex);
1068 p = isl_printer_print_str(p, " }");
1069 return p;
1072 /* Print the disjuncts of a map (or set) "map" to "p".
1073 * The names of the variables are taken from "space".
1074 * "latex" is set if the constraints should be printed in LaTeX format.
1076 static __isl_give isl_printer *print_disjuncts_core(__isl_keep isl_map *map,
1077 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1079 int i;
1081 if (map->n == 0)
1082 p = isl_printer_print_str(p, "false");
1083 for (i = 0; i < map->n; ++i) {
1084 if (i)
1085 p = isl_printer_print_str(p, s_or[latex]);
1086 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
1087 p = isl_printer_print_str(p, "(");
1088 p = print_disjunct(map->p[i], space, p, latex);
1089 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
1090 p = isl_printer_print_str(p, ")");
1092 return p;
1095 /* Print the disjuncts of a map (or set) "map" to "p".
1096 * The names of the variables are taken from "space".
1097 * "hull" describes constraints shared by all disjuncts of "map".
1098 * "latex" is set if the constraints should be printed in LaTeX format.
1100 * Print the disjuncts as a conjunction of "hull" and
1101 * the result of removing the constraints of "hull" from "map".
1102 * If this result turns out to be the universe, then simply print "hull".
1104 static __isl_give isl_printer *print_disjuncts_in_hull(__isl_keep isl_map *map,
1105 __isl_keep isl_space *space, __isl_take isl_basic_map *hull,
1106 __isl_take isl_printer *p, int latex)
1108 isl_bool is_universe;
1110 p = print_disjunct(hull, space, p, latex);
1111 map = isl_map_plain_gist_basic_map(isl_map_copy(map), hull);
1112 is_universe = isl_map_plain_is_universe(map);
1113 if (is_universe < 0)
1114 goto error;
1115 if (!is_universe) {
1116 p = isl_printer_print_str(p, s_and[latex]);
1117 p = isl_printer_print_str(p, "(");
1118 p = print_disjuncts_core(map, space, p, latex);
1119 p = isl_printer_print_str(p, ")");
1121 isl_map_free(map);
1123 return p;
1124 error:
1125 isl_map_free(map);
1126 isl_printer_free(p);
1127 return NULL;
1130 /* Print the disjuncts of a map (or set) "map" to "p".
1131 * The names of the variables are taken from "space".
1132 * "latex" is set if the constraints should be printed in LaTeX format.
1134 * If there are at least two disjuncts and "dump" mode is not turned out,
1135 * check for any shared constraints among all disjuncts.
1136 * If there are any, then print them separately in print_disjuncts_in_hull.
1138 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
1139 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1141 if (isl_map_plain_is_universe(map))
1142 return p;
1144 p = isl_printer_print_str(p, s_such_that[latex]);
1145 if (!p)
1146 return NULL;
1148 if (!p->dump && map->n >= 2) {
1149 isl_basic_map *hull;
1150 isl_bool is_universe;
1152 hull = isl_map_plain_unshifted_simple_hull(isl_map_copy(map));
1153 is_universe = isl_basic_map_plain_is_universe(hull);
1154 if (is_universe < 0)
1155 p = isl_printer_free(p);
1156 else if (!is_universe)
1157 return print_disjuncts_in_hull(map, space, hull,
1158 p, latex);
1159 isl_basic_map_free(hull);
1162 return print_disjuncts_core(map, space, p, latex);
1165 /* Print the disjuncts of a map (or set).
1166 * The names of the variables are taken from "space".
1167 * "latex" is set if the constraints should be printed in LaTeX format.
1169 * If the map turns out to be a universal parameter domain, then
1170 * we need to print the colon. Otherwise, the output looks identical
1171 * to the empty set.
1173 static __isl_give isl_printer *print_disjuncts_map(__isl_keep isl_map *map,
1174 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1176 if (isl_map_plain_is_universe(map) && isl_space_is_params(map->dim))
1177 return isl_printer_print_str(p, s_such_that[latex]);
1178 else
1179 return print_disjuncts(map, space, p, latex);
1182 /* Print the disjuncts of a set.
1183 * The names of the variables are taken from "space".
1184 * "latex" is set if the constraints should be printed in LaTeX format.
1186 static __isl_give isl_printer *print_disjuncts_set(__isl_keep isl_set *set,
1187 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1189 return print_disjuncts_map(set_to_map(set), space, p, latex);
1192 struct isl_aff_split {
1193 isl_basic_map *aff;
1194 isl_map *map;
1197 static void free_split(__isl_take struct isl_aff_split *split, int n)
1199 int i;
1201 if (!split)
1202 return;
1204 for (i = 0; i < n; ++i) {
1205 isl_basic_map_free(split[i].aff);
1206 isl_map_free(split[i].map);
1209 free(split);
1212 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
1214 int i, j;
1215 unsigned nparam, n_in, n_out, total;
1217 bmap = isl_basic_map_cow(bmap);
1218 if (!bmap)
1219 return NULL;
1220 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
1221 goto error;
1223 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1224 n_in = isl_basic_map_dim(bmap, isl_dim_in);
1225 n_out = isl_basic_map_dim(bmap, isl_dim_out);
1226 total = isl_basic_map_dim(bmap, isl_dim_all);
1227 for (i = bmap->n_eq - 1; i >= 0; --i) {
1228 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
1229 if (j >= nparam && j < nparam + n_in + n_out &&
1230 (isl_int_is_one(bmap->eq[i][1 + j]) ||
1231 isl_int_is_negone(bmap->eq[i][1 + j])))
1232 continue;
1233 if (isl_basic_map_drop_equality(bmap, i) < 0)
1234 goto error;
1237 bmap = isl_basic_map_finalize(bmap);
1239 return bmap;
1240 error:
1241 isl_basic_map_free(bmap);
1242 return NULL;
1245 static int aff_split_cmp(const void *p1, const void *p2, void *user)
1247 const struct isl_aff_split *s1, *s2;
1248 s1 = (const struct isl_aff_split *) p1;
1249 s2 = (const struct isl_aff_split *) p2;
1251 return isl_basic_map_plain_cmp(s1->aff, s2->aff);
1254 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
1255 __isl_keep isl_basic_map *aff)
1257 int i, j;
1258 unsigned total;
1260 if (!bmap || !aff)
1261 goto error;
1263 total = isl_space_dim(bmap->dim, isl_dim_all);
1265 for (i = bmap->n_eq - 1; i >= 0; --i) {
1266 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
1267 bmap->n_div) != -1)
1268 continue;
1269 for (j = 0; j < aff->n_eq; ++j) {
1270 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
1271 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
1272 continue;
1273 if (isl_basic_map_drop_equality(bmap, i) < 0)
1274 goto error;
1275 break;
1279 return bmap;
1280 error:
1281 isl_basic_map_free(bmap);
1282 return NULL;
1285 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
1287 int i, n;
1288 struct isl_aff_split *split;
1289 isl_ctx *ctx;
1291 ctx = isl_map_get_ctx(map);
1292 split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
1293 if (!split)
1294 return NULL;
1296 for (i = 0; i < map->n; ++i) {
1297 isl_basic_map *bmap;
1298 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
1299 bmap = isl_basic_map_copy(map->p[i]);
1300 bmap = isl_basic_map_cow(bmap);
1301 bmap = drop_aff(bmap, split[i].aff);
1302 split[i].map = isl_map_from_basic_map(bmap);
1303 if (!split[i].aff || !split[i].map)
1304 goto error;
1307 if (isl_sort(split, map->n, sizeof(struct isl_aff_split),
1308 &aff_split_cmp, NULL) < 0)
1309 goto error;
1311 n = map->n;
1312 for (i = n - 1; i >= 1; --i) {
1313 if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
1314 split[i].aff))
1315 continue;
1316 isl_basic_map_free(split[i].aff);
1317 split[i - 1].map = isl_map_union(split[i - 1].map,
1318 split[i].map);
1319 if (i != n - 1)
1320 split[i] = split[n - 1];
1321 split[n - 1].aff = NULL;
1322 split[n - 1].map = NULL;
1323 --n;
1326 return split;
1327 error:
1328 free_split(split, map->n);
1329 return NULL;
1332 static int defining_equality(__isl_keep isl_basic_map *eq,
1333 __isl_keep isl_space *dim, enum isl_dim_type type, int pos)
1335 int i;
1336 unsigned total;
1338 if (!eq)
1339 return -1;
1341 pos += isl_space_offset(dim, type);
1342 total = isl_basic_map_total_dim(eq);
1344 for (i = 0; i < eq->n_eq; ++i) {
1345 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
1346 continue;
1347 if (isl_int_is_one(eq->eq[i][1 + pos]))
1348 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
1349 return i;
1352 return -1;
1355 /* Print dimension "pos" of data->space to "p".
1357 * data->user is assumed to be an isl_basic_map keeping track of equalities.
1359 * If the current dimension is defined by these equalities, then print
1360 * the corresponding expression, assigned to the name of the dimension
1361 * if there is any. Otherwise, print the name of the dimension.
1363 static __isl_give isl_printer *print_dim_eq(__isl_take isl_printer *p,
1364 struct isl_print_space_data *data, unsigned pos)
1366 isl_basic_map *eq = data->user;
1367 int j;
1369 j = defining_equality(eq, data->space, data->type, pos);
1370 if (j >= 0) {
1371 if (isl_space_has_dim_name(data->space, data->type, pos)) {
1372 p = print_name(data->space, p, data->type, pos,
1373 data->latex);
1374 p = isl_printer_print_str(p, " = ");
1376 pos += 1 + isl_space_offset(data->space, data->type);
1377 p = print_affine_of_len(data->space, NULL, p, eq->eq[j], pos);
1378 } else {
1379 p = print_name(data->space, p, data->type, pos, data->latex);
1382 return p;
1385 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
1386 struct isl_aff_split *split, int n, __isl_keep isl_space *space)
1388 struct isl_print_space_data data = { 0 };
1389 int i;
1390 int rational;
1392 data.print_dim = &print_dim_eq;
1393 for (i = 0; i < n; ++i) {
1394 if (!split[i].map)
1395 break;
1396 rational = split[i].map->n > 0 &&
1397 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
1398 if (i)
1399 p = isl_printer_print_str(p, "; ");
1400 data.user = split[i].aff;
1401 p = isl_print_space(space, p, rational, &data);
1402 p = print_disjuncts_map(split[i].map, space, p, 0);
1405 return p;
1408 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
1409 __isl_take isl_printer *p)
1411 struct isl_print_space_data data = { 0 };
1412 struct isl_aff_split *split = NULL;
1413 int rational;
1415 if (!p || !map)
1416 return isl_printer_free(p);
1417 if (!p->dump && map->n > 0)
1418 split = split_aff(map);
1419 if (split) {
1420 p = print_split_map(p, split, map->n, map->dim);
1421 } else {
1422 rational = map->n > 0 &&
1423 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
1424 p = isl_print_space(map->dim, p, rational, &data);
1425 p = print_disjuncts_map(map, map->dim, p, 0);
1427 free_split(split, map->n);
1428 return p;
1431 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
1432 __isl_take isl_printer *p)
1434 struct isl_print_space_data data = { 0 };
1436 p = print_param_tuple(p, map->dim, &data);
1437 p = isl_printer_print_str(p, s_open_set[0]);
1438 p = isl_map_print_isl_body(map, p);
1439 p = isl_printer_print_str(p, s_close_set[0]);
1440 return p;
1443 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
1444 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff)
1446 struct isl_print_space_data data = { 0 };
1448 data.latex = 1;
1449 p = print_param_tuple(p, map->dim, &data);
1450 p = isl_printer_print_str(p, s_open_set[1]);
1451 data.print_dim = &print_dim_eq;
1452 data.user = aff;
1453 p = isl_print_space(map->dim, p, 0, &data);
1454 p = print_disjuncts_map(map, map->dim, p, 1);
1455 p = isl_printer_print_str(p, s_close_set[1]);
1457 return p;
1460 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
1461 __isl_take isl_printer *p)
1463 int i;
1464 struct isl_aff_split *split = NULL;
1466 if (map->n > 0)
1467 split = split_aff(map);
1469 if (!split)
1470 return print_latex_map(map, p, NULL);
1472 for (i = 0; i < map->n; ++i) {
1473 if (!split[i].map)
1474 break;
1475 if (i)
1476 p = isl_printer_print_str(p, " \\cup ");
1477 p = print_latex_map(split[i].map, p, split[i].aff);
1480 free_split(split, map->n);
1481 return p;
1484 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
1485 __isl_keep isl_basic_map *bmap)
1487 if (!p || !bmap)
1488 goto error;
1489 if (p->output_format == ISL_FORMAT_ISL)
1490 return isl_basic_map_print_isl(bmap, p, 0);
1491 else if (p->output_format == ISL_FORMAT_OMEGA)
1492 return basic_map_print_omega(bmap, p);
1493 isl_assert(bmap->ctx, 0, goto error);
1494 error:
1495 isl_printer_free(p);
1496 return NULL;
1499 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
1500 __isl_keep isl_basic_set *bset)
1502 if (!p || !bset)
1503 goto error;
1505 if (p->output_format == ISL_FORMAT_ISL)
1506 return isl_basic_map_print_isl(bset, p, 0);
1507 else if (p->output_format == ISL_FORMAT_POLYLIB)
1508 return isl_basic_set_print_polylib(bset, p, 0);
1509 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1510 return isl_basic_set_print_polylib(bset, p, 1);
1511 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
1512 return bset_print_constraints_polylib(bset, p);
1513 else if (p->output_format == ISL_FORMAT_OMEGA)
1514 return basic_set_print_omega(bset, p);
1515 isl_assert(p->ctx, 0, goto error);
1516 error:
1517 isl_printer_free(p);
1518 return NULL;
1521 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
1522 __isl_keep isl_set *set)
1524 if (!p || !set)
1525 goto error;
1526 if (p->output_format == ISL_FORMAT_ISL)
1527 return isl_map_print_isl(set_to_map(set), p);
1528 else if (p->output_format == ISL_FORMAT_POLYLIB)
1529 return isl_set_print_polylib(set, p, 0);
1530 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1531 return isl_set_print_polylib(set, p, 1);
1532 else if (p->output_format == ISL_FORMAT_OMEGA)
1533 return isl_set_print_omega(set, p);
1534 else if (p->output_format == ISL_FORMAT_LATEX)
1535 return isl_map_print_latex(set_to_map(set), p);
1536 isl_assert(set->ctx, 0, goto error);
1537 error:
1538 isl_printer_free(p);
1539 return NULL;
1542 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1543 __isl_keep isl_map *map)
1545 if (!p || !map)
1546 goto error;
1548 if (p->output_format == ISL_FORMAT_ISL)
1549 return isl_map_print_isl(map, p);
1550 else if (p->output_format == ISL_FORMAT_POLYLIB)
1551 return isl_map_print_polylib(map, p, 0);
1552 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1553 return isl_map_print_polylib(map, p, 1);
1554 else if (p->output_format == ISL_FORMAT_OMEGA)
1555 return isl_map_print_omega(map, p);
1556 else if (p->output_format == ISL_FORMAT_LATEX)
1557 return isl_map_print_latex(map, p);
1558 isl_assert(map->ctx, 0, goto error);
1559 error:
1560 isl_printer_free(p);
1561 return NULL;
1564 struct isl_union_print_data {
1565 isl_printer *p;
1566 int first;
1569 static isl_stat print_map_body(__isl_take isl_map *map, void *user)
1571 struct isl_union_print_data *data;
1572 data = (struct isl_union_print_data *)user;
1574 if (!data->first)
1575 data->p = isl_printer_print_str(data->p, "; ");
1576 data->first = 0;
1578 data->p = isl_map_print_isl_body(map, data->p);
1579 isl_map_free(map);
1581 return isl_stat_ok;
1584 /* Print the body of "umap" (everything except the parameter declarations)
1585 * to "p" in isl format.
1587 static __isl_give isl_printer *isl_printer_print_union_map_isl_body(
1588 __isl_take isl_printer *p, __isl_keep isl_union_map *umap)
1590 struct isl_union_print_data data;
1592 p = isl_printer_print_str(p, s_open_set[0]);
1593 data.p = p;
1594 data.first = 1;
1595 isl_union_map_foreach_map(umap, &print_map_body, &data);
1596 p = data.p;
1597 p = isl_printer_print_str(p, s_close_set[0]);
1598 return p;
1601 /* Print the body of "uset" (everything except the parameter declarations)
1602 * to "p" in isl format.
1604 static __isl_give isl_printer *isl_printer_print_union_set_isl_body(
1605 __isl_take isl_printer *p, __isl_keep isl_union_set *uset)
1607 return isl_printer_print_union_map_isl_body(p, uset_to_umap(uset));
1610 /* Print the isl_union_map "umap" to "p" in isl format.
1612 static __isl_give isl_printer *isl_union_map_print_isl(
1613 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1615 struct isl_print_space_data space_data = { 0 };
1616 isl_space *space;
1618 space = isl_union_map_get_space(umap);
1619 p = print_param_tuple(p, space, &space_data);
1620 isl_space_free(space);
1622 p = isl_printer_print_union_map_isl_body(p, umap);
1624 return p;
1627 static isl_stat print_latex_map_body(__isl_take isl_map *map, void *user)
1629 struct isl_union_print_data *data;
1630 data = (struct isl_union_print_data *)user;
1632 if (!data->first)
1633 data->p = isl_printer_print_str(data->p, " \\cup ");
1634 data->first = 0;
1636 data->p = isl_map_print_latex(map, data->p);
1637 isl_map_free(map);
1639 return isl_stat_ok;
1642 static __isl_give isl_printer *isl_union_map_print_latex(
1643 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1645 struct isl_union_print_data data = { p, 1 };
1646 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1647 p = data.p;
1648 return p;
1651 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1652 __isl_keep isl_union_map *umap)
1654 if (!p || !umap)
1655 goto error;
1657 if (p->output_format == ISL_FORMAT_ISL)
1658 return isl_union_map_print_isl(umap, p);
1659 if (p->output_format == ISL_FORMAT_LATEX)
1660 return isl_union_map_print_latex(umap, p);
1662 isl_die(p->ctx, isl_error_invalid,
1663 "invalid output format for isl_union_map", goto error);
1664 error:
1665 isl_printer_free(p);
1666 return NULL;
1669 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1670 __isl_keep isl_union_set *uset)
1672 if (!p || !uset)
1673 goto error;
1675 if (p->output_format == ISL_FORMAT_ISL)
1676 return isl_union_map_print_isl(uset_to_umap(uset), p);
1677 if (p->output_format == ISL_FORMAT_LATEX)
1678 return isl_union_map_print_latex(uset_to_umap(uset), p);
1680 isl_die(p->ctx, isl_error_invalid,
1681 "invalid output format for isl_union_set", goto error);
1682 error:
1683 isl_printer_free(p);
1684 return NULL;
1687 static int poly_rec_n_non_zero(__isl_keep isl_poly_rec *rec)
1689 int i;
1690 int n;
1692 if (!rec)
1693 return -1;
1695 for (i = 0, n = 0; i < rec->n; ++i) {
1696 isl_bool is_zero = isl_poly_is_zero(rec->p[i]);
1698 if (is_zero < 0)
1699 return -1;
1700 if (!is_zero)
1701 ++n;
1704 return n;
1707 static __isl_give isl_printer *poly_print_cst(__isl_keep isl_poly *poly,
1708 __isl_take isl_printer *p, int first)
1710 isl_poly_cst *cst;
1711 int neg;
1713 cst = isl_poly_as_cst(poly);
1714 if (!cst)
1715 goto error;
1716 neg = !first && isl_int_is_neg(cst->n);
1717 if (!first)
1718 p = isl_printer_print_str(p, neg ? " - " : " + ");
1719 if (neg)
1720 isl_int_neg(cst->n, cst->n);
1721 if (isl_int_is_zero(cst->d)) {
1722 int sgn = isl_int_sgn(cst->n);
1723 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1724 sgn == 0 ? "NaN" : "infty");
1725 } else
1726 p = isl_printer_print_isl_int(p, cst->n);
1727 if (neg)
1728 isl_int_neg(cst->n, cst->n);
1729 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1730 p = isl_printer_print_str(p, "/");
1731 p = isl_printer_print_isl_int(p, cst->d);
1733 return p;
1734 error:
1735 isl_printer_free(p);
1736 return NULL;
1739 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1740 __isl_keep isl_space *space, __isl_keep isl_mat *div, int var)
1742 unsigned total;
1744 total = isl_space_dim(space, isl_dim_all);
1745 if (var < total)
1746 p = print_term(space, NULL, space->ctx->one, 1 + var, p, 0);
1747 else
1748 p = print_div(space, div, var - total, p);
1749 return p;
1752 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1753 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var, int exp)
1755 p = print_base(p, dim, div, var);
1756 if (exp == 1)
1757 return p;
1758 if (p->output_format == ISL_FORMAT_C) {
1759 int i;
1760 for (i = 1; i < exp; ++i) {
1761 p = isl_printer_print_str(p, "*");
1762 p = print_base(p, dim, div, var);
1764 } else {
1765 p = isl_printer_print_str(p, "^");
1766 p = isl_printer_print_int(p, exp);
1768 return p;
1771 /* Print the polynomial "poly" defined over the domain space "space" and
1772 * local variables defined by "div" to "p".
1774 static __isl_give isl_printer *poly_print(__isl_keep isl_poly *poly,
1775 __isl_keep isl_space *space, __isl_keep isl_mat *div,
1776 __isl_take isl_printer *p)
1778 int i, n, first, print_parens;
1779 isl_bool is_cst;
1780 isl_poly_rec *rec;
1782 is_cst = isl_poly_is_cst(poly);
1783 if (!p || is_cst < 0 || !space || !div)
1784 goto error;
1786 if (is_cst)
1787 return poly_print_cst(poly, p, 1);
1789 rec = isl_poly_as_rec(poly);
1790 n = poly_rec_n_non_zero(rec);
1791 if (n < 0)
1792 return isl_printer_free(p);
1793 print_parens = n > 1;
1794 if (print_parens)
1795 p = isl_printer_print_str(p, "(");
1796 for (i = 0, first = 1; i < rec->n; ++i) {
1797 isl_bool is_zero = isl_poly_is_zero(rec->p[i]);
1798 isl_bool is_one = isl_poly_is_one(rec->p[i]);
1799 isl_bool is_negone = isl_poly_is_negone(rec->p[i]);
1800 isl_bool is_cst = isl_poly_is_cst(rec->p[i]);
1802 if (is_zero < 0 || is_one < 0 || is_negone < 0)
1803 return isl_printer_free(p);
1804 if (is_zero)
1805 continue;
1806 if (is_negone) {
1807 if (!i)
1808 p = isl_printer_print_str(p, "-1");
1809 else if (first)
1810 p = isl_printer_print_str(p, "-");
1811 else
1812 p = isl_printer_print_str(p, " - ");
1813 } else if (is_cst && !is_one)
1814 p = poly_print_cst(rec->p[i], p, first);
1815 else {
1816 if (!first)
1817 p = isl_printer_print_str(p, " + ");
1818 if (i == 0 || !is_one)
1819 p = poly_print(rec->p[i], space, div, p);
1821 first = 0;
1822 if (i == 0)
1823 continue;
1824 if (!is_one && !is_negone)
1825 p = isl_printer_print_str(p, " * ");
1826 p = print_pow(p, space, div, rec->poly.var, i);
1828 if (print_parens)
1829 p = isl_printer_print_str(p, ")");
1830 return p;
1831 error:
1832 isl_printer_free(p);
1833 return NULL;
1836 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1837 __isl_keep isl_qpolynomial *qp)
1839 if (!p || !qp)
1840 goto error;
1841 p = poly_print(qp->poly, qp->dim, qp->div, p);
1842 return p;
1843 error:
1844 isl_printer_free(p);
1845 return NULL;
1848 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1849 __isl_keep isl_qpolynomial *qp)
1851 struct isl_print_space_data data = { 0 };
1853 if (!p || !qp)
1854 goto error;
1856 p = print_param_tuple(p, qp->dim, &data);
1857 p = isl_printer_print_str(p, "{ ");
1858 if (!isl_space_is_params(qp->dim)) {
1859 p = isl_print_space(qp->dim, p, 0, &data);
1860 p = isl_printer_print_str(p, " -> ");
1862 p = print_qpolynomial(p, qp);
1863 p = isl_printer_print_str(p, " }");
1864 return p;
1865 error:
1866 isl_printer_free(p);
1867 return NULL;
1870 /* Print the quasi-polynomial "qp" to "p" in C format, with the variable names
1871 * taken from the domain space "space".
1873 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1874 __isl_keep isl_space *space, __isl_keep isl_qpolynomial *qp)
1876 isl_bool is_one;
1877 isl_val *den;
1879 den = isl_qpolynomial_get_den(qp);
1880 qp = isl_qpolynomial_copy(qp);
1881 qp = isl_qpolynomial_scale_val(qp, isl_val_copy(den));
1882 is_one = isl_val_is_one(den);
1883 if (is_one < 0)
1884 p = isl_printer_free(p);
1885 if (!is_one)
1886 p = isl_printer_print_str(p, "(");
1887 if (qp)
1888 p = poly_print(qp->poly, space, qp->div, p);
1889 else
1890 p = isl_printer_free(p);
1891 if (!is_one) {
1892 p = isl_printer_print_str(p, ")/");
1893 p = isl_printer_print_val(p, den);
1895 isl_qpolynomial_free(qp);
1896 isl_val_free(den);
1897 return p;
1900 __isl_give isl_printer *isl_printer_print_qpolynomial(
1901 __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1903 if (!p || !qp)
1904 goto error;
1906 if (p->output_format == ISL_FORMAT_ISL)
1907 return print_qpolynomial_isl(p, qp);
1908 else if (p->output_format == ISL_FORMAT_C)
1909 return print_qpolynomial_c(p, qp->dim, qp);
1910 else
1911 isl_die(qp->dim->ctx, isl_error_unsupported,
1912 "output format not supported for isl_qpolynomials",
1913 goto error);
1914 error:
1915 isl_printer_free(p);
1916 return NULL;
1919 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1920 unsigned output_format)
1922 isl_printer *p;
1924 if (!qp)
1925 return;
1927 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1928 p = isl_printer_to_file(qp->dim->ctx, out);
1929 p = isl_printer_print_qpolynomial(p, qp);
1930 isl_printer_free(p);
1933 static __isl_give isl_printer *qpolynomial_fold_print(
1934 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1936 int i;
1938 if (fold->type == isl_fold_min)
1939 p = isl_printer_print_str(p, "min");
1940 else if (fold->type == isl_fold_max)
1941 p = isl_printer_print_str(p, "max");
1942 p = isl_printer_print_str(p, "(");
1943 for (i = 0; i < fold->n; ++i) {
1944 if (i)
1945 p = isl_printer_print_str(p, ", ");
1946 p = print_qpolynomial(p, fold->qp[i]);
1948 p = isl_printer_print_str(p, ")");
1949 return p;
1952 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1953 FILE *out, unsigned output_format)
1955 isl_printer *p;
1957 if (!fold)
1958 return;
1960 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1962 p = isl_printer_to_file(fold->dim->ctx, out);
1963 p = isl_printer_print_qpolynomial_fold(p, fold);
1965 isl_printer_free(p);
1968 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1969 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1971 struct isl_print_space_data data = { 0 };
1972 int i = 0;
1974 for (i = 0; i < pwqp->n; ++i) {
1975 isl_space *space;
1977 if (i)
1978 p = isl_printer_print_str(p, "; ");
1979 space = isl_qpolynomial_get_domain_space(pwqp->p[i].qp);
1980 if (!isl_space_is_params(space)) {
1981 p = isl_print_space(space, p, 0, &data);
1982 p = isl_printer_print_str(p, " -> ");
1984 p = print_qpolynomial(p, pwqp->p[i].qp);
1985 p = print_disjuncts(set_to_map(pwqp->p[i].set), space, p, 0);
1986 isl_space_free(space);
1989 return p;
1992 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1993 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1995 struct isl_print_space_data data = { 0 };
1997 if (!p || !pwqp)
1998 goto error;
2000 p = print_param_tuple(p, pwqp->dim, &data);
2001 p = isl_printer_print_str(p, "{ ");
2002 if (pwqp->n == 0) {
2003 if (!isl_space_is_set(pwqp->dim)) {
2004 p = print_tuple(pwqp->dim, p, isl_dim_in, &data);
2005 p = isl_printer_print_str(p, " -> ");
2007 p = isl_printer_print_str(p, "0");
2009 p = isl_pwqp_print_isl_body(p, pwqp);
2010 p = isl_printer_print_str(p, " }");
2011 return p;
2012 error:
2013 isl_printer_free(p);
2014 return NULL;
2017 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
2018 unsigned output_format)
2020 isl_printer *p;
2022 if (!pwqp)
2023 return;
2025 p = isl_printer_to_file(pwqp->dim->ctx, out);
2026 p = isl_printer_set_output_format(p, output_format);
2027 p = isl_printer_print_pw_qpolynomial(p, pwqp);
2029 isl_printer_free(p);
2032 static __isl_give isl_printer *isl_pwf_print_isl_body(
2033 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2035 struct isl_print_space_data data = { 0 };
2036 int i = 0;
2038 for (i = 0; i < pwf->n; ++i) {
2039 isl_space *space;
2041 if (i)
2042 p = isl_printer_print_str(p, "; ");
2043 space = isl_qpolynomial_fold_get_domain_space(pwf->p[i].fold);
2044 if (!isl_space_is_params(space)) {
2045 p = isl_print_space(space, p, 0, &data);
2046 p = isl_printer_print_str(p, " -> ");
2048 p = qpolynomial_fold_print(pwf->p[i].fold, p);
2049 p = print_disjuncts(set_to_map(pwf->p[i].set), space, p, 0);
2050 isl_space_free(space);
2053 return p;
2056 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
2057 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2059 struct isl_print_space_data data = { 0 };
2061 p = print_param_tuple(p, pwf->dim, &data);
2062 p = isl_printer_print_str(p, "{ ");
2063 if (pwf->n == 0) {
2064 if (!isl_space_is_set(pwf->dim)) {
2065 p = print_tuple(pwf->dim, p, isl_dim_in, &data);
2066 p = isl_printer_print_str(p, " -> ");
2068 p = isl_printer_print_str(p, "0");
2070 p = isl_pwf_print_isl_body(p, pwf);
2071 p = isl_printer_print_str(p, " }");
2072 return p;
2075 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
2076 __isl_keep isl_space *space, __isl_keep isl_basic_set *bset,
2077 isl_int *c);
2079 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
2080 __isl_keep isl_space *dim,
2081 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
2083 if (type == isl_dim_div) {
2084 p = isl_printer_print_str(p, "floord(");
2085 p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
2086 p = isl_printer_print_str(p, ", ");
2087 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
2088 p = isl_printer_print_str(p, ")");
2089 } else {
2090 const char *name;
2092 name = isl_space_get_dim_name(dim, type, pos);
2093 if (!name)
2094 name = "UNNAMED";
2095 p = isl_printer_print_str(p, name);
2097 return p;
2100 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
2101 __isl_keep isl_space *space,
2102 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
2104 enum isl_dim_type type;
2106 if (!p || !space)
2107 return isl_printer_free(p);
2109 if (pos == 0)
2110 return isl_printer_print_isl_int(p, c);
2112 if (isl_int_is_one(c))
2114 else if (isl_int_is_negone(c))
2115 p = isl_printer_print_str(p, "-");
2116 else {
2117 p = isl_printer_print_isl_int(p, c);
2118 p = isl_printer_print_str(p, "*");
2120 type = pos2type(space, &pos);
2121 p = print_name_c(p, space, bset, type, pos);
2122 return p;
2125 static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
2126 __isl_keep isl_space *dim,
2127 __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
2129 int i;
2130 int first;
2132 for (i = 0, first = 1; i < len; ++i) {
2133 int flip = 0;
2134 if (isl_int_is_zero(c[i]))
2135 continue;
2136 if (!first) {
2137 if (isl_int_is_neg(c[i])) {
2138 flip = 1;
2139 isl_int_neg(c[i], c[i]);
2140 p = isl_printer_print_str(p, " - ");
2141 } else
2142 p = isl_printer_print_str(p, " + ");
2144 first = 0;
2145 p = print_term_c(p, dim, bset, c[i], i);
2146 if (flip)
2147 isl_int_neg(c[i], c[i]);
2149 if (first)
2150 p = isl_printer_print_str(p, "0");
2151 return p;
2154 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
2155 __isl_keep isl_space *space, __isl_keep isl_basic_set *bset, isl_int *c)
2157 unsigned len = 1 + isl_basic_set_total_dim(bset);
2158 return print_partial_affine_c(p, space, bset, c, len);
2161 /* We skip the constraint if it is implied by the div expression.
2163 * *first indicates whether this is the first constraint in the conjunction and
2164 * is updated if the constraint is actually printed.
2166 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
2167 __isl_keep isl_space *dim,
2168 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int *first)
2170 unsigned o_div;
2171 unsigned n_div;
2172 int div;
2174 o_div = isl_basic_set_offset(bset, isl_dim_div);
2175 n_div = isl_basic_set_dim(bset, isl_dim_div);
2176 div = isl_seq_last_non_zero(c + o_div, n_div);
2177 if (div >= 0) {
2178 isl_bool is_div = isl_basic_set_is_div_constraint(bset, c, div);
2179 if (is_div < 0)
2180 return isl_printer_free(p);
2181 if (is_div)
2182 return p;
2185 if (!*first)
2186 p = isl_printer_print_str(p, " && ");
2188 p = print_affine_c(p, dim, bset, c);
2189 p = isl_printer_print_str(p, " ");
2190 p = isl_printer_print_str(p, op);
2191 p = isl_printer_print_str(p, " 0");
2193 *first = 0;
2195 return p;
2198 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
2199 __isl_keep isl_space *space, __isl_keep isl_basic_set *bset)
2201 int i, j;
2202 int first = 1;
2203 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
2204 unsigned total = isl_basic_set_total_dim(bset) - n_div;
2206 for (i = 0; i < bset->n_eq; ++i) {
2207 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
2208 if (j < 0)
2209 p = print_constraint_c(p, space, bset,
2210 bset->eq[i], "==", &first);
2211 else {
2212 if (i)
2213 p = isl_printer_print_str(p, " && ");
2214 p = isl_printer_print_str(p, "(");
2215 p = print_partial_affine_c(p, space, bset, bset->eq[i],
2216 1 + total + j);
2217 p = isl_printer_print_str(p, ") % ");
2218 p = isl_printer_print_isl_int(p,
2219 bset->eq[i][1 + total + j]);
2220 p = isl_printer_print_str(p, " == 0");
2221 first = 0;
2224 for (i = 0; i < bset->n_ineq; ++i)
2225 p = print_constraint_c(p, space, bset, bset->ineq[i], ">=",
2226 &first);
2227 return p;
2230 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
2231 __isl_keep isl_space *space, __isl_keep isl_set *set)
2233 int i;
2235 if (!set)
2236 return isl_printer_free(p);
2238 if (set->n == 0)
2239 p = isl_printer_print_str(p, "0");
2241 for (i = 0; i < set->n; ++i) {
2242 if (i)
2243 p = isl_printer_print_str(p, " || ");
2244 if (set->n > 1)
2245 p = isl_printer_print_str(p, "(");
2246 p = print_basic_set_c(p, space, set->p[i]);
2247 if (set->n > 1)
2248 p = isl_printer_print_str(p, ")");
2250 return p;
2253 /* Print the piecewise quasi-polynomial "pwqp" to "p" in C format.
2255 static __isl_give isl_printer *print_pw_qpolynomial_c(
2256 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2258 int i;
2259 isl_space *space;
2261 space = isl_pw_qpolynomial_get_domain_space(pwqp);
2262 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set)) {
2263 p = print_qpolynomial_c(p, space, pwqp->p[0].qp);
2264 isl_space_free(space);
2265 return p;
2268 for (i = 0; i < pwqp->n; ++i) {
2269 p = isl_printer_print_str(p, "(");
2270 p = print_set_c(p, space, pwqp->p[i].set);
2271 p = isl_printer_print_str(p, ") ? (");
2272 p = print_qpolynomial_c(p, space, pwqp->p[i].qp);
2273 p = isl_printer_print_str(p, ") : ");
2276 isl_space_free(space);
2277 p = isl_printer_print_str(p, "0");
2278 return p;
2281 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
2282 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2284 if (!p || !pwqp)
2285 goto error;
2287 if (p->output_format == ISL_FORMAT_ISL)
2288 return print_pw_qpolynomial_isl(p, pwqp);
2289 else if (p->output_format == ISL_FORMAT_C)
2290 return print_pw_qpolynomial_c(p, pwqp);
2291 isl_assert(p->ctx, 0, goto error);
2292 error:
2293 isl_printer_free(p);
2294 return NULL;
2297 static isl_stat print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
2299 struct isl_union_print_data *data;
2300 data = (struct isl_union_print_data *)user;
2302 if (!data->first)
2303 data->p = isl_printer_print_str(data->p, "; ");
2304 data->first = 0;
2306 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
2307 isl_pw_qpolynomial_free(pwqp);
2309 return isl_stat_ok;
2312 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
2313 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2315 struct isl_union_print_data data;
2316 struct isl_print_space_data space_data = { 0 };
2317 isl_space *space;
2319 space = isl_union_pw_qpolynomial_get_space(upwqp);
2320 p = print_param_tuple(p, space, &space_data);
2321 isl_space_free(space);
2322 p = isl_printer_print_str(p, "{ ");
2323 data.p = p;
2324 data.first = 1;
2325 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
2326 &data);
2327 p = data.p;
2328 p = isl_printer_print_str(p, " }");
2329 return p;
2332 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
2333 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2335 if (!p || !upwqp)
2336 goto error;
2338 if (p->output_format == ISL_FORMAT_ISL)
2339 return print_union_pw_qpolynomial_isl(p, upwqp);
2340 isl_die(p->ctx, isl_error_invalid,
2341 "invalid output format for isl_union_pw_qpolynomial",
2342 goto error);
2343 error:
2344 isl_printer_free(p);
2345 return NULL;
2348 /* Print the quasi-polynomial reduction "fold" to "p" in C format,
2349 * with the variable names taken from the domain space "space".
2351 static __isl_give isl_printer *print_qpolynomial_fold_c(
2352 __isl_take isl_printer *p, __isl_keep isl_space *space,
2353 __isl_keep isl_qpolynomial_fold *fold)
2355 int i;
2357 for (i = 0; i < fold->n - 1; ++i)
2358 if (fold->type == isl_fold_min)
2359 p = isl_printer_print_str(p, "min(");
2360 else if (fold->type == isl_fold_max)
2361 p = isl_printer_print_str(p, "max(");
2363 for (i = 0; i < fold->n; ++i) {
2364 if (i)
2365 p = isl_printer_print_str(p, ", ");
2366 p = print_qpolynomial_c(p, space, fold->qp[i]);
2367 if (i)
2368 p = isl_printer_print_str(p, ")");
2370 return p;
2373 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
2374 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
2376 if (!p || !fold)
2377 goto error;
2378 if (p->output_format == ISL_FORMAT_ISL)
2379 return qpolynomial_fold_print(fold, p);
2380 else if (p->output_format == ISL_FORMAT_C)
2381 return print_qpolynomial_fold_c(p, fold->dim, fold);
2382 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2383 goto error);
2384 error:
2385 isl_printer_free(p);
2386 return NULL;
2389 /* Print the piecewise quasi-polynomial reduction "pwf" to "p" in C format.
2391 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
2392 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2394 int i;
2395 isl_space *space;
2397 space = isl_pw_qpolynomial_fold_get_domain_space(pwf);
2398 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set)) {
2399 p = print_qpolynomial_fold_c(p, space, pwf->p[0].fold);
2400 isl_space_free(space);
2401 return p;
2404 for (i = 0; i < pwf->n; ++i) {
2405 p = isl_printer_print_str(p, "(");
2406 p = print_set_c(p, space, pwf->p[i].set);
2407 p = isl_printer_print_str(p, ") ? (");
2408 p = print_qpolynomial_fold_c(p, space, pwf->p[i].fold);
2409 p = isl_printer_print_str(p, ") : ");
2412 isl_space_free(space);
2413 p = isl_printer_print_str(p, "0");
2414 return p;
2417 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
2418 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2420 if (!p || !pwf)
2421 goto error;
2423 if (p->output_format == ISL_FORMAT_ISL)
2424 return print_pw_qpolynomial_fold_isl(p, pwf);
2425 else if (p->output_format == ISL_FORMAT_C)
2426 return print_pw_qpolynomial_fold_c(p, pwf);
2427 isl_assert(p->ctx, 0, goto error);
2428 error:
2429 isl_printer_free(p);
2430 return NULL;
2433 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
2434 FILE *out, unsigned output_format)
2436 isl_printer *p;
2438 if (!pwf)
2439 return;
2441 p = isl_printer_to_file(pwf->dim->ctx, out);
2442 p = isl_printer_set_output_format(p, output_format);
2443 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
2445 isl_printer_free(p);
2448 static isl_stat print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf,
2449 void *user)
2451 struct isl_union_print_data *data;
2452 data = (struct isl_union_print_data *)user;
2454 if (!data->first)
2455 data->p = isl_printer_print_str(data->p, "; ");
2456 data->first = 0;
2458 data->p = isl_pwf_print_isl_body(data->p, pwf);
2459 isl_pw_qpolynomial_fold_free(pwf);
2461 return isl_stat_ok;
2464 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
2465 __isl_take isl_printer *p,
2466 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2468 struct isl_union_print_data data;
2469 struct isl_print_space_data space_data = { 0 };
2470 isl_space *space;
2472 space = isl_union_pw_qpolynomial_fold_get_space(upwf);
2473 p = print_param_tuple(p, space, &space_data);
2474 isl_space_free(space);
2475 p = isl_printer_print_str(p, "{ ");
2476 data.p = p;
2477 data.first = 1;
2478 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
2479 &print_pwf_body, &data);
2480 p = data.p;
2481 p = isl_printer_print_str(p, " }");
2482 return p;
2485 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2486 __isl_take isl_printer *p,
2487 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2489 if (!p || !upwf)
2490 goto error;
2492 if (p->output_format == ISL_FORMAT_ISL)
2493 return print_union_pw_qpolynomial_fold_isl(p, upwf);
2494 isl_die(p->ctx, isl_error_invalid,
2495 "invalid output format for isl_union_pw_qpolynomial_fold",
2496 goto error);
2497 error:
2498 isl_printer_free(p);
2499 return NULL;
2502 /* Print the isl_constraint "c" to "p".
2504 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
2505 __isl_keep isl_constraint *c)
2507 struct isl_print_space_data data = { 0 };
2508 isl_local_space *ls;
2509 isl_space *space;
2510 isl_bool exists;
2512 if (!p || !c)
2513 goto error;
2515 ls = isl_constraint_get_local_space(c);
2516 if (!ls)
2517 return isl_printer_free(p);
2518 space = isl_local_space_get_space(ls);
2519 p = print_param_tuple(p, space, &data);
2520 p = isl_printer_print_str(p, "{ ");
2521 p = isl_print_space(space, p, 0, &data);
2522 p = isl_printer_print_str(p, " : ");
2523 exists = need_exists(p, ls->div);
2524 if (exists < 0)
2525 p = isl_printer_free(p);
2526 if (exists >= 0 && exists)
2527 p = open_exists(p, space, ls->div, 0);
2528 p = print_affine_of_len(space, ls->div, p, c->v->el, c->v->size);
2529 if (isl_constraint_is_equality(c))
2530 p = isl_printer_print_str(p, " = 0");
2531 else
2532 p = isl_printer_print_str(p, " >= 0");
2533 if (exists >= 0 && exists)
2534 p = isl_printer_print_str(p, s_close_exists[0]);
2535 p = isl_printer_print_str(p, " }");
2536 isl_space_free(space);
2537 isl_local_space_free(ls);
2539 return p;
2540 error:
2541 isl_printer_free(p);
2542 return NULL;
2545 static __isl_give isl_printer *isl_printer_print_space_isl(
2546 __isl_take isl_printer *p, __isl_keep isl_space *space)
2548 struct isl_print_space_data data = { 0 };
2550 if (!space)
2551 goto error;
2553 p = print_param_tuple(p, space, &data);
2555 p = isl_printer_print_str(p, "{ ");
2556 if (isl_space_is_params(space))
2557 p = isl_printer_print_str(p, s_such_that[0]);
2558 else
2559 p = isl_print_space(space, p, 0, &data);
2560 p = isl_printer_print_str(p, " }");
2562 return p;
2563 error:
2564 isl_printer_free(p);
2565 return NULL;
2568 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
2569 __isl_keep isl_space *space)
2571 if (!p || !space)
2572 return isl_printer_free(p);
2573 if (p->output_format == ISL_FORMAT_ISL)
2574 return isl_printer_print_space_isl(p, space);
2575 else if (p->output_format == ISL_FORMAT_OMEGA)
2576 return print_omega_parameters(space, p);
2578 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
2579 "output format not supported for space",
2580 return isl_printer_free(p));
2583 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
2584 __isl_keep isl_local_space *ls)
2586 struct isl_print_space_data data = { 0 };
2587 unsigned n_div;
2589 if (!ls)
2590 goto error;
2592 p = print_param_tuple(p, ls->dim, &data);
2593 p = isl_printer_print_str(p, "{ ");
2594 p = isl_print_space(ls->dim, p, 0, &data);
2595 n_div = isl_local_space_dim(ls, isl_dim_div);
2596 if (n_div > 0) {
2597 p = isl_printer_print_str(p, " : ");
2598 p = isl_printer_print_str(p, s_open_exists[0]);
2599 p = print_div_list(p, ls->dim, ls->div, 0, 1);
2600 p = isl_printer_print_str(p, s_close_exists[0]);
2601 } else if (isl_space_is_params(ls->dim))
2602 p = isl_printer_print_str(p, s_such_that[0]);
2603 p = isl_printer_print_str(p, " }");
2604 return p;
2605 error:
2606 isl_printer_free(p);
2607 return NULL;
2610 /* Print the (potentially rational) affine expression "aff" to "p",
2611 * with the variable names taken from "space".
2613 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2614 __isl_keep isl_space *space, __isl_keep isl_aff *aff)
2616 unsigned total;
2618 if (isl_aff_is_nan(aff))
2619 return isl_printer_print_str(p, "NaN");
2621 total = isl_local_space_dim(aff->ls, isl_dim_all);
2622 p = isl_printer_print_str(p, "(");
2623 p = print_affine_of_len(space, aff->ls->div, p,
2624 aff->v->el + 1, 1 + total);
2625 if (isl_int_is_one(aff->v->el[0]))
2626 p = isl_printer_print_str(p, ")");
2627 else {
2628 p = isl_printer_print_str(p, ")/");
2629 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2632 return p;
2635 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2636 __isl_keep isl_aff *aff)
2638 struct isl_print_space_data data = { 0 };
2640 if (isl_space_is_params(aff->ls->dim))
2642 else {
2643 p = print_tuple(aff->ls->dim, p, isl_dim_set, &data);
2644 p = isl_printer_print_str(p, " -> ");
2646 p = isl_printer_print_str(p, "[");
2647 p = print_aff_body(p, aff->ls->dim, aff);
2648 p = isl_printer_print_str(p, "]");
2650 return p;
2653 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2654 __isl_keep isl_aff *aff)
2656 struct isl_print_space_data data = { 0 };
2658 if (!aff)
2659 goto error;
2661 p = print_param_tuple(p, aff->ls->dim, &data);
2662 p = isl_printer_print_str(p, "{ ");
2663 p = print_aff(p, aff);
2664 p = isl_printer_print_str(p, " }");
2665 return p;
2666 error:
2667 isl_printer_free(p);
2668 return NULL;
2671 /* Print the body of an isl_pw_aff, i.e., a semicolon delimited
2672 * sequence of affine expressions, each followed by constraints.
2674 static __isl_give isl_printer *print_pw_aff_body(
2675 __isl_take isl_printer *p, __isl_keep isl_pw_aff *pa)
2677 int i;
2679 if (!pa)
2680 return isl_printer_free(p);
2682 for (i = 0; i < pa->n; ++i) {
2683 isl_space *space;
2685 if (i)
2686 p = isl_printer_print_str(p, "; ");
2687 p = print_aff(p, pa->p[i].aff);
2688 space = isl_aff_get_domain_space(pa->p[i].aff);
2689 p = print_disjuncts(set_to_map(pa->p[i].set), space, p, 0);
2690 isl_space_free(space);
2693 return p;
2696 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2697 __isl_keep isl_pw_aff *pwaff)
2699 struct isl_print_space_data data = { 0 };
2701 if (!pwaff)
2702 goto error;
2704 p = print_param_tuple(p, pwaff->dim, &data);
2705 p = isl_printer_print_str(p, "{ ");
2706 p = print_pw_aff_body(p, pwaff);
2707 p = isl_printer_print_str(p, " }");
2708 return p;
2709 error:
2710 isl_printer_free(p);
2711 return NULL;
2714 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2715 __isl_keep isl_local_space *ls, isl_int *c);
2717 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2718 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2720 if (type == isl_dim_div) {
2721 p = isl_printer_print_str(p, "floord(");
2722 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2723 p = isl_printer_print_str(p, ", ");
2724 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2725 p = isl_printer_print_str(p, ")");
2726 } else {
2727 const char *name;
2729 name = isl_space_get_dim_name(ls->dim, type, pos);
2730 if (!name)
2731 name = "UNNAMED";
2732 p = isl_printer_print_str(p, name);
2734 return p;
2737 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2738 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2740 enum isl_dim_type type;
2742 if (!p || !ls)
2743 return isl_printer_free(p);
2745 if (pos == 0)
2746 return isl_printer_print_isl_int(p, c);
2748 if (isl_int_is_one(c))
2750 else if (isl_int_is_negone(c))
2751 p = isl_printer_print_str(p, "-");
2752 else {
2753 p = isl_printer_print_isl_int(p, c);
2754 p = isl_printer_print_str(p, "*");
2756 type = pos2type(ls->dim, &pos);
2757 p = print_ls_name_c(p, ls, type, pos);
2758 return p;
2761 static __isl_give isl_printer *print_ls_partial_affine_c(
2762 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2763 isl_int *c, unsigned len)
2765 int i;
2766 int first;
2768 for (i = 0, first = 1; i < len; ++i) {
2769 int flip = 0;
2770 if (isl_int_is_zero(c[i]))
2771 continue;
2772 if (!first) {
2773 if (isl_int_is_neg(c[i])) {
2774 flip = 1;
2775 isl_int_neg(c[i], c[i]);
2776 p = isl_printer_print_str(p, " - ");
2777 } else
2778 p = isl_printer_print_str(p, " + ");
2780 first = 0;
2781 p = print_ls_term_c(p, ls, c[i], i);
2782 if (flip)
2783 isl_int_neg(c[i], c[i]);
2785 if (first)
2786 p = isl_printer_print_str(p, "0");
2787 return p;
2790 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2791 __isl_keep isl_local_space *ls, isl_int *c)
2793 unsigned len = 1 + isl_local_space_dim(ls, isl_dim_all);
2794 return print_ls_partial_affine_c(p, ls, c, len);
2797 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2798 __isl_keep isl_aff *aff)
2800 unsigned total;
2802 total = isl_local_space_dim(aff->ls, isl_dim_all);
2803 if (!isl_int_is_one(aff->v->el[0]))
2804 p = isl_printer_print_str(p, "(");
2805 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2806 if (!isl_int_is_one(aff->v->el[0])) {
2807 p = isl_printer_print_str(p, ")/");
2808 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2810 return p;
2813 /* In the C format, we cannot express that "pwaff" may be undefined
2814 * on parts of the domain space. We therefore assume that the expression
2815 * will only be evaluated on its definition domain and compute the gist
2816 * of each cell with respect to this domain.
2818 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2819 __isl_keep isl_pw_aff *pwaff)
2821 isl_set *domain;
2822 isl_ast_build *build;
2823 isl_ast_expr *expr;
2825 if (pwaff->n < 1)
2826 isl_die(p->ctx, isl_error_unsupported,
2827 "cannot print empty isl_pw_aff in C format",
2828 return isl_printer_free(p));
2830 domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
2831 build = isl_ast_build_from_context(domain);
2832 expr = isl_ast_build_expr_from_pw_aff(build, isl_pw_aff_copy(pwaff));
2833 p = isl_printer_print_ast_expr(p, expr);
2834 isl_ast_expr_free(expr);
2835 isl_ast_build_free(build);
2837 return p;
2840 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2841 __isl_keep isl_aff *aff)
2843 if (!p || !aff)
2844 goto error;
2846 if (p->output_format == ISL_FORMAT_ISL)
2847 return print_aff_isl(p, aff);
2848 else if (p->output_format == ISL_FORMAT_C)
2849 return print_aff_c(p, aff);
2850 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2851 goto error);
2852 error:
2853 isl_printer_free(p);
2854 return NULL;
2857 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2858 __isl_keep isl_pw_aff *pwaff)
2860 if (!p || !pwaff)
2861 goto error;
2863 if (p->output_format == ISL_FORMAT_ISL)
2864 return print_pw_aff_isl(p, pwaff);
2865 else if (p->output_format == ISL_FORMAT_C)
2866 return print_pw_aff_c(p, pwaff);
2867 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2868 goto error);
2869 error:
2870 isl_printer_free(p);
2871 return NULL;
2874 /* Print "pa" in a sequence of isl_pw_affs delimited by semicolons.
2875 * Each isl_pw_aff itself is also printed as semicolon delimited
2876 * sequence of pieces.
2877 * If data->first = 1, then this is the first in the sequence.
2878 * Update data->first to tell the next element that it is not the first.
2880 static isl_stat print_pw_aff_body_wrap(__isl_take isl_pw_aff *pa,
2881 void *user)
2883 struct isl_union_print_data *data;
2884 data = (struct isl_union_print_data *) user;
2886 if (!data->first)
2887 data->p = isl_printer_print_str(data->p, "; ");
2888 data->first = 0;
2890 data->p = print_pw_aff_body(data->p, pa);
2891 isl_pw_aff_free(pa);
2893 return data->p ? isl_stat_ok : isl_stat_error;
2896 /* Print the body of an isl_union_pw_aff, i.e., a semicolon delimited
2897 * sequence of affine expressions, each followed by constraints,
2898 * with the sequence enclosed in braces.
2900 static __isl_give isl_printer *print_union_pw_aff_body(
2901 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2903 struct isl_union_print_data data = { p, 1 };
2905 p = isl_printer_print_str(p, s_open_set[0]);
2906 data.p = p;
2907 if (isl_union_pw_aff_foreach_pw_aff(upa,
2908 &print_pw_aff_body_wrap, &data) < 0)
2909 data.p = isl_printer_free(p);
2910 p = data.p;
2911 p = isl_printer_print_str(p, s_close_set[0]);
2913 return p;
2916 /* Print the isl_union_pw_aff "upa" to "p" in isl format.
2918 * The individual isl_pw_affs are delimited by a semicolon.
2920 static __isl_give isl_printer *print_union_pw_aff_isl(
2921 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2923 struct isl_print_space_data data = { 0 };
2924 isl_space *space;
2926 space = isl_union_pw_aff_get_space(upa);
2927 p = print_param_tuple(p, space, &data);
2928 isl_space_free(space);
2929 p = print_union_pw_aff_body(p, upa);
2930 return p;
2933 /* Print the isl_union_pw_aff "upa" to "p".
2935 * We currently only support an isl format.
2937 __isl_give isl_printer *isl_printer_print_union_pw_aff(
2938 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2940 if (!p || !upa)
2941 return isl_printer_free(p);
2943 if (p->output_format == ISL_FORMAT_ISL)
2944 return print_union_pw_aff_isl(p, upa);
2945 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2946 "unsupported output format", return isl_printer_free(p));
2949 /* Print dimension "pos" of data->space to "p".
2951 * data->user is assumed to be an isl_multi_aff.
2953 * If the current dimension is an output dimension, then print
2954 * the corresponding expression. Otherwise, print the name of the dimension.
2956 static __isl_give isl_printer *print_dim_ma(__isl_take isl_printer *p,
2957 struct isl_print_space_data *data, unsigned pos)
2959 isl_multi_aff *ma = data->user;
2961 if (data->type == isl_dim_out) {
2962 isl_space *space;
2964 space = isl_multi_aff_get_domain_space(ma);
2965 p = print_aff_body(p, space, ma->u.p[pos]);
2966 isl_space_free(space);
2967 } else {
2968 p = print_name(data->space, p, data->type, pos, data->latex);
2971 return p;
2974 static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
2975 __isl_keep isl_multi_aff *maff)
2977 struct isl_print_space_data data = { 0 };
2979 data.print_dim = &print_dim_ma;
2980 data.user = maff;
2981 return isl_print_space(maff->space, p, 0, &data);
2984 static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
2985 __isl_keep isl_multi_aff *maff)
2987 struct isl_print_space_data data = { 0 };
2989 if (!maff)
2990 goto error;
2992 p = print_param_tuple(p, maff->space, &data);
2993 p = isl_printer_print_str(p, "{ ");
2994 p = print_multi_aff(p, maff);
2995 p = isl_printer_print_str(p, " }");
2996 return p;
2997 error:
2998 isl_printer_free(p);
2999 return NULL;
3002 __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
3003 __isl_keep isl_multi_aff *maff)
3005 if (!p || !maff)
3006 goto error;
3008 if (p->output_format == ISL_FORMAT_ISL)
3009 return print_multi_aff_isl(p, maff);
3010 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3011 goto error);
3012 error:
3013 isl_printer_free(p);
3014 return NULL;
3017 static __isl_give isl_printer *print_pw_multi_aff_body(
3018 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3020 int i;
3022 if (!pma)
3023 goto error;
3025 for (i = 0; i < pma->n; ++i) {
3026 isl_space *space;
3028 if (i)
3029 p = isl_printer_print_str(p, "; ");
3030 p = print_multi_aff(p, pma->p[i].maff);
3031 space = isl_multi_aff_get_domain_space(pma->p[i].maff);
3032 p = print_disjuncts(set_to_map(pma->p[i].set), space, p, 0);
3033 isl_space_free(space);
3035 return p;
3036 error:
3037 isl_printer_free(p);
3038 return NULL;
3041 static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
3042 __isl_keep isl_pw_multi_aff *pma)
3044 struct isl_print_space_data data = { 0 };
3046 if (!pma)
3047 goto error;
3049 p = print_param_tuple(p, pma->dim, &data);
3050 p = isl_printer_print_str(p, "{ ");
3051 p = print_pw_multi_aff_body(p, pma);
3052 p = isl_printer_print_str(p, " }");
3053 return p;
3054 error:
3055 isl_printer_free(p);
3056 return NULL;
3059 /* Print the unnamed, single-dimensional piecewise multi affine expression "pma"
3060 * to "p".
3062 static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
3063 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3065 int i;
3066 isl_space *space;
3068 space = isl_pw_multi_aff_get_domain_space(pma);
3069 for (i = 0; i < pma->n - 1; ++i) {
3070 p = isl_printer_print_str(p, "(");
3071 p = print_set_c(p, space, pma->p[i].set);
3072 p = isl_printer_print_str(p, ") ? (");
3073 p = print_aff_c(p, pma->p[i].maff->u.p[0]);
3074 p = isl_printer_print_str(p, ") : ");
3076 isl_space_free(space);
3078 return print_aff_c(p, pma->p[pma->n - 1].maff->u.p[0]);
3081 static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
3082 __isl_keep isl_pw_multi_aff *pma)
3084 int n;
3085 const char *name;
3087 if (!pma)
3088 goto error;
3089 if (pma->n < 1)
3090 isl_die(p->ctx, isl_error_unsupported,
3091 "cannot print empty isl_pw_multi_aff in C format",
3092 goto error);
3093 name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
3094 if (!name && isl_pw_multi_aff_dim(pma, isl_dim_out) == 1)
3095 return print_unnamed_pw_multi_aff_c(p, pma);
3096 if (!name)
3097 isl_die(p->ctx, isl_error_unsupported,
3098 "cannot print unnamed isl_pw_multi_aff in C format",
3099 goto error);
3101 p = isl_printer_print_str(p, name);
3102 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
3103 if (n != 0)
3104 isl_die(p->ctx, isl_error_unsupported,
3105 "not supported yet", goto error);
3107 return p;
3108 error:
3109 isl_printer_free(p);
3110 return NULL;
3113 __isl_give isl_printer *isl_printer_print_pw_multi_aff(
3114 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3116 if (!p || !pma)
3117 goto error;
3119 if (p->output_format == ISL_FORMAT_ISL)
3120 return print_pw_multi_aff_isl(p, pma);
3121 if (p->output_format == ISL_FORMAT_C)
3122 return print_pw_multi_aff_c(p, pma);
3123 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3124 goto error);
3125 error:
3126 isl_printer_free(p);
3127 return NULL;
3130 static isl_stat print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
3131 void *user)
3133 struct isl_union_print_data *data;
3134 data = (struct isl_union_print_data *) user;
3136 if (!data->first)
3137 data->p = isl_printer_print_str(data->p, "; ");
3138 data->first = 0;
3140 data->p = print_pw_multi_aff_body(data->p, pma);
3141 isl_pw_multi_aff_free(pma);
3143 return isl_stat_ok;
3146 static __isl_give isl_printer *print_union_pw_multi_aff_isl(
3147 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
3149 struct isl_union_print_data data;
3150 struct isl_print_space_data space_data = { 0 };
3151 isl_space *space;
3153 space = isl_union_pw_multi_aff_get_space(upma);
3154 p = print_param_tuple(p, space, &space_data);
3155 isl_space_free(space);
3156 p = isl_printer_print_str(p, s_open_set[0]);
3157 data.p = p;
3158 data.first = 1;
3159 isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3160 &print_pw_multi_aff_body_wrap, &data);
3161 p = data.p;
3162 p = isl_printer_print_str(p, s_close_set[0]);
3163 return p;
3166 __isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
3167 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
3169 if (!p || !upma)
3170 goto error;
3172 if (p->output_format == ISL_FORMAT_ISL)
3173 return print_union_pw_multi_aff_isl(p, upma);
3174 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3175 goto error);
3176 error:
3177 isl_printer_free(p);
3178 return NULL;
3181 /* Print dimension "pos" of data->space to "p".
3183 * data->user is assumed to be an isl_multi_pw_aff.
3185 * If the current dimension is an output dimension, then print
3186 * the corresponding piecewise affine expression.
3187 * Otherwise, print the name of the dimension.
3189 static __isl_give isl_printer *print_dim_mpa(__isl_take isl_printer *p,
3190 struct isl_print_space_data *data, unsigned pos)
3192 int i;
3193 int need_parens;
3194 isl_space *space;
3195 isl_multi_pw_aff *mpa = data->user;
3196 isl_pw_aff *pa;
3198 if (data->type != isl_dim_out)
3199 return print_name(data->space, p, data->type, pos, data->latex);
3201 pa = mpa->u.p[pos];
3202 if (pa->n == 0)
3203 return isl_printer_print_str(p, "(0 : false)");
3205 need_parens = pa->n != 1 || !isl_set_plain_is_universe(pa->p[0].set);
3206 if (need_parens)
3207 p = isl_printer_print_str(p, "(");
3208 space = isl_multi_pw_aff_get_domain_space(mpa);
3209 for (i = 0; i < pa->n; ++i) {
3211 if (i)
3212 p = isl_printer_print_str(p, "; ");
3213 p = print_aff_body(p, space, pa->p[i].aff);
3214 p = print_disjuncts(pa->p[i].set, space, p, 0);
3216 isl_space_free(space);
3217 if (need_parens)
3218 p = isl_printer_print_str(p, ")");
3220 return p;
3223 /* Print "mpa" to "p" in isl format.
3225 * If "mpa" is zero-dimensional and has a non-trivial explicit domain,
3226 * then it is printed after the tuple of affine expressions.
3228 static __isl_give isl_printer *print_multi_pw_aff_isl(__isl_take isl_printer *p,
3229 __isl_keep isl_multi_pw_aff *mpa)
3231 struct isl_print_space_data data = { 0 };
3232 isl_bool has_domain;
3234 if (!mpa)
3235 return isl_printer_free(p);
3237 p = print_param_tuple(p, mpa->space, &data);
3238 p = isl_printer_print_str(p, "{ ");
3239 data.print_dim = &print_dim_mpa;
3240 data.user = mpa;
3241 p = isl_print_space(mpa->space, p, 0, &data);
3242 has_domain = isl_multi_pw_aff_has_non_trivial_domain(mpa);
3243 if (has_domain < 0)
3244 return isl_printer_free(p);
3245 if (has_domain) {
3246 isl_space *space;
3248 space = isl_space_domain(isl_space_copy(mpa->space));
3249 p = print_disjuncts_set(mpa->u.dom, space, p, 0);
3250 isl_space_free(space);
3252 p = isl_printer_print_str(p, " }");
3253 return p;
3256 __isl_give isl_printer *isl_printer_print_multi_pw_aff(
3257 __isl_take isl_printer *p, __isl_keep isl_multi_pw_aff *mpa)
3259 if (!p || !mpa)
3260 return isl_printer_free(p);
3262 if (p->output_format == ISL_FORMAT_ISL)
3263 return print_multi_pw_aff_isl(p, mpa);
3264 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3265 return isl_printer_free(p));
3268 /* Print dimension "pos" of data->space to "p".
3270 * data->user is assumed to be an isl_multi_val.
3272 * If the current dimension is an output dimension, then print
3273 * the corresponding value. Otherwise, print the name of the dimension.
3275 static __isl_give isl_printer *print_dim_mv(__isl_take isl_printer *p,
3276 struct isl_print_space_data *data, unsigned pos)
3278 isl_multi_val *mv = data->user;
3280 if (data->type == isl_dim_out)
3281 return isl_printer_print_val(p, mv->u.p[pos]);
3282 else
3283 return print_name(data->space, p, data->type, pos, data->latex);
3286 /* Print the isl_multi_val "mv" to "p" in isl format.
3288 static __isl_give isl_printer *print_multi_val_isl(__isl_take isl_printer *p,
3289 __isl_keep isl_multi_val *mv)
3291 struct isl_print_space_data data = { 0 };
3293 if (!mv)
3294 return isl_printer_free(p);
3296 p = print_param_tuple(p, mv->space, &data);
3297 p = isl_printer_print_str(p, "{ ");
3298 data.print_dim = &print_dim_mv;
3299 data.user = mv;
3300 p = isl_print_space(mv->space, p, 0, &data);
3301 p = isl_printer_print_str(p, " }");
3302 return p;
3305 /* Print the isl_multi_val "mv" to "p".
3307 * Currently only supported in isl format.
3309 __isl_give isl_printer *isl_printer_print_multi_val(
3310 __isl_take isl_printer *p, __isl_keep isl_multi_val *mv)
3312 if (!p || !mv)
3313 return isl_printer_free(p);
3315 if (p->output_format == ISL_FORMAT_ISL)
3316 return print_multi_val_isl(p, mv);
3317 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3318 return isl_printer_free(p));
3321 /* Print dimension "pos" of data->space to "p".
3323 * data->user is assumed to be an isl_multi_union_pw_aff.
3325 * The current dimension is necessarily a set dimension, so
3326 * we print the corresponding isl_union_pw_aff, including
3327 * the braces.
3329 static __isl_give isl_printer *print_union_pw_aff_dim(__isl_take isl_printer *p,
3330 struct isl_print_space_data *data, unsigned pos)
3332 isl_multi_union_pw_aff *mupa = data->user;
3333 isl_union_pw_aff *upa;
3335 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, pos);
3336 p = print_union_pw_aff_body(p, upa);
3337 isl_union_pw_aff_free(upa);
3339 return p;
3342 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3344 * If "mupa" is zero-dimensional and has a non-trivial explicit domain,
3345 * then it is printed after the tuple of affine expressions.
3346 * In order to clarify that this domain belongs to the expression,
3347 * the tuple along with the domain are placed inside parentheses.
3348 * If "mupa" has any parameters, then the opening parenthesis
3349 * appears after the parameter declarations.
3351 static __isl_give isl_printer *print_multi_union_pw_aff_isl(
3352 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3354 struct isl_print_space_data data = { 0 };
3355 isl_bool has_domain;
3356 isl_space *space;
3358 if (!mupa)
3359 return isl_printer_free(p);
3360 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
3361 if (has_domain < 0)
3362 return isl_printer_free(p);
3364 space = isl_multi_union_pw_aff_get_space(mupa);
3365 p = print_param_tuple(p, space, &data);
3367 if (has_domain)
3368 p = isl_printer_print_str(p, "(");
3370 data.print_dim = &print_union_pw_aff_dim;
3371 data.user = mupa;
3373 p = isl_print_space(space, p, 0, &data);
3374 isl_space_free(space);
3376 if (has_domain) {
3377 p = isl_printer_print_str(p, " : ");
3378 p = isl_printer_print_union_set_isl_body(p, mupa->u.dom);
3379 p = isl_printer_print_str(p, ")");
3382 return p;
3385 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3387 * We currently only support an isl format.
3389 __isl_give isl_printer *isl_printer_print_multi_union_pw_aff(
3390 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3392 if (!p || !mupa)
3393 return isl_printer_free(p);
3395 if (p->output_format == ISL_FORMAT_ISL)
3396 return print_multi_union_pw_aff_isl(p, mupa);
3397 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
3398 "unsupported output format", return isl_printer_free(p));