add isl_aff_le_basic_set
[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_ctx_private.h>
13 #include <isl_map_private.h>
14 #include <isl_aff_private.h>
15 #include <isl_dim_private.h>
16 #include <isl_local_space_private.h>
17 #include <isl_mat_private.h>
18 #include <isl_list_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 __isl_give isl_aff *isl_aff_reset_dim(__isl_take isl_aff *aff,
154         __isl_take isl_dim *dim)
155 {
156         aff = isl_aff_cow(aff);
157         if (!aff || !dim)
158                 goto error;
159
160         aff->ls = isl_local_space_reset_dim(aff->ls, dim);
161         if (!aff->ls)
162                 return isl_aff_free(aff);
163
164         return aff;
165 error:
166         isl_aff_free(aff);
167         isl_dim_free(dim);
168         return NULL;
169 }
170
171 /* Reorder the coefficients of the affine expression based
172  * on the given reodering.
173  * The reordering r is assumed to have been extended with the local
174  * variables.
175  */
176 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
177         __isl_take isl_reordering *r, int n_div)
178 {
179         isl_vec *res;
180         int i;
181
182         if (!vec || !r)
183                 goto error;
184
185         res = isl_vec_alloc(vec->ctx, 2 + isl_dim_total(r->dim) + n_div);
186         isl_seq_cpy(res->el, vec->el, 2);
187         isl_seq_clr(res->el + 2, res->size - 2);
188         for (i = 0; i < r->len; ++i)
189                 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
190
191         isl_reordering_free(r);
192         isl_vec_free(vec);
193         return res;
194 error:
195         isl_vec_free(vec);
196         isl_reordering_free(r);
197         return NULL;
198 }
199
200 /* Reorder the dimensions of "aff" according to the given reordering.
201  */
202 __isl_give isl_aff *isl_aff_realign(__isl_take isl_aff *aff,
203         __isl_take isl_reordering *r)
204 {
205         aff = isl_aff_cow(aff);
206         if (!aff)
207                 goto error;
208
209         r = isl_reordering_extend(r, aff->ls->div->n_row);
210         aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
211                                 aff->ls->div->n_row);
212         aff->ls = isl_local_space_realign(aff->ls, r);
213
214         if (!aff->v || !aff->ls)
215                 return isl_aff_free(aff);
216
217         return aff;
218 error:
219         isl_aff_free(aff);
220         isl_reordering_free(r);
221         return NULL;
222 }
223
224 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
225 {
226         if (!aff)
227                 return -1;
228
229         return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
230 }
231
232 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
233 {
234         int equal;
235
236         if (!aff1 || !aff2)
237                 return -1;
238
239         equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
240         if (equal < 0 || !equal)
241                 return equal;
242
243         return isl_vec_is_equal(aff1->v, aff2->v);
244 }
245
246 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
247 {
248         if (!aff)
249                 return -1;
250         isl_int_set(*v, aff->v->el[0]);
251         return 0;
252 }
253
254 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
255 {
256         if (!aff)
257                 return -1;
258         isl_int_set(*v, aff->v->el[1]);
259         return 0;
260 }
261
262 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
263         enum isl_dim_type type, int pos, isl_int *v)
264 {
265         if (!aff)
266                 return -1;
267
268         if (pos >= isl_local_space_dim(aff->ls, type))
269                 isl_die(aff->v->ctx, isl_error_invalid,
270                         "position out of bounds", return -1);
271
272         pos += isl_local_space_offset(aff->ls, type);
273         isl_int_set(*v, aff->v->el[1 + pos]);
274
275         return 0;
276 }
277
278 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
279 {
280         aff = isl_aff_cow(aff);
281         if (!aff)
282                 return NULL;
283
284         aff->v = isl_vec_cow(aff->v);
285         if (!aff->v)
286                 return isl_aff_free(aff);
287
288         isl_int_set(aff->v->el[0], v);
289
290         return aff;
291 }
292
293 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
294 {
295         aff = isl_aff_cow(aff);
296         if (!aff)
297                 return NULL;
298
299         aff->v = isl_vec_cow(aff->v);
300         if (!aff->v)
301                 return isl_aff_free(aff);
302
303         isl_int_set(aff->v->el[1], v);
304
305         return aff;
306 }
307
308 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
309 {
310         if (isl_int_is_zero(v))
311                 return aff;
312
313         aff = isl_aff_cow(aff);
314         if (!aff)
315                 return NULL;
316
317         aff->v = isl_vec_cow(aff->v);
318         if (!aff->v)
319                 return isl_aff_free(aff);
320
321         isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
322
323         return aff;
324 }
325
326 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
327 {
328         isl_int t;
329
330         isl_int_init(t);
331         isl_int_set_si(t, v);
332         aff = isl_aff_add_constant(aff, t);
333         isl_int_clear(t);
334
335         return aff;
336 }
337
338 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
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         isl_int_set_si(aff->v->el[1], v);
349
350         return aff;
351 }
352
353 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
354         enum isl_dim_type type, int pos, isl_int v)
355 {
356         if (!aff)
357                 return NULL;
358
359         if (pos >= isl_local_space_dim(aff->ls, type))
360                 isl_die(aff->v->ctx, isl_error_invalid,
361                         "position out of bounds", return isl_aff_free(aff));
362
363         aff = isl_aff_cow(aff);
364         if (!aff)
365                 return NULL;
366
367         aff->v = isl_vec_cow(aff->v);
368         if (!aff->v)
369                 return isl_aff_free(aff);
370
371         pos += isl_local_space_offset(aff->ls, type);
372         isl_int_set(aff->v->el[1 + pos], v);
373
374         return aff;
375 }
376
377 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
378         enum isl_dim_type type, int pos, int v)
379 {
380         if (!aff)
381                 return NULL;
382
383         if (pos >= isl_local_space_dim(aff->ls, type))
384                 isl_die(aff->v->ctx, isl_error_invalid,
385                         "position out of bounds", return isl_aff_free(aff));
386
387         aff = isl_aff_cow(aff);
388         if (!aff)
389                 return NULL;
390
391         aff->v = isl_vec_cow(aff->v);
392         if (!aff->v)
393                 return isl_aff_free(aff);
394
395         pos += isl_local_space_offset(aff->ls, type);
396         isl_int_set_si(aff->v->el[1 + pos], v);
397
398         return aff;
399 }
400
401 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
402         enum isl_dim_type type, int pos, isl_int v)
403 {
404         if (!aff)
405                 return NULL;
406
407         if (pos >= isl_local_space_dim(aff->ls, type))
408                 isl_die(aff->v->ctx, isl_error_invalid,
409                         "position out of bounds", return isl_aff_free(aff));
410
411         aff = isl_aff_cow(aff);
412         if (!aff)
413                 return NULL;
414
415         aff->v = isl_vec_cow(aff->v);
416         if (!aff->v)
417                 return isl_aff_free(aff);
418
419         pos += isl_local_space_offset(aff->ls, type);
420         isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
421
422         return aff;
423 }
424
425 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
426         enum isl_dim_type type, int pos, int v)
427 {
428         isl_int t;
429
430         isl_int_init(t);
431         isl_int_set_si(t, v);
432         aff = isl_aff_add_coefficient(aff, type, pos, t);
433         isl_int_clear(t);
434
435         return aff;
436 }
437
438 __isl_give isl_div *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
439 {
440         if (!aff)
441                 return NULL;
442
443         return isl_local_space_get_div(aff->ls, pos);
444 }
445
446 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
447 {
448         aff = isl_aff_cow(aff);
449         if (!aff)
450                 return NULL;
451         aff->v = isl_vec_cow(aff->v);
452         if (!aff->v)
453                 return isl_aff_free(aff);
454
455         isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
456
457         return aff;
458 }
459
460 /* Given f, return floor(f).
461  * If f is an integer expression, then just return f.
462  * Otherwise, if f = g/m, write g = q m + r,
463  * create a new div d = [r/m] and return the expression q + d.
464  * The coefficients in r are taken to lie between -m/2 and m/2.
465  */
466 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
467 {
468         int i;
469         int size;
470         isl_ctx *ctx;
471         isl_vec *div;
472
473         if (!aff)
474                 return NULL;
475
476         if (isl_int_is_one(aff->v->el[0]))
477                 return aff;
478
479         aff = isl_aff_cow(aff);
480         if (!aff)
481                 return NULL;
482
483         aff->v = isl_vec_cow(aff->v);
484         div = isl_vec_copy(aff->v);
485         div = isl_vec_cow(div);
486         if (!div)
487                 return isl_aff_free(aff);
488
489         ctx = isl_aff_get_ctx(aff);
490         isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
491         for (i = 1; i < aff->v->size; ++i) {
492                 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
493                 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
494                 if (isl_int_gt(div->el[i], aff->v->el[0])) {
495                         isl_int_sub(div->el[i], div->el[i], div->el[0]);
496                         isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
497                 }
498         }
499
500         aff->ls = isl_local_space_add_div(aff->ls, div);
501         if (!aff->ls)
502                 return isl_aff_free(aff);
503
504         size = aff->v->size;
505         aff->v = isl_vec_extend(aff->v, size + 1);
506         if (!aff->v)
507                 return isl_aff_free(aff);
508         isl_int_set_si(aff->v->el[0], 1);
509         isl_int_set_si(aff->v->el[size], 1);
510
511         return aff;
512 }
513
514 /* Compute
515  *
516  *      aff mod m = aff - m * floor(aff/m)
517  */
518 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
519 {
520         isl_aff *res;
521
522         res = isl_aff_copy(aff);
523         aff = isl_aff_scale_down(aff, m);
524         aff = isl_aff_floor(aff);
525         aff = isl_aff_scale(aff, m);
526         res = isl_aff_sub(res, aff);
527
528         return res;
529 }
530
531 /* Compute
532  *
533  *      pwaff mod m = pwaff - m * floor(pwaff/m)
534  */
535 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
536 {
537         isl_pw_aff *res;
538
539         res = isl_pw_aff_copy(pwaff);
540         pwaff = isl_pw_aff_scale_down(pwaff, m);
541         pwaff = isl_pw_aff_floor(pwaff);
542         pwaff = isl_pw_aff_scale(pwaff, m);
543         res = isl_pw_aff_sub(res, pwaff);
544
545         return res;
546 }
547
548 /* Given f, return ceil(f).
549  * If f is an integer expression, then just return f.
550  * Otherwise, create a new div d = [-f] and return the expression -d.
551  */
552 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
553 {
554         if (!aff)
555                 return NULL;
556
557         if (isl_int_is_one(aff->v->el[0]))
558                 return aff;
559
560         aff = isl_aff_neg(aff);
561         aff = isl_aff_floor(aff);
562         aff = isl_aff_neg(aff);
563
564         return aff;
565 }
566
567 /* Apply the expansion computed by isl_merge_divs.
568  * The expansion itself is given by "exp" while the resulting
569  * list of divs is given by "div".
570  */
571 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
572         __isl_take isl_mat *div, int *exp)
573 {
574         int i, j;
575         int old_n_div;
576         int new_n_div;
577         int offset;
578
579         aff = isl_aff_cow(aff);
580         if (!aff || !div)
581                 goto error;
582
583         old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
584         new_n_div = isl_mat_rows(div);
585         if (new_n_div < old_n_div)
586                 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
587                         "not an expansion", goto error);
588
589         aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
590         if (!aff->v)
591                 goto error;
592
593         offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
594         j = old_n_div - 1;
595         for (i = new_n_div - 1; i >= 0; --i) {
596                 if (j >= 0 && exp[j] == i) {
597                         if (i != j)
598                                 isl_int_swap(aff->v->el[offset + i],
599                                              aff->v->el[offset + j]);
600                         j--;
601                 } else
602                         isl_int_set_si(aff->v->el[offset + i], 0);
603         }
604
605         aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
606         if (!aff->ls)
607                 goto error;
608         isl_mat_free(div);
609         return aff;
610 error:
611         isl_aff_free(aff);
612         isl_mat_free(div);
613         return NULL;
614 }
615
616 /* Add two affine expressions that live in the same local space.
617  */
618 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
619         __isl_take isl_aff *aff2)
620 {
621         isl_int gcd, f;
622
623         aff1 = isl_aff_cow(aff1);
624         if (!aff1 || !aff2)
625                 goto error;
626
627         aff1->v = isl_vec_cow(aff1->v);
628         if (!aff1->v)
629                 goto error;
630
631         isl_int_init(gcd);
632         isl_int_init(f);
633         isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
634         isl_int_divexact(f, aff2->v->el[0], gcd);
635         isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
636         isl_int_divexact(f, aff1->v->el[0], gcd);
637         isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
638         isl_int_divexact(f, aff2->v->el[0], gcd);
639         isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
640         isl_int_clear(f);
641         isl_int_clear(gcd);
642
643         isl_aff_free(aff2);
644         return aff1;
645 error:
646         isl_aff_free(aff1);
647         isl_aff_free(aff2);
648         return NULL;
649 }
650
651 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
652         __isl_take isl_aff *aff2)
653 {
654         isl_ctx *ctx;
655         int *exp1 = NULL;
656         int *exp2 = NULL;
657         isl_mat *div;
658
659         if (!aff1 || !aff2)
660                 goto error;
661
662         ctx = isl_aff_get_ctx(aff1);
663         if (!isl_dim_equal(aff1->ls->dim, aff2->ls->dim))
664                 isl_die(ctx, isl_error_invalid,
665                         "spaces don't match", goto error);
666
667         if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
668                 return add_expanded(aff1, aff2);
669
670         exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
671         exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
672         if (!exp1 || !exp2)
673                 goto error;
674
675         div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
676         aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
677         aff2 = isl_aff_expand_divs(aff2, div, exp2);
678         free(exp1);
679         free(exp2);
680
681         return add_expanded(aff1, aff2);
682 error:
683         free(exp1);
684         free(exp2);
685         isl_aff_free(aff1);
686         isl_aff_free(aff2);
687         return NULL;
688 }
689
690 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
691         __isl_take isl_aff *aff2)
692 {
693         return isl_aff_add(aff1, isl_aff_neg(aff2));
694 }
695
696 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
697 {
698         isl_int gcd;
699
700         if (isl_int_is_one(f))
701                 return aff;
702
703         aff = isl_aff_cow(aff);
704         if (!aff)
705                 return NULL;
706         aff->v = isl_vec_cow(aff->v);
707         if (!aff->v)
708                 return isl_aff_free(aff);
709
710         isl_int_init(gcd);
711         isl_int_gcd(gcd, aff->v->el[0], f);
712         isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
713         isl_int_divexact(gcd, f, gcd);
714         isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
715         isl_int_clear(gcd);
716
717         return aff;
718 }
719
720 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
721 {
722         isl_int gcd;
723
724         if (isl_int_is_one(f))
725                 return aff;
726
727         aff = isl_aff_cow(aff);
728         if (!aff)
729                 return NULL;
730         aff->v = isl_vec_cow(aff->v);
731         if (!aff->v)
732                 return isl_aff_free(aff);
733
734         isl_int_init(gcd);
735         isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
736         isl_int_gcd(gcd, gcd, f);
737         isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
738         isl_int_divexact(gcd, f, gcd);
739         isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
740         isl_int_clear(gcd);
741
742         return aff;
743 }
744
745 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
746 {
747         isl_int v;
748
749         if (f == 1)
750                 return aff;
751
752         isl_int_init(v);
753         isl_int_set_ui(v, f);
754         aff = isl_aff_scale_down(aff, v);
755         isl_int_clear(v);
756
757         return aff;
758 }
759
760 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
761         enum isl_dim_type type, unsigned pos, const char *s)
762 {
763         aff = isl_aff_cow(aff);
764         if (!aff)
765                 return NULL;
766         aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
767         if (!aff->ls)
768                 return isl_aff_free(aff);
769
770         return aff;
771 }
772
773 /* Exploit the equalities in "eq" to simplify the affine expression
774  * and the expressions of the integer divisions in the local space.
775  * The integer divisions in this local space are assumed to appear
776  * as regular dimensions in "eq".
777  */
778 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
779         __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
780 {
781         int i, j;
782         unsigned total;
783         unsigned n_div;
784
785         if (!eq)
786                 goto error;
787         if (eq->n_eq == 0) {
788                 isl_basic_set_free(eq);
789                 return aff;
790         }
791
792         aff = isl_aff_cow(aff);
793         if (!aff)
794                 goto error;
795
796         aff->ls = isl_local_space_substitute_equalities(aff->ls,
797                                                         isl_basic_set_copy(eq));
798         if (!aff->ls)
799                 goto error;
800
801         total = 1 + isl_dim_total(eq->dim);
802         n_div = eq->n_div;
803         for (i = 0; i < eq->n_eq; ++i) {
804                 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
805                 if (j < 0 || j == 0 || j >= total)
806                         continue;
807
808                 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
809                                 &aff->v->el[0]);
810         }
811
812         isl_basic_set_free(eq);
813         return aff;
814 error:
815         isl_basic_set_free(eq);
816         isl_aff_free(aff);
817         return NULL;
818 }
819
820 /* Exploit the equalities in "eq" to simplify the affine expression
821  * and the expressions of the integer divisions in the local space.
822  */
823 static __isl_give isl_aff *isl_aff_substitute_equalities(
824         __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
825 {
826         int n_div;
827
828         if (!aff || !eq)
829                 goto error;
830         n_div = isl_local_space_dim(aff->ls, isl_dim_div);
831         if (n_div > 0)
832                 eq = isl_basic_set_add(eq, isl_dim_set, n_div);
833         return isl_aff_substitute_equalities_lifted(aff, eq);
834 error:
835         isl_basic_set_free(eq);
836         isl_aff_free(aff);
837         return NULL;
838 }
839
840 /* Look for equalities among the variables shared by context and aff
841  * and the integer divisions of aff, if any.
842  * The equalities are then used to eliminate coefficients and/or integer
843  * divisions from aff.
844  */
845 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
846         __isl_take isl_set *context)
847 {
848         isl_basic_set *hull;
849         int n_div;
850
851         if (!aff)
852                 goto error;
853         n_div = isl_local_space_dim(aff->ls, isl_dim_div);
854         if (n_div > 0) {
855                 isl_basic_set *bset;
856                 context = isl_set_add_dims(context, isl_dim_set, n_div);
857                 bset = isl_basic_set_from_local_space(
858                                             isl_aff_get_local_space(aff));
859                 bset = isl_basic_set_lift(bset);
860                 bset = isl_basic_set_flatten(bset);
861                 context = isl_set_intersect(context,
862                                             isl_set_from_basic_set(bset));
863         }
864
865         hull = isl_set_affine_hull(context);
866         return isl_aff_substitute_equalities_lifted(aff, hull);
867 error:
868         isl_aff_free(aff);
869         isl_set_free(context);
870         return NULL;
871 }
872
873 /* Return a basic set containing those elements in the space
874  * of aff where it is non-negative.
875  */
876 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
877 {
878         isl_constraint *ineq;
879
880         ineq = isl_inequality_from_aff(aff);
881
882         return isl_basic_set_from_constraint(ineq);
883 }
884
885 /* Return a basic set containing those elements in the space
886  * of aff where it is zero.
887  */
888 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
889 {
890         isl_constraint *ineq;
891
892         ineq = isl_equality_from_aff(aff);
893
894         return isl_basic_set_from_constraint(ineq);
895 }
896
897 /* Return a basic set containing those elements in the shared space
898  * of aff1 and aff2 where aff1 is greater than or equal to aff2.
899  */
900 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
901         __isl_take isl_aff *aff2)
902 {
903         aff1 = isl_aff_sub(aff1, aff2);
904
905         return isl_aff_nonneg_basic_set(aff1);
906 }
907
908 /* Return a basic set containing those elements in the shared space
909  * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
910  */
911 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
912         __isl_take isl_aff *aff2)
913 {
914         return isl_aff_ge_basic_set(aff2, aff1);
915 }
916
917 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
918         __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
919 {
920         aff1 = isl_aff_add(aff1, aff2);
921         aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
922         return aff1;
923 }
924
925 int isl_aff_is_empty(__isl_keep isl_aff *aff)
926 {
927         if (!aff)
928                 return -1;
929
930         return 0;
931 }
932
933 /* Set active[i] to 1 if the dimension at position i is involved
934  * in the affine expression.
935  */
936 static int set_active(__isl_keep isl_aff *aff, int *active)
937 {
938         int i, j;
939         unsigned total;
940         unsigned offset;
941
942         if (!aff || !active)
943                 return -1;
944
945         total = aff->v->size - 2;
946         for (i = 0; i < total; ++i)
947                 active[i] = !isl_int_is_zero(aff->v->el[2 + i]);
948
949         offset = isl_local_space_offset(aff->ls, isl_dim_div) - 1;
950         for (i = aff->ls->div->n_row - 1; i >= 0; --i) {
951                 if (!active[offset + i])
952                         continue;
953                 for (j = 0; j < total; ++j)
954                         active[j] |=
955                                 !isl_int_is_zero(aff->ls->div->row[i][2 + j]);
956         }
957
958         return 0;
959 }
960
961 /* Check whether the given affine expression has non-zero coefficient
962  * for any dimension in the given range or if any of these dimensions
963  * appear with non-zero coefficients in any of the integer divisions
964  * involved in the affine expression.
965  */
966 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
967         enum isl_dim_type type, unsigned first, unsigned n)
968 {
969         int i;
970         isl_ctx *ctx;
971         int *active = NULL;
972         int involves = 0;
973
974         if (!aff)
975                 return -1;
976         if (n == 0)
977                 return 0;
978
979         ctx = isl_aff_get_ctx(aff);
980         if (first + n > isl_aff_dim(aff, type))
981                 isl_die(ctx, isl_error_invalid,
982                         "range out of bounds", return -1);
983
984         active = isl_calloc_array(ctx, int,
985                                   isl_local_space_dim(aff->ls, isl_dim_all));
986         if (set_active(aff, active) < 0)
987                 goto error;
988
989         first += isl_local_space_offset(aff->ls, type) - 1;
990         for (i = 0; i < n; ++i)
991                 if (active[first + i]) {
992                         involves = 1;
993                         break;
994                 }
995
996         free(active);
997
998         return involves;
999 error:
1000         free(active);
1001         return -1;
1002 }
1003
1004 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1005         enum isl_dim_type type, unsigned first, unsigned n)
1006 {
1007         isl_ctx *ctx;
1008
1009         if (!aff)
1010                 return NULL;
1011         if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1012                 return aff;
1013
1014         ctx = isl_aff_get_ctx(aff);
1015         if (first + n > isl_aff_dim(aff, type))
1016                 isl_die(ctx, isl_error_invalid, "range out of bounds",
1017                         return isl_aff_free(aff));
1018
1019         aff = isl_aff_cow(aff);
1020         if (!aff)
1021                 return NULL;
1022
1023         aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1024         if (!aff->ls)
1025                 return isl_aff_free(aff);
1026
1027         first += 1 + isl_local_space_offset(aff->ls, type);
1028         aff->v = isl_vec_drop_els(aff->v, first, n);
1029         if (!aff->v)
1030                 return isl_aff_free(aff);
1031
1032         return aff;
1033 }
1034
1035 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1036         enum isl_dim_type type, unsigned first, unsigned n)
1037 {
1038         isl_ctx *ctx;
1039
1040         if (!aff)
1041                 return NULL;
1042         if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1043                 return aff;
1044
1045         ctx = isl_aff_get_ctx(aff);
1046         if (first > isl_aff_dim(aff, type))
1047                 isl_die(ctx, isl_error_invalid, "position out of bounds",
1048                         return isl_aff_free(aff));
1049
1050         aff = isl_aff_cow(aff);
1051         if (!aff)
1052                 return NULL;
1053
1054         aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1055         if (!aff->ls)
1056                 return isl_aff_free(aff);
1057
1058         first += 1 + isl_local_space_offset(aff->ls, type);
1059         aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1060         if (!aff->v)
1061                 return isl_aff_free(aff);
1062
1063         return aff;
1064 }
1065
1066 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1067         enum isl_dim_type type, unsigned n)
1068 {
1069         unsigned pos;
1070
1071         pos = isl_aff_dim(aff, type);
1072
1073         return isl_aff_insert_dims(aff, type, pos, n);
1074 }
1075
1076 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1077         enum isl_dim_type type, unsigned n)
1078 {
1079         unsigned pos;
1080
1081         pos = isl_pw_aff_dim(pwaff, type);
1082
1083         return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1084 }
1085
1086 __isl_give isl_pw_aff *isl_pw_aff_set_tuple_id(__isl_take isl_pw_aff *pwaff,
1087         __isl_take isl_id *id)
1088 {
1089         isl_dim *dim;
1090
1091         dim = isl_pw_aff_get_dim(pwaff);
1092         dim = isl_dim_set_tuple_id(dim, isl_dim_set, id);
1093
1094         return isl_pw_aff_reset_dim(pwaff, dim);
1095 }
1096
1097 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1098 {
1099         isl_set *dom = isl_set_universe(isl_aff_get_dim(aff));
1100         return isl_pw_aff_alloc(dom, aff);
1101 }
1102
1103 #undef PW
1104 #define PW isl_pw_aff
1105 #undef EL
1106 #define EL isl_aff
1107 #undef EL_IS_ZERO
1108 #define EL_IS_ZERO is_empty
1109 #undef ZERO
1110 #define ZERO empty
1111 #undef IS_ZERO
1112 #define IS_ZERO is_empty
1113 #undef FIELD
1114 #define FIELD aff
1115
1116 #define NO_EVAL
1117 #define NO_OPT
1118 #define NO_MOVE_DIMS
1119 #define NO_LIFT
1120 #define NO_MORPH
1121
1122 #include <isl_pw_templ.c>
1123
1124 static __isl_give isl_set *align_params_pw_pw_set_and(
1125         __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1126         __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1127                                     __isl_take isl_pw_aff *pwaff2))
1128 {
1129         if (!pwaff1 || !pwaff2)
1130                 goto error;
1131         if (isl_dim_match(pwaff1->dim, isl_dim_param,
1132                           pwaff2->dim, isl_dim_param))
1133                 return fn(pwaff1, pwaff2);
1134         if (!isl_dim_has_named_params(pwaff1->dim) ||
1135             !isl_dim_has_named_params(pwaff2->dim))
1136                 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1137                         "unaligned unnamed parameters", goto error);
1138         pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_dim(pwaff2));
1139         pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_dim(pwaff1));
1140         return fn(pwaff1, pwaff2);
1141 error:
1142         isl_pw_aff_free(pwaff1);
1143         isl_pw_aff_free(pwaff2);
1144         return NULL;
1145 }
1146
1147 /* Compute a piecewise quasi-affine expression with a domain that
1148  * is the union of those of pwaff1 and pwaff2 and such that on each
1149  * cell, the quasi-affine expression is the maximum of those of pwaff1
1150  * and pwaff2.  If only one of pwaff1 or pwaff2 is defined on a given
1151  * cell, then the associated expression is the defined one.
1152  */
1153 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1154         __isl_take isl_pw_aff *pwaff2)
1155 {
1156         int i, j, n;
1157         isl_pw_aff *res;
1158         isl_ctx *ctx;
1159         isl_set *set;
1160
1161         if (!pwaff1 || !pwaff2)
1162                 goto error;
1163
1164         ctx = isl_dim_get_ctx(pwaff1->dim);
1165         if (!isl_dim_equal(pwaff1->dim, pwaff2->dim))
1166                 isl_die(ctx, isl_error_invalid,
1167                         "arguments should live in same space", goto error);
1168
1169         if (isl_pw_aff_is_empty(pwaff1)) {
1170                 isl_pw_aff_free(pwaff1);
1171                 return pwaff2;
1172         }
1173
1174         if (isl_pw_aff_is_empty(pwaff2)) {
1175                 isl_pw_aff_free(pwaff2);
1176                 return pwaff1;
1177         }
1178
1179         n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1180         res = isl_pw_aff_alloc_(isl_dim_copy(pwaff1->dim), n);
1181
1182         for (i = 0; i < pwaff1->n; ++i) {
1183                 set = isl_set_copy(pwaff1->p[i].set);
1184                 for (j = 0; j < pwaff2->n; ++j) {
1185                         struct isl_set *common;
1186                         isl_set *ge;
1187
1188                         common = isl_set_intersect(
1189                                         isl_set_copy(pwaff1->p[i].set),
1190                                         isl_set_copy(pwaff2->p[j].set));
1191                         ge = isl_set_from_basic_set(isl_aff_ge_basic_set(
1192                                         isl_aff_copy(pwaff2->p[j].aff),
1193                                         isl_aff_copy(pwaff1->p[i].aff)));
1194                         ge = isl_set_intersect(common, ge);
1195                         if (isl_set_plain_is_empty(ge)) {
1196                                 isl_set_free(ge);
1197                                 continue;
1198                         }
1199                         set = isl_set_subtract(set, isl_set_copy(ge));
1200
1201                         res = isl_pw_aff_add_piece(res, ge,
1202                                                 isl_aff_copy(pwaff2->p[j].aff));
1203                 }
1204                 res = isl_pw_aff_add_piece(res, set,
1205                                                 isl_aff_copy(pwaff1->p[i].aff));
1206         }
1207
1208         for (j = 0; j < pwaff2->n; ++j) {
1209                 set = isl_set_copy(pwaff2->p[j].set);
1210                 for (i = 0; i < pwaff1->n; ++i)
1211                         set = isl_set_subtract(set,
1212                                         isl_set_copy(pwaff1->p[i].set));
1213                 res = isl_pw_aff_add_piece(res, set,
1214                                                 isl_aff_copy(pwaff2->p[j].aff));
1215         }
1216
1217         isl_pw_aff_free(pwaff1);
1218         isl_pw_aff_free(pwaff2);
1219
1220         return res;
1221 error:
1222         isl_pw_aff_free(pwaff1);
1223         isl_pw_aff_free(pwaff2);
1224         return NULL;
1225 }
1226
1227 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1228         __isl_take isl_pw_aff *pwaff2)
1229 {
1230         return align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_union_max);
1231 }
1232
1233 /* Construct a map with as domain the domain of pwaff and
1234  * one-dimensional range corresponding to the affine expressions.
1235  */
1236 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1237 {
1238         int i;
1239         isl_dim *dim;
1240         isl_map *map;
1241
1242         if (!pwaff)
1243                 return NULL;
1244
1245         dim = isl_pw_aff_get_dim(pwaff);
1246         dim = isl_dim_from_domain(dim);
1247         dim = isl_dim_add(dim, isl_dim_out, 1);
1248         map = isl_map_empty(dim);
1249
1250         for (i = 0; i < pwaff->n; ++i) {
1251                 isl_basic_map *bmap;
1252                 isl_map *map_i;
1253
1254                 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1255                 map_i = isl_map_from_basic_map(bmap);
1256                 map_i = isl_map_intersect_domain(map_i,
1257                                                 isl_set_copy(pwaff->p[i].set));
1258                 map = isl_map_union_disjoint(map, map_i);
1259         }
1260
1261         isl_pw_aff_free(pwaff);
1262
1263         return map;
1264 }
1265
1266 /* Return a set containing those elements in the domain
1267  * of pwaff where it is non-negative.
1268  */
1269 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1270 {
1271         int i;
1272         isl_set *set;
1273
1274         if (!pwaff)
1275                 return NULL;
1276
1277         set = isl_set_empty(isl_pw_aff_get_dim(pwaff));
1278
1279         for (i = 0; i < pwaff->n; ++i) {
1280                 isl_basic_set *bset;
1281                 isl_set *set_i;
1282
1283                 bset = isl_aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff));
1284                 set_i = isl_set_from_basic_set(bset);
1285                 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1286                 set = isl_set_union_disjoint(set, set_i);
1287         }
1288
1289         isl_pw_aff_free(pwaff);
1290
1291         return set;
1292 }
1293
1294 /* Return a set containing those elements in the domain
1295  * of pwaff where it is zero.
1296  */
1297 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
1298 {
1299         int i;
1300         isl_set *set;
1301
1302         if (!pwaff)
1303                 return NULL;
1304
1305         set = isl_set_empty(isl_pw_aff_get_dim(pwaff));
1306
1307         for (i = 0; i < pwaff->n; ++i) {
1308                 isl_basic_set *bset;
1309                 isl_set *set_i;
1310
1311                 bset = isl_aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff));
1312                 set_i = isl_set_from_basic_set(bset);
1313                 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1314                 set = isl_set_union_disjoint(set, set_i);
1315         }
1316
1317         isl_pw_aff_free(pwaff);
1318
1319         return set;
1320 }
1321
1322 /* Return a set containing those elements in the domain
1323  * of pwaff where it is not zero.
1324  */
1325 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
1326 {
1327         return isl_set_complement(isl_pw_aff_zero_set(pwaff));
1328 }
1329
1330 /* Return a set containing those elements in the shared domain
1331  * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1332  *
1333  * We compute the difference on the shared domain and then construct
1334  * the set of values where this difference is non-negative.
1335  * If strict is set, we first subtract 1 from the difference.
1336  * If equal is set, we only return the elements where pwaff1 and pwaff2
1337  * are equal.
1338  */
1339 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
1340         __isl_take isl_pw_aff *pwaff2, int strict, int equal)
1341 {
1342         isl_set *set1, *set2;
1343
1344         set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
1345         set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
1346         set1 = isl_set_intersect(set1, set2);
1347         pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
1348         pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
1349         pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
1350
1351         if (strict) {
1352                 isl_dim *dim = isl_set_get_dim(set1);
1353                 isl_aff *aff;
1354                 aff = isl_aff_zero(isl_local_space_from_dim(dim));
1355                 aff = isl_aff_add_constant_si(aff, -1);
1356                 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
1357         } else
1358                 isl_set_free(set1);
1359
1360         if (equal)
1361                 return isl_pw_aff_zero_set(pwaff1);
1362         return isl_pw_aff_nonneg_set(pwaff1);
1363 }
1364
1365 /* Return a set containing those elements in the shared domain
1366  * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1367  */
1368 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1369         __isl_take isl_pw_aff *pwaff2)
1370 {
1371         return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
1372 }
1373
1374 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1375         __isl_take isl_pw_aff *pwaff2)
1376 {
1377         return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
1378 }
1379
1380 /* Return a set containing those elements in the shared domain
1381  * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1382  */
1383 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1384         __isl_take isl_pw_aff *pwaff2)
1385 {
1386         return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
1387 }
1388
1389 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1390         __isl_take isl_pw_aff *pwaff2)
1391 {
1392         return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
1393 }
1394
1395 /* Return a set containing those elements in the shared domain
1396  * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1397  */
1398 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1399         __isl_take isl_pw_aff *pwaff2)
1400 {
1401         return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
1402 }
1403
1404 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1405         __isl_take isl_pw_aff *pwaff2)
1406 {
1407         return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
1408 }
1409
1410 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
1411         __isl_take isl_pw_aff *pwaff2)
1412 {
1413         return isl_pw_aff_ge_set(pwaff2, pwaff1);
1414 }
1415
1416 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
1417         __isl_take isl_pw_aff *pwaff2)
1418 {
1419         return isl_pw_aff_gt_set(pwaff2, pwaff1);
1420 }
1421
1422 /* Return a set containing those elements in the shared domain
1423  * of the elements of list1 and list2 where each element in list1
1424  * has the relation specified by "fn" with each element in list2.
1425  */
1426 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
1427         __isl_take isl_pw_aff_list *list2,
1428         __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1429                                     __isl_take isl_pw_aff *pwaff2))
1430 {
1431         int i, j;
1432         isl_ctx *ctx;
1433         isl_set *set;
1434
1435         if (!list1 || !list2)
1436                 goto error;
1437
1438         ctx = isl_pw_aff_list_get_ctx(list1);
1439         if (list1->n < 1 || list2->n < 1)
1440                 isl_die(ctx, isl_error_invalid,
1441                         "list should contain at least one element", goto error);
1442
1443         set = isl_set_universe(isl_pw_aff_get_dim(list1->p[0]));
1444         for (i = 0; i < list1->n; ++i)
1445                 for (j = 0; j < list2->n; ++j) {
1446                         isl_set *set_ij;
1447
1448                         set_ij = fn(isl_pw_aff_copy(list1->p[i]),
1449                                     isl_pw_aff_copy(list2->p[j]));
1450                         set = isl_set_intersect(set, set_ij);
1451                 }
1452
1453         isl_pw_aff_list_free(list1);
1454         isl_pw_aff_list_free(list2);
1455         return set;
1456 error:
1457         isl_pw_aff_list_free(list1);
1458         isl_pw_aff_list_free(list2);
1459         return NULL;
1460 }
1461
1462 /* Return a set containing those elements in the shared domain
1463  * of the elements of list1 and list2 where each element in list1
1464  * is equal to each element in list2.
1465  */
1466 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
1467         __isl_take isl_pw_aff_list *list2)
1468 {
1469         return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
1470 }
1471
1472 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
1473         __isl_take isl_pw_aff_list *list2)
1474 {
1475         return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
1476 }
1477
1478 /* Return a set containing those elements in the shared domain
1479  * of the elements of list1 and list2 where each element in list1
1480  * is less than or equal to each element in list2.
1481  */
1482 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
1483         __isl_take isl_pw_aff_list *list2)
1484 {
1485         return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
1486 }
1487
1488 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
1489         __isl_take isl_pw_aff_list *list2)
1490 {
1491         return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
1492 }
1493
1494 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
1495         __isl_take isl_pw_aff_list *list2)
1496 {
1497         return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
1498 }
1499
1500 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
1501         __isl_take isl_pw_aff_list *list2)
1502 {
1503         return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
1504 }
1505
1506
1507 /* Return a set containing those elements in the shared domain
1508  * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1509  */
1510 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1511         __isl_take isl_pw_aff *pwaff2)
1512 {
1513         isl_set *set_lt, *set_gt;
1514
1515         set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
1516                                    isl_pw_aff_copy(pwaff2));
1517         set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
1518         return isl_set_union_disjoint(set_lt, set_gt);
1519 }
1520
1521 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1522         __isl_take isl_pw_aff *pwaff2)
1523 {
1524         return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
1525 }
1526
1527 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
1528         isl_int v)
1529 {
1530         int i;
1531
1532         if (isl_int_is_one(v))
1533                 return pwaff;
1534         if (!isl_int_is_pos(v))
1535                 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1536                         "factor needs to be positive",
1537                         return isl_pw_aff_free(pwaff));
1538         pwaff = isl_pw_aff_cow(pwaff);
1539         if (!pwaff)
1540                 return NULL;
1541         if (pwaff->n == 0)
1542                 return pwaff;
1543
1544         for (i = 0; i < pwaff->n; ++i) {
1545                 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
1546                 if (!pwaff->p[i].aff)
1547                         return isl_pw_aff_free(pwaff);
1548         }
1549
1550         return pwaff;
1551 }
1552
1553 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
1554 {
1555         int i;
1556
1557         pwaff = isl_pw_aff_cow(pwaff);
1558         if (!pwaff)
1559                 return NULL;
1560         if (pwaff->n == 0)
1561                 return pwaff;
1562
1563         for (i = 0; i < pwaff->n; ++i) {
1564                 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
1565                 if (!pwaff->p[i].aff)
1566                         return isl_pw_aff_free(pwaff);
1567         }
1568
1569         return pwaff;
1570 }
1571
1572 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
1573 {
1574         int i;
1575
1576         pwaff = isl_pw_aff_cow(pwaff);
1577         if (!pwaff)
1578                 return NULL;
1579         if (pwaff->n == 0)
1580                 return pwaff;
1581
1582         for (i = 0; i < pwaff->n; ++i) {
1583                 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
1584                 if (!pwaff->p[i].aff)
1585                         return isl_pw_aff_free(pwaff);
1586         }
1587
1588         return pwaff;
1589 }
1590
1591 /* Return an affine expression that is equal to pwaff_true for elements
1592  * in "cond" and to pwaff_false for elements not in "cond".
1593  * That is, return cond ? pwaff_true : pwaff_false;
1594  */
1595 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_set *cond,
1596         __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
1597 {
1598         isl_set *comp;
1599
1600         comp = isl_set_complement(isl_set_copy(cond));
1601         pwaff_true = isl_pw_aff_intersect_domain(pwaff_true, cond);
1602         pwaff_false = isl_pw_aff_intersect_domain(pwaff_false, comp);
1603
1604         return isl_pw_aff_add_disjoint(pwaff_true, pwaff_false);
1605 }
1606
1607 int isl_aff_is_cst(__isl_keep isl_aff *aff)
1608 {
1609         if (!aff)
1610                 return -1;
1611
1612         return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
1613 }
1614
1615 /* Check whether pwaff is a piecewise constant.
1616  */
1617 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
1618 {
1619         int i;
1620
1621         if (!pwaff)
1622                 return -1;
1623
1624         for (i = 0; i < pwaff->n; ++i) {
1625                 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
1626                 if (is_cst < 0 || !is_cst)
1627                         return is_cst;
1628         }
1629
1630         return 1;
1631 }
1632
1633 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
1634         __isl_take isl_aff *aff2)
1635 {
1636         if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
1637                 return isl_aff_mul(aff2, aff1);
1638
1639         if (!isl_aff_is_cst(aff2))
1640                 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
1641                         "at least one affine expression should be constant",
1642                         goto error);
1643
1644         aff1 = isl_aff_cow(aff1);
1645         if (!aff1 || !aff2)
1646                 goto error;
1647
1648         aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
1649         aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
1650
1651         isl_aff_free(aff2);
1652         return aff1;
1653 error:
1654         isl_aff_free(aff1);
1655         isl_aff_free(aff2);
1656         return NULL;
1657 }
1658
1659 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1660         __isl_take isl_pw_aff *pwaff2)
1661 {
1662         int i, j, n;
1663         isl_pw_aff *res;
1664
1665         if (!pwaff1 || !pwaff2)
1666                 goto error;
1667
1668         n = pwaff1->n * pwaff2->n;
1669         res = isl_pw_aff_alloc_(isl_dim_copy(pwaff1->dim), n);
1670
1671         for (i = 0; i < pwaff1->n; ++i) {
1672                 for (j = 0; j < pwaff2->n; ++j) {
1673                         isl_set *common;
1674                         isl_aff *prod;
1675                         common = isl_set_intersect(
1676                                         isl_set_copy(pwaff1->p[i].set),
1677                                         isl_set_copy(pwaff2->p[j].set));
1678                         if (isl_set_plain_is_empty(common)) {
1679                                 isl_set_free(common);
1680                                 continue;
1681                         }
1682
1683                         prod = isl_aff_mul(isl_aff_copy(pwaff1->p[i].aff),
1684                                             isl_aff_copy(pwaff2->p[j].aff));
1685
1686                         res = isl_pw_aff_add_piece(res, common, prod);
1687                 }
1688         }
1689
1690         isl_pw_aff_free(pwaff1);
1691         isl_pw_aff_free(pwaff2);
1692         return res;
1693 error:
1694         isl_pw_aff_free(pwaff1);
1695         isl_pw_aff_free(pwaff2);
1696         return NULL;
1697 }
1698
1699 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1700         __isl_take isl_pw_aff *pwaff2)
1701 {
1702         return align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
1703 }
1704
1705 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1706         __isl_take isl_pw_aff *pwaff2)
1707 {
1708         isl_set *le;
1709
1710         le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
1711                                 isl_pw_aff_copy(pwaff2));
1712         return isl_pw_aff_cond(le, pwaff1, pwaff2);
1713 }
1714
1715 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1716         __isl_take isl_pw_aff *pwaff2)
1717 {
1718         return align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
1719 }
1720
1721 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1722         __isl_take isl_pw_aff *pwaff2)
1723 {
1724         isl_set *le;
1725
1726         le = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
1727                                 isl_pw_aff_copy(pwaff2));
1728         return isl_pw_aff_cond(le, pwaff1, pwaff2);
1729 }
1730
1731 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1732         __isl_take isl_pw_aff *pwaff2)
1733 {
1734         return align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
1735 }
1736
1737 static __isl_give isl_pw_aff *pw_aff_list_reduce(
1738         __isl_take isl_pw_aff_list *list,
1739         __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
1740                                         __isl_take isl_pw_aff *pwaff2))
1741 {
1742         int i;
1743         isl_ctx *ctx;
1744         isl_pw_aff *res;
1745
1746         if (!list)
1747                 return NULL;
1748
1749         ctx = isl_pw_aff_list_get_ctx(list);
1750         if (list->n < 1)
1751                 isl_die(ctx, isl_error_invalid,
1752                         "list should contain at least one element",
1753                         return isl_pw_aff_list_free(list));
1754
1755         res = isl_pw_aff_copy(list->p[0]);
1756         for (i = 1; i < list->n; ++i)
1757                 res = fn(res, isl_pw_aff_copy(list->p[i]));
1758
1759         isl_pw_aff_list_free(list);
1760         return res;
1761 }
1762
1763 /* Return an isl_pw_aff that maps each element in the intersection of the
1764  * domains of the elements of list to the minimal corresponding affine
1765  * expression.
1766  */
1767 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
1768 {
1769         return pw_aff_list_reduce(list, &isl_pw_aff_min);
1770 }
1771
1772 /* Return an isl_pw_aff that maps each element in the intersection of the
1773  * domains of the elements of list to the maximal corresponding affine
1774  * expression.
1775  */
1776 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
1777 {
1778         return pw_aff_list_reduce(list, &isl_pw_aff_max);
1779 }