doc: add a note on isl_dim_type
[isl.git] / isl_output.c
blob5e75ee9f62a47035a9283dc86c16fa2720887c2a
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include <stdlib.h>
14 #include <string.h>
15 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
17 #include <isl/set.h>
18 #include <isl/seq.h>
19 #include <isl_polynomial_private.h>
20 #include <isl_printer_private.h>
21 #include <isl_space_private.h>
22 #include <isl_mat_private.h>
23 #include <isl/union_map.h>
24 #include <isl/constraint.h>
25 #include <isl_local_space_private.h>
26 #include <isl_aff_private.h>
28 static const char *s_to[2] = { " -> ", " \\to " };
29 static const char *s_and[2] = { " and ", " \\wedge " };
30 static const char *s_or[2] = { " or ", " \\vee " };
31 static const char *s_le[2] = { "<=", "\\le" };
32 static const char *s_ge[2] = { ">=", "\\ge" };
33 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
34 static const char *s_close_set[2] = { " }", " \\,\\}" };
35 static const char *s_open_list[2] = { "[", "(" };
36 static const char *s_close_list[2] = { "]", ")" };
37 static const char *s_such_that[2] = { " : ", " \\mid " };
38 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
39 static const char *s_close_exists[2] = { ")", "" };
40 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
41 static const char *s_param_prefix[2] = { "p", "p_" };
42 static const char *s_input_prefix[2] = { "i", "i_" };
43 static const char *s_output_prefix[2] = { "o", "o_" };
45 static __isl_give isl_printer *print_constraint_polylib(
46 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
48 int i;
49 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
50 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
51 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
52 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
54 p = isl_printer_start_line(p);
55 p = isl_printer_print_int(p, ineq);
56 for (i = 0; i < n_out; ++i) {
57 p = isl_printer_print_str(p, " ");
58 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
60 for (i = 0; i < n_in; ++i) {
61 p = isl_printer_print_str(p, " ");
62 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
64 for (i = 0; i < bmap->n_div; ++i) {
65 p = isl_printer_print_str(p, " ");
66 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
68 for (i = 0; i < nparam; ++i) {
69 p = isl_printer_print_str(p, " ");
70 p = isl_printer_print_isl_int(p, c[1+i]);
72 p = isl_printer_print_str(p, " ");
73 p = isl_printer_print_isl_int(p, c[0]);
74 p = isl_printer_end_line(p);
75 return p;
78 static __isl_give isl_printer *print_constraints_polylib(
79 struct isl_basic_map *bmap, __isl_take isl_printer *p)
81 int i;
83 p = isl_printer_set_isl_int_width(p, 5);
85 for (i = 0; i < bmap->n_eq; ++i)
86 p = print_constraint_polylib(bmap, 0, i, p);
87 for (i = 0; i < bmap->n_ineq; ++i)
88 p = print_constraint_polylib(bmap, 1, i, p);
90 return p;
93 static __isl_give isl_printer *bset_print_constraints_polylib(
94 struct isl_basic_set *bset, __isl_take isl_printer *p)
96 return print_constraints_polylib((struct isl_basic_map *)bset, p);
99 static __isl_give isl_printer *isl_basic_map_print_polylib(
100 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
102 unsigned total = isl_basic_map_total_dim(bmap);
103 p = isl_printer_start_line(p);
104 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
105 p = isl_printer_print_str(p, " ");
106 p = isl_printer_print_int(p, 1 + total + 1);
107 if (ext) {
108 p = isl_printer_print_str(p, " ");
109 p = isl_printer_print_int(p,
110 isl_basic_map_dim(bmap, isl_dim_out));
111 p = isl_printer_print_str(p, " ");
112 p = isl_printer_print_int(p,
113 isl_basic_map_dim(bmap, isl_dim_in));
114 p = isl_printer_print_str(p, " ");
115 p = isl_printer_print_int(p,
116 isl_basic_map_dim(bmap, isl_dim_div));
117 p = isl_printer_print_str(p, " ");
118 p = isl_printer_print_int(p,
119 isl_basic_map_dim(bmap, isl_dim_param));
121 p = isl_printer_end_line(p);
122 return print_constraints_polylib(bmap, p);
125 static __isl_give isl_printer *isl_basic_set_print_polylib(
126 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
128 return isl_basic_map_print_polylib((struct isl_basic_map *)bset, p, ext);
131 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
132 __isl_take isl_printer *p, int ext)
134 int i;
136 p = isl_printer_start_line(p);
137 p = isl_printer_print_int(p, map->n);
138 p = isl_printer_end_line(p);
139 for (i = 0; i < map->n; ++i) {
140 p = isl_printer_start_line(p);
141 p = isl_printer_end_line(p);
142 p = isl_basic_map_print_polylib(map->p[i], p, ext);
144 return p;
147 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
148 __isl_take isl_printer *p, int ext)
150 return isl_map_print_polylib((struct isl_map *)set, p, ext);
153 static int count_same_name(__isl_keep isl_space *dim,
154 enum isl_dim_type type, unsigned pos, const char *name)
156 enum isl_dim_type t;
157 unsigned p, s;
158 int count = 0;
160 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
161 s = t == type ? pos : isl_space_dim(dim, t);
162 for (p = 0; p < s; ++p) {
163 const char *n = isl_space_get_dim_name(dim, t, p);
164 if (n && !strcmp(n, name))
165 count++;
168 return count;
171 static __isl_give isl_printer *print_name(__isl_keep isl_space *dim,
172 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
173 int latex)
175 const char *name;
176 char buffer[20];
177 int primes;
179 name = type == isl_dim_div ? NULL : isl_space_get_dim_name(dim, type, pos);
181 if (!name) {
182 const char *prefix;
183 if (type == isl_dim_param)
184 prefix = s_param_prefix[latex];
185 else if (type == isl_dim_div)
186 prefix = s_div_prefix[latex];
187 else if (isl_space_is_set(dim) || type == isl_dim_in)
188 prefix = s_input_prefix[latex];
189 else
190 prefix = s_output_prefix[latex];
191 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
192 name = buffer;
194 primes = count_same_name(dim, name == buffer ? isl_dim_div : type,
195 pos, name);
196 p = isl_printer_print_str(p, name);
197 while (primes-- > 0)
198 p = isl_printer_print_str(p, "'");
199 return p;
202 static enum isl_dim_type pos2type(__isl_keep isl_space *dim, unsigned *pos)
204 enum isl_dim_type type;
205 unsigned n_in = isl_space_dim(dim, isl_dim_in);
206 unsigned n_out = isl_space_dim(dim, isl_dim_out);
207 unsigned nparam = isl_space_dim(dim, isl_dim_param);
209 if (*pos < 1 + nparam) {
210 type = isl_dim_param;
211 *pos -= 1;
212 } else if (*pos < 1 + nparam + n_in) {
213 type = isl_dim_in;
214 *pos -= 1 + nparam;
215 } else if (*pos < 1 + nparam + n_in + n_out) {
216 type = isl_dim_out;
217 *pos -= 1 + nparam + n_in;
218 } else {
219 type = isl_dim_div;
220 *pos -= 1 + nparam + n_in + n_out;
223 return type;
226 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
227 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
229 static __isl_give isl_printer *print_term(__isl_keep isl_space *dim,
230 __isl_keep isl_mat *div,
231 isl_int c, unsigned pos, __isl_take isl_printer *p, int latex)
233 enum isl_dim_type type;
234 int print_div_def;
236 if (pos == 0)
237 return isl_printer_print_isl_int(p, c);
239 type = pos2type(dim, &pos);
240 print_div_def = type == isl_dim_div && div &&
241 !isl_int_is_zero(div->row[pos][0]);
243 if (isl_int_is_one(c))
245 else if (isl_int_is_negone(c))
246 p = isl_printer_print_str(p, "-");
247 else {
248 p = isl_printer_print_isl_int(p, c);
249 if (p->output_format == ISL_FORMAT_C || print_div_def)
250 p = isl_printer_print_str(p, "*");
252 if (print_div_def)
253 p = print_div(dim, div, pos, p);
254 else
255 p = print_name(dim, p, type, pos, latex);
256 return p;
259 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_space *dim,
260 __isl_keep isl_mat *div,
261 __isl_take isl_printer *p, isl_int *c, int len)
263 int i;
264 int first;
266 for (i = 0, first = 1; i < len; ++i) {
267 int flip = 0;
268 if (isl_int_is_zero(c[i]))
269 continue;
270 if (!first) {
271 if (isl_int_is_neg(c[i])) {
272 flip = 1;
273 isl_int_neg(c[i], c[i]);
274 p = isl_printer_print_str(p, " - ");
275 } else
276 p = isl_printer_print_str(p, " + ");
278 first = 0;
279 p = print_term(dim, div, c[i], i, p, 0);
280 if (flip)
281 isl_int_neg(c[i], c[i]);
283 if (first)
284 p = isl_printer_print_str(p, "0");
285 return p;
288 static __isl_give isl_printer *print_affine(__isl_keep isl_basic_map *bmap,
289 __isl_keep isl_space *dim, __isl_take isl_printer *p, isl_int *c)
291 unsigned len = 1 + isl_basic_map_total_dim(bmap);
292 return print_affine_of_len(dim, NULL, p, c, len);
295 static int defining_equality(__isl_keep isl_basic_map *eq,
296 __isl_keep isl_space *dim, enum isl_dim_type type, int pos)
298 int i;
299 unsigned total;
301 if (!eq)
302 return -1;
304 pos += isl_space_offset(dim, type);
305 total = isl_basic_map_total_dim(eq);
307 for (i = 0; i < eq->n_eq; ++i) {
308 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
309 continue;
310 if (isl_int_is_one(eq->eq[i][1 + pos]))
311 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
312 return i;
315 return -1;
318 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
319 __isl_keep isl_aff *aff);
321 /* offset is the offset of local_dim inside global_type of global_dim.
323 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
324 __isl_keep isl_space *global_dim, enum isl_dim_type global_type,
325 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
326 int latex, __isl_keep isl_basic_map *eq,
327 __isl_keep isl_multi_aff *maff, int offset)
329 int i, j;
331 if (global_dim != local_dim && local_type == isl_dim_out)
332 offset += local_dim->n_in;
334 for (i = 0; i < isl_space_dim(local_dim, local_type); ++i) {
335 if (i)
336 p = isl_printer_print_str(p, ", ");
337 if (maff && global_type == isl_dim_out) {
338 p = print_aff_body(p, maff->p[offset + i]);
339 continue;
341 j = defining_equality(eq, global_dim, global_type, offset + i);
342 if (j >= 0) {
343 int pos = 1 + isl_space_offset(global_dim, global_type)
344 + offset + i;
345 p = print_affine_of_len(eq->dim, NULL,
346 p, eq->eq[j], pos);
347 } else {
348 p = print_name(global_dim, p, global_type, offset + i,
349 latex);
352 return p;
355 static __isl_give isl_printer *print_var_list(__isl_keep isl_space *dim,
356 __isl_take isl_printer *p, enum isl_dim_type type,
357 int latex, __isl_keep isl_basic_map *eq, __isl_keep isl_multi_aff *maff)
359 return print_nested_var_list(p, dim, type, dim, type, latex,
360 eq, maff, 0);
363 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
364 __isl_keep isl_space *global_dim, enum isl_dim_type global_type,
365 __isl_keep isl_space *local_dim,
366 int latex, __isl_keep isl_basic_map *eq,
367 __isl_keep isl_multi_aff *maff, int offset);
369 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
370 __isl_keep isl_space *global_dim, enum isl_dim_type global_type,
371 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
372 int latex, __isl_keep isl_basic_map *eq,
373 __isl_keep isl_multi_aff *maff, int offset)
375 const char *name = NULL;
376 unsigned n = isl_space_dim(local_dim, local_type);
377 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
378 name = isl_space_get_tuple_name(local_dim, local_type);
379 if (name) {
380 if (latex)
381 p = isl_printer_print_str(p, "\\mathrm{");
382 p = isl_printer_print_str(p, name);
383 if (latex)
384 p = isl_printer_print_str(p, "}");
387 if (!latex || n != 1 || name)
388 p = isl_printer_print_str(p, s_open_list[latex]);
389 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
390 local_dim->nested[local_type - isl_dim_in]) {
391 if (global_dim != local_dim && local_type == isl_dim_out)
392 offset += local_dim->n_in;
393 p = print_nested_map_dim(p, global_dim, global_type,
394 local_dim->nested[local_type - isl_dim_in],
395 latex, eq, maff, offset);
396 } else
397 p = print_nested_var_list(p, global_dim, global_type,
398 local_dim, local_type, latex,
399 eq, maff, offset);
400 if (!latex || n != 1 || name)
401 p = isl_printer_print_str(p, s_close_list[latex]);
402 return p;
405 static __isl_give isl_printer *print_tuple(__isl_keep isl_space *dim,
406 __isl_take isl_printer *p, enum isl_dim_type type,
407 int latex, __isl_keep isl_basic_map *eq, __isl_keep isl_multi_aff *maff)
409 return print_nested_tuple(p, dim, type, dim, type, latex, eq, maff, 0);
412 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
413 __isl_keep isl_space *global_dim, enum isl_dim_type global_type,
414 __isl_keep isl_space *local_dim,
415 int latex, __isl_keep isl_basic_map *eq,
416 __isl_keep isl_multi_aff *maff, int offset)
418 p = print_nested_tuple(p, global_dim, global_type,
419 local_dim, isl_dim_in, latex, eq, maff, offset);
420 p = isl_printer_print_str(p, s_to[latex]);
421 p = print_nested_tuple(p, global_dim, global_type,
422 local_dim, isl_dim_out, latex, eq, maff, offset);
424 return p;
427 static __isl_give isl_printer *print_space(__isl_keep isl_space *dim,
428 __isl_take isl_printer *p, int latex, int rational,
429 __isl_keep isl_basic_map *eq, __isl_keep isl_multi_aff *maff)
431 if (rational && !latex)
432 p = isl_printer_print_str(p, "rat: ");
433 if (isl_space_is_params(dim))
435 else if (isl_space_is_set(dim))
436 p = print_tuple(dim, p, isl_dim_set, latex, eq, maff);
437 else {
438 p = print_tuple(dim, p, isl_dim_in, latex, eq, maff);
439 p = isl_printer_print_str(p, s_to[latex]);
440 p = print_tuple(dim, p, isl_dim_out, latex, eq, maff);
443 return p;
446 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_space *dim,
447 __isl_take isl_printer *p)
449 if (isl_space_dim(dim, isl_dim_param) == 0)
450 return p;
452 p = isl_printer_start_line(p);
453 p = isl_printer_print_str(p, "symbolic ");
454 p = print_var_list(dim, p, isl_dim_param, 0, NULL, NULL);
455 p = isl_printer_print_str(p, ";");
456 p = isl_printer_end_line(p);
457 return p;
460 static __isl_give isl_printer *print_constraint(struct isl_basic_map *bmap,
461 __isl_keep isl_space *dim, __isl_take isl_printer *p,
462 isl_int *c, int last, const char *op, int first_constraint, int latex)
464 if (!first_constraint)
465 p = isl_printer_print_str(p, s_and[latex]);
467 isl_int_abs(c[last], c[last]);
469 p = print_term(dim, NULL, c[last], last, p, latex);
471 p = isl_printer_print_str(p, " ");
472 p = isl_printer_print_str(p, op);
473 p = isl_printer_print_str(p, " ");
475 isl_int_set_si(c[last], 0);
476 p = print_affine(bmap, dim, p, c);
478 return p;
481 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
482 __isl_keep isl_space *dim, __isl_take isl_printer *p, int latex)
484 int i;
485 struct isl_vec *c;
486 unsigned total = isl_basic_map_total_dim(bmap);
488 c = isl_vec_alloc(bmap->ctx, 1 + total);
489 if (!c)
490 goto error;
492 for (i = bmap->n_eq - 1; i >= 0; --i) {
493 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
494 if (l < 0) {
495 if (i != bmap->n_eq - 1)
496 p = isl_printer_print_str(p, s_and[latex]);
497 p = isl_printer_print_str(p, "0 = 0");
498 continue;
500 if (isl_int_is_neg(bmap->eq[i][l]))
501 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
502 else
503 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
504 p = print_constraint(bmap, dim, p, c->el, l,
505 "=", i == bmap->n_eq - 1, latex);
507 for (i = 0; i < bmap->n_ineq; ++i) {
508 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
509 int s;
510 const char *op;
511 if (l < 0)
512 continue;
513 s = isl_int_sgn(bmap->ineq[i][l]);
514 if (s < 0)
515 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
516 else
517 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
518 op = s < 0 ? s_le[latex] : s_ge[latex];
519 p = print_constraint(bmap, dim, p, c->el, l,
520 op, !bmap->n_eq && !i, latex);
523 isl_vec_free(c);
525 return p;
526 error:
527 isl_vec_free(c);
528 isl_printer_free(p);
529 return NULL;
532 static __isl_give isl_printer *print_omega_constraints(
533 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
535 if (bmap->n_eq + bmap->n_ineq == 0)
536 return p;
538 p = isl_printer_print_str(p, ": ");
539 if (bmap->n_div > 0) {
540 int i;
541 p = isl_printer_print_str(p, "exists (");
542 for (i = 0; i < bmap->n_div; ++i) {
543 if (i)
544 p = isl_printer_print_str(p, ", ");
545 p = print_name(bmap->dim, p, isl_dim_div, i, 0);
547 p = isl_printer_print_str(p, ": ");
549 p = print_constraints(bmap, bmap->dim, p, 0);
550 if (bmap->n_div > 0)
551 p = isl_printer_print_str(p, ")");
552 return p;
555 static __isl_give isl_printer *basic_map_print_omega(
556 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
558 p = isl_printer_print_str(p, "{ [");
559 p = print_var_list(bmap->dim, p, isl_dim_in, 0, NULL, NULL);
560 p = isl_printer_print_str(p, "] -> [");
561 p = print_var_list(bmap->dim, p, isl_dim_out, 0, NULL, NULL);
562 p = isl_printer_print_str(p, "] ");
563 p = print_omega_constraints(bmap, p);
564 p = isl_printer_print_str(p, " }");
565 return p;
568 static __isl_give isl_printer *isl_basic_map_print_omega(
569 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
571 p = print_omega_parameters(bmap->dim, p);
573 p = isl_printer_start_line(p);
574 p = basic_map_print_omega(bmap, p);
575 p = isl_printer_end_line(p);
576 return p;
579 static __isl_give isl_printer *basic_set_print_omega(
580 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
582 p = isl_printer_print_str(p, "{ [");
583 p = print_var_list(bset->dim, p, isl_dim_set, 0, NULL, NULL);
584 p = isl_printer_print_str(p, "] ");
585 p = print_omega_constraints((isl_basic_map *)bset, p);
586 p = isl_printer_print_str(p, " }");
587 return p;
590 static __isl_give isl_printer *isl_basic_set_print_omega(
591 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
593 p = print_omega_parameters(bset->dim, p);
595 p = isl_printer_start_line(p);
596 p = basic_set_print_omega(bset, p);
597 p = isl_printer_end_line(p);
598 return p;
601 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
602 __isl_take isl_printer *p)
604 int i;
606 p = print_omega_parameters(map->dim, p);
608 p = isl_printer_start_line(p);
609 for (i = 0; i < map->n; ++i) {
610 if (i)
611 p = isl_printer_print_str(p, " union ");
612 p = basic_map_print_omega(map->p[i], p);
614 p = isl_printer_end_line(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 p = print_omega_parameters(set->dim, p);
625 p = isl_printer_start_line(p);
626 for (i = 0; i < set->n; ++i) {
627 if (i)
628 p = isl_printer_print_str(p, " union ");
629 p = basic_set_print_omega(set->p[i], p);
631 p = isl_printer_end_line(p);
632 return p;
635 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
636 __isl_keep isl_space *dim, __isl_take isl_printer *p, int latex)
638 if (bmap->n_div > 0) {
639 int i;
640 p = isl_printer_print_str(p, s_open_exists[latex]);
641 for (i = 0; i < bmap->n_div; ++i) {
642 if (i)
643 p = isl_printer_print_str(p, ", ");
644 p = print_name(dim, p, isl_dim_div, i, latex);
645 if (latex || isl_int_is_zero(bmap->div[i][0]))
646 continue;
647 p = isl_printer_print_str(p, " = [(");
648 p = print_affine(bmap, dim, p, bmap->div[i] + 1);
649 p = isl_printer_print_str(p, ")/");
650 p = isl_printer_print_isl_int(p, bmap->div[i][0]);
651 p = isl_printer_print_str(p, "]");
653 p = isl_printer_print_str(p, ": ");
656 p = print_constraints(bmap, dim, p, latex);
658 if (bmap->n_div > 0)
659 p = isl_printer_print_str(p, s_close_exists[latex]);
660 return p;
663 static __isl_give isl_printer *isl_basic_map_print_isl(
664 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
665 int latex)
667 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
668 if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
669 p = print_tuple(bmap->dim, p, isl_dim_param, latex, NULL, NULL);
670 p = isl_printer_print_str(p, " -> ");
672 p = isl_printer_print_str(p, "{ ");
673 p = print_space(bmap->dim, p, latex, rational, NULL, NULL);
674 p = isl_printer_print_str(p, " : ");
675 p = print_disjunct(bmap, bmap->dim, p, latex);
676 p = isl_printer_print_str(p, " }");
677 return p;
680 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
681 __isl_take isl_printer *p, int latex)
683 int i;
685 if (isl_map_plain_is_universe(map))
686 return p;
688 p = isl_printer_print_str(p, s_such_that[latex]);
689 if (map->n == 0)
690 p = isl_printer_print_str(p, "1 = 0");
691 for (i = 0; i < map->n; ++i) {
692 if (i)
693 p = isl_printer_print_str(p, s_or[latex]);
694 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
695 p = isl_printer_print_str(p, "(");
696 p = print_disjunct(map->p[i], map->dim, p, latex);
697 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
698 p = isl_printer_print_str(p, ")");
700 return p;
703 /* Print the disjuncts of a map (or set).
704 * If the map turns out to be a universal parameter domain, then
705 * we need to print the colon. Otherwise, the output looks identical
706 * to the empty set.
708 static __isl_give isl_printer *print_disjuncts_map(__isl_keep isl_map *map,
709 __isl_take isl_printer *p, int latex)
711 if (isl_map_plain_is_universe(map) && isl_space_is_params(map->dim))
712 return isl_printer_print_str(p, s_such_that[latex]);
713 else
714 return print_disjuncts(map, p, latex);
717 struct isl_aff_split {
718 isl_basic_map *aff;
719 isl_map *map;
722 static void free_split(__isl_take struct isl_aff_split *split, int n)
724 int i;
726 if (!split)
727 return;
729 for (i = 0; i < n; ++i) {
730 isl_basic_map_free(split[i].aff);
731 isl_map_free(split[i].map);
734 free(split);
737 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
739 int i, j;
740 unsigned nparam, n_in, n_out, total;
742 bmap = isl_basic_map_cow(bmap);
743 if (!bmap)
744 return NULL;
745 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
746 goto error;
748 nparam = isl_basic_map_dim(bmap, isl_dim_param);
749 n_in = isl_basic_map_dim(bmap, isl_dim_in);
750 n_out = isl_basic_map_dim(bmap, isl_dim_out);
751 total = isl_basic_map_dim(bmap, isl_dim_all);
752 for (i = bmap->n_eq - 1; i >= 0; --i) {
753 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
754 if (j >= nparam && j < nparam + n_in + n_out &&
755 (isl_int_is_one(bmap->eq[i][1 + j]) ||
756 isl_int_is_negone(bmap->eq[i][1 + j])))
757 continue;
758 if (isl_basic_map_drop_equality(bmap, i) < 0)
759 goto error;
762 bmap = isl_basic_map_finalize(bmap);
764 return bmap;
765 error:
766 isl_basic_map_free(bmap);
767 return NULL;
770 static int aff_split_cmp(const void *p1, const void *p2)
772 const struct isl_aff_split *s1, *s2;
773 s1 = (const struct isl_aff_split *) p1;
774 s2 = (const struct isl_aff_split *) p2;
776 return isl_basic_map_plain_cmp(s1->aff, s2->aff);
779 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
780 __isl_keep isl_basic_map *aff)
782 int i, j;
783 unsigned total;
785 if (!bmap || !aff)
786 goto error;
788 total = isl_space_dim(bmap->dim, isl_dim_all);
790 for (i = bmap->n_eq - 1; i >= 0; --i) {
791 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
792 bmap->n_div) != -1)
793 continue;
794 for (j = 0; j < aff->n_eq; ++j) {
795 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
796 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
797 continue;
798 if (isl_basic_map_drop_equality(bmap, i) < 0)
799 goto error;
800 break;
804 return bmap;
805 error:
806 isl_basic_map_free(bmap);
807 return NULL;
810 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
812 int i, n;
813 struct isl_aff_split *split;
814 isl_ctx *ctx;
816 ctx = isl_map_get_ctx(map);
817 split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
818 if (!split)
819 return NULL;
821 for (i = 0; i < map->n; ++i) {
822 isl_basic_map *bmap;
823 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
824 bmap = isl_basic_map_copy(map->p[i]);
825 bmap = isl_basic_map_cow(bmap);
826 bmap = drop_aff(bmap, split[i].aff);
827 split[i].map = isl_map_from_basic_map(bmap);
828 if (!split[i].aff || !split[i].map)
829 goto error;
832 qsort(split, map->n, sizeof(struct isl_aff_split), &aff_split_cmp);
834 n = map->n;
835 for (i = n - 1; i >= 1; --i) {
836 if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
837 split[i].aff))
838 continue;
839 isl_basic_map_free(split[i].aff);
840 split[i - 1].map = isl_map_union(split[i - 1].map,
841 split[i].map);
842 if (i != n - 1)
843 split[i] = split[n - 1];
844 split[n - 1].aff = NULL;
845 split[n - 1].map = NULL;
846 --n;
849 return split;
850 error:
851 free_split(split, map->n);
852 return NULL;
855 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
856 struct isl_aff_split *split, int n)
858 int i;
859 int rational;
861 for (i = 0; i < n; ++i) {
862 isl_space *dim;
864 if (!split[i].map)
865 break;
866 dim = split[i].map->dim;
867 rational = split[i].map->n > 0 &&
868 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
869 if (i)
870 p = isl_printer_print_str(p, "; ");
871 p = print_space(dim, p, 0, rational, split[i].aff, NULL);
872 p = print_disjuncts_map(split[i].map, p, 0);
875 return p;
878 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
879 __isl_take isl_printer *p)
881 struct isl_aff_split *split = NULL;
882 int rational;
884 if (map->n > 0)
885 split = split_aff(map);
886 if (split) {
887 p = print_split_map(p, split, map->n);
888 } else {
889 rational = map->n > 0 &&
890 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
891 p = print_space(map->dim, p, 0, rational, NULL, NULL);
892 p = print_disjuncts_map(map, p, 0);
894 free_split(split, map->n);
895 return p;
898 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
899 __isl_take isl_printer *p)
901 if (isl_map_dim(map, isl_dim_param) > 0) {
902 p = print_tuple(map->dim, p, isl_dim_param, 0, NULL, NULL);
903 p = isl_printer_print_str(p, s_to[0]);
905 p = isl_printer_print_str(p, s_open_set[0]);
906 p = isl_map_print_isl_body(map, p);
907 p = isl_printer_print_str(p, s_close_set[0]);
908 return p;
911 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
912 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff)
914 if (isl_map_dim(map, isl_dim_param) > 0) {
915 p = print_tuple(map->dim, p, isl_dim_param, 1, NULL, NULL);
916 p = isl_printer_print_str(p, s_to[1]);
918 p = isl_printer_print_str(p, s_open_set[1]);
919 p = print_space(map->dim, p, 1, 0, aff, NULL);
920 p = print_disjuncts_map(map, p, 1);
921 p = isl_printer_print_str(p, s_close_set[1]);
923 return p;
926 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
927 __isl_take isl_printer *p)
929 int i;
930 struct isl_aff_split *split = NULL;
932 if (map->n > 0)
933 split = split_aff(map);
935 if (!split)
936 return print_latex_map(map, p, NULL);
938 for (i = 0; i < map->n; ++i) {
939 if (!split[i].map)
940 break;
941 if (i)
942 p = isl_printer_print_str(p, " \\cup ");
943 p = print_latex_map(split[i].map, p, split[i].aff);
946 free_split(split, map->n);
947 return p;
950 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
951 __isl_keep isl_basic_map *bmap)
953 if (!p || !bmap)
954 goto error;
955 if (p->output_format == ISL_FORMAT_ISL)
956 return isl_basic_map_print_isl(bmap, p, 0);
957 else if (p->output_format == ISL_FORMAT_OMEGA)
958 return isl_basic_map_print_omega(bmap, p);
959 isl_assert(bmap->ctx, 0, goto error);
960 error:
961 isl_printer_free(p);
962 return NULL;
965 void isl_basic_map_print(__isl_keep isl_basic_map *bmap, FILE *out, int indent,
966 const char *prefix, const char *suffix, unsigned output_format)
968 isl_printer *printer;
970 if (!bmap)
971 return;
973 printer = isl_printer_to_file(bmap->ctx, out);
974 printer = isl_printer_set_indent(printer, indent);
975 printer = isl_printer_set_prefix(printer, prefix);
976 printer = isl_printer_set_suffix(printer, suffix);
977 printer = isl_printer_set_output_format(printer, output_format);
978 isl_printer_print_basic_map(printer, bmap);
980 isl_printer_free(printer);
983 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
984 __isl_keep isl_basic_set *bset)
986 if (!p || !bset)
987 goto error;
989 if (p->output_format == ISL_FORMAT_ISL)
990 return isl_basic_map_print_isl(bset, p, 0);
991 else if (p->output_format == ISL_FORMAT_POLYLIB)
992 return isl_basic_set_print_polylib(bset, p, 0);
993 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
994 return isl_basic_set_print_polylib(bset, p, 1);
995 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
996 return bset_print_constraints_polylib(bset, p);
997 else if (p->output_format == ISL_FORMAT_OMEGA)
998 return isl_basic_set_print_omega(bset, p);
999 isl_assert(p->ctx, 0, goto error);
1000 error:
1001 isl_printer_free(p);
1002 return NULL;
1005 void isl_basic_set_print(struct isl_basic_set *bset, FILE *out, int indent,
1006 const char *prefix, const char *suffix, unsigned output_format)
1008 isl_printer *printer;
1010 if (!bset)
1011 return;
1013 printer = isl_printer_to_file(bset->ctx, out);
1014 printer = isl_printer_set_indent(printer, indent);
1015 printer = isl_printer_set_prefix(printer, prefix);
1016 printer = isl_printer_set_suffix(printer, suffix);
1017 printer = isl_printer_set_output_format(printer, output_format);
1018 isl_printer_print_basic_set(printer, bset);
1020 isl_printer_free(printer);
1023 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
1024 __isl_keep isl_set *set)
1026 if (!p || !set)
1027 goto error;
1028 if (p->output_format == ISL_FORMAT_ISL)
1029 return isl_map_print_isl((isl_map *)set, p);
1030 else if (p->output_format == ISL_FORMAT_POLYLIB)
1031 return isl_set_print_polylib(set, p, 0);
1032 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1033 return isl_set_print_polylib(set, p, 1);
1034 else if (p->output_format == ISL_FORMAT_OMEGA)
1035 return isl_set_print_omega(set, p);
1036 else if (p->output_format == ISL_FORMAT_LATEX)
1037 return isl_map_print_latex((isl_map *)set, p);
1038 isl_assert(set->ctx, 0, goto error);
1039 error:
1040 isl_printer_free(p);
1041 return NULL;
1044 void isl_set_print(struct isl_set *set, FILE *out, int indent,
1045 unsigned output_format)
1047 isl_printer *printer;
1049 if (!set)
1050 return;
1052 printer = isl_printer_to_file(set->ctx, out);
1053 printer = isl_printer_set_indent(printer, indent);
1054 printer = isl_printer_set_output_format(printer, output_format);
1055 printer = isl_printer_print_set(printer, set);
1057 isl_printer_free(printer);
1060 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1061 __isl_keep isl_map *map)
1063 if (!p || !map)
1064 goto error;
1066 if (p->output_format == ISL_FORMAT_ISL)
1067 return isl_map_print_isl(map, p);
1068 else if (p->output_format == ISL_FORMAT_POLYLIB)
1069 return isl_map_print_polylib(map, p, 0);
1070 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1071 return isl_map_print_polylib(map, p, 1);
1072 else if (p->output_format == ISL_FORMAT_OMEGA)
1073 return isl_map_print_omega(map, p);
1074 else if (p->output_format == ISL_FORMAT_LATEX)
1075 return isl_map_print_latex(map, p);
1076 isl_assert(map->ctx, 0, goto error);
1077 error:
1078 isl_printer_free(p);
1079 return NULL;
1082 struct isl_union_print_data {
1083 isl_printer *p;
1084 int first;
1087 static int print_map_body(__isl_take isl_map *map, void *user)
1089 struct isl_union_print_data *data;
1090 data = (struct isl_union_print_data *)user;
1092 if (!data->first)
1093 data->p = isl_printer_print_str(data->p, "; ");
1094 data->first = 0;
1096 data->p = isl_map_print_isl_body(map, data->p);
1097 isl_map_free(map);
1099 return 0;
1102 static __isl_give isl_printer *isl_union_map_print_isl(
1103 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1105 struct isl_union_print_data data = { p, 1 };
1106 isl_space *dim;
1107 dim = isl_union_map_get_space(umap);
1108 if (isl_space_dim(dim, isl_dim_param) > 0) {
1109 p = print_tuple(dim, p, isl_dim_param, 0, NULL, NULL);
1110 p = isl_printer_print_str(p, s_to[0]);
1112 isl_space_free(dim);
1113 p = isl_printer_print_str(p, s_open_set[0]);
1114 isl_union_map_foreach_map(umap, &print_map_body, &data);
1115 p = data.p;
1116 p = isl_printer_print_str(p, s_close_set[0]);
1117 return p;
1120 static int print_latex_map_body(__isl_take isl_map *map, void *user)
1122 struct isl_union_print_data *data;
1123 data = (struct isl_union_print_data *)user;
1125 if (!data->first)
1126 data->p = isl_printer_print_str(data->p, " \\cup ");
1127 data->first = 0;
1129 data->p = isl_map_print_latex(map, data->p);
1130 isl_map_free(map);
1132 return 0;
1135 static __isl_give isl_printer *isl_union_map_print_latex(
1136 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1138 struct isl_union_print_data data = { p, 1 };
1139 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1140 p = data.p;
1141 return p;
1144 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1145 __isl_keep isl_union_map *umap)
1147 if (!p || !umap)
1148 goto error;
1150 if (p->output_format == ISL_FORMAT_ISL)
1151 return isl_union_map_print_isl(umap, p);
1152 if (p->output_format == ISL_FORMAT_LATEX)
1153 return isl_union_map_print_latex(umap, p);
1155 isl_die(p->ctx, isl_error_invalid,
1156 "invalid output format for isl_union_map", goto error);
1157 error:
1158 isl_printer_free(p);
1159 return NULL;
1162 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1163 __isl_keep isl_union_set *uset)
1165 if (!p || !uset)
1166 goto error;
1168 if (p->output_format == ISL_FORMAT_ISL)
1169 return isl_union_map_print_isl((isl_union_map *)uset, p);
1170 if (p->output_format == ISL_FORMAT_LATEX)
1171 return isl_union_map_print_latex((isl_union_map *)uset, p);
1173 isl_die(p->ctx, isl_error_invalid,
1174 "invalid output format for isl_union_set", goto error);
1175 error:
1176 isl_printer_free(p);
1177 return NULL;
1180 void isl_map_print(__isl_keep isl_map *map, FILE *out, int indent,
1181 unsigned output_format)
1183 isl_printer *printer;
1185 if (!map)
1186 return;
1188 printer = isl_printer_to_file(map->ctx, out);
1189 printer = isl_printer_set_indent(printer, indent);
1190 printer = isl_printer_set_output_format(printer, output_format);
1191 printer = isl_printer_print_map(printer, map);
1193 isl_printer_free(printer);
1196 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
1198 int i;
1199 int n;
1201 for (i = 0, n = 0; i < rec->n; ++i)
1202 if (!isl_upoly_is_zero(rec->p[i]))
1203 ++n;
1205 return n;
1208 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
1209 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
1211 int c = p->output_format == ISL_FORMAT_C;
1212 p = isl_printer_print_str(p, c ? "floord(" : "[(");
1213 p = print_affine_of_len(dim, div, p,
1214 div->row[pos] + 1, div->n_col - 1);
1215 p = isl_printer_print_str(p, c ? ", " : ")/");
1216 p = isl_printer_print_isl_int(p, div->row[pos][0]);
1217 p = isl_printer_print_str(p, c ? ")" : "]");
1218 return p;
1221 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
1222 __isl_take isl_printer *p, int first)
1224 struct isl_upoly_cst *cst;
1225 int neg;
1227 cst = isl_upoly_as_cst(up);
1228 if (!cst)
1229 goto error;
1230 neg = !first && isl_int_is_neg(cst->n);
1231 if (!first)
1232 p = isl_printer_print_str(p, neg ? " - " : " + ");
1233 if (neg)
1234 isl_int_neg(cst->n, cst->n);
1235 if (isl_int_is_zero(cst->d)) {
1236 int sgn = isl_int_sgn(cst->n);
1237 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1238 sgn == 0 ? "NaN" : "infty");
1239 } else
1240 p = isl_printer_print_isl_int(p, cst->n);
1241 if (neg)
1242 isl_int_neg(cst->n, cst->n);
1243 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1244 p = isl_printer_print_str(p, "/");
1245 p = isl_printer_print_isl_int(p, cst->d);
1247 return p;
1248 error:
1249 isl_printer_free(p);
1250 return NULL;
1253 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1254 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var)
1256 unsigned total;
1258 total = isl_space_dim(dim, isl_dim_all);
1259 if (var < total)
1260 p = print_term(dim, NULL, dim->ctx->one, 1 + var, p, 0);
1261 else
1262 p = print_div(dim, div, var - total, p);
1263 return p;
1266 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1267 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var, int exp)
1269 p = print_base(p, dim, div, var);
1270 if (exp == 1)
1271 return p;
1272 if (p->output_format == ISL_FORMAT_C) {
1273 int i;
1274 for (i = 1; i < exp; ++i) {
1275 p = isl_printer_print_str(p, "*");
1276 p = print_base(p, dim, div, var);
1278 } else {
1279 p = isl_printer_print_str(p, "^");
1280 p = isl_printer_print_int(p, exp);
1282 return p;
1285 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1286 __isl_keep isl_space *dim, __isl_keep isl_mat *div,
1287 __isl_take isl_printer *p, int outer)
1289 int i, n, first, print_parens;
1290 struct isl_upoly_rec *rec;
1292 if (!p || !up || !dim || !div)
1293 goto error;
1295 if (isl_upoly_is_cst(up))
1296 return upoly_print_cst(up, p, 1);
1298 rec = isl_upoly_as_rec(up);
1299 if (!rec)
1300 goto error;
1301 n = upoly_rec_n_non_zero(rec);
1302 print_parens = n > 1 ||
1303 (outer && rec->up.var >= isl_space_dim(dim, isl_dim_all));
1304 if (print_parens)
1305 p = isl_printer_print_str(p, "(");
1306 for (i = 0, first = 1; i < rec->n; ++i) {
1307 if (isl_upoly_is_zero(rec->p[i]))
1308 continue;
1309 if (isl_upoly_is_negone(rec->p[i])) {
1310 if (!i)
1311 p = isl_printer_print_str(p, "-1");
1312 else if (first)
1313 p = isl_printer_print_str(p, "-");
1314 else
1315 p = isl_printer_print_str(p, " - ");
1316 } else if (isl_upoly_is_cst(rec->p[i]) &&
1317 !isl_upoly_is_one(rec->p[i]))
1318 p = upoly_print_cst(rec->p[i], p, first);
1319 else {
1320 if (!first)
1321 p = isl_printer_print_str(p, " + ");
1322 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1323 p = upoly_print(rec->p[i], dim, div, p, 0);
1325 first = 0;
1326 if (i == 0)
1327 continue;
1328 if (!isl_upoly_is_one(rec->p[i]) &&
1329 !isl_upoly_is_negone(rec->p[i]))
1330 p = isl_printer_print_str(p, " * ");
1331 p = print_pow(p, dim, div, rec->up.var, i);
1333 if (print_parens)
1334 p = isl_printer_print_str(p, ")");
1335 return p;
1336 error:
1337 isl_printer_free(p);
1338 return NULL;
1341 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1342 __isl_keep isl_qpolynomial *qp)
1344 if (!p || !qp)
1345 goto error;
1346 p = upoly_print(qp->upoly, qp->dim, qp->div, p, 1);
1347 return p;
1348 error:
1349 isl_printer_free(p);
1350 return NULL;
1353 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1354 __isl_keep isl_qpolynomial *qp)
1356 if (!p || !qp)
1357 goto error;
1359 if (isl_space_dim(qp->dim, isl_dim_param) > 0) {
1360 p = print_tuple(qp->dim, p, isl_dim_param, 0, NULL, NULL);
1361 p = isl_printer_print_str(p, " -> ");
1363 p = isl_printer_print_str(p, "{ ");
1364 if (!isl_space_is_params(qp->dim)) {
1365 p = print_space(qp->dim, p, 0, 0, NULL, NULL);
1366 p = isl_printer_print_str(p, " -> ");
1368 p = print_qpolynomial(p, qp);
1369 p = isl_printer_print_str(p, " }");
1370 return p;
1371 error:
1372 isl_printer_free(p);
1373 return NULL;
1376 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1377 __isl_keep isl_space *dim, __isl_keep isl_qpolynomial *qp)
1379 isl_int den;
1381 isl_int_init(den);
1382 isl_qpolynomial_get_den(qp, &den);
1383 if (!isl_int_is_one(den)) {
1384 isl_qpolynomial *f;
1385 p = isl_printer_print_str(p, "(");
1386 qp = isl_qpolynomial_copy(qp);
1387 f = isl_qpolynomial_rat_cst_on_domain(isl_space_copy(qp->dim),
1388 den, qp->dim->ctx->one);
1389 qp = isl_qpolynomial_mul(qp, f);
1391 if (qp)
1392 p = upoly_print(qp->upoly, dim, qp->div, p, 0);
1393 if (!isl_int_is_one(den)) {
1394 p = isl_printer_print_str(p, ")/");
1395 p = isl_printer_print_isl_int(p, den);
1396 isl_qpolynomial_free(qp);
1398 isl_int_clear(den);
1399 return p;
1402 __isl_give isl_printer *isl_printer_print_qpolynomial(
1403 __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1405 if (!p || !qp)
1406 goto error;
1408 if (p->output_format == ISL_FORMAT_ISL)
1409 return print_qpolynomial_isl(p, qp);
1410 else if (p->output_format == ISL_FORMAT_C)
1411 return print_qpolynomial_c(p, qp->dim, qp);
1412 else
1413 isl_die(qp->dim->ctx, isl_error_unsupported,
1414 "output format not supported for isl_qpolynomials",
1415 goto error);
1416 error:
1417 isl_printer_free(p);
1418 return NULL;
1421 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1422 unsigned output_format)
1424 isl_printer *p;
1426 if (!qp)
1427 return;
1429 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1430 p = isl_printer_to_file(qp->dim->ctx, out);
1431 p = isl_printer_print_qpolynomial(p, qp);
1432 isl_printer_free(p);
1435 static __isl_give isl_printer *qpolynomial_fold_print(
1436 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1438 int i;
1440 if (fold->type == isl_fold_min)
1441 p = isl_printer_print_str(p, "min");
1442 else if (fold->type == isl_fold_max)
1443 p = isl_printer_print_str(p, "max");
1444 p = isl_printer_print_str(p, "(");
1445 for (i = 0; i < fold->n; ++i) {
1446 if (i)
1447 p = isl_printer_print_str(p, ", ");
1448 p = print_qpolynomial(p, fold->qp[i]);
1450 p = isl_printer_print_str(p, ")");
1451 return p;
1454 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1455 FILE *out, unsigned output_format)
1457 isl_printer *p;
1459 if (!fold)
1460 return;
1462 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1464 p = isl_printer_to_file(fold->dim->ctx, out);
1465 p = isl_printer_print_qpolynomial_fold(p, fold);
1467 isl_printer_free(p);
1470 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1471 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1473 int i = 0;
1475 for (i = 0; i < pwqp->n; ++i) {
1476 if (i)
1477 p = isl_printer_print_str(p, "; ");
1478 if (!isl_space_is_params(pwqp->p[i].set->dim)) {
1479 p = print_space(pwqp->p[i].set->dim, p, 0, 0, NULL, NULL);
1480 p = isl_printer_print_str(p, " -> ");
1482 p = print_qpolynomial(p, pwqp->p[i].qp);
1483 p = print_disjuncts((isl_map *)pwqp->p[i].set, p, 0);
1486 return p;
1489 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1490 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1492 if (!p || !pwqp)
1493 goto error;
1495 if (isl_space_dim(pwqp->dim, isl_dim_param) > 0) {
1496 p = print_tuple(pwqp->dim, p, isl_dim_param, 0, NULL, NULL);
1497 p = isl_printer_print_str(p, " -> ");
1499 p = isl_printer_print_str(p, "{ ");
1500 if (pwqp->n == 0) {
1501 if (!isl_space_is_set(pwqp->dim)) {
1502 p = print_tuple(pwqp->dim, p, isl_dim_in, 0, NULL, NULL);
1503 p = isl_printer_print_str(p, " -> ");
1505 p = isl_printer_print_str(p, "0");
1507 p = isl_pwqp_print_isl_body(p, pwqp);
1508 p = isl_printer_print_str(p, " }");
1509 return p;
1510 error:
1511 isl_printer_free(p);
1512 return NULL;
1515 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
1516 unsigned output_format)
1518 isl_printer *p;
1520 if (!pwqp)
1521 return;
1523 p = isl_printer_to_file(pwqp->dim->ctx, out);
1524 p = isl_printer_set_output_format(p, output_format);
1525 p = isl_printer_print_pw_qpolynomial(p, pwqp);
1527 isl_printer_free(p);
1530 static __isl_give isl_printer *isl_pwf_print_isl_body(
1531 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1533 int i = 0;
1535 for (i = 0; i < pwf->n; ++i) {
1536 if (i)
1537 p = isl_printer_print_str(p, "; ");
1538 if (!isl_space_is_params(pwf->p[i].set->dim)) {
1539 p = print_space(pwf->p[i].set->dim, p, 0, 0, NULL, NULL);
1540 p = isl_printer_print_str(p, " -> ");
1542 p = qpolynomial_fold_print(pwf->p[i].fold, p);
1543 p = print_disjuncts((isl_map *)pwf->p[i].set, p, 0);
1546 return p;
1549 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
1550 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1552 if (isl_space_dim(pwf->dim, isl_dim_param) > 0) {
1553 p = print_tuple(pwf->dim, p, isl_dim_param, 0, NULL, NULL);
1554 p = isl_printer_print_str(p, " -> ");
1556 p = isl_printer_print_str(p, "{ ");
1557 if (pwf->n == 0) {
1558 if (!isl_space_is_set(pwf->dim)) {
1559 p = print_tuple(pwf->dim, p, isl_dim_in, 0, NULL, NULL);
1560 p = isl_printer_print_str(p, " -> ");
1562 p = isl_printer_print_str(p, "0");
1564 p = isl_pwf_print_isl_body(p, pwf);
1565 p = isl_printer_print_str(p, " }");
1566 return p;
1569 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1570 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c);
1572 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1573 __isl_keep isl_space *dim,
1574 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1576 if (type == isl_dim_div) {
1577 p = isl_printer_print_str(p, "floord(");
1578 p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
1579 p = isl_printer_print_str(p, ", ");
1580 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1581 p = isl_printer_print_str(p, ")");
1582 } else {
1583 const char *name;
1585 name = isl_space_get_dim_name(dim, type, pos);
1586 if (!name)
1587 name = "UNNAMED";
1588 p = isl_printer_print_str(p, name);
1590 return p;
1593 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1594 __isl_keep isl_space *dim,
1595 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1597 enum isl_dim_type type;
1599 if (pos == 0)
1600 return isl_printer_print_isl_int(p, c);
1602 if (isl_int_is_one(c))
1604 else if (isl_int_is_negone(c))
1605 p = isl_printer_print_str(p, "-");
1606 else {
1607 p = isl_printer_print_isl_int(p, c);
1608 p = isl_printer_print_str(p, "*");
1610 type = pos2type(dim, &pos);
1611 p = print_name_c(p, dim, bset, type, pos);
1612 return p;
1615 static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
1616 __isl_keep isl_space *dim,
1617 __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
1619 int i;
1620 int first;
1622 for (i = 0, first = 1; i < len; ++i) {
1623 int flip = 0;
1624 if (isl_int_is_zero(c[i]))
1625 continue;
1626 if (!first) {
1627 if (isl_int_is_neg(c[i])) {
1628 flip = 1;
1629 isl_int_neg(c[i], c[i]);
1630 p = isl_printer_print_str(p, " - ");
1631 } else
1632 p = isl_printer_print_str(p, " + ");
1634 first = 0;
1635 p = print_term_c(p, dim, bset, c[i], i);
1636 if (flip)
1637 isl_int_neg(c[i], c[i]);
1639 if (first)
1640 p = isl_printer_print_str(p, "0");
1641 return p;
1644 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1645 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c)
1647 unsigned len = 1 + isl_basic_set_total_dim(bset);
1648 return print_partial_affine_c(p, dim, bset, c, len);
1651 /* We skip the constraint if it is implied by the div expression.
1653 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1654 __isl_keep isl_space *dim,
1655 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int first)
1657 unsigned o_div;
1658 unsigned n_div;
1659 int div;
1661 o_div = isl_basic_set_offset(bset, isl_dim_div);
1662 n_div = isl_basic_set_dim(bset, isl_dim_div);
1663 div = isl_seq_last_non_zero(c + o_div, n_div);
1664 if (div >= 0 && isl_basic_set_is_div_constraint(bset, c, div))
1665 return p;
1667 if (!first)
1668 p = isl_printer_print_str(p, " && ");
1670 p = print_affine_c(p, dim, bset, c);
1671 p = isl_printer_print_str(p, " ");
1672 p = isl_printer_print_str(p, op);
1673 p = isl_printer_print_str(p, " 0");
1674 return p;
1677 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1678 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset)
1680 int i, j;
1681 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
1682 unsigned total = isl_basic_set_total_dim(bset) - n_div;
1684 for (i = 0; i < bset->n_eq; ++i) {
1685 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
1686 if (j < 0)
1687 p = print_constraint_c(p, dim, bset,
1688 bset->eq[i], "==", !i);
1689 else {
1690 if (i)
1691 p = isl_printer_print_str(p, " && ");
1692 p = isl_printer_print_str(p, "(");
1693 p = print_partial_affine_c(p, dim, bset, bset->eq[i],
1694 1 + total + j);
1695 p = isl_printer_print_str(p, ") % ");
1696 p = isl_printer_print_isl_int(p,
1697 bset->eq[i][1 + total + j]);
1698 p = isl_printer_print_str(p, " == 0");
1701 for (i = 0; i < bset->n_ineq; ++i)
1702 p = print_constraint_c(p, dim, bset, bset->ineq[i], ">=",
1703 !bset->n_eq && !i);
1704 return p;
1707 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
1708 __isl_keep isl_space *dim, __isl_keep isl_set *set)
1710 int i;
1712 if (set->n == 0)
1713 p = isl_printer_print_str(p, "0");
1715 for (i = 0; i < set->n; ++i) {
1716 if (i)
1717 p = isl_printer_print_str(p, " || ");
1718 if (set->n > 1)
1719 p = isl_printer_print_str(p, "(");
1720 p = print_basic_set_c(p, dim, set->p[i]);
1721 if (set->n > 1)
1722 p = isl_printer_print_str(p, ")");
1724 return p;
1727 static __isl_give isl_printer *print_pw_qpolynomial_c(
1728 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1730 int i;
1732 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set))
1733 return print_qpolynomial_c(p, pwqp->dim, pwqp->p[0].qp);
1735 for (i = 0; i < pwqp->n; ++i) {
1736 p = isl_printer_print_str(p, "(");
1737 p = print_set_c(p, pwqp->dim, pwqp->p[i].set);
1738 p = isl_printer_print_str(p, ") ? (");
1739 p = print_qpolynomial_c(p, pwqp->dim, pwqp->p[i].qp);
1740 p = isl_printer_print_str(p, ") : ");
1743 p = isl_printer_print_str(p, "0");
1744 return p;
1747 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
1748 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1750 if (!p || !pwqp)
1751 goto error;
1753 if (p->output_format == ISL_FORMAT_ISL)
1754 return print_pw_qpolynomial_isl(p, pwqp);
1755 else if (p->output_format == ISL_FORMAT_C)
1756 return print_pw_qpolynomial_c(p, pwqp);
1757 isl_assert(p->ctx, 0, goto error);
1758 error:
1759 isl_printer_free(p);
1760 return NULL;
1763 static int print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1765 struct isl_union_print_data *data;
1766 data = (struct isl_union_print_data *)user;
1768 if (!data->first)
1769 data->p = isl_printer_print_str(data->p, "; ");
1770 data->first = 0;
1772 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
1773 isl_pw_qpolynomial_free(pwqp);
1775 return 0;
1778 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
1779 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1781 struct isl_union_print_data data = { p, 1 };
1782 isl_space *dim;
1783 dim = isl_union_pw_qpolynomial_get_space(upwqp);
1784 if (isl_space_dim(dim, isl_dim_param) > 0) {
1785 p = print_tuple(dim, p, isl_dim_param, 0, NULL, NULL);
1786 p = isl_printer_print_str(p, " -> ");
1788 isl_space_free(dim);
1789 p = isl_printer_print_str(p, "{ ");
1790 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
1791 &data);
1792 p = data.p;
1793 p = isl_printer_print_str(p, " }");
1794 return p;
1797 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
1798 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1800 if (!p || !upwqp)
1801 goto error;
1803 if (p->output_format == ISL_FORMAT_ISL)
1804 return print_union_pw_qpolynomial_isl(p, upwqp);
1805 isl_die(p->ctx, isl_error_invalid,
1806 "invalid output format for isl_union_pw_qpolynomial",
1807 goto error);
1808 error:
1809 isl_printer_free(p);
1810 return NULL;
1813 static __isl_give isl_printer *print_qpolynomial_fold_c(
1814 __isl_take isl_printer *p, __isl_keep isl_space *dim,
1815 __isl_keep isl_qpolynomial_fold *fold)
1817 int i;
1819 for (i = 0; i < fold->n - 1; ++i)
1820 if (fold->type == isl_fold_min)
1821 p = isl_printer_print_str(p, "min(");
1822 else if (fold->type == isl_fold_max)
1823 p = isl_printer_print_str(p, "max(");
1825 for (i = 0; i < fold->n; ++i) {
1826 if (i)
1827 p = isl_printer_print_str(p, ", ");
1828 p = print_qpolynomial_c(p, dim, fold->qp[i]);
1829 if (i)
1830 p = isl_printer_print_str(p, ")");
1832 return p;
1835 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
1836 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
1838 if (!p || !fold)
1839 goto error;
1840 if (p->output_format == ISL_FORMAT_ISL)
1841 return qpolynomial_fold_print(fold, p);
1842 else if (p->output_format == ISL_FORMAT_C)
1843 return print_qpolynomial_fold_c(p, fold->dim, fold);
1844 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
1845 goto error);
1846 error:
1847 isl_printer_free(p);
1848 return NULL;
1851 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
1852 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1854 int i;
1856 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set))
1857 return print_qpolynomial_fold_c(p, pwf->dim, pwf->p[0].fold);
1859 for (i = 0; i < pwf->n; ++i) {
1860 p = isl_printer_print_str(p, "(");
1861 p = print_set_c(p, pwf->dim, pwf->p[i].set);
1862 p = isl_printer_print_str(p, ") ? (");
1863 p = print_qpolynomial_fold_c(p, pwf->dim, pwf->p[i].fold);
1864 p = isl_printer_print_str(p, ") : ");
1867 p = isl_printer_print_str(p, "0");
1868 return p;
1871 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
1872 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1874 if (!p || !pwf)
1875 goto error;
1877 if (p->output_format == ISL_FORMAT_ISL)
1878 return print_pw_qpolynomial_fold_isl(p, pwf);
1879 else if (p->output_format == ISL_FORMAT_C)
1880 return print_pw_qpolynomial_fold_c(p, pwf);
1881 isl_assert(p->ctx, 0, goto error);
1882 error:
1883 isl_printer_free(p);
1884 return NULL;
1887 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
1888 FILE *out, unsigned output_format)
1890 isl_printer *p;
1892 if (!pwf)
1893 return;
1895 p = isl_printer_to_file(pwf->dim->ctx, out);
1896 p = isl_printer_set_output_format(p, output_format);
1897 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
1899 isl_printer_free(p);
1902 static int print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf, void *user)
1904 struct isl_union_print_data *data;
1905 data = (struct isl_union_print_data *)user;
1907 if (!data->first)
1908 data->p = isl_printer_print_str(data->p, "; ");
1909 data->first = 0;
1911 data->p = isl_pwf_print_isl_body(data->p, pwf);
1912 isl_pw_qpolynomial_fold_free(pwf);
1914 return 0;
1917 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
1918 __isl_take isl_printer *p,
1919 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1921 struct isl_union_print_data data = { p, 1 };
1922 isl_space *dim;
1923 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
1924 if (isl_space_dim(dim, isl_dim_param) > 0) {
1925 p = print_tuple(dim, p, isl_dim_param, 0, NULL, NULL);
1926 p = isl_printer_print_str(p, " -> ");
1928 isl_space_free(dim);
1929 p = isl_printer_print_str(p, "{ ");
1930 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
1931 &print_pwf_body, &data);
1932 p = data.p;
1933 p = isl_printer_print_str(p, " }");
1934 return p;
1937 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
1938 __isl_take isl_printer *p,
1939 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1941 if (!p || !upwf)
1942 goto error;
1944 if (p->output_format == ISL_FORMAT_ISL)
1945 return print_union_pw_qpolynomial_fold_isl(p, upwf);
1946 isl_die(p->ctx, isl_error_invalid,
1947 "invalid output format for isl_union_pw_qpolynomial_fold",
1948 goto error);
1949 error:
1950 isl_printer_free(p);
1951 return NULL;
1954 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
1955 __isl_keep isl_constraint *c)
1957 isl_basic_map *bmap;
1959 if (!p || !c)
1960 goto error;
1962 bmap = isl_basic_map_from_constraint(isl_constraint_copy(c));
1963 p = isl_printer_print_basic_map(p, bmap);
1964 isl_basic_map_free(bmap);
1965 return p;
1966 error:
1967 isl_printer_free(p);
1968 return NULL;
1971 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
1972 __isl_keep isl_space *dim)
1974 if (!dim)
1975 goto error;
1977 if (isl_space_dim(dim, isl_dim_param) > 0) {
1978 p = print_tuple(dim, p, isl_dim_param, 0, NULL, NULL);
1979 p = isl_printer_print_str(p, " -> ");
1982 p = isl_printer_print_str(p, "{ ");
1983 if (isl_space_is_params(dim))
1984 p = isl_printer_print_str(p, s_such_that[0]);
1985 else
1986 p = print_space(dim, p, 0, 0, NULL, NULL);
1987 p = isl_printer_print_str(p, " }");
1989 return p;
1990 error:
1991 isl_printer_free(p);
1992 return NULL;
1995 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
1996 __isl_keep isl_local_space *ls)
1998 unsigned total;
1999 unsigned n_div;
2001 if (!ls)
2002 goto error;
2004 total = isl_local_space_dim(ls, isl_dim_all);
2005 if (isl_local_space_dim(ls, isl_dim_param) > 0) {
2006 p = print_tuple(ls->dim, p, isl_dim_param, 0, NULL, NULL);
2007 p = isl_printer_print_str(p, " -> ");
2009 p = isl_printer_print_str(p, "{ ");
2010 p = print_space(ls->dim, p, 0, 0, NULL, NULL);
2011 n_div = isl_local_space_dim(ls, isl_dim_div);
2012 if (n_div > 0) {
2013 int i;
2014 p = isl_printer_print_str(p, " : ");
2015 p = isl_printer_print_str(p, s_open_exists[0]);
2016 for (i = 0; i < n_div; ++i) {
2017 if (i)
2018 p = isl_printer_print_str(p, ", ");
2019 p = print_name(ls->dim, p, isl_dim_div, i, 0);
2020 if (isl_int_is_zero(ls->div->row[i][0]))
2021 continue;
2022 p = isl_printer_print_str(p, " = [(");
2023 p = print_affine_of_len(ls->dim, ls->div, p,
2024 ls->div->row[i] + 1, 1 + total);
2025 p = isl_printer_print_str(p, ")/");
2026 p = isl_printer_print_isl_int(p, ls->div->row[i][0]);
2027 p = isl_printer_print_str(p, "]");
2029 } else if (isl_space_is_params(ls->dim))
2030 p = isl_printer_print_str(p, s_such_that[0]);
2031 p = isl_printer_print_str(p, " }");
2032 return p;
2033 error:
2034 isl_printer_free(p);
2035 return NULL;
2038 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2039 __isl_keep isl_aff *aff)
2041 unsigned total;
2043 total = isl_local_space_dim(aff->ls, isl_dim_all);
2044 p = isl_printer_print_str(p, "(");
2045 p = print_affine_of_len(aff->ls->dim, aff->ls->div, p,
2046 aff->v->el + 1, 1 + total);
2047 if (isl_int_is_one(aff->v->el[0]))
2048 p = isl_printer_print_str(p, ")");
2049 else {
2050 p = isl_printer_print_str(p, ")/");
2051 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2054 return p;
2057 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2058 __isl_keep isl_aff *aff)
2060 if (isl_space_is_params(aff->ls->dim))
2062 else {
2063 p = print_tuple(aff->ls->dim, p, isl_dim_set, 0, NULL, NULL);
2064 p = isl_printer_print_str(p, " -> ");
2066 p = isl_printer_print_str(p, "[");
2067 p = print_aff_body(p, aff);
2068 p = isl_printer_print_str(p, "]");
2070 return p;
2073 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2074 __isl_keep isl_aff *aff)
2076 if (!aff)
2077 goto error;
2079 if (isl_local_space_dim(aff->ls, isl_dim_param) > 0) {
2080 p = print_tuple(aff->ls->dim, p, isl_dim_param, 0, NULL, NULL);
2081 p = isl_printer_print_str(p, " -> ");
2083 p = isl_printer_print_str(p, "{ ");
2084 p = print_aff(p, aff);
2085 p = isl_printer_print_str(p, " }");
2086 return p;
2087 error:
2088 isl_printer_free(p);
2089 return NULL;
2092 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2093 __isl_keep isl_pw_aff *pwaff)
2095 int i;
2097 if (!pwaff)
2098 goto error;
2100 if (isl_space_dim(pwaff->dim, isl_dim_param) > 0) {
2101 p = print_tuple(pwaff->dim, p, isl_dim_param, 0, NULL, NULL);
2102 p = isl_printer_print_str(p, " -> ");
2104 p = isl_printer_print_str(p, "{ ");
2105 for (i = 0; i < pwaff->n; ++i) {
2106 if (i)
2107 p = isl_printer_print_str(p, "; ");
2108 p = print_aff(p, pwaff->p[i].aff);
2109 p = print_disjuncts((isl_map *)pwaff->p[i].set, p, 0);
2111 p = isl_printer_print_str(p, " }");
2112 return p;
2113 error:
2114 isl_printer_free(p);
2115 return NULL;
2118 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2119 __isl_keep isl_local_space *ls, isl_int *c);
2121 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2122 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2124 if (type == isl_dim_div) {
2125 p = isl_printer_print_str(p, "floord(");
2126 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2127 p = isl_printer_print_str(p, ", ");
2128 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2129 p = isl_printer_print_str(p, ")");
2130 } else {
2131 const char *name;
2133 name = isl_space_get_dim_name(ls->dim, type, pos);
2134 if (!name)
2135 name = "UNNAMED";
2136 p = isl_printer_print_str(p, name);
2138 return p;
2141 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2142 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2144 enum isl_dim_type type;
2146 if (pos == 0)
2147 return isl_printer_print_isl_int(p, c);
2149 if (isl_int_is_one(c))
2151 else if (isl_int_is_negone(c))
2152 p = isl_printer_print_str(p, "-");
2153 else {
2154 p = isl_printer_print_isl_int(p, c);
2155 p = isl_printer_print_str(p, "*");
2157 type = pos2type(ls->dim, &pos);
2158 p = print_ls_name_c(p, ls, type, pos);
2159 return p;
2162 static __isl_give isl_printer *print_ls_partial_affine_c(
2163 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2164 isl_int *c, unsigned len)
2166 int i;
2167 int first;
2169 for (i = 0, first = 1; i < len; ++i) {
2170 int flip = 0;
2171 if (isl_int_is_zero(c[i]))
2172 continue;
2173 if (!first) {
2174 if (isl_int_is_neg(c[i])) {
2175 flip = 1;
2176 isl_int_neg(c[i], c[i]);
2177 p = isl_printer_print_str(p, " - ");
2178 } else
2179 p = isl_printer_print_str(p, " + ");
2181 first = 0;
2182 p = print_ls_term_c(p, ls, c[i], i);
2183 if (flip)
2184 isl_int_neg(c[i], c[i]);
2186 if (first)
2187 p = isl_printer_print_str(p, "0");
2188 return p;
2191 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2192 __isl_keep isl_local_space *ls, isl_int *c)
2194 unsigned len = 1 + isl_local_space_dim(ls, isl_dim_all);
2195 return print_ls_partial_affine_c(p, ls, c, len);
2198 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2199 __isl_keep isl_aff *aff)
2201 unsigned total;
2203 total = isl_local_space_dim(aff->ls, isl_dim_all);
2204 if (!isl_int_is_one(aff->v->el[0]))
2205 p = isl_printer_print_str(p, "(");
2206 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2207 if (!isl_int_is_one(aff->v->el[0])) {
2208 p = isl_printer_print_str(p, ")/");
2209 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2211 return p;
2214 /* In the C format, we cannot express that "pwaff" may be undefined
2215 * on parts of the domain space. We therefore assume that the expression
2216 * will only be evaluated on its definition domain and compute the gist
2217 * of each cell with respect to this domain.
2219 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2220 __isl_keep isl_pw_aff *pwaff)
2222 int i;
2223 isl_set *domain;
2224 isl_space *space;
2226 if (pwaff->n < 1)
2227 isl_die(p->ctx, isl_error_unsupported,
2228 "cannot print empty isl_pw_aff in C format", goto error);
2229 space = isl_pw_aff_get_domain_space(pwaff);
2230 if (!space)
2231 goto error;
2233 domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
2235 for (i = 0; i < pwaff->n - 1; ++i) {
2236 isl_set *set_i;
2238 p = isl_printer_print_str(p, "(");
2240 set_i = isl_set_copy(pwaff->p[i].set);
2241 set_i = isl_set_gist(set_i, isl_set_copy(domain));
2242 p = print_set_c(p, space, set_i);
2243 isl_set_free(set_i);
2245 p = isl_printer_print_str(p, ") ? (");
2246 p = print_aff_c(p, pwaff->p[i].aff);
2247 p = isl_printer_print_str(p, ") : ");
2250 isl_set_free(domain);
2251 isl_space_free(space);
2253 return print_aff_c(p, pwaff->p[pwaff->n - 1].aff);
2254 error:
2255 isl_printer_free(p);
2256 return NULL;
2259 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2260 __isl_keep isl_aff *aff)
2262 if (!p || !aff)
2263 goto error;
2265 if (p->output_format == ISL_FORMAT_ISL)
2266 return print_aff_isl(p, aff);
2267 else if (p->output_format == ISL_FORMAT_C)
2268 return print_aff_c(p, aff);
2269 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2270 goto error);
2271 error:
2272 isl_printer_free(p);
2273 return NULL;
2276 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2277 __isl_keep isl_pw_aff *pwaff)
2279 if (!p || !pwaff)
2280 goto error;
2282 if (p->output_format == ISL_FORMAT_ISL)
2283 return print_pw_aff_isl(p, pwaff);
2284 else if (p->output_format == ISL_FORMAT_C)
2285 return print_pw_aff_c(p, pwaff);
2286 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2287 goto error);
2288 error:
2289 isl_printer_free(p);
2290 return NULL;
2293 static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
2294 __isl_keep isl_multi_aff *maff)
2296 return print_space(maff->space, p, 0, 0, NULL, maff);
2299 static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
2300 __isl_keep isl_multi_aff *maff)
2302 if (!maff)
2303 goto error;
2305 if (isl_space_dim(maff->space, isl_dim_param) > 0) {
2306 p = print_tuple(maff->space, p, isl_dim_param, 0, NULL, NULL);
2307 p = isl_printer_print_str(p, " -> ");
2309 p = isl_printer_print_str(p, "{ ");
2310 p = print_multi_aff(p, maff);
2311 p = isl_printer_print_str(p, " }");
2312 return p;
2313 error:
2314 isl_printer_free(p);
2315 return NULL;
2318 __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
2319 __isl_keep isl_multi_aff *maff)
2321 if (!p || !maff)
2322 goto error;
2324 if (p->output_format == ISL_FORMAT_ISL)
2325 return print_multi_aff_isl(p, maff);
2326 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2327 goto error);
2328 error:
2329 isl_printer_free(p);
2330 return NULL;
2333 static __isl_give isl_printer *print_pw_multi_aff_body(
2334 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2336 int i;
2338 if (!pma)
2339 goto error;
2341 for (i = 0; i < pma->n; ++i) {
2342 if (i)
2343 p = isl_printer_print_str(p, "; ");
2344 p = print_multi_aff(p, pma->p[i].maff);
2345 p = print_disjuncts((isl_map *)pma->p[i].set, p, 0);
2347 return p;
2348 error:
2349 isl_printer_free(p);
2350 return NULL;
2353 static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
2354 __isl_keep isl_pw_multi_aff *pma)
2356 if (!pma)
2357 goto error;
2359 if (isl_space_dim(pma->dim, isl_dim_param) > 0) {
2360 p = print_tuple(pma->dim, p, isl_dim_param, 0, NULL, NULL);
2361 p = isl_printer_print_str(p, " -> ");
2363 p = isl_printer_print_str(p, "{ ");
2364 p = print_pw_multi_aff_body(p, pma);
2365 p = isl_printer_print_str(p, " }");
2366 return p;
2367 error:
2368 isl_printer_free(p);
2369 return NULL;
2372 static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
2373 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2375 int i;
2377 for (i = 0; i < pma->n - 1; ++i) {
2378 p = isl_printer_print_str(p, "(");
2379 p = print_set_c(p, pma->dim, pma->p[i].set);
2380 p = isl_printer_print_str(p, ") ? (");
2381 p = print_aff_c(p, pma->p[i].maff->p[0]);
2382 p = isl_printer_print_str(p, ") : ");
2385 return print_aff_c(p, pma->p[pma->n - 1].maff->p[0]);
2388 static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
2389 __isl_keep isl_pw_multi_aff *pma)
2391 int n;
2392 const char *name;
2394 if (!pma)
2395 goto error;
2396 if (pma->n < 1)
2397 isl_die(p->ctx, isl_error_unsupported,
2398 "cannot print empty isl_pw_multi_aff in C format",
2399 goto error);
2400 name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
2401 if (!name && isl_pw_multi_aff_dim(pma, isl_dim_out) == 1)
2402 return print_unnamed_pw_multi_aff_c(p, pma);
2403 if (!name)
2404 isl_die(p->ctx, isl_error_unsupported,
2405 "cannot print unnamed isl_pw_multi_aff in C format",
2406 goto error);
2408 p = isl_printer_print_str(p, name);
2409 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
2410 if (n != 0)
2411 isl_die(p->ctx, isl_error_unsupported,
2412 "not supported yet", goto error);
2414 return p;
2415 error:
2416 isl_printer_free(p);
2417 return NULL;
2420 __isl_give isl_printer *isl_printer_print_pw_multi_aff(
2421 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2423 if (!p || !pma)
2424 goto error;
2426 if (p->output_format == ISL_FORMAT_ISL)
2427 return print_pw_multi_aff_isl(p, pma);
2428 if (p->output_format == ISL_FORMAT_C)
2429 return print_pw_multi_aff_c(p, pma);
2430 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2431 goto error);
2432 error:
2433 isl_printer_free(p);
2434 return NULL;
2437 static int print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
2438 void *user)
2440 struct isl_union_print_data *data;
2441 data = (struct isl_union_print_data *) user;
2443 if (!data->first)
2444 data->p = isl_printer_print_str(data->p, "; ");
2445 data->first = 0;
2447 data->p = print_pw_multi_aff_body(data->p, pma);
2448 isl_pw_multi_aff_free(pma);
2450 return 0;
2453 static __isl_give isl_printer *print_union_pw_multi_aff_isl(
2454 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2456 struct isl_union_print_data data = { p, 1 };
2457 isl_space *space;
2459 space = isl_union_pw_multi_aff_get_space(upma);
2460 if (isl_space_dim(space, isl_dim_param) > 0) {
2461 p = print_tuple(space, p, isl_dim_param, 0, NULL, NULL);
2462 p = isl_printer_print_str(p, s_to[0]);
2464 isl_space_free(space);
2465 p = isl_printer_print_str(p, s_open_set[0]);
2466 isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
2467 &print_pw_multi_aff_body_wrap, &data);
2468 p = data.p;
2469 p = isl_printer_print_str(p, s_close_set[0]);
2470 return p;
2473 __isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
2474 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2476 if (!p || !upma)
2477 goto error;
2479 if (p->output_format == ISL_FORMAT_ISL)
2480 return print_union_pw_multi_aff_isl(p, upma);
2481 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2482 goto error);
2483 error:
2484 isl_printer_free(p);
2485 return NULL;