mark some data structures and functions for export to language bindings
[isl.git] / isl_output.c
blob6883e7f50a2016df3c4b9e8851a78a83f3cd8a9b
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 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1652 __isl_keep isl_space *dim,
1653 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int first)
1655 if (!first)
1656 p = isl_printer_print_str(p, " && ");
1658 p = print_affine_c(p, dim, bset, c);
1659 p = isl_printer_print_str(p, " ");
1660 p = isl_printer_print_str(p, op);
1661 p = isl_printer_print_str(p, " 0");
1662 return p;
1665 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1666 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset)
1668 int i, j;
1669 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
1670 unsigned total = isl_basic_set_total_dim(bset) - n_div;
1672 for (i = 0; i < bset->n_eq; ++i) {
1673 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
1674 if (j < 0)
1675 p = print_constraint_c(p, dim, bset,
1676 bset->eq[i], "==", !i);
1677 else {
1678 if (i)
1679 p = isl_printer_print_str(p, " && ");
1680 p = isl_printer_print_str(p, "(");
1681 p = print_partial_affine_c(p, dim, bset, bset->eq[i],
1682 1 + total + j);
1683 p = isl_printer_print_str(p, ") % ");
1684 p = isl_printer_print_isl_int(p,
1685 bset->eq[i][1 + total + j]);
1686 p = isl_printer_print_str(p, " == 0");
1689 for (i = 0; i < bset->n_ineq; ++i)
1690 p = print_constraint_c(p, dim, bset, bset->ineq[i], ">=",
1691 !bset->n_eq && !i);
1692 return p;
1695 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
1696 __isl_keep isl_space *dim, __isl_keep isl_set *set)
1698 int i;
1700 if (set->n == 0)
1701 p = isl_printer_print_str(p, "0");
1703 for (i = 0; i < set->n; ++i) {
1704 if (i)
1705 p = isl_printer_print_str(p, " || ");
1706 if (set->n > 1)
1707 p = isl_printer_print_str(p, "(");
1708 p = print_basic_set_c(p, dim, set->p[i]);
1709 if (set->n > 1)
1710 p = isl_printer_print_str(p, ")");
1712 return p;
1715 static __isl_give isl_printer *print_pw_qpolynomial_c(
1716 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1718 int i;
1720 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set))
1721 return print_qpolynomial_c(p, pwqp->dim, pwqp->p[0].qp);
1723 for (i = 0; i < pwqp->n; ++i) {
1724 p = isl_printer_print_str(p, "(");
1725 p = print_set_c(p, pwqp->dim, pwqp->p[i].set);
1726 p = isl_printer_print_str(p, ") ? (");
1727 p = print_qpolynomial_c(p, pwqp->dim, pwqp->p[i].qp);
1728 p = isl_printer_print_str(p, ") : ");
1731 p = isl_printer_print_str(p, "0");
1732 return p;
1735 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
1736 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1738 if (!p || !pwqp)
1739 goto error;
1741 if (p->output_format == ISL_FORMAT_ISL)
1742 return print_pw_qpolynomial_isl(p, pwqp);
1743 else if (p->output_format == ISL_FORMAT_C)
1744 return print_pw_qpolynomial_c(p, pwqp);
1745 isl_assert(p->ctx, 0, goto error);
1746 error:
1747 isl_printer_free(p);
1748 return NULL;
1751 static int print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1753 struct isl_union_print_data *data;
1754 data = (struct isl_union_print_data *)user;
1756 if (!data->first)
1757 data->p = isl_printer_print_str(data->p, "; ");
1758 data->first = 0;
1760 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
1761 isl_pw_qpolynomial_free(pwqp);
1763 return 0;
1766 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
1767 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1769 struct isl_union_print_data data = { p, 1 };
1770 isl_space *dim;
1771 dim = isl_union_pw_qpolynomial_get_space(upwqp);
1772 if (isl_space_dim(dim, isl_dim_param) > 0) {
1773 p = print_tuple(dim, p, isl_dim_param, 0, NULL, NULL);
1774 p = isl_printer_print_str(p, " -> ");
1776 isl_space_free(dim);
1777 p = isl_printer_print_str(p, "{ ");
1778 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
1779 &data);
1780 p = data.p;
1781 p = isl_printer_print_str(p, " }");
1782 return p;
1785 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
1786 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1788 if (!p || !upwqp)
1789 goto error;
1791 if (p->output_format == ISL_FORMAT_ISL)
1792 return print_union_pw_qpolynomial_isl(p, upwqp);
1793 isl_die(p->ctx, isl_error_invalid,
1794 "invalid output format for isl_union_pw_qpolynomial",
1795 goto error);
1796 error:
1797 isl_printer_free(p);
1798 return NULL;
1801 static __isl_give isl_printer *print_qpolynomial_fold_c(
1802 __isl_take isl_printer *p, __isl_keep isl_space *dim,
1803 __isl_keep isl_qpolynomial_fold *fold)
1805 int i;
1807 for (i = 0; i < fold->n - 1; ++i)
1808 if (fold->type == isl_fold_min)
1809 p = isl_printer_print_str(p, "min(");
1810 else if (fold->type == isl_fold_max)
1811 p = isl_printer_print_str(p, "max(");
1813 for (i = 0; i < fold->n; ++i) {
1814 if (i)
1815 p = isl_printer_print_str(p, ", ");
1816 p = print_qpolynomial_c(p, dim, fold->qp[i]);
1817 if (i)
1818 p = isl_printer_print_str(p, ")");
1820 return p;
1823 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
1824 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
1826 if (!p || !fold)
1827 goto error;
1828 if (p->output_format == ISL_FORMAT_ISL)
1829 return qpolynomial_fold_print(fold, p);
1830 else if (p->output_format == ISL_FORMAT_C)
1831 return print_qpolynomial_fold_c(p, fold->dim, fold);
1832 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
1833 goto error);
1834 error:
1835 isl_printer_free(p);
1836 return NULL;
1839 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
1840 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1842 int i;
1844 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set))
1845 return print_qpolynomial_fold_c(p, pwf->dim, pwf->p[0].fold);
1847 for (i = 0; i < pwf->n; ++i) {
1848 p = isl_printer_print_str(p, "(");
1849 p = print_set_c(p, pwf->dim, pwf->p[i].set);
1850 p = isl_printer_print_str(p, ") ? (");
1851 p = print_qpolynomial_fold_c(p, pwf->dim, pwf->p[i].fold);
1852 p = isl_printer_print_str(p, ") : ");
1855 p = isl_printer_print_str(p, "0");
1856 return p;
1859 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
1860 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1862 if (!p || !pwf)
1863 goto error;
1865 if (p->output_format == ISL_FORMAT_ISL)
1866 return print_pw_qpolynomial_fold_isl(p, pwf);
1867 else if (p->output_format == ISL_FORMAT_C)
1868 return print_pw_qpolynomial_fold_c(p, pwf);
1869 isl_assert(p->ctx, 0, goto error);
1870 error:
1871 isl_printer_free(p);
1872 return NULL;
1875 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
1876 FILE *out, unsigned output_format)
1878 isl_printer *p;
1880 if (!pwf)
1881 return;
1883 p = isl_printer_to_file(pwf->dim->ctx, out);
1884 p = isl_printer_set_output_format(p, output_format);
1885 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
1887 isl_printer_free(p);
1890 static int print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf, void *user)
1892 struct isl_union_print_data *data;
1893 data = (struct isl_union_print_data *)user;
1895 if (!data->first)
1896 data->p = isl_printer_print_str(data->p, "; ");
1897 data->first = 0;
1899 data->p = isl_pwf_print_isl_body(data->p, pwf);
1900 isl_pw_qpolynomial_fold_free(pwf);
1902 return 0;
1905 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
1906 __isl_take isl_printer *p,
1907 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1909 struct isl_union_print_data data = { p, 1 };
1910 isl_space *dim;
1911 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
1912 if (isl_space_dim(dim, isl_dim_param) > 0) {
1913 p = print_tuple(dim, p, isl_dim_param, 0, NULL, NULL);
1914 p = isl_printer_print_str(p, " -> ");
1916 isl_space_free(dim);
1917 p = isl_printer_print_str(p, "{ ");
1918 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
1919 &print_pwf_body, &data);
1920 p = data.p;
1921 p = isl_printer_print_str(p, " }");
1922 return p;
1925 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
1926 __isl_take isl_printer *p,
1927 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1929 if (!p || !upwf)
1930 goto error;
1932 if (p->output_format == ISL_FORMAT_ISL)
1933 return print_union_pw_qpolynomial_fold_isl(p, upwf);
1934 isl_die(p->ctx, isl_error_invalid,
1935 "invalid output format for isl_union_pw_qpolynomial_fold",
1936 goto error);
1937 error:
1938 isl_printer_free(p);
1939 return NULL;
1942 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
1943 __isl_keep isl_constraint *c)
1945 isl_basic_map *bmap;
1947 if (!p || !c)
1948 goto error;
1950 bmap = isl_basic_map_from_constraint(isl_constraint_copy(c));
1951 p = isl_printer_print_basic_map(p, bmap);
1952 isl_basic_map_free(bmap);
1953 return p;
1954 error:
1955 isl_printer_free(p);
1956 return NULL;
1959 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
1960 __isl_keep isl_space *dim)
1962 if (!dim)
1963 goto error;
1965 if (isl_space_dim(dim, isl_dim_param) > 0) {
1966 p = print_tuple(dim, p, isl_dim_param, 0, NULL, NULL);
1967 p = isl_printer_print_str(p, " -> ");
1970 p = isl_printer_print_str(p, "{ ");
1971 if (isl_space_is_params(dim))
1972 p = isl_printer_print_str(p, s_such_that[0]);
1973 else
1974 p = print_space(dim, p, 0, 0, NULL, NULL);
1975 p = isl_printer_print_str(p, " }");
1977 return p;
1978 error:
1979 isl_printer_free(p);
1980 return NULL;
1983 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
1984 __isl_keep isl_local_space *ls)
1986 unsigned total;
1987 unsigned n_div;
1989 if (!ls)
1990 goto error;
1992 total = isl_local_space_dim(ls, isl_dim_all);
1993 if (isl_local_space_dim(ls, isl_dim_param) > 0) {
1994 p = print_tuple(ls->dim, p, isl_dim_param, 0, NULL, NULL);
1995 p = isl_printer_print_str(p, " -> ");
1997 p = isl_printer_print_str(p, "{ ");
1998 p = print_space(ls->dim, p, 0, 0, NULL, NULL);
1999 n_div = isl_local_space_dim(ls, isl_dim_div);
2000 if (n_div > 0) {
2001 int i;
2002 p = isl_printer_print_str(p, " : ");
2003 p = isl_printer_print_str(p, s_open_exists[0]);
2004 for (i = 0; i < n_div; ++i) {
2005 if (i)
2006 p = isl_printer_print_str(p, ", ");
2007 p = print_name(ls->dim, p, isl_dim_div, i, 0);
2008 if (isl_int_is_zero(ls->div->row[i][0]))
2009 continue;
2010 p = isl_printer_print_str(p, " = [(");
2011 p = print_affine_of_len(ls->dim, ls->div, p,
2012 ls->div->row[i] + 1, 1 + total);
2013 p = isl_printer_print_str(p, ")/");
2014 p = isl_printer_print_isl_int(p, ls->div->row[i][0]);
2015 p = isl_printer_print_str(p, "]");
2017 } else if (isl_space_is_params(ls->dim))
2018 p = isl_printer_print_str(p, s_such_that[0]);
2019 p = isl_printer_print_str(p, " }");
2020 return p;
2021 error:
2022 isl_printer_free(p);
2023 return NULL;
2026 static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2027 __isl_keep isl_aff *aff)
2029 unsigned total;
2031 total = isl_local_space_dim(aff->ls, isl_dim_all);
2032 p = isl_printer_print_str(p, "(");
2033 p = print_affine_of_len(aff->ls->dim, aff->ls->div, p,
2034 aff->v->el + 1, 1 + total);
2035 if (isl_int_is_one(aff->v->el[0]))
2036 p = isl_printer_print_str(p, ")");
2037 else {
2038 p = isl_printer_print_str(p, ")/");
2039 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2042 return p;
2045 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2046 __isl_keep isl_aff *aff)
2048 if (isl_space_is_params(aff->ls->dim))
2050 else {
2051 p = print_tuple(aff->ls->dim, p, isl_dim_set, 0, NULL, NULL);
2052 p = isl_printer_print_str(p, " -> ");
2054 p = isl_printer_print_str(p, "[");
2055 p = print_aff_body(p, aff);
2056 p = isl_printer_print_str(p, "]");
2058 return p;
2061 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2062 __isl_keep isl_aff *aff)
2064 if (!aff)
2065 goto error;
2067 if (isl_local_space_dim(aff->ls, isl_dim_param) > 0) {
2068 p = print_tuple(aff->ls->dim, p, isl_dim_param, 0, NULL, NULL);
2069 p = isl_printer_print_str(p, " -> ");
2071 p = isl_printer_print_str(p, "{ ");
2072 p = print_aff(p, aff);
2073 p = isl_printer_print_str(p, " }");
2074 return p;
2075 error:
2076 isl_printer_free(p);
2077 return NULL;
2080 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2081 __isl_keep isl_pw_aff *pwaff)
2083 int i;
2085 if (!pwaff)
2086 goto error;
2088 if (isl_space_dim(pwaff->dim, isl_dim_param) > 0) {
2089 p = print_tuple(pwaff->dim, p, isl_dim_param, 0, NULL, NULL);
2090 p = isl_printer_print_str(p, " -> ");
2092 p = isl_printer_print_str(p, "{ ");
2093 for (i = 0; i < pwaff->n; ++i) {
2094 if (i)
2095 p = isl_printer_print_str(p, "; ");
2096 p = print_aff(p, pwaff->p[i].aff);
2097 p = print_disjuncts((isl_map *)pwaff->p[i].set, p, 0);
2099 p = isl_printer_print_str(p, " }");
2100 return p;
2101 error:
2102 isl_printer_free(p);
2103 return NULL;
2106 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2107 __isl_keep isl_local_space *ls, isl_int *c);
2109 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2110 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2112 if (type == isl_dim_div) {
2113 p = isl_printer_print_str(p, "floord(");
2114 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2115 p = isl_printer_print_str(p, ", ");
2116 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2117 p = isl_printer_print_str(p, ")");
2118 } else {
2119 const char *name;
2121 name = isl_space_get_dim_name(ls->dim, type, pos);
2122 if (!name)
2123 name = "UNNAMED";
2124 p = isl_printer_print_str(p, name);
2126 return p;
2129 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2130 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2132 enum isl_dim_type type;
2134 if (pos == 0)
2135 return isl_printer_print_isl_int(p, c);
2137 if (isl_int_is_one(c))
2139 else if (isl_int_is_negone(c))
2140 p = isl_printer_print_str(p, "-");
2141 else {
2142 p = isl_printer_print_isl_int(p, c);
2143 p = isl_printer_print_str(p, "*");
2145 type = pos2type(ls->dim, &pos);
2146 p = print_ls_name_c(p, ls, type, pos);
2147 return p;
2150 static __isl_give isl_printer *print_ls_partial_affine_c(
2151 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2152 isl_int *c, unsigned len)
2154 int i;
2155 int first;
2157 for (i = 0, first = 1; i < len; ++i) {
2158 int flip = 0;
2159 if (isl_int_is_zero(c[i]))
2160 continue;
2161 if (!first) {
2162 if (isl_int_is_neg(c[i])) {
2163 flip = 1;
2164 isl_int_neg(c[i], c[i]);
2165 p = isl_printer_print_str(p, " - ");
2166 } else
2167 p = isl_printer_print_str(p, " + ");
2169 first = 0;
2170 p = print_ls_term_c(p, ls, c[i], i);
2171 if (flip)
2172 isl_int_neg(c[i], c[i]);
2174 if (first)
2175 p = isl_printer_print_str(p, "0");
2176 return p;
2179 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2180 __isl_keep isl_local_space *ls, isl_int *c)
2182 unsigned len = 1 + isl_local_space_dim(ls, isl_dim_all);
2183 return print_ls_partial_affine_c(p, ls, c, len);
2186 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2187 __isl_keep isl_aff *aff)
2189 unsigned total;
2191 total = isl_local_space_dim(aff->ls, isl_dim_all);
2192 p = isl_printer_print_str(p, "(");
2193 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2194 if (isl_int_is_one(aff->v->el[0]))
2195 p = isl_printer_print_str(p, ")");
2196 else {
2197 p = isl_printer_print_str(p, ")/");
2198 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2200 return p;
2203 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2204 __isl_keep isl_pw_aff *pwaff)
2206 int i;
2208 if (pwaff->n < 1)
2209 isl_die(p->ctx, isl_error_unsupported,
2210 "cannot print empty isl_pw_aff in C format", goto error);
2212 for (i = 0; i < pwaff->n - 1; ++i) {
2213 p = isl_printer_print_str(p, "(");
2214 p = print_set_c(p, pwaff->dim, pwaff->p[i].set);
2215 p = isl_printer_print_str(p, ") ? (");
2216 p = print_aff_c(p, pwaff->p[i].aff);
2217 p = isl_printer_print_str(p, ") : ");
2220 return print_aff_c(p, pwaff->p[pwaff->n - 1].aff);
2221 error:
2222 isl_printer_free(p);
2223 return NULL;
2226 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2227 __isl_keep isl_aff *aff)
2229 if (!p || !aff)
2230 goto error;
2232 if (p->output_format == ISL_FORMAT_ISL)
2233 return print_aff_isl(p, aff);
2234 else if (p->output_format == ISL_FORMAT_C)
2235 return print_aff_c(p, aff);
2236 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2237 goto error);
2238 error:
2239 isl_printer_free(p);
2240 return NULL;
2243 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2244 __isl_keep isl_pw_aff *pwaff)
2246 if (!p || !pwaff)
2247 goto error;
2249 if (p->output_format == ISL_FORMAT_ISL)
2250 return print_pw_aff_isl(p, pwaff);
2251 else if (p->output_format == ISL_FORMAT_C)
2252 return print_pw_aff_c(p, pwaff);
2253 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2254 goto error);
2255 error:
2256 isl_printer_free(p);
2257 return NULL;
2260 static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
2261 __isl_keep isl_multi_aff *maff)
2263 return print_space(maff->space, p, 0, 0, NULL, maff);
2266 static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
2267 __isl_keep isl_multi_aff *maff)
2269 if (!maff)
2270 goto error;
2272 if (isl_space_dim(maff->space, isl_dim_param) > 0) {
2273 p = print_tuple(maff->space, p, isl_dim_param, 0, NULL, NULL);
2274 p = isl_printer_print_str(p, " -> ");
2276 p = isl_printer_print_str(p, "{ ");
2277 p = print_multi_aff(p, maff);
2278 p = isl_printer_print_str(p, " }");
2279 return p;
2280 error:
2281 isl_printer_free(p);
2282 return NULL;
2285 __isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
2286 __isl_keep isl_multi_aff *maff)
2288 if (!p || !maff)
2289 goto error;
2291 if (p->output_format == ISL_FORMAT_ISL)
2292 return print_multi_aff_isl(p, maff);
2293 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2294 goto error);
2295 error:
2296 isl_printer_free(p);
2297 return NULL;
2300 static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
2301 __isl_keep isl_pw_multi_aff *pma)
2303 int i;
2305 if (!pma)
2306 goto error;
2308 if (isl_space_dim(pma->dim, isl_dim_param) > 0) {
2309 p = print_tuple(pma->dim, p, isl_dim_param, 0, NULL, NULL);
2310 p = isl_printer_print_str(p, " -> ");
2312 p = isl_printer_print_str(p, "{ ");
2313 for (i = 0; i < pma->n; ++i) {
2314 if (i)
2315 p = isl_printer_print_str(p, "; ");
2316 p = print_multi_aff(p, pma->p[i].maff);
2317 p = print_disjuncts((isl_map *)pma->p[i].set, p, 0);
2319 p = isl_printer_print_str(p, " }");
2320 return p;
2321 error:
2322 isl_printer_free(p);
2323 return NULL;
2326 static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
2327 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2329 int i;
2331 for (i = 0; i < pma->n - 1; ++i) {
2332 p = isl_printer_print_str(p, "(");
2333 p = print_set_c(p, pma->dim, pma->p[i].set);
2334 p = isl_printer_print_str(p, ") ? (");
2335 p = print_aff_c(p, pma->p[i].maff->p[0]);
2336 p = isl_printer_print_str(p, ") : ");
2339 return print_aff_c(p, pma->p[pma->n - 1].maff->p[0]);
2342 static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
2343 __isl_keep isl_pw_multi_aff *pma)
2345 int n;
2346 const char *name;
2348 if (!pma)
2349 goto error;
2350 if (pma->n < 1)
2351 isl_die(p->ctx, isl_error_unsupported,
2352 "cannot print empty isl_pw_multi_aff in C format",
2353 goto error);
2354 name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
2355 if (!name && isl_pw_multi_aff_dim(pma, isl_dim_out) == 1)
2356 return print_unnamed_pw_multi_aff_c(p, pma);
2357 if (!name)
2358 isl_die(p->ctx, isl_error_unsupported,
2359 "cannot print unnamed isl_pw_multi_aff in C format",
2360 goto error);
2362 p = isl_printer_print_str(p, name);
2363 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
2364 if (n != 0)
2365 isl_die(p->ctx, isl_error_unsupported,
2366 "not supported yet", goto error);
2368 return p;
2369 error:
2370 isl_printer_free(p);
2371 return NULL;
2374 __isl_give isl_printer *isl_printer_print_pw_multi_aff(
2375 __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2377 if (!p || !pma)
2378 goto error;
2380 if (p->output_format == ISL_FORMAT_ISL)
2381 return print_pw_multi_aff_isl(p, pma);
2382 if (p->output_format == ISL_FORMAT_C)
2383 return print_pw_multi_aff_c(p, pma);
2384 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2385 goto error);
2386 error:
2387 isl_printer_free(p);
2388 return NULL;