isl_pw_aff_non_zero_set: don't assume isl_pw_aff is total
[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         isl_basic_set *bset;
1015
1016         ineq = isl_inequality_from_aff(aff);
1017
1018         bset = isl_basic_set_from_constraint(ineq);
1019         bset = isl_basic_set_simplify(bset);
1020         return bset;
1021 }
1022
1023 /* Return a basic set containing those elements in the space
1024  * of aff where it is zero.
1025  */
1026 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1027 {
1028         isl_constraint *ineq;
1029         isl_basic_set *bset;
1030
1031         ineq = isl_equality_from_aff(aff);
1032
1033         bset = isl_basic_set_from_constraint(ineq);
1034         bset = isl_basic_set_simplify(bset);
1035         return bset;
1036 }
1037
1038 /* Return a basic set containing those elements in the shared space
1039  * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1040  */
1041 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1042         __isl_take isl_aff *aff2)
1043 {
1044         aff1 = isl_aff_sub(aff1, aff2);
1045
1046         return isl_aff_nonneg_basic_set(aff1);
1047 }
1048
1049 /* Return a basic set containing those elements in the shared space
1050  * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1051  */
1052 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1053         __isl_take isl_aff *aff2)
1054 {
1055         return isl_aff_ge_basic_set(aff2, aff1);
1056 }
1057
1058 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1059         __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1060 {
1061         aff1 = isl_aff_add(aff1, aff2);
1062         aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1063         return aff1;
1064 }
1065
1066 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1067 {
1068         if (!aff)
1069                 return -1;
1070
1071         return 0;
1072 }
1073
1074 /* Check whether the given affine expression has non-zero coefficient
1075  * for any dimension in the given range or if any of these dimensions
1076  * appear with non-zero coefficients in any of the integer divisions
1077  * involved in the affine expression.
1078  */
1079 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1080         enum isl_dim_type type, unsigned first, unsigned n)
1081 {
1082         int i;
1083         isl_ctx *ctx;
1084         int *active = NULL;
1085         int involves = 0;
1086
1087         if (!aff)
1088                 return -1;
1089         if (n == 0)
1090                 return 0;
1091
1092         ctx = isl_aff_get_ctx(aff);
1093         if (first + n > isl_aff_dim(aff, type))
1094                 isl_die(ctx, isl_error_invalid,
1095                         "range out of bounds", return -1);
1096
1097         active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1098         if (!active)
1099                 goto error;
1100
1101         first += isl_local_space_offset(aff->ls, type) - 1;
1102         for (i = 0; i < n; ++i)
1103                 if (active[first + i]) {
1104                         involves = 1;
1105                         break;
1106                 }
1107
1108         free(active);
1109
1110         return involves;
1111 error:
1112         free(active);
1113         return -1;
1114 }
1115
1116 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1117         enum isl_dim_type type, unsigned first, unsigned n)
1118 {
1119         isl_ctx *ctx;
1120
1121         if (!aff)
1122                 return NULL;
1123         if (type == isl_dim_out)
1124                 isl_die(aff->v->ctx, isl_error_invalid,
1125                         "cannot drop output/set dimension",
1126                         return isl_aff_free(aff));
1127         if (type == isl_dim_in)
1128                 type = isl_dim_set;
1129         if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1130                 return aff;
1131
1132         ctx = isl_aff_get_ctx(aff);
1133         if (first + n > isl_local_space_dim(aff->ls, type))
1134                 isl_die(ctx, isl_error_invalid, "range out of bounds",
1135                         return isl_aff_free(aff));
1136
1137         aff = isl_aff_cow(aff);
1138         if (!aff)
1139                 return NULL;
1140
1141         aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1142         if (!aff->ls)
1143                 return isl_aff_free(aff);
1144
1145         first += 1 + isl_local_space_offset(aff->ls, type);
1146         aff->v = isl_vec_drop_els(aff->v, first, n);
1147         if (!aff->v)
1148                 return isl_aff_free(aff);
1149
1150         return aff;
1151 }
1152
1153 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1154         enum isl_dim_type type, unsigned first, unsigned n)
1155 {
1156         isl_ctx *ctx;
1157
1158         if (!aff)
1159                 return NULL;
1160         if (type == isl_dim_out)
1161                 isl_die(aff->v->ctx, isl_error_invalid,
1162                         "cannot insert output/set dimensions",
1163                         return isl_aff_free(aff));
1164         if (type == isl_dim_in)
1165                 type = isl_dim_set;
1166         if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1167                 return aff;
1168
1169         ctx = isl_aff_get_ctx(aff);
1170         if (first > isl_local_space_dim(aff->ls, type))
1171                 isl_die(ctx, isl_error_invalid, "position out of bounds",
1172                         return isl_aff_free(aff));
1173
1174         aff = isl_aff_cow(aff);
1175         if (!aff)
1176                 return NULL;
1177
1178         aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1179         if (!aff->ls)
1180                 return isl_aff_free(aff);
1181
1182         first += 1 + isl_local_space_offset(aff->ls, type);
1183         aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1184         if (!aff->v)
1185                 return isl_aff_free(aff);
1186
1187         return aff;
1188 }
1189
1190 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1191         enum isl_dim_type type, unsigned n)
1192 {
1193         unsigned pos;
1194
1195         pos = isl_aff_dim(aff, type);
1196
1197         return isl_aff_insert_dims(aff, type, pos, n);
1198 }
1199
1200 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1201         enum isl_dim_type type, unsigned n)
1202 {
1203         unsigned pos;
1204
1205         pos = isl_pw_aff_dim(pwaff, type);
1206
1207         return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1208 }
1209
1210 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1211 {
1212         isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
1213         return isl_pw_aff_alloc(dom, aff);
1214 }
1215
1216 #undef PW
1217 #define PW isl_pw_aff
1218 #undef EL
1219 #define EL isl_aff
1220 #undef EL_IS_ZERO
1221 #define EL_IS_ZERO is_empty
1222 #undef ZERO
1223 #define ZERO empty
1224 #undef IS_ZERO
1225 #define IS_ZERO is_empty
1226 #undef FIELD
1227 #define FIELD aff
1228 #undef DEFAULT_IS_ZERO
1229 #define DEFAULT_IS_ZERO 0
1230
1231 #define NO_EVAL
1232 #define NO_OPT
1233 #define NO_MOVE_DIMS
1234 #define NO_LIFT
1235 #define NO_MORPH
1236
1237 #include <isl_pw_templ.c>
1238
1239 static __isl_give isl_set *align_params_pw_pw_set_and(
1240         __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1241         __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1242                                     __isl_take isl_pw_aff *pwaff2))
1243 {
1244         if (!pwaff1 || !pwaff2)
1245                 goto error;
1246         if (isl_space_match(pwaff1->dim, isl_dim_param,
1247                           pwaff2->dim, isl_dim_param))
1248                 return fn(pwaff1, pwaff2);
1249         if (!isl_space_has_named_params(pwaff1->dim) ||
1250             !isl_space_has_named_params(pwaff2->dim))
1251                 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1252                         "unaligned unnamed parameters", goto error);
1253         pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
1254         pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
1255         return fn(pwaff1, pwaff2);
1256 error:
1257         isl_pw_aff_free(pwaff1);
1258         isl_pw_aff_free(pwaff2);
1259         return NULL;
1260 }
1261
1262 /* Compute a piecewise quasi-affine expression with a domain that
1263  * is the union of those of pwaff1 and pwaff2 and such that on each
1264  * cell, the quasi-affine expression is the better (according to cmp)
1265  * of those of pwaff1 and pwaff2.  If only one of pwaff1 or pwaff2
1266  * is defined on a given cell, then the associated expression
1267  * is the defined one.
1268  */
1269 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1270         __isl_take isl_pw_aff *pwaff2,
1271         __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
1272                                         __isl_take isl_aff *aff2))
1273 {
1274         int i, j, n;
1275         isl_pw_aff *res;
1276         isl_ctx *ctx;
1277         isl_set *set;
1278
1279         if (!pwaff1 || !pwaff2)
1280                 goto error;
1281
1282         ctx = isl_space_get_ctx(pwaff1->dim);
1283         if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
1284                 isl_die(ctx, isl_error_invalid,
1285                         "arguments should live in same space", goto error);
1286
1287         if (isl_pw_aff_is_empty(pwaff1)) {
1288                 isl_pw_aff_free(pwaff1);
1289                 return pwaff2;
1290         }
1291
1292         if (isl_pw_aff_is_empty(pwaff2)) {
1293                 isl_pw_aff_free(pwaff2);
1294                 return pwaff1;
1295         }
1296
1297         n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1298         res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
1299
1300         for (i = 0; i < pwaff1->n; ++i) {
1301                 set = isl_set_copy(pwaff1->p[i].set);
1302                 for (j = 0; j < pwaff2->n; ++j) {
1303                         struct isl_set *common;
1304                         isl_set *better;
1305
1306                         common = isl_set_intersect(
1307                                         isl_set_copy(pwaff1->p[i].set),
1308                                         isl_set_copy(pwaff2->p[j].set));
1309                         better = isl_set_from_basic_set(cmp(
1310                                         isl_aff_copy(pwaff2->p[j].aff),
1311                                         isl_aff_copy(pwaff1->p[i].aff)));
1312                         better = isl_set_intersect(common, better);
1313                         if (isl_set_plain_is_empty(better)) {
1314                                 isl_set_free(better);
1315                                 continue;
1316                         }
1317                         set = isl_set_subtract(set, isl_set_copy(better));
1318
1319                         res = isl_pw_aff_add_piece(res, better,
1320                                                 isl_aff_copy(pwaff2->p[j].aff));
1321                 }
1322                 res = isl_pw_aff_add_piece(res, set,
1323                                                 isl_aff_copy(pwaff1->p[i].aff));
1324         }
1325
1326         for (j = 0; j < pwaff2->n; ++j) {
1327                 set = isl_set_copy(pwaff2->p[j].set);
1328                 for (i = 0; i < pwaff1->n; ++i)
1329                         set = isl_set_subtract(set,
1330                                         isl_set_copy(pwaff1->p[i].set));
1331                 res = isl_pw_aff_add_piece(res, set,
1332                                                 isl_aff_copy(pwaff2->p[j].aff));
1333         }
1334
1335         isl_pw_aff_free(pwaff1);
1336         isl_pw_aff_free(pwaff2);
1337
1338         return res;
1339 error:
1340         isl_pw_aff_free(pwaff1);
1341         isl_pw_aff_free(pwaff2);
1342         return NULL;
1343 }
1344
1345 /* Compute a piecewise quasi-affine expression with a domain that
1346  * is the union of those of pwaff1 and pwaff2 and such that on each
1347  * cell, the quasi-affine expression is the maximum of those of pwaff1
1348  * and pwaff2.  If only one of pwaff1 or pwaff2 is defined on a given
1349  * cell, then the associated expression is the defined one.
1350  */
1351 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1352         __isl_take isl_pw_aff *pwaff2)
1353 {
1354         return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
1355 }
1356
1357 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1358         __isl_take isl_pw_aff *pwaff2)
1359 {
1360         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1361                                                         &pw_aff_union_max);
1362 }
1363
1364 /* Compute a piecewise quasi-affine expression with a domain that
1365  * is the union of those of pwaff1 and pwaff2 and such that on each
1366  * cell, the quasi-affine expression is the minimum of those of pwaff1
1367  * and pwaff2.  If only one of pwaff1 or pwaff2 is defined on a given
1368  * cell, then the associated expression is the defined one.
1369  */
1370 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1371         __isl_take isl_pw_aff *pwaff2)
1372 {
1373         return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
1374 }
1375
1376 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1377         __isl_take isl_pw_aff *pwaff2)
1378 {
1379         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1380                                                         &pw_aff_union_min);
1381 }
1382
1383 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1384         __isl_take isl_pw_aff *pwaff2, int max)
1385 {
1386         if (max)
1387                 return isl_pw_aff_union_max(pwaff1, pwaff2);
1388         else
1389                 return isl_pw_aff_union_min(pwaff1, pwaff2);
1390 }
1391
1392 /* Construct a map with as domain the domain of pwaff and
1393  * one-dimensional range corresponding to the affine expressions.
1394  */
1395 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1396 {
1397         int i;
1398         isl_space *dim;
1399         isl_map *map;
1400
1401         if (!pwaff)
1402                 return NULL;
1403
1404         dim = isl_pw_aff_get_space(pwaff);
1405         map = isl_map_empty(dim);
1406
1407         for (i = 0; i < pwaff->n; ++i) {
1408                 isl_basic_map *bmap;
1409                 isl_map *map_i;
1410
1411                 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1412                 map_i = isl_map_from_basic_map(bmap);
1413                 map_i = isl_map_intersect_domain(map_i,
1414                                                 isl_set_copy(pwaff->p[i].set));
1415                 map = isl_map_union_disjoint(map, map_i);
1416         }
1417
1418         isl_pw_aff_free(pwaff);
1419
1420         return map;
1421 }
1422
1423 /* Construct a map with as domain the domain of pwaff and
1424  * one-dimensional range corresponding to the affine expressions.
1425  */
1426 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1427 {
1428         if (!pwaff)
1429                 return NULL;
1430         if (isl_space_is_set(pwaff->dim))
1431                 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1432                         "space of input is not a map",
1433                         return isl_pw_aff_free(pwaff));
1434         return map_from_pw_aff(pwaff);
1435 }
1436
1437 /* Construct a one-dimensional set with as parameter domain
1438  * the domain of pwaff and the single set dimension
1439  * corresponding to the affine expressions.
1440  */
1441 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1442 {
1443         if (!pwaff)
1444                 return NULL;
1445         if (!isl_space_is_set(pwaff->dim))
1446                 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1447                         "space of input is not a set",
1448                         return isl_pw_aff_free(pwaff));
1449         return map_from_pw_aff(pwaff);
1450 }
1451
1452 /* Return a set containing those elements in the domain
1453  * of pwaff where it is non-negative.
1454  */
1455 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1456 {
1457         int i;
1458         isl_set *set;
1459
1460         if (!pwaff)
1461                 return NULL;
1462
1463         set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1464
1465         for (i = 0; i < pwaff->n; ++i) {
1466                 isl_basic_set *bset;
1467                 isl_set *set_i;
1468
1469                 bset = isl_aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff));
1470                 set_i = isl_set_from_basic_set(bset);
1471                 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1472                 set = isl_set_union_disjoint(set, set_i);
1473         }
1474
1475         isl_pw_aff_free(pwaff);
1476
1477         return set;
1478 }
1479
1480 /* Return a set containing those elements in the domain
1481  * of pwaff where it is zero (if complement is 0) or not zero
1482  * (if complement is 1).
1483  */
1484 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
1485         int complement)
1486 {
1487         int i;
1488         isl_set *set;
1489
1490         if (!pwaff)
1491                 return NULL;
1492
1493         set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1494
1495         for (i = 0; i < pwaff->n; ++i) {
1496                 isl_basic_set *bset;
1497                 isl_set *set_i, *zero;
1498
1499                 bset = isl_aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff));
1500                 zero = isl_set_from_basic_set(bset);
1501                 set_i = isl_set_copy(pwaff->p[i].set);
1502                 if (complement)
1503                         set_i = isl_set_subtract(set_i, zero);
1504                 else
1505                         set_i = isl_set_intersect(set_i, zero);
1506                 set = isl_set_union_disjoint(set, set_i);
1507         }
1508
1509         isl_pw_aff_free(pwaff);
1510
1511         return set;
1512 }
1513
1514 /* Return a set containing those elements in the domain
1515  * of pwaff where it is zero.
1516  */
1517 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
1518 {
1519         return pw_aff_zero_set(pwaff, 0);
1520 }
1521
1522 /* Return a set containing those elements in the domain
1523  * of pwaff where it is not zero.
1524  */
1525 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
1526 {
1527         return pw_aff_zero_set(pwaff, 1);
1528 }
1529
1530 /* Return a set containing those elements in the shared domain
1531  * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1532  *
1533  * We compute the difference on the shared domain and then construct
1534  * the set of values where this difference is non-negative.
1535  * If strict is set, we first subtract 1 from the difference.
1536  * If equal is set, we only return the elements where pwaff1 and pwaff2
1537  * are equal.
1538  */
1539 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
1540         __isl_take isl_pw_aff *pwaff2, int strict, int equal)
1541 {
1542         isl_set *set1, *set2;
1543
1544         set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
1545         set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
1546         set1 = isl_set_intersect(set1, set2);
1547         pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
1548         pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
1549         pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
1550
1551         if (strict) {
1552                 isl_space *dim = isl_set_get_space(set1);
1553                 isl_aff *aff;
1554                 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1555                 aff = isl_aff_add_constant_si(aff, -1);
1556                 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
1557         } else
1558                 isl_set_free(set1);
1559
1560         if (equal)
1561                 return isl_pw_aff_zero_set(pwaff1);
1562         return isl_pw_aff_nonneg_set(pwaff1);
1563 }
1564
1565 /* Return a set containing those elements in the shared domain
1566  * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1567  */
1568 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1569         __isl_take isl_pw_aff *pwaff2)
1570 {
1571         return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
1572 }
1573
1574 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1575         __isl_take isl_pw_aff *pwaff2)
1576 {
1577         return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
1578 }
1579
1580 /* Return a set containing those elements in the shared domain
1581  * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1582  */
1583 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1584         __isl_take isl_pw_aff *pwaff2)
1585 {
1586         return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
1587 }
1588
1589 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1590         __isl_take isl_pw_aff *pwaff2)
1591 {
1592         return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
1593 }
1594
1595 /* Return a set containing those elements in the shared domain
1596  * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1597  */
1598 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1599         __isl_take isl_pw_aff *pwaff2)
1600 {
1601         return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
1602 }
1603
1604 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1605         __isl_take isl_pw_aff *pwaff2)
1606 {
1607         return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
1608 }
1609
1610 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
1611         __isl_take isl_pw_aff *pwaff2)
1612 {
1613         return isl_pw_aff_ge_set(pwaff2, pwaff1);
1614 }
1615
1616 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
1617         __isl_take isl_pw_aff *pwaff2)
1618 {
1619         return isl_pw_aff_gt_set(pwaff2, pwaff1);
1620 }
1621
1622 /* Return a set containing those elements in the shared domain
1623  * of the elements of list1 and list2 where each element in list1
1624  * has the relation specified by "fn" with each element in list2.
1625  */
1626 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
1627         __isl_take isl_pw_aff_list *list2,
1628         __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1629                                     __isl_take isl_pw_aff *pwaff2))
1630 {
1631         int i, j;
1632         isl_ctx *ctx;
1633         isl_set *set;
1634
1635         if (!list1 || !list2)
1636                 goto error;
1637
1638         ctx = isl_pw_aff_list_get_ctx(list1);
1639         if (list1->n < 1 || list2->n < 1)
1640                 isl_die(ctx, isl_error_invalid,
1641                         "list should contain at least one element", goto error);
1642
1643         set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
1644         for (i = 0; i < list1->n; ++i)
1645                 for (j = 0; j < list2->n; ++j) {
1646                         isl_set *set_ij;
1647
1648                         set_ij = fn(isl_pw_aff_copy(list1->p[i]),
1649                                     isl_pw_aff_copy(list2->p[j]));
1650                         set = isl_set_intersect(set, set_ij);
1651                 }
1652
1653         isl_pw_aff_list_free(list1);
1654         isl_pw_aff_list_free(list2);
1655         return set;
1656 error:
1657         isl_pw_aff_list_free(list1);
1658         isl_pw_aff_list_free(list2);
1659         return NULL;
1660 }
1661
1662 /* Return a set containing those elements in the shared domain
1663  * of the elements of list1 and list2 where each element in list1
1664  * is equal to each element in list2.
1665  */
1666 __isl_give isl_set *isl_pw_aff_list_eq_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_eq_set);
1670 }
1671
1672 __isl_give isl_set *isl_pw_aff_list_ne_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_ne_set);
1676 }
1677
1678 /* Return a set containing those elements in the shared domain
1679  * of the elements of list1 and list2 where each element in list1
1680  * is less than or equal to each element in list2.
1681  */
1682 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
1683         __isl_take isl_pw_aff_list *list2)
1684 {
1685         return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
1686 }
1687
1688 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
1689         __isl_take isl_pw_aff_list *list2)
1690 {
1691         return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
1692 }
1693
1694 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
1695         __isl_take isl_pw_aff_list *list2)
1696 {
1697         return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
1698 }
1699
1700 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
1701         __isl_take isl_pw_aff_list *list2)
1702 {
1703         return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
1704 }
1705
1706
1707 /* Return a set containing those elements in the shared domain
1708  * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1709  */
1710 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1711         __isl_take isl_pw_aff *pwaff2)
1712 {
1713         isl_set *set_lt, *set_gt;
1714
1715         set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
1716                                    isl_pw_aff_copy(pwaff2));
1717         set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
1718         return isl_set_union_disjoint(set_lt, set_gt);
1719 }
1720
1721 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1722         __isl_take isl_pw_aff *pwaff2)
1723 {
1724         return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
1725 }
1726
1727 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
1728         isl_int v)
1729 {
1730         int i;
1731
1732         if (isl_int_is_one(v))
1733                 return pwaff;
1734         if (!isl_int_is_pos(v))
1735                 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1736                         "factor needs to be positive",
1737                         return isl_pw_aff_free(pwaff));
1738         pwaff = isl_pw_aff_cow(pwaff);
1739         if (!pwaff)
1740                 return NULL;
1741         if (pwaff->n == 0)
1742                 return pwaff;
1743
1744         for (i = 0; i < pwaff->n; ++i) {
1745                 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
1746                 if (!pwaff->p[i].aff)
1747                         return isl_pw_aff_free(pwaff);
1748         }
1749
1750         return pwaff;
1751 }
1752
1753 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
1754 {
1755         int i;
1756
1757         pwaff = isl_pw_aff_cow(pwaff);
1758         if (!pwaff)
1759                 return NULL;
1760         if (pwaff->n == 0)
1761                 return pwaff;
1762
1763         for (i = 0; i < pwaff->n; ++i) {
1764                 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
1765                 if (!pwaff->p[i].aff)
1766                         return isl_pw_aff_free(pwaff);
1767         }
1768
1769         return pwaff;
1770 }
1771
1772 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
1773 {
1774         int i;
1775
1776         pwaff = isl_pw_aff_cow(pwaff);
1777         if (!pwaff)
1778                 return NULL;
1779         if (pwaff->n == 0)
1780                 return pwaff;
1781
1782         for (i = 0; i < pwaff->n; ++i) {
1783                 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
1784                 if (!pwaff->p[i].aff)
1785                         return isl_pw_aff_free(pwaff);
1786         }
1787
1788         return pwaff;
1789 }
1790
1791 /* Return an affine expression that is equal to pwaff_true for elements
1792  * in "cond" and to pwaff_false for elements not in "cond".
1793  * That is, return cond ? pwaff_true : pwaff_false;
1794  */
1795 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_set *cond,
1796         __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
1797 {
1798         isl_set *comp;
1799
1800         comp = isl_set_complement(isl_set_copy(cond));
1801         pwaff_true = isl_pw_aff_intersect_domain(pwaff_true, cond);
1802         pwaff_false = isl_pw_aff_intersect_domain(pwaff_false, comp);
1803
1804         return isl_pw_aff_add_disjoint(pwaff_true, pwaff_false);
1805 }
1806
1807 int isl_aff_is_cst(__isl_keep isl_aff *aff)
1808 {
1809         if (!aff)
1810                 return -1;
1811
1812         return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
1813 }
1814
1815 /* Check whether pwaff is a piecewise constant.
1816  */
1817 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
1818 {
1819         int i;
1820
1821         if (!pwaff)
1822                 return -1;
1823
1824         for (i = 0; i < pwaff->n; ++i) {
1825                 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
1826                 if (is_cst < 0 || !is_cst)
1827                         return is_cst;
1828         }
1829
1830         return 1;
1831 }
1832
1833 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
1834         __isl_take isl_aff *aff2)
1835 {
1836         if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
1837                 return isl_aff_mul(aff2, aff1);
1838
1839         if (!isl_aff_is_cst(aff2))
1840                 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
1841                         "at least one affine expression should be constant",
1842                         goto error);
1843
1844         aff1 = isl_aff_cow(aff1);
1845         if (!aff1 || !aff2)
1846                 goto error;
1847
1848         aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
1849         aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
1850
1851         isl_aff_free(aff2);
1852         return aff1;
1853 error:
1854         isl_aff_free(aff1);
1855         isl_aff_free(aff2);
1856         return NULL;
1857 }
1858
1859 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
1860         __isl_take isl_pw_aff *pwaff2)
1861 {
1862         return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
1863 }
1864
1865 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
1866         __isl_take isl_pw_aff *pwaff2)
1867 {
1868         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
1869 }
1870
1871 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
1872         __isl_take isl_pw_aff *pwaff2)
1873 {
1874         return isl_pw_aff_union_add_(pwaff1, pwaff2);
1875 }
1876
1877 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1878         __isl_take isl_pw_aff *pwaff2)
1879 {
1880         return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
1881 }
1882
1883 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1884         __isl_take isl_pw_aff *pwaff2)
1885 {
1886         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
1887 }
1888
1889 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1890         __isl_take isl_pw_aff *pwaff2)
1891 {
1892         isl_set *le;
1893
1894         le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
1895                                 isl_pw_aff_copy(pwaff2));
1896         return isl_pw_aff_cond(le, pwaff1, pwaff2);
1897 }
1898
1899 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1900         __isl_take isl_pw_aff *pwaff2)
1901 {
1902         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
1903 }
1904
1905 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1906         __isl_take isl_pw_aff *pwaff2)
1907 {
1908         isl_set *le;
1909
1910         le = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
1911                                 isl_pw_aff_copy(pwaff2));
1912         return isl_pw_aff_cond(le, pwaff1, pwaff2);
1913 }
1914
1915 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1916         __isl_take isl_pw_aff *pwaff2)
1917 {
1918         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
1919 }
1920
1921 static __isl_give isl_pw_aff *pw_aff_list_reduce(
1922         __isl_take isl_pw_aff_list *list,
1923         __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
1924                                         __isl_take isl_pw_aff *pwaff2))
1925 {
1926         int i;
1927         isl_ctx *ctx;
1928         isl_pw_aff *res;
1929
1930         if (!list)
1931                 return NULL;
1932
1933         ctx = isl_pw_aff_list_get_ctx(list);
1934         if (list->n < 1)
1935                 isl_die(ctx, isl_error_invalid,
1936                         "list should contain at least one element",
1937                         return isl_pw_aff_list_free(list));
1938
1939         res = isl_pw_aff_copy(list->p[0]);
1940         for (i = 1; i < list->n; ++i)
1941                 res = fn(res, isl_pw_aff_copy(list->p[i]));
1942
1943         isl_pw_aff_list_free(list);
1944         return res;
1945 }
1946
1947 /* Return an isl_pw_aff that maps each element in the intersection of the
1948  * domains of the elements of list to the minimal corresponding affine
1949  * expression.
1950  */
1951 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
1952 {
1953         return pw_aff_list_reduce(list, &isl_pw_aff_min);
1954 }
1955
1956 /* Return an isl_pw_aff that maps each element in the intersection of the
1957  * domains of the elements of list to the maximal corresponding affine
1958  * expression.
1959  */
1960 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
1961 {
1962         return pw_aff_list_reduce(list, &isl_pw_aff_max);
1963 }
1964
1965 #undef BASE
1966 #define BASE aff
1967
1968 #include <isl_multi_templ.c>
1969
1970 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
1971         __isl_take isl_multi_aff *maff2)
1972 {
1973         int i;
1974         isl_ctx *ctx;
1975
1976         maff1 = isl_multi_aff_cow(maff1);
1977         if (!maff1 || !maff2)
1978                 goto error;
1979
1980         ctx = isl_multi_aff_get_ctx(maff1);
1981         if (!isl_space_is_equal(maff1->space, maff2->space))
1982                 isl_die(ctx, isl_error_invalid,
1983                         "spaces don't match", goto error);
1984
1985         for (i = 0; i < maff1->n; ++i) {
1986                 maff1->p[i] = isl_aff_add(maff1->p[i],
1987                                             isl_aff_copy(maff2->p[i]));
1988                 if (!maff1->p[i])
1989                         goto error;
1990         }
1991
1992         isl_multi_aff_free(maff2);
1993         return maff1;
1994 error:
1995         isl_multi_aff_free(maff1);
1996         isl_multi_aff_free(maff2);
1997         return NULL;
1998 }
1999
2000 /* Exploit the equalities in "eq" to simplify the affine expressions.
2001  */
2002 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
2003         __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
2004 {
2005         int i;
2006
2007         maff = isl_multi_aff_cow(maff);
2008         if (!maff || !eq)
2009                 goto error;
2010
2011         for (i = 0; i < maff->n; ++i) {
2012                 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
2013                                                     isl_basic_set_copy(eq));
2014                 if (!maff->p[i])
2015                         goto error;
2016         }
2017
2018         isl_basic_set_free(eq);
2019         return maff;
2020 error:
2021         isl_basic_set_free(eq);
2022         isl_multi_aff_free(maff);
2023         return NULL;
2024 }
2025
2026 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
2027         isl_int f)
2028 {
2029         int i;
2030
2031         maff = isl_multi_aff_cow(maff);
2032         if (!maff)
2033                 return NULL;
2034
2035         for (i = 0; i < maff->n; ++i) {
2036                 maff->p[i] = isl_aff_scale(maff->p[i], f);
2037                 if (!maff->p[i])
2038                         return isl_multi_aff_free(maff);
2039         }
2040
2041         return maff;
2042 }
2043
2044 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
2045         __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
2046 {
2047         maff1 = isl_multi_aff_add(maff1, maff2);
2048         maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
2049         return maff1;
2050 }
2051
2052 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
2053 {
2054         if (!maff)
2055                 return -1;
2056
2057         return 0;
2058 }
2059
2060 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
2061         __isl_keep isl_multi_aff *maff2)
2062 {
2063         int i;
2064         int equal;
2065
2066         if (!maff1 || !maff2)
2067                 return -1;
2068         if (maff1->n != maff2->n)
2069                 return 0;
2070         equal = isl_space_is_equal(maff1->space, maff2->space);
2071         if (equal < 0 || !equal)
2072                 return equal;
2073
2074         for (i = 0; i < maff1->n; ++i) {
2075                 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
2076                 if (equal < 0 || !equal)
2077                         return equal;
2078         }
2079
2080         return 1;
2081 }
2082
2083 __isl_give isl_multi_aff *isl_multi_aff_set_dim_name(
2084         __isl_take isl_multi_aff *maff,
2085         enum isl_dim_type type, unsigned pos, const char *s)
2086 {
2087         int i;
2088
2089         maff = isl_multi_aff_cow(maff);
2090         if (!maff)
2091                 return NULL;
2092
2093         maff->space = isl_space_set_dim_name(maff->space, type, pos, s);
2094         if (!maff->space)
2095                 return isl_multi_aff_free(maff);
2096         for (i = 0; i < maff->n; ++i) {
2097                 maff->p[i] = isl_aff_set_dim_name(maff->p[i], type, pos, s);
2098                 if (!maff->p[i])
2099                         return isl_multi_aff_free(maff);
2100         }
2101
2102         return maff;
2103 }
2104
2105 __isl_give isl_multi_aff *isl_multi_aff_drop_dims(__isl_take isl_multi_aff *maff,
2106         enum isl_dim_type type, unsigned first, unsigned n)
2107 {
2108         int i;
2109
2110         maff = isl_multi_aff_cow(maff);
2111         if (!maff)
2112                 return NULL;
2113
2114         maff->space = isl_space_drop_dims(maff->space, type, first, n);
2115         if (!maff->space)
2116                 return isl_multi_aff_free(maff);
2117         for (i = 0; i < maff->n; ++i) {
2118                 maff->p[i] = isl_aff_drop_dims(maff->p[i], type, first, n);
2119                 if (!maff->p[i])
2120                         return isl_multi_aff_free(maff);
2121         }
2122
2123         return maff;
2124 }
2125
2126 #undef PW
2127 #define PW isl_pw_multi_aff
2128 #undef EL
2129 #define EL isl_multi_aff
2130 #undef EL_IS_ZERO
2131 #define EL_IS_ZERO is_empty
2132 #undef ZERO
2133 #define ZERO empty
2134 #undef IS_ZERO
2135 #define IS_ZERO is_empty
2136 #undef FIELD
2137 #define FIELD maff
2138 #undef DEFAULT_IS_ZERO
2139 #define DEFAULT_IS_ZERO 0
2140
2141 #define NO_NEG
2142 #define NO_EVAL
2143 #define NO_OPT
2144 #define NO_INVOLVES_DIMS
2145 #define NO_MOVE_DIMS
2146 #define NO_INSERT_DIMS
2147 #define NO_LIFT
2148 #define NO_MORPH
2149
2150 #include <isl_pw_templ.c>
2151
2152 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
2153         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2154 {
2155         return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
2156                                                 &isl_multi_aff_add);
2157 }
2158
2159 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
2160         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2161 {
2162         return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
2163                                                 &pw_multi_aff_add);
2164 }
2165
2166 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
2167         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2168 {
2169         return isl_pw_multi_aff_union_add_(pma1, pma2);
2170 }
2171
2172 /* Construct a map mapping the domain the piecewise multi-affine expression
2173  * to its range, with each dimension in the range equated to the
2174  * corresponding affine expression on its cell.
2175  */
2176 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2177 {
2178         int i;
2179         isl_map *map;
2180
2181         if (!pma)
2182                 return NULL;
2183
2184         map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
2185
2186         for (i = 0; i < pma->n; ++i) {
2187                 isl_multi_aff *maff;
2188                 isl_basic_map *bmap;
2189                 isl_map *map_i;
2190
2191                 maff = isl_multi_aff_copy(pma->p[i].maff);
2192                 bmap = isl_basic_map_from_multi_aff(maff);
2193                 map_i = isl_map_from_basic_map(bmap);
2194                 map_i = isl_map_intersect_domain(map_i,
2195                                                 isl_set_copy(pma->p[i].set));
2196                 map = isl_map_union_disjoint(map, map_i);
2197         }
2198
2199         isl_pw_multi_aff_free(pma);
2200         return map;
2201 }
2202
2203 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2204 {
2205         if (!isl_space_is_set(pma->dim))
2206                 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
2207                         "isl_pw_multi_aff cannot be converted into an isl_set",
2208                         return isl_pw_multi_aff_free(pma));
2209
2210         return isl_map_from_pw_multi_aff(pma);
2211 }
2212
2213 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
2214  * This obivously only works if the input "map" is single-valued.
2215  * If so, we compute the lexicographic minimum of the image in the form
2216  * of an isl_pw_multi_aff.  Since the image is unique, it is equal
2217  * to its lexicographic minimum.
2218  * If the input is not single-valued, we produce an error.
2219  */
2220 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
2221 {
2222         int i;
2223         int sv;
2224         isl_pw_multi_aff *pma;
2225
2226         if (!map)
2227                 return NULL;
2228
2229         sv = isl_map_is_single_valued(map);
2230         if (sv < 0)
2231                 goto error;
2232         if (!sv)
2233                 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2234                         "map is not single-valued", goto error);
2235         map = isl_map_make_disjoint(map);
2236         if (!map)
2237                 return NULL;
2238
2239         pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
2240
2241         for (i = 0; i < map->n; ++i) {
2242                 isl_pw_multi_aff *pma_i;
2243                 isl_basic_map *bmap;
2244                 bmap = isl_basic_map_copy(map->p[i]);
2245                 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
2246                 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
2247         }
2248
2249         isl_map_free(map);
2250         return pma;
2251 error:
2252         isl_map_free(map);
2253         return NULL;
2254 }
2255
2256 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
2257 {
2258         return isl_pw_multi_aff_from_map(set);
2259 }
2260
2261 /* Plug in "subs" for dimension "type", "pos" of "aff".
2262  *
2263  * Let i be the dimension to replace and let "subs" be of the form
2264  *
2265  *      f/d
2266  *
2267  * and "aff" of the form
2268  *
2269  *      (a i + g)/m
2270  *
2271  * The result is
2272  *
2273  *      floor((a f + d g')/(m d))
2274  *
2275  * where g' is the result of plugging in "subs" in each of the integer
2276  * divisions in g.
2277  */
2278 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
2279         enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
2280 {
2281         isl_ctx *ctx;
2282         isl_int v;
2283
2284         aff = isl_aff_cow(aff);
2285         if (!aff || !subs)
2286                 return isl_aff_free(aff);
2287
2288         ctx = isl_aff_get_ctx(aff);
2289         if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
2290                 isl_die(ctx, isl_error_invalid,
2291                         "spaces don't match", return isl_aff_free(aff));
2292         if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
2293                 isl_die(ctx, isl_error_unsupported,
2294                         "cannot handle divs yet", return isl_aff_free(aff));
2295
2296         aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
2297         if (!aff->ls)
2298                 return isl_aff_free(aff);
2299
2300         aff->v = isl_vec_cow(aff->v);
2301         if (!aff->v)
2302                 return isl_aff_free(aff);
2303
2304         pos += isl_local_space_offset(aff->ls, type);
2305
2306         isl_int_init(v);
2307         isl_int_set(v, aff->v->el[1 + pos]);
2308         isl_int_set_si(aff->v->el[1 + pos], 0);
2309         isl_seq_combine(aff->v->el + 1, subs->v->el[0], aff->v->el + 1,
2310                         v, subs->v->el + 1, subs->v->size - 1);
2311         isl_int_mul(aff->v->el[0], aff->v->el[0], subs->v->el[0]);
2312         isl_int_clear(v);
2313
2314         return aff;
2315 }
2316
2317 /* Plug in "subs" for dimension "type", "pos" in each of the affine
2318  * expressions in "maff".
2319  */
2320 __isl_give isl_multi_aff *isl_multi_aff_substitute(
2321         __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
2322         __isl_keep isl_aff *subs)
2323 {
2324         int i;
2325
2326         maff = isl_multi_aff_cow(maff);
2327         if (!maff || !subs)
2328                 return isl_multi_aff_free(maff);
2329
2330         if (type == isl_dim_in)
2331                 type = isl_dim_set;
2332
2333         for (i = 0; i < maff->n; ++i) {
2334                 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
2335                 if (!maff->p[i])
2336                         return isl_multi_aff_free(maff);
2337         }
2338
2339         return maff;
2340 }
2341
2342 /* Plug in "subs" for dimension "type", "pos" of "pma".
2343  *
2344  * pma is of the form
2345  *
2346  *      A_i(v) -> M_i(v)
2347  *
2348  * while subs is of the form
2349  *
2350  *      v' = B_j(v) -> S_j
2351  *
2352  * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
2353  * has a contribution in the result, in particular
2354  *
2355  *      C_ij(S_j) -> M_i(S_j)
2356  *
2357  * Note that plugging in S_j in C_ij may also result in an empty set
2358  * and this contribution should simply be discarded.
2359  */
2360 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
2361         __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
2362         __isl_keep isl_pw_aff *subs)
2363 {
2364         int i, j, n;
2365         isl_pw_multi_aff *res;
2366
2367         if (!pma || !subs)
2368                 return isl_pw_multi_aff_free(pma);
2369
2370         n = pma->n * subs->n;
2371         res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
2372
2373         for (i = 0; i < pma->n; ++i) {
2374                 for (j = 0; j < subs->n; ++j) {
2375                         isl_set *common;
2376                         isl_multi_aff *res_ij;
2377                         common = isl_set_intersect(
2378                                         isl_set_copy(pma->p[i].set),
2379                                         isl_set_copy(subs->p[j].set));
2380                         common = isl_set_substitute(common,
2381                                         type, pos, subs->p[j].aff);
2382                         if (isl_set_plain_is_empty(common)) {
2383                                 isl_set_free(common);
2384                                 continue;
2385                         }
2386
2387                         res_ij = isl_multi_aff_substitute(
2388                                         isl_multi_aff_copy(pma->p[i].maff),
2389                                         type, pos, subs->p[j].aff);
2390
2391                         res = isl_pw_multi_aff_add_piece(res, common, res_ij);
2392                 }
2393         }
2394
2395         isl_pw_multi_aff_free(pma);
2396         return res;
2397 }
2398
2399 /* Extend the local space of "dst" to include the divs
2400  * in the local space of "src".
2401  */
2402 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
2403         __isl_keep isl_aff *src)
2404 {
2405         isl_ctx *ctx;
2406         int *exp1 = NULL;
2407         int *exp2 = NULL;
2408         isl_mat *div;
2409
2410         if (!src || !dst)
2411                 return isl_aff_free(dst);
2412
2413         ctx = isl_aff_get_ctx(src);
2414         if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
2415                 isl_die(ctx, isl_error_invalid,
2416                         "spaces don't match", goto error);
2417
2418         if (src->ls->div->n_row == 0)
2419                 return dst;
2420
2421         exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
2422         exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
2423         if (!exp1 || !exp2)
2424                 goto error;
2425
2426         div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
2427         dst = isl_aff_expand_divs(dst, div, exp2);
2428         free(exp1);
2429         free(exp2);
2430
2431         return dst;
2432 error:
2433         free(exp1);
2434         free(exp2);
2435         return isl_aff_free(dst);
2436 }
2437
2438 /* Adjust the local spaces of the affine expressions in "maff"
2439  * such that they all have the save divs.
2440  */
2441 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
2442         __isl_take isl_multi_aff *maff)
2443 {
2444         int i;
2445
2446         if (!maff)
2447                 return NULL;
2448         if (maff->n == 0)
2449                 return maff;
2450         maff = isl_multi_aff_cow(maff);
2451         if (!maff)
2452                 return NULL;
2453
2454         for (i = 1; i < maff->n; ++i)
2455                 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
2456         for (i = 1; i < maff->n; ++i) {
2457                 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
2458                 if (!maff->p[i])
2459                         return isl_multi_aff_free(maff);
2460         }
2461
2462         return maff;
2463 }
2464
2465 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
2466 {
2467         aff = isl_aff_cow(aff);
2468         if (!aff)
2469                 return NULL;
2470
2471         aff->ls = isl_local_space_lift(aff->ls);
2472         if (!aff->ls)
2473                 return isl_aff_free(aff);
2474
2475         return aff;
2476 }
2477
2478 /* Lift "maff" to a space with extra dimensions such that the result
2479  * has no more existentially quantified variables.
2480  * If "ls" is not NULL, then *ls is assigned the local space that lies
2481  * at the basis of the lifting applied to "maff".
2482  */
2483 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
2484         __isl_give isl_local_space **ls)
2485 {
2486         int i;
2487         isl_space *space;
2488         unsigned n_div;
2489
2490         if (ls)
2491                 *ls = NULL;
2492
2493         if (!maff)
2494                 return NULL;
2495
2496         if (maff->n == 0) {
2497                 if (ls) {
2498                         isl_space *space = isl_multi_aff_get_domain_space(maff);
2499                         *ls = isl_local_space_from_space(space);
2500                         if (!*ls)
2501                                 return isl_multi_aff_free(maff);
2502                 }
2503                 return maff;
2504         }
2505
2506         maff = isl_multi_aff_cow(maff);
2507         maff = isl_multi_aff_align_divs(maff);
2508         if (!maff)
2509                 return NULL;
2510
2511         n_div = isl_aff_dim(maff->p[0], isl_dim_div);
2512         space = isl_multi_aff_get_space(maff);
2513         space = isl_space_lift(isl_space_domain(space), n_div);
2514         space = isl_space_extend_domain_with_range(space,
2515                                                 isl_multi_aff_get_space(maff));
2516         if (!space)
2517                 return isl_multi_aff_free(maff);
2518         isl_space_free(maff->space);
2519         maff->space = space;
2520
2521         if (ls) {
2522                 *ls = isl_aff_get_domain_local_space(maff->p[0]);
2523                 if (!*ls)
2524                         return isl_multi_aff_free(maff);
2525         }
2526
2527         for (i = 0; i < maff->n; ++i) {
2528                 maff->p[i] = isl_aff_lift(maff->p[i]);
2529                 if (!maff->p[i])
2530                         goto error;
2531         }
2532
2533         return maff;
2534 error:
2535         if (ls)
2536                 isl_local_space_free(*ls);
2537         return isl_multi_aff_free(maff);
2538 }
2539
2540
2541 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
2542  */
2543 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
2544         __isl_keep isl_pw_multi_aff *pma, int pos)
2545 {
2546         int i;
2547         int n_out;
2548         isl_space *space;
2549         isl_pw_aff *pa;
2550
2551         if (!pma)
2552                 return NULL;
2553
2554         n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
2555         if (pos < 0 || pos >= n_out)
2556                 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
2557                         "index out of bounds", return NULL);
2558
2559         space = isl_pw_multi_aff_get_space(pma);
2560         space = isl_space_drop_dims(space, isl_dim_out,
2561                                     pos + 1, n_out - pos - 1);
2562         space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
2563
2564         pa = isl_pw_aff_alloc_size(space, pma->n);
2565         for (i = 0; i < pma->n; ++i) {
2566                 isl_aff *aff;
2567                 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
2568                 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
2569         }
2570
2571         return pa;
2572 }