Coverage Report

Created: 2018-06-19 22:08

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/External/isl/isl_aff.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2011      INRIA Saclay
3
 * Copyright 2011      Sven Verdoolaege
4
 * Copyright 2012-2014 Ecole Normale Superieure
5
 * Copyright 2014      INRIA Rocquencourt
6
 *
7
 * Use of this software is governed by the MIT license
8
 *
9
 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10
 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11
 * 91893 Orsay, France
12
 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13
 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
14
 * B.P. 105 - 78153 Le Chesnay, France
15
 */
16
17
#include <isl_ctx_private.h>
18
#include <isl_map_private.h>
19
#include <isl_union_map_private.h>
20
#include <isl_aff_private.h>
21
#include <isl_space_private.h>
22
#include <isl_local_space_private.h>
23
#include <isl_vec_private.h>
24
#include <isl_mat_private.h>
25
#include <isl/id.h>
26
#include <isl/constraint.h>
27
#include <isl_seq.h>
28
#include <isl/set.h>
29
#include <isl_val_private.h>
30
#include <isl_point_private.h>
31
#include <isl_config.h>
32
33
#undef BASE
34
#define BASE aff
35
36
#include <isl_list_templ.c>
37
38
#undef BASE
39
#define BASE pw_aff
40
41
#include <isl_list_templ.c>
42
43
#undef BASE
44
#define BASE pw_multi_aff
45
46
#include <isl_list_templ.c>
47
48
#undef BASE
49
#define BASE union_pw_aff
50
51
#include <isl_list_templ.c>
52
53
#undef BASE
54
#define BASE union_pw_multi_aff
55
56
#include <isl_list_templ.c>
57
58
__isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
59
  __isl_take isl_vec *v)
60
338k
{
61
338k
  isl_aff *aff;
62
338k
63
338k
  if (!ls || !v)
64
0
    goto error;
65
338k
66
338k
  aff = isl_calloc_type(v->ctx, struct isl_aff);
67
338k
  if (!aff)
68
0
    goto error;
69
338k
70
338k
  aff->ref = 1;
71
338k
  aff->ls = ls;
72
338k
  aff->v = v;
73
338k
74
338k
  return aff;
75
0
error:
76
0
  isl_local_space_free(ls);
77
0
  isl_vec_free(v);
78
0
  return NULL;
79
338k
}
80
81
__isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
82
176k
{
83
176k
  isl_ctx *ctx;
84
176k
  isl_vec *v;
85
176k
  unsigned total;
86
176k
87
176k
  if (!ls)
88
3
    return NULL;
89
176k
90
176k
  ctx = isl_local_space_get_ctx(ls);
91
176k
  if (!isl_local_space_divs_known(ls))
92
176k
    
isl_die0
(ctx, isl_error_invalid, "local space has unknown divs",
93
176k
      goto error);
94
176k
  if (!isl_local_space_is_set(ls))
95
176k
    
isl_die0
(ctx, isl_error_invalid,
96
176k
      "domain of affine expression should be a set",
97
176k
      goto error);
98
176k
99
176k
  total = isl_local_space_dim(ls, isl_dim_all);
100
176k
  v = isl_vec_alloc(ctx, 1 + 1 + total);
101
176k
  return isl_aff_alloc_vec(ls, v);
102
0
error:
103
0
  isl_local_space_free(ls);
104
0
  return NULL;
105
176k
}
106
107
__isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
108
67.3k
{
109
67.3k
  isl_aff *aff;
110
67.3k
111
67.3k
  aff = isl_aff_alloc(ls);
112
67.3k
  if (!aff)
113
3
    return NULL;
114
67.2k
115
67.2k
  isl_int_set_si(aff->v->el[0], 1);
116
67.2k
  isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
117
67.2k
118
67.2k
  return aff;
119
67.2k
}
120
121
/* Return a piecewise affine expression defined on the specified domain
122
 * that is equal to zero.
123
 */
124
__isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
125
642
{
126
642
  return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
127
642
}
128
129
/* Return an affine expression defined on the specified domain
130
 * that represents NaN.
131
 */
132
__isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
133
381
{
134
381
  isl_aff *aff;
135
381
136
381
  aff = isl_aff_alloc(ls);
137
381
  if (!aff)
138
0
    return NULL;
139
381
140
381
  isl_seq_clr(aff->v->el, aff->v->size);
141
381
142
381
  return aff;
143
381
}
144
145
/* Return a piecewise affine expression defined on the specified domain
146
 * that represents NaN.
147
 */
148
__isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
149
32
{
150
32
  return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
151
32
}
152
153
/* Return an affine expression that is equal to "val" on
154
 * domain local space "ls".
155
 */
156
__isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
157
  __isl_take isl_val *val)
158
28.7k
{
159
28.7k
  isl_aff *aff;
160
28.7k
161
28.7k
  if (!ls || !val)
162
0
    goto error;
163
28.7k
  if (!isl_val_is_rat(val))
164
28.7k
    
isl_die0
(isl_val_get_ctx(val), isl_error_invalid,
165
28.7k
      "expecting rational value", goto error);
166
28.7k
167
28.7k
  aff = isl_aff_alloc(isl_local_space_copy(ls));
168
28.7k
  if (!aff)
169
0
    goto error;
170
28.7k
171
28.7k
  isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
172
28.7k
  isl_int_set(aff->v->el[1], val->n);
173
28.7k
  isl_int_set(aff->v->el[0], val->d);
174
28.7k
175
28.7k
  isl_local_space_free(ls);
176
28.7k
  isl_val_free(val);
177
28.7k
  return aff;
178
0
error:
179
0
  isl_local_space_free(ls);
180
0
  isl_val_free(val);
181
0
  return NULL;
182
28.7k
}
183
184
/* Return an affine expression that is equal to the specified dimension
185
 * in "ls".
186
 */
187
__isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
188
  enum isl_dim_type type, unsigned pos)
189
29.5k
{
190
29.5k
  isl_space *space;
191
29.5k
  isl_aff *aff;
192
29.5k
193
29.5k
  if (!ls)
194
0
    return NULL;
195
29.5k
196
29.5k
  space = isl_local_space_get_space(ls);
197
29.5k
  if (!space)
198
0
    goto error;
199
29.5k
  if (isl_space_is_map(space))
200
29.5k
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
201
29.5k
      "expecting (parameter) set space", goto error);
202
29.5k
  if (pos >= isl_local_space_dim(ls, type))
203
29.5k
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
204
29.5k
      "position out of bounds", goto error);
205
29.5k
206
29.5k
  isl_space_free(space);
207
29.5k
  aff = isl_aff_alloc(ls);
208
29.5k
  if (!aff)
209
0
    return NULL;
210
29.5k
211
29.5k
  pos += isl_local_space_offset(aff->ls, type);
212
29.5k
213
29.5k
  isl_int_set_si(aff->v->el[0], 1);
214
29.5k
  isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
215
29.5k
  isl_int_set_si(aff->v->el[1 + pos], 1);
216
29.5k
217
29.5k
  return aff;
218
0
error:
219
0
  isl_local_space_free(ls);
220
0
  isl_space_free(space);
221
0
  return NULL;
222
29.5k
}
223
224
/* Return a piecewise affine expression that is equal to
225
 * the specified dimension in "ls".
226
 */
227
__isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
228
  enum isl_dim_type type, unsigned pos)
229
546
{
230
546
  return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
231
546
}
232
233
/* Return an affine expression that is equal to the parameter
234
 * in the domain space "space" with identifier "id".
235
 */
236
__isl_give isl_aff *isl_aff_param_on_domain_space_id(
237
  __isl_take isl_space *space, __isl_take isl_id *id)
238
1
{
239
1
  int pos;
240
1
  isl_local_space *ls;
241
1
242
1
  if (!space || !id)
243
0
    goto error;
244
1
  pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
245
1
  if (pos < 0)
246
1
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
247
1
      "parameter not found in space", goto error);
248
1
  isl_id_free(id);
249
1
  ls = isl_local_space_from_space(space);
250
1
  return isl_aff_var_on_domain(ls, isl_dim_param, pos);
251
0
error:
252
0
  isl_space_free(space);
253
0
  isl_id_free(id);
254
0
  return NULL;
255
1
}
256
257
__isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
258
1.28M
{
259
1.28M
  if (!aff)
260
6
    return NULL;
261
1.28M
262
1.28M
  aff->ref++;
263
1.28M
  return aff;
264
1.28M
}
265
266
__isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
267
162k
{
268
162k
  if (!aff)
269
0
    return NULL;
270
162k
271
162k
  return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
272
162k
         isl_vec_copy(aff->v));
273
162k
}
274
275
__isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
276
339k
{
277
339k
  if (!aff)
278
0
    return NULL;
279
339k
280
339k
  if (aff->ref == 1)
281
177k
    return aff;
282
162k
  aff->ref--;
283
162k
  return isl_aff_dup(aff);
284
162k
}
285
286
__isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
287
2.40M
{
288
2.40M
  if (!aff)
289
942k
    return NULL;
290
1.46M
291
1.46M
  if (--aff->ref > 0)
292
1.12M
    return NULL;
293
338k
294
338k
  isl_local_space_free(aff->ls);
295
338k
  isl_vec_free(aff->v);
296
338k
297
338k
  free(aff);
298
338k
299
338k
  return NULL;
300
338k
}
301
302
isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
303
1.64M
{
304
1.64M
  return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
305
1.64M
}
306
307
/* Return a hash value that digests "aff".
308
 */
309
uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
310
0
{
311
0
  uint32_t hash, ls_hash, v_hash;
312
0
313
0
  if (!aff)
314
0
    return 0;
315
0
316
0
  hash = isl_hash_init();
317
0
  ls_hash = isl_local_space_get_hash(aff->ls);
318
0
  isl_hash_hash(hash, ls_hash);
319
0
  v_hash = isl_vec_get_hash(aff->v);
320
0
  isl_hash_hash(hash, v_hash);
321
0
322
0
  return hash;
323
0
}
324
325
/* Externally, an isl_aff has a map space, but internally, the
326
 * ls field corresponds to the domain of that space.
327
 */
328
int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
329
336k
{
330
336k
  if (!aff)
331
0
    return 0;
332
336k
  if (type == isl_dim_out)
333
0
    return 1;
334
336k
  if (type == isl_dim_in)
335
33.4k
    type = isl_dim_set;
336
336k
  return isl_local_space_dim(aff->ls, type);
337
336k
}
338
339
/* Return the position of the dimension of the given type and name
340
 * in "aff".
341
 * Return -1 if no such dimension can be found.
342
 */
343
int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
344
  const char *name)
345
0
{
346
0
  if (!aff)
347
0
    return -1;
348
0
  if (type == isl_dim_out)
349
0
    return -1;
350
0
  if (type == isl_dim_in)
351
0
    type = isl_dim_set;
352
0
  return isl_local_space_find_dim_by_name(aff->ls, type, name);
353
0
}
354
355
/* Return the domain space of "aff".
356
 */
357
static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
358
1.95M
{
359
1.95M
  return aff ? 
isl_local_space_peek_space(aff->ls)1.95M
: NULL;
360
1.95M
}
361
362
__isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
363
1.95M
{
364
1.95M
  return isl_space_copy(isl_aff_peek_domain_space(aff));
365
1.95M
}
366
367
__isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
368
416k
{
369
416k
  isl_space *space;
370
416k
  if (!aff)
371
0
    return NULL;
372
416k
  space = isl_local_space_get_space(aff->ls);
373
416k
  space = isl_space_from_domain(space);
374
416k
  space = isl_space_add_dims(space, isl_dim_out, 1);
375
416k
  return space;
376
416k
}
377
378
__isl_give isl_local_space *isl_aff_get_domain_local_space(
379
  __isl_keep isl_aff *aff)
380
80.6k
{
381
80.6k
  return aff ? isl_local_space_copy(aff->ls) : NULL;
382
80.6k
}
383
384
__isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
385
77.0k
{
386
77.0k
  isl_local_space *ls;
387
77.0k
  if (!aff)
388
0
    return NULL;
389
77.0k
  ls = isl_local_space_copy(aff->ls);
390
77.0k
  ls = isl_local_space_from_domain(ls);
391
77.0k
  ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
392
77.0k
  return ls;
393
77.0k
}
394
395
/* Return the local space of the domain of "aff".
396
 * This may be either a copy or the local space itself
397
 * if there is only one reference to "aff".
398
 * This allows the local space to be modified inplace
399
 * if both the expression and its local space have only a single reference.
400
 * The caller is not allowed to modify "aff" between this call and
401
 * a subsequent call to isl_aff_restore_domain_local_space.
402
 * The only exception is that isl_aff_free can be called instead.
403
 */
404
__isl_give isl_local_space *isl_aff_take_domain_local_space(
405
  __isl_keep isl_aff *aff)
406
0
{
407
0
  isl_local_space *ls;
408
0
409
0
  if (!aff)
410
0
    return NULL;
411
0
  if (aff->ref != 1)
412
0
    return isl_aff_get_domain_local_space(aff);
413
0
  ls = aff->ls;
414
0
  aff->ls = NULL;
415
0
  return ls;
416
0
}
417
418
/* Set the local space of the domain of "aff" to "ls",
419
 * where the local space of "aff" may be missing
420
 * due to a preceding call to isl_aff_take_domain_local_space.
421
 * However, in this case, "aff" only has a single reference and
422
 * then the call to isl_aff_cow has no effect.
423
 */
424
__isl_give isl_aff *isl_aff_restore_domain_local_space(
425
  __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
426
0
{
427
0
  if (!aff || !ls)
428
0
    goto error;
429
0
430
0
  if (aff->ls == ls) {
431
0
    isl_local_space_free(ls);
432
0
    return aff;
433
0
  }
434
0
435
0
  aff = isl_aff_cow(aff);
436
0
  if (!aff)
437
0
    goto error;
438
0
  isl_local_space_free(aff->ls);
439
0
  aff->ls = ls;
440
0
441
0
  return aff;
442
0
error:
443
0
  isl_aff_free(aff);
444
0
  isl_local_space_free(ls);
445
0
  return NULL;
446
0
}
447
448
/* Externally, an isl_aff has a map space, but internally, the
449
 * ls field corresponds to the domain of that space.
450
 */
451
const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
452
  enum isl_dim_type type, unsigned pos)
453
0
{
454
0
  if (!aff)
455
0
    return NULL;
456
0
  if (type == isl_dim_out)
457
0
    return NULL;
458
0
  if (type == isl_dim_in)
459
0
    type = isl_dim_set;
460
0
  return isl_local_space_get_dim_name(aff->ls, type, pos);
461
0
}
462
463
__isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
464
  __isl_take isl_space *dim)
465
84.6k
{
466
84.6k
  aff = isl_aff_cow(aff);
467
84.6k
  if (!aff || !dim)
468
0
    goto error;
469
84.6k
470
84.6k
  aff->ls = isl_local_space_reset_space(aff->ls, dim);
471
84.6k
  if (!aff->ls)
472
0
    return isl_aff_free(aff);
473
84.6k
474
84.6k
  return aff;
475
0
error:
476
0
  isl_aff_free(aff);
477
0
  isl_space_free(dim);
478
0
  return NULL;
479
84.6k
}
480
481
/* Reset the space of "aff".  This function is called from isl_pw_templ.c
482
 * and doesn't know if the space of an element object is represented
483
 * directly or through its domain.  It therefore passes along both.
484
 */
485
__isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
486
  __isl_take isl_space *space, __isl_take isl_space *domain)
487
68.1k
{
488
68.1k
  isl_space_free(space);
489
68.1k
  return isl_aff_reset_domain_space(aff, domain);
490
68.1k
}
491
492
/* Reorder the coefficients of the affine expression based
493
 * on the given reordering.
494
 * The reordering r is assumed to have been extended with the local
495
 * variables.
496
 */
497
static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
498
  __isl_take isl_reordering *r, int n_div)
499
10.1k
{
500
10.1k
  isl_space *space;
501
10.1k
  isl_vec *res;
502
10.1k
  int i;
503
10.1k
504
10.1k
  if (!vec || !r)
505
0
    goto error;
506
10.1k
507
10.1k
  space = isl_reordering_peek_space(r);
508
10.1k
  res = isl_vec_alloc(vec->ctx,
509
10.1k
          2 + isl_space_dim(space, isl_dim_all) + n_div);
510
10.1k
  if (!res)
511
0
    goto error;
512
10.1k
  isl_seq_cpy(res->el, vec->el, 2);
513
10.1k
  isl_seq_clr(res->el + 2, res->size - 2);
514
29.4k
  for (i = 0; i < r->len; 
++i19.3k
)
515
19.3k
    isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
516
10.1k
517
10.1k
  isl_reordering_free(r);
518
10.1k
  isl_vec_free(vec);
519
10.1k
  return res;
520
0
error:
521
0
  isl_vec_free(vec);
522
0
  isl_reordering_free(r);
523
0
  return NULL;
524
10.1k
}
525
526
/* Reorder the dimensions of the domain of "aff" according
527
 * to the given reordering.
528
 */
529
__isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
530
  __isl_take isl_reordering *r)
531
10.1k
{
532
10.1k
  aff = isl_aff_cow(aff);
533
10.1k
  if (!aff)
534
0
    goto error;
535
10.1k
536
10.1k
  r = isl_reordering_extend(r, aff->ls->div->n_row);
537
10.1k
  aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
538
10.1k
        aff->ls->div->n_row);
539
10.1k
  aff->ls = isl_local_space_realign(aff->ls, r);
540
10.1k
541
10.1k
  if (!aff->v || !aff->ls)
542
0
    return isl_aff_free(aff);
543
10.1k
544
10.1k
  return aff;
545
0
error:
546
0
  isl_aff_free(aff);
547
0
  isl_reordering_free(r);
548
0
  return NULL;
549
10.1k
}
550
551
__isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
552
  __isl_take isl_space *model)
553
1
{
554
1
  isl_bool equal_params;
555
1
556
1
  if (!aff || !model)
557
0
    goto error;
558
1
559
1
  equal_params = isl_space_has_equal_params(aff->ls->dim, model);
560
1
  if (equal_params < 0)
561
0
    goto error;
562
1
  if (!equal_params) {
563
0
    isl_reordering *exp;
564
0
565
0
    exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
566
0
    exp = isl_reordering_extend_space(exp,
567
0
          isl_aff_get_domain_space(aff));
568
0
    aff = isl_aff_realign_domain(aff, exp);
569
0
  }
570
1
571
1
  isl_space_free(model);
572
1
  return aff;
573
0
error:
574
0
  isl_space_free(model);
575
0
  isl_aff_free(aff);
576
0
  return NULL;
577
1
}
578
579
/* Is "aff" obviously equal to zero?
580
 *
581
 * If the denominator is zero, then "aff" is not equal to zero.
582
 */
583
isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
584
1
{
585
1
  if (!aff)
586
0
    return isl_bool_error;
587
1
588
1
  if (isl_int_is_zero(aff->v->el[0]))
589
1
    
return isl_bool_false0
;
590
1
  return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
591
1
}
592
593
/* Does "aff" represent NaN?
594
 */
595
isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
596
540k
{
597
540k
  if (!aff)
598
0
    return isl_bool_error;
599
540k
600
540k
  return isl_seq_first_non_zero(aff->v->el, 2) < 0;
601
540k
}
602
603
/* Are "aff1" and "aff2" obviously equal?
604
 *
605
 * NaN is not equal to anything, not even to another NaN.
606
 */
607
isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
608
  __isl_keep isl_aff *aff2)
609
7.80k
{
610
7.80k
  isl_bool equal;
611
7.80k
612
7.80k
  if (!aff1 || !aff2)
613
0
    return isl_bool_error;
614
7.80k
615
7.80k
  if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
616
0
    return isl_bool_false;
617
7.80k
618
7.80k
  equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
619
7.80k
  if (equal < 0 || !equal)
620
1.39k
    return equal;
621
6.40k
622
6.40k
  return isl_vec_is_equal(aff1->v, aff2->v);
623
6.40k
}
624
625
/* Return the common denominator of "aff" in "v".
626
 *
627
 * We cannot return anything meaningful in case of a NaN.
628
 */
629
isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
630
0
{
631
0
  if (!aff)
632
0
    return isl_stat_error;
633
0
  if (isl_aff_is_nan(aff))
634
0
    isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
635
0
      "cannot get denominator of NaN", return isl_stat_error);
636
0
  isl_int_set(*v, aff->v->el[0]);
637
0
  return isl_stat_ok;
638
0
}
639
640
/* Return the common denominator of "aff".
641
 */
642
__isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
643
8.36k
{
644
8.36k
  isl_ctx *ctx;
645
8.36k
646
8.36k
  if (!aff)
647
0
    return NULL;
648
8.36k
649
8.36k
  ctx = isl_aff_get_ctx(aff);
650
8.36k
  if (isl_aff_is_nan(aff))
651
0
    return isl_val_nan(ctx);
652
8.36k
  return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
653
8.36k
}
654
655
/* Return the constant term of "aff".
656
 */
657
__isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
658
9.61k
{
659
9.61k
  isl_ctx *ctx;
660
9.61k
  isl_val *v;
661
9.61k
662
9.61k
  if (!aff)
663
0
    return NULL;
664
9.61k
665
9.61k
  ctx = isl_aff_get_ctx(aff);
666
9.61k
  if (isl_aff_is_nan(aff))
667
0
    return isl_val_nan(ctx);
668
9.61k
  v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
669
9.61k
  return isl_val_normalize(v);
670
9.61k
}
671
672
/* Return the coefficient of the variable of type "type" at position "pos"
673
 * of "aff".
674
 */
675
__isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
676
  enum isl_dim_type type, int pos)
677
54.4k
{
678
54.4k
  isl_ctx *ctx;
679
54.4k
  isl_val *v;
680
54.4k
681
54.4k
  if (!aff)
682
0
    return NULL;
683
54.4k
684
54.4k
  ctx = isl_aff_get_ctx(aff);
685
54.4k
  if (type == isl_dim_out)
686
54.4k
    
isl_die0
(ctx, isl_error_invalid,
687
54.4k
      "output/set dimension does not have a coefficient",
688
54.4k
      return NULL);
689
54.4k
  if (type == isl_dim_in)
690
47.7k
    type = isl_dim_set;
691
54.4k
692
54.4k
  if (pos >= isl_local_space_dim(aff->ls, type))
693
54.4k
    
isl_die0
(ctx, isl_error_invalid,
694
54.4k
      "position out of bounds", return NULL);
695
54.4k
696
54.4k
  if (isl_aff_is_nan(aff))
697
0
    return isl_val_nan(ctx);
698
54.4k
  pos += isl_local_space_offset(aff->ls, type);
699
54.4k
  v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
700
54.4k
  return isl_val_normalize(v);
701
54.4k
}
702
703
/* Return the sign of the coefficient of the variable of type "type"
704
 * at position "pos" of "aff".
705
 */
706
int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
707
  int pos)
708
86
{
709
86
  isl_ctx *ctx;
710
86
711
86
  if (!aff)
712
0
    return 0;
713
86
714
86
  ctx = isl_aff_get_ctx(aff);
715
86
  if (type == isl_dim_out)
716
86
    
isl_die0
(ctx, isl_error_invalid,
717
86
      "output/set dimension does not have a coefficient",
718
86
      return 0);
719
86
  if (type == isl_dim_in)
720
63
    type = isl_dim_set;
721
86
722
86
  if (pos >= isl_local_space_dim(aff->ls, type))
723
86
    
isl_die0
(ctx, isl_error_invalid,
724
86
      "position out of bounds", return 0);
725
86
726
86
  pos += isl_local_space_offset(aff->ls, type);
727
86
  return isl_int_sgn(aff->v->el[1 + pos]);
728
86
}
729
730
/* Replace the numerator of the constant term of "aff" by "v".
731
 *
732
 * A NaN is unaffected by this operation.
733
 */
734
__isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
735
1.04k
{
736
1.04k
  if (!aff)
737
0
    return NULL;
738
1.04k
  if (isl_aff_is_nan(aff))
739
0
    return aff;
740
1.04k
  aff = isl_aff_cow(aff);
741
1.04k
  if (!aff)
742
0
    return NULL;
743
1.04k
744
1.04k
  aff->v = isl_vec_cow(aff->v);
745
1.04k
  if (!aff->v)
746
0
    return isl_aff_free(aff);
747
1.04k
748
1.04k
  isl_int_set(aff->v->el[1], v);
749
1.04k
750
1.04k
  return aff;
751
1.04k
}
752
753
/* Replace the constant term of "aff" by "v".
754
 *
755
 * A NaN is unaffected by this operation.
756
 */
757
__isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
758
  __isl_take isl_val *v)
759
36
{
760
36
  if (!aff || !v)
761
0
    goto error;
762
36
763
36
  if (isl_aff_is_nan(aff)) {
764
0
    isl_val_free(v);
765
0
    return aff;
766
0
  }
767
36
768
36
  if (!isl_val_is_rat(v))
769
36
    
isl_die0
(isl_aff_get_ctx(aff), isl_error_invalid,
770
36
      "expecting rational value", goto error);
771
36
772
36
  if (isl_int_eq(aff->v->el[1], v->n) &&
773
36
      
isl_int_eq34
(aff->v->el[0], v->d)) {
774
34
    isl_val_free(v);
775
34
    return aff;
776
34
  }
777
2
778
2
  aff = isl_aff_cow(aff);
779
2
  if (!aff)
780
0
    goto error;
781
2
  aff->v = isl_vec_cow(aff->v);
782
2
  if (!aff->v)
783
0
    goto error;
784
2
785
2
  if (isl_int_eq(aff->v->el[0], v->d)) {
786
2
    isl_int_set(aff->v->el[1], v->n);
787
2
  } else 
if (0
isl_int_is_one0
(v->d)) {
788
0
    isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
789
0
  } else {
790
0
    isl_seq_scale(aff->v->el + 1,
791
0
        aff->v->el + 1, v->d, aff->v->size - 1);
792
0
    isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
793
0
    isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
794
0
    aff->v = isl_vec_normalize(aff->v);
795
0
    if (!aff->v)
796
0
      goto error;
797
2
  }
798
2
799
2
  isl_val_free(v);
800
2
  return aff;
801
0
error:
802
0
  isl_aff_free(aff);
803
0
  isl_val_free(v);
804
0
  return NULL;
805
2
}
806
807
/* Add "v" to the constant term of "aff".
808
 *
809
 * A NaN is unaffected by this operation.
810
 */
811
__isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
812
23.9k
{
813
23.9k
  if (isl_int_is_zero(v))
814
23.9k
    
return aff4.64k
;
815
19.3k
816
19.3k
  if (!aff)
817
3
    return NULL;
818
19.3k
  if (isl_aff_is_nan(aff))
819
0
    return aff;
820
19.3k
  aff = isl_aff_cow(aff);
821
19.3k
  if (!aff)
822
0
    return NULL;
823
19.3k
824
19.3k
  aff->v = isl_vec_cow(aff->v);
825
19.3k
  if (!aff->v)
826
0
    return isl_aff_free(aff);
827
19.3k
828
19.3k
  isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
829
19.3k
830
19.3k
  return aff;
831
19.3k
}
832
833
/* Add "v" to the constant term of "aff".
834
 *
835
 * A NaN is unaffected by this operation.
836
 */
837
__isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
838
  __isl_take isl_val *v)
839
63
{
840
63
  if (!aff || !v)
841
0
    goto error;
842
63
843
63
  if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
844
0
    isl_val_free(v);
845
0
    return aff;
846
0
  }
847
63
848
63
  if (!isl_val_is_rat(v))
849
63
    
isl_die0
(isl_aff_get_ctx(aff), isl_error_invalid,
850
63
      "expecting rational value", goto error);
851
63
852
63
  aff = isl_aff_cow(aff);
853
63
  if (!aff)
854
0
    goto error;
855
63
856
63
  aff->v = isl_vec_cow(aff->v);
857
63
  if (!aff->v)
858
0
    goto error;
859
63
860
63
  if (isl_int_is_one(v->d)) {
861
63
    isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
862
63
  } else 
if (0
isl_int_eq0
(aff->v->el[0], v->d)) {
863
0
    isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
864
0
    aff->v = isl_vec_normalize(aff->v);
865
0
    if (!aff->v)
866
0
      goto error;
867
0
  } else {
868
0
    isl_seq_scale(aff->v->el + 1,
869
0
        aff->v->el + 1, v->d, aff->v->size - 1);
870
0
    isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
871
0
    isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
872
0
    aff->v = isl_vec_normalize(aff->v);
873
0
    if (!aff->v)
874
0
      goto error;
875
63
  }
876
63
877
63
  isl_val_free(v);
878
63
  return aff;
879
0
error:
880
0
  isl_aff_free(aff);
881
0
  isl_val_free(v);
882
0
  return NULL;
883
63
}
884
885
__isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
886
17.9k
{
887
17.9k
  isl_int t;
888
17.9k
889
17.9k
  isl_int_init(t);
890
17.9k
  isl_int_set_si(t, v);
891
17.9k
  aff = isl_aff_add_constant(aff, t);
892
17.9k
  isl_int_clear(t);
893
17.9k
894
17.9k
  return aff;
895
17.9k
}
896
897
/* Add "v" to the numerator of the constant term of "aff".
898
 *
899
 * A NaN is unaffected by this operation.
900
 */
901
__isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
902
108
{
903
108
  if (isl_int_is_zero(v))
904
108
    
return aff0
;
905
108
906
108
  if (!aff)
907
0
    return NULL;
908
108
  if (isl_aff_is_nan(aff))
909
0
    return aff;
910
108
  aff = isl_aff_cow(aff);
911
108
  if (!aff)
912
0
    return NULL;
913
108
914
108
  aff->v = isl_vec_cow(aff->v);
915
108
  if (!aff->v)
916
0
    return isl_aff_free(aff);
917
108
918
108
  isl_int_add(aff->v->el[1], aff->v->el[1], v);
919
108
920
108
  return aff;
921
108
}
922
923
/* Add "v" to the numerator of the constant term of "aff".
924
 *
925
 * A NaN is unaffected by this operation.
926
 */
927
__isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
928
108
{
929
108
  isl_int t;
930
108
931
108
  if (v == 0)
932
0
    return aff;
933
108
934
108
  isl_int_init(t);
935
108
  isl_int_set_si(t, v);
936
108
  aff = isl_aff_add_constant_num(aff, t);
937
108
  isl_int_clear(t);
938
108
939
108
  return aff;
940
108
}
941
942
/* Replace the numerator of the constant term of "aff" by "v".
943
 *
944
 * A NaN is unaffected by this operation.
945
 */
946
__isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
947
942
{
948
942
  if (!aff)
949
0
    return NULL;
950
942
  if (isl_aff_is_nan(aff))
951
0
    return aff;
952
942
  aff = isl_aff_cow(aff);
953
942
  if (!aff)
954
0
    return NULL;
955
942
956
942
  aff->v = isl_vec_cow(aff->v);
957
942
  if (!aff->v)
958
0
    return isl_aff_free(aff);
959
942
960
942
  isl_int_set_si(aff->v->el[1], v);
961
942
962
942
  return aff;
963
942
}
964
965
/* Replace the numerator of the coefficient of the variable of type "type"
966
 * at position "pos" of "aff" by "v".
967
 *
968
 * A NaN is unaffected by this operation.
969
 */
970
__isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
971
  enum isl_dim_type type, int pos, isl_int v)
972
2.44k
{
973
2.44k
  if (!aff)
974
0
    return NULL;
975
2.44k
976
2.44k
  if (type == isl_dim_out)
977
2.44k
    
isl_die0
(aff->v->ctx, isl_error_invalid,
978
2.44k
      "output/set dimension does not have a coefficient",
979
2.44k
      return isl_aff_free(aff));
980
2.44k
  if (type == isl_dim_in)
981
1.89k
    type = isl_dim_set;
982
2.44k
983
2.44k
  if (pos >= isl_local_space_dim(aff->ls, type))
984
2.44k
    
isl_die0
(aff->v->ctx, isl_error_invalid,
985
2.44k
      "position out of bounds", return isl_aff_free(aff));
986
2.44k
987
2.44k
  if (isl_aff_is_nan(aff))
988
0
    return aff;
989
2.44k
  aff = isl_aff_cow(aff);
990
2.44k
  if (!aff)
991
0
    return NULL;
992
2.44k
993
2.44k
  aff->v = isl_vec_cow(aff->v);
994
2.44k
  if (!aff->v)
995
0
    return isl_aff_free(aff);
996
2.44k
997
2.44k
  pos += isl_local_space_offset(aff->ls, type);
998
2.44k
  isl_int_set(aff->v->el[1 + pos], v);
999
2.44k
1000
2.44k
  return aff;
1001
2.44k
}
1002
1003
/* Replace the numerator of the coefficient of the variable of type "type"
1004
 * at position "pos" of "aff" by "v".
1005
 *
1006
 * A NaN is unaffected by this operation.
1007
 */
1008
__isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1009
  enum isl_dim_type type, int pos, int v)
1010
3.21k
{
1011
3.21k
  if (!aff)
1012
0
    return NULL;
1013
3.21k
1014
3.21k
  if (type == isl_dim_out)
1015
3.21k
    
isl_die0
(aff->v->ctx, isl_error_invalid,
1016
3.21k
      "output/set dimension does not have a coefficient",
1017
3.21k
      return isl_aff_free(aff));
1018
3.21k
  if (type == isl_dim_in)
1019
3.09k
    type = isl_dim_set;
1020
3.21k
1021
3.21k
  if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
1022
3.21k
    
isl_die0
(aff->v->ctx, isl_error_invalid,
1023
3.21k
      "position out of bounds", return isl_aff_free(aff));
1024
3.21k
1025
3.21k
  if (isl_aff_is_nan(aff))
1026
0
    return aff;
1027
3.21k
  pos += isl_local_space_offset(aff->ls, type);
1028
3.21k
  if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1029
2
    return aff;
1030
3.21k
1031
3.21k
  aff = isl_aff_cow(aff);
1032
3.21k
  if (!aff)
1033
0
    return NULL;
1034
3.21k
1035
3.21k
  aff->v = isl_vec_cow(aff->v);
1036
3.21k
  if (!aff->v)
1037
0
    return isl_aff_free(aff);
1038
3.21k
1039
3.21k
  isl_int_set_si(aff->v->el[1 + pos], v);
1040
3.21k
1041
3.21k
  return aff;
1042
3.21k
}
1043
1044
/* Replace the coefficient of the variable of type "type" at position "pos"
1045
 * of "aff" by "v".
1046
 *
1047
 * A NaN is unaffected by this operation.
1048
 */
1049
__isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1050
  enum isl_dim_type type, int pos, __isl_take isl_val *v)
1051
0
{
1052
0
  if (!aff || !v)
1053
0
    goto error;
1054
0
1055
0
  if (type == isl_dim_out)
1056
0
    isl_die(aff->v->ctx, isl_error_invalid,
1057
0
      "output/set dimension does not have a coefficient",
1058
0
      goto error);
1059
0
  if (type == isl_dim_in)
1060
0
    type = isl_dim_set;
1061
0
1062
0
  if (pos >= isl_local_space_dim(aff->ls, type))
1063
0
    isl_die(aff->v->ctx, isl_error_invalid,
1064
0
      "position out of bounds", goto error);
1065
0
1066
0
  if (isl_aff_is_nan(aff)) {
1067
0
    isl_val_free(v);
1068
0
    return aff;
1069
0
  }
1070
0
  if (!isl_val_is_rat(v))
1071
0
    isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1072
0
      "expecting rational value", goto error);
1073
0
1074
0
  pos += isl_local_space_offset(aff->ls, type);
1075
0
  if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1076
0
      isl_int_eq(aff->v->el[0], v->d)) {
1077
0
    isl_val_free(v);
1078
0
    return aff;
1079
0
  }
1080
0
1081
0
  aff = isl_aff_cow(aff);
1082
0
  if (!aff)
1083
0
    goto error;
1084
0
  aff->v = isl_vec_cow(aff->v);
1085
0
  if (!aff->v)
1086
0
    goto error;
1087
0
1088
0
  if (isl_int_eq(aff->v->el[0], v->d)) {
1089
0
    isl_int_set(aff->v->el[1 + pos], v->n);
1090
0
  } else if (isl_int_is_one(v->d)) {
1091
0
    isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1092
0
  } else {
1093
0
    isl_seq_scale(aff->v->el + 1,
1094
0
        aff->v->el + 1, v->d, aff->v->size - 1);
1095
0
    isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1096
0
    isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1097
0
    aff->v = isl_vec_normalize(aff->v);
1098
0
    if (!aff->v)
1099
0
      goto error;
1100
0
  }
1101
0
1102
0
  isl_val_free(v);
1103
0
  return aff;
1104
0
error:
1105
0
  isl_aff_free(aff);
1106
0
  isl_val_free(v);
1107
0
  return NULL;
1108
0
}
1109
1110
/* Add "v" to the coefficient of the variable of type "type"
1111
 * at position "pos" of "aff".
1112
 *
1113
 * A NaN is unaffected by this operation.
1114
 */
1115
__isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1116
  enum isl_dim_type type, int pos, isl_int v)
1117
22.2k
{
1118
22.2k
  if (!aff)
1119
0
    return NULL;
1120
22.2k
1121
22.2k
  if (type == isl_dim_out)
1122
22.2k
    
isl_die0
(aff->v->ctx, isl_error_invalid,
1123
22.2k
      "output/set dimension does not have a coefficient",
1124
22.2k
      return isl_aff_free(aff));
1125
22.2k
  if (type == isl_dim_in)
1126
20.4k
    type = isl_dim_set;
1127
22.2k
1128
22.2k
  if (pos >= isl_local_space_dim(aff->ls, type))
1129
22.2k
    
isl_die0
(aff->v->ctx, isl_error_invalid,
1130
22.2k
      "position out of bounds", return isl_aff_free(aff));
1131
22.2k
1132
22.2k
  if (isl_aff_is_nan(aff))
1133
0
    return aff;
1134
22.2k
  aff = isl_aff_cow(aff);
1135
22.2k
  if (!aff)
1136
0
    return NULL;
1137
22.2k
1138
22.2k
  aff->v = isl_vec_cow(aff->v);
1139
22.2k
  if (!aff->v)
1140
0
    return isl_aff_free(aff);
1141
22.2k
1142
22.2k
  pos += isl_local_space_offset(aff->ls, type);
1143
22.2k
  isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1144
22.2k
1145
22.2k
  return aff;
1146
22.2k
}
1147
1148
/* Add "v" to the coefficient of the variable of type "type"
1149
 * at position "pos" of "aff".
1150
 *
1151
 * A NaN is unaffected by this operation.
1152
 */
1153
__isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1154
  enum isl_dim_type type, int pos, __isl_take isl_val *v)
1155
0
{
1156
0
  if (!aff || !v)
1157
0
    goto error;
1158
0
1159
0
  if (isl_val_is_zero(v)) {
1160
0
    isl_val_free(v);
1161
0
    return aff;
1162
0
  }
1163
0
1164
0
  if (type == isl_dim_out)
1165
0
    isl_die(aff->v->ctx, isl_error_invalid,
1166
0
      "output/set dimension does not have a coefficient",
1167
0
      goto error);
1168
0
  if (type == isl_dim_in)
1169
0
    type = isl_dim_set;
1170
0
1171
0
  if (pos >= isl_local_space_dim(aff->ls, type))
1172
0
    isl_die(aff->v->ctx, isl_error_invalid,
1173
0
      "position out of bounds", goto error);
1174
0
1175
0
  if (isl_aff_is_nan(aff)) {
1176
0
    isl_val_free(v);
1177
0
    return aff;
1178
0
  }
1179
0
  if (!isl_val_is_rat(v))
1180
0
    isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1181
0
      "expecting rational value", goto error);
1182
0
1183
0
  aff = isl_aff_cow(aff);
1184
0
  if (!aff)
1185
0
    goto error;
1186
0
1187
0
  aff->v = isl_vec_cow(aff->v);
1188
0
  if (!aff->v)
1189
0
    goto error;
1190
0
1191
0
  pos += isl_local_space_offset(aff->ls, type);
1192
0
  if (isl_int_is_one(v->d)) {
1193
0
    isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1194
0
  } else if (isl_int_eq(aff->v->el[0], v->d)) {
1195
0
    isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1196
0
    aff->v = isl_vec_normalize(aff->v);
1197
0
    if (!aff->v)
1198
0
      goto error;
1199
0
  } else {
1200
0
    isl_seq_scale(aff->v->el + 1,
1201
0
        aff->v->el + 1, v->d, aff->v->size - 1);
1202
0
    isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1203
0
    isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1204
0
    aff->v = isl_vec_normalize(aff->v);
1205
0
    if (!aff->v)
1206
0
      goto error;
1207
0
  }
1208
0
1209
0
  isl_val_free(v);
1210
0
  return aff;
1211
0
error:
1212
0
  isl_aff_free(aff);
1213
0
  isl_val_free(v);
1214
0
  return NULL;
1215
0
}
1216
1217
__isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1218
  enum isl_dim_type type, int pos, int v)
1219
22.2k
{
1220
22.2k
  isl_int t;
1221
22.2k
1222
22.2k
  isl_int_init(t);
1223
22.2k
  isl_int_set_si(t, v);
1224
22.2k
  aff = isl_aff_add_coefficient(aff, type, pos, t);
1225
22.2k
  isl_int_clear(t);
1226
22.2k
1227
22.2k
  return aff;
1228
22.2k
}
1229
1230
__isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1231
97
{
1232
97
  if (!aff)
1233
0
    return NULL;
1234
97
1235
97
  return isl_local_space_get_div(aff->ls, pos);
1236
97
}
1237
1238
/* Return the negation of "aff".
1239
 *
1240
 * As a special case, -NaN = NaN.
1241
 */
1242
__isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1243
28.6k
{
1244
28.6k
  if (!aff)
1245
0
    return NULL;
1246
28.6k
  if (isl_aff_is_nan(aff))
1247
1
    return aff;
1248
28.6k
  aff = isl_aff_cow(aff);
1249
28.6k
  if (!aff)
1250
0
    return NULL;
1251
28.6k
  aff->v = isl_vec_cow(aff->v);
1252
28.6k
  if (!aff->v)
1253
0
    return isl_aff_free(aff);
1254
28.6k
1255
28.6k
  isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1256
28.6k
1257
28.6k
  return aff;
1258
28.6k
}
1259
1260
/* Remove divs from the local space that do not appear in the affine
1261
 * expression.
1262
 * We currently only remove divs at the end.
1263
 * Some intermediate divs may also not appear directly in the affine
1264
 * expression, but we would also need to check that no other divs are
1265
 * defined in terms of them.
1266
 */
1267
__isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1268
53.6k
{
1269
53.6k
  int pos;
1270
53.6k
  int off;
1271
53.6k
  int n;
1272
53.6k
1273
53.6k
  if (!aff)
1274
0
    return NULL;
1275
53.6k
1276
53.6k
  n = isl_local_space_dim(aff->ls, isl_dim_div);
1277
53.6k
  off = isl_local_space_offset(aff->ls, isl_dim_div);
1278
53.6k
1279
53.6k
  pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1280
53.6k
  if (pos == n)
1281
51.4k
    return aff;
1282
2.19k
1283
2.19k
  aff = isl_aff_cow(aff);
1284
2.19k
  if (!aff)
1285
0
    return NULL;
1286
2.19k
1287
2.19k
  aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1288
2.19k
  aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1289
2.19k
  if (!aff->ls || !aff->v)
1290
0
    return isl_aff_free(aff);
1291
2.19k
1292
2.19k
  return aff;
1293
2.19k
}
1294
1295
/* Look for any divs in the aff->ls with a denominator equal to one
1296
 * and plug them into the affine expression and any subsequent divs
1297
 * that may reference the div.
1298
 */
1299
static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1300
43.6k
{
1301
43.6k
  int i, n;
1302
43.6k
  int len;
1303
43.6k
  isl_int v;
1304
43.6k
  isl_vec *vec;
1305
43.6k
  isl_local_space *ls;
1306
43.6k
  unsigned pos;
1307
43.6k
1308
43.6k
  if (!aff)
1309
0
    return NULL;
1310
43.6k
1311
43.6k
  n = isl_local_space_dim(aff->ls, isl_dim_div);
1312
43.6k
  len = aff->v->size;
1313
52.5k
  for (i = 0; i < n; 
++i8.88k
) {
1314
8.88k
    if (!isl_int_is_one(aff->ls->div->row[i][0]))
1315
8.88k
      
continue8.07k
;
1316
805
    ls = isl_local_space_copy(aff->ls);
1317
805
    ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1318
805
        aff->ls->div->row[i], len, i + 1, n - (i + 1));
1319
805
    vec = isl_vec_copy(aff->v);
1320
805
    vec = isl_vec_cow(vec);
1321
805
    if (!ls || !vec)
1322
0
      goto error;
1323
805
1324
805
    isl_int_init(v);
1325
805
1326
805
    pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1327
805
    isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1328
805
          len, len, v);
1329
805
1330
805
    isl_int_clear(v);
1331
805
1332
805
    isl_vec_free(aff->v);
1333
805
    aff->v = vec;
1334
805
    isl_local_space_free(aff->ls);
1335
805
    aff->ls = ls;
1336
805
  }
1337
43.6k
1338
43.6k
  return aff;
1339
0
error:
1340
0
  isl_vec_free(vec);
1341
0
  isl_local_space_free(ls);
1342
0
  return isl_aff_free(aff);
1343
43.6k
}
1344
1345
/* Look for any divs j that appear with a unit coefficient inside
1346
 * the definitions of other divs i and plug them into the definitions
1347
 * of the divs i.
1348
 *
1349
 * In particular, an expression of the form
1350
 *
1351
 *  floor((f(..) + floor(g(..)/n))/m)
1352
 *
1353
 * is simplified to
1354
 *
1355
 *  floor((n * f(..) + g(..))/(n * m))
1356
 *
1357
 * This simplification is correct because we can move the expression
1358
 * f(..) into the inner floor in the original expression to obtain
1359
 *
1360
 *  floor(floor((n * f(..) + g(..))/n)/m)
1361
 *
1362
 * from which we can derive the simplified expression.
1363
 */
1364
static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1365
43.6k
{
1366
43.6k
  int i, j, n;
1367
43.6k
  int off;
1368
43.6k
1369
43.6k
  if (!aff)
1370
0
    return NULL;
1371
43.6k
1372
43.6k
  n = isl_local_space_dim(aff->ls, isl_dim_div);
1373
43.6k
  off = isl_local_space_offset(aff->ls, isl_dim_div);
1374
45.7k
  for (i = 1; i < n; 
++i2.10k
) {
1375
5.13k
    for (j = 0; j < i; 
++j3.03k
) {
1376
3.03k
      if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1377
3.03k
        
continue2.83k
;
1378
204
      aff->ls = isl_local_space_substitute_seq(aff->ls,
1379
204
        isl_dim_div, j, aff->ls->div->row[j],
1380
204
        aff->v->size, i, 1);
1381
204
      if (!aff->ls)
1382
0
        return isl_aff_free(aff);
1383
204
    }
1384
2.10k
  }
1385
43.6k
1386
43.6k
  return aff;
1387
43.6k
}
1388
1389
/* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1390
 *
1391
 * Even though this function is only called on isl_affs with a single
1392
 * reference, we are careful to only change aff->v and aff->ls together.
1393
 */
1394
static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1395
1.13k
{
1396
1.13k
  unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1397
1.13k
  isl_local_space *ls;
1398
1.13k
  isl_vec *v;
1399
1.13k
1400
1.13k
  ls = isl_local_space_copy(aff->ls);
1401
1.13k
  ls = isl_local_space_swap_div(ls, a, b);
1402
1.13k
  v = isl_vec_copy(aff->v);
1403
1.13k
  v = isl_vec_cow(v);
1404
1.13k
  if (!ls || !v)
1405
0
    goto error;
1406
1.13k
1407
1.13k
  isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1408
1.13k
  isl_vec_free(aff->v);
1409
1.13k
  aff->v = v;
1410
1.13k
  isl_local_space_free(aff->ls);
1411
1.13k
  aff->ls = ls;
1412
1.13k
1413
1.13k
  return aff;
1414
0
error:
1415
0
  isl_vec_free(v);
1416
0
  isl_local_space_free(ls);
1417
0
  return isl_aff_free(aff);
1418
1.13k
}
1419
1420
/* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1421
 *
1422
 * We currently do not actually remove div "b", but simply add its
1423
 * coefficient to that of "a" and then zero it out.
1424
 */
1425
static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1426
101
{
1427
101
  unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1428
101
1429
101
  if (isl_int_is_zero(aff->v->el[1 + off + b]))
1430
101
    
return aff74
;
1431
27
1432
27
  aff->v = isl_vec_cow(aff->v);
1433
27
  if (!aff->v)
1434
0
    return isl_aff_free(aff);
1435
27
1436
27
  isl_int_add(aff->v->el[1 + off + a],
1437
27
        aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1438
27
  isl_int_set_si(aff->v->el[1 + off + b], 0);
1439
27
1440
27
  return aff;
1441
27
}
1442
1443
/* Sort the divs in the local space of "aff" according to
1444
 * the comparison function "cmp_row" in isl_local_space.c,
1445
 * combining the coefficients of identical divs.
1446
 *
1447
 * Reordering divs does not change the semantics of "aff",
1448
 * so there is no need to call isl_aff_cow.
1449
 * Moreover, this function is currently only called on isl_affs
1450
 * with a single reference.
1451
 */
1452
static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1453
43.6k
{
1454
43.6k
  int i, j, n;
1455
43.6k
1456
43.6k
  if (!aff)
1457
0
    return NULL;
1458
43.6k
1459
43.6k
  n = isl_aff_dim(aff, isl_dim_div);
1460
45.7k
  for (i = 1; i < n; 
++i2.10k
) {
1461
3.34k
    for (j = i - 1; j >= 0; 
--j1.24k
) {
1462
2.60k
      int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1463
2.60k
      if (cmp < 0)
1464
1.36k
        break;
1465
1.24k
      if (cmp == 0)
1466
101
        aff = merge_divs(aff, j, j + 1);
1467
1.13k
      else
1468
1.13k
        aff = swap_div(aff, j, j + 1);
1469
1.24k
      if (!aff)
1470
0
        return NULL;
1471
1.24k
    }
1472
2.10k
  }
1473
43.6k
1474
43.6k
  return aff;
1475
43.6k
}
1476
1477
/* Normalize the representation of "aff".
1478
 *
1479
 * This function should only be called of "new" isl_affs, i.e.,
1480
 * with only a single reference.  We therefore do not need to
1481
 * worry about affecting other instances.
1482
 */
1483
__isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1484
43.6k
{
1485
43.6k
  if (!aff)
1486
0
    return NULL;
1487
43.6k
  aff->v = isl_vec_normalize(aff->v);
1488
43.6k
  if (!aff->v)
1489
0
    return isl_aff_free(aff);
1490
43.6k
  aff = plug_in_integral_divs(aff);
1491
43.6k
  aff = plug_in_unit_divs(aff);
1492
43.6k
  aff = sort_divs(aff);
1493
43.6k
  aff = isl_aff_remove_unused_divs(aff);
1494
43.6k
  return aff;
1495
43.6k
}
1496
1497
/* Given f, return floor(f).
1498
 * If f is an integer expression, then just return f.
1499
 * If f is a constant, then return the constant floor(f).
1500
 * Otherwise, if f = g/m, write g = q m + r,
1501
 * create a new div d = [r/m] and return the expression q + d.
1502
 * The coefficients in r are taken to lie between -m/2 and m/2.
1503
 *
1504
 * reduce_div_coefficients performs the same normalization.
1505
 *
1506
 * As a special case, floor(NaN) = NaN.
1507
 */
1508
__isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1509
19.2k
{
1510
19.2k
  int i;
1511
19.2k
  int size;
1512
19.2k
  isl_ctx *ctx;
1513
19.2k
  isl_vec *div;
1514
19.2k
1515
19.2k
  if (!aff)
1516
0
    return NULL;
1517
19.2k
1518
19.2k
  if (isl_aff_is_nan(aff))
1519
0
    return aff;
1520
19.2k
  if (isl_int_is_one(aff->v->el[0]))
1521
19.2k
    
return aff14.6k
;
1522
4.59k
1523
4.59k
  aff = isl_aff_cow(aff);
1524
4.59k
  if (!aff)
1525
0
    return NULL;
1526
4.59k
1527
4.59k
  aff->v = isl_vec_cow(aff->v);
1528
4.59k
  if (!aff->v)
1529
0
    return isl_aff_free(aff);
1530
4.59k
1531
4.59k
  if (isl_aff_is_cst(aff)) {
1532
312
    isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1533
312
    isl_int_set_si(aff->v->el[0], 1);
1534
312
    return aff;
1535
312
  }
1536
4.28k
1537
4.28k
  div = isl_vec_copy(aff->v);
1538
4.28k
  div = isl_vec_cow(div);
1539
4.28k
  if (!div)
1540
0
    return isl_aff_free(aff);
1541
4.28k
1542
4.28k
  ctx = isl_aff_get_ctx(aff);
1543
4.28k
  isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1544
18.6k
  for (i = 1; i < aff->v->size; 
++i14.3k
) {
1545
14.3k
    isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1546
14.3k
    isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1547
14.3k
    if (isl_int_gt(div->el[i], aff->v->el[0])) {
1548
1.62k
      isl_int_sub(div->el[i], div->el[i], div->el[0]);
1549
1.62k
      isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1550
1.62k
    }
1551
14.3k
  }
1552
4.28k
1553
4.28k
  aff->ls = isl_local_space_add_div(aff->ls, div);
1554
4.28k
  if (!aff->ls)
1555
0
    return isl_aff_free(aff);
1556
4.28k
1557
4.28k
  size = aff->v->size;
1558
4.28k
  aff->v = isl_vec_extend(aff->v, size + 1);
1559
4.28k
  if (!aff->v)
1560
0
    return isl_aff_free(aff);
1561
4.28k
  isl_int_set_si(aff->v->el[0], 1);
1562
4.28k
  isl_int_set_si(aff->v->el[size], 1);
1563
4.28k
1564
4.28k
  aff = isl_aff_normalize(aff);
1565
4.28k
1566
4.28k
  return aff;
1567
4.28k
}
1568
1569
/* Compute
1570
 *
1571
 *  aff mod m = aff - m * floor(aff/m)
1572
 *
1573
 * with m an integer value.
1574
 */
1575
__isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1576
  __isl_take isl_val *m)
1577
118
{
1578
118
  isl_aff *res;
1579
118
1580
118
  if (!aff || !m)
1581
0
    goto error;
1582
118
1583
118
  if (!isl_val_is_int(m))
1584
118
    
isl_die0
(isl_val_get_ctx(m), isl_error_invalid,
1585
118
      "expecting integer modulo", goto error);
1586
118
1587
118
  res = isl_aff_copy(aff);
1588
118
  aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1589
118
  aff = isl_aff_floor(aff);
1590
118
  aff = isl_aff_scale_val(aff, m);
1591
118
  res = isl_aff_sub(res, aff);
1592
118
1593
118
  return res;
1594
0
error:
1595
0
  isl_aff_free(aff);
1596
0
  isl_val_free(m);
1597
0
  return NULL;
1598
118
}
1599
1600
/* Compute
1601
 *
1602
 *  pwaff mod m = pwaff - m * floor(pwaff/m)
1603
 */
1604
__isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1605
3.09k
{
1606
3.09k
  isl_pw_aff *res;
1607
3.09k
1608
3.09k
  res = isl_pw_aff_copy(pwaff);
1609
3.09k
  pwaff = isl_pw_aff_scale_down(pwaff, m);
1610
3.09k
  pwaff = isl_pw_aff_floor(pwaff);
1611
3.09k
  pwaff = isl_pw_aff_scale(pwaff, m);
1612
3.09k
  res = isl_pw_aff_sub(res, pwaff);
1613
3.09k
1614
3.09k
  return res;
1615
3.09k
}
1616
1617
/* Compute
1618
 *
1619
 *  pa mod m = pa - m * floor(pa/m)
1620
 *
1621
 * with m an integer value.
1622
 */
1623
__isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1624
  __isl_take isl_val *m)
1625
3.09k
{
1626
3.09k
  if (!pa || !m)
1627
0
    goto error;
1628
3.09k
  if (!isl_val_is_int(m))
1629
3.09k
    
isl_die0
(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1630
3.09k
      "expecting integer modulo", goto error);
1631
3.09k
  pa = isl_pw_aff_mod(pa, m->n);
1632
3.09k
  isl_val_free(m);
1633
3.09k
  return pa;
1634
0
error:
1635
0
  isl_pw_aff_free(pa);
1636
0
  isl_val_free(m);
1637
0
  return NULL;
1638
3.09k
}
1639
1640
/* Given f, return ceil(f).
1641
 * If f is an integer expression, then just return f.
1642
 * Otherwise, let f be the expression
1643
 *
1644
 *  e/m
1645
 *
1646
 * then return
1647
 *
1648
 *  floor((e + m - 1)/m)
1649
 *
1650
 * As a special case, ceil(NaN) = NaN.
1651
 */
1652
__isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1653
3.24k
{
1654
3.24k
  if (!aff)
1655
0
    return NULL;
1656
3.24k
1657
3.24k
  if (isl_aff_is_nan(aff))
1658
0
    return aff;
1659
3.24k
  if (isl_int_is_one(aff->v->el[0]))
1660
3.24k
    
return aff3.13k
;
1661
110
1662
110
  aff = isl_aff_cow(aff);
1663
110
  if (!aff)
1664
0
    return NULL;
1665
110
  aff->v = isl_vec_cow(aff->v);
1666
110
  if (!aff->v)
1667
0
    return isl_aff_free(aff);
1668
110
1669
110
  isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1670
110
  isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1671
110
  aff = isl_aff_floor(aff);
1672
110
1673
110
  return aff;
1674
110
}
1675
1676
/* Apply the expansion computed by isl_merge_divs.
1677
 * The expansion itself is given by "exp" while the resulting
1678
 * list of divs is given by "div".
1679
 */
1680
__isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1681
  __isl_take isl_mat *div, int *exp)
1682
48.3k
{
1683
48.3k
  int old_n_div;
1684
48.3k
  int new_n_div;
1685
48.3k
  int offset;
1686
48.3k
1687
48.3k
  aff = isl_aff_cow(aff);
1688
48.3k
  if (!aff || !div)
1689
0
    goto error;
1690
48.3k
1691
48.3k
  old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1692
48.3k
  new_n_div = isl_mat_rows(div);
1693
48.3k
  offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1694
48.3k
1695
48.3k
  aff->v = isl_vec_expand(aff->v, offset, old_n_div, exp, new_n_div);
1696
48.3k
  aff->ls = isl_local_space_replace_divs(aff->ls, div);
1697
48.3k
  if (!aff->v || !aff->ls)
1698
0
    return isl_aff_free(aff);
1699
48.3k
  return aff;
1700
0
error:
1701
0
  isl_aff_free(aff);
1702
0
  isl_mat_free(div);
1703
0
  return NULL;
1704
48.3k
}
1705
1706
/* Add two affine expressions that live in the same local space.
1707
 */
1708
static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1709
  __isl_take isl_aff *aff2)
1710
67.2k
{
1711
67.2k
  isl_int gcd, f;
1712
67.2k
1713
67.2k
  aff1 = isl_aff_cow(aff1);
1714
67.2k
  if (!aff1 || !aff2)
1715
0
    goto error;
1716
67.2k
1717
67.2k
  aff1->v = isl_vec_cow(aff1->v);
1718
67.2k
  if (!aff1->v)
1719
0
    goto error;
1720
67.2k
1721
67.2k
  isl_int_init(gcd);
1722
67.2k
  isl_int_init(f);
1723
67.2k
  isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1724
67.2k
  isl_int_divexact(f, aff2->v->el[0], gcd);
1725
67.2k
  isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1726
67.2k
  isl_int_divexact(f, aff1->v->el[0], gcd);
1727
67.2k
  isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1728
67.2k
  isl_int_divexact(f, aff2->v->el[0], gcd);
1729
67.2k
  isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1730
67.2k
  isl_int_clear(f);
1731
67.2k
  isl_int_clear(gcd);
1732
67.2k
1733
67.2k
  isl_aff_free(aff2);
1734
67.2k
  return aff1;
1735
0
error:
1736
0
  isl_aff_free(aff1);
1737
0
  isl_aff_free(aff2);
1738
0
  return NULL;
1739
67.2k
}
1740
1741
/* Return the sum of "aff1" and "aff2".
1742
 *
1743
 * If either of the two is NaN, then the result is NaN.
1744
 */
1745
__isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1746
  __isl_take isl_aff *aff2)
1747
67.2k
{
1748
67.2k
  isl_ctx *ctx;
1749
67.2k
  int *exp1 = NULL;
1750
67.2k
  int *exp2 = NULL;
1751
67.2k
  isl_mat *div;
1752
67.2k
  int n_div1, n_div2;
1753
67.2k
1754
67.2k
  if (!aff1 || !aff2)
1755
0
    goto error;
1756
67.2k
1757
67.2k
  ctx = isl_aff_get_ctx(aff1);
1758
67.2k
  if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1759
67.2k
    
isl_die0
(ctx, isl_error_invalid,
1760
67.2k
      "spaces don't match", goto error);
1761
67.2k
1762
67.2k
  if (isl_aff_is_nan(aff1)) {
1763
2
    isl_aff_free(aff2);
1764
2
    return aff1;
1765
2
  }
1766
67.2k
  if (isl_aff_is_nan(aff2)) {
1767
32
    isl_aff_free(aff1);
1768
32
    return aff2;
1769
32
  }
1770
67.2k
1771
67.2k
  n_div1 = isl_aff_dim(aff1, isl_dim_div);
1772
67.2k
  n_div2 = isl_aff_dim(aff2, isl_dim_div);
1773
67.2k
  if (n_div1 == 0 && 
n_div2 == 053.4k
)
1774
46.4k
    return add_expanded(aff1, aff2);
1775
20.8k
1776
20.8k
  exp1 = isl_alloc_array(ctx, int, n_div1);
1777
20.8k
  exp2 = isl_alloc_array(ctx, int, n_div2);
1778
20.8k
  if ((n_div1 && 
!exp113.7k
) || (n_div2 &&
!exp27.69k
))
1779
0
    goto error;
1780
20.8k
1781
20.8k
  div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1782
20.8k
  aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1783
20.8k
  aff2 = isl_aff_expand_divs(aff2, div, exp2);
1784
20.8k
  free(exp1);
1785
20.8k
  free(exp2);
1786
20.8k
1787
20.8k
  return add_expanded(aff1, aff2);
1788
0
error:
1789
0
  free(exp1);
1790
0
  free(exp2);
1791
0
  isl_aff_free(aff1);
1792
0
  isl_aff_free(aff2);
1793
0
  return NULL;
1794
20.8k
}
1795
1796
__isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1797
  __isl_take isl_aff *aff2)
1798
738
{
1799
738
  return isl_aff_add(aff1, isl_aff_neg(aff2));
1800
738
}
1801
1802
/* Return the result of scaling "aff" by a factor of "f".
1803
 *
1804
 * As a special case, f * NaN = NaN.
1805
 */
1806
__isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1807
21.9k
{
1808
21.9k
  isl_int gcd;
1809
21.9k
1810
21.9k
  if (!aff)
1811
0
    return NULL;
1812
21.9k
  if (isl_aff_is_nan(aff))
1813
0
    return aff;
1814
21.9k
1815
21.9k
  if (isl_int_is_one(f))
1816
21.9k
    
return aff11.8k
;
1817
10.0k
1818
10.0k
  aff = isl_aff_cow(aff);
1819
10.0k
  if (!aff)
1820
0
    return NULL;
1821
10.0k
  aff->v = isl_vec_cow(aff->v);
1822
10.0k
  if (!aff->v)
1823
0
    return isl_aff_free(aff);
1824
10.0k
1825
10.0k
  if (isl_int_is_pos(f) && 
isl_int_is_divisible_by8.66k
(aff->v->el[0], f)) {
1826
132
    isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1827
132
    return aff;
1828
132
  }
1829
9.91k
1830
9.91k
  isl_int_init(gcd);
1831
9.91k
  isl_int_gcd(gcd, aff->v->el[0], f);
1832
9.91k
  isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1833
9.91k
  isl_int_divexact(gcd, f, gcd);
1834
9.91k
  isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1835
9.91k
  isl_int_clear(gcd);
1836
9.91k
1837
9.91k
  return aff;
1838
9.91k
}
1839
1840
/* Multiple "aff" by "v".
1841
 */
1842
__isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1843
  __isl_take isl_val *v)
1844
522
{
1845
522
  if (!aff || !v)
1846
0
    goto error;
1847
522
1848
522
  if (isl_val_is_one(v)) {
1849
63
    isl_val_free(v);
1850
63
    return aff;
1851
63
  }
1852
459
1853
459
  if (!isl_val_is_rat(v))
1854
459
    
isl_die0
(isl_aff_get_ctx(aff), isl_error_invalid,
1855
459
      "expecting rational factor", goto error);
1856
459
1857
459
  aff = isl_aff_scale(aff, v->n);
1858
459
  aff = isl_aff_scale_down(aff, v->d);
1859
459
1860
459
  isl_val_free(v);
1861
459
  return aff;
1862
0
error:
1863
0
  isl_aff_free(aff);
1864
0
  isl_val_free(v);
1865
0
  return NULL;
1866
459
}
1867
1868
/* Return the result of scaling "aff" down by a factor of "f".
1869
 *
1870
 * As a special case, NaN/f = NaN.
1871
 */
1872
__isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1873
20.9k
{
1874
20.9k
  isl_int gcd;
1875
20.9k
1876
20.9k
  if (!aff)
1877
0
    return NULL;
1878
20.9k
  if (isl_aff_is_nan(aff))
1879
0
    return aff;
1880
20.9k
1881
20.9k
  if (isl_int_is_one(f))
1882
20.9k
    
return aff16.5k
;
1883
4.38k
1884
4.38k
  aff = isl_aff_cow(aff);
1885
4.38k
  if (!aff)
1886
0
    return NULL;
1887
4.38k
1888
4.38k
  if (isl_int_is_zero(f))
1889
4.38k
    
isl_die0
(isl_aff_get_ctx(aff), isl_error_invalid,
1890
4.38k
      "cannot scale down by zero", return isl_aff_free(aff));
1891
4.38k
1892
4.38k
  aff->v = isl_vec_cow(aff->v);
1893
4.38k
  if (!aff->v)
1894
0
    return isl_aff_free(aff);
1895
4.38k
1896
4.38k
  isl_int_init(gcd);
1897
4.38k
  isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1898
4.38k
  isl_int_gcd(gcd, gcd, f);
1899
4.38k
  isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1900
4.38k
  isl_int_divexact(gcd, f, gcd);
1901
4.38k
  isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1902
4.38k
  isl_int_clear(gcd);
1903
4.38k
1904
4.38k
  return aff;
1905
4.38k
}
1906
1907
/* Divide "aff" by "v".
1908
 */
1909
__isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1910
  __isl_take isl_val *v)
1911
321
{
1912
321
  if (!aff || !v)
1913
0
    goto error;
1914
321
1915
321
  if (isl_val_is_one(v)) {
1916
12
    isl_val_free(v);
1917
12
    return aff;
1918
12
  }
1919
309
1920
309
  if (!isl_val_is_rat(v))
1921
309
    
isl_die0
(isl_aff_get_ctx(aff), isl_error_invalid,
1922
309
      "expecting rational factor", goto error);
1923
309
  if (!isl_val_is_pos(v))
1924
309
    
isl_die0
(isl_aff_get_ctx(aff), isl_error_invalid,
1925
309
      "factor needs to be positive", goto error);
1926
309
1927
309
  aff = isl_aff_scale(aff, v->d);
1928
309
  aff = isl_aff_scale_down(aff, v->n);
1929
309
1930
309
  isl_val_free(v);
1931
309
  return aff;
1932
0
error:
1933
0
  isl_aff_free(aff);
1934
0
  isl_val_free(v);
1935
0
  return NULL;
1936
309
}
1937
1938
__isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1939
3
{
1940
3
  isl_int v;
1941
3
1942
3
  if (f == 1)
1943
0
    return aff;
1944
3
1945
3
  isl_int_init(v);
1946
3
  isl_int_set_ui(v, f);
1947
3
  aff = isl_aff_scale_down(aff, v);
1948
3
  isl_int_clear(v);
1949
3
1950
3
  return aff;
1951
3
}
1952
1953
__isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1954
  enum isl_dim_type type, unsigned pos, const char *s)
1955
0
{
1956
0
  aff = isl_aff_cow(aff);
1957
0
  if (!aff)
1958
0
    return NULL;
1959
0
  if (type == isl_dim_out)
1960
0
    isl_die(aff->v->ctx, isl_error_invalid,
1961
0
      "cannot set name of output/set dimension",
1962
0
      return isl_aff_free(aff));
1963
0
  if (type == isl_dim_in)
1964
0
    type = isl_dim_set;
1965
0
  aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1966
0
  if (!aff->ls)
1967
0
    return isl_aff_free(aff);
1968
0
1969
0
  return aff;
1970
0
}
1971
1972
__isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1973
  enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1974
0
{
1975
0
  aff = isl_aff_cow(aff);
1976
0
  if (!aff)
1977
0
    goto error;
1978
0
  if (type == isl_dim_out)
1979
0
    isl_die(aff->v->ctx, isl_error_invalid,
1980
0
      "cannot set name of output/set dimension",
1981
0
      goto error);
1982
0
  if (type == isl_dim_in)
1983
0
    type = isl_dim_set;
1984
0
  aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1985
0
  if (!aff->ls)
1986
0
    return isl_aff_free(aff);
1987
0
1988
0
  return aff;
1989
0
error:
1990
0
  isl_id_free(id);
1991
0
  isl_aff_free(aff);
1992
0
  return NULL;
1993
0
}
1994
1995
/* Replace the identifier of the input tuple of "aff" by "id".
1996
 * type is currently required to be equal to isl_dim_in
1997
 */
1998
__isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
1999
  enum isl_dim_type type, __isl_take isl_id *id)
2000
1
{
2001
1
  aff = isl_aff_cow(aff);
2002
1
  if (!aff)
2003
0
    goto error;
2004
1
  if (type != isl_dim_in)
2005
1
    
isl_die0
(aff->v->ctx, isl_error_invalid,
2006
1
      "cannot only set id of input tuple", goto error);
2007
1
  aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2008
1
  if (!aff->ls)
2009
0
    return isl_aff_free(aff);
2010
1
2011
1
  return aff;
2012
0
error:
2013
0
  isl_id_free(id);
2014
0
  isl_aff_free(aff);
2015
0
  return NULL;
2016
1
}
2017
2018
/* Exploit the equalities in "eq" to simplify the affine expression
2019
 * and the expressions of the integer divisions in the local space.
2020
 * The integer divisions in this local space are assumed to appear
2021
 * as regular dimensions in "eq".
2022
 */
2023
static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2024
  __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2025
150k
{
2026
150k
  int i, j;
2027
150k
  unsigned total;
2028
150k
  unsigned n_div;
2029
150k
2030
150k
  if (!eq)
2031
0
    goto error;
2032
150k
  if (eq->n_eq == 0) {
2033
146k
    isl_basic_set_free(eq);
2034
146k
    return aff;
2035
146k
  }
2036
4.66k
2037
4.66k
  aff = isl_aff_cow(aff);
2038
4.66k
  if (!aff)
2039
0
    goto error;
2040
4.66k
2041
4.66k
  aff->ls = isl_local_space_substitute_equalities(aff->ls,
2042
4.66k
              isl_basic_set_copy(eq));
2043
4.66k
  aff->v = isl_vec_cow(aff->v);
2044
4.66k
  if (!aff->ls || !aff->v)
2045
0
    goto error;
2046
4.66k
2047
4.66k
  total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2048
4.66k
  n_div = eq->n_div;
2049
10.9k
  for (i = 0; i < eq->n_eq; 
++i6.25k
) {
2050
6.25k
    j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2051
6.25k
    if (j < 0 || j == 0 || j >= total)
2052
800
      continue;
2053
5.45k
2054
5.45k
    isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2055
5.45k
        &aff->v->el[0]);
2056
5.45k
  }
2057
4.66k
2058
4.66k
  isl_basic_set_free(eq);
2059
4.66k
  aff = isl_aff_normalize(aff);
2060
4.66k
  return aff;
2061
0
error:
2062
0
  isl_basic_set_free(eq);
2063
0
  isl_aff_free(aff);
2064
0
  return NULL;
2065
4.66k
}
2066
2067
/* Exploit the equalities in "eq" to simplify the affine expression
2068
 * and the expressions of the integer divisions in the local space.
2069
 */
2070
__isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2071
  __isl_take isl_basic_set *eq)
2072
50.3k
{
2073
50.3k
  int n_div;
2074
50.3k
2075
50.3k
  if (!aff || !eq)
2076
0
    goto error;
2077
50.3k
  n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2078
50.3k
  if (n_div > 0)
2079
7.63k
    eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2080
50.3k
  return isl_aff_substitute_equalities_lifted(aff, eq);
2081
0
error:
2082
0
  isl_basic_set_free(eq);
2083
0
  isl_aff_free(aff);
2084
0
  return NULL;
2085
50.3k
}
2086
2087
/* Look for equalities among the variables shared by context and aff
2088
 * and the integer divisions of aff, if any.
2089
 * The equalities are then used to eliminate coefficients and/or integer
2090
 * divisions from aff.
2091
 */
2092
__isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2093
  __isl_take isl_set *context)
2094
100k
{
2095
100k
  isl_basic_set *hull;
2096
100k
  int n_div;
2097
100k
2098
100k
  if (!aff)
2099
0
    goto error;
2100
100k
  n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2101
100k
  if (n_div > 0) {
2102
21.5k
    isl_basic_set *bset;
2103
21.5k
    isl_local_space *ls;
2104
21.5k
    context = isl_set_add_dims(context, isl_dim_set, n_div);
2105
21.5k
    ls = isl_aff_get_domain_local_space(aff);
2106
21.5k
    bset = isl_basic_set_from_local_space(ls);
2107
21.5k
    bset = isl_basic_set_lift(bset);
2108
21.5k
    bset = isl_basic_set_flatten(bset);
2109
21.5k
    context = isl_set_intersect(context,
2110
21.5k
              isl_set_from_basic_set(bset));
2111
21.5k
  }
2112
100k
2113
100k
  hull = isl_set_affine_hull(context);
2114
100k
  return isl_aff_substitute_equalities_lifted(aff, hull);
2115
0
error:
2116
0
  isl_aff_free(aff);
2117
0
  isl_set_free(context);
2118
0
  return NULL;
2119
100k
}
2120
2121
__isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2122
  __isl_take isl_set *context)
2123
0
{
2124
0
  isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2125
0
  dom_context = isl_set_intersect_params(dom_context, context);
2126
0
  return isl_aff_gist(aff, dom_context);
2127
0
}
2128
2129
/* Return a basic set containing those elements in the space
2130
 * of aff where it is positive.  "rational" should not be set.
2131
 *
2132
 * If "aff" is NaN, then it is not positive.
2133
 */
2134
static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2135
  int rational)
2136
175
{
2137
175
  isl_constraint *ineq;
2138
175
  isl_basic_set *bset;
2139
175
  isl_val *c;
2140
175
2141
175
  if (!aff)
2142
0
    return NULL;
2143
175
  if (isl_aff_is_nan(aff)) {
2144
0
    isl_space *space = isl_aff_get_domain_space(aff);
2145
0
    isl_aff_free(aff);
2146
0
    return isl_basic_set_empty(space);
2147
0
  }
2148
175
  if (rational)
2149
175
    
isl_die0
(isl_aff_get_ctx(aff), isl_error_unsupported,
2150
175
      "rational sets not supported", goto error);
2151
175
2152
175
  ineq = isl_inequality_from_aff(aff);
2153
175
  c = isl_constraint_get_constant_val(ineq);
2154
175
  c = isl_val_sub_ui(c, 1);
2155
175
  ineq = isl_constraint_set_constant_val(ineq, c);
2156
175
2157
175
  bset = isl_basic_set_from_constraint(ineq);
2158
175
  bset = isl_basic_set_simplify(bset);
2159
175
  return bset;
2160
0
error:
2161
0
  isl_aff_free(aff);
2162
0
  return NULL;
2163
175
}
2164
2165
/* Return a basic set containing those elements in the space
2166
 * of aff where it is non-negative.
2167
 * If "rational" is set, then return a rational basic set.
2168
 *
2169
 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2170
 */
2171
static __isl_give isl_basic_set *aff_nonneg_basic_set(
2172
  __isl_take isl_aff *aff, int rational)
2173
17.0k
{
2174
17.0k
  isl_constraint *ineq;
2175
17.0k
  isl_basic_set *bset;
2176
17.0k
2177
17.0k
  if (!aff)
2178
0
    return NULL;
2179
17.0k
  if (isl_aff_is_nan(aff)) {
2180
0
    isl_space *space = isl_aff_get_domain_space(aff);
2181
0
    isl_aff_free(aff);
2182
0
    return isl_basic_set_empty(space);
2183
0
  }
2184
17.0k
2185
17.0k
  ineq = isl_inequality_from_aff(aff);
2186
17.0k
2187
17.0k
  bset = isl_basic_set_from_constraint(ineq);
2188
17.0k
  if (rational)
2189
24
    bset = isl_basic_set_set_rational(bset);
2190
17.0k
  bset = isl_basic_set_simplify(bset);
2191
17.0k
  return bset;
2192
17.0k
}
2193
2194
/* Return a basic set containing those elements in the space
2195
 * of aff where it is non-negative.
2196
 */
2197
__isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2198
562
{
2199
562
  return aff_nonneg_basic_set(aff, 0);
2200
562
}
2201
2202
/* Return a basic set containing those elements in the domain space
2203
 * of "aff" where it is positive.
2204
 */
2205
__isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2206
108
{
2207
108
  aff = isl_aff_add_constant_num_si(aff, -1);
2208
108
  return isl_aff_nonneg_basic_set(aff);
2209
108
}
2210
2211
/* Return a basic set containing those elements in the domain space
2212
 * of aff where it is negative.
2213
 */
2214
__isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2215
108
{
2216
108
  aff = isl_aff_neg(aff);
2217
108
  return isl_aff_pos_basic_set(aff);
2218
108
}
2219
2220
/* Return a basic set containing those elements in the space
2221
 * of aff where it is zero.
2222
 * If "rational" is set, then return a rational basic set.
2223
 *
2224
 * If "aff" is NaN, then it is not zero.
2225
 */
2226
static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2227
  int rational)
2228
9.95k
{
2229
9.95k
  isl_constraint *ineq;
2230
9.95k
  isl_basic_set *bset;
2231
9.95k
2232
9.95k
  if (!aff)
2233
0
    return NULL;
2234
9.95k
  if (isl_aff_is_nan(aff)) {
2235
0
    isl_space *space = isl_aff_get_domain_space(aff);
2236
0
    isl_aff_free(aff);
2237
0
    return isl_basic_set_empty(space);
2238
0
  }
2239
9.95k
2240
9.95k
  ineq = isl_equality_from_aff(aff);
2241
9.95k
2242
9.95k
  bset = isl_basic_set_from_constraint(ineq);
2243
9.95k
  if (rational)
2244
255
    bset = isl_basic_set_set_rational(bset);
2245
9.95k
  bset = isl_basic_set_simplify(bset);
2246
9.95k
  return bset;
2247
9.95k
}
2248
2249
/* Return a basic set containing those elements in the space
2250
 * of aff where it is zero.
2251
 */
2252
__isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2253
70
{
2254
70
  return aff_zero_basic_set(aff, 0);
2255
70
}
2256
2257
/* Return a basic set containing those elements in the shared space
2258
 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2259
 */
2260
__isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2261
  __isl_take isl_aff *aff2)
2262
454
{
2263
454
  aff1 = isl_aff_sub(aff1, aff2);
2264
454
2265
454
  return isl_aff_nonneg_basic_set(aff1);
2266
454
}
2267
2268
/* Return a basic set containing those elements in the shared domain space
2269
 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2270
 */
2271
__isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2272
  __isl_take isl_aff *aff2)
2273
0
{
2274
0
  aff1 = isl_aff_sub(aff1, aff2);
2275
0
2276
0
  return isl_aff_pos_basic_set(aff1);
2277
0
}
2278
2279
/* Return a set containing those elements in the shared space
2280
 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2281
 */
2282
__isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2283
  __isl_take isl_aff *aff2)
2284
374
{
2285
374
  return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2286
374
}
2287
2288
/* Return a set containing those elements in the shared domain space
2289
 * of aff1 and aff2 where aff1 is greater than aff2.
2290
 *
2291
 * If either of the two inputs is NaN, then the result is empty,
2292
 * as comparisons with NaN always return false.
2293
 */
2294
__isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2295
  __isl_take isl_aff *aff2)
2296
0
{
2297
0
  return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2298
0
}
2299
2300
/* Return a basic set containing those elements in the shared space
2301
 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2302
 */
2303
__isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2304
  __isl_take isl_aff *aff2)
2305
34
{
2306
34
  return isl_aff_ge_basic_set(aff2, aff1);
2307
34
}
2308
2309
/* Return a basic set containing those elements in the shared domain space
2310
 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2311
 */
2312
__isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2313
  __isl_take isl_aff *aff2)
2314
0
{
2315
0
  return isl_aff_gt_basic_set(aff2, aff1);
2316
0
}
2317
2318
/* Return a set containing those elements in the shared space
2319
 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2320
 */
2321
__isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2322
  __isl_take isl_aff *aff2)
2323
193
{
2324
193
  return isl_aff_ge_set(aff2, aff1);
2325
193
}
2326
2327
/* Return a set containing those elements in the shared domain space
2328
 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2329
 */
2330
__isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2331
  __isl_take isl_aff *aff2)
2332
0
{
2333
0
  return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2334
0
}
2335
2336
/* Return a basic set containing those elements in the shared space
2337
 * of aff1 and aff2 where aff1 and aff2 are equal.
2338
 */
2339
__isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2340
  __isl_take isl_aff *aff2)
2341
34
{
2342
34
  aff1 = isl_aff_sub(aff1, aff2);
2343
34
2344
34
  return isl_aff_zero_basic_set(aff1);
2345
34
}
2346
2347
/* Return a set containing those elements in the shared space
2348
 * of aff1 and aff2 where aff1 and aff2 are equal.
2349
 */
2350
__isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2351
  __isl_take isl_aff *aff2)
2352
34
{
2353
34
  return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2354
34
}
2355
2356
/* Return a set containing those elements in the shared domain space
2357
 * of aff1 and aff2 where aff1 and aff2 are not equal.
2358
 *
2359
 * If either of the two inputs is NaN, then the result is empty,
2360
 * as comparisons with NaN always return false.
2361
 */
2362
__isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2363
  __isl_take isl_aff *aff2)
2364
0
{
2365
0
  isl_set *set_lt, *set_gt;
2366
0
2367
0
  set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2368
0
        isl_aff_copy(aff2));
2369
0
  set_gt = isl_aff_gt_set(aff1, aff2);
2370
0
  return isl_set_union_disjoint(set_lt, set_gt);
2371
0
}
2372
2373
__isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2374
  __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2375
2
{
2376
2
  aff1 = isl_aff_add(aff1, aff2);
2377
2
  aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2378
2
  return aff1;
2379
2
}
2380
2381
int isl_aff_is_empty(__isl_keep isl_aff *aff)
2382
250k
{
2383
250k
  if (!aff)
2384
0
    return -1;
2385
250k
2386
250k
  return 0;
2387
250k
}
2388
2389
/* Check whether the given affine expression has non-zero coefficient
2390
 * for any dimension in the given range or if any of these dimensions
2391
 * appear with non-zero coefficients in any of the integer divisions
2392
 * involved in the affine expression.
2393
 */
2394
isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2395
  enum isl_dim_type type, unsigned first, unsigned n)
2396
27.1k
{
2397
27.1k
  int i;
2398
27.1k
  isl_ctx *ctx;
2399
27.1k
  int *active = NULL;
2400
27.1k
  isl_bool involves = isl_bool_false;
2401
27.1k
2402
27.1k
  if (!aff)
2403
0
    return isl_bool_error;
2404
27.1k
  if (n == 0)
2405
19
    return isl_bool_false;
2406
27.1k
2407
27.1k
  ctx = isl_aff_get_ctx(aff);
2408
27.1k
  if (first + n > isl_aff_dim(aff, type))
2409
27.1k
    
isl_die0
(ctx, isl_error_invalid,
2410
27.1k
      "range out of bounds", return isl_bool_error);
2411
27.1k
2412
27.1k
  active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2413
27.1k
  if (!active)
2414
0
    goto error;
2415
27.1k
2416
27.1k
  first += isl_local_space_offset(aff->ls, type) - 1;
2417
49.2k
  for (i = 0; i < n; 
++i22.1k
)
2418
27.2k
    if (active[first + i]) {
2419
5.13k
      involves = isl_bool_true;
2420
5.13k
      break;
2421
5.13k
    }
2422
27.1k
2423
27.1k
  free(active);
2424
27.1k
2425
27.1k
  return involves;
2426
0
error:
2427
0
  free(active);
2428
0
  return isl_bool_error;
2429
27.1k
}
2430
2431
__isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2432
  enum isl_dim_type type, unsigned first, unsigned n)
2433
1.69k
{
2434
1.69k
  isl_ctx *ctx;
2435
1.69k
2436
1.69k
  if (!aff)
2437
0
    return NULL;
2438
1.69k
  if (type == isl_dim_out)
2439
1.69k
    
isl_die0
(aff->v->ctx, isl_error_invalid,
2440
1.69k
      "cannot drop output/set dimension",
2441
1.69k
      return isl_aff_free(aff));
2442
1.69k
  if (type == isl_dim_in)
2443
1.67k
    type = isl_dim_set;
2444
1.69k
  if (n == 0 && 
!isl_local_space_is_named_or_nested(aff->ls, type)61
)
2445
41
    return aff;
2446
1.65k
2447
1.65k
  ctx = isl_aff_get_ctx(aff);
2448
1.65k
  if (first + n > isl_local_space_dim(aff->ls, type))
2449
1.65k
    
isl_die0
(ctx, isl_error_invalid, "range out of bounds",
2450
1.65k
      return isl_aff_free(aff));
2451
1.65k
2452
1.65k
  aff = isl_aff_cow(aff);
2453
1.65k
  if (!aff)
2454
0
    return NULL;
2455
1.65k
2456
1.65k
  aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2457
1.65k
  if (!aff->ls)
2458
0
    return isl_aff_free(aff);
2459
1.65k
2460
1.65k
  first += 1 + isl_local_space_offset(aff->ls, type);
2461
1.65k
  aff->v = isl_vec_drop_els(aff->v, first, n);
2462
1.65k
  if (!aff->v)
2463
0
    return isl_aff_free(aff);
2464
1.65k
2465
1.65k
  return aff;
2466
1.65k
}
2467
2468
/* Drop the "n" domain dimensions starting at "first" from "aff",
2469
 * after checking that they do not appear in the affine expression.
2470
 */
2471
static __isl_give isl_aff *drop_domain(__isl_take isl_aff *aff, unsigned first,
2472
  unsigned n)
2473
19
{
2474
19
  isl_bool involves;
2475
19
2476
19
  involves = isl_aff_involves_dims(aff, isl_dim_in, first, n);
2477
19
  if (involves < 0)
2478
0
    return isl_aff_free(aff);
2479
19
  if (involves)
2480
19
    
isl_die0
(isl_aff_get_ctx(aff), isl_error_invalid,
2481
19
        "affine expression involves some of the domain dimensions",
2482
19
        return isl_aff_free(aff));
2483
19
  return isl_aff_drop_dims(aff, isl_dim_in, first, n);
2484
19
}
2485
2486
/* Project the domain of the affine expression onto its parameter space.
2487
 * The affine expression may not involve any of the domain dimensions.
2488
 */
2489
__isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2490
19
{
2491
19
  isl_space *space;
2492
19
  unsigned n;
2493
19
2494
19
  n = isl_aff_dim(aff, isl_dim_in);
2495
19
  aff = drop_domain(aff, 0, n);
2496
19
  space = isl_aff_get_domain_space(aff);
2497
19
  space = isl_space_params(space);
2498
19
  aff = isl_aff_reset_domain_space(aff, space);
2499
19
  return aff;
2500
19
}
2501
2502
/* Check that the domain of "aff" is a product.
2503
 */
2504
static isl_stat check_domain_product(__isl_keep isl_aff *aff)
2505
0
{
2506
0
  isl_bool is_product;
2507
0
2508
0
  is_product = isl_space_is_product(isl_aff_peek_domain_space(aff));
2509
0
  if (is_product < 0)
2510
0
    return isl_stat_error;
2511
0
  if (!is_product)
2512
0
    isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2513
0
      "domain is not a product", return isl_stat_error);
2514
0
  return isl_stat_ok;
2515
0
}
2516
2517
/* Given an affine function with a domain of the form [A -> B] that
2518
 * does not depend on B, return the same function on domain A.
2519
 */
2520
__isl_give isl_aff *isl_aff_domain_factor_domain(__isl_take isl_aff *aff)
2521
0
{
2522
0
  isl_space *space;
2523
0
  int n, n_in;
2524
0
2525
0
  if (check_domain_product(aff) < 0)
2526
0
    return isl_aff_free(aff);
2527
0
  space = isl_aff_get_domain_space(aff);
2528
0
  n = isl_space_dim(space, isl_dim_set);
2529
0
  space = isl_space_factor_domain(space);
2530
0
  n_in = isl_space_dim(space, isl_dim_set);
2531
0
  aff = drop_domain(aff, n_in, n - n_in);
2532
0
  aff = isl_aff_reset_domain_space(aff, space);
2533
0
  return aff;
2534
0
}
2535
2536
/* Convert an affine expression defined over a parameter domain
2537
 * into one that is defined over a zero-dimensional set.
2538
 */
2539
__isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2540
0
{
2541
0
  isl_local_space *ls;
2542
0
2543
0
  ls = isl_aff_take_domain_local_space(aff);
2544
0
  ls = isl_local_space_set_from_params(ls);
2545
0
  aff = isl_aff_restore_domain_local_space(aff, ls);
2546
0
2547
0
  return aff;
2548
0
}
2549
2550
__isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2551
  enum isl_dim_type type, unsigned first, unsigned n)
2552
8.01k
{
2553
8.01k
  isl_ctx *ctx;
2554
8.01k
2555
8.01k
  if (!aff)
2556
0
    return NULL;
2557
8.01k
  if (type == isl_dim_out)
2558
8.01k
    
isl_die0
(aff->v->ctx, isl_error_invalid,
2559
8.01k
      "cannot insert output/set dimensions",
2560
8.01k
      return isl_aff_free(aff));
2561
8.01k
  if (type == isl_dim_in)
2562
8.01k
    type = isl_dim_set;
2563
8.01k
  if (n == 0 && 
!isl_local_space_is_named_or_nested(aff->ls, type)1.67k
)
2564
1.66k
    return aff;
2565
6.34k
2566
6.34k
  ctx = isl_aff_get_ctx(aff);
2567
6.34k
  if (first > isl_local_space_dim(aff->ls, type))
2568
6.34k
    
isl_die0
(ctx, isl_error_invalid, "position out of bounds",
2569
6.34k
      return isl_aff_free(aff));
2570
6.34k
2571
6.34k
  aff = isl_aff_cow(aff);
2572
6.34k
  if (!aff)
2573
0
    return NULL;
2574
6.34k
2575
6.34k
  aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2576
6.34k
  if (!aff->ls)
2577
0
    return isl_aff_free(aff);
2578
6.34k
2579
6.34k
  first += 1 + isl_local_space_offset(aff->ls, type);
2580
6.34k
  aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2581
6.34k
  if (!aff->v)
2582
0
    return isl_aff_free(aff);
2583
6.34k
2584
6.34k
  return aff;
2585
6.34k
}
2586
2587
__isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2588
  enum isl_dim_type type, unsigned n)
2589
3
{
2590
3
  unsigned pos;
2591
3
2592
3
  pos = isl_aff_dim(aff, type);
2593
3
2594
3
  return isl_aff_insert_dims(aff, type, pos, n);
2595
3
}
2596
2597
__isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2598
  enum isl_dim_type type, unsigned n)
2599
3.84k
{
2600
3.84k
  unsigned pos;
2601
3.84k
2602
3.84k
  pos = isl_pw_aff_dim(pwaff, type);
2603
3.84k
2604
3.84k
  return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2605
3.84k
}
2606
2607
/* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2608
 * to dimensions of "dst_type" at "dst_pos".
2609
 *
2610
 * We only support moving input dimensions to parameters and vice versa.
2611
 */
2612
__isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2613
  enum isl_dim_type dst_type, unsigned dst_pos,
2614
  enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2615
0
{
2616
0
  unsigned g_dst_pos;
2617
0
  unsigned g_src_pos;
2618
0
2619
0
  if (!aff)
2620
0
    return NULL;
2621
0
  if (n == 0 &&
2622
0
      !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2623
0
      !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2624
0
    return aff;
2625
0
2626
0
  if (dst_type == isl_dim_out || src_type == isl_dim_out)
2627
0
    isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2628
0
      "cannot move output/set dimension",
2629
0
      return isl_aff_free(aff));
2630
0
  if (dst_type == isl_dim_div || src_type == isl_dim_div)
2631
0
    isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2632
0
      "cannot move divs", return isl_aff_free(aff));
2633
0
  if (dst_type == isl_dim_in)
2634
0
    dst_type = isl_dim_set;
2635
0
  if (src_type == isl_dim_in)
2636
0
    src_type = isl_dim_set;
2637
0
2638
0
  if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2639
0
    isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2640
0
      "range out of bounds", return isl_aff_free(aff));
2641
0
  if (dst_type == src_type)
2642
0
    isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2643
0
      "moving dims within the same type not supported",
2644
0
      return isl_aff_free(aff));
2645
0
2646
0
  aff = isl_aff_cow(aff);
2647
0
  if (!aff)
2648
0
    return NULL;
2649
0
2650
0
  g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2651
0
  g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2652
0
  if (dst_type > src_type)
2653
0
    g_dst_pos -= n;
2654
0
2655
0
  aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2656
0
  aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2657
0
            src_type, src_pos, n);
2658
0
  if (!aff->v || !aff->ls)
2659
0
    return isl_aff_free(aff);
2660
0
2661
0
  aff = sort_divs(aff);
2662
0
2663
0
  return aff;
2664
0
}
2665
2666
__isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2667
66.6k
{
2668
66.6k
  isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2669
66.6k
  return isl_pw_aff_alloc(dom, aff);
2670
66.6k
}
2671
2672
3.46k
#define isl_aff_involves_nan isl_aff_is_nan
2673
2674
#undef PW
2675
232k
#define PW isl_pw_aff
2676
#undef EL
2677
87.4k
#define EL isl_aff
2678
#undef EL_IS_ZERO
2679
#define EL_IS_ZERO is_empty
2680
#undef ZERO
2681
#define ZERO empty
2682
#undef IS_ZERO
2683
#define IS_ZERO is_empty
2684
#undef FIELD
2685
1.32M
#define FIELD aff
2686
#undef DEFAULT_IS_ZERO
2687
9.75k
#define DEFAULT_IS_ZERO 0
2688
2689
#define NO_OPT
2690
#define NO_LIFT
2691
#define NO_MORPH
2692
2693
#include <isl_pw_templ.c>
2694
#include <isl_pw_eval.c>
2695
#include <isl_pw_hash.c>
2696
#include <isl_pw_union_opt.c>
2697
2698
#undef BASE
2699
#define BASE pw_aff
2700
2701
#include <isl_union_single.c>
2702
#include <isl_union_neg.c>
2703
2704
static __isl_give isl_set *align_params_pw_pw_set_and(
2705
  __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2706
  __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2707
            __isl_take isl_pw_aff *pwaff2))
2708
21.0k
{
2709
21.0k
  isl_bool equal_params;
2710
21.0k
2711
21.0k
  if (!pwaff1 || !pwaff2)
2712
0
    goto error;
2713
21.0k
  equal_params = isl_space_has_equal_params(pwaff1->dim, pwaff2->dim);
2714
21.0k
  if (equal_params < 0)
2715
0
    goto error;
2716
21.0k
  if (equal_params)
2717
19.4k
    return fn(pwaff1, pwaff2);
2718
1.55k
  if (isl_pw_aff_check_named_params(pwaff1) < 0 ||
2719
1.55k
      isl_pw_aff_check_named_params(pwaff2) < 0)
2720
0
    goto error;
2721
1.55k
  pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2722
1.55k
  pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2723
1.55k
  return fn(pwaff1, pwaff2);
2724
0
error:
2725
0
  isl_pw_aff_free(pwaff1);
2726
0
  isl_pw_aff_free(pwaff2);
2727
0
  return NULL;
2728
1.55k
}
2729
2730
/* Align the parameters of the to isl_pw_aff arguments and
2731
 * then apply a function "fn" on them that returns an isl_map.
2732
 */
2733
static __isl_give isl_map *align_params_pw_pw_map_and(
2734
  __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2735
  __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2736
            __isl_take isl_pw_aff *pa2))
2737
16
{
2738
16
  isl_bool equal_params;
2739
16
2740
16
  if (!pa1 || !pa2)
2741
0
    goto error;
2742
16
  equal_params = isl_space_has_equal_params(pa1->dim, pa2->dim);
2743
16
  if (equal_params < 0)
2744
0
    goto error;
2745
16
  if (equal_params)
2746
16
    return fn(pa1, pa2);
2747
0
  if (isl_pw_aff_check_named_params(pa1) < 0 ||
2748
0
      isl_pw_aff_check_named_params(pa2) < 0)
2749
0
    goto error;
2750
0
  pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2751
0
  pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2752
0
  return fn(pa1, pa2);
2753
0
error:
2754
0
  isl_pw_aff_free(pa1);
2755
0
  isl_pw_aff_free(pa2);
2756
0
  return NULL;
2757
0
}
2758
2759
/* Compute a piecewise quasi-affine expression with a domain that
2760
 * is the union of those of pwaff1 and pwaff2 and such that on each
2761
 * cell, the quasi-affine expression is the maximum of those of pwaff1
2762
 * and pwaff2.  If only one of pwaff1 or pwaff2 is defined on a given
2763
 * cell, then the associated expression is the defined one.
2764
 */
2765
static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2766
  __isl_take isl_pw_aff *pwaff2)
2767
178
{
2768
178
  return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2769
178
}
2770
2771
__isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2772
  __isl_take isl_pw_aff *pwaff2)
2773
178
{
2774
178
  return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2775
178
              &pw_aff_union_max);
2776
178
}
2777
2778
/* Compute a piecewise quasi-affine expression with a domain that
2779
 * is the union of those of pwaff1 and pwaff2 and such that on each
2780
 * cell, the quasi-affine expression is the minimum of those of pwaff1
2781
 * and pwaff2.  If only one of pwaff1 or pwaff2 is defined on a given
2782
 * cell, then the associated expression is the defined one.
2783
 */
2784
static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2785
  __isl_take isl_pw_aff *pwaff2)
2786
187
{
2787
187
  return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2788
187
}
2789
2790
__isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2791
  __isl_take isl_pw_aff *pwaff2)
2792
187
{
2793
187
  return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2794
187
              &pw_aff_union_min);
2795
187
}
2796
2797
__isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2798
  __isl_take isl_pw_aff *pwaff2, int max)
2799
365
{
2800
365
  if (max)
2801
178
    return isl_pw_aff_union_max(pwaff1, pwaff2);
2802
187
  else
2803
187
    return isl_pw_aff_union_min(pwaff1, pwaff2);
2804
365
}
2805
2806
/* Construct a map with as domain the domain of pwaff and
2807
 * one-dimensional range corresponding to the affine expressions.
2808
 */
2809
static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2810
23.4k
{
2811
23.4k
  int i;
2812
23.4k
  isl_space *dim;
2813
23.4k
  isl_map *map;
2814
23.4k
2815
23.4k
  if (!pwaff)
2816
0
    return NULL;
2817
23.4k
2818
23.4k
  dim = isl_pw_aff_get_space(pwaff);
2819
23.4k
  map = isl_map_empty(dim);
2820
23.4k
2821
47.1k
  for (i = 0; i < pwaff->n; 
++i23.6k
) {
2822
23.6k
    isl_basic_map *bmap;
2823
23.6k
    isl_map *map_i;
2824
23.6k
2825
23.6k
    bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2826
23.6k
    map_i = isl_map_from_basic_map(bmap);
2827
23.6k
    map_i = isl_map_intersect_domain(map_i,
2828
23.6k
            isl_set_copy(pwaff->p[i].set));
2829
23.6k
    map = isl_map_union_disjoint(map, map_i);
2830
23.6k
  }
2831
23.4k
2832
23.4k
  isl_pw_aff_free(pwaff);
2833
23.4k
2834
23.4k
  return map;
2835
23.4k
}
2836
2837
/* Construct a map with as domain the domain of pwaff and
2838
 * one-dimensional range corresponding to the affine expressions.
2839
 */
2840
__isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2841
23.4k
{
2842
23.4k
  if (!pwaff)
2843
0
    return NULL;
2844
23.4k
  if (isl_space_is_set(pwaff->dim))
2845
23.4k
    
isl_die0
(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2846
23.4k
      "space of input is not a map", goto error);
2847
23.4k
  return map_from_pw_aff(pwaff);
2848
0
error:
2849
0
  isl_pw_aff_free(pwaff);
2850
0
  return NULL;
2851
23.4k
}
2852
2853
/* Construct a one-dimensional set with as parameter domain
2854
 * the domain of pwaff and the single set dimension
2855
 * corresponding to the affine expressions.
2856
 */
2857
__isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2858
7
{
2859
7
  if (!pwaff)
2860
0
    return NULL;
2861
7
  if (!isl_space_is_set(pwaff->dim))
2862
7
    
isl_die0
(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2863
7
      "space of input is not a set", goto error);
2864
7
  return map_from_pw_aff(pwaff);
2865
0
error:
2866
0
  isl_pw_aff_free(pwaff);
2867
0
  return NULL;
2868
7
}
2869
2870
/* Return a set containing those elements in the domain
2871
 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2872
 * does not satisfy "fn" (if complement is 1).
2873
 *
2874
 * The pieces with a NaN never belong to the result since
2875
 * NaN does not satisfy any property.
2876
 */
2877
static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2878
  __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2879
  int complement)
2880
25.5k
{
2881
25.5k
  int i;
2882
25.5k
  isl_set *set;
2883
25.5k
2884
25.5k
  if (!pwaff)
2885
0
    return NULL;
2886
25.5k
2887
25.5k
  set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2888
25.5k
2889
52.1k
  for (i = 0; i < pwaff->n; 
++i26.5k
) {
2890
26.5k
    isl_basic_set *bset;
2891
26.5k
    isl_set *set_i, *locus;
2892
26.5k
    isl_bool rational;
2893
26.5k
2894
26.5k
    if (isl_aff_is_nan(pwaff->p[i].aff))
2895
0
      continue;
2896
26.5k
2897
26.5k
    rational = isl_set_has_rational(pwaff->p[i].set);
2898
26.5k
    bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2899
26.5k
    locus = isl_set_from_basic_set(bset);
2900
26.5k
    set_i = isl_set_copy(pwaff->p[i].set);
2901
26.5k
    if (complement)
2902
161
      set_i = isl_set_subtract(set_i, locus);
2903
26.4k
    else
2904
26.4k
      set_i = isl_set_intersect(set_i, locus);
2905
26.5k
    set = isl_set_union_disjoint(set, set_i);
2906
26.5k
  }
2907
25.5k
2908
25.5k
  isl_pw_aff_free(pwaff);
2909
25.5k
2910
25.5k
  return set;
2911
25.5k
}
2912
2913
/* Return a set containing those elements in the domain
2914
 * of "pa" where it is positive.
2915
 */
2916
__isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2917
156
{
2918
156
  return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2919
156
}
2920
2921
/* Return a set containing those elements in the domain
2922
 * of pwaff where it is non-negative.
2923
 */
2924
__isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2925
15.7k
{
2926
15.7k
  return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2927
15.7k
}
2928
2929
/* Return a set containing those elements in the domain
2930
 * of pwaff where it is zero.
2931
 */
2932
__isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2933
9.54k
{
2934
9.54k
  return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2935
9.54k
}
2936
2937
/* Return a set containing those elements in the domain
2938
 * of pwaff where it is not zero.
2939
 */
2940
__isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2941
101
{
2942
101
  return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2943
101
}
2944
2945
/* Return a set containing those elements in the shared domain
2946
 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2947
 *
2948
 * We compute the difference on the shared domain and then construct
2949
 * the set of values where this difference is non-negative.
2950
 * If strict is set, we first subtract 1 from the difference.
2951
 * If equal is set, we only return the elements where pwaff1 and pwaff2
2952
 * are equal.
2953
 */
2954
static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2955
  __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2956
16.9k
{
2957
16.9k
  isl_set *set1, *set2;
2958
16.9k
2959
16.9k
  set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2960
16.9k
  set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2961
16.9k
  set1 = isl_set_intersect(set1, set2);
2962
16.9k
  pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2963
16.9k
  pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2964
16.9k
  pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2965
16.9k
2966
16.9k
  if (strict) {
2967
10.1k
    isl_space *dim = isl_set_get_space(set1);
2968
10.1k
    isl_aff *aff;
2969
10.1k
    aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2970
10.1k
    aff = isl_aff_add_constant_si(aff, -1);
2971
10.1k
    pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2972
10.1k
  } else
2973
6.72k
    isl_set_free(set1);
2974
16.9k
2975
16.9k
  if (equal)
2976
1.28k
    return isl_pw_aff_zero_set(pwaff1);
2977
15.6k
  return isl_pw_aff_nonneg_set(pwaff1);
2978
15.6k
}
2979
2980
/* Return a set containing those elements in the shared domain
2981
 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2982
 */
2983
static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2984
  __isl_take isl_pw_aff *pwaff2)
2985
1.28k
{
2986
1.28k
  return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2987
1.28k
}
2988
2989
__isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2990
  __isl_take isl_pw_aff *pwaff2)
2991
1.28k
{
2992
1.28k
  return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2993
1.28k
}
2994
2995
/* Return a set containing those elements in the shared domain
2996
 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2997
 */
2998
static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2999
  __isl_take isl_pw_aff *pwaff2)
3000
5.44k
{
3001
5.44k
  return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
3002
5.44k
}
3003
3004
__isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
3005
  __isl_take isl_pw_aff *pwaff2)
3006
5.44k
{
3007
5.44k
  return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
3008
5.44k
}
3009
3010
/* Return a set containing those elements in the shared domain
3011
 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3012
 */
3013
static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3014
  __isl_take isl_pw_aff *pwaff2)
3015
10.1k
{
3016
10.1k
  return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
3017
10.1k
}
3018
3019
__isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3020
  __isl_take isl_pw_aff *pwaff2)
3021
10.1k
{
3022
10.1k
  return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
3023
10.1k
}
3024
3025
__isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
3026
  __isl_take isl_pw_aff *pwaff2)
3027
3.97k
{
3028
3.97k
  return isl_pw_aff_ge_set(pwaff2, pwaff1);
3029
3.97k
}
3030
3031
__isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
3032
  __isl_take isl_pw_aff *pwaff2)
3033
5.86k
{
3034
5.86k
  return isl_pw_aff_gt_set(pwaff2, pwaff1);
3035
5.86k
}
3036
3037
/* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3038
 * where the function values are ordered in the same way as "order",
3039
 * which returns a set in the shared domain of its two arguments.
3040
 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3041
 *
3042
 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3043
 * We first pull back the two functions such that they are defined on
3044
 * the domain [A -> B].  Then we apply "order", resulting in a set
3045
 * in the space [A -> B].  Finally, we unwrap this set to obtain
3046
 * a map in the space A -> B.
3047
 */
3048
static __isl_give isl_map *isl_pw_aff_order_map_aligned(
3049
  __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3050
  __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3051
    __isl_take isl_pw_aff *pa2))
3052
16
{
3053
16
  isl_space *space1, *space2;
3054
16
  isl_multi_aff *ma;
3055
16
  isl_set *set;
3056
16
3057
16
  space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3058
16
  space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3059
16
  space1 = isl_space_map_from_domain_and_range(space1, space2);
3060
16
  ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3061
16
  pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3062
16
  ma = isl_multi_aff_range_map(space1);
3063
16
  pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3064
16
  set = order(pa1, pa2);
3065
16
3066
16
  return isl_set_unwrap(set);
3067
16
}
3068
3069
/* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3070
 * where the function values are equal.
3071
 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3072
 */
3073
static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
3074
  __isl_take isl_pw_aff *pa2)
3075
12
{
3076
12
  return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
3077
12
}
3078
3079
/* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3080
 * where the function values are equal.
3081
 */
3082
__isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3083
  __isl_take isl_pw_aff *pa2)
3084
12
{
3085
12
  return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3086
12
}
3087
3088
/* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3089
 * where the function value of "pa1" is less than the function value of "pa2".
3090
 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3091
 */
3092
static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3093
  __isl_take isl_pw_aff *pa2)
3094
2
{
3095
2
  return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3096
2
}
3097
3098
/* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3099
 * where the function value of "pa1" is less than the function value of "pa2".
3100
 */
3101
__isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3102
  __isl_take isl_pw_aff *pa2)
3103
2
{
3104
2
  return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3105
2
}
3106
3107
/* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3108
 * where the function value of "pa1" is greater than the function value
3109
 * of "pa2".
3110
 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3111
 */
3112
static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3113
  __isl_take isl_pw_aff *pa2)
3114
2
{
3115
2
  return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3116
2
}
3117
3118
/* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3119
 * where the function value of "pa1" is greater than the function value
3120
 * of "pa2".
3121
 */
3122
__isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3123
  __isl_take isl_pw_aff *pa2)
3124
2
{
3125
2
  return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3126
2
}
3127
3128
/* Return a set containing those elements in the shared domain
3129
 * of the elements of list1 and list2 where each element in list1
3130
 * has the relation specified by "fn" with each element in list2.
3131
 */
3132
static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3133
  __isl_take isl_pw_aff_list *list2,
3134
  __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3135
            __isl_take isl_pw_aff *pwaff2))
3136
5.49k
{
3137
5.49k
  int i, j;
3138
5.49k
  isl_ctx *ctx;
3139
5.49k
  isl_set *set;
3140
5.49k
3141
5.49k
  if (!list1 || !list2)
3142
0
    goto error;
3143
5.49k
3144
5.49k
  ctx = isl_pw_aff_list_get_ctx(list1);
3145
5.49k
  if (list1->n < 1 || list2->n < 1)
3146
5.49k
    
isl_die0
(ctx, isl_error_invalid,
3147
5.49k
      "list should contain at least one element", goto error);
3148
5.49k
3149
5.49k
  set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3150
11.1k
  for (i = 0; i < list1->n; 
++i5.62k
)
3151
11.4k
    
for (j = 0; 5.62k
j < list2->n;
++j5.81k
) {
3152
5.81k
      isl_set *set_ij;
3153
5.81k
3154
5.81k
      set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3155
5.81k
            isl_pw_aff_copy(list2->p[j]));
3156
5.81k
      set = isl_set_intersect(set, set_ij);
3157
5.81k
    }
3158
5.49k
3159
5.49k
  isl_pw_aff_list_free(list1);
3160
5.49k
  isl_pw_aff_list_free(list2);
3161
5.49k
  return set;
3162
0
error:
3163
0
  isl_pw_aff_list_free(list1);
3164
0
  isl_pw_aff_list_free(list2);
3165
0
  return NULL;
3166
5.49k
}
3167
3168
/* Return a set containing those elements in the shared domain
3169
 * of the elements of list1 and list2 where each element in list1
3170
 * is equal to each element in list2.
3171
 */
3172
__isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3173
  __isl_take isl_pw_aff_list *list2)
3174
539
{
3175
539
  return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3176
539
}
3177
3178
__isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3179
  __isl_take isl_pw_aff_list *list2)
3180
12
{
3181
12
  return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3182
12
}
3183
3184
/* Return a set containing those elements in the shared domain
3185
 * of the elements of list1 and list2 where each element in list1
3186
 * is less than or equal to each element in list2.
3187
 */
3188
__isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3189
  __isl_take isl_pw_aff_list *list2)
3190
3.08k
{
3191
3.08k
  return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3192
3.08k
}
3193
3194
__isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3195
  __isl_take isl_pw_aff_list *list2)
3196
391
{
3197
391
  return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3198
391
}
3199
3200
__isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3201
  __isl_take isl_pw_aff_list *list2)
3202
1.34k
{
3203
1.34k
  return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3204
1.34k
}
3205
3206
__isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3207
  __isl_take isl_pw_aff_list *list2)
3208
125
{
3209
125
  return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3210
125
}
3211
3212
3213
/* Return a set containing those elements in the shared domain
3214
 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3215
 */
3216
static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3217
  __isl_take isl_pw_aff *pwaff2)
3218
4.08k
{
3219
4.08k
  isl_set *set_lt, *set_gt;
3220
4.08k
3221
4.08k
  set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3222
4.08k
           isl_pw_aff_copy(pwaff2));
3223
4.08k
  set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3224
4.08k
  return isl_set_union_disjoint(set_lt, set_gt);
3225
4.08k
}
3226
3227
__isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3228
  __isl_take isl_pw_aff *pwaff2)
3229
4.08k
{
3230
4.08k
  return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3231
4.08k
}
3232
3233
__isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3234
  isl_int v)
3235
3.57k
{
3236
3.57k
  int i;
3237
3.57k
3238
3.57k
  if (isl_int_is_one(v))
3239
3.57k
    
return pwaff0
;
3240
3.57k
  if (!isl_int_is_pos(v))
3241
3.57k
    
isl_die0
(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3242
3.57k
      "factor needs to be positive",
3243
3.57k
      return isl_pw_aff_free(pwaff));
3244
3.57k
  pwaff = isl_pw_aff_cow(pwaff);
3245
3.57k
  if (!pwaff)
3246
0
    return NULL;
3247
3.57k
  if (pwaff->n == 0)
3248
0
    return pwaff;
3249
3.57k
3250
7.44k
  
for (i = 0; 3.57k
i < pwaff->n;
++i3.87k
) {
3251
3.87k
    pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3252
3.87k
    if (!pwaff->p[i].aff)
3253
0
      return isl_pw_aff_free(pwaff);
3254
3.87k
  }
3255
3.57k
3256
3.57k
  return pwaff;
3257
3.57k
}
3258
3259
__isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3260
7.55k
{
3261
7.55k
  int i;
3262
7.55k
3263
7.55k
  pwaff = isl_pw_aff_cow(pwaff);
3264
7.55k
  if (!pwaff)
3265
0
    return NULL;
3266
7.55k
  if (pwaff->n == 0)
3267
0
    return pwaff;
3268
7.55k
3269
15.4k
  
for (i = 0; 7.55k
i < pwaff->n;
++i7.90k
) {
3270
7.90k
    pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3271
7.90k
    if (!pwaff->p[i].aff)
3272
0
      return isl_pw_aff_free(pwaff);
3273
7.90k
  }
3274
7.55k
3275
7.55k
  return pwaff;
3276
7.55k
}
3277
3278
__isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3279
111
{
3280
111
  int i;
3281
111
3282
111
  pwaff = isl_pw_aff_cow(pwaff);
3283
111
  if (!pwaff)
3284
0
    return NULL;
3285
111
  if (pwaff->n == 0)
3286
0
    return pwaff;
3287
111
3288
224
  
for (i = 0; 111
i < pwaff->n;
++i113
) {
3289
113
    pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3290
113
    if (!pwaff->p[i].aff)
3291
0
      return isl_pw_aff_free(pwaff);
3292
113
  }
3293
111
3294
111
  return pwaff;
3295
111
}
3296
3297
/* Assuming that "cond1" and "cond2" are disjoint,
3298
 * return an affine expression that is equal to pwaff1 on cond1
3299
 * and to pwaff2 on cond2.
3300
 */
3301
static __isl_give isl_pw_aff *isl_pw_aff_select(
3302
  __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3303
  __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3304
188
{
3305
188
  pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3306
188
  pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3307
188
3308
188
  return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3309
188
}
3310
3311
/* Return an affine expression that is equal to pwaff_true for elements
3312
 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3313
 * is zero.
3314
 * That is, return cond ? pwaff_true : pwaff_false;
3315
 *
3316
 * If "cond" involves and NaN, then we conservatively return a NaN
3317
 * on its entire domain.  In principle, we could consider the pieces
3318
 * where it is NaN separately from those where it is not.
3319
 *
3320
 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3321
 * then only use the domain of "cond" to restrict the domain.
3322
 */
3323
__isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3324
  __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3325
112
{
3326
112
  isl_set *cond_true, *cond_false;
3327
112
  isl_bool equal;
3328
112
3329
112
  if (!cond)
3330
0
    goto error;
3331
112
  if (isl_pw_aff_involves_nan(cond)) {
3332
0
    isl_space *space = isl_pw_aff_get_domain_space(cond);
3333
0
    isl_local_space *ls = isl_local_space_from_space(space);
3334
0
    isl_pw_aff_free(cond);
3335
0
    isl_pw_aff_free(pwaff_true);
3336
0
    isl_pw_aff_free(pwaff_false);
3337
0
    return isl_pw_aff_nan_on_domain(ls);
3338
0
  }
3339
112
3340
112
  pwaff_true = isl_pw_aff_align_params(pwaff_true,
3341
112
              isl_pw_aff_get_space(pwaff_false));
3342
112
  pwaff_false = isl_pw_aff_align_params(pwaff_false,
3343
112
              isl_pw_aff_get_space(pwaff_true));
3344
112
  equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3345
112
  if (equal < 0)
3346
0
    goto error;
3347
112
  if (equal) {
3348
11
    isl_set *dom;
3349
11
3350
11
    dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3351
11
    isl_pw_aff_free(pwaff_false);
3352
11
    return isl_pw_aff_intersect_domain(pwaff_true, dom);
3353
11
  }
3354
101
3355
101
  cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3356
101
  cond_false = isl_pw_aff_zero_set(cond);
3357
101
  return isl_pw_aff_select(cond_true, pwaff_true,
3358
101
         cond_false, pwaff_false);
3359
0
error:
3360
0
  isl_pw_aff_free(cond);
3361
0
  isl_pw_aff_free(pwaff_true);
3362
0
  isl_pw_aff_free(pwaff_false);
3363
0
  return NULL;
3364
101
}
3365
3366
isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3367
43.8k
{
3368
43.8k
  if (!aff)
3369
0
    return isl_bool_error;
3370
43.8k
3371
43.8k
  return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3372
43.8k
}
3373
3374
/* Check whether pwaff is a piecewise constant.
3375
 */
3376
isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3377
328
{
3378
328
  int i;
3379
328
3380
328
  if (!pwaff)
3381
0
    return isl_bool_error;
3382
328
3383
656
  
for (i = 0; 328
i < pwaff->n;
++i328
) {
3384
328
    isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3385
328
    if (is_cst < 0 || !is_cst)
3386
0
      return is_cst;
3387
328
  }
3388
328
3389
328
  return isl_bool_true;
3390
328
}
3391
3392
/* Are all elements of "mpa" piecewise constants?
3393
 */
3394
isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3395
0
{
3396
0
  int i;
3397
0
3398
0
  if (!mpa)
3399
0
    return isl_bool_error;
3400
0
3401
0
  for (i = 0; i < mpa->n; ++i) {
3402
0
    isl_bool is_cst = isl_pw_aff_is_cst(mpa->u.p[i]);
3403
0
    if (is_cst < 0 || !is_cst)
3404
0
      return is_cst;
3405
0
  }
3406
0
3407
0
  return isl_bool_true;
3408
0
}
3409
3410
/* Return the product of "aff1" and "aff2".
3411
 *
3412
 * If either of the two is NaN, then the result is NaN.
3413
 *
3414
 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3415
 */
3416
__isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3417
  __isl_take isl_aff *aff2)
3418
19.2k
{
3419
19.2k
  if (!aff1 || !aff2)
3420
0
    goto error;
3421
19.2k
3422
19.2k
  if (isl_aff_is_nan(aff1)) {
3423
2
    isl_aff_free(aff2);
3424
2
    return aff1;
3425
2
  }
3426
19.2k
  if (isl_aff_is_nan(aff2)) {
3427
2
    isl_aff_free(aff1);
3428
2
    return aff2;
3429
2
  }
3430
19.2k
3431
19.2k
  if (!isl_aff_is_cst(aff2) && 
isl_aff_is_cst(aff1)3.13k
)
3432
3.13k
    return isl_aff_mul(aff2, aff1);
3433
16.1k
3434
16.1k
  if (!isl_aff_is_cst(aff2))
3435
16.1k
    
isl_die0
(isl_aff_get_ctx(aff1), isl_error_invalid,
3436
16.1k
      "at least one affine expression should be constant",
3437
16.1k
      goto error);
3438
16.1k
3439
16.1k
  aff1 = isl_aff_cow(aff1);
3440
16.1k
  if (!aff1 || !aff2)
3441
0
    goto error;
3442
16.1k
3443
16.1k
  aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3444
16.1k
  aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3445
16.1k
3446
16.1k
  isl_aff_free(aff2);
3447
16.1k
  return aff1;
3448
0
error:
3449
0
  isl_aff_free(aff1);
3450
0
  isl_aff_free(aff2);
3451
0
  return NULL;
3452
16.1k
}
3453
3454
/* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3455
 *
3456
 * If either of the two is NaN, then the result is NaN.
3457
 */
3458
__isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3459
  __isl_take isl_aff *aff2)
3460
198
{
3461
198
  int is_cst;
3462
198
  int neg;
3463
198
3464
198
  if (!aff1 || !aff2)
3465
0
    goto error;
3466
198
3467
198
  if (isl_aff_is_nan(aff1)) {
3468
2
    isl_aff_free(aff2);
3469
2
    return aff1;
3470
2
  }
3471
196
  if (isl_aff_is_nan(aff2)) {
3472
2
    isl_aff_free(aff1);
3473
2
    return aff2;
3474
2
  }
3475
194
3476
194
  is_cst = isl_aff_is_cst(aff2);
3477
194
  if (is_cst < 0)
3478
0
    goto error;
3479
194
  if (!is_cst)
3480
194
    
isl_die0
(isl_aff_get_ctx(aff2), isl_error_invalid,
3481
194
      "second argument should be a constant", goto error);
3482
194
3483
194
  if (!aff2)
3484
0
    goto error;
3485
194
3486
194
  neg = isl_int_is_neg(aff2->v->el[1]);
3487
194
  if (neg) {
3488
23
    isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3489
23
    isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3490
23
  }
3491
194
3492
194
  aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3493
194
  aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3494
194
3495
194
  if (neg) {
3496
23
    isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3497
23
    isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3498
23
  }
3499
194
3500
194
  isl_aff_free(aff2);
3501
194
  return aff1;
3502
0
error:
3503
0
  isl_aff_free(aff1);
3504
0
  isl_aff_free(aff2);
3505
0
  return NULL;
3506
194
}
3507
3508
static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3509
  __isl_take isl_pw_aff *pwaff2)
3510
63.6k
{
3511
63.6k
  return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3512
63.6k
}
3513
3514
__isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3515
  __isl_take isl_pw_aff *pwaff2)
3516
63.6k
{
3517
63.6k
  return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3518
63.6k
}
3519
3520
__isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3521
  __isl_take isl_pw_aff *pwaff2)
3522
3.44k
{
3523
3.44k
  return isl_pw_aff_union_add_(pwaff1, pwaff2);
3524
3.44k
}
3525
3526
static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3527
  __isl_take isl_pw_aff *pwaff2)
3528
15.6k
{
3529
15.6k
  return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3530
15.6k
}
3531
3532
__isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3533
  __isl_take isl_pw_aff *pwaff2)
3534
15.6k
{
3535
15.6k
  return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3536
15.6k
}
3537
3538
static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3539
  __isl_take isl_pw_aff *pa2)
3540
164
{
3541
164
  return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3542
164
}
3543
3544
/* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3545
 */
3546
__isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3547
  __isl_take isl_pw_aff *pa2)
3548
164
{
3549
164
  int is_cst;
3550
164
3551
164
  is_cst = isl_pw_aff_is_cst(pa2);
3552
164
  if (is_cst < 0)
3553
0
    goto error;
3554
164
  if (!is_cst)
3555
164
    
isl_die0
(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3556
164
      "second argument should be a piecewise constant",
3557
164
      goto error);
3558
164
  return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3559
0
error:
3560
0
  isl_pw_aff_free(pa1);
3561
0
  isl_pw_aff_free(pa2);
3562
0
  return NULL;
3563
164
}
3564
3565
/* Compute the quotient of the integer division of "pa1" by "pa2"
3566
 * with rounding towards zero.
3567
 * "pa2" is assumed to be a piecewise constant.
3568
 *
3569
 * In particular, return
3570
 *
3571
 *  pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3572
 *
3573
 */
3574
__isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3575
  __isl_take isl_pw_aff *pa2)
3576
111
{
3577
111
  int is_cst;
3578
111
  isl_set *cond;
3579
111
  isl_pw_aff *f, *c;
3580
111
3581
111
  is_cst = isl_pw_aff_is_cst(pa2);
3582
111
  if (is_cst < 0)
3583
0
    goto error;
3584
111
  if (!is_cst)
3585
111
    
isl_die0
(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3586
111
      "second argument should be a piecewise constant",
3587
111
      goto error);
3588
111
3589
111
  pa1 = isl_pw_aff_div(pa1, pa2);
3590
111
3591
111
  cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3592
111
  f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3593
111
  c = isl_pw_aff_ceil(pa1);
3594
111
  return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3595
0
error:
3596
0
  isl_pw_aff_free(pa1);
3597
0
  isl_pw_aff_free(pa2);
3598
0
  return NULL;
3599
111
}
3600
3601
/* Compute the remainder of the integer division of "pa1" by "pa2"
3602
 * with rounding towards zero.
3603
 * "pa2" is assumed to be a piecewise constant.
3604
 *
3605
 * In particular, return
3606
 *
3607
 *  pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3608
 *
3609
 */
3610
__isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3611
  __isl_take isl_pw_aff *pa2)
3612
52
{
3613
52
  int is_cst;
3614
52
  isl_pw_aff *res;
3615
52
3616
52
  is_cst = isl_pw_aff_is_cst(pa2);
3617
52
  if (is_cst < 0)
3618
0
    goto error;
3619
52
  if (!is_cst)
3620
52
    
isl_die0
(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3621
52
      "second argument should be a piecewise constant",
3622
52
      goto error);
3623
52
  res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3624
52
  res = isl_pw_aff_mul(pa2, res);
3625
52
  res = isl_pw_aff_sub(pa1, res);
3626
52
  return res;
3627
0
error:
3628
0
  isl_pw_aff_free(pa1);
3629
0
  isl_pw_aff_free(pa2);
3630
0
  return NULL;
3631
52
}
3632
3633
/* Does either of "pa1" or "pa2" involve any NaN2?
3634
 */
3635
static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3636
  __isl_keep isl_pw_aff *pa2)
3637
89
{
3638
89
  isl_bool has_nan;
3639
89
3640
89
  has_nan = isl_pw_aff_involves_nan(pa1);
3641
89
  if (has_nan < 0 || has_nan)
3642
1
    return has_nan;
3643
88
  return isl_pw_aff_involves_nan(pa2);
3644
88
}
3645
3646
/* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3647
 * by a NaN on their shared domain.
3648
 *
3649
 * In principle, the result could be refined to only being NaN
3650
 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3651
 */
3652
static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3653
  __isl_take isl_pw_aff *pa2)
3654
2
{
3655
2
  isl_local_space *ls;
3656
2
  isl_set *dom;
3657
2
  isl_pw_aff *pa;
3658
2
3659
2
  dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3660
2
  ls = isl_local_space_from_space(isl_set_get_space(dom));
3661
2
  pa = isl_pw_aff_nan_on_domain(ls);
3662
2
  pa = isl_pw_aff_intersect_domain(pa, dom);
3663
2
3664
2
  return pa;
3665
2
}
3666
3667
static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3668
  __isl_take isl_pw_aff *pwaff2)
3669
7
{
3670
7
  isl_set *le;
3671
7
  isl_set *dom;
3672
7
3673
7
  dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3674
7
        isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3675
7
  le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3676
7
        isl_pw_aff_copy(pwaff2));
3677
7
  dom = isl_set_subtract(dom, isl_set_copy(le));
3678
7
  return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3679
7
}
3680
3681
static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3682
  __isl_take isl_pw_aff *pwaff2)
3683
80
{
3684
80
  isl_set *ge;
3685
80
  isl_set *dom;
3686
80
3687
80
  dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3688
80
        isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3689
80
  ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3690
80
        isl_pw_aff_copy(pwaff2));
3691
80
  dom = isl_set_subtract(dom, isl_set_copy(ge));
3692
80
  return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3693
80
}
3694
3695
/* Return an expression for the minimum (if "max" is not set) or
3696
 * the maximum (if "max" is set) of "pa1" and "pa2".
3697
 * If either expression involves any NaN, then return a NaN
3698
 * on the shared domain as result.
3699
 */
3700
static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3701
  __isl_take isl_pw_aff *pa2, int max)
3702
89
{
3703
89
  isl_bool has_nan;
3704
89
3705
89
  has_nan = either_involves_nan(pa1, pa2);
3706
89
  if (has_nan < 0)
3707
0
    pa1 = isl_pw_aff_free(pa1);
3708
89
  else if (has_nan)
3709
2
    return replace_by_nan(pa1, pa2);
3710
87
3711
87
  if (max)
3712
80
    return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3713
7
  else
3714
7
    return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3715
87
}
3716
3717
/* Return an expression for the minimum of "pwaff1" and "pwaff2".
3718
 */
3719
__isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3720
  __isl_take isl_pw_aff *pwaff2)
3721
9
{
3722
9
  return pw_aff_min_max(pwaff1, pwaff2, 0);
3723
9
}
3724
3725
/* Return an expression for the maximum of "pwaff1" and "pwaff2".
3726
 */
3727
__isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3728
  __isl_take isl_pw_aff *pwaff2)
3729
80
{
3730
80
  return pw_aff_min_max(pwaff1, pwaff2, 1);
3731
80
}
3732
3733
static __isl_give isl_pw_aff *pw_aff_list_reduce(
3734
  __isl_take isl_pw_aff_list *list,
3735
  __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3736
          __isl_take isl_pw_aff *pwaff2))
3737
6
{
3738
6
  int i;
3739
6
  isl_ctx *ctx;
3740
6
  isl_pw_aff *res;
3741
6
3742
6
  if (!list)
3743
0
    return NULL;
3744
6
3745
6
  ctx = isl_pw_aff_list_get_ctx(list);
3746
6
  if (list->n < 1)
3747
6
    
isl_die0
(ctx, isl_error_invalid,
3748
6
      "list should contain at least one element", goto error);
3749
6
3750
6
  res = isl_pw_aff_copy(list->p[0]);
3751
12
  for (i = 1; i < list->n; 
++i6
)
3752
6
    res = fn(res, isl_pw_aff_copy(list->p[i]));
3753
6
3754
6
  isl_pw_aff_list_free(list);
3755
6
  return res;
3756
0
error:
3757
0
  isl_pw_aff_list_free(list);
3758
0
  return NULL;
3759
6
}
3760
3761
/* Return an isl_pw_aff that maps each element in the intersection of the
3762
 * domains of the elements of list to the minimal corresponding affine
3763
 * expression.
3764
 */
3765
__isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3766
5
{
3767
5
  return pw_aff_list_reduce(list, &isl_pw_aff_min);
3768
5
}
3769
3770
/* Return an isl_pw_aff that maps each element in the intersection of the
3771
 * domains of the elements of list to the maximal corresponding affine
3772
 * expression.
3773
 */
3774
__isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3775
1
{
3776
1
  return pw_aff_list_reduce(list, &isl_pw_aff_max);
3777
1
}
3778
3779
/* Mark the domains of "pwaff" as rational.
3780
 */
3781
__isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3782
307
{
3783
307
  int i;
3784
307
3785
307
  pwaff = isl_pw_aff_cow(pwaff);
3786
307
  if (!pwaff)
3787
0
    return NULL;
3788
307
  if (pwaff->n == 0)
3789
0
    return pwaff;
3790
307
3791
614
  
for (i = 0; 307
i < pwaff->n;
++i307
) {
3792
307
    pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3793
307
    if (!pwaff->p[i].set)
3794
0
      return isl_pw_aff_free(pwaff);
3795
307
  }
3796
307
3797
307
  return pwaff;
3798
307
}
3799
3800
/* Mark the domains of the elements of "list" as rational.
3801
 */
3802
__isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3803
  __isl_take isl_pw_aff_list *list)
3804
48
{
3805
48
  int i, n;
3806
48
3807
48
  if (!list)
3808
0
    return NULL;
3809
48
  if (list->n == 0)
3810
0
    return list;
3811
48
3812
48
  n = list->n;
3813
98
  for (i = 0; i < n; 
++i50
) {
3814
50
    isl_pw_aff *pa;
3815
50
3816
50
    pa = isl_pw_aff_list_get_pw_aff(list, i);
3817
50
    pa = isl_pw_aff_set_rational(pa);
3818
50
    list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3819
50
  }
3820
48
3821
48
  return list;
3822
48
}
3823
3824
/* Do the parameters of "aff" match those of "space"?
3825
 */
3826
isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3827
  __isl_keep isl_space *space)
3828
945k
{
3829
945k
  isl_space *aff_space;
3830
945k
  isl_bool match;
3831
945k
3832
945k
  if (!aff || !space)
3833
0
    return isl_bool_error;
3834
945k
3835
945k
  aff_space = isl_aff_get_domain_space(aff);
3836
945k
3837
945k
  match = isl_space_has_equal_params(space, aff_space);
3838
945k
3839
945k
  isl_space_free(aff_space);
3840
945k
  return match;
3841
945k
}
3842
3843
/* Check that the domain space of "aff" matches "space".
3844
 */
3845
isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3846
  __isl_keep isl_space *space)
3847
945k
{
3848
945k
  isl_space *aff_space;
3849
945k
  isl_bool match;
3850
945k
3851
945k
  if (!aff || !space)
3852
0
    return isl_stat_error;
3853
945k
3854
945k
  aff_space = isl_aff_get_domain_space(aff);
3855
945k
3856
945k
  match = isl_space_has_equal_params(space, aff_space);
3857
945k
  if (match < 0)
3858
0
    goto error;
3859
945k
  if (!match)
3860
945k
    
isl_die0
(isl_aff_get_ctx(aff), isl_error_invalid,
3861
945k
      "parameters don't match", goto error);
3862
945k
  match = isl_space_tuple_is_equal(space, isl_dim_in,
3863
945k
          aff_space, isl_dim_set);
3864
945k
  if (match < 0)
3865
0
    goto error;
3866
945k
  if (!match)
3867
945k
    
isl_die0
(isl_aff_get_ctx(aff), isl_error_invalid,
3868
945k
      "domains don't match", goto error);
3869
945k
  isl_space_free(aff_space);
3870
945k
  return isl_stat_ok;
3871
0
error:
3872
0
  isl_space_free(aff_space);
3873
0
  return isl_stat_error;
3874
945k
}
3875
3876
#undef BASE
3877
#define BASE aff
3878
#undef DOMBASE
3879
#define DOMBASE set
3880
#define NO_DOMAIN
3881
3882
#include <isl_multi_no_explicit_domain.c>
3883
#include <isl_multi_templ.c>
3884
#include <isl_multi_apply_set.c>
3885
#include <isl_multi_cmp.c>
3886
#include <isl_multi_dims.c>
3887
#include <isl_multi_floor.c>
3888
#include <isl_multi_gist.c>
3889
3890
#undef NO_DOMAIN
3891
3892
/* Construct an isl_multi_aff living in "space" that corresponds
3893
 * to the affine transformation matrix "mat".
3894
 */
3895
__isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3896
  __isl_take isl_space *space, __isl_take isl_mat *mat)
3897
2
{
3898
2
  isl_ctx *ctx;
3899
2
  isl_local_space *ls = NULL;
3900
2
  isl_multi_aff *ma = NULL;
3901
2
  int n_row, n_col, n_out, total;
3902
2
  int i;
3903
2
3904
2
  if (!space || !mat)
3905
0
    goto error;
3906
2
3907
2
  ctx = isl_mat_get_ctx(mat);
3908
2
3909
2
  n_row = isl_mat_rows(mat);
3910
2
  n_col = isl_mat_cols(mat);
3911
2
  if (n_row < 1)
3912
2
    
isl_die0
(ctx, isl_error_invalid,
3913
2
      "insufficient number of rows", goto error);
3914
2
  if (n_col < 1)
3915
2
    
isl_die0
(ctx, isl_error_invalid,
3916
2
      "insufficient number of columns", goto error);
3917
2
  n_out = isl_space_dim(space, isl_dim_out);
3918
2
  total = isl_space_dim(space, isl_dim_all);
3919
2
  if (1 + n_out != n_row || 2 + total != n_row + n_col)
3920
2
    
isl_die0
(ctx, isl_error_invalid,
3921
2
      "dimension mismatch", goto error);
3922
2
3923
2
  ma = isl_multi_aff_zero(isl_space_copy(space));
3924
2
  ls = isl_local_space_from_space(isl_space_domain(space));
3925
2
3926
4
  for (i = 0; i < n_row - 1; 
++i2
) {
3927
2
    isl_vec *v;
3928
2
    isl_aff *aff;
3929
2
3930
2
    v = isl_vec_alloc(ctx, 1 + n_col);
3931
2
    if (!v)
3932
0
      goto error;
3933
2
    isl_int_set(v->el[0], mat->row[0][0]);
3934
2
    isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3935
2
    v = isl_vec_normalize(v);
3936
2
    aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3937
2
    ma = isl_multi_aff_set_aff(ma, i, aff);
3938
2
  }
3939
2
3940
2
  isl_local_space_free(ls);
3941
2
  isl_mat_free(mat);
3942
2
  return ma;
3943
0
error:
3944
0
  isl_local_space_free(ls);
3945
0
  isl_mat_free(mat);
3946
0
  isl_multi_aff_free(ma);
3947
0
  return NULL;
3948
2
}
3949
3950
/* Remove any internal structure of the domain of "ma".
3951
 * If there is any such internal structure in the input,
3952
 * then the name of the corresponding space is also removed.
3953
 */
3954
__isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3955
  __isl_take isl_multi_aff *ma)
3956
0
{
3957
0
  isl_space *space;
3958
0
3959
0
  if (!ma)
3960
0
    return NULL;
3961
0
3962
0
  if (!ma->space->nested[0])
3963
0
    return ma;
3964
0
3965
0
  space = isl_multi_aff_get_space(ma);
3966
0
  space = isl_space_flatten_domain(space);
3967
0
  ma = isl_multi_aff_reset_space(ma, space);
3968
0
3969
0
  return ma;
3970
0
}
3971
3972
/* Given a map space, return an isl_multi_aff that maps a wrapped copy
3973
 * of the space to its domain.
3974
 */
3975
__isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3976
764
{
3977
764
  int i, n_in;
3978
764
  isl_local_space *ls;
3979
764
  isl_multi_aff *ma;
3980
764
3981
764
  if (!space)
3982
0
    return NULL;
3983
764
  if (!isl_space_is_map(space))
3984
764
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
3985
764
      "not a map space", goto error);
3986
764
3987
764
  n_in = isl_space_dim(space, isl_dim_in);
3988
764
  space = isl_space_domain_map(space);
3989
764
3990
764
  ma = isl_multi_aff_alloc(isl_space_copy(space));
3991
764
  if (n_in == 0) {
3992
4
    isl_space_free(space);
3993
4
    return ma;
3994
4
  }
3995
760
3996
760
  space = isl_space_domain(space);
3997
760
  ls = isl_local_space_from_space(space);
3998
1.89k
  for (i = 0; i < n_in; 
++i1.13k
) {
3999
1.13k
    isl_aff *aff;
4000
1.13k
4001
1.13k
    aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4002
1.13k
            isl_dim_set, i);
4003
1.13k
    ma = isl_multi_aff_set_aff(ma, i, aff);
4004
1.13k
  }
4005
760
  isl_local_space_free(ls);
4006
760
  return ma;
4007
0
error:
4008
0
  isl_space_free(space);
4009
0
  return NULL;
4010
760
}
4011
4012
/* Given a map space, return an isl_multi_aff that maps a wrapped copy
4013
 * of the space to its range.
4014
 */
4015
__isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4016
16
{
4017
16
  int i, n_in, n_out;
4018
16
  isl_local_space *ls;
4019
16
  isl_multi_aff *ma;
4020
16
4021
16
  if (!space)
4022
0
    return NULL;
4023
16
  if (!isl_space_is_map(space))
4024
16
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
4025
16
      "not a map space", goto error);
4026
16
4027
16
  n_in = isl_space_dim(space, isl_dim_in);
4028
16
  n_out = isl_space_dim(space, isl_dim_out);
4029
16
  space = isl_space_range_map(space);
4030
16
4031
16
  ma = isl_multi_aff_alloc(isl_space_copy(space));
4032
16
  if (n_out == 0) {
4033
0
    isl_space_free(space);
4034
0
    return ma;
4035
0
  }
4036
16
4037
16
  space = isl_space_domain(space);
4038
16
  ls = isl_local_space_from_space(space);
4039
48
  for (i = 0; i < n_out; 
++i32
) {
4040
32
    isl_aff *aff;
4041
32
4042
32
    aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4043
32
            isl_dim_set, n_in + i);
4044
32
    ma = isl_multi_aff_set_aff(ma, i, aff);
4045
32
  }
4046
16
  isl_local_space_free(ls);
4047
16
  return ma;
4048
0
error:
4049
0
  isl_space_free(space);
4050
0
  return NULL;
4051
16
}
4052
4053
/* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4054
 * of the space to its range.
4055
 */
4056
__isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4057
  __isl_take isl_space *space)
4058
0
{
4059
0
  return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4060
0
}
4061
4062
/* Given the space of a set and a range of set dimensions,
4063
 * construct an isl_multi_aff that projects out those dimensions.
4064
 */
4065
__isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4066
  __isl_take isl_space *space, enum isl_dim_type type,
4067
  unsigned first, unsigned n)
4068
2.60k
{
4069
2.60k
  int i, dim;
4070
2.60k
  isl_local_space *ls;
4071
2.60k
  isl_multi_aff *ma;
4072
2.60k
4073
2.60k
  if (!space)
4074
0
    return NULL;
4075
2.60k
  if (!isl_space_is_set(space))
4076
2.60k
    
isl_die0
(isl_space_get_ctx(space), isl_error_unsupported,
4077
2.60k
      "expecting set space", goto error);
4078
2.60k
  if (type != isl_dim_set)
4079
2.60k
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
4080
2.60k
      "only set dimensions can be projected out", goto error);
4081
2.60k
4082
2.60k
  dim = isl_space_dim(space, isl_dim_set);
4083
2.60k
  if (first + n > dim)
4084
2.60k
    
isl_die0
(isl_space_get_ctx(space), isl_error_invalid,
4085
2.60k
      "range out of bounds", goto error);
4086
2.60k
4087
2.60k
  space = isl_space_from_domain(space);
4088
2.60k
  space = isl_space_add_dims(space, isl_dim_out, dim - n);
4089
2.60k
4090
2.60k
  if (dim == n)
4091
0
    return isl_multi_aff_alloc(space);
4092
2.60k
4093
2.60k
  ma = isl_multi_aff_alloc(isl_space_copy(space));
4094
2.60k
  space = isl_space_domain(space);
4095
2.60k
  ls = isl_local_space_from_space(space);
4096
2.60k
4097
5.91k
  for (i = 0; i < first; 
++i3.30k
) {
4098
3.30k
    isl_aff *aff;
4099
3.30k
4100
3.30k
    aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4101
3.30k
            isl_dim_set, i);
4102
3.30k
    ma = isl_multi_aff_set_aff(ma, i, aff);
4103
3.30k
  }
4104
2.60k
4105
2.64k
  for (i = 0; i < dim - (first + n); 
++i40
) {
4106
40
    isl_aff *aff;
4107
40
4108
40
    aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4109
40
            isl_dim_set, first + n + i);
4110
40
    ma = isl_multi_aff_set_aff(ma, first + i, aff);
4111
40
  }
4112
2.60k
4113
2.60k
  isl_local_space_free(ls);
4114
2.60k
  return ma;
4115
0
error:
4116
0
  isl_space_free(space);
4117
0
  return NULL;
4118
2.60k
}
4119
4120
/* Given the space of a set and a range of set dimensions,
4121
 * construct an isl_pw_multi_aff that projects out those dimensions.
4122
 */
4123
__isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4124
  __isl_take isl_space *space, enum isl_dim_type type,
4125
  unsigned first, unsigned n)
4126
2.54k
{
4127
2.54k
  isl_multi_aff *ma;
4128
2.54k
4129
2.54k
  ma = isl_multi_aff_project_out_map(space, type, first, n);
4130
2.54k
  return isl_pw_multi_aff_from_multi_aff(ma);
4131
2.54k
}
4132
4133
/* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4134
 * domain.
4135
 */
4136
__isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
4137
  __isl_take isl_multi_aff *ma)
4138
5.79k
{
4139
5.79k
  isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
4140
5.79k
  return isl_pw_multi_aff_alloc(dom, ma);
4141
5.79k
}
4142
4143
/* Create a piecewise multi-affine expression in the given space that maps each
4144
 * input dimension to the corresponding output dimension.
4145
 */
4146
__isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4147
  __isl_take isl_space *space)
4148
26
{
4149
26
  return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4150
26
}
4151
4152
/* Exploit the equalities in "eq" to simplify the affine expressions.
4153
 */
4154
static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4155
  __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4156
2.99k
{
4157
2.99k
  int i;
4158
2.99k
4159
2.99k
  maff = isl_multi_aff_cow(maff);
4160
2.99k
  if (!maff || !eq)
4161
0
    goto error;
4162
2.99k
4163
9.31k
  
for (i = 0; 2.99k
i < maff->n;
++i6.32k
) {
4164
6.32k
    maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4165
6.32k
                isl_basic_set_copy(eq));
4166
6.32k
    if (!maff->u.p[i])
4167
0
      goto error;
4168
6.32k
  }
4169
2.99k
4170
2.99k
  isl_basic_set_free(eq);
4171
2.99k
  return maff;
4172
0
error:
4173
0
  isl_basic_set_free(eq);
4174
0
  isl_multi_aff_free(maff);
4175
0
  return NULL;
4176
2.99k
}
4177
4178
__isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4179
  isl_int f)
4180
0
{
4181
0
  int i;
4182
0
4183
0
  maff = isl_multi_aff_cow(maff);
4184
0
  if (!maff)
4185
0
    return NULL;
4186
0
4187
0
  for (i = 0; i < maff->n; ++i) {
4188
0
    maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4189
0
    if (!maff->u.p[i])
4190
0
      return isl_multi_aff_free(maff);
4191
0
  }
4192
0
4193
0
  return maff;
4194
0
}
4195
4196
__isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4197
  __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4198
2
{
4199
2
  maff1 = isl_multi_aff_add(maff1, maff2);
4200
2
  maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4201
2
  return maff1;
4202
2
}
4203
4204
int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4205
32.4k
{
4206
32.4k
  if (!maff)
4207
0
    return -1;
4208
32.4k
4209
32.4k
  return 0;
4210
32.4k
}
4211
4212
/* Return the set of domain elements where "ma1" is lexicographically
4213
 * smaller than or equal to "ma2".
4214
 */
4215
__isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4216
  __isl_take isl_multi_aff *ma2)
4217
640
{
4218
640
  return isl_multi_aff_lex_ge_set(ma2, ma1);
4219
640
}
4220
4221
/* Return the set of domain elements where "ma1" is lexicographically
4222
 * smaller than "ma2".
4223
 */
4224
__isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4225
  __isl_take isl_multi_aff *ma2)
4226
0
{
4227
0
  return isl_multi_aff_lex_gt_set(ma2, ma1);
4228
0
}
4229
4230
/* Return the set of domain elements where "ma1" and "ma2"
4231
 * satisfy "order".
4232
 */
4233
static __isl_give isl_set *isl_multi_aff_order_set(
4234
  __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4235
  __isl_give isl_map *order(__isl_take isl_space *set_space))
4236
1.40k
{
4237
1.40k
  isl_space *space;
4238
1.40k
  isl_map *map1, *map2;
4239
1.40k
  isl_map *map, *ge;
4240
1.40k
4241
1.40k
  map1 = isl_map_from_multi_aff(ma1);
4242
1.40k
  map2 = isl_map_from_multi_aff(ma2);
4243
1.40k
  map = isl_map_range_product(map1, map2);
4244
1.40k
  space = isl_space_range(isl_map_get_space(map));
4245
1.40k
  space = isl_space_domain(isl_space_unwrap(space));
4246
1.40k
  ge = order(space);
4247
1.40k
  map = isl_map_intersect_range(map, isl_map_wrap(ge));
4248
1.40k
4249
1.40k
  return isl_map_domain(map);
4250
1.40k
}
4251
4252
/* Return the set of domain elements where "ma1" is lexicographically
4253
 * greater than or equal to "ma2".
4254
 */
4255
__isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4256
  __isl_take isl_multi_aff *ma2)
4257
1.40k
{
4258
1.40k
  return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4259
1.40k
}
4260
4261
/* Return the set of domain elements where "ma1" is lexicographically
4262
 * greater than "ma2".
4263
 */
4264
__isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4265
  __isl_take isl_multi_aff *ma2)
4266
0
{
4267
0
  return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4268
0
}
4269
4270
#undef PW
4271
29.1k
#define PW isl_pw_multi_aff
4272
#undef EL
4273
5.60k
#define EL isl_multi_aff
4274
#undef EL_IS_ZERO
4275
#define EL_IS_ZERO is_empty
4276
#undef ZERO
4277
#define ZERO empty
4278
#undef IS_ZERO
4279
#define IS_ZERO is_empty
4280
#undef FIELD
4281
141k
#define FIELD maff
4282
#undef DEFAULT_IS_ZERO
4283
0
#define DEFAULT_IS_ZERO 0
4284
4285
#define NO_SUB
4286
#define NO_OPT
4287
#define NO_INSERT_DIMS
4288
#define NO_LIFT
4289
#define NO_MORPH
4290
4291
#include <isl_pw_templ.c>
4292
#include <isl_pw_union_opt.c>
4293
4294
#undef NO_SUB
4295
4296
#undef BASE
4297
#define BASE pw_multi_aff
4298
4299
#include <isl_union_multi.c>
4300
#include <isl_union_neg.c>
4301
4302
static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4303
  __isl_take isl_pw_multi_aff *pma1,
4304
  __isl_take isl_pw_multi_aff *pma2)
4305
971
{
4306
971
  return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4307
971
              &isl_multi_aff_lex_ge_set);
4308
971
}
4309
4310
/* Given two piecewise multi affine expressions, return a piecewise
4311
 * multi-affine expression defined on the union of the definition domains
4312
 * of the inputs that is equal to the lexicographic maximum of the two
4313
 * inputs on each cell.  If only one of the two inputs is defined on
4314
 * a given cell, then it is considered to be the maximum.
4315
 */
4316
__isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4317
  __isl_take isl_pw_multi_aff *pma1,
4318
  __isl_take isl_pw_multi_aff *pma2)
4319
971
{
4320
971
  return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4321
971
                &pw_multi_aff_union_lexmax);
4322
971
}
4323
4324
static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4325
  __isl_take isl_pw_multi_aff *pma1,
4326
  __isl_take isl_pw_multi_aff *pma2)
4327
688
{
4328
688
  return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4329
688
              &isl_multi_aff_lex_le_set);
4330
688
}
4331
4332
/* Given two piecewise multi affine expressions, return a piecewise
4333
 * multi-affine expression defined on the union of the definition domains
4334
 * of the inputs that is equal to the lexicographic minimum of the two
4335
 * inputs on each cell.  If only one of the two inputs is defined on
4336
 * a given cell, then it is considered to be the minimum.
4337
 */
4338
__isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4339
  __isl_take isl_pw_multi_aff *pma1,
4340
  __isl_take isl_pw_multi_aff *pma2)
4341
688
{
4342
688
  return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4343
688
                &pw_multi_aff_union_lexmin);
4344
688
}
4345
4346
static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4347
  __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4348
1
{
4349
1
  return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4350
1
            &isl_multi_aff_add);
4351
1
}
4352
4353
__isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4354
  __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4355
1
{
4356
1
  return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4357
1
            &pw_multi_aff_add);
4358
1
}
4359
4360
static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4361
  __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4362
0
{
4363
0
  return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4364
0
            &isl_multi_aff_sub);
4365
0
}
4366
4367
/* Subtract "pma2" from "pma1" and return the result.
4368
 */
4369
__isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4370
  __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4371
0
{
4372
0
  return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4373
0
            &pw_multi_aff_sub);
4374
0
}
4375
4376
__isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4377
  __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4378
461
{
4379
461
  return isl_pw_multi_aff_union_add_(pma1, pma2);
4380
461
}
4381
4382
/* Compute the sum of "upa1" and "upa2" on the union of their domains,
4383
 * with the actual sum on the shared domain and
4384
 * the defined expression on the symmetric difference of the domains.
4385
 */
4386
__isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4387
  __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4388
338
{
4389
338
  return isl_union_pw_aff_union_add_(upa1, upa2);
4390
338
}
4391
4392
/* Compute the sum of "upma1" and "upma2" on the union of their domains,
4393
 * with the actual sum on the shared domain and
4394
 * the defined expression on the symmetric difference of the domains.
4395
 */
4396
__isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4397
  __isl_take isl_union_pw_multi_aff *upma1,
4398
  __isl_take isl_union_pw_multi_aff *upma2)
4399
119
{
4400
119
  return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4401
119
}
4402
4403
/* Given two piecewise multi-affine expressions A -> B and C -> D,
4404
 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4405
 */
4406
static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4407
  __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4408
1
{
4409
1
  int i, j, n;
4410
1
  isl_space *space;
4411
1
  isl_pw_multi_aff *res;
4412
1
4413
1
  if (!pma1 || !pma2)
4414
0
    goto error;
4415
1
4416
1
  n = pma1->n * pma2->n;
4417
1
  space = isl_space_product(isl_space_copy(pma1->dim),
4418
1
          isl_space_copy(pma2->dim));
4419
1
  res = isl_pw_multi_aff_alloc_size(space, n);
4420
1
4421
3
  for (i = 0; i < pma1->n; 
++i2
) {
4422
4
    for (j = 0; j < pma2->n; 
++j2
) {
4423
2
      isl_set *domain;
4424
2
      isl_multi_aff *ma;
4425
2
4426
2
      domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4427
2
             isl_set_copy(pma2->p[j].set));
4428
2
      ma = isl_multi_aff_product(
4429
2
          isl_multi_aff_copy(pma1->p[i].maff),
4430
2
          isl_multi_aff_copy(pma2->p[j].maff));
4431
2
      res = isl_pw_multi_aff_add_piece(res, domain, ma);
4432
2
    }
4433
2
  }
4434
1
4435
1
  isl_pw_multi_aff_free(pma1);
4436
1
  isl_pw_multi_aff_free(pma2);
4437
1
  return res;
4438
0
error:
4439
0
  isl_pw_multi_aff_free(pma1);
4440
0
  isl_pw_multi_aff_free(pma2);
4441
0
  return NULL;
4442
1
}
4443
4444
__isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4445
  __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4446
1
{
4447
1
  return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4448
1
            &pw_multi_aff_product);
4449
1
}
4450
4451
/* Construct a map mapping the domain of the piecewise multi-affine expression
4452
 * to its range, with each dimension in the range equated to the
4453
 * corresponding affine expression on its cell.
4454
 *
4455
 * If the domain of "pma" is rational, then so is the constructed "map".
4456
 */
4457
__isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4458
8.00k
{
4459
8.00k
  int i;
4460
8.00k
  isl_map *map;
4461
8.00k
4462
8.00k
  if (!pma)
4463
0
    return NULL;
4464
8.00k
4465
8.00k
  map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4466
8.00k
4467
16.5k
  for (i = 0; i < pma->n; 
++i8.56k
) {
4468
8.56k
    isl_bool rational;
4469
8.56k
    isl_multi_aff *maff;
4470
8.56k
    isl_basic_map *bmap;
4471
8.56k
    isl_map *map_i;
4472
8.56k
4473
8.56k
    rational = isl_set_is_rational(pma->p[i].set);
4474
8.56k
    if (rational < 0)
4475
0
      map = isl_map_free(map);
4476
8.56k
    maff = isl_multi_aff_copy(pma->p[i].maff);
4477
8.56k
    bmap = isl_basic_map_from_multi_aff2(maff, rational);
4478
8.56k
    map_i = isl_map_from_basic_map(bmap);
4479
8.56k
    map_i = isl_map_intersect_domain(map_i,
4480
8.56k
            isl_set_copy(pma->p[i].set));
4481
8.56k
    map = isl_map_union_disjoint(map, map_i);
4482
8.56k
  }
4483
8.00k
4484
8.00k
  isl_pw_multi_aff_free(pma);
4485
8.00k
  return map;
4486
8.00k
}
4487
4488
__isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4489
7
{
4490
7
  if (!pma)
4491
0
    return NULL;
4492
7
4493
7
  if (!isl_space_is_set(pma->dim))
4494
7
    
isl_die0
(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4495
7
      "isl_pw_multi_aff cannot be converted into an isl_set",
4496
7
      goto error);
4497
7
4498
7
  return isl_map_from_pw_multi_aff(pma);
4499
0
error:
4500
0
  isl_pw_multi_aff_free(pma);
4501
0
  return NULL;
4502
7
}
4503
4504
/* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4505
 * denominator "denom".
4506
 * "denom" is allowed to be negative, in which case the actual denominator
4507
 * is -denom and the expressions are added instead.
4508
 */
4509
static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4510
  __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4511
10.0k
{
4512
10.0k
  int i, first;
4513
10.0k
  int sign;
4514
10.0k
  isl_int d;
4515
10.0k
4516
10.0k
  first = isl_seq_first_non_zero(c, n);
4517
10.0k
  if (first == -1)
4518
10.0k
    return aff;
4519
3
4520
3
  sign = isl_int_sgn(denom);
4521
3
  isl_int_init(d);
4522
3
  isl_int_abs(d, denom);
4523
6
  for (i = first; i < n; 
++i3
) {
4524
3
    isl_aff *aff_i;
4525
3
4526
3
    if (isl_int_is_zero(c[i]))
4527
3
      
continue0
;
4528
3
    aff_i = isl_multi_aff_get_aff(ma, i);
4529
3
    aff_i = isl_aff_scale(aff_i, c[i]);
4530
3
    aff_i = isl_aff_scale_down(aff_i, d);
4531
3
    if (sign >= 0)
4532
2
      aff = isl_aff_sub(aff, aff_i);
4533
1
    else
4534
1
      aff = isl_aff_add(aff, aff_i);
4535
3
  }
4536
3
  isl_int_clear(d);
4537
3
4538
3
  return aff;
4539
3
}
4540
4541
/* Extract an affine expression that expresses the output dimension "pos"
4542
 * of "bmap" in terms of the parameters and input dimensions from
4543
 * equality "eq".
4544
 * Note that this expression may involve integer divisions defined
4545
 * in terms of parameters and input dimensions.
4546
 * The equality may also involve references to earlier (but not later)
4547
 * output dimensions.  These are replaced by the corresponding elements
4548
 * in "ma".
4549
 *
4550
 * If the equality is of the form
4551
 *
4552
 *  f(i) + h(j) + a x + g(i) = 0,
4553
 *
4554
 * with f(i) a linear combinations of the parameters and input dimensions,
4555
 * g(i) a linear combination of integer divisions defined in terms of the same
4556
 * and h(j) a linear combinations of earlier output dimensions,
4557
 * then the affine expression is
4558
 *
4559
 *  (-f(i) - g(i))/a - h(j)/a
4560
 *
4561
 * If the equality is of the form
4562
 *
4563
 *  f(i) + h(j) - a x + g(i) = 0,
4564
 *
4565
 * then the affine expression is
4566
 *
4567
 *  (f(i) + g(i))/a - h(j)/(-a)
4568
 *
4569
 *
4570
 * If "div" refers to an integer division (i.e., it is smaller than
4571
 * the number of integer divisions), then the equality constraint
4572
 * does involve an integer division (the one at position "div") that
4573
 * is defined in terms of output dimensions.  However, this integer
4574
 * division can be eliminated by exploiting a pair of constraints
4575
 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4576
 * in the equality constraint.  "ineq" refers to inequality x >= l, i.e.,
4577
 * -l + x >= 0.
4578
 * In particular, let
4579
 *
4580
 *  x = e(i) + m floor(...)
4581
 *
4582
 * with e(i) the expression derived above and floor(...) the integer
4583
 * division involving output dimensions.
4584
 * From
4585
 *
4586
 *  l <= x <= l + n,
4587
 *
4588
 * we have
4589
 *
4590
 *  0 <= x - l <= n
4591
 *
4592
 * This means
4593
 *
4594
 *  e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4595
 *                          = (e(i) - l) mod m
4596
 *
4597
 * Therefore,
4598
 *
4599
 *  x - l = (e(i) - l) mod m
4600
 *
4601
 * or
4602
 *
4603
 *  x = ((e(i) - l) mod m) + l
4604
 *
4605
 * The variable "shift" below contains the expression -l, which may
4606
 * also involve a linear combination of earlier output dimensions.
4607
 */
4608
static __isl_give isl_aff *extract_aff_from_equality(
4609
  __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4610
  __isl_keep isl_multi_aff *ma)
4611
9.96k
{
4612
9.96k
  unsigned o_out;
4613
9.96k
  unsigned n_div, n_out;
4614
9.96k
  isl_ctx *ctx;
4615
9.96k
  isl_local_space *ls;
4616
9.96k
  isl_aff *aff, *shift;
4617
9.96k
  isl_val *mod;
4618
9.96k
4619
9.96k
  ctx = isl_basic_map_get_ctx(bmap);
4620
9.96k
  ls = isl_basic_map_get_local_space(bmap);
4621
9.96k
  ls = isl_local_space_domain(ls);
4622
9.96k
  aff = isl_aff_alloc(isl_local_space_copy(ls));
4623
9.96k
  if (!aff)
4624
0
    goto error;
4625
9.96k
  o_out = isl_basic_map_offset(bmap, isl_dim_out);
4626
9.96k
  n_out = isl_basic_map_dim(bmap, isl_dim_out);
4627
9.96k
  n_div = isl_basic_map_dim(bmap, isl_dim_div);
4628
9.96k
  if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4629
319
    isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4630
319
    isl_seq_cpy(aff->v->el + 1 + o_out,
4631
319
          bmap->eq[eq] + o_out + n_out, n_div);
4632
9.64k
  } else {
4633
9.64k
    isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4634
9.64k
    isl_seq_neg(aff->v->el + 1 + o_out,
4635
9.64k
          bmap->eq[eq] + o_out + n_out, n_div);
4636
9.64k
  }
4637
9.96k
  if (div < n_div)
4638
9.96k
    
isl_int_set_si65
(aff->v->el[1 + o_out + div], 0);
4639
9.96k
  isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4640
9.96k
  aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4641
9.96k
          bmap->eq[eq][o_out + pos]);
4642
9.96k
  if (div < n_div) {
4643
65
    shift = isl_aff_alloc(isl_local_space_copy(ls));
4644
65
    if (!shift)
4645
0
      goto error;
4646
65
    isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4647
65
    isl_seq_cpy(shift->v->el + 1 + o_out,
4648
65
          bmap->ineq[ineq] + o_out + n_out, n_div);
4649
65
    isl_int_set_si(shift->v->el[0], 1);
4650
65
    shift = subtract_initial(shift, ma, pos,
4651
65
          bmap->ineq[ineq] + o_out, ctx->negone);
4652
65
    aff = isl_aff_add(aff, isl_aff_copy(shift));
4653
65
    mod = isl_val_int_from_isl_int(ctx,
4654
65
              bmap->eq[eq][o_out + n_out + div]);
4655
65
    mod = isl_val_abs(mod);
4656
65
    aff = isl_aff_mod_val(aff, mod);
4657
65
    aff = isl_aff_sub(aff, shift);
4658
65
  }
4659
9.96k
4660
9.96k
  isl_local_space_free(ls);
4661
9.96k
  return aff;
4662
0
error:
4663
0
  isl_local_space_free(ls);
4664
0
  isl_aff_free(aff);
4665
0
  return NULL;
4666
9.96k
}
4667
4668
/* Given a basic map with output dimensions defined
4669
 * in terms of the parameters input dimensions and earlier
4670
 * output dimensions using an equality (and possibly a pair on inequalities),
4671
 * extract an isl_aff that expresses output dimension "pos" in terms
4672
 * of the parameters and input dimensions.
4673
 * Note that this expression may involve integer divisions defined
4674
 * in terms of parameters and input dimensions.
4675
 * "ma" contains the expressions corresponding to earlier output dimensions.
4676
 *
4677
 * This function shares some similarities with
4678
 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4679
 */
4680
static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4681
  __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4682
9.96k
{
4683
9.96k
  int eq, div, ineq;
4684
9.96k
  isl_aff *aff;
4685
9.96k
4686
9.96k
  if (!bmap)
4687
0
    return NULL;
4688
9.96k
  eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4689
9.96k
  if (eq >= bmap->n_eq)
4690
9.96k
    
isl_die0
(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4691
9.96k
      "unable to find suitable equality", return NULL);
4692
9.96k
  aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4693
9.96k
4694
9.96k
  aff = isl_aff_remove_unused_divs(aff);
4695
9.96k
  return aff;
4696
9.96k
}
4697
4698
/* Given a basic map where each output dimension is defined
4699
 * in terms of the parameters and input dimensions using an equality,
4700
 * extract an isl_multi_aff that expresses the output dimensions in terms
4701
 * of the parameters and input dimensions.
4702
 */
4703
static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4704
  __isl_take isl_basic_map *bmap)
4705
6.53k
{
4706
6.53k
  int i;
4707
6.53k
  unsigned n_out;
4708
6.53k
  isl_multi_aff *ma;
4709
6.53k
4710
6.53k
  if (!bmap)
4711
0
    return NULL;
4712
6.53k
4713
6.53k
  ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4714
6.53k
  n_out = isl_basic_map_dim(bmap, isl_dim_out);
4715
6.53k
4716
16.5k
  for (i = 0; i < n_out; 
++i9.96k
) {
4717
9.96k
    isl_aff *aff;
4718
9.96k
4719
9.96k
    aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4720
9.96k
    ma = isl_multi_aff_set_aff(ma, i, aff);
4721
9.96k
  }
4722
6.53k
4723
6.53k
  isl_basic_map_free(bmap);
4724
6.53k
4725
6.53k
  return ma;
4726
6.53k
}
4727
4728
/* Given a basic set where each set dimension is defined
4729
 * in terms of the parameters using an equality,
4730
 * extract an isl_multi_aff that expresses the set dimensions in terms
4731
 * of the parameters.
4732
 */
4733
__isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4734
  __isl_take isl_basic_set *bset)
4735
9
{
4736
9
  return extract_isl_multi_aff_from_basic_map(bset);
4737
9
}
4738
4739
/* Create an isl_pw_multi_aff that is equivalent to
4740
 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4741
 * The given basic map is such that each output dimension is defined
4742
 * in terms of the parameters and input dimensions using an equality.
4743
 *
4744
 * Since some applications expect the result of isl_pw_multi_aff_from_map
4745
 * to only contain integer affine expressions, we compute the floor
4746
 * of the expression before returning.
4747
 *
4748
 * Remove all constraints involving local variables without
4749
 * an explicit representation (resulting in the removal of those
4750
 * local variables) prior to the actual extraction to ensure
4751
 * that the local spaces in which the resulting affine expressions
4752
 * are created do not contain any unknown local variables.
4753
 * Removing such constraints is safe because constraints involving
4754
 * unknown local variables are not used to determine whether
4755
 * a basic map is obviously single-valued.
4756
 */
4757
static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4758
  __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4759
6.53k
{
4760
6.53k
  isl_multi_aff *ma;
4761
6.53k
4762
6.53k
  bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4763
6.53k
  ma = extract_isl_multi_aff_from_basic_map(bmap);
4764
6.53k
  ma = isl_multi_aff_floor(ma);
4765
6.53k
  return isl_pw_multi_aff_alloc(domain, ma);
4766
6.53k
}
4767
4768
/* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4769
 * This obviously only works if the input "map" is single-valued.
4770
 * If so, we compute the lexicographic minimum of the image in the form
4771
 * of an isl_pw_multi_aff.  Since the image is unique, it is equal
4772
 * to its lexicographic minimum.
4773
 * If the input is not single-valued, we produce an error.
4774
 */
4775
static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4776
  __isl_take isl_map *map)
4777
19
{
4778
19
  int i;
4779
19
  int sv;
4780
19
  isl_pw_multi_aff *pma;
4781
19
4782
19
  sv = isl_map_is_single_valued(map);
4783
19
  if (sv < 0)
4784
0
    goto error;
4785
19
  if (!sv)
4786
19
    
isl_die0
(isl_map_get_ctx(map), isl_error_invalid,
4787
19
      "map is not single-valued", goto error);
4788
19
  map = isl_map_make_disjoint(map);
4789
19
  if (!map)
4790
0
    return NULL;
4791
19
4792
19
  pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4793
19
4794
83
  for (i = 0; i < map->n; 
++i64
) {
4795
64
    isl_pw_multi_aff *pma_i;
4796
64
    isl_basic_map *bmap;
4797
64
    bmap = isl_basic_map_copy(map->p[i]);
4798
64
    pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4799
64
    pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4800
64
  }
4801
19
4802
19
  isl_map_free(map);
4803
19
  return pma;
4804
0
error:
4805
0
  isl_map_free(map);
4806
0
  return NULL;
4807
19
}
4808
4809
/* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4810
 * taking into account that the output dimension at position "d"
4811
 * can be represented as
4812
 *
4813
 *  x = floor((e(...) + c1) / m)
4814
 *
4815
 * given that constraint "i" is of the form
4816
 *
4817
 *  e(...) + c1 - m x >= 0
4818
 *
4819
 *
4820
 * Let "map" be of the form
4821
 *
4822
 *  A -> B
4823
 *
4824
 * We construct a mapping
4825
 *
4826
 *  A -> [A -> x = floor(...)]
4827
 *
4828
 * apply that to the map, obtaining
4829
 *
4830
 *  [A -> x = floor(...)] -> B
4831
 *
4832
 * and equate dimension "d" to x.
4833
 * We then compute a isl_pw_multi_aff representation of the resulting map
4834
 * and plug in the mapping above.
4835
 */
4836
static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4837
  __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4838
16
{
4839
16
  isl_ctx *ctx;
4840
16
  isl_space *space;
4841
16
  isl_local_space *ls;
4842
16
  isl_multi_aff *ma;
4843
16
  isl_aff *aff;
4844
16
  isl_vec *v;
4845
16
  isl_map *insert;
4846
16
  int offset;
4847
16
  int n;
4848
16
  int n_in;
4849
16
  isl_pw_multi_aff *pma;
4850
16
  isl_bool is_set;
4851
16
4852
16
  is_set = isl_map_is_set(map);
4853
16
  if (is_set < 0)
4854
0
    goto error;
4855
16
4856
16
  offset = isl_basic_map_offset(hull, isl_dim_out);
4857
16
  ctx = isl_map_get_ctx(map);
4858
16
  space = isl_space_domain(isl_map_get_space(map));
4859
16
  n_in = isl_space_dim(space, isl_dim_set);
4860
16
  n = isl_space_dim(space, isl_dim_all);
4861
16
4862
16
  v = isl_vec_alloc(ctx, 1 + 1 + n);
4863
16
  if (v) {
4864
16
    isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4865
16
    isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4866
16
  }
4867
16
  isl_basic_map_free(hull);
4868
16
4869
16
  ls = isl_local_space_from_space(isl_space_copy(space));
4870
16
  aff = isl_aff_alloc_vec(ls, v);
4871
16
  aff = isl_aff_floor(aff);
4872
16
  if (is_set) {
4873
1
    isl_space_free(space);
4874
1
    ma = isl_multi_aff_from_aff(aff);
4875
15
  } else {
4876
15
    ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4877
15
    ma = isl_multi_aff_range_product(ma,
4878
15
            isl_multi_aff_from_aff(aff));
4879
15
  }
4880
16
4881
16
  insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4882
16
  map = isl_map_apply_domain(map, insert);
4883
16
  map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4884
16
  pma = isl_pw_multi_aff_from_map(map);
4885
16
  pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4886
16
4887
16
  return pma;
4888
0
error:
4889
0
  isl_map_free(map);
4890
0
  isl_basic_map_free(hull);
4891
0
  return NULL;
4892
16
}
4893
4894
/* Is constraint "c" of the form
4895
 *
4896
 *  e(...) + c1 - m x >= 0
4897
 *
4898
 * or
4899
 *
4900
 *  -e(...) + c2 + m x >= 0
4901
 *
4902
 * where m > 1 and e only depends on parameters and input dimemnsions?
4903
 *
4904
 * "offset" is the offset of the output dimensions
4905
 * "pos" is the position of output dimension x.
4906
 */
4907
static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4908
167
{
4909
167
  if (isl_int_is_zero(c[offset + d]))
4910
167
    
return 0124
;
4911
43
  if (isl_int_is_one(c[offset + d]))
4912
43
    
return 017
;
4913
26
  if (isl_int_is_negone(c[offset + d]))
4914
26
    
return 010
;
4915
16
  if (isl_seq_first_non_zero(c + offset, d) != -1)
4916
0
    return 0;
4917
16
  if (isl_seq_first_non_zero(c + offset + d + 1,
4918
16
            total - (offset + d + 1)) != -1)
4919
0
    return 0;
4920
16
  return 1;
4921
16
}
4922
4923
/* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4924
 *
4925
 * As a special case, we first check if there is any pair of constraints,
4926
 * shared by all the basic maps in "map" that force a given dimension
4927
 * to be equal to the floor of some affine combination of the input dimensions.
4928
 *
4929
 * In particular, if we can find two constraints
4930
 *
4931
 *  e(...) + c1 - m x >= 0    i.e.,   m x <= e(...) + c1
4932
 *
4933
 * and
4934
 *
4935
 *  -e(...) + c2 + m x >= 0   i.e.,   m x >= e(...) - c2
4936
 *
4937
 * where m > 1 and e only depends on parameters and input dimemnsions,
4938
 * and such that
4939
 *
4940
 *  c1 + c2 < m     i.e.,   -c2 >= c1 - (m - 1)
4941
 *
4942
 * then we know that we can take
4943
 *
4944
 *  x = floor((e(...) + c1) / m)
4945
 *
4946
 * without having to perform any computation.
4947
 *
4948
 * Note that we know that
4949
 *
4950
 *  c1 + c2 >= 1
4951
 *
4952
 * If c1 + c2 were 0, then we would have detected an equality during
4953
 * simplification.  If c1 + c2 were negative, then we would have detected
4954
 * a contradiction.
4955
 */
4956
static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4957
  __isl_take isl_map *map)
4958
35
{
4959
35
  int d, dim;
4960
35
  int i, j, n;
4961
35
  int offset, total;
4962
35
  isl_int sum;
4963
35
  isl_basic_map *hull;
4964
35
4965
35
  hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4966
35
  if (!hull)
4967
0
    goto error;
4968
35
4969
35
  isl_int_init(sum);
4970
35
  dim = isl_map_dim(map, isl_dim_out);
4971
35
  offset = isl_basic_map_offset(hull, isl_dim_out);
4972
35
  total = 1 + isl_basic_map_total_dim(hull);
4973
35
  n = hull->n_ineq;
4974
81
  for (d = 0; d < dim; 
++d46
) {
4975
213
    for (i = 0; i < n; 
++i151
) {
4976
167
      if (!is_potential_div_constraint(hull->ineq[i],
4977
167
              offset, d, total))
4978
151
        continue;
4979
21
      
for (j = i + 1; 16
j < n;
++j5
) {
4980
21
        if (!isl_seq_is_neg(hull->ineq[i] + 1,
4981
21
            hull->ineq[j] + 1, total - 1))
4982
5
          continue;
4983
16
        isl_int_add(sum, hull->ineq[i][0],
4984
16
            hull->ineq[j][0]);
4985
16
        if (isl_int_abs_lt(sum,
4986
16
                hull->ineq[i][offset + d]))
4987
16
          break;
4988
16
4989
16
      }
4990
16
      if (j >= n)
4991
0
        continue;
4992
16
      isl_int_clear(sum);
4993
16
      if (isl_int_is_pos(hull->ineq[j][offset + d]))
4994
16
        
j = i1
;
4995
16
      return pw_multi_aff_from_map_div(map, hull, d, j);
4996
16
    }
4997
62
  }
4998
35
  
isl_int_clear19
(sum);
4999
19
  isl_basic_map_free(hull);
5000
19
  return pw_multi_aff_from_map_base(map);
5001
0
error:
5002
0
  isl_map_free(map);
5003
0
  isl_basic_map_free(hull);
5004
0
  return NULL;
5005
35
}
5006
5007
/* Given an affine expression
5008
 *
5009
 *  [A -> B] -> f(A,B)
5010
 *
5011
 * construct an isl_multi_aff
5012
 *
5013
 *  [A -> B] -> B'
5014
 *
5015
 * such that dimension "d" in B' is set to "aff" and the remaining
5016
 * dimensions are set equal to the corresponding dimensions in B.
5017
 * "n_in" is the dimension of the space A.
5018
 * "n_out" is the dimension of the space B.
5019
 *
5020
 * If "is_set" is set, then the affine expression is of the form
5021
 *
5022
 *  [B] -> f(B)
5023
 *
5024
 * and we construct an isl_multi_aff
5025
 *
5026
 *  B -> B'
5027
 */
5028
static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5029
  unsigned n_in, unsigned n_out, int is_set)
5030
5
{
5031
5
  int i;
5032
5
  isl_multi_aff *ma;
5033
5
  isl_space *space, *space2;
5034
5
  isl_local_space *ls;
5035
5
5036
5
  space = isl_aff_get_domain_space(aff);
5037
5
  ls = isl_local_space_from_space(isl_space_copy(space));
5038
5
  space2 = isl_space_copy(space);
5039
5
  if (!is_set)
5040
5
    space2 = isl_space_range(isl_space_unwrap(space2));
5041
5
  space = isl_space_map_from_domain_and_range(space, space2);
5042
5
  ma = isl_multi_aff_alloc(space);
5043
5
  ma = isl_multi_aff_set_aff(ma, d, aff);
5044
5
5045
26
  for (i = 0; i < n_out; 
++i21
) {
5046
21
    if (i == d)
5047
5
      continue;
5048
16
    aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5049
16
            isl_dim_set, n_in + i);
5050
16
    ma = isl_multi_aff_set_aff(ma, i, aff);
5051
16
  }
5052
5
5053
5
  isl_local_space_free(ls);
5054
5
5055
5
  return ma;
5056
5
}
5057
5058
/* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5059
 * taking into account that the dimension at position "d" can be written as
5060
 *
5061
 *  x = m a + f(..)           (1)
5062
 *
5063
 * where m is equal to "gcd".
5064
 * "i" is the index of the equality in "hull" that defines f(..).
5065
 * In particular, the equality is of the form
5066
 *
5067
 *  f(..) - x + m g(existentials) = 0
5068
 *
5069
 * or
5070
 *
5071
 *  -f(..) + x + m g(existentials) = 0
5072
 *
5073
 * We basically plug (1) into "map", resulting in a map with "a"
5074
 * in the range instead of "x".  The corresponding isl_pw_multi_aff
5075
 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5076
 *
5077
 * Specifically, given the input map
5078
 *
5079
 *  A -> B
5080
 *
5081
 * We first wrap it into a set
5082
 *
5083
 *  [A -> B]
5084
 *
5085
 * and define (1) on top of the corresponding space, resulting in "aff".
5086
 * We use this to create an isl_multi_aff that maps the output position "d"
5087
 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5088
 * We plug this into the wrapped map, unwrap the result and compute the
5089
 * corresponding isl_pw_multi_aff.
5090
 * The result is an expression
5091
 *
5092
 *  A -> T(A)
5093
 *
5094
 * We adjust that to
5095
 *
5096
 *  A -> [A -> T(A)]
5097
 *
5098
 * so that we can plug that into "aff", after extending the latter to
5099
 * a mapping
5100
 *
5101
 *  [A -> B] -> B'
5102
 *
5103
 *
5104
 * If "map" is actually a set, then there is no "A" space, meaning
5105
 * that we do not need to perform any wrapping, and that the result
5106
 * of the recursive call is of the form
5107
 *
5108
 *  [T]
5109
 *
5110
 * which is plugged into a mapping of the form
5111
 *
5112
 *  B -> B'
5113
 */
5114
static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5115
  __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5116
  isl_int gcd)
5117
5
{
5118
5
  isl_set *set;
5119
5
  isl_space *space;
5120
5
  isl_local_space *ls;
5121
5
  isl_aff *aff;
5122
5
  isl_multi_aff *ma;
5123
5
  isl_pw_multi_aff *pma, *id;
5124
5
  unsigned n_in;
5125
5
  unsigned o_out;
5126
5
  unsigned n_out;
5127
5
  isl_bool is_set;
5128
5
5129
5
  is_set = isl_map_is_set(map);
5130
5
  if (is_set < 0)
5131
0
    goto error;
5132
5
5133
5
  n_in = isl_basic_map_dim(hull, isl_dim_in);
5134
5
  n_out = isl_basic_map_dim(hull, isl_dim_out);
5135
5
  o_out = isl_basic_map_offset(hull, isl_dim_out);
5136
5
5137
5
  if (is_set)
5138
0
    set = map;
5139
5
  else
5140
5
    set = isl_map_wrap(map);
5141
5
  space = isl_space_map_from_set(isl_set_get_space(set));
5142
5
  ma = isl_multi_aff_identity(space);
5143
5
  ls = isl_local_space_from_space(isl_set_get_space(set));
5144
5
  aff = isl_aff_alloc(ls);
5145
5
  if (aff) {
5146
5
    isl_int_set_si(aff->v->el[0], 1);
5147
5
    if (isl_int_is_one(hull->eq[i][o_out + d]))
5148
5
      isl_seq_neg(aff->v->el + 1, hull->eq[i],
5149
0
            aff->v->size - 1);
5150
5
    else
5151
5
      isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5152
5
            aff->v->size - 1);
5153
5
    isl_int_set(aff->v->el[1 + o_out + d], gcd);
5154
5
  }
5155
5
  ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5156
5
  set = isl_set_preimage_multi_aff(set, ma);
5157
5
5158
5
  ma = range_map(aff, d, n_in, n_out, is_set);
5159
5
5160
5
  if (is_set)
5161
0
    map = set;
5162
5
  else
5163
5
    map = isl_set_unwrap(set);
5164
5
  pma = isl_pw_multi_aff_from_map(map);
5165
5
5166
5
  if (!is_set) {
5167
5
    space = isl_pw_multi_aff_get_domain_space(pma);
5168
5
    space = isl_space_map_from_set(space);
5169
5
    id = isl_pw_multi_aff_identity(space);
5170
5
    pma = isl_pw_multi_aff_range_product(id, pma);
5171
5
  }
5172
5
  id = isl_pw_multi_aff_from_multi_aff(ma);
5173
5
  pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5174
5
5175
5
  isl_basic_map_free(hull);
5176
5
  return pma;
5177
0
error:
5178
0
  isl_map_free(map);
5179
0
  isl_basic_map_free(hull);
5180
0
  return NULL;
5181
5
}
5182
5183
/* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5184
 * "hull" contains the equalities valid for "map".
5185
 *
5186
 * Check if any of the output dimensions is "strided".
5187
 * That is, we check if it can be written as
5188
 *
5189
 *  x = m a + f(..)
5190
 *
5191
 * with m greater than 1, a some combination of existentially quantified
5192
 * variables and f an expression in the parameters and input dimensions.
5193
 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5194
 *
5195
 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5196
 * special case.
5197
 */
5198
static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5199
  __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5200
40
{
5201
40
  int i, j;
5202
40
  unsigned n_out;
5203
40
  unsigned o_out;
5204
40
  unsigned n_div;
5205
40
  unsigned o_div;
5206
40
  isl_int gcd;
5207
40
5208
40
  n_div = isl_basic_map_dim(hull, isl_dim_div);
5209
40
  o_div = isl_basic_map_offset(hull, isl_dim_div);
5210
40
5211
40
  if (n_div == 0) {
5212
27
    isl_basic_map_free(hull);
5213
27
    return pw_multi_aff_from_map_check_div(map);
5214
27
  }
5215
13
5216
13
  isl_int_init(gcd);
5217
13
5218
13
  n_out = isl_basic_map_dim(hull, isl_dim_out);
5219
13
  o_out = isl_basic_map_offset(hull, isl_dim_out);
5220
13
5221
45
  for (i = 0; i < n_out; 
++i32
) {
5222
169
    for (j = 0; j < hull->n_eq; 
++j132
) {
5223
137
      isl_int *eq = hull->eq[j];
5224
137
      isl_pw_multi_aff *res;
5225
137
5226
137
      if (!isl_int_is_one(eq[o_out + i]) &&
5227
137
          
!118
isl_int_is_negone118
(eq[o_out + i]))
5228
137
        
continue113
;
5229
24
      if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5230
0
        continue;
5231
24
      if (isl_seq_first_non_zero(eq + o_out + i + 1,
5232
24
                n_out - (i + 1)) != -1)
5233
0
        continue;
5234
24
      isl_seq_gcd(eq + o_div, n_div, &gcd);
5235
24
      if (isl_int_is_zero(gcd))
5236
24
        
continue19
;
5237
5
      if (isl_int_is_one(gcd))
5238
5
        
continue0
;
5239
5
5240
5
      res = pw_multi_aff_from_map_stride(map, hull,
5241
5
                i, j, gcd);
5242
5
      isl_int_clear(gcd);
5243
5
      return res;
5244
5
    }
5245
37
  }
5246
13
5247
13
  
isl_int_clear8
(gcd);
5248
8
  isl_basic_map_free(hull);
5249
8
  return pw_multi_aff_from_map_check_div(map);
5250
13
}
5251
5252
/* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5253
 *
5254
 * As a special case, we first check if all output dimensions are uniquely
5255
 * defined in terms of the parameters and input dimensions over the entire
5256
 * domain.  If so, we extract the desired isl_pw_multi_aff directly
5257
 * from the affine hull of "map" and its domain.
5258
 *
5259
 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5260
 * special cases.
5261
 */
5262
__isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5263
6.57k
{
5264
6.57k
  isl_bool sv;
5265
6.57k
  isl_basic_map *hull;
5266
6.57k
5267
6.57k
  if (!map)
5268
0
    return NULL;
5269
6.57k
5270
6.57k
  if (isl_map_n_basic_map(map) == 1) {
5271
6.47k
    hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5272
6.47k
    hull = isl_basic_map_plain_affine_hull(hull);
5273
6.47k
    sv = isl_basic_map_plain_is_single_valued(hull);
5274
6.47k
    if (sv >= 0 && sv)
5275
6.32k
      return plain_pw_multi_aff_from_map(isl_map_domain(map),
5276
6.32k
                  hull);
5277
151
    isl_basic_map_free(hull);
5278
151
  }
5279
6.57k
  map = isl_map_detect_equalities(map);
5280
243
  hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5281
243
  sv = isl_basic_map_plain_is_single_valued(hull);
5282
243
  if (sv >= 0 && sv)
5283
203
    return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5284
40
  if (sv >= 0)
5285
40
    return pw_multi_aff_from_map_check_strides(map, hull);
5286
0
  isl_basic_map_free(hull);
5287
0
  isl_map_free(map);
5288
0
  return NULL;
5289
0
}
5290
5291
__isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5292
111
{
5293
111
  return isl_pw_multi_aff_from_map(set);
5294
111
}
5295
5296
/* Convert "map" into an isl_pw_multi_aff (if possible) and
5297
 * add it to *user.
5298
 */
5299
static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5300
887
{
5301
887
  isl_union_pw_multi_aff **upma = user;
5302
887
  isl_pw_multi_aff *pma;
5303
887
5304
887
  pma = isl_pw_multi_aff_from_map(map);
5305
887
  *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5306
887
5307
887
  return *upma ? isl_stat_ok : 
isl_stat_error0
;
5308
887
}
5309
5310
/* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5311
 * domain.
5312
 */
5313
__isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5314
  __isl_take isl_aff *aff)
5315
0
{
5316
0
  isl_multi_aff *ma;
5317
0
  isl_pw_multi_aff *pma;
5318
0
5319
0
  ma = isl_multi_aff_from_aff(aff);
5320
0
  pma = isl_pw_multi_aff_from_multi_aff(ma);
5321
0
  return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5322
0
}
5323
5324
/* Try and create an isl_union_pw_multi_aff that is equivalent
5325
 * to the given isl_union_map.
5326
 * The isl_union_map is required to be single-valued in each space.
5327
 * Otherwise, an error is produced.
5328
 */
5329
__isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5330
  __isl_take isl_union_map *umap)
5331
431
{
5332
431
  isl_space *space;
5333
431
  isl_union_pw_multi_aff *upma;
5334
431
5335
431
  space = isl_union_map_get_space(umap);
5336
431
  upma = isl_union_pw_multi_aff_empty(space);
5337
431
  if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5338
0
    upma = isl_union_pw_multi_aff_free(upma);
5339
431
  isl_union_map_free(umap);
5340
431
5341
431
  return upma;
5342
431
}
5343
5344
/* Try and create an isl_union_pw_multi_aff that is equivalent
5345
 * to the given isl_union_set.
5346
 * The isl_union_set is required to be a singleton in each space.
5347
 * Otherwise, an error is produced.
5348
 */
5349
__isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5350
  __isl_take isl_union_set *uset)
5351
1
{
5352
1
  return isl_union_pw_multi_aff_from_union_map(uset);
5353
1
}
5354
5355
/* Return the piecewise affine expression "set ? 1 : 0".
5356
 */
5357
__isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5358
112
{
5359
112
  isl_pw_aff *pa;
5360
112
  isl_space *space = isl_set_get_space(set);
5361
112
  isl_local_space *ls = isl_local_space_from_space(space);
5362
112
  isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5363
112
  isl_aff *one = isl_aff_zero_on_domain(ls);
5364
112
5365
112
  one = isl_aff_add_constant_si(one, 1);
5366
112
  pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5367
112
  set = isl_set_complement(set);
5368
112
  pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5369
112
5370
112
  return pa;
5371
112
}
5372
5373
/* Plug in "subs" for dimension "type", "pos" of "aff".
5374
 *
5375
 * Let i be the dimension to replace and let "subs" be of the form
5376
 *
5377
 *  f/d
5378
 *
5379
 * and "aff" of the form
5380
 *
5381
 *  (a i + g)/m
5382
 *
5383
 * The result is
5384
 *
5385
 *  (a f + d g')/(m d)
5386
 *
5387
 * where g' is the result of plugging in "subs" in each of the integer
5388
 * divisions in g.
5389
 */
5390
__isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5391
  enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5392
1.19k
{
5393
1.19k
  isl_ctx *ctx;
5394
1.19k
  isl_int v;
5395
1.19k
5396
1.19k
  aff = isl_aff_cow(aff);