isl_basic_map_normalize: avoid invalid access on error
[isl.git] / isl_output.c
blobd6851605a9b5bfbd883d3c5d4712bdc244a93941
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the MIT 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>
27 #include <isl_ast_build_expr.h>
29 static const char *s_to[2] = { " -> ", " \\to " };
30 static const char *s_and[2] = { " and ", " \\wedge " };
31 static const char *s_or[2] = { " or ", " \\vee " };
32 static const char *s_le[2] = { "<=", "\\le" };
33 static const char *s_ge[2] = { ">=", "\\ge" };
34 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
35 static const char *s_close_set[2] = { " }", " \\,\\}" };
36 static const char *s_open_list[2] = { "[", "(" };
37 static const char *s_close_list[2] = { "]", ")" };
38 static const char *s_such_that[2] = { " : ", " \\mid " };
39 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
40 static const char *s_close_exists[2] = { ")", "" };
41 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
42 static const char *s_param_prefix[2] = { "p", "p_" };
43 static const char *s_input_prefix[2] = { "i", "i_" };
44 static const char *s_output_prefix[2] = { "o", "o_" };
46 static __isl_give isl_printer *print_constraint_polylib(
47 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
49 int i;
50 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
51 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
52 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
53 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
55 p = isl_printer_start_line(p);
56 p = isl_printer_print_int(p, ineq);
57 for (i = 0; i < n_out; ++i) {
58 p = isl_printer_print_str(p, " ");
59 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
61 for (i = 0; i < n_in; ++i) {
62 p = isl_printer_print_str(p, " ");
63 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
65 for (i = 0; i < bmap->n_div; ++i) {
66 p = isl_printer_print_str(p, " ");
67 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
69 for (i = 0; i < nparam; ++i) {
70 p = isl_printer_print_str(p, " ");
71 p = isl_printer_print_isl_int(p, c[1+i]);
73 p = isl_printer_print_str(p, " ");
74 p = isl_printer_print_isl_int(p, c[0]);
75 p = isl_printer_end_line(p);
76 return p;
79 static __isl_give isl_printer *print_constraints_polylib(
80 struct isl_basic_map *bmap, __isl_take isl_printer *p)
82 int i;
84 p = isl_printer_set_isl_int_width(p, 5);
86 for (i = 0; i < bmap->n_eq; ++i)
87 p = print_constraint_polylib(bmap, 0, i, p);
88 for (i = 0; i < bmap->n_ineq; ++i)
89 p = print_constraint_polylib(bmap, 1, i, p);
91 return p;
94 static __isl_give isl_printer *bset_print_constraints_polylib(
95 struct isl_basic_set *bset, __isl_take isl_printer *p)
97 return print_constraints_polylib((struct isl_basic_map *)bset, p);
100 static __isl_give isl_printer *isl_basic_map_print_polylib(
101 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
103 unsigned total = isl_basic_map_total_dim(bmap);
104 p = isl_printer_start_line(p);
105 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
106 p = isl_printer_print_str(p, " ");
107 p = isl_printer_print_int(p, 1 + total + 1);
108 if (ext) {
109 p = isl_printer_print_str(p, " ");
110 p = isl_printer_print_int(p,
111 isl_basic_map_dim(bmap, isl_dim_out));
112 p = isl_printer_print_str(p, " ");
113 p = isl_printer_print_int(p,
114 isl_basic_map_dim(bmap, isl_dim_in));
115 p = isl_printer_print_str(p, " ");
116 p = isl_printer_print_int(p,
117 isl_basic_map_dim(bmap, isl_dim_div));
118 p = isl_printer_print_str(p, " ");
119 p = isl_printer_print_int(p,
120 isl_basic_map_dim(bmap, isl_dim_param));
122 p = isl_printer_end_line(p);
123 return print_constraints_polylib(bmap, p);
126 static __isl_give isl_printer *isl_basic_set_print_polylib(
127 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
129 return isl_basic_map_print_polylib((struct isl_basic_map *)bset, p, ext);
132 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
133 __isl_take isl_printer *p, int ext)
135 int i;
137 p = isl_printer_start_line(p);
138 p = isl_printer_print_int(p, map->n);
139 p = isl_printer_end_line(p);
140 for (i = 0; i < map->n; ++i) {
141 p = isl_printer_start_line(p);
142 p = isl_printer_end_line(p);
143 p = isl_basic_map_print_polylib(map->p[i], p, ext);
145 return p;
148 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
149 __isl_take isl_printer *p, int ext)
151 return isl_map_print_polylib((struct isl_map *)set, p, ext);
154 static int count_same_name(__isl_keep isl_space *dim,
155 enum isl_dim_type type, unsigned pos, const char *name)
157 enum isl_dim_type t;
158 unsigned p, s;
159 int count = 0;
161 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
162 s = t == type ? pos : isl_space_dim(dim, t);
163 for (p = 0; p < s; ++p) {
164 const char *n = isl_space_get_dim_name(dim, t, p);
165 if (n && !strcmp(n, name))
166 count++;
169 return count;
172 static __isl_give isl_printer *print_name(__isl_keep isl_space *dim,
173 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
174 int latex)
176 const char *name;
177 char buffer[20];
178 int primes;
180 name = type == isl_dim_div ? NULL : isl_space_get_dim_name(dim, type, pos);
182 if (!name) {
183 const char *prefix;
184 if (type == isl_dim_param)
185 prefix = s_param_prefix[latex];
186 else if (type == isl_dim_div)
187 prefix = s_div_prefix[latex];
188 else if (isl_space_is_set(dim) || type == isl_dim_in)
189 prefix = s_input_prefix[latex];
190 else
191 prefix = s_output_prefix[latex];
192 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
193 name = buffer;
195 primes = count_same_name(dim, name == buffer ? isl_dim_div : type,
196 pos, name);
197 p = isl_printer_print_str(p, name);
198 while (primes-- > 0)
199 p = isl_printer_print_str(p, "'");
200 return p;
203 static enum isl_dim_type pos2type(__isl_keep isl_space *dim, unsigned *pos)
205 enum isl_dim_type type;
206 unsigned n_in = isl_space_dim(dim, isl_dim_in);
207 unsigned n_out = isl_space_dim(dim, isl_dim_out);
208 unsigned nparam = isl_space_dim(dim, isl_dim_param);
210 if (*pos < 1 + nparam) {
211 type = isl_dim_param;
212 *pos -= 1;
213 } else if (*pos < 1 + nparam + n_in) {
214 type = isl_dim_in;
215 *pos -= 1 + nparam;
216 } else if (*pos < 1 + nparam + n_in + n_out) {
217 type = isl_dim_out;
218 *pos -= 1 + nparam + n_in;
219 } else {
220 type = isl_dim_div;
221 *pos -= 1 + nparam + n_in + n_out;
224 return type;
227 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
228 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
230 static __isl_give isl_printer *print_term(__isl_keep isl_space *dim,
231 __isl_keep isl_mat *div,
232 isl_int c, unsigned pos, __isl_take isl_printer *p, int latex)
234 enum isl_dim_type type;
235 int print_div_def;
237 if (pos == 0)
238 return isl_printer_print_isl_int(p, c);
240 type = pos2type(dim, &pos);
241 print_div_def = type == isl_dim_div && div &&
242 !isl_int_is_zero(div->row[pos][0]);
244 if (isl_int_is_one(c))
246 else if (isl_int_is_negone(c))
247 p = isl_printer_print_str(p, "-");
248 else {
249 p = isl_printer_print_isl_int(p, c);
250 if (p->output_format == ISL_FORMAT_C || print_div_def)
251 p = isl_printer_print_str(p, "*");
253 if (print_div_def)
254 p = print_div(dim, div, pos, p);
255 else
256 p = print_name(dim, p, type, pos, latex);
257 return p;
260 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_space *dim,
261 __isl_keep isl_mat *div,
262 __isl_take isl_printer *p, isl_int *c, int len)
264 int i;
265 int first;
267 for (i = 0, first = 1; i < len; ++i) {
268 int flip = 0;
269 if (isl_int_is_zero(c[i]))
270 continue;
271 if (!first) {
272 if (isl_int_is_neg(c[i])) {
273 flip = 1;
274 isl_int_neg(c[i], c[i]);
275 p = isl_printer_print_str(p, " - ");
276 } else
277 p = isl_printer_print_str(p, " + ");
279 first = 0;
280 p = print_term(dim, div, c[i], i, p, 0);
281 if (flip)
282 isl_int_neg(c[i], c[i]);
284 if (first)
285 p = isl_printer_print_str(p, "0");
286 return p;
289 static __isl_give isl_printer *print_affine(__isl_keep isl_basic_map *bmap,
290 __isl_keep isl_space *dim, __isl_take isl_printer *p, isl_int *c)
292 unsigned len = 1 + isl_basic_map_total_dim(bmap);
293 return print_affine_of_len(dim, NULL, p, c, len);
296 static int defining_equality(__isl_keep isl_basic_map *eq,
297 __isl_keep isl_space *dim, enum isl_dim_type type, int pos)
299 int i;
300 unsigned total;
302 if (!eq)
303 return -1;
305 pos += isl_space_offset(dim, type);
306 total = isl_basic_map_total_dim(eq);
308 for (i = 0; i < eq->n_eq; ++i) {
309 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
310 continue;
311 if (isl_int_is_one(eq->eq[i][1 + pos]))
312 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
313 return i;
316 return -1;
319 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
320 __isl_keep isl_aff *aff);
322 /* offset is the offset of local_dim inside global_type of global_dim.
324 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
325 __isl_keep isl_space *global_dim, enum isl_dim_type global_type,
326 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
327 int latex, __isl_keep isl_basic_map *eq,
328 __isl_keep isl_multi_aff *maff, int offset)
330 int i, j;
332 if (global_dim != local_dim && local_type == isl_dim_out)
333 offset += local_dim->n_in;
335 for (i = 0; i < isl_space_dim(local_dim, local_type); ++i) {
336 if (i)
337 p = isl_printer_print_str(p, ", ");
338 if (maff && global_type == isl_dim_out) {
339 p = print_aff_body(p, maff->p[offset + i]);
340 continue;
342 j = defining_equality(eq, global_dim, global_type, offset + i);
343 if (j >= 0) {
344 int pos = 1 + isl_space_offset(global_dim, global_type)
345 + offset + i;
346 p = print_affine_of_len(eq->dim, NULL,
347 p, eq->eq[j], pos);
348 } else {
349 p = print_name(global_dim, p, global_type, offset + i,
350 latex);
353 return p;
356 static __isl_give isl_printer *print_var_list(__isl_keep isl_space *dim,
357 __isl_take isl_printer *p, enum isl_dim_type type,
358 int latex, __isl_keep isl_basic_map *eq, __isl_keep isl_multi_aff *maff)
360 return print_nested_var_list(p, dim, type, dim, type, latex,
361 eq, maff, 0);
364 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
365 __isl_keep isl_space *global_dim, enum isl_dim_type global_type,
366 __isl_keep isl_space *local_dim,
367 int latex, __isl_keep isl_basic_map *eq,
368 __isl_keep isl_multi_aff *maff, int offset);
370 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
371 __isl_keep isl_space *global_dim, enum isl_dim_type global_type,
372 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
373 int latex, __isl_keep isl_basic_map *eq,
374 __isl_keep isl_multi_aff *maff, int offset)
376 const char *name = NULL;
377 unsigned n = isl_space_dim(local_dim, local_type);
378 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
379 name = isl_space_get_tuple_name(local_dim, local_type);
380 if (name) {
381 if (latex)
382 p = isl_printer_print_str(p, "\\mathrm{");
383 p = isl_printer_print_str(p, name);
384 if (latex)
385 p = isl_printer_print_str(p, "}");
388 if (!latex || n != 1 || name)
389 p = isl_printer_print_str(p, s_open_list[latex]);
390 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
391 local_dim->nested[local_type - isl_dim_in]) {
392 if (global_dim != local_dim && local_type == isl_dim_out)
393 offset += local_dim->n_in;
394 p = print_nested_map_dim(p, global_dim, global_type,
395 local_dim->nested[local_type - isl_dim_in],
396 latex, eq, maff, offset);
397 } else
398 p = print_nested_var_list(p, global_dim, global_type,
399 local_dim, local_type, latex,
400 eq, maff, offset);
401 if (!latex || n != 1 || name)
402 p = isl_printer_print_str(p, s_close_list[latex]);
403 return p;
406 static __isl_give isl_printer *print_tuple(__isl_keep isl_space *dim,
407 __isl_take isl_printer *p, enum isl_dim_type type,
408 int latex, __isl_keep isl_basic_map *eq, __isl_keep isl_multi_aff *maff)
410 return print_nested_tuple(p, dim, type, dim, type, latex, eq, maff, 0);
413 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
414 __isl_keep isl_space *global_dim, enum isl_dim_type global_type,
415 __isl_keep isl_space *local_dim,
416 int latex, __isl_keep isl_basic_map *eq,
417 __isl_keep isl_multi_aff *maff, int offset)
419 p = print_nested_tuple(p, global_dim, global_type,
420 local_dim, isl_dim_in, latex, eq, maff, offset);
421 p = isl_printer_print_str(p, s_to[latex]);
422 p = print_nested_tuple(p, global_dim, global_type,
423 local_dim, isl_dim_out, latex, eq, maff, offset);
425 return p;
428 static __isl_give isl_printer *print_space(__isl_keep isl_space *dim,
429 __isl_take isl_printer *p, int latex, int rational,
430 __isl_keep isl_basic_map *eq, __isl_keep isl_multi_aff *maff)
432 if (rational && !latex)
433 p = isl_printer_print_str(p, "rat: ");
434 if (isl_space_is_params(dim))
436 else if (isl_space_is_set(dim))
437 p = print_tuple(dim, p, isl_dim_set, latex, eq, maff);
438 else {
439 p = print_tuple(dim, p, isl_dim_in, latex, eq, maff);
440 p = isl_printer_print_str(p, s_to[latex]);
441 p = print_tuple(dim, p, isl_dim_out, latex, eq, maff);
444 return p;
447 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_space *dim,
448 __isl_take isl_printer *p)
450 if (isl_space_dim(dim, isl_dim_param) == 0)
451 return p;
453 p = isl_printer_start_line(p);
454 p = isl_printer_print_str(p, "symbolic ");
455 p = print_var_list(dim, p, isl_dim_param, 0, NULL, NULL);
456 p = isl_printer_print_str(p, ";");
457 p = isl_printer_end_line(p);
458 return p;
461 static __isl_give isl_printer *print_constraint(struct isl_basic_map *bmap,
462 __isl_keep isl_space *dim, __isl_take isl_printer *p,
463 isl_int *c, int last, const char *op, int first_constraint, int latex)
465 if (!first_constraint)
466 p = isl_printer_print_str(p, s_and[latex]);
468 isl_int_abs(c[last], c[last]);
470 p = print_term(dim, NULL, c[last], last, p, latex);
472 p = isl_printer_print_str(p, " ");
473 p = isl_printer_print_str(p, op);
474 p = isl_printer_print_str(p, " ");
476 isl_int_set_si(c[last], 0);
477 p = print_affine(bmap, dim, p, c);
479 return p;
482 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
483 __isl_keep isl_space *dim, __isl_take isl_printer *p, int latex)
485 int i;
486 struct isl_vec *c;
487 unsigned total = isl_basic_map_total_dim(bmap);
489 c = isl_vec_alloc(bmap->ctx, 1 + total);
490 if (!c)
491 goto error;
493 for (i = bmap->n_eq - 1; i >= 0; --i) {
494 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
495 if (l < 0) {
496 if (i != bmap->n_eq - 1)
497 p = isl_printer_print_str(p, s_and[latex]);
498 p = isl_printer_print_str(p, "0 = 0");
499 continue;
501 if (isl_int_is_neg(bmap->eq[i][l]))
502 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
503 else
504 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
505 p = print_constraint(bmap, dim, p, c->el, l,
506 "=", i == bmap->n_eq - 1, latex);
508 for (i = 0; i < bmap->n_ineq; ++i) {
509 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
510 int s;
511 const char *op;
512 if (l < 0)
513 continue;
514 s = isl_int_sgn(bmap->ineq[i][l]);
515 if (s < 0)
516 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
517 else
518 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
519 op = s < 0 ? s_le[latex] : s_ge[latex];
520 p = print_constraint(bmap, dim, p, c->el, l,
521 op, !bmap->n_eq && !i, latex);
524 isl_vec_free(c);
526 return p;
527 error:
528 isl_vec_free(c);
529 isl_printer_free(p);
530 return NULL;
533 static __isl_give isl_printer *print_omega_constraints(
534 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
536 if (bmap->n_eq + bmap->n_ineq == 0)
537 return p;
539 p = isl_printer_print_str(p, ": ");
540 if (bmap->n_div > 0) {
541 int i;
542 p = isl_printer_print_str(p, "exists (");
543 for (i = 0; i < bmap->n_div; ++i) {
544 if (i)
545 p = isl_printer_print_str(p, ", ");
546 p = print_name(bmap->dim, p, isl_dim_div, i, 0);
548 p = isl_printer_print_str(p, ": ");
550 p = print_constraints(bmap, bmap->dim, p, 0);
551 if (bmap->n_div > 0)
552 p = isl_printer_print_str(p, ")");
553 return p;
556 static __isl_give isl_printer *basic_map_print_omega(
557 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
559 p = isl_printer_print_str(p, "{ [");
560 p = print_var_list(bmap->dim, p, isl_dim_in, 0, NULL, NULL);
561 p = isl_printer_print_str(p, "] -> [");
562 p = print_var_list(bmap->dim, p, isl_dim_out, 0, NULL, NULL);
563 p = isl_printer_print_str(p, "] ");
564 p = print_omega_constraints(bmap, p);
565 p = isl_printer_print_str(p, " }");
566 return p;
569 static __isl_give isl_printer *basic_set_print_omega(
570 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
572 p = isl_printer_print_str(p, "{ [");
573 p = print_var_list(bset->dim, p, isl_dim_set, 0, NULL, NULL);
574 p = isl_printer_print_str(p, "] ");
575 p = print_omega_constraints((isl_basic_map *)bset, p);
576 p = isl_printer_print_str(p, " }");
577 return p;
580 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
581 __isl_take isl_printer *p)
583 int i;
585 for (i = 0; i < map->n; ++i) {
586 if (i)
587 p = isl_printer_print_str(p, " union ");
588 p = basic_map_print_omega(map->p[i], p);
590 return p;
593 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
594 __isl_take isl_printer *p)
596 int i;
598 for (i = 0; i < set->n; ++i) {
599 if (i)
600 p = isl_printer_print_str(p, " union ");
601 p = basic_set_print_omega(set->p[i], p);
603 return p;
606 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
607 __isl_keep isl_space *dim, __isl_take isl_printer *p, int latex)
609 if (bmap->n_div > 0) {
610 int i;
611 p = isl_printer_print_str(p, s_open_exists[latex]);
612 for (i = 0; i < bmap->n_div; ++i) {
613 if (i)
614 p = isl_printer_print_str(p, ", ");
615 p = print_name(dim, p, isl_dim_div, i, latex);
616 if (latex || isl_int_is_zero(bmap->div[i][0]))
617 continue;
618 p = isl_printer_print_str(p, " = [(");
619 p = print_affine(bmap, dim, p, bmap->div[i] + 1);
620 p = isl_printer_print_str(p, ")/");
621 p = isl_printer_print_isl_int(p, bmap->div[i][0]);
622 p = isl_printer_print_str(p, "]");
624 p = isl_printer_print_str(p, ": ");
627 p = print_constraints(bmap, dim, p, latex);
629 if (bmap->n_div > 0)
630 p = isl_printer_print_str(p, s_close_exists[latex]);
631 return p;
634 static __isl_give isl_printer *isl_basic_map_print_isl(
635 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
636 int latex)
638 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
639 if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
640 p = print_tuple(bmap->dim, p, isl_dim_param, latex, NULL, NULL);
641 p = isl_printer_print_str(p, " -> ");
643 p = isl_printer_print_str(p, "{ ");
644 p = print_space(bmap->dim, p, latex, rational, NULL, NULL);
645 p = isl_printer_print_str(p, " : ");
646 p = print_disjunct(bmap, bmap->dim, p, latex);
647 p = isl_printer_print_str(p, " }");
648 return p;
651 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
652 __isl_take isl_printer *p, int latex)
654 int i;
656 if (isl_map_plain_is_universe(map))
657 return p;
659 p = isl_printer_print_str(p, s_such_that[latex]);
660 if (map->n == 0)
661 p = isl_printer_print_str(p, "1 = 0");
662 for (i = 0; i < map->n; ++i) {
663 if (i)
664 p = isl_printer_print_str(p, s_or[latex]);
665 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
666 p = isl_printer_print_str(p, "(");
667 p = print_disjunct(map->p[i], map->dim, p, latex);
668 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
669 p = isl_printer_print_str(p, ")");
671 return p;
674 /* Print the disjuncts of a map (or set).
675 * If the map turns out to be a universal parameter domain, then
676 * we need to print the colon. Otherwise, the output looks identical
677 * to the empty set.
679 static __isl_give isl_printer *print_disjuncts_map(__isl_keep isl_map *map,
680 __isl_take isl_printer *p, int latex)
682 if (isl_map_plain_is_universe(map) && isl_space_is_params(map->dim))
683 return isl_printer_print_str(p, s_such_that[latex]);
684 else
685 return print_disjuncts(map, p, latex);
688 struct isl_aff_split {
689 isl_basic_map *aff;
690 isl_map *map;
693 static void free_split(__isl_take struct isl_aff_split *split, int n)
695 int i;
697 if (!split)
698 return;
700 for (i = 0; i < n; ++i) {
701 isl_basic_map_free(split[i].aff);
702 isl_map_free(split[i].map);
705 free(split);
708 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
710 int i, j;
711 unsigned nparam, n_in, n_out, total;
713 bmap = isl_basic_map_cow(bmap);
714 if (!bmap)
715 return NULL;
716 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
717 goto error;
719 nparam = isl_basic_map_dim(bmap, isl_dim_param);
720 n_in = isl_basic_map_dim(bmap, isl_dim_in);
721 n_out = isl_basic_map_dim(bmap, isl_dim_out);
722 total = isl_basic_map_dim(bmap, isl_dim_all);
723 for (i = bmap->n_eq - 1; i >= 0; --i) {
724 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
725 if (j >= nparam && j < nparam + n_in + n_out &&
726 (isl_int_is_one(bmap->eq[i][1 + j]) ||
727 isl_int_is_negone(bmap->eq[i][1 + j])))
728 continue;
729 if (isl_basic_map_drop_equality(bmap, i) < 0)
730 goto error;
733 bmap = isl_basic_map_finalize(bmap);
735 return bmap;
736 error:
737 isl_basic_map_free(bmap);
738 return NULL;
741 static int aff_split_cmp(const void *p1, const void *p2)
743 const struct isl_aff_split *s1, *s2;
744 s1 = (const struct isl_aff_split *) p1;
745 s2 = (const struct isl_aff_split *) p2;
747 return isl_basic_map_plain_cmp(s1->aff, s2->aff);
750 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
751 __isl_keep isl_basic_map *aff)
753 int i, j;
754 unsigned total;
756 if (!bmap || !aff)
757 goto error;
759 total = isl_space_dim(bmap->dim, isl_dim_all);
761 for (i = bmap->n_eq - 1; i >= 0; --i) {
762 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
763 bmap->n_div) != -1)
764 continue;
765 for (j = 0; j < aff->n_eq; ++j) {
766 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
767 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
768 continue;
769 if (isl_basic_map_drop_equality(bmap, i) < 0)
770 goto error;
771 break;
775 return bmap;
776 error:
777 isl_basic_map_free(bmap);
778 return NULL;
781 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
783 int i, n;
784 struct isl_aff_split *split;
785 isl_ctx *ctx;
787 ctx = isl_map_get_ctx(map);
788 split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
789 if (!split)
790 return NULL;
792 for (i = 0; i < map->n; ++i) {
793 isl_basic_map *bmap;
794 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
795 bmap = isl_basic_map_copy(map->p[i]);
796 bmap = isl_basic_map_cow(bmap);
797 bmap = drop_aff(bmap, split[i].aff);
798 split[i].map = isl_map_from_basic_map(bmap);
799 if (!split[i].aff || !split[i].map)
800 goto error;
803 qsort(split, map->n, sizeof(struct isl_aff_split), &aff_split_cmp);
805 n = map->n;
806 for (i = n - 1; i >= 1; --i) {
807 if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
808 split[i].aff))
809 continue;
810 isl_basic_map_free(split[i].aff);
811 split[i - 1].map = isl_map_union(split[i - 1].map,
812 split[i].map);
813 if (i != n - 1)
814 split[i] = split[n - 1];
815 split[n - 1].aff = NULL;
816 split[n - 1].map = NULL;
817 --n;
820 return split;
821 error:
822 free_split(split, map->n);
823 return NULL;
826 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
827 struct isl_aff_split *split, int n)
829 int i;
830 int rational;
832 for (i = 0; i < n; ++i) {
833 isl_space *dim;
835 if (!split[i].map)
836 break;
837 dim = split[i].map->dim;
838 rational = split[i].map->n > 0 &&
839 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
840 if (i)
841 p = isl_printer_print_str(p, "; ");
842 p = print_space(dim, p, 0, rational, split[i].aff, NULL);
843 p = print_disjuncts_map(split[i].map, p, 0);
846 return p;
849 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
850 __isl_take isl_printer *p)
852 struct isl_aff_split *split = NULL;
853 int rational;
855 if (map->n > 0)
856 split = split_aff(map);
857 if (split) {
858 p = print_split_map(p, split, map->n);
859 } else {
860 rational = map->n > 0 &&
861 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
862 p = print_space(map->dim, p, 0, rational, NULL, NULL);
863 p = print_disjuncts_map(map, p, 0);
865 free_split(split, map->n);
866 return p;
869 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
870 __isl_take isl_printer *p)
872 if (isl_map_dim(map, isl_dim_param) > 0) {
873 p = print_tuple(map->dim, p, isl_dim_param, 0, NULL, NULL);
874 p = isl_printer_print_str(p, s_to[0]);
876 p = isl_printer_print_str(p, s_open_set[0]);
877 p = isl_map_print_isl_body(map, p);
878 p = isl_printer_print_str(p, s_close_set[0]);
879 return p;
882 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
883 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff)
885 if (isl_map_dim(map, isl_dim_param) > 0) {
886 p = print_tuple(map->dim, p, isl_dim_param, 1, NULL, NULL);
887 p = isl_printer_print_str(p, s_to[1]);
889 p = isl_printer_print_str(p, s_open_set[1]);
890 p = print_space(map->dim, p, 1, 0, aff, NULL);
891 p = print_disjuncts_map(map, p, 1);
892 p = isl_printer_print_str(p, s_close_set[1]);
894 return p;
897 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
898 __isl_take isl_printer *p)
900 int i;
901 struct isl_aff_split *split = NULL;
903 if (map->n > 0)
904 split = split_aff(map);
906 if (!split)
907 return print_latex_map(map, p, NULL);
909 for (i = 0; i < map->n; ++i) {
910 if (!split[i].map)
911 break;
912 if (i)
913 p = isl_printer_print_str(p, " \\cup ");
914 p = print_latex_map(split[i].map, p, split[i].aff);
917 free_split(split, map->n);
918 return p;
921 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
922 __isl_keep isl_basic_map *bmap)
924 if (!p || !bmap)
925 goto error;
926 if (p->output_format == ISL_FORMAT_ISL)
927 return isl_basic_map_print_isl(bmap, p, 0);
928 else if (p->output_format == ISL_FORMAT_OMEGA)
929 return basic_map_print_omega(bmap, p);
930 isl_assert(bmap->ctx, 0, goto error);
931 error:
932 isl_printer_free(p);
933 return NULL;
936 void isl_basic_map_print(__isl_keep isl_basic_map *bmap, FILE *out, int indent,
937 const char *prefix, const char *suffix, unsigned output_format)
939 isl_printer *printer;
941 if (!bmap)
942 return;
944 printer = isl_printer_to_file(bmap->ctx, out);
945 printer = isl_printer_set_indent(printer, indent);
946 printer = isl_printer_set_prefix(printer, prefix);
947 printer = isl_printer_set_suffix(printer, suffix);
948 printer = isl_printer_set_output_format(printer, output_format);
949 isl_printer_print_basic_map(printer, bmap);
951 isl_printer_free(printer);
954 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
955 __isl_keep isl_basic_set *bset)
957 if (!p || !bset)
958 goto error;
960 if (p->output_format == ISL_FORMAT_ISL)
961 return isl_basic_map_print_isl(bset, p, 0);
962 else if (p->output_format == ISL_FORMAT_POLYLIB)
963 return isl_basic_set_print_polylib(bset, p, 0);
964 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
965 return isl_basic_set_print_polylib(bset, p, 1);
966 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
967 return bset_print_constraints_polylib(bset, p);
968 else if (p->output_format == ISL_FORMAT_OMEGA)
969 return basic_set_print_omega(bset, p);
970 isl_assert(p->ctx, 0, goto error);
971 error:
972 isl_printer_free(p);
973 return NULL;
976 void isl_basic_set_print(struct isl_basic_set *bset, FILE *out, int indent,
977 const char *prefix, const char *suffix, unsigned output_format)
979 isl_printer *printer;
981 if (!bset)
982 return;
984 printer = isl_printer_to_file(bset->ctx, out);
985 printer = isl_printer_set_indent(printer, indent);
986 printer = isl_printer_set_prefix(printer, prefix);
987 printer = isl_printer_set_suffix(printer, suffix);
988 printer = isl_printer_set_output_format(printer, output_format);
989 isl_printer_print_basic_set(printer, bset);
991 isl_printer_free(printer);
994 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
995 __isl_keep isl_set *set)
997 if (!p || !set)
998 goto error;
999 if (p->output_format == ISL_FORMAT_ISL)
1000 return isl_map_print_isl((isl_map *)set, p);
1001 else if (p->output_format == ISL_FORMAT_POLYLIB)
1002 return isl_set_print_polylib(set, p, 0);
1003 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1004 return isl_set_print_polylib(set, p, 1);
1005 else if (p->output_format == ISL_FORMAT_OMEGA)
1006 return isl_set_print_omega(set, p);
1007 else if (p->output_format == ISL_FORMAT_LATEX)
1008 return isl_map_print_latex((isl_map *)set, p);
1009 isl_assert(set->ctx, 0, goto error);
1010 error:
1011 isl_printer_free(p);
1012 return NULL;
1015 void isl_set_print(struct isl_set *set, FILE *out, int indent,
1016 unsigned output_format)
1018 isl_printer *printer;
1020 if (!set)
1021 return;
1023 printer = isl_printer_to_file(set->ctx, out);
1024 printer = isl_printer_set_indent(printer, indent);
1025 printer = isl_printer_set_output_format(printer, output_format);
1026 printer = isl_printer_print_set(printer, set);
1028 isl_printer_free(printer);
1031 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1032 __isl_keep isl_map *map)
1034 if (!p || !map)
1035 goto error;
1037 if (p->output_format == ISL_FORMAT_ISL)
1038 return isl_map_print_isl(map, p);
1039 else if (p->output_format == ISL_FORMAT_POLYLIB)
1040 return isl_map_print_polylib(map, p, 0);
1041 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1042 return isl_map_print_polylib(map, p, 1);
1043 else if (p->output_format == ISL_FORMAT_OMEGA)
1044 return isl_map_print_omega(map, p);
1045 else if (p->output_format == ISL_FORMAT_LATEX)
1046 return isl_map_print_latex(map, p);
1047 isl_assert(map->ctx, 0, goto error);
1048 error:
1049 isl_printer_free(p);
1050 return NULL;
1053 struct isl_union_print_data {
1054 isl_printer *p;
1055 int first;
1058 static int print_map_body(__isl_take isl_map *map, void *user)
1060 struct isl_union_print_data *data;
1061 data = (struct isl_union_print_data *)user;
1063 if (!data->first)
1064 data->p = isl_printer_print_str(data->p, "; ");
1065 data->first = 0;
1067 data->p = isl_map_print_isl_body(map, data->p);
1068 isl_map_free(map);
1070 return 0;
1073 static __isl_give isl_printer *isl_union_map_print_isl(
1074 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1076 struct isl_union_print_data data = { p, 1 };
1077 isl_space *dim;
1078 dim = isl_union_map_get_space(umap);
1079 if (isl_space_dim(dim, isl_dim_param) > 0) {
1080 p = print_tuple(dim, p, isl_dim_param, 0, NULL, NULL);
1081 p = isl_printer_print_str(p, s_to[0]);
1083 isl_space_free(dim);
1084 p = isl_printer_print_str(p, s_open_set[0]);
1085 isl_union_map_foreach_map(umap, &print_map_body, &data);
1086 p = data.p;
1087 p = isl_printer_print_str(p, s_close_set[0]);
1088 return p;
1091 static int print_latex_map_body(__isl_take isl_map *map, void *user)
1093 struct isl_union_print_data *data;
1094 data = (struct isl_union_print_data *)user;
1096 if (!data->first)
1097 data->p = isl_printer_print_str(data->p, " \\cup ");
1098 data->first = 0;
1100 data->p = isl_map_print_latex(map, data->p);
1101 isl_map_free(map);
1103 return 0;
1106 static __isl_give isl_printer *isl_union_map_print_latex(
1107 __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1109 struct isl_union_print_data data = { p, 1 };
1110 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1111 p = data.p;
1112 return p;
1115 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1116 __isl_keep isl_union_map *umap)
1118 if (!p || !umap)
1119 goto error;
1121 if (p->output_format == ISL_FORMAT_ISL)
1122 return isl_union_map_print_isl(umap, p);
1123 if (p->output_format == ISL_FORMAT_LATEX)
1124 return isl_union_map_print_latex(umap, p);
1126 isl_die(p->ctx, isl_error_invalid,
1127 "invalid output format for isl_union_map", goto error);
1128 error:
1129 isl_printer_free(p);
1130 return NULL;
1133 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1134 __isl_keep isl_union_set *uset)
1136 if (!p || !uset)
1137 goto error;
1139 if (p->output_format == ISL_FORMAT_ISL)
1140 return isl_union_map_print_isl((isl_union_map *)uset, p);
1141 if (p->output_format == ISL_FORMAT_LATEX)
1142 return isl_union_map_print_latex((isl_union_map *)uset, p);
1144 isl_die(p->ctx, isl_error_invalid,
1145 "invalid output format for isl_union_set", goto error);
1146 error:
1147 isl_printer_free(p);
1148 return NULL;
1151 void isl_map_print(__isl_keep isl_map *map, FILE *out, int indent,
1152 unsigned output_format)
1154 isl_printer *printer;
1156 if (!map)
1157 return;
1159 printer = isl_printer_to_file(map->ctx, out);
1160 printer = isl_printer_set_indent(printer, indent);
1161 printer = isl_printer_set_output_format(printer, output_format);
1162 printer = isl_printer_print_map(printer, map);
1164 isl_printer_free(printer);
1167 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
1169 int i;
1170 int n;
1172 for (i = 0, n = 0; i < rec->n; ++i)
1173 if (!isl_upoly_is_zero(rec->p[i]))
1174 ++n;
1176 return n;
1179 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
1180 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
1182 int c = p->output_format == ISL_FORMAT_C;
1183 p = isl_printer_print_str(p, c ? "floord(" : "[(");
1184 p = print_affine_of_len(dim, div, p,
1185 div->row[pos] + 1, div->n_col - 1);
1186 p = isl_printer_print_str(p, c ? ", " : ")/");
1187 p = isl_printer_print_isl_int(p, div->row[pos][0]);
1188 p = isl_printer_print_str(p, c ? ")" : "]");
1189 return p;
1192 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
1193 __isl_take isl_printer *p, int first)
1195 struct isl_upoly_cst *cst;
1196 int neg;
1198 cst = isl_upoly_as_cst(up);
1199 if (!cst)
1200 goto error;
1201 neg = !first && isl_int_is_neg(cst->n);
1202 if (!first)
1203 p = isl_printer_print_str(p, neg ? " - " : " + ");
1204 if (neg)
1205 isl_int_neg(cst->n, cst->n);
1206 if (isl_int_is_zero(cst->d)) {
1207 int sgn = isl_int_sgn(cst->n);
1208 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1209 sgn == 0 ? "NaN" : "infty");
1210 } else
1211 p = isl_printer_print_isl_int(p, cst->n);
1212 if (neg)
1213 isl_int_neg(cst->n, cst->n);
1214 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1215 p = isl_printer_print_str(p, "/");
1216 p = isl_printer_print_isl_int(p, cst->d);
1218 return p;
1219 error:
1220 isl_printer_free(p);
1221 return NULL;
1224 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1225 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var)
1227 unsigned total;
1229 total = isl_space_dim(dim, isl_dim_all);
1230 if (var < total)
1231 p = print_term(dim, NULL, dim->ctx->one, 1 + var, p, 0);
1232 else
1233 p = print_div(dim, div, var - total, p);
1234 return p;
1237 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1238 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var, int exp)
1240 p = print_base(p, dim, div, var);
1241 if (exp == 1)
1242 return p;
1243 if (p->output_format == ISL_FORMAT_C) {
1244 int i;
1245 for (i = 1; i < exp; ++i) {
1246 p = isl_printer_print_str(p, "*");
1247 p = print_base(p, dim, div, var);
1249 } else {
1250 p = isl_printer_print_str(p, "^");
1251 p = isl_printer_print_int(p, exp);
1253 return p;
1256 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1257 __isl_keep isl_space *dim, __isl_keep isl_mat *div,
1258 __isl_take isl_printer *p, int outer)
1260 int i, n, first, print_parens;
1261 struct isl_upoly_rec *rec;
1263 if (!p || !up || !dim || !div)
1264 goto error;
1266 if (isl_upoly_is_cst(up))
1267 return upoly_print_cst(up, p, 1);
1269 rec = isl_upoly_as_rec(up);
1270 if (!rec)
1271 goto error;
1272 n = upoly_rec_n_non_zero(rec);
1273 print_parens = n > 1 ||
1274 (outer && rec->up.var >= isl_space_dim(dim, isl_dim_all));
1275 if (print_parens)
1276 p = isl_printer_print_str(p, "(");
1277 for (i = 0, first = 1; i < rec->n; ++i) {
1278 if (isl_upoly_is_zero(rec->p[i]))
1279 continue;
1280 if (isl_upoly_is_negone(rec->p[i])) {
1281 if (!i)
1282 p = isl_printer_print_str(p, "-1");
1283 else if (first)
1284 p = isl_printer_print_str(p, "-");
1285 else
1286 p = isl_printer_print_str(p, " - ");
1287 } else if (isl_upoly_is_cst(rec->p[i]) &&
1288 !isl_upoly_is_one(rec->p[i]))
1289 p = upoly_print_cst(rec->p[i], p, first);
1290 else {
1291 if (!first)
1292 p = isl_printer_print_str(p, " + ");
1293 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1294 p = upoly_print(rec->p[i], dim, div, p, 0);
1296 first = 0;
1297 if (i == 0)
1298 continue;
1299 if (!isl_upoly_is_one(rec->p[i]) &&
1300 !isl_upoly_is_negone(rec->p[i]))
1301 p = isl_printer_print_str(p, " * ");
1302 p = print_pow(p, dim, div, rec->up.var, i);
1304 if (print_parens)
1305 p = isl_printer_print_str(p, ")");
1306 return p;
1307 error:
1308 isl_printer_free(p);
1309 return NULL;
1312 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1313 __isl_keep isl_qpolynomial *qp)
1315 if (!p || !qp)
1316 goto error;
1317 p = upoly_print(qp->upoly, qp->dim, qp->div, p, 1);
1318 return p;
1319 error:
1320 isl_printer_free(p);
1321 return NULL;
1324 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1325 __isl_keep isl_qpolynomial *qp)
1327 if (!p || !qp)
1328 goto error;
1330 if (isl_space_dim(qp->dim, isl_dim_param) > 0) {
1331 p = print_tuple(qp->dim, p, isl_dim_param, 0, NULL, NULL);
1332 p = isl_printer_print_str(p, " -> ");
1334 p = isl_printer_print_str(p, "{ ");
1335 if (!isl_space_is_params(qp->dim)) {
1336 p = print_space(qp->dim, p, 0, 0, NULL, NULL);
1337 p = isl_printer_print_str(p, " -> ");
1339 p = print_qpolynomial(p, qp);
1340 p = isl_printer_print_str(p, " }");
1341 return p;
1342 error:
1343 isl_printer_free(p);
1344 return NULL;
1347 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1348 __isl_keep isl_space *dim, __isl_keep isl_qpolynomial *qp)
1350 isl_int den;
1352 isl_int_init(den);
1353 isl_qpolynomial_get_den(qp, &den);
1354 if (!isl_int_is_one(den)) {
1355 isl_qpolynomial *f;
1356 p = isl_printer_print_str(p, "(");
1357 qp = isl_qpolynomial_copy(qp);
1358 f = isl_qpolynomial_rat_cst_on_domain(isl_space_copy(qp->dim),
1359 den, qp->dim->ctx->one);
1360 qp = isl_qpolynomial_mul(qp, f);
1362 if (qp)
1363 p = upoly_print(qp->upoly, dim, qp->div, p, 0);
1364 if (!isl_int_is_one(den)) {
1365 p = isl_printer_print_str(p, ")/");
1366 p = isl_printer_print_isl_int(p, den);
1367 isl_qpolynomial_free(qp);
1369 isl_int_clear(den);
1370 return p;
1373 __isl_give isl_printer *isl_printer_print_qpolynomial(
1374 __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1376 if (!p || !qp)
1377 goto error;
1379 if (p->output_format == ISL_FORMAT_ISL)
1380 return print_qpolynomial_isl(p, qp);
1381 else if (p->output_format == ISL_FORMAT_C)
1382 return print_qpolynomial_c(p, qp->dim, qp);
1383 else
1384 isl_die(qp->dim->ctx, isl_error_unsupported,
1385 "output format not supported for isl_qpolynomials",
1386 goto error);
1387 error:
1388 isl_printer_free(p);
1389 return NULL;
1392 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1393 unsigned output_format)
1395 isl_printer *p;
1397 if (!qp)
1398 return;
1400 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1401 p = isl_printer_to_file(qp->dim->ctx, out);
1402 p = isl_printer_print_qpolynomial(p, qp);
1403 isl_printer_free(p);
1406 static __isl_give isl_printer *qpolynomial_fold_print(
1407 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1409 int i;
1411 if (fold->type == isl_fold_min)
1412 p = isl_printer_print_str(p, "min");
1413 else if (fold->type == isl_fold_max)
1414 p = isl_printer_print_str(p, "max");
1415 p = isl_printer_print_str(p, "(");
1416 for (i = 0; i < fold->n; ++i) {
1417 if (i)
1418 p = isl_printer_print_str(p, ", ");
1419 p = print_qpolynomial(p, fold->qp[i]);
1421 p = isl_printer_print_str(p, ")");
1422 return p;
1425 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1426 FILE *out, unsigned output_format)
1428 isl_printer *p;
1430 if (!fold)
1431 return;
1433 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1435 p = isl_printer_to_file(fold->dim->ctx, out);
1436 p = isl_printer_print_qpolynomial_fold(p, fold);
1438 isl_printer_free(p);
1441 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1442 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1444 int i = 0;
1446 for (i = 0; i < pwqp->n; ++i) {
1447 if (i)
1448 p = isl_printer_print_str(p, "; ");
1449 if (!isl_space_is_params(pwqp->p[i].set->dim)) {
1450 p = print_space(pwqp->p[i].set->dim, p, 0, 0, NULL, NULL);
1451 p = isl_printer_print_str(p, " -> ");
1453 p = print_qpolynomial(p, pwqp->p[i].qp);
1454 p = print_disjuncts((isl_map *)pwqp->p[i].set, p, 0);
1457 return p;
1460 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1461 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1463 if (!p || !pwqp)
1464 goto error;
1466 if (isl_space_dim(pwqp->dim, isl_dim_param) > 0) {
1467 p = print_tuple(pwqp->dim, p, isl_dim_param, 0, NULL, NULL);
1468 p = isl_printer_print_str(p, " -> ");
1470 p = isl_printer_print_str(p, "{ ");
1471 if (pwqp->n == 0) {
1472 if (!isl_space_is_set(pwqp->dim)) {
1473 p = print_tuple(pwqp->dim, p, isl_dim_in, 0, NULL, NULL);
1474 p = isl_printer_print_str(p, " -> ");
1476 p = isl_printer_print_str(p, "0");
1478 p = isl_pwqp_print_isl_body(p, pwqp);
1479 p = isl_printer_print_str(p, " }");
1480 return p;
1481 error:
1482 isl_printer_free(p);
1483 return NULL;
1486 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
1487 unsigned output_format)
1489 isl_printer *p;
1491 if (!pwqp)
1492 return;
1494 p = isl_printer_to_file(pwqp->dim->ctx, out);
1495 p = isl_printer_set_output_format(p, output_format);
1496 p = isl_printer_print_pw_qpolynomial(p, pwqp);
1498 isl_printer_free(p);
1501 static __isl_give isl_printer *isl_pwf_print_isl_body(
1502 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1504 int i = 0;
1506 for (i = 0; i < pwf->n; ++i) {
1507 if (i)
1508 p = isl_printer_print_str(p, "; ");
1509 if (!isl_space_is_params(pwf->p[i].set->dim)) {
1510 p = print_space(pwf->p[i].set->dim, p, 0, 0, NULL, NULL);
1511 p = isl_printer_print_str(p, " -> ");
1513 p = qpolynomial_fold_print(pwf->p[i].fold, p);
1514 p = print_disjuncts((isl_map *)pwf->p[i].set, p, 0);
1517 return p;
1520 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
1521 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1523 if (isl_space_dim(pwf->dim, isl_dim_param) > 0) {
1524 p = print_tuple(pwf->dim, p, isl_dim_param, 0, NULL, NULL);
1525 p = isl_printer_print_str(p, " -> ");
1527 p = isl_printer_print_str(p, "{ ");
1528 if (pwf->n == 0) {
1529 if (!isl_space_is_set(pwf->dim)) {
1530 p = print_tuple(pwf->dim, p, isl_dim_in, 0, NULL, NULL);
1531 p = isl_printer_print_str(p, " -> ");
1533 p = isl_printer_print_str(p, "0");
1535 p = isl_pwf_print_isl_body(p, pwf);
1536 p = isl_printer_print_str(p, " }");
1537 return p;
1540 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1541 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c);
1543 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1544 __isl_keep isl_space *dim,
1545 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1547 if (type == isl_dim_div) {
1548 p = isl_printer_print_str(p, "floord(");
1549 p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
1550 p = isl_printer_print_str(p, ", ");
1551 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1552 p = isl_printer_print_str(p, ")");
1553 } else {
1554 const char *name;
1556 name = isl_space_get_dim_name(dim, type, pos);
1557 if (!name)
1558 name = "UNNAMED";
1559 p = isl_printer_print_str(p, name);
1561 return p;
1564 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1565 __isl_keep isl_space *dim,
1566 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1568 enum isl_dim_type type;
1570 if (pos == 0)
1571 return isl_printer_print_isl_int(p, c);
1573 if (isl_int_is_one(c))
1575 else if (isl_int_is_negone(c))
1576 p = isl_printer_print_str(p, "-");
1577 else {
1578 p = isl_printer_print_isl_int(p, c);
1579 p = isl_printer_print_str(p, "*");
1581 type = pos2type(dim, &pos);
1582 p = print_name_c(p, dim, bset, type, pos);
1583 return p;
1586 static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
1587 __isl_keep isl_space *dim,
1588 __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
1590 int i;
1591 int first;
1593 for (i = 0, first = 1; i < len; ++i) {
1594 int flip = 0;
1595 if (isl_int_is_zero(c[i]))
1596 continue;
1597 if (!first) {
1598 if (isl_int_is_neg(c[i])) {
1599 flip = 1;
1600 isl_int_neg(c[i], c[i]);
1601 p = isl_printer_print_str(p, " - ");
1602 } else
1603 p = isl_printer_print_str(p, " + ");
1605 first = 0;
1606 p = print_term_c(p, dim, bset, c[i], i);
1607 if (flip)
1608 isl_int_neg(c[i], c[i]);
1610 if (first)
1611 p = isl_printer_print_str(p, "0");
1612 return p;
1615 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1616 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c)
1618 unsigned len = 1 + isl_basic_set_total_dim(bset);
1619 return print_partial_affine_c(p, dim, bset, c, len);
1622 /* We skip the constraint if it is implied by the div expression.
1624 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1625 __isl_keep isl_space *dim,
1626 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int first)
1628 unsigned o_div;
1629 unsigned n_div;
1630 int div;
1632 o_div = isl_basic_set_offset(bset, isl_dim_div);
1633 n_div = isl_basic_set_dim(bset, isl_dim_div);
1634 div = isl_seq_last_non_zero(c + o_div, n_div);
1635 if (div >= 0 && isl_basic_set_is_div_constraint(bset, c, div))
1636 return p;
1638 if (!first)
1639 p = isl_printer_print_str(p, " && ");
1641 p = print_affine_c(p, dim, bset, c);
1642 p = isl_printer_print_str(p, " ");
1643 p = isl_printer_print_str(p, op);
1644 p = isl_printer_print_str(p, " 0");
1645 return p;
1648 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1649 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset)
1651 int i, j;
1652 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
1653 unsigned total = isl_basic_set_total_dim(bset) - n_div;
1655 for (i = 0; i < bset->n_eq; ++i) {
1656 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
1657 if (j < 0)
1658 p = print_constraint_c(p, dim, bset,
1659 bset->eq[i], "==", !i);
1660 else {
1661 if (i)
1662 p = isl_printer_print_str(p, " && ");
1663 p = isl_printer_print_str(p, "(");
1664 p = print_partial_affine_c(p, dim, bset, bset->eq[i],
1665 1 + total + j);
1666 p = isl_printer_print_str(p, ") % ");
1667 p = isl_printer_print_isl_int(p,
1668 bset->eq[i][1 + total + j]);
1669 p = isl_printer_print_str(p, " == 0");
1672 for (i = 0; i < bset->n_ineq; ++i)
1673 p = print_constraint_c(p, dim, bset, bset->ineq[i], ">=",
1674 !bset->n_eq && !i);
1675 return p;
1678 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
1679 __isl_keep isl_space *dim, __isl_keep isl_set *set)
1681 int i;
1683 if (set->n == 0)
1684 p = isl_printer_print_str(p, "0");
1686 for (i = 0; i < set->n; ++i) {
1687 if (i)
1688 p = isl_printer_print_str(p, " || ");
1689 if (set->n > 1)
1690 p = isl_printer_print_str(p, "(");
1691 p = print_basic_set_c(p, dim, set->p[i]);
1692 if (set->n > 1)
1693 p = isl_printer_print_str(p, ")");
1695 return p;
1698 static __isl_give isl_printer *print_pw_qpolynomial_c(
1699 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1701 int i;
1703 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set))
1704 return print_qpolynomial_c(p, pwqp->dim, pwqp->p[0].qp);
1706 for (i = 0; i < pwqp->n; ++i) {
1707 p = isl_printer_print_str(p, "(");
1708 p = print_set_c(p, pwqp->dim, pwqp->p[i].set);
1709 p = isl_printer_print_str(p, ") ? (");
1710 p = print_qpolynomial_c(p, pwqp->dim, pwqp->p[i].qp);
1711 p = isl_printer_print_str(p, ") : ");
1714 p = isl_printer_print_str(p, "0");
1715 return p;
1718 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
1719 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1721 if (!p || !pwqp)
1722 goto error;
1724 if (p->output_format == ISL_FORMAT_ISL)
1725 return print_pw_qpolynomial_isl(p, pwqp);
1726 else if (p->output_format == ISL_FORMAT_C)
1727 return print_pw_qpolynomial_c(p, pwqp);
1728 isl_assert(p->ctx, 0, goto error);
1729 error:
1730 isl_printer_free(p);
1731 return NULL;
1734 static int print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1736 struct isl_union_print_data *data;
1737 data = (struct isl_union_print_data *)user;
1739 if (!data->first)
1740 data->p = isl_printer_print_str(data->p, "; ");
1741 data->first = 0;
1743 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
1744 isl_pw_qpolynomial_free(pwqp);
1746 return 0;
1749 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
1750 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1752 struct isl_union_print_data data = { p, 1 };
1753 isl_space *dim;
1754 dim = isl_union_pw_qpolynomial_get_space(upwqp);
1755 if (isl_space_dim(dim, isl_dim_param) > 0) {
1756 p = print_tuple(dim, p, isl_dim_param, 0, NULL, NULL);
1757 p = isl_printer_print_str(p, " -> ");
1759 isl_space_free(dim);
1760 p = isl_printer_print_str(p, "{ ");
1761 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
1762 &data);
1763 p = data.p;
1764 p = isl_printer_print_str(p, " }");
1765 return p;
1768 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
1769 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1771 if (!p || !upwqp)
1772 goto error;
1774 if (p->output_format == ISL_FORMAT_ISL)
1775 return print_union_pw_qpolynomial_isl(p, upwqp);
1776 isl_die(p->ctx, isl_error_invalid,
1777 "invalid output format for isl_union_pw_qpolynomial",
1778 goto error);
1779 error:
1780 isl_printer_free(p);
1781 return NULL;
1784 static __isl_give isl_printer *print_qpolynomial_fold_c(
1785 __isl_take isl_printer *p, __isl_keep isl_space *dim,
1786 __isl_keep isl_qpolynomial_fold *fold)
1788 int i;
1790 for (i = 0; i < fold->n - 1; ++i)
1791 if (fold->type == isl_fold_min)
1792 p = isl_printer_print_str(p, "min(");
1793 else if (fold->type == isl_fold_max)
1794 p = isl_printer_print_str(p, "max(");
1796 for (i = 0; i < fold->n; ++i) {
1797 if (i)
1798 p = isl_printer_print_str(p, ", ");
1799 p = print_qpolynomial_c(p, dim, fold->qp[i]);
1800 if (i)
1801 p = isl_printer_print_str(p, ")");
1803 return p;
1806 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
1807 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
1809 if (!p || !fold)
1810 goto error;
1811 if (p->output_format == ISL_FORMAT_ISL)
1812 return qpolynomial_fold_print(fold, p);
1813 else if (p->output_format == ISL_FORMAT_C)
1814 return print_qpolynomial_fold_c(p, fold->dim, fold);
1815 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
1816 goto error);
1817 error:
1818 isl_printer_free(p);
1819 return NULL;
1822 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
1823 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1825 int i;
1827 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set))
1828 return print_qpolynomial_fold_c(p, pwf->dim, pwf->p[0].fold);
1830 for (i = 0; i < pwf->n; ++i) {
1831 p = isl_printer_print_str(p, "(");
1832 p = print_set_c(p, pwf->dim, pwf->p[i].set);
1833 p = isl_printer_print_str(p, ") ? (");
1834 p = print_qpolynomial_fold_c(p, pwf->dim, pwf->p[i].fold);
1835 p = isl_printer_print_str(p, ") : ");
1838 p = isl_printer_print_str(p, "0");
1839 return p;
1842 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
1843 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1845 if (!p || !pwf)
1846 goto error;
1848 if (p->output_format == ISL_FORMAT_ISL)
1849 return print_pw_qpolynomial_fold_isl(p, pwf);
1850 else if (p->output_format == ISL_FORMAT_C)
1851 return print_pw_qpolynomial_fold_c(p, pwf);
1852 isl_assert(p->ctx, 0, goto error);
1853 error:
1854 isl_printer_free(p);
1855 return NULL;
1858 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
1859 FILE *out, unsigned output_format)
1861 isl_printer *p;
1863 if (!pwf)
1864 return;
1866 p = isl_printer_to_file(pwf->dim->ctx, out);
1867 p = isl_printer_set_output_format(p, output_format);
1868 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
1870 isl_printer_free(p);
1873 static int print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf, void *user)
1875 struct isl_union_print_data *data;
1876 data = (struct isl_union_print_data *)user;
1878 if (!data->first)
1879 data->p = isl_printer_print_str(data->p, "; ");
1880 data->first = 0;
1882 data->p = isl_pwf_print_isl_body(data->p, pwf);
1883 isl_pw_qpolynomial_fold_free(pwf);
1885 return 0;
1888 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
1889 __isl_take isl_printer *p,
1890 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1892 struct isl_union_print_data data = { p, 1 };
1893 isl_space *dim;
1894 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
1895 if (isl_space_dim(dim, isl_dim_param) > 0) {
1896 p = print_tuple(dim, p, isl_dim_param, 0, NULL, NULL);
1897 p = isl_printer_print_str(p, " -> ");
1899 isl_space_free(dim);
1900 p = isl_printer_print_str(p, "{ ");
1901 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
1902 &print_pwf_body, &data);
1903 p = data.p;
1904 p = isl_printer_print_str(p, " }");
1905 return p;
1908 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
1909 __isl_take isl_printer *p,
1910 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1912 if (!p || !upwf)
1913 goto error;
1915 if (p->output_format == ISL_FORMAT_ISL)
1916 return print_union_pw_qpolynomial_fold_isl(p, upwf);
1917 isl_die(p->ctx, isl_error_invalid,
1918 "invalid output format for isl_union_pw_qpolynomial_fold",
1919 goto error);
1920 error:
1921 isl_printer_free(p);
1922 return NULL;
1925 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
1926 __isl_keep isl_constraint *c)
1928 isl_basic_map *bmap;
1930 if (!p || !c)
1931 goto error;
1933 bmap = isl_basic_map_from_constraint(isl_constraint_copy(c));
1934 p = isl_printer_print_basic_map(p, bmap);
1935 isl_basic_map_free(bmap);
1936 return p;
1937 error:
1938 isl_printer_free(p);
1939 return NULL;
1942 static __isl_give isl_printer *isl_printer_print_space_isl(
1943 __isl_take isl_printer *p, __isl_keep isl_space *dim)
1945 if (!dim)
1946 goto error;
1948 if (isl_space_dim(dim, isl_dim_param) > 0) {
1949 p = print_tuple(dim, p, isl_dim_param, 0, NULL, NULL);
1950 p = isl_printer_print_str(p, " -> ");
1953 p = isl_printer_print_str(p, "{ ");
1954 if (isl_space_is_params(dim))
1955 p = isl_printer_print_str(p, s_such_that[0]);
1956 else
1957 p = print_space(dim, p, 0, 0, NULL, NULL);
1958 p = isl_printer_print_str(p, " }");
1960 return p;
1961 error:
1962 isl_printer_free(p);
1963 return NULL;
1966 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
1967 __isl_keep isl_space *space)
1969 if (!p || !space)
1970 return isl_printer_free(p);
1971 if (p->output_format == ISL_FORMAT_ISL)
1972 return isl_printer_print_space_isl(p, space);
1973 else if (p->output_format == ISL_FORMAT_OMEGA)
1974 return print_omega_parameters(space, p);
1976 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
1977 "output format not supported for space",
1978 return isl_printer_free(p));
1981 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
1982 __isl_keep isl_local_space *ls)
1984 unsigned total;
1985 unsigned n_div;
1987 if (!ls)
1988 goto error;
1990 total = isl_local_space_dim(ls, isl_dim_all);
1991 if (isl_local_space_dim(ls, isl_dim_param) > 0) {
1992 p = print_tuple(ls->dim, p, isl_dim_param, 0, NULL, NULL);
1993 p = isl_printer_print_str(p, " -> ");
1995 p = isl_printer_print_str(p, "{ ");
1996 p = print_space(ls->dim, p, 0, 0, NULL, NULL);
1997 n_div = isl_local_space_dim(ls, isl_dim_div);
1998 if (n_div > 0) {
1999 int i;
2000 p = isl_printer_print_str(p, " : ");
2001 p = isl_printer_print_str(p, s_open_exists[0]);
2002 for (i = 0; i < n_div; ++i) {
2003 if (i)
2004 p = isl_printer_print_str(p, ", ");
2005 p = print_name(ls->dim, p, isl_dim_div, i, 0);
2006 if (isl_int_is_zero(ls->div->row[i][0]))
2007 continue;
2008 p = isl_printer_print_str(p, " = [(");
2009 p = print_affine_of_len(ls->dim, ls->div, p,
2010 ls->div->row[i] + 1, 1 + total);
2011 p = isl_printer_print_str(p, ")/");
2012 p = isl_printer_print_isl_int(p, ls->div->row[i][0]);
2013 p = isl_printer_print_str(p, "]");
2015 } else if (isl_space_is_params(ls->dim))
2016 p = isl_printer_print_str(p, s_such_that[0]);
2017 p = isl_printer_print_str(p, " }");
2018 return p;
2019 error:
2020 isl_printer_free(p);
2021 return NULL;
2024 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2025 __isl_keep isl_aff *aff)
2027 unsigned total;
2029 total = isl_local_space_dim(aff->ls, isl_dim_all);
2030 p = isl_printer_print_str(p, "(");
2031 p = print_affine_of_len(aff->ls->dim, aff->ls->div, p,
2032 aff->v->el + 1, 1 + total);
2033 if (isl_int_is_one(aff->v->el[0]))
2034 p = isl_printer_print_str(p, ")");
2035 else {
2036 p = isl_printer_print_str(p, ")/");
2037 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2040 return p;
2043 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2044 __isl_keep isl_aff *aff)
2046 if (isl_space_is_params(aff->ls->dim))
2048 else {
2049 p = print_tuple(aff->ls->dim, p, isl_dim_set, 0, NULL, NULL);
2050 p = isl_printer_print_str(p, " -> ");
2052 p = isl_printer_print_str(p, "[");
2053 p = print_aff_body(p, aff);
2054 p = isl_printer_print_str(p, "]");
2056 return p;
2059 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2060 __isl_keep isl_aff *aff)
2062 if (!aff)
2063 goto error;
2065 if (isl_local_space_dim(aff->ls, isl_dim_param) > 0) {
2066 p = print_tuple(aff->ls->dim, p, isl_dim_param, 0, NULL, NULL);
2067 p = isl_printer_print_str(p, " -> ");
2069 p = isl_printer_print_str(p, "{ ");
2070 p = print_aff(p, aff);
2071 p = isl_printer_print_str(p, " }");
2072 return p;
2073 error:
2074 isl_printer_free(p);
2075 return NULL;
2078 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2079 __isl_keep isl_pw_aff *pwaff)
2081 int i;
2083 if (!pwaff)
2084 goto error;
2086 if (isl_space_dim(pwaff->dim, isl_dim_param) > 0) {
2087 p = print_tuple(pwaff->dim, p, isl_dim_param, 0, NULL, NULL);
2088 p = isl_printer_print_str(p, " -> ");
2090 p = isl_printer_print_str(p, "{ ");
2091 for (i = 0; i < pwaff->n; ++i) {
2092 if (i)
2093 p = isl_printer_print_str(p, "; ");
2094 p = print_aff(p, pwaff->p[i].aff);
2095 p = print_disjuncts((isl_map *)pwaff->p[i].set, p, 0);
2097 p = isl_printer_print_str(p, " }");
2098 return p;
2099 error:
2100 isl_printer_free(p);
2101 return NULL;
2104 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2105 __isl_keep isl_local_space *ls, isl_int *c);
2107 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2108 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2110 if (type == isl_dim_div) {
2111 p = isl_printer_print_str(p, "floord(");
2112 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2113 p = isl_printer_print_str(p, ", ");
2114 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2115 p = isl_printer_print_str(p, ")");
2116 } else {
2117 const char *name;
2119 name = isl_space_get_dim_name(ls->dim, type, pos);
2120 if (!name)
2121 name = "UNNAMED";
2122 p = isl_printer_print_str(p, name);
2124 return p;
2127 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2128 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2130 enum isl_dim_type type;
2132 if (pos == 0)
2133 return isl_printer_print_isl_int(p, c);
2135 if (isl_int_is_one(c))
2137 else if (isl_int_is_negone(c))
2138 p = isl_printer_print_str(p, "-");
2139 else {
2140 p = isl_printer_print_isl_int(p, c);
2141 p = isl_printer_print_str(p, "*");
2143 type = pos2type(ls->dim, &pos);
2144 p = print_ls_name_c(p, ls, type, pos);
2145 return p;
2148 static __isl_give isl_printer *print_ls_partial_affine_c(
2149 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2150 isl_int *c, unsigned len)
2152 int i;
2153 int first;
2155 for (i = 0, first = 1; i < len; ++i) {
2156 int flip = 0;
2157 if (isl_int_is_zero(c[i]))
2158 continue;
2159 if (!first) {
2160 if (isl_int_is_neg(c[i])) {
2161 flip = 1;
2162 isl_int_neg(c[i], c[i]);
2163 p = isl_printer_print_str(p, " - ");
2164 } else
2165 p = isl_printer_print_str(p, " + ");
2167 first = 0;
2168 p = print_ls_term_c(p, ls, c[i], i);
2169 if (flip)
2170 isl_int_neg(c[i], c[i]);
2172 if (first)
2173 p = isl_printer_print_str(p, "0");
2174 return p;
2177 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2178 __isl_keep isl_local_space *ls, isl_int *c)
2180 unsigned len = 1 + isl_local_space_dim(ls, isl_dim_all);
2181 return print_ls_partial_affine_c(p, ls, c, len);
2184 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2185 __isl_keep isl_aff *aff)
2187 unsigned total;
2189 total = isl_local_space_dim(aff->ls, isl_dim_all);
2190 if (!isl_int_is_one(aff->v->el[0]))
2191 p = isl_printer_print_str(p, "(");
2192 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2193 if (!isl_int_is_one(aff->v->el[0])) {
2194 p = isl_printer_print_str(p, ")/");
2195 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2197 return p;
2200 /* Print the affine expression "aff" in C format to "p".
2201 * The dimension names are taken from "space"
2202 * "set" represents the domain of the affine expression.
2204 static __isl_give isl_printer *print_aff_on_domain_c(__isl_take isl_printer *p,
2205 __isl_keep isl_space *space, __isl_keep isl_aff *aff,
2206 __isl_keep isl_set *set)
2208 isl_set *u;
2209 isl_ast_build *build;
2210 isl_ast_expr *expr;
2212 u = isl_set_universe(isl_space_copy(space));
2213 build = isl_ast_build_from_context(u);
2214 build = isl_ast_build_restrict(build, isl_set_copy(set));
2215 expr = isl_ast_expr_from_aff(isl_aff_copy(aff), build);
2216 p = isl_printer_print_ast_expr(p, expr);
2217 isl_ast_expr_free(expr);
2218 isl_ast_build_free(build);
2220 return p;
2223 /* In the C format, we cannot express that "pwaff" may be undefined
2224 * on parts of the domain space. We therefore assume that the expression
2225 * will only be evaluated on its definition domain and compute the gist
2226 * of each cell with respect to this domain.
2228 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2229 __isl_keep isl_pw_aff *pwaff)
2231 int i;
2232 isl_set *domain;
2233 isl_space *space;
2235 if (pwaff->n < 1)
2236 isl_die(p->ctx, isl_error_unsupported,
2237 "cannot print empty isl_pw_aff in C format", goto error);
2238 space = isl_pw_aff_get_domain_space(pwaff);
2239 if (!space)
2240 goto error;
2242 domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
2244 for (i = 0; i < pwaff->n - 1; ++i) {
2245 isl_set *set_i;
2247 p = isl_printer_print_str(p, "(");
2249 set_i = isl_set_copy(pwaff->p[i].set);
2250 set_i = isl_set_gist(set_i, isl_set_copy(domain));
2251 p = print_set_c(p, space, set_i);
2252 isl_set_free(set_i);
2254 p = isl_printer_print_str(p, ") ? (");
2255 p = print_aff_on_domain_c(p, space,
2256 pwaff->p[i].aff, pwaff->p[i].set);
2257 p = isl_printer_print_str(p, ") : ");
2260 isl_set_free(domain);
2262 p = print_aff_on_domain_c(p, space, pwaff->p[pwaff->n - 1].aff,
2263 pwaff->p[pwaff->n - 1].set);
2264 isl_space_free(space);
2265 return p;
2266 error:
2267 isl_printer_free(p);
2268 return NULL;
2271 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2272 __isl_keep isl_aff *aff)
2274 if (!p || !aff)
2275 goto error;
2277 if (p->output_format == ISL_FORMAT_ISL)
2278 return print_aff_isl(p, aff);
2279 else if (p->output_format == ISL_FORMAT_C)
2280 return print_aff_c(p, aff);
2281 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2282 goto error);
2283 error:
2284 isl_printer_free(p);
2285 return NULL;
2288 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2289 __isl_keep isl_pw_aff *pwaff)
2291 if (!p || !pwaff)
2292 goto error;
2294 if (p->output_format == ISL_FORMAT_ISL)
2295 return print_pw_aff_isl(p, pwaff);
2296 else if (p->output_format == ISL_FORMAT_C)
2297 return print_pw_aff_c(p, pwaff);
2298 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2299 goto error);
2300 error:
2301 isl_printer_free(p);
2302 return NULL;
2305 static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
2306 __isl_keep isl_multi_aff *maff)
2308 return print_space(maff->space, p, 0, 0, NULL, maff);
2311 static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
2312 __isl_keep isl_multi_aff *maff)
2314 if (!maff)
2315 goto error;
2317 if (isl_space_dim(maff->space, isl_dim_param) > 0) {
2318 p = print_tuple(maff->space, p, isl_dim_param, 0, NULL, NULL);
2319 p = isl_printer_print_str(p, " -> ");
2321 p = isl_printer_print_str(p, "{ ");
2322 p = print_multi_aff(p, maff);
2323 p = isl_printer_print_str(p, " }");
2324 return p;
2325 error:
2326 isl_printer_free(p);
2327 return NULL;
2330 __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
2331 __isl_keep isl_multi_aff *maff)
2333 if (!p || !maff)
2334 goto error;
2336 if (p->output_format == ISL_FORMAT_ISL)
2337 return print_multi_aff_isl(p, maff);
2338 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2339 goto error);
2340 error:
2341 isl_printer_free(p);
2342 return NULL;
2345 static __isl_give isl_printer *print_pw_multi_aff_body(
2346 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2348 int i;
2350 if (!pma)
2351 goto error;
2353 for (i = 0; i < pma->n; ++i) {
2354 if (i)
2355 p = isl_printer_print_str(p, "; ");
2356 p = print_multi_aff(p, pma->p[i].maff);
2357 p = print_disjuncts((isl_map *)pma->p[i].set, p, 0);
2359 return p;
2360 error:
2361 isl_printer_free(p);
2362 return NULL;
2365 static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
2366 __isl_keep isl_pw_multi_aff *pma)
2368 if (!pma)
2369 goto error;
2371 if (isl_space_dim(pma->dim, isl_dim_param) > 0) {
2372 p = print_tuple(pma->dim, p, isl_dim_param, 0, NULL, NULL);
2373 p = isl_printer_print_str(p, " -> ");
2375 p = isl_printer_print_str(p, "{ ");
2376 p = print_pw_multi_aff_body(p, pma);
2377 p = isl_printer_print_str(p, " }");
2378 return p;
2379 error:
2380 isl_printer_free(p);
2381 return NULL;
2384 static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
2385 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2387 int i;
2389 for (i = 0; i < pma->n - 1; ++i) {
2390 p = isl_printer_print_str(p, "(");
2391 p = print_set_c(p, pma->dim, pma->p[i].set);
2392 p = isl_printer_print_str(p, ") ? (");
2393 p = print_aff_c(p, pma->p[i].maff->p[0]);
2394 p = isl_printer_print_str(p, ") : ");
2397 return print_aff_c(p, pma->p[pma->n - 1].maff->p[0]);
2400 static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
2401 __isl_keep isl_pw_multi_aff *pma)
2403 int n;
2404 const char *name;
2406 if (!pma)
2407 goto error;
2408 if (pma->n < 1)
2409 isl_die(p->ctx, isl_error_unsupported,
2410 "cannot print empty isl_pw_multi_aff in C format",
2411 goto error);
2412 name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
2413 if (!name && isl_pw_multi_aff_dim(pma, isl_dim_out) == 1)
2414 return print_unnamed_pw_multi_aff_c(p, pma);
2415 if (!name)
2416 isl_die(p->ctx, isl_error_unsupported,
2417 "cannot print unnamed isl_pw_multi_aff in C format",
2418 goto error);
2420 p = isl_printer_print_str(p, name);
2421 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
2422 if (n != 0)
2423 isl_die(p->ctx, isl_error_unsupported,
2424 "not supported yet", goto error);
2426 return p;
2427 error:
2428 isl_printer_free(p);
2429 return NULL;
2432 __isl_give isl_printer *isl_printer_print_pw_multi_aff(
2433 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2435 if (!p || !pma)
2436 goto error;
2438 if (p->output_format == ISL_FORMAT_ISL)
2439 return print_pw_multi_aff_isl(p, pma);
2440 if (p->output_format == ISL_FORMAT_C)
2441 return print_pw_multi_aff_c(p, pma);
2442 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2443 goto error);
2444 error:
2445 isl_printer_free(p);
2446 return NULL;
2449 static int print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
2450 void *user)
2452 struct isl_union_print_data *data;
2453 data = (struct isl_union_print_data *) user;
2455 if (!data->first)
2456 data->p = isl_printer_print_str(data->p, "; ");
2457 data->first = 0;
2459 data->p = print_pw_multi_aff_body(data->p, pma);
2460 isl_pw_multi_aff_free(pma);
2462 return 0;
2465 static __isl_give isl_printer *print_union_pw_multi_aff_isl(
2466 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2468 struct isl_union_print_data data = { p, 1 };
2469 isl_space *space;
2471 space = isl_union_pw_multi_aff_get_space(upma);
2472 if (isl_space_dim(space, isl_dim_param) > 0) {
2473 p = print_tuple(space, p, isl_dim_param, 0, NULL, NULL);
2474 p = isl_printer_print_str(p, s_to[0]);
2476 isl_space_free(space);
2477 p = isl_printer_print_str(p, s_open_set[0]);
2478 isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
2479 &print_pw_multi_aff_body_wrap, &data);
2480 p = data.p;
2481 p = isl_printer_print_str(p, s_close_set[0]);
2482 return p;
2485 __isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
2486 __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
2488 if (!p || !upma)
2489 goto error;
2491 if (p->output_format == ISL_FORMAT_ISL)
2492 return print_union_pw_multi_aff_isl(p, upma);
2493 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2494 goto error);
2495 error:
2496 isl_printer_free(p);
2497 return NULL;
2500 static __isl_give isl_printer *print_multi_pw_aff_isl(__isl_take isl_printer *p,
2501 __isl_keep isl_multi_pw_aff *mpa)
2503 int i;
2505 if (!mpa)
2506 return isl_printer_free(p);
2508 p = isl_printer_print_str(p, "(");
2509 for (i = 0; i < mpa->n; ++i) {
2510 if (i)
2511 p = isl_printer_print_str(p, ",");
2512 p = isl_printer_print_pw_aff(p, mpa->p[i]);
2514 p = isl_printer_print_str(p, ")");
2515 return p;
2518 __isl_give isl_printer *isl_printer_print_multi_pw_aff(
2519 __isl_take isl_printer *p, __isl_keep isl_multi_pw_aff *mpa)
2521 if (!p || !mpa)
2522 return isl_printer_free(p);
2524 if (p->output_format == ISL_FORMAT_ISL)
2525 return print_multi_pw_aff_isl(p, mpa);
2526 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2527 return isl_printer_free(p));