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
15 #include <isl_map_private.h>
18 #include <isl_polynomial_private.h>
19 #include <isl_printer_private.h>
20 #include <isl_dim_private.h>
21 #include <isl_mat_private.h>
22 #include <isl/union_map.h>
24 static const char *s_to[2] = { " -> ", " \\to " };
25 static const char *s_and[2] = { " and ", " \\wedge " };
26 static const char *s_or[2] = { " or ", " \\vee " };
27 static const char *s_le[2] = { "<=", "\\le" };
28 static const char *s_ge[2] = { ">=", "\\ge" };
29 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
30 static const char *s_close_set[2] = { " }", " \\,\\}" };
31 static const char *s_open_list[2] = { "[", "(" };
32 static const char *s_close_list[2] = { "]", ")" };
33 static const char *s_such_that[2] = { " : ", " \\mid " };
34 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
35 static const char *s_close_exists[2] = { ")", "" };
36 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
37 static const char *s_param_prefix[2] = { "p", "p_" };
38 static const char *s_input_prefix[2] = { "i", "i_" };
39 static const char *s_output_prefix[2] = { "o", "o_" };
41 static __isl_give isl_printer *print_constraint_polylib(
42 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
45 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
46 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
47 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
48 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
50 p = isl_printer_start_line(p);
51 p = isl_printer_print_int(p, ineq);
52 for (i = 0; i < n_out; ++i) {
53 p = isl_printer_print_str(p, " ");
54 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
56 for (i = 0; i < n_in; ++i) {
57 p = isl_printer_print_str(p, " ");
58 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
60 for (i = 0; i < bmap->n_div; ++i) {
61 p = isl_printer_print_str(p, " ");
62 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
64 for (i = 0; i < nparam; ++i) {
65 p = isl_printer_print_str(p, " ");
66 p = isl_printer_print_isl_int(p, c[1+i]);
68 p = isl_printer_print_str(p, " ");
69 p = isl_printer_print_isl_int(p, c[0]);
70 p = isl_printer_end_line(p);
74 static __isl_give isl_printer *print_constraints_polylib(
75 struct isl_basic_map *bmap, __isl_take isl_printer *p)
79 p = isl_printer_set_isl_int_width(p, 5);
81 for (i = 0; i < bmap->n_eq; ++i)
82 p = print_constraint_polylib(bmap, 0, i, p);
83 for (i = 0; i < bmap->n_ineq; ++i)
84 p = print_constraint_polylib(bmap, 1, i, p);
89 static __isl_give isl_printer *bset_print_constraints_polylib(
90 struct isl_basic_set *bset, __isl_take isl_printer *p)
92 return print_constraints_polylib((struct isl_basic_map *)bset, p);
95 static __isl_give isl_printer *isl_basic_map_print_polylib(
96 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
98 unsigned total = isl_basic_map_total_dim(bmap);
99 p = isl_printer_start_line(p);
100 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
101 p = isl_printer_print_str(p, " ");
102 p = isl_printer_print_int(p, 1 + total + 1);
104 p = isl_printer_print_str(p, " ");
105 p = isl_printer_print_int(p,
106 isl_basic_map_dim(bmap, isl_dim_out));
107 p = isl_printer_print_str(p, " ");
108 p = isl_printer_print_int(p,
109 isl_basic_map_dim(bmap, isl_dim_in));
110 p = isl_printer_print_str(p, " ");
111 p = isl_printer_print_int(p,
112 isl_basic_map_dim(bmap, isl_dim_div));
113 p = isl_printer_print_str(p, " ");
114 p = isl_printer_print_int(p,
115 isl_basic_map_dim(bmap, isl_dim_param));
117 p = isl_printer_end_line(p);
118 return print_constraints_polylib(bmap, p);
121 static __isl_give isl_printer *isl_basic_set_print_polylib(
122 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
124 return isl_basic_map_print_polylib((struct isl_basic_map *)bset, p, ext);
127 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
128 __isl_take isl_printer *p, int ext)
132 p = isl_printer_start_line(p);
133 p = isl_printer_print_int(p, map->n);
134 p = isl_printer_end_line(p);
135 for (i = 0; i < map->n; ++i) {
136 p = isl_printer_start_line(p);
137 p = isl_printer_end_line(p);
138 p = isl_basic_map_print_polylib(map->p[i], p, ext);
143 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
144 __isl_take isl_printer *p, int ext)
146 return isl_map_print_polylib((struct isl_map *)set, p, ext);
149 static int count_same_name(__isl_keep isl_dim *dim,
150 enum isl_dim_type type, unsigned pos, const char *name)
156 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
157 s = t == type ? pos : isl_dim_size(dim, t);
158 for (p = 0; p < s; ++p) {
159 const char *n = isl_dim_get_name(dim, t, p);
160 if (n && !strcmp(n, name))
167 static __isl_give isl_printer *print_name(__isl_keep isl_dim *dim,
168 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
175 name = type == isl_dim_div ? NULL : isl_dim_get_name(dim, type, pos);
179 if (type == isl_dim_param)
180 prefix = s_param_prefix[latex];
181 else if (type == isl_dim_div)
182 prefix = s_div_prefix[latex];
183 else if (set || type == isl_dim_in)
184 prefix = s_input_prefix[latex];
186 prefix = s_output_prefix[latex];
187 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
190 primes = count_same_name(dim, name == buffer ? isl_dim_div : type,
192 p = isl_printer_print_str(p, name);
194 p = isl_printer_print_str(p, "'");
198 static enum isl_dim_type pos2type(__isl_keep isl_dim *dim, unsigned *pos)
200 enum isl_dim_type type;
201 unsigned n_in = isl_dim_size(dim, isl_dim_in);
202 unsigned n_out = isl_dim_size(dim, isl_dim_out);
203 unsigned nparam = isl_dim_size(dim, isl_dim_param);
205 if (*pos < 1 + nparam) {
206 type = isl_dim_param;
208 } else if (*pos < 1 + nparam + n_in) {
211 } else if (*pos < 1 + nparam + n_in + n_out) {
213 *pos -= 1 + nparam + n_in;
216 *pos -= 1 + nparam + n_in + n_out;
222 static __isl_give isl_printer *print_div(__isl_keep isl_dim *dim,
223 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
225 static __isl_give isl_printer *print_term(__isl_keep isl_dim *dim,
226 __isl_keep isl_mat *div,
227 isl_int c, unsigned pos, __isl_take isl_printer *p, int set, int latex)
229 enum isl_dim_type type;
233 return isl_printer_print_isl_int(p, c);
235 type = pos2type(dim, &pos);
236 print_div_def = type == isl_dim_div && div &&
237 !isl_int_is_zero(div->row[pos][0]);
239 if (isl_int_is_one(c))
241 else if (isl_int_is_negone(c))
242 p = isl_printer_print_str(p, "-");
244 p = isl_printer_print_isl_int(p, c);
245 if (p->output_format == ISL_FORMAT_C || print_div_def)
246 p = isl_printer_print_str(p, "*");
249 p = print_div(dim, div, pos, p);
251 p = print_name(dim, p, type, pos, set, latex);
255 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_dim *dim,
256 __isl_keep isl_mat *div,
257 __isl_take isl_printer *p, isl_int *c, int len, int set)
262 for (i = 0, first = 1; i < len; ++i) {
264 if (isl_int_is_zero(c[i]))
267 if (isl_int_is_neg(c[i])) {
269 isl_int_neg(c[i], c[i]);
270 p = isl_printer_print_str(p, " - ");
272 p = isl_printer_print_str(p, " + ");
275 p = print_term(dim, div, c[i], i, p, set, 0);
277 isl_int_neg(c[i], c[i]);
280 p = isl_printer_print_str(p, "0");
284 static __isl_give isl_printer *print_affine(__isl_keep isl_basic_map *bmap,
285 __isl_keep isl_dim *dim, __isl_take isl_printer *p, isl_int *c, int set)
287 unsigned len = 1 + isl_basic_map_total_dim(bmap);
288 return print_affine_of_len(dim, NULL, p, c, len, set);
291 static int defining_equality(__isl_keep isl_basic_map *eq,
292 __isl_keep isl_dim *dim, enum isl_dim_type type, int pos)
300 pos += isl_dim_offset(dim, type);
301 total = isl_basic_map_total_dim(eq);
303 for (i = 0; i < eq->n_eq; ++i) {
304 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
306 if (isl_int_is_one(eq->eq[i][1 + pos]))
307 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
314 /* offset is the offset of local_dim inside global_type of global_dim.
316 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
317 __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
318 __isl_keep isl_dim *local_dim, enum isl_dim_type local_type,
319 int set, int latex, __isl_keep isl_basic_map *eq, int offset)
323 if (global_dim != local_dim && local_type == isl_dim_out)
324 offset += local_dim->n_in;
326 for (i = 0; i < isl_dim_size(local_dim, local_type); ++i) {
328 p = isl_printer_print_str(p, ", ");
329 j = defining_equality(eq, global_dim, global_type, offset + i);
331 int pos = 1 + isl_dim_offset(global_dim, global_type)
333 p = print_affine_of_len(eq->dim, NULL,
334 p, eq->eq[j], pos, set);
336 p = print_name(global_dim, p, global_type, offset + i,
343 static __isl_give isl_printer *print_var_list(__isl_keep isl_dim *dim,
344 __isl_take isl_printer *p, enum isl_dim_type type,
345 int set, int latex, __isl_keep isl_basic_map *eq)
347 return print_nested_var_list(p, dim, type, dim, type, set, latex, eq, 0);
350 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
351 __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
352 __isl_keep isl_dim *local_dim,
353 int set, int latex, __isl_keep isl_basic_map *eq, int offset);
355 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
356 __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
357 __isl_keep isl_dim *local_dim, enum isl_dim_type local_type,
358 int set, int latex, __isl_keep isl_basic_map *eq, int offset)
360 const char *name = NULL;
361 unsigned n = isl_dim_size(local_dim, local_type);
362 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
363 name = isl_dim_get_tuple_name(local_dim, local_type);
366 p = isl_printer_print_str(p, "\\mathrm{");
367 p = isl_printer_print_str(p, name);
369 p = isl_printer_print_str(p, "}");
372 if (!latex || n != 1 || name)
373 p = isl_printer_print_str(p, s_open_list[latex]);
374 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
375 local_dim->nested[local_type - isl_dim_in]) {
376 if (global_dim != local_dim && local_type == isl_dim_out)
377 offset += local_dim->n_in;
378 p = print_nested_map_dim(p, global_dim, global_type,
379 local_dim->nested[local_type - isl_dim_in],
380 set, latex, eq, offset);
382 p = print_nested_var_list(p, global_dim, global_type,
383 local_dim, local_type, set, latex, eq, offset);
384 if (!latex || n != 1 || name)
385 p = isl_printer_print_str(p, s_close_list[latex]);
389 static __isl_give isl_printer *print_tuple(__isl_keep isl_dim *dim,
390 __isl_take isl_printer *p, enum isl_dim_type type,
391 int set, int latex, __isl_keep isl_basic_map *eq)
393 return print_nested_tuple(p, dim, type, dim, type, set, latex, eq, 0);
396 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
397 __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
398 __isl_keep isl_dim *local_dim,
399 int set, int latex, __isl_keep isl_basic_map *eq, int offset)
401 p = print_nested_tuple(p, global_dim, global_type,
402 local_dim, isl_dim_in, set, latex, eq, offset);
403 p = isl_printer_print_str(p, s_to[latex]);
404 p = print_nested_tuple(p, global_dim, global_type,
405 local_dim, isl_dim_out, set, latex, eq, offset);
410 static __isl_give isl_printer *print_dim(__isl_keep isl_dim *dim,
411 __isl_take isl_printer *p, int set, int latex, int rational,
412 __isl_keep isl_basic_map *eq)
414 if (rational && !latex)
415 p = isl_printer_print_str(p, "rat: ");
417 p = print_tuple(dim, p, isl_dim_set, 1, latex, eq);
419 p = print_tuple(dim, p, isl_dim_in, 0, latex, eq);
420 p = isl_printer_print_str(p, s_to[latex]);
421 p = print_tuple(dim, p, isl_dim_out, 0, latex, eq);
427 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_dim *dim,
428 __isl_take isl_printer *p)
430 if (isl_dim_size(dim, isl_dim_param) == 0)
433 p = isl_printer_start_line(p);
434 p = isl_printer_print_str(p, "symbolic ");
435 p = print_var_list(dim, p, isl_dim_param, 0, 0, NULL);
436 p = isl_printer_print_str(p, ";");
437 p = isl_printer_end_line(p);
441 static __isl_give isl_printer *print_constraint(struct isl_basic_map *bmap,
442 __isl_keep isl_dim *dim, __isl_take isl_printer *p,
443 isl_int *c, int last, const char *op, int first_constraint, int set,
446 if (!first_constraint)
447 p = isl_printer_print_str(p, s_and[latex]);
449 isl_int_abs(c[last], c[last]);
451 p = print_term(dim, NULL, c[last], last, p, set, latex);
453 p = isl_printer_print_str(p, " ");
454 p = isl_printer_print_str(p, op);
455 p = isl_printer_print_str(p, " ");
457 isl_int_set_si(c[last], 0);
458 p = print_affine(bmap, dim, p, c, set);
463 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
464 __isl_keep isl_dim *dim, __isl_take isl_printer *p, int set, int latex)
468 unsigned total = isl_basic_map_total_dim(bmap);
470 c = isl_vec_alloc(bmap->ctx, 1 + total);
474 for (i = bmap->n_eq - 1; i >= 0; --i) {
475 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
476 isl_assert(bmap->ctx, l >= 0, goto error);
477 if (isl_int_is_neg(bmap->eq[i][l]))
478 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
480 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
481 p = print_constraint(bmap, dim, p, c->el, l,
482 "=", i == bmap->n_eq - 1, set, latex);
484 for (i = 0; i < bmap->n_ineq; ++i) {
485 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
490 s = isl_int_sgn(bmap->ineq[i][l]);
492 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
494 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
495 op = s < 0 ? s_le[latex] : s_ge[latex];
496 p = print_constraint(bmap, dim, p, c->el, l,
497 op, !bmap->n_eq && !i, set, latex);
509 static __isl_give isl_printer *print_omega_constraints(
510 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int set)
512 if (bmap->n_eq + bmap->n_ineq == 0)
515 p = isl_printer_print_str(p, ": ");
516 if (bmap->n_div > 0) {
518 p = isl_printer_print_str(p, "exists (");
519 for (i = 0; i < bmap->n_div; ++i) {
521 p = isl_printer_print_str(p, ", ");
522 p = print_name(bmap->dim, p, isl_dim_div, i, 0, 0);
524 p = isl_printer_print_str(p, ": ");
526 p = print_constraints(bmap, bmap->dim, p, set, 0);
528 p = isl_printer_print_str(p, ")");
532 static __isl_give isl_printer *basic_map_print_omega(
533 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
535 p = isl_printer_print_str(p, "{ [");
536 p = print_var_list(bmap->dim, p, isl_dim_in, 0, 0, NULL);
537 p = isl_printer_print_str(p, "] -> [");
538 p = print_var_list(bmap->dim, p, isl_dim_out, 0, 0, NULL);
539 p = isl_printer_print_str(p, "] ");
540 p = print_omega_constraints(bmap, p, 0);
541 p = isl_printer_print_str(p, " }");
545 static __isl_give isl_printer *isl_basic_map_print_omega(
546 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
548 p = print_omega_parameters(bmap->dim, p);
550 p = isl_printer_start_line(p);
551 p = basic_map_print_omega(bmap, p);
552 p = isl_printer_end_line(p);
556 static __isl_give isl_printer *basic_set_print_omega(
557 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
559 p = isl_printer_print_str(p, "{ [");
560 p = print_var_list(bset->dim, p, isl_dim_set, 1, 0, NULL);
561 p = isl_printer_print_str(p, "] ");
562 p = print_omega_constraints((isl_basic_map *)bset, p, 1);
563 p = isl_printer_print_str(p, " }");
567 static __isl_give isl_printer *isl_basic_set_print_omega(
568 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
570 p = print_omega_parameters(bset->dim, p);
572 p = isl_printer_start_line(p);
573 p = basic_set_print_omega(bset, p);
574 p = isl_printer_end_line(p);
578 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
579 __isl_take isl_printer *p)
583 p = print_omega_parameters(map->dim, p);
585 p = isl_printer_start_line(p);
586 for (i = 0; i < map->n; ++i) {
588 p = isl_printer_print_str(p, " union ");
589 p = basic_map_print_omega(map->p[i], p);
591 p = isl_printer_end_line(p);
595 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
596 __isl_take isl_printer *p)
600 p = print_omega_parameters(set->dim, p);
602 p = isl_printer_start_line(p);
603 for (i = 0; i < set->n; ++i) {
605 p = isl_printer_print_str(p, " union ");
606 p = basic_set_print_omega(set->p[i], p);
608 p = isl_printer_end_line(p);
612 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
613 __isl_keep isl_dim *dim, __isl_take isl_printer *p, int set, int latex)
615 if (bmap->n_div > 0) {
617 p = isl_printer_print_str(p, s_open_exists[latex]);
618 for (i = 0; i < bmap->n_div; ++i) {
620 p = isl_printer_print_str(p, ", ");
621 p = print_name(dim, p, isl_dim_div, i, 0, latex);
622 if (latex || isl_int_is_zero(bmap->div[i][0]))
624 p = isl_printer_print_str(p, " = [(");
625 p = print_affine(bmap, dim, p, bmap->div[i] + 1, set);
626 p = isl_printer_print_str(p, ")/");
627 p = isl_printer_print_isl_int(p, bmap->div[i][0]);
628 p = isl_printer_print_str(p, "]");
630 p = isl_printer_print_str(p, ": ");
633 p = print_constraints(bmap, dim, p, set, latex);
636 p = isl_printer_print_str(p, s_close_exists[latex]);
640 static __isl_give isl_printer *isl_basic_map_print_isl(
641 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
644 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
645 if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
646 p = print_tuple(bmap->dim, p, isl_dim_param, 0, latex, NULL);
647 p = isl_printer_print_str(p, " -> ");
649 p = isl_printer_print_str(p, "{ ");
650 p = print_dim(bmap->dim, p, set, latex, rational, NULL);
651 p = isl_printer_print_str(p, " : ");
652 p = print_disjunct(bmap, bmap->dim, p, set, latex);
653 p = isl_printer_print_str(p, " }");
657 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
658 __isl_take isl_printer *p, int set, int latex)
662 if (isl_map_fast_is_universe(map))
665 p = isl_printer_print_str(p, s_such_that[latex]);
667 p = isl_printer_print_str(p, "1 = 0");
668 for (i = 0; i < map->n; ++i) {
670 p = isl_printer_print_str(p, s_or[latex]);
671 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
672 p = isl_printer_print_str(p, "(");
673 p = print_disjunct(map->p[i], map->dim, p, set, latex);
674 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
675 p = isl_printer_print_str(p, ")");
680 struct isl_aff_split {
685 static void free_split(__isl_take struct isl_aff_split *split, int n)
692 for (i = 0; i < n; ++i) {
693 isl_basic_map_free(split[i].aff);
694 isl_map_free(split[i].map);
700 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
703 unsigned nparam, n_in, n_out, total;
705 bmap = isl_basic_map_cow(bmap);
708 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
711 nparam = isl_basic_map_dim(bmap, isl_dim_param);
712 n_in = isl_basic_map_dim(bmap, isl_dim_in);
713 n_out = isl_basic_map_dim(bmap, isl_dim_out);
714 total = isl_basic_map_dim(bmap, isl_dim_all);
715 for (i = bmap->n_eq - 1; i >= 0; --i) {
716 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
717 if (j >= nparam && j < nparam + n_in + n_out &&
718 (isl_int_is_one(bmap->eq[i][1 + j]) ||
719 isl_int_is_negone(bmap->eq[i][1 + j])))
721 if (isl_basic_map_drop_equality(bmap, i) < 0)
725 bmap = isl_basic_map_finalize(bmap);
729 isl_basic_map_free(bmap);
733 static int aff_split_cmp(const void *p1, const void *p2)
735 const struct isl_aff_split *s1, *s2;
736 s1 = (const struct isl_aff_split *) p1;
737 s2 = (const struct isl_aff_split *) p2;
739 return isl_basic_map_fast_cmp(s1->aff, s2->aff);
742 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
743 __isl_keep isl_basic_map *aff)
751 total = isl_dim_total(bmap->dim);
753 for (i = bmap->n_eq - 1; i >= 0; --i) {
754 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
757 for (j = 0; j < aff->n_eq; ++j) {
758 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
759 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
761 if (isl_basic_map_drop_equality(bmap, i) < 0)
769 isl_basic_map_free(bmap);
773 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
776 struct isl_aff_split *split;
778 split = isl_calloc_array(map->isl, struct isl_aff_split, map->n);
782 for (i = 0; i < map->n; ++i) {
784 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
785 bmap = isl_basic_map_copy(map->p[i]);
786 bmap = isl_basic_map_cow(bmap);
787 bmap = drop_aff(bmap, split[i].aff);
788 split[i].map = isl_map_from_basic_map(bmap);
789 if (!split[i].aff || !split[i].map)
793 qsort(split, map->n, sizeof(struct isl_aff_split), &aff_split_cmp);
796 for (i = n - 1; i >= 1; --i) {
797 if (!isl_basic_map_fast_is_equal(split[i - 1].aff,
800 isl_basic_map_free(split[i].aff);
801 split[i - 1].map = isl_map_union(split[i - 1].map,
804 split[i] = split[n - 1];
805 split[n - 1].aff = NULL;
806 split[n - 1].map = NULL;
812 free_split(split, map->n);
816 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
817 struct isl_aff_split *split, int n, int set)
822 for (i = 0; i < n; ++i) {
827 dim = split[i].map->dim;
828 rational = split[i].map->n > 0 &&
829 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
831 p = isl_printer_print_str(p, "; ");
832 p = print_dim(dim, p, set, 0, rational, split[i].aff);
833 p = print_disjuncts(split[i].map, p, set, 0);
839 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
840 __isl_take isl_printer *p, int set)
842 struct isl_aff_split *split = NULL;
846 split = split_aff(map);
848 p = print_split_map(p, split, map->n, set);
850 rational = map->n > 0 &&
851 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
852 p = print_dim(map->dim, p, set, 0, rational, NULL);
853 p = print_disjuncts(map, p, set, 0);
855 free_split(split, map->n);
859 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
860 __isl_take isl_printer *p, int set)
862 if (isl_map_dim(map, isl_dim_param) > 0) {
863 p = print_tuple(map->dim, p, isl_dim_param, set, 0, NULL);
864 p = isl_printer_print_str(p, s_to[0]);
866 p = isl_printer_print_str(p, s_open_set[0]);
867 p = isl_map_print_isl_body(map, p, set);
868 p = isl_printer_print_str(p, s_close_set[0]);
872 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
873 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff, int set)
875 if (isl_map_dim(map, isl_dim_param) > 0) {
876 p = print_tuple(map->dim, p, isl_dim_param, set, 1, NULL);
877 p = isl_printer_print_str(p, s_to[1]);
879 p = isl_printer_print_str(p, s_open_set[1]);
880 p = print_dim(map->dim, p, set, 1, 0, aff);
881 p = print_disjuncts(map, p, set, 1);
882 p = isl_printer_print_str(p, s_close_set[1]);
887 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
888 __isl_take isl_printer *p, int set)
891 struct isl_aff_split *split = NULL;
894 split = split_aff(map);
897 return print_latex_map(map, p, NULL, set);
899 for (i = 0; i < map->n; ++i) {
903 p = isl_printer_print_str(p, " \\cup ");
904 p = print_latex_map(split[i].map, p, split[i].aff, set);
907 free_split(split, map->n);
911 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
912 __isl_keep isl_basic_map *bmap)
916 if (p->output_format == ISL_FORMAT_ISL)
917 return isl_basic_map_print_isl(bmap, p, 0, 0);
918 else if (p->output_format == ISL_FORMAT_OMEGA)
919 return isl_basic_map_print_omega(bmap, p);
920 isl_assert(bmap->ctx, 0, goto error);
926 void isl_basic_map_print(__isl_keep isl_basic_map *bmap, FILE *out, int indent,
927 const char *prefix, const char *suffix, unsigned output_format)
929 isl_printer *printer;
934 printer = isl_printer_to_file(bmap->ctx, out);
935 printer = isl_printer_set_indent(printer, indent);
936 printer = isl_printer_set_prefix(printer, prefix);
937 printer = isl_printer_set_suffix(printer, suffix);
938 printer = isl_printer_set_output_format(printer, output_format);
939 isl_printer_print_basic_map(printer, bmap);
941 isl_printer_free(printer);
944 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
945 __isl_keep isl_basic_set *bset)
950 if (p->output_format == ISL_FORMAT_ISL)
951 return isl_basic_map_print_isl(bset, p, 1, 0);
952 else if (p->output_format == ISL_FORMAT_POLYLIB)
953 return isl_basic_set_print_polylib(bset, p, 0);
954 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
955 return isl_basic_set_print_polylib(bset, p, 1);
956 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
957 return bset_print_constraints_polylib(bset, p);
958 else if (p->output_format == ISL_FORMAT_OMEGA)
959 return isl_basic_set_print_omega(bset, p);
960 isl_assert(p->ctx, 0, goto error);
966 void isl_basic_set_print(struct isl_basic_set *bset, FILE *out, int indent,
967 const char *prefix, const char *suffix, unsigned output_format)
969 isl_printer *printer;
974 printer = isl_printer_to_file(bset->ctx, out);
975 printer = isl_printer_set_indent(printer, indent);
976 printer = isl_printer_set_prefix(printer, prefix);
977 printer = isl_printer_set_suffix(printer, suffix);
978 printer = isl_printer_set_output_format(printer, output_format);
979 isl_printer_print_basic_set(printer, bset);
981 isl_printer_free(printer);
984 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
985 __isl_keep isl_set *set)
989 if (p->output_format == ISL_FORMAT_ISL)
990 return isl_map_print_isl((isl_map *)set, p, 1);
991 else if (p->output_format == ISL_FORMAT_POLYLIB)
992 return isl_set_print_polylib(set, p, 0);
993 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
994 return isl_set_print_polylib(set, p, 1);
995 else if (p->output_format == ISL_FORMAT_OMEGA)
996 return isl_set_print_omega(set, p);
997 else if (p->output_format == ISL_FORMAT_LATEX)
998 return isl_map_print_latex((isl_map *)set, p, 1);
999 isl_assert(set->ctx, 0, goto error);
1001 isl_printer_free(p);
1005 void isl_set_print(struct isl_set *set, FILE *out, int indent,
1006 unsigned output_format)
1008 isl_printer *printer;
1013 printer = isl_printer_to_file(set->ctx, out);
1014 printer = isl_printer_set_indent(printer, indent);
1015 printer = isl_printer_set_output_format(printer, output_format);
1016 printer = isl_printer_print_set(printer, set);
1018 isl_printer_free(printer);
1021 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1022 __isl_keep isl_map *map)
1027 if (p->output_format == ISL_FORMAT_ISL)
1028 return isl_map_print_isl(map, p, 0);
1029 else if (p->output_format == ISL_FORMAT_POLYLIB)
1030 return isl_map_print_polylib(map, p, 0);
1031 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1032 return isl_map_print_polylib(map, p, 1);
1033 else if (p->output_format == ISL_FORMAT_OMEGA)
1034 return isl_map_print_omega(map, p);
1035 else if (p->output_format == ISL_FORMAT_LATEX)
1036 return isl_map_print_latex(map, p, 0);
1037 isl_assert(map->ctx, 0, goto error);
1039 isl_printer_free(p);
1043 struct isl_union_print_data {
1049 static int print_map_body(__isl_take isl_map *map, void *user)
1051 struct isl_union_print_data *data;
1052 data = (struct isl_union_print_data *)user;
1055 data->p = isl_printer_print_str(data->p, "; ");
1058 data->p = isl_map_print_isl_body(map, data->p, data->set);
1064 static __isl_give isl_printer *isl_union_map_print_isl(
1065 __isl_keep isl_union_map *umap, __isl_take isl_printer *p, int set)
1067 struct isl_union_print_data data = { p, set, 1 };
1069 dim = isl_union_map_get_dim(umap);
1070 if (isl_dim_size(dim, isl_dim_param) > 0) {
1071 p = print_tuple(dim, p, isl_dim_param, set, 0, NULL);
1072 p = isl_printer_print_str(p, s_to[0]);
1075 p = isl_printer_print_str(p, s_open_set[0]);
1076 isl_union_map_foreach_map(umap, &print_map_body, &data);
1078 p = isl_printer_print_str(p, s_close_set[0]);
1082 static int print_latex_map_body(__isl_take isl_map *map, void *user)
1084 struct isl_union_print_data *data;
1085 data = (struct isl_union_print_data *)user;
1088 data->p = isl_printer_print_str(data->p, " \\cup ");
1091 data->p = isl_map_print_latex(map, data->p, data->set);
1097 static __isl_give isl_printer *isl_union_map_print_latex(
1098 __isl_keep isl_union_map *umap, __isl_take isl_printer *p, int set)
1100 struct isl_union_print_data data = { p, set, 1 };
1101 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1106 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1107 __isl_keep isl_union_map *umap)
1112 if (p->output_format == ISL_FORMAT_ISL)
1113 return isl_union_map_print_isl(umap, p, 0);
1114 if (p->output_format == ISL_FORMAT_LATEX)
1115 return isl_union_map_print_latex(umap, p, 0);
1117 isl_die(p->ctx, isl_error_invalid,
1118 "invalid output format for isl_union_map", goto error);
1120 isl_printer_free(p);
1124 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1125 __isl_keep isl_union_set *uset)
1130 if (p->output_format == ISL_FORMAT_ISL)
1131 return isl_union_map_print_isl((isl_union_map *)uset, p, 1);
1132 if (p->output_format == ISL_FORMAT_LATEX)
1133 return isl_union_map_print_latex((isl_union_map *)uset, p, 1);
1135 isl_die(p->ctx, isl_error_invalid,
1136 "invalid output format for isl_union_set", goto error);
1138 isl_printer_free(p);
1142 void isl_map_print(__isl_keep isl_map *map, FILE *out, int indent,
1143 unsigned output_format)
1145 isl_printer *printer;
1150 printer = isl_printer_to_file(map->ctx, out);
1151 printer = isl_printer_set_indent(printer, indent);
1152 printer = isl_printer_set_output_format(printer, output_format);
1153 printer = isl_printer_print_map(printer, map);
1155 isl_printer_free(printer);
1158 void isl_basic_map_dump(__isl_keep isl_basic_map *bmap)
1160 isl_printer *printer;
1165 printer = isl_printer_to_file(isl_basic_map_get_ctx(bmap), stderr);
1166 printer = isl_printer_print_basic_map(printer, bmap);
1167 printer = isl_printer_end_line(printer);
1169 isl_printer_free(printer);
1172 void isl_basic_set_dump(__isl_keep isl_basic_set *bset)
1174 isl_printer *printer;
1179 printer = isl_printer_to_file(isl_basic_set_get_ctx(bset), stderr);
1180 printer = isl_printer_print_basic_set(printer, bset);
1181 printer = isl_printer_end_line(printer);
1183 isl_printer_free(printer);
1186 void isl_map_dump(__isl_keep isl_map *map)
1188 isl_printer *printer;
1193 printer = isl_printer_to_file(isl_map_get_ctx(map), stderr);
1194 printer = isl_printer_print_map(printer, map);
1195 printer = isl_printer_end_line(printer);
1197 isl_printer_free(printer);
1200 void isl_set_dump(__isl_keep isl_set *set)
1202 isl_printer *printer;
1207 printer = isl_printer_to_file(isl_set_get_ctx(set), stderr);
1208 printer = isl_printer_print_set(printer, set);
1209 printer = isl_printer_end_line(printer);
1211 isl_printer_free(printer);
1214 void isl_union_map_dump(__isl_keep isl_union_map *umap)
1216 isl_printer *printer;
1221 printer = isl_printer_to_file(isl_union_map_get_ctx(umap), stderr);
1222 printer = isl_printer_print_union_map(printer, umap);
1223 printer = isl_printer_end_line(printer);
1225 isl_printer_free(printer);
1228 void isl_union_set_dump(__isl_keep isl_union_set *uset)
1230 isl_printer *printer;
1235 printer = isl_printer_to_file(isl_union_set_get_ctx(uset), stderr);
1236 printer = isl_printer_print_union_set(printer, uset);
1237 printer = isl_printer_end_line(printer);
1239 isl_printer_free(printer);
1242 void isl_union_pw_qpolynomial_dump(__isl_keep isl_union_pw_qpolynomial *upwqp)
1244 isl_printer *printer;
1249 printer = isl_printer_to_file(isl_union_pw_qpolynomial_get_ctx(upwqp),
1251 printer = isl_printer_print_union_pw_qpolynomial(printer, upwqp);
1252 printer = isl_printer_end_line(printer);
1254 isl_printer_free(printer);
1257 void isl_qpolynomial_dump(__isl_keep isl_qpolynomial *qp)
1259 isl_printer *printer;
1264 printer = isl_printer_to_file(isl_qpolynomial_get_ctx(qp), stderr);
1265 printer = isl_printer_print_qpolynomial(printer, qp);
1266 printer = isl_printer_end_line(printer);
1268 isl_printer_free(printer);
1271 void isl_qpolynomial_fold_dump(__isl_keep isl_qpolynomial_fold *fold)
1273 isl_printer *printer;
1278 printer = isl_printer_to_file(isl_qpolynomial_fold_get_ctx(fold),
1280 printer = isl_printer_print_qpolynomial_fold(printer, fold);
1281 printer = isl_printer_end_line(printer);
1283 isl_printer_free(printer);
1286 void isl_pw_qpolynomial_dump(__isl_keep isl_pw_qpolynomial *pwqp)
1288 isl_printer *printer;
1293 printer = isl_printer_to_file(isl_pw_qpolynomial_get_ctx(pwqp), stderr);
1294 printer = isl_printer_print_pw_qpolynomial(printer, pwqp);
1295 printer = isl_printer_end_line(printer);
1297 isl_printer_free(printer);
1300 void isl_pw_qpolynomial_fold_dump(__isl_keep isl_pw_qpolynomial_fold *pwf)
1302 isl_printer *printer;
1307 printer = isl_printer_to_file(
1308 isl_pw_qpolynomial_fold_get_ctx(pwf), stderr);
1309 printer = isl_printer_print_pw_qpolynomial_fold(printer, pwf);
1310 printer = isl_printer_end_line(printer);
1312 isl_printer_free(printer);
1315 void isl_union_pw_qpolynomial_fold_dump(
1316 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1318 isl_printer *printer;
1323 printer = isl_printer_to_file(
1324 isl_union_pw_qpolynomial_fold_get_ctx(upwf), stderr);
1325 printer = isl_printer_print_union_pw_qpolynomial_fold(printer, upwf);
1326 printer = isl_printer_end_line(printer);
1328 isl_printer_free(printer);
1331 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
1336 for (i = 0, n = 0; i < rec->n; ++i)
1337 if (!isl_upoly_is_zero(rec->p[i]))
1343 static __isl_give isl_printer *print_div(__isl_keep isl_dim *dim,
1344 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
1346 int c = p->output_format == ISL_FORMAT_C;
1347 p = isl_printer_print_str(p, c ? "floord(" : "[(");
1348 p = print_affine_of_len(dim, div, p,
1349 div->row[pos] + 1, div->n_col - 1, 1);
1350 p = isl_printer_print_str(p, c ? ", " : ")/");
1351 p = isl_printer_print_isl_int(p, div->row[pos][0]);
1352 p = isl_printer_print_str(p, c ? ")" : "]");
1356 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
1357 __isl_take isl_printer *p, int first)
1359 struct isl_upoly_cst *cst;
1362 cst = isl_upoly_as_cst(up);
1365 neg = !first && isl_int_is_neg(cst->n);
1367 p = isl_printer_print_str(p, neg ? " - " : " + ");
1369 isl_int_neg(cst->n, cst->n);
1370 if (isl_int_is_zero(cst->d)) {
1371 int sgn = isl_int_sgn(cst->n);
1372 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1373 sgn == 0 ? "NaN" : "infty");
1375 p = isl_printer_print_isl_int(p, cst->n);
1377 isl_int_neg(cst->n, cst->n);
1378 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1379 p = isl_printer_print_str(p, "/");
1380 p = isl_printer_print_isl_int(p, cst->d);
1384 isl_printer_free(p);
1388 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1389 __isl_keep isl_dim *dim, __isl_keep isl_mat *div, int var)
1393 total = isl_dim_total(dim);
1395 p = print_term(dim, NULL, dim->ctx->one, 1 + var, p, 1, 0);
1397 p = print_div(dim, div, var - total, p);
1401 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1402 __isl_keep isl_dim *dim, __isl_keep isl_mat *div, int var, int exp)
1404 p = print_base(p, dim, div, var);
1407 if (p->output_format == ISL_FORMAT_C) {
1409 for (i = 1; i < exp; ++i) {
1410 p = isl_printer_print_str(p, "*");
1411 p = print_base(p, dim, div, var);
1414 p = isl_printer_print_str(p, "^");
1415 p = isl_printer_print_int(p, exp);
1420 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1421 __isl_keep isl_dim *dim, __isl_keep isl_mat *div,
1422 __isl_take isl_printer *p, int outer)
1424 int i, n, first, print_parens;
1425 struct isl_upoly_rec *rec;
1427 if (!p || !up || !dim || !div)
1430 if (isl_upoly_is_cst(up))
1431 return upoly_print_cst(up, p, 1);
1433 rec = isl_upoly_as_rec(up);
1436 n = upoly_rec_n_non_zero(rec);
1437 print_parens = n > 1 || (outer && rec->up.var >= isl_dim_total(dim));
1439 p = isl_printer_print_str(p, "(");
1440 for (i = 0, first = 1; i < rec->n; ++i) {
1441 if (isl_upoly_is_zero(rec->p[i]))
1443 if (isl_upoly_is_negone(rec->p[i])) {
1445 p = isl_printer_print_str(p, "-1");
1447 p = isl_printer_print_str(p, "-");
1449 p = isl_printer_print_str(p, " - ");
1450 } else if (isl_upoly_is_cst(rec->p[i]) &&
1451 !isl_upoly_is_one(rec->p[i]))
1452 p = upoly_print_cst(rec->p[i], p, first);
1455 p = isl_printer_print_str(p, " + ");
1456 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1457 p = upoly_print(rec->p[i], dim, div, p, 0);
1462 if (!isl_upoly_is_one(rec->p[i]) &&
1463 !isl_upoly_is_negone(rec->p[i]))
1464 p = isl_printer_print_str(p, " * ");
1465 p = print_pow(p, dim, div, rec->up.var, i);
1468 p = isl_printer_print_str(p, ")");
1471 isl_printer_free(p);
1475 __isl_give isl_printer *isl_printer_print_qpolynomial(__isl_take isl_printer *p,
1476 __isl_keep isl_qpolynomial *qp)
1480 p = upoly_print(qp->upoly, qp->dim, qp->div, p, 1);
1483 isl_printer_free(p);
1487 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1488 unsigned output_format)
1495 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1496 p = isl_printer_to_file(qp->dim->ctx, out);
1497 p = isl_printer_print_qpolynomial(p, qp);
1498 isl_printer_free(p);
1501 static __isl_give isl_printer *qpolynomial_fold_print(
1502 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1506 if (fold->type == isl_fold_min)
1507 p = isl_printer_print_str(p, "min");
1508 else if (fold->type == isl_fold_max)
1509 p = isl_printer_print_str(p, "max");
1510 p = isl_printer_print_str(p, "(");
1511 for (i = 0; i < fold->n; ++i) {
1513 p = isl_printer_print_str(p, ", ");
1514 p = isl_printer_print_qpolynomial(p, fold->qp[i]);
1516 p = isl_printer_print_str(p, ")");
1520 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1521 FILE *out, unsigned output_format)
1528 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1530 p = isl_printer_to_file(fold->dim->ctx, out);
1531 p = isl_printer_print_qpolynomial_fold(p, fold);
1533 isl_printer_free(p);
1536 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1537 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1541 for (i = 0; i < pwqp->n; ++i) {
1543 p = isl_printer_print_str(p, "; ");
1544 if (isl_dim_size(pwqp->dim, isl_dim_set) > 0 ||
1545 isl_dim_is_named_or_nested(pwqp->dim, isl_dim_set)) {
1546 p = print_dim(pwqp->p[i].set->dim, p, 1, 0, 0, NULL);
1547 p = isl_printer_print_str(p, " -> ");
1549 p = isl_printer_print_qpolynomial(p, pwqp->p[i].qp);
1550 p = print_disjuncts((isl_map *)pwqp->p[i].set, p, 1, 0);
1556 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1557 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1562 if (isl_dim_size(pwqp->dim, isl_dim_param) > 0) {
1563 p = print_tuple(pwqp->dim, p, isl_dim_param, 0, 0, NULL);
1564 p = isl_printer_print_str(p, " -> ");
1566 p = isl_printer_print_str(p, "{ ");
1568 if (isl_dim_size(pwqp->dim, isl_dim_set) > 0 ||
1569 isl_dim_is_named_or_nested(pwqp->dim, isl_dim_set)) {
1570 p = print_dim(pwqp->dim, p, 1, 0, 0, NULL);
1571 p = isl_printer_print_str(p, " -> ");
1573 p = isl_printer_print_str(p, "0");
1575 p = isl_pwqp_print_isl_body(p, pwqp);
1576 p = isl_printer_print_str(p, " }");
1579 isl_printer_free(p);
1583 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
1584 unsigned output_format)
1591 p = isl_printer_to_file(pwqp->dim->ctx, out);
1592 p = isl_printer_set_output_format(p, output_format);
1593 p = isl_printer_print_pw_qpolynomial(p, pwqp);
1595 isl_printer_free(p);
1598 static __isl_give isl_printer *isl_pwf_print_isl_body(
1599 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1603 for (i = 0; i < pwf->n; ++i) {
1605 p = isl_printer_print_str(p, "; ");
1606 if (isl_dim_size(pwf->dim, isl_dim_set) > 0 ||
1607 isl_dim_is_named_or_nested(pwf->dim, isl_dim_set)) {
1608 p = print_dim(pwf->p[i].set->dim, p, 1, 0, 0, NULL);
1609 p = isl_printer_print_str(p, " -> ");
1611 p = qpolynomial_fold_print(pwf->p[i].fold, p);
1612 p = print_disjuncts((isl_map *)pwf->p[i].set, p, 1, 0);
1618 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
1619 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1621 if (isl_dim_size(pwf->dim, isl_dim_param) > 0) {
1622 p = print_tuple(pwf->dim, p, isl_dim_param, 0, 0, NULL);
1623 p = isl_printer_print_str(p, " -> ");
1625 p = isl_printer_print_str(p, "{ ");
1627 if (isl_dim_size(pwf->dim, isl_dim_set) > 0 ||
1628 isl_dim_is_named_or_nested(pwf->dim, isl_dim_set)) {
1629 p = print_dim(pwf->dim, p, 1, 0, 0, NULL);
1630 p = isl_printer_print_str(p, " -> ");
1632 p = isl_printer_print_str(p, "0");
1634 p = isl_pwf_print_isl_body(p, pwf);
1635 p = isl_printer_print_str(p, " }");
1639 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1640 __isl_keep isl_dim *dim, __isl_keep isl_basic_set *bset, isl_int *c);
1642 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1643 __isl_keep isl_dim *dim,
1644 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1646 if (type == isl_dim_div) {
1647 p = isl_printer_print_str(p, "floord(");
1648 p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
1649 p = isl_printer_print_str(p, ", ");
1650 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1651 p = isl_printer_print_str(p, ")");
1655 name = isl_dim_get_name(dim, type, pos);
1658 p = isl_printer_print_str(p, name);
1663 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1664 __isl_keep isl_dim *dim,
1665 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1667 enum isl_dim_type type;
1668 unsigned nparam = isl_basic_set_dim(bset, isl_dim_param);
1671 return isl_printer_print_isl_int(p, c);
1673 if (isl_int_is_one(c))
1675 else if (isl_int_is_negone(c))
1676 p = isl_printer_print_str(p, "-");
1678 p = isl_printer_print_isl_int(p, c);
1679 p = isl_printer_print_str(p, "*");
1681 type = pos2type(dim, &pos);
1682 p = print_name_c(p, dim, bset, type, pos);
1686 static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
1687 __isl_keep isl_dim *dim,
1688 __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
1693 for (i = 0, first = 1; i < len; ++i) {
1695 if (isl_int_is_zero(c[i]))
1698 if (isl_int_is_neg(c[i])) {
1700 isl_int_neg(c[i], c[i]);
1701 p = isl_printer_print_str(p, " - ");
1703 p = isl_printer_print_str(p, " + ");
1706 p = print_term_c(p, dim, bset, c[i], i);
1708 isl_int_neg(c[i], c[i]);
1711 p = isl_printer_print_str(p, "0");
1715 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1716 __isl_keep isl_dim *dim, __isl_keep isl_basic_set *bset, isl_int *c)
1718 unsigned len = 1 + isl_basic_set_total_dim(bset);
1719 return print_partial_affine_c(p, dim, bset, c, len);
1722 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1723 __isl_keep isl_dim *dim,
1724 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int first)
1727 p = isl_printer_print_str(p, " && ");
1729 p = print_affine_c(p, dim, bset, c);
1730 p = isl_printer_print_str(p, " ");
1731 p = isl_printer_print_str(p, op);
1732 p = isl_printer_print_str(p, " 0");
1736 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1737 __isl_keep isl_dim *dim, __isl_keep isl_basic_set *bset)
1740 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
1741 unsigned total = isl_basic_set_total_dim(bset) - n_div;
1743 for (i = 0; i < bset->n_eq; ++i) {
1744 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
1746 p = print_constraint_c(p, dim, bset,
1747 bset->eq[i], "==", !i);
1750 p = isl_printer_print_str(p, " && ");
1751 p = isl_printer_print_str(p, "(");
1752 p = print_partial_affine_c(p, dim, bset, bset->eq[i],
1754 p = isl_printer_print_str(p, ") % ");
1755 p = isl_printer_print_isl_int(p,
1756 bset->eq[i][1 + total + j]);
1757 p = isl_printer_print_str(p, " == 0");
1760 for (i = 0; i < bset->n_ineq; ++i)
1761 p = print_constraint_c(p, dim, bset, bset->ineq[i], ">=",
1766 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
1767 __isl_keep isl_dim *dim, __isl_keep isl_set *set)
1772 p = isl_printer_print_str(p, "0");
1774 for (i = 0; i < set->n; ++i) {
1776 p = isl_printer_print_str(p, " || ");
1778 p = isl_printer_print_str(p, "(");
1779 p = print_basic_set_c(p, dim, set->p[i]);
1781 p = isl_printer_print_str(p, ")");
1786 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1787 __isl_keep isl_dim *dim, __isl_keep isl_qpolynomial *qp)
1792 isl_qpolynomial_get_den(qp, &den);
1793 if (!isl_int_is_one(den)) {
1795 p = isl_printer_print_str(p, "(");
1796 qp = isl_qpolynomial_copy(qp);
1797 f = isl_qpolynomial_rat_cst(isl_dim_copy(qp->dim),
1798 den, qp->dim->ctx->one);
1799 qp = isl_qpolynomial_mul(qp, f);
1802 p = upoly_print(qp->upoly, dim, qp->div, p, 0);
1803 if (!isl_int_is_one(den)) {
1804 p = isl_printer_print_str(p, ")/");
1805 p = isl_printer_print_isl_int(p, den);
1806 isl_qpolynomial_free(qp);
1812 static __isl_give isl_printer *print_pw_qpolynomial_c(
1813 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1817 if (pwqp->n == 1 && isl_set_fast_is_universe(pwqp->p[0].set))
1818 return print_qpolynomial_c(p, pwqp->dim, pwqp->p[0].qp);
1820 for (i = 0; i < pwqp->n; ++i) {
1821 p = isl_printer_print_str(p, "(");
1822 p = print_set_c(p, pwqp->dim, pwqp->p[i].set);
1823 p = isl_printer_print_str(p, ") ? (");
1824 p = print_qpolynomial_c(p, pwqp->dim, pwqp->p[i].qp);
1825 p = isl_printer_print_str(p, ") : ");
1828 p = isl_printer_print_str(p, "0");
1832 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
1833 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1838 if (p->output_format == ISL_FORMAT_ISL)
1839 return print_pw_qpolynomial_isl(p, pwqp);
1840 else if (p->output_format == ISL_FORMAT_C)
1841 return print_pw_qpolynomial_c(p, pwqp);
1842 isl_assert(p->ctx, 0, goto error);
1844 isl_printer_free(p);
1848 static int print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1850 struct isl_union_print_data *data;
1851 data = (struct isl_union_print_data *)user;
1854 data->p = isl_printer_print_str(data->p, "; ");
1857 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
1858 isl_pw_qpolynomial_free(pwqp);
1863 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
1864 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1866 struct isl_union_print_data data = { p, 1, 1 };
1868 dim = isl_union_pw_qpolynomial_get_dim(upwqp);
1869 if (isl_dim_size(dim, isl_dim_param) > 0) {
1870 p = print_tuple(dim, p, isl_dim_param, 1, 0, NULL);
1871 p = isl_printer_print_str(p, " -> ");
1874 p = isl_printer_print_str(p, "{ ");
1875 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
1878 p = isl_printer_print_str(p, " }");
1882 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
1883 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1888 if (p->output_format == ISL_FORMAT_ISL)
1889 return print_union_pw_qpolynomial_isl(p, upwqp);
1890 isl_die(p->ctx, isl_error_invalid,
1891 "invalid output format for isl_union_pw_qpolynomial",
1894 isl_printer_free(p);
1898 static __isl_give isl_printer *print_qpolynomial_fold_c(
1899 __isl_take isl_printer *p, __isl_keep isl_dim *dim,
1900 __isl_keep isl_qpolynomial_fold *fold)
1904 for (i = 0; i < fold->n - 1; ++i)
1905 if (fold->type == isl_fold_min)
1906 p = isl_printer_print_str(p, "min(");
1907 else if (fold->type == isl_fold_max)
1908 p = isl_printer_print_str(p, "max(");
1910 for (i = 0; i < fold->n; ++i) {
1912 p = isl_printer_print_str(p, ", ");
1913 p = print_qpolynomial_c(p, dim, fold->qp[i]);
1915 p = isl_printer_print_str(p, ")");
1920 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
1921 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
1925 if (p->output_format == ISL_FORMAT_ISL)
1926 return qpolynomial_fold_print(fold, p);
1927 else if (p->output_format == ISL_FORMAT_C)
1928 return print_qpolynomial_fold_c(p, fold->dim, fold);
1929 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
1932 isl_printer_free(p);
1936 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
1937 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1941 if (pwf->n == 1 && isl_set_fast_is_universe(pwf->p[0].set))
1942 return print_qpolynomial_fold_c(p, pwf->dim, pwf->p[0].fold);
1944 for (i = 0; i < pwf->n; ++i) {
1945 p = isl_printer_print_str(p, "(");
1946 p = print_set_c(p, pwf->dim, pwf->p[i].set);
1947 p = isl_printer_print_str(p, ") ? (");
1948 p = print_qpolynomial_fold_c(p, pwf->dim, pwf->p[i].fold);
1949 p = isl_printer_print_str(p, ") : ");
1952 p = isl_printer_print_str(p, "0");
1956 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
1957 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1962 if (p->output_format == ISL_FORMAT_ISL)
1963 return print_pw_qpolynomial_fold_isl(p, pwf);
1964 else if (p->output_format == ISL_FORMAT_C)
1965 return print_pw_qpolynomial_fold_c(p, pwf);
1966 isl_assert(p->ctx, 0, goto error);
1968 isl_printer_free(p);
1972 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
1973 FILE *out, unsigned output_format)
1980 p = isl_printer_to_file(pwf->dim->ctx, out);
1981 p = isl_printer_set_output_format(p, output_format);
1982 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
1984 isl_printer_free(p);
1987 static int print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf, void *user)
1989 struct isl_union_print_data *data;
1990 data = (struct isl_union_print_data *)user;
1993 data->p = isl_printer_print_str(data->p, "; ");
1996 data->p = isl_pwf_print_isl_body(data->p, pwf);
1997 isl_pw_qpolynomial_fold_free(pwf);
2002 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
2003 __isl_take isl_printer *p,
2004 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2006 struct isl_union_print_data data = { p, 1, 1 };
2008 dim = isl_union_pw_qpolynomial_fold_get_dim(upwf);
2009 if (isl_dim_size(dim, isl_dim_param) > 0) {
2010 p = print_tuple(dim, p, isl_dim_param, 1, 0, NULL);
2011 p = isl_printer_print_str(p, " -> ");
2014 p = isl_printer_print_str(p, "{ ");
2015 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
2016 &print_pwf_body, &data);
2018 p = isl_printer_print_str(p, " }");
2022 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2023 __isl_take isl_printer *p,
2024 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2029 if (p->output_format == ISL_FORMAT_ISL)
2030 return print_union_pw_qpolynomial_fold_isl(p, upwf);
2031 isl_die(p->ctx, isl_error_invalid,
2032 "invalid output format for isl_union_pw_qpolynomial_fold",
2035 isl_printer_free(p);