Coverage Report

Created: 2018-06-24 14:39

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/External/isl/isl_output.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2008-2009 Katholieke Universiteit Leuven
3
 * Copyright 2010      INRIA Saclay
4
 * Copyright 2012-2013 Ecole Normale Superieure
5
 *
6
 * Use of this software is governed by the MIT license
7
 *
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
13
 */
14
15
#include <stdlib.h>
16
#include <string.h>
17
#include <isl_ctx_private.h>
18
#include <isl_map_private.h>
19
#include <isl/set.h>
20
#include <isl_seq.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_set.h>
27
#include <isl/union_map.h>
28
#include <isl/constraint.h>
29
#include <isl_local_space_private.h>
30
#include <isl_aff_private.h>
31
#include <isl_val_private.h>
32
#include <isl_constraint_private.h>
33
#include <isl/ast_build.h>
34
#include <isl_sort.h>
35
#include <isl_output_private.h>
36
37
#include <bset_to_bmap.c>
38
#include <set_to_map.c>
39
#include <uset_to_umap.c>
40
41
static const char *s_to[2] = { " -> ", " \\to " };
42
static const char *s_and[2] = { " and ", " \\wedge " };
43
static const char *s_or[2] = { " or ", " \\vee " };
44
static const char *s_le[2] = { "<=", "\\le" };
45
static const char *s_ge[2] = { ">=", "\\ge" };
46
static const char *s_open_set[2] = { "{ ", "\\{\\, " };
47
static const char *s_close_set[2] = { " }", " \\,\\}" };
48
static const char *s_open_list[2] = { "[", "(" };
49
static const char *s_close_list[2] = { "]", ")" };
50
static const char *s_such_that[2] = { " : ", " \\mid " };
51
static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
52
static const char *s_close_exists[2] = { ")", "" };
53
static const char *s_div_prefix[2] = { "e", "\\alpha_" };
54
static const char *s_mod[2] = { "mod", "\\bmod" };
55
static const char *s_param_prefix[2] = { "p", "p_" };
56
static const char *s_input_prefix[2] = { "i", "i_" };
57
static const char *s_output_prefix[2] = { "o", "o_" };
58
59
static __isl_give isl_printer *print_constraint_polylib(
60
  struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
61
0
{
62
0
  int i;
63
0
  unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
64
0
  unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
65
0
  unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
66
0
  isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
67
0
68
0
  p = isl_printer_start_line(p);
69
0
  p = isl_printer_print_int(p, ineq);
70
0
  for (i = 0; i < n_out; ++i) {
71
0
    p = isl_printer_print_str(p, " ");
72
0
    p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
73
0
  }
74
0
  for (i = 0; i < n_in; ++i) {
75
0
    p = isl_printer_print_str(p, " ");
76
0
    p = isl_printer_print_isl_int(p, c[1+nparam+i]);
77
0
  }
78
0
  for (i = 0; i < bmap->n_div; ++i) {
79
0
    p = isl_printer_print_str(p, " ");
80
0
    p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
81
0
  }
82
0
  for (i = 0; i < nparam; ++i) {
83
0
    p = isl_printer_print_str(p, " ");
84
0
    p = isl_printer_print_isl_int(p, c[1+i]);
85
0
  }
86
0
  p = isl_printer_print_str(p, " ");
87
0
  p = isl_printer_print_isl_int(p, c[0]);
88
0
  p = isl_printer_end_line(p);
89
0
  return p;
90
0
}
91
92
static __isl_give isl_printer *print_constraints_polylib(
93
  struct isl_basic_map *bmap, __isl_take isl_printer *p)
94
0
{
95
0
  int i;
96
0
97
0
  p = isl_printer_set_isl_int_width(p, 5);
98
0
99
0
  for (i = 0; i < bmap->n_eq; ++i)
100
0
    p = print_constraint_polylib(bmap, 0, i, p);
101
0
  for (i = 0; i < bmap->n_ineq; ++i)
102
0
    p = print_constraint_polylib(bmap, 1, i, p);
103
0
104
0
  return p;
105
0
}
106
107
static __isl_give isl_printer *bset_print_constraints_polylib(
108
  struct isl_basic_set *bset, __isl_take isl_printer *p)
109
0
{
110
0
  return print_constraints_polylib(bset_to_bmap(bset), p);
111
0
}
112
113
static __isl_give isl_printer *isl_basic_map_print_polylib(
114
  __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
115
0
{
116
0
  unsigned total = isl_basic_map_total_dim(bmap);
117
0
  p = isl_printer_start_line(p);
118
0
  p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
119
0
  p = isl_printer_print_str(p, " ");
120
0
  p = isl_printer_print_int(p, 1 + total + 1);
121
0
  if (ext) {
122
0
    p = isl_printer_print_str(p, " ");
123
0
    p = isl_printer_print_int(p,
124
0
            isl_basic_map_dim(bmap, isl_dim_out));
125
0
    p = isl_printer_print_str(p, " ");
126
0
    p = isl_printer_print_int(p,
127
0
            isl_basic_map_dim(bmap, isl_dim_in));
128
0
    p = isl_printer_print_str(p, " ");
129
0
    p = isl_printer_print_int(p,
130
0
            isl_basic_map_dim(bmap, isl_dim_div));
131
0
    p = isl_printer_print_str(p, " ");
132
0
    p = isl_printer_print_int(p,
133
0
            isl_basic_map_dim(bmap, isl_dim_param));
134
0
  }
135
0
  p = isl_printer_end_line(p);
136
0
  return print_constraints_polylib(bmap, p);
137
0
}
138
139
static __isl_give isl_printer *isl_basic_set_print_polylib(
140
  __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
141
0
{
142
0
  return isl_basic_map_print_polylib(bset_to_bmap(bset), p, ext);
143
0
}
144
145
static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
146
  __isl_take isl_printer *p, int ext)
147
0
{
148
0
  int i;
149
0
150
0
  p = isl_printer_start_line(p);
151
0
  p = isl_printer_print_int(p, map->n);
152
0
  p = isl_printer_end_line(p);
153
0
  for (i = 0; i < map->n; ++i) {
154
0
    p = isl_printer_start_line(p);
155
0
    p = isl_printer_end_line(p);
156
0
    p = isl_basic_map_print_polylib(map->p[i], p, ext);
157
0
  }
158
0
  return p;
159
0
}
160
161
static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
162
  __isl_take isl_printer *p, int ext)
163
0
{
164
0
  return isl_map_print_polylib(set_to_map(set), p, ext);
165
0
}
166
167
static int count_same_name(__isl_keep isl_space *dim,
168
  enum isl_dim_type type, unsigned pos, const char *name)
169
34.4k
{
170
34.4k
  enum isl_dim_type t;
171
34.4k
  unsigned p, s;
172
34.4k
  int count = 0;
173
34.4k
174
93.9k
  for (t = isl_dim_param; t <= type && 
t <= isl_dim_out71.7k
;
++t59.5k
) {
175
59.5k
    s = t == type ? 
pos22.2k
:
isl_space_dim(dim, t)37.3k
;
176
336k
    for (p = 0; p < s; 
++p277k
) {
177
277k
      const char *n = isl_space_get_dim_name(dim, t, p);
178
277k
      if (n && 
!strcmp(n, name)238k
)
179
0
        count++;
180
277k
    }
181
59.5k
  }
182
34.4k
  return count;
183
34.4k
}
184
185
/* Print the name of the variable of type "type" and position "pos"
186
 * in "space" to "p".
187
 */
188
static __isl_give isl_printer *print_name(__isl_keep isl_space *space,
189
  __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
190
  int latex)
191
34.4k
{
192
34.4k
  const char *name;
193
34.4k
  char buffer[20];
194
34.4k
  int primes;
195
34.4k
196
34.4k
  name = type == isl_dim_div ? NULL
197
34.4k
           : 
isl_space_get_dim_name(space, type, pos)34.4k
;
198
34.4k
199
34.4k
  if (!name) {
200
12.2k
    const char *prefix;
201
12.2k
    if (type == isl_dim_param)
202
0
      prefix = s_param_prefix[latex];
203
12.2k
    else if (type == isl_dim_div)
204
10
      prefix = s_div_prefix[latex];
205
12.2k
    else if (isl_space_is_set(space) || 
type == isl_dim_in10.3k
)
206
11.3k
      prefix = s_input_prefix[latex];
207
841
    else
208
841
      prefix = s_output_prefix[latex];
209
12.2k
    snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
210
12.2k
    name = buffer;
211
12.2k
  }
212
34.4k
  primes = count_same_name(space, name == buffer ? 
isl_dim_div12.2k
:
type22.2k
,
213
34.4k
         pos, name);
214
34.4k
  p = isl_printer_print_str(p, name);
215
34.4k
  while (primes-- > 0)
216
0
    p = isl_printer_print_str(p, "'");
217
34.4k
  return p;
218
34.4k
}
219
220
static enum isl_dim_type pos2type(__isl_keep isl_space *dim, unsigned *pos)
221
11.9k
{
222
11.9k
  enum isl_dim_type type;
223
11.9k
  unsigned n_in = isl_space_dim(dim, isl_dim_in);
224
11.9k
  unsigned n_out = isl_space_dim(dim, isl_dim_out);
225
11.9k
  unsigned nparam = isl_space_dim(dim, isl_dim_param);
226
11.9k
227
11.9k
  if (*pos < 1 + nparam) {
228
5.71k
    type = isl_dim_param;
229
5.71k
    *pos -= 1;
230
6.19k
  } else if (*pos < 1 + nparam + n_in) {
231
4.39k
    type = isl_dim_in;
232
4.39k
    *pos -= 1 + nparam;
233
4.39k
  } else 
if (1.80k
*pos < 1 + nparam + n_in + n_out1.80k
) {
234
1.60k
    type = isl_dim_out;
235
1.60k
    *pos -= 1 + nparam + n_in;
236
1.60k
  } else {
237
202
    type = isl_dim_div;
238
202
    *pos -= 1 + nparam + n_in + n_out;
239
202
  }
240
11.9k
241
11.9k
  return type;
242
11.9k
}
243
244
/* Can the div expression of the integer division at position "row" of "div"
245
 * be printed?
246
 * In particular, are the div expressions available and does the selected
247
 * variable have a known explicit representation?
248
 * Furthermore, the Omega format does not allow any div expressions
249
 * to be printed.
250
 */
251
static isl_bool can_print_div_expr(__isl_keep isl_printer *p,
252
  __isl_keep isl_mat *div, int pos)
253
618
{
254
618
  if (p->output_format == ISL_FORMAT_OMEGA)
255
618
    
return isl_bool_false0
;
256
618
  if (!div)
257
0
    return isl_bool_false;
258
618
  return !isl_int_is_zero(div->row[pos][0]);
259
618
}
260
261
static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
262
  __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
263
264
static __isl_give isl_printer *print_term(__isl_keep isl_space *space,
265
  __isl_keep isl_mat *div,
266
  isl_int c, unsigned pos, __isl_take isl_printer *p, int latex)
267
18.1k
{
268
18.1k
  enum isl_dim_type type;
269
18.1k
  int print_div_def;
270
18.1k
271
18.1k
  if (pos == 0)
272
6.56k
    return isl_printer_print_isl_int(p, c);
273
11.5k
274
11.5k
  type = pos2type(space, &pos);
275
11.5k
  print_div_def = type == isl_dim_div && 
can_print_div_expr(p, div, pos)125
;
276
11.5k
277
11.5k
  if (isl_int_is_one(c))
278
11.5k
    
;10.4k
279
1.17k
  else if (isl_int_is_negone(c))
280
1.17k
    
p = isl_printer_print_str(p, "-")231
;
281
941
  else {
282
941
    p = isl_printer_print_isl_int(p, c);
283
941
    if (p->output_format == ISL_FORMAT_C || print_div_def)
284
117
      p = isl_printer_print_str(p, "*");
285
941
  }
286
11.5k
  if (print_div_def)
287
117
    p = print_div(space, div, pos, p);
288
11.4k
  else
289
11.4k
    p = print_name(space, p, type, pos, latex);
290
11.5k
  return p;
291
11.5k
}
292
293
static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_space *dim,
294
  __isl_keep isl_mat *div,
295
  __isl_take isl_printer *p, isl_int *c, int len)
296
14.8k
{
297
14.8k
  int i;
298
14.8k
  int first;
299
14.8k
300
115k
  for (i = 0, first = 1; i < len; 
++i101k
) {
301
101k
    int flip = 0;
302
101k
    if (isl_int_is_zero(c[i]))
303
101k
      
continue88.4k
;
304
12.5k
    if (!first) {
305
1.91k
      if (isl_int_is_neg(c[i])) {
306
474
        flip = 1;
307
474
        isl_int_neg(c[i], c[i]);
308
474
        p = isl_printer_print_str(p, " - ");
309
474
      } else 
310
1.43k
        p = isl_printer_print_str(p, " + ");
311
1.91k
    }
312
12.5k
    first = 0;
313
12.5k
    p = print_term(dim, div, c[i], i, p, 0);
314
12.5k
    if (flip)
315
12.5k
      
isl_int_neg474
(c[i], c[i]);
316
12.5k
  }
317
14.8k
  if (first)
318
4.12k
    p = isl_printer_print_str(p, "0");
319
14.8k
  return p;
320
14.8k
}
321
322
/* Print an affine expression "c"
323
 * to "p", with the variable names taken from "space" and
324
 * the integer division definitions taken from "div".
325
 */
326
static __isl_give isl_printer *print_affine(__isl_take isl_printer *p,
327
  __isl_keep isl_space *space, __isl_keep isl_mat *div, isl_int *c)
328
7.99k
{
329
7.99k
  unsigned n_div;
330
7.99k
  unsigned len;
331
7.99k
332
7.99k
  if (!space || !div)
333
0
    return isl_printer_free(p);
334
7.99k
  n_div = isl_mat_rows(div);
335
7.99k
  len = 1 + isl_space_dim(space, isl_dim_all) + n_div;
336
7.99k
  return print_affine_of_len(space, div, p, c, len);
337
7.99k
}
338
339
/* offset is the offset of local_dim inside data->type of data->space.
340
 */
341
static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
342
  __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
343
  struct isl_print_space_data *data, int offset)
344
15.2k
{
345
15.2k
  int i;
346
15.2k
347
15.2k
  if (data->space != local_dim && 
local_type == isl_dim_out114
)
348
57
    offset += local_dim->n_in;
349
15.2k
350
44.5k
  for (i = 0; i < isl_space_dim(local_dim, local_type); 
++i29.2k
) {
351
29.2k
    if (i)
352
15.7k
      p = isl_printer_print_str(p, ", ");
353
29.2k
    if (data->print_dim)
354
13.0k
      p = data->print_dim(p, data, offset + i);
355
16.2k
    else
356
16.2k
      p = print_name(data->space, p, data->type, offset + i,
357
16.2k
          data->latex);
358
29.2k
  }
359
15.2k
  return p;
360
15.2k
}
361
362
static __isl_give isl_printer *print_var_list(__isl_take isl_printer *p,
363
  __isl_keep isl_space *space, enum isl_dim_type type)
364
0
{
365
0
  struct isl_print_space_data data = { .space = space, .type = type };
366
0
367
0
  return print_nested_var_list(p, space, type, &data, 0);
368
0
}
369
370
static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
371
  __isl_keep isl_space *local_dim,
372
  struct isl_print_space_data *data, int offset);
373
374
static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
375
  __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
376
  struct isl_print_space_data *data, int offset)
377
15.3k
{
378
15.3k
  const char *name = NULL;
379
15.3k
  unsigned n = isl_space_dim(local_dim, local_type);
380
15.3k
  if ((local_type == isl_dim_in || 
local_type == isl_dim_out10.9k
)) {
381
10.5k
    name = isl_space_get_tuple_name(local_dim, local_type);
382
10.5k
    if (name) {
383
9.12k
      if (data->latex)
384
0
        p = isl_printer_print_str(p, "\\mathrm{");
385
9.12k
      p = isl_printer_print_str(p, name);
386
9.12k
      if (data->latex)
387
0
        p = isl_printer_print_str(p, "}");
388
9.12k
    }
389
10.5k
  }
390
15.3k
  if (!data->latex || 
n != 10
||
name0
)
391
15.3k
    p = isl_printer_print_str(p, s_open_list[data->latex]);
392
15.3k
  if ((local_type == isl_dim_in || 
local_type == isl_dim_out10.9k
) &&
393
15.3k
      
local_dim->nested[local_type - isl_dim_in]10.5k
) {
394
57
    if (data->space != local_dim && 
local_type == isl_dim_out0
)
395
0
      offset += local_dim->n_in;
396
57
    p = print_nested_map_dim(p,
397
57
        local_dim->nested[local_type - isl_dim_in],
398
57
        data, offset);
399
57
  } else
400
15.2k
    p = print_nested_var_list(p, local_dim, local_type, data,
401
15.2k
            offset);
402
15.3k
  if (!data->latex || 
n != 10
||
name0
)
403
15.3k
    p = isl_printer_print_str(p, s_close_list[data->latex]);
404
15.3k
  return p;
405
15.3k
}
406
407
static __isl_give isl_printer *print_tuple(__isl_keep isl_space *dim,
408
  __isl_take isl_printer *p, enum isl_dim_type type,
409
  struct isl_print_space_data *data)
410
15.1k
{
411
15.1k
  data->space = dim;
412
15.1k
  data->type = type;
413
15.1k
  return print_nested_tuple(p, dim, type, data, 0);
414
15.1k
}
415
416
static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
417
  __isl_keep isl_space *local_dim,
418
  struct isl_print_space_data *data, int offset)
419
57
{
420
57
  p = print_nested_tuple(p, local_dim, isl_dim_in, data, offset);
421
57
  p = isl_printer_print_str(p, s_to[data->latex]);
422
57
  p = print_nested_tuple(p, local_dim, isl_dim_out, data, offset);
423
57
424
57
  return p;
425
57
}
426
427
__isl_give isl_printer *isl_print_space(__isl_keep isl_space *space,
428
  __isl_take isl_printer *p, int rational,
429
  struct isl_print_space_data *data)
430
8.12k
{
431
8.12k
  if (rational && 
!data->latex0
)
432
0
    p = isl_printer_print_str(p, "rat: ");
433
8.12k
  if (isl_space_is_params(space))
434
2.30k
    ;
435
5.81k
  else if (isl_space_is_set(space))
436
1.48k
    p = print_tuple(space, p, isl_dim_set, data);
437
4.33k
  else {
438
4.33k
    p = print_tuple(space, p, isl_dim_in, data);
439
4.33k
    p = isl_printer_print_str(p, s_to[data->latex]);
440
4.33k
    p = print_tuple(space, p, isl_dim_out, data);
441
4.33k
  }
442
8.12k
443
8.12k
  return p;
444
8.12k
}
445
446
static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_space *dim,
447
  __isl_take isl_printer *p)
448
0
{
449
0
  if (isl_space_dim(dim, isl_dim_param) == 0)
450
0
    return p;
451
0
452
0
  p = isl_printer_start_line(p);
453
0
  p = isl_printer_print_str(p, "symbolic ");
454
0
  p = print_var_list(p, dim, isl_dim_param);
455
0
  p = isl_printer_print_str(p, ";");
456
0
  p = isl_printer_end_line(p);
457
0
  return p;
458
0
}
459
460
/* Does the inequality constraint following "i" in "bmap"
461
 * have an opposite value for the same last coefficient?
462
 * "last" is the position of the last coefficient of inequality "i".
463
 * If the next constraint is a div constraint, then it is ignored
464
 * since div constraints are not printed.
465
 */
466
static int next_is_opposite(__isl_keep isl_basic_map *bmap, int i, int last)
467
7.67k
{
468
7.67k
  unsigned total = isl_basic_map_total_dim(bmap);
469
7.67k
  unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
470
7.67k
471
7.67k
  if (i + 1 >= bmap->n_ineq)
472
3.45k
    return 0;
473
4.22k
  if (isl_seq_last_non_zero(bmap->ineq[i + 1], 1 + total) != last)
474
1.56k
    return 0;
475
2.65k
  if (last >= o_div) {
476
79
    isl_bool is_div;
477
79
    is_div = isl_basic_map_is_div_constraint(bmap,
478
79
              bmap->ineq[i + 1], last - o_div);
479
79
    if (is_div < 0)
480
0
      return -1;
481
79
    if (is_div)
482
12
      return 0;
483
2.64k
  }
484
2.64k
  return isl_int_abs_eq(bmap->ineq[i][last], bmap->ineq[i + 1][last]) &&
485
2.64k
    
!2.60k
isl_int_eq2.60k
(bmap->ineq[i][last], bmap->ineq[i + 1][last]);
486
2.64k
}
487
488
/* Return a string representation of the operator used when
489
 * printing a constraint where the LHS is greater than or equal to the LHS
490
 * (sign > 0) or smaller than or equal to the LHS (sign < 0).
491
 * If "strict" is set, then return the strict version of the comparison
492
 * operator.
493
 */
494
static const char *constraint_op(int sign, int strict, int latex)
495
7.67k
{
496
7.67k
  if (strict)
497
1.24k
    return sign < 0 ? 
"<"812
:
">"429
;
498
6.43k
  if (sign < 0)
499
5.11k
    return s_le[latex];
500
1.31k
  else
501
1.31k
    return s_ge[latex];
502
6.43k
}
503
504
/* Print one side of a constraint "c" to "p", with
505
 * the variable names taken from "space" and the integer division definitions
506
 * taken from "div".
507
 * "last" is the position of the last non-zero coefficient.
508
 * Let c' be the result of zeroing out this coefficient, then
509
 * the partial constraint
510
 *
511
 *  c' op
512
 *
513
 * is printed.
514
 */
515
static __isl_give isl_printer *print_half_constraint(__isl_take isl_printer *p,
516
  __isl_keep isl_space *space, __isl_keep isl_mat *div,
517
  isl_int *c, int last, const char *op, int latex)
518
2.36k
{
519
2.36k
  isl_int_set_si(c[last], 0);
520
2.36k
  p = print_affine(p, space, div, c);
521
2.36k
522
2.36k
  p = isl_printer_print_str(p, " ");
523
2.36k
  p = isl_printer_print_str(p, op);
524
2.36k
  p = isl_printer_print_str(p, " ");
525
2.36k
526
2.36k
  return p;
527
2.36k
}
528
529
/* Print a constraint "c" to "p", with the variable names
530
 * taken from "space" and the integer division definitions taken from "div".
531
 * "last" is the position of the last non-zero coefficient, which is
532
 * moreover assumed to be negative.
533
 * Let c' be the result of zeroing out this coefficient, then
534
 * the constraint is printed in the form
535
 *
536
 *  -c[last] op c'
537
 */
538
static __isl_give isl_printer *print_constraint(__isl_take isl_printer *p,
539
  __isl_keep isl_space *space, __isl_keep isl_mat *div,
540
  isl_int *c, int last, const char *op, int latex)
541
5.55k
{
542
5.55k
  isl_int_abs(c[last], c[last]);
543
5.55k
544
5.55k
  p = print_term(space, div, c[last], last, p, latex);
545
5.55k
546
5.55k
  p = isl_printer_print_str(p, " ");
547
5.55k
  p = isl_printer_print_str(p, op);
548
5.55k
  p = isl_printer_print_str(p, " ");
549
5.55k
550
5.55k
  isl_int_set_si(c[last], 0);
551
5.55k
  p = print_affine(p, space, div, c);
552
5.55k
553
5.55k
  return p;
554
5.55k
}
555
556
/* Given an integer division
557
 *
558
 *  floor(f/m)
559
 *
560
 * at position "pos" in "div", print the corresponding modulo expression
561
 *
562
 *  (f) mod m
563
 *
564
 * to "p".  The variable names are taken from "space", while any
565
 * nested integer division definitions are taken from "div".
566
 */
567
static __isl_give isl_printer *print_mod(__isl_take isl_printer *p,
568
  __isl_keep isl_space *space, __isl_keep isl_mat *div, int pos,
569
  int latex)
570
75
{
571
75
  if (!p || !div)
572
0
    return isl_printer_free(p);
573
75
574
75
  p = isl_printer_print_str(p, "(");
575
75
  p = print_affine_of_len(space, div, p,
576
75
        div->row[pos] + 1, div->n_col - 1);
577
75
  p = isl_printer_print_str(p, ") ");
578
75
  p = isl_printer_print_str(p, s_mod[latex]);
579
75
  p = isl_printer_print_str(p, " ");
580
75
  p = isl_printer_print_isl_int(p, div->row[pos][0]);
581
75
  return p;
582
75
}
583
584
/* Can the equality constraints "c" be printed as a modulo constraint?
585
 * In particular, is of the form
586
 *
587
 *  f - a m floor(g/m) = 0,
588
 *
589
 * with c = -a m the coefficient at position "pos"?
590
 * Return the position of the corresponding integer division if so.
591
 * Return the number of integer divisions if not.
592
 * Return -1 on error.
593
 *
594
 * Modulo constraints are currently not printed in C format.
595
 * Other than that, "pos" needs to correspond to an integer division
596
 * with explicit representation and "c" needs to be a multiple
597
 * of the denominator of the integer division.
598
 */
599
static int print_as_modulo_pos(__isl_keep isl_printer *p,
600
  __isl_keep isl_space *space, __isl_keep isl_mat *div, unsigned pos,
601
  isl_int c)
602
317
{
603
317
  isl_bool can_print;
604
317
  unsigned n_div;
605
317
  enum isl_dim_type type;
606
317
607
317
  if (!p)
608
0
    return -1;
609
317
  n_div = isl_mat_rows(div);
610
317
  if (p->output_format == ISL_FORMAT_C)
611
317
    
return n_div0
;
612
317
  type = pos2type(space, &pos);
613
317
  if (type != isl_dim_div)
614
240
    return n_div;
615
77
  can_print = can_print_div_expr(p, div, pos);
616
77
  if (can_print < 0)
617
0
    return -1;
618
77
  if (!can_print)
619
0
    return n_div;
620
77
  if (!isl_int_is_divisible_by(c, div->row[pos][0]))
621
77
    
return n_div2
;
622
75
  return pos;
623
75
}
624
625
/* Print equality constraint "c" to "p" as a modulo constraint,
626
 * with the variable names taken from "space" and
627
 * the integer division definitions taken from "div".
628
 * "last" is the position of the last non-zero coefficient, which is
629
 * moreover assumed to be negative and a multiple of the denominator
630
 * of the corresponding integer division.  "div_pos" is the corresponding
631
 * position in the sequence of integer divisions.
632
 *
633
 * The equality is of the form
634
 *
635
 *  f - a m floor(g/m) = 0.
636
 *
637
 * Print it as
638
 *
639
 *  a (g mod m) = -f + a g
640
 */
641
static __isl_give isl_printer *print_eq_mod_constraint(
642
  __isl_take isl_printer *p, __isl_keep isl_space *space,
643
  __isl_keep isl_mat *div, unsigned div_pos,
644
  isl_int *c, int last, int latex)
645
75
{
646
75
  isl_ctx *ctx;
647
75
  int multiple;
648
75
649
75
  ctx = isl_printer_get_ctx(p);
650
75
  isl_int_divexact(c[last], c[last], div->row[div_pos][0]);
651
75
  isl_int_abs(c[last], c[last]);
652
75
  multiple = !isl_int_is_one(c[last]);
653
75
  if (multiple) {
654
0
    p = isl_printer_print_isl_int(p, c[last]);
655
0
    p = isl_printer_print_str(p, "*(");
656
0
  }
657
75
  p = print_mod(p, space, div, div_pos, latex);
658
75
  if (multiple)
659
0
    p = isl_printer_print_str(p, ")");
660
75
  p = isl_printer_print_str(p, " = ");
661
75
  isl_seq_combine(c, ctx->negone, c,
662
75
          c[last], div->row[div_pos] + 1, last);
663
75
  isl_int_set_si(c[last], 0);
664
75
  p = print_affine(p, space, div, c);
665
75
  return p;
666
75
}
667
668
/* Print equality constraint "c" to "p", with the variable names
669
 * taken from "space" and the integer division definitions taken from "div".
670
 * "last" is the position of the last non-zero coefficient, which is
671
 * moreover assumed to be negative.
672
 *
673
 * If possible, print the equality constraint as a modulo constraint.
674
 */
675
static __isl_give isl_printer *print_eq_constraint(__isl_take isl_printer *p,
676
  __isl_keep isl_space *space, __isl_keep isl_mat *div, isl_int *c,
677
  int last, int latex)
678
317
{
679
317
  unsigned n_div;
680
317
  int div_pos;
681
317
682
317
  n_div = isl_mat_rows(div);
683
317
  div_pos = print_as_modulo_pos(p, space, div, last, c[last]);
684
317
  if (div_pos < 0)
685
0
    return isl_printer_free(p);
686
317
  if (div_pos < n_div)
687
75
    return print_eq_mod_constraint(p, space, div, div_pos,
688
75
            c, last, latex);
689
242
  return print_constraint(p, space, div, c, last, "=", latex);
690
242
}
691
692
/* Print the constraints of "bmap" to "p".
693
 * The names of the variables are taken from "space" and
694
 * the integer division definitions are taken from "div".
695
 * Div constraints are only printed in "dump" mode.
696
 * The constraints are sorted prior to printing (except in "dump" mode).
697
 *
698
 * If x is the last variable with a non-zero coefficient,
699
 * then a lower bound
700
 *
701
 *  f - a x >= 0
702
 *
703
 * is printed as
704
 *
705
 *  a x <= f
706
 *
707
 * while an upper bound
708
 *
709
 *  f + a x >= 0
710
 *
711
 * is printed as
712
 *
713
 *  a x >= -f
714
 *
715
 * If the next constraint has an opposite sign for the same last coefficient,
716
 * then it is printed as
717
 *
718
 *  f >= a x
719
 *
720
 * or
721
 *
722
 *  -f <= a x
723
 *
724
 * instead.  In fact, the "a x" part is not printed explicitly, but
725
 * reused from the next constraint, which is therefore treated as
726
 * a first constraint in the conjunction.
727
 *
728
 * If the constant term of "f" is -1, then "f" is replaced by "f + 1" and
729
 * the comparison operator is replaced by the strict variant.
730
 * Essentially, ">= 1" is replaced by "> 0".
731
 */
732
static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
733
  __isl_keep isl_space *space, __isl_keep isl_mat *div,
734
  __isl_take isl_printer *p, int latex)
735
3.65k
{
736
3.65k
  int i;
737
3.65k
  isl_vec *c = NULL;
738
3.65k
  int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
739
3.65k
  unsigned total = isl_basic_map_total_dim(bmap);
740
3.65k
  unsigned o_div = isl_basic_map_offset(bmap, isl_dim_div);
741
3.65k
  int first = 1;
742
3.65k
  int dump;
743
3.65k
744
3.65k
  if (!p)
745
0
    return NULL;
746
3.65k
  bmap = isl_basic_map_copy(bmap);
747
3.65k
  dump = p->dump;
748
3.65k
  if (!dump)
749
3.65k
    bmap = isl_basic_map_sort_constraints(bmap);
750
3.65k
  if (!bmap)
751
0
    goto error;
752
3.65k
753
3.65k
  c = isl_vec_alloc(bmap->ctx, 1 + total);
754
3.65k
  if (!c)
755
0
    goto error;
756
3.65k
757
3.96k
  
for (i = bmap->n_eq - 1; 3.65k
i >= 0;
--i317
) {
758
317
    int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
759
317
    if (l < 0) {
760
0
      if (i != bmap->n_eq - 1)
761
0
        p = isl_printer_print_str(p, s_and[latex]);
762
0
      p = isl_printer_print_str(p, "0 = 0");
763
0
      continue;
764
0
    }
765
317
    if (!first)
766
13
      p = isl_printer_print_str(p, s_and[latex]);
767
317
    if (isl_int_is_neg(bmap->eq[i][l]))
768
317
      
isl_seq_cpy(c->el, bmap->eq[i], 1 + total)0
;
769
317
    else
770
317
      isl_seq_neg(c->el, bmap->eq[i], 1 + total);
771
317
    p = print_eq_constraint(p, space, div, c->el, l, latex);
772
317
    first = 0;
773
317
  }
774
11.3k
  for (i = 0; i < bmap->n_ineq; 
++i7.74k
) {
775
7.74k
    int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
776
7.74k
    int strict;
777
7.74k
    int s;
778
7.74k
    const char *op;
779
7.74k
    if (l < 0)
780
0
      continue;
781
7.74k
    if (!dump && l >= o_div &&
782
7.74k
        
can_print_div_expr(p, div, l - o_div)231
) {
783
223
      isl_bool is_div;
784
223
      is_div = isl_basic_map_is_div_constraint(bmap,
785
223
                bmap->ineq[i], l - o_div);
786
223
      if (is_div < 0)
787
0
        goto error;
788
223
      if (is_div)
789
67
        continue;
790
7.67k
    }
791
7.67k
    if (!first)
792
1.96k
      p = isl_printer_print_str(p, s_and[latex]);
793
7.67k
    s = isl_int_sgn(bmap->ineq[i][l]);
794
7.67k
    strict = !rational && isl_int_is_negone(bmap->ineq[i][0]);
795
7.67k
    if (s < 0)
796
3.57k
      isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
797
4.10k
    else
798
4.10k
      isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
799
7.67k
    if (strict)
800
7.67k
      
isl_int_set_si1.24k
(c->el[0], 0);
801
7.67k
    if (!dump && next_is_opposite(bmap, i, l)) {
802
2.36k
      op = constraint_op(-s, strict, latex);
803
2.36k
      p = print_half_constraint(p, space, div, c->el, l,
804
2.36k
            op, latex);
805
2.36k
      first = 1;
806
5.31k
    } else {
807
5.31k
      op = constraint_op(s, strict, latex);
808
5.31k
      p = print_constraint(p, space, div, c->el, l,
809
5.31k
            op, latex);
810
5.31k
      first = 0;
811
5.31k
    }
812
7.67k
  }
813
3.65k
814
3.65k
  isl_basic_map_free(bmap);
815
3.65k
  isl_vec_free(c);
816
3.65k
817
3.65k
  return p;
818
0
error:
819
0
  isl_basic_map_free(bmap);
820
0
  isl_vec_free(c);
821
0
  isl_printer_free(p);
822
0
  return NULL;
823
3.65k
}
824
825
static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
826
  __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
827
117
{
828
117
  int c;
829
117
830
117
  if (!p || !div)
831
0
    return isl_printer_free(p);
832
117
833
117
  c = p->output_format == ISL_FORMAT_C;
834
117
  p = isl_printer_print_str(p, c ? 
"floord("0
: "floor((");
835
117
  p = print_affine_of_len(dim, div, p,
836
117
        div->row[pos] + 1, div->n_col - 1);
837
117
  p = isl_printer_print_str(p, c ? 
", "0
: ")/");
838
117
  p = isl_printer_print_isl_int(p, div->row[pos][0]);
839
117
  p = isl_printer_print_str(p, ")");
840
117
  return p;
841
117
}
842
843
/* Print a comma separated list of div names, except those that have
844
 * a definition that can be printed.
845
 * If "print_defined_divs" is set, then those div names are printed
846
 * as well, along with their definitions.
847
 */
848
static __isl_give isl_printer *print_div_list(__isl_take isl_printer *p,
849
  __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex,
850
  int print_defined_divs)
851
2
{
852
2
  int i;
853
2
  int first = 1;
854
2
  unsigned n_div;
855
2
856
2
  if (!p || !space || !div)
857
0
    return isl_printer_free(p);
858
2
859
2
  n_div = isl_mat_rows(div);
860
2
861
6
  for (i = 0; i < n_div; 
++i4
) {
862
4
    if (!print_defined_divs && can_print_div_expr(p, div, i))
863
2
      continue;
864
2
    if (!first)
865
0
      p = isl_printer_print_str(p, ", ");
866
2
    p = print_name(space, p, isl_dim_div, i, latex);
867
2
    first = 0;
868
2
    if (!can_print_div_expr(p, div, i))
869
2
      continue;
870
0
    p = isl_printer_print_str(p, " = ");
871
0
    p = print_div(space, div, i, p);
872
0
  }
873
2
874
2
  return p;
875
2
}
876
877
/* Does printing an object with local variables described by "div"
878
 * require an "exists" clause?
879
 * That is, are there any local variables without an explicit representation?
880
 * An exists clause is also needed in "dump" mode because
881
 * explicit div representations are not printed inline in that case.
882
 */
883
static isl_bool need_exists(__isl_keep isl_printer *p, __isl_keep isl_mat *div)
884
3.65k
{
885
3.65k
  int i, n;
886
3.65k
887
3.65k
  if (!p || !div)
888
0
    return isl_bool_error;
889
3.65k
  n = isl_mat_rows(div);
890
3.65k
  if (n == 0)
891
3.49k
    return isl_bool_false;
892
161
  if (p->dump)
893
0
    return isl_bool_true;
894
338
  
for (i = 0; 161
i < n;
++i177
)
895
179
    if (!can_print_div_expr(p, div, i))
896
2
      return isl_bool_true;
897
161
  
return isl_bool_false159
;
898
161
}
899
900
/* Print the start of an exists clause, i.e.,
901
 *
902
 *  (exists variables:
903
 *
904
 * In dump mode, local variables with an explicit definition are printed
905
 * as well because they will not be printed inline.
906
 */
907
static __isl_give isl_printer *open_exists(__isl_take isl_printer *p,
908
  __isl_keep isl_space *space, __isl_keep isl_mat *div, int latex)
909
2
{
910
2
  int dump;
911
2
912
2
  if (!p)
913
0
    return NULL;
914
2
915
2
  dump = p->dump;
916
2
  p = isl_printer_print_str(p, s_open_exists[latex]);
917
2
  p = print_div_list(p, space, div, latex, dump);
918
2
  p = isl_printer_print_str(p, ": ");
919
2
920
2
  return p;
921
2
}
922
923
/* Remove the explicit representations of all local variables in "div".
924
 */
925
static __isl_give isl_mat *mark_all_unknown(__isl_take isl_mat *div)
926
0
{
927
0
  int i, n_div;
928
0
929
0
  if (!div)
930
0
    return NULL;
931
0
932
0
  n_div = isl_mat_rows(div);
933
0
  for (i = 0; i < n_div; ++i)
934
0
    div = isl_mat_set_element_si(div, i, 0, 0);
935
0
  return div;
936
0
}
937
938
/* Print the constraints of "bmap" to "p".
939
 * The names of the variables are taken from "space".
940
 * "latex" is set if the constraints should be printed in LaTeX format.
941
 * Do not print inline explicit div representations in "dump" mode.
942
 */
943
static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
944
  __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
945
3.65k
{
946
3.65k
  int dump;
947
3.65k
  isl_mat *div;
948
3.65k
  isl_bool exists;
949
3.65k
950
3.65k
  if (!p)
951
0
    return NULL;
952
3.65k
  dump = p->dump;
953
3.65k
  div = isl_basic_map_get_divs(bmap);
954
3.65k
  exists = need_exists(p, div);
955
3.65k
  if (exists >= 0 && exists)
956
2
    p = open_exists(p, space, div, latex);
957
3.65k
958
3.65k
  if (dump)
959
0
    div = mark_all_unknown(div);
960
3.65k
  p = print_constraints(bmap, space, div, p, latex);
961
3.65k
  isl_mat_free(div);
962
3.65k
963
3.65k
  if (exists >= 0 && exists)
964
2
    p = isl_printer_print_str(p, s_close_exists[latex]);
965
3.65k
  return p;
966
3.65k
}
967
968
/* Print a colon followed by the constraints of "bmap"
969
 * to "p", provided there are any constraints.
970
 * The names of the variables are taken from "space".
971
 * "latex" is set if the constraints should be printed in LaTeX format.
972
 */
973
static __isl_give isl_printer *print_optional_disjunct(
974
  __isl_keep isl_basic_map *bmap, __isl_keep isl_space *space,
975
  __isl_take isl_printer *p, int latex)
976
0
{
977
0
  if (isl_basic_map_plain_is_universe(bmap))
978
0
    return p;
979
0
980
0
  p = isl_printer_print_str(p, ": ");
981
0
  p = print_disjunct(bmap, space, p, latex);
982
0
983
0
  return p;
984
0
}
985
986
static __isl_give isl_printer *basic_map_print_omega(
987
  __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
988
0
{
989
0
  p = isl_printer_print_str(p, "{ [");
990
0
  p = print_var_list(p, bmap->dim, isl_dim_in);
991
0
  p = isl_printer_print_str(p, "] -> [");
992
0
  p = print_var_list(p, bmap->dim, isl_dim_out);
993
0
  p = isl_printer_print_str(p, "] ");
994
0
  p = print_optional_disjunct(bmap, bmap->dim, p, 0);
995
0
  p = isl_printer_print_str(p, " }");
996
0
  return p;
997
0
}
998
999
static __isl_give isl_printer *basic_set_print_omega(
1000
  __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
1001
0
{
1002
0
  p = isl_printer_print_str(p, "{ [");
1003
0
  p = print_var_list(p, bset->dim, isl_dim_set);
1004
0
  p = isl_printer_print_str(p, "] ");
1005
0
  p = print_optional_disjunct(bset, bset->dim, p, 0);
1006
0
  p = isl_printer_print_str(p, " }");
1007
0
  return p;
1008
0
}
1009
1010
static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
1011
  __isl_take isl_printer *p)
1012
0
{
1013
0
  int i;
1014
0
1015
0
  for (i = 0; i < map->n; ++i) {
1016
0
    if (i)
1017
0
      p = isl_printer_print_str(p, " union ");
1018
0
    p = basic_map_print_omega(map->p[i], p);
1019
0
  }
1020
0
  return p;
1021
0
}
1022
1023
static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
1024
  __isl_take isl_printer *p)
1025
0
{
1026
0
  int i;
1027
0
1028
0
  for (i = 0; i < set->n; ++i) {
1029
0
    if (i)
1030
0
      p = isl_printer_print_str(p, " union ");
1031
0
    p = basic_set_print_omega(set->p[i], p);
1032
0
  }
1033
0
  return p;
1034
0
}
1035
1036
/* Print the list of parameters in "space", followed by an arrow, to "p",
1037
 * if there are any parameters.
1038
 */
1039
static __isl_give isl_printer *print_param_tuple(__isl_take isl_printer *p,
1040
  __isl_keep isl_space *space, struct isl_print_space_data *data)
1041
8.07k
{
1042
8.07k
  if (!p || !space)
1043
0
    return isl_printer_free(p);
1044
8.07k
  if (isl_space_dim(space, isl_dim_param) == 0)
1045
3.35k
    return p;
1046
4.71k
1047
4.71k
  p = print_tuple(space, p, isl_dim_param, data);
1048
4.71k
  p = isl_printer_print_str(p, s_to[data->latex]);
1049
4.71k
1050
4.71k
  return p;
1051
4.71k
}
1052
1053
static __isl_give isl_printer *isl_basic_map_print_isl(
1054
  __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
1055
  int latex)
1056
0
{
1057
0
  struct isl_print_space_data data = { .latex = latex };
1058
0
  int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1059
0
1060
0
  p = print_param_tuple(p, bmap->dim, &data);
1061
0
  p = isl_printer_print_str(p, "{ ");
1062
0
  p = isl_print_space(bmap->dim, p, rational, &data);
1063
0
  p = isl_printer_print_str(p, " : ");
1064
0
  p = print_disjunct(bmap, bmap->dim, p, latex);
1065
0
  p = isl_printer_print_str(p, " }");
1066
0
  return p;
1067
0
}
1068
1069
/* Print the disjuncts of a map (or set) "map" to "p".
1070
 * The names of the variables are taken from "space".
1071
 * "latex" is set if the constraints should be printed in LaTeX format.
1072
 */
1073
static __isl_give isl_printer *print_disjuncts_core(__isl_keep isl_map *map,
1074
  __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1075
3.28k
{
1076
3.28k
  int i;
1077
3.28k
1078
3.28k
  if (map->n == 0)
1079
366
    p = isl_printer_print_str(p, "false");
1080
6.78k
  for (i = 0; i < map->n; 
++i3.49k
) {
1081
3.49k
    if (i)
1082
579
      p = isl_printer_print_str(p, s_or[latex]);
1083
3.49k
    if (map->n > 1 && 
map->p[i]->n_eq + map->p[i]->n_ineq > 11.01k
)
1084
286
      p = isl_printer_print_str(p, "(");
1085
3.49k
    p = print_disjunct(map->p[i], space, p, latex);
1086
3.49k
    if (map->n > 1 && 
map->p[i]->n_eq + map->p[i]->n_ineq > 11.01k
)
1087
286
      p = isl_printer_print_str(p, ")");
1088
3.49k
  }
1089
3.28k
  return p;
1090
3.28k
}
1091
1092
/* Print the disjuncts of a map (or set) "map" to "p".
1093
 * The names of the variables are taken from "space".
1094
 * "hull" describes constraints shared by all disjuncts of "map".
1095
 * "latex" is set if the constraints should be printed in LaTeX format.
1096
 *
1097
 * Print the disjuncts as a conjunction of "hull" and
1098
 * the result of removing the constraints of "hull" from "map".
1099
 * If this result turns out to be the universe, then simply print "hull".
1100
 */
1101
static __isl_give isl_printer *print_disjuncts_in_hull(__isl_keep isl_map *map,
1102
  __isl_keep isl_space *space, __isl_take isl_basic_map *hull,
1103
  __isl_take isl_printer *p, int latex)
1104
154
{
1105
154
  isl_bool is_universe;
1106
154
1107
154
  p = print_disjunct(hull, space, p, latex);
1108
154
  map = isl_map_plain_gist_basic_map(isl_map_copy(map), hull);
1109
154
  is_universe = isl_map_plain_is_universe(map);
1110
154
  if (is_universe < 0)
1111
0
    goto error;
1112
154
  if (!is_universe) {
1113
149
    p = isl_printer_print_str(p, s_and[latex]);
1114
149
    p = isl_printer_print_str(p, "(");
1115
149
    p = print_disjuncts_core(map, space, p, latex);
1116
149
    p = isl_printer_print_str(p, ")");
1117
149
  }
1118
154
  isl_map_free(map);
1119
154
1120
154
  return p;
1121
0
error:
1122
0
  isl_map_free(map);
1123
0
  isl_printer_free(p);
1124
0
  return NULL;
1125
154
}
1126
1127
/* Print the disjuncts of a map (or set) "map" to "p".
1128
 * The names of the variables are taken from "space".
1129
 * "latex" is set if the constraints should be printed in LaTeX format.
1130
 *
1131
 * If there are at least two disjuncts and "dump" mode is not turned out,
1132
 * check for any shared constraints among all disjuncts.
1133
 * If there are any, then print them separately in print_disjuncts_in_hull.
1134
 */
1135
static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
1136
  __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1137
7.68k
{
1138
7.68k
  if (isl_map_plain_is_universe(map))
1139
4.39k
    return p;
1140
3.29k
1141
3.29k
  p = isl_printer_print_str(p, s_such_that[latex]);
1142
3.29k
  if (!p)
1143
0
    return NULL;
1144
3.29k
1145
3.29k
  if (!p->dump && map->n >= 2) {
1146
438
    isl_basic_map *hull;
1147
438
    isl_bool is_universe;
1148
438
1149
438
    hull = isl_map_plain_unshifted_simple_hull(isl_map_copy(map));
1150
438
    is_universe = isl_basic_map_plain_is_universe(hull);
1151
438
    if (is_universe < 0)
1152
0
      p = isl_printer_free(p);
1153
438
    else if (!is_universe)
1154
154
      return print_disjuncts_in_hull(map, space, hull,
1155
154
              p, latex);
1156
284
    isl_basic_map_free(hull);
1157
284
  }
1158
3.29k
1159
3.29k
  
return print_disjuncts_core(map, space, p, latex)3.13k
;
1160
3.29k
}
1161
1162
/* Print the disjuncts of a map (or set).
1163
 * The names of the variables are taken from "space".
1164
 * "latex" is set if the constraints should be printed in LaTeX format.
1165
 *
1166
 * If the map turns out to be a universal parameter domain, then
1167
 * we need to print the colon.  Otherwise, the output looks identical
1168
 * to the empty set.
1169
 */
1170
static __isl_give isl_printer *print_disjuncts_map(__isl_keep isl_map *map,
1171
  __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1172
7.53k
{
1173
7.53k
  if (isl_map_plain_is_universe(map) && 
isl_space_is_params(map->dim)4.43k
)
1174
752
    return isl_printer_print_str(p, s_such_that[latex]);
1175
6.77k
  else
1176
6.77k
    return print_disjuncts(map, space, p, latex);
1177
7.53k
}
1178
1179
/* Print the disjuncts of a set.
1180
 * The names of the variables are taken from "space".
1181
 * "latex" is set if the constraints should be printed in LaTeX format.
1182
 */
1183
static __isl_give isl_printer *print_disjuncts_set(__isl_keep isl_set *set,
1184
  __isl_keep isl_space *space, __isl_take isl_printer *p, int latex)
1185
5
{
1186
5
  return print_disjuncts_map(set_to_map(set), space, p, latex);
1187
5
}
1188
1189
struct isl_aff_split {
1190
  isl_basic_map *aff;
1191
  isl_map *map;
1192
};
1193
1194
static void free_split(__isl_take struct isl_aff_split *split, int n)
1195
7.27k
{
1196
7.27k
  int i;
1197
7.27k
1198
7.27k
  if (!split)
1199
366
    return;
1200
6.90k
1201
14.6k
  
for (i = 0; 6.90k
i < n;
++i7.72k
) {
1202
7.72k
    isl_basic_map_free(split[i].aff);
1203
7.72k
    isl_map_free(split[i].map);
1204
7.72k
  }
1205
6.90k
1206
6.90k
  free(split);
1207
6.90k
}
1208
1209
static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
1210
7.72k
{
1211
7.72k
  int i, j;
1212
7.72k
  unsigned nparam, n_in, n_out, total;
1213
7.72k
1214
7.72k
  bmap = isl_basic_map_cow(bmap);
1215
7.72k
  if (!bmap)
1216
0
    return NULL;
1217
7.72k
  if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
1218
0
    goto error;
1219
7.72k
1220
7.72k
  nparam = isl_basic_map_dim(bmap, isl_dim_param);
1221
7.72k
  n_in = isl_basic_map_dim(bmap, isl_dim_in);
1222
7.72k
  n_out = isl_basic_map_dim(bmap, isl_dim_out);
1223
7.72k
  total = isl_basic_map_dim(bmap, isl_dim_all);
1224
13.7k
  for (i = bmap->n_eq - 1; i >= 0; 
--i5.97k
) {
1225
5.97k
    j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
1226
5.97k
    if (j >= nparam && 
j < nparam + n_in + n_out5.78k
&&
1227
5.97k
        
(5.70k
isl_int_is_one5.70k
(bmap->eq[i][1 + j]) ||
1228
5.70k
         
isl_int_is_negone6
(bmap->eq[i][1 + j])))
1229
5.70k
      continue;
1230
277
    if (isl_basic_map_drop_equality(bmap, i) < 0)
1231
0
      goto error;
1232
277
  }
1233
7.72k
1234
7.72k
  bmap = isl_basic_map_finalize(bmap);
1235
7.72k
1236
7.72k
  return bmap;
1237
0
error:
1238
0
  isl_basic_map_free(bmap);
1239
0
  return NULL;
1240
7.72k
}
1241
1242
static int aff_split_cmp(const void *p1, const void *p2, void *user)
1243
987
{
1244
987
  const struct isl_aff_split *s1, *s2;
1245
987
  s1 = (const struct isl_aff_split *) p1;
1246
987
  s2 = (const struct isl_aff_split *) p2;
1247
987
1248
987
  return isl_basic_map_plain_cmp(s1->aff, s2->aff);
1249
987
}
1250
1251
static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
1252
  __isl_keep isl_basic_map *aff)
1253
7.72k
{
1254
7.72k
  int i, j;
1255
7.72k
  unsigned total;
1256
7.72k
1257
7.72k
  if (!bmap || !aff)
1258
0
    goto error;
1259
7.72k
1260
7.72k
  total = isl_space_dim(bmap->dim, isl_dim_all);
1261
7.72k
1262
13.7k
  for (i = bmap->n_eq - 1; i >= 0; 
--i5.97k
) {
1263
5.97k
    if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
1264
5.97k
              bmap->n_div) != -1)
1265
81
      continue;
1266
9.44k
    
for (j = 0; 5.89k
j < aff->n_eq;
++j3.54k
) {
1267
9.25k
      if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
1268
9.25k
          
!isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total)3.54k
)
1269
3.54k
        continue;
1270
5.70k
      if (isl_basic_map_drop_equality(bmap, i) < 0)
1271
0
        goto error;
1272
5.70k
      break;
1273
5.70k
    }
1274
5.89k
  }
1275
7.72k
1276
7.72k
  return bmap;
1277
0
error:
1278
0
  isl_basic_map_free(bmap);
1279
0
  return NULL;
1280
7.72k
}
1281
1282
static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
1283
6.90k
{
1284
6.90k
  int i, n;
1285
6.90k
  struct isl_aff_split *split;
1286
6.90k
  isl_ctx *ctx;
1287
6.90k
1288
6.90k
  ctx = isl_map_get_ctx(map);
1289
6.90k
  split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
1290
6.90k
  if (!split)
1291
0
    return NULL;
1292
6.90k
1293
14.6k
  
for (i = 0; 6.90k
i < map->n;
++i7.72k
) {
1294
7.72k
    isl_basic_map *bmap;
1295
7.72k
    split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
1296
7.72k
    bmap = isl_basic_map_copy(map->p[i]);
1297
7.72k
    bmap = isl_basic_map_cow(bmap);
1298
7.72k
    bmap = drop_aff(bmap, split[i].aff);
1299
7.72k
    split[i].map = isl_map_from_basic_map(bmap);
1300
7.72k
    if (!split[i].aff || !split[i].map)
1301
0
      goto error;
1302
7.72k
  }
1303
6.90k
1304
6.90k
  if (isl_sort(split, map->n, sizeof(struct isl_aff_split),
1305
6.90k
      &aff_split_cmp, NULL) < 0)
1306
0
    goto error;
1307
6.90k
1308
6.90k
  n = map->n;
1309
7.72k
  for (i = n - 1; i >= 1; 
--i815
) {
1310
815
    if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
1311
815
             split[i].aff))
1312
253
      continue;
1313
562
    isl_basic_map_free(split[i].aff);
1314
562
    split[i - 1].map = isl_map_union(split[i - 1].map,
1315
562
             split[i].map);
1316
562
    if (i != n - 1)
1317
19
      split[i] = split[n - 1];
1318
562
    split[n - 1].aff = NULL;
1319
562
    split[n - 1].map = NULL;
1320
562
    --n;
1321
562
  }
1322
6.90k
1323
6.90k
  return split;
1324
0
error:
1325
0
  free_split(split, map->n);
1326
0
  return NULL;
1327
6.90k
}
1328
1329
static int defining_equality(__isl_keep isl_basic_map *eq,
1330
  __isl_keep isl_space *dim, enum isl_dim_type type, int pos)
1331
12.3k
{
1332
12.3k
  int i;
1333
12.3k
  unsigned total;
1334
12.3k
1335
12.3k
  if (!eq)
1336
0
    return -1;
1337
12.3k
1338
12.3k
  pos += isl_space_offset(dim, type);
1339
12.3k
  total = isl_basic_map_total_dim(eq);
1340
12.3k
1341
23.8k
  for (i = 0; i < eq->n_eq; 
++i11.4k
) {
1342
17.0k
    if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
1343
11.4k
      continue;
1344
5.60k
    if (isl_int_is_one(eq->eq[i][1 + pos]))
1345
5.60k
      isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
1346
5.60k
    return i;
1347
5.60k
  }
1348
12.3k
1349
12.3k
  
return -16.71k
;
1350
12.3k
}
1351
1352
/* Print dimension "pos" of data->space to "p".
1353
 *
1354
 * data->user is assumed to be an isl_basic_map keeping track of equalities.
1355
 *
1356
 * If the current dimension is defined by these equalities, then print
1357
 * the corresponding expression, assigned to the name of the dimension
1358
 * if there is any.  Otherwise, print the name of the dimension.
1359
 */
1360
static __isl_give isl_printer *print_dim_eq(__isl_take isl_printer *p,
1361
  struct isl_print_space_data *data, unsigned pos)
1362
12.3k
{
1363
12.3k
  isl_basic_map *eq = data->user;
1364
12.3k
  int j;
1365
12.3k
1366
12.3k
  j = defining_equality(eq, data->space, data->type, pos);
1367
12.3k
  if (j >= 0) {
1368
5.60k
    if (isl_space_has_dim_name(data->space, data->type, pos)) {
1369
1
      p = print_name(data->space, p, data->type, pos,
1370
1
          data->latex);
1371
1
      p = isl_printer_print_str(p, " = ");
1372
1
    }
1373
5.60k
    pos += 1 + isl_space_offset(data->space, data->type);
1374
5.60k
    p = print_affine_of_len(data->space, NULL, p, eq->eq[j], pos);
1375
6.71k
  } else {
1376
6.71k
    p = print_name(data->space, p, data->type, pos, data->latex);
1377
6.71k
  }
1378
12.3k
1379
12.3k
  return p;
1380
12.3k
}
1381
1382
static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
1383
  struct isl_aff_split *split, int n, __isl_keep isl_space *space)
1384
6.90k
{
1385
6.90k
  struct isl_print_space_data data = { 0 };
1386
6.90k
  int i;
1387
6.90k
  int rational;
1388
6.90k
1389
6.90k
  data.print_dim = &print_dim_eq;
1390
14.0k
  for (i = 0; i < n; 
++i7.16k
) {
1391
7.57k
    if (!split[i].map)
1392
412
      break;
1393
7.16k
    rational = split[i].map->n > 0 &&
1394
7.16k
        ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
1395
7.16k
    if (i)
1396
253
      p = isl_printer_print_str(p, "; ");
1397
7.16k
    data.user = split[i].aff;
1398
7.16k
    p = isl_print_space(space, p, rational, &data);
1399
7.16k
    p = print_disjuncts_map(split[i].map, space, p, 0);
1400
7.16k
  }
1401
6.90k
1402
6.90k
  return p;
1403
6.90k
}
1404
1405
static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
1406
  __isl_take isl_printer *p)
1407
7.27k
{
1408
7.27k
  struct isl_print_space_data data = { 0 };
1409
7.27k
  struct isl_aff_split *split = NULL;
1410
7.27k
  int rational;
1411
7.27k
1412
7.27k
  if (!p || !map)
1413
0
    return isl_printer_free(p);
1414
7.27k
  if (!p->dump && map->n > 0)
1415
6.90k
    split = split_aff(map);
1416
7.27k
  if (split) {
1417
6.90k
    p = print_split_map(p, split, map->n, map->dim);
1418
6.90k
  } else {
1419
366
    rational = map->n > 0 &&
1420
366
        ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
1421
366
    p = isl_print_space(map->dim, p, rational, &data);
1422
366
    p = print_disjuncts_map(map, map->dim, p, 0);
1423
366
  }
1424
7.27k
  free_split(split, map->n);
1425
7.27k
  return p;
1426
7.27k
}
1427
1428
static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
1429
  __isl_take isl_printer *p)
1430
6.91k
{
1431
6.91k
  struct isl_print_space_data data = { 0 };
1432
6.91k
1433
6.91k
  p = print_param_tuple(p, map->dim, &data);
1434
6.91k
  p = isl_printer_print_str(p, s_open_set[0]);
1435
6.91k
  p = isl_map_print_isl_body(map, p);
1436
6.91k
  p = isl_printer_print_str(p, s_close_set[0]);
1437
6.91k
  return p;
1438
6.91k
}
1439
1440
static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
1441
  __isl_take isl_printer *p, __isl_keep isl_basic_map *aff)
1442
0
{
1443
0
  struct isl_print_space_data data = { 0 };
1444
0
1445
0
  data.latex = 1;
1446
0
  p = print_param_tuple(p, map->dim, &data);
1447
0
  p = isl_printer_print_str(p, s_open_set[1]);
1448
0
  data.print_dim = &print_dim_eq;
1449
0
  data.user = aff;
1450
0
  p = isl_print_space(map->dim, p, 0, &data);
1451
0
  p = print_disjuncts_map(map, map->dim, p, 1);
1452
0
  p = isl_printer_print_str(p, s_close_set[1]);
1453
0
1454
0
  return p;
1455
0
}
1456
1457
static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
1458
  __isl_take isl_printer *p)
1459
0
{
1460
0
  int i;
1461
0
  struct isl_aff_split *split = NULL;
1462
0
1463
0
  if (map->n > 0)
1464
0
    split = split_aff(map);
1465
0
1466
0
  if (!split)
1467
0
    return print_latex_map(map, p, NULL);
1468
0
1469
0
  for (i = 0; i < map->n; ++i) {
1470
0
    if (!split[i].map)
1471
0
      break;
1472
0
    if (i)
1473
0
      p = isl_printer_print_str(p, " \\cup ");
1474
0
    p = print_latex_map(split[i].map, p, split[i].aff);
1475
0
  }
1476
0
1477
0
  free_split(split, map->n);
1478
0
  return p;
1479
0
}
1480
1481
__isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
1482
  __isl_keep isl_basic_map *bmap)
1483
0
{
1484
0
  if (!p || !bmap)
1485
0
    goto error;
1486
0
  if (p->output_format == ISL_FORMAT_ISL)
1487
0
    return isl_basic_map_print_isl(bmap, p, 0);
1488
0
  else if (p->output_format == ISL_FORMAT_OMEGA)
1489
0
    return basic_map_print_omega(bmap, p);
1490
0
  isl_assert(bmap->ctx, 0, goto error);
1491
0
error:
1492
0
  isl_printer_free(p);
1493
0
  return NULL;
1494
0
}
1495
1496
__isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
1497
  __isl_keep isl_basic_set *bset)
1498
0
{
1499
0
  if (!p || !bset)
1500
0
    goto error;
1501
0
1502
0
  if (p->output_format == ISL_FORMAT_ISL)
1503
0
    return isl_basic_map_print_isl(bset, p, 0);
1504
0
  else if (p->output_format == ISL_FORMAT_POLYLIB)
1505
0
    return isl_basic_set_print_polylib(bset, p, 0);
1506
0
  else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1507
0
    return isl_basic_set_print_polylib(bset, p, 1);
1508
0
  else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
1509
0
    return bset_print_constraints_polylib(bset, p);
1510
0
  else if (p->output_format == ISL_FORMAT_OMEGA)
1511
0
    return basic_set_print_omega(bset, p);
1512
0
  isl_assert(p->ctx, 0, goto error);
1513
0
error:
1514
0
  isl_printer_free(p);
1515
0
  return NULL;
1516
0
}
1517
1518
__isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
1519
  __isl_keep isl_set *set)
1520
3.16k
{
1521
3.16k
  if (!p || !set)
1522
0
    goto error;
1523
3.16k
  if (p->output_format == ISL_FORMAT_ISL)
1524
3.16k
    return isl_map_print_isl(set_to_map(set), p);
1525
0
  else if (p->output_format == ISL_FORMAT_POLYLIB)
1526
0
    return isl_set_print_polylib(set, p, 0);
1527
0
  else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1528
0
    return isl_set_print_polylib(set, p, 1);
1529
0
  else if (p->output_format == ISL_FORMAT_OMEGA)
1530
0
    return isl_set_print_omega(set, p);
1531
0
  else if (p->output_format == ISL_FORMAT_LATEX)
1532
0
    return isl_map_print_latex(set_to_map(set), p);
1533
0
  isl_assert(set->ctx, 0, goto error);
1534
0
error:
1535
0
  isl_printer_free(p);
1536
0
  return NULL;
1537
0
}
1538
1539
__isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1540
  __isl_keep isl_map *map)
1541
3.75k
{
1542
3.75k
  if (!p || !map)
1543
0
    goto error;
1544
3.75k
1545
3.75k
  if (p->output_format == ISL_FORMAT_ISL)
1546
3.75k
    return isl_map_print_isl(map, p);
1547
0
  else if (p->output_format == ISL_FORMAT_POLYLIB)
1548
0
    return isl_map_print_polylib(map, p, 0);
1549
0
  else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1550
0
    return isl_map_print_polylib(map, p, 1);
1551
0
  else if (p->output_format == ISL_FORMAT_OMEGA)
1552
0
    return isl_map_print_omega(map, p);
1553
0
  else if (p->output_format == ISL_FORMAT_LATEX)
1554
0
    return isl_map_print_latex(map, p);
1555
0
  isl_assert(map->ctx, 0, goto error);
1556
0
error:
1557
0
  isl_printer_free(p);
1558
0
  return NULL;
1559
0
}
1560
1561
struct isl_union_print_data {
1562
  isl_printer *p;
1563
  int first;
1564
};
1565
1566
static isl_stat print_map_body(__isl_take isl_map *map, void *user)
1567
354
{
1568
354
  struct isl_union_print_data *data;
1569
354
  data = (struct isl_union_print_data *)user;
1570
354
1571
354
  if (!data->first)
1572
194
    data->p = isl_printer_print_str(data->p, "; ");
1573
354
  data->first = 0;
1574
354
1575
354
  data->p = isl_map_print_isl_body(map, data->p);
1576
354
  isl_map_free(map);
1577
354
1578
354
  return isl_stat_ok;
1579
354
}
1580
1581
/* Print the body of "umap" (everything except the parameter declarations)
1582
 * to "p" in isl format.
1583
 */
1584
static __isl_give isl_printer *isl_printer_print_union_map_isl_body(
1585
  __isl_take isl_printer *p, __isl_keep isl_union_map *umap)
1586
274
{
1587
274
  struct isl_union_print_data data;
1588
274
1589
274
  p = isl_printer_print_str(p, s_open_set[0]);
1590
274
  data.p = p;
1591
274
  data.first = 1;
1592
274
  isl_union_map_foreach_map(umap, &print_map_body, &data);
1593
274
  p = data.p;
1594
274
  p = isl_printer_print_str(p, s_close_set[0]);
1595
274
  return p;
1596
274
}
1597
1598
/* Print the body of "uset" (everything except the parameter declarations)
1599
 * to "p" in isl format.
1600
 */
1601
static __isl_give isl_printer *isl_printer_print_union_set_isl_body(
1602
  __isl_take isl_printer *p, __isl_keep isl_union_set *uset)
1603
7
{
1604
7
  return isl_printer_print_union_map_isl_body(p, uset_to_umap(uset));
1605
7
}
1606
1607
/* Print the isl_union_map "umap" to "p" in isl format.
1608
 */
1609
static __isl_give isl_printer *isl_union_map_print_isl(
1610
  __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1611
267
{
1612
267
  struct isl_print_space_data space_data = { 0 };
1613
267
  isl_space *space;
1614
267
1615
267
  space = isl_union_map_get_space(umap);
1616
267
  p = print_param_tuple(p, space, &space_data);
1617
267
  isl_space_free(space);
1618
267
1619
267
  p = isl_printer_print_union_map_isl_body(p, umap);
1620
267
1621
267
  return p;
1622
267
}
1623
1624
static isl_stat print_latex_map_body(__isl_take isl_map *map, void *user)
1625
0
{
1626
0
  struct isl_union_print_data *data;
1627
0
  data = (struct isl_union_print_data *)user;
1628
0
1629
0
  if (!data->first)
1630
0
    data->p = isl_printer_print_str(data->p, " \\cup ");
1631
0
  data->first = 0;
1632
0
1633
0
  data->p = isl_map_print_latex(map, data->p);
1634
0
  isl_map_free(map);
1635
0
1636
0
  return isl_stat_ok;
1637
0
}
1638
1639
static __isl_give isl_printer *isl_union_map_print_latex(
1640
  __isl_keep isl_union_map *umap, __isl_take isl_printer *p)
1641
0
{
1642
0
  struct isl_union_print_data data = { p, 1 };
1643
0
  isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1644
0
  p = data.p;
1645
0
  return p;
1646
0
}
1647
1648
__isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1649
  __isl_keep isl_union_map *umap)
1650
267
{
1651
267
  if (!p || !umap)
1652
0
    goto error;
1653
267
1654
267
  if (p->output_format == ISL_FORMAT_ISL)
1655
267
    return isl_union_map_print_isl(umap, p);
1656
0
  if (p->output_format == ISL_FORMAT_LATEX)
1657
0
    return isl_union_map_print_latex(umap, p);
1658
0
1659
0
  isl_die(p->ctx, isl_error_invalid,
1660
0
    "invalid output format for isl_union_map", goto error);
1661
0
error:
1662
0
  isl_printer_free(p);
1663
0
  return NULL;
1664
0
}
1665
1666
__isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1667
  __isl_keep isl_union_set *uset)
1668
0
{
1669
0
  if (!p || !uset)
1670
0
    goto error;
1671
0
1672
0
  if (p->output_format == ISL_FORMAT_ISL)
1673
0
    return isl_union_map_print_isl(uset_to_umap(uset), p);
1674
0
  if (p->output_format == ISL_FORMAT_LATEX)
1675
0
    return isl_union_map_print_latex(uset_to_umap(uset), p);
1676
0
1677
0
  isl_die(p->ctx, isl_error_invalid,
1678
0
    "invalid output format for isl_union_set", goto error);
1679
0
error:
1680
0
  isl_printer_free(p);
1681
0
  return NULL;
1682
0
}
1683
1684
static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
1685
0
{
1686
0
  int i;
1687
0
  int n;
1688
0
1689
0
  for (i = 0, n = 0; i < rec->n; ++i)
1690
0
    if (!isl_upoly_is_zero(rec->p[i]))
1691
0
      ++n;
1692
0
1693
0
  return n;
1694
0
}
1695
1696
static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
1697
  __isl_take isl_printer *p, int first)
1698
0
{
1699
0
  struct isl_upoly_cst *cst;
1700
0
  int neg;
1701
0
1702
0
  cst = isl_upoly_as_cst(up);
1703
0
  if (!cst)
1704
0
    goto error;
1705
0
  neg = !first && isl_int_is_neg(cst->n);
1706
0
  if (!first)
1707
0
    p = isl_printer_print_str(p, neg ? " - " :  " + ");
1708
0
  if (neg)
1709
0
    isl_int_neg(cst->n, cst->n);
1710
0
  if (isl_int_is_zero(cst->d)) {
1711
0
    int sgn = isl_int_sgn(cst->n);
1712
0
    p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1713
0
              sgn == 0 ? "NaN" : "infty");
1714
0
  } else
1715
0
    p = isl_printer_print_isl_int(p, cst->n);
1716
0
  if (neg)
1717
0
    isl_int_neg(cst->n, cst->n);
1718
0
  if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1719
0
    p = isl_printer_print_str(p, "/");
1720
0
    p = isl_printer_print_isl_int(p, cst->d);
1721
0
  }
1722
0
  return p;
1723
0
error:
1724
0
  isl_printer_free(p);
1725
0
  return NULL;
1726
0
}
1727
1728
static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1729
  __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var)
1730
0
{
1731
0
  unsigned total;
1732
0
1733
0
  total = isl_space_dim(dim, isl_dim_all);
1734
0
  if (var < total)
1735
0
    p = print_term(dim, NULL, dim->ctx->one, 1 + var, p, 0);
1736
0
  else
1737
0
    p = print_div(dim, div, var - total, p);
1738
0
  return p;
1739
0
}
1740
1741
static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1742
  __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var, int exp)
1743
0
{
1744
0
  p = print_base(p, dim, div, var);
1745
0
  if (exp == 1)
1746
0
    return p;
1747
0
  if (p->output_format == ISL_FORMAT_C) {
1748
0
    int i;
1749
0
    for (i = 1; i < exp; ++i) {
1750
0
      p = isl_printer_print_str(p, "*");
1751
0
      p = print_base(p, dim, div, var);
1752
0
    }
1753
0
  } else {
1754
0
    p = isl_printer_print_str(p, "^");
1755
0
    p = isl_printer_print_int(p, exp);
1756
0
  }
1757
0
  return p;
1758
0
}
1759
1760
/* Print the polynomial "up" defined over the domain space "space" and
1761
 * local variables defined by "div" to "p".
1762
 */
1763
static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1764
  __isl_keep isl_space *space, __isl_keep isl_mat *div,
1765
  __isl_take isl_printer *p)
1766
0
{
1767
0
  int i, n, first, print_parens;
1768
0
  struct isl_upoly_rec *rec;
1769
0
1770
0
  if (!p || !up || !space || !div)
1771
0
    goto error;
1772
0
1773
0
  if (isl_upoly_is_cst(up))
1774
0
    return upoly_print_cst(up, p, 1);
1775
0
1776
0
  rec = isl_upoly_as_rec(up);
1777
0
  if (!rec)
1778
0
    goto error;
1779
0
  n = upoly_rec_n_non_zero(rec);
1780
0
  print_parens = n > 1;
1781
0
  if (print_parens)
1782
0
    p = isl_printer_print_str(p, "(");
1783
0
  for (i = 0, first = 1; i < rec->n; ++i) {
1784
0
    if (isl_upoly_is_zero(rec->p[i]))
1785
0
      continue;
1786
0
    if (isl_upoly_is_negone(rec->p[i])) {
1787
0
      if (!i)
1788
0
        p = isl_printer_print_str(p, "-1");
1789
0
      else if (first)
1790
0
        p = isl_printer_print_str(p, "-");
1791
0
      else
1792
0
        p = isl_printer_print_str(p, " - ");
1793
0
    } else if (isl_upoly_is_cst(rec->p[i]) &&
1794
0
        !isl_upoly_is_one(rec->p[i]))
1795
0
      p = upoly_print_cst(rec->p[i], p, first);
1796
0
    else {
1797
0
      if (!first)
1798
0
        p = isl_printer_print_str(p, " + ");
1799
0
      if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1800
0
        p = upoly_print(rec->p[i], space, div, p);
1801
0
    }
1802
0
    first = 0;
1803
0
    if (i == 0)
1804
0
      continue;
1805
0
    if (!isl_upoly_is_one(rec->p[i]) &&
1806
0
        !isl_upoly_is_negone(rec->p[i]))
1807
0
      p = isl_printer_print_str(p, " * ");
1808
0
    p = print_pow(p, space, div, rec->up.var, i);
1809
0
  }
1810
0
  if (print_parens)
1811
0
    p = isl_printer_print_str(p, ")");
1812
0
  return p;
1813
0
error:
1814
0
  isl_printer_free(p);
1815
0
  return NULL;
1816
0
}
1817
1818
static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1819
  __isl_keep isl_qpolynomial *qp)
1820
0
{
1821
0
  if (!p || !qp)
1822
0
    goto error;
1823
0
  p = upoly_print(qp->upoly, qp->dim, qp->div, p);
1824
0
  return p;
1825
0
error:
1826
0
  isl_printer_free(p);
1827
0
  return NULL;
1828
0
}
1829
1830
static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1831
  __isl_keep isl_qpolynomial *qp)
1832
0
{
1833
0
  struct isl_print_space_data data = { 0 };
1834
0
1835
0
  if (!p || !qp)
1836
0
    goto error;
1837
0
1838
0
  p = print_param_tuple(p, qp->dim, &data);
1839
0
  p = isl_printer_print_str(p, "{ ");
1840
0
  if (!isl_space_is_params(qp->dim)) {
1841
0
    p = isl_print_space(qp->dim, p, 0, &data);
1842
0
    p = isl_printer_print_str(p, " -> ");
1843
0
  }
1844
0
  p = print_qpolynomial(p, qp);
1845
0
  p = isl_printer_print_str(p, " }");
1846
0
  return p;
1847
0
error:
1848
0
  isl_printer_free(p);
1849
0
  return NULL;
1850
0
}
1851
1852
/* Print the quasi-polynomial "qp" to "p" in C format, with the variable names
1853
 * taken from the domain space "space".
1854
 */
1855
static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1856
  __isl_keep isl_space *space, __isl_keep isl_qpolynomial *qp)
1857
0
{
1858
0
  isl_int den;
1859
0
1860
0
  isl_int_init(den);
1861
0
  isl_qpolynomial_get_den(qp, &den);
1862
0
  if (!isl_int_is_one(den)) {
1863
0
    isl_qpolynomial *f;
1864
0
    p = isl_printer_print_str(p, "(");
1865
0
    qp = isl_qpolynomial_copy(qp);
1866
0
    f = isl_qpolynomial_rat_cst_on_domain(isl_space_copy(qp->dim),
1867
0
            den, qp->dim->ctx->one);
1868
0
    qp = isl_qpolynomial_mul(qp, f);
1869
0
  }
1870
0
  if (qp)
1871
0
    p = upoly_print(qp->upoly, space, qp->div, p);
1872
0
  else
1873
0
    p = isl_printer_free(p);
1874
0
  if (!isl_int_is_one(den)) {
1875
0
    p = isl_printer_print_str(p, ")/");
1876
0
    p = isl_printer_print_isl_int(p, den);
1877
0
    isl_qpolynomial_free(qp);
1878
0
  }
1879
0
  isl_int_clear(den);
1880
0
  return p;
1881
0
}
1882
1883
__isl_give isl_printer *isl_printer_print_qpolynomial(
1884
  __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1885
0
{
1886
0
  if (!p || !qp)
1887
0
    goto error;
1888
0
1889
0
  if (p->output_format == ISL_FORMAT_ISL)
1890
0
    return print_qpolynomial_isl(p, qp);
1891
0
  else if (p->output_format == ISL_FORMAT_C)
1892
0
    return print_qpolynomial_c(p, qp->dim, qp);
1893
0
  else
1894
0
    isl_die(qp->dim->ctx, isl_error_unsupported,
1895
0
      "output format not supported for isl_qpolynomials",
1896
0
      goto error);
1897
0
error:
1898
0
  isl_printer_free(p);
1899
0
  return NULL;
1900
0
}
1901
1902
void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1903
  unsigned output_format)
1904
0
{
1905
0
  isl_printer *p;
1906
0
1907
0
  if  (!qp)
1908
0
    return;
1909
0
1910
0
  isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1911
0
  p = isl_printer_to_file(qp->dim->ctx, out);
1912
0
  p = isl_printer_print_qpolynomial(p, qp);
1913
0
  isl_printer_free(p);
1914
0
}
1915
1916
static __isl_give isl_printer *qpolynomial_fold_print(
1917
  __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1918
0
{
1919
0
  int i;
1920
0
1921
0
  if (fold->type == isl_fold_min)
1922
0
    p = isl_printer_print_str(p, "min");
1923
0
  else if (fold->type == isl_fold_max)
1924
0
    p = isl_printer_print_str(p, "max");
1925
0
  p = isl_printer_print_str(p, "(");
1926
0
  for (i = 0; i < fold->n; ++i) {
1927
0
    if (i)
1928
0
      p = isl_printer_print_str(p, ", ");
1929
0
    p = print_qpolynomial(p, fold->qp[i]);
1930
0
  }
1931
0
  p = isl_printer_print_str(p, ")");
1932
0
  return p;
1933
0
}
1934
1935
void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1936
  FILE *out, unsigned output_format)
1937
0
{
1938
0
  isl_printer *p;
1939
0
1940
0
  if (!fold)
1941
0
    return;
1942
0
1943
0
  isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1944
0
1945
0
  p = isl_printer_to_file(fold->dim->ctx, out);
1946
0
  p = isl_printer_print_qpolynomial_fold(p, fold);
1947
0
1948
0
  isl_printer_free(p);
1949
0
}
1950
1951
static __isl_give isl_printer *isl_pwqp_print_isl_body(
1952
  __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1953
0
{
1954
0
  struct isl_print_space_data data = { 0 };
1955
0
  int i = 0;
1956
0
1957
0
  for (i = 0; i < pwqp->n; ++i) {
1958
0
    isl_space *space;
1959
0
1960
0
    if (i)
1961
0
      p = isl_printer_print_str(p, "; ");
1962
0
    space = isl_qpolynomial_get_domain_space(pwqp->p[i].qp);
1963
0
    if (!isl_space_is_params(space)) {
1964
0
      p = isl_print_space(space, p, 0, &data);
1965
0
      p = isl_printer_print_str(p, " -> ");
1966
0
    }
1967
0
    p = print_qpolynomial(p, pwqp->p[i].qp);
1968
0
    p = print_disjuncts(set_to_map(pwqp->p[i].set), space, p, 0);
1969
0
    isl_space_free(space);
1970
0
  }
1971
0
1972
0
  return p;
1973
0
}
1974
1975
static __isl_give isl_printer *print_pw_qpolynomial_isl(
1976
  __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1977
0
{
1978
0
  struct isl_print_space_data data = { 0 };
1979
0
1980
0
  if (!p || !pwqp)
1981
0
    goto error;
1982
0
1983
0
  p = print_param_tuple(p, pwqp->dim, &data);
1984
0
  p = isl_printer_print_str(p, "{ ");
1985
0
  if (pwqp->n == 0) {
1986
0
    if (!isl_space_is_set(pwqp->dim)) {
1987
0
      p = print_tuple(pwqp->dim, p, isl_dim_in, &data);
1988
0
      p = isl_printer_print_str(p, " -> ");
1989
0
    }
1990
0
    p = isl_printer_print_str(p, "0");
1991
0
  }
1992
0
  p = isl_pwqp_print_isl_body(p, pwqp);
1993
0
  p = isl_printer_print_str(p, " }");
1994
0
  return p;
1995
0
error:
1996
0
  isl_printer_free(p);
1997
0
  return NULL;
1998
0
}
1999
2000
void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
2001
  unsigned output_format)
2002
0
{
2003
0
  isl_printer *p;
2004
0
2005
0
  if (!pwqp)
2006
0
    return;
2007
0
2008
0
  p = isl_printer_to_file(pwqp->dim->ctx, out);
2009
0
  p = isl_printer_set_output_format(p, output_format);
2010
0
  p = isl_printer_print_pw_qpolynomial(p, pwqp);
2011
0
2012
0
  isl_printer_free(p);
2013
0
}
2014
2015
static __isl_give isl_printer *isl_pwf_print_isl_body(
2016
  __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2017
0
{
2018
0
  struct isl_print_space_data data = { 0 };
2019
0
  int i = 0;
2020
0
2021
0
  for (i = 0; i < pwf->n; ++i) {
2022
0
    isl_space *space;
2023
0
2024
0
    if (i)
2025
0
      p = isl_printer_print_str(p, "; ");
2026
0
    space = isl_qpolynomial_fold_get_domain_space(pwf->p[i].fold);
2027
0
    if (!isl_space_is_params(space)) {
2028
0
      p = isl_print_space(space, p, 0, &data);
2029
0
      p = isl_printer_print_str(p, " -> ");
2030
0
    }
2031
0
    p = qpolynomial_fold_print(pwf->p[i].fold, p);
2032
0
    p = print_disjuncts(set_to_map(pwf->p[i].set), space, p, 0);
2033
0
    isl_space_free(space);
2034
0
  }
2035
0
2036
0
  return p;
2037
0
}
2038
2039
static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
2040
  __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2041
0
{
2042
0
  struct isl_print_space_data data = { 0 };
2043
0
2044
0
  p = print_param_tuple(p, pwf->dim, &data);
2045
0
  p = isl_printer_print_str(p, "{ ");
2046
0
  if (pwf->n == 0) {
2047
0
    if (!isl_space_is_set(pwf->dim)) {
2048
0
      p = print_tuple(pwf->dim, p, isl_dim_in, &data);
2049
0
      p = isl_printer_print_str(p, " -> ");
2050
0
    }
2051
0
    p = isl_printer_print_str(p, "0");
2052
0
  }
2053
0
  p = isl_pwf_print_isl_body(p, pwf);
2054
0
  p = isl_printer_print_str(p, " }");
2055
0
  return p;
2056
0
}
2057
2058
static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
2059
  __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c);
2060
2061
static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
2062
  __isl_keep isl_space *dim,
2063
  __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
2064
0
{
2065
0
  if (type == isl_dim_div) {
2066
0
    p = isl_printer_print_str(p, "floord(");
2067
0
    p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
2068
0
    p = isl_printer_print_str(p, ", ");
2069
0
    p = isl_printer_print_isl_int(p, bset->div[pos][0]);
2070
0
    p = isl_printer_print_str(p, ")");
2071
0
  } else {
2072
0
    const char *name;
2073
0
2074
0
    name = isl_space_get_dim_name(dim, type, pos);
2075
0
    if (!name)
2076
0
      name = "UNNAMED";
2077
0
    p = isl_printer_print_str(p, name);
2078
0
  }
2079
0
  return p;
2080
0
}
2081
2082
static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
2083
  __isl_keep isl_space *dim,
2084
  __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
2085
0
{
2086
0
  enum isl_dim_type type;
2087
0
2088
0
  if (pos == 0)
2089
0
    return isl_printer_print_isl_int(p, c);
2090
0
2091
0
  if (isl_int_is_one(c))
2092
0
    ;
2093
0
  else if (isl_int_is_negone(c))
2094
0
    p = isl_printer_print_str(p, "-");
2095
0
  else {
2096
0
    p = isl_printer_print_isl_int(p, c);
2097
0
    p = isl_printer_print_str(p, "*");
2098
0
  }
2099
0
  type = pos2type(dim, &pos);
2100
0
  p = print_name_c(p, dim, bset, type, pos);
2101
0
  return p;
2102
0
}
2103
2104
static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
2105
  __isl_keep isl_space *dim,
2106
  __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
2107
0
{
2108
0
  int i;
2109
0
  int first;
2110
0
2111
0
  for (i = 0, first = 1; i < len; ++i) {
2112
0
    int flip = 0;
2113
0
    if (isl_int_is_zero(c[i]))
2114
0
      continue;
2115
0
    if (!first) {
2116
0
      if (isl_int_is_neg(c[i])) {
2117
0
        flip = 1;
2118
0
        isl_int_neg(c[i], c[i]);
2119
0
        p = isl_printer_print_str(p, " - ");
2120
0
      } else 
2121
0
        p = isl_printer_print_str(p, " + ");
2122
0
    }
2123
0
    first = 0;
2124
0
    p = print_term_c(p, dim, bset, c[i], i);
2125
0
    if (flip)
2126
0
      isl_int_neg(c[i], c[i]);
2127
0
  }
2128
0
  if (first)
2129
0
    p = isl_printer_print_str(p, "0");
2130
0
  return p;
2131
0
}
2132
2133
static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
2134
  __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c)
2135
0
{
2136
0
  unsigned len = 1 + isl_basic_set_total_dim(bset);
2137
0
  return print_partial_affine_c(p, dim, bset, c, len);
2138
0
}
2139
2140
/* We skip the constraint if it is implied by the div expression.
2141
 *
2142
 * *first indicates whether this is the first constraint in the conjunction and
2143
 * is updated if the constraint is actually printed.
2144
 */
2145
static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
2146
  __isl_keep isl_space *dim,
2147
  __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int *first)
2148
0
{
2149
0
  unsigned o_div;
2150
0
  unsigned n_div;
2151
0
  int div;
2152
0
2153
0
  o_div = isl_basic_set_offset(bset, isl_dim_div);
2154
0
  n_div = isl_basic_set_dim(bset, isl_dim_div);
2155
0
  div = isl_seq_last_non_zero(c + o_div, n_div);
2156
0
  if (div >= 0) {
2157
0
    isl_bool is_div = isl_basic_set_is_div_constraint(bset, c, div);
2158
0
    if (is_div < 0)
2159
0
      return isl_printer_free(p);
2160
0
    if (is_div)
2161
0
      return p;
2162
0
  }
2163
0
2164
0
  if (!*first)
2165
0
    p = isl_printer_print_str(p, " && ");
2166
0
2167
0
  p = print_affine_c(p, dim, bset, c);
2168
0
  p = isl_printer_print_str(p, " ");
2169
0
  p = isl_printer_print_str(p, op);
2170
0
  p = isl_printer_print_str(p, " 0");
2171
0
2172
0
  *first = 0;
2173
0
2174
0
  return p;
2175
0
}
2176
2177
static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
2178
  __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset)
2179
0
{
2180
0
  int i, j;
2181
0
  int first = 1;
2182
0
  unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
2183
0
  unsigned total = isl_basic_set_total_dim(bset) - n_div;
2184
0
2185
0
  for (i = 0; i < bset->n_eq; ++i) {
2186
0
    j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
2187
0
    if (j < 0)
2188
0
      p = print_constraint_c(p, dim, bset,
2189
0
            bset->eq[i], "==", &first);
2190
0
    else {
2191
0
      if (i)
2192
0
        p = isl_printer_print_str(p, " && ");
2193
0
      p = isl_printer_print_str(p, "(");
2194
0
      p = print_partial_affine_c(p, dim, bset, bset->eq[i],
2195
0
               1 + total + j);
2196
0
      p = isl_printer_print_str(p, ") % ");
2197
0
      p = isl_printer_print_isl_int(p,
2198
0
            bset->eq[i][1 + total + j]);
2199
0
      p = isl_printer_print_str(p, " == 0");
2200
0
      first = 0;
2201
0
    }
2202
0
  }
2203
0
  for (i = 0; i < bset->n_ineq; ++i)
2204
0
    p = print_constraint_c(p, dim, bset, bset->ineq[i], ">=",
2205
0
          &first);
2206
0
  return p;
2207
0
}
2208
2209
static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
2210
  __isl_keep isl_space *dim, __isl_keep isl_set *set)
2211
0
{
2212
0
  int i;
2213
0
2214
0
  if (!set)
2215
0
    return isl_printer_free(p);
2216
0
2217
0
  if (set->n == 0)
2218
0
    p = isl_printer_print_str(p, "0");
2219
0
2220
0
  for (i = 0; i < set->n; ++i) {
2221
0
    if (i)
2222
0
      p = isl_printer_print_str(p, " || ");
2223
0
    if (set->n > 1)
2224
0
      p = isl_printer_print_str(p, "(");
2225
0
    p = print_basic_set_c(p, dim, set->p[i]);
2226
0
    if (set->n > 1)
2227
0
      p = isl_printer_print_str(p, ")");
2228
0
  }
2229
0
  return p;
2230
0
}
2231
2232
/* Print the piecewise quasi-polynomial "pwqp" to "p" in C format.
2233
 */
2234
static __isl_give isl_printer *print_pw_qpolynomial_c(
2235
  __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2236
0
{
2237
0
  int i;
2238
0
  isl_space *space;
2239
0
2240
0
  space = isl_pw_qpolynomial_get_domain_space(pwqp);
2241
0
  if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set)) {
2242
0
    p = print_qpolynomial_c(p, space, pwqp->p[0].qp);
2243
0
    isl_space_free(space);
2244
0
    return p;
2245
0
  }
2246
0
2247
0
  for (i = 0; i < pwqp->n; ++i) {
2248
0
    p = isl_printer_print_str(p, "(");
2249
0
    p = print_set_c(p, space, pwqp->p[i].set);
2250
0
    p = isl_printer_print_str(p, ") ? (");
2251
0
    p = print_qpolynomial_c(p, space, pwqp->p[i].qp);
2252
0
    p = isl_printer_print_str(p, ") : ");
2253
0
  }
2254
0
2255
0
  isl_space_free(space);
2256
0
  p = isl_printer_print_str(p, "0");
2257
0
  return p;
2258
0
}
2259
2260
__isl_give isl_printer *isl_printer_print_pw_qpolynomial(
2261
  __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
2262
0
{
2263
0
  if (!p || !pwqp)
2264
0
    goto error;
2265
0
2266
0
  if (p->output_format == ISL_FORMAT_ISL)
2267
0
    return print_pw_qpolynomial_isl(p, pwqp);
2268
0
  else if (p->output_format == ISL_FORMAT_C)
2269
0
    return print_pw_qpolynomial_c(p, pwqp);
2270
0
  isl_assert(p->ctx, 0, goto error);
2271
0
error:
2272
0
  isl_printer_free(p);
2273
0
  return NULL;
2274
0
}
2275
2276
static isl_stat print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
2277
0
{
2278
0
  struct isl_union_print_data *data;
2279
0
  data = (struct isl_union_print_data *)user;
2280
0
2281
0
  if (!data->first)
2282
0
    data->p = isl_printer_print_str(data->p, "; ");
2283
0
  data->first = 0;
2284
0
2285
0
  data->p = isl_pwqp_print_isl_body(data->p, pwqp);
2286
0
  isl_pw_qpolynomial_free(pwqp);
2287
0
2288
0
  return isl_stat_ok;
2289
0
}
2290
2291
static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
2292
  __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2293
0
{
2294
0
  struct isl_union_print_data data;
2295
0
  struct isl_print_space_data space_data = { 0 };
2296
0
  isl_space *space;
2297
0
2298
0
  space = isl_union_pw_qpolynomial_get_space(upwqp);
2299
0
  p = print_param_tuple(p, space, &space_data);
2300
0
  isl_space_free(space);
2301
0
  p = isl_printer_print_str(p, "{ ");
2302
0
  data.p = p;
2303
0
  data.first = 1;
2304
0
  isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
2305
0
              &data);
2306
0
  p = data.p;
2307
0
  p = isl_printer_print_str(p, " }");
2308
0
  return p;
2309
0
}
2310
2311
__isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
2312
  __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
2313
0
{
2314
0
  if (!p || !upwqp)
2315
0
    goto error;
2316
0
2317
0
  if (p->output_format == ISL_FORMAT_ISL)
2318
0
    return print_union_pw_qpolynomial_isl(p, upwqp);
2319
0
  isl_die(p->ctx, isl_error_invalid,
2320
0
    "invalid output format for isl_union_pw_qpolynomial",
2321
0
    goto error);
2322
0
error:
2323
0
  isl_printer_free(p);
2324
0
  return NULL;
2325
0
}
2326
2327
/* Print the quasi-polynomial reduction "fold" to "p" in C format,
2328
 * with the variable names taken from the domain space "space".
2329
 */
2330
static __isl_give isl_printer *print_qpolynomial_fold_c(
2331
  __isl_take isl_printer *p, __isl_keep isl_space *space,
2332
  __isl_keep isl_qpolynomial_fold *fold)
2333
0
{
2334
0
  int i;
2335
0
2336
0
  for (i = 0; i < fold->n - 1; ++i)
2337
0
    if (fold->type == isl_fold_min)
2338
0
      p = isl_printer_print_str(p, "min(");
2339
0
    else if (fold->type == isl_fold_max)
2340
0
      p = isl_printer_print_str(p, "max(");
2341
0
2342
0
  for (i = 0; i < fold->n; ++i) {
2343
0
    if (i)
2344
0
      p = isl_printer_print_str(p, ", ");
2345
0
    p = print_qpolynomial_c(p, space, fold->qp[i]);
2346
0
    if (i)
2347
0
      p = isl_printer_print_str(p, ")");
2348
0
  }
2349
0
  return p;
2350
0
}
2351
2352
__isl_give isl_printer *isl_printer_print_qpolynomial_fold(
2353
  __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
2354
0
{
2355
0
  if  (!p || !fold)
2356
0
    goto error;
2357
0
  if (p->output_format == ISL_FORMAT_ISL)
2358
0
    return qpolynomial_fold_print(fold, p);
2359
0
  else if (p->output_format == ISL_FORMAT_C)
2360
0
    return print_qpolynomial_fold_c(p, fold->dim, fold);
2361
0
  isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2362
0
    goto error);
2363
0
error:
2364
0
  isl_printer_free(p);
2365
0
  return NULL;
2366
0
}
2367
2368
/* Print the piecewise quasi-polynomial reduction "pwf" to "p" in C format.
2369
 */
2370
static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
2371
  __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2372
0
{
2373
0
  int i;
2374
0
  isl_space *space;
2375
0
2376
0
  space = isl_pw_qpolynomial_fold_get_domain_space(pwf);
2377
0
  if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set)) {
2378
0
    p = print_qpolynomial_fold_c(p, space, pwf->p[0].fold);
2379
0
    isl_space_free(space);
2380
0
    return p;
2381
0
  }
2382
0
2383
0
  for (i = 0; i < pwf->n; ++i) {
2384
0
    p = isl_printer_print_str(p, "(");
2385
0
    p = print_set_c(p, space, pwf->p[i].set);
2386
0
    p = isl_printer_print_str(p, ") ? (");
2387
0
    p = print_qpolynomial_fold_c(p, space, pwf->p[i].fold);
2388
0
    p = isl_printer_print_str(p, ") : ");
2389
0
  }
2390
0
2391
0
  isl_space_free(space);
2392
0
  p = isl_printer_print_str(p, "0");
2393
0
  return p;
2394
0
}
2395
2396
__isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
2397
  __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
2398
0
{
2399
0
  if (!p || !pwf)
2400
0
    goto error;
2401
0
2402
0
  if (p->output_format == ISL_FORMAT_ISL)
2403
0
    return print_pw_qpolynomial_fold_isl(p, pwf);
2404
0
  else if (p->output_format == ISL_FORMAT_C)
2405
0
    return print_pw_qpolynomial_fold_c(p, pwf);
2406
0
  isl_assert(p->ctx, 0, goto error);
2407
0
error:
2408
0
  isl_printer_free(p);
2409
0
  return NULL;
2410
0
}
2411
2412
void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
2413
  FILE *out, unsigned output_format)
2414
0
{
2415
0
  isl_printer *p;
2416
0
2417
0
  if (!pwf)
2418
0
    return;
2419
0
2420
0
  p = isl_printer_to_file(pwf->dim->ctx, out);
2421
0
  p = isl_printer_set_output_format(p, output_format);
2422
0
  p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
2423
0
2424
0
  isl_printer_free(p);
2425
0
}
2426
2427
static isl_stat print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf,
2428
  void *user)
2429
0
{
2430
0
  struct isl_union_print_data *data;
2431
0
  data = (struct isl_union_print_data *)user;
2432
0
2433
0
  if (!data->first)
2434
0
    data->p = isl_printer_print_str(data->p, "; ");
2435
0
  data->first = 0;
2436
0
2437
0
  data->p = isl_pwf_print_isl_body(data->p, pwf);
2438
0
  isl_pw_qpolynomial_fold_free(pwf);
2439
0
2440
0
  return isl_stat_ok;
2441
0
}
2442
2443
static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
2444
  __isl_take isl_printer *p,
2445
  __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2446
0
{
2447
0
  struct isl_union_print_data data;
2448
0
  struct isl_print_space_data space_data = { 0 };
2449
0
  isl_space *space;
2450
0
2451
0
  space = isl_union_pw_qpolynomial_fold_get_space(upwf);
2452
0
  p = print_param_tuple(p, space, &space_data);
2453
0
  isl_space_free(space);
2454
0
  p = isl_printer_print_str(p, "{ ");
2455
0
  data.p = p;
2456
0
  data.first = 1;
2457
0
  isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
2458
0
              &print_pwf_body, &data);
2459
0
  p = data.p;
2460
0
  p = isl_printer_print_str(p, " }");
2461
0
  return p;
2462
0
}
2463
2464
__isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2465
  __isl_take isl_printer *p,
2466
  __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2467
0
{
2468
0
  if (!p || !upwf)
2469
0
    goto error;
2470
0
2471
0
  if (p->output_format == ISL_FORMAT_ISL)
2472
0
    return print_union_pw_qpolynomial_fold_isl(p, upwf);
2473
0
  isl_die(p->ctx, isl_error_invalid,
2474
0
    "invalid output format for isl_union_pw_qpolynomial_fold",
2475
0
    goto error);
2476
0
error:
2477
0
  isl_printer_free(p);
2478
0
  return NULL;
2479
0
}
2480
2481
/* Print the isl_constraint "c" to "p".
2482
 */
2483
__isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
2484
  __isl_keep isl_constraint *c)
2485
0
{
2486
0
  struct isl_print_space_data data = { 0 };
2487
0
  isl_local_space *ls;
2488
0
  isl_space *space;
2489
0
  isl_bool exists;
2490
0
2491
0
  if (!p || !c)
2492
0
    goto error;
2493
0
2494
0
  ls = isl_constraint_get_local_space(c);
2495
0
  if (!ls)
2496
0
    return isl_printer_free(p);
2497
0
  space = isl_local_space_get_space(ls);
2498
0
  p = print_param_tuple(p, space, &data);
2499
0
  p = isl_printer_print_str(p, "{ ");
2500
0
  p = isl_print_space(space, p, 0, &data);
2501
0
  p = isl_printer_print_str(p, " : ");
2502
0
  exists = need_exists(p, ls->div);
2503
0
  if (exists < 0)
2504
0
    p = isl_printer_free(p);
2505
0
  if (exists >= 0 && exists)
2506
0
    p = open_exists(p, space, ls->div, 0);
2507
0
  p = print_affine_of_len(space, ls->div, p, c->v->el, c->v->size);
2508
0
  if (isl_constraint_is_equality(c))
2509
0
    p = isl_printer_print_str(p, " = 0");
2510
0
  else
2511
0
    p = isl_printer_print_str(p, " >= 0");
2512
0
  if (exists >= 0 && exists)
2513
0
    p = isl_printer_print_str(p, s_close_exists[0]);
2514
0
  p = isl_printer_print_str(p, " }");
2515
0
  isl_space_free(space);
2516
0
  isl_local_space_free(ls);
2517
0
2518
0
  return p;
2519
0
error:
2520
0
  isl_printer_free(p);
2521
0
  return NULL;
2522
0
}
2523
2524
static __isl_give isl_printer *isl_printer_print_space_isl(
2525
  __isl_take isl_printer *p, __isl_keep isl_space *space)
2526
2
{
2527
2
  struct isl_print_space_data data = { 0 };
2528
2
2529
2
  if (!space)
2530
0
    goto error;
2531
2
2532
2
  p = print_param_tuple(p, space, &data);
2533
2
2534
2
  p = isl_printer_print_str(p, "{ ");
2535
2
  if (isl_space_is_params(space))
2536
2
    p = isl_printer_print_str(p, s_such_that[0]);
2537
0
  else
2538
0
    p = isl_print_space(space, p, 0, &data);
2539
2
  p = isl_printer_print_str(p, " }");
2540
2
2541
2
  return p;
2542
0
error:
2543
0
  isl_printer_free(p);
2544
0
  return NULL;
2545
2
}
2546
2547
__isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
2548
  __isl_keep isl_space *space)
2549
2
{
2550
2
  if (!p || !space)
2551
0
    return isl_printer_free(p);
2552
2
  if (p->output_format == ISL_FORMAT_ISL)
2553
2
    return isl_printer_print_space_isl(p, space);
2554
0
  else if (p->output_format == ISL_FORMAT_OMEGA)
2555
0
    return print_omega_parameters(space, p);
2556
0
2557
0
  isl_die(isl_space_get_ctx(space), isl_error_unsupported,
2558
0
    "output format not supported for space",
2559
0
    return isl_printer_free(p));
2560
0
}
2561
2562
__isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
2563
  __isl_keep isl_local_space *ls)
2564
0
{
2565
0
  struct isl_print_space_data data = { 0 };
2566
0
  unsigned n_div;
2567
0
2568
0
  if (!ls)
2569
0
    goto error;
2570
0
2571
0
  p = print_param_tuple(p, ls->dim, &data);
2572
0
  p = isl_printer_print_str(p, "{ ");
2573
0
  p = isl_print_space(ls->dim, p, 0, &data);
2574
0
  n_div = isl_local_space_dim(ls, isl_dim_div);
2575
0
  if (n_div > 0) {
2576
0
    p = isl_printer_print_str(p, " : ");
2577
0
    p = isl_printer_print_str(p, s_open_exists[0]);
2578
0
    p = print_div_list(p, ls->dim, ls->div, 0, 1);
2579
0
    p = isl_printer_print_str(p, s_close_exists[0]);
2580
0
  } else if (isl_space_is_params(ls->dim))
2581
0
    p = isl_printer_print_str(p, s_such_that[0]);
2582
0
  p = isl_printer_print_str(p, " }");
2583
0
  return p;
2584
0
error:
2585
0
  isl_printer_free(p);
2586
0
  return NULL;
2587
0
}
2588
2589
/* Print the (potentially rational) affine expression "aff" to "p",
2590
 * with the variable names taken from "space".
2591
 */
2592
static __isl_give isl_printer *print_aff_body(__isl_take isl_printer *p,
2593
  __isl_keep isl_space *space, __isl_keep isl_aff *aff)
2594
1.01k
{
2595
1.01k
  unsigned total;
2596
1.01k
2597
1.01k
  if (isl_aff_is_nan(aff))
2598
0
    return isl_printer_print_str(p, "NaN");
2599
1.01k
2600
1.01k
  total = isl_local_space_dim(aff->ls, isl_dim_all);
2601
1.01k
  p = isl_printer_print_str(p, "(");
2602
1.01k
  p = print_affine_of_len(space, aff->ls->div, p,
2603
1.01k
        aff->v->el + 1, 1 + total);
2604
1.01k
  if (isl_int_is_one(aff->v->el[0]))
2605
1.01k
    p = isl_printer_print_str(p, ")");
2606
0
  else {
2607
0
    p = isl_printer_print_str(p, ")/");
2608
0
    p = isl_printer_print_isl_int(p, aff->v->el[0]);
2609
0
  }
2610
1.01k
2611
1.01k
  return p;
2612
1.01k
}
2613
2614
static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2615
  __isl_keep isl_aff *aff)
2616
329
{
2617
329
  struct isl_print_space_data data = { 0 };
2618
329
2619
329
  if (isl_space_is_params(aff->ls->dim))
2620
0
    ;
2621
329
  else {
2622
329
    p = print_tuple(aff->ls->dim, p, isl_dim_set, &data);
2623
329
    p = isl_printer_print_str(p, " -> ");
2624
329
  }
2625
329
  p = isl_printer_print_str(p, "[");
2626
329
  p = print_aff_body(p, aff->ls->dim, aff);
2627
329
  p = isl_printer_print_str(p, "]");
2628
329
2629
329
  return p;
2630
329
}
2631
2632
static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2633
  __isl_keep isl_aff *aff)
2634
0
{
2635
0
  struct isl_print_space_data data = { 0 };
2636
0
2637
0
  if (!aff)
2638
0
    goto error;
2639
0
2640
0
  p = print_param_tuple(p, aff->ls->dim, &data);
2641
0
  p = isl_printer_print_str(p, "{ ");
2642
0
  p = print_aff(p, aff);
2643
0
  p = isl_printer_print_str(p, " }");
2644
0
  return p;
2645
0
error:
2646
0
  isl_printer_free(p);
2647
0
  return NULL;
2648
0
}
2649
2650
/* Print the body of an isl_pw_aff, i.e., a semicolon delimited
2651
 * sequence of affine expressions, each followed by constraints.
2652
 */
2653
static __isl_give isl_printer *print_pw_aff_body(
2654
  __isl_take isl_printer *p, __isl_keep isl_pw_aff *pa)
2655
329
{
2656
329
  int i;
2657
329
2658
329
  if (!pa)
2659
0
    return isl_printer_free(p);
2660
329
2661
658
  
for (i = 0; 329
i < pa->n;
++i329
) {
2662
329
    isl_space *space;
2663
329
2664
329
    if (i)
2665
0
      p = isl_printer_print_str(p, "; ");
2666
329
    p = print_aff(p, pa->p[i].aff);
2667
329
    space = isl_aff_get_domain_space(pa->p[i].aff);
2668
329
    p = print_disjuncts(set_to_map(pa->p[i].set), space, p, 0);
2669
329
    isl_space_free(space);
2670
329
  }
2671
329
2672
329
  return p;
2673
329
}
2674
2675
static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2676
  __isl_keep isl_pw_aff *pwaff)
2677
325
{
2678
325
  struct isl_print_space_data data = { 0 };
2679
325
2680
325
  if (!pwaff)
2681
0
    goto error;
2682
325
2683
325
  p = print_param_tuple(p, pwaff->dim, &data);
2684
325
  p = isl_printer_print_str(p, "{ ");
2685
325
  p = print_pw_aff_body(p, pwaff);
2686
325
  p = isl_printer_print_str(p, " }");
2687
325
  return p;
2688
0
error:
2689
0
  isl_printer_free(p);
2690
0
  return NULL;
2691
325
}
2692
2693
static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2694
  __isl_keep isl_local_space *ls, isl_int *c);
2695
2696
static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2697
  __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2698
0
{
2699
0
  if (type == isl_dim_div) {
2700
0
    p = isl_printer_print_str(p, "floord(");
2701
0
    p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2702
0
    p = isl_printer_print_str(p, ", ");
2703
0
    p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2704
0
    p = isl_printer_print_str(p, ")");
2705
0
  } else {
2706
0
    const char *name;
2707
0
2708
0
    name = isl_space_get_dim_name(ls->dim, type, pos);
2709
0
    if (!name)
2710
0
      name = "UNNAMED";
2711
0
    p = isl_printer_print_str(p, name);
2712
0
  }
2713
0
  return p;
2714
0
}
2715
2716
static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2717
  __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2718
0
{
2719
0
  enum isl_dim_type type;
2720
0
2721
0
  if (pos == 0)
2722
0
    return isl_printer_print_isl_int(p, c);
2723
0
2724
0
  if (isl_int_is_one(c))
2725
0
    ;
2726
0
  else if (isl_int_is_negone(c))
2727
0
    p = isl_printer_print_str(p, "-");
2728
0
  else {
2729
0
    p = isl_printer_print_isl_int(p, c);
2730
0
    p = isl_printer_print_str(p, "*");
2731
0
  }
2732
0
  type = pos2type(ls->dim, &pos);
2733
0
  p = print_ls_name_c(p, ls, type, pos);
2734
0
  return p;
2735
0
}
2736
2737
static __isl_give isl_printer *print_ls_partial_affine_c(
2738
  __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2739
  isl_int *c, unsigned len)
2740
0
{
2741
0
  int i;
2742
0
  int first;
2743
0
2744
0
  for (i = 0, first = 1; i < len; ++i) {
2745
0
    int flip = 0;
2746
0
    if (isl_int_is_zero(c[i]))
2747
0
      continue;
2748
0
    if (!first) {
2749
0
      if (isl_int_is_neg(c[i])) {
2750
0
        flip = 1;
2751
0
        isl_int_neg(c[i], c[i]);
2752
0
        p = isl_printer_print_str(p, " - ");
2753
0
      } else 
2754
0
        p = isl_printer_print_str(p, " + ");
2755
0
    }
2756
0
    first = 0;
2757
0
    p = print_ls_term_c(p, ls, c[i], i);
2758
0
    if (flip)
2759
0
      isl_int_neg(c[i], c[i]);
2760
0
  }
2761
0
  if (first)
2762
0
    p = isl_printer_print_str(p, "0");
2763
0
  return p;
2764
0
}
2765
2766
static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2767
  __isl_keep isl_local_space *ls, isl_int *c)
2768
0
{
2769
0
  unsigned len = 1 + isl_local_space_dim(ls, isl_dim_all);
2770
0
  return print_ls_partial_affine_c(p, ls, c, len);
2771
0
}
2772
2773
static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2774
  __isl_keep isl_aff *aff)
2775
0
{
2776
0
  unsigned total;
2777
0
2778
0
  total = isl_local_space_dim(aff->ls, isl_dim_all);
2779
0
  if (!isl_int_is_one(aff->v->el[0]))
2780
0
    p = isl_printer_print_str(p, "(");
2781
0
  p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2782
0
  if (!isl_int_is_one(aff->v->el[0])) {
2783
0
    p = isl_printer_print_str(p, ")/");
2784
0
    p = isl_printer_print_isl_int(p, aff->v->el[0]);
2785
0
  }
2786
0
  return p;
2787
0
}
2788
2789
/* In the C format, we cannot express that "pwaff" may be undefined
2790
 * on parts of the domain space.  We therefore assume that the expression
2791
 * will only be evaluated on its definition domain and compute the gist
2792
 * of each cell with respect to this domain.
2793
 */
2794
static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2795
  __isl_keep isl_pw_aff *pwaff)
2796
24
{
2797
24
  isl_set *domain;
2798
24
  isl_ast_build *build;
2799
24
  isl_ast_expr *expr;
2800
24
2801
24
  if (pwaff->n < 1)
2802
24
    
isl_die0
(p->ctx, isl_error_unsupported,
2803
24
      "cannot print empty isl_pw_aff in C format",
2804
24
      return isl_printer_free(p));
2805
24
2806
24
  domain = isl_pw_aff_domain(isl_pw_aff_copy(pwaff));
2807
24
  build = isl_ast_build_from_context(domain);
2808
24
  expr = isl_ast_build_expr_from_pw_aff(build, isl_pw_aff_copy(pwaff));
2809
24
  p = isl_printer_print_ast_expr(p, expr);
2810
24
  isl_ast_expr_free(expr);
2811
24
  isl_ast_build_free(build);
2812
24
2813
24
  return p;
2814
24
}
2815
2816
__isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2817
  __isl_keep isl_aff *aff)
2818
0
{
2819
0
  if (!p || !aff)
2820
0
    goto error;
2821
0
2822
0
  if (p->output_format == ISL_FORMAT_ISL)
2823
0
    return print_aff_isl(p, aff);
2824
0
  else if (p->output_format == ISL_FORMAT_C)
2825
0
    return print_aff_c(p, aff);
2826
0
  isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2827
0
    goto error);
2828
0
error:
2829
0
  isl_printer_free(p);
2830
0
  return NULL;
2831
0
}
2832
2833
__isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2834
  __isl_keep isl_pw_aff *pwaff)
2835
349
{
2836
349
  if (!p || !pwaff)
2837
0
    goto error;
2838
349
2839
349
  if (p->output_format == ISL_FORMAT_ISL)
2840
349
    
return print_pw_aff_isl(p, pwaff)325
;
2841
24
  else if (p->output_format == ISL_FORMAT_C)
2842
24
    return print_pw_aff_c(p, pwaff);
2843
0
  isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2844
0
    goto error);
2845
0
error:
2846
0
  isl_printer_free(p);
2847
0
  return NULL;
2848
0
}
2849
2850
/* Print "pa" in a sequence of isl_pw_affs delimited by semicolons.
2851
 * Each isl_pw_aff itself is also printed as semicolon delimited
2852
 * sequence of pieces.
2853
 * If data->first = 1, then this is the first in the sequence.
2854
 * Update data->first to tell the next element that it is not the first.
2855
 */
2856
static isl_stat print_pw_aff_body_wrap(__isl_take isl_pw_aff *pa,
2857
  void *user)
2858
4
{
2859
4
  struct isl_union_print_data *data;
2860
4
  data = (struct isl_union_print_data *) user;
2861
4
2862
4
  if (!data->first)
2863
2
    data->p = isl_printer_print_str(data->p, "; ");
2864
4
  data->first = 0;
2865
4
2866
4
  data->p = print_pw_aff_body(data->p, pa);
2867
4
  isl_pw_aff_free(pa);
2868
4
2869
4
  return data->p ? isl_stat_ok : 
isl_stat_error0
;
2870
4
}
2871
2872
/* Print the body of an isl_union_pw_aff, i.e., a semicolon delimited
2873
 * sequence of affine expressions, each followed by constraints,
2874
 * with the sequence enclosed in braces.
2875
 */
2876
static __isl_give isl_printer *print_union_pw_aff_body(
2877
  __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2878
2
{
2879
2
  struct isl_union_print_data data = { p, 1 };
2880
2
2881
2
  p = isl_printer_print_str(p, s_open_set[0]);
2882
2
  data.p = p;
2883
2
  if (isl_union_pw_aff_foreach_pw_aff(upa,
2884
2
              &print_pw_aff_body_wrap, &data) < 0)
2885
0
    data.p = isl_printer_free(p);
2886
2
  p = data.p;
2887
2
  p = isl_printer_print_str(p, s_close_set[0]);
2888
2
2889
2
  return p;
2890
2
}
2891
2892
/* Print the isl_union_pw_aff "upa" to "p" in isl format.
2893
 *
2894
 * The individual isl_pw_affs are delimited by a semicolon.
2895
 */
2896
static __isl_give isl_printer *print_union_pw_aff_isl(
2897
  __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2898
0
{
2899
0
  struct isl_print_space_data data = { 0 };
2900
0
  isl_space *space;
2901
0
2902
0
  space = isl_union_pw_aff_get_space(upa);
2903
0
  p = print_param_tuple(p, space, &data);
2904
0
  isl_space_free(space);
2905
0
  p = print_union_pw_aff_body(p, upa);
2906
0
  return p;
2907
0
}
2908
2909
/* Print the isl_union_pw_aff "upa" to "p".
2910
 *
2911
 * We currently only support an isl format.
2912
 */
2913
__isl_give isl_printer *isl_printer_print_union_pw_aff(
2914
  __isl_take isl_printer *p, __isl_keep isl_union_pw_aff *upa)
2915
0
{
2916
0
  if (!p || !upa)
2917
0
    return isl_printer_free(p);
2918
0
2919
0
  if (p->output_format == ISL_FORMAT_ISL)
2920
0
    return print_union_pw_aff_isl(p, upa);
2921
0
  isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2922
0
    "unsupported output format", return isl_printer_free(p));
2923
0
}
2924
2925
/* Print dimension "pos" of data->space to "p".
2926
 *
2927
 * data->user is assumed to be an isl_multi_aff.
2928
 *
2929
 * If the current dimension is an output dimension, then print
2930
 * the corresponding expression.  Otherwise, print the name of the dimension.
2931
 */
2932
static __isl_give isl_printer *print_dim_ma(__isl_take isl_printer *p,
2933
  struct isl_print_space_data *data, unsigned pos)
2934
680
{
2935
680
  isl_multi_aff *ma = data->user;
2936
680
2937
680
  if (data->type == isl_dim_out) {
2938
678
    isl_space *space;
2939
678
2940
678
    space = isl_multi_aff_get_domain_space(ma);
2941
678
    p = print_aff_body(p, space, ma->u.p[pos]);
2942
678
    isl_space_free(space);
2943
678
  } else {
2944
2
    p = print_name(data->space, p, data->type, pos, data->latex);
2945
2
  }
2946
680
2947
680
  return p;
2948
680
}
2949
2950
static __isl_give isl_printer *print_multi_aff(__isl_take isl_printer *p,
2951
  __isl_keep isl_multi_aff *maff)
2952
568
{
2953
568
  struct isl_print_space_data data = { 0 };
2954
568
2955
568
  data.print_dim = &print_dim_ma;
2956
568
  data.user = maff;
2957
568
  return isl_print_space(maff->space, p, 0, &data);
2958
568
}
2959
2960
static __isl_give isl_printer *print_multi_aff_isl(__isl_take isl_printer *p,
2961
  __isl_keep isl_multi_aff *maff)
2962
1
{
2963
1
  struct isl_print_space_data data = { 0 };
2964
1
2965
1
  if (!maff)
2966
0
    goto error;
2967
1
2968
1
  p = print_param_tuple(p, maff->space, &data);
2969
1
  p = isl_printer_print_str(p, "{ ");
2970
1
  p = print_multi_aff(p, maff);
2971
1
  p = isl_printer_print_str(p, " }");
2972
1
  return p;
2973
0
error:
2974
0
  isl_printer_free(p);
2975
0
  return NULL;
2976
1
}
2977
2978
__isl_give isl_printer *isl_printer_print_multi_aff(__isl_take isl_printer *p,
2979
  __isl_keep isl_multi_aff *maff)
2980
1
{
2981
1
  if (!p || !maff)
2982
0
    goto error;
2983
1
2984
1
  if (p->output_format == ISL_FORMAT_ISL)
2985
1
    return print_multi_aff_isl(p, maff);
2986
0
  isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2987
0
    goto error);
2988
0
error:
2989
0
  isl_printer_free(p);
2990
0
  return NULL;
2991
0
}
2992
2993
static __isl_give isl_printer *print_pw_multi_aff_body(
2994
  __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
2995
532
{
2996
532
  int i;
2997
532
2998
532
  if (!pma)
2999
0
    goto error;
3000
532
3001
1.09k
  
for (i = 0; 532
i < pma->n;
++i567
) {
3002
567
    isl_space *space;
3003
567
3004
567
    if (i)
3005
35
      p = isl_printer_print_str(p, "; ");
3006
567
    p = print_multi_aff(p, pma->p[i].maff);
3007
567
    space = isl_multi_aff_get_domain_space(pma->p[i].maff);
3008
567
    p = print_disjuncts(set_to_map(pma->p[i].set), space, p, 0);
3009
567
    isl_space_free(space);
3010
567
  }
3011
532
  return p;
3012
0
error:
3013
0
  isl_printer_free(p);
3014
0
  return NULL;
3015
532
}
3016
3017
static __isl_give isl_printer *print_pw_multi_aff_isl(__isl_take isl_printer *p,
3018
  __isl_keep isl_pw_multi_aff *pma)
3019
532
{
3020
532
  struct isl_print_space_data data = { 0 };
3021
532
3022
532
  if (!pma)
3023
0
    goto error;
3024
532
3025
532
  p = print_param_tuple(p, pma->dim, &data);
3026
532
  p = isl_printer_print_str(p, "{ ");
3027
532
  p = print_pw_multi_aff_body(p, pma);
3028
532
  p = isl_printer_print_str(p, " }");
3029
532
  return p;
3030
0
error:
3031
0
  isl_printer_free(p);
3032
0
  return NULL;
3033
532
}
3034
3035
/* Print the unnamed, single-dimensional piecewise multi affine expression "pma"
3036
 * to "p".
3037
 */
3038
static __isl_give isl_printer *print_unnamed_pw_multi_aff_c(
3039
  __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3040
0
{
3041
0
  int i;
3042
0
  isl_space *space;
3043
0
3044
0
  space = isl_pw_multi_aff_get_domain_space(pma);
3045
0
  for (i = 0; i < pma->n - 1; ++i) {
3046
0
    p = isl_printer_print_str(p, "(");
3047
0
    p = print_set_c(p, space, pma->p[i].set);
3048
0
    p = isl_printer_print_str(p, ") ? (");
3049
0
    p = print_aff_c(p, pma->p[i].maff->u.p[0]);
3050
0
    p = isl_printer_print_str(p, ") : ");
3051
0
  }
3052
0
  isl_space_free(space);
3053
0
3054
0
  return print_aff_c(p, pma->p[pma->n - 1].maff->u.p[0]);
3055
0
}
3056
3057
static __isl_give isl_printer *print_pw_multi_aff_c(__isl_take isl_printer *p,
3058
  __isl_keep isl_pw_multi_aff *pma)
3059
0
{
3060
0
  int n;
3061
0
  const char *name;
3062
0
3063
0
  if (!pma)
3064
0
    goto error;
3065
0
  if (pma->n < 1)
3066
0
    isl_die(p->ctx, isl_error_unsupported,
3067
0
      "cannot print empty isl_pw_multi_aff in C format",
3068
0
      goto error);
3069
0
  name = isl_pw_multi_aff_get_tuple_name(pma, isl_dim_out);
3070
0
  if (!name && isl_pw_multi_aff_dim(pma, isl_dim_out) == 1)
3071
0
    return print_unnamed_pw_multi_aff_c(p, pma);
3072
0
  if (!name)
3073
0
    isl_die(p->ctx, isl_error_unsupported,
3074
0
      "cannot print unnamed isl_pw_multi_aff in C format",
3075
0
      goto error);
3076
0
3077
0
  p = isl_printer_print_str(p, name);
3078
0
  n = isl_pw_multi_aff_dim(pma, isl_dim_out);
3079
0
  if (n != 0)
3080
0
    isl_die(p->ctx, isl_error_unsupported,
3081
0
      "not supported yet", goto error);
3082
0
3083
0
  return p;
3084
0
error:
3085
0
  isl_printer_free(p);
3086
0
  return NULL;
3087
0
}
3088
3089
__isl_give isl_printer *isl_printer_print_pw_multi_aff(
3090
  __isl_take isl_printer *p, __isl_keep isl_pw_multi_aff *pma)
3091
532
{
3092
532
  if (!p || !pma)
3093
0
    goto error;
3094
532
3095
532
  if (p->output_format == ISL_FORMAT_ISL)
3096
532
    return print_pw_multi_aff_isl(p, pma);
3097
0
  if (p->output_format == ISL_FORMAT_C)
3098
0
    return print_pw_multi_aff_c(p, pma);
3099
0
  isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3100
0
    goto error);
3101
0
error:
3102
0
  isl_printer_free(p);
3103
0
  return NULL;
3104
0
}
3105
3106
static isl_stat print_pw_multi_aff_body_wrap(__isl_take isl_pw_multi_aff *pma,
3107
  void *user)
3108
0
{
3109
0
  struct isl_union_print_data *data;
3110
0
  data = (struct isl_union_print_data *) user;
3111
0
3112
0
  if (!data->first)
3113
0
    data->p = isl_printer_print_str(data->p, "; ");
3114
0
  data->first = 0;
3115
0
3116
0
  data->p = print_pw_multi_aff_body(data->p, pma);
3117
0
  isl_pw_multi_aff_free(pma);
3118
0
3119
0
  return isl_stat_ok;
3120
0
}
3121
3122
static __isl_give isl_printer *print_union_pw_multi_aff_isl(
3123
  __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
3124
0
{
3125
0
  struct isl_union_print_data data;
3126
0
  struct isl_print_space_data space_data = { 0 };
3127
0
  isl_space *space;
3128
0
3129
0
  space = isl_union_pw_multi_aff_get_space(upma);
3130
0
  p = print_param_tuple(p, space, &space_data);
3131
0
  isl_space_free(space);
3132
0
  p = isl_printer_print_str(p, s_open_set[0]);
3133
0
  data.p = p;
3134
0
  data.first = 1;
3135
0
  isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3136
0
          &print_pw_multi_aff_body_wrap, &data);
3137
0
  p = data.p;
3138
0
  p = isl_printer_print_str(p, s_close_set[0]);
3139
0
  return p;
3140
0
}
3141
3142
__isl_give isl_printer *isl_printer_print_union_pw_multi_aff(
3143
  __isl_take isl_printer *p, __isl_keep isl_union_pw_multi_aff *upma)
3144
0
{
3145
0
  if (!p || !upma)
3146
0
    goto error;
3147
0
3148
0
  if (p->output_format == ISL_FORMAT_ISL)
3149
0
    return print_union_pw_multi_aff_isl(p, upma);
3150
0
  isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3151
0
    goto error);
3152
0
error:
3153
0
  isl_printer_free(p);
3154
0
  return NULL;
3155
0
}
3156
3157
/* Print dimension "pos" of data->space to "p".
3158
 *
3159
 * data->user is assumed to be an isl_multi_pw_aff.
3160
 *
3161
 * If the current dimension is an output dimension, then print
3162
 * the corresponding piecewise affine expression.
3163
 * Otherwise, print the name of the dimension.
3164
 */
3165
static __isl_give isl_printer *print_dim_mpa(__isl_take isl_printer *p,
3166
  struct isl_print_space_data *data, unsigned pos)
3167
26
{
3168
26
  int i;
3169
26
  int need_parens;
3170
26
  isl_space *space;
3171
26
  isl_multi_pw_aff *mpa = data->user;
3172
26
  isl_pw_aff *pa;
3173
26
3174
26
  if (data->type != isl_dim_out)
3175
14
    return print_name(data->space, p, data->type, pos, data->latex);
3176
12
3177
12
  pa = mpa->u.p[pos];
3178
12
  if (pa->n == 0)
3179
0
    return isl_printer_print_str(p, "(0 : false)");
3180
12
3181
12
  need_parens = pa->n != 1 || !isl_set_plain_is_universe(pa->p[0].set);
3182
12
  if (need_parens)
3183
11
    p = isl_printer_print_str(p, "(");
3184
12
  space = isl_multi_pw_aff_get_domain_space(mpa);
3185
24
  for (i = 0; i < pa->n; 
++i12
) {
3186
12
3187
12
    if (i)
3188
0
      p = isl_printer_print_str(p, "; ");
3189
12
    p = print_aff_body(p, space, pa->p[i].aff);
3190
12
    p = print_disjuncts(pa->p[i].set, space, p, 0);
3191
12
  }
3192
12
  isl_space_free(space);
3193
12
  if (need_parens)
3194
11
    p = isl_printer_print_str(p, ")");
3195
12
3196
12
  return p;
3197
12
}
3198
3199
/* Print "mpa" to "p" in isl format.
3200
 *
3201
 * If "mpa" is zero-dimensional and has a non-trivial explicit domain,
3202
 * then it is printed after the tuple of affine expressions.
3203
 */
3204
static __isl_give isl_printer *print_multi_pw_aff_isl(__isl_take isl_printer *p,
3205
  __isl_keep isl_multi_pw_aff *mpa)
3206
14
{
3207
14
  struct isl_print_space_data data = { 0 };
3208
14
  isl_bool has_domain;
3209
14
3210
14
  if (!mpa)
3211
0
    return isl_printer_free(p);
3212
14
3213
14
  p = print_param_tuple(p, mpa->space, &data);
3214
14
  p = isl_printer_print_str(p, "{ ");
3215
14
  data.print_dim = &print_dim_mpa;
3216
14
  data.user = mpa;
3217
14
  p = isl_print_space(mpa->space, p, 0, &data);
3218
14
  has_domain = isl_multi_pw_aff_has_non_trivial_domain(mpa);
3219
14
  if (has_domain < 0)
3220
0
    return isl_printer_free(p);
3221
14
  if (has_domain) {
3222
5
    isl_space *space;
3223
5
3224
5
    space = isl_space_domain(isl_space_copy(mpa->space));
3225
5
    p = print_disjuncts_set(mpa->u.dom, space, p, 0);
3226
5
    isl_space_free(space);
3227
5
  }
3228
14
  p = isl_printer_print_str(p, " }");
3229
14
  return p;
3230
14
}
3231
3232
__isl_give isl_printer *isl_printer_print_multi_pw_aff(
3233
  __isl_take isl_printer *p, __isl_keep isl_multi_pw_aff *mpa)
3234
14
{
3235
14
  if (!p || !mpa)
3236
0
    return isl_printer_free(p);
3237
14
3238
14
  if (p->output_format == ISL_FORMAT_ISL)
3239
14
    return print_multi_pw_aff_isl(p, mpa);
3240
0
  isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3241
0
    return isl_printer_free(p));
3242
0
}
3243
3244
/* Print dimension "pos" of data->space to "p".
3245
 *
3246
 * data->user is assumed to be an isl_multi_val.
3247
 *
3248
 * If the current dimension is an output dimension, then print
3249
 * the corresponding value.  Otherwise, print the name of the dimension.
3250
 */
3251
static __isl_give isl_printer *print_dim_mv(__isl_take isl_printer *p,
3252
  struct isl_print_space_data *data, unsigned pos)
3253
0
{
3254
0
  isl_multi_val *mv = data->user;
3255
0
3256
0
  if (data->type == isl_dim_out)
3257
0
    return isl_printer_print_val(p, mv->u.p[pos]);
3258
0
  else
3259
0
    return print_name(data->space, p, data->type, pos, data->latex);
3260
0
}
3261
3262
/* Print the isl_multi_val "mv" to "p" in isl format.
3263
 */
3264
static __isl_give isl_printer *print_multi_val_isl(__isl_take isl_printer *p,
3265
  __isl_keep isl_multi_val *mv)
3266
0
{
3267
0
  struct isl_print_space_data data = { 0 };
3268
0
3269
0
  if (!mv)
3270
0
    return isl_printer_free(p);
3271
0
3272
0
  p = print_param_tuple(p, mv->space, &data);
3273
0
  p = isl_printer_print_str(p, "{ ");
3274
0
  data.print_dim = &print_dim_mv;
3275
0
  data.user = mv;
3276
0
  p = isl_print_space(mv->space, p, 0, &data);
3277
0
  p = isl_printer_print_str(p, " }");
3278
0
  return p;
3279
0
}
3280
3281
/* Print the isl_multi_val "mv" to "p".
3282
 *
3283
 * Currently only supported in isl format.
3284
 */
3285
__isl_give isl_printer *isl_printer_print_multi_val(
3286
  __isl_take isl_printer *p, __isl_keep isl_multi_val *mv)
3287
0
{
3288
0
  if (!p || !mv)
3289
0
    return isl_printer_free(p);
3290
0
3291
0
  if (p->output_format == ISL_FORMAT_ISL)
3292
0
    return print_multi_val_isl(p, mv);
3293
0
  isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
3294
0
    return isl_printer_free(p));
3295
0
}
3296
3297
/* Print dimension "pos" of data->space to "p".
3298
 *
3299
 * data->user is assumed to be an isl_multi_union_pw_aff.
3300
 *
3301
 * The current dimension is necessarily a set dimension, so
3302
 * we print the corresponding isl_union_pw_aff, including
3303
 * the braces.
3304
 */
3305
static __isl_give isl_printer *print_union_pw_aff_dim(__isl_take isl_printer *p,
3306
  struct isl_print_space_data *data, unsigned pos)
3307
2
{
3308
2
  isl_multi_union_pw_aff *mupa = data->user;
3309
2
  isl_union_pw_aff *upa;
3310
2
3311
2
  upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, pos);
3312
2
  p = print_union_pw_aff_body(p, upa);
3313
2
  isl_union_pw_aff_free(upa);
3314
2
3315
2
  return p;
3316
2
}
3317
3318
/* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3319
 *
3320
 * If "mupa" is zero-dimensional and has a non-trivial explicit domain,
3321
 * then it is printed after the tuple of affine expressions.
3322
 * In order to clarify that this domain belongs to the expression,
3323
 * the tuple along with the domain are placed inside parentheses.
3324
 * If "mupa" has any parameters, then the opening parenthesis
3325
 * appears after the parameter declarations.
3326
 */
3327
static __isl_give isl_printer *print_multi_union_pw_aff_isl(
3328
  __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3329
13
{
3330
13
  struct isl_print_space_data data = { 0 };
3331
13
  isl_bool has_domain;
3332
13
  isl_space *space;
3333
13
3334
13
  if (!mupa)
3335
0
    return isl_printer_free(p);
3336
13
  has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
3337
13
  if (has_domain < 0)
3338
0
    return isl_printer_free(p);
3339
13
3340
13
  space = isl_multi_union_pw_aff_get_space(mupa);
3341
13
  p = print_param_tuple(p, space, &data);
3342
13
3343
13
  if (has_domain)
3344
7
    p = isl_printer_print_str(p, "(");
3345
13
3346
13
  data.print_dim = &print_union_pw_aff_dim;
3347
13
  data.user = mupa;
3348
13
3349
13
  p = isl_print_space(space, p, 0, &data);
3350
13
  isl_space_free(space);
3351
13
3352
13
  if (has_domain) {
3353
7
    p = isl_printer_print_str(p, " : ");
3354
7
    p = isl_printer_print_union_set_isl_body(p, mupa->u.dom);
3355
7
    p = isl_printer_print_str(p, ")");
3356
7
  }
3357
13
3358
13
  return p;
3359
13
}
3360
3361
/* Print the isl_multi_union_pw_aff "mupa" to "p" in isl format.
3362
 *
3363
 * We currently only support an isl format.
3364
 */
3365
__isl_give isl_printer *isl_printer_print_multi_union_pw_aff(
3366
  __isl_take isl_printer *p, __isl_keep isl_multi_union_pw_aff *mupa)
3367
13
{
3368
13
  if (!p || !mupa)
3369
0
    return isl_printer_free(p);
3370
13
3371
13
  if (p->output_format == ISL_FORMAT_ISL)
3372
13
    return print_multi_union_pw_aff_isl(p, mupa);
3373
0
  isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
3374
0
    "unsupported output format", return isl_printer_free(p));
3375
0
}