941dcb6e05e171651c0e7dea22c8929e7bdc33d2
[platform/upstream/isl.git] / isl_aff.c
1 /*
2  * Copyright 2011      INRIA Saclay
3  * Copyright 2011      Sven Verdoolaege
4  *
5  * Use of this software is governed by the GNU LGPLv2.1 license
6  *
7  * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8  * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9  * 91893 Orsay, France
10  */
11
12 #include <isl_map_private.h>
13 #include <isl_aff_private.h>
14 #include <isl_dim_private.h>
15 #include <isl_local_space_private.h>
16 #include <isl_mat_private.h>
17 #include <isl/constraint.h>
18 #include <isl/seq.h>
19 #include <isl/set.h>
20
21 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
22         __isl_take isl_vec *v)
23 {
24         isl_aff *aff;
25
26         if (!ls || !v)
27                 goto error;
28
29         aff = isl_calloc_type(v->ctx, struct isl_aff);
30         if (!aff)
31                 goto error;
32
33         aff->ref = 1;
34         aff->ls = ls;
35         aff->v = v;
36
37         return aff;
38 error:
39         isl_local_space_free(ls);
40         isl_vec_free(v);
41         return NULL;
42 }
43
44 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
45 {
46         isl_ctx *ctx;
47         isl_vec *v;
48         unsigned total;
49
50         if (!ls)
51                 return NULL;
52
53         ctx = isl_local_space_get_ctx(ls);
54         if (!isl_local_space_divs_known(ls))
55                 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
56                         goto error);
57
58         total = isl_local_space_dim(ls, isl_dim_all);
59         v = isl_vec_alloc(ctx, 1 + 1 + total);
60         return isl_aff_alloc_vec(ls, v);
61 error:
62         isl_local_space_free(ls);
63         return NULL;
64 }
65
66 __isl_give isl_aff *isl_aff_zero(__isl_take isl_local_space *ls)
67 {
68         isl_aff *aff;
69
70         aff = isl_aff_alloc(ls);
71         if (!aff)
72                 return NULL;
73
74         isl_int_set_si(aff->v->el[0], 1);
75         isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
76
77         return aff;
78 }
79
80 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
81 {
82         if (!aff)
83                 return NULL;
84
85         aff->ref++;
86         return aff;
87 }
88
89 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
90 {
91         if (!aff)
92                 return NULL;
93
94         return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
95                                  isl_vec_copy(aff->v));
96 }
97
98 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
99 {
100         if (!aff)
101                 return NULL;
102
103         if (aff->ref == 1)
104                 return aff;
105         aff->ref--;
106         return isl_aff_dup(aff);
107 }
108
109 void *isl_aff_free(__isl_take isl_aff *aff)
110 {
111         if (!aff)
112                 return NULL;
113
114         if (--aff->ref > 0)
115                 return NULL;
116
117         isl_local_space_free(aff->ls);
118         isl_vec_free(aff->v);
119
120         free(aff);
121
122         return NULL;
123 }
124
125 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
126 {
127         return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
128 }
129
130 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
131 {
132         return aff ? isl_local_space_dim(aff->ls, type) : 0;
133 }
134
135 __isl_give isl_dim *isl_aff_get_dim(__isl_keep isl_aff *aff)
136 {
137         return aff ? isl_local_space_get_dim(aff->ls) : NULL;
138 }
139
140 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
141 {
142         return aff ? isl_local_space_copy(aff->ls) : NULL;
143 }
144
145 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
146         enum isl_dim_type type, unsigned pos)
147 {
148         return aff ? isl_local_space_get_dim_name(aff->ls, type, pos) : 0;
149 }
150
151 __isl_give isl_aff *isl_aff_reset_dim(__isl_take isl_aff *aff,
152         __isl_take isl_dim *dim)
153 {
154         aff = isl_aff_cow(aff);
155         if (!aff || !dim)
156                 goto error;
157
158         aff->ls = isl_local_space_reset_dim(aff->ls, dim);
159         if (!aff->ls)
160                 return isl_aff_free(aff);
161
162         return aff;
163 error:
164         isl_aff_free(aff);
165         isl_dim_free(dim);
166         return NULL;
167 }
168
169 /* Reorder the coefficients of the affine expression based
170  * on the given reodering.
171  * The reordering r is assumed to have been extended with the local
172  * variables.
173  */
174 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
175         __isl_take isl_reordering *r, int n_div)
176 {
177         isl_vec *res;
178         int i;
179
180         if (!vec || !r)
181                 goto error;
182
183         res = isl_vec_alloc(vec->ctx, 2 + isl_dim_total(r->dim) + n_div);
184         isl_seq_cpy(res->el, vec->el, 2);
185         isl_seq_clr(res->el + 2, res->size - 2);
186         for (i = 0; i < r->len; ++i)
187                 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
188
189         isl_reordering_free(r);
190         isl_vec_free(vec);
191         return res;
192 error:
193         isl_vec_free(vec);
194         isl_reordering_free(r);
195         return NULL;
196 }
197
198 /* Reorder the dimensions of "aff" according to the given reordering.
199  */
200 __isl_give isl_aff *isl_aff_realign(__isl_take isl_aff *aff,
201         __isl_take isl_reordering *r)
202 {
203         aff = isl_aff_cow(aff);
204         if (!aff)
205                 goto error;
206
207         r = isl_reordering_extend(r, aff->ls->div->n_row);
208         aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
209                                 aff->ls->div->n_row);
210         aff->ls = isl_local_space_realign(aff->ls, r);
211
212         if (!aff->v || !aff->ls)
213                 return isl_aff_free(aff);
214
215         return aff;
216 error:
217         isl_aff_free(aff);
218         isl_reordering_free(r);
219         return NULL;
220 }
221
222 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
223 {
224         if (!aff)
225                 return -1;
226
227         return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
228 }
229
230 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
231 {
232         int equal;
233
234         if (!aff1 || !aff2)
235                 return -1;
236
237         equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
238         if (equal < 0 || !equal)
239                 return equal;
240
241         return isl_vec_is_equal(aff1->v, aff2->v);
242 }
243
244 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
245 {
246         if (!aff)
247                 return -1;
248         isl_int_set(*v, aff->v->el[0]);
249         return 0;
250 }
251
252 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
253 {
254         if (!aff)
255                 return -1;
256         isl_int_set(*v, aff->v->el[1]);
257         return 0;
258 }
259
260 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
261         enum isl_dim_type type, int pos, isl_int *v)
262 {
263         if (!aff)
264                 return -1;
265
266         if (pos >= isl_local_space_dim(aff->ls, type))
267                 isl_die(aff->v->ctx, isl_error_invalid,
268                         "position out of bounds", return -1);
269
270         pos += isl_local_space_offset(aff->ls, type);
271         isl_int_set(*v, aff->v->el[1 + pos]);
272
273         return 0;
274 }
275
276 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
277 {
278         aff = isl_aff_cow(aff);
279         if (!aff)
280                 return NULL;
281
282         aff->v = isl_vec_cow(aff->v);
283         if (!aff->v)
284                 return isl_aff_free(aff);
285
286         isl_int_set(aff->v->el[0], v);
287
288         return aff;
289 }
290
291 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
292 {
293         aff = isl_aff_cow(aff);
294         if (!aff)
295                 return NULL;
296
297         aff->v = isl_vec_cow(aff->v);
298         if (!aff->v)
299                 return isl_aff_free(aff);
300
301         isl_int_set(aff->v->el[1], v);
302
303         return aff;
304 }
305
306 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
307 {
308         if (isl_int_is_zero(v))
309                 return aff;
310
311         aff = isl_aff_cow(aff);
312         if (!aff)
313                 return NULL;
314
315         aff->v = isl_vec_cow(aff->v);
316         if (!aff->v)
317                 return isl_aff_free(aff);
318
319         isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
320
321         return aff;
322 }
323
324 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
325 {
326         isl_int t;
327
328         isl_int_init(t);
329         isl_int_set_si(t, v);
330         aff = isl_aff_add_constant(aff, t);
331         isl_int_clear(t);
332
333         return aff;
334 }
335
336 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
337 {
338         aff = isl_aff_cow(aff);
339         if (!aff)
340                 return NULL;
341
342         aff->v = isl_vec_cow(aff->v);
343         if (!aff->v)
344                 return isl_aff_free(aff);
345
346         isl_int_set_si(aff->v->el[1], v);
347
348         return aff;
349 }
350
351 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
352         enum isl_dim_type type, int pos, isl_int v)
353 {
354         if (!aff)
355                 return NULL;
356
357         if (pos >= isl_local_space_dim(aff->ls, type))
358                 isl_die(aff->v->ctx, isl_error_invalid,
359                         "position out of bounds", return isl_aff_free(aff));
360
361         aff = isl_aff_cow(aff);
362         if (!aff)
363                 return NULL;
364
365         aff->v = isl_vec_cow(aff->v);
366         if (!aff->v)
367                 return isl_aff_free(aff);
368
369         pos += isl_local_space_offset(aff->ls, type);
370         isl_int_set(aff->v->el[1 + pos], v);
371
372         return aff;
373 }
374
375 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
376         enum isl_dim_type type, int pos, int v)
377 {
378         if (!aff)
379                 return NULL;
380
381         if (pos >= isl_local_space_dim(aff->ls, type))
382                 isl_die(aff->v->ctx, isl_error_invalid,
383                         "position out of bounds", return isl_aff_free(aff));
384
385         aff = isl_aff_cow(aff);
386         if (!aff)
387                 return NULL;
388
389         aff->v = isl_vec_cow(aff->v);
390         if (!aff->v)
391                 return isl_aff_free(aff);
392
393         pos += isl_local_space_offset(aff->ls, type);
394         isl_int_set_si(aff->v->el[1 + pos], v);
395
396         return aff;
397 }
398
399 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
400         enum isl_dim_type type, int pos, isl_int v)
401 {
402         if (!aff)
403                 return NULL;
404
405         if (pos >= isl_local_space_dim(aff->ls, type))
406                 isl_die(aff->v->ctx, isl_error_invalid,
407                         "position out of bounds", return isl_aff_free(aff));
408
409         aff = isl_aff_cow(aff);
410         if (!aff)
411                 return NULL;
412
413         aff->v = isl_vec_cow(aff->v);
414         if (!aff->v)
415                 return isl_aff_free(aff);
416
417         pos += isl_local_space_offset(aff->ls, type);
418         isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
419
420         return aff;
421 }
422
423 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
424         enum isl_dim_type type, int pos, int v)
425 {
426         isl_int t;
427
428         isl_int_init(t);
429         isl_int_set_si(t, v);
430         aff = isl_aff_add_coefficient(aff, type, pos, t);
431         isl_int_clear(t);
432
433         return aff;
434 }
435
436 __isl_give isl_div *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
437 {
438         if (!aff)
439                 return NULL;
440
441         return isl_local_space_get_div(aff->ls, pos);
442 }
443
444 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
445 {
446         aff = isl_aff_cow(aff);
447         if (!aff)
448                 return NULL;
449         aff->v = isl_vec_cow(aff->v);
450         if (!aff->v)
451                 return isl_aff_free(aff);
452
453         isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
454
455         return aff;
456 }
457
458 /* Given f, return floor(f).
459  * If f is an integer expression, then just return f.
460  * Otherwise, create a new div d = [f] and return the expression d.
461  */
462 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
463 {
464         int size;
465         isl_ctx *ctx;
466
467         if (!aff)
468                 return NULL;
469
470         if (isl_int_is_one(aff->v->el[0]))
471                 return aff;
472
473         aff = isl_aff_cow(aff);
474         if (!aff)
475                 return NULL;
476
477         aff->ls = isl_local_space_add_div(aff->ls, isl_vec_copy(aff->v));
478         if (!aff->ls)
479                 return isl_aff_free(aff);
480
481         ctx = isl_aff_get_ctx(aff);
482         size = aff->v->size;
483         isl_vec_free(aff->v);
484         aff->v = isl_vec_alloc(ctx, size + 1);
485         aff->v = isl_vec_clr(aff->v);
486         if (!aff->v)
487                 return isl_aff_free(aff);
488         isl_int_set_si(aff->v->el[0], 1);
489         isl_int_set_si(aff->v->el[size], 1);
490
491         return aff;
492 }
493
494 /* Given f, return ceil(f).
495  * If f is an integer expression, then just return f.
496  * Otherwise, create a new div d = [-f] and return the expression -d.
497  */
498 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
499 {
500         if (!aff)
501                 return NULL;
502
503         if (isl_int_is_one(aff->v->el[0]))
504                 return aff;
505
506         aff = isl_aff_neg(aff);
507         aff = isl_aff_floor(aff);
508         aff = isl_aff_neg(aff);
509
510         return aff;
511 }
512
513 /* Apply the expansion computed by isl_merge_divs.
514  * The expansion itself is given by "exp" while the resulting
515  * list of divs is given by "div".
516  */
517 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
518         __isl_take isl_mat *div, int *exp)
519 {
520         int i, j;
521         int old_n_div;
522         int new_n_div;
523         int offset;
524
525         aff = isl_aff_cow(aff);
526         if (!aff || !div)
527                 goto error;
528
529         old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
530         new_n_div = isl_mat_rows(div);
531         if (new_n_div < old_n_div)
532                 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
533                         "not an expansion", goto error);
534
535         aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
536         if (!aff->v)
537                 goto error;
538
539         offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
540         j = old_n_div - 1;
541         for (i = new_n_div - 1; i >= 0; --i) {
542                 if (j >= 0 && exp[j] == i) {
543                         if (i != j)
544                                 isl_int_swap(aff->v->el[offset + i],
545                                              aff->v->el[offset + j]);
546                         j--;
547                 } else
548                         isl_int_set_si(aff->v->el[offset + i], 0);
549         }
550
551         aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
552         if (!aff->ls)
553                 goto error;
554         isl_mat_free(div);
555         return aff;
556 error:
557         isl_aff_free(aff);
558         isl_mat_free(div);
559         return NULL;
560 }
561
562 /* Add two affine expressions that live in the same local space.
563  */
564 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
565         __isl_take isl_aff *aff2)
566 {
567         isl_int gcd, f;
568
569         aff1 = isl_aff_cow(aff1);
570         if (!aff1 || !aff2)
571                 goto error;
572
573         aff1->v = isl_vec_cow(aff1->v);
574         if (!aff1->v)
575                 goto error;
576
577         isl_int_init(gcd);
578         isl_int_init(f);
579         isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
580         isl_int_divexact(f, aff2->v->el[0], gcd);
581         isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
582         isl_int_divexact(f, aff1->v->el[0], gcd);
583         isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
584         isl_int_divexact(f, aff2->v->el[0], gcd);
585         isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
586         isl_int_clear(f);
587         isl_int_clear(gcd);
588
589         isl_aff_free(aff2);
590         return aff1;
591 error:
592         isl_aff_free(aff1);
593         isl_aff_free(aff2);
594         return NULL;
595 }
596
597 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
598         __isl_take isl_aff *aff2)
599 {
600         isl_ctx *ctx;
601         int *exp1 = NULL;
602         int *exp2 = NULL;
603         isl_mat *div;
604
605         if (!aff1 || !aff2)
606                 goto error;
607
608         ctx = isl_aff_get_ctx(aff1);
609         if (!isl_dim_equal(aff1->ls->dim, aff2->ls->dim))
610                 isl_die(ctx, isl_error_invalid,
611                         "spaces don't match", goto error);
612
613         if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
614                 return add_expanded(aff1, aff2);
615
616         exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
617         exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
618         if (!exp1 || !exp2)
619                 goto error;
620
621         div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
622         aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
623         aff2 = isl_aff_expand_divs(aff2, div, exp2);
624         free(exp1);
625         free(exp2);
626
627         return add_expanded(aff1, aff2);
628 error:
629         free(exp1);
630         free(exp2);
631         isl_aff_free(aff1);
632         isl_aff_free(aff2);
633         return NULL;
634 }
635
636 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
637         __isl_take isl_aff *aff2)
638 {
639         return isl_aff_add(aff1, isl_aff_neg(aff2));
640 }
641
642 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
643 {
644         isl_int gcd;
645
646         if (isl_int_is_one(f))
647                 return aff;
648
649         aff = isl_aff_cow(aff);
650         if (!aff)
651                 return NULL;
652         aff->v = isl_vec_cow(aff->v);
653         if (!aff->v)
654                 return isl_aff_free(aff);
655
656         isl_int_init(gcd);
657         isl_int_gcd(gcd, aff->v->el[0], f);
658         isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
659         isl_int_divexact(gcd, f, gcd);
660         isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
661         isl_int_clear(gcd);
662
663         return aff;
664 }
665
666 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
667 {
668         isl_int gcd;
669
670         if (isl_int_is_one(f))
671                 return aff;
672
673         aff = isl_aff_cow(aff);
674         if (!aff)
675                 return NULL;
676         aff->v = isl_vec_cow(aff->v);
677         if (!aff->v)
678                 return isl_aff_free(aff);
679
680         isl_int_init(gcd);
681         isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
682         isl_int_gcd(gcd, gcd, f);
683         isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
684         isl_int_divexact(gcd, f, gcd);
685         isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
686         isl_int_clear(gcd);
687
688         return aff;
689 }
690
691 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
692 {
693         isl_int v;
694
695         if (f == 1)
696                 return aff;
697
698         isl_int_init(v);
699         isl_int_set_ui(v, f);
700         aff = isl_aff_scale_down(aff, v);
701         isl_int_clear(v);
702
703         return aff;
704 }
705
706 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
707         enum isl_dim_type type, unsigned pos, const char *s)
708 {
709         aff = isl_aff_cow(aff);
710         if (!aff)
711                 return NULL;
712         aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
713         if (!aff->ls)
714                 return isl_aff_free(aff);
715
716         return aff;
717 }
718
719 /* Exploit the equalities in "eq" to simplify the affine expression
720  * and the expressions of the integer divisions in the local space.
721  * The integer divisions in this local space are assumed to appear
722  * as regular dimensions in "eq".
723  */
724 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
725         __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
726 {
727         int i, j;
728         unsigned total;
729         unsigned n_div;
730
731         if (!eq)
732                 goto error;
733         if (eq->n_eq == 0) {
734                 isl_basic_set_free(eq);
735                 return aff;
736         }
737
738         aff = isl_aff_cow(aff);
739         if (!aff)
740                 goto error;
741
742         aff->ls = isl_local_space_substitute_equalities(aff->ls,
743                                                         isl_basic_set_copy(eq));
744         if (!aff->ls)
745                 goto error;
746
747         total = 1 + isl_dim_total(eq->dim);
748         n_div = eq->n_div;
749         for (i = 0; i < eq->n_eq; ++i) {
750                 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
751                 if (j < 0 || j == 0 || j >= total)
752                         continue;
753
754                 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
755                                 &aff->v->el[0]);
756         }
757
758         isl_basic_set_free(eq);
759         return aff;
760 error:
761         isl_basic_set_free(eq);
762         isl_aff_free(aff);
763         return NULL;
764 }
765
766 /* Exploit the equalities in "eq" to simplify the affine expression
767  * and the expressions of the integer divisions in the local space.
768  */
769 static __isl_give isl_aff *isl_aff_substitute_equalities(
770         __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
771 {
772         int n_div;
773
774         if (!aff || !eq)
775                 goto error;
776         n_div = isl_local_space_dim(aff->ls, isl_dim_div);
777         if (n_div > 0)
778                 eq = isl_basic_set_add(eq, isl_dim_set, n_div);
779         return isl_aff_substitute_equalities_lifted(aff, eq);
780 error:
781         isl_basic_set_free(eq);
782         isl_aff_free(aff);
783         return NULL;
784 }
785
786 /* Look for equalities among the variables shared by context and aff
787  * and the integer divisions of aff, if any.
788  * The equalities are then used to eliminate coefficients and/or integer
789  * divisions from aff.
790  */
791 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
792         __isl_take isl_set *context)
793 {
794         isl_basic_set *hull;
795         int n_div;
796
797         if (!aff)
798                 goto error;
799         n_div = isl_local_space_dim(aff->ls, isl_dim_div);
800         if (n_div > 0) {
801                 isl_basic_set *bset;
802                 context = isl_set_add_dims(context, isl_dim_set, n_div);
803                 bset = isl_basic_set_from_local_space(
804                                             isl_aff_get_local_space(aff));
805                 bset = isl_basic_set_lift(bset);
806                 bset = isl_basic_set_flatten(bset);
807                 context = isl_set_intersect(context,
808                                             isl_set_from_basic_set(bset));
809         }
810
811         hull = isl_set_affine_hull(context);
812         return isl_aff_substitute_equalities_lifted(aff, hull);
813 error:
814         isl_aff_free(aff);
815         isl_set_free(context);
816         return NULL;
817 }
818
819 /* Return a basic set containing those elements in the space
820  * of aff where it is non-negative.
821  */
822 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
823 {
824         isl_constraint *ineq;
825
826         ineq = isl_inequality_from_aff(aff);
827
828         return isl_basic_set_from_constraint(ineq);
829 }
830
831 /* Return a basic set containing those elements in the space
832  * of aff where it is zero.
833  */
834 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
835 {
836         isl_constraint *ineq;
837
838         ineq = isl_equality_from_aff(aff);
839
840         return isl_basic_set_from_constraint(ineq);
841 }
842
843 /* Return a basic set containing those elements in the shared space
844  * of aff1 and aff2 where aff1 is greater than or equal to aff2.
845  */
846 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
847         __isl_take isl_aff *aff2)
848 {
849         aff1 = isl_aff_sub(aff1, aff2);
850
851         return isl_aff_nonneg_basic_set(aff1);
852 }
853
854 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
855         __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
856 {
857         aff1 = isl_aff_add(aff1, aff2);
858         aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
859         return aff1;
860 }
861
862 int isl_aff_is_empty(__isl_keep isl_aff *aff)
863 {
864         if (!aff)
865                 return -1;
866
867         return 0;
868 }
869
870 /* Set active[i] to 1 if the dimension at position i is involved
871  * in the affine expression.
872  */
873 static int set_active(__isl_keep isl_aff *aff, int *active)
874 {
875         int i, j;
876         unsigned total;
877         unsigned offset;
878
879         if (!aff || !active)
880                 return -1;
881
882         total = aff->v->size - 2;
883         for (i = 0; i < total; ++i)
884                 active[i] = !isl_int_is_zero(aff->v->el[2 + i]);
885
886         offset = isl_local_space_offset(aff->ls, isl_dim_div) - 1;
887         for (i = aff->ls->div->n_row - 1; i >= 0; --i) {
888                 if (!active[offset + i])
889                         continue;
890                 for (j = 0; j < total; ++j)
891                         active[j] |=
892                                 !isl_int_is_zero(aff->ls->div->row[i][2 + j]);
893         }
894
895         return 0;
896 }
897
898 /* Check whether the given affine expression has non-zero coefficient
899  * for any dimension in the given range or if any of these dimensions
900  * appear with non-zero coefficients in any of the integer divisions
901  * involved in the affine expression.
902  */
903 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
904         enum isl_dim_type type, unsigned first, unsigned n)
905 {
906         int i;
907         isl_ctx *ctx;
908         int *active = NULL;
909         int involves = 0;
910
911         if (!aff)
912                 return -1;
913         if (n == 0)
914                 return 0;
915
916         ctx = isl_aff_get_ctx(aff);
917         if (first + n > isl_aff_dim(aff, type))
918                 isl_die(ctx, isl_error_invalid,
919                         "range out of bounds", return -1);
920
921         active = isl_calloc_array(ctx, int,
922                                   isl_local_space_dim(aff->ls, isl_dim_all));
923         if (set_active(aff, active) < 0)
924                 goto error;
925
926         first += isl_local_space_offset(aff->ls, type) - 1;
927         for (i = 0; i < n; ++i)
928                 if (active[first + i]) {
929                         involves = 1;
930                         break;
931                 }
932
933         free(active);
934
935         return involves;
936 error:
937         free(active);
938         return -1;
939 }
940
941 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
942         enum isl_dim_type type, unsigned first, unsigned n)
943 {
944         isl_ctx *ctx;
945
946         if (!aff)
947                 return NULL;
948         if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
949                 return aff;
950
951         ctx = isl_aff_get_ctx(aff);
952         if (first + n > isl_aff_dim(aff, type))
953                 isl_die(ctx, isl_error_invalid, "range out of bounds",
954                         return isl_aff_free(aff));
955
956         aff = isl_aff_cow(aff);
957         if (!aff)
958                 return NULL;
959
960         aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
961         if (!aff->ls)
962                 return isl_aff_free(aff);
963
964         first += 1 + isl_local_space_offset(aff->ls, type);
965         aff->v = isl_vec_drop_els(aff->v, first, n);
966         if (!aff->v)
967                 return isl_aff_free(aff);
968
969         return aff;
970 }
971
972 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
973         enum isl_dim_type type, unsigned first, unsigned n)
974 {
975         isl_ctx *ctx;
976
977         if (!aff)
978                 return NULL;
979         if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
980                 return aff;
981
982         ctx = isl_aff_get_ctx(aff);
983         if (first > isl_aff_dim(aff, type))
984                 isl_die(ctx, isl_error_invalid, "position out of bounds",
985                         return isl_aff_free(aff));
986
987         aff = isl_aff_cow(aff);
988         if (!aff)
989                 return NULL;
990
991         aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
992         if (!aff->ls)
993                 return isl_aff_free(aff);
994
995         first += 1 + isl_local_space_offset(aff->ls, type);
996         aff->v = isl_vec_insert_zero_els(aff->v, first, n);
997         if (!aff->v)
998                 return isl_aff_free(aff);
999
1000         return aff;
1001 }
1002
1003 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1004         enum isl_dim_type type, unsigned n)
1005 {
1006         unsigned pos;
1007
1008         pos = isl_aff_dim(aff, type);
1009
1010         return isl_aff_insert_dims(aff, type, pos, n);
1011 }
1012
1013 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1014         enum isl_dim_type type, unsigned n)
1015 {
1016         unsigned pos;
1017
1018         pos = isl_pw_aff_dim(pwaff, type);
1019
1020         return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1021 }
1022
1023 #undef PW
1024 #define PW isl_pw_aff
1025 #undef EL
1026 #define EL isl_aff
1027 #undef EL_IS_ZERO
1028 #define EL_IS_ZERO is_empty
1029 #undef ZERO
1030 #define ZERO empty
1031 #undef IS_ZERO
1032 #define IS_ZERO is_empty
1033 #undef FIELD
1034 #define FIELD aff
1035
1036 #define NO_EVAL
1037 #define NO_OPT
1038 #define NO_MOVE_DIMS
1039 #define NO_LIFT
1040 #define NO_MORPH
1041
1042 #include <isl_pw_templ.c>
1043
1044 /* Compute a piecewise quasi-affine expression with a domain that
1045  * is the union of those of pwaff1 and pwaff2 and such that on each
1046  * cell, the quasi-affine expression is the maximum of those of pwaff1
1047  * and pwaff2.  If only one of pwaff1 or pwaff2 is defined on a given
1048  * cell, then the associated expression is the defined one.
1049  */
1050 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1051         __isl_take isl_pw_aff *pwaff2)
1052 {
1053         int i, j, n;
1054         isl_pw_aff *res;
1055         isl_ctx *ctx;
1056         isl_set *set;
1057
1058         if (!pwaff1 || !pwaff2)
1059                 goto error;
1060
1061         ctx = isl_dim_get_ctx(pwaff1->dim);
1062         if (!isl_dim_equal(pwaff1->dim, pwaff2->dim))
1063                 isl_die(ctx, isl_error_invalid,
1064                         "arguments should live in same space", goto error);
1065
1066         if (isl_pw_aff_is_empty(pwaff1)) {
1067                 isl_pw_aff_free(pwaff1);
1068                 return pwaff2;
1069         }
1070
1071         if (isl_pw_aff_is_empty(pwaff2)) {
1072                 isl_pw_aff_free(pwaff2);
1073                 return pwaff1;
1074         }
1075
1076         n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1077         res = isl_pw_aff_alloc_(isl_dim_copy(pwaff1->dim), n);
1078
1079         for (i = 0; i < pwaff1->n; ++i) {
1080                 set = isl_set_copy(pwaff1->p[i].set);
1081                 for (j = 0; j < pwaff2->n; ++j) {
1082                         struct isl_set *common;
1083                         isl_set *ge;
1084
1085                         common = isl_set_intersect(
1086                                         isl_set_copy(pwaff1->p[i].set),
1087                                         isl_set_copy(pwaff2->p[j].set));
1088                         ge = isl_set_from_basic_set(isl_aff_ge_basic_set(
1089                                         isl_aff_copy(pwaff2->p[j].aff),
1090                                         isl_aff_copy(pwaff1->p[i].aff)));
1091                         ge = isl_set_intersect(common, ge);
1092                         if (isl_set_plain_is_empty(ge)) {
1093                                 isl_set_free(ge);
1094                                 continue;
1095                         }
1096                         set = isl_set_subtract(set, isl_set_copy(ge));
1097
1098                         res = isl_pw_aff_add_piece(res, ge,
1099                                                 isl_aff_copy(pwaff2->p[j].aff));
1100                 }
1101                 res = isl_pw_aff_add_piece(res, set,
1102                                                 isl_aff_copy(pwaff1->p[i].aff));
1103         }
1104
1105         for (j = 0; j < pwaff2->n; ++j) {
1106                 set = isl_set_copy(pwaff2->p[j].set);
1107                 for (i = 0; i < pwaff1->n; ++i)
1108                         set = isl_set_subtract(set,
1109                                         isl_set_copy(pwaff1->p[i].set));
1110                 res = isl_pw_aff_add_piece(res, set,
1111                                                 isl_aff_copy(pwaff2->p[j].aff));
1112         }
1113
1114         isl_pw_aff_free(pwaff1);
1115         isl_pw_aff_free(pwaff2);
1116
1117         return res;
1118 error:
1119         isl_pw_aff_free(pwaff1);
1120         isl_pw_aff_free(pwaff2);
1121         return NULL;
1122 }
1123
1124 /* Construct a map with as domain the domain of pwaff and
1125  * one-dimensional range corresponding to the affine expressions.
1126  */
1127 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1128 {
1129         int i;
1130         isl_dim *dim;
1131         isl_map *map;
1132
1133         if (!pwaff)
1134                 return NULL;
1135
1136         dim = isl_pw_aff_get_dim(pwaff);
1137         dim = isl_dim_from_domain(dim);
1138         dim = isl_dim_add(dim, isl_dim_out, 1);
1139         map = isl_map_empty(dim);
1140
1141         for (i = 0; i < pwaff->n; ++i) {
1142                 isl_basic_map *bmap;
1143                 isl_map *map_i;
1144
1145                 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1146                 map_i = isl_map_from_basic_map(bmap);
1147                 map_i = isl_map_intersect_domain(map_i,
1148                                                 isl_set_copy(pwaff->p[i].set));
1149                 map = isl_map_union_disjoint(map, map_i);
1150         }
1151
1152         isl_pw_aff_free(pwaff);
1153
1154         return map;
1155 }
1156
1157 /* Return a set containing those elements in the domain
1158  * of pwaff where it is non-negative.
1159  */
1160 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1161 {
1162         int i;
1163         isl_set *set;
1164
1165         if (!pwaff)
1166                 return NULL;
1167
1168         set = isl_set_empty(isl_pw_aff_get_dim(pwaff));
1169
1170         for (i = 0; i < pwaff->n; ++i) {
1171                 isl_basic_set *bset;
1172                 isl_set *set_i;
1173
1174                 bset = isl_aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff));
1175                 set_i = isl_set_from_basic_set(bset);
1176                 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1177                 set = isl_set_union_disjoint(set, set_i);
1178         }
1179
1180         isl_pw_aff_free(pwaff);
1181
1182         return set;
1183 }
1184
1185 /* Return a set containing those elements in the domain
1186  * of pwaff where it is zero.
1187  */
1188 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
1189 {
1190         int i;
1191         isl_set *set;
1192
1193         if (!pwaff)
1194                 return NULL;
1195
1196         set = isl_set_empty(isl_pw_aff_get_dim(pwaff));
1197
1198         for (i = 0; i < pwaff->n; ++i) {
1199                 isl_basic_set *bset;
1200                 isl_set *set_i;
1201
1202                 bset = isl_aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff));
1203                 set_i = isl_set_from_basic_set(bset);
1204                 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1205                 set = isl_set_union_disjoint(set, set_i);
1206         }
1207
1208         isl_pw_aff_free(pwaff);
1209
1210         return set;
1211 }
1212
1213 /* Return a set containing those elements in the shared domain
1214  * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1215  *
1216  * We compute the difference on the shared domain and then construct
1217  * the set of values where this difference is non-negative.
1218  * If strict is set, we first subtract 1 from the difference.
1219  * If equal is set, we only return the elements where pwaff1 and pwaff2
1220  * are equal.
1221  */
1222 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
1223         __isl_take isl_pw_aff *pwaff2, int strict, int equal)
1224 {
1225         isl_set *set1, *set2;
1226
1227         set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
1228         set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
1229         set1 = isl_set_intersect(set1, set2);
1230         pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
1231         pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
1232         pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
1233
1234         if (strict) {
1235                 isl_dim *dim = isl_set_get_dim(set1);
1236                 isl_aff *aff;
1237                 aff = isl_aff_zero(isl_local_space_from_dim(dim));
1238                 aff = isl_aff_add_constant_si(aff, -1);
1239                 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
1240         } else
1241                 isl_set_free(set1);
1242
1243         if (equal)
1244                 return isl_pw_aff_zero_set(pwaff1);
1245         return isl_pw_aff_nonneg_set(pwaff1);
1246 }
1247
1248 /* Return a set containing those elements in the shared domain
1249  * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1250  */
1251 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1252         __isl_take isl_pw_aff *pwaff2)
1253 {
1254         return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
1255 }
1256
1257 /* Return a set containing those elements in the shared domain
1258  * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1259  */
1260 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1261         __isl_take isl_pw_aff *pwaff2)
1262 {
1263         return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
1264 }
1265
1266 /* Return a set containing those elements in the shared domain
1267  * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1268  */
1269 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1270         __isl_take isl_pw_aff *pwaff2)
1271 {
1272         return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
1273 }
1274
1275 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
1276         __isl_take isl_pw_aff *pwaff2)
1277 {
1278         return isl_pw_aff_ge_set(pwaff2, pwaff1);
1279 }
1280
1281 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
1282         __isl_take isl_pw_aff *pwaff2)
1283 {
1284         return isl_pw_aff_gt_set(pwaff2, pwaff1);
1285 }
1286
1287 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
1288         isl_int v)
1289 {
1290         int i;
1291
1292         if (isl_int_is_one(v))
1293                 return pwaff;
1294         if (!isl_int_is_pos(v))
1295                 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1296                         "factor needs to be positive",
1297                         return isl_pw_aff_free(pwaff));
1298         pwaff = isl_pw_aff_cow(pwaff);
1299         if (!pwaff)
1300                 return NULL;
1301         if (pwaff->n == 0)
1302                 return pwaff;
1303
1304         for (i = 0; i < pwaff->n; ++i) {
1305                 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
1306                 if (!pwaff->p[i].aff)
1307                         return isl_pw_aff_free(pwaff);
1308         }
1309
1310         return pwaff;
1311 }
1312
1313 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
1314 {
1315         int i;
1316
1317         pwaff = isl_pw_aff_cow(pwaff);
1318         if (!pwaff)
1319                 return NULL;
1320         if (pwaff->n == 0)
1321                 return pwaff;
1322
1323         for (i = 0; i < pwaff->n; ++i) {
1324                 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
1325                 if (!pwaff->p[i].aff)
1326                         return isl_pw_aff_free(pwaff);
1327         }
1328
1329         return pwaff;
1330 }
1331
1332 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
1333 {
1334         int i;
1335
1336         pwaff = isl_pw_aff_cow(pwaff);
1337         if (!pwaff)
1338                 return NULL;
1339         if (pwaff->n == 0)
1340                 return pwaff;
1341
1342         for (i = 0; i < pwaff->n; ++i) {
1343                 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
1344                 if (!pwaff->p[i].aff)
1345                         return isl_pw_aff_free(pwaff);
1346         }
1347
1348         return pwaff;
1349 }
1350
1351 /* Return an affine expression that is equal to pwaff_true for elements
1352  * in "cond" and to pwaff_false for elements not in "cond".
1353  * That is, return cond ? pwaff_true : pwaff_false;
1354  */
1355 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_set *cond,
1356         __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
1357 {
1358         isl_set *comp;
1359
1360         comp = isl_set_complement(isl_set_copy(cond));
1361         pwaff_true = isl_pw_aff_intersect_domain(pwaff_true, cond);
1362         pwaff_false = isl_pw_aff_intersect_domain(pwaff_false, comp);
1363
1364         return isl_pw_aff_add_disjoint(pwaff_true, pwaff_false);
1365 }