isl_space_is_domain: extract out isl_space_has_domain_tuples
[isl.git] / isl_output.c
blob2831f9f8d3b01744e978da8c20046c3f7174b195
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <stdlib.h>
16 #include <string.h>
17 #include <isl_ctx_private.h>
18 #include <isl_map_private.h>
19 #include <isl/set.h>
20 #include <isl_seq.h>
21 #include <isl_polynomial_private.h>
22 #include <isl_printer_private.h>
23 #include <isl_space_private.h>
24 #include <isl_mat_private.h>
25 #include <isl_vec_private.h>
26 #include <isl/union_map.h>
27 #include <isl/constraint.h>
28 #include <isl_local_space_private.h>
29 #include <isl_aff_private.h>
30 #include <isl_val_private.h>
31 #include <isl_constraint_private.h>
32 #include <isl/ast_build.h>
33 #include <isl_sort.h>
34 #include <isl_output_private.h>
36 #include <bset_to_bmap.c>
37 #include <set_to_map.c>
38 #include <uset_to_umap.c>
40 static const char *s_to[2] = { " -> ", " \\to " };
41 static const char *s_and[2] = { " and ", " \\wedge " };
42 static const char *s_or[2] = { " or ", " \\vee " };
43 static const char *s_le[2] = { "<=", "\\le" };
44 static const char *s_ge[2] = { ">=", "\\ge" };
45 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
46 static const char *s_close_set[2] = { " }", " \\,\\}" };
47 static const char *s_open_list[2] = { "[", "(" };
48 static const char *s_close_list[2] = { "]", ")" };
49 static const char *s_such_that[2] = { " : ", " \\mid " };
50 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
51 static const char *s_close_exists[2] = { ")", "" };
52 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
53 static const char *s_mod[2] = { "mod", "\\bmod" };
54 static const char *s_param_prefix[2] = { "p", "p_" };
55 static const char *s_input_prefix[2] = { "i", "i_" };
56 static const char *s_output_prefix[2] = { "o", "o_" };
58 static __isl_give isl_printer *print_constraint_polylib(
59 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
61 int i;
62 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
63 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
64 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
65 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
67 p = isl_printer_start_line(p);
68 p = isl_printer_print_int(p, ineq);
69 for (i = 0; i < n_out; ++i) {
70 p = isl_printer_print_str(p, " ");
71 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
73 for (i = 0; i < n_in; ++i) {
74 p = isl_printer_print_str(p, " ");
75 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
77 for (i = 0; i < bmap->n_div; ++i) {
78 p = isl_printer_print_str(p, " ");
79 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
81 for (i = 0; i < nparam; ++i) {
82 p = isl_printer_print_str(p, " ");
83 p = isl_printer_print_isl_int(p, c[1+i]);
85 p = isl_printer_print_str(p, " ");
86 p = isl_printer_print_isl_int(p, c[0]);
87 p = isl_printer_end_line(p);
88 return p;
91 static __isl_give isl_printer *print_constraints_polylib(
92 struct isl_basic_map *bmap, __isl_take isl_printer *p)
94 int i;
96 p = isl_printer_set_isl_int_width(p, 5);
98 for (i = 0; i < bmap->n_eq; ++i)
99 p = print_constraint_polylib(bmap, 0, i, p);
100 for (i = 0; i < bmap->n_ineq; ++i)
101 p = print_constraint_polylib(bmap, 1, i, p);
103 return p;
106 static __isl_give isl_printer *bset_print_constraints_polylib(
107 struct isl_basic_set *bset, __isl_take isl_printer *p)
109 return print_constraints_polylib(bset_to_bmap(bset), p);
112 static __isl_give isl_printer *isl_basic_map_print_polylib(
113 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
115 unsigned total = isl_basic_map_total_dim(bmap);
116 p = isl_printer_start_line(p);
117 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
118 p = isl_printer_print_str(p, " ");
119 p = isl_printer_print_int(p, 1 + total + 1);
120 if (ext) {
121 p = isl_printer_print_str(p, " ");
122 p = isl_printer_print_int(p,
123 isl_basic_map_dim(bmap, isl_dim_out));
124 p = isl_printer_print_str(p, " ");
125 p = isl_printer_print_int(p,
126 isl_basic_map_dim(bmap, isl_dim_in));
127 p = isl_printer_print_str(p, " ");
128 p = isl_printer_print_int(p,
129 isl_basic_map_dim(bmap, isl_dim_div));
130 p = isl_printer_print_str(p, " ");
131 p = isl_printer_print_int(p,
132 isl_basic_map_dim(bmap, isl_dim_param));
134 p = isl_printer_end_line(p);
135 return print_constraints_polylib(bmap, p);
138 static __isl_give isl_printer *isl_basic_set_print_polylib(
139 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
141 return isl_basic_map_print_polylib(bset_to_bmap(bset), p, ext);
144 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
145 __isl_take isl_printer *p, int ext)
147 int i;
149 p = isl_printer_start_line(p);
150 p = isl_printer_print_int(p, map->n);
151 p = isl_printer_end_line(p);
152 for (i = 0; i < map->n; ++i) {
153 p = isl_printer_start_line(p);
154 p = isl_printer_end_line(p);
155 p = isl_basic_map_print_polylib(map->p[i], p, ext);
157 return p;
160 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
161 __isl_take isl_printer *p, int ext)
163 return isl_map_print_polylib(set_to_map(set), p, ext);
166 static int count_same_name(__isl_keep isl_space *dim,
167 enum isl_dim_type type, unsigned pos, const char *name)
169 enum isl_dim_type t;
170 unsigned p, s;
171 int count = 0;
173 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
174 s = t == type ? pos : isl_space_dim(dim, t);
175 for (p = 0; p < s; ++p) {
176 const char *n = isl_space_get_dim_name(dim, t, p);
177 if (n && !strcmp(n, name))
178 count++;
181 return count;
184 /* Print the name of the variable of type "type" and position "pos"
185 * in "space" to "p".
187 static __isl_give isl_printer *print_name(__isl_keep isl_space *space,
188 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
189 int latex)
191 const char *name;
192 char buffer[20];
193 int primes;
195 name = type == isl_dim_div ? NULL
196 : isl_space_get_dim_name(space, type, pos);
198 if (!name) {
199 const char *prefix;
200 if (type == isl_dim_param)
201 prefix = s_param_prefix[latex];
202 else if (type == isl_dim_div)
203 prefix = s_div_prefix[latex];
204 else if (isl_space_is_set(space) || type == isl_dim_in)
205 prefix = s_input_prefix[latex];
206 else
207 prefix = s_output_prefix[latex];
208 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
209 name = buffer;
211 primes = count_same_name(space, name == buffer ? isl_dim_div : type,
212 pos, name);
213 p = isl_printer_print_str(p, name);
214 while (primes-- > 0)
215 p = isl_printer_print_str(p, "'");
216 return p;
219 static enum isl_dim_type pos2type(__isl_keep isl_space *dim, unsigned *pos)
221 enum isl_dim_type type;
222 unsigned n_in = isl_space_dim(dim, isl_dim_in);
223 unsigned n_out = isl_space_dim(dim, isl_dim_out);
224 unsigned nparam = isl_space_dim(dim, isl_dim_param);
226 if (*pos < 1 + nparam) {
227 type = isl_dim_param;
228 *pos -= 1;
229 } else if (*pos < 1 + nparam + n_in) {
230 type = isl_dim_in;
231 *pos -= 1 + nparam;
232 } else if (*pos < 1 + nparam + n_in + n_out) {
233 type = isl_dim_out;
234 *pos -= 1 + nparam + n_in;
235 } else {
236 type = isl_dim_div;
237 *pos -= 1 + nparam + n_in + n_out;
240 return type;
243 /* Can the div expression of the integer division at position "row" of "div"
244 * be printed?
245 * In particular, are the div expressions available and does the selected
246 * variable have a known explicit representation?
247 * Furthermore, the Omega format does not allow any div expressions
248 * to be printed.
250 static isl_bool can_print_div_expr(__isl_keep isl_printer *p,
251 __isl_keep isl_mat *div, int pos)
253 if (p->output_format == ISL_FORMAT_OMEGA)
254 return isl_bool_false;
255 if (!div)
256 return isl_bool_false;
257 return !isl_int_is_zero(div->row[pos][0]);
260 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
261 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
263 static __isl_give isl_printer *print_term(__isl_keep isl_space *space,
264 __isl_keep isl_mat *div,
265 isl_int c, unsigned pos, __isl_take isl_printer *p, int latex)
267 enum isl_dim_type type;
268 int print_div_def;
270 if (pos == 0)
271 return isl_printer_print_isl_int(p, c);
273 type = pos2type(space, &pos);
274 print_div_def = type == isl_dim_div && can_print_div_expr(p, div, pos);
276 if (isl_int_is_one(c))
278 else if (isl_int_is_negone(c))
279 p = isl_printer_print_str(p, "-");
280 else {
281 p = isl_printer_print_isl_int(p, c);
282 if (p->output_format == ISL_FORMAT_C || print_div_def)
283 p = isl_printer_print_str(p, "*");
285 if (print_div_def)
286 p = print_div(space, div, pos, p);
287 else
288 p = print_name(space, p, type, pos, latex);
289 return p;
292 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_space *dim,
293 __isl_keep isl_mat *div,
294 __isl_take isl_printer *p, isl_int *c, int len)
296 int i;
297 int first;
299 for (i = 0, first = 1; i < len; ++i) {
300 int flip = 0;
301 if (isl_int_is_zero(c[i]))
302 continue;
303 if (!first) {
304 if (isl_int_is_neg(c[i])) {
305 flip = 1;
306 isl_int_neg(c[i], c[i]);
307 p = isl_printer_print_str(p, " - ");
308 } else
309 p = isl_printer_print_str(p, " + ");
311 first = 0;
312 p = print_term(dim, div, c[i], i, p, 0);
313 if (flip)
314 isl_int_neg(c[i], c[i]);
316 if (first)
317 p = isl_printer_print_str(p, "0");
318 return p;
321 /* Print an affine expression "c"
322 * to "p", with the variable names taken from "space" and
323 * the integer division definitions taken from "div".
325 static __isl_give isl_printer *print_affine(__isl_take isl_printer *p,
326 __isl_keep isl_space *space, __isl_keep isl_mat *div, isl_int *c)
328 unsigned n_div;
329 unsigned len;
331 if (!space || !div)
332 return isl_printer_free(p);
333 n_div = isl_mat_rows(div);
334 len = 1 + isl_space_dim(space, isl_dim_all) + n_div;
335 return print_affine_of_len(space, div, p, c, len);
338 /* offset is the offset of local_dim inside data->type of data->space.
340 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
341 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
342 struct isl_print_space_data *data, int offset)
344 int i;
346 if (data->space != local_dim && local_type == isl_dim_out)
347 offset += local_dim->n_in;
349 for (i = 0; i < isl_space_dim(local_dim, local_type); ++i) {
350 if (i)
351 p = isl_printer_print_str(p, ", ");
352 if (data->print_dim)
353 p = data->print_dim(p, data, offset + i);
354 else
355 p = print_name(data->space, p, data->type, offset + i,
356 data->latex);
358 return p;
361 static __isl_give isl_printer *print_var_list(__isl_take isl_printer *p,
362 __isl_keep isl_space *space, enum isl_dim_type type)
364 struct isl_print_space_data data = { .space = space, .type = type };
366 return print_nested_var_list(p, space, type, &data, 0);
369 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
370 __isl_keep isl_space *local_dim,
371 struct isl_print_space_data *data, int offset);
373 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
374 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
375 struct isl_print_space_data *data, int offset)
377 const char *name = NULL;
378 unsigned n = isl_space_dim(local_dim, local_type);
379 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
380 name = isl_space_get_tuple_name(local_dim, local_type);
381 if (name) {
382 if (data->latex)
383 p = isl_printer_print_str(p, "\\mathrm{");
384 p = isl_printer_print_str(p, name);
385 if (data->latex)
386 p = isl_printer_print_str(p, "}");
389 if (!data->latex || n != 1 || name)
390 p = isl_printer_print_str(p, s_open_list[data->latex]);
391 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
392 local_dim->nested[local_type - isl_dim_in]) {
393 if (data->space != local_dim && local_type == isl_dim_out)
394 offset += local_dim->n_in;
395 p = print_nested_map_dim(p,
396 local_dim->nested[local_type - isl_dim_in],
397 data, offset);
398 } else
399 p = print_nested_var_list(p, local_dim, local_type, data,
400 offset);
401 if (!data->latex || n != 1 || name)
402 p = isl_printer_print_str(p, s_close_list[data->latex]);
403 return p;
406 static __isl_give isl_printer *print_tuple(__isl_keep isl_space *dim,
407 __isl_take isl_printer *p, enum isl_dim_type type,
408 struct isl_print_space_data *data)
410 data->space = dim;
411 data->type = type;
412 return print_nested_tuple(p, dim, type, data, 0);
415 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
416 __isl_keep isl_space *local_dim,
417 struct isl_print_space_data *data, int offset)
419 p = print_nested_tuple(p, local_dim, isl_dim_in, data, offset);
420 p = isl_printer_print_str(p, s_to[data->latex]);
421 p = print_nested_tuple(p, local_dim, isl_dim_out, data, offset);
423 return p;
426 __isl_give isl_printer *isl_print_space(__isl_keep isl_space *space,
427 __isl_take isl_printer *p, int rational,
428 struct isl_print_space_data *data)
430 if (rational && !data->latex)
431 p = isl_printer_print_str(p, "rat: ");
432 if (isl_space_is_params(space))
434 else if (isl_space_is_set(space))
435 p = print_tuple(space, p, isl_dim_set, data);
436 else {
437 p = print_tuple(space, p, isl_dim_in, data);
438 p = isl_printer_print_str(p, s_to[data->latex]);
439 p = print_tuple(space, p, isl_dim_out, data);
442 return p;
445 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_space *dim,
446 __isl_take isl_printer *p)
448 if (isl_space_dim(dim, isl_dim_param) == 0)
449 return p;
451 p = isl_printer_start_line(p);
452 p = isl_printer_print_str(p, "symbolic ");
453 p = print_var_list(p, dim, isl_dim_param);
454 p = isl_printer_print_str(p, ";");
455 p = isl_printer_end_line(p);
456 return p;
459 /* Does the inequality constraint following "i" in "bmap"
460 * have an opposite value for the same last coefficient?
461 * "last" is the position of the last coefficient of inequality "i".
462 * If the next constraint is a div constraint, then it is ignored
463 * since div constraints are not printed.
465 static int next_is_opposite(__isl_keep isl_basic_map *bmap, int i, int last)
467 unsigned total = isl_basic_map_total_dim(bmap);
468 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
470 if (i + 1 >= bmap->n_ineq)
471 return 0;
472 if (isl_seq_last_non_zero(bmap->ineq[i + 1], 1 + total) != last)
473 return 0;
474 if (last >= o_div) {
475 isl_bool is_div;
476 is_div = isl_basic_map_is_div_constraint(bmap,
477 bmap->ineq[i + 1], last - o_div);
478 if (is_div < 0)
479 return -1;
480 if (is_div)
481 return 0;
483 return isl_int_abs_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]) &&
484 !isl_int_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]);
487 /* Return a string representation of the operator used when
488 * printing a constraint where the LHS is greater than or equal to the LHS
489 * (sign > 0) or smaller than or equal to the LHS (sign < 0).
490 * If "strict" is set, then return the strict version of the comparison
491 * operator.
493 static const char *constraint_op(int sign, int strict, int latex)
495 if (strict)
496 return sign < 0 ? "<" : ">";
497 if (sign < 0)
498 return s_le[latex];
499 else
500 return s_ge[latex];
503 /* Print one side of a constraint "c" to "p", with
504 * the variable names taken from "space" and the integer division definitions
505 * taken from "div".
506 * "last" is the position of the last non-zero coefficient.
507 * Let c' be the result of zeroing out this coefficient, then
508 * the partial constraint
510 * c' op
512 * is printed.
514 static __isl_give isl_printer *print_half_constraint(__isl_take isl_printer *p,
515 __isl_keep isl_space *space, __isl_keep isl_mat *div,
516 isl_int *c, int last, const char *op, int latex)
518 isl_int_set_si(c[last], 0);
519 p = print_affine(p, space, div, c);
521 p = isl_printer_print_str(p, " ");
522 p = isl_printer_print_str(p, op);
523 p = isl_printer_print_str(p, " ");
525 return p;
528 /* Print a constraint "c" to "p", with the variable names
529 * taken from "space" and the integer division definitions taken from "div".
530 * "last" is the position of the last non-zero coefficient, which is
531 * moreover assumed to be negative.
532 * Let c' be the result of zeroing out this coefficient, then
533 * the constraint is printed in the form
535 * -c[last] op c'
537 static __isl_give isl_printer *print_constraint(__isl_take isl_printer *p,
538 __isl_keep isl_space *space, __isl_keep isl_mat *div,
539 isl_int *c, int last, const char *op, int latex)
541 isl_int_abs(c[last], c[last]);
543 p = print_term(space, div, c[last], last, p, latex);
545 p = isl_printer_print_str(p, " ");
546 p = isl_printer_print_str(p, op);
547 p = isl_printer_print_str(p, " ");
549 isl_int_set_si(c[last], 0);
550 p = print_affine(p, space, div, c);
552 return p;
555 /* Given an integer division
557 * floor(f/m)
559 * at position "pos" in "div", print the corresponding modulo expression
561 * (f) mod m
563 * to "p". The variable names are taken from "space", while any
564 * nested integer division definitions are taken from "div".
566 static __isl_give isl_printer *print_mod(__isl_take isl_printer *p,
567 __isl_keep isl_space *space, __isl_keep isl_mat *div, int pos,
568 int latex)
570 if (!p || !div)
571 return isl_printer_free(p);
573 p = isl_printer_print_str(p, "(");
574 p = print_affine_of_len(space, div, p,
575 div->row[pos] + 1, div->n_col - 1);
576 p = isl_printer_print_str(p, ") ");
577 p = isl_printer_print_str(p, s_mod[latex]);
578 p = isl_printer_print_str(p, " ");
579 p = isl_printer_print_isl_int(p, div->row[pos][0]);
580 return p;
583 /* Can the equality constraints "c" be printed as a modulo constraint?
584 * In particular, is of the form
586 * f - a m floor(g/m) = 0,
588 * with c = -a m the coefficient at position "pos"?
589 * Return the position of the corresponding integer division if so.
590 * Return the number of integer divisions if not.
591 * Return -1 on error.
593 * Modulo constraints are currently not printed in C format.
594 * Other than that, "pos" needs to correspond to an integer division
595 * with explicit representation and "c" needs to be a multiple
596 * of the denominator of the integer division.
598 static int print_as_modulo_pos(__isl_keep isl_printer *p,
599 __isl_keep isl_space *space, __isl_keep isl_mat *div, unsigned pos,
600 isl_int c)
602 isl_bool can_print;
603 unsigned n_div;
604 enum isl_dim_type type;
606 if (!p)
607 return -1;
608 n_div = isl_mat_rows(div);
609 if (p->output_format == ISL_FORMAT_C)
610 return n_div;
611 type = pos2type(space, &pos);
612 if (type != isl_dim_div)
613 return n_div;
614 can_print = can_print_div_expr(p, div, pos);
615 if (can_print < 0)
616 return -1;
617 if (!can_print)
618 return n_div;
619 if (!isl_int_is_divisible_by(c, div->row[pos][0]))
620 return n_div;
621 return pos;
624 /* Print equality constraint "c" to "p" as a modulo constraint,
625 * with the variable names taken from "space" and
626 * the integer division definitions taken from "div".
627 * "last" is the position of the last non-zero coefficient, which is
628 * moreover assumed to be negative and a multiple of the denominator
629 * of the corresponding integer division. "div_pos" is the corresponding
630 * position in the sequence of integer divisions.
632 * The equality is of the form
634 * f - a m floor(g/m) = 0.
636 * Print it as
638 * a (g mod m) = -f + a g
640 static __isl_give isl_printer *print_eq_mod_constraint(
641 __isl_take isl_printer *p, __isl_keep isl_space *space,
642 __isl_keep isl_mat *div, unsigned div_pos,
643 isl_int *c, int last, int latex)
645 isl_ctx *ctx;
646 int multiple;
648 ctx = isl_printer_get_ctx(p);
649 isl_int_divexact(c[last], c[last], div->row[div_pos][0]);
650 isl_int_abs(c[last], c[last]);
651 multiple = !isl_int_is_one(c[last]);
652 if (multiple) {
653 p = isl_printer_print_isl_int(p, c[last]);
654 p = isl_printer_print_str(p, "*(");
656 p = print_mod(p, space, div, div_pos, latex);
657 if (multiple)
658 p = isl_printer_print_str(p, ")");
659 p = isl_printer_print_str(p, " = ");
660 isl_seq_combine(c, ctx->negone, c,
661 c[last], div->row[div_pos] + 1, last);
662 isl_int_set_si(c[last], 0);
663 p = print_affine(p, space, div, c);
664 return p;
667 /* Print equality constraint "c" to "p", with the variable names
668 * taken from "space" and the integer division definitions taken from "div".
669 * "last" is the position of the last non-zero coefficient, which is
670 * moreover assumed to be negative.
672 * If possible, print the equality constraint as a modulo constraint.
674 static __isl_give isl_printer *print_eq_constraint(__isl_take isl_printer *p,
675 __isl_keep isl_space *space, __isl_keep isl_mat *div, isl_int *c,
676 int last, int latex)
678 unsigned n_div;
679 int div_pos;
681 n_div = isl_mat_rows(div);
682 div_pos = print_as_modulo_pos(p, space, div, last, c[last]);
683 if (div_pos < 0)
684 return isl_printer_free(p);
685 if (div_pos < n_div)
686 return print_eq_mod_constraint(p, space, div, div_pos,
687 c, last, latex);
688 return print_constraint(p, space, div, c, last, "=", latex);
691 /* Print the constraints of "bmap" to "p".
692 * The names of the variables are taken from "space" and
693 * the integer division definitions are taken from "div".
694 * Div constraints are only printed in "dump" mode.
695 * The constraints are sorted prior to printing (except in "dump" mode).
697 * If x is the last variable with a non-zero coefficient,
698 * then a lower bound
700 * f - a x >= 0
702 * is printed as
704 * a x <= f
706 * while an upper bound
708 * f + a x >= 0
710 * is printed as
712 * a x >= -f
714 * If the next constraint has an opposite sign for the same last coefficient,
715 * then it is printed as
717 * f >= a x
719 * or
721 * -f <= a x
723 * instead. In fact, the "a x" part is not printed explicitly, but
724 * reused from the next constraint, which is therefore treated as
725 * a first constraint in the conjunction.
727 * If the constant term of "f" is -1, then "f" is replaced by "f + 1" and
728 * the comparison operator is replaced by the strict variant.
729 * Essentially, ">= 1" is replaced by "> 0".
731 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
732 __isl_keep isl_space *space, __isl_keep isl_mat *div,
733 __isl_take isl_printer *p, int latex)
735 int i;
736 isl_vec *c = NULL;
737 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
738 unsigned total = isl_basic_map_total_dim(bmap);
739 unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
740 int first = 1;
741 int dump;
743 if (!p)
744 return NULL;
745 bmap = isl_basic_map_copy(bmap);
746 dump = p->dump;
747 if (!dump)
748 bmap = isl_basic_map_sort_constraints(bmap);
749 if (!bmap)
750 goto error;
752 c = isl_vec_alloc(bmap->ctx, 1 + total);
753 if (!c)
754 goto error;
756 for (i = bmap->n_eq - 1; i >= 0; --i) {
757 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
758 if (l < 0) {
759 if (i != bmap->n_eq - 1)
760 p = isl_printer_print_str(p, s_and[latex]);
761 p = isl_printer_print_str(p, "0 = 0");
762 continue;
764 if (!first)
765 p = isl_printer_print_str(p, s_and[latex]);
766 if (isl_int_is_neg(bmap->eq[i][l]))
767 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
768 else
769 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
770 p = print_eq_constraint(p, space, div, c->el, l, latex);
771 first = 0;
773 for (i = 0; i < bmap->n_ineq; ++i) {
774 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
775 int strict;
776 int s;
777 const char *op;
778 if (l < 0)
779 continue;
780 if (!dump && l >= o_div &&
781 can_print_div_expr(p, div, l - o_div)) {
782 isl_bool is_div;
783 is_div = isl_basic_map_is_div_constraint(bmap,
784 bmap->ineq[i], l - o_div);
785 if (is_div < 0)
786 goto error;
787 if (is_div)
788 continue;
790 if (!first)
791 p = isl_printer_print_str(p, s_and[latex]);
792 s = isl_int_sgn(bmap->ineq[i][l]);
793 strict = !rational && isl_int_is_negone(bmap->ineq[i][0]);
794 if (s < 0)
795 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
796 else
797 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
798 if (strict)
799 isl_int_set_si(c->el[0], 0);
800 if (!dump && next_is_opposite(bmap, i, l)) {
801 op = constraint_op(-s, strict, latex);
802 p = print_half_constraint(p, space, div, c->el, l,
803 op, latex);
804 first = 1;
805 } else {
806 op = constraint_op(s, strict, latex);
807 p = print_constraint(p, space, div, c->el, l,
808 op, latex);
809 first = 0;
813 isl_basic_map_free(bmap);
814 isl_vec_free(c);
816 return p;
817 error:
818 isl_basic_map_free(bmap);
819 isl_vec_free(c);
820 isl_printer_free(p);
821 return NULL;
824 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
825 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
827 int c;
829 if (!p || !div)
830 return isl_printer_free(p);
832 c = p->output_format == ISL_FORMAT_C;
833 p = isl_printer_print_str(p, c ? "floord(" : "floor((");
834 p = print_affine_of_len(dim, div, p,
835 div->row[pos] + 1, div->n_col - 1);
836 p = isl_printer_print_str(p, c ? ", " : ")/");
837 p = isl_printer_print_isl_int(p, div->row[pos][0]);
838 p = isl_printer_print_str(p, ")");
839 return p;
842 /* Print a comma separated list of div names, except those that have
843 * a definition that can be printed.
844 * If "print_defined_divs" is set, then those div names are printed
845 * as well, along with their definitions.
847 static __isl_give isl_printer *print_div_list(__isl_take isl_printer *p,
848 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex,
849 int print_defined_divs)
851 int i;
852 int first = 1;
853 unsigned n_div;
855 if (!p || !space || !div)
856 return isl_printer_free(p);
858 n_div = isl_mat_rows(div);
860 for (i = 0; i < n_div; ++i) {
861 if (!print_defined_divs && can_print_div_expr(p, div, i))
862 continue;
863 if (!first)
864 p = isl_printer_print_str(p, ", ");
865 p = print_name(space, p, isl_dim_div, i, latex);
866 first = 0;
867 if (!can_print_div_expr(p, div, i))
868 continue;
869 p = isl_printer_print_str(p, " = ");
870 p = print_div(space, div, i, p);
873 return p;
876 /* Does printing an object with local variables described by "div"
877 * require an "exists" clause?
878 * That is, are there any local variables without an explicit representation?
879 * An exists clause is also needed in "dump" mode because
880 * explicit div representations are not printed inline in that case.
882 static isl_bool need_exists(__isl_keep isl_printer *p, __isl_keep isl_mat *div)
884 int i, n;
886 if (!p || !div)
887 return isl_bool_error;
888 n = isl_mat_rows(div);
889 if (n == 0)
890 return isl_bool_false;
891 if (p->dump)
892 return isl_bool_true;
893 for (i = 0; i < n; ++i)
894 if (!can_print_div_expr(p, div, i))
895 return isl_bool_true;
896 return isl_bool_false;
899 /* Print the start of an exists clause, i.e.,
901 * (exists variables:
903 * In dump mode, local variables with an explicit definition are printed
904 * as well because they will not be printed inline.
906 static __isl_give isl_printer *open_exists(__isl_take isl_printer *p,
907 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex)
909 int dump;
911 if (!p)
912 return NULL;
914 dump = p->dump;
915 p = isl_printer_print_str(p, s_open_exists[latex]);
916 p = print_div_list(p, space, div, latex, dump);
917 p = isl_printer_print_str(p, ": ");
919 return p;
922 /* Remove the explicit representations of all local variables in "div".
924 static __isl_give isl_mat *mark_all_unknown(__isl_take isl_mat *div)
926 int i, n_div;
928 if (!div)
929 return NULL;
931 n_div = isl_mat_rows(div);
932 for (i = 0; i < n_div; ++i)
933 div = isl_mat_set_element_si(div, i, 0, 0);
934 return div;
937 /* Print the constraints of "bmap" to "p".
938 * The names of the variables are taken from "space".
939 * "latex" is set if the constraints should be printed in LaTeX format.
940 * Do not print inline explicit div representations in "dump" mode.
942 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
943 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
945 int dump;
946 isl_mat *div;
947 isl_bool exists;
949 if (!p)
950 return NULL;
951 dump = p->dump;
952 div = isl_basic_map_get_divs(bmap);
953 exists = need_exists(p, div);
954 if (exists >= 0 && exists)
955 p = open_exists(p, space, div, latex);
957 if (dump)
958 div = mark_all_unknown(div);
959 p = print_constraints(bmap, space, div, p, latex);
960 isl_mat_free(div);
962 if (exists >= 0 && exists)
963 p = isl_printer_print_str(p, s_close_exists[latex]);
964 return p;
967 /* Print a colon followed by the constraints of "bmap"
968 * to "p", provided there are any constraints.
969 * The names of the variables are taken from "space".
970 * "latex" is set if the constraints should be printed in LaTeX format.
972 static __isl_give isl_printer *print_optional_disjunct(
973 __isl_keep isl_basic_map *bmap, __isl_keep isl_space *space,
974 __isl_take isl_printer *p, int latex)
976 if (isl_basic_map_plain_is_universe(bmap))
977 return p;
979 p = isl_printer_print_str(p, ": ");
980 p = print_disjunct(bmap, space, p, latex);
982 return p;
985 static __isl_give isl_printer *basic_map_print_omega(
986 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
988 p = isl_printer_print_str(p, "{ [");
989 p = print_var_list(p, bmap->dim, isl_dim_in);
990 p = isl_printer_print_str(p, "] -> [");
991 p = print_var_list(p, bmap->dim, isl_dim_out);
992 p = isl_printer_print_str(p, "] ");
993 p = print_optional_disjunct(bmap, bmap->dim, p, 0);
994 p = isl_printer_print_str(p, " }");
995 return p;
998 static __isl_give isl_printer *basic_set_print_omega(
999 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
1001 p = isl_printer_print_str(p, "{ [");
1002 p = print_var_list(p, bset->dim, isl_dim_set);
1003 p = isl_printer_print_str(p, "] ");
1004 p = print_optional_disjunct(bset, bset->dim, p, 0);
1005 p = isl_printer_print_str(p, " }");
1006 return p;
1009 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
1010 __isl_take isl_printer *p)
1012 int i;
1014 for (i = 0; i < map->n; ++i) {
1015 if (i)
1016 p = isl_printer_print_str(p, " union ");
1017 p = basic_map_print_omega(map->p[i], p);
1019 return p;
1022 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
1023 __isl_take isl_printer *p)
1025 int i;
1027 for (i = 0; i < set->n; ++i) {
1028 if (i)
1029 p = isl_printer_print_str(p, " union ");
1030 p = basic_set_print_omega(set->p[i], p);
1032 return p;
1035 /* Print the list of parameters in "space", followed by an arrow, to "p",
1036 * if there are any parameters.
1038 static __isl_give isl_printer *print_param_tuple(__isl_take isl_printer *p,
1039 __isl_keep isl_space *space, struct isl_print_space_data *data)
1041 if (!p || !space)
1042 return isl_printer_free(p);
1043 if (isl_space_dim(space, isl_dim_param) == 0)
1044 return p;
1046 p = print_tuple(space, p, isl_dim_param, data);
1047 p = isl_printer_print_str(p, s_to[data->latex]);
1049 return p;
1052 static __isl_give isl_printer *isl_basic_map_print_isl(
1053 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
1054 int latex)
1056 struct isl_print_space_data data = { .latex = latex };
1057 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1059 p = print_param_tuple(p, bmap->dim, &data);
1060 p = isl_printer_print_str(p, "{ ");
1061 p = isl_print_space(bmap->dim, p, rational, &data);
1062 p = isl_printer_print_str(p, " : ");
1063 p = print_disjunct(bmap, bmap->dim, p, latex);
1064 p = isl_printer_print_str(p, " }");
1065 return p;
1068 /* Print the disjuncts of a map (or set) "map" to "p".
1069 * The names of the variables are taken from "space".
1070 * "latex" is set if the constraints should be printed in LaTeX format.
1072 static __isl_give isl_printer *print_disjuncts_core(__isl_keep isl_map *map,
1073 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1075 int i;
1077 if (map->n == 0)
1078 p = isl_printer_print_str(p, "false");
1079 for (i = 0; i < map->n; ++i) {
1080 if (i)
1081 p = isl_printer_print_str(p, s_or[latex]);
1082 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
1083 p = isl_printer_print_str(p, "(");
1084 p = print_disjunct(map->p[i], space, p, latex);
1085 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
1086 p = isl_printer_print_str(p, ")");
1088 return p;
1091 /* Print the disjuncts of a map (or set) "map" to "p".
1092 * The names of the variables are taken from "space".
1093 * "hull" describes constraints shared by all disjuncts of "map".
1094 * "latex" is set if the constraints should be printed in LaTeX format.
1096 * Print the disjuncts as a conjunction of "hull" and
1097 * the result of removing the constraints of "hull" from "map".
1098 * If this result turns out to be the universe, then simply print "hull".
1100 static __isl_give isl_printer *print_disjuncts_in_hull(__isl_keep isl_map *map,
1101 __isl_keep isl_space *space, __isl_take isl_basic_map *hull,
1102 __isl_take isl_printer *p, int latex)
1104 isl_bool is_universe;
1106 p = print_disjunct(hull, space, p, latex);
1107 map = isl_map_plain_gist_basic_map(isl_map_copy(map), hull);
1108 is_universe = isl_map_plain_is_universe(map);
1109 if (is_universe < 0)
1110 goto error;
1111 if (!is_universe) {
1112 p = isl_printer_print_str(p, s_and[latex]);
1113 p = isl_printer_print_str(p, "(");
1114 p = print_disjuncts_core(map, space, p, latex);
1115 p = isl_printer_print_str(p, ")");
1117 isl_map_free(map);
1119 return p;
1120 error:
1121 isl_map_free(map);
1122 isl_printer_free(p);
1123 return NULL;
1126 /* Print the disjuncts of a map (or set) "map" to "p".
1127 * The names of the variables are taken from "space".
1128 * "latex" is set if the constraints should be printed in LaTeX format.
1130 * If there are at least two disjuncts and "dump" mode is not turned out,
1131 * check for any shared constraints among all disjuncts.
1132 * If there are any, then print them separately in print_disjuncts_in_hull.
1134 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
1135 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1137 if (isl_map_plain_is_universe(map))
1138 return p;
1140 p = isl_printer_print_str(p, s_such_that[latex]);
1141 if (!p)
1142 return NULL;
1144 if (!p->dump && map->n >= 2) {
1145 isl_basic_map *hull;
1146 isl_bool is_universe;
1148 hull = isl_map_plain_unshifted_simple_hull(isl_map_copy(map));
1149 is_universe = isl_basic_map_plain_is_universe(hull);
1150 if (is_universe < 0)
1151 p = isl_printer_free(p);
1152 else if (!is_universe)
1153 return print_disjuncts_in_hull(map, space, hull,
1154 p, latex);
1155 isl_basic_map_free(hull);
1158 return print_disjuncts_core(map, space, p, latex);
1161 /* Print the disjuncts of a map (or set).
1162 * The names of the variables are taken from "space".
1163 * "latex" is set if the constraints should be printed in LaTeX format.
1165 * If the map turns out to be a universal parameter domain, then
1166 * we need to print the colon. Otherwise, the output looks identical
1167 * to the empty set.
1169 static __isl_give isl_printer *print_disjuncts_map(__isl_keep isl_map *map,
1170 __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1172 if (isl_map_plain_is_universe(map) && isl_space_is_params(map->dim))
1173 return isl_printer_print_str(p, s_such_that[latex]);
1174 else
1175 return print_disjuncts(map, space, p, latex);
1178 struct isl_aff_split {
1179 isl_basic_map *aff;
1180 isl_map *map;
1183 static void free_split(__isl_take struct isl_aff_split *split, int n)
1185 int i;
1187 if (!split)
1188 return;
1190 for (i = 0; i < n; ++i) {
1191 isl_basic_map_free(split[i].aff);
1192 isl_map_free(split[i].map);
1195 free(split);
1198 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
1200 int i, j;
1201 unsigned nparam, n_in, n_out, total;
1203 bmap = isl_basic_map_cow(bmap);
1204 if (!bmap)
1205 return NULL;
1206 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
1207 goto error;
1209 nparam = isl_basic_map_dim(bmap, isl_dim_param);
1210 n_in = isl_basic_map_dim(bmap, isl_dim_in);
1211 n_out = isl_basic_map_dim(bmap, isl_dim_out);
1212 total = isl_basic_map_dim(bmap, isl_dim_all);
1213 for (i = bmap->n_eq - 1; i >= 0; --i) {
1214 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
1215 if (j >= nparam && j < nparam + n_in + n_out &&
1216 (isl_int_is_one(bmap->eq[i][1 + j]) ||
1217 isl_int_is_negone(bmap->eq[i][1 + j])))
1218 continue;
1219 if (isl_basic_map_drop_equality(bmap, i) < 0)
1220 goto error;
1223 bmap = isl_basic_map_finalize(bmap);
1225 return bmap;
1226 error:
1227 isl_basic_map_free(bmap);
1228 return NULL;
1231 static int aff_split_cmp(const void *p1, const void *p2, void *user)
1233 const struct isl_aff_split *s1, *s2;
1234 s1 = (const struct isl_aff_split *) p1;
1235 s2 = (const struct isl_aff_split *) p2;
1237 return isl_basic_map_plain_cmp(s1->aff, s2->aff);
1240 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
1241 __isl_keep isl_basic_map *aff)
1243 int i, j;
1244 unsigned total;
1246 if (!bmap || !aff)
1247 goto error;
1249 total = isl_space_dim(bmap->dim, isl_dim_all);
1251 for (i = bmap->n_eq - 1; i >= 0; --i) {
1252 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
1253 bmap->n_div) != -1)
1254 continue;
1255 for (j = 0; j < aff->n_eq; ++j) {
1256 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
1257 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
1258 continue;
1259 if (isl_basic_map_drop_equality(bmap, i) < 0)
1260 goto error;
1261 break;
1265 return bmap;
1266 error:
1267 isl_basic_map_free(bmap);
1268 return NULL;
1271 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
1273 int i, n;
1274 struct isl_aff_split *split;
1275 isl_ctx *ctx;
1277 ctx = isl_map_get_ctx(map);
1278 split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
1279 if (!split)
1280 return NULL;
1282 for (i = 0; i < map->n; ++i) {
1283 isl_basic_map *bmap;
1284 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
1285 bmap = isl_basic_map_copy(map->p[i]);
1286 bmap = isl_basic_map_cow(bmap);
1287 bmap = drop_aff(bmap, split[i].aff);
1288 split[i].map = isl_map_from_basic_map(bmap);
1289 if (!split[i].aff || !split[i].map)
1290 goto error;
1293 if (isl_sort(split, map->n, sizeof(struct isl_aff_split),
1294 &aff_split_cmp, NULL) < 0)
1295 goto error;
1297 n = map->n;
1298 for (i = n - 1; i >= 1; --i) {
1299 if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
1300 split[i].aff))
1301 continue;
1302 isl_basic_map_free(split[i].aff);
1303 split[i - 1].map = isl_map_union(split[i - 1].map,
1304 split[i].map);
1305 if (i != n - 1)
1306 split[i] = split[n - 1];
1307 split[n - 1].aff = NULL;
1308 split[n - 1].map = NULL;
1309 --n;
1312 return split;
1313 error:
1314 free_split(split, map->n);
1315 return NULL;
1318 static int defining_equality(__isl_keep isl_basic_map *eq,
1319 __isl_keep isl_space *dim, enum isl_dim_type type, int pos)
1321 int i;
1322 unsigned total;
1324 if (!eq)
1325 return -1;
1327 pos += isl_space_offset(dim, type);
1328 total = isl_basic_map_total_dim(eq);
1330 for (i = 0; i < eq->n_eq; ++i) {
1331 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
1332 continue;
1333 if (isl_int_is_one(eq->eq[i][1 + pos]))
1334 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
1335 return i;
1338 return -1;
1341 /* Print dimension "pos" of data->space to "p".
1343 * data->user is assumed to be an isl_basic_map keeping track of equalities.
1345 * If the current dimension is defined by these equalities, then print
1346 * the corresponding expression, assigned to the name of the dimension
1347 * if there is any. Otherwise, print the name of the dimension.
1349 static __isl_give isl_printer *print_dim_eq(__isl_take isl_printer *p,
1350 struct isl_print_space_data *data, unsigned pos)
1352 isl_basic_map *eq = data->user;
1353 int j;
1355 j = defining_equality(eq, data->space, data->type, pos);
1356 if (j >= 0) {
1357 if (isl_space_has_dim_name(data->space, data->type, pos)) {
1358 p = print_name(data->space, p, data->type, pos,
1359 data->latex);
1360 p = isl_printer_print_str(p, " = ");
1362 pos += 1 + isl_space_offset(data->space, data->type);
1363 p = print_affine_of_len(data->space, NULL, p, eq->eq[j], pos);
1364 } else {
1365 p = print_name(data->space, p, data->type, pos, data->latex);
1368 return p;
1371 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
1372 struct isl_aff_split *split, int n, __isl_keep isl_space *space)
1374 struct isl_print_space_data data = { 0 };
1375 int i;
1376 int rational;
1378 data.print_dim = &print_dim_eq;
1379 for (i = 0; i < n; ++i) {
1380 if (!split[i].map)
1381 break;
1382 rational = split[i].map->n > 0 &&
1383 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
1384 if (i)
1385 p = isl_printer_print_str(p, "; ");
1386 data.user = split[i].aff;
1387 p = isl_print_space(space, p, rational, &data);
1388 p = print_disjuncts_map(split[i].map, space, p, 0);
1391 return p;
1394 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
1395 __isl_take isl_printer *p)
1397 struct isl_print_space_data data = { 0 };
1398 struct isl_aff_split *split = NULL;
1399 int rational;
1401 if (!p || !map)
1402 return isl_printer_free(p);
1403 if (!p->dump && map->n > 0)
1404 split = split_aff(map);
1405 if (split) {
1406 p = print_split_map(p, split, map->n, map->dim);
1407 } else {
1408 rational = map->n > 0 &&
1409 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
1410 p = isl_print_space(map->dim, p, rational, &data);
1411 p = print_disjuncts_map(map, map->dim, p, 0);
1413 free_split(split, map->n);
1414 return p;
1417 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
1418 __isl_take isl_printer *p)
1420 struct isl_print_space_data data = { 0 };
1422 p = print_param_tuple(p, map->dim, &data);
1423 p = isl_printer_print_str(p, s_open_set[0]);
1424 p = isl_map_print_isl_body(map, p);
1425 p = isl_printer_print_str(p, s_close_set[0]);
1426 return p;
1429 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
1430 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff)
1432 struct isl_print_space_data data = { 0 };
1434 data.latex = 1;
1435 p = print_param_tuple(p, map->dim, &data);
1436 p = isl_printer_print_str(p, s_open_set[1]);
1437 data.print_dim = &print_dim_eq;
1438 data.user = aff;
1439 p = isl_print_space(map->dim, p, 0, &data);
1440 p = print_disjuncts_map(map, map->dim, p, 1);
1441 p = isl_printer_print_str(p, s_close_set[1]);
1443 return p;
1446 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
1447 __isl_take isl_printer *p)
1449 int i;
1450 struct isl_aff_split *split = NULL;
1452 if (map->n > 0)
1453 split = split_aff(map);
1455 if (!split)
1456 return print_latex_map(map, p, NULL);
1458 for (i = 0; i < map->n; ++i) {
1459 if (!split[i].map)
1460 break;
1461 if (i)
1462 p = isl_printer_print_str(p, " \\cup ");
1463 p = print_latex_map(split[i].map, p, split[i].aff);
1466 free_split(split, map->n);
1467 return p;
1470 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
1471 __isl_keep isl_basic_map *bmap)
1473 if (!p || !bmap)
1474 goto error;
1475 if (p->output_format == ISL_FORMAT_ISL)
1476 return isl_basic_map_print_isl(bmap, p, 0);
1477 else if (p->output_format == ISL_FORMAT_OMEGA)
1478 return basic_map_print_omega(bmap, p);
1479 isl_assert(bmap->ctx, 0, goto error);
1480 error:
1481 isl_printer_free(p);
1482 return NULL;
1485 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
1486 __isl_keep isl_basic_set *bset)
1488 if (!p || !bset)
1489 goto error;
1491 if (p->output_format == ISL_FORMAT_ISL)
1492 return isl_basic_map_print_isl(bset, p, 0);
1493 else if (p->output_format == ISL_FORMAT_POLYLIB)
1494 return isl_basic_set_print_polylib(bset, p, 0);
1495 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1496 return isl_basic_set_print_polylib(bset, p, 1);
1497 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
1498 return bset_print_constraints_polylib(bset, p);
1499 else if (p->output_format == ISL_FORMAT_OMEGA)
1500 return basic_set_print_omega(bset, p);
1501 isl_assert(p->ctx, 0, goto error);
1502 error:
1503 isl_printer_free(p);
1504 return NULL;
1507 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
1508 __isl_keep isl_set *set)
1510 if (!p || !set)
1511 goto error;
1512 if (p->output_format == ISL_FORMAT_ISL)
1513 return isl_map_print_isl(set_to_map(set), p);
1514 else if (p->output_format == ISL_FORMAT_POLYLIB)
1515 return isl_set_print_polylib(set, p, 0);
1516 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1517 return isl_set_print_polylib(set, p, 1);
1518 else if (p->output_format == ISL_FORMAT_OMEGA)
1519 return isl_set_print_omega(set, p);
1520 else if (p->output_format == ISL_FORMAT_LATEX)
1521 return isl_map_print_latex(set_to_map(set), p);
1522 isl_assert(set->ctx, 0, goto error);
1523 error:
1524 isl_printer_free(p);
1525 return NULL;
1528 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1529 __isl_keep isl_map *map)
1531 if (!p || !map)
1532 goto error;
1534 if (p->output_format == ISL_FORMAT_ISL)
1535 return isl_map_print_isl(map, p);
1536 else if (p->output_format == ISL_FORMAT_POLYLIB)
1537 return isl_map_print_polylib(map, p, 0);
1538 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1539 return isl_map_print_polylib(map, p, 1);
1540 else if (p->output_format == ISL_FORMAT_OMEGA)
1541 return isl_map_print_omega(map, p);
1542 else if (p->output_format == ISL_FORMAT_LATEX)
1543 return isl_map_print_latex(map, p);
1544 isl_assert(map->ctx, 0, goto error);
1545 error:
1546 isl_printer_free(p);
1547 return NULL;
1550 struct isl_union_print_data {
1551 isl_printer *p;
1552 int first;
1555 static isl_stat print_map_body(__isl_take isl_map *map, void *user)
1557 struct isl_union_print_data *data;
1558 data = (struct isl_union_print_data *)user;
1560 if (!data->first)
1561 data->p = isl_printer_print_str(data->p, "; ");
1562 data->first = 0;
1564 data->p = isl_map_print_isl_body(map, data->p);
1565 isl_map_free(map);
1567 return isl_stat_ok;
1570 /* Print the body of "umap" (everything except the parameter declarations)
1571 * to "p" in isl format.
1573 static __isl_give isl_printer *isl_printer_print_union_map_isl_body(
1574 __isl_take isl_printer *p, __isl_keep isl_union_map *umap)
1576 struct isl_union_print_data data;
1578 p = isl_printer_print_str(p, s_open_set[0]);
1579 data.p = p;
1580 data.first = 1;
1581 isl_union_map_foreach_map(umap, &print_map_body, &data);
1582 p = data.p;
1583 p = isl_printer_print_str(p, s_close_set[0]);
1584 return p;
1587 /* Print the isl_union_map "umap" to "p" in isl format.
1589 static __isl_give isl_printer *isl_union_map_print_isl(
1590 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1592 struct isl_print_space_data space_data = { 0 };
1593 isl_space *space;
1595 space = isl_union_map_get_space(umap);
1596 p = print_param_tuple(p, space, &space_data);
1597 isl_space_free(space);
1599 p = isl_printer_print_union_map_isl_body(p, umap);
1601 return p;
1604 static isl_stat print_latex_map_body(__isl_take isl_map *map, void *user)
1606 struct isl_union_print_data *data;
1607 data = (struct isl_union_print_data *)user;
1609 if (!data->first)
1610 data->p = isl_printer_print_str(data->p, " \\cup ");
1611 data->first = 0;
1613 data->p = isl_map_print_latex(map, data->p);
1614 isl_map_free(map);
1616 return isl_stat_ok;
1619 static __isl_give isl_printer *isl_union_map_print_latex(
1620 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1622 struct isl_union_print_data data = { p, 1 };
1623 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1624 p = data.p;
1625 return p;
1628 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1629 __isl_keep isl_union_map *umap)
1631 if (!p || !umap)
1632 goto error;
1634 if (p->output_format == ISL_FORMAT_ISL)
1635 return isl_union_map_print_isl(umap, p);
1636 if (p->output_format == ISL_FORMAT_LATEX)
1637 return isl_union_map_print_latex(umap, p);
1639 isl_die(p->ctx, isl_error_invalid,
1640 "invalid output format for isl_union_map", goto error);
1641 error:
1642 isl_printer_free(p);
1643 return NULL;
1646 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1647 __isl_keep isl_union_set *uset)
1649 if (!p || !uset)
1650 goto error;
1652 if (p->output_format == ISL_FORMAT_ISL)
1653 return isl_union_map_print_isl(uset_to_umap(uset), p);
1654 if (p->output_format == ISL_FORMAT_LATEX)
1655 return isl_union_map_print_latex(uset_to_umap(uset), p);
1657 isl_die(p->ctx, isl_error_invalid,
1658 "invalid output format for isl_union_set", goto error);
1659 error:
1660 isl_printer_free(p);
1661 return NULL;
1664 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
1666 int i;
1667 int n;
1669 for (i = 0, n = 0; i < rec->n; ++i)
1670 if (!isl_upoly_is_zero(rec->p[i]))
1671 ++n;
1673 return n;
1676 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
1677 __isl_take isl_printer *p, int first)
1679 struct isl_upoly_cst *cst;
1680 int neg;
1682 cst = isl_upoly_as_cst(up);
1683 if (!cst)
1684 goto error;
1685 neg = !first && isl_int_is_neg(cst->n);
1686 if (!first)
1687 p = isl_printer_print_str(p, neg ? " - " : " + ");
1688 if (neg)
1689 isl_int_neg(cst->n, cst->n);
1690 if (isl_int_is_zero(cst->d)) {
1691 int sgn = isl_int_sgn(cst->n);
1692 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1693 sgn == 0 ? "NaN" : "infty");
1694 } else
1695 p = isl_printer_print_isl_int(p, cst->n);
1696 if (neg)
1697 isl_int_neg(cst->n, cst->n);
1698 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1699 p = isl_printer_print_str(p, "/");
1700 p = isl_printer_print_isl_int(p, cst->d);
1702 return p;
1703 error:
1704 isl_printer_free(p);
1705 return NULL;
1708 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1709 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var)
1711 unsigned total;
1713 total = isl_space_dim(dim, isl_dim_all);
1714 if (var < total)
1715 p = print_term(dim, NULL, dim->ctx->one, 1 + var, p, 0);
1716 else
1717 p = print_div(dim, div, var - total, p);
1718 return p;
1721 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1722 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var, int exp)
1724 p = print_base(p, dim, div, var);
1725 if (exp == 1)
1726 return p;
1727 if (p->output_format == ISL_FORMAT_C) {
1728 int i;
1729 for (i = 1; i < exp; ++i) {
1730 p = isl_printer_print_str(p, "*");
1731 p = print_base(p, dim, div, var);
1733 } else {
1734 p = isl_printer_print_str(p, "^");
1735 p = isl_printer_print_int(p, exp);
1737 return p;
1740 /* Print the polynomial "up" defined over the domain space "space" and
1741 * local variables defined by "div" to "p".
1743 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1744 __isl_keep isl_space *space, __isl_keep isl_mat *div,
1745 __isl_take isl_printer *p)
1747 int i, n, first, print_parens;
1748 struct isl_upoly_rec *rec;
1750 if (!p || !up || !space || !div)
1751 goto error;
1753 if (isl_upoly_is_cst(up))
1754 return upoly_print_cst(up, p, 1);
1756 rec = isl_upoly_as_rec(up);
1757 if (!rec)
1758 goto error;
1759 n = upoly_rec_n_non_zero(rec);
1760 print_parens = n > 1;
1761 if (print_parens)
1762 p = isl_printer_print_str(p, "(");
1763 for (i = 0, first = 1; i < rec->n; ++i) {
1764 if (isl_upoly_is_zero(rec->p[i]))
1765 continue;
1766 if (isl_upoly_is_negone(rec->p[i])) {
1767 if (!i)
1768 p = isl_printer_print_str(p, "-1");
1769 else if (first)
1770 p = isl_printer_print_str(p, "-");
1771 else
1772 p = isl_printer_print_str(p, " - ");
1773 } else if (isl_upoly_is_cst(rec->p[i]) &&
1774 !isl_upoly_is_one(rec->p[i]))
1775 p = upoly_print_cst(rec->p[i], p, first);
1776 else {
1777 if (!first)
1778 p = isl_printer_print_str(p, " + ");
1779 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1780 p = upoly_print(rec->p[i], space, div, p);
1782 first = 0;
1783 if (i == 0)
1784 continue;
1785 if (!isl_upoly_is_one(rec->p[i]) &&
1786 !isl_upoly_is_negone(rec->p[i]))
1787 p = isl_printer_print_str(p, " * ");
1788 p = print_pow(p, space, div, rec->up.var, i);
1790 if (print_parens)
1791 p = isl_printer_print_str(p, ")");
1792 return p;
1793 error:
1794 isl_printer_free(p);
1795 return NULL;
1798 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1799 __isl_keep isl_qpolynomial *qp)
1801 if (!p || !qp)
1802 goto error;
1803 p = upoly_print(qp->upoly, qp->dim, qp->div, p);
1804 return p;
1805 error:
1806 isl_printer_free(p);
1807 return NULL;
1810 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1811 __isl_keep isl_qpolynomial *qp)
1813 struct isl_print_space_data data = { 0 };
1815 if (!p || !qp)
1816 goto error;
1818 p = print_param_tuple(p, qp->dim, &data);
1819 p = isl_printer_print_str(p, "{ ");
1820 if (!isl_space_is_params(qp->dim)) {
1821 p = isl_print_space(qp->dim, p, 0, &data);
1822 p = isl_printer_print_str(p, " -> ");
1824 p = print_qpolynomial(p, qp);
1825 p = isl_printer_print_str(p, " }");
1826 return p;
1827 error:
1828 isl_printer_free(p);
1829 return NULL;
1832 /* Print the quasi-polynomial "qp" to "p" in C format, with the variable names
1833 * taken from the domain space "space".
1835 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1836 __isl_keep isl_space *space, __isl_keep isl_qpolynomial *qp)
1838 isl_int den;
1840 isl_int_init(den);
1841 isl_qpolynomial_get_den(qp, &den);
1842 if (!isl_int_is_one(den)) {
1843 isl_qpolynomial *f;
1844 p = isl_printer_print_str(p, "(");
1845 qp = isl_qpolynomial_copy(qp);
1846 f = isl_qpolynomial_rat_cst_on_domain(isl_space_copy(qp->dim),
1847 den, qp->dim->ctx->one);
1848 qp = isl_qpolynomial_mul(qp, f);
1850 if (qp)
1851 p = upoly_print(qp->upoly, space, qp->div, p);
1852 else
1853 p = isl_printer_free(p);
1854 if (!isl_int_is_one(den)) {
1855 p = isl_printer_print_str(p, ")/");
1856 p = isl_printer_print_isl_int(p, den);
1857 isl_qpolynomial_free(qp);
1859 isl_int_clear(den);
1860 return p;
1863 __isl_give isl_printer *isl_printer_print_qpolynomial(
1864 __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1866 if (!p || !qp)
1867 goto error;
1869 if (p->output_format == ISL_FORMAT_ISL)
1870 return print_qpolynomial_isl(p, qp);
1871 else if (p->output_format == ISL_FORMAT_C)
1872 return print_qpolynomial_c(p, qp->dim, qp);
1873 else
1874 isl_die(qp->dim->ctx, isl_error_unsupported,
1875 "output format not supported for isl_qpolynomials",
1876 goto error);
1877 error:
1878 isl_printer_free(p);
1879 return NULL;
1882 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1883 unsigned output_format)
1885 isl_printer *p;
1887 if (!qp)
1888 return;
1890 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1891 p = isl_printer_to_file(qp->dim->ctx, out);
1892 p = isl_printer_print_qpolynomial(p, qp);
1893 isl_printer_free(p);
1896 static __isl_give isl_printer *qpolynomial_fold_print(
1897 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1899 int i;
1901 if (fold->type == isl_fold_min)
1902 p = isl_printer_print_str(p, "min");
1903 else if (fold->type == isl_fold_max)
1904 p = isl_printer_print_str(p, "max");
1905 p = isl_printer_print_str(p, "(");
1906 for (i = 0; i < fold->n; ++i) {
1907 if (i)
1908 p = isl_printer_print_str(p, ", ");
1909 p = print_qpolynomial(p, fold->qp[i]);
1911 p = isl_printer_print_str(p, ")");
1912 return p;
1915 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1916 FILE *out, unsigned output_format)
1918 isl_printer *p;
1920 if (!fold)
1921 return;
1923 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1925 p = isl_printer_to_file(fold->dim->ctx, out);
1926 p = isl_printer_print_qpolynomial_fold(p, fold);
1928 isl_printer_free(p);
1931 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1932 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1934 struct isl_print_space_data data = { 0 };
1935 int i = 0;
1937 for (i = 0; i < pwqp->n; ++i) {
1938 isl_space *space;
1940 if (i)
1941 p = isl_printer_print_str(p, "; ");
1942 space = isl_qpolynomial_get_domain_space(pwqp->p[i].qp);
1943 if (!isl_space_is_params(space)) {
1944 p = isl_print_space(space, p, 0, &data);
1945 p = isl_printer_print_str(p, " -> ");
1947 p = print_qpolynomial(p, pwqp->p[i].qp);
1948 p = print_disjuncts(set_to_map(pwqp->p[i].set), space, p, 0);
1949 isl_space_free(space);
1952 return p;
1955 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1956 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1958 struct isl_print_space_data data = { 0 };
1960 if (!p || !pwqp)
1961 goto error;
1963 p = print_param_tuple(p, pwqp->dim, &data);
1964 p = isl_printer_print_str(p, "{ ");
1965 if (pwqp->n == 0) {
1966 if (!isl_space_is_set(pwqp->dim)) {
1967 p = print_tuple(pwqp->dim, p, isl_dim_in, &data);
1968 p = isl_printer_print_str(p, " -> ");
1970 p = isl_printer_print_str(p, "0");
1972 p = isl_pwqp_print_isl_body(p, pwqp);
1973 p = isl_printer_print_str(p, " }");
1974 return p;
1975 error:
1976 isl_printer_free(p);
1977 return NULL;
1980 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
1981 unsigned output_format)
1983 isl_printer *p;
1985 if (!pwqp)
1986 return;
1988 p = isl_printer_to_file(pwqp->dim->ctx, out);
1989 p = isl_printer_set_output_format(p, output_format);
1990 p = isl_printer_print_pw_qpolynomial(p, pwqp);
1992 isl_printer_free(p);
1995 static __isl_give isl_printer *isl_pwf_print_isl_body(
1996 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1998 struct isl_print_space_data data = { 0 };
1999 int i = 0;
2001 for (i = 0; i < pwf->n; ++i) {
2002 isl_space *space;
2004 if (i)
2005 p = isl_printer_print_str(p, "; ");
2006 space = isl_qpolynomial_fold_get_domain_space(pwf->p[i].fold);
2007 if (!isl_space_is_params(space)) {
2008 p = isl_print_space(space, p, 0, &data);
2009 p = isl_printer_print_str(p, " -> ");
2011 p = qpolynomial_fold_print(pwf->p[i].fold, p);
2012 p = print_disjuncts(set_to_map(pwf->p[i].set), space, p, 0);
2013 isl_space_free(space);
2016 return p;
2019 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
2020 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2022 struct isl_print_space_data data = { 0 };
2024 p = print_param_tuple(p, pwf->dim, &data);
2025 p = isl_printer_print_str(p, "{ ");
2026 if (pwf->n == 0) {
2027 if (!isl_space_is_set(pwf->dim)) {
2028 p = print_tuple(pwf->dim, p, isl_dim_in, &data);
2029 p = isl_printer_print_str(p, " -> ");
2031 p = isl_printer_print_str(p, "0");
2033 p = isl_pwf_print_isl_body(p, pwf);
2034 p = isl_printer_print_str(p, " }");
2035 return p;
2038 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
2039 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c);
2041 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
2042 __isl_keep isl_space *dim,
2043 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
2045 if (type == isl_dim_div) {
2046 p = isl_printer_print_str(p, "floord(");
2047 p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
2048 p = isl_printer_print_str(p, ", ");
2049 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
2050 p = isl_printer_print_str(p, ")");
2051 } else {
2052 const char *name;
2054 name = isl_space_get_dim_name(dim, type, pos);
2055 if (!name)
2056 name = "UNNAMED";
2057 p = isl_printer_print_str(p, name);
2059 return p;
2062 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
2063 __isl_keep isl_space *dim,
2064 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
2066 enum isl_dim_type type;
2068 if (pos == 0)
2069 return isl_printer_print_isl_int(p, c);
2071 if (isl_int_is_one(c))
2073 else if (isl_int_is_negone(c))
2074 p = isl_printer_print_str(p, "-");
2075 else {
2076 p = isl_printer_print_isl_int(p, c);
2077 p = isl_printer_print_str(p, "*");
2079 type = pos2type(dim, &pos);
2080 p = print_name_c(p, dim, bset, type, pos);
2081 return p;
2084 static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
2085 __isl_keep isl_space *dim,
2086 __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
2088 int i;
2089 int first;
2091 for (i = 0, first = 1; i < len; ++i) {
2092 int flip = 0;
2093 if (isl_int_is_zero(c[i]))
2094 continue;
2095 if (!first) {
2096 if (isl_int_is_neg(c[i])) {
2097 flip = 1;
2098 isl_int_neg(c[i], c[i]);
2099 p = isl_printer_print_str(p, " - ");
2100 } else
2101 p = isl_printer_print_str(p, " + ");
2103 first = 0;
2104 p = print_term_c(p, dim, bset, c[i], i);
2105 if (flip)
2106 isl_int_neg(c[i], c[i]);
2108 if (first)
2109 p = isl_printer_print_str(p, "0");
2110 return p;
2113 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
2114 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c)
2116 unsigned len = 1 + isl_basic_set_total_dim(bset);
2117 return print_partial_affine_c(p, dim, bset, c, len);
2120 /* We skip the constraint if it is implied by the div expression.
2122 * *first indicates whether this is the first constraint in the conjunction and
2123 * is updated if the constraint is actually printed.
2125 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
2126 __isl_keep isl_space *dim,
2127 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int *first)
2129 unsigned o_div;
2130 unsigned n_div;
2131 int div;
2133 o_div = isl_basic_set_offset(bset, isl_dim_div);
2134 n_div = isl_basic_set_dim(bset, isl_dim_div);
2135 div = isl_seq_last_non_zero(c + o_div, n_div);
2136 if (div >= 0) {
2137 isl_bool is_div = isl_basic_set_is_div_constraint(bset, c, div);
2138 if (is_div < 0)
2139 return isl_printer_free(p);
2140 if (is_div)
2141 return p;
2144 if (!*first)
2145 p = isl_printer_print_str(p, " && ");
2147 p = print_affine_c(p, dim, bset, c);
2148 p = isl_printer_print_str(p, " ");
2149 p = isl_printer_print_str(p, op);
2150 p = isl_printer_print_str(p, " 0");
2152 *first = 0;
2154 return p;
2157 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
2158 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset)
2160 int i, j;
2161 int first = 1;
2162 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
2163 unsigned total = isl_basic_set_total_dim(bset) - n_div;
2165 for (i = 0; i < bset->n_eq; ++i) {
2166 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
2167 if (j < 0)
2168 p = print_constraint_c(p, dim, bset,
2169 bset->eq[i], "==", &first);
2170 else {
2171 if (i)
2172 p = isl_printer_print_str(p, " && ");
2173 p = isl_printer_print_str(p, "(");
2174 p = print_partial_affine_c(p, dim, bset, bset->eq[i],
2175 1 + total + j);
2176 p = isl_printer_print_str(p, ") % ");
2177 p = isl_printer_print_isl_int(p,
2178 bset->eq[i][1 + total + j]);
2179 p = isl_printer_print_str(p, " == 0");
2180 first = 0;
2183 for (i = 0; i < bset->n_ineq; ++i)
2184 p = print_constraint_c(p, dim, bset, bset->ineq[i], ">=",
2185 &first);
2186 return p;
2189 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
2190 __isl_keep isl_space *dim, __isl_keep isl_set *set)
2192 int i;
2194 if (!set)
2195 return isl_printer_free(p);
2197 if (set->n == 0)
2198 p = isl_printer_print_str(p, "0");
2200 for (i = 0; i < set->n; ++i) {
2201 if (i)
2202 p = isl_printer_print_str(p, " || ");
2203 if (set->n > 1)
2204 p = isl_printer_print_str(p, "(");
2205 p = print_basic_set_c(p, dim, set->p[i]);
2206 if (set->n > 1)
2207 p = isl_printer_print_str(p, ")");
2209 return p;
2212 /* Print the piecewise quasi-polynomial "pwqp" to "p" in C format.
2214 static __isl_give isl_printer *print_pw_qpolynomial_c(
2215 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2217 int i;
2218 isl_space *space;
2220 space = isl_pw_qpolynomial_get_domain_space(pwqp);
2221 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set)) {
2222 p = print_qpolynomial_c(p, space, pwqp->p[0].qp);
2223 isl_space_free(space);
2224 return p;
2227 for (i = 0; i < pwqp->n; ++i) {
2228 p = isl_printer_print_str(p, "(");
2229 p = print_set_c(p, space, pwqp->p[i].set);
2230 p = isl_printer_print_str(p, ") ? (");
2231 p = print_qpolynomial_c(p, space, pwqp->p[i].qp);
2232 p = isl_printer_print_str(p, ") : ");
2235 isl_space_free(space);
2236 p = isl_printer_print_str(p, "0");
2237 return p;
2240 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
2241 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2243 if (!p || !pwqp)
2244 goto error;
2246 if (p->output_format == ISL_FORMAT_ISL)
2247 return print_pw_qpolynomial_isl(p, pwqp);
2248 else if (p->output_format == ISL_FORMAT_C)
2249 return print_pw_qpolynomial_c(p, pwqp);
2250 isl_assert(p->ctx, 0, goto error);
2251 error:
2252 isl_printer_free(p);
2253 return NULL;
2256 static isl_stat print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
2258 struct isl_union_print_data *data;
2259 data = (struct isl_union_print_data *)user;
2261 if (!data->first)
2262 data->p = isl_printer_print_str(data->p, "; ");
2263 data->first = 0;
2265 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
2266 isl_pw_qpolynomial_free(pwqp);
2268 return isl_stat_ok;
2271 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
2272 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2274 struct isl_union_print_data data;
2275 struct isl_print_space_data space_data = { 0 };
2276 isl_space *space;
2278 space = isl_union_pw_qpolynomial_get_space(upwqp);
2279 p = print_param_tuple(p, space, &space_data);
2280 isl_space_free(space);
2281 p = isl_printer_print_str(p, "{ ");
2282 data.p = p;
2283 data.first = 1;
2284 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
2285 &data);
2286 p = data.p;
2287 p = isl_printer_print_str(p, " }");
2288 return p;
2291 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
2292 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2294 if (!p || !upwqp)
2295 goto error;
2297 if (p->output_format == ISL_FORMAT_ISL)
2298 return print_union_pw_qpolynomial_isl(p, upwqp);
2299 isl_die(p->ctx, isl_error_invalid,
2300 "invalid output format for isl_union_pw_qpolynomial",
2301 goto error);
2302 error:
2303 isl_printer_free(p);
2304 return NULL;
2307 /* Print the quasi-polynomial reduction "fold" to "p" in C format,
2308 * with the variable names taken from the domain space "space".
2310 static __isl_give isl_printer *print_qpolynomial_fold_c(
2311 __isl_take isl_printer *p, __isl_keep isl_space *space,
2312 __isl_keep isl_qpolynomial_fold *fold)
2314 int i;
2316 for (i = 0; i < fold->n - 1; ++i)
2317 if (fold->type == isl_fold_min)
2318 p = isl_printer_print_str(p, "min(");
2319 else if (fold->type == isl_fold_max)
2320 p = isl_printer_print_str(p, "max(");
2322 for (i = 0; i < fold->n; ++i) {
2323 if (i)
2324 p = isl_printer_print_str(p, ", ");
2325 p = print_qpolynomial_c(p, space, fold->qp[i]);
2326 if (i)
2327 p = isl_printer_print_str(p, ")");
2329 return p;
2332 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
2333 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
2335 if (!p || !fold)
2336 goto error;
2337 if (p->output_format == ISL_FORMAT_ISL)
2338 return qpolynomial_fold_print(fold, p);
2339 else if (p->output_format == ISL_FORMAT_C)
2340 return print_qpolynomial_fold_c(p, fold->dim, fold);
2341 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2342 goto error);
2343 error:
2344 isl_printer_free(p);
2345 return NULL;
2348 /* Print the piecewise quasi-polynomial reduction "pwf" to "p" in C format.
2350 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
2351 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2353 int i;
2354 isl_space *space;
2356 space = isl_pw_qpolynomial_fold_get_domain_space(pwf);
2357 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set)) {
2358 p = print_qpolynomial_fold_c(p, space, pwf->p[0].fold);
2359 isl_space_free(space);
2360 return p;
2363 for (i = 0; i < pwf->n; ++i) {
2364 p = isl_printer_print_str(p, "(");
2365 p = print_set_c(p, space, pwf->p[i].set);
2366 p = isl_printer_print_str(p, ") ? (");
2367 p = print_qpolynomial_fold_c(p, space, pwf->p[i].fold);
2368 p = isl_printer_print_str(p, ") : ");
2371 isl_space_free(space);
2372 p = isl_printer_print_str(p, "0");
2373 return p;
2376 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
2377 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2379 if (!p || !pwf)
2380 goto error;
2382 if (p->output_format == ISL_FORMAT_ISL)
2383 return print_pw_qpolynomial_fold_isl(p, pwf);
2384 else if (p->output_format == ISL_FORMAT_C)
2385 return print_pw_qpolynomial_fold_c(p, pwf);
2386 isl_assert(p->ctx, 0, goto error);
2387 error:
2388 isl_printer_free(p);
2389 return NULL;
2392 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
2393 FILE *out, unsigned output_format)
2395 isl_printer *p;
2397 if (!pwf)
2398 return;
2400 p = isl_printer_to_file(pwf->dim->ctx, out);
2401 p = isl_printer_set_output_format(p, output_format);
2402 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
2404 isl_printer_free(p);
2407 static isl_stat print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf,
2408 void *user)
2410 struct isl_union_print_data *data;
2411 data = (struct isl_union_print_data *)user;
2413 if (!data->first)
2414 data->p = isl_printer_print_str(data->p, "; ");
2415 data->first = 0;
2417 data->p = isl_pwf_print_isl_body(data->p, pwf);
2418 isl_pw_qpolynomial_fold_free(pwf);
2420 return isl_stat_ok;
2423 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
2424 __isl_take isl_printer *p,
2425 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2427 struct isl_union_print_data data;
2428 struct isl_print_space_data space_data = { 0 };
2429 isl_space *space;
2431 space = isl_union_pw_qpolynomial_fold_get_space(upwf);
2432 p = print_param_tuple(p, space, &space_data);
2433 isl_space_free(space);
2434 p = isl_printer_print_str(p, "{ ");
2435 data.p = p;
2436 data.first = 1;
2437 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
2438 &print_pwf_body, &data);
2439 p = data.p;
2440 p = isl_printer_print_str(p, " }");
2441 return p;
2444 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2445 __isl_take isl_printer *p,
2446 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2448 if (!p || !upwf)
2449 goto error;
2451 if (p->output_format == ISL_FORMAT_ISL)
2452 return print_union_pw_qpolynomial_fold_isl(p, upwf);
2453 isl_die(p->ctx, isl_error_invalid,
2454 "invalid output format for isl_union_pw_qpolynomial_fold",
2455 goto error);
2456 error:
2457 isl_printer_free(p);
2458 return NULL;
2461 /* Print the isl_constraint "c" to "p".
2463 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
2464 __isl_keep isl_constraint *c)
2466 struct isl_print_space_data data = { 0 };
2467 isl_local_space *ls;
2468 isl_space *space;
2469 isl_bool exists;
2471 if (!p || !c)
2472 goto error;
2474 ls = isl_constraint_get_local_space(c);
2475 if (!ls)
2476 return isl_printer_free(p);
2477 space = isl_local_space_get_space(ls);
2478 p = print_param_tuple(p, space, &data);
2479 p = isl_printer_print_str(p, "{ ");
2480 p = isl_print_space(space, p, 0, &data);
2481 p = isl_printer_print_str(p, " : ");
2482 exists = need_exists(p, ls->div);
2483 if (exists < 0)
2484 p = isl_printer_free(p);
2485 if (exists >= 0 && exists)
2486 p = open_exists(p, space, ls->div, 0);
2487 p = print_affine_of_len(space, ls->div, p, c->v->el, c->v->size);
2488 if (isl_constraint_is_equality(c))
2489 p = isl_printer_print_str(p, " = 0");
2490 else
2491 p = isl_printer_print_str(p, " >= 0");
2492 if (exists >= 0 && exists)
2493 p = isl_printer_print_str(p, s_close_exists[0]);
2494 p = isl_printer_print_str(p, " }");
2495 isl_space_free(space);
2496 isl_local_space_free(ls);
2498 return p;
2499 error:
2500 isl_printer_free(p);
2501 return NULL;
2504 static __isl_give isl_printer *isl_printer_print_space_isl(
2505 __isl_take isl_printer *p, __isl_keep isl_space *space)
2507 struct isl_print_space_data data = { 0 };
2509 if (!space)
2510 goto error;
2512 p = print_param_tuple(p, space, &data);
2514 p = isl_printer_print_str(p, "{ ");
2515 if (isl_space_is_params(space))
2516 p = isl_printer_print_str(p, s_such_that[0]);
2517 else
2518 p = isl_print_space(space, p, 0, &data);
2519 p = isl_printer_print_str(p, " }");
2521 return p;
2522 error:
2523 isl_printer_free(p);
2524 return NULL;
2527 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
2528 __isl_keep isl_space *space)
2530 if (!p || !space)
2531 return isl_printer_free(p);
2532 if (p->output_format == ISL_FORMAT_ISL)
2533 return isl_printer_print_space_isl(p, space);
2534 else if (p->output_format == ISL_FORMAT_OMEGA)
2535 return print_omega_parameters(space, p);
2537 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
2538 "output format not supported for space",
2539 return isl_printer_free(p));
2542 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
2543 __isl_keep isl_local_space *ls)
2545 struct isl_print_space_data data = { 0 };
2546 unsigned n_div;
2548 if (!ls)
2549 goto error;
2551 p = print_param_tuple(p, ls->dim, &data);
2552 p = isl_printer_print_str(p, "{ ");
2553 p = isl_print_space(ls->dim, p, 0, &data);
2554 n_div = isl_local_space_dim(ls, isl_dim_div);
2555 if (n_div > 0) {
2556 p = isl_printer_print_str(p, " : ");
2557 p = isl_printer_print_str(p, s_open_exists[0]);
2558 p = print_div_list(p, ls->dim, ls->div, 0, 1);
2559 p = isl_printer_print_str(p, s_close_exists[0]);
2560 } else if (isl_space_is_params(ls->dim))
2561 p = isl_printer_print_str(p, s_such_that[0]);
2562 p = isl_printer_print_str(p, " }");
2563 return p;
2564 error:
2565 isl_printer_free(p);
2566 return NULL;
2569 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2570 __isl_keep isl_aff *aff)
2572 unsigned total;
2574 if (isl_aff_is_nan(aff))
2575 return isl_printer_print_str(p, "NaN");
2577 total = isl_local_space_dim(aff->ls, isl_dim_all);
2578 p = isl_printer_print_str(p, "(");
2579 p = print_affine_of_len(aff->ls->dim, aff->ls->div, p,
2580 aff->v->el + 1, 1 + total);
2581 if (isl_int_is_one(aff->v->el[0]))
2582 p = isl_printer_print_str(p, ")");
2583 else {
2584 p = isl_printer_print_str(p, ")/");
2585 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2588 return p;
2591 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2592 __isl_keep isl_aff *aff)
2594 struct isl_print_space_data data = { 0 };
2596 if (isl_space_is_params(aff->ls->dim))
2598 else {
2599 p = print_tuple(aff->ls->dim, p, isl_dim_set, &data);
2600 p = isl_printer_print_str(p, " -> ");
2602 p = isl_printer_print_str(p, "[");
2603 p = print_aff_body(p, aff);
2604 p = isl_printer_print_str(p, "]");
2606 return p;
2609 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2610 __isl_keep isl_aff *aff)
2612 struct isl_print_space_data data = { 0 };
2614 if (!aff)
2615 goto error;
2617 p = print_param_tuple(p, aff->ls->dim, &data);
2618 p = isl_printer_print_str(p, "{ ");
2619 p = print_aff(p, aff);
2620 p = isl_printer_print_str(p, " }");
2621 return p;
2622 error:
2623 isl_printer_free(p);
2624 return NULL;
2627 /* Print the body of an isl_pw_aff, i.e., a semicolon delimited
2628 * sequence of affine expressions, each followed by constraints.
2630 static __isl_give isl_printer *print_pw_aff_body(
2631 __isl_take isl_printer *p, __isl_keep isl_pw_aff *pa)
2633 int i;
2635 if (!pa)
2636 return isl_printer_free(p);
2638 for (i = 0; i < pa->n; ++i) {
2639 isl_space *space;
2641 if (i)
2642 p = isl_printer_print_str(p, "; ");
2643 p = print_aff(p, pa->p[i].aff);
2644 space = isl_aff_get_domain_space(pa->p[i].aff);
2645 p = print_disjuncts(set_to_map(pa->p[i].set), space, p, 0);
2646 isl_space_free(space);
2649 return p;
2652 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2653 __isl_keep isl_pw_aff *pwaff)
2655 struct isl_print_space_data data = { 0 };
2657 if (!pwaff)
2658 goto error;
2660 p = print_param_tuple(p, pwaff->dim, &data);
2661 p = isl_printer_print_str(p, "{ ");
2662 p = print_pw_aff_body(p, pwaff);
2663 p = isl_printer_print_str(p, " }");
2664 return p;
2665 error:
2666 isl_printer_free(p);
2667 return NULL;
2670 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2671 __isl_keep isl_local_space *ls, isl_int *c);
2673 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2674 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2676 if (type == isl_dim_div) {
2677 p = isl_printer_print_str(p, "floord(");
2678 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2679 p = isl_printer_print_str(p, ", ");
2680 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2681 p = isl_printer_print_str(p, ")");
2682 } else {
2683 const char *name;
2685 name = isl_space_get_dim_name(ls->dim, type, pos);
2686 if (!name)
2687 name = "UNNAMED";
2688 p = isl_printer_print_str(p, name);
2690 return p;
2693 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2694 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2696 enum isl_dim_type type;
2698 if (pos == 0)
2699 return isl_printer_print_isl_int(p, c);
2701 if (isl_int_is_one(c))
2703 else if (isl_int_is_negone(c))
2704 p = isl_printer_print_str(p, "-");
2705 else {
2706 p = isl_printer_print_isl_int(p, c);
2707 p = isl_printer_print_str(p, "*");
2709 type = pos2type(ls->dim, &pos);
2710 p = print_ls_name_c(p, ls, type, pos);
2711 return p;
2714 static __isl_give isl_printer *print_ls_partial_affine_c(
2715 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2716 isl_int *c, unsigned len)
2718 int i;
2719 int first;
2721 for (i = 0, first = 1; i < len; ++i) {
2722 int flip = 0;
2723 if (isl_int_is_zero(c[i]))
2724 continue;
2725 if (!first) {
2726 if (isl_int_is_neg(c[i])) {
2727 flip = 1;
2728 isl_int_neg(c[i], c[i]);
2729 p = isl_printer_print_str(p, " - ");
2730 } else
2731 p = isl_printer_print_str(p, " + ");
2733 first = 0;
2734 p = print_ls_term_c(p, ls, c[i], i);
2735 if (flip)
2736 isl_int_neg(c[i], c[i]);
2738 if (first)
2739 p = isl_printer_print_str(p, "0");
2740 return p;
2743 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2744 __isl_keep isl_local_space *ls, isl_int *c)
2746 unsigned len = 1 + isl_local_space_dim(ls, isl_dim_all);
2747 return print_ls_partial_affine_c(p, ls, c, len);
2750 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2751 __isl_keep isl_aff *aff)
2753 unsigned total;
2755 total = isl_local_space_dim(aff->ls, isl_dim_all);
2756 if (!isl_int_is_one(aff->v->el[0]))
2757 p = isl_printer_print_str(p, "(");
2758 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2759 if (!isl_int_is_one(aff->v->el[0])) {
2760 p = isl_printer_print_str(p, ")/");
2761 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2763 return p;
2766 /* In the C format, we cannot express that "pwaff" may be undefined
2767 * on parts of the domain space. We therefore assume that the expression
2768 * will only be evaluated on its definition domain and compute the gist
2769 * of each cell with respect to this domain.
2771 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2772 __isl_keep isl_pw_aff *pwaff)
2774 isl_set *domain;
2775 isl_ast_build *build;
2776 isl_ast_expr *expr;
2778 if (pwaff->n < 1)
2779 isl_die(p->ctx, isl_error_unsupported,
2780 "cannot print empty isl_pw_aff in C format",
2781 return isl_printer_free(p));
2783 domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
2784 build = isl_ast_build_from_context(domain);
2785 expr = isl_ast_build_expr_from_pw_aff(build, isl_pw_aff_copy(pwaff));
2786 p = isl_printer_print_ast_expr(p, expr);
2787 isl_ast_expr_free(expr);
2788 isl_ast_build_free(build);
2790 return p;
2793 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2794 __isl_keep isl_aff *aff)
2796 if (!p || !aff)
2797 goto error;
2799 if (p->output_format == ISL_FORMAT_ISL)
2800 return print_aff_isl(p, aff);
2801 else if (p->output_format == ISL_FORMAT_C)
2802 return print_aff_c(p, aff);
2803 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2804 goto error);
2805 error:
2806 isl_printer_free(p);
2807 return NULL;
2810 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2811 __isl_keep isl_pw_aff *pwaff)
2813 if (!p || !pwaff)
2814 goto error;
2816 if (p->output_format == ISL_FORMAT_ISL)
2817 return print_pw_aff_isl(p, pwaff);
2818 else if (p->output_format == ISL_FORMAT_C)
2819 return print_pw_aff_c(p, pwaff);
2820 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2821 goto error);
2822 error:
2823 isl_printer_free(p);
2824 return NULL;
2827 /* Print "pa" in a sequence of isl_pw_affs delimited by semicolons.
2828 * Each isl_pw_aff itself is also printed as semicolon delimited
2829 * sequence of pieces.
2830 * If data->first = 1, then this is the first in the sequence.
2831 * Update data->first to tell the next element that it is not the first.
2833 static isl_stat print_pw_aff_body_wrap(__isl_take isl_pw_aff *pa,
2834 void *user)
2836 struct isl_union_print_data *data;
2837 data = (struct isl_union_print_data *) user;
2839 if (!data->first)
2840 data->p = isl_printer_print_str(data->p, "; ");
2841 data->first = 0;
2843 data->p = print_pw_aff_body(data->p, pa);
2844 isl_pw_aff_free(pa);
2846 return data->p ? isl_stat_ok : isl_stat_error;
2849 /* Print the body of an isl_union_pw_aff, i.e., a semicolon delimited
2850 * sequence of affine expressions, each followed by constraints,
2851 * with the sequence enclosed in braces.
2853 static __isl_give isl_printer *print_union_pw_aff_body(
2854 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2856 struct isl_union_print_data data = { p, 1 };
2858 p = isl_printer_print_str(p, s_open_set[0]);
2859 data.p = p;
2860 if (isl_union_pw_aff_foreach_pw_aff(upa,
2861 &print_pw_aff_body_wrap, &data) < 0)
2862 data.p = isl_printer_free(p);
2863 p = data.p;
2864 p = isl_printer_print_str(p, s_close_set[0]);
2866 return p;
2869 /* Print the isl_union_pw_aff "upa" to "p" in isl format.
2871 * The individual isl_pw_affs are delimited by a semicolon.
2873 static __isl_give isl_printer *print_union_pw_aff_isl(
2874 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2876 struct isl_print_space_data data = { 0 };
2877 isl_space *space;
2879 space = isl_union_pw_aff_get_space(upa);
2880 p = print_param_tuple(p, space, &data);
2881 isl_space_free(space);
2882 p = print_union_pw_aff_body(p, upa);
2883 return p;
2886 /* Print the isl_union_pw_aff "upa" to "p".
2888 * We currently only support an isl format.
2890 __isl_give isl_printer *isl_printer_print_union_pw_aff(
2891 __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2893 if (!p || !upa)
2894 return isl_printer_free(p);
2896 if (p->output_format == ISL_FORMAT_ISL)
2897 return print_union_pw_aff_isl(p, upa);
2898 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2899 "unsupported output format", return isl_printer_free(p));
2902 /* Print dimension "pos" of data->space to "p".
2904 * data->user is assumed to be an isl_multi_aff.
2906 * If the current dimension is an output dimension, then print
2907 * the corresponding expression. Otherwise, print the name of the dimension.
2909 static __isl_give isl_printer *print_dim_ma(__isl_take isl_printer *p,
2910 struct isl_print_space_data *data, unsigned pos)
2912 isl_multi_aff *ma = data->user;
2914 if (data->type == isl_dim_out)
2915 p = print_aff_body(p, ma->u.p[pos]);
2916 else
2917 p = print_name(data->space, p, data->type, pos, data->latex);
2919 return p;
2922 static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
2923 __isl_keep isl_multi_aff *maff)
2925 struct isl_print_space_data data = { 0 };
2927 data.print_dim = &print_dim_ma;
2928 data.user = maff;
2929 return isl_print_space(maff->space, p, 0, &data);
2932 static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
2933 __isl_keep isl_multi_aff *maff)
2935 struct isl_print_space_data data = { 0 };
2937 if (!maff)
2938 goto error;
2940 p = print_param_tuple(p, maff->space, &data);
2941 p = isl_printer_print_str(p, "{ ");
2942 p = print_multi_aff(p, maff);
2943 p = isl_printer_print_str(p, " }");
2944 return p;
2945 error:
2946 isl_printer_free(p);
2947 return NULL;
2950 __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
2951 __isl_keep isl_multi_aff *maff)
2953 if (!p || !maff)
2954 goto error;
2956 if (p->output_format == ISL_FORMAT_ISL)
2957 return print_multi_aff_isl(p, maff);
2958 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2959 goto error);
2960 error:
2961 isl_printer_free(p);
2962 return NULL;
2965 static __isl_give isl_printer *print_pw_multi_aff_body(
2966 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2968 int i;
2970 if (!pma)
2971 goto error;
2973 for (i = 0; i < pma->n; ++i) {
2974 isl_space *space;
2976 if (i)
2977 p = isl_printer_print_str(p, "; ");
2978 p = print_multi_aff(p, pma->p[i].maff);
2979 space = isl_multi_aff_get_domain_space(pma->p[i].maff);
2980 p = print_disjuncts(set_to_map(pma->p[i].set), space, p, 0);
2981 isl_space_free(space);
2983 return p;
2984 error:
2985 isl_printer_free(p);
2986 return NULL;
2989 static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
2990 __isl_keep isl_pw_multi_aff *pma)
2992 struct isl_print_space_data data = { 0 };
2994 if (!pma)
2995 goto error;
2997 p = print_param_tuple(p, pma->dim, &data);
2998 p = isl_printer_print_str(p, "{ ");
2999 p = print_pw_multi_aff_body(p, pma);
3000 p = isl_printer_print_str(p, " }");
3001 return p;
3002 error:
3003 isl_printer_free(p);
3004 return NULL;
3007 /* Print the unnamed, single-dimensional piecewise multi affine expression "pma"
3008 * to "p".
3010 static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
3011 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3013 int i;
3014 isl_space *space;
3016 space = isl_pw_multi_aff_get_domain_space(pma);
3017 for (i = 0; i < pma->n - 1; ++i) {
3018 p = isl_printer_print_str(p, "(");
3019 p = print_set_c(p, space, pma->p[i].set);
3020 p = isl_printer_print_str(p, ") ? (");
3021 p = print_aff_c(p, pma->p[i].maff->u.p[0]);
3022 p = isl_printer_print_str(p, ") : ");
3024 isl_space_free(space);
3026 return print_aff_c(p, pma->p[pma->n - 1].maff->u.p[0]);
3029 static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
3030 __isl_keep isl_pw_multi_aff *pma)
3032 int n;
3033 const char *name;
3035 if (!pma)
3036 goto error;
3037 if (pma->n < 1)
3038 isl_die(p->ctx, isl_error_unsupported,
3039 "cannot print empty isl_pw_multi_aff in C format",
3040 goto error);
3041 name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
3042 if (!name && isl_pw_multi_aff_dim(pma, isl_dim_out) == 1)
3043 return print_unnamed_pw_multi_aff_c(p, pma);
3044 if (!name)
3045 isl_die(p->ctx, isl_error_unsupported,
3046 "cannot print unnamed isl_pw_multi_aff in C format",
3047 goto error);
3049 p = isl_printer_print_str(p, name);
3050 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
3051 if (n != 0)
3052 isl_die(p->ctx, isl_error_unsupported,
3053 "not supported yet", goto error);
3055 return p;
3056 error:
3057 isl_printer_free(p);
3058 return NULL;
3061 __isl_give isl_printer *isl_printer_print_pw_multi_aff(
3062 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3064 if (!p || !pma)
3065 goto error;
3067 if (p->output_format == ISL_FORMAT_ISL)
3068 return print_pw_multi_aff_isl(p, pma);
3069 if (p->output_format == ISL_FORMAT_C)
3070 return print_pw_multi_aff_c(p, pma);
3071 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3072 goto error);
3073 error:
3074 isl_printer_free(p);
3075 return NULL;
3078 static isl_stat print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
3079 void *user)
3081 struct isl_union_print_data *data;
3082 data = (struct isl_union_print_data *) user;
3084 if (!data->first)
3085 data->p = isl_printer_print_str(data->p, "; ");
3086 data->first = 0;
3088 data->p = print_pw_multi_aff_body(data->p, pma);
3089 isl_pw_multi_aff_free(pma);
3091 return isl_stat_ok;
3094 static __isl_give isl_printer *print_union_pw_multi_aff_isl(
3095 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
3097 struct isl_union_print_data data;
3098 struct isl_print_space_data space_data = { 0 };
3099 isl_space *space;
3101 space = isl_union_pw_multi_aff_get_space(upma);
3102 p = print_param_tuple(p, space, &space_data);
3103 isl_space_free(space);
3104 p = isl_printer_print_str(p, s_open_set[0]);
3105 data.p = p;
3106 data.first = 1;
3107 isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3108 &print_pw_multi_aff_body_wrap, &data);
3109 p = data.p;
3110 p = isl_printer_print_str(p, s_close_set[0]);
3111 return p;
3114 __isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
3115 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
3117 if (!p || !upma)
3118 goto error;
3120 if (p->output_format == ISL_FORMAT_ISL)
3121 return print_union_pw_multi_aff_isl(p, upma);
3122 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3123 goto error);
3124 error:
3125 isl_printer_free(p);
3126 return NULL;
3129 /* Print dimension "pos" of data->space to "p".
3131 * data->user is assumed to be an isl_multi_pw_aff.
3133 * If the current dimension is an output dimension, then print
3134 * the corresponding piecewise affine expression.
3135 * Otherwise, print the name of the dimension.
3137 static __isl_give isl_printer *print_dim_mpa(__isl_take isl_printer *p,
3138 struct isl_print_space_data *data, unsigned pos)
3140 int i;
3141 int need_parens;
3142 isl_multi_pw_aff *mpa = data->user;
3143 isl_pw_aff *pa;
3145 if (data->type != isl_dim_out)
3146 return print_name(data->space, p, data->type, pos, data->latex);
3148 pa = mpa->u.p[pos];
3149 if (pa->n == 0)
3150 return isl_printer_print_str(p, "(0 : false)");
3152 need_parens = pa->n != 1 || !isl_set_plain_is_universe(pa->p[0].set);
3153 if (need_parens)
3154 p = isl_printer_print_str(p, "(");
3155 for (i = 0; i < pa->n; ++i) {
3156 isl_space *space;
3158 if (i)
3159 p = isl_printer_print_str(p, "; ");
3160 p = print_aff_body(p, pa->p[i].aff);
3161 space = isl_aff_get_domain_space(pa->p[i].aff);
3162 p = print_disjuncts(pa->p[i].set, space, p, 0);
3163 isl_space_free(space);
3165 if (need_parens)
3166 p = isl_printer_print_str(p, ")");
3168 return p;
3171 /* Print "mpa" to "p" in isl format.
3173 static __isl_give isl_printer *print_multi_pw_aff_isl(__isl_take isl_printer *p,
3174 __isl_keep isl_multi_pw_aff *mpa)
3176 struct isl_print_space_data data = { 0 };
3178 if (!mpa)
3179 return isl_printer_free(p);
3181 p = print_param_tuple(p, mpa->space, &data);
3182 p = isl_printer_print_str(p, "{ ");
3183 data.print_dim = &print_dim_mpa;
3184 data.user = mpa;
3185 p = isl_print_space(mpa->space, p, 0, &data);
3186 p = isl_printer_print_str(p, " }");
3187 return p;
3190 __isl_give isl_printer *isl_printer_print_multi_pw_aff(
3191 __isl_take isl_printer *p, __isl_keep isl_multi_pw_aff *mpa)
3193 if (!p || !mpa)
3194 return isl_printer_free(p);
3196 if (p->output_format == ISL_FORMAT_ISL)
3197 return print_multi_pw_aff_isl(p, mpa);
3198 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3199 return isl_printer_free(p));
3202 /* Print dimension "pos" of data->space to "p".
3204 * data->user is assumed to be an isl_multi_val.
3206 * If the current dimension is an output dimension, then print
3207 * the corresponding value. Otherwise, print the name of the dimension.
3209 static __isl_give isl_printer *print_dim_mv(__isl_take isl_printer *p,
3210 struct isl_print_space_data *data, unsigned pos)
3212 isl_multi_val *mv = data->user;
3214 if (data->type == isl_dim_out)
3215 return isl_printer_print_val(p, mv->u.p[pos]);
3216 else
3217 return print_name(data->space, p, data->type, pos, data->latex);
3220 /* Print the isl_multi_val "mv" to "p" in isl format.
3222 static __isl_give isl_printer *print_multi_val_isl(__isl_take isl_printer *p,
3223 __isl_keep isl_multi_val *mv)
3225 struct isl_print_space_data data = { 0 };
3227 if (!mv)
3228 return isl_printer_free(p);
3230 p = print_param_tuple(p, mv->space, &data);
3231 p = isl_printer_print_str(p, "{ ");
3232 data.print_dim = &print_dim_mv;
3233 data.user = mv;
3234 p = isl_print_space(mv->space, p, 0, &data);
3235 p = isl_printer_print_str(p, " }");
3236 return p;
3239 /* Print the isl_multi_val "mv" to "p".
3241 * Currently only supported in isl format.
3243 __isl_give isl_printer *isl_printer_print_multi_val(
3244 __isl_take isl_printer *p, __isl_keep isl_multi_val *mv)
3246 if (!p || !mv)
3247 return isl_printer_free(p);
3249 if (p->output_format == ISL_FORMAT_ISL)
3250 return print_multi_val_isl(p, mv);
3251 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3252 return isl_printer_free(p));
3255 /* Print dimension "pos" of data->space to "p".
3257 * data->user is assumed to be an isl_multi_union_pw_aff.
3259 * The current dimension is necessarily a set dimension, so
3260 * we print the corresponding isl_union_pw_aff, including
3261 * the braces.
3263 static __isl_give isl_printer *print_union_pw_aff_dim(__isl_take isl_printer *p,
3264 struct isl_print_space_data *data, unsigned pos)
3266 isl_multi_union_pw_aff *mupa = data->user;
3267 isl_union_pw_aff *upa;
3269 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, pos);
3270 p = print_union_pw_aff_body(p, upa);
3271 isl_union_pw_aff_free(upa);
3273 return p;
3276 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3278 static __isl_give isl_printer *print_multi_union_pw_aff_isl(
3279 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3281 struct isl_print_space_data data = { 0 };
3282 isl_space *space;
3284 space = isl_multi_union_pw_aff_get_space(mupa);
3285 p = print_param_tuple(p, space, &data);
3287 data.print_dim = &print_union_pw_aff_dim;
3288 data.user = mupa;
3290 p = isl_print_space(space, p, 0, &data);
3291 isl_space_free(space);
3293 return p;
3296 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3298 * We currently only support an isl format.
3300 __isl_give isl_printer *isl_printer_print_multi_union_pw_aff(
3301 __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3303 if (!p || !mupa)
3304 return isl_printer_free(p);
3306 if (p->output_format == ISL_FORMAT_ISL)
3307 return print_multi_union_pw_aff_isl(p, mupa);
3308 isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
3309 "unsupported output format", return isl_printer_free(p));