Coverage Report

Created: 2018-02-20 23:11

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