Merge branch 'maint'
[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 #include <isl_config.h>
23
24 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
25         __isl_take isl_vec *v)
26 {
27         isl_aff *aff;
28
29         if (!ls || !v)
30                 goto error;
31
32         aff = isl_calloc_type(v->ctx, struct isl_aff);
33         if (!aff)
34                 goto error;
35
36         aff->ref = 1;
37         aff->ls = ls;
38         aff->v = v;
39
40         return aff;
41 error:
42         isl_local_space_free(ls);
43         isl_vec_free(v);
44         return NULL;
45 }
46
47 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
48 {
49         isl_ctx *ctx;
50         isl_vec *v;
51         unsigned total;
52
53         if (!ls)
54                 return NULL;
55
56         ctx = isl_local_space_get_ctx(ls);
57         if (!isl_local_space_divs_known(ls))
58                 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
59                         goto error);
60         if (!isl_local_space_is_set(ls))
61                 isl_die(ctx, isl_error_invalid,
62                         "domain of affine expression should be a set",
63                         goto error);
64
65         total = isl_local_space_dim(ls, isl_dim_all);
66         v = isl_vec_alloc(ctx, 1 + 1 + total);
67         return isl_aff_alloc_vec(ls, v);
68 error:
69         isl_local_space_free(ls);
70         return NULL;
71 }
72
73 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
74 {
75         isl_aff *aff;
76
77         aff = isl_aff_alloc(ls);
78         if (!aff)
79                 return NULL;
80
81         isl_int_set_si(aff->v->el[0], 1);
82         isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
83
84         return aff;
85 }
86
87 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
88 {
89         if (!aff)
90                 return NULL;
91
92         aff->ref++;
93         return aff;
94 }
95
96 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
97 {
98         if (!aff)
99                 return NULL;
100
101         return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
102                                  isl_vec_copy(aff->v));
103 }
104
105 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
106 {
107         if (!aff)
108                 return NULL;
109
110         if (aff->ref == 1)
111                 return aff;
112         aff->ref--;
113         return isl_aff_dup(aff);
114 }
115
116 void *isl_aff_free(__isl_take isl_aff *aff)
117 {
118         if (!aff)
119                 return NULL;
120
121         if (--aff->ref > 0)
122                 return NULL;
123
124         isl_local_space_free(aff->ls);
125         isl_vec_free(aff->v);
126
127         free(aff);
128
129         return NULL;
130 }
131
132 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
133 {
134         return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
135 }
136
137 /* Externally, an isl_aff has a map space, but internally, the
138  * ls field corresponds to the domain of that space.
139  */
140 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
141 {
142         if (!aff)
143                 return 0;
144         if (type == isl_dim_out)
145                 return 1;
146         if (type == isl_dim_in)
147                 type = isl_dim_set;
148         return isl_local_space_dim(aff->ls, type);
149 }
150
151 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
152 {
153         return aff ? isl_local_space_get_space(aff->ls) : NULL;
154 }
155
156 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
157 {
158         isl_space *space;
159         if (!aff)
160                 return NULL;
161         space = isl_local_space_get_space(aff->ls);
162         space = isl_space_from_domain(space);
163         space = isl_space_add_dims(space, isl_dim_out, 1);
164         return space;
165 }
166
167 __isl_give isl_local_space *isl_aff_get_domain_local_space(
168         __isl_keep isl_aff *aff)
169 {
170         return aff ? isl_local_space_copy(aff->ls) : NULL;
171 }
172
173 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
174 {
175         isl_local_space *ls;
176         if (!aff)
177                 return NULL;
178         ls = isl_local_space_copy(aff->ls);
179         ls = isl_local_space_from_domain(ls);
180         ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
181         return ls;
182 }
183
184 /* Externally, an isl_aff has a map space, but internally, the
185  * ls field corresponds to the domain of that space.
186  */
187 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
188         enum isl_dim_type type, unsigned pos)
189 {
190         if (!aff)
191                 return NULL;
192         if (type == isl_dim_out)
193                 return NULL;
194         if (type == isl_dim_in)
195                 type = isl_dim_set;
196         return isl_local_space_get_dim_name(aff->ls, type, pos);
197 }
198
199 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
200         __isl_take isl_space *dim)
201 {
202         aff = isl_aff_cow(aff);
203         if (!aff || !dim)
204                 goto error;
205
206         aff->ls = isl_local_space_reset_space(aff->ls, dim);
207         if (!aff->ls)
208                 return isl_aff_free(aff);
209
210         return aff;
211 error:
212         isl_aff_free(aff);
213         isl_space_free(dim);
214         return NULL;
215 }
216
217 /* Reset the space of "aff".  This function is called from isl_pw_templ.c
218  * and doesn't know if the space of an element object is represented
219  * directly or through its domain.  It therefore passes along both.
220  */
221 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
222         __isl_take isl_space *space, __isl_take isl_space *domain)
223 {
224         isl_space_free(space);
225         return isl_aff_reset_domain_space(aff, domain);
226 }
227
228 /* Reorder the coefficients of the affine expression based
229  * on the given reodering.
230  * The reordering r is assumed to have been extended with the local
231  * variables.
232  */
233 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
234         __isl_take isl_reordering *r, int n_div)
235 {
236         isl_vec *res;
237         int i;
238
239         if (!vec || !r)
240                 goto error;
241
242         res = isl_vec_alloc(vec->ctx,
243                             2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
244         isl_seq_cpy(res->el, vec->el, 2);
245         isl_seq_clr(res->el + 2, res->size - 2);
246         for (i = 0; i < r->len; ++i)
247                 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
248
249         isl_reordering_free(r);
250         isl_vec_free(vec);
251         return res;
252 error:
253         isl_vec_free(vec);
254         isl_reordering_free(r);
255         return NULL;
256 }
257
258 /* Reorder the dimensions of the domain of "aff" according
259  * to the given reordering.
260  */
261 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
262         __isl_take isl_reordering *r)
263 {
264         aff = isl_aff_cow(aff);
265         if (!aff)
266                 goto error;
267
268         r = isl_reordering_extend(r, aff->ls->div->n_row);
269         aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
270                                 aff->ls->div->n_row);
271         aff->ls = isl_local_space_realign(aff->ls, r);
272
273         if (!aff->v || !aff->ls)
274                 return isl_aff_free(aff);
275
276         return aff;
277 error:
278         isl_aff_free(aff);
279         isl_reordering_free(r);
280         return NULL;
281 }
282
283 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
284 {
285         if (!aff)
286                 return -1;
287
288         return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
289 }
290
291 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
292 {
293         int equal;
294
295         if (!aff1 || !aff2)
296                 return -1;
297
298         equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
299         if (equal < 0 || !equal)
300                 return equal;
301
302         return isl_vec_is_equal(aff1->v, aff2->v);
303 }
304
305 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
306 {
307         if (!aff)
308                 return -1;
309         isl_int_set(*v, aff->v->el[0]);
310         return 0;
311 }
312
313 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
314 {
315         if (!aff)
316                 return -1;
317         isl_int_set(*v, aff->v->el[1]);
318         return 0;
319 }
320
321 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
322         enum isl_dim_type type, int pos, isl_int *v)
323 {
324         if (!aff)
325                 return -1;
326
327         if (type == isl_dim_out)
328                 isl_die(aff->v->ctx, isl_error_invalid,
329                         "output/set dimension does not have a coefficient",
330                         return -1);
331         if (type == isl_dim_in)
332                 type = isl_dim_set;
333
334         if (pos >= isl_local_space_dim(aff->ls, type))
335                 isl_die(aff->v->ctx, isl_error_invalid,
336                         "position out of bounds", return -1);
337
338         pos += isl_local_space_offset(aff->ls, type);
339         isl_int_set(*v, aff->v->el[1 + pos]);
340
341         return 0;
342 }
343
344 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
345 {
346         aff = isl_aff_cow(aff);
347         if (!aff)
348                 return NULL;
349
350         aff->v = isl_vec_cow(aff->v);
351         if (!aff->v)
352                 return isl_aff_free(aff);
353
354         isl_int_set(aff->v->el[0], v);
355
356         return aff;
357 }
358
359 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
360 {
361         aff = isl_aff_cow(aff);
362         if (!aff)
363                 return NULL;
364
365         aff->v = isl_vec_cow(aff->v);
366         if (!aff->v)
367                 return isl_aff_free(aff);
368
369         isl_int_set(aff->v->el[1], v);
370
371         return aff;
372 }
373
374 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
375 {
376         if (isl_int_is_zero(v))
377                 return aff;
378
379         aff = isl_aff_cow(aff);
380         if (!aff)
381                 return NULL;
382
383         aff->v = isl_vec_cow(aff->v);
384         if (!aff->v)
385                 return isl_aff_free(aff);
386
387         isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
388
389         return aff;
390 }
391
392 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
393 {
394         isl_int t;
395
396         isl_int_init(t);
397         isl_int_set_si(t, v);
398         aff = isl_aff_add_constant(aff, t);
399         isl_int_clear(t);
400
401         return aff;
402 }
403
404 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
405 {
406         aff = isl_aff_cow(aff);
407         if (!aff)
408                 return NULL;
409
410         aff->v = isl_vec_cow(aff->v);
411         if (!aff->v)
412                 return isl_aff_free(aff);
413
414         isl_int_set_si(aff->v->el[1], v);
415
416         return aff;
417 }
418
419 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
420         enum isl_dim_type type, int pos, isl_int v)
421 {
422         if (!aff)
423                 return NULL;
424
425         if (type == isl_dim_out)
426                 isl_die(aff->v->ctx, isl_error_invalid,
427                         "output/set dimension does not have a coefficient",
428                         return isl_aff_free(aff));
429         if (type == isl_dim_in)
430                 type = isl_dim_set;
431
432         if (pos >= isl_local_space_dim(aff->ls, type))
433                 isl_die(aff->v->ctx, isl_error_invalid,
434                         "position out of bounds", return isl_aff_free(aff));
435
436         aff = isl_aff_cow(aff);
437         if (!aff)
438                 return NULL;
439
440         aff->v = isl_vec_cow(aff->v);
441         if (!aff->v)
442                 return isl_aff_free(aff);
443
444         pos += isl_local_space_offset(aff->ls, type);
445         isl_int_set(aff->v->el[1 + pos], v);
446
447         return aff;
448 }
449
450 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
451         enum isl_dim_type type, int pos, int v)
452 {
453         if (!aff)
454                 return NULL;
455
456         if (type == isl_dim_out)
457                 isl_die(aff->v->ctx, isl_error_invalid,
458                         "output/set dimension does not have a coefficient",
459                         return isl_aff_free(aff));
460         if (type == isl_dim_in)
461                 type = isl_dim_set;
462
463         if (pos >= isl_local_space_dim(aff->ls, type))
464                 isl_die(aff->v->ctx, isl_error_invalid,
465                         "position out of bounds", return isl_aff_free(aff));
466
467         aff = isl_aff_cow(aff);
468         if (!aff)
469                 return NULL;
470
471         aff->v = isl_vec_cow(aff->v);
472         if (!aff->v)
473                 return isl_aff_free(aff);
474
475         pos += isl_local_space_offset(aff->ls, type);
476         isl_int_set_si(aff->v->el[1 + pos], v);
477
478         return aff;
479 }
480
481 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
482         enum isl_dim_type type, int pos, isl_int v)
483 {
484         if (!aff)
485                 return NULL;
486
487         if (type == isl_dim_out)
488                 isl_die(aff->v->ctx, isl_error_invalid,
489                         "output/set dimension does not have a coefficient",
490                         return isl_aff_free(aff));
491         if (type == isl_dim_in)
492                 type = isl_dim_set;
493
494         if (pos >= isl_local_space_dim(aff->ls, type))
495                 isl_die(aff->v->ctx, isl_error_invalid,
496                         "position out of bounds", return isl_aff_free(aff));
497
498         aff = isl_aff_cow(aff);
499         if (!aff)
500                 return NULL;
501
502         aff->v = isl_vec_cow(aff->v);
503         if (!aff->v)
504                 return isl_aff_free(aff);
505
506         pos += isl_local_space_offset(aff->ls, type);
507         isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
508
509         return aff;
510 }
511
512 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
513         enum isl_dim_type type, int pos, int v)
514 {
515         isl_int t;
516
517         isl_int_init(t);
518         isl_int_set_si(t, v);
519         aff = isl_aff_add_coefficient(aff, type, pos, t);
520         isl_int_clear(t);
521
522         return aff;
523 }
524
525 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
526 {
527         if (!aff)
528                 return NULL;
529
530         return isl_local_space_get_div(aff->ls, pos);
531 }
532
533 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
534 {
535         aff = isl_aff_cow(aff);
536         if (!aff)
537                 return NULL;
538         aff->v = isl_vec_cow(aff->v);
539         if (!aff->v)
540                 return isl_aff_free(aff);
541
542         isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
543
544         return aff;
545 }
546
547 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
548 {
549         if (!aff)
550                 return NULL;
551         aff->v = isl_vec_normalize(aff->v);
552         if (!aff->v)
553                 return isl_aff_free(aff);
554         return aff;
555 }
556
557 /* Given f, return floor(f).
558  * If f is an integer expression, then just return f.
559  * Otherwise, if f = g/m, write g = q m + r,
560  * create a new div d = [r/m] and return the expression q + d.
561  * The coefficients in r are taken to lie between -m/2 and m/2.
562  */
563 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
564 {
565         int i;
566         int size;
567         isl_ctx *ctx;
568         isl_vec *div;
569
570         if (!aff)
571                 return NULL;
572
573         if (isl_int_is_one(aff->v->el[0]))
574                 return aff;
575
576         aff = isl_aff_cow(aff);
577         if (!aff)
578                 return NULL;
579
580         aff->v = isl_vec_cow(aff->v);
581         div = isl_vec_copy(aff->v);
582         div = isl_vec_cow(div);
583         if (!div)
584                 return isl_aff_free(aff);
585
586         ctx = isl_aff_get_ctx(aff);
587         isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
588         for (i = 1; i < aff->v->size; ++i) {
589                 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
590                 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
591                 if (isl_int_gt(div->el[i], aff->v->el[0])) {
592                         isl_int_sub(div->el[i], div->el[i], div->el[0]);
593                         isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
594                 }
595         }
596
597         aff->ls = isl_local_space_add_div(aff->ls, div);
598         if (!aff->ls)
599                 return isl_aff_free(aff);
600
601         size = aff->v->size;
602         aff->v = isl_vec_extend(aff->v, size + 1);
603         if (!aff->v)
604                 return isl_aff_free(aff);
605         isl_int_set_si(aff->v->el[0], 1);
606         isl_int_set_si(aff->v->el[size], 1);
607
608         return aff;
609 }
610
611 /* Compute
612  *
613  *      aff mod m = aff - m * floor(aff/m)
614  */
615 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
616 {
617         isl_aff *res;
618
619         res = isl_aff_copy(aff);
620         aff = isl_aff_scale_down(aff, m);
621         aff = isl_aff_floor(aff);
622         aff = isl_aff_scale(aff, m);
623         res = isl_aff_sub(res, aff);
624
625         return res;
626 }
627
628 /* Compute
629  *
630  *      pwaff mod m = pwaff - m * floor(pwaff/m)
631  */
632 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
633 {
634         isl_pw_aff *res;
635
636         res = isl_pw_aff_copy(pwaff);
637         pwaff = isl_pw_aff_scale_down(pwaff, m);
638         pwaff = isl_pw_aff_floor(pwaff);
639         pwaff = isl_pw_aff_scale(pwaff, m);
640         res = isl_pw_aff_sub(res, pwaff);
641
642         return res;
643 }
644
645 /* Given f, return ceil(f).
646  * If f is an integer expression, then just return f.
647  * Otherwise, create a new div d = [-f] and return the expression -d.
648  */
649 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
650 {
651         if (!aff)
652                 return NULL;
653
654         if (isl_int_is_one(aff->v->el[0]))
655                 return aff;
656
657         aff = isl_aff_neg(aff);
658         aff = isl_aff_floor(aff);
659         aff = isl_aff_neg(aff);
660
661         return aff;
662 }
663
664 /* Apply the expansion computed by isl_merge_divs.
665  * The expansion itself is given by "exp" while the resulting
666  * list of divs is given by "div".
667  */
668 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
669         __isl_take isl_mat *div, int *exp)
670 {
671         int i, j;
672         int old_n_div;
673         int new_n_div;
674         int offset;
675
676         aff = isl_aff_cow(aff);
677         if (!aff || !div)
678                 goto error;
679
680         old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
681         new_n_div = isl_mat_rows(div);
682         if (new_n_div < old_n_div)
683                 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
684                         "not an expansion", goto error);
685
686         aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
687         if (!aff->v)
688                 goto error;
689
690         offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
691         j = old_n_div - 1;
692         for (i = new_n_div - 1; i >= 0; --i) {
693                 if (j >= 0 && exp[j] == i) {
694                         if (i != j)
695                                 isl_int_swap(aff->v->el[offset + i],
696                                              aff->v->el[offset + j]);
697                         j--;
698                 } else
699                         isl_int_set_si(aff->v->el[offset + i], 0);
700         }
701
702         aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
703         if (!aff->ls)
704                 goto error;
705         isl_mat_free(div);
706         return aff;
707 error:
708         isl_aff_free(aff);
709         isl_mat_free(div);
710         return NULL;
711 }
712
713 /* Add two affine expressions that live in the same local space.
714  */
715 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
716         __isl_take isl_aff *aff2)
717 {
718         isl_int gcd, f;
719
720         aff1 = isl_aff_cow(aff1);
721         if (!aff1 || !aff2)
722                 goto error;
723
724         aff1->v = isl_vec_cow(aff1->v);
725         if (!aff1->v)
726                 goto error;
727
728         isl_int_init(gcd);
729         isl_int_init(f);
730         isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
731         isl_int_divexact(f, aff2->v->el[0], gcd);
732         isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
733         isl_int_divexact(f, aff1->v->el[0], gcd);
734         isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
735         isl_int_divexact(f, aff2->v->el[0], gcd);
736         isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
737         isl_int_clear(f);
738         isl_int_clear(gcd);
739
740         isl_aff_free(aff2);
741         return aff1;
742 error:
743         isl_aff_free(aff1);
744         isl_aff_free(aff2);
745         return NULL;
746 }
747
748 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
749         __isl_take isl_aff *aff2)
750 {
751         isl_ctx *ctx;
752         int *exp1 = NULL;
753         int *exp2 = NULL;
754         isl_mat *div;
755
756         if (!aff1 || !aff2)
757                 goto error;
758
759         ctx = isl_aff_get_ctx(aff1);
760         if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
761                 isl_die(ctx, isl_error_invalid,
762                         "spaces don't match", goto error);
763
764         if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
765                 return add_expanded(aff1, aff2);
766
767         exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
768         exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
769         if (!exp1 || !exp2)
770                 goto error;
771
772         div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
773         aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
774         aff2 = isl_aff_expand_divs(aff2, div, exp2);
775         free(exp1);
776         free(exp2);
777
778         return add_expanded(aff1, aff2);
779 error:
780         free(exp1);
781         free(exp2);
782         isl_aff_free(aff1);
783         isl_aff_free(aff2);
784         return NULL;
785 }
786
787 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
788         __isl_take isl_aff *aff2)
789 {
790         return isl_aff_add(aff1, isl_aff_neg(aff2));
791 }
792
793 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
794 {
795         isl_int gcd;
796
797         if (isl_int_is_one(f))
798                 return aff;
799
800         aff = isl_aff_cow(aff);
801         if (!aff)
802                 return NULL;
803         aff->v = isl_vec_cow(aff->v);
804         if (!aff->v)
805                 return isl_aff_free(aff);
806
807         isl_int_init(gcd);
808         isl_int_gcd(gcd, aff->v->el[0], f);
809         isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
810         isl_int_divexact(gcd, f, gcd);
811         isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
812         isl_int_clear(gcd);
813
814         return aff;
815 }
816
817 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
818 {
819         isl_int gcd;
820
821         if (isl_int_is_one(f))
822                 return aff;
823
824         aff = isl_aff_cow(aff);
825         if (!aff)
826                 return NULL;
827         aff->v = isl_vec_cow(aff->v);
828         if (!aff->v)
829                 return isl_aff_free(aff);
830
831         isl_int_init(gcd);
832         isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
833         isl_int_gcd(gcd, gcd, f);
834         isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
835         isl_int_divexact(gcd, f, gcd);
836         isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
837         isl_int_clear(gcd);
838
839         return aff;
840 }
841
842 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
843 {
844         isl_int v;
845
846         if (f == 1)
847                 return aff;
848
849         isl_int_init(v);
850         isl_int_set_ui(v, f);
851         aff = isl_aff_scale_down(aff, v);
852         isl_int_clear(v);
853
854         return aff;
855 }
856
857 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
858         enum isl_dim_type type, unsigned pos, const char *s)
859 {
860         aff = isl_aff_cow(aff);
861         if (!aff)
862                 return NULL;
863         if (type == isl_dim_out)
864                 isl_die(aff->v->ctx, isl_error_invalid,
865                         "cannot set name of output/set dimension",
866                         return isl_aff_free(aff));
867         if (type == isl_dim_in)
868                 type = isl_dim_set;
869         aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
870         if (!aff->ls)
871                 return isl_aff_free(aff);
872
873         return aff;
874 }
875
876 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
877         enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
878 {
879         aff = isl_aff_cow(aff);
880         if (!aff)
881                 return isl_id_free(id);
882         if (type == isl_dim_out)
883                 isl_die(aff->v->ctx, isl_error_invalid,
884                         "cannot set name of output/set dimension",
885                         goto error);
886         if (type == isl_dim_in)
887                 type = isl_dim_set;
888         aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
889         if (!aff->ls)
890                 return isl_aff_free(aff);
891
892         return aff;
893 error:
894         isl_id_free(id);
895         isl_aff_free(aff);
896         return NULL;
897 }
898
899 /* Exploit the equalities in "eq" to simplify the affine expression
900  * and the expressions of the integer divisions in the local space.
901  * The integer divisions in this local space are assumed to appear
902  * as regular dimensions in "eq".
903  */
904 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
905         __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
906 {
907         int i, j;
908         unsigned total;
909         unsigned n_div;
910
911         if (!eq)
912                 goto error;
913         if (eq->n_eq == 0) {
914                 isl_basic_set_free(eq);
915                 return aff;
916         }
917
918         aff = isl_aff_cow(aff);
919         if (!aff)
920                 goto error;
921
922         aff->ls = isl_local_space_substitute_equalities(aff->ls,
923                                                         isl_basic_set_copy(eq));
924         if (!aff->ls)
925                 goto error;
926
927         total = 1 + isl_space_dim(eq->dim, isl_dim_all);
928         n_div = eq->n_div;
929         for (i = 0; i < eq->n_eq; ++i) {
930                 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
931                 if (j < 0 || j == 0 || j >= total)
932                         continue;
933
934                 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
935                                 &aff->v->el[0]);
936         }
937
938         isl_basic_set_free(eq);
939         return aff;
940 error:
941         isl_basic_set_free(eq);
942         isl_aff_free(aff);
943         return NULL;
944 }
945
946 /* Exploit the equalities in "eq" to simplify the affine expression
947  * and the expressions of the integer divisions in the local space.
948  */
949 static __isl_give isl_aff *isl_aff_substitute_equalities(
950         __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
951 {
952         int n_div;
953
954         if (!aff || !eq)
955                 goto error;
956         n_div = isl_local_space_dim(aff->ls, isl_dim_div);
957         if (n_div > 0)
958                 eq = isl_basic_set_add(eq, isl_dim_set, n_div);
959         return isl_aff_substitute_equalities_lifted(aff, eq);
960 error:
961         isl_basic_set_free(eq);
962         isl_aff_free(aff);
963         return NULL;
964 }
965
966 /* Look for equalities among the variables shared by context and aff
967  * and the integer divisions of aff, if any.
968  * The equalities are then used to eliminate coefficients and/or integer
969  * divisions from aff.
970  */
971 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
972         __isl_take isl_set *context)
973 {
974         isl_basic_set *hull;
975         int n_div;
976
977         if (!aff)
978                 goto error;
979         n_div = isl_local_space_dim(aff->ls, isl_dim_div);
980         if (n_div > 0) {
981                 isl_basic_set *bset;
982                 isl_local_space *ls;
983                 context = isl_set_add_dims(context, isl_dim_set, n_div);
984                 ls = isl_aff_get_domain_local_space(aff);
985                 bset = isl_basic_set_from_local_space(ls);
986                 bset = isl_basic_set_lift(bset);
987                 bset = isl_basic_set_flatten(bset);
988                 context = isl_set_intersect(context,
989                                             isl_set_from_basic_set(bset));
990         }
991
992         hull = isl_set_affine_hull(context);
993         return isl_aff_substitute_equalities_lifted(aff, hull);
994 error:
995         isl_aff_free(aff);
996         isl_set_free(context);
997         return NULL;
998 }
999
1000 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1001         __isl_take isl_set *context)
1002 {
1003         isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1004         dom_context = isl_set_intersect_params(dom_context, context);
1005         return isl_aff_gist(aff, dom_context);
1006 }
1007
1008 /* Return a basic set containing those elements in the space
1009  * of aff where it is non-negative.
1010  */
1011 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1012 {
1013         isl_constraint *ineq;
1014
1015         ineq = isl_inequality_from_aff(aff);
1016
1017         return isl_basic_set_from_constraint(ineq);
1018 }
1019
1020 /* Return a basic set containing those elements in the space
1021  * of aff where it is zero.
1022  */
1023 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1024 {
1025         isl_constraint *ineq;
1026
1027         ineq = isl_equality_from_aff(aff);
1028
1029         return isl_basic_set_from_constraint(ineq);
1030 }
1031
1032 /* Return a basic set containing those elements in the shared space
1033  * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1034  */
1035 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1036         __isl_take isl_aff *aff2)
1037 {
1038         aff1 = isl_aff_sub(aff1, aff2);
1039
1040         return isl_aff_nonneg_basic_set(aff1);
1041 }
1042
1043 /* Return a basic set containing those elements in the shared space
1044  * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1045  */
1046 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1047         __isl_take isl_aff *aff2)
1048 {
1049         return isl_aff_ge_basic_set(aff2, aff1);
1050 }
1051
1052 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1053         __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1054 {
1055         aff1 = isl_aff_add(aff1, aff2);
1056         aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1057         return aff1;
1058 }
1059
1060 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1061 {
1062         if (!aff)
1063                 return -1;
1064
1065         return 0;
1066 }
1067
1068 /* Check whether the given affine expression has non-zero coefficient
1069  * for any dimension in the given range or if any of these dimensions
1070  * appear with non-zero coefficients in any of the integer divisions
1071  * involved in the affine expression.
1072  */
1073 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1074         enum isl_dim_type type, unsigned first, unsigned n)
1075 {
1076         int i;
1077         isl_ctx *ctx;
1078         int *active = NULL;
1079         int involves = 0;
1080
1081         if (!aff)
1082                 return -1;
1083         if (n == 0)
1084                 return 0;
1085
1086         ctx = isl_aff_get_ctx(aff);
1087         if (first + n > isl_aff_dim(aff, type))
1088                 isl_die(ctx, isl_error_invalid,
1089                         "range out of bounds", return -1);
1090
1091         active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1092         if (!active)
1093                 goto error;
1094
1095         first += isl_local_space_offset(aff->ls, type) - 1;
1096         for (i = 0; i < n; ++i)
1097                 if (active[first + i]) {
1098                         involves = 1;
1099                         break;
1100                 }
1101
1102         free(active);
1103
1104         return involves;
1105 error:
1106         free(active);
1107         return -1;
1108 }
1109
1110 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1111         enum isl_dim_type type, unsigned first, unsigned n)
1112 {
1113         isl_ctx *ctx;
1114
1115         if (!aff)
1116                 return NULL;
1117         if (type == isl_dim_out)
1118                 isl_die(aff->v->ctx, isl_error_invalid,
1119                         "cannot drop output/set dimension",
1120                         return isl_aff_free(aff));
1121         if (type == isl_dim_in)
1122                 type = isl_dim_set;
1123         if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1124                 return aff;
1125
1126         ctx = isl_aff_get_ctx(aff);
1127         if (first + n > isl_local_space_dim(aff->ls, type))
1128                 isl_die(ctx, isl_error_invalid, "range out of bounds",
1129                         return isl_aff_free(aff));
1130
1131         aff = isl_aff_cow(aff);
1132         if (!aff)
1133                 return NULL;
1134
1135         aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1136         if (!aff->ls)
1137                 return isl_aff_free(aff);
1138
1139         first += 1 + isl_local_space_offset(aff->ls, type);
1140         aff->v = isl_vec_drop_els(aff->v, first, n);
1141         if (!aff->v)
1142                 return isl_aff_free(aff);
1143
1144         return aff;
1145 }
1146
1147 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1148         enum isl_dim_type type, unsigned first, unsigned n)
1149 {
1150         isl_ctx *ctx;
1151
1152         if (!aff)
1153                 return NULL;
1154         if (type == isl_dim_out)
1155                 isl_die(aff->v->ctx, isl_error_invalid,
1156                         "cannot insert output/set dimensions",
1157                         return isl_aff_free(aff));
1158         if (type == isl_dim_in)
1159                 type = isl_dim_set;
1160         if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1161                 return aff;
1162
1163         ctx = isl_aff_get_ctx(aff);
1164         if (first > isl_local_space_dim(aff->ls, type))
1165                 isl_die(ctx, isl_error_invalid, "position out of bounds",
1166                         return isl_aff_free(aff));
1167
1168         aff = isl_aff_cow(aff);
1169         if (!aff)
1170                 return NULL;
1171
1172         aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1173         if (!aff->ls)
1174                 return isl_aff_free(aff);
1175
1176         first += 1 + isl_local_space_offset(aff->ls, type);
1177         aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1178         if (!aff->v)
1179                 return isl_aff_free(aff);
1180
1181         return aff;
1182 }
1183
1184 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1185         enum isl_dim_type type, unsigned n)
1186 {
1187         unsigned pos;
1188
1189         pos = isl_aff_dim(aff, type);
1190
1191         return isl_aff_insert_dims(aff, type, pos, n);
1192 }
1193
1194 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1195         enum isl_dim_type type, unsigned n)
1196 {
1197         unsigned pos;
1198
1199         pos = isl_pw_aff_dim(pwaff, type);
1200
1201         return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1202 }
1203
1204 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1205 {
1206         isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
1207         return isl_pw_aff_alloc(dom, aff);
1208 }
1209
1210 #undef PW
1211 #define PW isl_pw_aff
1212 #undef EL
1213 #define EL isl_aff
1214 #undef EL_IS_ZERO
1215 #define EL_IS_ZERO is_empty
1216 #undef ZERO
1217 #define ZERO empty
1218 #undef IS_ZERO
1219 #define IS_ZERO is_empty
1220 #undef FIELD
1221 #define FIELD aff
1222 #undef DEFAULT_IS_ZERO
1223 #define DEFAULT_IS_ZERO 0
1224
1225 #define NO_EVAL
1226 #define NO_OPT
1227 #define NO_MOVE_DIMS
1228 #define NO_LIFT
1229 #define NO_MORPH
1230
1231 #include <isl_pw_templ.c>
1232
1233 static __isl_give isl_set *align_params_pw_pw_set_and(
1234         __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1235         __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1236                                     __isl_take isl_pw_aff *pwaff2))
1237 {
1238         if (!pwaff1 || !pwaff2)
1239                 goto error;
1240         if (isl_space_match(pwaff1->dim, isl_dim_param,
1241                           pwaff2->dim, isl_dim_param))
1242                 return fn(pwaff1, pwaff2);
1243         if (!isl_space_has_named_params(pwaff1->dim) ||
1244             !isl_space_has_named_params(pwaff2->dim))
1245                 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1246                         "unaligned unnamed parameters", goto error);
1247         pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
1248         pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
1249         return fn(pwaff1, pwaff2);
1250 error:
1251         isl_pw_aff_free(pwaff1);
1252         isl_pw_aff_free(pwaff2);
1253         return NULL;
1254 }
1255
1256 /* Compute a piecewise quasi-affine expression with a domain that
1257  * is the union of those of pwaff1 and pwaff2 and such that on each
1258  * cell, the quasi-affine expression is the better (according to cmp)
1259  * of those of pwaff1 and pwaff2.  If only one of pwaff1 or pwaff2
1260  * is defined on a given cell, then the associated expression
1261  * is the defined one.
1262  */
1263 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1264         __isl_take isl_pw_aff *pwaff2,
1265         __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
1266                                         __isl_take isl_aff *aff2))
1267 {
1268         int i, j, n;
1269         isl_pw_aff *res;
1270         isl_ctx *ctx;
1271         isl_set *set;
1272
1273         if (!pwaff1 || !pwaff2)
1274                 goto error;
1275
1276         ctx = isl_space_get_ctx(pwaff1->dim);
1277         if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
1278                 isl_die(ctx, isl_error_invalid,
1279                         "arguments should live in same space", goto error);
1280
1281         if (isl_pw_aff_is_empty(pwaff1)) {
1282                 isl_pw_aff_free(pwaff1);
1283                 return pwaff2;
1284         }
1285
1286         if (isl_pw_aff_is_empty(pwaff2)) {
1287                 isl_pw_aff_free(pwaff2);
1288                 return pwaff1;
1289         }
1290
1291         n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1292         res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
1293
1294         for (i = 0; i < pwaff1->n; ++i) {
1295                 set = isl_set_copy(pwaff1->p[i].set);
1296                 for (j = 0; j < pwaff2->n; ++j) {
1297                         struct isl_set *common;
1298                         isl_set *better;
1299
1300                         common = isl_set_intersect(
1301                                         isl_set_copy(pwaff1->p[i].set),
1302                                         isl_set_copy(pwaff2->p[j].set));
1303                         better = isl_set_from_basic_set(cmp(
1304                                         isl_aff_copy(pwaff2->p[j].aff),
1305                                         isl_aff_copy(pwaff1->p[i].aff)));
1306                         better = isl_set_intersect(common, better);
1307                         if (isl_set_plain_is_empty(better)) {
1308                                 isl_set_free(better);
1309                                 continue;
1310                         }
1311                         set = isl_set_subtract(set, isl_set_copy(better));
1312
1313                         res = isl_pw_aff_add_piece(res, better,
1314                                                 isl_aff_copy(pwaff2->p[j].aff));
1315                 }
1316                 res = isl_pw_aff_add_piece(res, set,
1317                                                 isl_aff_copy(pwaff1->p[i].aff));
1318         }
1319
1320         for (j = 0; j < pwaff2->n; ++j) {
1321                 set = isl_set_copy(pwaff2->p[j].set);
1322                 for (i = 0; i < pwaff1->n; ++i)
1323                         set = isl_set_subtract(set,
1324                                         isl_set_copy(pwaff1->p[i].set));
1325                 res = isl_pw_aff_add_piece(res, set,
1326                                                 isl_aff_copy(pwaff2->p[j].aff));
1327         }
1328
1329         isl_pw_aff_free(pwaff1);
1330         isl_pw_aff_free(pwaff2);
1331
1332         return res;
1333 error:
1334         isl_pw_aff_free(pwaff1);
1335         isl_pw_aff_free(pwaff2);
1336         return NULL;
1337 }
1338
1339 /* Compute a piecewise quasi-affine expression with a domain that
1340  * is the union of those of pwaff1 and pwaff2 and such that on each
1341  * cell, the quasi-affine expression is the maximum of those of pwaff1
1342  * and pwaff2.  If only one of pwaff1 or pwaff2 is defined on a given
1343  * cell, then the associated expression is the defined one.
1344  */
1345 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1346         __isl_take isl_pw_aff *pwaff2)
1347 {
1348         return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
1349 }
1350
1351 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1352         __isl_take isl_pw_aff *pwaff2)
1353 {
1354         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1355                                                         &pw_aff_union_max);
1356 }
1357
1358 /* Compute a piecewise quasi-affine expression with a domain that
1359  * is the union of those of pwaff1 and pwaff2 and such that on each
1360  * cell, the quasi-affine expression is the minimum of those of pwaff1
1361  * and pwaff2.  If only one of pwaff1 or pwaff2 is defined on a given
1362  * cell, then the associated expression is the defined one.
1363  */
1364 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1365         __isl_take isl_pw_aff *pwaff2)
1366 {
1367         return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
1368 }
1369
1370 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1371         __isl_take isl_pw_aff *pwaff2)
1372 {
1373         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1374                                                         &pw_aff_union_min);
1375 }
1376
1377 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1378         __isl_take isl_pw_aff *pwaff2, int max)
1379 {
1380         if (max)
1381                 return isl_pw_aff_union_max(pwaff1, pwaff2);
1382         else
1383                 return isl_pw_aff_union_min(pwaff1, pwaff2);
1384 }
1385
1386 /* Construct a map with as domain the domain of pwaff and
1387  * one-dimensional range corresponding to the affine expressions.
1388  */
1389 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1390 {
1391         int i;
1392         isl_space *dim;
1393         isl_map *map;
1394
1395         if (!pwaff)
1396                 return NULL;
1397
1398         dim = isl_pw_aff_get_space(pwaff);
1399         map = isl_map_empty(dim);
1400
1401         for (i = 0; i < pwaff->n; ++i) {
1402                 isl_basic_map *bmap;
1403                 isl_map *map_i;
1404
1405                 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1406                 map_i = isl_map_from_basic_map(bmap);
1407                 map_i = isl_map_intersect_domain(map_i,
1408                                                 isl_set_copy(pwaff->p[i].set));
1409                 map = isl_map_union_disjoint(map, map_i);
1410         }
1411
1412         isl_pw_aff_free(pwaff);
1413
1414         return map;
1415 }
1416
1417 /* Construct a map with as domain the domain of pwaff and
1418  * one-dimensional range corresponding to the affine expressions.
1419  */
1420 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1421 {
1422         if (isl_space_is_set(pwaff->dim))
1423                 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1424                         "space of input is not a map",
1425                         return isl_pw_aff_free(pwaff));
1426         return map_from_pw_aff(pwaff);
1427 }
1428
1429 /* Construct a one-dimensional set with as parameter domain
1430  * the domain of pwaff and the single set dimension
1431  * corresponding to the affine expressions.
1432  */
1433 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1434 {
1435         if (!pwaff)
1436                 return NULL;
1437         if (!isl_space_is_set(pwaff->dim))
1438                 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1439                         "space of input is not a set",
1440                         return isl_pw_aff_free(pwaff));
1441         return map_from_pw_aff(pwaff);
1442 }
1443
1444 /* Return a set containing those elements in the domain
1445  * of pwaff where it is non-negative.
1446  */
1447 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1448 {
1449         int i;
1450         isl_set *set;
1451
1452         if (!pwaff)
1453                 return NULL;
1454
1455         set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1456
1457         for (i = 0; i < pwaff->n; ++i) {
1458                 isl_basic_set *bset;
1459                 isl_set *set_i;
1460
1461                 bset = isl_aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff));
1462                 set_i = isl_set_from_basic_set(bset);
1463                 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1464                 set = isl_set_union_disjoint(set, set_i);
1465         }
1466
1467         isl_pw_aff_free(pwaff);
1468
1469         return set;
1470 }
1471
1472 /* Return a set containing those elements in the domain
1473  * of pwaff where it is zero.
1474  */
1475 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
1476 {
1477         int i;
1478         isl_set *set;
1479
1480         if (!pwaff)
1481                 return NULL;
1482
1483         set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1484
1485         for (i = 0; i < pwaff->n; ++i) {
1486                 isl_basic_set *bset;
1487                 isl_set *set_i;
1488
1489                 bset = isl_aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff));
1490                 set_i = isl_set_from_basic_set(bset);
1491                 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1492                 set = isl_set_union_disjoint(set, set_i);
1493         }
1494
1495         isl_pw_aff_free(pwaff);
1496
1497         return set;
1498 }
1499
1500 /* Return a set containing those elements in the domain
1501  * of pwaff where it is not zero.
1502  */
1503 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
1504 {
1505         return isl_set_complement(isl_pw_aff_zero_set(pwaff));
1506 }
1507
1508 /* Return a set containing those elements in the shared domain
1509  * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1510  *
1511  * We compute the difference on the shared domain and then construct
1512  * the set of values where this difference is non-negative.
1513  * If strict is set, we first subtract 1 from the difference.
1514  * If equal is set, we only return the elements where pwaff1 and pwaff2
1515  * are equal.
1516  */
1517 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
1518         __isl_take isl_pw_aff *pwaff2, int strict, int equal)
1519 {
1520         isl_set *set1, *set2;
1521
1522         set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
1523         set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
1524         set1 = isl_set_intersect(set1, set2);
1525         pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
1526         pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
1527         pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
1528
1529         if (strict) {
1530                 isl_space *dim = isl_set_get_space(set1);
1531                 isl_aff *aff;
1532                 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1533                 aff = isl_aff_add_constant_si(aff, -1);
1534                 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
1535         } else
1536                 isl_set_free(set1);
1537
1538         if (equal)
1539                 return isl_pw_aff_zero_set(pwaff1);
1540         return isl_pw_aff_nonneg_set(pwaff1);
1541 }
1542
1543 /* Return a set containing those elements in the shared domain
1544  * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1545  */
1546 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1547         __isl_take isl_pw_aff *pwaff2)
1548 {
1549         return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
1550 }
1551
1552 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1553         __isl_take isl_pw_aff *pwaff2)
1554 {
1555         return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
1556 }
1557
1558 /* Return a set containing those elements in the shared domain
1559  * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1560  */
1561 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1562         __isl_take isl_pw_aff *pwaff2)
1563 {
1564         return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
1565 }
1566
1567 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1568         __isl_take isl_pw_aff *pwaff2)
1569 {
1570         return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
1571 }
1572
1573 /* Return a set containing those elements in the shared domain
1574  * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1575  */
1576 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1577         __isl_take isl_pw_aff *pwaff2)
1578 {
1579         return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
1580 }
1581
1582 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1583         __isl_take isl_pw_aff *pwaff2)
1584 {
1585         return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
1586 }
1587
1588 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
1589         __isl_take isl_pw_aff *pwaff2)
1590 {
1591         return isl_pw_aff_ge_set(pwaff2, pwaff1);
1592 }
1593
1594 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
1595         __isl_take isl_pw_aff *pwaff2)
1596 {
1597         return isl_pw_aff_gt_set(pwaff2, pwaff1);
1598 }
1599
1600 /* Return a set containing those elements in the shared domain
1601  * of the elements of list1 and list2 where each element in list1
1602  * has the relation specified by "fn" with each element in list2.
1603  */
1604 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
1605         __isl_take isl_pw_aff_list *list2,
1606         __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1607                                     __isl_take isl_pw_aff *pwaff2))
1608 {
1609         int i, j;
1610         isl_ctx *ctx;
1611         isl_set *set;
1612
1613         if (!list1 || !list2)
1614                 goto error;
1615
1616         ctx = isl_pw_aff_list_get_ctx(list1);
1617         if (list1->n < 1 || list2->n < 1)
1618                 isl_die(ctx, isl_error_invalid,
1619                         "list should contain at least one element", goto error);
1620
1621         set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
1622         for (i = 0; i < list1->n; ++i)
1623                 for (j = 0; j < list2->n; ++j) {
1624                         isl_set *set_ij;
1625
1626                         set_ij = fn(isl_pw_aff_copy(list1->p[i]),
1627                                     isl_pw_aff_copy(list2->p[j]));
1628                         set = isl_set_intersect(set, set_ij);
1629                 }
1630
1631         isl_pw_aff_list_free(list1);
1632         isl_pw_aff_list_free(list2);
1633         return set;
1634 error:
1635         isl_pw_aff_list_free(list1);
1636         isl_pw_aff_list_free(list2);
1637         return NULL;
1638 }
1639
1640 /* Return a set containing those elements in the shared domain
1641  * of the elements of list1 and list2 where each element in list1
1642  * is equal to each element in list2.
1643  */
1644 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
1645         __isl_take isl_pw_aff_list *list2)
1646 {
1647         return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
1648 }
1649
1650 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
1651         __isl_take isl_pw_aff_list *list2)
1652 {
1653         return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
1654 }
1655
1656 /* Return a set containing those elements in the shared domain
1657  * of the elements of list1 and list2 where each element in list1
1658  * is less than or equal to each element in list2.
1659  */
1660 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
1661         __isl_take isl_pw_aff_list *list2)
1662 {
1663         return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
1664 }
1665
1666 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
1667         __isl_take isl_pw_aff_list *list2)
1668 {
1669         return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
1670 }
1671
1672 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
1673         __isl_take isl_pw_aff_list *list2)
1674 {
1675         return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
1676 }
1677
1678 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
1679         __isl_take isl_pw_aff_list *list2)
1680 {
1681         return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
1682 }
1683
1684
1685 /* Return a set containing those elements in the shared domain
1686  * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1687  */
1688 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1689         __isl_take isl_pw_aff *pwaff2)
1690 {
1691         isl_set *set_lt, *set_gt;
1692
1693         set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
1694                                    isl_pw_aff_copy(pwaff2));
1695         set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
1696         return isl_set_union_disjoint(set_lt, set_gt);
1697 }
1698
1699 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1700         __isl_take isl_pw_aff *pwaff2)
1701 {
1702         return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
1703 }
1704
1705 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
1706         isl_int v)
1707 {
1708         int i;
1709
1710         if (isl_int_is_one(v))
1711                 return pwaff;
1712         if (!isl_int_is_pos(v))
1713                 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1714                         "factor needs to be positive",
1715                         return isl_pw_aff_free(pwaff));
1716         pwaff = isl_pw_aff_cow(pwaff);
1717         if (!pwaff)
1718                 return NULL;
1719         if (pwaff->n == 0)
1720                 return pwaff;
1721
1722         for (i = 0; i < pwaff->n; ++i) {
1723                 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
1724                 if (!pwaff->p[i].aff)
1725                         return isl_pw_aff_free(pwaff);
1726         }
1727
1728         return pwaff;
1729 }
1730
1731 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
1732 {
1733         int i;
1734
1735         pwaff = isl_pw_aff_cow(pwaff);
1736         if (!pwaff)
1737                 return NULL;
1738         if (pwaff->n == 0)
1739                 return pwaff;
1740
1741         for (i = 0; i < pwaff->n; ++i) {
1742                 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
1743                 if (!pwaff->p[i].aff)
1744                         return isl_pw_aff_free(pwaff);
1745         }
1746
1747         return pwaff;
1748 }
1749
1750 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
1751 {
1752         int i;
1753
1754         pwaff = isl_pw_aff_cow(pwaff);
1755         if (!pwaff)
1756                 return NULL;
1757         if (pwaff->n == 0)
1758                 return pwaff;
1759
1760         for (i = 0; i < pwaff->n; ++i) {
1761                 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
1762                 if (!pwaff->p[i].aff)
1763                         return isl_pw_aff_free(pwaff);
1764         }
1765
1766         return pwaff;
1767 }
1768
1769 /* Return an affine expression that is equal to pwaff_true for elements
1770  * in "cond" and to pwaff_false for elements not in "cond".
1771  * That is, return cond ? pwaff_true : pwaff_false;
1772  */
1773 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_set *cond,
1774         __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
1775 {
1776         isl_set *comp;
1777
1778         comp = isl_set_complement(isl_set_copy(cond));
1779         pwaff_true = isl_pw_aff_intersect_domain(pwaff_true, cond);
1780         pwaff_false = isl_pw_aff_intersect_domain(pwaff_false, comp);
1781
1782         return isl_pw_aff_add_disjoint(pwaff_true, pwaff_false);
1783 }
1784
1785 int isl_aff_is_cst(__isl_keep isl_aff *aff)
1786 {
1787         if (!aff)
1788                 return -1;
1789
1790         return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
1791 }
1792
1793 /* Check whether pwaff is a piecewise constant.
1794  */
1795 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
1796 {
1797         int i;
1798
1799         if (!pwaff)
1800                 return -1;
1801
1802         for (i = 0; i < pwaff->n; ++i) {
1803                 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
1804                 if (is_cst < 0 || !is_cst)
1805                         return is_cst;
1806         }
1807
1808         return 1;
1809 }
1810
1811 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
1812         __isl_take isl_aff *aff2)
1813 {
1814         if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
1815                 return isl_aff_mul(aff2, aff1);
1816
1817         if (!isl_aff_is_cst(aff2))
1818                 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
1819                         "at least one affine expression should be constant",
1820                         goto error);
1821
1822         aff1 = isl_aff_cow(aff1);
1823         if (!aff1 || !aff2)
1824                 goto error;
1825
1826         aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
1827         aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
1828
1829         isl_aff_free(aff2);
1830         return aff1;
1831 error:
1832         isl_aff_free(aff1);
1833         isl_aff_free(aff2);
1834         return NULL;
1835 }
1836
1837 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
1838         __isl_take isl_pw_aff *pwaff2)
1839 {
1840         return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
1841 }
1842
1843 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
1844         __isl_take isl_pw_aff *pwaff2)
1845 {
1846         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
1847 }
1848
1849 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
1850         __isl_take isl_pw_aff *pwaff2)
1851 {
1852         return isl_pw_aff_union_add_(pwaff1, pwaff2);
1853 }
1854
1855 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1856         __isl_take isl_pw_aff *pwaff2)
1857 {
1858         return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
1859 }
1860
1861 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1862         __isl_take isl_pw_aff *pwaff2)
1863 {
1864         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
1865 }
1866
1867 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1868         __isl_take isl_pw_aff *pwaff2)
1869 {
1870         isl_set *le;
1871
1872         le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
1873                                 isl_pw_aff_copy(pwaff2));
1874         return isl_pw_aff_cond(le, pwaff1, pwaff2);
1875 }
1876
1877 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1878         __isl_take isl_pw_aff *pwaff2)
1879 {
1880         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
1881 }
1882
1883 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1884         __isl_take isl_pw_aff *pwaff2)
1885 {
1886         isl_set *le;
1887
1888         le = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
1889                                 isl_pw_aff_copy(pwaff2));
1890         return isl_pw_aff_cond(le, pwaff1, pwaff2);
1891 }
1892
1893 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1894         __isl_take isl_pw_aff *pwaff2)
1895 {
1896         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
1897 }
1898
1899 static __isl_give isl_pw_aff *pw_aff_list_reduce(
1900         __isl_take isl_pw_aff_list *list,
1901         __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
1902                                         __isl_take isl_pw_aff *pwaff2))
1903 {
1904         int i;
1905         isl_ctx *ctx;
1906         isl_pw_aff *res;
1907
1908         if (!list)
1909                 return NULL;
1910
1911         ctx = isl_pw_aff_list_get_ctx(list);
1912         if (list->n < 1)
1913                 isl_die(ctx, isl_error_invalid,
1914                         "list should contain at least one element",
1915                         return isl_pw_aff_list_free(list));
1916
1917         res = isl_pw_aff_copy(list->p[0]);
1918         for (i = 1; i < list->n; ++i)
1919                 res = fn(res, isl_pw_aff_copy(list->p[i]));
1920
1921         isl_pw_aff_list_free(list);
1922         return res;
1923 }
1924
1925 /* Return an isl_pw_aff that maps each element in the intersection of the
1926  * domains of the elements of list to the minimal corresponding affine
1927  * expression.
1928  */
1929 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
1930 {
1931         return pw_aff_list_reduce(list, &isl_pw_aff_min);
1932 }
1933
1934 /* Return an isl_pw_aff that maps each element in the intersection of the
1935  * domains of the elements of list to the maximal corresponding affine
1936  * expression.
1937  */
1938 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
1939 {
1940         return pw_aff_list_reduce(list, &isl_pw_aff_max);
1941 }
1942
1943 #undef BASE
1944 #define BASE aff
1945
1946 #include <isl_multi_templ.c>
1947
1948 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
1949         __isl_take isl_multi_aff *maff2)
1950 {
1951         int i;
1952         isl_ctx *ctx;
1953
1954         maff1 = isl_multi_aff_cow(maff1);
1955         if (!maff1 || !maff2)
1956                 goto error;
1957
1958         ctx = isl_multi_aff_get_ctx(maff1);
1959         if (!isl_space_is_equal(maff1->space, maff2->space))
1960                 isl_die(ctx, isl_error_invalid,
1961                         "spaces don't match", goto error);
1962
1963         for (i = 0; i < maff1->n; ++i) {
1964                 maff1->p[i] = isl_aff_add(maff1->p[i],
1965                                             isl_aff_copy(maff2->p[i]));
1966                 if (!maff1->p[i])
1967                         goto error;
1968         }
1969
1970         isl_multi_aff_free(maff2);
1971         return maff1;
1972 error:
1973         isl_multi_aff_free(maff1);
1974         isl_multi_aff_free(maff2);
1975         return NULL;
1976 }
1977
1978 /* Exploit the equalities in "eq" to simplify the affine expressions.
1979  */
1980 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
1981         __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
1982 {
1983         int i;
1984
1985         maff = isl_multi_aff_cow(maff);
1986         if (!maff || !eq)
1987                 goto error;
1988
1989         for (i = 0; i < maff->n; ++i) {
1990                 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
1991                                                     isl_basic_set_copy(eq));
1992                 if (!maff->p[i])
1993                         goto error;
1994         }
1995
1996         isl_basic_set_free(eq);
1997         return maff;
1998 error:
1999         isl_basic_set_free(eq);
2000         isl_multi_aff_free(maff);
2001         return NULL;
2002 }
2003
2004 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
2005         isl_int f)
2006 {
2007         int i;
2008
2009         maff = isl_multi_aff_cow(maff);
2010         if (!maff)
2011                 return NULL;
2012
2013         for (i = 0; i < maff->n; ++i) {
2014                 maff->p[i] = isl_aff_scale(maff->p[i], f);
2015                 if (!maff->p[i])
2016                         return isl_multi_aff_free(maff);
2017         }
2018
2019         return maff;
2020 }
2021
2022 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
2023         __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
2024 {
2025         maff1 = isl_multi_aff_add(maff1, maff2);
2026         maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
2027         return maff1;
2028 }
2029
2030 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
2031 {
2032         if (!maff)
2033                 return -1;
2034
2035         return 0;
2036 }
2037
2038 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
2039         __isl_keep isl_multi_aff *maff2)
2040 {
2041         int i;
2042         int equal;
2043
2044         if (!maff1 || !maff2)
2045                 return -1;
2046         if (maff1->n != maff2->n)
2047                 return 0;
2048         equal = isl_space_is_equal(maff1->space, maff2->space);
2049         if (equal < 0 || !equal)
2050                 return equal;
2051
2052         for (i = 0; i < maff1->n; ++i) {
2053                 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
2054                 if (equal < 0 || !equal)
2055                         return equal;
2056         }
2057
2058         return 1;
2059 }
2060
2061 __isl_give isl_multi_aff *isl_multi_aff_set_dim_name(
2062         __isl_take isl_multi_aff *maff,
2063         enum isl_dim_type type, unsigned pos, const char *s)
2064 {
2065         int i;
2066
2067         maff = isl_multi_aff_cow(maff);
2068         if (!maff)
2069                 return NULL;
2070
2071         maff->space = isl_space_set_dim_name(maff->space, type, pos, s);
2072         if (!maff->space)
2073                 return isl_multi_aff_free(maff);
2074         for (i = 0; i < maff->n; ++i) {
2075                 maff->p[i] = isl_aff_set_dim_name(maff->p[i], type, pos, s);
2076                 if (!maff->p[i])
2077                         return isl_multi_aff_free(maff);
2078         }
2079
2080         return maff;
2081 }
2082
2083 __isl_give isl_multi_aff *isl_multi_aff_drop_dims(__isl_take isl_multi_aff *maff,
2084         enum isl_dim_type type, unsigned first, unsigned n)
2085 {
2086         int i;
2087
2088         maff = isl_multi_aff_cow(maff);
2089         if (!maff)
2090                 return NULL;
2091
2092         maff->space = isl_space_drop_dims(maff->space, type, first, n);
2093         if (!maff->space)
2094                 return isl_multi_aff_free(maff);
2095         for (i = 0; i < maff->n; ++i) {
2096                 maff->p[i] = isl_aff_drop_dims(maff->p[i], type, first, n);
2097                 if (!maff->p[i])
2098                         return isl_multi_aff_free(maff);
2099         }
2100
2101         return maff;
2102 }
2103
2104 #undef PW
2105 #define PW isl_pw_multi_aff
2106 #undef EL
2107 #define EL isl_multi_aff
2108 #undef EL_IS_ZERO
2109 #define EL_IS_ZERO is_empty
2110 #undef ZERO
2111 #define ZERO empty
2112 #undef IS_ZERO
2113 #define IS_ZERO is_empty
2114 #undef FIELD
2115 #define FIELD maff
2116 #undef DEFAULT_IS_ZERO
2117 #define DEFAULT_IS_ZERO 0
2118
2119 #define NO_NEG
2120 #define NO_EVAL
2121 #define NO_OPT
2122 #define NO_INVOLVES_DIMS
2123 #define NO_MOVE_DIMS
2124 #define NO_INSERT_DIMS
2125 #define NO_LIFT
2126 #define NO_MORPH
2127
2128 #include <isl_pw_templ.c>
2129
2130 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
2131         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2132 {
2133         return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
2134                                                 &isl_multi_aff_add);
2135 }
2136
2137 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
2138         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2139 {
2140         return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
2141                                                 &pw_multi_aff_add);
2142 }
2143
2144 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
2145         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2146 {
2147         return isl_pw_multi_aff_union_add_(pma1, pma2);
2148 }
2149
2150 /* Construct a map mapping the domain the piecewise multi-affine expression
2151  * to its range, with each dimension in the range equated to the
2152  * corresponding affine expression on its cell.
2153  */
2154 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2155 {
2156         int i;
2157         isl_map *map;
2158
2159         if (!pma)
2160                 return NULL;
2161
2162         map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
2163
2164         for (i = 0; i < pma->n; ++i) {
2165                 isl_multi_aff *maff;
2166                 isl_basic_map *bmap;
2167                 isl_map *map_i;
2168
2169                 maff = isl_multi_aff_copy(pma->p[i].maff);
2170                 bmap = isl_basic_map_from_multi_aff(maff);
2171                 map_i = isl_map_from_basic_map(bmap);
2172                 map_i = isl_map_intersect_domain(map_i,
2173                                                 isl_set_copy(pma->p[i].set));
2174                 map = isl_map_union_disjoint(map, map_i);
2175         }
2176
2177         isl_pw_multi_aff_free(pma);
2178         return map;
2179 }
2180
2181 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2182 {
2183         if (!isl_space_is_set(pma->dim))
2184                 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
2185                         "isl_pw_multi_aff cannot be converted into an isl_set",
2186                         return isl_pw_multi_aff_free(pma));
2187
2188         return isl_map_from_pw_multi_aff(pma);
2189 }
2190
2191 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
2192  * This obivously only works if the input "map" is single-valued.
2193  * If so, we compute the lexicographic minimum of the image in the form
2194  * of an isl_pw_multi_aff.  Since the image is unique, it is equal
2195  * to its lexicographic minimum.
2196  * If the input is not single-valued, we produce an error.
2197  */
2198 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
2199 {
2200         int i;
2201         int sv;
2202         isl_pw_multi_aff *pma;
2203
2204         if (!map)
2205                 return NULL;
2206
2207         sv = isl_map_is_single_valued(map);
2208         if (sv < 0)
2209                 goto error;
2210         if (!sv)
2211                 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2212                         "map is not single-valued", goto error);
2213         map = isl_map_make_disjoint(map);
2214         if (!map)
2215                 return NULL;
2216
2217         pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
2218
2219         for (i = 0; i < map->n; ++i) {
2220                 isl_pw_multi_aff *pma_i;
2221                 isl_basic_map *bmap;
2222                 bmap = isl_basic_map_copy(map->p[i]);
2223                 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
2224                 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
2225         }
2226
2227         isl_map_free(map);
2228         return pma;
2229 error:
2230         isl_map_free(map);
2231         return NULL;
2232 }
2233
2234 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
2235 {
2236         return isl_pw_multi_aff_from_map(set);
2237 }
2238
2239 /* Plug in "subs" for dimension "type", "pos" of "aff".
2240  *
2241  * Let i be the dimension to replace and let "subs" be of the form
2242  *
2243  *      f/d
2244  *
2245  * and "aff" of the form
2246  *
2247  *      (a i + g)/m
2248  *
2249  * The result is
2250  *
2251  *      floor((a f + d g')/(m d))
2252  *
2253  * where g' is the result of plugging in "subs" in each of the integer
2254  * divisions in g.
2255  */
2256 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
2257         enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
2258 {
2259         isl_ctx *ctx;
2260         isl_int v;
2261
2262         aff = isl_aff_cow(aff);
2263         if (!aff || !subs)
2264                 return isl_aff_free(aff);
2265
2266         ctx = isl_aff_get_ctx(aff);
2267         if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
2268                 isl_die(ctx, isl_error_invalid,
2269                         "spaces don't match", return isl_aff_free(aff));
2270         if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
2271                 isl_die(ctx, isl_error_unsupported,
2272                         "cannot handle divs yet", return isl_aff_free(aff));
2273
2274         aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
2275         if (!aff->ls)
2276                 return isl_aff_free(aff);
2277
2278         aff->v = isl_vec_cow(aff->v);
2279         if (!aff->v)
2280                 return isl_aff_free(aff);
2281
2282         pos += isl_local_space_offset(aff->ls, type);
2283
2284         isl_int_init(v);
2285         isl_int_set(v, aff->v->el[1 + pos]);
2286         isl_int_set_si(aff->v->el[1 + pos], 0);
2287         isl_seq_combine(aff->v->el + 1, subs->v->el[0], aff->v->el + 1,
2288                         v, subs->v->el + 1, subs->v->size - 1);
2289         isl_int_mul(aff->v->el[0], aff->v->el[0], subs->v->el[0]);
2290         isl_int_clear(v);
2291
2292         return aff;
2293 }
2294
2295 /* Plug in "subs" for dimension "type", "pos" in each of the affine
2296  * expressions in "maff".
2297  */
2298 __isl_give isl_multi_aff *isl_multi_aff_substitute(
2299         __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
2300         __isl_keep isl_aff *subs)
2301 {
2302         int i;
2303
2304         maff = isl_multi_aff_cow(maff);
2305         if (!maff || !subs)
2306                 return isl_multi_aff_free(maff);
2307
2308         if (type == isl_dim_in)
2309                 type = isl_dim_set;
2310
2311         for (i = 0; i < maff->n; ++i) {
2312                 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
2313                 if (!maff->p[i])
2314                         return isl_multi_aff_free(maff);
2315         }
2316
2317         return maff;
2318 }
2319
2320 /* Plug in "subs" for dimension "type", "pos" of "pma".
2321  *
2322  * pma is of the form
2323  *
2324  *      A_i(v) -> M_i(v)
2325  *
2326  * while subs is of the form
2327  *
2328  *      v' = B_j(v) -> S_j
2329  *
2330  * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
2331  * has a contribution in the result, in particular
2332  *
2333  *      C_ij(S_j) -> M_i(S_j)
2334  *
2335  * Note that plugging in S_j in C_ij may also result in an empty set
2336  * and this contribution should simply be discarded.
2337  */
2338 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
2339         __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
2340         __isl_keep isl_pw_aff *subs)
2341 {
2342         int i, j, n;
2343         isl_pw_multi_aff *res;
2344
2345         if (!pma || !subs)
2346                 return isl_pw_multi_aff_free(pma);
2347
2348         n = pma->n * subs->n;
2349         res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
2350
2351         for (i = 0; i < pma->n; ++i) {
2352                 for (j = 0; j < subs->n; ++j) {
2353                         isl_set *common;
2354                         isl_multi_aff *res_ij;
2355                         common = isl_set_intersect(
2356                                         isl_set_copy(pma->p[i].set),
2357                                         isl_set_copy(subs->p[j].set));
2358                         common = isl_set_substitute(common,
2359                                         type, pos, subs->p[j].aff);
2360                         if (isl_set_plain_is_empty(common)) {
2361                                 isl_set_free(common);
2362                                 continue;
2363                         }
2364
2365                         res_ij = isl_multi_aff_substitute(
2366                                         isl_multi_aff_copy(pma->p[i].maff),
2367                                         type, pos, subs->p[j].aff);
2368
2369                         res = isl_pw_multi_aff_add_piece(res, common, res_ij);
2370                 }
2371         }
2372
2373         isl_pw_multi_aff_free(pma);
2374         return res;
2375 }
2376
2377 /* Extend the local space of "dst" to include the divs
2378  * in the local space of "src".
2379  */
2380 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
2381         __isl_keep isl_aff *src)
2382 {
2383         isl_ctx *ctx;
2384         int *exp1 = NULL;
2385         int *exp2 = NULL;
2386         isl_mat *div;
2387
2388         if (!src || !dst)
2389                 return isl_aff_free(dst);
2390
2391         ctx = isl_aff_get_ctx(src);
2392         if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
2393                 isl_die(ctx, isl_error_invalid,
2394                         "spaces don't match", goto error);
2395
2396         if (src->ls->div->n_row == 0)
2397                 return dst;
2398
2399         exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
2400         exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
2401         if (!exp1 || !exp2)
2402                 goto error;
2403
2404         div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
2405         dst = isl_aff_expand_divs(dst, div, exp2);
2406         free(exp1);
2407         free(exp2);
2408
2409         return dst;
2410 error:
2411         free(exp1);
2412         free(exp2);
2413         return isl_aff_free(dst);
2414 }
2415
2416 /* Adjust the local spaces of the affine expressions in "maff"
2417  * such that they all have the save divs.
2418  */
2419 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
2420         __isl_take isl_multi_aff *maff)
2421 {
2422         int i;
2423
2424         if (!maff)
2425                 return NULL;
2426         if (maff->n == 0)
2427                 return maff;
2428         maff = isl_multi_aff_cow(maff);
2429         if (!maff)
2430                 return NULL;
2431
2432         for (i = 1; i < maff->n; ++i)
2433                 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
2434         for (i = 1; i < maff->n; ++i) {
2435                 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
2436                 if (!maff->p[i])
2437                         return isl_multi_aff_free(maff);
2438         }
2439
2440         return maff;
2441 }
2442
2443 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
2444 {
2445         aff = isl_aff_cow(aff);
2446         if (!aff)
2447                 return NULL;
2448
2449         aff->ls = isl_local_space_lift(aff->ls);
2450         if (!aff->ls)
2451                 return isl_aff_free(aff);
2452
2453         return aff;
2454 }
2455
2456 /* Lift "maff" to a space with extra dimensions such that the result
2457  * has no more existentially quantified variables.
2458  * If "ls" is not NULL, then *ls is assigned the local space that lies
2459  * at the basis of the lifting applied to "maff".
2460  */
2461 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
2462         __isl_give isl_local_space **ls)
2463 {
2464         int i;
2465         isl_space *space;
2466         unsigned n_div;
2467
2468         if (ls)
2469                 *ls = NULL;
2470
2471         if (!maff)
2472                 return NULL;
2473
2474         if (maff->n == 0) {
2475                 if (ls) {
2476                         isl_space *space = isl_multi_aff_get_domain_space(maff);
2477                         *ls = isl_local_space_from_space(space);
2478                         if (!*ls)
2479                                 return isl_multi_aff_free(maff);
2480                 }
2481                 return maff;
2482         }
2483
2484         maff = isl_multi_aff_cow(maff);
2485         maff = isl_multi_aff_align_divs(maff);
2486         if (!maff)
2487                 return NULL;
2488
2489         n_div = isl_aff_dim(maff->p[0], isl_dim_div);
2490         space = isl_multi_aff_get_space(maff);
2491         space = isl_space_lift(isl_space_domain(space), n_div);
2492         space = isl_space_extend_domain_with_range(space,
2493                                                 isl_multi_aff_get_space(maff));
2494         if (!space)
2495                 return isl_multi_aff_free(maff);
2496         isl_space_free(maff->space);
2497         maff->space = space;
2498
2499         if (ls) {
2500                 *ls = isl_aff_get_domain_local_space(maff->p[0]);
2501                 if (!*ls)
2502                         return isl_multi_aff_free(maff);
2503         }
2504
2505         for (i = 0; i < maff->n; ++i) {
2506                 maff->p[i] = isl_aff_lift(maff->p[i]);
2507                 if (!maff->p[i])
2508                         goto error;
2509         }
2510
2511         return maff;
2512 error:
2513         if (ls)
2514                 isl_local_space_free(*ls);
2515         return isl_multi_aff_free(maff);
2516 }