isl_poly_is_cst: use isl_bool_ok
[isl.git] / isl_output.c
bloba3ca35f92b997db36970a80a6d23eac162504c61
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 isl_size n_in = isl_basic_map_dim(bmap, isl_dim_in);
64 isl_size n_out = isl_basic_map_dim(bmap, isl_dim_out);
65 isl_size nparam = isl_basic_map_dim(bmap, isl_dim_param);
66 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
68 if (n_in < 0 || n_out < 0 || nparam < 0)
69 return isl_printer_free(p);
71 p = isl_printer_start_line(p);
72 p = isl_printer_print_int(p, ineq);
73 for (i = 0; i < n_out; ++i) {
74 p = isl_printer_print_str(p, " ");
75 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
77 for (i = 0; i < n_in; ++i) {
78 p = isl_printer_print_str(p, " ");
79 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
81 for (i = 0; i < bmap->n_div; ++i) {
82 p = isl_printer_print_str(p, " ");
83 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
85 for (i = 0; i < nparam; ++i) {
86 p = isl_printer_print_str(p, " ");
87 p = isl_printer_print_isl_int(p, c[1+i]);
89 p = isl_printer_print_str(p, " ");
90 p = isl_printer_print_isl_int(p, c[0]);
91 p = isl_printer_end_line(p);
92 return p;
95 static __isl_give isl_printer *print_constraints_polylib(
96 struct isl_basic_map *bmap, __isl_take isl_printer *p)
98 int i;
100 p = isl_printer_set_isl_int_width(p, 5);
102 for (i = 0; i < bmap->n_eq; ++i)
103 p = print_constraint_polylib(bmap, 0, i, p);
104 for (i = 0; i < bmap->n_ineq; ++i)
105 p = print_constraint_polylib(bmap, 1, i, p);
107 return p;
110 static __isl_give isl_printer *bset_print_constraints_polylib(
111 struct isl_basic_set *bset, __isl_take isl_printer *p)
113 return print_constraints_polylib(bset_to_bmap(bset), p);
116 static __isl_give isl_printer *isl_basic_map_print_polylib(
117 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
119 isl_size total;
121 total = isl_basic_map_dim(bmap, isl_dim_all);
122 if (total < 0)
123 return isl_printer_free(p);
124 p = isl_printer_start_line(p);
125 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
126 p = isl_printer_print_str(p, " ");
127 p = isl_printer_print_int(p, 1 + total + 1);
128 if (ext) {
129 isl_size n_in = isl_basic_map_dim(bmap, isl_dim_in);
130 isl_size n_out = isl_basic_map_dim(bmap, isl_dim_out);
131 isl_size n_div = isl_basic_map_dim(bmap, isl_dim_div);
132 isl_size nparam = isl_basic_map_dim(bmap, isl_dim_param);
134 if (n_in < 0 || n_out < 0 || n_div < 0 || nparam < 0)
135 return isl_printer_free(p);
137 p = isl_printer_print_str(p, " ");
138 p = isl_printer_print_int(p, n_out);
139 p = isl_printer_print_str(p, " ");
140 p = isl_printer_print_int(p, n_in);
141 p = isl_printer_print_str(p, " ");
142 p = isl_printer_print_int(p, n_div);
143 p = isl_printer_print_str(p, " ");
144 p = isl_printer_print_int(p, nparam);
146 p = isl_printer_end_line(p);
147 return print_constraints_polylib(bmap, p);
150 static __isl_give isl_printer *isl_basic_set_print_polylib(
151 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
153 return isl_basic_map_print_polylib(bset_to_bmap(bset), p, ext);
156 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
157 __isl_take isl_printer *p, int ext)
159 int i;
161 p = isl_printer_start_line(p);
162 p = isl_printer_print_int(p, map->n);
163 p = isl_printer_end_line(p);
164 for (i = 0; i < map->n; ++i) {
165 p = isl_printer_start_line(p);
166 p = isl_printer_end_line(p);
167 p = isl_basic_map_print_polylib(map->p[i], p, ext);
169 return p;
172 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
173 __isl_take isl_printer *p, int ext)
175 return isl_map_print_polylib(set_to_map(set), p, ext);
178 static isl_size count_same_name(__isl_keep isl_space *space,
179 enum isl_dim_type type, unsigned pos, const char *name)
181 enum isl_dim_type t;
182 int p;
183 isl_size s;
184 int count = 0;
186 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
187 s = t == type ? pos : isl_space_dim(space, t);
188 if (s < 0)
189 return isl_size_error;
190 for (p = 0; p < s; ++p) {
191 const char *n = isl_space_get_dim_name(space, t, p);
192 if (n && !strcmp(n, name))
193 count++;
196 return count;
199 /* Print the name of the variable of type "type" and position "pos"
200 * in "space" to "p".
202 static __isl_give isl_printer *print_name(__isl_keep isl_space *space,
203 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
204 int latex)
206 const char *name;
207 char buffer[20];
208 isl_size primes;
210 name = type == isl_dim_div ? NULL
211 : isl_space_get_dim_name(space, type, pos);
213 if (!name) {
214 const char *prefix;
215 if (type == isl_dim_param)
216 prefix = s_param_prefix[latex];
217 else if (type == isl_dim_div)
218 prefix = s_div_prefix[latex];
219 else if (isl_space_is_set(space) || type == isl_dim_in)
220 prefix = s_input_prefix[latex];
221 else
222 prefix = s_output_prefix[latex];
223 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
224 name = buffer;
226 primes = count_same_name(space, name == buffer ? isl_dim_div : type,
227 pos, name);
228 if (primes < 0)
229 return isl_printer_free(p);
230 p = isl_printer_print_str(p, name);
231 while (primes-- > 0)
232 p = isl_printer_print_str(p, "'");
233 return p;
236 static isl_stat pos2type(__isl_keep isl_space *space,
237 enum isl_dim_type *type, unsigned *pos)
239 isl_size n_in = isl_space_dim(space, isl_dim_in);
240 isl_size n_out = isl_space_dim(space, isl_dim_out);
241 isl_size nparam = isl_space_dim(space, isl_dim_param);
243 if (n_in < 0 || n_out < 0 || nparam < 0)
244 return isl_stat_error;
246 if (*pos < 1 + nparam) {
247 *type = isl_dim_param;
248 *pos -= 1;
249 } else if (*pos < 1 + nparam + n_in) {
250 *type = isl_dim_in;
251 *pos -= 1 + nparam;
252 } else if (*pos < 1 + nparam + n_in + n_out) {
253 *type = isl_dim_out;
254 *pos -= 1 + nparam + n_in;
255 } else {
256 *type = isl_dim_div;
257 *pos -= 1 + nparam + n_in + n_out;
260 return isl_stat_ok;
263 /* Can the div expression of the integer division at position "row" of "div"
264 * be printed?
265 * In particular, are the div expressions available and does the selected
266 * variable have a known explicit representation?
267 * Furthermore, the Omega format does not allow any div expressions
268 * to be printed.
270 static isl_bool can_print_div_expr(__isl_keep isl_printer *p,
271 __isl_keep isl_mat *div, int pos)
273 if (p->output_format == ISL_FORMAT_OMEGA)
274 return isl_bool_false;
275 if (!div)
276 return isl_bool_false;
277 return !isl_int_is_zero(div->row[pos][0]);
280 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
281 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
283 static __isl_give isl_printer *print_term(__isl_keep isl_space *space,
284 __isl_keep isl_mat *div,
285 isl_int c, unsigned pos, __isl_take isl_printer *p, int latex)
287 enum isl_dim_type type;
288 int print_div_def;
290 if (!p || !space)
291 return isl_printer_free(p);
293 if (pos == 0)
294 return isl_printer_print_isl_int(p, c);
296 if (pos2type(space, &type, &pos) < 0)
297 return isl_printer_free(p);
298 print_div_def = type == isl_dim_div && can_print_div_expr(p, div, pos);
300 if (isl_int_is_one(c))
302 else if (isl_int_is_negone(c))
303 p = isl_printer_print_str(p, "-");
304 else {
305 p = isl_printer_print_isl_int(p, c);
306 if (p->output_format == ISL_FORMAT_C || print_div_def)
307 p = isl_printer_print_str(p, "*");
309 if (print_div_def)
310 p = print_div(space, div, pos, p);
311 else
312 p = print_name(space, p, type, pos, latex);
313 return p;
316 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_space *dim,
317 __isl_keep isl_mat *div,
318 __isl_take isl_printer *p, isl_int *c, int len)
320 int i;
321 int first;
323 for (i = 0, first = 1; i < len; ++i) {
324 int flip = 0;
325 if (isl_int_is_zero(c[i]))
326 continue;
327 if (!first) {
328 if (isl_int_is_neg(c[i])) {
329 flip = 1;
330 isl_int_neg(c[i], c[i]);
331 p = isl_printer_print_str(p, " - ");
332 } else
333 p = isl_printer_print_str(p, " + ");
335 first = 0;
336 p = print_term(dim, div, c[i], i, p, 0);
337 if (flip)
338 isl_int_neg(c[i], c[i]);
340 if (first)
341 p = isl_printer_print_str(p, "0");
342 return p;
345 /* Print an affine expression "c"
346 * to "p", with the variable names taken from "space" and
347 * the integer division definitions taken from "div".
349 static __isl_give isl_printer *print_affine(__isl_take isl_printer *p,
350 __isl_keep isl_space *space, __isl_keep isl_mat *div, isl_int *c)
352 isl_size n_div, total;
353 unsigned len;
355 total = isl_space_dim(space, isl_dim_all);
356 n_div = isl_mat_rows(div);
357 if (total < 0 || n_div < 0)
358 return isl_printer_free(p);
359 len = 1 + total + n_div;
360 return print_affine_of_len(space, div, p, c, len);
363 /* offset is the offset of local_space inside data->type of data->space.
365 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
366 __isl_keep isl_space *local_space, enum isl_dim_type local_type,
367 struct isl_print_space_data *data, int offset)
369 int i;
370 isl_size dim;
372 if (data->space != local_space && local_type == isl_dim_out)
373 offset += local_space->n_in;
375 dim = isl_space_dim(local_space, local_type);
376 if (dim < 0)
377 return isl_printer_free(p);
378 for (i = 0; i < dim; ++i) {
379 if (i)
380 p = isl_printer_print_str(p, ", ");
381 if (data->print_dim)
382 p = data->print_dim(p, data, offset + i);
383 else
384 p = print_name(data->space, p, data->type, offset + i,
385 data->latex);
387 return p;
390 static __isl_give isl_printer *print_var_list(__isl_take isl_printer *p,
391 __isl_keep isl_space *space, enum isl_dim_type type)
393 struct isl_print_space_data data = { .space = space, .type = type };
395 return print_nested_var_list(p, space, type, &data, 0);
398 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
399 __isl_keep isl_space *local_dim,
400 struct isl_print_space_data *data, int offset);
402 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
403 __isl_keep isl_space *local_space, enum isl_dim_type local_type,
404 struct isl_print_space_data *data, int offset)
406 const char *name = NULL;
407 isl_size n = isl_space_dim(local_space, local_type);
409 if (n < 0)
410 return isl_printer_free(p);
411 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
412 name = isl_space_get_tuple_name(local_space, local_type);
413 if (name) {
414 if (data->latex)
415 p = isl_printer_print_str(p, "\\mathrm{");
416 p = isl_printer_print_str(p, name);
417 if (data->latex)
418 p = isl_printer_print_str(p, "}");
421 if (!data->latex || n != 1 || name)
422 p = isl_printer_print_str(p, s_open_list[data->latex]);
423 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
424 local_space->nested[local_type - isl_dim_in]) {
425 if (data->space != local_space && local_type == isl_dim_out)
426 offset += local_space->n_in;
427 p = print_nested_map_dim(p,
428 local_space->nested[local_type - isl_dim_in],
429 data, offset);
430 } else
431 p = print_nested_var_list(p, local_space, local_type, data,
432 offset);
433 if (!data->latex || n != 1 || name)
434 p = isl_printer_print_str(p, s_close_list[data->latex]);
435 return p;
438 static __isl_give isl_printer *print_tuple(__isl_keep isl_space *dim,
439 __isl_take isl_printer *p, enum isl_dim_type type,
440 struct isl_print_space_data *data)
442 data->space = dim;
443 data->type = type;
444 return print_nested_tuple(p, dim, type, data, 0);
447 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
448 __isl_keep isl_space *local_dim,
449 struct isl_print_space_data *data, int offset)
451 p = print_nested_tuple(p, local_dim, isl_dim_in, data, offset);
452 p = isl_printer_print_str(p, s_to[data->latex]);
453 p = print_nested_tuple(p, local_dim, isl_dim_out, data, offset);
455 return p;
458 __isl_give isl_printer *isl_print_space(__isl_keep isl_space *space,
459 __isl_take isl_printer *p, int rational,
460 struct isl_print_space_data *data)
462 if (rational && !data->latex)
463 p = isl_printer_print_str(p, "rat: ");
464 if (isl_space_is_params(space))
466 else if (isl_space_is_set(space))
467 p = print_tuple(space, p, isl_dim_set, data);
468 else {
469 p = print_tuple(space, p, isl_dim_in, data);
470 p = isl_printer_print_str(p, s_to[data->latex]);
471 p = print_tuple(space, p, isl_dim_out, data);
474 return p;
477 static __isl_give isl_printer *print_omega_parameters(
478 __isl_keep isl_space *space, __isl_take isl_printer *p)
480 isl_size nparam = isl_space_dim(space, isl_dim_param);
482 if (nparam < 0)
483 return isl_printer_free(p);
484 if (nparam == 0)
485 return p;
487 p = isl_printer_start_line(p);
488 p = isl_printer_print_str(p, "symbolic ");
489 p = print_var_list(p, space, isl_dim_param);
490 p = isl_printer_print_str(p, ";");
491 p = isl_printer_end_line(p);
492 return p;
495 /* Does the inequality constraint following "i" in "bmap"
496 * have an opposite value for the same last coefficient?
497 * "last" is the position of the last coefficient of inequality "i".
498 * If the next constraint is a div constraint, then it is ignored
499 * since div constraints are not printed.
501 static isl_bool next_is_opposite(__isl_keep isl_basic_map *bmap, int i,
502 int last)
504 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
505 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
507 if (total < 0)
508 return isl_bool_error;
509 if (i + 1 >= bmap->n_ineq)
510 return isl_bool_false;
511 if (isl_seq_last_non_zero(bmap->ineq[i + 1], 1 + total) != last)
512 return isl_bool_false;
513 if (last >= o_div) {
514 isl_bool is_div;
515 is_div = isl_basic_map_is_div_constraint(bmap,
516 bmap->ineq[i + 1], last - o_div);
517 if (is_div < 0)
518 return isl_bool_error;
519 if (is_div)
520 return isl_bool_false;
522 return isl_int_abs_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]) &&
523 !isl_int_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]);
526 /* Return a string representation of the operator used when
527 * printing a constraint where the LHS is greater than or equal to the LHS
528 * (sign > 0) or smaller than or equal to the LHS (sign < 0).
529 * If "strict" is set, then return the strict version of the comparison
530 * operator.
532 static const char *constraint_op(int sign, int strict, int latex)
534 if (strict)
535 return sign < 0 ? "<" : ">";
536 if (sign < 0)
537 return s_le[latex];
538 else
539 return s_ge[latex];
542 /* Print one side of a constraint "c" to "p", with
543 * the variable names taken from "space" and the integer division definitions
544 * taken from "div".
545 * "last" is the position of the last non-zero coefficient.
546 * Let c' be the result of zeroing out this coefficient, then
547 * the partial constraint
549 * c' op
551 * is printed.
553 static __isl_give isl_printer *print_half_constraint(__isl_take isl_printer *p,
554 __isl_keep isl_space *space, __isl_keep isl_mat *div,
555 isl_int *c, int last, const char *op, int latex)
557 isl_int_set_si(c[last], 0);
558 p = print_affine(p, space, div, c);
560 p = isl_printer_print_str(p, " ");
561 p = isl_printer_print_str(p, op);
562 p = isl_printer_print_str(p, " ");
564 return p;
567 /* Print a constraint "c" to "p", with the variable names
568 * taken from "space" and the integer division definitions taken from "div".
569 * "last" is the position of the last non-zero coefficient, which is
570 * moreover assumed to be negative.
571 * Let c' be the result of zeroing out this coefficient, then
572 * the constraint is printed in the form
574 * -c[last] op c'
576 static __isl_give isl_printer *print_constraint(__isl_take isl_printer *p,
577 __isl_keep isl_space *space, __isl_keep isl_mat *div,
578 isl_int *c, int last, const char *op, int latex)
580 isl_int_abs(c[last], c[last]);
582 p = print_term(space, div, c[last], last, p, latex);
584 p = isl_printer_print_str(p, " ");
585 p = isl_printer_print_str(p, op);
586 p = isl_printer_print_str(p, " ");
588 isl_int_set_si(c[last], 0);
589 p = print_affine(p, space, div, c);
591 return p;
594 /* Given an integer division
596 * floor(f/m)
598 * at position "pos" in "div", print the corresponding modulo expression
600 * (f) mod m
602 * to "p". The variable names are taken from "space", while any
603 * nested integer division definitions are taken from "div".
605 static __isl_give isl_printer *print_mod(__isl_take isl_printer *p,
606 __isl_keep isl_space *space, __isl_keep isl_mat *div, int pos,
607 int latex)
609 if (!p || !div)
610 return isl_printer_free(p);
612 p = isl_printer_print_str(p, "(");
613 p = print_affine_of_len(space, div, p,
614 div->row[pos] + 1, div->n_col - 1);
615 p = isl_printer_print_str(p, ") ");
616 p = isl_printer_print_str(p, s_mod[latex]);
617 p = isl_printer_print_str(p, " ");
618 p = isl_printer_print_isl_int(p, div->row[pos][0]);
619 return p;
622 /* Can the equality constraints "c" be printed as a modulo constraint?
623 * In particular, is of the form
625 * f - a m floor(g/m) = 0,
627 * with c = -a m the coefficient at position "pos"?
628 * Return the position of the corresponding integer division if so.
629 * Return the number of integer divisions if not.
630 * Return -1 on error.
632 * Modulo constraints are currently not printed in C format.
633 * Other than that, "pos" needs to correspond to an integer division
634 * with explicit representation and "c" needs to be a multiple
635 * of the denominator of the integer division.
637 static isl_size print_as_modulo_pos(__isl_keep isl_printer *p,
638 __isl_keep isl_space *space, __isl_keep isl_mat *div, unsigned pos,
639 isl_int c)
641 isl_bool can_print;
642 isl_size n_div;
643 enum isl_dim_type type;
645 n_div = isl_mat_rows(div);
646 if (!p || !space || n_div < 0)
647 return isl_size_error;
648 if (p->output_format == ISL_FORMAT_C)
649 return n_div;
650 if (pos2type(space, &type, &pos) < 0)
651 return -1;
652 if (type != isl_dim_div)
653 return n_div;
654 can_print = can_print_div_expr(p, div, pos);
655 if (can_print < 0)
656 return isl_size_error;
657 if (!can_print)
658 return n_div;
659 if (!isl_int_is_divisible_by(c, div->row[pos][0]))
660 return n_div;
661 return pos;
664 /* Print equality constraint "c" to "p" as a modulo constraint,
665 * with the variable names taken from "space" and
666 * the integer division definitions taken from "div".
667 * "last" is the position of the last non-zero coefficient, which is
668 * moreover assumed to be negative and a multiple of the denominator
669 * of the corresponding integer division. "div_pos" is the corresponding
670 * position in the sequence of integer divisions.
672 * The equality is of the form
674 * f - a m floor(g/m) = 0.
676 * Print it as
678 * a (g mod m) = -f + a g
680 static __isl_give isl_printer *print_eq_mod_constraint(
681 __isl_take isl_printer *p, __isl_keep isl_space *space,
682 __isl_keep isl_mat *div, unsigned div_pos,
683 isl_int *c, int last, int latex)
685 isl_ctx *ctx;
686 int multiple;
688 ctx = isl_printer_get_ctx(p);
689 isl_int_divexact(c[last], c[last], div->row[div_pos][0]);
690 isl_int_abs(c[last], c[last]);
691 multiple = !isl_int_is_one(c[last]);
692 if (multiple) {
693 p = isl_printer_print_isl_int(p, c[last]);
694 p = isl_printer_print_str(p, "*(");
696 p = print_mod(p, space, div, div_pos, latex);
697 if (multiple)
698 p = isl_printer_print_str(p, ")");
699 p = isl_printer_print_str(p, " = ");
700 isl_seq_combine(c, ctx->negone, c,
701 c[last], div->row[div_pos] + 1, last);
702 isl_int_set_si(c[last], 0);
703 p = print_affine(p, space, div, c);
704 return p;
707 /* Print equality constraint "c" to "p", with the variable names
708 * taken from "space" and the integer division definitions taken from "div".
709 * "last" is the position of the last non-zero coefficient, which is
710 * moreover assumed to be negative.
712 * If possible, print the equality constraint as a modulo constraint.
714 static __isl_give isl_printer *print_eq_constraint(__isl_take isl_printer *p,
715 __isl_keep isl_space *space, __isl_keep isl_mat *div, isl_int *c,
716 int last, int latex)
718 isl_size n_div;
719 isl_size div_pos;
721 n_div = isl_mat_rows(div);
722 div_pos = print_as_modulo_pos(p, space, div, last, c[last]);
723 if (n_div < 0 || div_pos < 0)
724 return isl_printer_free(p);
725 if (div_pos < n_div)
726 return print_eq_mod_constraint(p, space, div, div_pos,
727 c, last, latex);
728 return print_constraint(p, space, div, c, last, "=", latex);
731 /* Print the constraints of "bmap" to "p".
732 * The names of the variables are taken from "space" and
733 * the integer division definitions are taken from "div".
734 * Div constraints are only printed in "dump" mode.
735 * The constraints are sorted prior to printing (except in "dump" mode).
737 * If x is the last variable with a non-zero coefficient,
738 * then a lower bound
740 * f - a x >= 0
742 * is printed as
744 * a x <= f
746 * while an upper bound
748 * f + a x >= 0
750 * is printed as
752 * a x >= -f
754 * If the next constraint has an opposite sign for the same last coefficient,
755 * then it is printed as
757 * f >= a x
759 * or
761 * -f <= a x
763 * instead. In fact, the "a x" part is not printed explicitly, but
764 * reused from the next constraint, which is therefore treated as
765 * a first constraint in the conjunction.
767 * If the constant term of "f" is -1, then "f" is replaced by "f + 1" and
768 * the comparison operator is replaced by the strict variant.
769 * Essentially, ">= 1" is replaced by "> 0".
771 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
772 __isl_keep isl_space *space, __isl_keep isl_mat *div,
773 __isl_take isl_printer *p, int latex)
775 int i;
776 isl_vec *c = NULL;
777 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
778 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
779 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
780 int first = 1;
781 int dump;
783 if (total < 0 || !p)
784 return isl_printer_free(p);
785 bmap = isl_basic_map_copy(bmap);
786 dump = p->dump;
787 if (!dump)
788 bmap = isl_basic_map_sort_constraints(bmap);
789 if (!bmap)
790 goto error;
792 c = isl_vec_alloc(bmap->ctx, 1 + total);
793 if (!c)
794 goto error;
796 for (i = bmap->n_eq - 1; i >= 0; --i) {
797 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
798 if (l < 0) {
799 if (i != bmap->n_eq - 1)
800 p = isl_printer_print_str(p, s_and[latex]);
801 p = isl_printer_print_str(p, "0 = 0");
802 continue;
804 if (!first)
805 p = isl_printer_print_str(p, s_and[latex]);
806 if (isl_int_is_neg(bmap->eq[i][l]))
807 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
808 else
809 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
810 p = print_eq_constraint(p, space, div, c->el, l, latex);
811 first = 0;
813 for (i = 0; i < bmap->n_ineq; ++i) {
814 isl_bool combine;
815 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
816 int strict;
817 int s;
818 const char *op;
819 if (l < 0)
820 continue;
821 if (!dump && l >= o_div &&
822 can_print_div_expr(p, div, l - o_div)) {
823 isl_bool is_div;
824 is_div = isl_basic_map_is_div_constraint(bmap,
825 bmap->ineq[i], l - o_div);
826 if (is_div < 0)
827 goto error;
828 if (is_div)
829 continue;
831 if (!first)
832 p = isl_printer_print_str(p, s_and[latex]);
833 s = isl_int_sgn(bmap->ineq[i][l]);
834 strict = !rational && isl_int_is_negone(bmap->ineq[i][0]);
835 if (s < 0)
836 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
837 else
838 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
839 if (strict)
840 isl_int_set_si(c->el[0], 0);
841 combine = dump ? isl_bool_false : next_is_opposite(bmap, i, l);
842 if (combine < 0)
843 goto error;
844 if (combine) {
845 op = constraint_op(-s, strict, latex);
846 p = print_half_constraint(p, space, div, c->el, l,
847 op, latex);
848 first = 1;
849 } else {
850 op = constraint_op(s, strict, latex);
851 p = print_constraint(p, space, div, c->el, l,
852 op, latex);
853 first = 0;
857 isl_basic_map_free(bmap);
858 isl_vec_free(c);
860 return p;
861 error:
862 isl_basic_map_free(bmap);
863 isl_vec_free(c);
864 isl_printer_free(p);
865 return NULL;
868 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
869 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
871 int c;
873 if (!p || !div)
874 return isl_printer_free(p);
876 c = p->output_format == ISL_FORMAT_C;
877 p = isl_printer_print_str(p, c ? "floord(" : "floor((");
878 p = print_affine_of_len(dim, div, p,
879 div->row[pos] + 1, div->n_col - 1);
880 p = isl_printer_print_str(p, c ? ", " : ")/");
881 p = isl_printer_print_isl_int(p, div->row[pos][0]);
882 p = isl_printer_print_str(p, ")");
883 return p;
886 /* Print a comma separated list of div names, except those that have
887 * a definition that can be printed.
888 * If "print_defined_divs" is set, then those div names are printed
889 * as well, along with their definitions.
891 static __isl_give isl_printer *print_div_list(__isl_take isl_printer *p,
892 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex,
893 int print_defined_divs)
895 int i;
896 int first = 1;
897 isl_size n_div;
899 n_div = isl_mat_rows(div);
900 if (!p || !space || n_div < 0)
901 return isl_printer_free(p);
903 for (i = 0; i < n_div; ++i) {
904 if (!print_defined_divs && can_print_div_expr(p, div, i))
905 continue;
906 if (!first)
907 p = isl_printer_print_str(p, ", ");
908 p = print_name(space, p, isl_dim_div, i, latex);
909 first = 0;
910 if (!can_print_div_expr(p, div, i))
911 continue;
912 p = isl_printer_print_str(p, " = ");
913 p = print_div(space, div, i, p);
916 return p;
919 /* Does printing an object with local variables described by "div"
920 * require an "exists" clause?
921 * That is, are there any local variables without an explicit representation?
922 * An exists clause is also needed in "dump" mode because
923 * explicit div representations are not printed inline in that case.
925 static isl_bool need_exists(__isl_keep isl_printer *p, __isl_keep isl_mat *div)
927 int i;
928 isl_size n;
930 n = isl_mat_rows(div);
931 if (!p || n < 0)
932 return isl_bool_error;
933 if (n == 0)
934 return isl_bool_false;
935 if (p->dump)
936 return isl_bool_true;
937 for (i = 0; i < n; ++i)
938 if (!can_print_div_expr(p, div, i))
939 return isl_bool_true;
940 return isl_bool_false;
943 /* Print the start of an exists clause, i.e.,
945 * (exists variables:
947 * In dump mode, local variables with an explicit definition are printed
948 * as well because they will not be printed inline.
950 static __isl_give isl_printer *open_exists(__isl_take isl_printer *p,
951 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex)
953 int dump;
955 if (!p)
956 return NULL;
958 dump = p->dump;
959 p = isl_printer_print_str(p, s_open_exists[latex]);
960 p = print_div_list(p, space, div, latex, dump);
961 p = isl_printer_print_str(p, ": ");
963 return p;
966 /* Remove the explicit representations of all local variables in "div".
968 static __isl_give isl_mat *mark_all_unknown(__isl_take isl_mat *div)
970 int i;
971 isl_size n_div;
973 n_div = isl_mat_rows(div);
974 if (n_div < 0)
975 return isl_mat_free(div);
977 for (i = 0; i < n_div; ++i)
978 div = isl_mat_set_element_si(div, i, 0, 0);
979 return div;
982 /* Print the constraints of "bmap" to "p".
983 * The names of the variables are taken from "space".
984 * "latex" is set if the constraints should be printed in LaTeX format.
985 * Do not print inline explicit div representations in "dump" mode.
987 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
988 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
990 int dump;
991 isl_mat *div;
992 isl_bool exists;
994 if (!p)
995 return NULL;
996 dump = p->dump;
997 div = isl_basic_map_get_divs(bmap);
998 exists = need_exists(p, div);
999 if (exists >= 0 && exists)
1000 p = open_exists(p, space, div, latex);
1002 if (dump)
1003 div = mark_all_unknown(div);
1004 p = print_constraints(bmap, space, div, p, latex);
1005 isl_mat_free(div);
1007 if (exists >= 0 && exists)
1008 p = isl_printer_print_str(p, s_close_exists[latex]);
1009 return p;
1012 /* Print a colon followed by the constraints of "bmap"
1013 * to "p", provided there are any constraints.
1014 * The names of the variables are taken from "space".
1015 * "latex" is set if the constraints should be printed in LaTeX format.
1017 static __isl_give isl_printer *print_optional_disjunct(
1018 __isl_keep isl_basic_map *bmap, __isl_keep isl_space *space,
1019 __isl_take isl_printer *p, int latex)
1021 if (isl_basic_map_plain_is_universe(bmap))
1022 return p;
1024 p = isl_printer_print_str(p, ": ");
1025 p = print_disjunct(bmap, space, p, latex);
1027 return p;
1030 static __isl_give isl_printer *basic_map_print_omega(
1031 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
1033 p = isl_printer_print_str(p, "{ [");
1034 p = print_var_list(p, bmap->dim, isl_dim_in);
1035 p = isl_printer_print_str(p, "] -> [");
1036 p = print_var_list(p, bmap->dim, isl_dim_out);
1037 p = isl_printer_print_str(p, "] ");
1038 p = print_optional_disjunct(bmap, bmap->dim, p, 0);
1039 p = isl_printer_print_str(p, " }");
1040 return p;
1043 static __isl_give isl_printer *basic_set_print_omega(
1044 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
1046 p = isl_printer_print_str(p, "{ [");
1047 p = print_var_list(p, bset->dim, isl_dim_set);
1048 p = isl_printer_print_str(p, "] ");
1049 p = print_optional_disjunct(bset, bset->dim, p, 0);
1050 p = isl_printer_print_str(p, " }");
1051 return p;
1054 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
1055 __isl_take isl_printer *p)
1057 int i;
1059 for (i = 0; i < map->n; ++i) {
1060 if (i)
1061 p = isl_printer_print_str(p, " union ");
1062 p = basic_map_print_omega(map->p[i], p);
1064 return p;
1067 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
1068 __isl_take isl_printer *p)
1070 int i;
1072 for (i = 0; i < set->n; ++i) {
1073 if (i)
1074 p = isl_printer_print_str(p, " union ");
1075 p = basic_set_print_omega(set->p[i], p);
1077 return p;
1080 /* Print the list of parameters in "space", followed by an arrow, to "p",
1081 * if there are any parameters.
1083 static __isl_give isl_printer *print_param_tuple(__isl_take isl_printer *p,
1084 __isl_keep isl_space *space, struct isl_print_space_data *data)
1086 isl_size nparam;
1088 nparam = isl_space_dim(space, isl_dim_param);
1089 if (!p || nparam < 0)
1090 return isl_printer_free(p);
1091 if (nparam == 0)
1092 return p;
1094 p = print_tuple(space, p, isl_dim_param, data);
1095 p = isl_printer_print_str(p, s_to[data->latex]);
1097 return p;
1100 static __isl_give isl_printer *isl_basic_map_print_isl(
1101 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
1102 int latex)
1104 struct isl_print_space_data data = { .latex = latex };
1105 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1107 p = print_param_tuple(p, bmap->dim, &data);
1108 p = isl_printer_print_str(p, "{ ");
1109 p = isl_print_space(bmap->dim, p, rational, &data);
1110 p = isl_printer_print_str(p, " : ");
1111 p = print_disjunct(bmap, bmap->dim, p, latex);
1112 p = isl_printer_print_str(p, " }");
1113 return p;
1116 /* Print the disjuncts of a map (or set) "map" to "p".
1117 * The names of the variables are taken from "space".
1118 * "latex" is set if the constraints should be printed in LaTeX format.
1120 static __isl_give isl_printer *print_disjuncts_core(__isl_keep isl_map *map,
1121 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1123 int i;
1125 if (map->n == 0)
1126 p = isl_printer_print_str(p, "false");
1127 for (i = 0; i < map->n; ++i) {
1128 if (i)
1129 p = isl_printer_print_str(p, s_or[latex]);
1130 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
1131 p = isl_printer_print_str(p, "(");
1132 p = print_disjunct(map->p[i], space, p, latex);
1133 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
1134 p = isl_printer_print_str(p, ")");
1136 return p;
1139 /* Print the disjuncts of a map (or set) "map" to "p".
1140 * The names of the variables are taken from "space".
1141 * "hull" describes constraints shared by all disjuncts of "map".
1142 * "latex" is set if the constraints should be printed in LaTeX format.
1144 * Print the disjuncts as a conjunction of "hull" and
1145 * the result of removing the constraints of "hull" from "map".
1146 * If this result turns out to be the universe, then simply print "hull".
1148 static __isl_give isl_printer *print_disjuncts_in_hull(__isl_keep isl_map *map,
1149 __isl_keep isl_space *space, __isl_take isl_basic_map *hull,
1150 __isl_take isl_printer *p, int latex)
1152 isl_bool is_universe;
1154 p = print_disjunct(hull, space, p, latex);
1155 map = isl_map_plain_gist_basic_map(isl_map_copy(map), hull);
1156 is_universe = isl_map_plain_is_universe(map);
1157 if (is_universe < 0)
1158 goto error;
1159 if (!is_universe) {
1160 p = isl_printer_print_str(p, s_and[latex]);
1161 p = isl_printer_print_str(p, "(");
1162 p = print_disjuncts_core(map, space, p, latex);
1163 p = isl_printer_print_str(p, ")");
1165 isl_map_free(map);
1167 return p;
1168 error:
1169 isl_map_free(map);
1170 isl_printer_free(p);
1171 return NULL;
1174 /* Print the disjuncts of a map (or set) "map" to "p".
1175 * The names of the variables are taken from "space".
1176 * "latex" is set if the constraints should be printed in LaTeX format.
1178 * If there are at least two disjuncts and "dump" mode is not turned out,
1179 * check for any shared constraints among all disjuncts.
1180 * If there are any, then print them separately in print_disjuncts_in_hull.
1182 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
1183 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1185 if (isl_map_plain_is_universe(map))
1186 return p;
1188 p = isl_printer_print_str(p, s_such_that[latex]);
1189 if (!p)
1190 return NULL;
1192 if (!p->dump && map->n >= 2) {
1193 isl_basic_map *hull;
1194 isl_bool is_universe;
1196 hull = isl_map_plain_unshifted_simple_hull(isl_map_copy(map));
1197 is_universe = isl_basic_map_plain_is_universe(hull);
1198 if (is_universe < 0)
1199 p = isl_printer_free(p);
1200 else if (!is_universe)
1201 return print_disjuncts_in_hull(map, space, hull,
1202 p, latex);
1203 isl_basic_map_free(hull);
1206 return print_disjuncts_core(map, space, p, latex);
1209 /* Print the disjuncts of a map (or set).
1210 * The names of the variables are taken from "space".
1211 * "latex" is set if the constraints should be printed in LaTeX format.
1213 * If the map turns out to be a universal parameter domain, then
1214 * we need to print the colon. Otherwise, the output looks identical
1215 * to the empty set.
1217 static __isl_give isl_printer *print_disjuncts_map(__isl_keep isl_map *map,
1218 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1220 if (isl_map_plain_is_universe(map) && isl_space_is_params(map->dim))
1221 return isl_printer_print_str(p, s_such_that[latex]);
1222 else
1223 return print_disjuncts(map, space, p, latex);
1226 /* Print the disjuncts of a set.
1227 * The names of the variables are taken from "space".
1228 * "latex" is set if the constraints should be printed in LaTeX format.
1230 static __isl_give isl_printer *print_disjuncts_set(__isl_keep isl_set *set,
1231 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1233 return print_disjuncts_map(set_to_map(set), space, p, latex);
1236 struct isl_aff_split {
1237 isl_basic_map *aff;
1238 isl_map *map;
1241 static void free_split(__isl_take struct isl_aff_split *split, int n)
1243 int i;
1245 if (!split)
1246 return;
1248 for (i = 0; i < n; ++i) {
1249 isl_basic_map_free(split[i].aff);
1250 isl_map_free(split[i].map);
1253 free(split);
1256 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
1258 int i, j;
1259 isl_size nparam, n_in, n_out, total;
1261 bmap = isl_basic_map_cow(bmap);
1262 if (!bmap)
1263 return NULL;
1264 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
1265 goto error;
1267 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1268 n_in = isl_basic_map_dim(bmap, isl_dim_in);
1269 n_out = isl_basic_map_dim(bmap, isl_dim_out);
1270 total = isl_basic_map_dim(bmap, isl_dim_all);
1271 if (n_in < 0 || n_out < 0 || nparam < 0 || total < 0)
1272 return isl_basic_map_free(bmap);
1274 for (i = bmap->n_eq - 1; i >= 0; --i) {
1275 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
1276 if (j >= nparam && j < nparam + n_in + n_out &&
1277 (isl_int_is_one(bmap->eq[i][1 + j]) ||
1278 isl_int_is_negone(bmap->eq[i][1 + j])))
1279 continue;
1280 if (isl_basic_map_drop_equality(bmap, i) < 0)
1281 goto error;
1284 bmap = isl_basic_map_finalize(bmap);
1286 return bmap;
1287 error:
1288 isl_basic_map_free(bmap);
1289 return NULL;
1292 static int aff_split_cmp(const void *p1, const void *p2, void *user)
1294 const struct isl_aff_split *s1, *s2;
1295 s1 = (const struct isl_aff_split *) p1;
1296 s2 = (const struct isl_aff_split *) p2;
1298 return isl_basic_map_plain_cmp(s1->aff, s2->aff);
1301 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
1302 __isl_keep isl_basic_map *aff)
1304 int i, j;
1305 isl_size v_div;
1307 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1308 if (v_div < 0 || !aff)
1309 goto error;
1311 for (i = bmap->n_eq - 1; i >= 0; --i) {
1312 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + v_div,
1313 bmap->n_div) != -1)
1314 continue;
1315 for (j = 0; j < aff->n_eq; ++j) {
1316 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + v_div) &&
1317 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + v_div))
1318 continue;
1319 if (isl_basic_map_drop_equality(bmap, i) < 0)
1320 goto error;
1321 break;
1325 return bmap;
1326 error:
1327 isl_basic_map_free(bmap);
1328 return NULL;
1331 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
1333 int i, n;
1334 struct isl_aff_split *split;
1335 isl_ctx *ctx;
1337 ctx = isl_map_get_ctx(map);
1338 split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
1339 if (!split)
1340 return NULL;
1342 for (i = 0; i < map->n; ++i) {
1343 isl_basic_map *bmap;
1344 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
1345 bmap = isl_basic_map_copy(map->p[i]);
1346 bmap = isl_basic_map_cow(bmap);
1347 bmap = drop_aff(bmap, split[i].aff);
1348 split[i].map = isl_map_from_basic_map(bmap);
1349 if (!split[i].aff || !split[i].map)
1350 goto error;
1353 if (isl_sort(split, map->n, sizeof(struct isl_aff_split),
1354 &aff_split_cmp, NULL) < 0)
1355 goto error;
1357 n = map->n;
1358 for (i = n - 1; i >= 1; --i) {
1359 if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
1360 split[i].aff))
1361 continue;
1362 isl_basic_map_free(split[i].aff);
1363 split[i - 1].map = isl_map_union(split[i - 1].map,
1364 split[i].map);
1365 if (i != n - 1)
1366 split[i] = split[n - 1];
1367 split[n - 1].aff = NULL;
1368 split[n - 1].map = NULL;
1369 --n;
1372 return split;
1373 error:
1374 free_split(split, map->n);
1375 return NULL;
1378 static int defining_equality(__isl_keep isl_basic_map *eq,
1379 __isl_keep isl_space *dim, enum isl_dim_type type, int pos)
1381 int i;
1382 isl_size total;
1384 total = isl_basic_map_dim(eq, isl_dim_all);
1385 if (total < 0)
1386 return -1;
1388 pos += isl_space_offset(dim, type);
1390 for (i = 0; i < eq->n_eq; ++i) {
1391 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
1392 continue;
1393 if (isl_int_is_one(eq->eq[i][1 + pos]))
1394 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
1395 return i;
1398 return -1;
1401 /* Print dimension "pos" of data->space to "p".
1403 * data->user is assumed to be an isl_basic_map keeping track of equalities.
1405 * If the current dimension is defined by these equalities, then print
1406 * the corresponding expression, assigned to the name of the dimension
1407 * if there is any. Otherwise, print the name of the dimension.
1409 static __isl_give isl_printer *print_dim_eq(__isl_take isl_printer *p,
1410 struct isl_print_space_data *data, unsigned pos)
1412 isl_basic_map *eq = data->user;
1413 int j;
1415 j = defining_equality(eq, data->space, data->type, pos);
1416 if (j >= 0) {
1417 if (isl_space_has_dim_name(data->space, data->type, pos)) {
1418 p = print_name(data->space, p, data->type, pos,
1419 data->latex);
1420 p = isl_printer_print_str(p, " = ");
1422 pos += 1 + isl_space_offset(data->space, data->type);
1423 p = print_affine_of_len(data->space, NULL, p, eq->eq[j], pos);
1424 } else {
1425 p = print_name(data->space, p, data->type, pos, data->latex);
1428 return p;
1431 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
1432 struct isl_aff_split *split, int n, __isl_keep isl_space *space)
1434 struct isl_print_space_data data = { 0 };
1435 int i;
1436 int rational;
1438 data.print_dim = &print_dim_eq;
1439 for (i = 0; i < n; ++i) {
1440 if (!split[i].map)
1441 break;
1442 rational = split[i].map->n > 0 &&
1443 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
1444 if (i)
1445 p = isl_printer_print_str(p, "; ");
1446 data.user = split[i].aff;
1447 p = isl_print_space(space, p, rational, &data);
1448 p = print_disjuncts_map(split[i].map, space, p, 0);
1451 return p;
1454 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
1455 __isl_take isl_printer *p)
1457 struct isl_print_space_data data = { 0 };
1458 struct isl_aff_split *split = NULL;
1459 int rational;
1461 if (!p || !map)
1462 return isl_printer_free(p);
1463 if (!p->dump && map->n > 0)
1464 split = split_aff(map);
1465 if (split) {
1466 p = print_split_map(p, split, map->n, map->dim);
1467 } else {
1468 rational = map->n > 0 &&
1469 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
1470 p = isl_print_space(map->dim, p, rational, &data);
1471 p = print_disjuncts_map(map, map->dim, p, 0);
1473 free_split(split, map->n);
1474 return p;
1477 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
1478 __isl_take isl_printer *p)
1480 struct isl_print_space_data data = { 0 };
1482 p = print_param_tuple(p, map->dim, &data);
1483 p = isl_printer_print_str(p, s_open_set[0]);
1484 p = isl_map_print_isl_body(map, p);
1485 p = isl_printer_print_str(p, s_close_set[0]);
1486 return p;
1489 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
1490 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff)
1492 struct isl_print_space_data data = { 0 };
1494 data.latex = 1;
1495 p = print_param_tuple(p, map->dim, &data);
1496 p = isl_printer_print_str(p, s_open_set[1]);
1497 data.print_dim = &print_dim_eq;
1498 data.user = aff;
1499 p = isl_print_space(map->dim, p, 0, &data);
1500 p = print_disjuncts_map(map, map->dim, p, 1);
1501 p = isl_printer_print_str(p, s_close_set[1]);
1503 return p;
1506 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
1507 __isl_take isl_printer *p)
1509 int i;
1510 struct isl_aff_split *split = NULL;
1512 if (map->n > 0)
1513 split = split_aff(map);
1515 if (!split)
1516 return print_latex_map(map, p, NULL);
1518 for (i = 0; i < map->n; ++i) {
1519 if (!split[i].map)
1520 break;
1521 if (i)
1522 p = isl_printer_print_str(p, " \\cup ");
1523 p = print_latex_map(split[i].map, p, split[i].aff);
1526 free_split(split, map->n);
1527 return p;
1530 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
1531 __isl_keep isl_basic_map *bmap)
1533 if (!p || !bmap)
1534 goto error;
1535 if (p->output_format == ISL_FORMAT_ISL)
1536 return isl_basic_map_print_isl(bmap, p, 0);
1537 else if (p->output_format == ISL_FORMAT_OMEGA)
1538 return basic_map_print_omega(bmap, p);
1539 isl_assert(bmap->ctx, 0, goto error);
1540 error:
1541 isl_printer_free(p);
1542 return NULL;
1545 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
1546 __isl_keep isl_basic_set *bset)
1548 if (!p || !bset)
1549 goto error;
1551 if (p->output_format == ISL_FORMAT_ISL)
1552 return isl_basic_map_print_isl(bset, p, 0);
1553 else if (p->output_format == ISL_FORMAT_POLYLIB)
1554 return isl_basic_set_print_polylib(bset, p, 0);
1555 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1556 return isl_basic_set_print_polylib(bset, p, 1);
1557 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
1558 return bset_print_constraints_polylib(bset, p);
1559 else if (p->output_format == ISL_FORMAT_OMEGA)
1560 return basic_set_print_omega(bset, p);
1561 isl_assert(p->ctx, 0, goto error);
1562 error:
1563 isl_printer_free(p);
1564 return NULL;
1567 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
1568 __isl_keep isl_set *set)
1570 if (!p || !set)
1571 goto error;
1572 if (p->output_format == ISL_FORMAT_ISL)
1573 return isl_map_print_isl(set_to_map(set), p);
1574 else if (p->output_format == ISL_FORMAT_POLYLIB)
1575 return isl_set_print_polylib(set, p, 0);
1576 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1577 return isl_set_print_polylib(set, p, 1);
1578 else if (p->output_format == ISL_FORMAT_OMEGA)
1579 return isl_set_print_omega(set, p);
1580 else if (p->output_format == ISL_FORMAT_LATEX)
1581 return isl_map_print_latex(set_to_map(set), p);
1582 isl_assert(set->ctx, 0, goto error);
1583 error:
1584 isl_printer_free(p);
1585 return NULL;
1588 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1589 __isl_keep isl_map *map)
1591 if (!p || !map)
1592 goto error;
1594 if (p->output_format == ISL_FORMAT_ISL)
1595 return isl_map_print_isl(map, p);
1596 else if (p->output_format == ISL_FORMAT_POLYLIB)
1597 return isl_map_print_polylib(map, p, 0);
1598 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1599 return isl_map_print_polylib(map, p, 1);
1600 else if (p->output_format == ISL_FORMAT_OMEGA)
1601 return isl_map_print_omega(map, p);
1602 else if (p->output_format == ISL_FORMAT_LATEX)
1603 return isl_map_print_latex(map, p);
1604 isl_assert(map->ctx, 0, goto error);
1605 error:
1606 isl_printer_free(p);
1607 return NULL;
1610 struct isl_union_print_data {
1611 isl_printer *p;
1612 int first;
1615 static isl_stat print_map_body(__isl_take isl_map *map, void *user)
1617 struct isl_union_print_data *data;
1618 data = (struct isl_union_print_data *)user;
1620 if (!data->first)
1621 data->p = isl_printer_print_str(data->p, "; ");
1622 data->first = 0;
1624 data->p = isl_map_print_isl_body(map, data->p);
1625 isl_map_free(map);
1627 return isl_stat_ok;
1630 /* Print the body of "umap" (everything except the parameter declarations)
1631 * to "p" in isl format.
1633 static __isl_give isl_printer *isl_printer_print_union_map_isl_body(
1634 __isl_take isl_printer *p, __isl_keep isl_union_map *umap)
1636 struct isl_union_print_data data;
1638 p = isl_printer_print_str(p, s_open_set[0]);
1639 data.p = p;
1640 data.first = 1;
1641 isl_union_map_foreach_map(umap, &print_map_body, &data);
1642 p = data.p;
1643 p = isl_printer_print_str(p, s_close_set[0]);
1644 return p;
1647 /* Print the body of "uset" (everything except the parameter declarations)
1648 * to "p" in isl format.
1650 static __isl_give isl_printer *isl_printer_print_union_set_isl_body(
1651 __isl_take isl_printer *p, __isl_keep isl_union_set *uset)
1653 return isl_printer_print_union_map_isl_body(p, uset_to_umap(uset));
1656 /* Print the isl_union_map "umap" to "p" in isl format.
1658 static __isl_give isl_printer *isl_union_map_print_isl(
1659 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1661 struct isl_print_space_data space_data = { 0 };
1662 isl_space *space;
1664 space = isl_union_map_get_space(umap);
1665 p = print_param_tuple(p, space, &space_data);
1666 isl_space_free(space);
1668 p = isl_printer_print_union_map_isl_body(p, umap);
1670 return p;
1673 static isl_stat print_latex_map_body(__isl_take isl_map *map, void *user)
1675 struct isl_union_print_data *data;
1676 data = (struct isl_union_print_data *)user;
1678 if (!data->first)
1679 data->p = isl_printer_print_str(data->p, " \\cup ");
1680 data->first = 0;
1682 data->p = isl_map_print_latex(map, data->p);
1683 isl_map_free(map);
1685 return isl_stat_ok;
1688 static __isl_give isl_printer *isl_union_map_print_latex(
1689 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1691 struct isl_union_print_data data = { p, 1 };
1692 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1693 p = data.p;
1694 return p;
1697 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1698 __isl_keep isl_union_map *umap)
1700 if (!p || !umap)
1701 goto error;
1703 if (p->output_format == ISL_FORMAT_ISL)
1704 return isl_union_map_print_isl(umap, p);
1705 if (p->output_format == ISL_FORMAT_LATEX)
1706 return isl_union_map_print_latex(umap, p);
1708 isl_die(p->ctx, isl_error_invalid,
1709 "invalid output format for isl_union_map", goto error);
1710 error:
1711 isl_printer_free(p);
1712 return NULL;
1715 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1716 __isl_keep isl_union_set *uset)
1718 if (!p || !uset)
1719 goto error;
1721 if (p->output_format == ISL_FORMAT_ISL)
1722 return isl_union_map_print_isl(uset_to_umap(uset), p);
1723 if (p->output_format == ISL_FORMAT_LATEX)
1724 return isl_union_map_print_latex(uset_to_umap(uset), p);
1726 isl_die(p->ctx, isl_error_invalid,
1727 "invalid output format for isl_union_set", goto error);
1728 error:
1729 isl_printer_free(p);
1730 return NULL;
1733 static isl_size poly_rec_n_non_zero(__isl_keep isl_poly_rec *rec)
1735 int i;
1736 int n;
1738 if (!rec)
1739 return isl_size_error;
1741 for (i = 0, n = 0; i < rec->n; ++i) {
1742 isl_bool is_zero = isl_poly_is_zero(rec->p[i]);
1744 if (is_zero < 0)
1745 return isl_size_error;
1746 if (!is_zero)
1747 ++n;
1750 return n;
1753 static __isl_give isl_printer *poly_print_cst(__isl_keep isl_poly *poly,
1754 __isl_take isl_printer *p, int first)
1756 isl_poly_cst *cst;
1757 int neg;
1759 cst = isl_poly_as_cst(poly);
1760 if (!cst)
1761 goto error;
1762 neg = !first && isl_int_is_neg(cst->n);
1763 if (!first)
1764 p = isl_printer_print_str(p, neg ? " - " : " + ");
1765 if (neg)
1766 isl_int_neg(cst->n, cst->n);
1767 if (isl_int_is_zero(cst->d)) {
1768 int sgn = isl_int_sgn(cst->n);
1769 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1770 sgn == 0 ? "NaN" : "infty");
1771 } else
1772 p = isl_printer_print_isl_int(p, cst->n);
1773 if (neg)
1774 isl_int_neg(cst->n, cst->n);
1775 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1776 p = isl_printer_print_str(p, "/");
1777 p = isl_printer_print_isl_int(p, cst->d);
1779 return p;
1780 error:
1781 isl_printer_free(p);
1782 return NULL;
1785 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1786 __isl_keep isl_space *space, __isl_keep isl_mat *div, int var)
1788 isl_size total;
1790 total = isl_space_dim(space, isl_dim_all);
1791 if (total < 0)
1792 return isl_printer_free(p);
1793 if (var < total)
1794 p = print_term(space, NULL, space->ctx->one, 1 + var, p, 0);
1795 else
1796 p = print_div(space, div, var - total, p);
1797 return p;
1800 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1801 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var, int exp)
1803 p = print_base(p, dim, div, var);
1804 if (exp == 1)
1805 return p;
1806 if (p->output_format == ISL_FORMAT_C) {
1807 int i;
1808 for (i = 1; i < exp; ++i) {
1809 p = isl_printer_print_str(p, "*");
1810 p = print_base(p, dim, div, var);
1812 } else {
1813 p = isl_printer_print_str(p, "^");
1814 p = isl_printer_print_int(p, exp);
1816 return p;
1819 /* Print the polynomial "poly" defined over the domain space "space" and
1820 * local variables defined by "div" to "p".
1822 static __isl_give isl_printer *poly_print(__isl_keep isl_poly *poly,
1823 __isl_keep isl_space *space, __isl_keep isl_mat *div,
1824 __isl_take isl_printer *p)
1826 int i, first, print_parens;
1827 isl_size n;
1828 isl_bool is_cst;
1829 isl_poly_rec *rec;
1831 is_cst = isl_poly_is_cst(poly);
1832 if (!p || is_cst < 0 || !space || !div)
1833 goto error;
1835 if (is_cst)
1836 return poly_print_cst(poly, p, 1);
1838 rec = isl_poly_as_rec(poly);
1839 n = poly_rec_n_non_zero(rec);
1840 if (n < 0)
1841 return isl_printer_free(p);
1842 print_parens = n > 1;
1843 if (print_parens)
1844 p = isl_printer_print_str(p, "(");
1845 for (i = 0, first = 1; i < rec->n; ++i) {
1846 isl_bool is_zero = isl_poly_is_zero(rec->p[i]);
1847 isl_bool is_one = isl_poly_is_one(rec->p[i]);
1848 isl_bool is_negone = isl_poly_is_negone(rec->p[i]);
1849 isl_bool is_cst = isl_poly_is_cst(rec->p[i]);
1851 if (is_zero < 0 || is_one < 0 || is_negone < 0)
1852 return isl_printer_free(p);
1853 if (is_zero)
1854 continue;
1855 if (is_negone) {
1856 if (!i)
1857 p = isl_printer_print_str(p, "-1");
1858 else if (first)
1859 p = isl_printer_print_str(p, "-");
1860 else
1861 p = isl_printer_print_str(p, " - ");
1862 } else if (is_cst && !is_one)
1863 p = poly_print_cst(rec->p[i], p, first);
1864 else {
1865 if (!first)
1866 p = isl_printer_print_str(p, " + ");
1867 if (i == 0 || !is_one)
1868 p = poly_print(rec->p[i], space, div, p);
1870 first = 0;
1871 if (i == 0)
1872 continue;
1873 if (!is_one && !is_negone)
1874 p = isl_printer_print_str(p, " * ");
1875 p = print_pow(p, space, div, rec->poly.var, i);
1877 if (print_parens)
1878 p = isl_printer_print_str(p, ")");
1879 return p;
1880 error:
1881 isl_printer_free(p);
1882 return NULL;
1885 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1886 __isl_keep isl_qpolynomial *qp)
1888 if (!p || !qp)
1889 goto error;
1890 p = poly_print(qp->poly, qp->dim, qp->div, p);
1891 return p;
1892 error:
1893 isl_printer_free(p);
1894 return NULL;
1897 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1898 __isl_keep isl_qpolynomial *qp)
1900 struct isl_print_space_data data = { 0 };
1902 if (!p || !qp)
1903 goto error;
1905 p = print_param_tuple(p, qp->dim, &data);
1906 p = isl_printer_print_str(p, "{ ");
1907 if (!isl_space_is_params(qp->dim)) {
1908 p = isl_print_space(qp->dim, p, 0, &data);
1909 p = isl_printer_print_str(p, " -> ");
1911 p = print_qpolynomial(p, qp);
1912 p = isl_printer_print_str(p, " }");
1913 return p;
1914 error:
1915 isl_printer_free(p);
1916 return NULL;
1919 /* Print the quasi-polynomial "qp" to "p" in C format, with the variable names
1920 * taken from the domain space "space".
1922 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1923 __isl_keep isl_space *space, __isl_keep isl_qpolynomial *qp)
1925 isl_bool is_one;
1926 isl_val *den;
1928 den = isl_qpolynomial_get_den(qp);
1929 qp = isl_qpolynomial_copy(qp);
1930 qp = isl_qpolynomial_scale_val(qp, isl_val_copy(den));
1931 is_one = isl_val_is_one(den);
1932 if (is_one < 0)
1933 p = isl_printer_free(p);
1934 if (!is_one)
1935 p = isl_printer_print_str(p, "(");
1936 if (qp)
1937 p = poly_print(qp->poly, space, qp->div, p);
1938 else
1939 p = isl_printer_free(p);
1940 if (!is_one) {
1941 p = isl_printer_print_str(p, ")/");
1942 p = isl_printer_print_val(p, den);
1944 isl_qpolynomial_free(qp);
1945 isl_val_free(den);
1946 return p;
1949 __isl_give isl_printer *isl_printer_print_qpolynomial(
1950 __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1952 if (!p || !qp)
1953 goto error;
1955 if (p->output_format == ISL_FORMAT_ISL)
1956 return print_qpolynomial_isl(p, qp);
1957 else if (p->output_format == ISL_FORMAT_C)
1958 return print_qpolynomial_c(p, qp->dim, qp);
1959 else
1960 isl_die(qp->dim->ctx, isl_error_unsupported,
1961 "output format not supported for isl_qpolynomials",
1962 goto error);
1963 error:
1964 isl_printer_free(p);
1965 return NULL;
1968 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1969 unsigned output_format)
1971 isl_printer *p;
1973 if (!qp)
1974 return;
1976 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1977 p = isl_printer_to_file(qp->dim->ctx, out);
1978 p = isl_printer_print_qpolynomial(p, qp);
1979 isl_printer_free(p);
1982 static __isl_give isl_printer *qpolynomial_fold_print(
1983 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1985 int i;
1987 if (fold->type == isl_fold_min)
1988 p = isl_printer_print_str(p, "min");
1989 else if (fold->type == isl_fold_max)
1990 p = isl_printer_print_str(p, "max");
1991 p = isl_printer_print_str(p, "(");
1992 for (i = 0; i < fold->n; ++i) {
1993 if (i)
1994 p = isl_printer_print_str(p, ", ");
1995 p = print_qpolynomial(p, fold->qp[i]);
1997 p = isl_printer_print_str(p, ")");
1998 return p;
2001 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
2002 FILE *out, unsigned output_format)
2004 isl_printer *p;
2006 if (!fold)
2007 return;
2009 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
2011 p = isl_printer_to_file(fold->dim->ctx, out);
2012 p = isl_printer_print_qpolynomial_fold(p, fold);
2014 isl_printer_free(p);
2017 static __isl_give isl_printer *isl_pwqp_print_isl_body(
2018 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2020 struct isl_print_space_data data = { 0 };
2021 int i = 0;
2023 for (i = 0; i < pwqp->n; ++i) {
2024 isl_space *space;
2026 if (i)
2027 p = isl_printer_print_str(p, "; ");
2028 space = isl_qpolynomial_get_domain_space(pwqp->p[i].qp);
2029 if (!isl_space_is_params(space)) {
2030 p = isl_print_space(space, p, 0, &data);
2031 p = isl_printer_print_str(p, " -> ");
2033 p = print_qpolynomial(p, pwqp->p[i].qp);
2034 p = print_disjuncts(set_to_map(pwqp->p[i].set), space, p, 0);
2035 isl_space_free(space);
2038 return p;
2041 static __isl_give isl_printer *print_pw_qpolynomial_isl(
2042 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2044 struct isl_print_space_data data = { 0 };
2046 if (!p || !pwqp)
2047 goto error;
2049 p = print_param_tuple(p, pwqp->dim, &data);
2050 p = isl_printer_print_str(p, "{ ");
2051 if (pwqp->n == 0) {
2052 if (!isl_space_is_set(pwqp->dim)) {
2053 p = print_tuple(pwqp->dim, p, isl_dim_in, &data);
2054 p = isl_printer_print_str(p, " -> ");
2056 p = isl_printer_print_str(p, "0");
2058 p = isl_pwqp_print_isl_body(p, pwqp);
2059 p = isl_printer_print_str(p, " }");
2060 return p;
2061 error:
2062 isl_printer_free(p);
2063 return NULL;
2066 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
2067 unsigned output_format)
2069 isl_printer *p;
2071 if (!pwqp)
2072 return;
2074 p = isl_printer_to_file(pwqp->dim->ctx, out);
2075 p = isl_printer_set_output_format(p, output_format);
2076 p = isl_printer_print_pw_qpolynomial(p, pwqp);
2078 isl_printer_free(p);
2081 static __isl_give isl_printer *isl_pwf_print_isl_body(
2082 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2084 struct isl_print_space_data data = { 0 };
2085 int i = 0;
2087 for (i = 0; i < pwf->n; ++i) {
2088 isl_space *space;
2090 if (i)
2091 p = isl_printer_print_str(p, "; ");
2092 space = isl_qpolynomial_fold_get_domain_space(pwf->p[i].fold);
2093 if (!isl_space_is_params(space)) {
2094 p = isl_print_space(space, p, 0, &data);
2095 p = isl_printer_print_str(p, " -> ");
2097 p = qpolynomial_fold_print(pwf->p[i].fold, p);
2098 p = print_disjuncts(set_to_map(pwf->p[i].set), space, p, 0);
2099 isl_space_free(space);
2102 return p;
2105 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
2106 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2108 struct isl_print_space_data data = { 0 };
2110 p = print_param_tuple(p, pwf->dim, &data);
2111 p = isl_printer_print_str(p, "{ ");
2112 if (pwf->n == 0) {
2113 if (!isl_space_is_set(pwf->dim)) {
2114 p = print_tuple(pwf->dim, p, isl_dim_in, &data);
2115 p = isl_printer_print_str(p, " -> ");
2117 p = isl_printer_print_str(p, "0");
2119 p = isl_pwf_print_isl_body(p, pwf);
2120 p = isl_printer_print_str(p, " }");
2121 return p;
2124 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2125 __isl_keep isl_local_space *ls, isl_int *c);
2127 /* We skip the constraint if it is implied by the div expression.
2129 * *first indicates whether this is the first constraint in the conjunction and
2130 * is updated if the constraint is actually printed.
2132 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
2133 __isl_keep isl_local_space *ls, isl_int *c, const char *op, int *first)
2135 unsigned o_div;
2136 isl_size n_div;
2137 int div;
2139 o_div = isl_local_space_offset(ls, isl_dim_div);
2140 n_div = isl_local_space_dim(ls, isl_dim_div);
2141 if (n_div < 0)
2142 return isl_printer_free(p);
2143 div = isl_seq_last_non_zero(c + o_div, n_div);
2144 if (div >= 0) {
2145 isl_bool is_div = isl_local_space_is_div_constraint(ls, c, div);
2146 if (is_div < 0)
2147 return isl_printer_free(p);
2148 if (is_div)
2149 return p;
2152 if (!*first)
2153 p = isl_printer_print_str(p, " && ");
2155 p = print_ls_affine_c(p, ls, c);
2156 p = isl_printer_print_str(p, " ");
2157 p = isl_printer_print_str(p, op);
2158 p = isl_printer_print_str(p, " 0");
2160 *first = 0;
2162 return p;
2165 static __isl_give isl_printer *print_ls_partial_affine_c(
2166 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2167 isl_int *c, unsigned len);
2169 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
2170 __isl_keep isl_space *space, __isl_keep isl_basic_set *bset)
2172 int i, j;
2173 int first = 1;
2174 isl_size n_div = isl_basic_set_dim(bset, isl_dim_div);
2175 isl_size total = isl_basic_set_dim(bset, isl_dim_all);
2176 isl_mat *div;
2177 isl_local_space *ls;
2179 if (n_div < 0 || total < 0)
2180 return isl_printer_free(p);
2182 total -= n_div;
2183 div = isl_basic_set_get_divs(bset);
2184 ls = isl_local_space_alloc_div(isl_space_copy(space), div);
2185 for (i = 0; i < bset->n_eq; ++i) {
2186 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
2187 if (j < 0)
2188 p = print_constraint_c(p, ls,
2189 bset->eq[i], "==", &first);
2190 else {
2191 if (i)
2192 p = isl_printer_print_str(p, " && ");
2193 p = isl_printer_print_str(p, "(");
2194 p = print_ls_partial_affine_c(p, ls, bset->eq[i],
2195 1 + total + j);
2196 p = isl_printer_print_str(p, ") % ");
2197 p = isl_printer_print_isl_int(p,
2198 bset->eq[i][1 + total + j]);
2199 p = isl_printer_print_str(p, " == 0");
2200 first = 0;
2203 for (i = 0; i < bset->n_ineq; ++i)
2204 p = print_constraint_c(p, ls, bset->ineq[i], ">=", &first);
2205 isl_local_space_free(ls);
2206 return p;
2209 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
2210 __isl_keep isl_space *space, __isl_keep isl_set *set)
2212 int i;
2214 if (!set)
2215 return isl_printer_free(p);
2217 if (set->n == 0)
2218 p = isl_printer_print_str(p, "0");
2220 for (i = 0; i < set->n; ++i) {
2221 if (i)
2222 p = isl_printer_print_str(p, " || ");
2223 if (set->n > 1)
2224 p = isl_printer_print_str(p, "(");
2225 p = print_basic_set_c(p, space, set->p[i]);
2226 if (set->n > 1)
2227 p = isl_printer_print_str(p, ")");
2229 return p;
2232 /* Print the piecewise quasi-polynomial "pwqp" to "p" in C format.
2234 static __isl_give isl_printer *print_pw_qpolynomial_c(
2235 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2237 int i;
2238 isl_space *space;
2240 space = isl_pw_qpolynomial_get_domain_space(pwqp);
2241 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set)) {
2242 p = print_qpolynomial_c(p, space, pwqp->p[0].qp);
2243 isl_space_free(space);
2244 return p;
2247 for (i = 0; i < pwqp->n; ++i) {
2248 p = isl_printer_print_str(p, "(");
2249 p = print_set_c(p, space, pwqp->p[i].set);
2250 p = isl_printer_print_str(p, ") ? (");
2251 p = print_qpolynomial_c(p, space, pwqp->p[i].qp);
2252 p = isl_printer_print_str(p, ") : ");
2255 isl_space_free(space);
2256 p = isl_printer_print_str(p, "0");
2257 return p;
2260 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
2261 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2263 if (!p || !pwqp)
2264 goto error;
2266 if (p->output_format == ISL_FORMAT_ISL)
2267 return print_pw_qpolynomial_isl(p, pwqp);
2268 else if (p->output_format == ISL_FORMAT_C)
2269 return print_pw_qpolynomial_c(p, pwqp);
2270 isl_assert(p->ctx, 0, goto error);
2271 error:
2272 isl_printer_free(p);
2273 return NULL;
2276 static isl_stat print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
2278 struct isl_union_print_data *data;
2279 data = (struct isl_union_print_data *)user;
2281 if (!data->first)
2282 data->p = isl_printer_print_str(data->p, "; ");
2283 data->first = 0;
2285 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
2286 isl_pw_qpolynomial_free(pwqp);
2288 return isl_stat_ok;
2291 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
2292 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2294 struct isl_union_print_data data;
2295 struct isl_print_space_data space_data = { 0 };
2296 isl_space *space;
2298 space = isl_union_pw_qpolynomial_get_space(upwqp);
2299 p = print_param_tuple(p, space, &space_data);
2300 isl_space_free(space);
2301 p = isl_printer_print_str(p, "{ ");
2302 data.p = p;
2303 data.first = 1;
2304 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
2305 &data);
2306 p = data.p;
2307 p = isl_printer_print_str(p, " }");
2308 return p;
2311 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
2312 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2314 if (!p || !upwqp)
2315 goto error;
2317 if (p->output_format == ISL_FORMAT_ISL)
2318 return print_union_pw_qpolynomial_isl(p, upwqp);
2319 isl_die(p->ctx, isl_error_invalid,
2320 "invalid output format for isl_union_pw_qpolynomial",
2321 goto error);
2322 error:
2323 isl_printer_free(p);
2324 return NULL;
2327 /* Print the quasi-polynomial reduction "fold" to "p" in C format,
2328 * with the variable names taken from the domain space "space".
2330 static __isl_give isl_printer *print_qpolynomial_fold_c(
2331 __isl_take isl_printer *p, __isl_keep isl_space *space,
2332 __isl_keep isl_qpolynomial_fold *fold)
2334 int i;
2336 for (i = 0; i < fold->n - 1; ++i)
2337 if (fold->type == isl_fold_min)
2338 p = isl_printer_print_str(p, "min(");
2339 else if (fold->type == isl_fold_max)
2340 p = isl_printer_print_str(p, "max(");
2342 for (i = 0; i < fold->n; ++i) {
2343 if (i)
2344 p = isl_printer_print_str(p, ", ");
2345 p = print_qpolynomial_c(p, space, fold->qp[i]);
2346 if (i)
2347 p = isl_printer_print_str(p, ")");
2349 return p;
2352 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
2353 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
2355 if (!p || !fold)
2356 goto error;
2357 if (p->output_format == ISL_FORMAT_ISL)
2358 return qpolynomial_fold_print(fold, p);
2359 else if (p->output_format == ISL_FORMAT_C)
2360 return print_qpolynomial_fold_c(p, fold->dim, fold);
2361 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2362 goto error);
2363 error:
2364 isl_printer_free(p);
2365 return NULL;
2368 /* Print the piecewise quasi-polynomial reduction "pwf" to "p" in C format.
2370 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
2371 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2373 int i;
2374 isl_space *space;
2376 space = isl_pw_qpolynomial_fold_get_domain_space(pwf);
2377 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set)) {
2378 p = print_qpolynomial_fold_c(p, space, pwf->p[0].fold);
2379 isl_space_free(space);
2380 return p;
2383 for (i = 0; i < pwf->n; ++i) {
2384 p = isl_printer_print_str(p, "(");
2385 p = print_set_c(p, space, pwf->p[i].set);
2386 p = isl_printer_print_str(p, ") ? (");
2387 p = print_qpolynomial_fold_c(p, space, pwf->p[i].fold);
2388 p = isl_printer_print_str(p, ") : ");
2391 isl_space_free(space);
2392 p = isl_printer_print_str(p, "0");
2393 return p;
2396 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
2397 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2399 if (!p || !pwf)
2400 goto error;
2402 if (p->output_format == ISL_FORMAT_ISL)
2403 return print_pw_qpolynomial_fold_isl(p, pwf);
2404 else if (p->output_format == ISL_FORMAT_C)
2405 return print_pw_qpolynomial_fold_c(p, pwf);
2406 isl_assert(p->ctx, 0, goto error);
2407 error:
2408 isl_printer_free(p);
2409 return NULL;
2412 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
2413 FILE *out, unsigned output_format)
2415 isl_printer *p;
2417 if (!pwf)
2418 return;
2420 p = isl_printer_to_file(pwf->dim->ctx, out);
2421 p = isl_printer_set_output_format(p, output_format);
2422 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
2424 isl_printer_free(p);
2427 static isl_stat print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf,
2428 void *user)
2430 struct isl_union_print_data *data;
2431 data = (struct isl_union_print_data *)user;
2433 if (!data->first)
2434 data->p = isl_printer_print_str(data->p, "; ");
2435 data->first = 0;
2437 data->p = isl_pwf_print_isl_body(data->p, pwf);
2438 isl_pw_qpolynomial_fold_free(pwf);
2440 return isl_stat_ok;
2443 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
2444 __isl_take isl_printer *p,
2445 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2447 struct isl_union_print_data data;
2448 struct isl_print_space_data space_data = { 0 };
2449 isl_space *space;
2451 space = isl_union_pw_qpolynomial_fold_get_space(upwf);
2452 p = print_param_tuple(p, space, &space_data);
2453 isl_space_free(space);
2454 p = isl_printer_print_str(p, "{ ");
2455 data.p = p;
2456 data.first = 1;
2457 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
2458 &print_pwf_body, &data);
2459 p = data.p;
2460 p = isl_printer_print_str(p, " }");
2461 return p;
2464 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2465 __isl_take isl_printer *p,
2466 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2468 if (!p || !upwf)
2469 goto error;
2471 if (p->output_format == ISL_FORMAT_ISL)
2472 return print_union_pw_qpolynomial_fold_isl(p, upwf);
2473 isl_die(p->ctx, isl_error_invalid,
2474 "invalid output format for isl_union_pw_qpolynomial_fold",
2475 goto error);
2476 error:
2477 isl_printer_free(p);
2478 return NULL;
2481 /* Print the isl_constraint "c" to "p".
2483 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
2484 __isl_keep isl_constraint *c)
2486 struct isl_print_space_data data = { 0 };
2487 isl_local_space *ls;
2488 isl_space *space;
2489 isl_bool exists;
2491 if (!p || !c)
2492 goto error;
2494 ls = isl_constraint_get_local_space(c);
2495 if (!ls)
2496 return isl_printer_free(p);
2497 space = isl_local_space_get_space(ls);
2498 p = print_param_tuple(p, space, &data);
2499 p = isl_printer_print_str(p, "{ ");
2500 p = isl_print_space(space, p, 0, &data);
2501 p = isl_printer_print_str(p, " : ");
2502 exists = need_exists(p, ls->div);
2503 if (exists < 0)
2504 p = isl_printer_free(p);
2505 if (exists >= 0 && exists)
2506 p = open_exists(p, space, ls->div, 0);
2507 p = print_affine_of_len(space, ls->div, p, c->v->el, c->v->size);
2508 if (isl_constraint_is_equality(c))
2509 p = isl_printer_print_str(p, " = 0");
2510 else
2511 p = isl_printer_print_str(p, " >= 0");
2512 if (exists >= 0 && exists)
2513 p = isl_printer_print_str(p, s_close_exists[0]);
2514 p = isl_printer_print_str(p, " }");
2515 isl_space_free(space);
2516 isl_local_space_free(ls);
2518 return p;
2519 error:
2520 isl_printer_free(p);
2521 return NULL;
2524 static __isl_give isl_printer *isl_printer_print_space_isl(
2525 __isl_take isl_printer *p, __isl_keep isl_space *space)
2527 struct isl_print_space_data data = { 0 };
2529 if (!space)
2530 goto error;
2532 p = print_param_tuple(p, space, &data);
2534 p = isl_printer_print_str(p, "{ ");
2535 if (isl_space_is_params(space))
2536 p = isl_printer_print_str(p, s_such_that[0]);
2537 else
2538 p = isl_print_space(space, p, 0, &data);
2539 p = isl_printer_print_str(p, " }");
2541 return p;
2542 error:
2543 isl_printer_free(p);
2544 return NULL;
2547 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
2548 __isl_keep isl_space *space)
2550 if (!p || !space)
2551 return isl_printer_free(p);
2552 if (p->output_format == ISL_FORMAT_ISL)
2553 return isl_printer_print_space_isl(p, space);
2554 else if (p->output_format == ISL_FORMAT_OMEGA)
2555 return print_omega_parameters(space, p);
2557 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
2558 "output format not supported for space",
2559 return isl_printer_free(p));
2562 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
2563 __isl_keep isl_local_space *ls)
2565 struct isl_print_space_data data = { 0 };
2566 isl_size n_div;
2568 n_div = isl_local_space_dim(ls, isl_dim_div);
2569 if (n_div < 0)
2570 goto error;
2572 p = print_param_tuple(p, ls->dim, &data);
2573 p = isl_printer_print_str(p, "{ ");
2574 p = isl_print_space(ls->dim, p, 0, &data);
2575 if (n_div > 0) {
2576 p = isl_printer_print_str(p, " : ");
2577 p = isl_printer_print_str(p, s_open_exists[0]);
2578 p = print_div_list(p, ls->dim, ls->div, 0, 1);
2579 p = isl_printer_print_str(p, s_close_exists[0]);
2580 } else if (isl_space_is_params(ls->dim))
2581 p = isl_printer_print_str(p, s_such_that[0]);
2582 p = isl_printer_print_str(p, " }");
2583 return p;
2584 error:
2585 isl_printer_free(p);
2586 return NULL;
2589 /* Print the (potentially rational) affine expression "aff" to "p",
2590 * with the variable names taken from "space".
2592 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2593 __isl_keep isl_space *space, __isl_keep isl_aff *aff)
2595 isl_size total;
2597 if (isl_aff_is_nan(aff))
2598 return isl_printer_print_str(p, "NaN");
2600 total = isl_local_space_dim(aff->ls, isl_dim_all);
2601 if (total < 0)
2602 return isl_printer_free(p);
2603 p = isl_printer_print_str(p, "(");
2604 p = print_affine_of_len(space, aff->ls->div, p,
2605 aff->v->el + 1, 1 + total);
2606 if (isl_int_is_one(aff->v->el[0]))
2607 p = isl_printer_print_str(p, ")");
2608 else {
2609 p = isl_printer_print_str(p, ")/");
2610 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2613 return p;
2616 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2617 __isl_keep isl_aff *aff)
2619 struct isl_print_space_data data = { 0 };
2621 if (isl_space_is_params(aff->ls->dim))
2623 else {
2624 p = print_tuple(aff->ls->dim, p, isl_dim_set, &data);
2625 p = isl_printer_print_str(p, " -> ");
2627 p = isl_printer_print_str(p, "[");
2628 p = print_aff_body(p, aff->ls->dim, aff);
2629 p = isl_printer_print_str(p, "]");
2631 return p;
2634 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2635 __isl_keep isl_aff *aff)
2637 struct isl_print_space_data data = { 0 };
2639 if (!aff)
2640 goto error;
2642 p = print_param_tuple(p, aff->ls->dim, &data);
2643 p = isl_printer_print_str(p, "{ ");
2644 p = print_aff(p, aff);
2645 p = isl_printer_print_str(p, " }");
2646 return p;
2647 error:
2648 isl_printer_free(p);
2649 return NULL;
2652 /* Print the body of an isl_pw_aff, i.e., a semicolon delimited
2653 * sequence of affine expressions, each followed by constraints.
2655 static __isl_give isl_printer *print_pw_aff_body(
2656 __isl_take isl_printer *p, __isl_keep isl_pw_aff *pa)
2658 int i;
2660 if (!pa)
2661 return isl_printer_free(p);
2663 for (i = 0; i < pa->n; ++i) {
2664 isl_space *space;
2666 if (i)
2667 p = isl_printer_print_str(p, "; ");
2668 p = print_aff(p, pa->p[i].aff);
2669 space = isl_aff_get_domain_space(pa->p[i].aff);
2670 p = print_disjuncts(set_to_map(pa->p[i].set), space, p, 0);
2671 isl_space_free(space);
2674 return p;
2677 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2678 __isl_keep isl_pw_aff *pwaff)
2680 struct isl_print_space_data data = { 0 };
2682 if (!pwaff)
2683 goto error;
2685 p = print_param_tuple(p, pwaff->dim, &data);
2686 p = isl_printer_print_str(p, "{ ");
2687 p = print_pw_aff_body(p, pwaff);
2688 p = isl_printer_print_str(p, " }");
2689 return p;
2690 error:
2691 isl_printer_free(p);
2692 return NULL;
2695 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2696 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2698 if (type == isl_dim_div) {
2699 p = isl_printer_print_str(p, "floord(");
2700 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2701 p = isl_printer_print_str(p, ", ");
2702 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2703 p = isl_printer_print_str(p, ")");
2704 } else {
2705 const char *name;
2707 name = isl_space_get_dim_name(ls->dim, type, pos);
2708 if (!name)
2709 name = "UNNAMED";
2710 p = isl_printer_print_str(p, name);
2712 return p;
2715 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2716 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2718 enum isl_dim_type type;
2720 if (!p || !ls)
2721 return isl_printer_free(p);
2723 if (pos == 0)
2724 return isl_printer_print_isl_int(p, c);
2726 if (isl_int_is_one(c))
2728 else if (isl_int_is_negone(c))
2729 p = isl_printer_print_str(p, "-");
2730 else {
2731 p = isl_printer_print_isl_int(p, c);
2732 p = isl_printer_print_str(p, "*");
2734 if (pos2type(ls->dim, &type, &pos) < 0)
2735 return isl_printer_free(p);
2736 p = print_ls_name_c(p, ls, type, pos);
2737 return p;
2740 static __isl_give isl_printer *print_ls_partial_affine_c(
2741 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2742 isl_int *c, unsigned len)
2744 int i;
2745 int first;
2747 for (i = 0, first = 1; i < len; ++i) {
2748 int flip = 0;
2749 if (isl_int_is_zero(c[i]))
2750 continue;
2751 if (!first) {
2752 if (isl_int_is_neg(c[i])) {
2753 flip = 1;
2754 isl_int_neg(c[i], c[i]);
2755 p = isl_printer_print_str(p, " - ");
2756 } else
2757 p = isl_printer_print_str(p, " + ");
2759 first = 0;
2760 p = print_ls_term_c(p, ls, c[i], i);
2761 if (flip)
2762 isl_int_neg(c[i], c[i]);
2764 if (first)
2765 p = isl_printer_print_str(p, "0");
2766 return p;
2769 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2770 __isl_keep isl_local_space *ls, isl_int *c)
2772 isl_size total = isl_local_space_dim(ls, isl_dim_all);
2774 if (total < 0)
2775 return isl_printer_free(p);
2776 return print_ls_partial_affine_c(p, ls, c, 1 + total);
2779 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2780 __isl_keep isl_aff *aff)
2782 isl_size total;
2784 total = isl_local_space_dim(aff->ls, isl_dim_all);
2785 if (total < 0)
2786 return isl_printer_free(p);
2787 if (!isl_int_is_one(aff->v->el[0]))
2788 p = isl_printer_print_str(p, "(");
2789 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2790 if (!isl_int_is_one(aff->v->el[0])) {
2791 p = isl_printer_print_str(p, ")/");
2792 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2794 return p;
2797 /* In the C format, we cannot express that "pwaff" may be undefined
2798 * on parts of the domain space. We therefore assume that the expression
2799 * will only be evaluated on its definition domain and compute the gist
2800 * of each cell with respect to this domain.
2802 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2803 __isl_keep isl_pw_aff *pwaff)
2805 isl_set *domain;
2806 isl_ast_build *build;
2807 isl_ast_expr *expr;
2809 if (pwaff->n < 1)
2810 isl_die(p->ctx, isl_error_unsupported,
2811 "cannot print empty isl_pw_aff in C format",
2812 return isl_printer_free(p));
2814 domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
2815 build = isl_ast_build_from_context(domain);
2816 expr = isl_ast_build_expr_from_pw_aff(build, isl_pw_aff_copy(pwaff));
2817 p = isl_printer_print_ast_expr(p, expr);
2818 isl_ast_expr_free(expr);
2819 isl_ast_build_free(build);
2821 return p;
2824 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2825 __isl_keep isl_aff *aff)
2827 if (!p || !aff)
2828 goto error;
2830 if (p->output_format == ISL_FORMAT_ISL)
2831 return print_aff_isl(p, aff);
2832 else if (p->output_format == ISL_FORMAT_C)
2833 return print_aff_c(p, aff);
2834 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2835 goto error);
2836 error:
2837 isl_printer_free(p);
2838 return NULL;
2841 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2842 __isl_keep isl_pw_aff *pwaff)
2844 if (!p || !pwaff)
2845 goto error;
2847 if (p->output_format == ISL_FORMAT_ISL)
2848 return print_pw_aff_isl(p, pwaff);
2849 else if (p->output_format == ISL_FORMAT_C)
2850 return print_pw_aff_c(p, pwaff);
2851 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2852 goto error);
2853 error:
2854 isl_printer_free(p);
2855 return NULL;
2858 /* Print "pa" in a sequence of isl_pw_affs delimited by semicolons.
2859 * Each isl_pw_aff itself is also printed as semicolon delimited
2860 * sequence of pieces.
2861 * If data->first = 1, then this is the first in the sequence.
2862 * Update data->first to tell the next element that it is not the first.
2864 static isl_stat print_pw_aff_body_wrap(__isl_take isl_pw_aff *pa,
2865 void *user)
2867 struct isl_union_print_data *data;
2868 data = (struct isl_union_print_data *) user;
2870 if (!data->first)
2871 data->p = isl_printer_print_str(data->p, "; ");
2872 data->first = 0;
2874 data->p = print_pw_aff_body(data->p, pa);
2875 isl_pw_aff_free(pa);
2877 return data->p ? isl_stat_ok : isl_stat_error;
2880 /* Print the body of an isl_union_pw_aff, i.e., a semicolon delimited
2881 * sequence of affine expressions, each followed by constraints,
2882 * with the sequence enclosed in braces.
2884 static __isl_give isl_printer *print_union_pw_aff_body(
2885 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2887 struct isl_union_print_data data = { p, 1 };
2889 p = isl_printer_print_str(p, s_open_set[0]);
2890 data.p = p;
2891 if (isl_union_pw_aff_foreach_pw_aff(upa,
2892 &print_pw_aff_body_wrap, &data) < 0)
2893 data.p = isl_printer_free(p);
2894 p = data.p;
2895 p = isl_printer_print_str(p, s_close_set[0]);
2897 return p;
2900 /* Print the isl_union_pw_aff "upa" to "p" in isl format.
2902 * The individual isl_pw_affs are delimited by a semicolon.
2904 static __isl_give isl_printer *print_union_pw_aff_isl(
2905 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2907 struct isl_print_space_data data = { 0 };
2908 isl_space *space;
2910 space = isl_union_pw_aff_get_space(upa);
2911 p = print_param_tuple(p, space, &data);
2912 isl_space_free(space);
2913 p = print_union_pw_aff_body(p, upa);
2914 return p;
2917 /* Print the isl_union_pw_aff "upa" to "p".
2919 * We currently only support an isl format.
2921 __isl_give isl_printer *isl_printer_print_union_pw_aff(
2922 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2924 if (!p || !upa)
2925 return isl_printer_free(p);
2927 if (p->output_format == ISL_FORMAT_ISL)
2928 return print_union_pw_aff_isl(p, upa);
2929 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2930 "unsupported output format", return isl_printer_free(p));
2933 /* Print dimension "pos" of data->space to "p".
2935 * data->user is assumed to be an isl_multi_aff.
2937 * If the current dimension is an output dimension, then print
2938 * the corresponding expression. Otherwise, print the name of the dimension.
2940 static __isl_give isl_printer *print_dim_ma(__isl_take isl_printer *p,
2941 struct isl_print_space_data *data, unsigned pos)
2943 isl_multi_aff *ma = data->user;
2945 if (data->type == isl_dim_out) {
2946 isl_space *space;
2948 space = isl_multi_aff_get_domain_space(ma);
2949 p = print_aff_body(p, space, ma->u.p[pos]);
2950 isl_space_free(space);
2951 } else {
2952 p = print_name(data->space, p, data->type, pos, data->latex);
2955 return p;
2958 static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
2959 __isl_keep isl_multi_aff *maff)
2961 struct isl_print_space_data data = { 0 };
2963 data.print_dim = &print_dim_ma;
2964 data.user = maff;
2965 return isl_print_space(maff->space, p, 0, &data);
2968 static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
2969 __isl_keep isl_multi_aff *maff)
2971 struct isl_print_space_data data = { 0 };
2973 if (!maff)
2974 goto error;
2976 p = print_param_tuple(p, maff->space, &data);
2977 p = isl_printer_print_str(p, "{ ");
2978 p = print_multi_aff(p, maff);
2979 p = isl_printer_print_str(p, " }");
2980 return p;
2981 error:
2982 isl_printer_free(p);
2983 return NULL;
2986 __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
2987 __isl_keep isl_multi_aff *maff)
2989 if (!p || !maff)
2990 goto error;
2992 if (p->output_format == ISL_FORMAT_ISL)
2993 return print_multi_aff_isl(p, maff);
2994 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2995 goto error);
2996 error:
2997 isl_printer_free(p);
2998 return NULL;
3001 static __isl_give isl_printer *print_pw_multi_aff_body(
3002 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3004 int i;
3006 if (!pma)
3007 goto error;
3009 for (i = 0; i < pma->n; ++i) {
3010 isl_space *space;
3012 if (i)
3013 p = isl_printer_print_str(p, "; ");
3014 p = print_multi_aff(p, pma->p[i].maff);
3015 space = isl_multi_aff_get_domain_space(pma->p[i].maff);
3016 p = print_disjuncts(set_to_map(pma->p[i].set), space, p, 0);
3017 isl_space_free(space);
3019 return p;
3020 error:
3021 isl_printer_free(p);
3022 return NULL;
3025 static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
3026 __isl_keep isl_pw_multi_aff *pma)
3028 struct isl_print_space_data data = { 0 };
3030 if (!pma)
3031 goto error;
3033 p = print_param_tuple(p, pma->dim, &data);
3034 p = isl_printer_print_str(p, "{ ");
3035 p = print_pw_multi_aff_body(p, pma);
3036 p = isl_printer_print_str(p, " }");
3037 return p;
3038 error:
3039 isl_printer_free(p);
3040 return NULL;
3043 /* Print the unnamed, single-dimensional piecewise multi affine expression "pma"
3044 * to "p".
3046 static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
3047 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3049 int i;
3050 isl_space *space;
3052 space = isl_pw_multi_aff_get_domain_space(pma);
3053 for (i = 0; i < pma->n - 1; ++i) {
3054 p = isl_printer_print_str(p, "(");
3055 p = print_set_c(p, space, pma->p[i].set);
3056 p = isl_printer_print_str(p, ") ? (");
3057 p = print_aff_c(p, pma->p[i].maff->u.p[0]);
3058 p = isl_printer_print_str(p, ") : ");
3060 isl_space_free(space);
3062 return print_aff_c(p, pma->p[pma->n - 1].maff->u.p[0]);
3065 static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
3066 __isl_keep isl_pw_multi_aff *pma)
3068 isl_size n;
3069 const char *name;
3071 if (!pma)
3072 goto error;
3073 if (pma->n < 1)
3074 isl_die(p->ctx, isl_error_unsupported,
3075 "cannot print empty isl_pw_multi_aff in C format",
3076 goto error);
3077 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
3078 if (n < 0)
3079 return isl_printer_free(p);
3080 name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
3081 if (!name && n == 1)
3082 return print_unnamed_pw_multi_aff_c(p, pma);
3083 if (!name)
3084 isl_die(p->ctx, isl_error_unsupported,
3085 "cannot print unnamed isl_pw_multi_aff in C format",
3086 goto error);
3088 p = isl_printer_print_str(p, name);
3089 if (n != 0)
3090 isl_die(p->ctx, isl_error_unsupported,
3091 "not supported yet", goto error);
3093 return p;
3094 error:
3095 isl_printer_free(p);
3096 return NULL;
3099 __isl_give isl_printer *isl_printer_print_pw_multi_aff(
3100 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3102 if (!p || !pma)
3103 goto error;
3105 if (p->output_format == ISL_FORMAT_ISL)
3106 return print_pw_multi_aff_isl(p, pma);
3107 if (p->output_format == ISL_FORMAT_C)
3108 return print_pw_multi_aff_c(p, pma);
3109 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3110 goto error);
3111 error:
3112 isl_printer_free(p);
3113 return NULL;
3116 static isl_stat print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
3117 void *user)
3119 struct isl_union_print_data *data;
3120 data = (struct isl_union_print_data *) user;
3122 if (!data->first)
3123 data->p = isl_printer_print_str(data->p, "; ");
3124 data->first = 0;
3126 data->p = print_pw_multi_aff_body(data->p, pma);
3127 isl_pw_multi_aff_free(pma);
3129 return isl_stat_ok;
3132 static __isl_give isl_printer *print_union_pw_multi_aff_isl(
3133 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
3135 struct isl_union_print_data data;
3136 struct isl_print_space_data space_data = { 0 };
3137 isl_space *space;
3139 space = isl_union_pw_multi_aff_get_space(upma);
3140 p = print_param_tuple(p, space, &space_data);
3141 isl_space_free(space);
3142 p = isl_printer_print_str(p, s_open_set[0]);
3143 data.p = p;
3144 data.first = 1;
3145 isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3146 &print_pw_multi_aff_body_wrap, &data);
3147 p = data.p;
3148 p = isl_printer_print_str(p, s_close_set[0]);
3149 return p;
3152 __isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
3153 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
3155 if (!p || !upma)
3156 goto error;
3158 if (p->output_format == ISL_FORMAT_ISL)
3159 return print_union_pw_multi_aff_isl(p, upma);
3160 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3161 goto error);
3162 error:
3163 isl_printer_free(p);
3164 return NULL;
3167 /* Print dimension "pos" of data->space to "p".
3169 * data->user is assumed to be an isl_multi_pw_aff.
3171 * If the current dimension is an output dimension, then print
3172 * the corresponding piecewise affine expression.
3173 * Otherwise, print the name of the dimension.
3175 static __isl_give isl_printer *print_dim_mpa(__isl_take isl_printer *p,
3176 struct isl_print_space_data *data, unsigned pos)
3178 int i;
3179 int need_parens;
3180 isl_space *space;
3181 isl_multi_pw_aff *mpa = data->user;
3182 isl_pw_aff *pa;
3184 if (data->type != isl_dim_out)
3185 return print_name(data->space, p, data->type, pos, data->latex);
3187 pa = mpa->u.p[pos];
3188 if (pa->n == 0)
3189 return isl_printer_print_str(p, "(0 : false)");
3191 need_parens = pa->n != 1 || !isl_set_plain_is_universe(pa->p[0].set);
3192 if (need_parens)
3193 p = isl_printer_print_str(p, "(");
3194 space = isl_multi_pw_aff_get_domain_space(mpa);
3195 for (i = 0; i < pa->n; ++i) {
3197 if (i)
3198 p = isl_printer_print_str(p, "; ");
3199 p = print_aff_body(p, space, pa->p[i].aff);
3200 p = print_disjuncts(pa->p[i].set, space, p, 0);
3202 isl_space_free(space);
3203 if (need_parens)
3204 p = isl_printer_print_str(p, ")");
3206 return p;
3209 /* Print "mpa" to "p" in isl format.
3211 * If "mpa" is zero-dimensional and has a non-trivial explicit domain,
3212 * then it is printed after the tuple of affine expressions.
3214 static __isl_give isl_printer *print_multi_pw_aff_isl(__isl_take isl_printer *p,
3215 __isl_keep isl_multi_pw_aff *mpa)
3217 struct isl_print_space_data data = { 0 };
3218 isl_bool has_domain;
3220 if (!mpa)
3221 return isl_printer_free(p);
3223 p = print_param_tuple(p, mpa->space, &data);
3224 p = isl_printer_print_str(p, "{ ");
3225 data.print_dim = &print_dim_mpa;
3226 data.user = mpa;
3227 p = isl_print_space(mpa->space, p, 0, &data);
3228 has_domain = isl_multi_pw_aff_has_non_trivial_domain(mpa);
3229 if (has_domain < 0)
3230 return isl_printer_free(p);
3231 if (has_domain) {
3232 isl_space *space;
3234 space = isl_space_domain(isl_space_copy(mpa->space));
3235 p = print_disjuncts_set(mpa->u.dom, space, p, 0);
3236 isl_space_free(space);
3238 p = isl_printer_print_str(p, " }");
3239 return p;
3242 __isl_give isl_printer *isl_printer_print_multi_pw_aff(
3243 __isl_take isl_printer *p, __isl_keep isl_multi_pw_aff *mpa)
3245 if (!p || !mpa)
3246 return isl_printer_free(p);
3248 if (p->output_format == ISL_FORMAT_ISL)
3249 return print_multi_pw_aff_isl(p, mpa);
3250 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3251 return isl_printer_free(p));
3254 /* Print dimension "pos" of data->space to "p".
3256 * data->user is assumed to be an isl_multi_val.
3258 * If the current dimension is an output dimension, then print
3259 * the corresponding value. Otherwise, print the name of the dimension.
3261 static __isl_give isl_printer *print_dim_mv(__isl_take isl_printer *p,
3262 struct isl_print_space_data *data, unsigned pos)
3264 isl_multi_val *mv = data->user;
3266 if (data->type == isl_dim_out)
3267 return isl_printer_print_val(p, mv->u.p[pos]);
3268 else
3269 return print_name(data->space, p, data->type, pos, data->latex);
3272 /* Print the isl_multi_val "mv" to "p" in isl format.
3274 static __isl_give isl_printer *print_multi_val_isl(__isl_take isl_printer *p,
3275 __isl_keep isl_multi_val *mv)
3277 struct isl_print_space_data data = { 0 };
3279 if (!mv)
3280 return isl_printer_free(p);
3282 p = print_param_tuple(p, mv->space, &data);
3283 p = isl_printer_print_str(p, "{ ");
3284 data.print_dim = &print_dim_mv;
3285 data.user = mv;
3286 p = isl_print_space(mv->space, p, 0, &data);
3287 p = isl_printer_print_str(p, " }");
3288 return p;
3291 /* Print the isl_multi_val "mv" to "p".
3293 * Currently only supported in isl format.
3295 __isl_give isl_printer *isl_printer_print_multi_val(
3296 __isl_take isl_printer *p, __isl_keep isl_multi_val *mv)
3298 if (!p || !mv)
3299 return isl_printer_free(p);
3301 if (p->output_format == ISL_FORMAT_ISL)
3302 return print_multi_val_isl(p, mv);
3303 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3304 return isl_printer_free(p));
3307 /* Print dimension "pos" of data->space to "p".
3309 * data->user is assumed to be an isl_multi_union_pw_aff.
3311 * The current dimension is necessarily a set dimension, so
3312 * we print the corresponding isl_union_pw_aff, including
3313 * the braces.
3315 static __isl_give isl_printer *print_union_pw_aff_dim(__isl_take isl_printer *p,
3316 struct isl_print_space_data *data, unsigned pos)
3318 isl_multi_union_pw_aff *mupa = data->user;
3319 isl_union_pw_aff *upa;
3321 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, pos);
3322 p = print_union_pw_aff_body(p, upa);
3323 isl_union_pw_aff_free(upa);
3325 return p;
3328 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3330 * If "mupa" is zero-dimensional and has a non-trivial explicit domain,
3331 * then it is printed after the tuple of affine expressions.
3332 * In order to clarify that this domain belongs to the expression,
3333 * the tuple along with the domain are placed inside parentheses.
3334 * If "mupa" has any parameters, then the opening parenthesis
3335 * appears after the parameter declarations.
3337 static __isl_give isl_printer *print_multi_union_pw_aff_isl(
3338 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3340 struct isl_print_space_data data = { 0 };
3341 isl_bool has_domain;
3342 isl_space *space;
3344 if (!mupa)
3345 return isl_printer_free(p);
3346 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
3347 if (has_domain < 0)
3348 return isl_printer_free(p);
3350 space = isl_multi_union_pw_aff_get_space(mupa);
3351 p = print_param_tuple(p, space, &data);
3353 if (has_domain)
3354 p = isl_printer_print_str(p, "(");
3356 data.print_dim = &print_union_pw_aff_dim;
3357 data.user = mupa;
3359 p = isl_print_space(space, p, 0, &data);
3360 isl_space_free(space);
3362 if (has_domain) {
3363 p = isl_printer_print_str(p, " : ");
3364 p = isl_printer_print_union_set_isl_body(p, mupa->u.dom);
3365 p = isl_printer_print_str(p, ")");
3368 return p;
3371 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3373 * We currently only support an isl format.
3375 __isl_give isl_printer *isl_printer_print_multi_union_pw_aff(
3376 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3378 if (!p || !mupa)
3379 return isl_printer_free(p);
3381 if (p->output_format == ISL_FORMAT_ISL)
3382 return print_multi_union_pw_aff_isl(p, mupa);
3383 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
3384 "unsupported output format", return isl_printer_free(p));