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
17 static void print_constraint_polylib(struct isl_basic_map *bmap,
19 FILE *out, int indent, const char *prefix, const char *suffix)
22 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
23 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
24 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
25 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
27 fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
28 fprintf(out, "%d", ineq);
29 for (i = 0; i < n_out; ++i) {
31 isl_int_print(out, c[1+nparam+n_in+i], 5);
33 for (i = 0; i < n_in; ++i) {
35 isl_int_print(out, c[1+nparam+i], 5);
37 for (i = 0; i < bmap->n_div; ++i) {
39 isl_int_print(out, c[1+nparam+n_in+n_out+i], 5);
41 for (i = 0; i < nparam; ++i) {
43 isl_int_print(out, c[1+i], 5);
46 isl_int_print(out, c[0], 5);
47 fprintf(out, "%s\n", suffix ? suffix : "");
50 static void print_constraints_polylib(struct isl_basic_map *bmap,
51 FILE *out, int indent, const char *prefix, const char *suffix)
55 for (i = 0; i < bmap->n_eq; ++i)
56 print_constraint_polylib(bmap, 0, i, out,
57 indent, prefix, suffix);
58 for (i = 0; i < bmap->n_ineq; ++i)
59 print_constraint_polylib(bmap, 1, i, out,
60 indent, prefix, suffix);
63 static void bset_print_constraints_polylib(struct isl_basic_set *bset,
64 FILE *out, int indent, const char *prefix, const char *suffix)
66 print_constraints_polylib((struct isl_basic_map *)bset,
67 out, indent, prefix, suffix);
70 static void isl_basic_map_print_polylib(struct isl_basic_map *bmap, FILE *out,
71 int indent, const char *prefix, const char *suffix)
73 unsigned total = isl_basic_map_total_dim(bmap);
74 fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
75 fprintf(out, "%d %d", bmap->n_eq + bmap->n_ineq, 1 + total + 1);
76 fprintf(out, "%s\n", suffix ? suffix : "");
77 print_constraints_polylib(bmap, out, indent, prefix, suffix);
80 static void isl_basic_set_print_polylib(struct isl_basic_set *bset, FILE *out,
81 int indent, const char *prefix, const char *suffix)
83 isl_basic_map_print_polylib((struct isl_basic_map *)bset, out,
84 indent, prefix, suffix);
87 static void isl_map_print_polylib(struct isl_map *map, FILE *out, int indent)
91 fprintf(out, "%*s", indent, "");
92 fprintf(out, "%d\n", map->n);
93 for (i = 0; i < map->n; ++i) {
95 isl_basic_map_print_polylib(map->p[i], out, indent, NULL, NULL);
99 static void isl_set_print_polylib(struct isl_set *set, FILE *out, int indent)
101 isl_map_print_polylib((struct isl_map *)set, out, indent);
104 static int count_same_name(__isl_keep isl_dim *dim,
105 enum isl_dim_type type, unsigned pos, const char *name)
111 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
112 s = t == type ? pos : isl_dim_size(dim, t);
113 for (p = 0; p < s; ++p) {
114 const char *n = isl_dim_get_name(dim, t, p);
115 if (n && !strcmp(n, name))
122 static void print_name(struct isl_dim *dim, FILE *out,
123 enum isl_dim_type type, unsigned pos, int set)
129 name = type == isl_dim_div ? NULL : isl_dim_get_name(dim, type, pos);
133 if (type == isl_dim_param)
135 else if (type == isl_dim_div)
137 else if (set || type == isl_dim_in)
141 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
144 primes = count_same_name(dim, name == buffer ? isl_dim_div : type,
146 fprintf(out, "%s", name);
151 static void print_var_list(struct isl_dim *dim, FILE *out,
152 enum isl_dim_type type, int set)
156 for (i = 0; i < isl_dim_size(dim, type); ++i) {
159 print_name(dim, out, type, i, set);
163 static void print_tuple(__isl_keep isl_dim *dim, FILE *out,
164 enum isl_dim_type type, int set)
167 print_var_list(dim, out, type, set);
171 static void print_omega_parameters(struct isl_dim *dim, FILE *out,
172 int indent, const char *prefix, const char *suffix)
174 if (isl_dim_size(dim, isl_dim_param) == 0)
177 fprintf(out, "%*s%ssymbolic ", indent, "", prefix ? prefix : "");
178 print_var_list(dim, out, isl_dim_param, 0);
179 fprintf(out, ";%s\n", suffix ? suffix : "");
182 static void print_term(__isl_keep isl_dim *dim,
183 isl_int c, int pos, FILE *out, int set)
185 enum isl_dim_type type;
186 unsigned n_in = isl_dim_size(dim, isl_dim_in);
187 unsigned n_out = isl_dim_size(dim, isl_dim_out);
188 unsigned nparam = isl_dim_size(dim, isl_dim_param);
191 isl_int_print(out, c, 0);
195 if (isl_int_is_one(c))
197 else if (isl_int_is_negone(c))
200 isl_int_print(out, c, 0);
201 if (pos < 1 + nparam) {
202 type = isl_dim_param;
204 } else if (pos < 1 + nparam + n_in) {
207 } else if (pos < 1 + nparam + n_in + n_out) {
209 pos -= 1 + nparam + n_in;
212 pos -= 1 + nparam + n_in + n_out;
214 print_name(dim, out, type, pos, set);
217 static void print_affine(__isl_keep isl_basic_map *bmap,
218 __isl_keep isl_dim *dim, FILE *out, isl_int *c, int set)
222 unsigned len = 1 + isl_basic_map_total_dim(bmap);
224 for (i = 0, first = 1; i < len; ++i) {
226 if (isl_int_is_zero(c[i]))
229 if (isl_int_is_neg(c[i])) {
231 isl_int_neg(c[i], c[i]);
237 print_term(dim, c[i], i, out, set);
239 isl_int_neg(c[i], c[i]);
245 static void print_constraint(struct isl_basic_map *bmap,
246 __isl_keep isl_dim *dim, FILE *out,
247 isl_int *c, int last, const char *op, int first_constraint, int set)
249 if (!first_constraint)
250 fprintf(out, " and ");
252 isl_int_abs(c[last], c[last]);
254 print_term(dim, c[last], last, out, set);
256 fprintf(out, " %s ", op);
258 isl_int_set_si(c[last], 0);
259 print_affine(bmap, dim, out, c, set);
262 static void print_constraints(__isl_keep isl_basic_map *bmap,
263 __isl_keep isl_dim *dim, FILE *out, int set)
267 unsigned total = isl_basic_map_total_dim(bmap);
269 c = isl_vec_alloc(bmap->ctx, 1 + total);
273 for (i = bmap->n_eq - 1; i >= 0; --i) {
274 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
275 isl_assert(bmap->ctx, l >= 0, return);
276 if (isl_int_is_neg(bmap->eq[i][l]))
277 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
279 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
280 print_constraint(bmap, dim, out, c->el, l,
281 "=", i == bmap->n_eq - 1, set);
283 for (i = 0; i < bmap->n_ineq; ++i) {
284 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
286 isl_assert(bmap->ctx, l >= 0, return);
287 s = isl_int_sgn(bmap->ineq[i][l]);
289 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
291 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
292 print_constraint(bmap, dim, out, c->el, l,
293 s < 0 ? "<=" : ">=", !bmap->n_eq && !i, set);
299 static void print_omega_constraints(__isl_keep isl_basic_map *bmap, FILE *out,
302 if (bmap->n_eq + bmap->n_ineq == 0)
306 if (bmap->n_div > 0) {
308 fprintf(out, "exists (");
309 for (i = 0; i < bmap->n_div; ++i) {
312 print_name(bmap->dim, out, isl_dim_div, i, 0);
316 print_constraints(bmap, bmap->dim, out, set);
321 static void basic_map_print_omega(struct isl_basic_map *bmap, FILE *out)
324 print_var_list(bmap->dim, out, isl_dim_in, 0);
325 fprintf(out, "] -> [");
326 print_var_list(bmap->dim, out, isl_dim_out, 0);
328 print_omega_constraints(bmap, out, 0);
332 static void isl_basic_map_print_omega(struct isl_basic_map *bmap, FILE *out,
333 int indent, const char *prefix, const char *suffix)
335 print_omega_parameters(bmap->dim, out, indent, prefix, suffix);
337 fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
338 basic_map_print_omega(bmap, out);
339 fprintf(out, "%s\n", suffix ? suffix : "");
342 static void basic_set_print_omega(struct isl_basic_set *bset, FILE *out)
345 print_var_list(bset->dim, out, isl_dim_set, 1);
347 print_omega_constraints((isl_basic_map *)bset, out, 1);
351 static void isl_basic_set_print_omega(struct isl_basic_set *bset, FILE *out,
352 int indent, const char *prefix, const char *suffix)
354 print_omega_parameters(bset->dim, out, indent, prefix, suffix);
356 fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
357 basic_set_print_omega(bset, out);
358 fprintf(out, "%s\n", suffix ? suffix : "");
361 static void isl_map_print_omega(struct isl_map *map, FILE *out, int indent)
365 print_omega_parameters(map->dim, out, indent, "", "");
367 fprintf(out, "%*s", indent, "");
368 for (i = 0; i < map->n; ++i) {
370 fprintf(out, " union ");
371 basic_map_print_omega(map->p[i], out);
376 static void isl_set_print_omega(struct isl_set *set, FILE *out, int indent)
380 print_omega_parameters(set->dim, out, indent, "", "");
382 fprintf(out, "%*s", indent, "");
383 for (i = 0; i < set->n; ++i) {
385 fprintf(out, " union ");
386 basic_set_print_omega(set->p[i], out);
391 static void print_disjunct(__isl_keep isl_basic_map *bmap,
392 __isl_keep isl_dim *dim, FILE *out, int set)
394 if (bmap->n_div > 0) {
396 fprintf(out, "exists (");
397 for (i = 0; i < bmap->n_div; ++i) {
400 print_name(dim, out, isl_dim_div, i, 0);
401 if (isl_int_is_zero(bmap->div[i][0]))
403 fprintf(out, " = [(");
404 print_affine(bmap, dim, out, bmap->div[i] + 1, set);
406 isl_int_print(out, bmap->div[i][0], 0);
412 print_constraints(bmap, dim, out, set);
418 static void isl_basic_map_print_isl(__isl_keep isl_basic_map *bmap, FILE *out,
419 int indent, const char *prefix, const char *suffix)
423 fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
424 if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
425 print_tuple(bmap->dim, out, isl_dim_param, 0);
426 fprintf(out, " -> ");
429 print_tuple(bmap->dim, out, isl_dim_in, 0);
430 fprintf(out, " -> ");
431 print_tuple(bmap->dim, out, isl_dim_out, 0);
433 print_disjunct(bmap, bmap->dim, out, 0);
434 fprintf(out, " }%s\n", suffix ? suffix : "");
437 static void isl_basic_set_print_isl(__isl_keep isl_basic_set *bset, FILE *out,
438 int indent, const char *prefix, const char *suffix)
442 fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
443 if (isl_basic_set_dim(bset, isl_dim_param) > 0) {
444 print_tuple(bset->dim, out, isl_dim_param, 0);
445 fprintf(out, " -> ");
448 print_tuple(bset->dim, out, isl_dim_set, 1);
450 print_disjunct((isl_basic_map *)bset, bset->dim, out, 1);
451 fprintf(out, " }%s\n", suffix ? suffix : "");
454 static void print_disjuncts(__isl_keep isl_map *map, FILE *out, int set)
458 if (isl_map_fast_is_universe(map))
463 fprintf(out, "1 = 0");
464 for (i = 0; i < map->n; ++i) {
466 fprintf(out, " or ");
467 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
469 print_disjunct(map->p[i], map->dim, out, set);
470 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
475 static void isl_map_print_isl(__isl_keep isl_map *map, FILE *out, int indent)
477 fprintf(out, "%*s", indent, "");
478 if (isl_map_dim(map, isl_dim_param) > 0) {
479 print_tuple(map->dim, out, isl_dim_param, 0);
480 fprintf(out, " -> ");
483 print_tuple(map->dim, out, isl_dim_in, 0);
484 fprintf(out, " -> ");
485 print_tuple(map->dim, out, isl_dim_out, 0);
486 print_disjuncts(map, out, 0);
487 fprintf(out, " }\n");
490 static void isl_set_print_isl(__isl_keep isl_set *set, FILE *out, int indent)
494 fprintf(out, "%*s", indent, "");
495 if (isl_set_dim(set, isl_dim_param) > 0) {
496 print_tuple(set->dim, out, isl_dim_param, 0);
497 fprintf(out, " -> ");
500 print_tuple(set->dim, out, isl_dim_set, 1);
501 print_disjuncts((isl_map *)set, out, 1);
502 fprintf(out, " }\n");
505 void isl_basic_map_print(__isl_keep isl_basic_map *bmap, FILE *out, int indent,
506 const char *prefix, const char *suffix, unsigned output_format)
510 if (output_format == ISL_FORMAT_ISL)
511 isl_basic_map_print_isl(bmap, out, indent, prefix, suffix);
512 else if (output_format == ISL_FORMAT_OMEGA)
513 isl_basic_map_print_omega(bmap, out, indent, prefix, suffix);
515 isl_assert(bmap->ctx, 0, return);
518 void isl_basic_set_print(struct isl_basic_set *bset, FILE *out, int indent,
519 const char *prefix, const char *suffix, unsigned output_format)
523 if (output_format == ISL_FORMAT_ISL)
524 isl_basic_set_print_isl(bset, out, indent, prefix, suffix);
525 else if (output_format == ISL_FORMAT_POLYLIB)
526 isl_basic_set_print_polylib(bset, out, indent, prefix, suffix);
527 else if (output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
528 bset_print_constraints_polylib(bset, out, indent, prefix, suffix);
529 else if (output_format == ISL_FORMAT_OMEGA)
530 isl_basic_set_print_omega(bset, out, indent, prefix, suffix);
532 isl_assert(bset->ctx, 0, return);
535 void isl_set_print(struct isl_set *set, FILE *out, int indent,
536 unsigned output_format)
540 if (output_format == ISL_FORMAT_ISL)
541 isl_set_print_isl(set, out, indent);
542 else if (output_format == ISL_FORMAT_POLYLIB)
543 isl_set_print_polylib(set, out, indent);
544 else if (output_format == ISL_FORMAT_OMEGA)
545 isl_set_print_omega(set, out, indent);
547 isl_assert(set->ctx, 0, return);
550 void isl_map_print(__isl_keep isl_map *map, FILE *out, int indent,
551 unsigned output_format)
555 if (output_format == ISL_FORMAT_ISL)
556 isl_map_print_isl(map, out, indent);
557 else if (output_format == ISL_FORMAT_POLYLIB)
558 isl_map_print_polylib(map, out, indent);
559 else if (output_format == ISL_FORMAT_OMEGA)
560 isl_map_print_omega(map, out, indent);
562 isl_assert(map->ctx, 0, return);