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