add_upper_div_constraint: return return modified result
[isl.git] / isl_output.c
blobea4d915b41b1293d124590ddc392a8caa3a005cb
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <stdlib.h>
16 #include <string.h>
17 #include <isl_ctx_private.h>
18 #include <isl_map_private.h>
19 #include <isl/set.h>
20 #include <isl_seq.h>
21 #include <isl_polynomial_private.h>
22 #include <isl_printer_private.h>
23 #include <isl_space_private.h>
24 #include <isl_mat_private.h>
25 #include <isl_vec_private.h>
26 #include <isl/union_set.h>
27 #include <isl/union_map.h>
28 #include <isl/constraint.h>
29 #include <isl_local_space_private.h>
30 #include <isl_aff_private.h>
31 #include <isl_val_private.h>
32 #include <isl_constraint_private.h>
33 #include <isl/ast_build.h>
34 #include <isl_sort.h>
35 #include <isl_output_private.h>
37 #include <bset_to_bmap.c>
38 #include <set_to_map.c>
39 #include <uset_to_umap.c>
41 static const char *s_to[2] = { " -> ", " \\to " };
42 static const char *s_and[2] = { " and ", " \\wedge " };
43 static const char *s_or[2] = { " or ", " \\vee " };
44 static const char *s_le[2] = { "<=", "\\le" };
45 static const char *s_ge[2] = { ">=", "\\ge" };
46 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
47 static const char *s_close_set[2] = { " }", " \\,\\}" };
48 static const char *s_open_list[2] = { "[", "(" };
49 static const char *s_close_list[2] = { "]", ")" };
50 static const char *s_such_that[2] = { " : ", " \\mid " };
51 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
52 static const char *s_close_exists[2] = { ")", "" };
53 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
54 static const char *s_mod[2] = { "mod", "\\bmod" };
55 static const char *s_param_prefix[2] = { "p", "p_" };
56 static const char *s_input_prefix[2] = { "i", "i_" };
57 static const char *s_output_prefix[2] = { "o", "o_" };
59 static __isl_give isl_printer *print_constraint_polylib(
60 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
62 int i;
63 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
64 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
65 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
66 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
68 p = isl_printer_start_line(p);
69 p = isl_printer_print_int(p, ineq);
70 for (i = 0; i < n_out; ++i) {
71 p = isl_printer_print_str(p, " ");
72 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
74 for (i = 0; i < n_in; ++i) {
75 p = isl_printer_print_str(p, " ");
76 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
78 for (i = 0; i < bmap->n_div; ++i) {
79 p = isl_printer_print_str(p, " ");
80 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
82 for (i = 0; i < nparam; ++i) {
83 p = isl_printer_print_str(p, " ");
84 p = isl_printer_print_isl_int(p, c[1+i]);
86 p = isl_printer_print_str(p, " ");
87 p = isl_printer_print_isl_int(p, c[0]);
88 p = isl_printer_end_line(p);
89 return p;
92 static __isl_give isl_printer *print_constraints_polylib(
93 struct isl_basic_map *bmap, __isl_take isl_printer *p)
95 int i;
97 p = isl_printer_set_isl_int_width(p, 5);
99 for (i = 0; i < bmap->n_eq; ++i)
100 p = print_constraint_polylib(bmap, 0, i, p);
101 for (i = 0; i < bmap->n_ineq; ++i)
102 p = print_constraint_polylib(bmap, 1, i, p);
104 return p;
107 static __isl_give isl_printer *bset_print_constraints_polylib(
108 struct isl_basic_set *bset, __isl_take isl_printer *p)
110 return print_constraints_polylib(bset_to_bmap(bset), p);
113 static __isl_give isl_printer *isl_basic_map_print_polylib(
114 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
116 unsigned total = isl_basic_map_total_dim(bmap);
117 p = isl_printer_start_line(p);
118 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
119 p = isl_printer_print_str(p, " ");
120 p = isl_printer_print_int(p, 1 + total + 1);
121 if (ext) {
122 p = isl_printer_print_str(p, " ");
123 p = isl_printer_print_int(p,
124 isl_basic_map_dim(bmap, isl_dim_out));
125 p = isl_printer_print_str(p, " ");
126 p = isl_printer_print_int(p,
127 isl_basic_map_dim(bmap, isl_dim_in));
128 p = isl_printer_print_str(p, " ");
129 p = isl_printer_print_int(p,
130 isl_basic_map_dim(bmap, isl_dim_div));
131 p = isl_printer_print_str(p, " ");
132 p = isl_printer_print_int(p,
133 isl_basic_map_dim(bmap, isl_dim_param));
135 p = isl_printer_end_line(p);
136 return print_constraints_polylib(bmap, p);
139 static __isl_give isl_printer *isl_basic_set_print_polylib(
140 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
142 return isl_basic_map_print_polylib(bset_to_bmap(bset), p, ext);
145 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
146 __isl_take isl_printer *p, int ext)
148 int i;
150 p = isl_printer_start_line(p);
151 p = isl_printer_print_int(p, map->n);
152 p = isl_printer_end_line(p);
153 for (i = 0; i < map->n; ++i) {
154 p = isl_printer_start_line(p);
155 p = isl_printer_end_line(p);
156 p = isl_basic_map_print_polylib(map->p[i], p, ext);
158 return p;
161 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
162 __isl_take isl_printer *p, int ext)
164 return isl_map_print_polylib(set_to_map(set), p, ext);
167 static int count_same_name(__isl_keep isl_space *space,
168 enum isl_dim_type type, unsigned pos, const char *name)
170 enum isl_dim_type t;
171 unsigned p, s;
172 int count = 0;
174 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
175 s = t == type ? pos : isl_space_dim(space, t);
176 for (p = 0; p < s; ++p) {
177 const char *n = isl_space_get_dim_name(space, t, p);
178 if (n && !strcmp(n, name))
179 count++;
182 return count;
185 /* Print the name of the variable of type "type" and position "pos"
186 * in "space" to "p".
188 static __isl_give isl_printer *print_name(__isl_keep isl_space *space,
189 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
190 int latex)
192 const char *name;
193 char buffer[20];
194 int primes;
196 name = type == isl_dim_div ? NULL
197 : isl_space_get_dim_name(space, type, pos);
199 if (!name) {
200 const char *prefix;
201 if (type == isl_dim_param)
202 prefix = s_param_prefix[latex];
203 else if (type == isl_dim_div)
204 prefix = s_div_prefix[latex];
205 else if (isl_space_is_set(space) || type == isl_dim_in)
206 prefix = s_input_prefix[latex];
207 else
208 prefix = s_output_prefix[latex];
209 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
210 name = buffer;
212 primes = count_same_name(space, name == buffer ? isl_dim_div : type,
213 pos, name);
214 p = isl_printer_print_str(p, name);
215 while (primes-- > 0)
216 p = isl_printer_print_str(p, "'");
217 return p;
220 static enum isl_dim_type pos2type(__isl_keep isl_space *space, unsigned *pos)
222 enum isl_dim_type type;
223 unsigned n_in = isl_space_dim(space, isl_dim_in);
224 unsigned n_out = isl_space_dim(space, isl_dim_out);
225 unsigned nparam = isl_space_dim(space, isl_dim_param);
227 if (*pos < 1 + nparam) {
228 type = isl_dim_param;
229 *pos -= 1;
230 } else if (*pos < 1 + nparam + n_in) {
231 type = isl_dim_in;
232 *pos -= 1 + nparam;
233 } else if (*pos < 1 + nparam + n_in + n_out) {
234 type = isl_dim_out;
235 *pos -= 1 + nparam + n_in;
236 } else {
237 type = isl_dim_div;
238 *pos -= 1 + nparam + n_in + n_out;
241 return type;
244 /* Can the div expression of the integer division at position "row" of "div"
245 * be printed?
246 * In particular, are the div expressions available and does the selected
247 * variable have a known explicit representation?
248 * Furthermore, the Omega format does not allow any div expressions
249 * to be printed.
251 static isl_bool can_print_div_expr(__isl_keep isl_printer *p,
252 __isl_keep isl_mat *div, int pos)
254 if (p->output_format == ISL_FORMAT_OMEGA)
255 return isl_bool_false;
256 if (!div)
257 return isl_bool_false;
258 return !isl_int_is_zero(div->row[pos][0]);
261 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
262 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
264 static __isl_give isl_printer *print_term(__isl_keep isl_space *space,
265 __isl_keep isl_mat *div,
266 isl_int c, unsigned pos, __isl_take isl_printer *p, int latex)
268 enum isl_dim_type type;
269 int print_div_def;
271 if (!p || !space)
272 return isl_printer_free(p);
274 if (pos == 0)
275 return isl_printer_print_isl_int(p, c);
277 type = pos2type(space, &pos);
278 print_div_def = type == isl_dim_div && can_print_div_expr(p, div, pos);
280 if (isl_int_is_one(c))
282 else if (isl_int_is_negone(c))
283 p = isl_printer_print_str(p, "-");
284 else {
285 p = isl_printer_print_isl_int(p, c);
286 if (p->output_format == ISL_FORMAT_C || print_div_def)
287 p = isl_printer_print_str(p, "*");
289 if (print_div_def)
290 p = print_div(space, div, pos, p);
291 else
292 p = print_name(space, p, type, pos, latex);
293 return p;
296 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_space *dim,
297 __isl_keep isl_mat *div,
298 __isl_take isl_printer *p, isl_int *c, int len)
300 int i;
301 int first;
303 for (i = 0, first = 1; i < len; ++i) {
304 int flip = 0;
305 if (isl_int_is_zero(c[i]))
306 continue;
307 if (!first) {
308 if (isl_int_is_neg(c[i])) {
309 flip = 1;
310 isl_int_neg(c[i], c[i]);
311 p = isl_printer_print_str(p, " - ");
312 } else
313 p = isl_printer_print_str(p, " + ");
315 first = 0;
316 p = print_term(dim, div, c[i], i, p, 0);
317 if (flip)
318 isl_int_neg(c[i], c[i]);
320 if (first)
321 p = isl_printer_print_str(p, "0");
322 return p;
325 /* Print an affine expression "c"
326 * to "p", with the variable names taken from "space" and
327 * the integer division definitions taken from "div".
329 static __isl_give isl_printer *print_affine(__isl_take isl_printer *p,
330 __isl_keep isl_space *space, __isl_keep isl_mat *div, isl_int *c)
332 unsigned n_div;
333 unsigned len;
335 if (!space || !div)
336 return isl_printer_free(p);
337 n_div = isl_mat_rows(div);
338 len = 1 + isl_space_dim(space, isl_dim_all) + n_div;
339 return print_affine_of_len(space, div, p, c, len);
342 /* offset is the offset of local_space inside data->type of data->space.
344 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
345 __isl_keep isl_space *local_space, enum isl_dim_type local_type,
346 struct isl_print_space_data *data, int offset)
348 int i;
350 if (data->space != local_space && local_type == isl_dim_out)
351 offset += local_space->n_in;
353 for (i = 0; i < isl_space_dim(local_space, local_type); ++i) {
354 if (i)
355 p = isl_printer_print_str(p, ", ");
356 if (data->print_dim)
357 p = data->print_dim(p, data, offset + i);
358 else
359 p = print_name(data->space, p, data->type, offset + i,
360 data->latex);
362 return p;
365 static __isl_give isl_printer *print_var_list(__isl_take isl_printer *p,
366 __isl_keep isl_space *space, enum isl_dim_type type)
368 struct isl_print_space_data data = { .space = space, .type = type };
370 return print_nested_var_list(p, space, type, &data, 0);
373 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
374 __isl_keep isl_space *local_dim,
375 struct isl_print_space_data *data, int offset);
377 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
378 __isl_keep isl_space *local_space, enum isl_dim_type local_type,
379 struct isl_print_space_data *data, int offset)
381 const char *name = NULL;
382 unsigned n = isl_space_dim(local_space, local_type);
383 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
384 name = isl_space_get_tuple_name(local_space, local_type);
385 if (name) {
386 if (data->latex)
387 p = isl_printer_print_str(p, "\\mathrm{");
388 p = isl_printer_print_str(p, name);
389 if (data->latex)
390 p = isl_printer_print_str(p, "}");
393 if (!data->latex || n != 1 || name)
394 p = isl_printer_print_str(p, s_open_list[data->latex]);
395 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
396 local_space->nested[local_type - isl_dim_in]) {
397 if (data->space != local_space && local_type == isl_dim_out)
398 offset += local_space->n_in;
399 p = print_nested_map_dim(p,
400 local_space->nested[local_type - isl_dim_in],
401 data, offset);
402 } else
403 p = print_nested_var_list(p, local_space, local_type, data,
404 offset);
405 if (!data->latex || n != 1 || name)
406 p = isl_printer_print_str(p, s_close_list[data->latex]);
407 return p;
410 static __isl_give isl_printer *print_tuple(__isl_keep isl_space *dim,
411 __isl_take isl_printer *p, enum isl_dim_type type,
412 struct isl_print_space_data *data)
414 data->space = dim;
415 data->type = type;
416 return print_nested_tuple(p, dim, type, data, 0);
419 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
420 __isl_keep isl_space *local_dim,
421 struct isl_print_space_data *data, int offset)
423 p = print_nested_tuple(p, local_dim, isl_dim_in, data, offset);
424 p = isl_printer_print_str(p, s_to[data->latex]);
425 p = print_nested_tuple(p, local_dim, isl_dim_out, data, offset);
427 return p;
430 __isl_give isl_printer *isl_print_space(__isl_keep isl_space *space,
431 __isl_take isl_printer *p, int rational,
432 struct isl_print_space_data *data)
434 if (rational && !data->latex)
435 p = isl_printer_print_str(p, "rat: ");
436 if (isl_space_is_params(space))
438 else if (isl_space_is_set(space))
439 p = print_tuple(space, p, isl_dim_set, data);
440 else {
441 p = print_tuple(space, p, isl_dim_in, data);
442 p = isl_printer_print_str(p, s_to[data->latex]);
443 p = print_tuple(space, p, isl_dim_out, data);
446 return p;
449 static __isl_give isl_printer *print_omega_parameters(
450 __isl_keep isl_space *space, __isl_take isl_printer *p)
452 if (isl_space_dim(space, isl_dim_param) == 0)
453 return p;
455 p = isl_printer_start_line(p);
456 p = isl_printer_print_str(p, "symbolic ");
457 p = print_var_list(p, space, isl_dim_param);
458 p = isl_printer_print_str(p, ";");
459 p = isl_printer_end_line(p);
460 return p;
463 /* Does the inequality constraint following "i" in "bmap"
464 * have an opposite value for the same last coefficient?
465 * "last" is the position of the last coefficient of inequality "i".
466 * If the next constraint is a div constraint, then it is ignored
467 * since div constraints are not printed.
469 static isl_bool next_is_opposite(__isl_keep isl_basic_map *bmap, int i,
470 int last)
472 unsigned total = isl_basic_map_total_dim(bmap);
473 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
475 if (i + 1 >= bmap->n_ineq)
476 return isl_bool_false;
477 if (isl_seq_last_non_zero(bmap->ineq[i + 1], 1 + total) != last)
478 return isl_bool_false;
479 if (last >= o_div) {
480 isl_bool is_div;
481 is_div = isl_basic_map_is_div_constraint(bmap,
482 bmap->ineq[i + 1], last - o_div);
483 if (is_div < 0)
484 return isl_bool_error;
485 if (is_div)
486 return isl_bool_false;
488 return isl_int_abs_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]) &&
489 !isl_int_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]);
492 /* Return a string representation of the operator used when
493 * printing a constraint where the LHS is greater than or equal to the LHS
494 * (sign > 0) or smaller than or equal to the LHS (sign < 0).
495 * If "strict" is set, then return the strict version of the comparison
496 * operator.
498 static const char *constraint_op(int sign, int strict, int latex)
500 if (strict)
501 return sign < 0 ? "<" : ">";
502 if (sign < 0)
503 return s_le[latex];
504 else
505 return s_ge[latex];
508 /* Print one side of a constraint "c" to "p", with
509 * the variable names taken from "space" and the integer division definitions
510 * taken from "div".
511 * "last" is the position of the last non-zero coefficient.
512 * Let c' be the result of zeroing out this coefficient, then
513 * the partial constraint
515 * c' op
517 * is printed.
519 static __isl_give isl_printer *print_half_constraint(__isl_take isl_printer *p,
520 __isl_keep isl_space *space, __isl_keep isl_mat *div,
521 isl_int *c, int last, const char *op, int latex)
523 isl_int_set_si(c[last], 0);
524 p = print_affine(p, space, div, c);
526 p = isl_printer_print_str(p, " ");
527 p = isl_printer_print_str(p, op);
528 p = isl_printer_print_str(p, " ");
530 return p;
533 /* Print a constraint "c" to "p", with the variable names
534 * taken from "space" and the integer division definitions taken from "div".
535 * "last" is the position of the last non-zero coefficient, which is
536 * moreover assumed to be negative.
537 * Let c' be the result of zeroing out this coefficient, then
538 * the constraint is printed in the form
540 * -c[last] op c'
542 static __isl_give isl_printer *print_constraint(__isl_take isl_printer *p,
543 __isl_keep isl_space *space, __isl_keep isl_mat *div,
544 isl_int *c, int last, const char *op, int latex)
546 isl_int_abs(c[last], c[last]);
548 p = print_term(space, div, c[last], last, p, latex);
550 p = isl_printer_print_str(p, " ");
551 p = isl_printer_print_str(p, op);
552 p = isl_printer_print_str(p, " ");
554 isl_int_set_si(c[last], 0);
555 p = print_affine(p, space, div, c);
557 return p;
560 /* Given an integer division
562 * floor(f/m)
564 * at position "pos" in "div", print the corresponding modulo expression
566 * (f) mod m
568 * to "p". The variable names are taken from "space", while any
569 * nested integer division definitions are taken from "div".
571 static __isl_give isl_printer *print_mod(__isl_take isl_printer *p,
572 __isl_keep isl_space *space, __isl_keep isl_mat *div, int pos,
573 int latex)
575 if (!p || !div)
576 return isl_printer_free(p);
578 p = isl_printer_print_str(p, "(");
579 p = print_affine_of_len(space, div, p,
580 div->row[pos] + 1, div->n_col - 1);
581 p = isl_printer_print_str(p, ") ");
582 p = isl_printer_print_str(p, s_mod[latex]);
583 p = isl_printer_print_str(p, " ");
584 p = isl_printer_print_isl_int(p, div->row[pos][0]);
585 return p;
588 /* Can the equality constraints "c" be printed as a modulo constraint?
589 * In particular, is of the form
591 * f - a m floor(g/m) = 0,
593 * with c = -a m the coefficient at position "pos"?
594 * Return the position of the corresponding integer division if so.
595 * Return the number of integer divisions if not.
596 * Return -1 on error.
598 * Modulo constraints are currently not printed in C format.
599 * Other than that, "pos" needs to correspond to an integer division
600 * with explicit representation and "c" needs to be a multiple
601 * of the denominator of the integer division.
603 static int print_as_modulo_pos(__isl_keep isl_printer *p,
604 __isl_keep isl_space *space, __isl_keep isl_mat *div, unsigned pos,
605 isl_int c)
607 isl_bool can_print;
608 unsigned n_div;
609 enum isl_dim_type type;
611 if (!p || !space)
612 return -1;
613 n_div = isl_mat_rows(div);
614 if (p->output_format == ISL_FORMAT_C)
615 return n_div;
616 type = pos2type(space, &pos);
617 if (type != isl_dim_div)
618 return n_div;
619 can_print = can_print_div_expr(p, div, pos);
620 if (can_print < 0)
621 return -1;
622 if (!can_print)
623 return n_div;
624 if (!isl_int_is_divisible_by(c, div->row[pos][0]))
625 return n_div;
626 return pos;
629 /* Print equality constraint "c" to "p" as a modulo constraint,
630 * with the variable names taken from "space" and
631 * the integer division definitions taken from "div".
632 * "last" is the position of the last non-zero coefficient, which is
633 * moreover assumed to be negative and a multiple of the denominator
634 * of the corresponding integer division. "div_pos" is the corresponding
635 * position in the sequence of integer divisions.
637 * The equality is of the form
639 * f - a m floor(g/m) = 0.
641 * Print it as
643 * a (g mod m) = -f + a g
645 static __isl_give isl_printer *print_eq_mod_constraint(
646 __isl_take isl_printer *p, __isl_keep isl_space *space,
647 __isl_keep isl_mat *div, unsigned div_pos,
648 isl_int *c, int last, int latex)
650 isl_ctx *ctx;
651 int multiple;
653 ctx = isl_printer_get_ctx(p);
654 isl_int_divexact(c[last], c[last], div->row[div_pos][0]);
655 isl_int_abs(c[last], c[last]);
656 multiple = !isl_int_is_one(c[last]);
657 if (multiple) {
658 p = isl_printer_print_isl_int(p, c[last]);
659 p = isl_printer_print_str(p, "*(");
661 p = print_mod(p, space, div, div_pos, latex);
662 if (multiple)
663 p = isl_printer_print_str(p, ")");
664 p = isl_printer_print_str(p, " = ");
665 isl_seq_combine(c, ctx->negone, c,
666 c[last], div->row[div_pos] + 1, last);
667 isl_int_set_si(c[last], 0);
668 p = print_affine(p, space, div, c);
669 return p;
672 /* Print equality constraint "c" to "p", with the variable names
673 * taken from "space" and the integer division definitions taken from "div".
674 * "last" is the position of the last non-zero coefficient, which is
675 * moreover assumed to be negative.
677 * If possible, print the equality constraint as a modulo constraint.
679 static __isl_give isl_printer *print_eq_constraint(__isl_take isl_printer *p,
680 __isl_keep isl_space *space, __isl_keep isl_mat *div, isl_int *c,
681 int last, int latex)
683 unsigned n_div;
684 int div_pos;
686 n_div = isl_mat_rows(div);
687 div_pos = print_as_modulo_pos(p, space, div, last, c[last]);
688 if (div_pos < 0)
689 return isl_printer_free(p);
690 if (div_pos < n_div)
691 return print_eq_mod_constraint(p, space, div, div_pos,
692 c, last, latex);
693 return print_constraint(p, space, div, c, last, "=", latex);
696 /* Print the constraints of "bmap" to "p".
697 * The names of the variables are taken from "space" and
698 * the integer division definitions are taken from "div".
699 * Div constraints are only printed in "dump" mode.
700 * The constraints are sorted prior to printing (except in "dump" mode).
702 * If x is the last variable with a non-zero coefficient,
703 * then a lower bound
705 * f - a x >= 0
707 * is printed as
709 * a x <= f
711 * while an upper bound
713 * f + a x >= 0
715 * is printed as
717 * a x >= -f
719 * If the next constraint has an opposite sign for the same last coefficient,
720 * then it is printed as
722 * f >= a x
724 * or
726 * -f <= a x
728 * instead. In fact, the "a x" part is not printed explicitly, but
729 * reused from the next constraint, which is therefore treated as
730 * a first constraint in the conjunction.
732 * If the constant term of "f" is -1, then "f" is replaced by "f + 1" and
733 * the comparison operator is replaced by the strict variant.
734 * Essentially, ">= 1" is replaced by "> 0".
736 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
737 __isl_keep isl_space *space, __isl_keep isl_mat *div,
738 __isl_take isl_printer *p, int latex)
740 int i;
741 isl_vec *c = NULL;
742 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
743 unsigned total = isl_basic_map_total_dim(bmap);
744 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
745 int first = 1;
746 int dump;
748 if (!p)
749 return NULL;
750 bmap = isl_basic_map_copy(bmap);
751 dump = p->dump;
752 if (!dump)
753 bmap = isl_basic_map_sort_constraints(bmap);
754 if (!bmap)
755 goto error;
757 c = isl_vec_alloc(bmap->ctx, 1 + total);
758 if (!c)
759 goto error;
761 for (i = bmap->n_eq - 1; i >= 0; --i) {
762 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
763 if (l < 0) {
764 if (i != bmap->n_eq - 1)
765 p = isl_printer_print_str(p, s_and[latex]);
766 p = isl_printer_print_str(p, "0 = 0");
767 continue;
769 if (!first)
770 p = isl_printer_print_str(p, s_and[latex]);
771 if (isl_int_is_neg(bmap->eq[i][l]))
772 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
773 else
774 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
775 p = print_eq_constraint(p, space, div, c->el, l, latex);
776 first = 0;
778 for (i = 0; i < bmap->n_ineq; ++i) {
779 isl_bool combine;
780 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
781 int strict;
782 int s;
783 const char *op;
784 if (l < 0)
785 continue;
786 if (!dump && l >= o_div &&
787 can_print_div_expr(p, div, l - o_div)) {
788 isl_bool is_div;
789 is_div = isl_basic_map_is_div_constraint(bmap,
790 bmap->ineq[i], l - o_div);
791 if (is_div < 0)
792 goto error;
793 if (is_div)
794 continue;
796 if (!first)
797 p = isl_printer_print_str(p, s_and[latex]);
798 s = isl_int_sgn(bmap->ineq[i][l]);
799 strict = !rational && isl_int_is_negone(bmap->ineq[i][0]);
800 if (s < 0)
801 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
802 else
803 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
804 if (strict)
805 isl_int_set_si(c->el[0], 0);
806 combine = dump ? isl_bool_false : next_is_opposite(bmap, i, l);
807 if (combine < 0)
808 goto error;
809 if (combine) {
810 op = constraint_op(-s, strict, latex);
811 p = print_half_constraint(p, space, div, c->el, l,
812 op, latex);
813 first = 1;
814 } else {
815 op = constraint_op(s, strict, latex);
816 p = print_constraint(p, space, div, c->el, l,
817 op, latex);
818 first = 0;
822 isl_basic_map_free(bmap);
823 isl_vec_free(c);
825 return p;
826 error:
827 isl_basic_map_free(bmap);
828 isl_vec_free(c);
829 isl_printer_free(p);
830 return NULL;
833 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
834 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
836 int c;
838 if (!p || !div)
839 return isl_printer_free(p);
841 c = p->output_format == ISL_FORMAT_C;
842 p = isl_printer_print_str(p, c ? "floord(" : "floor((");
843 p = print_affine_of_len(dim, div, p,
844 div->row[pos] + 1, div->n_col - 1);
845 p = isl_printer_print_str(p, c ? ", " : ")/");
846 p = isl_printer_print_isl_int(p, div->row[pos][0]);
847 p = isl_printer_print_str(p, ")");
848 return p;
851 /* Print a comma separated list of div names, except those that have
852 * a definition that can be printed.
853 * If "print_defined_divs" is set, then those div names are printed
854 * as well, along with their definitions.
856 static __isl_give isl_printer *print_div_list(__isl_take isl_printer *p,
857 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex,
858 int print_defined_divs)
860 int i;
861 int first = 1;
862 unsigned n_div;
864 if (!p || !space || !div)
865 return isl_printer_free(p);
867 n_div = isl_mat_rows(div);
869 for (i = 0; i < n_div; ++i) {
870 if (!print_defined_divs && can_print_div_expr(p, div, i))
871 continue;
872 if (!first)
873 p = isl_printer_print_str(p, ", ");
874 p = print_name(space, p, isl_dim_div, i, latex);
875 first = 0;
876 if (!can_print_div_expr(p, div, i))
877 continue;
878 p = isl_printer_print_str(p, " = ");
879 p = print_div(space, div, i, p);
882 return p;
885 /* Does printing an object with local variables described by "div"
886 * require an "exists" clause?
887 * That is, are there any local variables without an explicit representation?
888 * An exists clause is also needed in "dump" mode because
889 * explicit div representations are not printed inline in that case.
891 static isl_bool need_exists(__isl_keep isl_printer *p, __isl_keep isl_mat *div)
893 int i, n;
895 if (!p || !div)
896 return isl_bool_error;
897 n = isl_mat_rows(div);
898 if (n == 0)
899 return isl_bool_false;
900 if (p->dump)
901 return isl_bool_true;
902 for (i = 0; i < n; ++i)
903 if (!can_print_div_expr(p, div, i))
904 return isl_bool_true;
905 return isl_bool_false;
908 /* Print the start of an exists clause, i.e.,
910 * (exists variables:
912 * In dump mode, local variables with an explicit definition are printed
913 * as well because they will not be printed inline.
915 static __isl_give isl_printer *open_exists(__isl_take isl_printer *p,
916 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex)
918 int dump;
920 if (!p)
921 return NULL;
923 dump = p->dump;
924 p = isl_printer_print_str(p, s_open_exists[latex]);
925 p = print_div_list(p, space, div, latex, dump);
926 p = isl_printer_print_str(p, ": ");
928 return p;
931 /* Remove the explicit representations of all local variables in "div".
933 static __isl_give isl_mat *mark_all_unknown(__isl_take isl_mat *div)
935 int i, n_div;
937 if (!div)
938 return NULL;
940 n_div = isl_mat_rows(div);
941 for (i = 0; i < n_div; ++i)
942 div = isl_mat_set_element_si(div, i, 0, 0);
943 return div;
946 /* Print the constraints of "bmap" to "p".
947 * The names of the variables are taken from "space".
948 * "latex" is set if the constraints should be printed in LaTeX format.
949 * Do not print inline explicit div representations in "dump" mode.
951 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
952 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
954 int dump;
955 isl_mat *div;
956 isl_bool exists;
958 if (!p)
959 return NULL;
960 dump = p->dump;
961 div = isl_basic_map_get_divs(bmap);
962 exists = need_exists(p, div);
963 if (exists >= 0 && exists)
964 p = open_exists(p, space, div, latex);
966 if (dump)
967 div = mark_all_unknown(div);
968 p = print_constraints(bmap, space, div, p, latex);
969 isl_mat_free(div);
971 if (exists >= 0 && exists)
972 p = isl_printer_print_str(p, s_close_exists[latex]);
973 return p;
976 /* Print a colon followed by the constraints of "bmap"
977 * to "p", provided there are any constraints.
978 * The names of the variables are taken from "space".
979 * "latex" is set if the constraints should be printed in LaTeX format.
981 static __isl_give isl_printer *print_optional_disjunct(
982 __isl_keep isl_basic_map *bmap, __isl_keep isl_space *space,
983 __isl_take isl_printer *p, int latex)
985 if (isl_basic_map_plain_is_universe(bmap))
986 return p;
988 p = isl_printer_print_str(p, ": ");
989 p = print_disjunct(bmap, space, p, latex);
991 return p;
994 static __isl_give isl_printer *basic_map_print_omega(
995 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
997 p = isl_printer_print_str(p, "{ [");
998 p = print_var_list(p, bmap->dim, isl_dim_in);
999 p = isl_printer_print_str(p, "] -> [");
1000 p = print_var_list(p, bmap->dim, isl_dim_out);
1001 p = isl_printer_print_str(p, "] ");
1002 p = print_optional_disjunct(bmap, bmap->dim, p, 0);
1003 p = isl_printer_print_str(p, " }");
1004 return p;
1007 static __isl_give isl_printer *basic_set_print_omega(
1008 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
1010 p = isl_printer_print_str(p, "{ [");
1011 p = print_var_list(p, bset->dim, isl_dim_set);
1012 p = isl_printer_print_str(p, "] ");
1013 p = print_optional_disjunct(bset, bset->dim, p, 0);
1014 p = isl_printer_print_str(p, " }");
1015 return p;
1018 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
1019 __isl_take isl_printer *p)
1021 int i;
1023 for (i = 0; i < map->n; ++i) {
1024 if (i)
1025 p = isl_printer_print_str(p, " union ");
1026 p = basic_map_print_omega(map->p[i], p);
1028 return p;
1031 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
1032 __isl_take isl_printer *p)
1034 int i;
1036 for (i = 0; i < set->n; ++i) {
1037 if (i)
1038 p = isl_printer_print_str(p, " union ");
1039 p = basic_set_print_omega(set->p[i], p);
1041 return p;
1044 /* Print the list of parameters in "space", followed by an arrow, to "p",
1045 * if there are any parameters.
1047 static __isl_give isl_printer *print_param_tuple(__isl_take isl_printer *p,
1048 __isl_keep isl_space *space, struct isl_print_space_data *data)
1050 if (!p || !space)
1051 return isl_printer_free(p);
1052 if (isl_space_dim(space, isl_dim_param) == 0)
1053 return p;
1055 p = print_tuple(space, p, isl_dim_param, data);
1056 p = isl_printer_print_str(p, s_to[data->latex]);
1058 return p;
1061 static __isl_give isl_printer *isl_basic_map_print_isl(
1062 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
1063 int latex)
1065 struct isl_print_space_data data = { .latex = latex };
1066 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1068 p = print_param_tuple(p, bmap->dim, &data);
1069 p = isl_printer_print_str(p, "{ ");
1070 p = isl_print_space(bmap->dim, p, rational, &data);
1071 p = isl_printer_print_str(p, " : ");
1072 p = print_disjunct(bmap, bmap->dim, p, latex);
1073 p = isl_printer_print_str(p, " }");
1074 return p;
1077 /* Print the disjuncts of a map (or set) "map" to "p".
1078 * The names of the variables are taken from "space".
1079 * "latex" is set if the constraints should be printed in LaTeX format.
1081 static __isl_give isl_printer *print_disjuncts_core(__isl_keep isl_map *map,
1082 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1084 int i;
1086 if (map->n == 0)
1087 p = isl_printer_print_str(p, "false");
1088 for (i = 0; i < map->n; ++i) {
1089 if (i)
1090 p = isl_printer_print_str(p, s_or[latex]);
1091 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
1092 p = isl_printer_print_str(p, "(");
1093 p = print_disjunct(map->p[i], space, p, latex);
1094 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
1095 p = isl_printer_print_str(p, ")");
1097 return p;
1100 /* Print the disjuncts of a map (or set) "map" to "p".
1101 * The names of the variables are taken from "space".
1102 * "hull" describes constraints shared by all disjuncts of "map".
1103 * "latex" is set if the constraints should be printed in LaTeX format.
1105 * Print the disjuncts as a conjunction of "hull" and
1106 * the result of removing the constraints of "hull" from "map".
1107 * If this result turns out to be the universe, then simply print "hull".
1109 static __isl_give isl_printer *print_disjuncts_in_hull(__isl_keep isl_map *map,
1110 __isl_keep isl_space *space, __isl_take isl_basic_map *hull,
1111 __isl_take isl_printer *p, int latex)
1113 isl_bool is_universe;
1115 p = print_disjunct(hull, space, p, latex);
1116 map = isl_map_plain_gist_basic_map(isl_map_copy(map), hull);
1117 is_universe = isl_map_plain_is_universe(map);
1118 if (is_universe < 0)
1119 goto error;
1120 if (!is_universe) {
1121 p = isl_printer_print_str(p, s_and[latex]);
1122 p = isl_printer_print_str(p, "(");
1123 p = print_disjuncts_core(map, space, p, latex);
1124 p = isl_printer_print_str(p, ")");
1126 isl_map_free(map);
1128 return p;
1129 error:
1130 isl_map_free(map);
1131 isl_printer_free(p);
1132 return NULL;
1135 /* Print the disjuncts of a map (or set) "map" to "p".
1136 * The names of the variables are taken from "space".
1137 * "latex" is set if the constraints should be printed in LaTeX format.
1139 * If there are at least two disjuncts and "dump" mode is not turned out,
1140 * check for any shared constraints among all disjuncts.
1141 * If there are any, then print them separately in print_disjuncts_in_hull.
1143 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
1144 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1146 if (isl_map_plain_is_universe(map))
1147 return p;
1149 p = isl_printer_print_str(p, s_such_that[latex]);
1150 if (!p)
1151 return NULL;
1153 if (!p->dump && map->n >= 2) {
1154 isl_basic_map *hull;
1155 isl_bool is_universe;
1157 hull = isl_map_plain_unshifted_simple_hull(isl_map_copy(map));
1158 is_universe = isl_basic_map_plain_is_universe(hull);
1159 if (is_universe < 0)
1160 p = isl_printer_free(p);
1161 else if (!is_universe)
1162 return print_disjuncts_in_hull(map, space, hull,
1163 p, latex);
1164 isl_basic_map_free(hull);
1167 return print_disjuncts_core(map, space, p, latex);
1170 /* Print the disjuncts of a map (or set).
1171 * The names of the variables are taken from "space".
1172 * "latex" is set if the constraints should be printed in LaTeX format.
1174 * If the map turns out to be a universal parameter domain, then
1175 * we need to print the colon. Otherwise, the output looks identical
1176 * to the empty set.
1178 static __isl_give isl_printer *print_disjuncts_map(__isl_keep isl_map *map,
1179 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1181 if (isl_map_plain_is_universe(map) && isl_space_is_params(map->dim))
1182 return isl_printer_print_str(p, s_such_that[latex]);
1183 else
1184 return print_disjuncts(map, space, p, latex);
1187 /* Print the disjuncts of a set.
1188 * The names of the variables are taken from "space".
1189 * "latex" is set if the constraints should be printed in LaTeX format.
1191 static __isl_give isl_printer *print_disjuncts_set(__isl_keep isl_set *set,
1192 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1194 return print_disjuncts_map(set_to_map(set), space, p, latex);
1197 struct isl_aff_split {
1198 isl_basic_map *aff;
1199 isl_map *map;
1202 static void free_split(__isl_take struct isl_aff_split *split, int n)
1204 int i;
1206 if (!split)
1207 return;
1209 for (i = 0; i < n; ++i) {
1210 isl_basic_map_free(split[i].aff);
1211 isl_map_free(split[i].map);
1214 free(split);
1217 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
1219 int i, j;
1220 unsigned nparam, n_in, n_out, total;
1222 bmap = isl_basic_map_cow(bmap);
1223 if (!bmap)
1224 return NULL;
1225 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
1226 goto error;
1228 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1229 n_in = isl_basic_map_dim(bmap, isl_dim_in);
1230 n_out = isl_basic_map_dim(bmap, isl_dim_out);
1231 total = isl_basic_map_dim(bmap, isl_dim_all);
1232 for (i = bmap->n_eq - 1; i >= 0; --i) {
1233 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
1234 if (j >= nparam && j < nparam + n_in + n_out &&
1235 (isl_int_is_one(bmap->eq[i][1 + j]) ||
1236 isl_int_is_negone(bmap->eq[i][1 + j])))
1237 continue;
1238 if (isl_basic_map_drop_equality(bmap, i) < 0)
1239 goto error;
1242 bmap = isl_basic_map_finalize(bmap);
1244 return bmap;
1245 error:
1246 isl_basic_map_free(bmap);
1247 return NULL;
1250 static int aff_split_cmp(const void *p1, const void *p2, void *user)
1252 const struct isl_aff_split *s1, *s2;
1253 s1 = (const struct isl_aff_split *) p1;
1254 s2 = (const struct isl_aff_split *) p2;
1256 return isl_basic_map_plain_cmp(s1->aff, s2->aff);
1259 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
1260 __isl_keep isl_basic_map *aff)
1262 int i, j;
1263 unsigned total;
1265 if (!bmap || !aff)
1266 goto error;
1268 total = isl_space_dim(bmap->dim, isl_dim_all);
1270 for (i = bmap->n_eq - 1; i >= 0; --i) {
1271 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
1272 bmap->n_div) != -1)
1273 continue;
1274 for (j = 0; j < aff->n_eq; ++j) {
1275 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
1276 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
1277 continue;
1278 if (isl_basic_map_drop_equality(bmap, i) < 0)
1279 goto error;
1280 break;
1284 return bmap;
1285 error:
1286 isl_basic_map_free(bmap);
1287 return NULL;
1290 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
1292 int i, n;
1293 struct isl_aff_split *split;
1294 isl_ctx *ctx;
1296 ctx = isl_map_get_ctx(map);
1297 split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
1298 if (!split)
1299 return NULL;
1301 for (i = 0; i < map->n; ++i) {
1302 isl_basic_map *bmap;
1303 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
1304 bmap = isl_basic_map_copy(map->p[i]);
1305 bmap = isl_basic_map_cow(bmap);
1306 bmap = drop_aff(bmap, split[i].aff);
1307 split[i].map = isl_map_from_basic_map(bmap);
1308 if (!split[i].aff || !split[i].map)
1309 goto error;
1312 if (isl_sort(split, map->n, sizeof(struct isl_aff_split),
1313 &aff_split_cmp, NULL) < 0)
1314 goto error;
1316 n = map->n;
1317 for (i = n - 1; i >= 1; --i) {
1318 if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
1319 split[i].aff))
1320 continue;
1321 isl_basic_map_free(split[i].aff);
1322 split[i - 1].map = isl_map_union(split[i - 1].map,
1323 split[i].map);
1324 if (i != n - 1)
1325 split[i] = split[n - 1];
1326 split[n - 1].aff = NULL;
1327 split[n - 1].map = NULL;
1328 --n;
1331 return split;
1332 error:
1333 free_split(split, map->n);
1334 return NULL;
1337 static int defining_equality(__isl_keep isl_basic_map *eq,
1338 __isl_keep isl_space *dim, enum isl_dim_type type, int pos)
1340 int i;
1341 unsigned total;
1343 if (!eq)
1344 return -1;
1346 pos += isl_space_offset(dim, type);
1347 total = isl_basic_map_total_dim(eq);
1349 for (i = 0; i < eq->n_eq; ++i) {
1350 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
1351 continue;
1352 if (isl_int_is_one(eq->eq[i][1 + pos]))
1353 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
1354 return i;
1357 return -1;
1360 /* Print dimension "pos" of data->space to "p".
1362 * data->user is assumed to be an isl_basic_map keeping track of equalities.
1364 * If the current dimension is defined by these equalities, then print
1365 * the corresponding expression, assigned to the name of the dimension
1366 * if there is any. Otherwise, print the name of the dimension.
1368 static __isl_give isl_printer *print_dim_eq(__isl_take isl_printer *p,
1369 struct isl_print_space_data *data, unsigned pos)
1371 isl_basic_map *eq = data->user;
1372 int j;
1374 j = defining_equality(eq, data->space, data->type, pos);
1375 if (j >= 0) {
1376 if (isl_space_has_dim_name(data->space, data->type, pos)) {
1377 p = print_name(data->space, p, data->type, pos,
1378 data->latex);
1379 p = isl_printer_print_str(p, " = ");
1381 pos += 1 + isl_space_offset(data->space, data->type);
1382 p = print_affine_of_len(data->space, NULL, p, eq->eq[j], pos);
1383 } else {
1384 p = print_name(data->space, p, data->type, pos, data->latex);
1387 return p;
1390 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
1391 struct isl_aff_split *split, int n, __isl_keep isl_space *space)
1393 struct isl_print_space_data data = { 0 };
1394 int i;
1395 int rational;
1397 data.print_dim = &print_dim_eq;
1398 for (i = 0; i < n; ++i) {
1399 if (!split[i].map)
1400 break;
1401 rational = split[i].map->n > 0 &&
1402 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
1403 if (i)
1404 p = isl_printer_print_str(p, "; ");
1405 data.user = split[i].aff;
1406 p = isl_print_space(space, p, rational, &data);
1407 p = print_disjuncts_map(split[i].map, space, p, 0);
1410 return p;
1413 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
1414 __isl_take isl_printer *p)
1416 struct isl_print_space_data data = { 0 };
1417 struct isl_aff_split *split = NULL;
1418 int rational;
1420 if (!p || !map)
1421 return isl_printer_free(p);
1422 if (!p->dump && map->n > 0)
1423 split = split_aff(map);
1424 if (split) {
1425 p = print_split_map(p, split, map->n, map->dim);
1426 } else {
1427 rational = map->n > 0 &&
1428 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
1429 p = isl_print_space(map->dim, p, rational, &data);
1430 p = print_disjuncts_map(map, map->dim, p, 0);
1432 free_split(split, map->n);
1433 return p;
1436 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
1437 __isl_take isl_printer *p)
1439 struct isl_print_space_data data = { 0 };
1441 p = print_param_tuple(p, map->dim, &data);
1442 p = isl_printer_print_str(p, s_open_set[0]);
1443 p = isl_map_print_isl_body(map, p);
1444 p = isl_printer_print_str(p, s_close_set[0]);
1445 return p;
1448 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
1449 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff)
1451 struct isl_print_space_data data = { 0 };
1453 data.latex = 1;
1454 p = print_param_tuple(p, map->dim, &data);
1455 p = isl_printer_print_str(p, s_open_set[1]);
1456 data.print_dim = &print_dim_eq;
1457 data.user = aff;
1458 p = isl_print_space(map->dim, p, 0, &data);
1459 p = print_disjuncts_map(map, map->dim, p, 1);
1460 p = isl_printer_print_str(p, s_close_set[1]);
1462 return p;
1465 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
1466 __isl_take isl_printer *p)
1468 int i;
1469 struct isl_aff_split *split = NULL;
1471 if (map->n > 0)
1472 split = split_aff(map);
1474 if (!split)
1475 return print_latex_map(map, p, NULL);
1477 for (i = 0; i < map->n; ++i) {
1478 if (!split[i].map)
1479 break;
1480 if (i)
1481 p = isl_printer_print_str(p, " \\cup ");
1482 p = print_latex_map(split[i].map, p, split[i].aff);
1485 free_split(split, map->n);
1486 return p;
1489 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
1490 __isl_keep isl_basic_map *bmap)
1492 if (!p || !bmap)
1493 goto error;
1494 if (p->output_format == ISL_FORMAT_ISL)
1495 return isl_basic_map_print_isl(bmap, p, 0);
1496 else if (p->output_format == ISL_FORMAT_OMEGA)
1497 return basic_map_print_omega(bmap, p);
1498 isl_assert(bmap->ctx, 0, goto error);
1499 error:
1500 isl_printer_free(p);
1501 return NULL;
1504 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
1505 __isl_keep isl_basic_set *bset)
1507 if (!p || !bset)
1508 goto error;
1510 if (p->output_format == ISL_FORMAT_ISL)
1511 return isl_basic_map_print_isl(bset, p, 0);
1512 else if (p->output_format == ISL_FORMAT_POLYLIB)
1513 return isl_basic_set_print_polylib(bset, p, 0);
1514 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1515 return isl_basic_set_print_polylib(bset, p, 1);
1516 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
1517 return bset_print_constraints_polylib(bset, p);
1518 else if (p->output_format == ISL_FORMAT_OMEGA)
1519 return basic_set_print_omega(bset, p);
1520 isl_assert(p->ctx, 0, goto error);
1521 error:
1522 isl_printer_free(p);
1523 return NULL;
1526 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
1527 __isl_keep isl_set *set)
1529 if (!p || !set)
1530 goto error;
1531 if (p->output_format == ISL_FORMAT_ISL)
1532 return isl_map_print_isl(set_to_map(set), p);
1533 else if (p->output_format == ISL_FORMAT_POLYLIB)
1534 return isl_set_print_polylib(set, p, 0);
1535 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1536 return isl_set_print_polylib(set, p, 1);
1537 else if (p->output_format == ISL_FORMAT_OMEGA)
1538 return isl_set_print_omega(set, p);
1539 else if (p->output_format == ISL_FORMAT_LATEX)
1540 return isl_map_print_latex(set_to_map(set), p);
1541 isl_assert(set->ctx, 0, goto error);
1542 error:
1543 isl_printer_free(p);
1544 return NULL;
1547 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1548 __isl_keep isl_map *map)
1550 if (!p || !map)
1551 goto error;
1553 if (p->output_format == ISL_FORMAT_ISL)
1554 return isl_map_print_isl(map, p);
1555 else if (p->output_format == ISL_FORMAT_POLYLIB)
1556 return isl_map_print_polylib(map, p, 0);
1557 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1558 return isl_map_print_polylib(map, p, 1);
1559 else if (p->output_format == ISL_FORMAT_OMEGA)
1560 return isl_map_print_omega(map, p);
1561 else if (p->output_format == ISL_FORMAT_LATEX)
1562 return isl_map_print_latex(map, p);
1563 isl_assert(map->ctx, 0, goto error);
1564 error:
1565 isl_printer_free(p);
1566 return NULL;
1569 struct isl_union_print_data {
1570 isl_printer *p;
1571 int first;
1574 static isl_stat print_map_body(__isl_take isl_map *map, void *user)
1576 struct isl_union_print_data *data;
1577 data = (struct isl_union_print_data *)user;
1579 if (!data->first)
1580 data->p = isl_printer_print_str(data->p, "; ");
1581 data->first = 0;
1583 data->p = isl_map_print_isl_body(map, data->p);
1584 isl_map_free(map);
1586 return isl_stat_ok;
1589 /* Print the body of "umap" (everything except the parameter declarations)
1590 * to "p" in isl format.
1592 static __isl_give isl_printer *isl_printer_print_union_map_isl_body(
1593 __isl_take isl_printer *p, __isl_keep isl_union_map *umap)
1595 struct isl_union_print_data data;
1597 p = isl_printer_print_str(p, s_open_set[0]);
1598 data.p = p;
1599 data.first = 1;
1600 isl_union_map_foreach_map(umap, &print_map_body, &data);
1601 p = data.p;
1602 p = isl_printer_print_str(p, s_close_set[0]);
1603 return p;
1606 /* Print the body of "uset" (everything except the parameter declarations)
1607 * to "p" in isl format.
1609 static __isl_give isl_printer *isl_printer_print_union_set_isl_body(
1610 __isl_take isl_printer *p, __isl_keep isl_union_set *uset)
1612 return isl_printer_print_union_map_isl_body(p, uset_to_umap(uset));
1615 /* Print the isl_union_map "umap" to "p" in isl format.
1617 static __isl_give isl_printer *isl_union_map_print_isl(
1618 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1620 struct isl_print_space_data space_data = { 0 };
1621 isl_space *space;
1623 space = isl_union_map_get_space(umap);
1624 p = print_param_tuple(p, space, &space_data);
1625 isl_space_free(space);
1627 p = isl_printer_print_union_map_isl_body(p, umap);
1629 return p;
1632 static isl_stat print_latex_map_body(__isl_take isl_map *map, void *user)
1634 struct isl_union_print_data *data;
1635 data = (struct isl_union_print_data *)user;
1637 if (!data->first)
1638 data->p = isl_printer_print_str(data->p, " \\cup ");
1639 data->first = 0;
1641 data->p = isl_map_print_latex(map, data->p);
1642 isl_map_free(map);
1644 return isl_stat_ok;
1647 static __isl_give isl_printer *isl_union_map_print_latex(
1648 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1650 struct isl_union_print_data data = { p, 1 };
1651 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1652 p = data.p;
1653 return p;
1656 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1657 __isl_keep isl_union_map *umap)
1659 if (!p || !umap)
1660 goto error;
1662 if (p->output_format == ISL_FORMAT_ISL)
1663 return isl_union_map_print_isl(umap, p);
1664 if (p->output_format == ISL_FORMAT_LATEX)
1665 return isl_union_map_print_latex(umap, p);
1667 isl_die(p->ctx, isl_error_invalid,
1668 "invalid output format for isl_union_map", goto error);
1669 error:
1670 isl_printer_free(p);
1671 return NULL;
1674 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1675 __isl_keep isl_union_set *uset)
1677 if (!p || !uset)
1678 goto error;
1680 if (p->output_format == ISL_FORMAT_ISL)
1681 return isl_union_map_print_isl(uset_to_umap(uset), p);
1682 if (p->output_format == ISL_FORMAT_LATEX)
1683 return isl_union_map_print_latex(uset_to_umap(uset), p);
1685 isl_die(p->ctx, isl_error_invalid,
1686 "invalid output format for isl_union_set", goto error);
1687 error:
1688 isl_printer_free(p);
1689 return NULL;
1692 static int poly_rec_n_non_zero(__isl_keep isl_poly_rec *rec)
1694 int i;
1695 int n;
1697 if (!rec)
1698 return -1;
1700 for (i = 0, n = 0; i < rec->n; ++i) {
1701 isl_bool is_zero = isl_poly_is_zero(rec->p[i]);
1703 if (is_zero < 0)
1704 return -1;
1705 if (!is_zero)
1706 ++n;
1709 return n;
1712 static __isl_give isl_printer *poly_print_cst(__isl_keep isl_poly *poly,
1713 __isl_take isl_printer *p, int first)
1715 isl_poly_cst *cst;
1716 int neg;
1718 cst = isl_poly_as_cst(poly);
1719 if (!cst)
1720 goto error;
1721 neg = !first && isl_int_is_neg(cst->n);
1722 if (!first)
1723 p = isl_printer_print_str(p, neg ? " - " : " + ");
1724 if (neg)
1725 isl_int_neg(cst->n, cst->n);
1726 if (isl_int_is_zero(cst->d)) {
1727 int sgn = isl_int_sgn(cst->n);
1728 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1729 sgn == 0 ? "NaN" : "infty");
1730 } else
1731 p = isl_printer_print_isl_int(p, cst->n);
1732 if (neg)
1733 isl_int_neg(cst->n, cst->n);
1734 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1735 p = isl_printer_print_str(p, "/");
1736 p = isl_printer_print_isl_int(p, cst->d);
1738 return p;
1739 error:
1740 isl_printer_free(p);
1741 return NULL;
1744 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1745 __isl_keep isl_space *space, __isl_keep isl_mat *div, int var)
1747 unsigned total;
1749 total = isl_space_dim(space, isl_dim_all);
1750 if (var < total)
1751 p = print_term(space, NULL, space->ctx->one, 1 + var, p, 0);
1752 else
1753 p = print_div(space, div, var - total, p);
1754 return p;
1757 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1758 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var, int exp)
1760 p = print_base(p, dim, div, var);
1761 if (exp == 1)
1762 return p;
1763 if (p->output_format == ISL_FORMAT_C) {
1764 int i;
1765 for (i = 1; i < exp; ++i) {
1766 p = isl_printer_print_str(p, "*");
1767 p = print_base(p, dim, div, var);
1769 } else {
1770 p = isl_printer_print_str(p, "^");
1771 p = isl_printer_print_int(p, exp);
1773 return p;
1776 /* Print the polynomial "poly" defined over the domain space "space" and
1777 * local variables defined by "div" to "p".
1779 static __isl_give isl_printer *poly_print(__isl_keep isl_poly *poly,
1780 __isl_keep isl_space *space, __isl_keep isl_mat *div,
1781 __isl_take isl_printer *p)
1783 int i, n, first, print_parens;
1784 isl_bool is_cst;
1785 isl_poly_rec *rec;
1787 is_cst = isl_poly_is_cst(poly);
1788 if (!p || is_cst < 0 || !space || !div)
1789 goto error;
1791 if (is_cst)
1792 return poly_print_cst(poly, p, 1);
1794 rec = isl_poly_as_rec(poly);
1795 n = poly_rec_n_non_zero(rec);
1796 if (n < 0)
1797 return isl_printer_free(p);
1798 print_parens = n > 1;
1799 if (print_parens)
1800 p = isl_printer_print_str(p, "(");
1801 for (i = 0, first = 1; i < rec->n; ++i) {
1802 isl_bool is_zero = isl_poly_is_zero(rec->p[i]);
1803 isl_bool is_one = isl_poly_is_one(rec->p[i]);
1804 isl_bool is_negone = isl_poly_is_negone(rec->p[i]);
1805 isl_bool is_cst = isl_poly_is_cst(rec->p[i]);
1807 if (is_zero < 0 || is_one < 0 || is_negone < 0)
1808 return isl_printer_free(p);
1809 if (is_zero)
1810 continue;
1811 if (is_negone) {
1812 if (!i)
1813 p = isl_printer_print_str(p, "-1");
1814 else if (first)
1815 p = isl_printer_print_str(p, "-");
1816 else
1817 p = isl_printer_print_str(p, " - ");
1818 } else if (is_cst && !is_one)
1819 p = poly_print_cst(rec->p[i], p, first);
1820 else {
1821 if (!first)
1822 p = isl_printer_print_str(p, " + ");
1823 if (i == 0 || !is_one)
1824 p = poly_print(rec->p[i], space, div, p);
1826 first = 0;
1827 if (i == 0)
1828 continue;
1829 if (!is_one && !is_negone)
1830 p = isl_printer_print_str(p, " * ");
1831 p = print_pow(p, space, div, rec->poly.var, i);
1833 if (print_parens)
1834 p = isl_printer_print_str(p, ")");
1835 return p;
1836 error:
1837 isl_printer_free(p);
1838 return NULL;
1841 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1842 __isl_keep isl_qpolynomial *qp)
1844 if (!p || !qp)
1845 goto error;
1846 p = poly_print(qp->poly, qp->dim, qp->div, p);
1847 return p;
1848 error:
1849 isl_printer_free(p);
1850 return NULL;
1853 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1854 __isl_keep isl_qpolynomial *qp)
1856 struct isl_print_space_data data = { 0 };
1858 if (!p || !qp)
1859 goto error;
1861 p = print_param_tuple(p, qp->dim, &data);
1862 p = isl_printer_print_str(p, "{ ");
1863 if (!isl_space_is_params(qp->dim)) {
1864 p = isl_print_space(qp->dim, p, 0, &data);
1865 p = isl_printer_print_str(p, " -> ");
1867 p = print_qpolynomial(p, qp);
1868 p = isl_printer_print_str(p, " }");
1869 return p;
1870 error:
1871 isl_printer_free(p);
1872 return NULL;
1875 /* Print the quasi-polynomial "qp" to "p" in C format, with the variable names
1876 * taken from the domain space "space".
1878 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1879 __isl_keep isl_space *space, __isl_keep isl_qpolynomial *qp)
1881 isl_bool is_one;
1882 isl_val *den;
1884 den = isl_qpolynomial_get_den(qp);
1885 qp = isl_qpolynomial_copy(qp);
1886 qp = isl_qpolynomial_scale_val(qp, isl_val_copy(den));
1887 is_one = isl_val_is_one(den);
1888 if (is_one < 0)
1889 p = isl_printer_free(p);
1890 if (!is_one)
1891 p = isl_printer_print_str(p, "(");
1892 if (qp)
1893 p = poly_print(qp->poly, space, qp->div, p);
1894 else
1895 p = isl_printer_free(p);
1896 if (!is_one) {
1897 p = isl_printer_print_str(p, ")/");
1898 p = isl_printer_print_val(p, den);
1900 isl_qpolynomial_free(qp);
1901 isl_val_free(den);
1902 return p;
1905 __isl_give isl_printer *isl_printer_print_qpolynomial(
1906 __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1908 if (!p || !qp)
1909 goto error;
1911 if (p->output_format == ISL_FORMAT_ISL)
1912 return print_qpolynomial_isl(p, qp);
1913 else if (p->output_format == ISL_FORMAT_C)
1914 return print_qpolynomial_c(p, qp->dim, qp);
1915 else
1916 isl_die(qp->dim->ctx, isl_error_unsupported,
1917 "output format not supported for isl_qpolynomials",
1918 goto error);
1919 error:
1920 isl_printer_free(p);
1921 return NULL;
1924 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1925 unsigned output_format)
1927 isl_printer *p;
1929 if (!qp)
1930 return;
1932 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1933 p = isl_printer_to_file(qp->dim->ctx, out);
1934 p = isl_printer_print_qpolynomial(p, qp);
1935 isl_printer_free(p);
1938 static __isl_give isl_printer *qpolynomial_fold_print(
1939 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1941 int i;
1943 if (fold->type == isl_fold_min)
1944 p = isl_printer_print_str(p, "min");
1945 else if (fold->type == isl_fold_max)
1946 p = isl_printer_print_str(p, "max");
1947 p = isl_printer_print_str(p, "(");
1948 for (i = 0; i < fold->n; ++i) {
1949 if (i)
1950 p = isl_printer_print_str(p, ", ");
1951 p = print_qpolynomial(p, fold->qp[i]);
1953 p = isl_printer_print_str(p, ")");
1954 return p;
1957 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1958 FILE *out, unsigned output_format)
1960 isl_printer *p;
1962 if (!fold)
1963 return;
1965 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1967 p = isl_printer_to_file(fold->dim->ctx, out);
1968 p = isl_printer_print_qpolynomial_fold(p, fold);
1970 isl_printer_free(p);
1973 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1974 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1976 struct isl_print_space_data data = { 0 };
1977 int i = 0;
1979 for (i = 0; i < pwqp->n; ++i) {
1980 isl_space *space;
1982 if (i)
1983 p = isl_printer_print_str(p, "; ");
1984 space = isl_qpolynomial_get_domain_space(pwqp->p[i].qp);
1985 if (!isl_space_is_params(space)) {
1986 p = isl_print_space(space, p, 0, &data);
1987 p = isl_printer_print_str(p, " -> ");
1989 p = print_qpolynomial(p, pwqp->p[i].qp);
1990 p = print_disjuncts(set_to_map(pwqp->p[i].set), space, p, 0);
1991 isl_space_free(space);
1994 return p;
1997 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1998 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2000 struct isl_print_space_data data = { 0 };
2002 if (!p || !pwqp)
2003 goto error;
2005 p = print_param_tuple(p, pwqp->dim, &data);
2006 p = isl_printer_print_str(p, "{ ");
2007 if (pwqp->n == 0) {
2008 if (!isl_space_is_set(pwqp->dim)) {
2009 p = print_tuple(pwqp->dim, p, isl_dim_in, &data);
2010 p = isl_printer_print_str(p, " -> ");
2012 p = isl_printer_print_str(p, "0");
2014 p = isl_pwqp_print_isl_body(p, pwqp);
2015 p = isl_printer_print_str(p, " }");
2016 return p;
2017 error:
2018 isl_printer_free(p);
2019 return NULL;
2022 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
2023 unsigned output_format)
2025 isl_printer *p;
2027 if (!pwqp)
2028 return;
2030 p = isl_printer_to_file(pwqp->dim->ctx, out);
2031 p = isl_printer_set_output_format(p, output_format);
2032 p = isl_printer_print_pw_qpolynomial(p, pwqp);
2034 isl_printer_free(p);
2037 static __isl_give isl_printer *isl_pwf_print_isl_body(
2038 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2040 struct isl_print_space_data data = { 0 };
2041 int i = 0;
2043 for (i = 0; i < pwf->n; ++i) {
2044 isl_space *space;
2046 if (i)
2047 p = isl_printer_print_str(p, "; ");
2048 space = isl_qpolynomial_fold_get_domain_space(pwf->p[i].fold);
2049 if (!isl_space_is_params(space)) {
2050 p = isl_print_space(space, p, 0, &data);
2051 p = isl_printer_print_str(p, " -> ");
2053 p = qpolynomial_fold_print(pwf->p[i].fold, p);
2054 p = print_disjuncts(set_to_map(pwf->p[i].set), space, p, 0);
2055 isl_space_free(space);
2058 return p;
2061 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
2062 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2064 struct isl_print_space_data data = { 0 };
2066 p = print_param_tuple(p, pwf->dim, &data);
2067 p = isl_printer_print_str(p, "{ ");
2068 if (pwf->n == 0) {
2069 if (!isl_space_is_set(pwf->dim)) {
2070 p = print_tuple(pwf->dim, p, isl_dim_in, &data);
2071 p = isl_printer_print_str(p, " -> ");
2073 p = isl_printer_print_str(p, "0");
2075 p = isl_pwf_print_isl_body(p, pwf);
2076 p = isl_printer_print_str(p, " }");
2077 return p;
2080 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2081 __isl_keep isl_local_space *ls, isl_int *c);
2083 /* We skip the constraint if it is implied by the div expression.
2085 * *first indicates whether this is the first constraint in the conjunction and
2086 * is updated if the constraint is actually printed.
2088 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
2089 __isl_keep isl_local_space *ls, isl_int *c, const char *op, int *first)
2091 unsigned o_div;
2092 unsigned n_div;
2093 int div;
2095 o_div = isl_local_space_offset(ls, isl_dim_div);
2096 n_div = isl_local_space_dim(ls, isl_dim_div);
2097 div = isl_seq_last_non_zero(c + o_div, n_div);
2098 if (div >= 0) {
2099 isl_bool is_div = isl_local_space_is_div_constraint(ls, c, div);
2100 if (is_div < 0)
2101 return isl_printer_free(p);
2102 if (is_div)
2103 return p;
2106 if (!*first)
2107 p = isl_printer_print_str(p, " && ");
2109 p = print_ls_affine_c(p, ls, c);
2110 p = isl_printer_print_str(p, " ");
2111 p = isl_printer_print_str(p, op);
2112 p = isl_printer_print_str(p, " 0");
2114 *first = 0;
2116 return p;
2119 static __isl_give isl_printer *print_ls_partial_affine_c(
2120 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2121 isl_int *c, unsigned len);
2123 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
2124 __isl_keep isl_space *space, __isl_keep isl_basic_set *bset)
2126 int i, j;
2127 int first = 1;
2128 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
2129 unsigned total = isl_basic_set_total_dim(bset) - n_div;
2130 isl_mat *div;
2131 isl_local_space *ls;
2133 div = isl_basic_set_get_divs(bset);
2134 ls = isl_local_space_alloc_div(isl_space_copy(space), div);
2135 for (i = 0; i < bset->n_eq; ++i) {
2136 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
2137 if (j < 0)
2138 p = print_constraint_c(p, ls,
2139 bset->eq[i], "==", &first);
2140 else {
2141 if (i)
2142 p = isl_printer_print_str(p, " && ");
2143 p = isl_printer_print_str(p, "(");
2144 p = print_ls_partial_affine_c(p, ls, bset->eq[i],
2145 1 + total + j);
2146 p = isl_printer_print_str(p, ") % ");
2147 p = isl_printer_print_isl_int(p,
2148 bset->eq[i][1 + total + j]);
2149 p = isl_printer_print_str(p, " == 0");
2150 first = 0;
2153 for (i = 0; i < bset->n_ineq; ++i)
2154 p = print_constraint_c(p, ls, bset->ineq[i], ">=", &first);
2155 isl_local_space_free(ls);
2156 return p;
2159 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
2160 __isl_keep isl_space *space, __isl_keep isl_set *set)
2162 int i;
2164 if (!set)
2165 return isl_printer_free(p);
2167 if (set->n == 0)
2168 p = isl_printer_print_str(p, "0");
2170 for (i = 0; i < set->n; ++i) {
2171 if (i)
2172 p = isl_printer_print_str(p, " || ");
2173 if (set->n > 1)
2174 p = isl_printer_print_str(p, "(");
2175 p = print_basic_set_c(p, space, set->p[i]);
2176 if (set->n > 1)
2177 p = isl_printer_print_str(p, ")");
2179 return p;
2182 /* Print the piecewise quasi-polynomial "pwqp" to "p" in C format.
2184 static __isl_give isl_printer *print_pw_qpolynomial_c(
2185 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2187 int i;
2188 isl_space *space;
2190 space = isl_pw_qpolynomial_get_domain_space(pwqp);
2191 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set)) {
2192 p = print_qpolynomial_c(p, space, pwqp->p[0].qp);
2193 isl_space_free(space);
2194 return p;
2197 for (i = 0; i < pwqp->n; ++i) {
2198 p = isl_printer_print_str(p, "(");
2199 p = print_set_c(p, space, pwqp->p[i].set);
2200 p = isl_printer_print_str(p, ") ? (");
2201 p = print_qpolynomial_c(p, space, pwqp->p[i].qp);
2202 p = isl_printer_print_str(p, ") : ");
2205 isl_space_free(space);
2206 p = isl_printer_print_str(p, "0");
2207 return p;
2210 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
2211 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2213 if (!p || !pwqp)
2214 goto error;
2216 if (p->output_format == ISL_FORMAT_ISL)
2217 return print_pw_qpolynomial_isl(p, pwqp);
2218 else if (p->output_format == ISL_FORMAT_C)
2219 return print_pw_qpolynomial_c(p, pwqp);
2220 isl_assert(p->ctx, 0, goto error);
2221 error:
2222 isl_printer_free(p);
2223 return NULL;
2226 static isl_stat print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
2228 struct isl_union_print_data *data;
2229 data = (struct isl_union_print_data *)user;
2231 if (!data->first)
2232 data->p = isl_printer_print_str(data->p, "; ");
2233 data->first = 0;
2235 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
2236 isl_pw_qpolynomial_free(pwqp);
2238 return isl_stat_ok;
2241 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
2242 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2244 struct isl_union_print_data data;
2245 struct isl_print_space_data space_data = { 0 };
2246 isl_space *space;
2248 space = isl_union_pw_qpolynomial_get_space(upwqp);
2249 p = print_param_tuple(p, space, &space_data);
2250 isl_space_free(space);
2251 p = isl_printer_print_str(p, "{ ");
2252 data.p = p;
2253 data.first = 1;
2254 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
2255 &data);
2256 p = data.p;
2257 p = isl_printer_print_str(p, " }");
2258 return p;
2261 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
2262 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2264 if (!p || !upwqp)
2265 goto error;
2267 if (p->output_format == ISL_FORMAT_ISL)
2268 return print_union_pw_qpolynomial_isl(p, upwqp);
2269 isl_die(p->ctx, isl_error_invalid,
2270 "invalid output format for isl_union_pw_qpolynomial",
2271 goto error);
2272 error:
2273 isl_printer_free(p);
2274 return NULL;
2277 /* Print the quasi-polynomial reduction "fold" to "p" in C format,
2278 * with the variable names taken from the domain space "space".
2280 static __isl_give isl_printer *print_qpolynomial_fold_c(
2281 __isl_take isl_printer *p, __isl_keep isl_space *space,
2282 __isl_keep isl_qpolynomial_fold *fold)
2284 int i;
2286 for (i = 0; i < fold->n - 1; ++i)
2287 if (fold->type == isl_fold_min)
2288 p = isl_printer_print_str(p, "min(");
2289 else if (fold->type == isl_fold_max)
2290 p = isl_printer_print_str(p, "max(");
2292 for (i = 0; i < fold->n; ++i) {
2293 if (i)
2294 p = isl_printer_print_str(p, ", ");
2295 p = print_qpolynomial_c(p, space, fold->qp[i]);
2296 if (i)
2297 p = isl_printer_print_str(p, ")");
2299 return p;
2302 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
2303 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
2305 if (!p || !fold)
2306 goto error;
2307 if (p->output_format == ISL_FORMAT_ISL)
2308 return qpolynomial_fold_print(fold, p);
2309 else if (p->output_format == ISL_FORMAT_C)
2310 return print_qpolynomial_fold_c(p, fold->dim, fold);
2311 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2312 goto error);
2313 error:
2314 isl_printer_free(p);
2315 return NULL;
2318 /* Print the piecewise quasi-polynomial reduction "pwf" to "p" in C format.
2320 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
2321 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2323 int i;
2324 isl_space *space;
2326 space = isl_pw_qpolynomial_fold_get_domain_space(pwf);
2327 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set)) {
2328 p = print_qpolynomial_fold_c(p, space, pwf->p[0].fold);
2329 isl_space_free(space);
2330 return p;
2333 for (i = 0; i < pwf->n; ++i) {
2334 p = isl_printer_print_str(p, "(");
2335 p = print_set_c(p, space, pwf->p[i].set);
2336 p = isl_printer_print_str(p, ") ? (");
2337 p = print_qpolynomial_fold_c(p, space, pwf->p[i].fold);
2338 p = isl_printer_print_str(p, ") : ");
2341 isl_space_free(space);
2342 p = isl_printer_print_str(p, "0");
2343 return p;
2346 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
2347 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2349 if (!p || !pwf)
2350 goto error;
2352 if (p->output_format == ISL_FORMAT_ISL)
2353 return print_pw_qpolynomial_fold_isl(p, pwf);
2354 else if (p->output_format == ISL_FORMAT_C)
2355 return print_pw_qpolynomial_fold_c(p, pwf);
2356 isl_assert(p->ctx, 0, goto error);
2357 error:
2358 isl_printer_free(p);
2359 return NULL;
2362 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
2363 FILE *out, unsigned output_format)
2365 isl_printer *p;
2367 if (!pwf)
2368 return;
2370 p = isl_printer_to_file(pwf->dim->ctx, out);
2371 p = isl_printer_set_output_format(p, output_format);
2372 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
2374 isl_printer_free(p);
2377 static isl_stat print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf,
2378 void *user)
2380 struct isl_union_print_data *data;
2381 data = (struct isl_union_print_data *)user;
2383 if (!data->first)
2384 data->p = isl_printer_print_str(data->p, "; ");
2385 data->first = 0;
2387 data->p = isl_pwf_print_isl_body(data->p, pwf);
2388 isl_pw_qpolynomial_fold_free(pwf);
2390 return isl_stat_ok;
2393 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
2394 __isl_take isl_printer *p,
2395 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2397 struct isl_union_print_data data;
2398 struct isl_print_space_data space_data = { 0 };
2399 isl_space *space;
2401 space = isl_union_pw_qpolynomial_fold_get_space(upwf);
2402 p = print_param_tuple(p, space, &space_data);
2403 isl_space_free(space);
2404 p = isl_printer_print_str(p, "{ ");
2405 data.p = p;
2406 data.first = 1;
2407 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
2408 &print_pwf_body, &data);
2409 p = data.p;
2410 p = isl_printer_print_str(p, " }");
2411 return p;
2414 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2415 __isl_take isl_printer *p,
2416 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2418 if (!p || !upwf)
2419 goto error;
2421 if (p->output_format == ISL_FORMAT_ISL)
2422 return print_union_pw_qpolynomial_fold_isl(p, upwf);
2423 isl_die(p->ctx, isl_error_invalid,
2424 "invalid output format for isl_union_pw_qpolynomial_fold",
2425 goto error);
2426 error:
2427 isl_printer_free(p);
2428 return NULL;
2431 /* Print the isl_constraint "c" to "p".
2433 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
2434 __isl_keep isl_constraint *c)
2436 struct isl_print_space_data data = { 0 };
2437 isl_local_space *ls;
2438 isl_space *space;
2439 isl_bool exists;
2441 if (!p || !c)
2442 goto error;
2444 ls = isl_constraint_get_local_space(c);
2445 if (!ls)
2446 return isl_printer_free(p);
2447 space = isl_local_space_get_space(ls);
2448 p = print_param_tuple(p, space, &data);
2449 p = isl_printer_print_str(p, "{ ");
2450 p = isl_print_space(space, p, 0, &data);
2451 p = isl_printer_print_str(p, " : ");
2452 exists = need_exists(p, ls->div);
2453 if (exists < 0)
2454 p = isl_printer_free(p);
2455 if (exists >= 0 && exists)
2456 p = open_exists(p, space, ls->div, 0);
2457 p = print_affine_of_len(space, ls->div, p, c->v->el, c->v->size);
2458 if (isl_constraint_is_equality(c))
2459 p = isl_printer_print_str(p, " = 0");
2460 else
2461 p = isl_printer_print_str(p, " >= 0");
2462 if (exists >= 0 && exists)
2463 p = isl_printer_print_str(p, s_close_exists[0]);
2464 p = isl_printer_print_str(p, " }");
2465 isl_space_free(space);
2466 isl_local_space_free(ls);
2468 return p;
2469 error:
2470 isl_printer_free(p);
2471 return NULL;
2474 static __isl_give isl_printer *isl_printer_print_space_isl(
2475 __isl_take isl_printer *p, __isl_keep isl_space *space)
2477 struct isl_print_space_data data = { 0 };
2479 if (!space)
2480 goto error;
2482 p = print_param_tuple(p, space, &data);
2484 p = isl_printer_print_str(p, "{ ");
2485 if (isl_space_is_params(space))
2486 p = isl_printer_print_str(p, s_such_that[0]);
2487 else
2488 p = isl_print_space(space, p, 0, &data);
2489 p = isl_printer_print_str(p, " }");
2491 return p;
2492 error:
2493 isl_printer_free(p);
2494 return NULL;
2497 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
2498 __isl_keep isl_space *space)
2500 if (!p || !space)
2501 return isl_printer_free(p);
2502 if (p->output_format == ISL_FORMAT_ISL)
2503 return isl_printer_print_space_isl(p, space);
2504 else if (p->output_format == ISL_FORMAT_OMEGA)
2505 return print_omega_parameters(space, p);
2507 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
2508 "output format not supported for space",
2509 return isl_printer_free(p));
2512 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
2513 __isl_keep isl_local_space *ls)
2515 struct isl_print_space_data data = { 0 };
2516 unsigned n_div;
2518 if (!ls)
2519 goto error;
2521 p = print_param_tuple(p, ls->dim, &data);
2522 p = isl_printer_print_str(p, "{ ");
2523 p = isl_print_space(ls->dim, p, 0, &data);
2524 n_div = isl_local_space_dim(ls, isl_dim_div);
2525 if (n_div > 0) {
2526 p = isl_printer_print_str(p, " : ");
2527 p = isl_printer_print_str(p, s_open_exists[0]);
2528 p = print_div_list(p, ls->dim, ls->div, 0, 1);
2529 p = isl_printer_print_str(p, s_close_exists[0]);
2530 } else if (isl_space_is_params(ls->dim))
2531 p = isl_printer_print_str(p, s_such_that[0]);
2532 p = isl_printer_print_str(p, " }");
2533 return p;
2534 error:
2535 isl_printer_free(p);
2536 return NULL;
2539 /* Print the (potentially rational) affine expression "aff" to "p",
2540 * with the variable names taken from "space".
2542 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2543 __isl_keep isl_space *space, __isl_keep isl_aff *aff)
2545 unsigned total;
2547 if (isl_aff_is_nan(aff))
2548 return isl_printer_print_str(p, "NaN");
2550 total = isl_local_space_dim(aff->ls, isl_dim_all);
2551 p = isl_printer_print_str(p, "(");
2552 p = print_affine_of_len(space, aff->ls->div, p,
2553 aff->v->el + 1, 1 + total);
2554 if (isl_int_is_one(aff->v->el[0]))
2555 p = isl_printer_print_str(p, ")");
2556 else {
2557 p = isl_printer_print_str(p, ")/");
2558 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2561 return p;
2564 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2565 __isl_keep isl_aff *aff)
2567 struct isl_print_space_data data = { 0 };
2569 if (isl_space_is_params(aff->ls->dim))
2571 else {
2572 p = print_tuple(aff->ls->dim, p, isl_dim_set, &data);
2573 p = isl_printer_print_str(p, " -> ");
2575 p = isl_printer_print_str(p, "[");
2576 p = print_aff_body(p, aff->ls->dim, aff);
2577 p = isl_printer_print_str(p, "]");
2579 return p;
2582 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2583 __isl_keep isl_aff *aff)
2585 struct isl_print_space_data data = { 0 };
2587 if (!aff)
2588 goto error;
2590 p = print_param_tuple(p, aff->ls->dim, &data);
2591 p = isl_printer_print_str(p, "{ ");
2592 p = print_aff(p, aff);
2593 p = isl_printer_print_str(p, " }");
2594 return p;
2595 error:
2596 isl_printer_free(p);
2597 return NULL;
2600 /* Print the body of an isl_pw_aff, i.e., a semicolon delimited
2601 * sequence of affine expressions, each followed by constraints.
2603 static __isl_give isl_printer *print_pw_aff_body(
2604 __isl_take isl_printer *p, __isl_keep isl_pw_aff *pa)
2606 int i;
2608 if (!pa)
2609 return isl_printer_free(p);
2611 for (i = 0; i < pa->n; ++i) {
2612 isl_space *space;
2614 if (i)
2615 p = isl_printer_print_str(p, "; ");
2616 p = print_aff(p, pa->p[i].aff);
2617 space = isl_aff_get_domain_space(pa->p[i].aff);
2618 p = print_disjuncts(set_to_map(pa->p[i].set), space, p, 0);
2619 isl_space_free(space);
2622 return p;
2625 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2626 __isl_keep isl_pw_aff *pwaff)
2628 struct isl_print_space_data data = { 0 };
2630 if (!pwaff)
2631 goto error;
2633 p = print_param_tuple(p, pwaff->dim, &data);
2634 p = isl_printer_print_str(p, "{ ");
2635 p = print_pw_aff_body(p, pwaff);
2636 p = isl_printer_print_str(p, " }");
2637 return p;
2638 error:
2639 isl_printer_free(p);
2640 return NULL;
2643 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2644 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2646 if (type == isl_dim_div) {
2647 p = isl_printer_print_str(p, "floord(");
2648 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2649 p = isl_printer_print_str(p, ", ");
2650 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2651 p = isl_printer_print_str(p, ")");
2652 } else {
2653 const char *name;
2655 name = isl_space_get_dim_name(ls->dim, type, pos);
2656 if (!name)
2657 name = "UNNAMED";
2658 p = isl_printer_print_str(p, name);
2660 return p;
2663 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2664 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2666 enum isl_dim_type type;
2668 if (!p || !ls)
2669 return isl_printer_free(p);
2671 if (pos == 0)
2672 return isl_printer_print_isl_int(p, c);
2674 if (isl_int_is_one(c))
2676 else if (isl_int_is_negone(c))
2677 p = isl_printer_print_str(p, "-");
2678 else {
2679 p = isl_printer_print_isl_int(p, c);
2680 p = isl_printer_print_str(p, "*");
2682 type = pos2type(ls->dim, &pos);
2683 p = print_ls_name_c(p, ls, type, pos);
2684 return p;
2687 static __isl_give isl_printer *print_ls_partial_affine_c(
2688 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2689 isl_int *c, unsigned len)
2691 int i;
2692 int first;
2694 for (i = 0, first = 1; i < len; ++i) {
2695 int flip = 0;
2696 if (isl_int_is_zero(c[i]))
2697 continue;
2698 if (!first) {
2699 if (isl_int_is_neg(c[i])) {
2700 flip = 1;
2701 isl_int_neg(c[i], c[i]);
2702 p = isl_printer_print_str(p, " - ");
2703 } else
2704 p = isl_printer_print_str(p, " + ");
2706 first = 0;
2707 p = print_ls_term_c(p, ls, c[i], i);
2708 if (flip)
2709 isl_int_neg(c[i], c[i]);
2711 if (first)
2712 p = isl_printer_print_str(p, "0");
2713 return p;
2716 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2717 __isl_keep isl_local_space *ls, isl_int *c)
2719 unsigned len = 1 + isl_local_space_dim(ls, isl_dim_all);
2720 return print_ls_partial_affine_c(p, ls, c, len);
2723 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2724 __isl_keep isl_aff *aff)
2726 unsigned total;
2728 total = isl_local_space_dim(aff->ls, isl_dim_all);
2729 if (!isl_int_is_one(aff->v->el[0]))
2730 p = isl_printer_print_str(p, "(");
2731 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2732 if (!isl_int_is_one(aff->v->el[0])) {
2733 p = isl_printer_print_str(p, ")/");
2734 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2736 return p;
2739 /* In the C format, we cannot express that "pwaff" may be undefined
2740 * on parts of the domain space. We therefore assume that the expression
2741 * will only be evaluated on its definition domain and compute the gist
2742 * of each cell with respect to this domain.
2744 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2745 __isl_keep isl_pw_aff *pwaff)
2747 isl_set *domain;
2748 isl_ast_build *build;
2749 isl_ast_expr *expr;
2751 if (pwaff->n < 1)
2752 isl_die(p->ctx, isl_error_unsupported,
2753 "cannot print empty isl_pw_aff in C format",
2754 return isl_printer_free(p));
2756 domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
2757 build = isl_ast_build_from_context(domain);
2758 expr = isl_ast_build_expr_from_pw_aff(build, isl_pw_aff_copy(pwaff));
2759 p = isl_printer_print_ast_expr(p, expr);
2760 isl_ast_expr_free(expr);
2761 isl_ast_build_free(build);
2763 return p;
2766 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2767 __isl_keep isl_aff *aff)
2769 if (!p || !aff)
2770 goto error;
2772 if (p->output_format == ISL_FORMAT_ISL)
2773 return print_aff_isl(p, aff);
2774 else if (p->output_format == ISL_FORMAT_C)
2775 return print_aff_c(p, aff);
2776 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2777 goto error);
2778 error:
2779 isl_printer_free(p);
2780 return NULL;
2783 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2784 __isl_keep isl_pw_aff *pwaff)
2786 if (!p || !pwaff)
2787 goto error;
2789 if (p->output_format == ISL_FORMAT_ISL)
2790 return print_pw_aff_isl(p, pwaff);
2791 else if (p->output_format == ISL_FORMAT_C)
2792 return print_pw_aff_c(p, pwaff);
2793 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2794 goto error);
2795 error:
2796 isl_printer_free(p);
2797 return NULL;
2800 /* Print "pa" in a sequence of isl_pw_affs delimited by semicolons.
2801 * Each isl_pw_aff itself is also printed as semicolon delimited
2802 * sequence of pieces.
2803 * If data->first = 1, then this is the first in the sequence.
2804 * Update data->first to tell the next element that it is not the first.
2806 static isl_stat print_pw_aff_body_wrap(__isl_take isl_pw_aff *pa,
2807 void *user)
2809 struct isl_union_print_data *data;
2810 data = (struct isl_union_print_data *) user;
2812 if (!data->first)
2813 data->p = isl_printer_print_str(data->p, "; ");
2814 data->first = 0;
2816 data->p = print_pw_aff_body(data->p, pa);
2817 isl_pw_aff_free(pa);
2819 return data->p ? isl_stat_ok : isl_stat_error;
2822 /* Print the body of an isl_union_pw_aff, i.e., a semicolon delimited
2823 * sequence of affine expressions, each followed by constraints,
2824 * with the sequence enclosed in braces.
2826 static __isl_give isl_printer *print_union_pw_aff_body(
2827 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2829 struct isl_union_print_data data = { p, 1 };
2831 p = isl_printer_print_str(p, s_open_set[0]);
2832 data.p = p;
2833 if (isl_union_pw_aff_foreach_pw_aff(upa,
2834 &print_pw_aff_body_wrap, &data) < 0)
2835 data.p = isl_printer_free(p);
2836 p = data.p;
2837 p = isl_printer_print_str(p, s_close_set[0]);
2839 return p;
2842 /* Print the isl_union_pw_aff "upa" to "p" in isl format.
2844 * The individual isl_pw_affs are delimited by a semicolon.
2846 static __isl_give isl_printer *print_union_pw_aff_isl(
2847 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2849 struct isl_print_space_data data = { 0 };
2850 isl_space *space;
2852 space = isl_union_pw_aff_get_space(upa);
2853 p = print_param_tuple(p, space, &data);
2854 isl_space_free(space);
2855 p = print_union_pw_aff_body(p, upa);
2856 return p;
2859 /* Print the isl_union_pw_aff "upa" to "p".
2861 * We currently only support an isl format.
2863 __isl_give isl_printer *isl_printer_print_union_pw_aff(
2864 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2866 if (!p || !upa)
2867 return isl_printer_free(p);
2869 if (p->output_format == ISL_FORMAT_ISL)
2870 return print_union_pw_aff_isl(p, upa);
2871 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2872 "unsupported output format", return isl_printer_free(p));
2875 /* Print dimension "pos" of data->space to "p".
2877 * data->user is assumed to be an isl_multi_aff.
2879 * If the current dimension is an output dimension, then print
2880 * the corresponding expression. Otherwise, print the name of the dimension.
2882 static __isl_give isl_printer *print_dim_ma(__isl_take isl_printer *p,
2883 struct isl_print_space_data *data, unsigned pos)
2885 isl_multi_aff *ma = data->user;
2887 if (data->type == isl_dim_out) {
2888 isl_space *space;
2890 space = isl_multi_aff_get_domain_space(ma);
2891 p = print_aff_body(p, space, ma->u.p[pos]);
2892 isl_space_free(space);
2893 } else {
2894 p = print_name(data->space, p, data->type, pos, data->latex);
2897 return p;
2900 static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
2901 __isl_keep isl_multi_aff *maff)
2903 struct isl_print_space_data data = { 0 };
2905 data.print_dim = &print_dim_ma;
2906 data.user = maff;
2907 return isl_print_space(maff->space, p, 0, &data);
2910 static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
2911 __isl_keep isl_multi_aff *maff)
2913 struct isl_print_space_data data = { 0 };
2915 if (!maff)
2916 goto error;
2918 p = print_param_tuple(p, maff->space, &data);
2919 p = isl_printer_print_str(p, "{ ");
2920 p = print_multi_aff(p, maff);
2921 p = isl_printer_print_str(p, " }");
2922 return p;
2923 error:
2924 isl_printer_free(p);
2925 return NULL;
2928 __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
2929 __isl_keep isl_multi_aff *maff)
2931 if (!p || !maff)
2932 goto error;
2934 if (p->output_format == ISL_FORMAT_ISL)
2935 return print_multi_aff_isl(p, maff);
2936 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2937 goto error);
2938 error:
2939 isl_printer_free(p);
2940 return NULL;
2943 static __isl_give isl_printer *print_pw_multi_aff_body(
2944 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2946 int i;
2948 if (!pma)
2949 goto error;
2951 for (i = 0; i < pma->n; ++i) {
2952 isl_space *space;
2954 if (i)
2955 p = isl_printer_print_str(p, "; ");
2956 p = print_multi_aff(p, pma->p[i].maff);
2957 space = isl_multi_aff_get_domain_space(pma->p[i].maff);
2958 p = print_disjuncts(set_to_map(pma->p[i].set), space, p, 0);
2959 isl_space_free(space);
2961 return p;
2962 error:
2963 isl_printer_free(p);
2964 return NULL;
2967 static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
2968 __isl_keep isl_pw_multi_aff *pma)
2970 struct isl_print_space_data data = { 0 };
2972 if (!pma)
2973 goto error;
2975 p = print_param_tuple(p, pma->dim, &data);
2976 p = isl_printer_print_str(p, "{ ");
2977 p = print_pw_multi_aff_body(p, pma);
2978 p = isl_printer_print_str(p, " }");
2979 return p;
2980 error:
2981 isl_printer_free(p);
2982 return NULL;
2985 /* Print the unnamed, single-dimensional piecewise multi affine expression "pma"
2986 * to "p".
2988 static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
2989 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2991 int i;
2992 isl_space *space;
2994 space = isl_pw_multi_aff_get_domain_space(pma);
2995 for (i = 0; i < pma->n - 1; ++i) {
2996 p = isl_printer_print_str(p, "(");
2997 p = print_set_c(p, space, pma->p[i].set);
2998 p = isl_printer_print_str(p, ") ? (");
2999 p = print_aff_c(p, pma->p[i].maff->u.p[0]);
3000 p = isl_printer_print_str(p, ") : ");
3002 isl_space_free(space);
3004 return print_aff_c(p, pma->p[pma->n - 1].maff->u.p[0]);
3007 static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
3008 __isl_keep isl_pw_multi_aff *pma)
3010 int n;
3011 const char *name;
3013 if (!pma)
3014 goto error;
3015 if (pma->n < 1)
3016 isl_die(p->ctx, isl_error_unsupported,
3017 "cannot print empty isl_pw_multi_aff in C format",
3018 goto error);
3019 name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
3020 if (!name && isl_pw_multi_aff_dim(pma, isl_dim_out) == 1)
3021 return print_unnamed_pw_multi_aff_c(p, pma);
3022 if (!name)
3023 isl_die(p->ctx, isl_error_unsupported,
3024 "cannot print unnamed isl_pw_multi_aff in C format",
3025 goto error);
3027 p = isl_printer_print_str(p, name);
3028 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
3029 if (n != 0)
3030 isl_die(p->ctx, isl_error_unsupported,
3031 "not supported yet", goto error);
3033 return p;
3034 error:
3035 isl_printer_free(p);
3036 return NULL;
3039 __isl_give isl_printer *isl_printer_print_pw_multi_aff(
3040 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3042 if (!p || !pma)
3043 goto error;
3045 if (p->output_format == ISL_FORMAT_ISL)
3046 return print_pw_multi_aff_isl(p, pma);
3047 if (p->output_format == ISL_FORMAT_C)
3048 return print_pw_multi_aff_c(p, pma);
3049 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3050 goto error);
3051 error:
3052 isl_printer_free(p);
3053 return NULL;
3056 static isl_stat print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
3057 void *user)
3059 struct isl_union_print_data *data;
3060 data = (struct isl_union_print_data *) user;
3062 if (!data->first)
3063 data->p = isl_printer_print_str(data->p, "; ");
3064 data->first = 0;
3066 data->p = print_pw_multi_aff_body(data->p, pma);
3067 isl_pw_multi_aff_free(pma);
3069 return isl_stat_ok;
3072 static __isl_give isl_printer *print_union_pw_multi_aff_isl(
3073 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
3075 struct isl_union_print_data data;
3076 struct isl_print_space_data space_data = { 0 };
3077 isl_space *space;
3079 space = isl_union_pw_multi_aff_get_space(upma);
3080 p = print_param_tuple(p, space, &space_data);
3081 isl_space_free(space);
3082 p = isl_printer_print_str(p, s_open_set[0]);
3083 data.p = p;
3084 data.first = 1;
3085 isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3086 &print_pw_multi_aff_body_wrap, &data);
3087 p = data.p;
3088 p = isl_printer_print_str(p, s_close_set[0]);
3089 return p;
3092 __isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
3093 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
3095 if (!p || !upma)
3096 goto error;
3098 if (p->output_format == ISL_FORMAT_ISL)
3099 return print_union_pw_multi_aff_isl(p, upma);
3100 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3101 goto error);
3102 error:
3103 isl_printer_free(p);
3104 return NULL;
3107 /* Print dimension "pos" of data->space to "p".
3109 * data->user is assumed to be an isl_multi_pw_aff.
3111 * If the current dimension is an output dimension, then print
3112 * the corresponding piecewise affine expression.
3113 * Otherwise, print the name of the dimension.
3115 static __isl_give isl_printer *print_dim_mpa(__isl_take isl_printer *p,
3116 struct isl_print_space_data *data, unsigned pos)
3118 int i;
3119 int need_parens;
3120 isl_space *space;
3121 isl_multi_pw_aff *mpa = data->user;
3122 isl_pw_aff *pa;
3124 if (data->type != isl_dim_out)
3125 return print_name(data->space, p, data->type, pos, data->latex);
3127 pa = mpa->u.p[pos];
3128 if (pa->n == 0)
3129 return isl_printer_print_str(p, "(0 : false)");
3131 need_parens = pa->n != 1 || !isl_set_plain_is_universe(pa->p[0].set);
3132 if (need_parens)
3133 p = isl_printer_print_str(p, "(");
3134 space = isl_multi_pw_aff_get_domain_space(mpa);
3135 for (i = 0; i < pa->n; ++i) {
3137 if (i)
3138 p = isl_printer_print_str(p, "; ");
3139 p = print_aff_body(p, space, pa->p[i].aff);
3140 p = print_disjuncts(pa->p[i].set, space, p, 0);
3142 isl_space_free(space);
3143 if (need_parens)
3144 p = isl_printer_print_str(p, ")");
3146 return p;
3149 /* Print "mpa" to "p" in isl format.
3151 * If "mpa" is zero-dimensional and has a non-trivial explicit domain,
3152 * then it is printed after the tuple of affine expressions.
3154 static __isl_give isl_printer *print_multi_pw_aff_isl(__isl_take isl_printer *p,
3155 __isl_keep isl_multi_pw_aff *mpa)
3157 struct isl_print_space_data data = { 0 };
3158 isl_bool has_domain;
3160 if (!mpa)
3161 return isl_printer_free(p);
3163 p = print_param_tuple(p, mpa->space, &data);
3164 p = isl_printer_print_str(p, "{ ");
3165 data.print_dim = &print_dim_mpa;
3166 data.user = mpa;
3167 p = isl_print_space(mpa->space, p, 0, &data);
3168 has_domain = isl_multi_pw_aff_has_non_trivial_domain(mpa);
3169 if (has_domain < 0)
3170 return isl_printer_free(p);
3171 if (has_domain) {
3172 isl_space *space;
3174 space = isl_space_domain(isl_space_copy(mpa->space));
3175 p = print_disjuncts_set(mpa->u.dom, space, p, 0);
3176 isl_space_free(space);
3178 p = isl_printer_print_str(p, " }");
3179 return p;
3182 __isl_give isl_printer *isl_printer_print_multi_pw_aff(
3183 __isl_take isl_printer *p, __isl_keep isl_multi_pw_aff *mpa)
3185 if (!p || !mpa)
3186 return isl_printer_free(p);
3188 if (p->output_format == ISL_FORMAT_ISL)
3189 return print_multi_pw_aff_isl(p, mpa);
3190 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3191 return isl_printer_free(p));
3194 /* Print dimension "pos" of data->space to "p".
3196 * data->user is assumed to be an isl_multi_val.
3198 * If the current dimension is an output dimension, then print
3199 * the corresponding value. Otherwise, print the name of the dimension.
3201 static __isl_give isl_printer *print_dim_mv(__isl_take isl_printer *p,
3202 struct isl_print_space_data *data, unsigned pos)
3204 isl_multi_val *mv = data->user;
3206 if (data->type == isl_dim_out)
3207 return isl_printer_print_val(p, mv->u.p[pos]);
3208 else
3209 return print_name(data->space, p, data->type, pos, data->latex);
3212 /* Print the isl_multi_val "mv" to "p" in isl format.
3214 static __isl_give isl_printer *print_multi_val_isl(__isl_take isl_printer *p,
3215 __isl_keep isl_multi_val *mv)
3217 struct isl_print_space_data data = { 0 };
3219 if (!mv)
3220 return isl_printer_free(p);
3222 p = print_param_tuple(p, mv->space, &data);
3223 p = isl_printer_print_str(p, "{ ");
3224 data.print_dim = &print_dim_mv;
3225 data.user = mv;
3226 p = isl_print_space(mv->space, p, 0, &data);
3227 p = isl_printer_print_str(p, " }");
3228 return p;
3231 /* Print the isl_multi_val "mv" to "p".
3233 * Currently only supported in isl format.
3235 __isl_give isl_printer *isl_printer_print_multi_val(
3236 __isl_take isl_printer *p, __isl_keep isl_multi_val *mv)
3238 if (!p || !mv)
3239 return isl_printer_free(p);
3241 if (p->output_format == ISL_FORMAT_ISL)
3242 return print_multi_val_isl(p, mv);
3243 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3244 return isl_printer_free(p));
3247 /* Print dimension "pos" of data->space to "p".
3249 * data->user is assumed to be an isl_multi_union_pw_aff.
3251 * The current dimension is necessarily a set dimension, so
3252 * we print the corresponding isl_union_pw_aff, including
3253 * the braces.
3255 static __isl_give isl_printer *print_union_pw_aff_dim(__isl_take isl_printer *p,
3256 struct isl_print_space_data *data, unsigned pos)
3258 isl_multi_union_pw_aff *mupa = data->user;
3259 isl_union_pw_aff *upa;
3261 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, pos);
3262 p = print_union_pw_aff_body(p, upa);
3263 isl_union_pw_aff_free(upa);
3265 return p;
3268 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3270 * If "mupa" is zero-dimensional and has a non-trivial explicit domain,
3271 * then it is printed after the tuple of affine expressions.
3272 * In order to clarify that this domain belongs to the expression,
3273 * the tuple along with the domain are placed inside parentheses.
3274 * If "mupa" has any parameters, then the opening parenthesis
3275 * appears after the parameter declarations.
3277 static __isl_give isl_printer *print_multi_union_pw_aff_isl(
3278 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3280 struct isl_print_space_data data = { 0 };
3281 isl_bool has_domain;
3282 isl_space *space;
3284 if (!mupa)
3285 return isl_printer_free(p);
3286 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
3287 if (has_domain < 0)
3288 return isl_printer_free(p);
3290 space = isl_multi_union_pw_aff_get_space(mupa);
3291 p = print_param_tuple(p, space, &data);
3293 if (has_domain)
3294 p = isl_printer_print_str(p, "(");
3296 data.print_dim = &print_union_pw_aff_dim;
3297 data.user = mupa;
3299 p = isl_print_space(space, p, 0, &data);
3300 isl_space_free(space);
3302 if (has_domain) {
3303 p = isl_printer_print_str(p, " : ");
3304 p = isl_printer_print_union_set_isl_body(p, mupa->u.dom);
3305 p = isl_printer_print_str(p, ")");
3308 return p;
3311 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3313 * We currently only support an isl format.
3315 __isl_give isl_printer *isl_printer_print_multi_union_pw_aff(
3316 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3318 if (!p || !mupa)
3319 return isl_printer_free(p);
3321 if (p->output_format == ISL_FORMAT_ISL)
3322 return print_multi_union_pw_aff_isl(p, mupa);
3323 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
3324 "unsupported output format", return isl_printer_free(p));