isl_map_print: use consistent names on all disjuncts
[platform/upstream/isl.git] / isl_output.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  * Copyright 2010      INRIA Saclay
4  *
5  * Use of this software is governed by the GNU LGPLv2.1 license
6  *
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 
11  */
12
13 #include <isl_set.h>
14 #include <isl_seq.h>
15
16 static void print_constraint_polylib(struct isl_basic_map *bmap,
17         int ineq, int n,
18         FILE *out, int indent, const char *prefix, const char *suffix)
19 {
20         int i;
21         unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
22         unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
23         unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
24         isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
25
26         fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
27         fprintf(out, "%d", ineq);
28         for (i = 0; i < n_out; ++i) {
29                 fprintf(out, " ");
30                 isl_int_print(out, c[1+nparam+n_in+i], 5);
31         }
32         for (i = 0; i < n_in; ++i) {
33                 fprintf(out, " ");
34                 isl_int_print(out, c[1+nparam+i], 5);
35         }
36         for (i = 0; i < bmap->n_div; ++i) {
37                 fprintf(out, " ");
38                 isl_int_print(out, c[1+nparam+n_in+n_out+i], 5);
39         }
40         for (i = 0; i < nparam; ++i) {
41                 fprintf(out, " ");
42                 isl_int_print(out, c[1+i], 5);
43         }
44         fprintf(out, " ");
45         isl_int_print(out, c[0], 5);
46         fprintf(out, "%s\n", suffix ? suffix : "");
47 }
48
49 static void print_constraints_polylib(struct isl_basic_map *bmap,
50         FILE *out, int indent, const char *prefix, const char *suffix)
51 {
52         int i;
53
54         for (i = 0; i < bmap->n_eq; ++i)
55                 print_constraint_polylib(bmap, 0, i, out,
56                                         indent, prefix, suffix);
57         for (i = 0; i < bmap->n_ineq; ++i)
58                 print_constraint_polylib(bmap, 1, i, out,
59                                         indent, prefix, suffix);
60 }
61
62 static void bset_print_constraints_polylib(struct isl_basic_set *bset,
63         FILE *out, int indent, const char *prefix, const char *suffix)
64 {
65         print_constraints_polylib((struct isl_basic_map *)bset,
66                                  out, indent, prefix, suffix);
67 }
68
69 static void isl_basic_map_print_polylib(struct isl_basic_map *bmap, FILE *out,
70         int indent, const char *prefix, const char *suffix)
71 {
72         unsigned total = isl_basic_map_total_dim(bmap);
73         fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
74         fprintf(out, "%d %d", bmap->n_eq + bmap->n_ineq, 1 + total + 1);
75         fprintf(out, "%s\n", suffix ? suffix : "");
76         print_constraints_polylib(bmap, out, indent, prefix, suffix);
77 }
78
79 static void isl_basic_set_print_polylib(struct isl_basic_set *bset, FILE *out,
80         int indent, const char *prefix, const char *suffix)
81 {
82         isl_basic_map_print_polylib((struct isl_basic_map *)bset, out,
83                                         indent, prefix, suffix);
84 }
85
86 static void isl_map_print_polylib(struct isl_map *map, FILE *out, int indent)
87 {
88         int i;
89
90         fprintf(out, "%*s", indent, "");
91         fprintf(out, "%d\n", map->n);
92         for (i = 0; i < map->n; ++i) {
93                 fprintf(out, "\n");
94                 isl_basic_map_print_polylib(map->p[i], out, indent, NULL, NULL);
95         }
96 }
97
98 static void isl_set_print_polylib(struct isl_set *set, FILE *out, int indent)
99 {
100         isl_map_print_polylib((struct isl_map *)set, out, indent);
101 }
102
103 static print_name(struct isl_dim *dim, FILE *out,
104         enum isl_dim_type type, unsigned pos, int set)
105 {
106         const char *name;
107
108         name = type == isl_dim_div ? NULL : isl_dim_get_name(dim, type, pos);
109
110         if (name)
111                 fprintf(out, "%s", name);
112         else {
113                 const char *prefix;
114                 if (type == isl_dim_param)
115                         prefix = "p";
116                 else if (type == isl_dim_div)
117                         prefix = "e";
118                 else if (set || type == isl_dim_in)
119                         prefix = "i";
120                 else
121                         prefix = "o";
122                 fprintf(out, "%s%d", prefix, pos);
123         }
124 }
125
126 static void print_var_list(struct isl_dim *dim, FILE *out,
127         enum isl_dim_type type, int set)
128 {
129         int i;
130
131         for (i = 0; i < isl_dim_size(dim, type); ++i) {
132                 if (i)
133                         fprintf(out, ", ");
134                 print_name(dim, out, type, i, set);
135         }
136 }
137
138 static void print_tuple(__isl_keep isl_dim *dim, FILE *out,
139         enum isl_dim_type type, int set)
140 {
141         fprintf(out, "[");
142         print_var_list(dim, out, type, set);
143         fprintf(out, "]");
144 }
145
146 static void print_omega_parameters(struct isl_dim *dim, FILE *out,
147         int indent, const char *prefix, const char *suffix)
148 {
149         if (isl_dim_size(dim, isl_dim_param) == 0)
150                 return;
151
152         fprintf(out, "%*s%ssymbolic ", indent, "", prefix ? prefix : "");
153         print_var_list(dim, out, isl_dim_param, 0);
154         fprintf(out, ";%s\n", suffix ? suffix : "");
155 }
156
157 static void print_term(__isl_keep isl_dim *dim,
158                         isl_int c, int pos, FILE *out, int set)
159 {
160         enum isl_dim_type type;
161         unsigned n_in = isl_dim_size(dim, isl_dim_in);
162         unsigned n_out = isl_dim_size(dim, isl_dim_out);
163         unsigned nparam = isl_dim_size(dim, isl_dim_param);
164
165         if (pos == 0) {
166                 isl_int_print(out, c, 0);
167                 return;
168         }
169
170         if (isl_int_is_one(c))
171                 ;
172         else if (isl_int_is_negone(c))
173                 fprintf(out, "-");
174         else
175                 isl_int_print(out, c, 0);
176         if (pos < 1 + nparam) {
177                 type = isl_dim_param;
178                 pos -= 1;
179         } else if (pos < 1 + nparam + n_in) {
180                 type = isl_dim_in;
181                 pos -= 1 + nparam;
182         } else if (pos < 1 + nparam + n_in + n_out) {
183                 type = isl_dim_out;
184                 pos -= 1 + nparam + n_in;
185         } else {
186                 type = isl_dim_div;
187                 pos -= 1 + nparam + n_in + n_out;
188         }
189         print_name(dim, out, type, pos, set);
190 }
191
192 static void print_affine(__isl_keep isl_basic_map *bmap,
193         __isl_keep isl_dim *dim, FILE *out, isl_int *c, int set)
194 {
195         int i;
196         int first;
197         unsigned len = 1 + isl_basic_map_total_dim(bmap);
198
199         for (i = 0, first = 1; i < len; ++i) {
200                 int flip = 0;
201                 if (isl_int_is_zero(c[i]))
202                         continue;
203                 if (!first) {
204                         if (isl_int_is_neg(c[i])) {
205                                 flip = 1;
206                                 isl_int_neg(c[i], c[i]);
207                                 fprintf(out, " - ");
208                         } else 
209                                 fprintf(out, " + ");
210                 }
211                 first = 0;
212                 print_term(dim, c[i], i, out, set);
213                 if (flip)
214                         isl_int_neg(c[i], c[i]);
215         }
216         if (first)
217                 fprintf(out, "0");
218 }
219
220 static void print_constraint(struct isl_basic_map *bmap,
221         __isl_keep isl_dim *dim, FILE *out,
222         isl_int *c, int last, const char *op, int first_constraint, int set)
223 {
224         if (!first_constraint)
225                 fprintf(out, " and ");
226
227         isl_int_abs(c[last], c[last]);
228
229         print_term(dim, c[last], last, out, set);
230
231         fprintf(out, " %s ", op);
232
233         isl_int_set_si(c[last], 0);
234         print_affine(bmap, dim, out, c, set);
235 }
236
237 static void print_constraints(__isl_keep isl_basic_map *bmap,
238         __isl_keep isl_dim *dim, FILE *out, int set)
239 {
240         int i;
241         struct isl_vec *c;
242         unsigned total = isl_basic_map_total_dim(bmap);
243
244         c = isl_vec_alloc(bmap->ctx, 1 + total);
245         if (!c)
246                 return;
247
248         for (i = bmap->n_eq - 1; i >= 0; --i) {
249                 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
250                 isl_assert(bmap->ctx, l >= 0, return);
251                 if (isl_int_is_neg(bmap->eq[i][l]))
252                         isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
253                 else
254                         isl_seq_neg(c->el, bmap->eq[i], 1 + total);
255                 print_constraint(bmap, dim, out, c->el, l,
256                                     "=", i == bmap->n_eq - 1, set);
257         }
258         for (i = 0; i < bmap->n_ineq; ++i) {
259                 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
260                 int s;
261                 isl_assert(bmap->ctx, l >= 0, return);
262                 s = isl_int_sgn(bmap->ineq[i][l]);
263                 if (s < 0)
264                         isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
265                 else
266                         isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
267                 print_constraint(bmap, dim, out, c->el, l,
268                                     s < 0 ? "<=" : ">=", !bmap->n_eq && !i, set);
269         }
270
271         isl_vec_free(c);
272 }
273
274 static void print_omega_constraints(__isl_keep isl_basic_map *bmap, FILE *out,
275         int set)
276 {
277         if (bmap->n_eq + bmap->n_ineq == 0)
278                 return;
279
280         fprintf(out, ": ");
281         if (bmap->n_div > 0) {
282                 int i;
283                 fprintf(out, "exists (");
284                 for (i = 0; i < bmap->n_div; ++i) {
285                         if (i)
286                                 fprintf(out, ", ");
287                         print_name(bmap->dim, out, isl_dim_div, i, 0);
288                 }
289                 fprintf(out, ": ");
290         }
291         print_constraints(bmap, bmap->dim, out, set);
292         if (bmap->n_div > 0)
293                 fprintf(out, ")");
294 }
295
296 static void basic_map_print_omega(struct isl_basic_map *bmap, FILE *out)
297 {
298         fprintf(out, "{ [");
299         print_var_list(bmap->dim, out, isl_dim_in, 0);
300         fprintf(out, "] -> [");
301         print_var_list(bmap->dim, out, isl_dim_out, 0);
302         fprintf(out, "] ");
303         print_omega_constraints(bmap, out, 0);
304         fprintf(out, " }");
305 }
306
307 static void isl_basic_map_print_omega(struct isl_basic_map *bmap, FILE *out,
308         int indent, const char *prefix, const char *suffix)
309 {
310         print_omega_parameters(bmap->dim, out, indent, prefix, suffix);
311
312         fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
313         basic_map_print_omega(bmap, out);
314         fprintf(out, "%s\n", suffix ? suffix : "");
315 }
316
317 static void basic_set_print_omega(struct isl_basic_set *bset, FILE *out)
318 {
319         fprintf(out, "{ [");
320         print_var_list(bset->dim, out, isl_dim_set, 1);
321         fprintf(out, "] ");
322         print_omega_constraints((isl_basic_map *)bset, out, 1);
323         fprintf(out, " }");
324 }
325
326 static void isl_basic_set_print_omega(struct isl_basic_set *bset, FILE *out,
327         int indent, const char *prefix, const char *suffix)
328 {
329         print_omega_parameters(bset->dim, out, indent, prefix, suffix);
330
331         fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
332         basic_set_print_omega(bset, out);
333         fprintf(out, "%s\n", suffix ? suffix : "");
334 }
335
336 static void isl_map_print_omega(struct isl_map *map, FILE *out, int indent)
337 {
338         int i;
339
340         print_omega_parameters(map->dim, out, indent, "", "");
341
342         fprintf(out, "%*s", indent, "");
343         for (i = 0; i < map->n; ++i) {
344                 if (i)
345                         fprintf(out, " union ");
346                 basic_map_print_omega(map->p[i], out);
347         }
348         fprintf(out, "\n");
349 }
350
351 static void isl_set_print_omega(struct isl_set *set, FILE *out, int indent)
352 {
353         int i;
354
355         print_omega_parameters(set->dim, out, indent, "", "");
356
357         fprintf(out, "%*s", indent, "");
358         for (i = 0; i < set->n; ++i) {
359                 if (i)
360                         fprintf(out, " union ");
361                 basic_set_print_omega(set->p[i], out);
362         }
363         fprintf(out, "\n");
364 }
365
366 static void print_disjunct(__isl_keep isl_basic_map *bmap,
367         __isl_keep isl_dim *dim, FILE *out, int set)
368 {
369         if (bmap->n_div > 0) {
370                 int i;
371                 fprintf(out, "exists (");
372                 for (i = 0; i < bmap->n_div; ++i) {
373                         if (i)
374                                 fprintf(out, ", ");
375                         print_name(dim, out, isl_dim_div, i, 0);
376                         if (isl_int_is_zero(bmap->div[i][0]))
377                                 continue;
378                         fprintf(out, " = [(");
379                         print_affine(bmap, dim, out, bmap->div[i] + 1, set);
380                         fprintf(out, ")/");
381                         isl_int_print(out, bmap->div[i][0], 0);
382                         fprintf(out, "]");
383                 }
384                 fprintf(out, ": ");
385         }
386
387         print_constraints(bmap, dim, out, set);
388
389         if (bmap->n_div > 0)
390                 fprintf(out, ")");
391 }
392
393 static void isl_basic_map_print_isl(__isl_keep isl_basic_map *bmap, FILE *out,
394         int indent, const char *prefix, const char *suffix)
395 {
396         int i;
397
398         fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
399         if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
400                 print_tuple(bmap->dim, out, isl_dim_param, 0);
401                 fprintf(out, " -> ");
402         }
403         fprintf(out, "{ ");
404         print_tuple(bmap->dim, out, isl_dim_in, 0);
405         fprintf(out, " -> ");
406         print_tuple(bmap->dim, out, isl_dim_out, 0);
407         fprintf(out, " : ");
408         print_disjunct(bmap, bmap->dim, out, 0);
409         fprintf(out, " }%s\n", suffix ? suffix : "");
410 }
411
412 static void isl_basic_set_print_isl(__isl_keep isl_basic_set *bset, FILE *out,
413         int indent, const char *prefix, const char *suffix)
414 {
415         int i;
416
417         fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
418         if (isl_basic_set_dim(bset, isl_dim_param) > 0) {
419                 print_tuple(bset->dim, out, isl_dim_param, 0);
420                 fprintf(out, " -> ");
421         }
422         fprintf(out, "{ ");
423         print_tuple(bset->dim, out, isl_dim_set, 1);
424         fprintf(out, " : ");
425         print_disjunct((isl_basic_map *)bset, bset->dim, out, 1);
426         fprintf(out, " }%s\n", suffix ? suffix : "");
427 }
428
429 static void isl_map_print_isl(__isl_keep isl_map *map, FILE *out, int indent)
430 {
431         int i;
432
433         fprintf(out, "%*s", indent, "");
434         if (isl_map_dim(map, isl_dim_param) > 0) {
435                 print_tuple(map->dim, out, isl_dim_param, 0);
436                 fprintf(out, " -> ");
437         }
438         fprintf(out, "{ ");
439         print_tuple(map->dim, out, isl_dim_in, 0);
440         fprintf(out, " -> ");
441         print_tuple(map->dim, out, isl_dim_out, 0);
442         fprintf(out, " : ");
443         if (map->n == 0)
444                 fprintf(out, "1 = 0");
445         for (i = 0; i < map->n; ++i) {
446                 if (i)
447                         fprintf(out, " or ");
448                 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
449                         fprintf(out, "(");
450                 print_disjunct(map->p[i], map->dim, out, 0);
451                 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
452                         fprintf(out, ")");
453         }
454         fprintf(out, " }\n");
455 }
456
457 static void isl_set_print_isl(__isl_keep isl_set *set, FILE *out, int indent)
458 {
459         int i;
460
461         fprintf(out, "%*s", indent, "");
462         if (isl_set_dim(set, isl_dim_param) > 0) {
463                 print_tuple(set->dim, out, isl_dim_param, 0);
464                 fprintf(out, " -> ");
465         }
466         fprintf(out, "{ ");
467         print_tuple(set->dim, out, isl_dim_set, 1);
468         fprintf(out, " : ");
469         if (set->n == 0)
470                 fprintf(out, "1 = 0");
471         for (i = 0; i < set->n; ++i) {
472                 if (i)
473                         fprintf(out, " or ");
474                 print_disjunct((isl_basic_map *)set->p[i], set->dim, out, 1);
475         }
476         fprintf(out, " }\n");
477 }
478
479 void isl_basic_map_print(__isl_keep isl_basic_map *bmap, FILE *out, int indent,
480         const char *prefix, const char *suffix, unsigned output_format)
481 {
482         if (!bmap)
483                 return;
484         if (output_format == ISL_FORMAT_ISL)
485                 isl_basic_map_print_isl(bmap, out, indent, prefix, suffix);
486         else if (output_format == ISL_FORMAT_OMEGA)
487                 isl_basic_map_print_omega(bmap, out, indent, prefix, suffix);
488         else
489                 isl_assert(bmap->ctx, 0, return);
490 }
491
492 void isl_basic_set_print(struct isl_basic_set *bset, FILE *out, int indent,
493         const char *prefix, const char *suffix, unsigned output_format)
494 {
495         if (!bset)
496                 return;
497         if (output_format == ISL_FORMAT_ISL)
498                 isl_basic_set_print_isl(bset, out, indent, prefix, suffix);
499         else if (output_format == ISL_FORMAT_POLYLIB)
500                 isl_basic_set_print_polylib(bset, out, indent, prefix, suffix);
501         else if (output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
502                 bset_print_constraints_polylib(bset, out, indent, prefix, suffix);
503         else if (output_format == ISL_FORMAT_OMEGA)
504                 isl_basic_set_print_omega(bset, out, indent, prefix, suffix);
505         else
506                 isl_assert(bset->ctx, 0, return);
507 }
508
509 void isl_set_print(struct isl_set *set, FILE *out, int indent,
510         unsigned output_format)
511 {
512         if (!set)
513                 return;
514         if (output_format == ISL_FORMAT_ISL)
515                 isl_set_print_isl(set, out, indent);
516         else if (output_format == ISL_FORMAT_POLYLIB)
517                 isl_set_print_polylib(set, out, indent);
518         else if (output_format == ISL_FORMAT_OMEGA)
519                 isl_set_print_omega(set, out, indent);
520         else
521                 isl_assert(set->ctx, 0, return);
522 }
523
524 void isl_map_print(__isl_keep isl_map *map, FILE *out, int indent,
525         unsigned output_format)
526 {
527         if (!map)
528                 return;
529         if (output_format == ISL_FORMAT_ISL)
530                 isl_map_print_isl(map, out, indent);
531         else if (output_format == ISL_FORMAT_POLYLIB)
532                 isl_map_print_polylib(map, out, indent);
533         else if (output_format == ISL_FORMAT_OMEGA)
534                 isl_map_print_omega(map, out, indent);
535         else
536                 isl_assert(map->ctx, 0, return);
537 }