isl_basic_{map,set}_free_inequality: return modified result
[isl.git] / isl_output.c
blob913d2f51d3e607027688ece6ecc9efb621ad0886
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.h>
30 #include <isl_local_space_private.h>
31 #include <isl_aff_private.h>
32 #include <isl_val_private.h>
33 #include <isl_constraint_private.h>
34 #include <isl/ast_build.h>
35 #include <isl_sort.h>
36 #include <isl_output_private.h>
38 #include <bset_to_bmap.c>
39 #include <set_to_map.c>
40 #include <uset_to_umap.c>
42 static const char *s_to[2] = { " -> ", " \\to " };
43 static const char *s_and[2] = { " and ", " \\wedge " };
44 static const char *s_or[2] = { " or ", " \\vee " };
45 static const char *s_le[2] = { "<=", "\\le" };
46 static const char *s_ge[2] = { ">=", "\\ge" };
47 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
48 static const char *s_close_set[2] = { " }", " \\,\\}" };
49 static const char *s_open_list[2] = { "[", "(" };
50 static const char *s_close_list[2] = { "]", ")" };
51 static const char *s_such_that[2] = { " : ", " \\mid " };
52 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
53 static const char *s_close_exists[2] = { ")", "" };
54 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
55 static const char *s_mod[2] = { "mod", "\\bmod" };
56 static const char *s_param_prefix[2] = { "p", "p_" };
57 static const char *s_input_prefix[2] = { "i", "i_" };
58 static const char *s_output_prefix[2] = { "o", "o_" };
60 static __isl_give isl_printer *print_constraint_polylib(
61 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
63 int i;
64 isl_size n_in = isl_basic_map_dim(bmap, isl_dim_in);
65 isl_size n_out = isl_basic_map_dim(bmap, isl_dim_out);
66 isl_size nparam = isl_basic_map_dim(bmap, isl_dim_param);
67 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
69 if (n_in < 0 || n_out < 0 || nparam < 0)
70 return isl_printer_free(p);
72 p = isl_printer_start_line(p);
73 p = isl_printer_print_int(p, ineq);
74 for (i = 0; i < n_out; ++i) {
75 p = isl_printer_print_str(p, " ");
76 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
78 for (i = 0; i < n_in; ++i) {
79 p = isl_printer_print_str(p, " ");
80 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
82 for (i = 0; i < bmap->n_div; ++i) {
83 p = isl_printer_print_str(p, " ");
84 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
86 for (i = 0; i < nparam; ++i) {
87 p = isl_printer_print_str(p, " ");
88 p = isl_printer_print_isl_int(p, c[1+i]);
90 p = isl_printer_print_str(p, " ");
91 p = isl_printer_print_isl_int(p, c[0]);
92 p = isl_printer_end_line(p);
93 return p;
96 static __isl_give isl_printer *print_constraints_polylib(
97 struct isl_basic_map *bmap, __isl_take isl_printer *p)
99 int i;
101 p = isl_printer_set_isl_int_width(p, 5);
103 for (i = 0; i < bmap->n_eq; ++i)
104 p = print_constraint_polylib(bmap, 0, i, p);
105 for (i = 0; i < bmap->n_ineq; ++i)
106 p = print_constraint_polylib(bmap, 1, i, p);
108 return p;
111 static __isl_give isl_printer *bset_print_constraints_polylib(
112 struct isl_basic_set *bset, __isl_take isl_printer *p)
114 return print_constraints_polylib(bset_to_bmap(bset), p);
117 static __isl_give isl_printer *isl_basic_map_print_polylib(
118 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
120 isl_size total;
122 total = isl_basic_map_dim(bmap, isl_dim_all);
123 if (total < 0)
124 return isl_printer_free(p);
125 p = isl_printer_start_line(p);
126 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
127 p = isl_printer_print_str(p, " ");
128 p = isl_printer_print_int(p, 1 + total + 1);
129 if (ext) {
130 isl_size n_in = isl_basic_map_dim(bmap, isl_dim_in);
131 isl_size n_out = isl_basic_map_dim(bmap, isl_dim_out);
132 isl_size n_div = isl_basic_map_dim(bmap, isl_dim_div);
133 isl_size nparam = isl_basic_map_dim(bmap, isl_dim_param);
135 if (n_in < 0 || n_out < 0 || n_div < 0 || nparam < 0)
136 return isl_printer_free(p);
138 p = isl_printer_print_str(p, " ");
139 p = isl_printer_print_int(p, n_out);
140 p = isl_printer_print_str(p, " ");
141 p = isl_printer_print_int(p, n_in);
142 p = isl_printer_print_str(p, " ");
143 p = isl_printer_print_int(p, n_div);
144 p = isl_printer_print_str(p, " ");
145 p = isl_printer_print_int(p, nparam);
147 p = isl_printer_end_line(p);
148 return print_constraints_polylib(bmap, p);
151 static __isl_give isl_printer *isl_basic_set_print_polylib(
152 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
154 return isl_basic_map_print_polylib(bset_to_bmap(bset), p, ext);
157 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
158 __isl_take isl_printer *p, int ext)
160 int i;
162 p = isl_printer_start_line(p);
163 p = isl_printer_print_int(p, map->n);
164 p = isl_printer_end_line(p);
165 for (i = 0; i < map->n; ++i) {
166 p = isl_printer_start_line(p);
167 p = isl_printer_end_line(p);
168 p = isl_basic_map_print_polylib(map->p[i], p, ext);
170 return p;
173 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
174 __isl_take isl_printer *p, int ext)
176 return isl_map_print_polylib(set_to_map(set), p, ext);
179 static isl_size count_same_name(__isl_keep isl_space *space,
180 enum isl_dim_type type, unsigned pos, const char *name)
182 enum isl_dim_type t;
183 int p;
184 isl_size s;
185 int count = 0;
187 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
188 s = t == type ? pos : isl_space_dim(space, t);
189 if (s < 0)
190 return isl_size_error;
191 for (p = 0; p < s; ++p) {
192 const char *n = isl_space_get_dim_name(space, t, p);
193 if (n && !strcmp(n, name))
194 count++;
197 return count;
200 /* Print the name of the variable of type "type" and position "pos"
201 * in "space" to "p".
203 static __isl_give isl_printer *print_name(__isl_keep isl_space *space,
204 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
205 int latex)
207 const char *name;
208 char buffer[20];
209 isl_size primes;
211 name = type == isl_dim_div ? NULL
212 : isl_space_get_dim_name(space, type, pos);
214 if (!name) {
215 const char *prefix;
216 if (type == isl_dim_param)
217 prefix = s_param_prefix[latex];
218 else if (type == isl_dim_div)
219 prefix = s_div_prefix[latex];
220 else if (isl_space_is_set(space) || type == isl_dim_in)
221 prefix = s_input_prefix[latex];
222 else
223 prefix = s_output_prefix[latex];
224 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
225 name = buffer;
227 primes = count_same_name(space, name == buffer ? isl_dim_div : type,
228 pos, name);
229 if (primes < 0)
230 return isl_printer_free(p);
231 p = isl_printer_print_str(p, name);
232 while (primes-- > 0)
233 p = isl_printer_print_str(p, "'");
234 return p;
237 static isl_stat pos2type(__isl_keep isl_space *space,
238 enum isl_dim_type *type, unsigned *pos)
240 isl_size n_in = isl_space_dim(space, isl_dim_in);
241 isl_size n_out = isl_space_dim(space, isl_dim_out);
242 isl_size nparam = isl_space_dim(space, isl_dim_param);
244 if (n_in < 0 || n_out < 0 || nparam < 0)
245 return isl_stat_error;
247 if (*pos < 1 + nparam) {
248 *type = isl_dim_param;
249 *pos -= 1;
250 } else if (*pos < 1 + nparam + n_in) {
251 *type = isl_dim_in;
252 *pos -= 1 + nparam;
253 } else if (*pos < 1 + nparam + n_in + n_out) {
254 *type = isl_dim_out;
255 *pos -= 1 + nparam + n_in;
256 } else {
257 *type = isl_dim_div;
258 *pos -= 1 + nparam + n_in + n_out;
261 return isl_stat_ok;
264 /* Can the div expression of the integer division at position "row" of "div"
265 * be printed?
266 * In particular, are the div expressions available and does the selected
267 * variable have a known explicit representation?
268 * Furthermore, the Omega format does not allow any div expressions
269 * to be printed.
271 static isl_bool can_print_div_expr(__isl_keep isl_printer *p,
272 __isl_keep isl_mat *div, int pos)
274 if (p->output_format == ISL_FORMAT_OMEGA)
275 return isl_bool_false;
276 if (!div)
277 return isl_bool_false;
278 return isl_bool_not(isl_local_div_is_marked_unknown(div, pos));
281 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
282 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
284 static __isl_give isl_printer *print_term(__isl_keep isl_space *space,
285 __isl_keep isl_mat *div,
286 isl_int c, unsigned pos, __isl_take isl_printer *p, int latex)
288 enum isl_dim_type type;
289 int print_div_def;
291 if (!p || !space)
292 return isl_printer_free(p);
294 if (pos == 0)
295 return isl_printer_print_isl_int(p, c);
297 if (pos2type(space, &type, &pos) < 0)
298 return isl_printer_free(p);
299 print_div_def = type == isl_dim_div && can_print_div_expr(p, div, pos);
301 if (isl_int_is_one(c))
303 else if (isl_int_is_negone(c))
304 p = isl_printer_print_str(p, "-");
305 else {
306 p = isl_printer_print_isl_int(p, c);
307 if (p->output_format == ISL_FORMAT_C || print_div_def)
308 p = isl_printer_print_str(p, "*");
310 if (print_div_def)
311 p = print_div(space, div, pos, p);
312 else
313 p = print_name(space, p, type, pos, latex);
314 return p;
317 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_space *dim,
318 __isl_keep isl_mat *div,
319 __isl_take isl_printer *p, isl_int *c, int len)
321 int i;
322 int first;
324 for (i = 0, first = 1; i < len; ++i) {
325 int flip = 0;
326 if (isl_int_is_zero(c[i]))
327 continue;
328 if (!first) {
329 if (isl_int_is_neg(c[i])) {
330 flip = 1;
331 isl_int_neg(c[i], c[i]);
332 p = isl_printer_print_str(p, " - ");
333 } else
334 p = isl_printer_print_str(p, " + ");
336 first = 0;
337 p = print_term(dim, div, c[i], i, p, 0);
338 if (flip)
339 isl_int_neg(c[i], c[i]);
341 if (first)
342 p = isl_printer_print_str(p, "0");
343 return p;
346 /* Print an affine expression "c"
347 * to "p", with the variable names taken from "space" and
348 * the integer division definitions taken from "div".
350 static __isl_give isl_printer *print_affine(__isl_take isl_printer *p,
351 __isl_keep isl_space *space, __isl_keep isl_mat *div, isl_int *c)
353 isl_size n_div, total;
354 unsigned len;
356 total = isl_space_dim(space, isl_dim_all);
357 n_div = isl_mat_rows(div);
358 if (total < 0 || n_div < 0)
359 return isl_printer_free(p);
360 len = 1 + total + n_div;
361 return print_affine_of_len(space, div, p, c, len);
364 /* offset is the offset of local_space inside data->type of data->space.
366 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
367 __isl_keep isl_space *local_space, enum isl_dim_type local_type,
368 struct isl_print_space_data *data, int offset)
370 int i;
371 isl_size dim;
373 if (data->space != local_space && local_type == isl_dim_out)
374 offset += local_space->n_in;
376 dim = isl_space_dim(local_space, local_type);
377 if (dim < 0)
378 return isl_printer_free(p);
379 for (i = 0; i < dim; ++i) {
380 if (i)
381 p = isl_printer_print_str(p, ", ");
382 if (data->print_dim)
383 p = data->print_dim(p, data, offset + i);
384 else
385 p = print_name(data->space, p, data->type, offset + i,
386 data->latex);
388 return p;
391 static __isl_give isl_printer *print_var_list(__isl_take isl_printer *p,
392 __isl_keep isl_space *space, enum isl_dim_type type)
394 struct isl_print_space_data data = { .space = space, .type = type };
396 return print_nested_var_list(p, space, type, &data, 0);
399 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
400 __isl_keep isl_space *local_dim,
401 struct isl_print_space_data *data, int offset);
403 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
404 __isl_keep isl_space *local_space, enum isl_dim_type local_type,
405 struct isl_print_space_data *data, int offset)
407 const char *name = NULL;
408 isl_size n = isl_space_dim(local_space, local_type);
410 if (n < 0)
411 return isl_printer_free(p);
412 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
413 name = isl_space_get_tuple_name(local_space, local_type);
414 if (name) {
415 if (data->latex)
416 p = isl_printer_print_str(p, "\\mathrm{");
417 p = isl_printer_print_str(p, name);
418 if (data->latex)
419 p = isl_printer_print_str(p, "}");
422 if (!data->latex || n != 1 || name)
423 p = isl_printer_print_str(p, s_open_list[data->latex]);
424 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
425 local_space->nested[local_type - isl_dim_in]) {
426 if (data->space != local_space && local_type == isl_dim_out)
427 offset += local_space->n_in;
428 p = print_nested_map_dim(p,
429 local_space->nested[local_type - isl_dim_in],
430 data, offset);
431 } else
432 p = print_nested_var_list(p, local_space, local_type, data,
433 offset);
434 if (!data->latex || n != 1 || name)
435 p = isl_printer_print_str(p, s_close_list[data->latex]);
436 return p;
439 static __isl_give isl_printer *print_tuple(__isl_keep isl_space *dim,
440 __isl_take isl_printer *p, enum isl_dim_type type,
441 struct isl_print_space_data *data)
443 data->space = dim;
444 data->type = type;
445 return print_nested_tuple(p, dim, type, data, 0);
448 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
449 __isl_keep isl_space *local_dim,
450 struct isl_print_space_data *data, int offset)
452 p = print_nested_tuple(p, local_dim, isl_dim_in, data, offset);
453 p = isl_printer_print_str(p, s_to[data->latex]);
454 p = print_nested_tuple(p, local_dim, isl_dim_out, data, offset);
456 return p;
459 __isl_give isl_printer *isl_print_space(__isl_keep isl_space *space,
460 __isl_take isl_printer *p, int rational,
461 struct isl_print_space_data *data)
463 if (rational && !data->latex)
464 p = isl_printer_print_str(p, "rat: ");
465 if (isl_space_is_params(space))
467 else if (isl_space_is_set(space))
468 p = print_tuple(space, p, isl_dim_set, data);
469 else {
470 p = print_tuple(space, p, isl_dim_in, data);
471 p = isl_printer_print_str(p, s_to[data->latex]);
472 p = print_tuple(space, p, isl_dim_out, data);
475 return p;
478 static __isl_give isl_printer *print_omega_parameters(
479 __isl_keep isl_space *space, __isl_take isl_printer *p)
481 isl_size nparam = isl_space_dim(space, isl_dim_param);
483 if (nparam < 0)
484 return isl_printer_free(p);
485 if (nparam == 0)
486 return p;
488 p = isl_printer_start_line(p);
489 p = isl_printer_print_str(p, "symbolic ");
490 p = print_var_list(p, space, isl_dim_param);
491 p = isl_printer_print_str(p, ";");
492 p = isl_printer_end_line(p);
493 return p;
496 /* Does the inequality constraint following "i" in "bmap"
497 * have an opposite value for the same last coefficient?
498 * "last" is the position of the last coefficient of inequality "i".
499 * If the next constraint is a div constraint, then it is ignored
500 * since div constraints are not printed.
502 static isl_bool next_is_opposite(__isl_keep isl_basic_map *bmap, int i,
503 int last)
505 int r;
506 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
507 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
509 if (total < 0)
510 return isl_bool_error;
511 if (i + 1 >= bmap->n_ineq)
512 return isl_bool_false;
513 if (isl_seq_last_non_zero(bmap->ineq[i + 1], 1 + total) != last)
514 return isl_bool_false;
515 if (last >= o_div) {
516 isl_bool is_div;
517 is_div = isl_basic_map_is_div_constraint(bmap,
518 bmap->ineq[i + 1], last - o_div);
519 if (is_div < 0)
520 return isl_bool_error;
521 if (is_div)
522 return isl_bool_false;
524 r = isl_int_abs_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]) &&
525 !isl_int_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]);
526 return isl_bool_ok(r);
529 /* Return a string representation of the operator used when
530 * printing a constraint where the LHS is greater than or equal to the LHS
531 * (sign > 0) or smaller than or equal to the LHS (sign < 0).
532 * If "strict" is set, then return the strict version of the comparison
533 * operator.
535 static const char *constraint_op(int sign, int strict, int latex)
537 if (strict)
538 return sign < 0 ? "<" : ">";
539 if (sign < 0)
540 return s_le[latex];
541 else
542 return s_ge[latex];
545 /* Print one side of a constraint "c" to "p", with
546 * the variable names taken from "space" and the integer division definitions
547 * taken from "div".
548 * "last" is the position of the last non-zero coefficient.
549 * Let c' be the result of zeroing out this coefficient, then
550 * the partial constraint
552 * c' op
554 * is printed.
556 static __isl_give isl_printer *print_half_constraint(__isl_take isl_printer *p,
557 __isl_keep isl_space *space, __isl_keep isl_mat *div,
558 isl_int *c, int last, const char *op, int latex)
560 isl_int_set_si(c[last], 0);
561 p = print_affine(p, space, div, c);
563 p = isl_printer_print_str(p, " ");
564 p = isl_printer_print_str(p, op);
565 p = isl_printer_print_str(p, " ");
567 return p;
570 /* Print a constraint "c" to "p", with the variable names
571 * taken from "space" and the integer division definitions taken from "div".
572 * "last" is the position of the last non-zero coefficient, which is
573 * moreover assumed to be negative.
574 * Let c' be the result of zeroing out this coefficient, then
575 * the constraint is printed in the form
577 * -c[last] op c'
579 static __isl_give isl_printer *print_constraint(__isl_take isl_printer *p,
580 __isl_keep isl_space *space, __isl_keep isl_mat *div,
581 isl_int *c, int last, const char *op, int latex)
583 isl_int_abs(c[last], c[last]);
585 p = print_term(space, div, c[last], last, p, latex);
587 p = isl_printer_print_str(p, " ");
588 p = isl_printer_print_str(p, op);
589 p = isl_printer_print_str(p, " ");
591 isl_int_set_si(c[last], 0);
592 p = print_affine(p, space, div, c);
594 return p;
597 /* Given an integer division
599 * floor(f/m)
601 * at position "pos" in "div", print the corresponding modulo expression
603 * (f) mod m
605 * to "p". The variable names are taken from "space", while any
606 * nested integer division definitions are taken from "div".
608 static __isl_give isl_printer *print_mod(__isl_take isl_printer *p,
609 __isl_keep isl_space *space, __isl_keep isl_mat *div, int pos,
610 int latex)
612 if (!p || !div)
613 return isl_printer_free(p);
615 p = isl_printer_print_str(p, "(");
616 p = print_affine_of_len(space, div, p,
617 div->row[pos] + 1, div->n_col - 1);
618 p = isl_printer_print_str(p, ") ");
619 p = isl_printer_print_str(p, s_mod[latex]);
620 p = isl_printer_print_str(p, " ");
621 p = isl_printer_print_isl_int(p, div->row[pos][0]);
622 return p;
625 /* Can the equality constraints "c" be printed as a modulo constraint?
626 * In particular, is of the form
628 * f - a m floor(g/m) = 0,
630 * with c = -a m the coefficient at position "pos"?
631 * Return the position of the corresponding integer division if so.
632 * Return the number of integer divisions if not.
633 * Return -1 on error.
635 * Modulo constraints are currently not printed in C format.
636 * Other than that, "pos" needs to correspond to an integer division
637 * with explicit representation and "c" needs to be a multiple
638 * of the denominator of the integer division.
640 static isl_size print_as_modulo_pos(__isl_keep isl_printer *p,
641 __isl_keep isl_space *space, __isl_keep isl_mat *div, unsigned pos,
642 isl_int c)
644 isl_bool can_print;
645 isl_size n_div;
646 enum isl_dim_type type;
648 n_div = isl_mat_rows(div);
649 if (!p || !space || n_div < 0)
650 return isl_size_error;
651 if (p->output_format == ISL_FORMAT_C)
652 return n_div;
653 if (pos2type(space, &type, &pos) < 0)
654 return -1;
655 if (type != isl_dim_div)
656 return n_div;
657 can_print = can_print_div_expr(p, div, pos);
658 if (can_print < 0)
659 return isl_size_error;
660 if (!can_print)
661 return n_div;
662 if (!isl_int_is_divisible_by(c, div->row[pos][0]))
663 return n_div;
664 return pos;
667 /* Print equality constraint "c" to "p" as a modulo constraint,
668 * with the variable names taken from "space" and
669 * the integer division definitions taken from "div".
670 * "last" is the position of the last non-zero coefficient, which is
671 * moreover assumed to be negative and a multiple of the denominator
672 * of the corresponding integer division. "div_pos" is the corresponding
673 * position in the sequence of integer divisions.
675 * The equality is of the form
677 * f - a m floor(g/m) = 0.
679 * Print it as
681 * a (g mod m) = -f + a g
683 static __isl_give isl_printer *print_eq_mod_constraint(
684 __isl_take isl_printer *p, __isl_keep isl_space *space,
685 __isl_keep isl_mat *div, unsigned div_pos,
686 isl_int *c, int last, int latex)
688 isl_ctx *ctx;
689 int multiple;
691 ctx = isl_printer_get_ctx(p);
692 isl_int_divexact(c[last], c[last], div->row[div_pos][0]);
693 isl_int_abs(c[last], c[last]);
694 multiple = !isl_int_is_one(c[last]);
695 if (multiple) {
696 p = isl_printer_print_isl_int(p, c[last]);
697 p = isl_printer_print_str(p, "*(");
699 p = print_mod(p, space, div, div_pos, latex);
700 if (multiple)
701 p = isl_printer_print_str(p, ")");
702 p = isl_printer_print_str(p, " = ");
703 isl_seq_combine(c, ctx->negone, c,
704 c[last], div->row[div_pos] + 1, last);
705 isl_int_set_si(c[last], 0);
706 p = print_affine(p, space, div, c);
707 return p;
710 /* Print equality constraint "c" to "p", with the variable names
711 * taken from "space" and the integer division definitions taken from "div".
712 * "last" is the position of the last non-zero coefficient, which is
713 * moreover assumed to be negative.
715 * If possible, print the equality constraint as a modulo constraint.
717 static __isl_give isl_printer *print_eq_constraint(__isl_take isl_printer *p,
718 __isl_keep isl_space *space, __isl_keep isl_mat *div, isl_int *c,
719 int last, int latex)
721 isl_size n_div;
722 isl_size div_pos;
724 n_div = isl_mat_rows(div);
725 div_pos = print_as_modulo_pos(p, space, div, last, c[last]);
726 if (n_div < 0 || div_pos < 0)
727 return isl_printer_free(p);
728 if (div_pos < n_div)
729 return print_eq_mod_constraint(p, space, div, div_pos,
730 c, last, latex);
731 return print_constraint(p, space, div, c, last, "=", latex);
734 /* Print the constraints of "bmap" to "p".
735 * The names of the variables are taken from "space" and
736 * the integer division definitions are taken from "div".
737 * Div constraints are only printed in "dump" mode.
738 * The constraints are sorted prior to printing (except in "dump" mode).
740 * If x is the last variable with a non-zero coefficient,
741 * then a lower bound
743 * f - a x >= 0
745 * is printed as
747 * a x <= f
749 * while an upper bound
751 * f + a x >= 0
753 * is printed as
755 * a x >= -f
757 * If the next constraint has an opposite sign for the same last coefficient,
758 * then it is printed as
760 * f >= a x
762 * or
764 * -f <= a x
766 * instead. In fact, the "a x" part is not printed explicitly, but
767 * reused from the next constraint, which is therefore treated as
768 * a first constraint in the conjunction.
770 * If the constant term of "f" is -1, then "f" is replaced by "f + 1" and
771 * the comparison operator is replaced by the strict variant.
772 * Essentially, ">= 1" is replaced by "> 0".
774 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
775 __isl_keep isl_space *space, __isl_keep isl_mat *div,
776 __isl_take isl_printer *p, int latex)
778 int i;
779 isl_vec *c = NULL;
780 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
781 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
782 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
783 int first = 1;
784 int dump;
786 if (total < 0 || !p)
787 return isl_printer_free(p);
788 bmap = isl_basic_map_copy(bmap);
789 dump = p->dump;
790 if (!dump)
791 bmap = isl_basic_map_sort_constraints(bmap);
792 if (!bmap)
793 goto error;
795 c = isl_vec_alloc(bmap->ctx, 1 + total);
796 if (!c)
797 goto error;
799 for (i = bmap->n_eq - 1; i >= 0; --i) {
800 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
801 if (l < 0) {
802 if (i != bmap->n_eq - 1)
803 p = isl_printer_print_str(p, s_and[latex]);
804 p = isl_printer_print_str(p, "0 = 0");
805 continue;
807 if (!first)
808 p = isl_printer_print_str(p, s_and[latex]);
809 if (isl_int_is_neg(bmap->eq[i][l]))
810 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
811 else
812 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
813 p = print_eq_constraint(p, space, div, c->el, l, latex);
814 first = 0;
816 for (i = 0; i < bmap->n_ineq; ++i) {
817 isl_bool combine;
818 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
819 int strict;
820 int s;
821 const char *op;
822 if (l < 0)
823 continue;
824 if (!dump && l >= o_div &&
825 can_print_div_expr(p, div, l - o_div)) {
826 isl_bool is_div;
827 is_div = isl_basic_map_is_div_constraint(bmap,
828 bmap->ineq[i], l - o_div);
829 if (is_div < 0)
830 goto error;
831 if (is_div)
832 continue;
834 if (!first)
835 p = isl_printer_print_str(p, s_and[latex]);
836 s = isl_int_sgn(bmap->ineq[i][l]);
837 strict = !rational && isl_int_is_negone(bmap->ineq[i][0]);
838 if (s < 0)
839 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
840 else
841 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
842 if (strict)
843 isl_int_set_si(c->el[0], 0);
844 combine = dump ? isl_bool_false : next_is_opposite(bmap, i, l);
845 if (combine < 0)
846 goto error;
847 if (combine) {
848 op = constraint_op(-s, strict, latex);
849 p = print_half_constraint(p, space, div, c->el, l,
850 op, latex);
851 first = 1;
852 } else {
853 op = constraint_op(s, strict, latex);
854 p = print_constraint(p, space, div, c->el, l,
855 op, latex);
856 first = 0;
860 isl_basic_map_free(bmap);
861 isl_vec_free(c);
863 return p;
864 error:
865 isl_basic_map_free(bmap);
866 isl_vec_free(c);
867 isl_printer_free(p);
868 return NULL;
871 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
872 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
874 int c;
876 if (!p || !div)
877 return isl_printer_free(p);
879 c = p->output_format == ISL_FORMAT_C;
880 p = isl_printer_print_str(p, c ? "floord(" : "floor((");
881 p = print_affine_of_len(dim, div, p,
882 div->row[pos] + 1, div->n_col - 1);
883 p = isl_printer_print_str(p, c ? ", " : ")/");
884 p = isl_printer_print_isl_int(p, div->row[pos][0]);
885 p = isl_printer_print_str(p, ")");
886 return p;
889 /* Print a comma separated list of div names, except those that have
890 * a definition that can be printed.
891 * If "print_defined_divs" is set, then those div names are printed
892 * as well, along with their definitions.
894 static __isl_give isl_printer *print_div_list(__isl_take isl_printer *p,
895 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex,
896 int print_defined_divs)
898 int i;
899 int first = 1;
900 isl_size n_div;
902 n_div = isl_mat_rows(div);
903 if (!p || !space || n_div < 0)
904 return isl_printer_free(p);
906 for (i = 0; i < n_div; ++i) {
907 if (!print_defined_divs && can_print_div_expr(p, div, i))
908 continue;
909 if (!first)
910 p = isl_printer_print_str(p, ", ");
911 p = print_name(space, p, isl_dim_div, i, latex);
912 first = 0;
913 if (!can_print_div_expr(p, div, i))
914 continue;
915 p = isl_printer_print_str(p, " = ");
916 p = print_div(space, div, i, p);
919 return p;
922 /* Does printing an object with local variables described by "div"
923 * require an "exists" clause?
924 * That is, are there any local variables without an explicit representation?
925 * An exists clause is also needed in "dump" mode because
926 * explicit div representations are not printed inline in that case.
928 static isl_bool need_exists(__isl_keep isl_printer *p, __isl_keep isl_mat *div)
930 int i;
931 isl_size n;
933 n = isl_mat_rows(div);
934 if (!p || n < 0)
935 return isl_bool_error;
936 if (n == 0)
937 return isl_bool_false;
938 if (p->dump)
939 return isl_bool_true;
940 for (i = 0; i < n; ++i)
941 if (!can_print_div_expr(p, div, i))
942 return isl_bool_true;
943 return isl_bool_false;
946 /* Print the start of an exists clause, i.e.,
948 * (exists variables:
950 * In dump mode, local variables with an explicit definition are printed
951 * as well because they will not be printed inline.
953 static __isl_give isl_printer *open_exists(__isl_take isl_printer *p,
954 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex)
956 int dump;
958 if (!p)
959 return NULL;
961 dump = p->dump;
962 p = isl_printer_print_str(p, s_open_exists[latex]);
963 p = print_div_list(p, space, div, latex, dump);
964 p = isl_printer_print_str(p, ": ");
966 return p;
969 /* Remove the explicit representations of all local variables in "div".
971 static __isl_give isl_mat *mark_all_unknown(__isl_take isl_mat *div)
973 int i;
974 isl_size n_div;
976 n_div = isl_mat_rows(div);
977 if (n_div < 0)
978 return isl_mat_free(div);
980 for (i = 0; i < n_div; ++i)
981 div = isl_mat_set_element_si(div, i, 0, 0);
982 return div;
985 /* Print the constraints of "bmap" to "p".
986 * The names of the variables are taken from "space".
987 * "latex" is set if the constraints should be printed in LaTeX format.
988 * Do not print inline explicit div representations in "dump" mode.
990 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
991 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
993 int dump;
994 isl_mat *div;
995 isl_bool exists;
997 if (!p)
998 return NULL;
999 dump = p->dump;
1000 div = isl_basic_map_get_divs(bmap);
1001 exists = need_exists(p, div);
1002 if (exists >= 0 && exists)
1003 p = open_exists(p, space, div, latex);
1005 if (dump)
1006 div = mark_all_unknown(div);
1007 p = print_constraints(bmap, space, div, p, latex);
1008 isl_mat_free(div);
1010 if (exists >= 0 && exists)
1011 p = isl_printer_print_str(p, s_close_exists[latex]);
1012 return p;
1015 /* Print a colon followed by the constraints of "bmap"
1016 * to "p", provided there are any constraints.
1017 * The names of the variables are taken from "space".
1018 * "latex" is set if the constraints should be printed in LaTeX format.
1020 static __isl_give isl_printer *print_optional_disjunct(
1021 __isl_keep isl_basic_map *bmap, __isl_keep isl_space *space,
1022 __isl_take isl_printer *p, int latex)
1024 if (isl_basic_map_plain_is_universe(bmap))
1025 return p;
1027 p = isl_printer_print_str(p, ": ");
1028 p = print_disjunct(bmap, space, p, latex);
1030 return p;
1033 static __isl_give isl_printer *basic_map_print_omega(
1034 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
1036 p = isl_printer_print_str(p, "{ [");
1037 p = print_var_list(p, bmap->dim, isl_dim_in);
1038 p = isl_printer_print_str(p, "] -> [");
1039 p = print_var_list(p, bmap->dim, isl_dim_out);
1040 p = isl_printer_print_str(p, "] ");
1041 p = print_optional_disjunct(bmap, bmap->dim, p, 0);
1042 p = isl_printer_print_str(p, " }");
1043 return p;
1046 static __isl_give isl_printer *basic_set_print_omega(
1047 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
1049 p = isl_printer_print_str(p, "{ [");
1050 p = print_var_list(p, bset->dim, isl_dim_set);
1051 p = isl_printer_print_str(p, "] ");
1052 p = print_optional_disjunct(bset, bset->dim, p, 0);
1053 p = isl_printer_print_str(p, " }");
1054 return p;
1057 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
1058 __isl_take isl_printer *p)
1060 int i;
1062 for (i = 0; i < map->n; ++i) {
1063 if (i)
1064 p = isl_printer_print_str(p, " union ");
1065 p = basic_map_print_omega(map->p[i], p);
1067 return p;
1070 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
1071 __isl_take isl_printer *p)
1073 int i;
1075 for (i = 0; i < set->n; ++i) {
1076 if (i)
1077 p = isl_printer_print_str(p, " union ");
1078 p = basic_set_print_omega(set->p[i], p);
1080 return p;
1083 /* Print the list of parameters in "space", followed by an arrow, to "p",
1084 * if there are any parameters.
1086 static __isl_give isl_printer *print_param_tuple(__isl_take isl_printer *p,
1087 __isl_keep isl_space *space, struct isl_print_space_data *data)
1089 isl_size nparam;
1091 nparam = isl_space_dim(space, isl_dim_param);
1092 if (!p || nparam < 0)
1093 return isl_printer_free(p);
1094 if (nparam == 0)
1095 return p;
1097 p = print_tuple(space, p, isl_dim_param, data);
1098 p = isl_printer_print_str(p, s_to[data->latex]);
1100 return p;
1103 static __isl_give isl_printer *isl_basic_map_print_isl(
1104 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
1105 int latex)
1107 struct isl_print_space_data data = { .latex = latex };
1108 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1110 p = print_param_tuple(p, bmap->dim, &data);
1111 p = isl_printer_print_str(p, "{ ");
1112 p = isl_print_space(bmap->dim, p, rational, &data);
1113 p = isl_printer_print_str(p, " : ");
1114 p = print_disjunct(bmap, bmap->dim, p, latex);
1115 p = isl_printer_print_str(p, " }");
1116 return p;
1119 /* Print the disjuncts of a map (or set) "map" to "p".
1120 * The names of the variables are taken from "space".
1121 * "latex" is set if the constraints should be printed in LaTeX format.
1123 static __isl_give isl_printer *print_disjuncts_core(__isl_keep isl_map *map,
1124 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1126 int i;
1128 if (map->n == 0)
1129 p = isl_printer_print_str(p, "false");
1130 for (i = 0; i < map->n; ++i) {
1131 if (i)
1132 p = isl_printer_print_str(p, s_or[latex]);
1133 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
1134 p = isl_printer_print_str(p, "(");
1135 p = print_disjunct(map->p[i], space, p, latex);
1136 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
1137 p = isl_printer_print_str(p, ")");
1139 return p;
1142 /* Print the disjuncts of a map (or set) "map" to "p".
1143 * The names of the variables are taken from "space".
1144 * "hull" describes constraints shared by all disjuncts of "map".
1145 * "latex" is set if the constraints should be printed in LaTeX format.
1147 * Print the disjuncts as a conjunction of "hull" and
1148 * the result of removing the constraints of "hull" from "map".
1149 * If this result turns out to be the universe, then simply print "hull".
1151 static __isl_give isl_printer *print_disjuncts_in_hull(__isl_keep isl_map *map,
1152 __isl_keep isl_space *space, __isl_take isl_basic_map *hull,
1153 __isl_take isl_printer *p, int latex)
1155 isl_bool is_universe;
1157 p = print_disjunct(hull, space, p, latex);
1158 map = isl_map_plain_gist_basic_map(isl_map_copy(map), hull);
1159 is_universe = isl_map_plain_is_universe(map);
1160 if (is_universe < 0)
1161 goto error;
1162 if (!is_universe) {
1163 p = isl_printer_print_str(p, s_and[latex]);
1164 p = isl_printer_print_str(p, "(");
1165 p = print_disjuncts_core(map, space, p, latex);
1166 p = isl_printer_print_str(p, ")");
1168 isl_map_free(map);
1170 return p;
1171 error:
1172 isl_map_free(map);
1173 isl_printer_free(p);
1174 return NULL;
1177 /* Print the disjuncts of a map (or set) "map" to "p".
1178 * The names of the variables are taken from "space".
1179 * "latex" is set if the constraints should be printed in LaTeX format.
1181 * If there are at least two disjuncts and "dump" mode is not turned out,
1182 * check for any shared constraints among all disjuncts.
1183 * If there are any, then print them separately in print_disjuncts_in_hull.
1185 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
1186 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1188 if (isl_map_plain_is_universe(map))
1189 return p;
1191 p = isl_printer_print_str(p, s_such_that[latex]);
1192 if (!p)
1193 return NULL;
1195 if (!p->dump && map->n >= 2) {
1196 isl_basic_map *hull;
1197 isl_bool is_universe;
1199 hull = isl_map_plain_unshifted_simple_hull(isl_map_copy(map));
1200 is_universe = isl_basic_map_plain_is_universe(hull);
1201 if (is_universe < 0)
1202 p = isl_printer_free(p);
1203 else if (!is_universe)
1204 return print_disjuncts_in_hull(map, space, hull,
1205 p, latex);
1206 isl_basic_map_free(hull);
1209 return print_disjuncts_core(map, space, p, latex);
1212 /* Print the disjuncts of a map (or set).
1213 * The names of the variables are taken from "space".
1214 * "latex" is set if the constraints should be printed in LaTeX format.
1216 * If the map turns out to be a universal parameter domain, then
1217 * we need to print the colon. Otherwise, the output looks identical
1218 * to the empty set.
1220 static __isl_give isl_printer *print_disjuncts_map(__isl_keep isl_map *map,
1221 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1223 if (isl_map_plain_is_universe(map) && isl_space_is_params(map->dim))
1224 return isl_printer_print_str(p, s_such_that[latex]);
1225 else
1226 return print_disjuncts(map, space, p, latex);
1229 /* Print the disjuncts of a set.
1230 * The names of the variables are taken from "space".
1231 * "latex" is set if the constraints should be printed in LaTeX format.
1233 static __isl_give isl_printer *print_disjuncts_set(__isl_keep isl_set *set,
1234 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1236 return print_disjuncts_map(set_to_map(set), space, p, latex);
1239 struct isl_aff_split {
1240 isl_basic_map *aff;
1241 isl_map *map;
1244 static void free_split(__isl_take struct isl_aff_split *split, int n)
1246 int i;
1248 if (!split)
1249 return;
1251 for (i = 0; i < n; ++i) {
1252 isl_basic_map_free(split[i].aff);
1253 isl_map_free(split[i].map);
1256 free(split);
1259 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
1261 int i, j;
1262 isl_size nparam, n_in, n_out, total;
1264 bmap = isl_basic_map_cow(bmap);
1265 if (!bmap)
1266 return NULL;
1267 bmap = isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1269 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1270 n_in = isl_basic_map_dim(bmap, isl_dim_in);
1271 n_out = isl_basic_map_dim(bmap, isl_dim_out);
1272 total = isl_basic_map_dim(bmap, isl_dim_all);
1273 if (n_in < 0 || n_out < 0 || nparam < 0 || total < 0)
1274 return isl_basic_map_free(bmap);
1276 for (i = bmap->n_eq - 1; i >= 0; --i) {
1277 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
1278 if (j >= nparam && j < nparam + n_in + n_out &&
1279 (isl_int_is_one(bmap->eq[i][1 + j]) ||
1280 isl_int_is_negone(bmap->eq[i][1 + j])))
1281 continue;
1282 if (isl_basic_map_drop_equality(bmap, i) < 0)
1283 goto error;
1286 bmap = isl_basic_map_finalize(bmap);
1288 return bmap;
1289 error:
1290 isl_basic_map_free(bmap);
1291 return NULL;
1294 static int aff_split_cmp(const void *p1, const void *p2, void *user)
1296 const struct isl_aff_split *s1, *s2;
1297 s1 = (const struct isl_aff_split *) p1;
1298 s2 = (const struct isl_aff_split *) p2;
1300 return isl_basic_map_plain_cmp(s1->aff, s2->aff);
1303 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
1304 __isl_keep isl_basic_map *aff)
1306 int i, j;
1307 isl_size v_div;
1309 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1310 if (v_div < 0 || !aff)
1311 goto error;
1313 for (i = bmap->n_eq - 1; i >= 0; --i) {
1314 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + v_div,
1315 bmap->n_div) != -1)
1316 continue;
1317 for (j = 0; j < aff->n_eq; ++j) {
1318 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + v_div) &&
1319 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + v_div))
1320 continue;
1321 if (isl_basic_map_drop_equality(bmap, i) < 0)
1322 goto error;
1323 break;
1327 return bmap;
1328 error:
1329 isl_basic_map_free(bmap);
1330 return NULL;
1333 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
1335 int i, n;
1336 struct isl_aff_split *split;
1337 isl_ctx *ctx;
1339 ctx = isl_map_get_ctx(map);
1340 split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
1341 if (!split)
1342 return NULL;
1344 for (i = 0; i < map->n; ++i) {
1345 isl_basic_map *bmap;
1346 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
1347 bmap = isl_basic_map_copy(map->p[i]);
1348 bmap = isl_basic_map_cow(bmap);
1349 bmap = drop_aff(bmap, split[i].aff);
1350 split[i].map = isl_map_from_basic_map(bmap);
1351 if (!split[i].aff || !split[i].map)
1352 goto error;
1355 if (isl_sort(split, map->n, sizeof(struct isl_aff_split),
1356 &aff_split_cmp, NULL) < 0)
1357 goto error;
1359 n = map->n;
1360 for (i = n - 1; i >= 1; --i) {
1361 if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
1362 split[i].aff))
1363 continue;
1364 isl_basic_map_free(split[i].aff);
1365 split[i - 1].map = isl_map_union(split[i - 1].map,
1366 split[i].map);
1367 if (i != n - 1)
1368 split[i] = split[n - 1];
1369 split[n - 1].aff = NULL;
1370 split[n - 1].map = NULL;
1371 --n;
1374 return split;
1375 error:
1376 free_split(split, map->n);
1377 return NULL;
1380 static int defining_equality(__isl_keep isl_basic_map *eq,
1381 __isl_keep isl_space *dim, enum isl_dim_type type, int pos)
1383 int i;
1384 isl_size total;
1386 total = isl_basic_map_dim(eq, isl_dim_all);
1387 if (total < 0)
1388 return -1;
1390 pos += isl_space_offset(dim, type);
1392 for (i = 0; i < eq->n_eq; ++i) {
1393 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
1394 continue;
1395 if (isl_int_is_one(eq->eq[i][1 + pos]))
1396 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
1397 return i;
1400 return -1;
1403 /* Print dimension "pos" of data->space to "p".
1405 * data->user is assumed to be an isl_basic_map keeping track of equalities.
1407 * If the current dimension is defined by these equalities, then print
1408 * the corresponding expression, assigned to the name of the dimension
1409 * if there is any. Otherwise, print the name of the dimension.
1411 static __isl_give isl_printer *print_dim_eq(__isl_take isl_printer *p,
1412 struct isl_print_space_data *data, unsigned pos)
1414 isl_basic_map *eq = data->user;
1415 int j;
1417 j = defining_equality(eq, data->space, data->type, pos);
1418 if (j >= 0) {
1419 if (isl_space_has_dim_name(data->space, data->type, pos)) {
1420 p = print_name(data->space, p, data->type, pos,
1421 data->latex);
1422 p = isl_printer_print_str(p, " = ");
1424 pos += 1 + isl_space_offset(data->space, data->type);
1425 p = print_affine_of_len(data->space, NULL, p, eq->eq[j], pos);
1426 } else {
1427 p = print_name(data->space, p, data->type, pos, data->latex);
1430 return p;
1433 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
1434 struct isl_aff_split *split, int n, __isl_keep isl_space *space)
1436 struct isl_print_space_data data = { 0 };
1437 int i;
1438 int rational;
1440 data.print_dim = &print_dim_eq;
1441 for (i = 0; i < n; ++i) {
1442 if (!split[i].map)
1443 break;
1444 rational = split[i].map->n > 0 &&
1445 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
1446 if (i)
1447 p = isl_printer_print_str(p, "; ");
1448 data.user = split[i].aff;
1449 p = isl_print_space(space, p, rational, &data);
1450 p = print_disjuncts_map(split[i].map, space, p, 0);
1453 return p;
1456 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
1457 __isl_take isl_printer *p)
1459 struct isl_print_space_data data = { 0 };
1460 struct isl_aff_split *split = NULL;
1461 int rational;
1463 if (!p || !map)
1464 return isl_printer_free(p);
1465 if (!p->dump && map->n > 0)
1466 split = split_aff(map);
1467 if (split) {
1468 p = print_split_map(p, split, map->n, map->dim);
1469 } else {
1470 rational = map->n > 0 &&
1471 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
1472 p = isl_print_space(map->dim, p, rational, &data);
1473 p = print_disjuncts_map(map, map->dim, p, 0);
1475 free_split(split, map->n);
1476 return p;
1479 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
1480 __isl_take isl_printer *p)
1482 struct isl_print_space_data data = { 0 };
1484 p = print_param_tuple(p, map->dim, &data);
1485 p = isl_printer_print_str(p, s_open_set[0]);
1486 p = isl_map_print_isl_body(map, p);
1487 p = isl_printer_print_str(p, s_close_set[0]);
1488 return p;
1491 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
1492 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff)
1494 struct isl_print_space_data data = { 0 };
1496 data.latex = 1;
1497 p = print_param_tuple(p, map->dim, &data);
1498 p = isl_printer_print_str(p, s_open_set[1]);
1499 data.print_dim = &print_dim_eq;
1500 data.user = aff;
1501 p = isl_print_space(map->dim, p, 0, &data);
1502 p = print_disjuncts_map(map, map->dim, p, 1);
1503 p = isl_printer_print_str(p, s_close_set[1]);
1505 return p;
1508 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
1509 __isl_take isl_printer *p)
1511 int i;
1512 struct isl_aff_split *split = NULL;
1514 if (map->n > 0)
1515 split = split_aff(map);
1517 if (!split)
1518 return print_latex_map(map, p, NULL);
1520 for (i = 0; i < map->n; ++i) {
1521 if (!split[i].map)
1522 break;
1523 if (i)
1524 p = isl_printer_print_str(p, " \\cup ");
1525 p = print_latex_map(split[i].map, p, split[i].aff);
1528 free_split(split, map->n);
1529 return p;
1532 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
1533 __isl_keep isl_basic_map *bmap)
1535 if (!p || !bmap)
1536 goto error;
1537 if (p->output_format == ISL_FORMAT_ISL)
1538 return isl_basic_map_print_isl(bmap, p, 0);
1539 else if (p->output_format == ISL_FORMAT_OMEGA)
1540 return basic_map_print_omega(bmap, p);
1541 isl_assert(bmap->ctx, 0, goto error);
1542 error:
1543 isl_printer_free(p);
1544 return NULL;
1547 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
1548 __isl_keep isl_basic_set *bset)
1550 if (!p || !bset)
1551 goto error;
1553 if (p->output_format == ISL_FORMAT_ISL)
1554 return isl_basic_map_print_isl(bset, p, 0);
1555 else if (p->output_format == ISL_FORMAT_POLYLIB)
1556 return isl_basic_set_print_polylib(bset, p, 0);
1557 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1558 return isl_basic_set_print_polylib(bset, p, 1);
1559 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
1560 return bset_print_constraints_polylib(bset, p);
1561 else if (p->output_format == ISL_FORMAT_OMEGA)
1562 return basic_set_print_omega(bset, p);
1563 isl_assert(p->ctx, 0, goto error);
1564 error:
1565 isl_printer_free(p);
1566 return NULL;
1569 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
1570 __isl_keep isl_set *set)
1572 if (!p || !set)
1573 goto error;
1574 if (p->output_format == ISL_FORMAT_ISL)
1575 return isl_map_print_isl(set_to_map(set), p);
1576 else if (p->output_format == ISL_FORMAT_POLYLIB)
1577 return isl_set_print_polylib(set, p, 0);
1578 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1579 return isl_set_print_polylib(set, p, 1);
1580 else if (p->output_format == ISL_FORMAT_OMEGA)
1581 return isl_set_print_omega(set, p);
1582 else if (p->output_format == ISL_FORMAT_LATEX)
1583 return isl_map_print_latex(set_to_map(set), p);
1584 isl_assert(set->ctx, 0, goto error);
1585 error:
1586 isl_printer_free(p);
1587 return NULL;
1590 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1591 __isl_keep isl_map *map)
1593 if (!p || !map)
1594 goto error;
1596 if (p->output_format == ISL_FORMAT_ISL)
1597 return isl_map_print_isl(map, p);
1598 else if (p->output_format == ISL_FORMAT_POLYLIB)
1599 return isl_map_print_polylib(map, p, 0);
1600 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1601 return isl_map_print_polylib(map, p, 1);
1602 else if (p->output_format == ISL_FORMAT_OMEGA)
1603 return isl_map_print_omega(map, p);
1604 else if (p->output_format == ISL_FORMAT_LATEX)
1605 return isl_map_print_latex(map, p);
1606 isl_assert(map->ctx, 0, goto error);
1607 error:
1608 isl_printer_free(p);
1609 return NULL;
1612 struct isl_union_print_data {
1613 isl_printer *p;
1614 int first;
1617 static isl_stat print_map_body(__isl_take isl_map *map, void *user)
1619 struct isl_union_print_data *data;
1620 data = (struct isl_union_print_data *)user;
1622 if (!data->first)
1623 data->p = isl_printer_print_str(data->p, "; ");
1624 data->first = 0;
1626 data->p = isl_map_print_isl_body(map, data->p);
1627 isl_map_free(map);
1629 return isl_stat_ok;
1632 /* Print the body of "umap" (everything except the parameter declarations)
1633 * to "p" in isl format.
1635 static __isl_give isl_printer *isl_printer_print_union_map_isl_body(
1636 __isl_take isl_printer *p, __isl_keep isl_union_map *umap)
1638 struct isl_union_print_data data;
1640 p = isl_printer_print_str(p, s_open_set[0]);
1641 data.p = p;
1642 data.first = 1;
1643 isl_union_map_foreach_map(umap, &print_map_body, &data);
1644 p = data.p;
1645 p = isl_printer_print_str(p, s_close_set[0]);
1646 return p;
1649 /* Print the body of "uset" (everything except the parameter declarations)
1650 * to "p" in isl format.
1652 static __isl_give isl_printer *isl_printer_print_union_set_isl_body(
1653 __isl_take isl_printer *p, __isl_keep isl_union_set *uset)
1655 return isl_printer_print_union_map_isl_body(p, uset_to_umap(uset));
1658 /* Print the isl_union_map "umap" to "p" in isl format.
1660 static __isl_give isl_printer *isl_union_map_print_isl(
1661 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1663 struct isl_print_space_data space_data = { 0 };
1664 isl_space *space;
1666 space = isl_union_map_get_space(umap);
1667 p = print_param_tuple(p, space, &space_data);
1668 isl_space_free(space);
1670 p = isl_printer_print_union_map_isl_body(p, umap);
1672 return p;
1675 static isl_stat print_latex_map_body(__isl_take isl_map *map, void *user)
1677 struct isl_union_print_data *data;
1678 data = (struct isl_union_print_data *)user;
1680 if (!data->first)
1681 data->p = isl_printer_print_str(data->p, " \\cup ");
1682 data->first = 0;
1684 data->p = isl_map_print_latex(map, data->p);
1685 isl_map_free(map);
1687 return isl_stat_ok;
1690 static __isl_give isl_printer *isl_union_map_print_latex(
1691 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1693 struct isl_union_print_data data = { p, 1 };
1694 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1695 p = data.p;
1696 return p;
1699 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1700 __isl_keep isl_union_map *umap)
1702 if (!p || !umap)
1703 goto error;
1705 if (p->output_format == ISL_FORMAT_ISL)
1706 return isl_union_map_print_isl(umap, p);
1707 if (p->output_format == ISL_FORMAT_LATEX)
1708 return isl_union_map_print_latex(umap, p);
1710 isl_die(p->ctx, isl_error_invalid,
1711 "invalid output format for isl_union_map", goto error);
1712 error:
1713 isl_printer_free(p);
1714 return NULL;
1717 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1718 __isl_keep isl_union_set *uset)
1720 if (!p || !uset)
1721 goto error;
1723 if (p->output_format == ISL_FORMAT_ISL)
1724 return isl_union_map_print_isl(uset_to_umap(uset), p);
1725 if (p->output_format == ISL_FORMAT_LATEX)
1726 return isl_union_map_print_latex(uset_to_umap(uset), p);
1728 isl_die(p->ctx, isl_error_invalid,
1729 "invalid output format for isl_union_set", goto error);
1730 error:
1731 isl_printer_free(p);
1732 return NULL;
1735 static isl_size poly_rec_n_non_zero(__isl_keep isl_poly_rec *rec)
1737 int i;
1738 int n;
1740 if (!rec)
1741 return isl_size_error;
1743 for (i = 0, n = 0; i < rec->n; ++i) {
1744 isl_bool is_zero = isl_poly_is_zero(rec->p[i]);
1746 if (is_zero < 0)
1747 return isl_size_error;
1748 if (!is_zero)
1749 ++n;
1752 return n;
1755 static __isl_give isl_printer *poly_print_cst(__isl_keep isl_poly *poly,
1756 __isl_take isl_printer *p, int first)
1758 isl_poly_cst *cst;
1759 int neg;
1761 cst = isl_poly_as_cst(poly);
1762 if (!cst)
1763 goto error;
1764 neg = !first && isl_int_is_neg(cst->n);
1765 if (!first)
1766 p = isl_printer_print_str(p, neg ? " - " : " + ");
1767 if (neg)
1768 isl_int_neg(cst->n, cst->n);
1769 if (isl_int_is_zero(cst->d)) {
1770 int sgn = isl_int_sgn(cst->n);
1771 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1772 sgn == 0 ? "NaN" : "infty");
1773 } else
1774 p = isl_printer_print_isl_int(p, cst->n);
1775 if (neg)
1776 isl_int_neg(cst->n, cst->n);
1777 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1778 p = isl_printer_print_str(p, "/");
1779 p = isl_printer_print_isl_int(p, cst->d);
1781 return p;
1782 error:
1783 isl_printer_free(p);
1784 return NULL;
1787 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1788 __isl_keep isl_space *space, __isl_keep isl_mat *div, int var)
1790 isl_size total;
1792 total = isl_space_dim(space, isl_dim_all);
1793 if (total < 0)
1794 return isl_printer_free(p);
1795 if (var < total)
1796 p = print_term(space, NULL, space->ctx->one, 1 + var, p, 0);
1797 else
1798 p = print_div(space, div, var - total, p);
1799 return p;
1802 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1803 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var, int exp)
1805 p = print_base(p, dim, div, var);
1806 if (exp == 1)
1807 return p;
1808 if (p->output_format == ISL_FORMAT_C) {
1809 int i;
1810 for (i = 1; i < exp; ++i) {
1811 p = isl_printer_print_str(p, "*");
1812 p = print_base(p, dim, div, var);
1814 } else {
1815 p = isl_printer_print_str(p, "^");
1816 p = isl_printer_print_int(p, exp);
1818 return p;
1821 /* Print the polynomial "poly" defined over the domain space "space" and
1822 * local variables defined by "div" to "p".
1824 static __isl_give isl_printer *poly_print(__isl_keep isl_poly *poly,
1825 __isl_keep isl_space *space, __isl_keep isl_mat *div,
1826 __isl_take isl_printer *p)
1828 int i, first, print_parens;
1829 isl_size n;
1830 isl_bool is_cst;
1831 isl_poly_rec *rec;
1833 is_cst = isl_poly_is_cst(poly);
1834 if (!p || is_cst < 0 || !space || !div)
1835 goto error;
1837 if (is_cst)
1838 return poly_print_cst(poly, p, 1);
1840 rec = isl_poly_as_rec(poly);
1841 n = poly_rec_n_non_zero(rec);
1842 if (n < 0)
1843 return isl_printer_free(p);
1844 print_parens = n > 1;
1845 if (print_parens)
1846 p = isl_printer_print_str(p, "(");
1847 for (i = 0, first = 1; i < rec->n; ++i) {
1848 isl_bool is_zero = isl_poly_is_zero(rec->p[i]);
1849 isl_bool is_one = isl_poly_is_one(rec->p[i]);
1850 isl_bool is_negone = isl_poly_is_negone(rec->p[i]);
1851 isl_bool is_cst = isl_poly_is_cst(rec->p[i]);
1853 if (is_zero < 0 || is_one < 0 || is_negone < 0)
1854 return isl_printer_free(p);
1855 if (is_zero)
1856 continue;
1857 if (is_negone) {
1858 if (!i)
1859 p = isl_printer_print_str(p, "-1");
1860 else if (first)
1861 p = isl_printer_print_str(p, "-");
1862 else
1863 p = isl_printer_print_str(p, " - ");
1864 } else if (is_cst && !is_one)
1865 p = poly_print_cst(rec->p[i], p, first);
1866 else {
1867 if (!first)
1868 p = isl_printer_print_str(p, " + ");
1869 if (i == 0 || !is_one)
1870 p = poly_print(rec->p[i], space, div, p);
1872 first = 0;
1873 if (i == 0)
1874 continue;
1875 if (!is_one && !is_negone)
1876 p = isl_printer_print_str(p, " * ");
1877 p = print_pow(p, space, div, rec->poly.var, i);
1879 if (print_parens)
1880 p = isl_printer_print_str(p, ")");
1881 return p;
1882 error:
1883 isl_printer_free(p);
1884 return NULL;
1887 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1888 __isl_keep isl_qpolynomial *qp)
1890 if (!p || !qp)
1891 goto error;
1892 p = poly_print(qp->poly, qp->dim, qp->div, p);
1893 return p;
1894 error:
1895 isl_printer_free(p);
1896 return NULL;
1899 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1900 __isl_keep isl_qpolynomial *qp)
1902 struct isl_print_space_data data = { 0 };
1904 if (!p || !qp)
1905 goto error;
1907 p = print_param_tuple(p, qp->dim, &data);
1908 p = isl_printer_print_str(p, "{ ");
1909 if (!isl_space_is_params(qp->dim)) {
1910 p = isl_print_space(qp->dim, p, 0, &data);
1911 p = isl_printer_print_str(p, " -> ");
1913 p = print_qpolynomial(p, qp);
1914 p = isl_printer_print_str(p, " }");
1915 return p;
1916 error:
1917 isl_printer_free(p);
1918 return NULL;
1921 /* Print the quasi-polynomial "qp" to "p" in C format, with the variable names
1922 * taken from the domain space "space".
1924 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1925 __isl_keep isl_space *space, __isl_keep isl_qpolynomial *qp)
1927 isl_bool is_one;
1928 isl_val *den;
1930 den = isl_qpolynomial_get_den(qp);
1931 qp = isl_qpolynomial_copy(qp);
1932 qp = isl_qpolynomial_scale_val(qp, isl_val_copy(den));
1933 is_one = isl_val_is_one(den);
1934 if (is_one < 0)
1935 p = isl_printer_free(p);
1936 if (!is_one)
1937 p = isl_printer_print_str(p, "(");
1938 if (qp)
1939 p = poly_print(qp->poly, space, qp->div, p);
1940 else
1941 p = isl_printer_free(p);
1942 if (!is_one) {
1943 p = isl_printer_print_str(p, ")/");
1944 p = isl_printer_print_val(p, den);
1946 isl_qpolynomial_free(qp);
1947 isl_val_free(den);
1948 return p;
1951 __isl_give isl_printer *isl_printer_print_qpolynomial(
1952 __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1954 if (!p || !qp)
1955 goto error;
1957 if (p->output_format == ISL_FORMAT_ISL)
1958 return print_qpolynomial_isl(p, qp);
1959 else if (p->output_format == ISL_FORMAT_C)
1960 return print_qpolynomial_c(p, qp->dim, qp);
1961 else
1962 isl_die(qp->dim->ctx, isl_error_unsupported,
1963 "output format not supported for isl_qpolynomials",
1964 goto error);
1965 error:
1966 isl_printer_free(p);
1967 return NULL;
1970 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1971 unsigned output_format)
1973 isl_printer *p;
1975 if (!qp)
1976 return;
1978 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1979 p = isl_printer_to_file(qp->dim->ctx, out);
1980 p = isl_printer_print_qpolynomial(p, qp);
1981 isl_printer_free(p);
1984 static __isl_give isl_printer *qpolynomial_fold_print(
1985 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1987 int i;
1989 if (fold->type == isl_fold_min)
1990 p = isl_printer_print_str(p, "min");
1991 else if (fold->type == isl_fold_max)
1992 p = isl_printer_print_str(p, "max");
1993 p = isl_printer_print_str(p, "(");
1994 for (i = 0; i < fold->n; ++i) {
1995 if (i)
1996 p = isl_printer_print_str(p, ", ");
1997 p = print_qpolynomial(p, fold->qp[i]);
1999 p = isl_printer_print_str(p, ")");
2000 return p;
2003 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
2004 FILE *out, unsigned output_format)
2006 isl_printer *p;
2008 if (!fold)
2009 return;
2011 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
2013 p = isl_printer_to_file(fold->dim->ctx, out);
2014 p = isl_printer_print_qpolynomial_fold(p, fold);
2016 isl_printer_free(p);
2019 static __isl_give isl_printer *isl_pwqp_print_isl_body(
2020 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2022 struct isl_print_space_data data = { 0 };
2023 int i = 0;
2025 for (i = 0; i < pwqp->n; ++i) {
2026 isl_space *space;
2028 if (i)
2029 p = isl_printer_print_str(p, "; ");
2030 space = isl_qpolynomial_get_domain_space(pwqp->p[i].qp);
2031 if (!isl_space_is_params(space)) {
2032 p = isl_print_space(space, p, 0, &data);
2033 p = isl_printer_print_str(p, " -> ");
2035 p = print_qpolynomial(p, pwqp->p[i].qp);
2036 p = print_disjuncts(set_to_map(pwqp->p[i].set), space, p, 0);
2037 isl_space_free(space);
2040 return p;
2043 static __isl_give isl_printer *print_pw_qpolynomial_isl(
2044 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2046 struct isl_print_space_data data = { 0 };
2048 if (!p || !pwqp)
2049 goto error;
2051 p = print_param_tuple(p, pwqp->dim, &data);
2052 p = isl_printer_print_str(p, "{ ");
2053 if (pwqp->n == 0) {
2054 if (!isl_space_is_set(pwqp->dim)) {
2055 p = print_tuple(pwqp->dim, p, isl_dim_in, &data);
2056 p = isl_printer_print_str(p, " -> ");
2058 p = isl_printer_print_str(p, "0");
2060 p = isl_pwqp_print_isl_body(p, pwqp);
2061 p = isl_printer_print_str(p, " }");
2062 return p;
2063 error:
2064 isl_printer_free(p);
2065 return NULL;
2068 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
2069 unsigned output_format)
2071 isl_printer *p;
2073 if (!pwqp)
2074 return;
2076 p = isl_printer_to_file(pwqp->dim->ctx, out);
2077 p = isl_printer_set_output_format(p, output_format);
2078 p = isl_printer_print_pw_qpolynomial(p, pwqp);
2080 isl_printer_free(p);
2083 static __isl_give isl_printer *isl_pwf_print_isl_body(
2084 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2086 struct isl_print_space_data data = { 0 };
2087 int i = 0;
2089 for (i = 0; i < pwf->n; ++i) {
2090 isl_space *space;
2092 if (i)
2093 p = isl_printer_print_str(p, "; ");
2094 space = isl_qpolynomial_fold_get_domain_space(pwf->p[i].fold);
2095 if (!isl_space_is_params(space)) {
2096 p = isl_print_space(space, p, 0, &data);
2097 p = isl_printer_print_str(p, " -> ");
2099 p = qpolynomial_fold_print(pwf->p[i].fold, p);
2100 p = print_disjuncts(set_to_map(pwf->p[i].set), space, p, 0);
2101 isl_space_free(space);
2104 return p;
2107 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
2108 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2110 struct isl_print_space_data data = { 0 };
2112 p = print_param_tuple(p, pwf->dim, &data);
2113 p = isl_printer_print_str(p, "{ ");
2114 if (pwf->n == 0) {
2115 if (!isl_space_is_set(pwf->dim)) {
2116 p = print_tuple(pwf->dim, p, isl_dim_in, &data);
2117 p = isl_printer_print_str(p, " -> ");
2119 p = isl_printer_print_str(p, "0");
2121 p = isl_pwf_print_isl_body(p, pwf);
2122 p = isl_printer_print_str(p, " }");
2123 return p;
2126 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2127 __isl_keep isl_local_space *ls, isl_int *c);
2129 /* We skip the constraint if it is implied by the div expression.
2131 * *first indicates whether this is the first constraint in the conjunction and
2132 * is updated if the constraint is actually printed.
2134 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
2135 __isl_keep isl_local_space *ls, isl_int *c, const char *op, int *first)
2137 unsigned o_div;
2138 isl_size n_div;
2139 int div;
2141 o_div = isl_local_space_offset(ls, isl_dim_div);
2142 n_div = isl_local_space_dim(ls, isl_dim_div);
2143 if (n_div < 0)
2144 return isl_printer_free(p);
2145 div = isl_seq_last_non_zero(c + o_div, n_div);
2146 if (div >= 0) {
2147 isl_bool is_div = isl_local_space_is_div_constraint(ls, c, div);
2148 if (is_div < 0)
2149 return isl_printer_free(p);
2150 if (is_div)
2151 return p;
2154 if (!*first)
2155 p = isl_printer_print_str(p, " && ");
2157 p = print_ls_affine_c(p, ls, c);
2158 p = isl_printer_print_str(p, " ");
2159 p = isl_printer_print_str(p, op);
2160 p = isl_printer_print_str(p, " 0");
2162 *first = 0;
2164 return p;
2167 static __isl_give isl_printer *print_ls_partial_affine_c(
2168 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2169 isl_int *c, unsigned len);
2171 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
2172 __isl_keep isl_space *space, __isl_keep isl_basic_set *bset)
2174 int i, j;
2175 int first = 1;
2176 isl_size n_div = isl_basic_set_dim(bset, isl_dim_div);
2177 isl_size total = isl_basic_set_dim(bset, isl_dim_all);
2178 isl_mat *div;
2179 isl_local_space *ls;
2181 if (n_div < 0 || total < 0)
2182 return isl_printer_free(p);
2184 total -= n_div;
2185 div = isl_basic_set_get_divs(bset);
2186 ls = isl_local_space_alloc_div(isl_space_copy(space), div);
2187 for (i = 0; i < bset->n_eq; ++i) {
2188 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
2189 if (j < 0)
2190 p = print_constraint_c(p, ls,
2191 bset->eq[i], "==", &first);
2192 else {
2193 if (i)
2194 p = isl_printer_print_str(p, " && ");
2195 p = isl_printer_print_str(p, "(");
2196 p = print_ls_partial_affine_c(p, ls, bset->eq[i],
2197 1 + total + j);
2198 p = isl_printer_print_str(p, ") % ");
2199 p = isl_printer_print_isl_int(p,
2200 bset->eq[i][1 + total + j]);
2201 p = isl_printer_print_str(p, " == 0");
2202 first = 0;
2205 for (i = 0; i < bset->n_ineq; ++i)
2206 p = print_constraint_c(p, ls, bset->ineq[i], ">=", &first);
2207 isl_local_space_free(ls);
2208 return p;
2211 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
2212 __isl_keep isl_space *space, __isl_keep isl_set *set)
2214 int i;
2216 if (!set)
2217 return isl_printer_free(p);
2219 if (set->n == 0)
2220 p = isl_printer_print_str(p, "0");
2222 for (i = 0; i < set->n; ++i) {
2223 if (i)
2224 p = isl_printer_print_str(p, " || ");
2225 if (set->n > 1)
2226 p = isl_printer_print_str(p, "(");
2227 p = print_basic_set_c(p, space, set->p[i]);
2228 if (set->n > 1)
2229 p = isl_printer_print_str(p, ")");
2231 return p;
2234 /* Print the piecewise quasi-polynomial "pwqp" to "p" in C format.
2236 static __isl_give isl_printer *print_pw_qpolynomial_c(
2237 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2239 int i;
2240 isl_space *space;
2242 space = isl_pw_qpolynomial_get_domain_space(pwqp);
2243 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set)) {
2244 p = print_qpolynomial_c(p, space, pwqp->p[0].qp);
2245 isl_space_free(space);
2246 return p;
2249 for (i = 0; i < pwqp->n; ++i) {
2250 p = isl_printer_print_str(p, "(");
2251 p = print_set_c(p, space, pwqp->p[i].set);
2252 p = isl_printer_print_str(p, ") ? (");
2253 p = print_qpolynomial_c(p, space, pwqp->p[i].qp);
2254 p = isl_printer_print_str(p, ") : ");
2257 isl_space_free(space);
2258 p = isl_printer_print_str(p, "0");
2259 return p;
2262 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
2263 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2265 if (!p || !pwqp)
2266 goto error;
2268 if (p->output_format == ISL_FORMAT_ISL)
2269 return print_pw_qpolynomial_isl(p, pwqp);
2270 else if (p->output_format == ISL_FORMAT_C)
2271 return print_pw_qpolynomial_c(p, pwqp);
2272 isl_assert(p->ctx, 0, goto error);
2273 error:
2274 isl_printer_free(p);
2275 return NULL;
2278 static isl_stat print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
2280 struct isl_union_print_data *data;
2281 data = (struct isl_union_print_data *)user;
2283 if (!data->first)
2284 data->p = isl_printer_print_str(data->p, "; ");
2285 data->first = 0;
2287 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
2288 isl_pw_qpolynomial_free(pwqp);
2290 return isl_stat_ok;
2293 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
2294 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2296 struct isl_union_print_data data;
2297 struct isl_print_space_data space_data = { 0 };
2298 isl_space *space;
2300 space = isl_union_pw_qpolynomial_get_space(upwqp);
2301 p = print_param_tuple(p, space, &space_data);
2302 isl_space_free(space);
2303 p = isl_printer_print_str(p, "{ ");
2304 data.p = p;
2305 data.first = 1;
2306 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
2307 &data);
2308 p = data.p;
2309 p = isl_printer_print_str(p, " }");
2310 return p;
2313 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
2314 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2316 if (!p || !upwqp)
2317 goto error;
2319 if (p->output_format == ISL_FORMAT_ISL)
2320 return print_union_pw_qpolynomial_isl(p, upwqp);
2321 isl_die(p->ctx, isl_error_invalid,
2322 "invalid output format for isl_union_pw_qpolynomial",
2323 goto error);
2324 error:
2325 isl_printer_free(p);
2326 return NULL;
2329 /* Print the quasi-polynomial reduction "fold" to "p" in C format,
2330 * with the variable names taken from the domain space "space".
2332 static __isl_give isl_printer *print_qpolynomial_fold_c(
2333 __isl_take isl_printer *p, __isl_keep isl_space *space,
2334 __isl_keep isl_qpolynomial_fold *fold)
2336 int i;
2338 for (i = 0; i < fold->n - 1; ++i)
2339 if (fold->type == isl_fold_min)
2340 p = isl_printer_print_str(p, "min(");
2341 else if (fold->type == isl_fold_max)
2342 p = isl_printer_print_str(p, "max(");
2344 for (i = 0; i < fold->n; ++i) {
2345 if (i)
2346 p = isl_printer_print_str(p, ", ");
2347 p = print_qpolynomial_c(p, space, fold->qp[i]);
2348 if (i)
2349 p = isl_printer_print_str(p, ")");
2351 return p;
2354 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
2355 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
2357 if (!p || !fold)
2358 goto error;
2359 if (p->output_format == ISL_FORMAT_ISL)
2360 return qpolynomial_fold_print(fold, p);
2361 else if (p->output_format == ISL_FORMAT_C)
2362 return print_qpolynomial_fold_c(p, fold->dim, fold);
2363 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2364 goto error);
2365 error:
2366 isl_printer_free(p);
2367 return NULL;
2370 /* Print the piecewise quasi-polynomial reduction "pwf" to "p" in C format.
2372 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
2373 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2375 int i;
2376 isl_space *space;
2378 space = isl_pw_qpolynomial_fold_get_domain_space(pwf);
2379 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set)) {
2380 p = print_qpolynomial_fold_c(p, space, pwf->p[0].fold);
2381 isl_space_free(space);
2382 return p;
2385 for (i = 0; i < pwf->n; ++i) {
2386 p = isl_printer_print_str(p, "(");
2387 p = print_set_c(p, space, pwf->p[i].set);
2388 p = isl_printer_print_str(p, ") ? (");
2389 p = print_qpolynomial_fold_c(p, space, pwf->p[i].fold);
2390 p = isl_printer_print_str(p, ") : ");
2393 isl_space_free(space);
2394 p = isl_printer_print_str(p, "0");
2395 return p;
2398 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
2399 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2401 if (!p || !pwf)
2402 goto error;
2404 if (p->output_format == ISL_FORMAT_ISL)
2405 return print_pw_qpolynomial_fold_isl(p, pwf);
2406 else if (p->output_format == ISL_FORMAT_C)
2407 return print_pw_qpolynomial_fold_c(p, pwf);
2408 isl_assert(p->ctx, 0, goto error);
2409 error:
2410 isl_printer_free(p);
2411 return NULL;
2414 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
2415 FILE *out, unsigned output_format)
2417 isl_printer *p;
2419 if (!pwf)
2420 return;
2422 p = isl_printer_to_file(pwf->dim->ctx, out);
2423 p = isl_printer_set_output_format(p, output_format);
2424 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
2426 isl_printer_free(p);
2429 static isl_stat print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf,
2430 void *user)
2432 struct isl_union_print_data *data;
2433 data = (struct isl_union_print_data *)user;
2435 if (!data->first)
2436 data->p = isl_printer_print_str(data->p, "; ");
2437 data->first = 0;
2439 data->p = isl_pwf_print_isl_body(data->p, pwf);
2440 isl_pw_qpolynomial_fold_free(pwf);
2442 return isl_stat_ok;
2445 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
2446 __isl_take isl_printer *p,
2447 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2449 struct isl_union_print_data data;
2450 struct isl_print_space_data space_data = { 0 };
2451 isl_space *space;
2453 space = isl_union_pw_qpolynomial_fold_get_space(upwf);
2454 p = print_param_tuple(p, space, &space_data);
2455 isl_space_free(space);
2456 p = isl_printer_print_str(p, "{ ");
2457 data.p = p;
2458 data.first = 1;
2459 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
2460 &print_pwf_body, &data);
2461 p = data.p;
2462 p = isl_printer_print_str(p, " }");
2463 return p;
2466 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2467 __isl_take isl_printer *p,
2468 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2470 if (!p || !upwf)
2471 goto error;
2473 if (p->output_format == ISL_FORMAT_ISL)
2474 return print_union_pw_qpolynomial_fold_isl(p, upwf);
2475 isl_die(p->ctx, isl_error_invalid,
2476 "invalid output format for isl_union_pw_qpolynomial_fold",
2477 goto error);
2478 error:
2479 isl_printer_free(p);
2480 return NULL;
2483 /* Print the isl_constraint "c" to "p".
2485 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
2486 __isl_keep isl_constraint *c)
2488 struct isl_print_space_data data = { 0 };
2489 isl_local_space *ls;
2490 isl_space *space;
2491 isl_bool exists;
2493 if (!p || !c)
2494 goto error;
2496 ls = isl_constraint_get_local_space(c);
2497 if (!ls)
2498 return isl_printer_free(p);
2499 space = isl_local_space_get_space(ls);
2500 p = print_param_tuple(p, space, &data);
2501 p = isl_printer_print_str(p, "{ ");
2502 p = isl_print_space(space, p, 0, &data);
2503 p = isl_printer_print_str(p, " : ");
2504 exists = need_exists(p, ls->div);
2505 if (exists < 0)
2506 p = isl_printer_free(p);
2507 if (exists >= 0 && exists)
2508 p = open_exists(p, space, ls->div, 0);
2509 p = print_affine_of_len(space, ls->div, p, c->v->el, c->v->size);
2510 if (isl_constraint_is_equality(c))
2511 p = isl_printer_print_str(p, " = 0");
2512 else
2513 p = isl_printer_print_str(p, " >= 0");
2514 if (exists >= 0 && exists)
2515 p = isl_printer_print_str(p, s_close_exists[0]);
2516 p = isl_printer_print_str(p, " }");
2517 isl_space_free(space);
2518 isl_local_space_free(ls);
2520 return p;
2521 error:
2522 isl_printer_free(p);
2523 return NULL;
2526 static __isl_give isl_printer *isl_printer_print_space_isl(
2527 __isl_take isl_printer *p, __isl_keep isl_space *space)
2529 struct isl_print_space_data data = { 0 };
2531 if (!space)
2532 goto error;
2534 p = print_param_tuple(p, space, &data);
2536 p = isl_printer_print_str(p, "{ ");
2537 if (isl_space_is_params(space))
2538 p = isl_printer_print_str(p, s_such_that[0]);
2539 else
2540 p = isl_print_space(space, p, 0, &data);
2541 p = isl_printer_print_str(p, " }");
2543 return p;
2544 error:
2545 isl_printer_free(p);
2546 return NULL;
2549 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
2550 __isl_keep isl_space *space)
2552 if (!p || !space)
2553 return isl_printer_free(p);
2554 if (p->output_format == ISL_FORMAT_ISL)
2555 return isl_printer_print_space_isl(p, space);
2556 else if (p->output_format == ISL_FORMAT_OMEGA)
2557 return print_omega_parameters(space, p);
2559 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
2560 "output format not supported for space",
2561 return isl_printer_free(p));
2564 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
2565 __isl_keep isl_local_space *ls)
2567 struct isl_print_space_data data = { 0 };
2568 isl_size n_div;
2570 n_div = isl_local_space_dim(ls, isl_dim_div);
2571 if (n_div < 0)
2572 goto error;
2574 p = print_param_tuple(p, ls->dim, &data);
2575 p = isl_printer_print_str(p, "{ ");
2576 p = isl_print_space(ls->dim, p, 0, &data);
2577 if (n_div > 0) {
2578 p = isl_printer_print_str(p, " : ");
2579 p = isl_printer_print_str(p, s_open_exists[0]);
2580 p = print_div_list(p, ls->dim, ls->div, 0, 1);
2581 p = isl_printer_print_str(p, s_close_exists[0]);
2582 } else if (isl_space_is_params(ls->dim))
2583 p = isl_printer_print_str(p, s_such_that[0]);
2584 p = isl_printer_print_str(p, " }");
2585 return p;
2586 error:
2587 isl_printer_free(p);
2588 return NULL;
2591 /* Print the (potentially rational) affine expression "aff" to "p",
2592 * with the variable names taken from "space".
2594 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2595 __isl_keep isl_space *space, __isl_keep isl_aff *aff)
2597 isl_size total;
2599 if (isl_aff_is_nan(aff))
2600 return isl_printer_print_str(p, "NaN");
2602 total = isl_local_space_dim(aff->ls, isl_dim_all);
2603 if (total < 0)
2604 return isl_printer_free(p);
2605 p = isl_printer_print_str(p, "(");
2606 p = print_affine_of_len(space, aff->ls->div, p,
2607 aff->v->el + 1, 1 + total);
2608 if (isl_int_is_one(aff->v->el[0]))
2609 p = isl_printer_print_str(p, ")");
2610 else {
2611 p = isl_printer_print_str(p, ")/");
2612 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2615 return p;
2618 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2619 __isl_keep isl_aff *aff)
2621 struct isl_print_space_data data = { 0 };
2623 if (isl_space_is_params(aff->ls->dim))
2625 else {
2626 p = print_tuple(aff->ls->dim, p, isl_dim_set, &data);
2627 p = isl_printer_print_str(p, " -> ");
2629 p = isl_printer_print_str(p, "[");
2630 p = print_aff_body(p, aff->ls->dim, aff);
2631 p = isl_printer_print_str(p, "]");
2633 return p;
2636 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2637 __isl_keep isl_aff *aff)
2639 struct isl_print_space_data data = { 0 };
2641 if (!aff)
2642 goto error;
2644 p = print_param_tuple(p, aff->ls->dim, &data);
2645 p = isl_printer_print_str(p, "{ ");
2646 p = print_aff(p, aff);
2647 p = isl_printer_print_str(p, " }");
2648 return p;
2649 error:
2650 isl_printer_free(p);
2651 return NULL;
2654 /* Print the body of an isl_pw_aff, i.e., a semicolon delimited
2655 * sequence of affine expressions, each followed by constraints.
2657 static __isl_give isl_printer *print_pw_aff_body(
2658 __isl_take isl_printer *p, __isl_keep isl_pw_aff *pa)
2660 int i;
2662 if (!pa)
2663 return isl_printer_free(p);
2665 for (i = 0; i < pa->n; ++i) {
2666 isl_space *space;
2668 if (i)
2669 p = isl_printer_print_str(p, "; ");
2670 p = print_aff(p, pa->p[i].aff);
2671 space = isl_aff_get_domain_space(pa->p[i].aff);
2672 p = print_disjuncts(set_to_map(pa->p[i].set), space, p, 0);
2673 isl_space_free(space);
2676 return p;
2679 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2680 __isl_keep isl_pw_aff *pwaff)
2682 struct isl_print_space_data data = { 0 };
2684 if (!pwaff)
2685 goto error;
2687 p = print_param_tuple(p, pwaff->dim, &data);
2688 p = isl_printer_print_str(p, "{ ");
2689 p = print_pw_aff_body(p, pwaff);
2690 p = isl_printer_print_str(p, " }");
2691 return p;
2692 error:
2693 isl_printer_free(p);
2694 return NULL;
2697 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2698 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2700 if (type == isl_dim_div) {
2701 p = isl_printer_print_str(p, "floord(");
2702 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2703 p = isl_printer_print_str(p, ", ");
2704 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2705 p = isl_printer_print_str(p, ")");
2706 } else {
2707 const char *name;
2709 name = isl_space_get_dim_name(ls->dim, type, pos);
2710 if (!name)
2711 name = "UNNAMED";
2712 p = isl_printer_print_str(p, name);
2714 return p;
2717 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2718 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2720 enum isl_dim_type type;
2722 if (!p || !ls)
2723 return isl_printer_free(p);
2725 if (pos == 0)
2726 return isl_printer_print_isl_int(p, c);
2728 if (isl_int_is_one(c))
2730 else if (isl_int_is_negone(c))
2731 p = isl_printer_print_str(p, "-");
2732 else {
2733 p = isl_printer_print_isl_int(p, c);
2734 p = isl_printer_print_str(p, "*");
2736 if (pos2type(ls->dim, &type, &pos) < 0)
2737 return isl_printer_free(p);
2738 p = print_ls_name_c(p, ls, type, pos);
2739 return p;
2742 static __isl_give isl_printer *print_ls_partial_affine_c(
2743 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2744 isl_int *c, unsigned len)
2746 int i;
2747 int first;
2749 for (i = 0, first = 1; i < len; ++i) {
2750 int flip = 0;
2751 if (isl_int_is_zero(c[i]))
2752 continue;
2753 if (!first) {
2754 if (isl_int_is_neg(c[i])) {
2755 flip = 1;
2756 isl_int_neg(c[i], c[i]);
2757 p = isl_printer_print_str(p, " - ");
2758 } else
2759 p = isl_printer_print_str(p, " + ");
2761 first = 0;
2762 p = print_ls_term_c(p, ls, c[i], i);
2763 if (flip)
2764 isl_int_neg(c[i], c[i]);
2766 if (first)
2767 p = isl_printer_print_str(p, "0");
2768 return p;
2771 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2772 __isl_keep isl_local_space *ls, isl_int *c)
2774 isl_size total = isl_local_space_dim(ls, isl_dim_all);
2776 if (total < 0)
2777 return isl_printer_free(p);
2778 return print_ls_partial_affine_c(p, ls, c, 1 + total);
2781 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2782 __isl_keep isl_aff *aff)
2784 isl_size total;
2786 total = isl_local_space_dim(aff->ls, isl_dim_all);
2787 if (total < 0)
2788 return isl_printer_free(p);
2789 if (!isl_int_is_one(aff->v->el[0]))
2790 p = isl_printer_print_str(p, "(");
2791 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2792 if (!isl_int_is_one(aff->v->el[0])) {
2793 p = isl_printer_print_str(p, ")/");
2794 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2796 return p;
2799 /* In the C format, we cannot express that "pwaff" may be undefined
2800 * on parts of the domain space. We therefore assume that the expression
2801 * will only be evaluated on its definition domain and compute the gist
2802 * of each cell with respect to this domain.
2804 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2805 __isl_keep isl_pw_aff *pwaff)
2807 isl_set *domain;
2808 isl_ast_build *build;
2809 isl_ast_expr *expr;
2811 if (pwaff->n < 1)
2812 isl_die(p->ctx, isl_error_unsupported,
2813 "cannot print empty isl_pw_aff in C format",
2814 return isl_printer_free(p));
2816 domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
2817 build = isl_ast_build_from_context(domain);
2818 expr = isl_ast_build_expr_from_pw_aff(build, isl_pw_aff_copy(pwaff));
2819 p = isl_printer_print_ast_expr(p, expr);
2820 isl_ast_expr_free(expr);
2821 isl_ast_build_free(build);
2823 return p;
2826 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2827 __isl_keep isl_aff *aff)
2829 if (!p || !aff)
2830 goto error;
2832 if (p->output_format == ISL_FORMAT_ISL)
2833 return print_aff_isl(p, aff);
2834 else if (p->output_format == ISL_FORMAT_C)
2835 return print_aff_c(p, aff);
2836 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2837 goto error);
2838 error:
2839 isl_printer_free(p);
2840 return NULL;
2843 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2844 __isl_keep isl_pw_aff *pwaff)
2846 if (!p || !pwaff)
2847 goto error;
2849 if (p->output_format == ISL_FORMAT_ISL)
2850 return print_pw_aff_isl(p, pwaff);
2851 else if (p->output_format == ISL_FORMAT_C)
2852 return print_pw_aff_c(p, pwaff);
2853 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2854 goto error);
2855 error:
2856 isl_printer_free(p);
2857 return NULL;
2860 /* Print "pa" in a sequence of isl_pw_affs delimited by semicolons.
2861 * Each isl_pw_aff itself is also printed as semicolon delimited
2862 * sequence of pieces.
2863 * If data->first = 1, then this is the first in the sequence.
2864 * Update data->first to tell the next element that it is not the first.
2866 static isl_stat print_pw_aff_body_wrap(__isl_take isl_pw_aff *pa,
2867 void *user)
2869 struct isl_union_print_data *data;
2870 data = (struct isl_union_print_data *) user;
2872 if (!data->first)
2873 data->p = isl_printer_print_str(data->p, "; ");
2874 data->first = 0;
2876 data->p = print_pw_aff_body(data->p, pa);
2877 isl_pw_aff_free(pa);
2879 return data->p ? isl_stat_ok : isl_stat_error;
2882 /* Print the body of an isl_union_pw_aff, i.e., a semicolon delimited
2883 * sequence of affine expressions, each followed by constraints,
2884 * with the sequence enclosed in braces.
2886 static __isl_give isl_printer *print_union_pw_aff_body(
2887 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2889 struct isl_union_print_data data = { p, 1 };
2891 p = isl_printer_print_str(p, s_open_set[0]);
2892 data.p = p;
2893 if (isl_union_pw_aff_foreach_pw_aff(upa,
2894 &print_pw_aff_body_wrap, &data) < 0)
2895 data.p = isl_printer_free(p);
2896 p = data.p;
2897 p = isl_printer_print_str(p, s_close_set[0]);
2899 return p;
2902 /* Print the isl_union_pw_aff "upa" to "p" in isl format.
2904 * The individual isl_pw_affs are delimited by a semicolon.
2906 static __isl_give isl_printer *print_union_pw_aff_isl(
2907 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2909 struct isl_print_space_data data = { 0 };
2910 isl_space *space;
2912 space = isl_union_pw_aff_get_space(upa);
2913 p = print_param_tuple(p, space, &data);
2914 isl_space_free(space);
2915 p = print_union_pw_aff_body(p, upa);
2916 return p;
2919 /* Print the isl_union_pw_aff "upa" to "p".
2921 * We currently only support an isl format.
2923 __isl_give isl_printer *isl_printer_print_union_pw_aff(
2924 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2926 if (!p || !upa)
2927 return isl_printer_free(p);
2929 if (p->output_format == ISL_FORMAT_ISL)
2930 return print_union_pw_aff_isl(p, upa);
2931 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2932 "unsupported output format", return isl_printer_free(p));
2935 /* Print dimension "pos" of data->space to "p".
2937 * data->user is assumed to be an isl_multi_aff.
2939 * If the current dimension is an output dimension, then print
2940 * the corresponding expression. Otherwise, print the name of the dimension.
2942 static __isl_give isl_printer *print_dim_ma(__isl_take isl_printer *p,
2943 struct isl_print_space_data *data, unsigned pos)
2945 isl_multi_aff *ma = data->user;
2947 if (data->type == isl_dim_out) {
2948 isl_space *space;
2950 space = isl_multi_aff_get_domain_space(ma);
2951 p = print_aff_body(p, space, ma->u.p[pos]);
2952 isl_space_free(space);
2953 } else {
2954 p = print_name(data->space, p, data->type, pos, data->latex);
2957 return p;
2960 static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
2961 __isl_keep isl_multi_aff *maff)
2963 struct isl_print_space_data data = { 0 };
2965 data.print_dim = &print_dim_ma;
2966 data.user = maff;
2967 return isl_print_space(maff->space, p, 0, &data);
2970 static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
2971 __isl_keep isl_multi_aff *maff)
2973 struct isl_print_space_data data = { 0 };
2975 if (!maff)
2976 goto error;
2978 p = print_param_tuple(p, maff->space, &data);
2979 p = isl_printer_print_str(p, "{ ");
2980 p = print_multi_aff(p, maff);
2981 p = isl_printer_print_str(p, " }");
2982 return p;
2983 error:
2984 isl_printer_free(p);
2985 return NULL;
2988 __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
2989 __isl_keep isl_multi_aff *maff)
2991 if (!p || !maff)
2992 goto error;
2994 if (p->output_format == ISL_FORMAT_ISL)
2995 return print_multi_aff_isl(p, maff);
2996 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2997 goto error);
2998 error:
2999 isl_printer_free(p);
3000 return NULL;
3003 static __isl_give isl_printer *print_pw_multi_aff_body(
3004 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3006 int i;
3008 if (!pma)
3009 goto error;
3011 for (i = 0; i < pma->n; ++i) {
3012 isl_space *space;
3014 if (i)
3015 p = isl_printer_print_str(p, "; ");
3016 p = print_multi_aff(p, pma->p[i].maff);
3017 space = isl_multi_aff_get_domain_space(pma->p[i].maff);
3018 p = print_disjuncts(set_to_map(pma->p[i].set), space, p, 0);
3019 isl_space_free(space);
3021 return p;
3022 error:
3023 isl_printer_free(p);
3024 return NULL;
3027 static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
3028 __isl_keep isl_pw_multi_aff *pma)
3030 struct isl_print_space_data data = { 0 };
3032 if (!pma)
3033 goto error;
3035 p = print_param_tuple(p, pma->dim, &data);
3036 p = isl_printer_print_str(p, "{ ");
3037 p = print_pw_multi_aff_body(p, pma);
3038 p = isl_printer_print_str(p, " }");
3039 return p;
3040 error:
3041 isl_printer_free(p);
3042 return NULL;
3045 /* Print the unnamed, single-dimensional piecewise multi affine expression "pma"
3046 * to "p".
3048 static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
3049 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3051 int i;
3052 isl_space *space;
3054 space = isl_pw_multi_aff_get_domain_space(pma);
3055 for (i = 0; i < pma->n - 1; ++i) {
3056 p = isl_printer_print_str(p, "(");
3057 p = print_set_c(p, space, pma->p[i].set);
3058 p = isl_printer_print_str(p, ") ? (");
3059 p = print_aff_c(p, pma->p[i].maff->u.p[0]);
3060 p = isl_printer_print_str(p, ") : ");
3062 isl_space_free(space);
3064 return print_aff_c(p, pma->p[pma->n - 1].maff->u.p[0]);
3067 static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
3068 __isl_keep isl_pw_multi_aff *pma)
3070 isl_size n;
3071 const char *name;
3073 if (!pma)
3074 goto error;
3075 if (pma->n < 1)
3076 isl_die(p->ctx, isl_error_unsupported,
3077 "cannot print empty isl_pw_multi_aff in C format",
3078 goto error);
3079 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
3080 if (n < 0)
3081 return isl_printer_free(p);
3082 name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
3083 if (!name && n == 1)
3084 return print_unnamed_pw_multi_aff_c(p, pma);
3085 if (!name)
3086 isl_die(p->ctx, isl_error_unsupported,
3087 "cannot print unnamed isl_pw_multi_aff in C format",
3088 goto error);
3090 p = isl_printer_print_str(p, name);
3091 if (n != 0)
3092 isl_die(p->ctx, isl_error_unsupported,
3093 "not supported yet", goto error);
3095 return p;
3096 error:
3097 isl_printer_free(p);
3098 return NULL;
3101 __isl_give isl_printer *isl_printer_print_pw_multi_aff(
3102 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3104 if (!p || !pma)
3105 goto error;
3107 if (p->output_format == ISL_FORMAT_ISL)
3108 return print_pw_multi_aff_isl(p, pma);
3109 if (p->output_format == ISL_FORMAT_C)
3110 return print_pw_multi_aff_c(p, pma);
3111 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3112 goto error);
3113 error:
3114 isl_printer_free(p);
3115 return NULL;
3118 static isl_stat print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
3119 void *user)
3121 struct isl_union_print_data *data;
3122 data = (struct isl_union_print_data *) user;
3124 if (!data->first)
3125 data->p = isl_printer_print_str(data->p, "; ");
3126 data->first = 0;
3128 data->p = print_pw_multi_aff_body(data->p, pma);
3129 isl_pw_multi_aff_free(pma);
3131 return isl_stat_ok;
3134 static __isl_give isl_printer *print_union_pw_multi_aff_isl(
3135 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
3137 struct isl_union_print_data data;
3138 struct isl_print_space_data space_data = { 0 };
3139 isl_space *space;
3141 space = isl_union_pw_multi_aff_get_space(upma);
3142 p = print_param_tuple(p, space, &space_data);
3143 isl_space_free(space);
3144 p = isl_printer_print_str(p, s_open_set[0]);
3145 data.p = p;
3146 data.first = 1;
3147 isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3148 &print_pw_multi_aff_body_wrap, &data);
3149 p = data.p;
3150 p = isl_printer_print_str(p, s_close_set[0]);
3151 return p;
3154 __isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
3155 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
3157 if (!p || !upma)
3158 goto error;
3160 if (p->output_format == ISL_FORMAT_ISL)
3161 return print_union_pw_multi_aff_isl(p, upma);
3162 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3163 goto error);
3164 error:
3165 isl_printer_free(p);
3166 return NULL;
3169 /* Print dimension "pos" of data->space to "p".
3171 * data->user is assumed to be an isl_multi_pw_aff.
3173 * If the current dimension is an output dimension, then print
3174 * the corresponding piecewise affine expression.
3175 * Otherwise, print the name of the dimension.
3177 static __isl_give isl_printer *print_dim_mpa(__isl_take isl_printer *p,
3178 struct isl_print_space_data *data, unsigned pos)
3180 int i;
3181 int need_parens;
3182 isl_space *space;
3183 isl_multi_pw_aff *mpa = data->user;
3184 isl_pw_aff *pa;
3186 if (data->type != isl_dim_out)
3187 return print_name(data->space, p, data->type, pos, data->latex);
3189 pa = mpa->u.p[pos];
3190 if (pa->n == 0)
3191 return isl_printer_print_str(p, "(0 : false)");
3193 need_parens = pa->n != 1 || !isl_set_plain_is_universe(pa->p[0].set);
3194 if (need_parens)
3195 p = isl_printer_print_str(p, "(");
3196 space = isl_multi_pw_aff_get_domain_space(mpa);
3197 for (i = 0; i < pa->n; ++i) {
3199 if (i)
3200 p = isl_printer_print_str(p, "; ");
3201 p = print_aff_body(p, space, pa->p[i].aff);
3202 p = print_disjuncts(pa->p[i].set, space, p, 0);
3204 isl_space_free(space);
3205 if (need_parens)
3206 p = isl_printer_print_str(p, ")");
3208 return p;
3211 /* Print "mpa" to "p" in isl format.
3213 * If "mpa" is zero-dimensional and has a non-trivial explicit domain,
3214 * then it is printed after the tuple of affine expressions.
3216 static __isl_give isl_printer *print_multi_pw_aff_isl(__isl_take isl_printer *p,
3217 __isl_keep isl_multi_pw_aff *mpa)
3219 struct isl_print_space_data data = { 0 };
3220 isl_bool has_domain;
3222 if (!mpa)
3223 return isl_printer_free(p);
3225 p = print_param_tuple(p, mpa->space, &data);
3226 p = isl_printer_print_str(p, "{ ");
3227 data.print_dim = &print_dim_mpa;
3228 data.user = mpa;
3229 p = isl_print_space(mpa->space, p, 0, &data);
3230 has_domain = isl_multi_pw_aff_has_non_trivial_domain(mpa);
3231 if (has_domain < 0)
3232 return isl_printer_free(p);
3233 if (has_domain) {
3234 isl_space *space;
3236 space = isl_space_domain(isl_space_copy(mpa->space));
3237 p = print_disjuncts_set(mpa->u.dom, space, p, 0);
3238 isl_space_free(space);
3240 p = isl_printer_print_str(p, " }");
3241 return p;
3244 __isl_give isl_printer *isl_printer_print_multi_pw_aff(
3245 __isl_take isl_printer *p, __isl_keep isl_multi_pw_aff *mpa)
3247 if (!p || !mpa)
3248 return isl_printer_free(p);
3250 if (p->output_format == ISL_FORMAT_ISL)
3251 return print_multi_pw_aff_isl(p, mpa);
3252 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3253 return isl_printer_free(p));
3256 /* Print dimension "pos" of data->space to "p".
3258 * data->user is assumed to be an isl_multi_val.
3260 * If the current dimension is an output dimension, then print
3261 * the corresponding value. Otherwise, print the name of the dimension.
3263 static __isl_give isl_printer *print_dim_mv(__isl_take isl_printer *p,
3264 struct isl_print_space_data *data, unsigned pos)
3266 isl_multi_val *mv = data->user;
3268 if (data->type == isl_dim_out)
3269 return isl_printer_print_val(p, mv->u.p[pos]);
3270 else
3271 return print_name(data->space, p, data->type, pos, data->latex);
3274 /* Print the isl_multi_val "mv" to "p" in isl format.
3276 static __isl_give isl_printer *print_multi_val_isl(__isl_take isl_printer *p,
3277 __isl_keep isl_multi_val *mv)
3279 struct isl_print_space_data data = { 0 };
3281 if (!mv)
3282 return isl_printer_free(p);
3284 p = print_param_tuple(p, mv->space, &data);
3285 p = isl_printer_print_str(p, "{ ");
3286 data.print_dim = &print_dim_mv;
3287 data.user = mv;
3288 p = isl_print_space(mv->space, p, 0, &data);
3289 p = isl_printer_print_str(p, " }");
3290 return p;
3293 /* Print the isl_multi_val "mv" to "p".
3295 * Currently only supported in isl format.
3297 __isl_give isl_printer *isl_printer_print_multi_val(
3298 __isl_take isl_printer *p, __isl_keep isl_multi_val *mv)
3300 if (!p || !mv)
3301 return isl_printer_free(p);
3303 if (p->output_format == ISL_FORMAT_ISL)
3304 return print_multi_val_isl(p, mv);
3305 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3306 return isl_printer_free(p));
3309 /* Print dimension "pos" of data->space to "p".
3311 * data->user is assumed to be an isl_multi_union_pw_aff.
3313 * The current dimension is necessarily a set dimension, so
3314 * we print the corresponding isl_union_pw_aff, including
3315 * the braces.
3317 static __isl_give isl_printer *print_union_pw_aff_dim(__isl_take isl_printer *p,
3318 struct isl_print_space_data *data, unsigned pos)
3320 isl_multi_union_pw_aff *mupa = data->user;
3321 isl_union_pw_aff *upa;
3323 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, pos);
3324 p = print_union_pw_aff_body(p, upa);
3325 isl_union_pw_aff_free(upa);
3327 return p;
3330 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3332 * If "mupa" is zero-dimensional and has a non-trivial explicit domain,
3333 * then it is printed after the tuple of affine expressions.
3334 * In order to clarify that this domain belongs to the expression,
3335 * the tuple along with the domain are placed inside parentheses.
3336 * If "mupa" has any parameters, then the opening parenthesis
3337 * appears after the parameter declarations.
3339 static __isl_give isl_printer *print_multi_union_pw_aff_isl(
3340 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3342 struct isl_print_space_data data = { 0 };
3343 isl_bool has_domain;
3344 isl_space *space;
3346 if (!mupa)
3347 return isl_printer_free(p);
3348 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
3349 if (has_domain < 0)
3350 return isl_printer_free(p);
3352 space = isl_multi_union_pw_aff_get_space(mupa);
3353 p = print_param_tuple(p, space, &data);
3355 if (has_domain)
3356 p = isl_printer_print_str(p, "(");
3358 data.print_dim = &print_union_pw_aff_dim;
3359 data.user = mupa;
3361 p = isl_print_space(space, p, 0, &data);
3362 isl_space_free(space);
3364 if (has_domain) {
3365 p = isl_printer_print_str(p, " : ");
3366 p = isl_printer_print_union_set_isl_body(p, mupa->u.dom);
3367 p = isl_printer_print_str(p, ")");
3370 return p;
3373 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3375 * We currently only support an isl format.
3377 __isl_give isl_printer *isl_printer_print_multi_union_pw_aff(
3378 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3380 if (!p || !mupa)
3381 return isl_printer_free(p);
3383 if (p->output_format == ISL_FORMAT_ISL)
3384 return print_multi_union_pw_aff_isl(p, mupa);
3385 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
3386 "unsupported output format", return isl_printer_free(p));