isl_qpolynomial_fold_fold_on_domain: do not consider zero to dominate NaN
[isl.git] / isl_output.c
blobda2554215c2896b82c720349cd1a242cae3011eb
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/ast_build.h>
32 #include <isl_sort.h>
34 static const char *s_to[2] = { " -> ", " \\to " };
35 static const char *s_and[2] = { " and ", " \\wedge " };
36 static const char *s_or[2] = { " or ", " \\vee " };
37 static const char *s_le[2] = { "<=", "\\le" };
38 static const char *s_ge[2] = { ">=", "\\ge" };
39 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
40 static const char *s_close_set[2] = { " }", " \\,\\}" };
41 static const char *s_open_list[2] = { "[", "(" };
42 static const char *s_close_list[2] = { "]", ")" };
43 static const char *s_such_that[2] = { " : ", " \\mid " };
44 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
45 static const char *s_close_exists[2] = { ")", "" };
46 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
47 static const char *s_param_prefix[2] = { "p", "p_" };
48 static const char *s_input_prefix[2] = { "i", "i_" };
49 static const char *s_output_prefix[2] = { "o", "o_" };
51 static __isl_give isl_printer *print_constraint_polylib(
52 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
54 int i;
55 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
56 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
57 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
58 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
60 p = isl_printer_start_line(p);
61 p = isl_printer_print_int(p, ineq);
62 for (i = 0; i < n_out; ++i) {
63 p = isl_printer_print_str(p, " ");
64 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
66 for (i = 0; i < n_in; ++i) {
67 p = isl_printer_print_str(p, " ");
68 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
70 for (i = 0; i < bmap->n_div; ++i) {
71 p = isl_printer_print_str(p, " ");
72 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
74 for (i = 0; i < nparam; ++i) {
75 p = isl_printer_print_str(p, " ");
76 p = isl_printer_print_isl_int(p, c[1+i]);
78 p = isl_printer_print_str(p, " ");
79 p = isl_printer_print_isl_int(p, c[0]);
80 p = isl_printer_end_line(p);
81 return p;
84 static __isl_give isl_printer *print_constraints_polylib(
85 struct isl_basic_map *bmap, __isl_take isl_printer *p)
87 int i;
89 p = isl_printer_set_isl_int_width(p, 5);
91 for (i = 0; i < bmap->n_eq; ++i)
92 p = print_constraint_polylib(bmap, 0, i, p);
93 for (i = 0; i < bmap->n_ineq; ++i)
94 p = print_constraint_polylib(bmap, 1, i, p);
96 return p;
99 static __isl_give isl_printer *bset_print_constraints_polylib(
100 struct isl_basic_set *bset, __isl_take isl_printer *p)
102 return print_constraints_polylib((struct isl_basic_map *)bset, p);
105 static __isl_give isl_printer *isl_basic_map_print_polylib(
106 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
108 unsigned total = isl_basic_map_total_dim(bmap);
109 p = isl_printer_start_line(p);
110 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
111 p = isl_printer_print_str(p, " ");
112 p = isl_printer_print_int(p, 1 + total + 1);
113 if (ext) {
114 p = isl_printer_print_str(p, " ");
115 p = isl_printer_print_int(p,
116 isl_basic_map_dim(bmap, isl_dim_out));
117 p = isl_printer_print_str(p, " ");
118 p = isl_printer_print_int(p,
119 isl_basic_map_dim(bmap, isl_dim_in));
120 p = isl_printer_print_str(p, " ");
121 p = isl_printer_print_int(p,
122 isl_basic_map_dim(bmap, isl_dim_div));
123 p = isl_printer_print_str(p, " ");
124 p = isl_printer_print_int(p,
125 isl_basic_map_dim(bmap, isl_dim_param));
127 p = isl_printer_end_line(p);
128 return print_constraints_polylib(bmap, p);
131 static __isl_give isl_printer *isl_basic_set_print_polylib(
132 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
134 return isl_basic_map_print_polylib((struct isl_basic_map *)bset, p, ext);
137 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
138 __isl_take isl_printer *p, int ext)
140 int i;
142 p = isl_printer_start_line(p);
143 p = isl_printer_print_int(p, map->n);
144 p = isl_printer_end_line(p);
145 for (i = 0; i < map->n; ++i) {
146 p = isl_printer_start_line(p);
147 p = isl_printer_end_line(p);
148 p = isl_basic_map_print_polylib(map->p[i], p, ext);
150 return p;
153 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
154 __isl_take isl_printer *p, int ext)
156 return isl_map_print_polylib((struct isl_map *)set, p, ext);
159 static int count_same_name(__isl_keep isl_space *dim,
160 enum isl_dim_type type, unsigned pos, const char *name)
162 enum isl_dim_type t;
163 unsigned p, s;
164 int count = 0;
166 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
167 s = t == type ? pos : isl_space_dim(dim, t);
168 for (p = 0; p < s; ++p) {
169 const char *n = isl_space_get_dim_name(dim, t, p);
170 if (n && !strcmp(n, name))
171 count++;
174 return count;
177 static __isl_give isl_printer *print_name(__isl_keep isl_space *dim,
178 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
179 int latex)
181 const char *name;
182 char buffer[20];
183 int primes;
185 name = type == isl_dim_div ? NULL : isl_space_get_dim_name(dim, type, pos);
187 if (!name) {
188 const char *prefix;
189 if (type == isl_dim_param)
190 prefix = s_param_prefix[latex];
191 else if (type == isl_dim_div)
192 prefix = s_div_prefix[latex];
193 else if (isl_space_is_set(dim) || type == isl_dim_in)
194 prefix = s_input_prefix[latex];
195 else
196 prefix = s_output_prefix[latex];
197 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
198 name = buffer;
200 primes = count_same_name(dim, name == buffer ? isl_dim_div : type,
201 pos, name);
202 p = isl_printer_print_str(p, name);
203 while (primes-- > 0)
204 p = isl_printer_print_str(p, "'");
205 return p;
208 static enum isl_dim_type pos2type(__isl_keep isl_space *dim, unsigned *pos)
210 enum isl_dim_type type;
211 unsigned n_in = isl_space_dim(dim, isl_dim_in);
212 unsigned n_out = isl_space_dim(dim, isl_dim_out);
213 unsigned nparam = isl_space_dim(dim, isl_dim_param);
215 if (*pos < 1 + nparam) {
216 type = isl_dim_param;
217 *pos -= 1;
218 } else if (*pos < 1 + nparam + n_in) {
219 type = isl_dim_in;
220 *pos -= 1 + nparam;
221 } else if (*pos < 1 + nparam + n_in + n_out) {
222 type = isl_dim_out;
223 *pos -= 1 + nparam + n_in;
224 } else {
225 type = isl_dim_div;
226 *pos -= 1 + nparam + n_in + n_out;
229 return type;
232 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
233 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
235 static __isl_give isl_printer *print_term(__isl_keep isl_space *dim,
236 __isl_keep isl_mat *div,
237 isl_int c, unsigned pos, __isl_take isl_printer *p, int latex)
239 enum isl_dim_type type;
240 int print_div_def;
242 if (pos == 0)
243 return isl_printer_print_isl_int(p, c);
245 type = pos2type(dim, &pos);
246 print_div_def = type == isl_dim_div && div &&
247 !isl_int_is_zero(div->row[pos][0]);
249 if (isl_int_is_one(c))
251 else if (isl_int_is_negone(c))
252 p = isl_printer_print_str(p, "-");
253 else {
254 p = isl_printer_print_isl_int(p, c);
255 if (p->output_format == ISL_FORMAT_C || print_div_def)
256 p = isl_printer_print_str(p, "*");
258 if (print_div_def)
259 p = print_div(dim, div, pos, p);
260 else
261 p = print_name(dim, p, type, pos, latex);
262 return p;
265 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_space *dim,
266 __isl_keep isl_mat *div,
267 __isl_take isl_printer *p, isl_int *c, int len)
269 int i;
270 int first;
272 for (i = 0, first = 1; i < len; ++i) {
273 int flip = 0;
274 if (isl_int_is_zero(c[i]))
275 continue;
276 if (!first) {
277 if (isl_int_is_neg(c[i])) {
278 flip = 1;
279 isl_int_neg(c[i], c[i]);
280 p = isl_printer_print_str(p, " - ");
281 } else
282 p = isl_printer_print_str(p, " + ");
284 first = 0;
285 p = print_term(dim, div, c[i], i, p, 0);
286 if (flip)
287 isl_int_neg(c[i], c[i]);
289 if (first)
290 p = isl_printer_print_str(p, "0");
291 return p;
294 static __isl_give isl_printer *print_affine(__isl_keep isl_basic_map *bmap,
295 __isl_keep isl_space *dim, __isl_take isl_printer *p, isl_int *c)
297 unsigned len = 1 + isl_basic_map_total_dim(bmap);
298 return print_affine_of_len(dim, NULL, p, c, len);
301 /* Internal data structure for print_space.
303 * latex is set if that is the output format.
304 * print_dim (if not NULL) is called on each dimension.
305 * user is set by the caller of print_space and may be used inside print_dim.
307 * space is the global space that is being printed. This field is set by
308 * print_space.
309 * type is the tuple of the global space that is currently being printed.
310 * This field is set by print_space.
312 struct isl_print_space_data {
313 int latex;
314 __isl_give isl_printer *(*print_dim)(__isl_take isl_printer *p,
315 struct isl_print_space_data *data, unsigned pos);
316 void *user;
318 isl_space *space;
319 enum isl_dim_type type;
322 /* offset is the offset of local_dim inside data->type of data->space.
324 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
325 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
326 struct isl_print_space_data *data, int offset)
328 int i;
330 if (data->space != local_dim && local_type == isl_dim_out)
331 offset += local_dim->n_in;
333 for (i = 0; i < isl_space_dim(local_dim, local_type); ++i) {
334 if (i)
335 p = isl_printer_print_str(p, ", ");
336 if (data->print_dim)
337 p = data->print_dim(p, data, offset + i);
338 else
339 p = print_name(data->space, p, data->type, offset + i,
340 data->latex);
342 return p;
345 static __isl_give isl_printer *print_var_list(__isl_take isl_printer *p,
346 __isl_keep isl_space *space, enum isl_dim_type type)
348 struct isl_print_space_data data = { .space = space, .type = type };
350 return print_nested_var_list(p, space, type, &data, 0);
353 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
354 __isl_keep isl_space *local_dim,
355 struct isl_print_space_data *data, int offset);
357 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
358 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
359 struct isl_print_space_data *data, int offset)
361 const char *name = NULL;
362 unsigned n = isl_space_dim(local_dim, local_type);
363 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
364 name = isl_space_get_tuple_name(local_dim, local_type);
365 if (name) {
366 if (data->latex)
367 p = isl_printer_print_str(p, "\\mathrm{");
368 p = isl_printer_print_str(p, name);
369 if (data->latex)
370 p = isl_printer_print_str(p, "}");
373 if (!data->latex || n != 1 || name)
374 p = isl_printer_print_str(p, s_open_list[data->latex]);
375 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
376 local_dim->nested[local_type - isl_dim_in]) {
377 if (data->space != local_dim && local_type == isl_dim_out)
378 offset += local_dim->n_in;
379 p = print_nested_map_dim(p,
380 local_dim->nested[local_type - isl_dim_in],
381 data, offset);
382 } else
383 p = print_nested_var_list(p, local_dim, local_type, data,
384 offset);
385 if (!data->latex || n != 1 || name)
386 p = isl_printer_print_str(p, s_close_list[data->latex]);
387 return p;
390 static __isl_give isl_printer *print_tuple(__isl_keep isl_space *dim,
391 __isl_take isl_printer *p, enum isl_dim_type type,
392 struct isl_print_space_data *data)
394 data->space = dim;
395 data->type = type;
396 return print_nested_tuple(p, dim, type, data, 0);
399 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
400 __isl_keep isl_space *local_dim,
401 struct isl_print_space_data *data, int offset)
403 p = print_nested_tuple(p, local_dim, isl_dim_in, data, offset);
404 p = isl_printer_print_str(p, s_to[data->latex]);
405 p = print_nested_tuple(p, local_dim, isl_dim_out, data, offset);
407 return p;
410 static __isl_give isl_printer *print_space(__isl_keep isl_space *dim,
411 __isl_take isl_printer *p, int rational,
412 struct isl_print_space_data *data)
414 if (rational && !data->latex)
415 p = isl_printer_print_str(p, "rat: ");
416 if (isl_space_is_params(dim))
418 else if (isl_space_is_set(dim))
419 p = print_tuple(dim, p, isl_dim_set, data);
420 else {
421 p = print_tuple(dim, p, isl_dim_in, data);
422 p = isl_printer_print_str(p, s_to[data->latex]);
423 p = print_tuple(dim, p, isl_dim_out, data);
426 return p;
429 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_space *dim,
430 __isl_take isl_printer *p)
432 if (isl_space_dim(dim, isl_dim_param) == 0)
433 return p;
435 p = isl_printer_start_line(p);
436 p = isl_printer_print_str(p, "symbolic ");
437 p = print_var_list(p, dim, isl_dim_param);
438 p = isl_printer_print_str(p, ";");
439 p = isl_printer_end_line(p);
440 return p;
443 static __isl_give isl_printer *print_constraint(struct isl_basic_map *bmap,
444 __isl_keep isl_space *dim, __isl_take isl_printer *p,
445 isl_int *c, int last, const char *op, int first_constraint, int latex)
447 if (!first_constraint)
448 p = isl_printer_print_str(p, s_and[latex]);
450 isl_int_abs(c[last], c[last]);
452 p = print_term(dim, NULL, c[last], last, p, latex);
454 p = isl_printer_print_str(p, " ");
455 p = isl_printer_print_str(p, op);
456 p = isl_printer_print_str(p, " ");
458 isl_int_set_si(c[last], 0);
459 p = print_affine(bmap, dim, p, c);
461 return p;
464 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
465 __isl_keep isl_space *dim, __isl_take isl_printer *p, int latex)
467 int i;
468 struct isl_vec *c;
469 unsigned total = isl_basic_map_total_dim(bmap);
471 c = isl_vec_alloc(bmap->ctx, 1 + total);
472 if (!c)
473 goto error;
475 for (i = bmap->n_eq - 1; i >= 0; --i) {
476 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
477 if (l < 0) {
478 if (i != bmap->n_eq - 1)
479 p = isl_printer_print_str(p, s_and[latex]);
480 p = isl_printer_print_str(p, "0 = 0");
481 continue;
483 if (isl_int_is_neg(bmap->eq[i][l]))
484 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
485 else
486 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
487 p = print_constraint(bmap, dim, p, c->el, l,
488 "=", i == bmap->n_eq - 1, latex);
490 for (i = 0; i < bmap->n_ineq; ++i) {
491 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
492 int s;
493 const char *op;
494 if (l < 0)
495 continue;
496 s = isl_int_sgn(bmap->ineq[i][l]);
497 if (s < 0)
498 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
499 else
500 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
501 op = s < 0 ? s_le[latex] : s_ge[latex];
502 p = print_constraint(bmap, dim, p, c->el, l,
503 op, !bmap->n_eq && !i, latex);
506 isl_vec_free(c);
508 return p;
509 error:
510 isl_vec_free(c);
511 isl_printer_free(p);
512 return NULL;
515 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
516 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
518 int c = p->output_format == ISL_FORMAT_C;
519 p = isl_printer_print_str(p, c ? "floord(" : "floor((");
520 p = print_affine_of_len(dim, div, p,
521 div->row[pos] + 1, div->n_col - 1);
522 p = isl_printer_print_str(p, c ? ", " : ")/");
523 p = isl_printer_print_isl_int(p, div->row[pos][0]);
524 p = isl_printer_print_str(p, ")");
525 return p;
528 /* Print a comma separated list of div names, with their definitions
529 * (provided that they have a definition and we are printing in isl format).
531 static __isl_give isl_printer *print_div_list(__isl_take isl_printer *p,
532 __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex)
534 int i;
535 unsigned n_div;
537 if (!p || !space || !div)
538 return isl_printer_free(p);
540 n_div = isl_mat_rows(div);
542 for (i = 0; i < n_div; ++i) {
543 if (i)
544 p = isl_printer_print_str(p, ", ");
545 p = print_name(space, p, isl_dim_div, i, latex);
546 if (p->output_format != ISL_FORMAT_ISL ||
547 isl_int_is_zero(div->row[i][0]))
548 continue;
549 p = isl_printer_print_str(p, " = ");
550 p = print_div(space, div, i, p);
553 return p;
556 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
557 __isl_keep isl_space *dim, __isl_take isl_printer *p, int latex)
559 if (bmap->n_div > 0) {
560 isl_space *space;
561 isl_mat *div;
563 space = isl_basic_map_get_space(bmap);
564 div = isl_basic_map_get_divs(bmap);
565 p = isl_printer_print_str(p, s_open_exists[latex]);
566 p = print_div_list(p, space, div, latex);
567 isl_space_free(space);
568 isl_mat_free(div);
569 p = isl_printer_print_str(p, ": ");
572 p = print_constraints(bmap, dim, p, latex);
574 if (bmap->n_div > 0)
575 p = isl_printer_print_str(p, s_close_exists[latex]);
576 return p;
579 /* Print a colon followed by the constraints of "bmap"
580 * to "p", provided there are any constraints.
581 * The names of the variables are taken from "space".
582 * "latex" is set if the constraints should be printed in LaTeX format.
584 static __isl_give isl_printer *print_optional_disjunct(
585 __isl_keep isl_basic_map *bmap, __isl_keep isl_space *space,
586 __isl_take isl_printer *p, int latex)
588 if (isl_basic_map_is_universe(bmap))
589 return p;
591 p = isl_printer_print_str(p, ": ");
592 p = print_disjunct(bmap, space, p, latex);
594 return p;
597 static __isl_give isl_printer *basic_map_print_omega(
598 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
600 p = isl_printer_print_str(p, "{ [");
601 p = print_var_list(p, bmap->dim, isl_dim_in);
602 p = isl_printer_print_str(p, "] -> [");
603 p = print_var_list(p, bmap->dim, isl_dim_out);
604 p = isl_printer_print_str(p, "] ");
605 p = print_optional_disjunct(bmap, bmap->dim, p, 0);
606 p = isl_printer_print_str(p, " }");
607 return p;
610 static __isl_give isl_printer *basic_set_print_omega(
611 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
613 p = isl_printer_print_str(p, "{ [");
614 p = print_var_list(p, bset->dim, isl_dim_set);
615 p = isl_printer_print_str(p, "] ");
616 p = print_optional_disjunct(bset, bset->dim, p, 0);
617 p = isl_printer_print_str(p, " }");
618 return p;
621 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
622 __isl_take isl_printer *p)
624 int i;
626 for (i = 0; i < map->n; ++i) {
627 if (i)
628 p = isl_printer_print_str(p, " union ");
629 p = basic_map_print_omega(map->p[i], p);
631 return p;
634 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
635 __isl_take isl_printer *p)
637 int i;
639 for (i = 0; i < set->n; ++i) {
640 if (i)
641 p = isl_printer_print_str(p, " union ");
642 p = basic_set_print_omega(set->p[i], p);
644 return p;
647 static __isl_give isl_printer *isl_basic_map_print_isl(
648 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
649 int latex)
651 struct isl_print_space_data data = { .latex = latex };
652 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
654 if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
655 p = print_tuple(bmap->dim, p, isl_dim_param, &data);
656 p = isl_printer_print_str(p, " -> ");
658 p = isl_printer_print_str(p, "{ ");
659 p = print_space(bmap->dim, p, rational, &data);
660 p = isl_printer_print_str(p, " : ");
661 p = print_disjunct(bmap, bmap->dim, p, latex);
662 p = isl_printer_print_str(p, " }");
663 return p;
666 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
667 __isl_take isl_printer *p, int latex)
669 int i;
671 if (isl_map_plain_is_universe(map))
672 return p;
674 p = isl_printer_print_str(p, s_such_that[latex]);
675 if (map->n == 0)
676 p = isl_printer_print_str(p, "1 = 0");
677 for (i = 0; i < map->n; ++i) {
678 if (i)
679 p = isl_printer_print_str(p, s_or[latex]);
680 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
681 p = isl_printer_print_str(p, "(");
682 p = print_disjunct(map->p[i], map->dim, p, latex);
683 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
684 p = isl_printer_print_str(p, ")");
686 return p;
689 /* Print the disjuncts of a map (or set).
690 * If the map turns out to be a universal parameter domain, then
691 * we need to print the colon. Otherwise, the output looks identical
692 * to the empty set.
694 static __isl_give isl_printer *print_disjuncts_map(__isl_keep isl_map *map,
695 __isl_take isl_printer *p, int latex)
697 if (isl_map_plain_is_universe(map) && isl_space_is_params(map->dim))
698 return isl_printer_print_str(p, s_such_that[latex]);
699 else
700 return print_disjuncts(map, p, latex);
703 struct isl_aff_split {
704 isl_basic_map *aff;
705 isl_map *map;
708 static void free_split(__isl_take struct isl_aff_split *split, int n)
710 int i;
712 if (!split)
713 return;
715 for (i = 0; i < n; ++i) {
716 isl_basic_map_free(split[i].aff);
717 isl_map_free(split[i].map);
720 free(split);
723 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
725 int i, j;
726 unsigned nparam, n_in, n_out, total;
728 bmap = isl_basic_map_cow(bmap);
729 if (!bmap)
730 return NULL;
731 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
732 goto error;
734 nparam = isl_basic_map_dim(bmap, isl_dim_param);
735 n_in = isl_basic_map_dim(bmap, isl_dim_in);
736 n_out = isl_basic_map_dim(bmap, isl_dim_out);
737 total = isl_basic_map_dim(bmap, isl_dim_all);
738 for (i = bmap->n_eq - 1; i >= 0; --i) {
739 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
740 if (j >= nparam && j < nparam + n_in + n_out &&
741 (isl_int_is_one(bmap->eq[i][1 + j]) ||
742 isl_int_is_negone(bmap->eq[i][1 + j])))
743 continue;
744 if (isl_basic_map_drop_equality(bmap, i) < 0)
745 goto error;
748 bmap = isl_basic_map_finalize(bmap);
750 return bmap;
751 error:
752 isl_basic_map_free(bmap);
753 return NULL;
756 static int aff_split_cmp(const void *p1, const void *p2, void *user)
758 const struct isl_aff_split *s1, *s2;
759 s1 = (const struct isl_aff_split *) p1;
760 s2 = (const struct isl_aff_split *) p2;
762 return isl_basic_map_plain_cmp(s1->aff, s2->aff);
765 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
766 __isl_keep isl_basic_map *aff)
768 int i, j;
769 unsigned total;
771 if (!bmap || !aff)
772 goto error;
774 total = isl_space_dim(bmap->dim, isl_dim_all);
776 for (i = bmap->n_eq - 1; i >= 0; --i) {
777 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
778 bmap->n_div) != -1)
779 continue;
780 for (j = 0; j < aff->n_eq; ++j) {
781 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
782 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
783 continue;
784 if (isl_basic_map_drop_equality(bmap, i) < 0)
785 goto error;
786 break;
790 return bmap;
791 error:
792 isl_basic_map_free(bmap);
793 return NULL;
796 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
798 int i, n;
799 struct isl_aff_split *split;
800 isl_ctx *ctx;
802 ctx = isl_map_get_ctx(map);
803 split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
804 if (!split)
805 return NULL;
807 for (i = 0; i < map->n; ++i) {
808 isl_basic_map *bmap;
809 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
810 bmap = isl_basic_map_copy(map->p[i]);
811 bmap = isl_basic_map_cow(bmap);
812 bmap = drop_aff(bmap, split[i].aff);
813 split[i].map = isl_map_from_basic_map(bmap);
814 if (!split[i].aff || !split[i].map)
815 goto error;
818 if (isl_sort(split, map->n, sizeof(struct isl_aff_split),
819 &aff_split_cmp, NULL) < 0)
820 goto error;
822 n = map->n;
823 for (i = n - 1; i >= 1; --i) {
824 if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
825 split[i].aff))
826 continue;
827 isl_basic_map_free(split[i].aff);
828 split[i - 1].map = isl_map_union(split[i - 1].map,
829 split[i].map);
830 if (i != n - 1)
831 split[i] = split[n - 1];
832 split[n - 1].aff = NULL;
833 split[n - 1].map = NULL;
834 --n;
837 return split;
838 error:
839 free_split(split, map->n);
840 return NULL;
843 static int defining_equality(__isl_keep isl_basic_map *eq,
844 __isl_keep isl_space *dim, enum isl_dim_type type, int pos)
846 int i;
847 unsigned total;
849 if (!eq)
850 return -1;
852 pos += isl_space_offset(dim, type);
853 total = isl_basic_map_total_dim(eq);
855 for (i = 0; i < eq->n_eq; ++i) {
856 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
857 continue;
858 if (isl_int_is_one(eq->eq[i][1 + pos]))
859 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
860 return i;
863 return -1;
866 /* Print dimension "pos" of data->space to "p".
868 * data->user is assumed to be an isl_basic_map keeping track of equalities.
870 * If the current dimension is defined by these equalities, then print
871 * the corresponding expression. Otherwise, print the name of the dimension.
873 static __isl_give isl_printer *print_dim_eq(__isl_take isl_printer *p,
874 struct isl_print_space_data *data, unsigned pos)
876 isl_basic_map *eq = data->user;
877 int j;
879 j = defining_equality(eq, data->space, data->type, pos);
880 if (j >= 0) {
881 pos += 1 + isl_space_offset(data->space, data->type);
882 p = print_affine_of_len(eq->dim, NULL, p, eq->eq[j], pos);
883 } else {
884 p = print_name(data->space, p, data->type, pos, data->latex);
887 return p;
890 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
891 struct isl_aff_split *split, int n)
893 struct isl_print_space_data data = { 0 };
894 int i;
895 int rational;
897 data.print_dim = &print_dim_eq;
898 for (i = 0; i < n; ++i) {
899 isl_space *dim;
901 if (!split[i].map)
902 break;
903 dim = split[i].map->dim;
904 rational = split[i].map->n > 0 &&
905 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
906 if (i)
907 p = isl_printer_print_str(p, "; ");
908 data.user = split[i].aff;
909 p = print_space(dim, p, rational, &data);
910 p = print_disjuncts_map(split[i].map, p, 0);
913 return p;
916 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
917 __isl_take isl_printer *p)
919 struct isl_print_space_data data = { 0 };
920 struct isl_aff_split *split = NULL;
921 int rational;
923 if (map->n > 0)
924 split = split_aff(map);
925 if (split) {
926 p = print_split_map(p, split, map->n);
927 } else {
928 rational = map->n > 0 &&
929 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
930 p = print_space(map->dim, p, rational, &data);
931 p = print_disjuncts_map(map, p, 0);
933 free_split(split, map->n);
934 return p;
937 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
938 __isl_take isl_printer *p)
940 struct isl_print_space_data data = { 0 };
942 if (isl_map_dim(map, isl_dim_param) > 0) {
943 p = print_tuple(map->dim, p, isl_dim_param, &data);
944 p = isl_printer_print_str(p, s_to[0]);
946 p = isl_printer_print_str(p, s_open_set[0]);
947 p = isl_map_print_isl_body(map, p);
948 p = isl_printer_print_str(p, s_close_set[0]);
949 return p;
952 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
953 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff)
955 struct isl_print_space_data data = { 0 };
957 data.latex = 1;
958 if (isl_map_dim(map, isl_dim_param) > 0) {
959 p = print_tuple(map->dim, p, isl_dim_param, &data);
960 p = isl_printer_print_str(p, s_to[1]);
962 p = isl_printer_print_str(p, s_open_set[1]);
963 data.print_dim = &print_dim_eq;
964 data.user = aff;
965 p = print_space(map->dim, p, 0, &data);
966 p = print_disjuncts_map(map, p, 1);
967 p = isl_printer_print_str(p, s_close_set[1]);
969 return p;
972 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
973 __isl_take isl_printer *p)
975 int i;
976 struct isl_aff_split *split = NULL;
978 if (map->n > 0)
979 split = split_aff(map);
981 if (!split)
982 return print_latex_map(map, p, NULL);
984 for (i = 0; i < map->n; ++i) {
985 if (!split[i].map)
986 break;
987 if (i)
988 p = isl_printer_print_str(p, " \\cup ");
989 p = print_latex_map(split[i].map, p, split[i].aff);
992 free_split(split, map->n);
993 return p;
996 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
997 __isl_keep isl_basic_map *bmap)
999 if (!p || !bmap)
1000 goto error;
1001 if (p->output_format == ISL_FORMAT_ISL)
1002 return isl_basic_map_print_isl(bmap, p, 0);
1003 else if (p->output_format == ISL_FORMAT_OMEGA)
1004 return basic_map_print_omega(bmap, p);
1005 isl_assert(bmap->ctx, 0, goto error);
1006 error:
1007 isl_printer_free(p);
1008 return NULL;
1011 void isl_basic_map_print(__isl_keep isl_basic_map *bmap, FILE *out, int indent,
1012 const char *prefix, const char *suffix, unsigned output_format)
1014 isl_printer *printer;
1016 if (!bmap)
1017 return;
1019 printer = isl_printer_to_file(bmap->ctx, out);
1020 printer = isl_printer_set_indent(printer, indent);
1021 printer = isl_printer_set_prefix(printer, prefix);
1022 printer = isl_printer_set_suffix(printer, suffix);
1023 printer = isl_printer_set_output_format(printer, output_format);
1024 isl_printer_print_basic_map(printer, bmap);
1026 isl_printer_free(printer);
1029 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
1030 __isl_keep isl_basic_set *bset)
1032 if (!p || !bset)
1033 goto error;
1035 if (p->output_format == ISL_FORMAT_ISL)
1036 return isl_basic_map_print_isl(bset, p, 0);
1037 else if (p->output_format == ISL_FORMAT_POLYLIB)
1038 return isl_basic_set_print_polylib(bset, p, 0);
1039 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1040 return isl_basic_set_print_polylib(bset, p, 1);
1041 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
1042 return bset_print_constraints_polylib(bset, p);
1043 else if (p->output_format == ISL_FORMAT_OMEGA)
1044 return basic_set_print_omega(bset, p);
1045 isl_assert(p->ctx, 0, goto error);
1046 error:
1047 isl_printer_free(p);
1048 return NULL;
1051 void isl_basic_set_print(struct isl_basic_set *bset, FILE *out, int indent,
1052 const char *prefix, const char *suffix, unsigned output_format)
1054 isl_printer *printer;
1056 if (!bset)
1057 return;
1059 printer = isl_printer_to_file(bset->ctx, out);
1060 printer = isl_printer_set_indent(printer, indent);
1061 printer = isl_printer_set_prefix(printer, prefix);
1062 printer = isl_printer_set_suffix(printer, suffix);
1063 printer = isl_printer_set_output_format(printer, output_format);
1064 isl_printer_print_basic_set(printer, bset);
1066 isl_printer_free(printer);
1069 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
1070 __isl_keep isl_set *set)
1072 if (!p || !set)
1073 goto error;
1074 if (p->output_format == ISL_FORMAT_ISL)
1075 return isl_map_print_isl((isl_map *)set, p);
1076 else if (p->output_format == ISL_FORMAT_POLYLIB)
1077 return isl_set_print_polylib(set, p, 0);
1078 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1079 return isl_set_print_polylib(set, p, 1);
1080 else if (p->output_format == ISL_FORMAT_OMEGA)
1081 return isl_set_print_omega(set, p);
1082 else if (p->output_format == ISL_FORMAT_LATEX)
1083 return isl_map_print_latex((isl_map *)set, p);
1084 isl_assert(set->ctx, 0, goto error);
1085 error:
1086 isl_printer_free(p);
1087 return NULL;
1090 void isl_set_print(struct isl_set *set, FILE *out, int indent,
1091 unsigned output_format)
1093 isl_printer *printer;
1095 if (!set)
1096 return;
1098 printer = isl_printer_to_file(set->ctx, out);
1099 printer = isl_printer_set_indent(printer, indent);
1100 printer = isl_printer_set_output_format(printer, output_format);
1101 printer = isl_printer_print_set(printer, set);
1103 isl_printer_free(printer);
1106 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1107 __isl_keep isl_map *map)
1109 if (!p || !map)
1110 goto error;
1112 if (p->output_format == ISL_FORMAT_ISL)
1113 return isl_map_print_isl(map, p);
1114 else if (p->output_format == ISL_FORMAT_POLYLIB)
1115 return isl_map_print_polylib(map, p, 0);
1116 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1117 return isl_map_print_polylib(map, p, 1);
1118 else if (p->output_format == ISL_FORMAT_OMEGA)
1119 return isl_map_print_omega(map, p);
1120 else if (p->output_format == ISL_FORMAT_LATEX)
1121 return isl_map_print_latex(map, p);
1122 isl_assert(map->ctx, 0, goto error);
1123 error:
1124 isl_printer_free(p);
1125 return NULL;
1128 struct isl_union_print_data {
1129 isl_printer *p;
1130 int first;
1133 static int print_map_body(__isl_take isl_map *map, void *user)
1135 struct isl_union_print_data *data;
1136 data = (struct isl_union_print_data *)user;
1138 if (!data->first)
1139 data->p = isl_printer_print_str(data->p, "; ");
1140 data->first = 0;
1142 data->p = isl_map_print_isl_body(map, data->p);
1143 isl_map_free(map);
1145 return 0;
1148 static __isl_give isl_printer *isl_union_map_print_isl(
1149 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1151 struct isl_union_print_data data;
1152 struct isl_print_space_data space_data = { 0 };
1153 isl_space *dim;
1155 dim = isl_union_map_get_space(umap);
1156 if (isl_space_dim(dim, isl_dim_param) > 0) {
1157 p = print_tuple(dim, p, isl_dim_param, &space_data);
1158 p = isl_printer_print_str(p, s_to[0]);
1160 isl_space_free(dim);
1161 p = isl_printer_print_str(p, s_open_set[0]);
1162 data.p = p;
1163 data.first = 1;
1164 isl_union_map_foreach_map(umap, &print_map_body, &data);
1165 p = data.p;
1166 p = isl_printer_print_str(p, s_close_set[0]);
1167 return p;
1170 static int print_latex_map_body(__isl_take isl_map *map, void *user)
1172 struct isl_union_print_data *data;
1173 data = (struct isl_union_print_data *)user;
1175 if (!data->first)
1176 data->p = isl_printer_print_str(data->p, " \\cup ");
1177 data->first = 0;
1179 data->p = isl_map_print_latex(map, data->p);
1180 isl_map_free(map);
1182 return 0;
1185 static __isl_give isl_printer *isl_union_map_print_latex(
1186 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1188 struct isl_union_print_data data = { p, 1 };
1189 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1190 p = data.p;
1191 return p;
1194 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1195 __isl_keep isl_union_map *umap)
1197 if (!p || !umap)
1198 goto error;
1200 if (p->output_format == ISL_FORMAT_ISL)
1201 return isl_union_map_print_isl(umap, p);
1202 if (p->output_format == ISL_FORMAT_LATEX)
1203 return isl_union_map_print_latex(umap, p);
1205 isl_die(p->ctx, isl_error_invalid,
1206 "invalid output format for isl_union_map", goto error);
1207 error:
1208 isl_printer_free(p);
1209 return NULL;
1212 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1213 __isl_keep isl_union_set *uset)
1215 if (!p || !uset)
1216 goto error;
1218 if (p->output_format == ISL_FORMAT_ISL)
1219 return isl_union_map_print_isl((isl_union_map *)uset, p);
1220 if (p->output_format == ISL_FORMAT_LATEX)
1221 return isl_union_map_print_latex((isl_union_map *)uset, p);
1223 isl_die(p->ctx, isl_error_invalid,
1224 "invalid output format for isl_union_set", goto error);
1225 error:
1226 isl_printer_free(p);
1227 return NULL;
1230 void isl_map_print(__isl_keep isl_map *map, FILE *out, int indent,
1231 unsigned output_format)
1233 isl_printer *printer;
1235 if (!map)
1236 return;
1238 printer = isl_printer_to_file(map->ctx, out);
1239 printer = isl_printer_set_indent(printer, indent);
1240 printer = isl_printer_set_output_format(printer, output_format);
1241 printer = isl_printer_print_map(printer, map);
1243 isl_printer_free(printer);
1246 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
1248 int i;
1249 int n;
1251 for (i = 0, n = 0; i < rec->n; ++i)
1252 if (!isl_upoly_is_zero(rec->p[i]))
1253 ++n;
1255 return n;
1258 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
1259 __isl_take isl_printer *p, int first)
1261 struct isl_upoly_cst *cst;
1262 int neg;
1264 cst = isl_upoly_as_cst(up);
1265 if (!cst)
1266 goto error;
1267 neg = !first && isl_int_is_neg(cst->n);
1268 if (!first)
1269 p = isl_printer_print_str(p, neg ? " - " : " + ");
1270 if (neg)
1271 isl_int_neg(cst->n, cst->n);
1272 if (isl_int_is_zero(cst->d)) {
1273 int sgn = isl_int_sgn(cst->n);
1274 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1275 sgn == 0 ? "NaN" : "infty");
1276 } else
1277 p = isl_printer_print_isl_int(p, cst->n);
1278 if (neg)
1279 isl_int_neg(cst->n, cst->n);
1280 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1281 p = isl_printer_print_str(p, "/");
1282 p = isl_printer_print_isl_int(p, cst->d);
1284 return p;
1285 error:
1286 isl_printer_free(p);
1287 return NULL;
1290 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1291 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var)
1293 unsigned total;
1295 total = isl_space_dim(dim, isl_dim_all);
1296 if (var < total)
1297 p = print_term(dim, NULL, dim->ctx->one, 1 + var, p, 0);
1298 else
1299 p = print_div(dim, div, var - total, p);
1300 return p;
1303 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1304 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var, int exp)
1306 p = print_base(p, dim, div, var);
1307 if (exp == 1)
1308 return p;
1309 if (p->output_format == ISL_FORMAT_C) {
1310 int i;
1311 for (i = 1; i < exp; ++i) {
1312 p = isl_printer_print_str(p, "*");
1313 p = print_base(p, dim, div, var);
1315 } else {
1316 p = isl_printer_print_str(p, "^");
1317 p = isl_printer_print_int(p, exp);
1319 return p;
1322 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1323 __isl_keep isl_space *dim, __isl_keep isl_mat *div,
1324 __isl_take isl_printer *p, int outer)
1326 int i, n, first, print_parens;
1327 struct isl_upoly_rec *rec;
1329 if (!p || !up || !dim || !div)
1330 goto error;
1332 if (isl_upoly_is_cst(up))
1333 return upoly_print_cst(up, p, 1);
1335 rec = isl_upoly_as_rec(up);
1336 if (!rec)
1337 goto error;
1338 n = upoly_rec_n_non_zero(rec);
1339 print_parens = n > 1 ||
1340 (outer && rec->up.var >= isl_space_dim(dim, isl_dim_all));
1341 if (print_parens)
1342 p = isl_printer_print_str(p, "(");
1343 for (i = 0, first = 1; i < rec->n; ++i) {
1344 if (isl_upoly_is_zero(rec->p[i]))
1345 continue;
1346 if (isl_upoly_is_negone(rec->p[i])) {
1347 if (!i)
1348 p = isl_printer_print_str(p, "-1");
1349 else if (first)
1350 p = isl_printer_print_str(p, "-");
1351 else
1352 p = isl_printer_print_str(p, " - ");
1353 } else if (isl_upoly_is_cst(rec->p[i]) &&
1354 !isl_upoly_is_one(rec->p[i]))
1355 p = upoly_print_cst(rec->p[i], p, first);
1356 else {
1357 if (!first)
1358 p = isl_printer_print_str(p, " + ");
1359 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1360 p = upoly_print(rec->p[i], dim, div, p, 0);
1362 first = 0;
1363 if (i == 0)
1364 continue;
1365 if (!isl_upoly_is_one(rec->p[i]) &&
1366 !isl_upoly_is_negone(rec->p[i]))
1367 p = isl_printer_print_str(p, " * ");
1368 p = print_pow(p, dim, div, rec->up.var, i);
1370 if (print_parens)
1371 p = isl_printer_print_str(p, ")");
1372 return p;
1373 error:
1374 isl_printer_free(p);
1375 return NULL;
1378 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1379 __isl_keep isl_qpolynomial *qp)
1381 if (!p || !qp)
1382 goto error;
1383 p = upoly_print(qp->upoly, qp->dim, qp->div, p, 1);
1384 return p;
1385 error:
1386 isl_printer_free(p);
1387 return NULL;
1390 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1391 __isl_keep isl_qpolynomial *qp)
1393 struct isl_print_space_data data = { 0 };
1395 if (!p || !qp)
1396 goto error;
1398 if (isl_space_dim(qp->dim, isl_dim_param) > 0) {
1399 p = print_tuple(qp->dim, p, isl_dim_param, &data);
1400 p = isl_printer_print_str(p, " -> ");
1402 p = isl_printer_print_str(p, "{ ");
1403 if (!isl_space_is_params(qp->dim)) {
1404 p = print_space(qp->dim, p, 0, &data);
1405 p = isl_printer_print_str(p, " -> ");
1407 p = print_qpolynomial(p, qp);
1408 p = isl_printer_print_str(p, " }");
1409 return p;
1410 error:
1411 isl_printer_free(p);
1412 return NULL;
1415 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1416 __isl_keep isl_space *dim, __isl_keep isl_qpolynomial *qp)
1418 isl_int den;
1420 isl_int_init(den);
1421 isl_qpolynomial_get_den(qp, &den);
1422 if (!isl_int_is_one(den)) {
1423 isl_qpolynomial *f;
1424 p = isl_printer_print_str(p, "(");
1425 qp = isl_qpolynomial_copy(qp);
1426 f = isl_qpolynomial_rat_cst_on_domain(isl_space_copy(qp->dim),
1427 den, qp->dim->ctx->one);
1428 qp = isl_qpolynomial_mul(qp, f);
1430 if (qp)
1431 p = upoly_print(qp->upoly, dim, qp->div, p, 0);
1432 else
1433 p = isl_printer_free(p);
1434 if (!isl_int_is_one(den)) {
1435 p = isl_printer_print_str(p, ")/");
1436 p = isl_printer_print_isl_int(p, den);
1437 isl_qpolynomial_free(qp);
1439 isl_int_clear(den);
1440 return p;
1443 __isl_give isl_printer *isl_printer_print_qpolynomial(
1444 __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1446 if (!p || !qp)
1447 goto error;
1449 if (p->output_format == ISL_FORMAT_ISL)
1450 return print_qpolynomial_isl(p, qp);
1451 else if (p->output_format == ISL_FORMAT_C)
1452 return print_qpolynomial_c(p, qp->dim, qp);
1453 else
1454 isl_die(qp->dim->ctx, isl_error_unsupported,
1455 "output format not supported for isl_qpolynomials",
1456 goto error);
1457 error:
1458 isl_printer_free(p);
1459 return NULL;
1462 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1463 unsigned output_format)
1465 isl_printer *p;
1467 if (!qp)
1468 return;
1470 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1471 p = isl_printer_to_file(qp->dim->ctx, out);
1472 p = isl_printer_print_qpolynomial(p, qp);
1473 isl_printer_free(p);
1476 static __isl_give isl_printer *qpolynomial_fold_print(
1477 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1479 int i;
1481 if (fold->type == isl_fold_min)
1482 p = isl_printer_print_str(p, "min");
1483 else if (fold->type == isl_fold_max)
1484 p = isl_printer_print_str(p, "max");
1485 p = isl_printer_print_str(p, "(");
1486 for (i = 0; i < fold->n; ++i) {
1487 if (i)
1488 p = isl_printer_print_str(p, ", ");
1489 p = print_qpolynomial(p, fold->qp[i]);
1491 p = isl_printer_print_str(p, ")");
1492 return p;
1495 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1496 FILE *out, unsigned output_format)
1498 isl_printer *p;
1500 if (!fold)
1501 return;
1503 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1505 p = isl_printer_to_file(fold->dim->ctx, out);
1506 p = isl_printer_print_qpolynomial_fold(p, fold);
1508 isl_printer_free(p);
1511 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1512 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1514 struct isl_print_space_data data = { 0 };
1515 int i = 0;
1517 for (i = 0; i < pwqp->n; ++i) {
1518 if (i)
1519 p = isl_printer_print_str(p, "; ");
1520 if (!isl_space_is_params(pwqp->p[i].set->dim)) {
1521 p = print_space(pwqp->p[i].set->dim, p, 0, &data);
1522 p = isl_printer_print_str(p, " -> ");
1524 p = print_qpolynomial(p, pwqp->p[i].qp);
1525 p = print_disjuncts((isl_map *)pwqp->p[i].set, p, 0);
1528 return p;
1531 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1532 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1534 struct isl_print_space_data data = { 0 };
1536 if (!p || !pwqp)
1537 goto error;
1539 if (isl_space_dim(pwqp->dim, isl_dim_param) > 0) {
1540 p = print_tuple(pwqp->dim, p, isl_dim_param, &data);
1541 p = isl_printer_print_str(p, " -> ");
1543 p = isl_printer_print_str(p, "{ ");
1544 if (pwqp->n == 0) {
1545 if (!isl_space_is_set(pwqp->dim)) {
1546 p = print_tuple(pwqp->dim, p, isl_dim_in, &data);
1547 p = isl_printer_print_str(p, " -> ");
1549 p = isl_printer_print_str(p, "0");
1551 p = isl_pwqp_print_isl_body(p, pwqp);
1552 p = isl_printer_print_str(p, " }");
1553 return p;
1554 error:
1555 isl_printer_free(p);
1556 return NULL;
1559 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
1560 unsigned output_format)
1562 isl_printer *p;
1564 if (!pwqp)
1565 return;
1567 p = isl_printer_to_file(pwqp->dim->ctx, out);
1568 p = isl_printer_set_output_format(p, output_format);
1569 p = isl_printer_print_pw_qpolynomial(p, pwqp);
1571 isl_printer_free(p);
1574 static __isl_give isl_printer *isl_pwf_print_isl_body(
1575 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1577 struct isl_print_space_data data = { 0 };
1578 int i = 0;
1580 for (i = 0; i < pwf->n; ++i) {
1581 if (i)
1582 p = isl_printer_print_str(p, "; ");
1583 if (!isl_space_is_params(pwf->p[i].set->dim)) {
1584 p = print_space(pwf->p[i].set->dim, p, 0, &data);
1585 p = isl_printer_print_str(p, " -> ");
1587 p = qpolynomial_fold_print(pwf->p[i].fold, p);
1588 p = print_disjuncts((isl_map *)pwf->p[i].set, p, 0);
1591 return p;
1594 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
1595 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1597 struct isl_print_space_data data = { 0 };
1599 if (isl_space_dim(pwf->dim, isl_dim_param) > 0) {
1600 p = print_tuple(pwf->dim, p, isl_dim_param, &data);
1601 p = isl_printer_print_str(p, " -> ");
1603 p = isl_printer_print_str(p, "{ ");
1604 if (pwf->n == 0) {
1605 if (!isl_space_is_set(pwf->dim)) {
1606 p = print_tuple(pwf->dim, p, isl_dim_in, &data);
1607 p = isl_printer_print_str(p, " -> ");
1609 p = isl_printer_print_str(p, "0");
1611 p = isl_pwf_print_isl_body(p, pwf);
1612 p = isl_printer_print_str(p, " }");
1613 return p;
1616 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1617 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c);
1619 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1620 __isl_keep isl_space *dim,
1621 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1623 if (type == isl_dim_div) {
1624 p = isl_printer_print_str(p, "floord(");
1625 p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
1626 p = isl_printer_print_str(p, ", ");
1627 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1628 p = isl_printer_print_str(p, ")");
1629 } else {
1630 const char *name;
1632 name = isl_space_get_dim_name(dim, type, pos);
1633 if (!name)
1634 name = "UNNAMED";
1635 p = isl_printer_print_str(p, name);
1637 return p;
1640 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1641 __isl_keep isl_space *dim,
1642 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1644 enum isl_dim_type type;
1646 if (pos == 0)
1647 return isl_printer_print_isl_int(p, c);
1649 if (isl_int_is_one(c))
1651 else if (isl_int_is_negone(c))
1652 p = isl_printer_print_str(p, "-");
1653 else {
1654 p = isl_printer_print_isl_int(p, c);
1655 p = isl_printer_print_str(p, "*");
1657 type = pos2type(dim, &pos);
1658 p = print_name_c(p, dim, bset, type, pos);
1659 return p;
1662 static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
1663 __isl_keep isl_space *dim,
1664 __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
1666 int i;
1667 int first;
1669 for (i = 0, first = 1; i < len; ++i) {
1670 int flip = 0;
1671 if (isl_int_is_zero(c[i]))
1672 continue;
1673 if (!first) {
1674 if (isl_int_is_neg(c[i])) {
1675 flip = 1;
1676 isl_int_neg(c[i], c[i]);
1677 p = isl_printer_print_str(p, " - ");
1678 } else
1679 p = isl_printer_print_str(p, " + ");
1681 first = 0;
1682 p = print_term_c(p, dim, bset, c[i], i);
1683 if (flip)
1684 isl_int_neg(c[i], c[i]);
1686 if (first)
1687 p = isl_printer_print_str(p, "0");
1688 return p;
1691 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1692 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c)
1694 unsigned len = 1 + isl_basic_set_total_dim(bset);
1695 return print_partial_affine_c(p, dim, bset, c, len);
1698 /* We skip the constraint if it is implied by the div expression.
1700 * *first indicates whether this is the first constraint in the conjunction and
1701 * is updated if the constraint is actually printed.
1703 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1704 __isl_keep isl_space *dim,
1705 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int *first)
1707 unsigned o_div;
1708 unsigned n_div;
1709 int div;
1711 o_div = isl_basic_set_offset(bset, isl_dim_div);
1712 n_div = isl_basic_set_dim(bset, isl_dim_div);
1713 div = isl_seq_last_non_zero(c + o_div, n_div);
1714 if (div >= 0 && isl_basic_set_is_div_constraint(bset, c, div))
1715 return p;
1717 if (!*first)
1718 p = isl_printer_print_str(p, " && ");
1720 p = print_affine_c(p, dim, bset, c);
1721 p = isl_printer_print_str(p, " ");
1722 p = isl_printer_print_str(p, op);
1723 p = isl_printer_print_str(p, " 0");
1725 *first = 0;
1727 return p;
1730 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1731 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset)
1733 int i, j;
1734 int first = 1;
1735 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
1736 unsigned total = isl_basic_set_total_dim(bset) - n_div;
1738 for (i = 0; i < bset->n_eq; ++i) {
1739 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
1740 if (j < 0)
1741 p = print_constraint_c(p, dim, bset,
1742 bset->eq[i], "==", &first);
1743 else {
1744 if (i)
1745 p = isl_printer_print_str(p, " && ");
1746 p = isl_printer_print_str(p, "(");
1747 p = print_partial_affine_c(p, dim, bset, bset->eq[i],
1748 1 + total + j);
1749 p = isl_printer_print_str(p, ") % ");
1750 p = isl_printer_print_isl_int(p,
1751 bset->eq[i][1 + total + j]);
1752 p = isl_printer_print_str(p, " == 0");
1753 first = 0;
1756 for (i = 0; i < bset->n_ineq; ++i)
1757 p = print_constraint_c(p, dim, bset, bset->ineq[i], ">=",
1758 &first);
1759 return p;
1762 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
1763 __isl_keep isl_space *dim, __isl_keep isl_set *set)
1765 int i;
1767 if (!set)
1768 return isl_printer_free(p);
1770 if (set->n == 0)
1771 p = isl_printer_print_str(p, "0");
1773 for (i = 0; i < set->n; ++i) {
1774 if (i)
1775 p = isl_printer_print_str(p, " || ");
1776 if (set->n > 1)
1777 p = isl_printer_print_str(p, "(");
1778 p = print_basic_set_c(p, dim, set->p[i]);
1779 if (set->n > 1)
1780 p = isl_printer_print_str(p, ")");
1782 return p;
1785 static __isl_give isl_printer *print_pw_qpolynomial_c(
1786 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1788 int i;
1790 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set))
1791 return print_qpolynomial_c(p, pwqp->dim, pwqp->p[0].qp);
1793 for (i = 0; i < pwqp->n; ++i) {
1794 p = isl_printer_print_str(p, "(");
1795 p = print_set_c(p, pwqp->dim, pwqp->p[i].set);
1796 p = isl_printer_print_str(p, ") ? (");
1797 p = print_qpolynomial_c(p, pwqp->dim, pwqp->p[i].qp);
1798 p = isl_printer_print_str(p, ") : ");
1801 p = isl_printer_print_str(p, "0");
1802 return p;
1805 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
1806 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1808 if (!p || !pwqp)
1809 goto error;
1811 if (p->output_format == ISL_FORMAT_ISL)
1812 return print_pw_qpolynomial_isl(p, pwqp);
1813 else if (p->output_format == ISL_FORMAT_C)
1814 return print_pw_qpolynomial_c(p, pwqp);
1815 isl_assert(p->ctx, 0, goto error);
1816 error:
1817 isl_printer_free(p);
1818 return NULL;
1821 static int print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1823 struct isl_union_print_data *data;
1824 data = (struct isl_union_print_data *)user;
1826 if (!data->first)
1827 data->p = isl_printer_print_str(data->p, "; ");
1828 data->first = 0;
1830 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
1831 isl_pw_qpolynomial_free(pwqp);
1833 return 0;
1836 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
1837 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1839 struct isl_union_print_data data;
1840 struct isl_print_space_data space_data = { 0 };
1841 isl_space *dim;
1843 dim = isl_union_pw_qpolynomial_get_space(upwqp);
1844 if (isl_space_dim(dim, isl_dim_param) > 0) {
1845 p = print_tuple(dim, p, isl_dim_param, &space_data);
1846 p = isl_printer_print_str(p, " -> ");
1848 isl_space_free(dim);
1849 p = isl_printer_print_str(p, "{ ");
1850 data.p = p;
1851 data.first = 1;
1852 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
1853 &data);
1854 p = data.p;
1855 p = isl_printer_print_str(p, " }");
1856 return p;
1859 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
1860 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1862 if (!p || !upwqp)
1863 goto error;
1865 if (p->output_format == ISL_FORMAT_ISL)
1866 return print_union_pw_qpolynomial_isl(p, upwqp);
1867 isl_die(p->ctx, isl_error_invalid,
1868 "invalid output format for isl_union_pw_qpolynomial",
1869 goto error);
1870 error:
1871 isl_printer_free(p);
1872 return NULL;
1875 static __isl_give isl_printer *print_qpolynomial_fold_c(
1876 __isl_take isl_printer *p, __isl_keep isl_space *dim,
1877 __isl_keep isl_qpolynomial_fold *fold)
1879 int i;
1881 for (i = 0; i < fold->n - 1; ++i)
1882 if (fold->type == isl_fold_min)
1883 p = isl_printer_print_str(p, "min(");
1884 else if (fold->type == isl_fold_max)
1885 p = isl_printer_print_str(p, "max(");
1887 for (i = 0; i < fold->n; ++i) {
1888 if (i)
1889 p = isl_printer_print_str(p, ", ");
1890 p = print_qpolynomial_c(p, dim, fold->qp[i]);
1891 if (i)
1892 p = isl_printer_print_str(p, ")");
1894 return p;
1897 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
1898 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
1900 if (!p || !fold)
1901 goto error;
1902 if (p->output_format == ISL_FORMAT_ISL)
1903 return qpolynomial_fold_print(fold, p);
1904 else if (p->output_format == ISL_FORMAT_C)
1905 return print_qpolynomial_fold_c(p, fold->dim, fold);
1906 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
1907 goto error);
1908 error:
1909 isl_printer_free(p);
1910 return NULL;
1913 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
1914 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1916 int i;
1918 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set))
1919 return print_qpolynomial_fold_c(p, pwf->dim, pwf->p[0].fold);
1921 for (i = 0; i < pwf->n; ++i) {
1922 p = isl_printer_print_str(p, "(");
1923 p = print_set_c(p, pwf->dim, pwf->p[i].set);
1924 p = isl_printer_print_str(p, ") ? (");
1925 p = print_qpolynomial_fold_c(p, pwf->dim, pwf->p[i].fold);
1926 p = isl_printer_print_str(p, ") : ");
1929 p = isl_printer_print_str(p, "0");
1930 return p;
1933 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
1934 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1936 if (!p || !pwf)
1937 goto error;
1939 if (p->output_format == ISL_FORMAT_ISL)
1940 return print_pw_qpolynomial_fold_isl(p, pwf);
1941 else if (p->output_format == ISL_FORMAT_C)
1942 return print_pw_qpolynomial_fold_c(p, pwf);
1943 isl_assert(p->ctx, 0, goto error);
1944 error:
1945 isl_printer_free(p);
1946 return NULL;
1949 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
1950 FILE *out, unsigned output_format)
1952 isl_printer *p;
1954 if (!pwf)
1955 return;
1957 p = isl_printer_to_file(pwf->dim->ctx, out);
1958 p = isl_printer_set_output_format(p, output_format);
1959 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
1961 isl_printer_free(p);
1964 static int print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf, void *user)
1966 struct isl_union_print_data *data;
1967 data = (struct isl_union_print_data *)user;
1969 if (!data->first)
1970 data->p = isl_printer_print_str(data->p, "; ");
1971 data->first = 0;
1973 data->p = isl_pwf_print_isl_body(data->p, pwf);
1974 isl_pw_qpolynomial_fold_free(pwf);
1976 return 0;
1979 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
1980 __isl_take isl_printer *p,
1981 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1983 struct isl_union_print_data data;
1984 struct isl_print_space_data space_data = { 0 };
1985 isl_space *dim;
1987 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
1988 if (isl_space_dim(dim, isl_dim_param) > 0) {
1989 p = print_tuple(dim, p, isl_dim_param, &space_data);
1990 p = isl_printer_print_str(p, " -> ");
1992 isl_space_free(dim);
1993 p = isl_printer_print_str(p, "{ ");
1994 data.p = p;
1995 data.first = 1;
1996 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
1997 &print_pwf_body, &data);
1998 p = data.p;
1999 p = isl_printer_print_str(p, " }");
2000 return p;
2003 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2004 __isl_take isl_printer *p,
2005 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2007 if (!p || !upwf)
2008 goto error;
2010 if (p->output_format == ISL_FORMAT_ISL)
2011 return print_union_pw_qpolynomial_fold_isl(p, upwf);
2012 isl_die(p->ctx, isl_error_invalid,
2013 "invalid output format for isl_union_pw_qpolynomial_fold",
2014 goto error);
2015 error:
2016 isl_printer_free(p);
2017 return NULL;
2020 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
2021 __isl_keep isl_constraint *c)
2023 isl_basic_map *bmap;
2025 if (!p || !c)
2026 goto error;
2028 bmap = isl_basic_map_from_constraint(isl_constraint_copy(c));
2029 p = isl_printer_print_basic_map(p, bmap);
2030 isl_basic_map_free(bmap);
2031 return p;
2032 error:
2033 isl_printer_free(p);
2034 return NULL;
2037 static __isl_give isl_printer *isl_printer_print_space_isl(
2038 __isl_take isl_printer *p, __isl_keep isl_space *dim)
2040 struct isl_print_space_data data = { 0 };
2042 if (!dim)
2043 goto error;
2045 if (isl_space_dim(dim, isl_dim_param) > 0) {
2046 p = print_tuple(dim, p, isl_dim_param, &data);
2047 p = isl_printer_print_str(p, " -> ");
2050 p = isl_printer_print_str(p, "{ ");
2051 if (isl_space_is_params(dim))
2052 p = isl_printer_print_str(p, s_such_that[0]);
2053 else
2054 p = print_space(dim, p, 0, &data);
2055 p = isl_printer_print_str(p, " }");
2057 return p;
2058 error:
2059 isl_printer_free(p);
2060 return NULL;
2063 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
2064 __isl_keep isl_space *space)
2066 if (!p || !space)
2067 return isl_printer_free(p);
2068 if (p->output_format == ISL_FORMAT_ISL)
2069 return isl_printer_print_space_isl(p, space);
2070 else if (p->output_format == ISL_FORMAT_OMEGA)
2071 return print_omega_parameters(space, p);
2073 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
2074 "output format not supported for space",
2075 return isl_printer_free(p));
2078 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
2079 __isl_keep isl_local_space *ls)
2081 struct isl_print_space_data data = { 0 };
2082 unsigned total;
2083 unsigned n_div;
2085 if (!ls)
2086 goto error;
2088 total = isl_local_space_dim(ls, isl_dim_all);
2089 if (isl_local_space_dim(ls, isl_dim_param) > 0) {
2090 p = print_tuple(ls->dim, p, isl_dim_param, &data);
2091 p = isl_printer_print_str(p, " -> ");
2093 p = isl_printer_print_str(p, "{ ");
2094 p = print_space(ls->dim, p, 0, &data);
2095 n_div = isl_local_space_dim(ls, isl_dim_div);
2096 if (n_div > 0) {
2097 p = isl_printer_print_str(p, " : ");
2098 p = isl_printer_print_str(p, s_open_exists[0]);
2099 p = print_div_list(p, ls->dim, ls->div, 0);
2100 } else if (isl_space_is_params(ls->dim))
2101 p = isl_printer_print_str(p, s_such_that[0]);
2102 p = isl_printer_print_str(p, " }");
2103 return p;
2104 error:
2105 isl_printer_free(p);
2106 return NULL;
2109 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2110 __isl_keep isl_aff *aff)
2112 unsigned total;
2114 if (isl_aff_is_nan(aff))
2115 return isl_printer_print_str(p, "NaN");
2117 total = isl_local_space_dim(aff->ls, isl_dim_all);
2118 p = isl_printer_print_str(p, "(");
2119 p = print_affine_of_len(aff->ls->dim, aff->ls->div, p,
2120 aff->v->el + 1, 1 + total);
2121 if (isl_int_is_one(aff->v->el[0]))
2122 p = isl_printer_print_str(p, ")");
2123 else {
2124 p = isl_printer_print_str(p, ")/");
2125 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2128 return p;
2131 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2132 __isl_keep isl_aff *aff)
2134 struct isl_print_space_data data = { 0 };
2136 if (isl_space_is_params(aff->ls->dim))
2138 else {
2139 p = print_tuple(aff->ls->dim, p, isl_dim_set, &data);
2140 p = isl_printer_print_str(p, " -> ");
2142 p = isl_printer_print_str(p, "[");
2143 p = print_aff_body(p, aff);
2144 p = isl_printer_print_str(p, "]");
2146 return p;
2149 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2150 __isl_keep isl_aff *aff)
2152 struct isl_print_space_data data = { 0 };
2154 if (!aff)
2155 goto error;
2157 if (isl_local_space_dim(aff->ls, isl_dim_param) > 0) {
2158 p = print_tuple(aff->ls->dim, p, isl_dim_param, &data);
2159 p = isl_printer_print_str(p, " -> ");
2161 p = isl_printer_print_str(p, "{ ");
2162 p = print_aff(p, aff);
2163 p = isl_printer_print_str(p, " }");
2164 return p;
2165 error:
2166 isl_printer_free(p);
2167 return NULL;
2170 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2171 __isl_keep isl_pw_aff *pwaff)
2173 struct isl_print_space_data data = { 0 };
2174 int i;
2176 if (!pwaff)
2177 goto error;
2179 if (isl_space_dim(pwaff->dim, isl_dim_param) > 0) {
2180 p = print_tuple(pwaff->dim, p, isl_dim_param, &data);
2181 p = isl_printer_print_str(p, " -> ");
2183 p = isl_printer_print_str(p, "{ ");
2184 for (i = 0; i < pwaff->n; ++i) {
2185 if (i)
2186 p = isl_printer_print_str(p, "; ");
2187 p = print_aff(p, pwaff->p[i].aff);
2188 p = print_disjuncts((isl_map *)pwaff->p[i].set, p, 0);
2190 p = isl_printer_print_str(p, " }");
2191 return p;
2192 error:
2193 isl_printer_free(p);
2194 return NULL;
2197 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2198 __isl_keep isl_local_space *ls, isl_int *c);
2200 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2201 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2203 if (type == isl_dim_div) {
2204 p = isl_printer_print_str(p, "floord(");
2205 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2206 p = isl_printer_print_str(p, ", ");
2207 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2208 p = isl_printer_print_str(p, ")");
2209 } else {
2210 const char *name;
2212 name = isl_space_get_dim_name(ls->dim, type, pos);
2213 if (!name)
2214 name = "UNNAMED";
2215 p = isl_printer_print_str(p, name);
2217 return p;
2220 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2221 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2223 enum isl_dim_type type;
2225 if (pos == 0)
2226 return isl_printer_print_isl_int(p, c);
2228 if (isl_int_is_one(c))
2230 else if (isl_int_is_negone(c))
2231 p = isl_printer_print_str(p, "-");
2232 else {
2233 p = isl_printer_print_isl_int(p, c);
2234 p = isl_printer_print_str(p, "*");
2236 type = pos2type(ls->dim, &pos);
2237 p = print_ls_name_c(p, ls, type, pos);
2238 return p;
2241 static __isl_give isl_printer *print_ls_partial_affine_c(
2242 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2243 isl_int *c, unsigned len)
2245 int i;
2246 int first;
2248 for (i = 0, first = 1; i < len; ++i) {
2249 int flip = 0;
2250 if (isl_int_is_zero(c[i]))
2251 continue;
2252 if (!first) {
2253 if (isl_int_is_neg(c[i])) {
2254 flip = 1;
2255 isl_int_neg(c[i], c[i]);
2256 p = isl_printer_print_str(p, " - ");
2257 } else
2258 p = isl_printer_print_str(p, " + ");
2260 first = 0;
2261 p = print_ls_term_c(p, ls, c[i], i);
2262 if (flip)
2263 isl_int_neg(c[i], c[i]);
2265 if (first)
2266 p = isl_printer_print_str(p, "0");
2267 return p;
2270 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2271 __isl_keep isl_local_space *ls, isl_int *c)
2273 unsigned len = 1 + isl_local_space_dim(ls, isl_dim_all);
2274 return print_ls_partial_affine_c(p, ls, c, len);
2277 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2278 __isl_keep isl_aff *aff)
2280 unsigned total;
2282 total = isl_local_space_dim(aff->ls, isl_dim_all);
2283 if (!isl_int_is_one(aff->v->el[0]))
2284 p = isl_printer_print_str(p, "(");
2285 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2286 if (!isl_int_is_one(aff->v->el[0])) {
2287 p = isl_printer_print_str(p, ")/");
2288 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2290 return p;
2293 /* In the C format, we cannot express that "pwaff" may be undefined
2294 * on parts of the domain space. We therefore assume that the expression
2295 * will only be evaluated on its definition domain and compute the gist
2296 * of each cell with respect to this domain.
2298 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2299 __isl_keep isl_pw_aff *pwaff)
2301 isl_set *domain;
2302 isl_ast_build *build;
2303 isl_ast_expr *expr;
2305 if (pwaff->n < 1)
2306 isl_die(p->ctx, isl_error_unsupported,
2307 "cannot print empty isl_pw_aff in C format",
2308 return isl_printer_free(p));
2310 domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
2311 build = isl_ast_build_from_context(domain);
2312 expr = isl_ast_build_expr_from_pw_aff(build, isl_pw_aff_copy(pwaff));
2313 p = isl_printer_print_ast_expr(p, expr);
2314 isl_ast_expr_free(expr);
2315 isl_ast_build_free(build);
2317 return p;
2320 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2321 __isl_keep isl_aff *aff)
2323 if (!p || !aff)
2324 goto error;
2326 if (p->output_format == ISL_FORMAT_ISL)
2327 return print_aff_isl(p, aff);
2328 else if (p->output_format == ISL_FORMAT_C)
2329 return print_aff_c(p, aff);
2330 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2331 goto error);
2332 error:
2333 isl_printer_free(p);
2334 return NULL;
2337 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2338 __isl_keep isl_pw_aff *pwaff)
2340 if (!p || !pwaff)
2341 goto error;
2343 if (p->output_format == ISL_FORMAT_ISL)
2344 return print_pw_aff_isl(p, pwaff);
2345 else if (p->output_format == ISL_FORMAT_C)
2346 return print_pw_aff_c(p, pwaff);
2347 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2348 goto error);
2349 error:
2350 isl_printer_free(p);
2351 return NULL;
2354 /* Print dimension "pos" of data->space to "p".
2356 * data->user is assumed to be an isl_multi_aff.
2358 * If the current dimension is an output dimension, then print
2359 * the corresponding expression. Otherwise, print the name of the dimension.
2361 static __isl_give isl_printer *print_dim_ma(__isl_take isl_printer *p,
2362 struct isl_print_space_data *data, unsigned pos)
2364 isl_multi_aff *ma = data->user;
2366 if (data->type == isl_dim_out)
2367 p = print_aff_body(p, ma->p[pos]);
2368 else
2369 p = print_name(data->space, p, data->type, pos, data->latex);
2371 return p;
2374 static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
2375 __isl_keep isl_multi_aff *maff)
2377 struct isl_print_space_data data = { 0 };
2379 data.print_dim = &print_dim_ma;
2380 data.user = maff;
2381 return print_space(maff->space, p, 0, &data);
2384 static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
2385 __isl_keep isl_multi_aff *maff)
2387 struct isl_print_space_data data = { 0 };
2389 if (!maff)
2390 goto error;
2392 if (isl_space_dim(maff->space, isl_dim_param) > 0) {
2393 p = print_tuple(maff->space, p, isl_dim_param, &data);
2394 p = isl_printer_print_str(p, " -> ");
2396 p = isl_printer_print_str(p, "{ ");
2397 p = print_multi_aff(p, maff);
2398 p = isl_printer_print_str(p, " }");
2399 return p;
2400 error:
2401 isl_printer_free(p);
2402 return NULL;
2405 __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
2406 __isl_keep isl_multi_aff *maff)
2408 if (!p || !maff)
2409 goto error;
2411 if (p->output_format == ISL_FORMAT_ISL)
2412 return print_multi_aff_isl(p, maff);
2413 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2414 goto error);
2415 error:
2416 isl_printer_free(p);
2417 return NULL;
2420 static __isl_give isl_printer *print_pw_multi_aff_body(
2421 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2423 int i;
2425 if (!pma)
2426 goto error;
2428 for (i = 0; i < pma->n; ++i) {
2429 if (i)
2430 p = isl_printer_print_str(p, "; ");
2431 p = print_multi_aff(p, pma->p[i].maff);
2432 p = print_disjuncts((isl_map *)pma->p[i].set, p, 0);
2434 return p;
2435 error:
2436 isl_printer_free(p);
2437 return NULL;
2440 static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
2441 __isl_keep isl_pw_multi_aff *pma)
2443 struct isl_print_space_data data = { 0 };
2445 if (!pma)
2446 goto error;
2448 if (isl_space_dim(pma->dim, isl_dim_param) > 0) {
2449 p = print_tuple(pma->dim, p, isl_dim_param, &data);
2450 p = isl_printer_print_str(p, " -> ");
2452 p = isl_printer_print_str(p, "{ ");
2453 p = print_pw_multi_aff_body(p, pma);
2454 p = isl_printer_print_str(p, " }");
2455 return p;
2456 error:
2457 isl_printer_free(p);
2458 return NULL;
2461 static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
2462 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2464 int i;
2466 for (i = 0; i < pma->n - 1; ++i) {
2467 p = isl_printer_print_str(p, "(");
2468 p = print_set_c(p, pma->dim, pma->p[i].set);
2469 p = isl_printer_print_str(p, ") ? (");
2470 p = print_aff_c(p, pma->p[i].maff->p[0]);
2471 p = isl_printer_print_str(p, ") : ");
2474 return print_aff_c(p, pma->p[pma->n - 1].maff->p[0]);
2477 static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
2478 __isl_keep isl_pw_multi_aff *pma)
2480 int n;
2481 const char *name;
2483 if (!pma)
2484 goto error;
2485 if (pma->n < 1)
2486 isl_die(p->ctx, isl_error_unsupported,
2487 "cannot print empty isl_pw_multi_aff in C format",
2488 goto error);
2489 name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
2490 if (!name && isl_pw_multi_aff_dim(pma, isl_dim_out) == 1)
2491 return print_unnamed_pw_multi_aff_c(p, pma);
2492 if (!name)
2493 isl_die(p->ctx, isl_error_unsupported,
2494 "cannot print unnamed isl_pw_multi_aff in C format",
2495 goto error);
2497 p = isl_printer_print_str(p, name);
2498 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
2499 if (n != 0)
2500 isl_die(p->ctx, isl_error_unsupported,
2501 "not supported yet", goto error);
2503 return p;
2504 error:
2505 isl_printer_free(p);
2506 return NULL;
2509 __isl_give isl_printer *isl_printer_print_pw_multi_aff(
2510 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2512 if (!p || !pma)
2513 goto error;
2515 if (p->output_format == ISL_FORMAT_ISL)
2516 return print_pw_multi_aff_isl(p, pma);
2517 if (p->output_format == ISL_FORMAT_C)
2518 return print_pw_multi_aff_c(p, pma);
2519 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2520 goto error);
2521 error:
2522 isl_printer_free(p);
2523 return NULL;
2526 static int print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
2527 void *user)
2529 struct isl_union_print_data *data;
2530 data = (struct isl_union_print_data *) user;
2532 if (!data->first)
2533 data->p = isl_printer_print_str(data->p, "; ");
2534 data->first = 0;
2536 data->p = print_pw_multi_aff_body(data->p, pma);
2537 isl_pw_multi_aff_free(pma);
2539 return 0;
2542 static __isl_give isl_printer *print_union_pw_multi_aff_isl(
2543 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2545 struct isl_union_print_data data;
2546 struct isl_print_space_data space_data = { 0 };
2547 isl_space *space;
2549 space = isl_union_pw_multi_aff_get_space(upma);
2550 if (isl_space_dim(space, isl_dim_param) > 0) {
2551 p = print_tuple(space, p, isl_dim_param, &space_data);
2552 p = isl_printer_print_str(p, s_to[0]);
2554 isl_space_free(space);
2555 p = isl_printer_print_str(p, s_open_set[0]);
2556 data.p = p;
2557 data.first = 1;
2558 isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
2559 &print_pw_multi_aff_body_wrap, &data);
2560 p = data.p;
2561 p = isl_printer_print_str(p, s_close_set[0]);
2562 return p;
2565 __isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
2566 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2568 if (!p || !upma)
2569 goto error;
2571 if (p->output_format == ISL_FORMAT_ISL)
2572 return print_union_pw_multi_aff_isl(p, upma);
2573 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2574 goto error);
2575 error:
2576 isl_printer_free(p);
2577 return NULL;
2580 /* Print dimension "pos" of data->space to "p".
2582 * data->user is assumed to be an isl_multi_pw_aff.
2584 * If the current dimension is an output dimension, then print
2585 * the corresponding piecewise affine expression.
2586 * Otherwise, print the name of the dimension.
2588 static __isl_give isl_printer *print_dim_mpa(__isl_take isl_printer *p,
2589 struct isl_print_space_data *data, unsigned pos)
2591 int i;
2592 int need_parens;
2593 isl_multi_pw_aff *mpa = data->user;
2594 isl_pw_aff *pa;
2596 if (data->type != isl_dim_out)
2597 return print_name(data->space, p, data->type, pos, data->latex);
2599 pa = mpa->p[pos];
2600 if (pa->n == 0)
2601 return isl_printer_print_str(p, "(0 : 1 = 0)");
2603 need_parens = pa->n != 1 || !isl_set_plain_is_universe(pa->p[0].set);
2604 if (need_parens)
2605 p = isl_printer_print_str(p, "(");
2606 for (i = 0; i < pa->n; ++i) {
2607 if (i)
2608 p = isl_printer_print_str(p, "; ");
2609 p = print_aff_body(p, pa->p[i].aff);
2610 p = print_disjuncts(pa->p[i].set, p, 0);
2612 if (need_parens)
2613 p = isl_printer_print_str(p, ")");
2615 return p;
2618 /* Print "mpa" to "p" in isl format.
2620 static __isl_give isl_printer *print_multi_pw_aff_isl(__isl_take isl_printer *p,
2621 __isl_keep isl_multi_pw_aff *mpa)
2623 struct isl_print_space_data data = { 0 };
2625 if (!mpa)
2626 return isl_printer_free(p);
2628 if (isl_space_dim(mpa->space, isl_dim_param) > 0) {
2629 p = print_tuple(mpa->space, p, isl_dim_param, &data);
2630 p = isl_printer_print_str(p, " -> ");
2632 p = isl_printer_print_str(p, "{ ");
2633 data.print_dim = &print_dim_mpa;
2634 data.user = mpa;
2635 p = print_space(mpa->space, p, 0, &data);
2636 p = isl_printer_print_str(p, " }");
2637 return p;
2640 __isl_give isl_printer *isl_printer_print_multi_pw_aff(
2641 __isl_take isl_printer *p, __isl_keep isl_multi_pw_aff *mpa)
2643 if (!p || !mpa)
2644 return isl_printer_free(p);
2646 if (p->output_format == ISL_FORMAT_ISL)
2647 return print_multi_pw_aff_isl(p, mpa);
2648 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2649 return isl_printer_free(p));
2652 /* Print dimension "pos" of data->space to "p".
2654 * data->user is assumed to be an isl_multi_val.
2656 * If the current dimension is an output dimension, then print
2657 * the corresponding value. Otherwise, print the name of the dimension.
2659 static __isl_give isl_printer *print_dim_mv(__isl_take isl_printer *p,
2660 struct isl_print_space_data *data, unsigned pos)
2662 isl_multi_val *mv = data->user;
2664 if (data->type == isl_dim_out)
2665 return isl_printer_print_val(p, mv->p[pos]);
2666 else
2667 return print_name(data->space, p, data->type, pos, data->latex);
2670 /* Print the isl_multi_val "mv" to "p" in isl format.
2672 static __isl_give isl_printer *print_multi_val_isl(__isl_take isl_printer *p,
2673 __isl_keep isl_multi_val *mv)
2675 struct isl_print_space_data data = { 0 };
2677 if (!mv)
2678 return isl_printer_free(p);
2680 if (isl_space_dim(mv->space, isl_dim_param) > 0) {
2681 p = print_tuple(mv->space, p, isl_dim_param, &data);
2682 p = isl_printer_print_str(p, " -> ");
2684 p = isl_printer_print_str(p, "{ ");
2685 data.print_dim = &print_dim_mv;
2686 data.user = mv;
2687 p = print_space(mv->space, p, 0, &data);
2688 p = isl_printer_print_str(p, " }");
2689 return p;
2692 /* Print the isl_multi_val "mv" to "p".
2694 * Currently only supported in isl format.
2696 __isl_give isl_printer *isl_printer_print_multi_val(
2697 __isl_take isl_printer *p, __isl_keep isl_multi_val *mv)
2699 if (!p || !mv)
2700 return isl_printer_free(p);
2702 if (p->output_format == ISL_FORMAT_ISL)
2703 return print_multi_val_isl(p, mv);
2704 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2705 return isl_printer_free(p));