2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
17 #include <isl_ctx_private.h>
18 #include <isl_map_private.h>
21 #include <isl_polynomial_private.h>
22 #include <isl_printer_private.h>
23 #include <isl_space_private.h>
24 #include <isl_mat_private.h>
25 #include <isl_vec_private.h>
26 #include <isl/union_map.h>
27 #include <isl/constraint.h>
28 #include <isl_local_space_private.h>
29 #include <isl_aff_private.h>
30 #include <isl_val_private.h>
31 #include <isl/ast_build.h>
34 static const char *s_to
[2] = { " -> ", " \\to " };
35 static const char *s_and
[2] = { " and ", " \\wedge " };
36 static const char *s_or
[2] = { " or ", " \\vee " };
37 static const char *s_le
[2] = { "<=", "\\le" };
38 static const char *s_ge
[2] = { ">=", "\\ge" };
39 static const char *s_open_set
[2] = { "{ ", "\\{\\, " };
40 static const char *s_close_set
[2] = { " }", " \\,\\}" };
41 static const char *s_open_list
[2] = { "[", "(" };
42 static const char *s_close_list
[2] = { "]", ")" };
43 static const char *s_such_that
[2] = { " : ", " \\mid " };
44 static const char *s_open_exists
[2] = { "exists (", "\\exists \\, " };
45 static const char *s_close_exists
[2] = { ")", "" };
46 static const char *s_div_prefix
[2] = { "e", "\\alpha_" };
47 static const char *s_param_prefix
[2] = { "p", "p_" };
48 static const char *s_input_prefix
[2] = { "i", "i_" };
49 static const char *s_output_prefix
[2] = { "o", "o_" };
51 static __isl_give isl_printer
*print_constraint_polylib(
52 struct isl_basic_map
*bmap
, int ineq
, int n
, __isl_take isl_printer
*p
)
55 unsigned n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
56 unsigned n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
57 unsigned nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
58 isl_int
*c
= ineq
? bmap
->ineq
[n
] : bmap
->eq
[n
];
60 p
= isl_printer_start_line(p
);
61 p
= isl_printer_print_int(p
, ineq
);
62 for (i
= 0; i
< n_out
; ++i
) {
63 p
= isl_printer_print_str(p
, " ");
64 p
= isl_printer_print_isl_int(p
, c
[1+nparam
+n_in
+i
]);
66 for (i
= 0; i
< n_in
; ++i
) {
67 p
= isl_printer_print_str(p
, " ");
68 p
= isl_printer_print_isl_int(p
, c
[1+nparam
+i
]);
70 for (i
= 0; i
< bmap
->n_div
; ++i
) {
71 p
= isl_printer_print_str(p
, " ");
72 p
= isl_printer_print_isl_int(p
, c
[1+nparam
+n_in
+n_out
+i
]);
74 for (i
= 0; i
< nparam
; ++i
) {
75 p
= isl_printer_print_str(p
, " ");
76 p
= isl_printer_print_isl_int(p
, c
[1+i
]);
78 p
= isl_printer_print_str(p
, " ");
79 p
= isl_printer_print_isl_int(p
, c
[0]);
80 p
= isl_printer_end_line(p
);
84 static __isl_give isl_printer
*print_constraints_polylib(
85 struct isl_basic_map
*bmap
, __isl_take isl_printer
*p
)
89 p
= isl_printer_set_isl_int_width(p
, 5);
91 for (i
= 0; i
< bmap
->n_eq
; ++i
)
92 p
= print_constraint_polylib(bmap
, 0, i
, p
);
93 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
94 p
= print_constraint_polylib(bmap
, 1, i
, p
);
99 static __isl_give isl_printer
*bset_print_constraints_polylib(
100 struct isl_basic_set
*bset
, __isl_take isl_printer
*p
)
102 return print_constraints_polylib((struct isl_basic_map
*)bset
, p
);
105 static __isl_give isl_printer
*isl_basic_map_print_polylib(
106 __isl_keep isl_basic_map
*bmap
, __isl_take isl_printer
*p
, int ext
)
108 unsigned total
= isl_basic_map_total_dim(bmap
);
109 p
= isl_printer_start_line(p
);
110 p
= isl_printer_print_int(p
, bmap
->n_eq
+ bmap
->n_ineq
);
111 p
= isl_printer_print_str(p
, " ");
112 p
= isl_printer_print_int(p
, 1 + total
+ 1);
114 p
= isl_printer_print_str(p
, " ");
115 p
= isl_printer_print_int(p
,
116 isl_basic_map_dim(bmap
, isl_dim_out
));
117 p
= isl_printer_print_str(p
, " ");
118 p
= isl_printer_print_int(p
,
119 isl_basic_map_dim(bmap
, isl_dim_in
));
120 p
= isl_printer_print_str(p
, " ");
121 p
= isl_printer_print_int(p
,
122 isl_basic_map_dim(bmap
, isl_dim_div
));
123 p
= isl_printer_print_str(p
, " ");
124 p
= isl_printer_print_int(p
,
125 isl_basic_map_dim(bmap
, isl_dim_param
));
127 p
= isl_printer_end_line(p
);
128 return print_constraints_polylib(bmap
, p
);
131 static __isl_give isl_printer
*isl_basic_set_print_polylib(
132 __isl_keep isl_basic_set
*bset
, __isl_take isl_printer
*p
, int ext
)
134 return isl_basic_map_print_polylib((struct isl_basic_map
*)bset
, p
, ext
);
137 static __isl_give isl_printer
*isl_map_print_polylib(__isl_keep isl_map
*map
,
138 __isl_take isl_printer
*p
, int ext
)
142 p
= isl_printer_start_line(p
);
143 p
= isl_printer_print_int(p
, map
->n
);
144 p
= isl_printer_end_line(p
);
145 for (i
= 0; i
< map
->n
; ++i
) {
146 p
= isl_printer_start_line(p
);
147 p
= isl_printer_end_line(p
);
148 p
= isl_basic_map_print_polylib(map
->p
[i
], p
, ext
);
153 static __isl_give isl_printer
*isl_set_print_polylib(__isl_keep isl_set
*set
,
154 __isl_take isl_printer
*p
, int ext
)
156 return isl_map_print_polylib((struct isl_map
*)set
, p
, ext
);
159 static int count_same_name(__isl_keep isl_space
*dim
,
160 enum isl_dim_type type
, unsigned pos
, const char *name
)
166 for (t
= isl_dim_param
; t
<= type
&& t
<= isl_dim_out
; ++t
) {
167 s
= t
== type
? pos
: isl_space_dim(dim
, t
);
168 for (p
= 0; p
< s
; ++p
) {
169 const char *n
= isl_space_get_dim_name(dim
, t
, p
);
170 if (n
&& !strcmp(n
, name
))
177 static __isl_give isl_printer
*print_name(__isl_keep isl_space
*dim
,
178 __isl_take isl_printer
*p
, enum isl_dim_type type
, unsigned pos
,
185 name
= type
== isl_dim_div
? NULL
: isl_space_get_dim_name(dim
, type
, pos
);
189 if (type
== isl_dim_param
)
190 prefix
= s_param_prefix
[latex
];
191 else if (type
== isl_dim_div
)
192 prefix
= s_div_prefix
[latex
];
193 else if (isl_space_is_set(dim
) || type
== isl_dim_in
)
194 prefix
= s_input_prefix
[latex
];
196 prefix
= s_output_prefix
[latex
];
197 snprintf(buffer
, sizeof(buffer
), "%s%d", prefix
, pos
);
200 primes
= count_same_name(dim
, name
== buffer
? isl_dim_div
: type
,
202 p
= isl_printer_print_str(p
, name
);
204 p
= isl_printer_print_str(p
, "'");
208 static enum isl_dim_type
pos2type(__isl_keep isl_space
*dim
, unsigned *pos
)
210 enum isl_dim_type type
;
211 unsigned n_in
= isl_space_dim(dim
, isl_dim_in
);
212 unsigned n_out
= isl_space_dim(dim
, isl_dim_out
);
213 unsigned nparam
= isl_space_dim(dim
, isl_dim_param
);
215 if (*pos
< 1 + nparam
) {
216 type
= isl_dim_param
;
218 } else if (*pos
< 1 + nparam
+ n_in
) {
221 } else if (*pos
< 1 + nparam
+ n_in
+ n_out
) {
223 *pos
-= 1 + nparam
+ n_in
;
226 *pos
-= 1 + nparam
+ n_in
+ n_out
;
232 static __isl_give isl_printer
*print_div(__isl_keep isl_space
*dim
,
233 __isl_keep isl_mat
*div
, int pos
, __isl_take isl_printer
*p
);
235 static __isl_give isl_printer
*print_term(__isl_keep isl_space
*dim
,
236 __isl_keep isl_mat
*div
,
237 isl_int c
, unsigned pos
, __isl_take isl_printer
*p
, int latex
)
239 enum isl_dim_type type
;
243 return isl_printer_print_isl_int(p
, c
);
245 type
= pos2type(dim
, &pos
);
246 print_div_def
= type
== isl_dim_div
&& div
&&
247 !isl_int_is_zero(div
->row
[pos
][0]);
249 if (isl_int_is_one(c
))
251 else if (isl_int_is_negone(c
))
252 p
= isl_printer_print_str(p
, "-");
254 p
= isl_printer_print_isl_int(p
, c
);
255 if (p
->output_format
== ISL_FORMAT_C
|| print_div_def
)
256 p
= isl_printer_print_str(p
, "*");
259 p
= print_div(dim
, div
, pos
, p
);
261 p
= print_name(dim
, p
, type
, pos
, latex
);
265 static __isl_give isl_printer
*print_affine_of_len(__isl_keep isl_space
*dim
,
266 __isl_keep isl_mat
*div
,
267 __isl_take isl_printer
*p
, isl_int
*c
, int len
)
272 for (i
= 0, first
= 1; i
< len
; ++i
) {
274 if (isl_int_is_zero(c
[i
]))
277 if (isl_int_is_neg(c
[i
])) {
279 isl_int_neg(c
[i
], c
[i
]);
280 p
= isl_printer_print_str(p
, " - ");
282 p
= isl_printer_print_str(p
, " + ");
285 p
= print_term(dim
, div
, c
[i
], i
, p
, 0);
287 isl_int_neg(c
[i
], c
[i
]);
290 p
= isl_printer_print_str(p
, "0");
294 static __isl_give isl_printer
*print_affine(__isl_keep isl_basic_map
*bmap
,
295 __isl_keep isl_space
*dim
, __isl_take isl_printer
*p
, isl_int
*c
)
297 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
298 return print_affine_of_len(dim
, NULL
, p
, c
, len
);
301 /* Internal data structure for print_space.
303 * latex is set if that is the output format.
304 * print_dim (if not NULL) is called on each dimension.
305 * user is set by the caller of print_space and may be used inside print_dim.
307 * space is the global space that is being printed. This field is set by
309 * type is the tuple of the global space that is currently being printed.
310 * This field is set by print_space.
312 struct isl_print_space_data
{
314 __isl_give isl_printer
*(*print_dim
)(__isl_take isl_printer
*p
,
315 struct isl_print_space_data
*data
, unsigned pos
);
319 enum isl_dim_type type
;
322 /* offset is the offset of local_dim inside data->type of data->space.
324 static __isl_give isl_printer
*print_nested_var_list(__isl_take isl_printer
*p
,
325 __isl_keep isl_space
*local_dim
, enum isl_dim_type local_type
,
326 struct isl_print_space_data
*data
, int offset
)
330 if (data
->space
!= local_dim
&& local_type
== isl_dim_out
)
331 offset
+= local_dim
->n_in
;
333 for (i
= 0; i
< isl_space_dim(local_dim
, local_type
); ++i
) {
335 p
= isl_printer_print_str(p
, ", ");
337 p
= data
->print_dim(p
, data
, offset
+ i
);
339 p
= print_name(data
->space
, p
, data
->type
, offset
+ i
,
345 static __isl_give isl_printer
*print_var_list(__isl_take isl_printer
*p
,
346 __isl_keep isl_space
*space
, enum isl_dim_type type
)
348 struct isl_print_space_data data
= { .space
= space
, .type
= type
};
350 return print_nested_var_list(p
, space
, type
, &data
, 0);
353 static __isl_give isl_printer
*print_nested_map_dim(__isl_take isl_printer
*p
,
354 __isl_keep isl_space
*local_dim
,
355 struct isl_print_space_data
*data
, int offset
);
357 static __isl_give isl_printer
*print_nested_tuple(__isl_take isl_printer
*p
,
358 __isl_keep isl_space
*local_dim
, enum isl_dim_type local_type
,
359 struct isl_print_space_data
*data
, int offset
)
361 const char *name
= NULL
;
362 unsigned n
= isl_space_dim(local_dim
, local_type
);
363 if ((local_type
== isl_dim_in
|| local_type
== isl_dim_out
)) {
364 name
= isl_space_get_tuple_name(local_dim
, local_type
);
367 p
= isl_printer_print_str(p
, "\\mathrm{");
368 p
= isl_printer_print_str(p
, name
);
370 p
= isl_printer_print_str(p
, "}");
373 if (!data
->latex
|| n
!= 1 || name
)
374 p
= isl_printer_print_str(p
, s_open_list
[data
->latex
]);
375 if ((local_type
== isl_dim_in
|| local_type
== isl_dim_out
) &&
376 local_dim
->nested
[local_type
- isl_dim_in
]) {
377 if (data
->space
!= local_dim
&& local_type
== isl_dim_out
)
378 offset
+= local_dim
->n_in
;
379 p
= print_nested_map_dim(p
,
380 local_dim
->nested
[local_type
- isl_dim_in
],
383 p
= print_nested_var_list(p
, local_dim
, local_type
, data
,
385 if (!data
->latex
|| n
!= 1 || name
)
386 p
= isl_printer_print_str(p
, s_close_list
[data
->latex
]);
390 static __isl_give isl_printer
*print_tuple(__isl_keep isl_space
*dim
,
391 __isl_take isl_printer
*p
, enum isl_dim_type type
,
392 struct isl_print_space_data
*data
)
396 return print_nested_tuple(p
, dim
, type
, data
, 0);
399 static __isl_give isl_printer
*print_nested_map_dim(__isl_take isl_printer
*p
,
400 __isl_keep isl_space
*local_dim
,
401 struct isl_print_space_data
*data
, int offset
)
403 p
= print_nested_tuple(p
, local_dim
, isl_dim_in
, data
, offset
);
404 p
= isl_printer_print_str(p
, s_to
[data
->latex
]);
405 p
= print_nested_tuple(p
, local_dim
, isl_dim_out
, data
, offset
);
410 static __isl_give isl_printer
*print_space(__isl_keep isl_space
*dim
,
411 __isl_take isl_printer
*p
, int rational
,
412 struct isl_print_space_data
*data
)
414 if (rational
&& !data
->latex
)
415 p
= isl_printer_print_str(p
, "rat: ");
416 if (isl_space_is_params(dim
))
418 else if (isl_space_is_set(dim
))
419 p
= print_tuple(dim
, p
, isl_dim_set
, data
);
421 p
= print_tuple(dim
, p
, isl_dim_in
, data
);
422 p
= isl_printer_print_str(p
, s_to
[data
->latex
]);
423 p
= print_tuple(dim
, p
, isl_dim_out
, data
);
429 static __isl_give isl_printer
*print_omega_parameters(__isl_keep isl_space
*dim
,
430 __isl_take isl_printer
*p
)
432 if (isl_space_dim(dim
, isl_dim_param
) == 0)
435 p
= isl_printer_start_line(p
);
436 p
= isl_printer_print_str(p
, "symbolic ");
437 p
= print_var_list(p
, dim
, isl_dim_param
);
438 p
= isl_printer_print_str(p
, ";");
439 p
= isl_printer_end_line(p
);
443 static __isl_give isl_printer
*print_constraint(struct isl_basic_map
*bmap
,
444 __isl_keep isl_space
*dim
, __isl_take isl_printer
*p
,
445 isl_int
*c
, int last
, const char *op
, int first_constraint
, int latex
)
447 if (!first_constraint
)
448 p
= isl_printer_print_str(p
, s_and
[latex
]);
450 isl_int_abs(c
[last
], c
[last
]);
452 p
= print_term(dim
, NULL
, c
[last
], last
, p
, latex
);
454 p
= isl_printer_print_str(p
, " ");
455 p
= isl_printer_print_str(p
, op
);
456 p
= isl_printer_print_str(p
, " ");
458 isl_int_set_si(c
[last
], 0);
459 p
= print_affine(bmap
, dim
, p
, c
);
464 static __isl_give isl_printer
*print_constraints(__isl_keep isl_basic_map
*bmap
,
465 __isl_keep isl_space
*dim
, __isl_take isl_printer
*p
, int latex
)
469 unsigned total
= isl_basic_map_total_dim(bmap
);
471 c
= isl_vec_alloc(bmap
->ctx
, 1 + total
);
475 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
476 int l
= isl_seq_last_non_zero(bmap
->eq
[i
], 1 + total
);
478 if (i
!= bmap
->n_eq
- 1)
479 p
= isl_printer_print_str(p
, s_and
[latex
]);
480 p
= isl_printer_print_str(p
, "0 = 0");
483 if (isl_int_is_neg(bmap
->eq
[i
][l
]))
484 isl_seq_cpy(c
->el
, bmap
->eq
[i
], 1 + total
);
486 isl_seq_neg(c
->el
, bmap
->eq
[i
], 1 + total
);
487 p
= print_constraint(bmap
, dim
, p
, c
->el
, l
,
488 "=", i
== bmap
->n_eq
- 1, latex
);
490 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
491 int l
= isl_seq_last_non_zero(bmap
->ineq
[i
], 1 + total
);
496 s
= isl_int_sgn(bmap
->ineq
[i
][l
]);
498 isl_seq_cpy(c
->el
, bmap
->ineq
[i
], 1 + total
);
500 isl_seq_neg(c
->el
, bmap
->ineq
[i
], 1 + total
);
501 op
= s
< 0 ? s_le
[latex
] : s_ge
[latex
];
502 p
= print_constraint(bmap
, dim
, p
, c
->el
, l
,
503 op
, !bmap
->n_eq
&& !i
, latex
);
515 static __isl_give isl_printer
*print_div(__isl_keep isl_space
*dim
,
516 __isl_keep isl_mat
*div
, int pos
, __isl_take isl_printer
*p
)
518 int c
= p
->output_format
== ISL_FORMAT_C
;
519 p
= isl_printer_print_str(p
, c
? "floord(" : "floor((");
520 p
= print_affine_of_len(dim
, div
, p
,
521 div
->row
[pos
] + 1, div
->n_col
- 1);
522 p
= isl_printer_print_str(p
, c
? ", " : ")/");
523 p
= isl_printer_print_isl_int(p
, div
->row
[pos
][0]);
524 p
= isl_printer_print_str(p
, ")");
528 /* Print a comma separated list of div names, with their definitions
529 * (provided that they have a definition and we are printing in isl format).
531 static __isl_give isl_printer
*print_div_list(__isl_take isl_printer
*p
,
532 __isl_keep isl_space
*space
, __isl_keep isl_mat
*div
, int latex
)
537 if (!p
|| !space
|| !div
)
538 return isl_printer_free(p
);
540 n_div
= isl_mat_rows(div
);
542 for (i
= 0; i
< n_div
; ++i
) {
544 p
= isl_printer_print_str(p
, ", ");
545 p
= print_name(space
, p
, isl_dim_div
, i
, latex
);
546 if (p
->output_format
!= ISL_FORMAT_ISL
||
547 isl_int_is_zero(div
->row
[i
][0]))
549 p
= isl_printer_print_str(p
, " = ");
550 p
= print_div(space
, div
, i
, p
);
556 static __isl_give isl_printer
*print_disjunct(__isl_keep isl_basic_map
*bmap
,
557 __isl_keep isl_space
*dim
, __isl_take isl_printer
*p
, int latex
)
559 if (bmap
->n_div
> 0) {
563 space
= isl_basic_map_get_space(bmap
);
564 div
= isl_basic_map_get_divs(bmap
);
565 p
= isl_printer_print_str(p
, s_open_exists
[latex
]);
566 p
= print_div_list(p
, space
, div
, latex
);
567 isl_space_free(space
);
569 p
= isl_printer_print_str(p
, ": ");
572 p
= print_constraints(bmap
, dim
, p
, latex
);
575 p
= isl_printer_print_str(p
, s_close_exists
[latex
]);
579 /* Print a colon followed by the constraints of "bmap"
580 * to "p", provided there are any constraints.
581 * The names of the variables are taken from "space".
582 * "latex" is set if the constraints should be printed in LaTeX format.
584 static __isl_give isl_printer
*print_optional_disjunct(
585 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_space
*space
,
586 __isl_take isl_printer
*p
, int latex
)
588 if (isl_basic_map_is_universe(bmap
))
591 p
= isl_printer_print_str(p
, ": ");
592 p
= print_disjunct(bmap
, space
, p
, latex
);
597 static __isl_give isl_printer
*basic_map_print_omega(
598 __isl_keep isl_basic_map
*bmap
, __isl_take isl_printer
*p
)
600 p
= isl_printer_print_str(p
, "{ [");
601 p
= print_var_list(p
, bmap
->dim
, isl_dim_in
);
602 p
= isl_printer_print_str(p
, "] -> [");
603 p
= print_var_list(p
, bmap
->dim
, isl_dim_out
);
604 p
= isl_printer_print_str(p
, "] ");
605 p
= print_optional_disjunct(bmap
, bmap
->dim
, p
, 0);
606 p
= isl_printer_print_str(p
, " }");
610 static __isl_give isl_printer
*basic_set_print_omega(
611 __isl_keep isl_basic_set
*bset
, __isl_take isl_printer
*p
)
613 p
= isl_printer_print_str(p
, "{ [");
614 p
= print_var_list(p
, bset
->dim
, isl_dim_set
);
615 p
= isl_printer_print_str(p
, "] ");
616 p
= print_optional_disjunct(bset
, bset
->dim
, p
, 0);
617 p
= isl_printer_print_str(p
, " }");
621 static __isl_give isl_printer
*isl_map_print_omega(__isl_keep isl_map
*map
,
622 __isl_take isl_printer
*p
)
626 for (i
= 0; i
< map
->n
; ++i
) {
628 p
= isl_printer_print_str(p
, " union ");
629 p
= basic_map_print_omega(map
->p
[i
], p
);
634 static __isl_give isl_printer
*isl_set_print_omega(__isl_keep isl_set
*set
,
635 __isl_take isl_printer
*p
)
639 for (i
= 0; i
< set
->n
; ++i
) {
641 p
= isl_printer_print_str(p
, " union ");
642 p
= basic_set_print_omega(set
->p
[i
], p
);
647 static __isl_give isl_printer
*isl_basic_map_print_isl(
648 __isl_keep isl_basic_map
*bmap
, __isl_take isl_printer
*p
,
651 struct isl_print_space_data data
= { .latex
= latex
};
652 int rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
654 if (isl_basic_map_dim(bmap
, isl_dim_param
) > 0) {
655 p
= print_tuple(bmap
->dim
, p
, isl_dim_param
, &data
);
656 p
= isl_printer_print_str(p
, " -> ");
658 p
= isl_printer_print_str(p
, "{ ");
659 p
= print_space(bmap
->dim
, p
, rational
, &data
);
660 p
= isl_printer_print_str(p
, " : ");
661 p
= print_disjunct(bmap
, bmap
->dim
, p
, latex
);
662 p
= isl_printer_print_str(p
, " }");
666 static __isl_give isl_printer
*print_disjuncts(__isl_keep isl_map
*map
,
667 __isl_take isl_printer
*p
, int latex
)
671 if (isl_map_plain_is_universe(map
))
674 p
= isl_printer_print_str(p
, s_such_that
[latex
]);
676 p
= isl_printer_print_str(p
, "1 = 0");
677 for (i
= 0; i
< map
->n
; ++i
) {
679 p
= isl_printer_print_str(p
, s_or
[latex
]);
680 if (map
->n
> 1 && map
->p
[i
]->n_eq
+ map
->p
[i
]->n_ineq
> 1)
681 p
= isl_printer_print_str(p
, "(");
682 p
= print_disjunct(map
->p
[i
], map
->dim
, p
, latex
);
683 if (map
->n
> 1 && map
->p
[i
]->n_eq
+ map
->p
[i
]->n_ineq
> 1)
684 p
= isl_printer_print_str(p
, ")");
689 /* Print the disjuncts of a map (or set).
690 * If the map turns out to be a universal parameter domain, then
691 * we need to print the colon. Otherwise, the output looks identical
694 static __isl_give isl_printer
*print_disjuncts_map(__isl_keep isl_map
*map
,
695 __isl_take isl_printer
*p
, int latex
)
697 if (isl_map_plain_is_universe(map
) && isl_space_is_params(map
->dim
))
698 return isl_printer_print_str(p
, s_such_that
[latex
]);
700 return print_disjuncts(map
, p
, latex
);
703 struct isl_aff_split
{
708 static void free_split(__isl_take
struct isl_aff_split
*split
, int n
)
715 for (i
= 0; i
< n
; ++i
) {
716 isl_basic_map_free(split
[i
].aff
);
717 isl_map_free(split
[i
].map
);
723 static __isl_give isl_basic_map
*get_aff(__isl_take isl_basic_map
*bmap
)
726 unsigned nparam
, n_in
, n_out
, total
;
728 bmap
= isl_basic_map_cow(bmap
);
731 if (isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
) < 0)
734 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
735 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
736 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
737 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
738 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
739 j
= isl_seq_last_non_zero(bmap
->eq
[i
] + 1, total
);
740 if (j
>= nparam
&& j
< nparam
+ n_in
+ n_out
&&
741 (isl_int_is_one(bmap
->eq
[i
][1 + j
]) ||
742 isl_int_is_negone(bmap
->eq
[i
][1 + j
])))
744 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
748 bmap
= isl_basic_map_finalize(bmap
);
752 isl_basic_map_free(bmap
);
756 static int aff_split_cmp(const void *p1
, const void *p2
, void *user
)
758 const struct isl_aff_split
*s1
, *s2
;
759 s1
= (const struct isl_aff_split
*) p1
;
760 s2
= (const struct isl_aff_split
*) p2
;
762 return isl_basic_map_plain_cmp(s1
->aff
, s2
->aff
);
765 static __isl_give isl_basic_map
*drop_aff(__isl_take isl_basic_map
*bmap
,
766 __isl_keep isl_basic_map
*aff
)
774 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
776 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
777 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
,
780 for (j
= 0; j
< aff
->n_eq
; ++j
) {
781 if (!isl_seq_eq(bmap
->eq
[i
], aff
->eq
[j
], 1 + total
) &&
782 !isl_seq_is_neg(bmap
->eq
[i
], aff
->eq
[j
], 1 + total
))
784 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
792 isl_basic_map_free(bmap
);
796 static __isl_give
struct isl_aff_split
*split_aff(__isl_keep isl_map
*map
)
799 struct isl_aff_split
*split
;
802 ctx
= isl_map_get_ctx(map
);
803 split
= isl_calloc_array(ctx
, struct isl_aff_split
, map
->n
);
807 for (i
= 0; i
< map
->n
; ++i
) {
809 split
[i
].aff
= get_aff(isl_basic_map_copy(map
->p
[i
]));
810 bmap
= isl_basic_map_copy(map
->p
[i
]);
811 bmap
= isl_basic_map_cow(bmap
);
812 bmap
= drop_aff(bmap
, split
[i
].aff
);
813 split
[i
].map
= isl_map_from_basic_map(bmap
);
814 if (!split
[i
].aff
|| !split
[i
].map
)
818 if (isl_sort(split
, map
->n
, sizeof(struct isl_aff_split
),
819 &aff_split_cmp
, NULL
) < 0)
823 for (i
= n
- 1; i
>= 1; --i
) {
824 if (!isl_basic_map_plain_is_equal(split
[i
- 1].aff
,
827 isl_basic_map_free(split
[i
].aff
);
828 split
[i
- 1].map
= isl_map_union(split
[i
- 1].map
,
831 split
[i
] = split
[n
- 1];
832 split
[n
- 1].aff
= NULL
;
833 split
[n
- 1].map
= NULL
;
839 free_split(split
, map
->n
);
843 static int defining_equality(__isl_keep isl_basic_map
*eq
,
844 __isl_keep isl_space
*dim
, enum isl_dim_type type
, int pos
)
852 pos
+= isl_space_offset(dim
, type
);
853 total
= isl_basic_map_total_dim(eq
);
855 for (i
= 0; i
< eq
->n_eq
; ++i
) {
856 if (isl_seq_last_non_zero(eq
->eq
[i
] + 1, total
) != pos
)
858 if (isl_int_is_one(eq
->eq
[i
][1 + pos
]))
859 isl_seq_neg(eq
->eq
[i
], eq
->eq
[i
], 1 + total
);
866 /* Print dimension "pos" of data->space to "p".
868 * data->user is assumed to be an isl_basic_map keeping track of equalities.
870 * If the current dimension is defined by these equalities, then print
871 * the corresponding expression. Otherwise, print the name of the dimension.
873 static __isl_give isl_printer
*print_dim_eq(__isl_take isl_printer
*p
,
874 struct isl_print_space_data
*data
, unsigned pos
)
876 isl_basic_map
*eq
= data
->user
;
879 j
= defining_equality(eq
, data
->space
, data
->type
, pos
);
881 pos
+= 1 + isl_space_offset(data
->space
, data
->type
);
882 p
= print_affine_of_len(eq
->dim
, NULL
, p
, eq
->eq
[j
], pos
);
884 p
= print_name(data
->space
, p
, data
->type
, pos
, data
->latex
);
890 static __isl_give isl_printer
*print_split_map(__isl_take isl_printer
*p
,
891 struct isl_aff_split
*split
, int n
)
893 struct isl_print_space_data data
= { 0 };
897 data
.print_dim
= &print_dim_eq
;
898 for (i
= 0; i
< n
; ++i
) {
903 dim
= split
[i
].map
->dim
;
904 rational
= split
[i
].map
->n
> 0 &&
905 ISL_F_ISSET(split
[i
].map
->p
[0], ISL_BASIC_MAP_RATIONAL
);
907 p
= isl_printer_print_str(p
, "; ");
908 data
.user
= split
[i
].aff
;
909 p
= print_space(dim
, p
, rational
, &data
);
910 p
= print_disjuncts_map(split
[i
].map
, p
, 0);
916 static __isl_give isl_printer
*isl_map_print_isl_body(__isl_keep isl_map
*map
,
917 __isl_take isl_printer
*p
)
919 struct isl_print_space_data data
= { 0 };
920 struct isl_aff_split
*split
= NULL
;
924 split
= split_aff(map
);
926 p
= print_split_map(p
, split
, map
->n
);
928 rational
= map
->n
> 0 &&
929 ISL_F_ISSET(map
->p
[0], ISL_BASIC_MAP_RATIONAL
);
930 p
= print_space(map
->dim
, p
, rational
, &data
);
931 p
= print_disjuncts_map(map
, p
, 0);
933 free_split(split
, map
->n
);
937 static __isl_give isl_printer
*isl_map_print_isl(__isl_keep isl_map
*map
,
938 __isl_take isl_printer
*p
)
940 struct isl_print_space_data data
= { 0 };
942 if (isl_map_dim(map
, isl_dim_param
) > 0) {
943 p
= print_tuple(map
->dim
, p
, isl_dim_param
, &data
);
944 p
= isl_printer_print_str(p
, s_to
[0]);
946 p
= isl_printer_print_str(p
, s_open_set
[0]);
947 p
= isl_map_print_isl_body(map
, p
);
948 p
= isl_printer_print_str(p
, s_close_set
[0]);
952 static __isl_give isl_printer
*print_latex_map(__isl_keep isl_map
*map
,
953 __isl_take isl_printer
*p
, __isl_keep isl_basic_map
*aff
)
955 struct isl_print_space_data data
= { 0 };
958 if (isl_map_dim(map
, isl_dim_param
) > 0) {
959 p
= print_tuple(map
->dim
, p
, isl_dim_param
, &data
);
960 p
= isl_printer_print_str(p
, s_to
[1]);
962 p
= isl_printer_print_str(p
, s_open_set
[1]);
963 data
.print_dim
= &print_dim_eq
;
965 p
= print_space(map
->dim
, p
, 0, &data
);
966 p
= print_disjuncts_map(map
, p
, 1);
967 p
= isl_printer_print_str(p
, s_close_set
[1]);
972 static __isl_give isl_printer
*isl_map_print_latex(__isl_keep isl_map
*map
,
973 __isl_take isl_printer
*p
)
976 struct isl_aff_split
*split
= NULL
;
979 split
= split_aff(map
);
982 return print_latex_map(map
, p
, NULL
);
984 for (i
= 0; i
< map
->n
; ++i
) {
988 p
= isl_printer_print_str(p
, " \\cup ");
989 p
= print_latex_map(split
[i
].map
, p
, split
[i
].aff
);
992 free_split(split
, map
->n
);
996 __isl_give isl_printer
*isl_printer_print_basic_map(__isl_take isl_printer
*p
,
997 __isl_keep isl_basic_map
*bmap
)
1001 if (p
->output_format
== ISL_FORMAT_ISL
)
1002 return isl_basic_map_print_isl(bmap
, p
, 0);
1003 else if (p
->output_format
== ISL_FORMAT_OMEGA
)
1004 return basic_map_print_omega(bmap
, p
);
1005 isl_assert(bmap
->ctx
, 0, goto error
);
1007 isl_printer_free(p
);
1011 __isl_give isl_printer
*isl_printer_print_basic_set(__isl_take isl_printer
*p
,
1012 __isl_keep isl_basic_set
*bset
)
1017 if (p
->output_format
== ISL_FORMAT_ISL
)
1018 return isl_basic_map_print_isl(bset
, p
, 0);
1019 else if (p
->output_format
== ISL_FORMAT_POLYLIB
)
1020 return isl_basic_set_print_polylib(bset
, p
, 0);
1021 else if (p
->output_format
== ISL_FORMAT_EXT_POLYLIB
)
1022 return isl_basic_set_print_polylib(bset
, p
, 1);
1023 else if (p
->output_format
== ISL_FORMAT_POLYLIB_CONSTRAINTS
)
1024 return bset_print_constraints_polylib(bset
, p
);
1025 else if (p
->output_format
== ISL_FORMAT_OMEGA
)
1026 return basic_set_print_omega(bset
, p
);
1027 isl_assert(p
->ctx
, 0, goto error
);
1029 isl_printer_free(p
);
1033 __isl_give isl_printer
*isl_printer_print_set(__isl_take isl_printer
*p
,
1034 __isl_keep isl_set
*set
)
1038 if (p
->output_format
== ISL_FORMAT_ISL
)
1039 return isl_map_print_isl((isl_map
*)set
, p
);
1040 else if (p
->output_format
== ISL_FORMAT_POLYLIB
)
1041 return isl_set_print_polylib(set
, p
, 0);
1042 else if (p
->output_format
== ISL_FORMAT_EXT_POLYLIB
)
1043 return isl_set_print_polylib(set
, p
, 1);
1044 else if (p
->output_format
== ISL_FORMAT_OMEGA
)
1045 return isl_set_print_omega(set
, p
);
1046 else if (p
->output_format
== ISL_FORMAT_LATEX
)
1047 return isl_map_print_latex((isl_map
*)set
, p
);
1048 isl_assert(set
->ctx
, 0, goto error
);
1050 isl_printer_free(p
);
1054 __isl_give isl_printer
*isl_printer_print_map(__isl_take isl_printer
*p
,
1055 __isl_keep isl_map
*map
)
1060 if (p
->output_format
== ISL_FORMAT_ISL
)
1061 return isl_map_print_isl(map
, p
);
1062 else if (p
->output_format
== ISL_FORMAT_POLYLIB
)
1063 return isl_map_print_polylib(map
, p
, 0);
1064 else if (p
->output_format
== ISL_FORMAT_EXT_POLYLIB
)
1065 return isl_map_print_polylib(map
, p
, 1);
1066 else if (p
->output_format
== ISL_FORMAT_OMEGA
)
1067 return isl_map_print_omega(map
, p
);
1068 else if (p
->output_format
== ISL_FORMAT_LATEX
)
1069 return isl_map_print_latex(map
, p
);
1070 isl_assert(map
->ctx
, 0, goto error
);
1072 isl_printer_free(p
);
1076 struct isl_union_print_data
{
1081 static int print_map_body(__isl_take isl_map
*map
, void *user
)
1083 struct isl_union_print_data
*data
;
1084 data
= (struct isl_union_print_data
*)user
;
1087 data
->p
= isl_printer_print_str(data
->p
, "; ");
1090 data
->p
= isl_map_print_isl_body(map
, data
->p
);
1096 static __isl_give isl_printer
*isl_union_map_print_isl(
1097 __isl_keep isl_union_map
*umap
, __isl_take isl_printer
*p
)
1099 struct isl_union_print_data data
= { p
, 1 };
1100 struct isl_print_space_data space_data
= { 0 };
1103 dim
= isl_union_map_get_space(umap
);
1104 if (isl_space_dim(dim
, isl_dim_param
) > 0) {
1105 p
= print_tuple(dim
, p
, isl_dim_param
, &space_data
);
1106 p
= isl_printer_print_str(p
, s_to
[0]);
1108 isl_space_free(dim
);
1109 p
= isl_printer_print_str(p
, s_open_set
[0]);
1110 isl_union_map_foreach_map(umap
, &print_map_body
, &data
);
1112 p
= isl_printer_print_str(p
, s_close_set
[0]);
1116 static int print_latex_map_body(__isl_take isl_map
*map
, void *user
)
1118 struct isl_union_print_data
*data
;
1119 data
= (struct isl_union_print_data
*)user
;
1122 data
->p
= isl_printer_print_str(data
->p
, " \\cup ");
1125 data
->p
= isl_map_print_latex(map
, data
->p
);
1131 static __isl_give isl_printer
*isl_union_map_print_latex(
1132 __isl_keep isl_union_map
*umap
, __isl_take isl_printer
*p
)
1134 struct isl_union_print_data data
= { p
, 1 };
1135 isl_union_map_foreach_map(umap
, &print_latex_map_body
, &data
);
1140 __isl_give isl_printer
*isl_printer_print_union_map(__isl_take isl_printer
*p
,
1141 __isl_keep isl_union_map
*umap
)
1146 if (p
->output_format
== ISL_FORMAT_ISL
)
1147 return isl_union_map_print_isl(umap
, p
);
1148 if (p
->output_format
== ISL_FORMAT_LATEX
)
1149 return isl_union_map_print_latex(umap
, p
);
1151 isl_die(p
->ctx
, isl_error_invalid
,
1152 "invalid output format for isl_union_map", goto error
);
1154 isl_printer_free(p
);
1158 __isl_give isl_printer
*isl_printer_print_union_set(__isl_take isl_printer
*p
,
1159 __isl_keep isl_union_set
*uset
)
1164 if (p
->output_format
== ISL_FORMAT_ISL
)
1165 return isl_union_map_print_isl((isl_union_map
*)uset
, p
);
1166 if (p
->output_format
== ISL_FORMAT_LATEX
)
1167 return isl_union_map_print_latex((isl_union_map
*)uset
, p
);
1169 isl_die(p
->ctx
, isl_error_invalid
,
1170 "invalid output format for isl_union_set", goto error
);
1172 isl_printer_free(p
);
1176 static int upoly_rec_n_non_zero(__isl_keep
struct isl_upoly_rec
*rec
)
1181 for (i
= 0, n
= 0; i
< rec
->n
; ++i
)
1182 if (!isl_upoly_is_zero(rec
->p
[i
]))
1188 static __isl_give isl_printer
*upoly_print_cst(__isl_keep
struct isl_upoly
*up
,
1189 __isl_take isl_printer
*p
, int first
)
1191 struct isl_upoly_cst
*cst
;
1194 cst
= isl_upoly_as_cst(up
);
1197 neg
= !first
&& isl_int_is_neg(cst
->n
);
1199 p
= isl_printer_print_str(p
, neg
? " - " : " + ");
1201 isl_int_neg(cst
->n
, cst
->n
);
1202 if (isl_int_is_zero(cst
->d
)) {
1203 int sgn
= isl_int_sgn(cst
->n
);
1204 p
= isl_printer_print_str(p
, sgn
< 0 ? "-infty" :
1205 sgn
== 0 ? "NaN" : "infty");
1207 p
= isl_printer_print_isl_int(p
, cst
->n
);
1209 isl_int_neg(cst
->n
, cst
->n
);
1210 if (!isl_int_is_zero(cst
->d
) && !isl_int_is_one(cst
->d
)) {
1211 p
= isl_printer_print_str(p
, "/");
1212 p
= isl_printer_print_isl_int(p
, cst
->d
);
1216 isl_printer_free(p
);
1220 static __isl_give isl_printer
*print_base(__isl_take isl_printer
*p
,
1221 __isl_keep isl_space
*dim
, __isl_keep isl_mat
*div
, int var
)
1225 total
= isl_space_dim(dim
, isl_dim_all
);
1227 p
= print_term(dim
, NULL
, dim
->ctx
->one
, 1 + var
, p
, 0);
1229 p
= print_div(dim
, div
, var
- total
, p
);
1233 static __isl_give isl_printer
*print_pow(__isl_take isl_printer
*p
,
1234 __isl_keep isl_space
*dim
, __isl_keep isl_mat
*div
, int var
, int exp
)
1236 p
= print_base(p
, dim
, div
, var
);
1239 if (p
->output_format
== ISL_FORMAT_C
) {
1241 for (i
= 1; i
< exp
; ++i
) {
1242 p
= isl_printer_print_str(p
, "*");
1243 p
= print_base(p
, dim
, div
, var
);
1246 p
= isl_printer_print_str(p
, "^");
1247 p
= isl_printer_print_int(p
, exp
);
1252 static __isl_give isl_printer
*upoly_print(__isl_keep
struct isl_upoly
*up
,
1253 __isl_keep isl_space
*dim
, __isl_keep isl_mat
*div
,
1254 __isl_take isl_printer
*p
, int outer
)
1256 int i
, n
, first
, print_parens
;
1257 struct isl_upoly_rec
*rec
;
1259 if (!p
|| !up
|| !dim
|| !div
)
1262 if (isl_upoly_is_cst(up
))
1263 return upoly_print_cst(up
, p
, 1);
1265 rec
= isl_upoly_as_rec(up
);
1268 n
= upoly_rec_n_non_zero(rec
);
1269 print_parens
= n
> 1 ||
1270 (outer
&& rec
->up
.var
>= isl_space_dim(dim
, isl_dim_all
));
1272 p
= isl_printer_print_str(p
, "(");
1273 for (i
= 0, first
= 1; i
< rec
->n
; ++i
) {
1274 if (isl_upoly_is_zero(rec
->p
[i
]))
1276 if (isl_upoly_is_negone(rec
->p
[i
])) {
1278 p
= isl_printer_print_str(p
, "-1");
1280 p
= isl_printer_print_str(p
, "-");
1282 p
= isl_printer_print_str(p
, " - ");
1283 } else if (isl_upoly_is_cst(rec
->p
[i
]) &&
1284 !isl_upoly_is_one(rec
->p
[i
]))
1285 p
= upoly_print_cst(rec
->p
[i
], p
, first
);
1288 p
= isl_printer_print_str(p
, " + ");
1289 if (i
== 0 || !isl_upoly_is_one(rec
->p
[i
]))
1290 p
= upoly_print(rec
->p
[i
], dim
, div
, p
, 0);
1295 if (!isl_upoly_is_one(rec
->p
[i
]) &&
1296 !isl_upoly_is_negone(rec
->p
[i
]))
1297 p
= isl_printer_print_str(p
, " * ");
1298 p
= print_pow(p
, dim
, div
, rec
->up
.var
, i
);
1301 p
= isl_printer_print_str(p
, ")");
1304 isl_printer_free(p
);
1308 static __isl_give isl_printer
*print_qpolynomial(__isl_take isl_printer
*p
,
1309 __isl_keep isl_qpolynomial
*qp
)
1313 p
= upoly_print(qp
->upoly
, qp
->dim
, qp
->div
, p
, 1);
1316 isl_printer_free(p
);
1320 static __isl_give isl_printer
*print_qpolynomial_isl(__isl_take isl_printer
*p
,
1321 __isl_keep isl_qpolynomial
*qp
)
1323 struct isl_print_space_data data
= { 0 };
1328 if (isl_space_dim(qp
->dim
, isl_dim_param
) > 0) {
1329 p
= print_tuple(qp
->dim
, p
, isl_dim_param
, &data
);
1330 p
= isl_printer_print_str(p
, " -> ");
1332 p
= isl_printer_print_str(p
, "{ ");
1333 if (!isl_space_is_params(qp
->dim
)) {
1334 p
= print_space(qp
->dim
, p
, 0, &data
);
1335 p
= isl_printer_print_str(p
, " -> ");
1337 p
= print_qpolynomial(p
, qp
);
1338 p
= isl_printer_print_str(p
, " }");
1341 isl_printer_free(p
);
1345 static __isl_give isl_printer
*print_qpolynomial_c(__isl_take isl_printer
*p
,
1346 __isl_keep isl_space
*dim
, __isl_keep isl_qpolynomial
*qp
)
1351 isl_qpolynomial_get_den(qp
, &den
);
1352 if (!isl_int_is_one(den
)) {
1354 p
= isl_printer_print_str(p
, "(");
1355 qp
= isl_qpolynomial_copy(qp
);
1356 f
= isl_qpolynomial_rat_cst_on_domain(isl_space_copy(qp
->dim
),
1357 den
, qp
->dim
->ctx
->one
);
1358 qp
= isl_qpolynomial_mul(qp
, f
);
1361 p
= upoly_print(qp
->upoly
, dim
, qp
->div
, p
, 0);
1363 p
= isl_printer_free(p
);
1364 if (!isl_int_is_one(den
)) {
1365 p
= isl_printer_print_str(p
, ")/");
1366 p
= isl_printer_print_isl_int(p
, den
);
1367 isl_qpolynomial_free(qp
);
1373 __isl_give isl_printer
*isl_printer_print_qpolynomial(
1374 __isl_take isl_printer
*p
, __isl_keep isl_qpolynomial
*qp
)
1379 if (p
->output_format
== ISL_FORMAT_ISL
)
1380 return print_qpolynomial_isl(p
, qp
);
1381 else if (p
->output_format
== ISL_FORMAT_C
)
1382 return print_qpolynomial_c(p
, qp
->dim
, qp
);
1384 isl_die(qp
->dim
->ctx
, isl_error_unsupported
,
1385 "output format not supported for isl_qpolynomials",
1388 isl_printer_free(p
);
1392 void isl_qpolynomial_print(__isl_keep isl_qpolynomial
*qp
, FILE *out
,
1393 unsigned output_format
)
1400 isl_assert(qp
->dim
->ctx
, output_format
== ISL_FORMAT_ISL
, return);
1401 p
= isl_printer_to_file(qp
->dim
->ctx
, out
);
1402 p
= isl_printer_print_qpolynomial(p
, qp
);
1403 isl_printer_free(p
);
1406 static __isl_give isl_printer
*qpolynomial_fold_print(
1407 __isl_keep isl_qpolynomial_fold
*fold
, __isl_take isl_printer
*p
)
1411 if (fold
->type
== isl_fold_min
)
1412 p
= isl_printer_print_str(p
, "min");
1413 else if (fold
->type
== isl_fold_max
)
1414 p
= isl_printer_print_str(p
, "max");
1415 p
= isl_printer_print_str(p
, "(");
1416 for (i
= 0; i
< fold
->n
; ++i
) {
1418 p
= isl_printer_print_str(p
, ", ");
1419 p
= print_qpolynomial(p
, fold
->qp
[i
]);
1421 p
= isl_printer_print_str(p
, ")");
1425 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold
*fold
,
1426 FILE *out
, unsigned output_format
)
1433 isl_assert(fold
->dim
->ctx
, output_format
== ISL_FORMAT_ISL
, return);
1435 p
= isl_printer_to_file(fold
->dim
->ctx
, out
);
1436 p
= isl_printer_print_qpolynomial_fold(p
, fold
);
1438 isl_printer_free(p
);
1441 static __isl_give isl_printer
*isl_pwqp_print_isl_body(
1442 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial
*pwqp
)
1444 struct isl_print_space_data data
= { 0 };
1447 for (i
= 0; i
< pwqp
->n
; ++i
) {
1449 p
= isl_printer_print_str(p
, "; ");
1450 if (!isl_space_is_params(pwqp
->p
[i
].set
->dim
)) {
1451 p
= print_space(pwqp
->p
[i
].set
->dim
, p
, 0, &data
);
1452 p
= isl_printer_print_str(p
, " -> ");
1454 p
= print_qpolynomial(p
, pwqp
->p
[i
].qp
);
1455 p
= print_disjuncts((isl_map
*)pwqp
->p
[i
].set
, p
, 0);
1461 static __isl_give isl_printer
*print_pw_qpolynomial_isl(
1462 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial
*pwqp
)
1464 struct isl_print_space_data data
= { 0 };
1469 if (isl_space_dim(pwqp
->dim
, isl_dim_param
) > 0) {
1470 p
= print_tuple(pwqp
->dim
, p
, isl_dim_param
, &data
);
1471 p
= isl_printer_print_str(p
, " -> ");
1473 p
= isl_printer_print_str(p
, "{ ");
1475 if (!isl_space_is_set(pwqp
->dim
)) {
1476 p
= print_tuple(pwqp
->dim
, p
, isl_dim_in
, &data
);
1477 p
= isl_printer_print_str(p
, " -> ");
1479 p
= isl_printer_print_str(p
, "0");
1481 p
= isl_pwqp_print_isl_body(p
, pwqp
);
1482 p
= isl_printer_print_str(p
, " }");
1485 isl_printer_free(p
);
1489 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial
*pwqp
, FILE *out
,
1490 unsigned output_format
)
1497 p
= isl_printer_to_file(pwqp
->dim
->ctx
, out
);
1498 p
= isl_printer_set_output_format(p
, output_format
);
1499 p
= isl_printer_print_pw_qpolynomial(p
, pwqp
);
1501 isl_printer_free(p
);
1504 static __isl_give isl_printer
*isl_pwf_print_isl_body(
1505 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial_fold
*pwf
)
1507 struct isl_print_space_data data
= { 0 };
1510 for (i
= 0; i
< pwf
->n
; ++i
) {
1512 p
= isl_printer_print_str(p
, "; ");
1513 if (!isl_space_is_params(pwf
->p
[i
].set
->dim
)) {
1514 p
= print_space(pwf
->p
[i
].set
->dim
, p
, 0, &data
);
1515 p
= isl_printer_print_str(p
, " -> ");
1517 p
= qpolynomial_fold_print(pwf
->p
[i
].fold
, p
);
1518 p
= print_disjuncts((isl_map
*)pwf
->p
[i
].set
, p
, 0);
1524 static __isl_give isl_printer
*print_pw_qpolynomial_fold_isl(
1525 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial_fold
*pwf
)
1527 struct isl_print_space_data data
= { 0 };
1529 if (isl_space_dim(pwf
->dim
, isl_dim_param
) > 0) {
1530 p
= print_tuple(pwf
->dim
, p
, isl_dim_param
, &data
);
1531 p
= isl_printer_print_str(p
, " -> ");
1533 p
= isl_printer_print_str(p
, "{ ");
1535 if (!isl_space_is_set(pwf
->dim
)) {
1536 p
= print_tuple(pwf
->dim
, p
, isl_dim_in
, &data
);
1537 p
= isl_printer_print_str(p
, " -> ");
1539 p
= isl_printer_print_str(p
, "0");
1541 p
= isl_pwf_print_isl_body(p
, pwf
);
1542 p
= isl_printer_print_str(p
, " }");
1546 static __isl_give isl_printer
*print_affine_c(__isl_take isl_printer
*p
,
1547 __isl_keep isl_space
*dim
, __isl_keep isl_basic_set
*bset
, isl_int
*c
);
1549 static __isl_give isl_printer
*print_name_c(__isl_take isl_printer
*p
,
1550 __isl_keep isl_space
*dim
,
1551 __isl_keep isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
)
1553 if (type
== isl_dim_div
) {
1554 p
= isl_printer_print_str(p
, "floord(");
1555 p
= print_affine_c(p
, dim
, bset
, bset
->div
[pos
] + 1);
1556 p
= isl_printer_print_str(p
, ", ");
1557 p
= isl_printer_print_isl_int(p
, bset
->div
[pos
][0]);
1558 p
= isl_printer_print_str(p
, ")");
1562 name
= isl_space_get_dim_name(dim
, type
, pos
);
1565 p
= isl_printer_print_str(p
, name
);
1570 static __isl_give isl_printer
*print_term_c(__isl_take isl_printer
*p
,
1571 __isl_keep isl_space
*dim
,
1572 __isl_keep isl_basic_set
*bset
, isl_int c
, unsigned pos
)
1574 enum isl_dim_type type
;
1577 return isl_printer_print_isl_int(p
, c
);
1579 if (isl_int_is_one(c
))
1581 else if (isl_int_is_negone(c
))
1582 p
= isl_printer_print_str(p
, "-");
1584 p
= isl_printer_print_isl_int(p
, c
);
1585 p
= isl_printer_print_str(p
, "*");
1587 type
= pos2type(dim
, &pos
);
1588 p
= print_name_c(p
, dim
, bset
, type
, pos
);
1592 static __isl_give isl_printer
*print_partial_affine_c(__isl_take isl_printer
*p
,
1593 __isl_keep isl_space
*dim
,
1594 __isl_keep isl_basic_set
*bset
, isl_int
*c
, unsigned len
)
1599 for (i
= 0, first
= 1; i
< len
; ++i
) {
1601 if (isl_int_is_zero(c
[i
]))
1604 if (isl_int_is_neg(c
[i
])) {
1606 isl_int_neg(c
[i
], c
[i
]);
1607 p
= isl_printer_print_str(p
, " - ");
1609 p
= isl_printer_print_str(p
, " + ");
1612 p
= print_term_c(p
, dim
, bset
, c
[i
], i
);
1614 isl_int_neg(c
[i
], c
[i
]);
1617 p
= isl_printer_print_str(p
, "0");
1621 static __isl_give isl_printer
*print_affine_c(__isl_take isl_printer
*p
,
1622 __isl_keep isl_space
*dim
, __isl_keep isl_basic_set
*bset
, isl_int
*c
)
1624 unsigned len
= 1 + isl_basic_set_total_dim(bset
);
1625 return print_partial_affine_c(p
, dim
, bset
, c
, len
);
1628 /* We skip the constraint if it is implied by the div expression.
1630 * *first indicates whether this is the first constraint in the conjunction and
1631 * is updated if the constraint is actually printed.
1633 static __isl_give isl_printer
*print_constraint_c(__isl_take isl_printer
*p
,
1634 __isl_keep isl_space
*dim
,
1635 __isl_keep isl_basic_set
*bset
, isl_int
*c
, const char *op
, int *first
)
1641 o_div
= isl_basic_set_offset(bset
, isl_dim_div
);
1642 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1643 div
= isl_seq_last_non_zero(c
+ o_div
, n_div
);
1644 if (div
>= 0 && isl_basic_set_is_div_constraint(bset
, c
, div
))
1648 p
= isl_printer_print_str(p
, " && ");
1650 p
= print_affine_c(p
, dim
, bset
, c
);
1651 p
= isl_printer_print_str(p
, " ");
1652 p
= isl_printer_print_str(p
, op
);
1653 p
= isl_printer_print_str(p
, " 0");
1660 static __isl_give isl_printer
*print_basic_set_c(__isl_take isl_printer
*p
,
1661 __isl_keep isl_space
*dim
, __isl_keep isl_basic_set
*bset
)
1665 unsigned n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1666 unsigned total
= isl_basic_set_total_dim(bset
) - n_div
;
1668 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1669 j
= isl_seq_last_non_zero(bset
->eq
[i
] + 1 + total
, n_div
);
1671 p
= print_constraint_c(p
, dim
, bset
,
1672 bset
->eq
[i
], "==", &first
);
1675 p
= isl_printer_print_str(p
, " && ");
1676 p
= isl_printer_print_str(p
, "(");
1677 p
= print_partial_affine_c(p
, dim
, bset
, bset
->eq
[i
],
1679 p
= isl_printer_print_str(p
, ") % ");
1680 p
= isl_printer_print_isl_int(p
,
1681 bset
->eq
[i
][1 + total
+ j
]);
1682 p
= isl_printer_print_str(p
, " == 0");
1686 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1687 p
= print_constraint_c(p
, dim
, bset
, bset
->ineq
[i
], ">=",
1692 static __isl_give isl_printer
*print_set_c(__isl_take isl_printer
*p
,
1693 __isl_keep isl_space
*dim
, __isl_keep isl_set
*set
)
1698 return isl_printer_free(p
);
1701 p
= isl_printer_print_str(p
, "0");
1703 for (i
= 0; i
< set
->n
; ++i
) {
1705 p
= isl_printer_print_str(p
, " || ");
1707 p
= isl_printer_print_str(p
, "(");
1708 p
= print_basic_set_c(p
, dim
, set
->p
[i
]);
1710 p
= isl_printer_print_str(p
, ")");
1715 static __isl_give isl_printer
*print_pw_qpolynomial_c(
1716 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial
*pwqp
)
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");
1735 __isl_give isl_printer
*isl_printer_print_pw_qpolynomial(
1736 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial
*pwqp
)
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
);
1747 isl_printer_free(p
);
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
;
1757 data
->p
= isl_printer_print_str(data
->p
, "; ");
1760 data
->p
= isl_pwqp_print_isl_body(data
->p
, pwqp
);
1761 isl_pw_qpolynomial_free(pwqp
);
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 struct isl_print_space_data space_data
= { 0 };
1773 dim
= isl_union_pw_qpolynomial_get_space(upwqp
);
1774 if (isl_space_dim(dim
, isl_dim_param
) > 0) {
1775 p
= print_tuple(dim
, p
, isl_dim_param
, &space_data
);
1776 p
= isl_printer_print_str(p
, " -> ");
1778 isl_space_free(dim
);
1779 p
= isl_printer_print_str(p
, "{ ");
1780 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp
, &print_pwqp_body
,
1783 p
= isl_printer_print_str(p
, " }");
1787 __isl_give isl_printer
*isl_printer_print_union_pw_qpolynomial(
1788 __isl_take isl_printer
*p
, __isl_keep isl_union_pw_qpolynomial
*upwqp
)
1793 if (p
->output_format
== ISL_FORMAT_ISL
)
1794 return print_union_pw_qpolynomial_isl(p
, upwqp
);
1795 isl_die(p
->ctx
, isl_error_invalid
,
1796 "invalid output format for isl_union_pw_qpolynomial",
1799 isl_printer_free(p
);
1803 static __isl_give isl_printer
*print_qpolynomial_fold_c(
1804 __isl_take isl_printer
*p
, __isl_keep isl_space
*dim
,
1805 __isl_keep isl_qpolynomial_fold
*fold
)
1809 for (i
= 0; i
< fold
->n
- 1; ++i
)
1810 if (fold
->type
== isl_fold_min
)
1811 p
= isl_printer_print_str(p
, "min(");
1812 else if (fold
->type
== isl_fold_max
)
1813 p
= isl_printer_print_str(p
, "max(");
1815 for (i
= 0; i
< fold
->n
; ++i
) {
1817 p
= isl_printer_print_str(p
, ", ");
1818 p
= print_qpolynomial_c(p
, dim
, fold
->qp
[i
]);
1820 p
= isl_printer_print_str(p
, ")");
1825 __isl_give isl_printer
*isl_printer_print_qpolynomial_fold(
1826 __isl_take isl_printer
*p
, __isl_keep isl_qpolynomial_fold
*fold
)
1830 if (p
->output_format
== ISL_FORMAT_ISL
)
1831 return qpolynomial_fold_print(fold
, p
);
1832 else if (p
->output_format
== ISL_FORMAT_C
)
1833 return print_qpolynomial_fold_c(p
, fold
->dim
, fold
);
1834 isl_die(p
->ctx
, isl_error_unsupported
, "unsupported output format",
1837 isl_printer_free(p
);
1841 static __isl_give isl_printer
*print_pw_qpolynomial_fold_c(
1842 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial_fold
*pwf
)
1846 if (pwf
->n
== 1 && isl_set_plain_is_universe(pwf
->p
[0].set
))
1847 return print_qpolynomial_fold_c(p
, pwf
->dim
, pwf
->p
[0].fold
);
1849 for (i
= 0; i
< pwf
->n
; ++i
) {
1850 p
= isl_printer_print_str(p
, "(");
1851 p
= print_set_c(p
, pwf
->dim
, pwf
->p
[i
].set
);
1852 p
= isl_printer_print_str(p
, ") ? (");
1853 p
= print_qpolynomial_fold_c(p
, pwf
->dim
, pwf
->p
[i
].fold
);
1854 p
= isl_printer_print_str(p
, ") : ");
1857 p
= isl_printer_print_str(p
, "0");
1861 __isl_give isl_printer
*isl_printer_print_pw_qpolynomial_fold(
1862 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial_fold
*pwf
)
1867 if (p
->output_format
== ISL_FORMAT_ISL
)
1868 return print_pw_qpolynomial_fold_isl(p
, pwf
);
1869 else if (p
->output_format
== ISL_FORMAT_C
)
1870 return print_pw_qpolynomial_fold_c(p
, pwf
);
1871 isl_assert(p
->ctx
, 0, goto error
);
1873 isl_printer_free(p
);
1877 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold
*pwf
,
1878 FILE *out
, unsigned output_format
)
1885 p
= isl_printer_to_file(pwf
->dim
->ctx
, out
);
1886 p
= isl_printer_set_output_format(p
, output_format
);
1887 p
= isl_printer_print_pw_qpolynomial_fold(p
, pwf
);
1889 isl_printer_free(p
);
1892 static int print_pwf_body(__isl_take isl_pw_qpolynomial_fold
*pwf
, void *user
)
1894 struct isl_union_print_data
*data
;
1895 data
= (struct isl_union_print_data
*)user
;
1898 data
->p
= isl_printer_print_str(data
->p
, "; ");
1901 data
->p
= isl_pwf_print_isl_body(data
->p
, pwf
);
1902 isl_pw_qpolynomial_fold_free(pwf
);
1907 static __isl_give isl_printer
*print_union_pw_qpolynomial_fold_isl(
1908 __isl_take isl_printer
*p
,
1909 __isl_keep isl_union_pw_qpolynomial_fold
*upwf
)
1911 struct isl_union_print_data data
= { p
, 1 };
1912 struct isl_print_space_data space_data
= { 0 };
1915 dim
= isl_union_pw_qpolynomial_fold_get_space(upwf
);
1916 if (isl_space_dim(dim
, isl_dim_param
) > 0) {
1917 p
= print_tuple(dim
, p
, isl_dim_param
, &space_data
);
1918 p
= isl_printer_print_str(p
, " -> ");
1920 isl_space_free(dim
);
1921 p
= isl_printer_print_str(p
, "{ ");
1922 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf
,
1923 &print_pwf_body
, &data
);
1925 p
= isl_printer_print_str(p
, " }");
1929 __isl_give isl_printer
*isl_printer_print_union_pw_qpolynomial_fold(
1930 __isl_take isl_printer
*p
,
1931 __isl_keep isl_union_pw_qpolynomial_fold
*upwf
)
1936 if (p
->output_format
== ISL_FORMAT_ISL
)
1937 return print_union_pw_qpolynomial_fold_isl(p
, upwf
);
1938 isl_die(p
->ctx
, isl_error_invalid
,
1939 "invalid output format for isl_union_pw_qpolynomial_fold",
1942 isl_printer_free(p
);
1946 __isl_give isl_printer
*isl_printer_print_constraint(__isl_take isl_printer
*p
,
1947 __isl_keep isl_constraint
*c
)
1949 isl_basic_map
*bmap
;
1954 bmap
= isl_basic_map_from_constraint(isl_constraint_copy(c
));
1955 p
= isl_printer_print_basic_map(p
, bmap
);
1956 isl_basic_map_free(bmap
);
1959 isl_printer_free(p
);
1963 static __isl_give isl_printer
*isl_printer_print_space_isl(
1964 __isl_take isl_printer
*p
, __isl_keep isl_space
*dim
)
1966 struct isl_print_space_data data
= { 0 };
1971 if (isl_space_dim(dim
, isl_dim_param
) > 0) {
1972 p
= print_tuple(dim
, p
, isl_dim_param
, &data
);
1973 p
= isl_printer_print_str(p
, " -> ");
1976 p
= isl_printer_print_str(p
, "{ ");
1977 if (isl_space_is_params(dim
))
1978 p
= isl_printer_print_str(p
, s_such_that
[0]);
1980 p
= print_space(dim
, p
, 0, &data
);
1981 p
= isl_printer_print_str(p
, " }");
1985 isl_printer_free(p
);
1989 __isl_give isl_printer
*isl_printer_print_space(__isl_take isl_printer
*p
,
1990 __isl_keep isl_space
*space
)
1993 return isl_printer_free(p
);
1994 if (p
->output_format
== ISL_FORMAT_ISL
)
1995 return isl_printer_print_space_isl(p
, space
);
1996 else if (p
->output_format
== ISL_FORMAT_OMEGA
)
1997 return print_omega_parameters(space
, p
);
1999 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
2000 "output format not supported for space",
2001 return isl_printer_free(p
));
2004 __isl_give isl_printer
*isl_printer_print_local_space(__isl_take isl_printer
*p
,
2005 __isl_keep isl_local_space
*ls
)
2007 struct isl_print_space_data data
= { 0 };
2014 total
= isl_local_space_dim(ls
, isl_dim_all
);
2015 if (isl_local_space_dim(ls
, isl_dim_param
) > 0) {
2016 p
= print_tuple(ls
->dim
, p
, isl_dim_param
, &data
);
2017 p
= isl_printer_print_str(p
, " -> ");
2019 p
= isl_printer_print_str(p
, "{ ");
2020 p
= print_space(ls
->dim
, p
, 0, &data
);
2021 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
2023 p
= isl_printer_print_str(p
, " : ");
2024 p
= isl_printer_print_str(p
, s_open_exists
[0]);
2025 p
= print_div_list(p
, ls
->dim
, ls
->div
, 0);
2026 } else if (isl_space_is_params(ls
->dim
))
2027 p
= isl_printer_print_str(p
, s_such_that
[0]);
2028 p
= isl_printer_print_str(p
, " }");
2031 isl_printer_free(p
);
2035 static __isl_give isl_printer
*print_aff_body(__isl_take isl_printer
*p
,
2036 __isl_keep isl_aff
*aff
)
2040 if (isl_aff_is_nan(aff
))
2041 return isl_printer_print_str(p
, "NaN");
2043 total
= isl_local_space_dim(aff
->ls
, isl_dim_all
);
2044 p
= isl_printer_print_str(p
, "(");
2045 p
= print_affine_of_len(aff
->ls
->dim
, aff
->ls
->div
, p
,
2046 aff
->v
->el
+ 1, 1 + total
);
2047 if (isl_int_is_one(aff
->v
->el
[0]))
2048 p
= isl_printer_print_str(p
, ")");
2050 p
= isl_printer_print_str(p
, ")/");
2051 p
= isl_printer_print_isl_int(p
, aff
->v
->el
[0]);
2057 static __isl_give isl_printer
*print_aff(__isl_take isl_printer
*p
,
2058 __isl_keep isl_aff
*aff
)
2060 struct isl_print_space_data data
= { 0 };
2062 if (isl_space_is_params(aff
->ls
->dim
))
2065 p
= print_tuple(aff
->ls
->dim
, p
, isl_dim_set
, &data
);
2066 p
= isl_printer_print_str(p
, " -> ");
2068 p
= isl_printer_print_str(p
, "[");
2069 p
= print_aff_body(p
, aff
);
2070 p
= isl_printer_print_str(p
, "]");
2075 static __isl_give isl_printer
*print_aff_isl(__isl_take isl_printer
*p
,
2076 __isl_keep isl_aff
*aff
)
2078 struct isl_print_space_data data
= { 0 };
2083 if (isl_local_space_dim(aff
->ls
, isl_dim_param
) > 0) {
2084 p
= print_tuple(aff
->ls
->dim
, p
, isl_dim_param
, &data
);
2085 p
= isl_printer_print_str(p
, " -> ");
2087 p
= isl_printer_print_str(p
, "{ ");
2088 p
= print_aff(p
, aff
);
2089 p
= isl_printer_print_str(p
, " }");
2092 isl_printer_free(p
);
2096 /* Print the body of an isl_pw_aff, i.e., a semicolon delimited
2097 * sequence of affine expressions, each followed by constraints.
2099 static __isl_give isl_printer
*print_pw_aff_body(
2100 __isl_take isl_printer
*p
, __isl_keep isl_pw_aff
*pa
)
2105 return isl_printer_free(p
);
2107 for (i
= 0; i
< pa
->n
; ++i
) {
2109 p
= isl_printer_print_str(p
, "; ");
2110 p
= print_aff(p
, pa
->p
[i
].aff
);
2111 p
= print_disjuncts((isl_map
*)pa
->p
[i
].set
, p
, 0);
2117 static __isl_give isl_printer
*print_pw_aff_isl(__isl_take isl_printer
*p
,
2118 __isl_keep isl_pw_aff
*pwaff
)
2120 struct isl_print_space_data data
= { 0 };
2125 if (isl_space_dim(pwaff
->dim
, isl_dim_param
) > 0) {
2126 p
= print_tuple(pwaff
->dim
, p
, isl_dim_param
, &data
);
2127 p
= isl_printer_print_str(p
, " -> ");
2129 p
= isl_printer_print_str(p
, "{ ");
2130 p
= print_pw_aff_body(p
, pwaff
);
2131 p
= isl_printer_print_str(p
, " }");
2134 isl_printer_free(p
);
2138 static __isl_give isl_printer
*print_ls_affine_c(__isl_take isl_printer
*p
,
2139 __isl_keep isl_local_space
*ls
, isl_int
*c
);
2141 static __isl_give isl_printer
*print_ls_name_c(__isl_take isl_printer
*p
,
2142 __isl_keep isl_local_space
*ls
, enum isl_dim_type type
, unsigned pos
)
2144 if (type
== isl_dim_div
) {
2145 p
= isl_printer_print_str(p
, "floord(");
2146 p
= print_ls_affine_c(p
, ls
, ls
->div
->row
[pos
] + 1);
2147 p
= isl_printer_print_str(p
, ", ");
2148 p
= isl_printer_print_isl_int(p
, ls
->div
->row
[pos
][0]);
2149 p
= isl_printer_print_str(p
, ")");
2153 name
= isl_space_get_dim_name(ls
->dim
, type
, pos
);
2156 p
= isl_printer_print_str(p
, name
);
2161 static __isl_give isl_printer
*print_ls_term_c(__isl_take isl_printer
*p
,
2162 __isl_keep isl_local_space
*ls
, isl_int c
, unsigned pos
)
2164 enum isl_dim_type type
;
2167 return isl_printer_print_isl_int(p
, c
);
2169 if (isl_int_is_one(c
))
2171 else if (isl_int_is_negone(c
))
2172 p
= isl_printer_print_str(p
, "-");
2174 p
= isl_printer_print_isl_int(p
, c
);
2175 p
= isl_printer_print_str(p
, "*");
2177 type
= pos2type(ls
->dim
, &pos
);
2178 p
= print_ls_name_c(p
, ls
, type
, pos
);
2182 static __isl_give isl_printer
*print_ls_partial_affine_c(
2183 __isl_take isl_printer
*p
, __isl_keep isl_local_space
*ls
,
2184 isl_int
*c
, unsigned len
)
2189 for (i
= 0, first
= 1; i
< len
; ++i
) {
2191 if (isl_int_is_zero(c
[i
]))
2194 if (isl_int_is_neg(c
[i
])) {
2196 isl_int_neg(c
[i
], c
[i
]);
2197 p
= isl_printer_print_str(p
, " - ");
2199 p
= isl_printer_print_str(p
, " + ");
2202 p
= print_ls_term_c(p
, ls
, c
[i
], i
);
2204 isl_int_neg(c
[i
], c
[i
]);
2207 p
= isl_printer_print_str(p
, "0");
2211 static __isl_give isl_printer
*print_ls_affine_c(__isl_take isl_printer
*p
,
2212 __isl_keep isl_local_space
*ls
, isl_int
*c
)
2214 unsigned len
= 1 + isl_local_space_dim(ls
, isl_dim_all
);
2215 return print_ls_partial_affine_c(p
, ls
, c
, len
);
2218 static __isl_give isl_printer
*print_aff_c(__isl_take isl_printer
*p
,
2219 __isl_keep isl_aff
*aff
)
2223 total
= isl_local_space_dim(aff
->ls
, isl_dim_all
);
2224 if (!isl_int_is_one(aff
->v
->el
[0]))
2225 p
= isl_printer_print_str(p
, "(");
2226 p
= print_ls_partial_affine_c(p
, aff
->ls
, aff
->v
->el
+ 1, 1 + total
);
2227 if (!isl_int_is_one(aff
->v
->el
[0])) {
2228 p
= isl_printer_print_str(p
, ")/");
2229 p
= isl_printer_print_isl_int(p
, aff
->v
->el
[0]);
2234 /* In the C format, we cannot express that "pwaff" may be undefined
2235 * on parts of the domain space. We therefore assume that the expression
2236 * will only be evaluated on its definition domain and compute the gist
2237 * of each cell with respect to this domain.
2239 static __isl_give isl_printer
*print_pw_aff_c(__isl_take isl_printer
*p
,
2240 __isl_keep isl_pw_aff
*pwaff
)
2243 isl_ast_build
*build
;
2247 isl_die(p
->ctx
, isl_error_unsupported
,
2248 "cannot print empty isl_pw_aff in C format",
2249 return isl_printer_free(p
));
2251 domain
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff
));
2252 build
= isl_ast_build_from_context(domain
);
2253 expr
= isl_ast_build_expr_from_pw_aff(build
, isl_pw_aff_copy(pwaff
));
2254 p
= isl_printer_print_ast_expr(p
, expr
);
2255 isl_ast_expr_free(expr
);
2256 isl_ast_build_free(build
);
2261 __isl_give isl_printer
*isl_printer_print_aff(__isl_take isl_printer
*p
,
2262 __isl_keep isl_aff
*aff
)
2267 if (p
->output_format
== ISL_FORMAT_ISL
)
2268 return print_aff_isl(p
, aff
);
2269 else if (p
->output_format
== ISL_FORMAT_C
)
2270 return print_aff_c(p
, aff
);
2271 isl_die(p
->ctx
, isl_error_unsupported
, "unsupported output format",
2274 isl_printer_free(p
);
2278 __isl_give isl_printer
*isl_printer_print_pw_aff(__isl_take isl_printer
*p
,
2279 __isl_keep isl_pw_aff
*pwaff
)
2284 if (p
->output_format
== ISL_FORMAT_ISL
)
2285 return print_pw_aff_isl(p
, pwaff
);
2286 else if (p
->output_format
== ISL_FORMAT_C
)
2287 return print_pw_aff_c(p
, pwaff
);
2288 isl_die(p
->ctx
, isl_error_unsupported
, "unsupported output format",
2291 isl_printer_free(p
);
2295 /* Print "pa" in a sequence of isl_pw_affs delimited by semicolons.
2296 * Each isl_pw_aff itself is also printed as semicolon delimited
2297 * sequence of pieces.
2298 * If data->first = 1, then this is the first in the sequence.
2299 * Update data->first to tell the next element that it is not the first.
2301 static int print_pw_aff_body_wrap(__isl_take isl_pw_aff
*pa
,
2304 struct isl_union_print_data
*data
;
2305 data
= (struct isl_union_print_data
*) user
;
2308 data
->p
= isl_printer_print_str(data
->p
, "; ");
2311 data
->p
= print_pw_aff_body(data
->p
, pa
);
2312 isl_pw_aff_free(pa
);
2314 return data
->p
? 0 : -1;
2317 /* Print the body of an isl_union_pw_aff, i.e., a semicolon delimited
2318 * sequence of affine expressions, each followed by constraints,
2319 * with the sequence enclosed in braces.
2321 static __isl_give isl_printer
*print_union_pw_aff_body(
2322 __isl_take isl_printer
*p
, __isl_keep isl_union_pw_aff
*upa
)
2324 struct isl_union_print_data data
= { p
, 1 };
2326 p
= isl_printer_print_str(p
, s_open_set
[0]);
2328 if (isl_union_pw_aff_foreach_pw_aff(upa
,
2329 &print_pw_aff_body_wrap
, &data
) < 0)
2330 data
.p
= isl_printer_free(p
);
2332 p
= isl_printer_print_str(p
, s_close_set
[0]);
2337 /* Print the isl_union_pw_aff "upa" to "p" in isl format.
2339 * The individual isl_pw_affs are delimited by a semicolon.
2341 static __isl_give isl_printer
*print_union_pw_aff_isl(
2342 __isl_take isl_printer
*p
, __isl_keep isl_union_pw_aff
*upa
)
2344 struct isl_print_space_data data
= { 0 };
2347 space
= isl_union_pw_aff_get_space(upa
);
2348 if (isl_space_dim(space
, isl_dim_param
) > 0) {
2349 p
= print_tuple(space
, p
, isl_dim_param
, &data
);
2350 p
= isl_printer_print_str(p
, s_to
[0]);
2352 isl_space_free(space
);
2353 p
= print_union_pw_aff_body(p
, upa
);
2357 /* Print the isl_union_pw_aff "upa" to "p".
2359 * We currently only support an isl format.
2361 __isl_give isl_printer
*isl_printer_print_union_pw_aff(
2362 __isl_take isl_printer
*p
, __isl_keep isl_union_pw_aff
*upa
)
2365 return isl_printer_free(p
);
2367 if (p
->output_format
== ISL_FORMAT_ISL
)
2368 return print_union_pw_aff_isl(p
, upa
);
2369 isl_die(isl_printer_get_ctx(p
), isl_error_unsupported
,
2370 "unsupported output format", return isl_printer_free(p
));
2373 /* Print dimension "pos" of data->space to "p".
2375 * data->user is assumed to be an isl_multi_aff.
2377 * If the current dimension is an output dimension, then print
2378 * the corresponding expression. Otherwise, print the name of the dimension.
2380 static __isl_give isl_printer
*print_dim_ma(__isl_take isl_printer
*p
,
2381 struct isl_print_space_data
*data
, unsigned pos
)
2383 isl_multi_aff
*ma
= data
->user
;
2385 if (data
->type
== isl_dim_out
)
2386 p
= print_aff_body(p
, ma
->p
[pos
]);
2388 p
= print_name(data
->space
, p
, data
->type
, pos
, data
->latex
);
2393 static __isl_give isl_printer
*print_multi_aff(__isl_take isl_printer
*p
,
2394 __isl_keep isl_multi_aff
*maff
)
2396 struct isl_print_space_data data
= { 0 };
2398 data
.print_dim
= &print_dim_ma
;
2400 return print_space(maff
->space
, p
, 0, &data
);
2403 static __isl_give isl_printer
*print_multi_aff_isl(__isl_take isl_printer
*p
,
2404 __isl_keep isl_multi_aff
*maff
)
2406 struct isl_print_space_data data
= { 0 };
2411 if (isl_space_dim(maff
->space
, isl_dim_param
) > 0) {
2412 p
= print_tuple(maff
->space
, p
, isl_dim_param
, &data
);
2413 p
= isl_printer_print_str(p
, " -> ");
2415 p
= isl_printer_print_str(p
, "{ ");
2416 p
= print_multi_aff(p
, maff
);
2417 p
= isl_printer_print_str(p
, " }");
2420 isl_printer_free(p
);
2424 __isl_give isl_printer
*isl_printer_print_multi_aff(__isl_take isl_printer
*p
,
2425 __isl_keep isl_multi_aff
*maff
)
2430 if (p
->output_format
== ISL_FORMAT_ISL
)
2431 return print_multi_aff_isl(p
, maff
);
2432 isl_die(p
->ctx
, isl_error_unsupported
, "unsupported output format",
2435 isl_printer_free(p
);
2439 static __isl_give isl_printer
*print_pw_multi_aff_body(
2440 __isl_take isl_printer
*p
, __isl_keep isl_pw_multi_aff
*pma
)
2447 for (i
= 0; i
< pma
->n
; ++i
) {
2449 p
= isl_printer_print_str(p
, "; ");
2450 p
= print_multi_aff(p
, pma
->p
[i
].maff
);
2451 p
= print_disjuncts((isl_map
*)pma
->p
[i
].set
, p
, 0);
2455 isl_printer_free(p
);
2459 static __isl_give isl_printer
*print_pw_multi_aff_isl(__isl_take isl_printer
*p
,
2460 __isl_keep isl_pw_multi_aff
*pma
)
2462 struct isl_print_space_data data
= { 0 };
2467 if (isl_space_dim(pma
->dim
, isl_dim_param
) > 0) {
2468 p
= print_tuple(pma
->dim
, p
, isl_dim_param
, &data
);
2469 p
= isl_printer_print_str(p
, " -> ");
2471 p
= isl_printer_print_str(p
, "{ ");
2472 p
= print_pw_multi_aff_body(p
, pma
);
2473 p
= isl_printer_print_str(p
, " }");
2476 isl_printer_free(p
);
2480 static __isl_give isl_printer
*print_unnamed_pw_multi_aff_c(
2481 __isl_take isl_printer
*p
, __isl_keep isl_pw_multi_aff
*pma
)
2485 for (i
= 0; i
< pma
->n
- 1; ++i
) {
2486 p
= isl_printer_print_str(p
, "(");
2487 p
= print_set_c(p
, pma
->dim
, pma
->p
[i
].set
);
2488 p
= isl_printer_print_str(p
, ") ? (");
2489 p
= print_aff_c(p
, pma
->p
[i
].maff
->p
[0]);
2490 p
= isl_printer_print_str(p
, ") : ");
2493 return print_aff_c(p
, pma
->p
[pma
->n
- 1].maff
->p
[0]);
2496 static __isl_give isl_printer
*print_pw_multi_aff_c(__isl_take isl_printer
*p
,
2497 __isl_keep isl_pw_multi_aff
*pma
)
2505 isl_die(p
->ctx
, isl_error_unsupported
,
2506 "cannot print empty isl_pw_multi_aff in C format",
2508 name
= isl_pw_multi_aff_get_tuple_name(pma
, isl_dim_out
);
2509 if (!name
&& isl_pw_multi_aff_dim(pma
, isl_dim_out
) == 1)
2510 return print_unnamed_pw_multi_aff_c(p
, pma
);
2512 isl_die(p
->ctx
, isl_error_unsupported
,
2513 "cannot print unnamed isl_pw_multi_aff in C format",
2516 p
= isl_printer_print_str(p
, name
);
2517 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
2519 isl_die(p
->ctx
, isl_error_unsupported
,
2520 "not supported yet", goto error
);
2524 isl_printer_free(p
);
2528 __isl_give isl_printer
*isl_printer_print_pw_multi_aff(
2529 __isl_take isl_printer
*p
, __isl_keep isl_pw_multi_aff
*pma
)
2534 if (p
->output_format
== ISL_FORMAT_ISL
)
2535 return print_pw_multi_aff_isl(p
, pma
);
2536 if (p
->output_format
== ISL_FORMAT_C
)
2537 return print_pw_multi_aff_c(p
, pma
);
2538 isl_die(p
->ctx
, isl_error_unsupported
, "unsupported output format",
2541 isl_printer_free(p
);
2545 static int print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff
*pma
,
2548 struct isl_union_print_data
*data
;
2549 data
= (struct isl_union_print_data
*) user
;
2552 data
->p
= isl_printer_print_str(data
->p
, "; ");
2555 data
->p
= print_pw_multi_aff_body(data
->p
, pma
);
2556 isl_pw_multi_aff_free(pma
);
2561 static __isl_give isl_printer
*print_union_pw_multi_aff_isl(
2562 __isl_take isl_printer
*p
, __isl_keep isl_union_pw_multi_aff
*upma
)
2564 struct isl_union_print_data data
= { p
, 1 };
2565 struct isl_print_space_data space_data
= { 0 };
2568 space
= isl_union_pw_multi_aff_get_space(upma
);
2569 if (isl_space_dim(space
, isl_dim_param
) > 0) {
2570 p
= print_tuple(space
, p
, isl_dim_param
, &space_data
);
2571 p
= isl_printer_print_str(p
, s_to
[0]);
2573 isl_space_free(space
);
2574 p
= isl_printer_print_str(p
, s_open_set
[0]);
2575 isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
2576 &print_pw_multi_aff_body_wrap
, &data
);
2578 p
= isl_printer_print_str(p
, s_close_set
[0]);
2582 __isl_give isl_printer
*isl_printer_print_union_pw_multi_aff(
2583 __isl_take isl_printer
*p
, __isl_keep isl_union_pw_multi_aff
*upma
)
2588 if (p
->output_format
== ISL_FORMAT_ISL
)
2589 return print_union_pw_multi_aff_isl(p
, upma
);
2590 isl_die(p
->ctx
, isl_error_unsupported
, "unsupported output format",
2593 isl_printer_free(p
);
2597 /* Print dimension "pos" of data->space to "p".
2599 * data->user is assumed to be an isl_multi_pw_aff.
2601 * If the current dimension is an output dimension, then print
2602 * the corresponding piecewise affine expression.
2603 * Otherwise, print the name of the dimension.
2605 static __isl_give isl_printer
*print_dim_mpa(__isl_take isl_printer
*p
,
2606 struct isl_print_space_data
*data
, unsigned pos
)
2610 isl_multi_pw_aff
*mpa
= data
->user
;
2613 if (data
->type
!= isl_dim_out
)
2614 return print_name(data
->space
, p
, data
->type
, pos
, data
->latex
);
2618 return isl_printer_print_str(p
, "(0 : 1 = 0)");
2620 need_parens
= pa
->n
!= 1 || !isl_set_plain_is_universe(pa
->p
[0].set
);
2622 p
= isl_printer_print_str(p
, "(");
2623 for (i
= 0; i
< pa
->n
; ++i
) {
2625 p
= isl_printer_print_str(p
, "; ");
2626 p
= print_aff_body(p
, pa
->p
[i
].aff
);
2627 p
= print_disjuncts(pa
->p
[i
].set
, p
, 0);
2630 p
= isl_printer_print_str(p
, ")");
2635 /* Print "mpa" to "p" in isl format.
2637 static __isl_give isl_printer
*print_multi_pw_aff_isl(__isl_take isl_printer
*p
,
2638 __isl_keep isl_multi_pw_aff
*mpa
)
2640 struct isl_print_space_data data
= { 0 };
2643 return isl_printer_free(p
);
2645 if (isl_space_dim(mpa
->space
, isl_dim_param
) > 0) {
2646 p
= print_tuple(mpa
->space
, p
, isl_dim_param
, &data
);
2647 p
= isl_printer_print_str(p
, " -> ");
2649 p
= isl_printer_print_str(p
, "{ ");
2650 data
.print_dim
= &print_dim_mpa
;
2652 p
= print_space(mpa
->space
, p
, 0, &data
);
2653 p
= isl_printer_print_str(p
, " }");
2657 __isl_give isl_printer
*isl_printer_print_multi_pw_aff(
2658 __isl_take isl_printer
*p
, __isl_keep isl_multi_pw_aff
*mpa
)
2661 return isl_printer_free(p
);
2663 if (p
->output_format
== ISL_FORMAT_ISL
)
2664 return print_multi_pw_aff_isl(p
, mpa
);
2665 isl_die(p
->ctx
, isl_error_unsupported
, "unsupported output format",
2666 return isl_printer_free(p
));
2669 /* Print dimension "pos" of data->space to "p".
2671 * data->user is assumed to be an isl_multi_val.
2673 * If the current dimension is an output dimension, then print
2674 * the corresponding value. Otherwise, print the name of the dimension.
2676 static __isl_give isl_printer
*print_dim_mv(__isl_take isl_printer
*p
,
2677 struct isl_print_space_data
*data
, unsigned pos
)
2679 isl_multi_val
*mv
= data
->user
;
2681 if (data
->type
== isl_dim_out
)
2682 return isl_printer_print_val(p
, mv
->p
[pos
]);
2684 return print_name(data
->space
, p
, data
->type
, pos
, data
->latex
);
2687 /* Print the isl_multi_val "mv" to "p" in isl format.
2689 static __isl_give isl_printer
*print_multi_val_isl(__isl_take isl_printer
*p
,
2690 __isl_keep isl_multi_val
*mv
)
2692 struct isl_print_space_data data
= { 0 };
2695 return isl_printer_free(p
);
2697 if (isl_space_dim(mv
->space
, isl_dim_param
) > 0) {
2698 p
= print_tuple(mv
->space
, p
, isl_dim_param
, &data
);
2699 p
= isl_printer_print_str(p
, " -> ");
2701 p
= isl_printer_print_str(p
, "{ ");
2702 data
.print_dim
= &print_dim_mv
;
2704 p
= print_space(mv
->space
, p
, 0, &data
);
2705 p
= isl_printer_print_str(p
, " }");
2709 /* Print the isl_multi_val "mv" to "p".
2711 * Currently only supported in isl format.
2713 __isl_give isl_printer
*isl_printer_print_multi_val(
2714 __isl_take isl_printer
*p
, __isl_keep isl_multi_val
*mv
)
2717 return isl_printer_free(p
);
2719 if (p
->output_format
== ISL_FORMAT_ISL
)
2720 return print_multi_val_isl(p
, mv
);
2721 isl_die(p
->ctx
, isl_error_unsupported
, "unsupported output format",
2722 return isl_printer_free(p
));
2725 /* Print dimension "pos" of data->space to "p".
2727 * data->user is assumed to be an isl_multi_union_pw_aff.
2729 * The current dimension is necessarily a set dimension, so
2730 * we print the corresponding isl_union_pw_aff, including
2733 static __isl_give isl_printer
*print_union_pw_aff_dim(__isl_take isl_printer
*p
,
2734 struct isl_print_space_data
*data
, unsigned pos
)
2736 isl_multi_union_pw_aff
*mupa
= data
->user
;
2737 isl_union_pw_aff
*upa
;
2739 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, pos
);
2740 p
= print_union_pw_aff_body(p
, upa
);
2741 isl_union_pw_aff_free(upa
);
2746 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
2748 static __isl_give isl_printer
*print_multi_union_pw_aff_isl(
2749 __isl_take isl_printer
*p
, __isl_keep isl_multi_union_pw_aff
*mupa
)
2751 struct isl_print_space_data data
= { 0 };
2754 space
= isl_multi_union_pw_aff_get_space(mupa
);
2755 if (isl_space_dim(space
, isl_dim_param
) > 0) {
2756 struct isl_print_space_data space_data
= { 0 };
2757 p
= print_tuple(space
, p
, isl_dim_param
, &space_data
);
2758 p
= isl_printer_print_str(p
, s_to
[0]);
2761 data
.print_dim
= &print_union_pw_aff_dim
;
2764 p
= print_space(space
, p
, 0, &data
);
2765 isl_space_free(space
);
2770 /* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
2772 * We currently only support an isl format.
2774 __isl_give isl_printer
*isl_printer_print_multi_union_pw_aff(
2775 __isl_take isl_printer
*p
, __isl_keep isl_multi_union_pw_aff
*mupa
)
2778 return isl_printer_free(p
);
2780 if (p
->output_format
== ISL_FORMAT_ISL
)
2781 return print_multi_union_pw_aff_isl(p
, mupa
);
2782 isl_die(isl_printer_get_ctx(p
), isl_error_unsupported
,
2783 "unsupported output format", return isl_printer_free(p
));