isl_map_read: use more uniform way of reading in polylib constraint coefficients
[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
15 static void print_constraint_polylib(struct isl_basic_set *bset,
16         int ineq, int n,
17         FILE *out, int indent, const char *prefix, const char *suffix)
18 {
19         int i;
20         unsigned dim = isl_basic_set_n_dim(bset);
21         unsigned nparam = isl_basic_set_n_param(bset);
22         isl_int *c = ineq ? bset->ineq[n] : bset->eq[n];
23
24         fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
25         fprintf(out, "%d", ineq);
26         for (i = 0; i < dim; ++i) {
27                 fprintf(out, " ");
28                 isl_int_print(out, c[1+nparam+i], 5);
29         }
30         for (i = 0; i < bset->n_div; ++i) {
31                 fprintf(out, " ");
32                 isl_int_print(out, c[1+nparam+dim+i], 5);
33         }
34         for (i = 0; i < nparam; ++i) {
35                 fprintf(out, " ");
36                 isl_int_print(out, c[1+i], 5);
37         }
38         fprintf(out, " ");
39         isl_int_print(out, c[0], 5);
40         fprintf(out, "%s\n", suffix ? suffix : "");
41 }
42
43 static void print_constraints_polylib(struct isl_basic_set *bset,
44         FILE *out, int indent, const char *prefix, const char *suffix)
45 {
46         int i;
47
48         for (i = 0; i < bset->n_eq; ++i)
49                 print_constraint_polylib(bset, 0, i, out,
50                                         indent, prefix, suffix);
51         for (i = 0; i < bset->n_ineq; ++i)
52                 print_constraint_polylib(bset, 1, i, out,
53                                         indent, prefix, suffix);
54 }
55
56 static void isl_basic_set_print_polylib(struct isl_basic_set *bset, FILE *out,
57         int indent, const char *prefix, const char *suffix)
58 {
59         unsigned total = isl_basic_set_total_dim(bset);
60         fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
61         fprintf(out, "%d %d", bset->n_eq + bset->n_ineq, 1 + total + 1);
62         fprintf(out, "%s\n", suffix ? suffix : "");
63         print_constraints_polylib(bset, out, indent, prefix, suffix);
64 }
65
66 static void isl_set_print_polylib(struct isl_set *set, FILE *out, int indent)
67 {
68         int i;
69
70         fprintf(out, "%*s", indent, "");
71         fprintf(out, "%d\n", set->n);
72         for (i = 0; i < set->n; ++i) {
73                 fprintf(out, "\n");
74                 isl_basic_set_print_polylib(set->p[i], out, indent, NULL, NULL);
75         }
76 }
77
78 static print_name(struct isl_dim *dim, FILE *out,
79         enum isl_dim_type type, unsigned pos, int set)
80 {
81         const char *name;
82
83         name = type == isl_dim_div ? NULL : isl_dim_get_name(dim, type, pos);
84
85         if (name)
86                 fprintf(out, "%s", name);
87         else {
88                 const char *prefix;
89                 if (type == isl_dim_param)
90                         prefix = "p";
91                 else if (type == isl_dim_div)
92                         prefix = "e";
93                 else if (set || type == isl_dim_in)
94                         prefix = "i";
95                 else
96                         prefix = "o";
97                 fprintf(out, "%s%d", prefix, pos);
98         }
99 }
100
101 static void print_var_list(struct isl_dim *dim, FILE *out,
102         enum isl_dim_type type, int set)
103 {
104         int i;
105
106         for (i = 0; i < isl_dim_size(dim, type); ++i) {
107                 if (i)
108                         fprintf(out, ", ");
109                 print_name(dim, out, type, i, set);
110         }
111 }
112
113 static void print_tuple(__isl_keep isl_dim *dim, FILE *out,
114         enum isl_dim_type type, int set)
115 {
116         fprintf(out, "[");
117         print_var_list(dim, out, type, set);
118         fprintf(out, "]");
119 }
120
121 static void print_omega_parameters(struct isl_dim *dim, FILE *out,
122         int indent, const char *prefix, const char *suffix)
123 {
124         if (isl_dim_size(dim, isl_dim_param) == 0)
125                 return;
126
127         fprintf(out, "%*s%ssymbolic ", indent, "", prefix ? prefix : "");
128         print_var_list(dim, out, isl_dim_param, 0);
129         fprintf(out, ";%s\n", suffix ? suffix : "");
130 }
131
132 static void print_term(__isl_keep isl_dim *dim,
133                         isl_int c, int pos, FILE *out, int set)
134 {
135         enum isl_dim_type type;
136         unsigned n_in = isl_dim_size(dim, isl_dim_in);
137         unsigned n_out = isl_dim_size(dim, isl_dim_out);
138         unsigned nparam = isl_dim_size(dim, isl_dim_param);
139
140         if (pos == 0) {
141                 isl_int_print(out, c, 0);
142                 return;
143         }
144
145         if (!isl_int_is_one(c))
146                 isl_int_print(out, c, 0);
147         if (pos < 1 + nparam) {
148                 type = isl_dim_param;
149                 pos -= 1;
150         } else if (pos < 1 + nparam + n_in) {
151                 type = isl_dim_in;
152                 pos -= 1 + nparam;
153         } else if (pos < 1 + nparam + n_in + n_out) {
154                 type = isl_dim_out;
155                 pos -= 1 + nparam + n_in;
156         } else {
157                 type = isl_dim_div;
158                 pos -= 1 + nparam + n_in + n_out;
159         }
160         print_name(dim, out, type, pos, set);
161 }
162
163 static void print_affine(__isl_keep isl_basic_map *bmap, FILE *out,
164         isl_int *c, int set)
165 {
166         int i;
167         int first;
168         unsigned len = 1 + isl_basic_map_total_dim(bmap);
169
170         for (i = 0, first = 1; i < len; ++i) {
171                 if (isl_int_is_zero(c[i]))
172                         continue;
173                 if (!first && isl_int_is_pos(c[i]))
174                         fprintf(out, " + ");
175                 first = 0;
176                 print_term(bmap->dim, c[i], i, out, set);
177         }
178         if (first)
179                 fprintf(out, "0");
180 }
181
182 static void print_constraint(struct isl_basic_map *bmap, FILE *out,
183         isl_int *c, const char *suffix, int first_constraint, int set)
184 {
185         if (!first_constraint)
186                 fprintf(out, " and ");
187
188         print_affine(bmap, out, c, set);
189
190         fprintf(out, " %s", suffix);
191 }
192
193 static void print_constraints(__isl_keep isl_basic_map *bmap, FILE *out,
194         int set)
195 {
196         int i;
197
198         for (i = 0; i < bmap->n_eq; ++i)
199                 print_constraint(bmap, out, bmap->eq[i], "= 0", !i, set);
200         for (i = 0; i < bmap->n_ineq; ++i)
201                 print_constraint(bmap, out, bmap->ineq[i], ">= 0",
202                                         !bmap->n_eq && !i, set);
203 }
204
205 static void print_omega_constraints(__isl_keep isl_basic_map *bmap, FILE *out,
206         int set)
207 {
208         if (bmap->n_eq + bmap->n_ineq == 0)
209                 return;
210
211         fprintf(out, ": ");
212         if (bmap->n_div > 0) {
213                 int i;
214                 fprintf(out, "exists (");
215                 for (i = 0; i < bmap->n_div; ++i) {
216                         if (i)
217                                 fprintf(out, ", ");
218                         print_name(bmap->dim, out, isl_dim_div, i, 0);
219                 }
220                 fprintf(out, ": ");
221         }
222         print_constraints(bmap, out, set);
223         if (bmap->n_div > 0)
224                 fprintf(out, ")");
225 }
226
227 static void basic_map_print_omega(struct isl_basic_map *bmap, FILE *out)
228 {
229         fprintf(out, "{ [");
230         print_var_list(bmap->dim, out, isl_dim_in, 0);
231         fprintf(out, "] -> [");
232         print_var_list(bmap->dim, out, isl_dim_out, 0);
233         fprintf(out, "] ");
234         print_omega_constraints(bmap, out, 0);
235         fprintf(out, " }");
236 }
237
238 static void isl_basic_map_print_omega(struct isl_basic_map *bmap, FILE *out,
239         int indent, const char *prefix, const char *suffix)
240 {
241         print_omega_parameters(bmap->dim, out, indent, prefix, suffix);
242
243         fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
244         basic_map_print_omega(bmap, out);
245         fprintf(out, "%s\n", suffix ? suffix : "");
246 }
247
248 static void basic_set_print_omega(struct isl_basic_set *bset, FILE *out)
249 {
250         fprintf(out, "{ [");
251         print_var_list(bset->dim, out, isl_dim_set, 1);
252         fprintf(out, "] ");
253         print_omega_constraints((isl_basic_map *)bset, out, 1);
254         fprintf(out, " }");
255 }
256
257 static void isl_basic_set_print_omega(struct isl_basic_set *bset, FILE *out,
258         int indent, const char *prefix, const char *suffix)
259 {
260         print_omega_parameters(bset->dim, out, indent, prefix, suffix);
261
262         fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
263         basic_set_print_omega(bset, out);
264         fprintf(out, "%s\n", suffix ? suffix : "");
265 }
266
267 static void isl_map_print_omega(struct isl_map *map, FILE *out, int indent)
268 {
269         int i;
270
271         print_omega_parameters(map->dim, out, indent, "", "");
272
273         fprintf(out, "%*s", indent, "");
274         for (i = 0; i < map->n; ++i) {
275                 if (i)
276                         fprintf(out, " union ");
277                 basic_map_print_omega(map->p[i], out);
278         }
279         fprintf(out, "\n");
280 }
281
282 static void isl_set_print_omega(struct isl_set *set, FILE *out, int indent)
283 {
284         int i;
285
286         print_omega_parameters(set->dim, out, indent, "", "");
287
288         fprintf(out, "%*s", indent, "");
289         for (i = 0; i < set->n; ++i) {
290                 if (i)
291                         fprintf(out, " union ");
292                 basic_set_print_omega(set->p[i], out);
293         }
294         fprintf(out, "\n");
295 }
296
297 static void print_disjunct(__isl_keep isl_basic_map *bmap, FILE *out, int set)
298 {
299         if (bmap->n_div > 0) {
300                 int i;
301                 fprintf(out, "exists (");
302                 for (i = 0; i < bmap->n_div; ++i) {
303                         if (i)
304                                 fprintf(out, ", ");
305                         print_name(bmap->dim, out, isl_dim_div, i, 0);
306                         if (isl_int_is_zero(bmap->div[i][0]))
307                                 continue;
308                         fprintf(out, " = [(");
309                         print_affine(bmap, out, bmap->div[i] + 1, set);
310                         fprintf(out, ")/");
311                         isl_int_print(out, bmap->div[i][0], 0);
312                         fprintf(out, "]");
313                 }
314                 fprintf(out, ": ");
315         }
316
317         print_constraints(bmap, out, set);
318
319         if (bmap->n_div > 0)
320                 fprintf(out, ")");
321 }
322
323 static void isl_basic_map_print_isl(__isl_keep isl_basic_map *bmap, FILE *out,
324         int indent, const char *prefix, const char *suffix)
325 {
326         int i;
327
328         fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
329         if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
330                 print_tuple(bmap->dim, out, isl_dim_param, 0);
331                 fprintf(out, " -> ");
332         }
333         fprintf(out, "{ ");
334         print_tuple(bmap->dim, out, isl_dim_in, 0);
335         fprintf(out, " -> ");
336         print_tuple(bmap->dim, out, isl_dim_out, 0);
337         fprintf(out, " : ");
338         print_disjunct(bmap, out, 0);
339         fprintf(out, " }%s\n", suffix ? suffix : "");
340 }
341
342 static void isl_basic_set_print_isl(__isl_keep isl_basic_set *bset, FILE *out,
343         int indent, const char *prefix, const char *suffix)
344 {
345         int i;
346
347         fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
348         if (isl_basic_set_dim(bset, isl_dim_param) > 0) {
349                 print_tuple(bset->dim, out, isl_dim_param, 0);
350                 fprintf(out, " -> ");
351         }
352         fprintf(out, "{ ");
353         print_tuple(bset->dim, out, isl_dim_set, 1);
354         fprintf(out, " : ");
355         print_disjunct((isl_basic_map *)bset, out, 1);
356         fprintf(out, " }%s\n", suffix ? suffix : "");
357 }
358
359 static void isl_map_print_isl(__isl_keep isl_map *map, FILE *out, int indent)
360 {
361         int i;
362
363         fprintf(out, "%*s", indent, "");
364         if (isl_map_dim(map, isl_dim_param) > 0) {
365                 print_tuple(map->dim, out, isl_dim_param, 0);
366                 fprintf(out, " -> ");
367         }
368         fprintf(out, "{ ");
369         print_tuple(map->dim, out, isl_dim_in, 0);
370         fprintf(out, " -> ");
371         print_tuple(map->dim, out, isl_dim_out, 0);
372         fprintf(out, " : ");
373         if (map->n == 0)
374                 fprintf(out, "1 = 0");
375         for (i = 0; i < map->n; ++i) {
376                 if (i)
377                         fprintf(out, " or ");
378                 print_disjunct(map->p[i], out, 0);
379         }
380         fprintf(out, " }\n");
381 }
382
383 static void isl_set_print_isl(__isl_keep isl_set *set, FILE *out, int indent)
384 {
385         int i;
386
387         fprintf(out, "%*s", indent, "");
388         if (isl_set_dim(set, isl_dim_param) > 0) {
389                 print_tuple(set->dim, out, isl_dim_param, 0);
390                 fprintf(out, " -> ");
391         }
392         fprintf(out, "{ ");
393         print_tuple(set->dim, out, isl_dim_set, 1);
394         fprintf(out, " : ");
395         if (set->n == 0)
396                 fprintf(out, "1 = 0");
397         for (i = 0; i < set->n; ++i) {
398                 if (i)
399                         fprintf(out, " or ");
400                 print_disjunct((isl_basic_map *)set->p[i], out, 1);
401         }
402         fprintf(out, " }\n");
403 }
404
405 void isl_basic_map_print(__isl_keep isl_basic_map *bmap, FILE *out, int indent,
406         const char *prefix, const char *suffix, unsigned output_format)
407 {
408         if (!bmap)
409                 return;
410         if (output_format == ISL_FORMAT_ISL)
411                 isl_basic_map_print_isl(bmap, out, indent, prefix, suffix);
412         else if (output_format == ISL_FORMAT_OMEGA)
413                 isl_basic_map_print_omega(bmap, out, indent, prefix, suffix);
414         else
415                 isl_assert(bmap->ctx, 0, return);
416 }
417
418 void isl_basic_set_print(struct isl_basic_set *bset, FILE *out, int indent,
419         const char *prefix, const char *suffix, unsigned output_format)
420 {
421         if (!bset)
422                 return;
423         if (output_format == ISL_FORMAT_ISL)
424                 isl_basic_set_print_isl(bset, out, indent, prefix, suffix);
425         else if (output_format == ISL_FORMAT_POLYLIB)
426                 isl_basic_set_print_polylib(bset, out, indent, prefix, suffix);
427         else if (output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
428                 print_constraints_polylib(bset, out, indent, prefix, suffix);
429         else if (output_format == ISL_FORMAT_OMEGA)
430                 isl_basic_set_print_omega(bset, out, indent, prefix, suffix);
431         else
432                 isl_assert(bset->ctx, 0, return);
433 }
434
435 void isl_set_print(struct isl_set *set, FILE *out, int indent,
436         unsigned output_format)
437 {
438         if (!set)
439                 return;
440         if (output_format == ISL_FORMAT_ISL)
441                 isl_set_print_isl(set, out, indent);
442         else if (output_format == ISL_FORMAT_POLYLIB)
443                 isl_set_print_polylib(set, out, indent);
444         else if (output_format == ISL_FORMAT_OMEGA)
445                 isl_set_print_omega(set, out, indent);
446         else
447                 isl_assert(set->ctx, 0, return);
448 }
449
450 void isl_map_print(__isl_keep isl_map *map, FILE *out, int indent,
451         unsigned output_format)
452 {
453         if (!map)
454                 return;
455         if (output_format == ISL_FORMAT_ISL)
456                 isl_map_print_isl(map, out, indent);
457         else if (output_format == ISL_FORMAT_OMEGA)
458                 isl_map_print_omega(map, out, indent);
459         else
460                 isl_assert(map->ctx, 0, return);
461 }