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