isl_schedule.c: extract out reset_band
[isl.git] / isl_output.c
blob2bc8befff69c3bac914082309c9007f6d76feec3
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_expr.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 static __isl_give isl_printer *basic_map_print_omega(
580 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
582 p = isl_printer_print_str(p, "{ [");
583 p = print_var_list(p, bmap->dim, isl_dim_in);
584 p = isl_printer_print_str(p, "] -> [");
585 p = print_var_list(p, bmap->dim, isl_dim_out);
586 p = isl_printer_print_str(p, "] ");
587 p = isl_printer_print_str(p, ": ");
588 p = print_disjunct(bmap, bmap->dim, p, 0);
589 p = isl_printer_print_str(p, " }");
590 return p;
593 static __isl_give isl_printer *basic_set_print_omega(
594 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
596 p = isl_printer_print_str(p, "{ [");
597 p = print_var_list(p, bset->dim, isl_dim_set);
598 p = isl_printer_print_str(p, "] ");
599 p = isl_printer_print_str(p, ": ");
600 p = print_disjunct(bset, bset->dim, p, 0);
601 p = isl_printer_print_str(p, " }");
602 return p;
605 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
606 __isl_take isl_printer *p)
608 int i;
610 for (i = 0; i < map->n; ++i) {
611 if (i)
612 p = isl_printer_print_str(p, " union ");
613 p = basic_map_print_omega(map->p[i], p);
615 return p;
618 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
619 __isl_take isl_printer *p)
621 int i;
623 for (i = 0; i < set->n; ++i) {
624 if (i)
625 p = isl_printer_print_str(p, " union ");
626 p = basic_set_print_omega(set->p[i], p);
628 return p;
631 static __isl_give isl_printer *isl_basic_map_print_isl(
632 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
633 int latex)
635 struct isl_print_space_data data = { .latex = latex };
636 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
638 if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
639 p = print_tuple(bmap->dim, p, isl_dim_param, &data);
640 p = isl_printer_print_str(p, " -> ");
642 p = isl_printer_print_str(p, "{ ");
643 p = print_space(bmap->dim, p, rational, &data);
644 p = isl_printer_print_str(p, " : ");
645 p = print_disjunct(bmap, bmap->dim, p, latex);
646 p = isl_printer_print_str(p, " }");
647 return p;
650 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
651 __isl_take isl_printer *p, int latex)
653 int i;
655 if (isl_map_plain_is_universe(map))
656 return p;
658 p = isl_printer_print_str(p, s_such_that[latex]);
659 if (map->n == 0)
660 p = isl_printer_print_str(p, "1 = 0");
661 for (i = 0; i < map->n; ++i) {
662 if (i)
663 p = isl_printer_print_str(p, s_or[latex]);
664 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
665 p = isl_printer_print_str(p, "(");
666 p = print_disjunct(map->p[i], map->dim, p, latex);
667 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
668 p = isl_printer_print_str(p, ")");
670 return p;
673 /* Print the disjuncts of a map (or set).
674 * If the map turns out to be a universal parameter domain, then
675 * we need to print the colon. Otherwise, the output looks identical
676 * to the empty set.
678 static __isl_give isl_printer *print_disjuncts_map(__isl_keep isl_map *map,
679 __isl_take isl_printer *p, int latex)
681 if (isl_map_plain_is_universe(map) && isl_space_is_params(map->dim))
682 return isl_printer_print_str(p, s_such_that[latex]);
683 else
684 return print_disjuncts(map, p, latex);
687 struct isl_aff_split {
688 isl_basic_map *aff;
689 isl_map *map;
692 static void free_split(__isl_take struct isl_aff_split *split, int n)
694 int i;
696 if (!split)
697 return;
699 for (i = 0; i < n; ++i) {
700 isl_basic_map_free(split[i].aff);
701 isl_map_free(split[i].map);
704 free(split);
707 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
709 int i, j;
710 unsigned nparam, n_in, n_out, total;
712 bmap = isl_basic_map_cow(bmap);
713 if (!bmap)
714 return NULL;
715 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
716 goto error;
718 nparam = isl_basic_map_dim(bmap, isl_dim_param);
719 n_in = isl_basic_map_dim(bmap, isl_dim_in);
720 n_out = isl_basic_map_dim(bmap, isl_dim_out);
721 total = isl_basic_map_dim(bmap, isl_dim_all);
722 for (i = bmap->n_eq - 1; i >= 0; --i) {
723 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
724 if (j >= nparam && j < nparam + n_in + n_out &&
725 (isl_int_is_one(bmap->eq[i][1 + j]) ||
726 isl_int_is_negone(bmap->eq[i][1 + j])))
727 continue;
728 if (isl_basic_map_drop_equality(bmap, i) < 0)
729 goto error;
732 bmap = isl_basic_map_finalize(bmap);
734 return bmap;
735 error:
736 isl_basic_map_free(bmap);
737 return NULL;
740 static int aff_split_cmp(const void *p1, const void *p2, void *user)
742 const struct isl_aff_split *s1, *s2;
743 s1 = (const struct isl_aff_split *) p1;
744 s2 = (const struct isl_aff_split *) p2;
746 return isl_basic_map_plain_cmp(s1->aff, s2->aff);
749 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
750 __isl_keep isl_basic_map *aff)
752 int i, j;
753 unsigned total;
755 if (!bmap || !aff)
756 goto error;
758 total = isl_space_dim(bmap->dim, isl_dim_all);
760 for (i = bmap->n_eq - 1; i >= 0; --i) {
761 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
762 bmap->n_div) != -1)
763 continue;
764 for (j = 0; j < aff->n_eq; ++j) {
765 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
766 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
767 continue;
768 if (isl_basic_map_drop_equality(bmap, i) < 0)
769 goto error;
770 break;
774 return bmap;
775 error:
776 isl_basic_map_free(bmap);
777 return NULL;
780 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
782 int i, n;
783 struct isl_aff_split *split;
784 isl_ctx *ctx;
786 ctx = isl_map_get_ctx(map);
787 split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
788 if (!split)
789 return NULL;
791 for (i = 0; i < map->n; ++i) {
792 isl_basic_map *bmap;
793 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
794 bmap = isl_basic_map_copy(map->p[i]);
795 bmap = isl_basic_map_cow(bmap);
796 bmap = drop_aff(bmap, split[i].aff);
797 split[i].map = isl_map_from_basic_map(bmap);
798 if (!split[i].aff || !split[i].map)
799 goto error;
802 if (isl_sort(split, map->n, sizeof(struct isl_aff_split),
803 &aff_split_cmp, NULL) < 0)
804 goto error;
806 n = map->n;
807 for (i = n - 1; i >= 1; --i) {
808 if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
809 split[i].aff))
810 continue;
811 isl_basic_map_free(split[i].aff);
812 split[i - 1].map = isl_map_union(split[i - 1].map,
813 split[i].map);
814 if (i != n - 1)
815 split[i] = split[n - 1];
816 split[n - 1].aff = NULL;
817 split[n - 1].map = NULL;
818 --n;
821 return split;
822 error:
823 free_split(split, map->n);
824 return NULL;
827 static int defining_equality(__isl_keep isl_basic_map *eq,
828 __isl_keep isl_space *dim, enum isl_dim_type type, int pos)
830 int i;
831 unsigned total;
833 if (!eq)
834 return -1;
836 pos += isl_space_offset(dim, type);
837 total = isl_basic_map_total_dim(eq);
839 for (i = 0; i < eq->n_eq; ++i) {
840 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
841 continue;
842 if (isl_int_is_one(eq->eq[i][1 + pos]))
843 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
844 return i;
847 return -1;
850 /* Print dimension "pos" of data->space to "p".
852 * data->user is assumed to be an isl_basic_map keeping track of equalities.
854 * If the current dimension is defined by these equalities, then print
855 * the corresponding expression. Otherwise, print the name of the dimension.
857 static __isl_give isl_printer *print_dim_eq(__isl_take isl_printer *p,
858 struct isl_print_space_data *data, unsigned pos)
860 isl_basic_map *eq = data->user;
861 int j;
863 j = defining_equality(eq, data->space, data->type, pos);
864 if (j >= 0) {
865 pos += 1 + isl_space_offset(data->space, data->type);
866 p = print_affine_of_len(eq->dim, NULL, p, eq->eq[j], pos);
867 } else {
868 p = print_name(data->space, p, data->type, pos, data->latex);
871 return p;
874 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
875 struct isl_aff_split *split, int n)
877 struct isl_print_space_data data = { 0 };
878 int i;
879 int rational;
881 data.print_dim = &print_dim_eq;
882 for (i = 0; i < n; ++i) {
883 isl_space *dim;
885 if (!split[i].map)
886 break;
887 dim = split[i].map->dim;
888 rational = split[i].map->n > 0 &&
889 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
890 if (i)
891 p = isl_printer_print_str(p, "; ");
892 data.user = split[i].aff;
893 p = print_space(dim, p, rational, &data);
894 p = print_disjuncts_map(split[i].map, p, 0);
897 return p;
900 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
901 __isl_take isl_printer *p)
903 struct isl_print_space_data data = { 0 };
904 struct isl_aff_split *split = NULL;
905 int rational;
907 if (map->n > 0)
908 split = split_aff(map);
909 if (split) {
910 p = print_split_map(p, split, map->n);
911 } else {
912 rational = map->n > 0 &&
913 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
914 p = print_space(map->dim, p, rational, &data);
915 p = print_disjuncts_map(map, p, 0);
917 free_split(split, map->n);
918 return p;
921 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
922 __isl_take isl_printer *p)
924 struct isl_print_space_data data = { 0 };
926 if (isl_map_dim(map, isl_dim_param) > 0) {
927 p = print_tuple(map->dim, p, isl_dim_param, &data);
928 p = isl_printer_print_str(p, s_to[0]);
930 p = isl_printer_print_str(p, s_open_set[0]);
931 p = isl_map_print_isl_body(map, p);
932 p = isl_printer_print_str(p, s_close_set[0]);
933 return p;
936 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
937 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff)
939 struct isl_print_space_data data = { 0 };
941 data.latex = 1;
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[1]);
946 p = isl_printer_print_str(p, s_open_set[1]);
947 data.print_dim = &print_dim_eq;
948 data.user = aff;
949 p = print_space(map->dim, p, 0, &data);
950 p = print_disjuncts_map(map, p, 1);
951 p = isl_printer_print_str(p, s_close_set[1]);
953 return p;
956 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
957 __isl_take isl_printer *p)
959 int i;
960 struct isl_aff_split *split = NULL;
962 if (map->n > 0)
963 split = split_aff(map);
965 if (!split)
966 return print_latex_map(map, p, NULL);
968 for (i = 0; i < map->n; ++i) {
969 if (!split[i].map)
970 break;
971 if (i)
972 p = isl_printer_print_str(p, " \\cup ");
973 p = print_latex_map(split[i].map, p, split[i].aff);
976 free_split(split, map->n);
977 return p;
980 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
981 __isl_keep isl_basic_map *bmap)
983 if (!p || !bmap)
984 goto error;
985 if (p->output_format == ISL_FORMAT_ISL)
986 return isl_basic_map_print_isl(bmap, p, 0);
987 else if (p->output_format == ISL_FORMAT_OMEGA)
988 return basic_map_print_omega(bmap, p);
989 isl_assert(bmap->ctx, 0, goto error);
990 error:
991 isl_printer_free(p);
992 return NULL;
995 void isl_basic_map_print(__isl_keep isl_basic_map *bmap, FILE *out, int indent,
996 const char *prefix, const char *suffix, unsigned output_format)
998 isl_printer *printer;
1000 if (!bmap)
1001 return;
1003 printer = isl_printer_to_file(bmap->ctx, out);
1004 printer = isl_printer_set_indent(printer, indent);
1005 printer = isl_printer_set_prefix(printer, prefix);
1006 printer = isl_printer_set_suffix(printer, suffix);
1007 printer = isl_printer_set_output_format(printer, output_format);
1008 isl_printer_print_basic_map(printer, bmap);
1010 isl_printer_free(printer);
1013 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
1014 __isl_keep isl_basic_set *bset)
1016 if (!p || !bset)
1017 goto error;
1019 if (p->output_format == ISL_FORMAT_ISL)
1020 return isl_basic_map_print_isl(bset, p, 0);
1021 else if (p->output_format == ISL_FORMAT_POLYLIB)
1022 return isl_basic_set_print_polylib(bset, p, 0);
1023 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1024 return isl_basic_set_print_polylib(bset, p, 1);
1025 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
1026 return bset_print_constraints_polylib(bset, p);
1027 else if (p->output_format == ISL_FORMAT_OMEGA)
1028 return basic_set_print_omega(bset, p);
1029 isl_assert(p->ctx, 0, goto error);
1030 error:
1031 isl_printer_free(p);
1032 return NULL;
1035 void isl_basic_set_print(struct isl_basic_set *bset, FILE *out, int indent,
1036 const char *prefix, const char *suffix, unsigned output_format)
1038 isl_printer *printer;
1040 if (!bset)
1041 return;
1043 printer = isl_printer_to_file(bset->ctx, out);
1044 printer = isl_printer_set_indent(printer, indent);
1045 printer = isl_printer_set_prefix(printer, prefix);
1046 printer = isl_printer_set_suffix(printer, suffix);
1047 printer = isl_printer_set_output_format(printer, output_format);
1048 isl_printer_print_basic_set(printer, bset);
1050 isl_printer_free(printer);
1053 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
1054 __isl_keep isl_set *set)
1056 if (!p || !set)
1057 goto error;
1058 if (p->output_format == ISL_FORMAT_ISL)
1059 return isl_map_print_isl((isl_map *)set, p);
1060 else if (p->output_format == ISL_FORMAT_POLYLIB)
1061 return isl_set_print_polylib(set, p, 0);
1062 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1063 return isl_set_print_polylib(set, p, 1);
1064 else if (p->output_format == ISL_FORMAT_OMEGA)
1065 return isl_set_print_omega(set, p);
1066 else if (p->output_format == ISL_FORMAT_LATEX)
1067 return isl_map_print_latex((isl_map *)set, p);
1068 isl_assert(set->ctx, 0, goto error);
1069 error:
1070 isl_printer_free(p);
1071 return NULL;
1074 void isl_set_print(struct isl_set *set, FILE *out, int indent,
1075 unsigned output_format)
1077 isl_printer *printer;
1079 if (!set)
1080 return;
1082 printer = isl_printer_to_file(set->ctx, out);
1083 printer = isl_printer_set_indent(printer, indent);
1084 printer = isl_printer_set_output_format(printer, output_format);
1085 printer = isl_printer_print_set(printer, set);
1087 isl_printer_free(printer);
1090 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1091 __isl_keep isl_map *map)
1093 if (!p || !map)
1094 goto error;
1096 if (p->output_format == ISL_FORMAT_ISL)
1097 return isl_map_print_isl(map, p);
1098 else if (p->output_format == ISL_FORMAT_POLYLIB)
1099 return isl_map_print_polylib(map, p, 0);
1100 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1101 return isl_map_print_polylib(map, p, 1);
1102 else if (p->output_format == ISL_FORMAT_OMEGA)
1103 return isl_map_print_omega(map, p);
1104 else if (p->output_format == ISL_FORMAT_LATEX)
1105 return isl_map_print_latex(map, p);
1106 isl_assert(map->ctx, 0, goto error);
1107 error:
1108 isl_printer_free(p);
1109 return NULL;
1112 struct isl_union_print_data {
1113 isl_printer *p;
1114 int first;
1117 static int print_map_body(__isl_take isl_map *map, void *user)
1119 struct isl_union_print_data *data;
1120 data = (struct isl_union_print_data *)user;
1122 if (!data->first)
1123 data->p = isl_printer_print_str(data->p, "; ");
1124 data->first = 0;
1126 data->p = isl_map_print_isl_body(map, data->p);
1127 isl_map_free(map);
1129 return 0;
1132 static __isl_give isl_printer *isl_union_map_print_isl(
1133 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1135 struct isl_union_print_data data = { p, 1 };
1136 struct isl_print_space_data space_data = { 0 };
1137 isl_space *dim;
1139 dim = isl_union_map_get_space(umap);
1140 if (isl_space_dim(dim, isl_dim_param) > 0) {
1141 p = print_tuple(dim, p, isl_dim_param, &space_data);
1142 p = isl_printer_print_str(p, s_to[0]);
1144 isl_space_free(dim);
1145 p = isl_printer_print_str(p, s_open_set[0]);
1146 isl_union_map_foreach_map(umap, &print_map_body, &data);
1147 p = data.p;
1148 p = isl_printer_print_str(p, s_close_set[0]);
1149 return p;
1152 static int print_latex_map_body(__isl_take isl_map *map, void *user)
1154 struct isl_union_print_data *data;
1155 data = (struct isl_union_print_data *)user;
1157 if (!data->first)
1158 data->p = isl_printer_print_str(data->p, " \\cup ");
1159 data->first = 0;
1161 data->p = isl_map_print_latex(map, data->p);
1162 isl_map_free(map);
1164 return 0;
1167 static __isl_give isl_printer *isl_union_map_print_latex(
1168 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1170 struct isl_union_print_data data = { p, 1 };
1171 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1172 p = data.p;
1173 return p;
1176 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1177 __isl_keep isl_union_map *umap)
1179 if (!p || !umap)
1180 goto error;
1182 if (p->output_format == ISL_FORMAT_ISL)
1183 return isl_union_map_print_isl(umap, p);
1184 if (p->output_format == ISL_FORMAT_LATEX)
1185 return isl_union_map_print_latex(umap, p);
1187 isl_die(p->ctx, isl_error_invalid,
1188 "invalid output format for isl_union_map", goto error);
1189 error:
1190 isl_printer_free(p);
1191 return NULL;
1194 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1195 __isl_keep isl_union_set *uset)
1197 if (!p || !uset)
1198 goto error;
1200 if (p->output_format == ISL_FORMAT_ISL)
1201 return isl_union_map_print_isl((isl_union_map *)uset, p);
1202 if (p->output_format == ISL_FORMAT_LATEX)
1203 return isl_union_map_print_latex((isl_union_map *)uset, p);
1205 isl_die(p->ctx, isl_error_invalid,
1206 "invalid output format for isl_union_set", goto error);
1207 error:
1208 isl_printer_free(p);
1209 return NULL;
1212 void isl_map_print(__isl_keep isl_map *map, FILE *out, int indent,
1213 unsigned output_format)
1215 isl_printer *printer;
1217 if (!map)
1218 return;
1220 printer = isl_printer_to_file(map->ctx, out);
1221 printer = isl_printer_set_indent(printer, indent);
1222 printer = isl_printer_set_output_format(printer, output_format);
1223 printer = isl_printer_print_map(printer, map);
1225 isl_printer_free(printer);
1228 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
1230 int i;
1231 int n;
1233 for (i = 0, n = 0; i < rec->n; ++i)
1234 if (!isl_upoly_is_zero(rec->p[i]))
1235 ++n;
1237 return n;
1240 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
1241 __isl_take isl_printer *p, int first)
1243 struct isl_upoly_cst *cst;
1244 int neg;
1246 cst = isl_upoly_as_cst(up);
1247 if (!cst)
1248 goto error;
1249 neg = !first && isl_int_is_neg(cst->n);
1250 if (!first)
1251 p = isl_printer_print_str(p, neg ? " - " : " + ");
1252 if (neg)
1253 isl_int_neg(cst->n, cst->n);
1254 if (isl_int_is_zero(cst->d)) {
1255 int sgn = isl_int_sgn(cst->n);
1256 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1257 sgn == 0 ? "NaN" : "infty");
1258 } else
1259 p = isl_printer_print_isl_int(p, cst->n);
1260 if (neg)
1261 isl_int_neg(cst->n, cst->n);
1262 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1263 p = isl_printer_print_str(p, "/");
1264 p = isl_printer_print_isl_int(p, cst->d);
1266 return p;
1267 error:
1268 isl_printer_free(p);
1269 return NULL;
1272 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1273 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var)
1275 unsigned total;
1277 total = isl_space_dim(dim, isl_dim_all);
1278 if (var < total)
1279 p = print_term(dim, NULL, dim->ctx->one, 1 + var, p, 0);
1280 else
1281 p = print_div(dim, div, var - total, p);
1282 return p;
1285 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1286 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var, int exp)
1288 p = print_base(p, dim, div, var);
1289 if (exp == 1)
1290 return p;
1291 if (p->output_format == ISL_FORMAT_C) {
1292 int i;
1293 for (i = 1; i < exp; ++i) {
1294 p = isl_printer_print_str(p, "*");
1295 p = print_base(p, dim, div, var);
1297 } else {
1298 p = isl_printer_print_str(p, "^");
1299 p = isl_printer_print_int(p, exp);
1301 return p;
1304 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1305 __isl_keep isl_space *dim, __isl_keep isl_mat *div,
1306 __isl_take isl_printer *p, int outer)
1308 int i, n, first, print_parens;
1309 struct isl_upoly_rec *rec;
1311 if (!p || !up || !dim || !div)
1312 goto error;
1314 if (isl_upoly_is_cst(up))
1315 return upoly_print_cst(up, p, 1);
1317 rec = isl_upoly_as_rec(up);
1318 if (!rec)
1319 goto error;
1320 n = upoly_rec_n_non_zero(rec);
1321 print_parens = n > 1 ||
1322 (outer && rec->up.var >= isl_space_dim(dim, isl_dim_all));
1323 if (print_parens)
1324 p = isl_printer_print_str(p, "(");
1325 for (i = 0, first = 1; i < rec->n; ++i) {
1326 if (isl_upoly_is_zero(rec->p[i]))
1327 continue;
1328 if (isl_upoly_is_negone(rec->p[i])) {
1329 if (!i)
1330 p = isl_printer_print_str(p, "-1");
1331 else if (first)
1332 p = isl_printer_print_str(p, "-");
1333 else
1334 p = isl_printer_print_str(p, " - ");
1335 } else if (isl_upoly_is_cst(rec->p[i]) &&
1336 !isl_upoly_is_one(rec->p[i]))
1337 p = upoly_print_cst(rec->p[i], p, first);
1338 else {
1339 if (!first)
1340 p = isl_printer_print_str(p, " + ");
1341 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1342 p = upoly_print(rec->p[i], dim, div, p, 0);
1344 first = 0;
1345 if (i == 0)
1346 continue;
1347 if (!isl_upoly_is_one(rec->p[i]) &&
1348 !isl_upoly_is_negone(rec->p[i]))
1349 p = isl_printer_print_str(p, " * ");
1350 p = print_pow(p, dim, div, rec->up.var, i);
1352 if (print_parens)
1353 p = isl_printer_print_str(p, ")");
1354 return p;
1355 error:
1356 isl_printer_free(p);
1357 return NULL;
1360 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1361 __isl_keep isl_qpolynomial *qp)
1363 if (!p || !qp)
1364 goto error;
1365 p = upoly_print(qp->upoly, qp->dim, qp->div, p, 1);
1366 return p;
1367 error:
1368 isl_printer_free(p);
1369 return NULL;
1372 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1373 __isl_keep isl_qpolynomial *qp)
1375 struct isl_print_space_data data = { 0 };
1377 if (!p || !qp)
1378 goto error;
1380 if (isl_space_dim(qp->dim, isl_dim_param) > 0) {
1381 p = print_tuple(qp->dim, p, isl_dim_param, &data);
1382 p = isl_printer_print_str(p, " -> ");
1384 p = isl_printer_print_str(p, "{ ");
1385 if (!isl_space_is_params(qp->dim)) {
1386 p = print_space(qp->dim, p, 0, &data);
1387 p = isl_printer_print_str(p, " -> ");
1389 p = print_qpolynomial(p, qp);
1390 p = isl_printer_print_str(p, " }");
1391 return p;
1392 error:
1393 isl_printer_free(p);
1394 return NULL;
1397 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1398 __isl_keep isl_space *dim, __isl_keep isl_qpolynomial *qp)
1400 isl_int den;
1402 isl_int_init(den);
1403 isl_qpolynomial_get_den(qp, &den);
1404 if (!isl_int_is_one(den)) {
1405 isl_qpolynomial *f;
1406 p = isl_printer_print_str(p, "(");
1407 qp = isl_qpolynomial_copy(qp);
1408 f = isl_qpolynomial_rat_cst_on_domain(isl_space_copy(qp->dim),
1409 den, qp->dim->ctx->one);
1410 qp = isl_qpolynomial_mul(qp, f);
1412 if (qp)
1413 p = upoly_print(qp->upoly, dim, qp->div, p, 0);
1414 else
1415 p = isl_printer_free(p);
1416 if (!isl_int_is_one(den)) {
1417 p = isl_printer_print_str(p, ")/");
1418 p = isl_printer_print_isl_int(p, den);
1419 isl_qpolynomial_free(qp);
1421 isl_int_clear(den);
1422 return p;
1425 __isl_give isl_printer *isl_printer_print_qpolynomial(
1426 __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1428 if (!p || !qp)
1429 goto error;
1431 if (p->output_format == ISL_FORMAT_ISL)
1432 return print_qpolynomial_isl(p, qp);
1433 else if (p->output_format == ISL_FORMAT_C)
1434 return print_qpolynomial_c(p, qp->dim, qp);
1435 else
1436 isl_die(qp->dim->ctx, isl_error_unsupported,
1437 "output format not supported for isl_qpolynomials",
1438 goto error);
1439 error:
1440 isl_printer_free(p);
1441 return NULL;
1444 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1445 unsigned output_format)
1447 isl_printer *p;
1449 if (!qp)
1450 return;
1452 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1453 p = isl_printer_to_file(qp->dim->ctx, out);
1454 p = isl_printer_print_qpolynomial(p, qp);
1455 isl_printer_free(p);
1458 static __isl_give isl_printer *qpolynomial_fold_print(
1459 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1461 int i;
1463 if (fold->type == isl_fold_min)
1464 p = isl_printer_print_str(p, "min");
1465 else if (fold->type == isl_fold_max)
1466 p = isl_printer_print_str(p, "max");
1467 p = isl_printer_print_str(p, "(");
1468 for (i = 0; i < fold->n; ++i) {
1469 if (i)
1470 p = isl_printer_print_str(p, ", ");
1471 p = print_qpolynomial(p, fold->qp[i]);
1473 p = isl_printer_print_str(p, ")");
1474 return p;
1477 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1478 FILE *out, unsigned output_format)
1480 isl_printer *p;
1482 if (!fold)
1483 return;
1485 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1487 p = isl_printer_to_file(fold->dim->ctx, out);
1488 p = isl_printer_print_qpolynomial_fold(p, fold);
1490 isl_printer_free(p);
1493 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1494 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1496 struct isl_print_space_data data = { 0 };
1497 int i = 0;
1499 for (i = 0; i < pwqp->n; ++i) {
1500 if (i)
1501 p = isl_printer_print_str(p, "; ");
1502 if (!isl_space_is_params(pwqp->p[i].set->dim)) {
1503 p = print_space(pwqp->p[i].set->dim, p, 0, &data);
1504 p = isl_printer_print_str(p, " -> ");
1506 p = print_qpolynomial(p, pwqp->p[i].qp);
1507 p = print_disjuncts((isl_map *)pwqp->p[i].set, p, 0);
1510 return p;
1513 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1514 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1516 struct isl_print_space_data data = { 0 };
1518 if (!p || !pwqp)
1519 goto error;
1521 if (isl_space_dim(pwqp->dim, isl_dim_param) > 0) {
1522 p = print_tuple(pwqp->dim, p, isl_dim_param, &data);
1523 p = isl_printer_print_str(p, " -> ");
1525 p = isl_printer_print_str(p, "{ ");
1526 if (pwqp->n == 0) {
1527 if (!isl_space_is_set(pwqp->dim)) {
1528 p = print_tuple(pwqp->dim, p, isl_dim_in, &data);
1529 p = isl_printer_print_str(p, " -> ");
1531 p = isl_printer_print_str(p, "0");
1533 p = isl_pwqp_print_isl_body(p, pwqp);
1534 p = isl_printer_print_str(p, " }");
1535 return p;
1536 error:
1537 isl_printer_free(p);
1538 return NULL;
1541 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
1542 unsigned output_format)
1544 isl_printer *p;
1546 if (!pwqp)
1547 return;
1549 p = isl_printer_to_file(pwqp->dim->ctx, out);
1550 p = isl_printer_set_output_format(p, output_format);
1551 p = isl_printer_print_pw_qpolynomial(p, pwqp);
1553 isl_printer_free(p);
1556 static __isl_give isl_printer *isl_pwf_print_isl_body(
1557 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1559 struct isl_print_space_data data = { 0 };
1560 int i = 0;
1562 for (i = 0; i < pwf->n; ++i) {
1563 if (i)
1564 p = isl_printer_print_str(p, "; ");
1565 if (!isl_space_is_params(pwf->p[i].set->dim)) {
1566 p = print_space(pwf->p[i].set->dim, p, 0, &data);
1567 p = isl_printer_print_str(p, " -> ");
1569 p = qpolynomial_fold_print(pwf->p[i].fold, p);
1570 p = print_disjuncts((isl_map *)pwf->p[i].set, p, 0);
1573 return p;
1576 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
1577 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1579 struct isl_print_space_data data = { 0 };
1581 if (isl_space_dim(pwf->dim, isl_dim_param) > 0) {
1582 p = print_tuple(pwf->dim, p, isl_dim_param, &data);
1583 p = isl_printer_print_str(p, " -> ");
1585 p = isl_printer_print_str(p, "{ ");
1586 if (pwf->n == 0) {
1587 if (!isl_space_is_set(pwf->dim)) {
1588 p = print_tuple(pwf->dim, p, isl_dim_in, &data);
1589 p = isl_printer_print_str(p, " -> ");
1591 p = isl_printer_print_str(p, "0");
1593 p = isl_pwf_print_isl_body(p, pwf);
1594 p = isl_printer_print_str(p, " }");
1595 return p;
1598 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1599 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c);
1601 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1602 __isl_keep isl_space *dim,
1603 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1605 if (type == isl_dim_div) {
1606 p = isl_printer_print_str(p, "floord(");
1607 p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
1608 p = isl_printer_print_str(p, ", ");
1609 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1610 p = isl_printer_print_str(p, ")");
1611 } else {
1612 const char *name;
1614 name = isl_space_get_dim_name(dim, type, pos);
1615 if (!name)
1616 name = "UNNAMED";
1617 p = isl_printer_print_str(p, name);
1619 return p;
1622 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1623 __isl_keep isl_space *dim,
1624 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1626 enum isl_dim_type type;
1628 if (pos == 0)
1629 return isl_printer_print_isl_int(p, c);
1631 if (isl_int_is_one(c))
1633 else if (isl_int_is_negone(c))
1634 p = isl_printer_print_str(p, "-");
1635 else {
1636 p = isl_printer_print_isl_int(p, c);
1637 p = isl_printer_print_str(p, "*");
1639 type = pos2type(dim, &pos);
1640 p = print_name_c(p, dim, bset, type, pos);
1641 return p;
1644 static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
1645 __isl_keep isl_space *dim,
1646 __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
1648 int i;
1649 int first;
1651 for (i = 0, first = 1; i < len; ++i) {
1652 int flip = 0;
1653 if (isl_int_is_zero(c[i]))
1654 continue;
1655 if (!first) {
1656 if (isl_int_is_neg(c[i])) {
1657 flip = 1;
1658 isl_int_neg(c[i], c[i]);
1659 p = isl_printer_print_str(p, " - ");
1660 } else
1661 p = isl_printer_print_str(p, " + ");
1663 first = 0;
1664 p = print_term_c(p, dim, bset, c[i], i);
1665 if (flip)
1666 isl_int_neg(c[i], c[i]);
1668 if (first)
1669 p = isl_printer_print_str(p, "0");
1670 return p;
1673 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1674 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c)
1676 unsigned len = 1 + isl_basic_set_total_dim(bset);
1677 return print_partial_affine_c(p, dim, bset, c, len);
1680 /* We skip the constraint if it is implied by the div expression.
1682 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1683 __isl_keep isl_space *dim,
1684 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int first)
1686 unsigned o_div;
1687 unsigned n_div;
1688 int div;
1690 o_div = isl_basic_set_offset(bset, isl_dim_div);
1691 n_div = isl_basic_set_dim(bset, isl_dim_div);
1692 div = isl_seq_last_non_zero(c + o_div, n_div);
1693 if (div >= 0 && isl_basic_set_is_div_constraint(bset, c, div))
1694 return p;
1696 if (!first)
1697 p = isl_printer_print_str(p, " && ");
1699 p = print_affine_c(p, dim, bset, c);
1700 p = isl_printer_print_str(p, " ");
1701 p = isl_printer_print_str(p, op);
1702 p = isl_printer_print_str(p, " 0");
1703 return p;
1706 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1707 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset)
1709 int i, j;
1710 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
1711 unsigned total = isl_basic_set_total_dim(bset) - n_div;
1713 for (i = 0; i < bset->n_eq; ++i) {
1714 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
1715 if (j < 0)
1716 p = print_constraint_c(p, dim, bset,
1717 bset->eq[i], "==", !i);
1718 else {
1719 if (i)
1720 p = isl_printer_print_str(p, " && ");
1721 p = isl_printer_print_str(p, "(");
1722 p = print_partial_affine_c(p, dim, bset, bset->eq[i],
1723 1 + total + j);
1724 p = isl_printer_print_str(p, ") % ");
1725 p = isl_printer_print_isl_int(p,
1726 bset->eq[i][1 + total + j]);
1727 p = isl_printer_print_str(p, " == 0");
1730 for (i = 0; i < bset->n_ineq; ++i)
1731 p = print_constraint_c(p, dim, bset, bset->ineq[i], ">=",
1732 !bset->n_eq && !i);
1733 return p;
1736 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
1737 __isl_keep isl_space *dim, __isl_keep isl_set *set)
1739 int i;
1741 if (!set)
1742 return isl_printer_free(p);
1744 if (set->n == 0)
1745 p = isl_printer_print_str(p, "0");
1747 for (i = 0; i < set->n; ++i) {
1748 if (i)
1749 p = isl_printer_print_str(p, " || ");
1750 if (set->n > 1)
1751 p = isl_printer_print_str(p, "(");
1752 p = print_basic_set_c(p, dim, set->p[i]);
1753 if (set->n > 1)
1754 p = isl_printer_print_str(p, ")");
1756 return p;
1759 static __isl_give isl_printer *print_pw_qpolynomial_c(
1760 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1762 int i;
1764 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set))
1765 return print_qpolynomial_c(p, pwqp->dim, pwqp->p[0].qp);
1767 for (i = 0; i < pwqp->n; ++i) {
1768 p = isl_printer_print_str(p, "(");
1769 p = print_set_c(p, pwqp->dim, pwqp->p[i].set);
1770 p = isl_printer_print_str(p, ") ? (");
1771 p = print_qpolynomial_c(p, pwqp->dim, pwqp->p[i].qp);
1772 p = isl_printer_print_str(p, ") : ");
1775 p = isl_printer_print_str(p, "0");
1776 return p;
1779 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
1780 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1782 if (!p || !pwqp)
1783 goto error;
1785 if (p->output_format == ISL_FORMAT_ISL)
1786 return print_pw_qpolynomial_isl(p, pwqp);
1787 else if (p->output_format == ISL_FORMAT_C)
1788 return print_pw_qpolynomial_c(p, pwqp);
1789 isl_assert(p->ctx, 0, goto error);
1790 error:
1791 isl_printer_free(p);
1792 return NULL;
1795 static int print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1797 struct isl_union_print_data *data;
1798 data = (struct isl_union_print_data *)user;
1800 if (!data->first)
1801 data->p = isl_printer_print_str(data->p, "; ");
1802 data->first = 0;
1804 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
1805 isl_pw_qpolynomial_free(pwqp);
1807 return 0;
1810 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
1811 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1813 struct isl_union_print_data data = { p, 1 };
1814 struct isl_print_space_data space_data = { 0 };
1815 isl_space *dim;
1817 dim = isl_union_pw_qpolynomial_get_space(upwqp);
1818 if (isl_space_dim(dim, isl_dim_param) > 0) {
1819 p = print_tuple(dim, p, isl_dim_param, &space_data);
1820 p = isl_printer_print_str(p, " -> ");
1822 isl_space_free(dim);
1823 p = isl_printer_print_str(p, "{ ");
1824 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
1825 &data);
1826 p = data.p;
1827 p = isl_printer_print_str(p, " }");
1828 return p;
1831 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
1832 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1834 if (!p || !upwqp)
1835 goto error;
1837 if (p->output_format == ISL_FORMAT_ISL)
1838 return print_union_pw_qpolynomial_isl(p, upwqp);
1839 isl_die(p->ctx, isl_error_invalid,
1840 "invalid output format for isl_union_pw_qpolynomial",
1841 goto error);
1842 error:
1843 isl_printer_free(p);
1844 return NULL;
1847 static __isl_give isl_printer *print_qpolynomial_fold_c(
1848 __isl_take isl_printer *p, __isl_keep isl_space *dim,
1849 __isl_keep isl_qpolynomial_fold *fold)
1851 int i;
1853 for (i = 0; i < fold->n - 1; ++i)
1854 if (fold->type == isl_fold_min)
1855 p = isl_printer_print_str(p, "min(");
1856 else if (fold->type == isl_fold_max)
1857 p = isl_printer_print_str(p, "max(");
1859 for (i = 0; i < fold->n; ++i) {
1860 if (i)
1861 p = isl_printer_print_str(p, ", ");
1862 p = print_qpolynomial_c(p, dim, fold->qp[i]);
1863 if (i)
1864 p = isl_printer_print_str(p, ")");
1866 return p;
1869 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
1870 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
1872 if (!p || !fold)
1873 goto error;
1874 if (p->output_format == ISL_FORMAT_ISL)
1875 return qpolynomial_fold_print(fold, p);
1876 else if (p->output_format == ISL_FORMAT_C)
1877 return print_qpolynomial_fold_c(p, fold->dim, fold);
1878 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
1879 goto error);
1880 error:
1881 isl_printer_free(p);
1882 return NULL;
1885 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
1886 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1888 int i;
1890 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set))
1891 return print_qpolynomial_fold_c(p, pwf->dim, pwf->p[0].fold);
1893 for (i = 0; i < pwf->n; ++i) {
1894 p = isl_printer_print_str(p, "(");
1895 p = print_set_c(p, pwf->dim, pwf->p[i].set);
1896 p = isl_printer_print_str(p, ") ? (");
1897 p = print_qpolynomial_fold_c(p, pwf->dim, pwf->p[i].fold);
1898 p = isl_printer_print_str(p, ") : ");
1901 p = isl_printer_print_str(p, "0");
1902 return p;
1905 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
1906 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1908 if (!p || !pwf)
1909 goto error;
1911 if (p->output_format == ISL_FORMAT_ISL)
1912 return print_pw_qpolynomial_fold_isl(p, pwf);
1913 else if (p->output_format == ISL_FORMAT_C)
1914 return print_pw_qpolynomial_fold_c(p, pwf);
1915 isl_assert(p->ctx, 0, goto error);
1916 error:
1917 isl_printer_free(p);
1918 return NULL;
1921 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
1922 FILE *out, unsigned output_format)
1924 isl_printer *p;
1926 if (!pwf)
1927 return;
1929 p = isl_printer_to_file(pwf->dim->ctx, out);
1930 p = isl_printer_set_output_format(p, output_format);
1931 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
1933 isl_printer_free(p);
1936 static int print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf, void *user)
1938 struct isl_union_print_data *data;
1939 data = (struct isl_union_print_data *)user;
1941 if (!data->first)
1942 data->p = isl_printer_print_str(data->p, "; ");
1943 data->first = 0;
1945 data->p = isl_pwf_print_isl_body(data->p, pwf);
1946 isl_pw_qpolynomial_fold_free(pwf);
1948 return 0;
1951 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
1952 __isl_take isl_printer *p,
1953 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1955 struct isl_union_print_data data = { p, 1 };
1956 struct isl_print_space_data space_data = { 0 };
1957 isl_space *dim;
1959 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
1960 if (isl_space_dim(dim, isl_dim_param) > 0) {
1961 p = print_tuple(dim, p, isl_dim_param, &space_data);
1962 p = isl_printer_print_str(p, " -> ");
1964 isl_space_free(dim);
1965 p = isl_printer_print_str(p, "{ ");
1966 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
1967 &print_pwf_body, &data);
1968 p = data.p;
1969 p = isl_printer_print_str(p, " }");
1970 return p;
1973 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
1974 __isl_take isl_printer *p,
1975 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1977 if (!p || !upwf)
1978 goto error;
1980 if (p->output_format == ISL_FORMAT_ISL)
1981 return print_union_pw_qpolynomial_fold_isl(p, upwf);
1982 isl_die(p->ctx, isl_error_invalid,
1983 "invalid output format for isl_union_pw_qpolynomial_fold",
1984 goto error);
1985 error:
1986 isl_printer_free(p);
1987 return NULL;
1990 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
1991 __isl_keep isl_constraint *c)
1993 isl_basic_map *bmap;
1995 if (!p || !c)
1996 goto error;
1998 bmap = isl_basic_map_from_constraint(isl_constraint_copy(c));
1999 p = isl_printer_print_basic_map(p, bmap);
2000 isl_basic_map_free(bmap);
2001 return p;
2002 error:
2003 isl_printer_free(p);
2004 return NULL;
2007 static __isl_give isl_printer *isl_printer_print_space_isl(
2008 __isl_take isl_printer *p, __isl_keep isl_space *dim)
2010 struct isl_print_space_data data = { 0 };
2012 if (!dim)
2013 goto error;
2015 if (isl_space_dim(dim, isl_dim_param) > 0) {
2016 p = print_tuple(dim, p, isl_dim_param, &data);
2017 p = isl_printer_print_str(p, " -> ");
2020 p = isl_printer_print_str(p, "{ ");
2021 if (isl_space_is_params(dim))
2022 p = isl_printer_print_str(p, s_such_that[0]);
2023 else
2024 p = print_space(dim, p, 0, &data);
2025 p = isl_printer_print_str(p, " }");
2027 return p;
2028 error:
2029 isl_printer_free(p);
2030 return NULL;
2033 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
2034 __isl_keep isl_space *space)
2036 if (!p || !space)
2037 return isl_printer_free(p);
2038 if (p->output_format == ISL_FORMAT_ISL)
2039 return isl_printer_print_space_isl(p, space);
2040 else if (p->output_format == ISL_FORMAT_OMEGA)
2041 return print_omega_parameters(space, p);
2043 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
2044 "output format not supported for space",
2045 return isl_printer_free(p));
2048 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
2049 __isl_keep isl_local_space *ls)
2051 struct isl_print_space_data data = { 0 };
2052 unsigned total;
2053 unsigned n_div;
2055 if (!ls)
2056 goto error;
2058 total = isl_local_space_dim(ls, isl_dim_all);
2059 if (isl_local_space_dim(ls, isl_dim_param) > 0) {
2060 p = print_tuple(ls->dim, p, isl_dim_param, &data);
2061 p = isl_printer_print_str(p, " -> ");
2063 p = isl_printer_print_str(p, "{ ");
2064 p = print_space(ls->dim, p, 0, &data);
2065 n_div = isl_local_space_dim(ls, isl_dim_div);
2066 if (n_div > 0) {
2067 p = isl_printer_print_str(p, " : ");
2068 p = isl_printer_print_str(p, s_open_exists[0]);
2069 p = print_div_list(p, ls->dim, ls->div, 0);
2070 } else if (isl_space_is_params(ls->dim))
2071 p = isl_printer_print_str(p, s_such_that[0]);
2072 p = isl_printer_print_str(p, " }");
2073 return p;
2074 error:
2075 isl_printer_free(p);
2076 return NULL;
2079 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2080 __isl_keep isl_aff *aff)
2082 unsigned total;
2084 total = isl_local_space_dim(aff->ls, isl_dim_all);
2085 p = isl_printer_print_str(p, "(");
2086 p = print_affine_of_len(aff->ls->dim, aff->ls->div, p,
2087 aff->v->el + 1, 1 + total);
2088 if (isl_int_is_one(aff->v->el[0]))
2089 p = isl_printer_print_str(p, ")");
2090 else {
2091 p = isl_printer_print_str(p, ")/");
2092 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2095 return p;
2098 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2099 __isl_keep isl_aff *aff)
2101 struct isl_print_space_data data = { 0 };
2103 if (isl_space_is_params(aff->ls->dim))
2105 else {
2106 p = print_tuple(aff->ls->dim, p, isl_dim_set, &data);
2107 p = isl_printer_print_str(p, " -> ");
2109 p = isl_printer_print_str(p, "[");
2110 p = print_aff_body(p, aff);
2111 p = isl_printer_print_str(p, "]");
2113 return p;
2116 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2117 __isl_keep isl_aff *aff)
2119 struct isl_print_space_data data = { 0 };
2121 if (!aff)
2122 goto error;
2124 if (isl_local_space_dim(aff->ls, isl_dim_param) > 0) {
2125 p = print_tuple(aff->ls->dim, p, isl_dim_param, &data);
2126 p = isl_printer_print_str(p, " -> ");
2128 p = isl_printer_print_str(p, "{ ");
2129 p = print_aff(p, aff);
2130 p = isl_printer_print_str(p, " }");
2131 return p;
2132 error:
2133 isl_printer_free(p);
2134 return NULL;
2137 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2138 __isl_keep isl_pw_aff *pwaff)
2140 struct isl_print_space_data data = { 0 };
2141 int i;
2143 if (!pwaff)
2144 goto error;
2146 if (isl_space_dim(pwaff->dim, isl_dim_param) > 0) {
2147 p = print_tuple(pwaff->dim, p, isl_dim_param, &data);
2148 p = isl_printer_print_str(p, " -> ");
2150 p = isl_printer_print_str(p, "{ ");
2151 for (i = 0; i < pwaff->n; ++i) {
2152 if (i)
2153 p = isl_printer_print_str(p, "; ");
2154 p = print_aff(p, pwaff->p[i].aff);
2155 p = print_disjuncts((isl_map *)pwaff->p[i].set, p, 0);
2157 p = isl_printer_print_str(p, " }");
2158 return p;
2159 error:
2160 isl_printer_free(p);
2161 return NULL;
2164 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2165 __isl_keep isl_local_space *ls, isl_int *c);
2167 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2168 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2170 if (type == isl_dim_div) {
2171 p = isl_printer_print_str(p, "floord(");
2172 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2173 p = isl_printer_print_str(p, ", ");
2174 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2175 p = isl_printer_print_str(p, ")");
2176 } else {
2177 const char *name;
2179 name = isl_space_get_dim_name(ls->dim, type, pos);
2180 if (!name)
2181 name = "UNNAMED";
2182 p = isl_printer_print_str(p, name);
2184 return p;
2187 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2188 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2190 enum isl_dim_type type;
2192 if (pos == 0)
2193 return isl_printer_print_isl_int(p, c);
2195 if (isl_int_is_one(c))
2197 else if (isl_int_is_negone(c))
2198 p = isl_printer_print_str(p, "-");
2199 else {
2200 p = isl_printer_print_isl_int(p, c);
2201 p = isl_printer_print_str(p, "*");
2203 type = pos2type(ls->dim, &pos);
2204 p = print_ls_name_c(p, ls, type, pos);
2205 return p;
2208 static __isl_give isl_printer *print_ls_partial_affine_c(
2209 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2210 isl_int *c, unsigned len)
2212 int i;
2213 int first;
2215 for (i = 0, first = 1; i < len; ++i) {
2216 int flip = 0;
2217 if (isl_int_is_zero(c[i]))
2218 continue;
2219 if (!first) {
2220 if (isl_int_is_neg(c[i])) {
2221 flip = 1;
2222 isl_int_neg(c[i], c[i]);
2223 p = isl_printer_print_str(p, " - ");
2224 } else
2225 p = isl_printer_print_str(p, " + ");
2227 first = 0;
2228 p = print_ls_term_c(p, ls, c[i], i);
2229 if (flip)
2230 isl_int_neg(c[i], c[i]);
2232 if (first)
2233 p = isl_printer_print_str(p, "0");
2234 return p;
2237 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2238 __isl_keep isl_local_space *ls, isl_int *c)
2240 unsigned len = 1 + isl_local_space_dim(ls, isl_dim_all);
2241 return print_ls_partial_affine_c(p, ls, c, len);
2244 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2245 __isl_keep isl_aff *aff)
2247 unsigned total;
2249 total = isl_local_space_dim(aff->ls, isl_dim_all);
2250 if (!isl_int_is_one(aff->v->el[0]))
2251 p = isl_printer_print_str(p, "(");
2252 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2253 if (!isl_int_is_one(aff->v->el[0])) {
2254 p = isl_printer_print_str(p, ")/");
2255 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2257 return p;
2260 /* Print the affine expression "aff" in C format to "p".
2261 * The dimension names are taken from "space"
2262 * "set" represents the domain of the affine expression.
2264 static __isl_give isl_printer *print_aff_on_domain_c(__isl_take isl_printer *p,
2265 __isl_keep isl_space *space, __isl_keep isl_aff *aff,
2266 __isl_keep isl_set *set)
2268 isl_set *u;
2269 isl_ast_build *build;
2270 isl_ast_expr *expr;
2272 u = isl_set_universe(isl_space_copy(space));
2273 build = isl_ast_build_from_context(u);
2274 build = isl_ast_build_restrict(build, isl_set_copy(set));
2275 expr = isl_ast_expr_from_aff(isl_aff_copy(aff), build);
2276 p = isl_printer_print_ast_expr(p, expr);
2277 isl_ast_expr_free(expr);
2278 isl_ast_build_free(build);
2280 return p;
2283 /* In the C format, we cannot express that "pwaff" may be undefined
2284 * on parts of the domain space. We therefore assume that the expression
2285 * will only be evaluated on its definition domain and compute the gist
2286 * of each cell with respect to this domain.
2288 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2289 __isl_keep isl_pw_aff *pwaff)
2291 int i;
2292 isl_set *domain;
2293 isl_space *space;
2295 if (pwaff->n < 1)
2296 isl_die(p->ctx, isl_error_unsupported,
2297 "cannot print empty isl_pw_aff in C format", goto error);
2298 space = isl_pw_aff_get_domain_space(pwaff);
2299 if (!space)
2300 goto error;
2302 domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
2304 for (i = 0; i < pwaff->n - 1; ++i) {
2305 isl_set *set_i;
2307 p = isl_printer_print_str(p, "(");
2309 set_i = isl_set_copy(pwaff->p[i].set);
2310 set_i = isl_set_gist(set_i, isl_set_copy(domain));
2311 p = print_set_c(p, space, set_i);
2312 isl_set_free(set_i);
2314 p = isl_printer_print_str(p, ") ? (");
2315 p = print_aff_on_domain_c(p, space,
2316 pwaff->p[i].aff, pwaff->p[i].set);
2317 p = isl_printer_print_str(p, ") : ");
2320 isl_set_free(domain);
2322 p = print_aff_on_domain_c(p, space, pwaff->p[pwaff->n - 1].aff,
2323 pwaff->p[pwaff->n - 1].set);
2324 isl_space_free(space);
2325 return p;
2326 error:
2327 isl_printer_free(p);
2328 return NULL;
2331 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2332 __isl_keep isl_aff *aff)
2334 if (!p || !aff)
2335 goto error;
2337 if (p->output_format == ISL_FORMAT_ISL)
2338 return print_aff_isl(p, aff);
2339 else if (p->output_format == ISL_FORMAT_C)
2340 return print_aff_c(p, aff);
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 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2349 __isl_keep isl_pw_aff *pwaff)
2351 if (!p || !pwaff)
2352 goto error;
2354 if (p->output_format == ISL_FORMAT_ISL)
2355 return print_pw_aff_isl(p, pwaff);
2356 else if (p->output_format == ISL_FORMAT_C)
2357 return print_pw_aff_c(p, pwaff);
2358 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2359 goto error);
2360 error:
2361 isl_printer_free(p);
2362 return NULL;
2365 /* Print dimension "pos" of data->space to "p".
2367 * data->user is assumed to be an isl_multi_aff.
2369 * If the current dimension is an output dimension, then print
2370 * the corresponding expression. Otherwise, print the name of the dimension.
2372 static __isl_give isl_printer *print_dim_ma(__isl_take isl_printer *p,
2373 struct isl_print_space_data *data, unsigned pos)
2375 isl_multi_aff *ma = data->user;
2377 if (data->type == isl_dim_out)
2378 p = print_aff_body(p, ma->p[pos]);
2379 else
2380 p = print_name(data->space, p, data->type, pos, data->latex);
2382 return p;
2385 static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
2386 __isl_keep isl_multi_aff *maff)
2388 struct isl_print_space_data data = { 0 };
2390 data.print_dim = &print_dim_ma;
2391 data.user = maff;
2392 return print_space(maff->space, p, 0, &data);
2395 static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
2396 __isl_keep isl_multi_aff *maff)
2398 struct isl_print_space_data data = { 0 };
2400 if (!maff)
2401 goto error;
2403 if (isl_space_dim(maff->space, isl_dim_param) > 0) {
2404 p = print_tuple(maff->space, p, isl_dim_param, &data);
2405 p = isl_printer_print_str(p, " -> ");
2407 p = isl_printer_print_str(p, "{ ");
2408 p = print_multi_aff(p, maff);
2409 p = isl_printer_print_str(p, " }");
2410 return p;
2411 error:
2412 isl_printer_free(p);
2413 return NULL;
2416 __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
2417 __isl_keep isl_multi_aff *maff)
2419 if (!p || !maff)
2420 goto error;
2422 if (p->output_format == ISL_FORMAT_ISL)
2423 return print_multi_aff_isl(p, maff);
2424 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2425 goto error);
2426 error:
2427 isl_printer_free(p);
2428 return NULL;
2431 static __isl_give isl_printer *print_pw_multi_aff_body(
2432 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2434 int i;
2436 if (!pma)
2437 goto error;
2439 for (i = 0; i < pma->n; ++i) {
2440 if (i)
2441 p = isl_printer_print_str(p, "; ");
2442 p = print_multi_aff(p, pma->p[i].maff);
2443 p = print_disjuncts((isl_map *)pma->p[i].set, p, 0);
2445 return p;
2446 error:
2447 isl_printer_free(p);
2448 return NULL;
2451 static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
2452 __isl_keep isl_pw_multi_aff *pma)
2454 struct isl_print_space_data data = { 0 };
2456 if (!pma)
2457 goto error;
2459 if (isl_space_dim(pma->dim, isl_dim_param) > 0) {
2460 p = print_tuple(pma->dim, p, isl_dim_param, &data);
2461 p = isl_printer_print_str(p, " -> ");
2463 p = isl_printer_print_str(p, "{ ");
2464 p = print_pw_multi_aff_body(p, pma);
2465 p = isl_printer_print_str(p, " }");
2466 return p;
2467 error:
2468 isl_printer_free(p);
2469 return NULL;
2472 static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
2473 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2475 int i;
2477 for (i = 0; i < pma->n - 1; ++i) {
2478 p = isl_printer_print_str(p, "(");
2479 p = print_set_c(p, pma->dim, pma->p[i].set);
2480 p = isl_printer_print_str(p, ") ? (");
2481 p = print_aff_c(p, pma->p[i].maff->p[0]);
2482 p = isl_printer_print_str(p, ") : ");
2485 return print_aff_c(p, pma->p[pma->n - 1].maff->p[0]);
2488 static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
2489 __isl_keep isl_pw_multi_aff *pma)
2491 int n;
2492 const char *name;
2494 if (!pma)
2495 goto error;
2496 if (pma->n < 1)
2497 isl_die(p->ctx, isl_error_unsupported,
2498 "cannot print empty isl_pw_multi_aff in C format",
2499 goto error);
2500 name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
2501 if (!name && isl_pw_multi_aff_dim(pma, isl_dim_out) == 1)
2502 return print_unnamed_pw_multi_aff_c(p, pma);
2503 if (!name)
2504 isl_die(p->ctx, isl_error_unsupported,
2505 "cannot print unnamed isl_pw_multi_aff in C format",
2506 goto error);
2508 p = isl_printer_print_str(p, name);
2509 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
2510 if (n != 0)
2511 isl_die(p->ctx, isl_error_unsupported,
2512 "not supported yet", goto error);
2514 return p;
2515 error:
2516 isl_printer_free(p);
2517 return NULL;
2520 __isl_give isl_printer *isl_printer_print_pw_multi_aff(
2521 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2523 if (!p || !pma)
2524 goto error;
2526 if (p->output_format == ISL_FORMAT_ISL)
2527 return print_pw_multi_aff_isl(p, pma);
2528 if (p->output_format == ISL_FORMAT_C)
2529 return print_pw_multi_aff_c(p, pma);
2530 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2531 goto error);
2532 error:
2533 isl_printer_free(p);
2534 return NULL;
2537 static int print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
2538 void *user)
2540 struct isl_union_print_data *data;
2541 data = (struct isl_union_print_data *) user;
2543 if (!data->first)
2544 data->p = isl_printer_print_str(data->p, "; ");
2545 data->first = 0;
2547 data->p = print_pw_multi_aff_body(data->p, pma);
2548 isl_pw_multi_aff_free(pma);
2550 return 0;
2553 static __isl_give isl_printer *print_union_pw_multi_aff_isl(
2554 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2556 struct isl_union_print_data data = { p, 1 };
2557 struct isl_print_space_data space_data = { 0 };
2558 isl_space *space;
2560 space = isl_union_pw_multi_aff_get_space(upma);
2561 if (isl_space_dim(space, isl_dim_param) > 0) {
2562 p = print_tuple(space, p, isl_dim_param, &space_data);
2563 p = isl_printer_print_str(p, s_to[0]);
2565 isl_space_free(space);
2566 p = isl_printer_print_str(p, s_open_set[0]);
2567 isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
2568 &print_pw_multi_aff_body_wrap, &data);
2569 p = data.p;
2570 p = isl_printer_print_str(p, s_close_set[0]);
2571 return p;
2574 __isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
2575 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2577 if (!p || !upma)
2578 goto error;
2580 if (p->output_format == ISL_FORMAT_ISL)
2581 return print_union_pw_multi_aff_isl(p, upma);
2582 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2583 goto error);
2584 error:
2585 isl_printer_free(p);
2586 return NULL;
2589 /* Print dimension "pos" of data->space to "p".
2591 * data->user is assumed to be an isl_multi_pw_aff.
2593 * If the current dimension is an output dimension, then print
2594 * the corresponding piecewise affine expression.
2595 * Otherwise, print the name of the dimension.
2597 static __isl_give isl_printer *print_dim_mpa(__isl_take isl_printer *p,
2598 struct isl_print_space_data *data, unsigned pos)
2600 int i;
2601 int need_parens;
2602 isl_multi_pw_aff *mpa = data->user;
2603 isl_pw_aff *pa;
2605 if (data->type != isl_dim_out)
2606 return print_name(data->space, p, data->type, pos, data->latex);
2608 pa = mpa->p[pos];
2609 if (pa->n == 0)
2610 return isl_printer_print_str(p, "(0 : 1 = 0)");
2612 need_parens = pa->n != 1 || !isl_set_plain_is_universe(pa->p[0].set);
2613 if (need_parens)
2614 p = isl_printer_print_str(p, "(");
2615 for (i = 0; i < pa->n; ++i) {
2616 if (i)
2617 p = isl_printer_print_str(p, "; ");
2618 p = print_aff_body(p, pa->p[i].aff);
2619 p = print_disjuncts(pa->p[i].set, p, 0);
2621 if (need_parens)
2622 p = isl_printer_print_str(p, ")");
2624 return p;
2627 /* Print "mpa" to "p" in isl format.
2629 static __isl_give isl_printer *print_multi_pw_aff_isl(__isl_take isl_printer *p,
2630 __isl_keep isl_multi_pw_aff *mpa)
2632 struct isl_print_space_data data = { 0 };
2634 if (!mpa)
2635 return isl_printer_free(p);
2637 if (isl_space_dim(mpa->space, isl_dim_param) > 0) {
2638 p = print_tuple(mpa->space, p, isl_dim_param, &data);
2639 p = isl_printer_print_str(p, " -> ");
2641 p = isl_printer_print_str(p, "{ ");
2642 data.print_dim = &print_dim_mpa;
2643 data.user = mpa;
2644 p = print_space(mpa->space, p, 0, &data);
2645 p = isl_printer_print_str(p, " }");
2646 return p;
2649 __isl_give isl_printer *isl_printer_print_multi_pw_aff(
2650 __isl_take isl_printer *p, __isl_keep isl_multi_pw_aff *mpa)
2652 if (!p || !mpa)
2653 return isl_printer_free(p);
2655 if (p->output_format == ISL_FORMAT_ISL)
2656 return print_multi_pw_aff_isl(p, mpa);
2657 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2658 return isl_printer_free(p));
2661 /* Print dimension "pos" of data->space to "p".
2663 * data->user is assumed to be an isl_multi_val.
2665 * If the current dimension is an output dimension, then print
2666 * the corresponding value. Otherwise, print the name of the dimension.
2668 static __isl_give isl_printer *print_dim_mv(__isl_take isl_printer *p,
2669 struct isl_print_space_data *data, unsigned pos)
2671 isl_multi_val *mv = data->user;
2673 if (data->type == isl_dim_out)
2674 return isl_printer_print_val(p, mv->p[pos]);
2675 else
2676 return print_name(data->space, p, data->type, pos, data->latex);
2679 /* Print the isl_multi_val "mv" to "p" in isl format.
2681 static __isl_give isl_printer *print_multi_val_isl(__isl_take isl_printer *p,
2682 __isl_keep isl_multi_val *mv)
2684 struct isl_print_space_data data = { 0 };
2686 if (!mv)
2687 return isl_printer_free(p);
2689 if (isl_space_dim(mv->space, isl_dim_param) > 0) {
2690 p = print_tuple(mv->space, p, isl_dim_param, &data);
2691 p = isl_printer_print_str(p, " -> ");
2693 p = isl_printer_print_str(p, "{ ");
2694 data.print_dim = &print_dim_mv;
2695 data.user = mv;
2696 p = print_space(mv->space, p, 0, &data);
2697 p = isl_printer_print_str(p, " }");
2698 return p;
2701 /* Print the isl_multi_val "mv" to "p".
2703 * Currently only supported in isl format.
2705 __isl_give isl_printer *isl_printer_print_multi_val(
2706 __isl_take isl_printer *p, __isl_keep isl_multi_val *mv)
2708 if (!p || !mv)
2709 return isl_printer_free(p);
2711 if (p->output_format == ISL_FORMAT_ISL)
2712 return print_multi_val_isl(p, mv);
2713 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2714 return isl_printer_free(p));