add isl_aff_mod_val
[platform/upstream/isl.git] / isl_aff.c
1 /*
2  * Copyright 2011      INRIA Saclay
3  * Copyright 2011      Sven Verdoolaege
4  * Copyright 2012-2013 Ecole Normale Superieure
5  *
6  * Use of this software is governed by the MIT license
7  *
8  * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9  * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10  * 91893 Orsay, France
11  * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
12  */
13
14 #include <isl_ctx_private.h>
15 #define ISL_DIM_H
16 #include <isl_map_private.h>
17 #include <isl_union_map_private.h>
18 #include <isl_aff_private.h>
19 #include <isl_space_private.h>
20 #include <isl_local_space_private.h>
21 #include <isl_mat_private.h>
22 #include <isl/constraint.h>
23 #include <isl/seq.h>
24 #include <isl/set.h>
25 #include <isl_val_private.h>
26 #include <isl_config.h>
27
28 #undef BASE
29 #define BASE aff
30
31 #include <isl_list_templ.c>
32
33 #undef BASE
34 #define BASE pw_aff
35
36 #include <isl_list_templ.c>
37
38 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
39         __isl_take isl_vec *v)
40 {
41         isl_aff *aff;
42
43         if (!ls || !v)
44                 goto error;
45
46         aff = isl_calloc_type(v->ctx, struct isl_aff);
47         if (!aff)
48                 goto error;
49
50         aff->ref = 1;
51         aff->ls = ls;
52         aff->v = v;
53
54         return aff;
55 error:
56         isl_local_space_free(ls);
57         isl_vec_free(v);
58         return NULL;
59 }
60
61 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
62 {
63         isl_ctx *ctx;
64         isl_vec *v;
65         unsigned total;
66
67         if (!ls)
68                 return NULL;
69
70         ctx = isl_local_space_get_ctx(ls);
71         if (!isl_local_space_divs_known(ls))
72                 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
73                         goto error);
74         if (!isl_local_space_is_set(ls))
75                 isl_die(ctx, isl_error_invalid,
76                         "domain of affine expression should be a set",
77                         goto error);
78
79         total = isl_local_space_dim(ls, isl_dim_all);
80         v = isl_vec_alloc(ctx, 1 + 1 + total);
81         return isl_aff_alloc_vec(ls, v);
82 error:
83         isl_local_space_free(ls);
84         return NULL;
85 }
86
87 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
88 {
89         isl_aff *aff;
90
91         aff = isl_aff_alloc(ls);
92         if (!aff)
93                 return NULL;
94
95         isl_int_set_si(aff->v->el[0], 1);
96         isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
97
98         return aff;
99 }
100
101 /* Return a piecewise affine expression defined on the specified domain
102  * that is equal to zero.
103  */
104 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
105 {
106         return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
107 }
108
109 /* Return an affine expression that is equal to the specified dimension
110  * in "ls".
111  */
112 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
113         enum isl_dim_type type, unsigned pos)
114 {
115         isl_space *space;
116         isl_aff *aff;
117
118         if (!ls)
119                 return NULL;
120
121         space = isl_local_space_get_space(ls);
122         if (!space)
123                 goto error;
124         if (isl_space_is_map(space))
125                 isl_die(isl_space_get_ctx(space), isl_error_invalid,
126                         "expecting (parameter) set space", goto error);
127         if (pos >= isl_local_space_dim(ls, type))
128                 isl_die(isl_space_get_ctx(space), isl_error_invalid,
129                         "position out of bounds", goto error);
130
131         isl_space_free(space);
132         aff = isl_aff_alloc(ls);
133         if (!aff)
134                 return NULL;
135
136         pos += isl_local_space_offset(aff->ls, type);
137
138         isl_int_set_si(aff->v->el[0], 1);
139         isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
140         isl_int_set_si(aff->v->el[1 + pos], 1);
141
142         return aff;
143 error:
144         isl_local_space_free(ls);
145         isl_space_free(space);
146         return NULL;
147 }
148
149 /* Return a piecewise affine expression that is equal to
150  * the specified dimension in "ls".
151  */
152 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
153         enum isl_dim_type type, unsigned pos)
154 {
155         return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
156 }
157
158 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
159 {
160         if (!aff)
161                 return NULL;
162
163         aff->ref++;
164         return aff;
165 }
166
167 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
168 {
169         if (!aff)
170                 return NULL;
171
172         return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
173                                  isl_vec_copy(aff->v));
174 }
175
176 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
177 {
178         if (!aff)
179                 return NULL;
180
181         if (aff->ref == 1)
182                 return aff;
183         aff->ref--;
184         return isl_aff_dup(aff);
185 }
186
187 void *isl_aff_free(__isl_take isl_aff *aff)
188 {
189         if (!aff)
190                 return NULL;
191
192         if (--aff->ref > 0)
193                 return NULL;
194
195         isl_local_space_free(aff->ls);
196         isl_vec_free(aff->v);
197
198         free(aff);
199
200         return NULL;
201 }
202
203 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
204 {
205         return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
206 }
207
208 /* Externally, an isl_aff has a map space, but internally, the
209  * ls field corresponds to the domain of that space.
210  */
211 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
212 {
213         if (!aff)
214                 return 0;
215         if (type == isl_dim_out)
216                 return 1;
217         if (type == isl_dim_in)
218                 type = isl_dim_set;
219         return isl_local_space_dim(aff->ls, type);
220 }
221
222 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
223 {
224         return aff ? isl_local_space_get_space(aff->ls) : NULL;
225 }
226
227 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
228 {
229         isl_space *space;
230         if (!aff)
231                 return NULL;
232         space = isl_local_space_get_space(aff->ls);
233         space = isl_space_from_domain(space);
234         space = isl_space_add_dims(space, isl_dim_out, 1);
235         return space;
236 }
237
238 __isl_give isl_local_space *isl_aff_get_domain_local_space(
239         __isl_keep isl_aff *aff)
240 {
241         return aff ? isl_local_space_copy(aff->ls) : NULL;
242 }
243
244 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
245 {
246         isl_local_space *ls;
247         if (!aff)
248                 return NULL;
249         ls = isl_local_space_copy(aff->ls);
250         ls = isl_local_space_from_domain(ls);
251         ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
252         return ls;
253 }
254
255 /* Externally, an isl_aff has a map space, but internally, the
256  * ls field corresponds to the domain of that space.
257  */
258 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
259         enum isl_dim_type type, unsigned pos)
260 {
261         if (!aff)
262                 return NULL;
263         if (type == isl_dim_out)
264                 return NULL;
265         if (type == isl_dim_in)
266                 type = isl_dim_set;
267         return isl_local_space_get_dim_name(aff->ls, type, pos);
268 }
269
270 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
271         __isl_take isl_space *dim)
272 {
273         aff = isl_aff_cow(aff);
274         if (!aff || !dim)
275                 goto error;
276
277         aff->ls = isl_local_space_reset_space(aff->ls, dim);
278         if (!aff->ls)
279                 return isl_aff_free(aff);
280
281         return aff;
282 error:
283         isl_aff_free(aff);
284         isl_space_free(dim);
285         return NULL;
286 }
287
288 /* Reset the space of "aff".  This function is called from isl_pw_templ.c
289  * and doesn't know if the space of an element object is represented
290  * directly or through its domain.  It therefore passes along both.
291  */
292 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
293         __isl_take isl_space *space, __isl_take isl_space *domain)
294 {
295         isl_space_free(space);
296         return isl_aff_reset_domain_space(aff, domain);
297 }
298
299 /* Reorder the coefficients of the affine expression based
300  * on the given reodering.
301  * The reordering r is assumed to have been extended with the local
302  * variables.
303  */
304 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
305         __isl_take isl_reordering *r, int n_div)
306 {
307         isl_vec *res;
308         int i;
309
310         if (!vec || !r)
311                 goto error;
312
313         res = isl_vec_alloc(vec->ctx,
314                             2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
315         isl_seq_cpy(res->el, vec->el, 2);
316         isl_seq_clr(res->el + 2, res->size - 2);
317         for (i = 0; i < r->len; ++i)
318                 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
319
320         isl_reordering_free(r);
321         isl_vec_free(vec);
322         return res;
323 error:
324         isl_vec_free(vec);
325         isl_reordering_free(r);
326         return NULL;
327 }
328
329 /* Reorder the dimensions of the domain of "aff" according
330  * to the given reordering.
331  */
332 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
333         __isl_take isl_reordering *r)
334 {
335         aff = isl_aff_cow(aff);
336         if (!aff)
337                 goto error;
338
339         r = isl_reordering_extend(r, aff->ls->div->n_row);
340         aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
341                                 aff->ls->div->n_row);
342         aff->ls = isl_local_space_realign(aff->ls, r);
343
344         if (!aff->v || !aff->ls)
345                 return isl_aff_free(aff);
346
347         return aff;
348 error:
349         isl_aff_free(aff);
350         isl_reordering_free(r);
351         return NULL;
352 }
353
354 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
355         __isl_take isl_space *model)
356 {
357         if (!aff || !model)
358                 goto error;
359
360         if (!isl_space_match(aff->ls->dim, isl_dim_param,
361                              model, isl_dim_param)) {
362                 isl_reordering *exp;
363
364                 model = isl_space_drop_dims(model, isl_dim_in,
365                                         0, isl_space_dim(model, isl_dim_in));
366                 model = isl_space_drop_dims(model, isl_dim_out,
367                                         0, isl_space_dim(model, isl_dim_out));
368                 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
369                 exp = isl_reordering_extend_space(exp,
370                                         isl_aff_get_domain_space(aff));
371                 aff = isl_aff_realign_domain(aff, exp);
372         }
373
374         isl_space_free(model);
375         return aff;
376 error:
377         isl_space_free(model);
378         isl_aff_free(aff);
379         return NULL;
380 }
381
382 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
383 {
384         if (!aff)
385                 return -1;
386
387         return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
388 }
389
390 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
391 {
392         int equal;
393
394         if (!aff1 || !aff2)
395                 return -1;
396
397         equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
398         if (equal < 0 || !equal)
399                 return equal;
400
401         return isl_vec_is_equal(aff1->v, aff2->v);
402 }
403
404 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
405 {
406         if (!aff)
407                 return -1;
408         isl_int_set(*v, aff->v->el[0]);
409         return 0;
410 }
411
412 /* Return the common denominator of "aff".
413  */
414 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
415 {
416         isl_ctx *ctx;
417
418         if (!aff)
419                 return NULL;
420
421         ctx = isl_aff_get_ctx(aff);
422         return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
423 }
424
425 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
426 {
427         if (!aff)
428                 return -1;
429         isl_int_set(*v, aff->v->el[1]);
430         return 0;
431 }
432
433 /* Return the constant term of "aff".
434  */
435 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
436 {
437         isl_ctx *ctx;
438         isl_val *v;
439
440         if (!aff)
441                 return NULL;
442
443         ctx = isl_aff_get_ctx(aff);
444         v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
445         return isl_val_normalize(v);
446 }
447
448 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
449         enum isl_dim_type type, int pos, isl_int *v)
450 {
451         if (!aff)
452                 return -1;
453
454         if (type == isl_dim_out)
455                 isl_die(aff->v->ctx, isl_error_invalid,
456                         "output/set dimension does not have a coefficient",
457                         return -1);
458         if (type == isl_dim_in)
459                 type = isl_dim_set;
460
461         if (pos >= isl_local_space_dim(aff->ls, type))
462                 isl_die(aff->v->ctx, isl_error_invalid,
463                         "position out of bounds", return -1);
464
465         pos += isl_local_space_offset(aff->ls, type);
466         isl_int_set(*v, aff->v->el[1 + pos]);
467
468         return 0;
469 }
470
471 /* Return the coefficient of the variable of type "type" at position "pos"
472  * of "aff".
473  */
474 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
475         enum isl_dim_type type, int pos)
476 {
477         isl_ctx *ctx;
478         isl_val *v;
479
480         if (!aff)
481                 return NULL;
482
483         ctx = isl_aff_get_ctx(aff);
484         if (type == isl_dim_out)
485                 isl_die(ctx, isl_error_invalid,
486                         "output/set dimension does not have a coefficient",
487                         return NULL);
488         if (type == isl_dim_in)
489                 type = isl_dim_set;
490
491         if (pos >= isl_local_space_dim(aff->ls, type))
492                 isl_die(ctx, isl_error_invalid,
493                         "position out of bounds", return NULL);
494
495         pos += isl_local_space_offset(aff->ls, type);
496         v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
497         return isl_val_normalize(v);
498 }
499
500 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
501 {
502         aff = isl_aff_cow(aff);
503         if (!aff)
504                 return NULL;
505
506         aff->v = isl_vec_cow(aff->v);
507         if (!aff->v)
508                 return isl_aff_free(aff);
509
510         isl_int_set(aff->v->el[0], v);
511
512         return aff;
513 }
514
515 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
516 {
517         aff = isl_aff_cow(aff);
518         if (!aff)
519                 return NULL;
520
521         aff->v = isl_vec_cow(aff->v);
522         if (!aff->v)
523                 return isl_aff_free(aff);
524
525         isl_int_set(aff->v->el[1], v);
526
527         return aff;
528 }
529
530 /* Replace the constant term of "aff" by "v".
531  */
532 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
533         __isl_take isl_val *v)
534 {
535         if (!aff || !v)
536                 goto error;
537
538         if (!isl_val_is_rat(v))
539                 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
540                         "expecting rational value", goto error);
541
542         if (isl_int_eq(aff->v->el[1], v->n) &&
543             isl_int_eq(aff->v->el[0], v->d)) {
544                 isl_val_free(v);
545                 return aff;
546         }
547
548         aff = isl_aff_cow(aff);
549         if (!aff)
550                 goto error;
551         aff->v = isl_vec_cow(aff->v);
552         if (!aff->v)
553                 goto error;
554
555         if (isl_int_eq(aff->v->el[0], v->d)) {
556                 isl_int_set(aff->v->el[1], v->n);
557         } else if (isl_int_is_one(v->d)) {
558                 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
559         } else {
560                 isl_seq_scale(aff->v->el + 1,
561                                 aff->v->el + 1, v->d, aff->v->size - 1);
562                 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
563                 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
564                 aff->v = isl_vec_normalize(aff->v);
565                 if (!aff->v)
566                         goto error;
567         }
568
569         isl_val_free(v);
570         return aff;
571 error:
572         isl_aff_free(aff);
573         isl_val_free(v);
574         return NULL;
575 }
576
577 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
578 {
579         if (isl_int_is_zero(v))
580                 return aff;
581
582         aff = isl_aff_cow(aff);
583         if (!aff)
584                 return NULL;
585
586         aff->v = isl_vec_cow(aff->v);
587         if (!aff->v)
588                 return isl_aff_free(aff);
589
590         isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
591
592         return aff;
593 }
594
595 /* Add "v" to the constant term of "aff".
596  */
597 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
598         __isl_take isl_val *v)
599 {
600         if (!aff || !v)
601                 goto error;
602
603         if (isl_val_is_zero(v)) {
604                 isl_val_free(v);
605                 return aff;
606         }
607
608         if (!isl_val_is_rat(v))
609                 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
610                         "expecting rational value", goto error);
611
612         aff = isl_aff_cow(aff);
613         if (!aff)
614                 goto error;
615
616         aff->v = isl_vec_cow(aff->v);
617         if (!aff->v)
618                 goto error;
619
620         if (isl_int_is_one(v->d)) {
621                 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
622         } else if (isl_int_eq(aff->v->el[0], v->d)) {
623                 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
624                 aff->v = isl_vec_normalize(aff->v);
625                 if (!aff->v)
626                         goto error;
627         } else {
628                 isl_seq_scale(aff->v->el + 1,
629                                 aff->v->el + 1, v->d, aff->v->size - 1);
630                 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
631                 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
632                 aff->v = isl_vec_normalize(aff->v);
633                 if (!aff->v)
634                         goto error;
635         }
636
637         isl_val_free(v);
638         return aff;
639 error:
640         isl_aff_free(aff);
641         isl_val_free(v);
642         return NULL;
643 }
644
645 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
646 {
647         isl_int t;
648
649         isl_int_init(t);
650         isl_int_set_si(t, v);
651         aff = isl_aff_add_constant(aff, t);
652         isl_int_clear(t);
653
654         return aff;
655 }
656
657 /* Add "v" to the numerator of the constant term of "aff".
658  */
659 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
660 {
661         if (isl_int_is_zero(v))
662                 return aff;
663
664         aff = isl_aff_cow(aff);
665         if (!aff)
666                 return NULL;
667
668         aff->v = isl_vec_cow(aff->v);
669         if (!aff->v)
670                 return isl_aff_free(aff);
671
672         isl_int_add(aff->v->el[1], aff->v->el[1], v);
673
674         return aff;
675 }
676
677 /* Add "v" to the numerator of the constant term of "aff".
678  */
679 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
680 {
681         isl_int t;
682
683         if (v == 0)
684                 return aff;
685
686         isl_int_init(t);
687         isl_int_set_si(t, v);
688         aff = isl_aff_add_constant_num(aff, t);
689         isl_int_clear(t);
690
691         return aff;
692 }
693
694 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
695 {
696         aff = isl_aff_cow(aff);
697         if (!aff)
698                 return NULL;
699
700         aff->v = isl_vec_cow(aff->v);
701         if (!aff->v)
702                 return isl_aff_free(aff);
703
704         isl_int_set_si(aff->v->el[1], v);
705
706         return aff;
707 }
708
709 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
710         enum isl_dim_type type, int pos, isl_int v)
711 {
712         if (!aff)
713                 return NULL;
714
715         if (type == isl_dim_out)
716                 isl_die(aff->v->ctx, isl_error_invalid,
717                         "output/set dimension does not have a coefficient",
718                         return isl_aff_free(aff));
719         if (type == isl_dim_in)
720                 type = isl_dim_set;
721
722         if (pos >= isl_local_space_dim(aff->ls, type))
723                 isl_die(aff->v->ctx, isl_error_invalid,
724                         "position out of bounds", return isl_aff_free(aff));
725
726         aff = isl_aff_cow(aff);
727         if (!aff)
728                 return NULL;
729
730         aff->v = isl_vec_cow(aff->v);
731         if (!aff->v)
732                 return isl_aff_free(aff);
733
734         pos += isl_local_space_offset(aff->ls, type);
735         isl_int_set(aff->v->el[1 + pos], v);
736
737         return aff;
738 }
739
740 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
741         enum isl_dim_type type, int pos, int v)
742 {
743         if (!aff)
744                 return NULL;
745
746         if (type == isl_dim_out)
747                 isl_die(aff->v->ctx, isl_error_invalid,
748                         "output/set dimension does not have a coefficient",
749                         return isl_aff_free(aff));
750         if (type == isl_dim_in)
751                 type = isl_dim_set;
752
753         if (pos >= isl_local_space_dim(aff->ls, type))
754                 isl_die(aff->v->ctx, isl_error_invalid,
755                         "position out of bounds", return isl_aff_free(aff));
756
757         aff = isl_aff_cow(aff);
758         if (!aff)
759                 return NULL;
760
761         aff->v = isl_vec_cow(aff->v);
762         if (!aff->v)
763                 return isl_aff_free(aff);
764
765         pos += isl_local_space_offset(aff->ls, type);
766         isl_int_set_si(aff->v->el[1 + pos], v);
767
768         return aff;
769 }
770
771 /* Replace the coefficient of the variable of type "type" at position "pos"
772  * of "aff" by "v".
773  */
774 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
775         enum isl_dim_type type, int pos, __isl_take isl_val *v)
776 {
777         if (!aff || !v)
778                 goto error;
779
780         if (type == isl_dim_out)
781                 isl_die(aff->v->ctx, isl_error_invalid,
782                         "output/set dimension does not have a coefficient",
783                         goto error);
784         if (type == isl_dim_in)
785                 type = isl_dim_set;
786
787         if (pos >= isl_local_space_dim(aff->ls, type))
788                 isl_die(aff->v->ctx, isl_error_invalid,
789                         "position out of bounds", goto error);
790
791         if (!isl_val_is_rat(v))
792                 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
793                         "expecting rational value", goto error);
794
795         pos += isl_local_space_offset(aff->ls, type);
796         if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
797             isl_int_eq(aff->v->el[0], v->d)) {
798                 isl_val_free(v);
799                 return aff;
800         }
801
802         aff = isl_aff_cow(aff);
803         if (!aff)
804                 goto error;
805         aff->v = isl_vec_cow(aff->v);
806         if (!aff->v)
807                 goto error;
808
809         if (isl_int_eq(aff->v->el[0], v->d)) {
810                 isl_int_set(aff->v->el[1 + pos], v->n);
811         } else if (isl_int_is_one(v->d)) {
812                 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
813         } else {
814                 isl_seq_scale(aff->v->el + 1,
815                                 aff->v->el + 1, v->d, aff->v->size - 1);
816                 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
817                 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
818                 aff->v = isl_vec_normalize(aff->v);
819                 if (!aff->v)
820                         goto error;
821         }
822
823         isl_val_free(v);
824         return aff;
825 error:
826         isl_aff_free(aff);
827         isl_val_free(v);
828         return NULL;
829 }
830
831 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
832         enum isl_dim_type type, int pos, isl_int v)
833 {
834         if (!aff)
835                 return NULL;
836
837         if (type == isl_dim_out)
838                 isl_die(aff->v->ctx, isl_error_invalid,
839                         "output/set dimension does not have a coefficient",
840                         return isl_aff_free(aff));
841         if (type == isl_dim_in)
842                 type = isl_dim_set;
843
844         if (pos >= isl_local_space_dim(aff->ls, type))
845                 isl_die(aff->v->ctx, isl_error_invalid,
846                         "position out of bounds", return isl_aff_free(aff));
847
848         aff = isl_aff_cow(aff);
849         if (!aff)
850                 return NULL;
851
852         aff->v = isl_vec_cow(aff->v);
853         if (!aff->v)
854                 return isl_aff_free(aff);
855
856         pos += isl_local_space_offset(aff->ls, type);
857         isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
858
859         return aff;
860 }
861
862 /* Add "v" to the coefficient of the variable of type "type"
863  * at position "pos" of "aff".
864  */
865 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
866         enum isl_dim_type type, int pos, __isl_take isl_val *v)
867 {
868         if (!aff || !v)
869                 goto error;
870
871         if (isl_val_is_zero(v)) {
872                 isl_val_free(v);
873                 return aff;
874         }
875
876         if (type == isl_dim_out)
877                 isl_die(aff->v->ctx, isl_error_invalid,
878                         "output/set dimension does not have a coefficient",
879                         goto error);
880         if (type == isl_dim_in)
881                 type = isl_dim_set;
882
883         if (pos >= isl_local_space_dim(aff->ls, type))
884                 isl_die(aff->v->ctx, isl_error_invalid,
885                         "position out of bounds", goto error);
886
887         if (!isl_val_is_rat(v))
888                 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
889                         "expecting rational value", goto error);
890
891         aff = isl_aff_cow(aff);
892         if (!aff)
893                 goto error;
894
895         aff->v = isl_vec_cow(aff->v);
896         if (!aff->v)
897                 goto error;
898
899         pos += isl_local_space_offset(aff->ls, type);
900         if (isl_int_is_one(v->d)) {
901                 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
902         } else if (isl_int_eq(aff->v->el[0], v->d)) {
903                 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
904                 aff->v = isl_vec_normalize(aff->v);
905                 if (!aff->v)
906                         goto error;
907         } else {
908                 isl_seq_scale(aff->v->el + 1,
909                                 aff->v->el + 1, v->d, aff->v->size - 1);
910                 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
911                 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
912                 aff->v = isl_vec_normalize(aff->v);
913                 if (!aff->v)
914                         goto error;
915         }
916
917         isl_val_free(v);
918         return aff;
919 error:
920         isl_aff_free(aff);
921         isl_val_free(v);
922         return NULL;
923 }
924
925 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
926         enum isl_dim_type type, int pos, int v)
927 {
928         isl_int t;
929
930         isl_int_init(t);
931         isl_int_set_si(t, v);
932         aff = isl_aff_add_coefficient(aff, type, pos, t);
933         isl_int_clear(t);
934
935         return aff;
936 }
937
938 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
939 {
940         if (!aff)
941                 return NULL;
942
943         return isl_local_space_get_div(aff->ls, pos);
944 }
945
946 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
947 {
948         aff = isl_aff_cow(aff);
949         if (!aff)
950                 return NULL;
951         aff->v = isl_vec_cow(aff->v);
952         if (!aff->v)
953                 return isl_aff_free(aff);
954
955         isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
956
957         return aff;
958 }
959
960 /* Remove divs from the local space that do not appear in the affine
961  * expression.
962  * We currently only remove divs at the end.
963  * Some intermediate divs may also not appear directly in the affine
964  * expression, but we would also need to check that no other divs are
965  * defined in terms of them.
966  */
967 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
968 {
969         int pos;
970         int off;
971         int n;
972
973         if (!aff)
974                 return NULL;
975
976         n = isl_local_space_dim(aff->ls, isl_dim_div);
977         off = isl_local_space_offset(aff->ls, isl_dim_div);
978
979         pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
980         if (pos == n)
981                 return aff;
982
983         aff = isl_aff_cow(aff);
984         if (!aff)
985                 return NULL;
986
987         aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
988         aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
989         if (!aff->ls || !aff->v)
990                 return isl_aff_free(aff);
991
992         return aff;
993 }
994
995 /* Given two affine expressions "p" of length p_len (including the
996  * denominator and the constant term) and "subs" of length subs_len,
997  * plug in "subs" for the variable at position "pos".
998  * The variables of "subs" and "p" are assumed to match up to subs_len,
999  * but "p" may have additional variables.
1000  * "v" is an initialized isl_int that can be used internally.
1001  *
1002  * In particular, if "p" represents the expression
1003  *
1004  *      (a i + g)/m
1005  *
1006  * with i the variable at position "pos" and "subs" represents the expression
1007  *
1008  *      f/d
1009  *
1010  * then the result represents the expression
1011  *
1012  *      (a f + d g)/(m d)
1013  *
1014  */
1015 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
1016         int p_len, int subs_len, isl_int v)
1017 {
1018         isl_int_set(v, p[1 + pos]);
1019         isl_int_set_si(p[1 + pos], 0);
1020         isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
1021         isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
1022         isl_int_mul(p[0], p[0], subs[0]);
1023 }
1024
1025 /* Look for any divs in the aff->ls with a denominator equal to one
1026  * and plug them into the affine expression and any subsequent divs
1027  * that may reference the div.
1028  */
1029 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1030 {
1031         int i, n;
1032         int len;
1033         isl_int v;
1034         isl_vec *vec;
1035         isl_local_space *ls;
1036         unsigned pos;
1037
1038         if (!aff)
1039                 return NULL;
1040
1041         n = isl_local_space_dim(aff->ls, isl_dim_div);
1042         len = aff->v->size;
1043         for (i = 0; i < n; ++i) {
1044                 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1045                         continue;
1046                 ls = isl_local_space_copy(aff->ls);
1047                 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1048                                 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1049                 vec = isl_vec_copy(aff->v);
1050                 vec = isl_vec_cow(vec);
1051                 if (!ls || !vec)
1052                         goto error;
1053
1054                 isl_int_init(v);
1055
1056                 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1057                 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1058                                         len, len, v);
1059
1060                 isl_int_clear(v);
1061
1062                 isl_vec_free(aff->v);
1063                 aff->v = vec;
1064                 isl_local_space_free(aff->ls);
1065                 aff->ls = ls;
1066         }
1067
1068         return aff;
1069 error:
1070         isl_vec_free(vec);
1071         isl_local_space_free(ls);
1072         return isl_aff_free(aff);
1073 }
1074
1075 /* Look for any divs j that appear with a unit coefficient inside
1076  * the definitions of other divs i and plug them into the definitions
1077  * of the divs i.
1078  *
1079  * In particular, an expression of the form
1080  *
1081  *      floor((f(..) + floor(g(..)/n))/m)
1082  *
1083  * is simplified to
1084  *
1085  *      floor((n * f(..) + g(..))/(n * m))
1086  *
1087  * This simplification is correct because we can move the expression
1088  * f(..) into the inner floor in the original expression to obtain
1089  *
1090  *      floor(floor((n * f(..) + g(..))/n)/m)
1091  *
1092  * from which we can derive the simplified expression.
1093  */
1094 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1095 {
1096         int i, j, n;
1097         int off;
1098
1099         if (!aff)
1100                 return NULL;
1101
1102         n = isl_local_space_dim(aff->ls, isl_dim_div);
1103         off = isl_local_space_offset(aff->ls, isl_dim_div);
1104         for (i = 1; i < n; ++i) {
1105                 for (j = 0; j < i; ++j) {
1106                         if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1107                                 continue;
1108                         aff->ls = isl_local_space_substitute_seq(aff->ls,
1109                                 isl_dim_div, j, aff->ls->div->row[j],
1110                                 aff->v->size, i, 1);
1111                         if (!aff->ls)
1112                                 return isl_aff_free(aff);
1113                 }
1114         }
1115
1116         return aff;
1117 }
1118
1119 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1120  *
1121  * Even though this function is only called on isl_affs with a single
1122  * reference, we are careful to only change aff->v and aff->ls together.
1123  */
1124 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1125 {
1126         unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1127         isl_local_space *ls;
1128         isl_vec *v;
1129
1130         ls = isl_local_space_copy(aff->ls);
1131         ls = isl_local_space_swap_div(ls, a, b);
1132         v = isl_vec_copy(aff->v);
1133         v = isl_vec_cow(v);
1134         if (!ls || !v)
1135                 goto error;
1136
1137         isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1138         isl_vec_free(aff->v);
1139         aff->v = v;
1140         isl_local_space_free(aff->ls);
1141         aff->ls = ls;
1142
1143         return aff;
1144 error:
1145         isl_vec_free(v);
1146         isl_local_space_free(ls);
1147         return isl_aff_free(aff);
1148 }
1149
1150 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1151  *
1152  * We currently do not actually remove div "b", but simply add its
1153  * coefficient to that of "a" and then zero it out.
1154  */
1155 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1156 {
1157         unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1158
1159         if (isl_int_is_zero(aff->v->el[1 + off + b]))
1160                 return aff;
1161
1162         aff->v = isl_vec_cow(aff->v);
1163         if (!aff->v)
1164                 return isl_aff_free(aff);
1165
1166         isl_int_add(aff->v->el[1 + off + a],
1167                     aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1168         isl_int_set_si(aff->v->el[1 + off + b], 0);
1169
1170         return aff;
1171 }
1172
1173 /* Sort the divs in the local space of "aff" according to
1174  * the comparison function "cmp_row" in isl_local_space.c,
1175  * combining the coefficients of identical divs.
1176  *
1177  * Reordering divs does not change the semantics of "aff",
1178  * so there is no need to call isl_aff_cow.
1179  * Moreover, this function is currently only called on isl_affs
1180  * with a single reference.
1181  */
1182 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1183 {
1184         int i, j, n;
1185         unsigned off;
1186
1187         if (!aff)
1188                 return NULL;
1189
1190         off = isl_local_space_offset(aff->ls, isl_dim_div);
1191         n = isl_aff_dim(aff, isl_dim_div);
1192         for (i = 1; i < n; ++i) {
1193                 for (j = i - 1; j >= 0; --j) {
1194                         int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1195                         if (cmp < 0)
1196                                 break;
1197                         if (cmp == 0)
1198                                 aff = merge_divs(aff, j, j + 1);
1199                         else
1200                                 aff = swap_div(aff, j, j + 1);
1201                         if (!aff)
1202                                 return NULL;
1203                 }
1204         }
1205
1206         return aff;
1207 }
1208
1209 /* Normalize the representation of "aff".
1210  *
1211  * This function should only be called of "new" isl_affs, i.e.,
1212  * with only a single reference.  We therefore do not need to
1213  * worry about affecting other instances.
1214  */
1215 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1216 {
1217         if (!aff)
1218                 return NULL;
1219         aff->v = isl_vec_normalize(aff->v);
1220         if (!aff->v)
1221                 return isl_aff_free(aff);
1222         aff = plug_in_integral_divs(aff);
1223         aff = plug_in_unit_divs(aff);
1224         aff = sort_divs(aff);
1225         aff = isl_aff_remove_unused_divs(aff);
1226         return aff;
1227 }
1228
1229 /* Given f, return floor(f).
1230  * If f is an integer expression, then just return f.
1231  * If f is a constant, then return the constant floor(f).
1232  * Otherwise, if f = g/m, write g = q m + r,
1233  * create a new div d = [r/m] and return the expression q + d.
1234  * The coefficients in r are taken to lie between -m/2 and m/2.
1235  */
1236 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1237 {
1238         int i;
1239         int size;
1240         isl_ctx *ctx;
1241         isl_vec *div;
1242
1243         if (!aff)
1244                 return NULL;
1245
1246         if (isl_int_is_one(aff->v->el[0]))
1247                 return aff;
1248
1249         aff = isl_aff_cow(aff);
1250         if (!aff)
1251                 return NULL;
1252
1253         aff->v = isl_vec_cow(aff->v);
1254         if (!aff->v)
1255                 return isl_aff_free(aff);
1256
1257         if (isl_aff_is_cst(aff)) {
1258                 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1259                 isl_int_set_si(aff->v->el[0], 1);
1260                 return aff;
1261         }
1262
1263         div = isl_vec_copy(aff->v);
1264         div = isl_vec_cow(div);
1265         if (!div)
1266                 return isl_aff_free(aff);
1267
1268         ctx = isl_aff_get_ctx(aff);
1269         isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1270         for (i = 1; i < aff->v->size; ++i) {
1271                 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1272                 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1273                 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1274                         isl_int_sub(div->el[i], div->el[i], div->el[0]);
1275                         isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1276                 }
1277         }
1278
1279         aff->ls = isl_local_space_add_div(aff->ls, div);
1280         if (!aff->ls)
1281                 return isl_aff_free(aff);
1282
1283         size = aff->v->size;
1284         aff->v = isl_vec_extend(aff->v, size + 1);
1285         if (!aff->v)
1286                 return isl_aff_free(aff);
1287         isl_int_set_si(aff->v->el[0], 1);
1288         isl_int_set_si(aff->v->el[size], 1);
1289
1290         aff = isl_aff_normalize(aff);
1291
1292         return aff;
1293 }
1294
1295 /* Compute
1296  *
1297  *      aff mod m = aff - m * floor(aff/m)
1298  */
1299 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1300 {
1301         isl_aff *res;
1302
1303         res = isl_aff_copy(aff);
1304         aff = isl_aff_scale_down(aff, m);
1305         aff = isl_aff_floor(aff);
1306         aff = isl_aff_scale(aff, m);
1307         res = isl_aff_sub(res, aff);
1308
1309         return res;
1310 }
1311
1312 /* Compute
1313  *
1314  *      aff mod m = aff - m * floor(aff/m)
1315  *
1316  * with m an integer value.
1317  */
1318 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1319         __isl_take isl_val *m)
1320 {
1321         isl_aff *res;
1322
1323         if (!aff || !m)
1324                 goto error;
1325
1326         if (!isl_val_is_int(m))
1327                 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1328                         "expecting integer modulo", goto error);
1329
1330         res = isl_aff_copy(aff);
1331         aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1332         aff = isl_aff_floor(aff);
1333         aff = isl_aff_scale_val(aff, m);
1334         res = isl_aff_sub(res, aff);
1335
1336         return res;
1337 error:
1338         isl_aff_free(aff);
1339         isl_val_free(m);
1340         return NULL;
1341 }
1342
1343 /* Compute
1344  *
1345  *      pwaff mod m = pwaff - m * floor(pwaff/m)
1346  */
1347 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1348 {
1349         isl_pw_aff *res;
1350
1351         res = isl_pw_aff_copy(pwaff);
1352         pwaff = isl_pw_aff_scale_down(pwaff, m);
1353         pwaff = isl_pw_aff_floor(pwaff);
1354         pwaff = isl_pw_aff_scale(pwaff, m);
1355         res = isl_pw_aff_sub(res, pwaff);
1356
1357         return res;
1358 }
1359
1360 /* Given f, return ceil(f).
1361  * If f is an integer expression, then just return f.
1362  * Otherwise, let f be the expression
1363  *
1364  *      e/m
1365  *
1366  * then return
1367  *
1368  *      floor((e + m - 1)/m)
1369  */
1370 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1371 {
1372         if (!aff)
1373                 return NULL;
1374
1375         if (isl_int_is_one(aff->v->el[0]))
1376                 return aff;
1377
1378         aff = isl_aff_cow(aff);
1379         if (!aff)
1380                 return NULL;
1381         aff->v = isl_vec_cow(aff->v);
1382         if (!aff->v)
1383                 return isl_aff_free(aff);
1384
1385         isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1386         isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1387         aff = isl_aff_floor(aff);
1388
1389         return aff;
1390 }
1391
1392 /* Apply the expansion computed by isl_merge_divs.
1393  * The expansion itself is given by "exp" while the resulting
1394  * list of divs is given by "div".
1395  */
1396 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
1397         __isl_take isl_mat *div, int *exp)
1398 {
1399         int i, j;
1400         int old_n_div;
1401         int new_n_div;
1402         int offset;
1403
1404         aff = isl_aff_cow(aff);
1405         if (!aff || !div)
1406                 goto error;
1407
1408         old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1409         new_n_div = isl_mat_rows(div);
1410         if (new_n_div < old_n_div)
1411                 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
1412                         "not an expansion", goto error);
1413
1414         aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
1415         if (!aff->v)
1416                 goto error;
1417
1418         offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1419         j = old_n_div - 1;
1420         for (i = new_n_div - 1; i >= 0; --i) {
1421                 if (j >= 0 && exp[j] == i) {
1422                         if (i != j)
1423                                 isl_int_swap(aff->v->el[offset + i],
1424                                              aff->v->el[offset + j]);
1425                         j--;
1426                 } else
1427                         isl_int_set_si(aff->v->el[offset + i], 0);
1428         }
1429
1430         aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
1431         if (!aff->ls)
1432                 goto error;
1433         isl_mat_free(div);
1434         return aff;
1435 error:
1436         isl_aff_free(aff);
1437         isl_mat_free(div);
1438         return NULL;
1439 }
1440
1441 /* Add two affine expressions that live in the same local space.
1442  */
1443 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1444         __isl_take isl_aff *aff2)
1445 {
1446         isl_int gcd, f;
1447
1448         aff1 = isl_aff_cow(aff1);
1449         if (!aff1 || !aff2)
1450                 goto error;
1451
1452         aff1->v = isl_vec_cow(aff1->v);
1453         if (!aff1->v)
1454                 goto error;
1455
1456         isl_int_init(gcd);
1457         isl_int_init(f);
1458         isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1459         isl_int_divexact(f, aff2->v->el[0], gcd);
1460         isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1461         isl_int_divexact(f, aff1->v->el[0], gcd);
1462         isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1463         isl_int_divexact(f, aff2->v->el[0], gcd);
1464         isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1465         isl_int_clear(f);
1466         isl_int_clear(gcd);
1467
1468         isl_aff_free(aff2);
1469         return aff1;
1470 error:
1471         isl_aff_free(aff1);
1472         isl_aff_free(aff2);
1473         return NULL;
1474 }
1475
1476 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1477         __isl_take isl_aff *aff2)
1478 {
1479         isl_ctx *ctx;
1480         int *exp1 = NULL;
1481         int *exp2 = NULL;
1482         isl_mat *div;
1483
1484         if (!aff1 || !aff2)
1485                 goto error;
1486
1487         ctx = isl_aff_get_ctx(aff1);
1488         if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1489                 isl_die(ctx, isl_error_invalid,
1490                         "spaces don't match", goto error);
1491
1492         if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
1493                 return add_expanded(aff1, aff2);
1494
1495         exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
1496         exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
1497         if (!exp1 || !exp2)
1498                 goto error;
1499
1500         div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1501         aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1502         aff2 = isl_aff_expand_divs(aff2, div, exp2);
1503         free(exp1);
1504         free(exp2);
1505
1506         return add_expanded(aff1, aff2);
1507 error:
1508         free(exp1);
1509         free(exp2);
1510         isl_aff_free(aff1);
1511         isl_aff_free(aff2);
1512         return NULL;
1513 }
1514
1515 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1516         __isl_take isl_aff *aff2)
1517 {
1518         return isl_aff_add(aff1, isl_aff_neg(aff2));
1519 }
1520
1521 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1522 {
1523         isl_int gcd;
1524
1525         if (isl_int_is_one(f))
1526                 return aff;
1527
1528         aff = isl_aff_cow(aff);
1529         if (!aff)
1530                 return NULL;
1531         aff->v = isl_vec_cow(aff->v);
1532         if (!aff->v)
1533                 return isl_aff_free(aff);
1534
1535         if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1536                 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1537                 return aff;
1538         }
1539
1540         isl_int_init(gcd);
1541         isl_int_gcd(gcd, aff->v->el[0], f);
1542         isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1543         isl_int_divexact(gcd, f, gcd);
1544         isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1545         isl_int_clear(gcd);
1546
1547         return aff;
1548 }
1549
1550 /* Multiple "aff" by "v".
1551  */
1552 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1553         __isl_take isl_val *v)
1554 {
1555         if (!aff || !v)
1556                 goto error;
1557
1558         if (isl_val_is_one(v)) {
1559                 isl_val_free(v);
1560                 return aff;
1561         }
1562
1563         if (!isl_val_is_rat(v))
1564                 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1565                         "expecting rational factor", goto error);
1566
1567         aff = isl_aff_scale(aff, v->n);
1568         aff = isl_aff_scale_down(aff, v->d);
1569
1570         isl_val_free(v);
1571         return aff;
1572 error:
1573         isl_aff_free(aff);
1574         isl_val_free(v);
1575         return NULL;
1576 }
1577
1578 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1579 {
1580         isl_int gcd;
1581
1582         if (isl_int_is_one(f))
1583                 return aff;
1584
1585         aff = isl_aff_cow(aff);
1586         if (!aff)
1587                 return NULL;
1588
1589         if (isl_int_is_zero(f))
1590                 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1591                         "cannot scale down by zero", return isl_aff_free(aff));
1592
1593         aff->v = isl_vec_cow(aff->v);
1594         if (!aff->v)
1595                 return isl_aff_free(aff);
1596
1597         isl_int_init(gcd);
1598         isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1599         isl_int_gcd(gcd, gcd, f);
1600         isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1601         isl_int_divexact(gcd, f, gcd);
1602         isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1603         isl_int_clear(gcd);
1604
1605         return aff;
1606 }
1607
1608 /* Divide "aff" by "v".
1609  */
1610 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1611         __isl_take isl_val *v)
1612 {
1613         if (!aff || !v)
1614                 goto error;
1615
1616         if (isl_val_is_one(v)) {
1617                 isl_val_free(v);
1618                 return aff;
1619         }
1620
1621         if (!isl_val_is_rat(v))
1622                 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1623                         "expecting rational factor", goto error);
1624         if (!isl_val_is_pos(v))
1625                 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1626                         "factor needs to be positive", goto error);
1627
1628         aff = isl_aff_scale(aff, v->d);
1629         aff = isl_aff_scale_down(aff, v->n);
1630
1631         isl_val_free(v);
1632         return aff;
1633 error:
1634         isl_aff_free(aff);
1635         isl_val_free(v);
1636         return NULL;
1637 }
1638
1639 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1640 {
1641         isl_int v;
1642
1643         if (f == 1)
1644                 return aff;
1645
1646         isl_int_init(v);
1647         isl_int_set_ui(v, f);
1648         aff = isl_aff_scale_down(aff, v);
1649         isl_int_clear(v);
1650
1651         return aff;
1652 }
1653
1654 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1655         enum isl_dim_type type, unsigned pos, const char *s)
1656 {
1657         aff = isl_aff_cow(aff);
1658         if (!aff)
1659                 return NULL;
1660         if (type == isl_dim_out)
1661                 isl_die(aff->v->ctx, isl_error_invalid,
1662                         "cannot set name of output/set dimension",
1663                         return isl_aff_free(aff));
1664         if (type == isl_dim_in)
1665                 type = isl_dim_set;
1666         aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1667         if (!aff->ls)
1668                 return isl_aff_free(aff);
1669
1670         return aff;
1671 }
1672
1673 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1674         enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1675 {
1676         aff = isl_aff_cow(aff);
1677         if (!aff)
1678                 return isl_id_free(id);
1679         if (type == isl_dim_out)
1680                 isl_die(aff->v->ctx, isl_error_invalid,
1681                         "cannot set name of output/set dimension",
1682                         goto error);
1683         if (type == isl_dim_in)
1684                 type = isl_dim_set;
1685         aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1686         if (!aff->ls)
1687                 return isl_aff_free(aff);
1688
1689         return aff;
1690 error:
1691         isl_id_free(id);
1692         isl_aff_free(aff);
1693         return NULL;
1694 }
1695
1696 /* Exploit the equalities in "eq" to simplify the affine expression
1697  * and the expressions of the integer divisions in the local space.
1698  * The integer divisions in this local space are assumed to appear
1699  * as regular dimensions in "eq".
1700  */
1701 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
1702         __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1703 {
1704         int i, j;
1705         unsigned total;
1706         unsigned n_div;
1707
1708         if (!eq)
1709                 goto error;
1710         if (eq->n_eq == 0) {
1711                 isl_basic_set_free(eq);
1712                 return aff;
1713         }
1714
1715         aff = isl_aff_cow(aff);
1716         if (!aff)
1717                 goto error;
1718
1719         aff->ls = isl_local_space_substitute_equalities(aff->ls,
1720                                                         isl_basic_set_copy(eq));
1721         aff->v = isl_vec_cow(aff->v);
1722         if (!aff->ls || !aff->v)
1723                 goto error;
1724
1725         total = 1 + isl_space_dim(eq->dim, isl_dim_all);
1726         n_div = eq->n_div;
1727         for (i = 0; i < eq->n_eq; ++i) {
1728                 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1729                 if (j < 0 || j == 0 || j >= total)
1730                         continue;
1731
1732                 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
1733                                 &aff->v->el[0]);
1734         }
1735
1736         isl_basic_set_free(eq);
1737         aff = isl_aff_normalize(aff);
1738         return aff;
1739 error:
1740         isl_basic_set_free(eq);
1741         isl_aff_free(aff);
1742         return NULL;
1743 }
1744
1745 /* Exploit the equalities in "eq" to simplify the affine expression
1746  * and the expressions of the integer divisions in the local space.
1747  */
1748 static __isl_give isl_aff *isl_aff_substitute_equalities(
1749         __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1750 {
1751         int n_div;
1752
1753         if (!aff || !eq)
1754                 goto error;
1755         n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1756         if (n_div > 0)
1757                 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
1758         return isl_aff_substitute_equalities_lifted(aff, eq);
1759 error:
1760         isl_basic_set_free(eq);
1761         isl_aff_free(aff);
1762         return NULL;
1763 }
1764
1765 /* Look for equalities among the variables shared by context and aff
1766  * and the integer divisions of aff, if any.
1767  * The equalities are then used to eliminate coefficients and/or integer
1768  * divisions from aff.
1769  */
1770 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1771         __isl_take isl_set *context)
1772 {
1773         isl_basic_set *hull;
1774         int n_div;
1775
1776         if (!aff)
1777                 goto error;
1778         n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1779         if (n_div > 0) {
1780                 isl_basic_set *bset;
1781                 isl_local_space *ls;
1782                 context = isl_set_add_dims(context, isl_dim_set, n_div);
1783                 ls = isl_aff_get_domain_local_space(aff);
1784                 bset = isl_basic_set_from_local_space(ls);
1785                 bset = isl_basic_set_lift(bset);
1786                 bset = isl_basic_set_flatten(bset);
1787                 context = isl_set_intersect(context,
1788                                             isl_set_from_basic_set(bset));
1789         }
1790
1791         hull = isl_set_affine_hull(context);
1792         return isl_aff_substitute_equalities_lifted(aff, hull);
1793 error:
1794         isl_aff_free(aff);
1795         isl_set_free(context);
1796         return NULL;
1797 }
1798
1799 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1800         __isl_take isl_set *context)
1801 {
1802         isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1803         dom_context = isl_set_intersect_params(dom_context, context);
1804         return isl_aff_gist(aff, dom_context);
1805 }
1806
1807 /* Return a basic set containing those elements in the space
1808  * of aff where it is non-negative.
1809  * If "rational" is set, then return a rational basic set.
1810  */
1811 static __isl_give isl_basic_set *aff_nonneg_basic_set(
1812         __isl_take isl_aff *aff, int rational)
1813 {
1814         isl_constraint *ineq;
1815         isl_basic_set *bset;
1816
1817         ineq = isl_inequality_from_aff(aff);
1818
1819         bset = isl_basic_set_from_constraint(ineq);
1820         if (rational)
1821                 bset = isl_basic_set_set_rational(bset);
1822         bset = isl_basic_set_simplify(bset);
1823         return bset;
1824 }
1825
1826 /* Return a basic set containing those elements in the space
1827  * of aff where it is non-negative.
1828  */
1829 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1830 {
1831         return aff_nonneg_basic_set(aff, 0);
1832 }
1833
1834 /* Return a basic set containing those elements in the domain space
1835  * of aff where it is negative.
1836  */
1837 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
1838 {
1839         aff = isl_aff_neg(aff);
1840         aff = isl_aff_add_constant_num_si(aff, -1);
1841         return isl_aff_nonneg_basic_set(aff);
1842 }
1843
1844 /* Return a basic set containing those elements in the space
1845  * of aff where it is zero.
1846  * If "rational" is set, then return a rational basic set.
1847  */
1848 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
1849         int rational)
1850 {
1851         isl_constraint *ineq;
1852         isl_basic_set *bset;
1853
1854         ineq = isl_equality_from_aff(aff);
1855
1856         bset = isl_basic_set_from_constraint(ineq);
1857         if (rational)
1858                 bset = isl_basic_set_set_rational(bset);
1859         bset = isl_basic_set_simplify(bset);
1860         return bset;
1861 }
1862
1863 /* Return a basic set containing those elements in the space
1864  * of aff where it is zero.
1865  */
1866 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1867 {
1868         return aff_zero_basic_set(aff, 0);
1869 }
1870
1871 /* Return a basic set containing those elements in the shared space
1872  * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1873  */
1874 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1875         __isl_take isl_aff *aff2)
1876 {
1877         aff1 = isl_aff_sub(aff1, aff2);
1878
1879         return isl_aff_nonneg_basic_set(aff1);
1880 }
1881
1882 /* Return a basic set containing those elements in the shared space
1883  * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1884  */
1885 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1886         __isl_take isl_aff *aff2)
1887 {
1888         return isl_aff_ge_basic_set(aff2, aff1);
1889 }
1890
1891 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1892         __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1893 {
1894         aff1 = isl_aff_add(aff1, aff2);
1895         aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1896         return aff1;
1897 }
1898
1899 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1900 {
1901         if (!aff)
1902                 return -1;
1903
1904         return 0;
1905 }
1906
1907 /* Check whether the given affine expression has non-zero coefficient
1908  * for any dimension in the given range or if any of these dimensions
1909  * appear with non-zero coefficients in any of the integer divisions
1910  * involved in the affine expression.
1911  */
1912 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1913         enum isl_dim_type type, unsigned first, unsigned n)
1914 {
1915         int i;
1916         isl_ctx *ctx;
1917         int *active = NULL;
1918         int involves = 0;
1919
1920         if (!aff)
1921                 return -1;
1922         if (n == 0)
1923                 return 0;
1924
1925         ctx = isl_aff_get_ctx(aff);
1926         if (first + n > isl_aff_dim(aff, type))
1927                 isl_die(ctx, isl_error_invalid,
1928                         "range out of bounds", return -1);
1929
1930         active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1931         if (!active)
1932                 goto error;
1933
1934         first += isl_local_space_offset(aff->ls, type) - 1;
1935         for (i = 0; i < n; ++i)
1936                 if (active[first + i]) {
1937                         involves = 1;
1938                         break;
1939                 }
1940
1941         free(active);
1942
1943         return involves;
1944 error:
1945         free(active);
1946         return -1;
1947 }
1948
1949 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1950         enum isl_dim_type type, unsigned first, unsigned n)
1951 {
1952         isl_ctx *ctx;
1953
1954         if (!aff)
1955                 return NULL;
1956         if (type == isl_dim_out)
1957                 isl_die(aff->v->ctx, isl_error_invalid,
1958                         "cannot drop output/set dimension",
1959                         return isl_aff_free(aff));
1960         if (type == isl_dim_in)
1961                 type = isl_dim_set;
1962         if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1963                 return aff;
1964
1965         ctx = isl_aff_get_ctx(aff);
1966         if (first + n > isl_local_space_dim(aff->ls, type))
1967                 isl_die(ctx, isl_error_invalid, "range out of bounds",
1968                         return isl_aff_free(aff));
1969
1970         aff = isl_aff_cow(aff);
1971         if (!aff)
1972                 return NULL;
1973
1974         aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1975         if (!aff->ls)
1976                 return isl_aff_free(aff);
1977
1978         first += 1 + isl_local_space_offset(aff->ls, type);
1979         aff->v = isl_vec_drop_els(aff->v, first, n);
1980         if (!aff->v)
1981                 return isl_aff_free(aff);
1982
1983         return aff;
1984 }
1985
1986 /* Project the domain of the affine expression onto its parameter space.
1987  * The affine expression may not involve any of the domain dimensions.
1988  */
1989 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
1990 {
1991         isl_space *space;
1992         unsigned n;
1993         int involves;
1994
1995         n = isl_aff_dim(aff, isl_dim_in);
1996         involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
1997         if (involves < 0)
1998                 return isl_aff_free(aff);
1999         if (involves)
2000                 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2001                     "affine expression involves some of the domain dimensions",
2002                     return isl_aff_free(aff));
2003         aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2004         space = isl_aff_get_domain_space(aff);
2005         space = isl_space_params(space);
2006         aff = isl_aff_reset_domain_space(aff, space);
2007         return aff;
2008 }
2009
2010 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2011         enum isl_dim_type type, unsigned first, unsigned n)
2012 {
2013         isl_ctx *ctx;
2014
2015         if (!aff)
2016                 return NULL;
2017         if (type == isl_dim_out)
2018                 isl_die(aff->v->ctx, isl_error_invalid,
2019                         "cannot insert output/set dimensions",
2020                         return isl_aff_free(aff));
2021         if (type == isl_dim_in)
2022                 type = isl_dim_set;
2023         if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2024                 return aff;
2025
2026         ctx = isl_aff_get_ctx(aff);
2027         if (first > isl_local_space_dim(aff->ls, type))
2028                 isl_die(ctx, isl_error_invalid, "position out of bounds",
2029                         return isl_aff_free(aff));
2030
2031         aff = isl_aff_cow(aff);
2032         if (!aff)
2033                 return NULL;
2034
2035         aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2036         if (!aff->ls)
2037                 return isl_aff_free(aff);
2038
2039         first += 1 + isl_local_space_offset(aff->ls, type);
2040         aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2041         if (!aff->v)
2042                 return isl_aff_free(aff);
2043
2044         return aff;
2045 }
2046
2047 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2048         enum isl_dim_type type, unsigned n)
2049 {
2050         unsigned pos;
2051
2052         pos = isl_aff_dim(aff, type);
2053
2054         return isl_aff_insert_dims(aff, type, pos, n);
2055 }
2056
2057 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2058         enum isl_dim_type type, unsigned n)
2059 {
2060         unsigned pos;
2061
2062         pos = isl_pw_aff_dim(pwaff, type);
2063
2064         return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2065 }
2066
2067 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2068 {
2069         isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2070         return isl_pw_aff_alloc(dom, aff);
2071 }
2072
2073 #undef PW
2074 #define PW isl_pw_aff
2075 #undef EL
2076 #define EL isl_aff
2077 #undef EL_IS_ZERO
2078 #define EL_IS_ZERO is_empty
2079 #undef ZERO
2080 #define ZERO empty
2081 #undef IS_ZERO
2082 #define IS_ZERO is_empty
2083 #undef FIELD
2084 #define FIELD aff
2085 #undef DEFAULT_IS_ZERO
2086 #define DEFAULT_IS_ZERO 0
2087
2088 #define NO_EVAL
2089 #define NO_OPT
2090 #define NO_MOVE_DIMS
2091 #define NO_LIFT
2092 #define NO_MORPH
2093
2094 #include <isl_pw_templ.c>
2095
2096 static __isl_give isl_set *align_params_pw_pw_set_and(
2097         __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2098         __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2099                                     __isl_take isl_pw_aff *pwaff2))
2100 {
2101         if (!pwaff1 || !pwaff2)
2102                 goto error;
2103         if (isl_space_match(pwaff1->dim, isl_dim_param,
2104                           pwaff2->dim, isl_dim_param))
2105                 return fn(pwaff1, pwaff2);
2106         if (!isl_space_has_named_params(pwaff1->dim) ||
2107             !isl_space_has_named_params(pwaff2->dim))
2108                 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2109                         "unaligned unnamed parameters", goto error);
2110         pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2111         pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2112         return fn(pwaff1, pwaff2);
2113 error:
2114         isl_pw_aff_free(pwaff1);
2115         isl_pw_aff_free(pwaff2);
2116         return NULL;
2117 }
2118
2119 /* Compute a piecewise quasi-affine expression with a domain that
2120  * is the union of those of pwaff1 and pwaff2 and such that on each
2121  * cell, the quasi-affine expression is the better (according to cmp)
2122  * of those of pwaff1 and pwaff2.  If only one of pwaff1 or pwaff2
2123  * is defined on a given cell, then the associated expression
2124  * is the defined one.
2125  */
2126 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2127         __isl_take isl_pw_aff *pwaff2,
2128         __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
2129                                         __isl_take isl_aff *aff2))
2130 {
2131         int i, j, n;
2132         isl_pw_aff *res;
2133         isl_ctx *ctx;
2134         isl_set *set;
2135
2136         if (!pwaff1 || !pwaff2)
2137                 goto error;
2138
2139         ctx = isl_space_get_ctx(pwaff1->dim);
2140         if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
2141                 isl_die(ctx, isl_error_invalid,
2142                         "arguments should live in same space", goto error);
2143
2144         if (isl_pw_aff_is_empty(pwaff1)) {
2145                 isl_pw_aff_free(pwaff1);
2146                 return pwaff2;
2147         }
2148
2149         if (isl_pw_aff_is_empty(pwaff2)) {
2150                 isl_pw_aff_free(pwaff2);
2151                 return pwaff1;
2152         }
2153
2154         n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
2155         res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
2156
2157         for (i = 0; i < pwaff1->n; ++i) {
2158                 set = isl_set_copy(pwaff1->p[i].set);
2159                 for (j = 0; j < pwaff2->n; ++j) {
2160                         struct isl_set *common;
2161                         isl_set *better;
2162
2163                         common = isl_set_intersect(
2164                                         isl_set_copy(pwaff1->p[i].set),
2165                                         isl_set_copy(pwaff2->p[j].set));
2166                         better = isl_set_from_basic_set(cmp(
2167                                         isl_aff_copy(pwaff2->p[j].aff),
2168                                         isl_aff_copy(pwaff1->p[i].aff)));
2169                         better = isl_set_intersect(common, better);
2170                         if (isl_set_plain_is_empty(better)) {
2171                                 isl_set_free(better);
2172                                 continue;
2173                         }
2174                         set = isl_set_subtract(set, isl_set_copy(better));
2175
2176                         res = isl_pw_aff_add_piece(res, better,
2177                                                 isl_aff_copy(pwaff2->p[j].aff));
2178                 }
2179                 res = isl_pw_aff_add_piece(res, set,
2180                                                 isl_aff_copy(pwaff1->p[i].aff));
2181         }
2182
2183         for (j = 0; j < pwaff2->n; ++j) {
2184                 set = isl_set_copy(pwaff2->p[j].set);
2185                 for (i = 0; i < pwaff1->n; ++i)
2186                         set = isl_set_subtract(set,
2187                                         isl_set_copy(pwaff1->p[i].set));
2188                 res = isl_pw_aff_add_piece(res, set,
2189                                                 isl_aff_copy(pwaff2->p[j].aff));
2190         }
2191
2192         isl_pw_aff_free(pwaff1);
2193         isl_pw_aff_free(pwaff2);
2194
2195         return res;
2196 error:
2197         isl_pw_aff_free(pwaff1);
2198         isl_pw_aff_free(pwaff2);
2199         return NULL;
2200 }
2201
2202 /* Compute a piecewise quasi-affine expression with a domain that
2203  * is the union of those of pwaff1 and pwaff2 and such that on each
2204  * cell, the quasi-affine expression is the maximum of those of pwaff1
2205  * and pwaff2.  If only one of pwaff1 or pwaff2 is defined on a given
2206  * cell, then the associated expression is the defined one.
2207  */
2208 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2209         __isl_take isl_pw_aff *pwaff2)
2210 {
2211         return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
2212 }
2213
2214 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2215         __isl_take isl_pw_aff *pwaff2)
2216 {
2217         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2218                                                         &pw_aff_union_max);
2219 }
2220
2221 /* Compute a piecewise quasi-affine expression with a domain that
2222  * is the union of those of pwaff1 and pwaff2 and such that on each
2223  * cell, the quasi-affine expression is the minimum of those of pwaff1
2224  * and pwaff2.  If only one of pwaff1 or pwaff2 is defined on a given
2225  * cell, then the associated expression is the defined one.
2226  */
2227 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2228         __isl_take isl_pw_aff *pwaff2)
2229 {
2230         return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
2231 }
2232
2233 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2234         __isl_take isl_pw_aff *pwaff2)
2235 {
2236         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2237                                                         &pw_aff_union_min);
2238 }
2239
2240 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2241         __isl_take isl_pw_aff *pwaff2, int max)
2242 {
2243         if (max)
2244                 return isl_pw_aff_union_max(pwaff1, pwaff2);
2245         else
2246                 return isl_pw_aff_union_min(pwaff1, pwaff2);
2247 }
2248
2249 /* Construct a map with as domain the domain of pwaff and
2250  * one-dimensional range corresponding to the affine expressions.
2251  */
2252 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2253 {
2254         int i;
2255         isl_space *dim;
2256         isl_map *map;
2257
2258         if (!pwaff)
2259                 return NULL;
2260
2261         dim = isl_pw_aff_get_space(pwaff);
2262         map = isl_map_empty(dim);
2263
2264         for (i = 0; i < pwaff->n; ++i) {
2265                 isl_basic_map *bmap;
2266                 isl_map *map_i;
2267
2268                 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2269                 map_i = isl_map_from_basic_map(bmap);
2270                 map_i = isl_map_intersect_domain(map_i,
2271                                                 isl_set_copy(pwaff->p[i].set));
2272                 map = isl_map_union_disjoint(map, map_i);
2273         }
2274
2275         isl_pw_aff_free(pwaff);
2276
2277         return map;
2278 }
2279
2280 /* Construct a map with as domain the domain of pwaff and
2281  * one-dimensional range corresponding to the affine expressions.
2282  */
2283 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2284 {
2285         if (!pwaff)
2286                 return NULL;
2287         if (isl_space_is_set(pwaff->dim))
2288                 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2289                         "space of input is not a map",
2290                         return isl_pw_aff_free(pwaff));
2291         return map_from_pw_aff(pwaff);
2292 }
2293
2294 /* Construct a one-dimensional set with as parameter domain
2295  * the domain of pwaff and the single set dimension
2296  * corresponding to the affine expressions.
2297  */
2298 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2299 {
2300         if (!pwaff)
2301                 return NULL;
2302         if (!isl_space_is_set(pwaff->dim))
2303                 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2304                         "space of input is not a set",
2305                         return isl_pw_aff_free(pwaff));
2306         return map_from_pw_aff(pwaff);
2307 }
2308
2309 /* Return a set containing those elements in the domain
2310  * of pwaff where it is non-negative.
2311  */
2312 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2313 {
2314         int i;
2315         isl_set *set;
2316
2317         if (!pwaff)
2318                 return NULL;
2319
2320         set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2321
2322         for (i = 0; i < pwaff->n; ++i) {
2323                 isl_basic_set *bset;
2324                 isl_set *set_i;
2325                 int rational;
2326
2327                 rational = isl_set_has_rational(pwaff->p[i].set);
2328                 bset = aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff),
2329                                                 rational);
2330                 set_i = isl_set_from_basic_set(bset);
2331                 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
2332                 set = isl_set_union_disjoint(set, set_i);
2333         }
2334
2335         isl_pw_aff_free(pwaff);
2336
2337         return set;
2338 }
2339
2340 /* Return a set containing those elements in the domain
2341  * of pwaff where it is zero (if complement is 0) or not zero
2342  * (if complement is 1).
2343  */
2344 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
2345         int complement)
2346 {
2347         int i;
2348         isl_set *set;
2349
2350         if (!pwaff)
2351                 return NULL;
2352
2353         set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2354
2355         for (i = 0; i < pwaff->n; ++i) {
2356                 isl_basic_set *bset;
2357                 isl_set *set_i, *zero;
2358                 int rational;
2359
2360                 rational = isl_set_has_rational(pwaff->p[i].set);
2361                 bset = aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff),
2362                                                 rational);
2363                 zero = isl_set_from_basic_set(bset);
2364                 set_i = isl_set_copy(pwaff->p[i].set);
2365                 if (complement)
2366                         set_i = isl_set_subtract(set_i, zero);
2367                 else
2368                         set_i = isl_set_intersect(set_i, zero);
2369                 set = isl_set_union_disjoint(set, set_i);
2370         }
2371
2372         isl_pw_aff_free(pwaff);
2373
2374         return set;
2375 }
2376
2377 /* Return a set containing those elements in the domain
2378  * of pwaff where it is zero.
2379  */
2380 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2381 {
2382         return pw_aff_zero_set(pwaff, 0);
2383 }
2384
2385 /* Return a set containing those elements in the domain
2386  * of pwaff where it is not zero.
2387  */
2388 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2389 {
2390         return pw_aff_zero_set(pwaff, 1);
2391 }
2392
2393 /* Return a set containing those elements in the shared domain
2394  * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2395  *
2396  * We compute the difference on the shared domain and then construct
2397  * the set of values where this difference is non-negative.
2398  * If strict is set, we first subtract 1 from the difference.
2399  * If equal is set, we only return the elements where pwaff1 and pwaff2
2400  * are equal.
2401  */
2402 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2403         __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2404 {
2405         isl_set *set1, *set2;
2406
2407         set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2408         set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2409         set1 = isl_set_intersect(set1, set2);
2410         pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2411         pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2412         pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2413
2414         if (strict) {
2415                 isl_space *dim = isl_set_get_space(set1);
2416                 isl_aff *aff;
2417                 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2418                 aff = isl_aff_add_constant_si(aff, -1);
2419                 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2420         } else
2421                 isl_set_free(set1);
2422
2423         if (equal)
2424                 return isl_pw_aff_zero_set(pwaff1);
2425         return isl_pw_aff_nonneg_set(pwaff1);
2426 }
2427
2428 /* Return a set containing those elements in the shared domain
2429  * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2430  */
2431 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2432         __isl_take isl_pw_aff *pwaff2)
2433 {
2434         return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2435 }
2436
2437 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2438         __isl_take isl_pw_aff *pwaff2)
2439 {
2440         return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2441 }
2442
2443 /* Return a set containing those elements in the shared domain
2444  * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2445  */
2446 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2447         __isl_take isl_pw_aff *pwaff2)
2448 {
2449         return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2450 }
2451
2452 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2453         __isl_take isl_pw_aff *pwaff2)
2454 {
2455         return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2456 }
2457
2458 /* Return a set containing those elements in the shared domain
2459  * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2460  */
2461 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2462         __isl_take isl_pw_aff *pwaff2)
2463 {
2464         return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2465 }
2466
2467 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2468         __isl_take isl_pw_aff *pwaff2)
2469 {
2470         return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2471 }
2472
2473 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2474         __isl_take isl_pw_aff *pwaff2)
2475 {
2476         return isl_pw_aff_ge_set(pwaff2, pwaff1);
2477 }
2478
2479 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2480         __isl_take isl_pw_aff *pwaff2)
2481 {
2482         return isl_pw_aff_gt_set(pwaff2, pwaff1);
2483 }
2484
2485 /* Return a set containing those elements in the shared domain
2486  * of the elements of list1 and list2 where each element in list1
2487  * has the relation specified by "fn" with each element in list2.
2488  */
2489 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
2490         __isl_take isl_pw_aff_list *list2,
2491         __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2492                                     __isl_take isl_pw_aff *pwaff2))
2493 {
2494         int i, j;
2495         isl_ctx *ctx;
2496         isl_set *set;
2497
2498         if (!list1 || !list2)
2499                 goto error;
2500
2501         ctx = isl_pw_aff_list_get_ctx(list1);
2502         if (list1->n < 1 || list2->n < 1)
2503                 isl_die(ctx, isl_error_invalid,
2504                         "list should contain at least one element", goto error);
2505
2506         set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
2507         for (i = 0; i < list1->n; ++i)
2508                 for (j = 0; j < list2->n; ++j) {
2509                         isl_set *set_ij;
2510
2511                         set_ij = fn(isl_pw_aff_copy(list1->p[i]),
2512                                     isl_pw_aff_copy(list2->p[j]));
2513                         set = isl_set_intersect(set, set_ij);
2514                 }
2515
2516         isl_pw_aff_list_free(list1);
2517         isl_pw_aff_list_free(list2);
2518         return set;
2519 error:
2520         isl_pw_aff_list_free(list1);
2521         isl_pw_aff_list_free(list2);
2522         return NULL;
2523 }
2524
2525 /* Return a set containing those elements in the shared domain
2526  * of the elements of list1 and list2 where each element in list1
2527  * is equal to each element in list2.
2528  */
2529 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
2530         __isl_take isl_pw_aff_list *list2)
2531 {
2532         return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
2533 }
2534
2535 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
2536         __isl_take isl_pw_aff_list *list2)
2537 {
2538         return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
2539 }
2540
2541 /* Return a set containing those elements in the shared domain
2542  * of the elements of list1 and list2 where each element in list1
2543  * is less than or equal to each element in list2.
2544  */
2545 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
2546         __isl_take isl_pw_aff_list *list2)
2547 {
2548         return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
2549 }
2550
2551 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
2552         __isl_take isl_pw_aff_list *list2)
2553 {
2554         return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
2555 }
2556
2557 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
2558         __isl_take isl_pw_aff_list *list2)
2559 {
2560         return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
2561 }
2562
2563 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
2564         __isl_take isl_pw_aff_list *list2)
2565 {
2566         return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
2567 }
2568
2569
2570 /* Return a set containing those elements in the shared domain
2571  * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
2572  */
2573 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2574         __isl_take isl_pw_aff *pwaff2)
2575 {
2576         isl_set *set_lt, *set_gt;
2577
2578         set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
2579                                    isl_pw_aff_copy(pwaff2));
2580         set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
2581         return isl_set_union_disjoint(set_lt, set_gt);
2582 }
2583
2584 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2585         __isl_take isl_pw_aff *pwaff2)
2586 {
2587         return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
2588 }
2589
2590 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
2591         isl_int v)
2592 {
2593         int i;
2594
2595         if (isl_int_is_one(v))
2596                 return pwaff;
2597         if (!isl_int_is_pos(v))
2598                 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2599                         "factor needs to be positive",
2600                         return isl_pw_aff_free(pwaff));
2601         pwaff = isl_pw_aff_cow(pwaff);
2602         if (!pwaff)
2603                 return NULL;
2604         if (pwaff->n == 0)
2605                 return pwaff;
2606
2607         for (i = 0; i < pwaff->n; ++i) {
2608                 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
2609                 if (!pwaff->p[i].aff)
2610                         return isl_pw_aff_free(pwaff);
2611         }
2612
2613         return pwaff;
2614 }
2615
2616 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
2617 {
2618         int i;
2619
2620         pwaff = isl_pw_aff_cow(pwaff);
2621         if (!pwaff)
2622                 return NULL;
2623         if (pwaff->n == 0)
2624                 return pwaff;
2625
2626         for (i = 0; i < pwaff->n; ++i) {
2627                 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
2628                 if (!pwaff->p[i].aff)
2629                         return isl_pw_aff_free(pwaff);
2630         }
2631
2632         return pwaff;
2633 }
2634
2635 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
2636 {
2637         int i;
2638
2639         pwaff = isl_pw_aff_cow(pwaff);
2640         if (!pwaff)
2641                 return NULL;
2642         if (pwaff->n == 0)
2643                 return pwaff;
2644
2645         for (i = 0; i < pwaff->n; ++i) {
2646                 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
2647                 if (!pwaff->p[i].aff)
2648                         return isl_pw_aff_free(pwaff);
2649         }
2650
2651         return pwaff;
2652 }
2653
2654 /* Assuming that "cond1" and "cond2" are disjoint,
2655  * return an affine expression that is equal to pwaff1 on cond1
2656  * and to pwaff2 on cond2.
2657  */
2658 static __isl_give isl_pw_aff *isl_pw_aff_select(
2659         __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
2660         __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
2661 {
2662         pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
2663         pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
2664
2665         return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
2666 }
2667
2668 /* Return an affine expression that is equal to pwaff_true for elements
2669  * where "cond" is non-zero and to pwaff_false for elements where "cond"
2670  * is zero.
2671  * That is, return cond ? pwaff_true : pwaff_false;
2672  */
2673 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
2674         __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
2675 {
2676         isl_set *cond_true, *cond_false;
2677
2678         cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
2679         cond_false = isl_pw_aff_zero_set(cond);
2680         return isl_pw_aff_select(cond_true, pwaff_true,
2681                                  cond_false, pwaff_false);
2682 }
2683
2684 int isl_aff_is_cst(__isl_keep isl_aff *aff)
2685 {
2686         if (!aff)
2687                 return -1;
2688
2689         return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
2690 }
2691
2692 /* Check whether pwaff is a piecewise constant.
2693  */
2694 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
2695 {
2696         int i;
2697
2698         if (!pwaff)
2699                 return -1;
2700
2701         for (i = 0; i < pwaff->n; ++i) {
2702                 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
2703                 if (is_cst < 0 || !is_cst)
2704                         return is_cst;
2705         }
2706
2707         return 1;
2708 }
2709
2710 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
2711         __isl_take isl_aff *aff2)
2712 {
2713         if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
2714                 return isl_aff_mul(aff2, aff1);
2715
2716         if (!isl_aff_is_cst(aff2))
2717                 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
2718                         "at least one affine expression should be constant",
2719                         goto error);
2720
2721         aff1 = isl_aff_cow(aff1);
2722         if (!aff1 || !aff2)
2723                 goto error;
2724
2725         aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
2726         aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
2727
2728         isl_aff_free(aff2);
2729         return aff1;
2730 error:
2731         isl_aff_free(aff1);
2732         isl_aff_free(aff2);
2733         return NULL;
2734 }
2735
2736 /* Divide "aff1" by "aff2", assuming "aff2" is a piecewise constant.
2737  */
2738 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
2739         __isl_take isl_aff *aff2)
2740 {
2741         int is_cst;
2742         int neg;
2743
2744         is_cst = isl_aff_is_cst(aff2);
2745         if (is_cst < 0)
2746                 goto error;
2747         if (!is_cst)
2748                 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
2749                         "second argument should be a constant", goto error);
2750
2751         if (!aff2)
2752                 goto error;
2753
2754         neg = isl_int_is_neg(aff2->v->el[1]);
2755         if (neg) {
2756                 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2757                 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2758         }
2759
2760         aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
2761         aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
2762
2763         if (neg) {
2764                 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2765                 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2766         }
2767
2768         isl_aff_free(aff2);
2769         return aff1;
2770 error:
2771         isl_aff_free(aff1);
2772         isl_aff_free(aff2);
2773         return NULL;
2774 }
2775
2776 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2777         __isl_take isl_pw_aff *pwaff2)
2778 {
2779         return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
2780 }
2781
2782 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2783         __isl_take isl_pw_aff *pwaff2)
2784 {
2785         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
2786 }
2787
2788 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
2789         __isl_take isl_pw_aff *pwaff2)
2790 {
2791         return isl_pw_aff_union_add_(pwaff1, pwaff2);
2792 }
2793
2794 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2795         __isl_take isl_pw_aff *pwaff2)
2796 {
2797         return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
2798 }
2799
2800 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2801         __isl_take isl_pw_aff *pwaff2)
2802 {
2803         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
2804 }
2805
2806 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
2807         __isl_take isl_pw_aff *pa2)
2808 {
2809         return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
2810 }
2811
2812 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
2813  */
2814 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
2815         __isl_take isl_pw_aff *pa2)
2816 {
2817         int is_cst;
2818
2819         is_cst = isl_pw_aff_is_cst(pa2);
2820         if (is_cst < 0)
2821                 goto error;
2822         if (!is_cst)
2823                 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2824                         "second argument should be a piecewise constant",
2825                         goto error);
2826         return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
2827 error:
2828         isl_pw_aff_free(pa1);
2829         isl_pw_aff_free(pa2);
2830         return NULL;
2831 }
2832
2833 /* Compute the quotient of the integer division of "pa1" by "pa2"
2834  * with rounding towards zero.
2835  * "pa2" is assumed to be a piecewise constant.
2836  *
2837  * In particular, return
2838  *
2839  *      pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
2840  *
2841  */
2842 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
2843         __isl_take isl_pw_aff *pa2)
2844 {
2845         int is_cst;
2846         isl_set *cond;
2847         isl_pw_aff *f, *c;
2848
2849         is_cst = isl_pw_aff_is_cst(pa2);
2850         if (is_cst < 0)
2851                 goto error;
2852         if (!is_cst)
2853                 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2854                         "second argument should be a piecewise constant",
2855                         goto error);
2856
2857         pa1 = isl_pw_aff_div(pa1, pa2);
2858
2859         cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
2860         f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
2861         c = isl_pw_aff_ceil(pa1);
2862         return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
2863 error:
2864         isl_pw_aff_free(pa1);
2865         isl_pw_aff_free(pa2);
2866         return NULL;
2867 }
2868
2869 /* Compute the remainder of the integer division of "pa1" by "pa2"
2870  * with rounding towards zero.
2871  * "pa2" is assumed to be a piecewise constant.
2872  *
2873  * In particular, return
2874  *
2875  *      pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
2876  *
2877  */
2878 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
2879         __isl_take isl_pw_aff *pa2)
2880 {
2881         int is_cst;
2882         isl_pw_aff *res;
2883
2884         is_cst = isl_pw_aff_is_cst(pa2);
2885         if (is_cst < 0)
2886                 goto error;
2887         if (!is_cst)
2888                 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2889                         "second argument should be a piecewise constant",
2890                         goto error);
2891         res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
2892         res = isl_pw_aff_mul(pa2, res);
2893         res = isl_pw_aff_sub(pa1, res);
2894         return res;
2895 error:
2896         isl_pw_aff_free(pa1);
2897         isl_pw_aff_free(pa2);
2898         return NULL;
2899 }
2900
2901 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2902         __isl_take isl_pw_aff *pwaff2)
2903 {
2904         isl_set *le;
2905         isl_set *dom;
2906
2907         dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2908                                 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2909         le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
2910                                 isl_pw_aff_copy(pwaff2));
2911         dom = isl_set_subtract(dom, isl_set_copy(le));
2912         return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
2913 }
2914
2915 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2916         __isl_take isl_pw_aff *pwaff2)
2917 {
2918         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
2919 }
2920
2921 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2922         __isl_take isl_pw_aff *pwaff2)
2923 {
2924         isl_set *ge;
2925         isl_set *dom;
2926
2927         dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2928                                 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2929         ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
2930                                 isl_pw_aff_copy(pwaff2));
2931         dom = isl_set_subtract(dom, isl_set_copy(ge));
2932         return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
2933 }
2934
2935 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2936         __isl_take isl_pw_aff *pwaff2)
2937 {
2938         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
2939 }
2940
2941 static __isl_give isl_pw_aff *pw_aff_list_reduce(
2942         __isl_take isl_pw_aff_list *list,
2943         __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
2944                                         __isl_take isl_pw_aff *pwaff2))
2945 {
2946         int i;
2947         isl_ctx *ctx;
2948         isl_pw_aff *res;
2949
2950         if (!list)
2951                 return NULL;
2952
2953         ctx = isl_pw_aff_list_get_ctx(list);
2954         if (list->n < 1)
2955                 isl_die(ctx, isl_error_invalid,
2956                         "list should contain at least one element",
2957                         return isl_pw_aff_list_free(list));
2958
2959         res = isl_pw_aff_copy(list->p[0]);
2960         for (i = 1; i < list->n; ++i)
2961                 res = fn(res, isl_pw_aff_copy(list->p[i]));
2962
2963         isl_pw_aff_list_free(list);
2964         return res;
2965 }
2966
2967 /* Return an isl_pw_aff that maps each element in the intersection of the
2968  * domains of the elements of list to the minimal corresponding affine
2969  * expression.
2970  */
2971 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
2972 {
2973         return pw_aff_list_reduce(list, &isl_pw_aff_min);
2974 }
2975
2976 /* Return an isl_pw_aff that maps each element in the intersection of the
2977  * domains of the elements of list to the maximal corresponding affine
2978  * expression.
2979  */
2980 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
2981 {
2982         return pw_aff_list_reduce(list, &isl_pw_aff_max);
2983 }
2984
2985 /* Mark the domains of "pwaff" as rational.
2986  */
2987 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
2988 {
2989         int i;
2990
2991         pwaff = isl_pw_aff_cow(pwaff);
2992         if (!pwaff)
2993                 return NULL;
2994         if (pwaff->n == 0)
2995                 return pwaff;
2996
2997         for (i = 0; i < pwaff->n; ++i) {
2998                 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
2999                 if (!pwaff->p[i].set)
3000                         return isl_pw_aff_free(pwaff);
3001         }
3002
3003         return pwaff;
3004 }
3005
3006 /* Mark the domains of the elements of "list" as rational.
3007  */
3008 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3009         __isl_take isl_pw_aff_list *list)
3010 {
3011         int i, n;
3012
3013         if (!list)
3014                 return NULL;
3015         if (list->n == 0)
3016                 return list;
3017
3018         n = list->n;
3019         for (i = 0; i < n; ++i) {
3020                 isl_pw_aff *pa;
3021
3022                 pa = isl_pw_aff_list_get_pw_aff(list, i);
3023                 pa = isl_pw_aff_set_rational(pa);
3024                 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3025         }
3026
3027         return list;
3028 }
3029
3030 /* Check that the domain space of "aff" matches "space".
3031  *
3032  * Return 0 on success and -1 on error.
3033  */
3034 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3035         __isl_keep isl_space *space)
3036 {
3037         isl_space *aff_space;
3038         int match;
3039
3040         if (!aff || !space)
3041                 return -1;
3042
3043         aff_space = isl_aff_get_domain_space(aff);
3044
3045         match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3046         if (match < 0)
3047                 goto error;
3048         if (!match)
3049                 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3050                         "parameters don't match", goto error);
3051         match = isl_space_tuple_match(space, isl_dim_in,
3052                                         aff_space, isl_dim_set);
3053         if (match < 0)
3054                 goto error;
3055         if (!match)
3056                 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3057                         "domains don't match", goto error);
3058         isl_space_free(aff_space);
3059         return 0;
3060 error:
3061         isl_space_free(aff_space);
3062         return -1;
3063 }
3064
3065 #undef BASE
3066 #define BASE aff
3067
3068 #include <isl_multi_templ.c>
3069
3070 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3071  * domain.
3072  */
3073 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
3074         __isl_take isl_multi_aff *ma)
3075 {
3076         isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
3077         return isl_pw_multi_aff_alloc(dom, ma);
3078 }
3079
3080 /* Create a piecewise multi-affine expression in the given space that maps each
3081  * input dimension to the corresponding output dimension.
3082  */
3083 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
3084         __isl_take isl_space *space)
3085 {
3086         return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
3087 }
3088
3089 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
3090         __isl_take isl_multi_aff *maff2)
3091 {
3092         return isl_multi_aff_bin_op(maff1, maff2, &isl_aff_add);
3093 }
3094
3095 /* Subtract "ma2" from "ma1" and return the result.
3096  */
3097 __isl_give isl_multi_aff *isl_multi_aff_sub(__isl_take isl_multi_aff *ma1,
3098         __isl_take isl_multi_aff *ma2)
3099 {
3100         return isl_multi_aff_bin_op(ma1, ma2, &isl_aff_sub);
3101 }
3102
3103 /* Given two multi-affine expressions A -> B and C -> D,
3104  * construct a multi-affine expression [A -> C] -> [B -> D].
3105  */
3106 __isl_give isl_multi_aff *isl_multi_aff_product(
3107         __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
3108 {
3109         int i;
3110         isl_aff *aff;
3111         isl_space *space;
3112         isl_multi_aff *res;
3113         int in1, in2, out1, out2;
3114
3115         in1 = isl_multi_aff_dim(ma1, isl_dim_in);
3116         in2 = isl_multi_aff_dim(ma2, isl_dim_in);
3117         out1 = isl_multi_aff_dim(ma1, isl_dim_out);
3118         out2 = isl_multi_aff_dim(ma2, isl_dim_out);
3119         space = isl_space_product(isl_multi_aff_get_space(ma1),
3120                                   isl_multi_aff_get_space(ma2));
3121         res = isl_multi_aff_alloc(isl_space_copy(space));
3122         space = isl_space_domain(space);
3123
3124         for (i = 0; i < out1; ++i) {
3125                 aff = isl_multi_aff_get_aff(ma1, i);
3126                 aff = isl_aff_insert_dims(aff, isl_dim_in, in1, in2);
3127                 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
3128                 res = isl_multi_aff_set_aff(res, i, aff);
3129         }
3130
3131         for (i = 0; i < out2; ++i) {
3132                 aff = isl_multi_aff_get_aff(ma2, i);
3133                 aff = isl_aff_insert_dims(aff, isl_dim_in, 0, in1);
3134                 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
3135                 res = isl_multi_aff_set_aff(res, out1 + i, aff);
3136         }
3137
3138         isl_space_free(space);
3139         isl_multi_aff_free(ma1);
3140         isl_multi_aff_free(ma2);
3141         return res;
3142 }
3143
3144 /* Exploit the equalities in "eq" to simplify the affine expressions.
3145  */
3146 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
3147         __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
3148 {
3149         int i;
3150
3151         maff = isl_multi_aff_cow(maff);
3152         if (!maff || !eq)
3153                 goto error;
3154
3155         for (i = 0; i < maff->n; ++i) {
3156                 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
3157                                                     isl_basic_set_copy(eq));
3158                 if (!maff->p[i])
3159                         goto error;
3160         }
3161
3162         isl_basic_set_free(eq);
3163         return maff;
3164 error:
3165         isl_basic_set_free(eq);
3166         isl_multi_aff_free(maff);
3167         return NULL;
3168 }
3169
3170 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
3171         isl_int f)
3172 {
3173         int i;
3174
3175         maff = isl_multi_aff_cow(maff);
3176         if (!maff)
3177                 return NULL;
3178
3179         for (i = 0; i < maff->n; ++i) {
3180                 maff->p[i] = isl_aff_scale(maff->p[i], f);
3181                 if (!maff->p[i])
3182                         return isl_multi_aff_free(maff);
3183         }
3184
3185         return maff;
3186 }
3187
3188 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
3189         __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3190 {
3191         maff1 = isl_multi_aff_add(maff1, maff2);
3192         maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
3193         return maff1;
3194 }
3195
3196 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
3197 {
3198         if (!maff)
3199                 return -1;
3200
3201         return 0;
3202 }
3203
3204 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
3205         __isl_keep isl_multi_aff *maff2)
3206 {
3207         int i;
3208         int equal;
3209
3210         if (!maff1 || !maff2)
3211                 return -1;
3212         if (maff1->n != maff2->n)
3213                 return 0;
3214         equal = isl_space_is_equal(maff1->space, maff2->space);
3215         if (equal < 0 || !equal)
3216                 return equal;
3217
3218         for (i = 0; i < maff1->n; ++i) {
3219                 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
3220                 if (equal < 0 || !equal)
3221                         return equal;
3222         }
3223
3224         return 1;
3225 }
3226
3227 /* Return the set of domain elements where "ma1" is lexicographically
3228  * smaller than or equal to "ma2".
3229  */
3230 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
3231         __isl_take isl_multi_aff *ma2)
3232 {
3233         return isl_multi_aff_lex_ge_set(ma2, ma1);
3234 }
3235
3236 /* Return the set of domain elements where "ma1" is lexicographically
3237  * greater than or equal to "ma2".
3238  */
3239 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
3240         __isl_take isl_multi_aff *ma2)
3241 {
3242         isl_space *space;
3243         isl_map *map1, *map2;
3244         isl_map *map, *ge;
3245
3246         map1 = isl_map_from_multi_aff(ma1);
3247         map2 = isl_map_from_multi_aff(ma2);
3248         map = isl_map_range_product(map1, map2);
3249         space = isl_space_range(isl_map_get_space(map));
3250         space = isl_space_domain(isl_space_unwrap(space));
3251         ge = isl_map_lex_ge(space);
3252         map = isl_map_intersect_range(map, isl_map_wrap(ge));
3253
3254         return isl_map_domain(map);
3255 }
3256
3257 #undef PW
3258 #define PW isl_pw_multi_aff
3259 #undef EL
3260 #define EL isl_multi_aff
3261 #undef EL_IS_ZERO
3262 #define EL_IS_ZERO is_empty
3263 #undef ZERO
3264 #define ZERO empty
3265 #undef IS_ZERO
3266 #define IS_ZERO is_empty
3267 #undef FIELD
3268 #define FIELD maff
3269 #undef DEFAULT_IS_ZERO
3270 #define DEFAULT_IS_ZERO 0
3271
3272 #define NO_NEG
3273 #define NO_EVAL
3274 #define NO_OPT
3275 #define NO_INVOLVES_DIMS
3276 #define NO_MOVE_DIMS
3277 #define NO_INSERT_DIMS
3278 #define NO_LIFT
3279 #define NO_MORPH
3280
3281 #include <isl_pw_templ.c>
3282
3283 #undef UNION
3284 #define UNION isl_union_pw_multi_aff
3285 #undef PART
3286 #define PART isl_pw_multi_aff
3287 #undef PARTS
3288 #define PARTS pw_multi_aff
3289 #define ALIGN_DOMAIN
3290
3291 #define NO_EVAL
3292
3293 #include <isl_union_templ.c>
3294
3295 /* Given a function "cmp" that returns the set of elements where
3296  * "ma1" is "better" than "ma2", return the intersection of this
3297  * set with "dom1" and "dom2".
3298  */
3299 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
3300         __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
3301         __isl_keep isl_multi_aff *ma2,
3302         __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3303                                     __isl_take isl_multi_aff *ma2))
3304 {
3305         isl_set *common;
3306         isl_set *better;
3307         int is_empty;
3308
3309         common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
3310         is_empty = isl_set_plain_is_empty(common);
3311         if (is_empty >= 0 && is_empty)
3312                 return common;
3313         if (is_empty < 0)
3314                 return isl_set_free(common);
3315         better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
3316         better = isl_set_intersect(common, better);
3317
3318         return better;
3319 }
3320
3321 /* Given a function "cmp" that returns the set of elements where
3322  * "ma1" is "better" than "ma2", return a piecewise multi affine
3323  * expression defined on the union of the definition domains
3324  * of "pma1" and "pma2" that maps to the "best" of "pma1" and
3325  * "pma2" on each cell.  If only one of the two input functions
3326  * is defined on a given cell, then it is considered the best.
3327  */
3328 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
3329         __isl_take isl_pw_multi_aff *pma1,
3330         __isl_take isl_pw_multi_aff *pma2,
3331         __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3332                                     __isl_take isl_multi_aff *ma2))
3333 {
3334         int i, j, n;
3335         isl_pw_multi_aff *res = NULL;
3336         isl_ctx *ctx;
3337         isl_set *set = NULL;
3338
3339         if (!pma1 || !pma2)
3340                 goto error;
3341
3342         ctx = isl_space_get_ctx(pma1->dim);
3343         if (!isl_space_is_equal(pma1->dim, pma2->dim))
3344                 isl_die(ctx, isl_error_invalid,
3345                         "arguments should live in the same space", goto error);
3346
3347         if (isl_pw_multi_aff_is_empty(pma1)) {
3348                 isl_pw_multi_aff_free(pma1);
3349                 return pma2;
3350         }
3351
3352         if (isl_pw_multi_aff_is_empty(pma2)) {
3353                 isl_pw_multi_aff_free(pma2);
3354                 return pma1;
3355         }
3356
3357         n = 2 * (pma1->n + 1) * (pma2->n + 1);
3358         res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
3359
3360         for (i = 0; i < pma1->n; ++i) {
3361                 set = isl_set_copy(pma1->p[i].set);
3362                 for (j = 0; j < pma2->n; ++j) {
3363                         isl_set *better;
3364                         int is_empty;
3365
3366                         better = shared_and_better(pma2->p[j].set,
3367                                         pma1->p[i].set, pma2->p[j].maff,
3368                                         pma1->p[i].maff, cmp);
3369                         is_empty = isl_set_plain_is_empty(better);
3370                         if (is_empty < 0 || is_empty) {
3371                                 isl_set_free(better);
3372                                 if (is_empty < 0)
3373                                         goto error;
3374                                 continue;
3375                         }
3376                         set = isl_set_subtract(set, isl_set_copy(better));
3377
3378                         res = isl_pw_multi_aff_add_piece(res, better,
3379                                         isl_multi_aff_copy(pma2->p[j].maff));
3380                 }
3381                 res = isl_pw_multi_aff_add_piece(res, set,
3382                                         isl_multi_aff_copy(pma1->p[i].maff));
3383         }
3384
3385         for (j = 0; j < pma2->n; ++j) {
3386                 set = isl_set_copy(pma2->p[j].set);
3387                 for (i = 0; i < pma1->n; ++i)
3388                         set = isl_set_subtract(set,
3389                                         isl_set_copy(pma1->p[i].set));
3390                 res = isl_pw_multi_aff_add_piece(res, set,
3391                                         isl_multi_aff_copy(pma2->p[j].maff));
3392         }
3393
3394         isl_pw_multi_aff_free(pma1);
3395         isl_pw_multi_aff_free(pma2);
3396
3397         return res;
3398 error:
3399         isl_pw_multi_aff_free(pma1);
3400         isl_pw_multi_aff_free(pma2);
3401         isl_set_free(set);
3402         return isl_pw_multi_aff_free(res);
3403 }
3404
3405 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
3406         __isl_take isl_pw_multi_aff *pma1,
3407         __isl_take isl_pw_multi_aff *pma2)
3408 {
3409         return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
3410 }
3411
3412 /* Given two piecewise multi affine expressions, return a piecewise
3413  * multi-affine expression defined on the union of the definition domains
3414  * of the inputs that is equal to the lexicographic maximum of the two
3415  * inputs on each cell.  If only one of the two inputs is defined on
3416  * a given cell, then it is considered to be the maximum.
3417  */
3418 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
3419         __isl_take isl_pw_multi_aff *pma1,
3420         __isl_take isl_pw_multi_aff *pma2)
3421 {
3422         return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3423                                                     &pw_multi_aff_union_lexmax);
3424 }
3425
3426 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
3427         __isl_take isl_pw_multi_aff *pma1,
3428         __isl_take isl_pw_multi_aff *pma2)
3429 {
3430         return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
3431 }
3432
3433 /* Given two piecewise multi affine expressions, return a piecewise
3434  * multi-affine expression defined on the union of the definition domains
3435  * of the inputs that is equal to the lexicographic minimum of the two
3436  * inputs on each cell.  If only one of the two inputs is defined on
3437  * a given cell, then it is considered to be the minimum.
3438  */
3439 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
3440         __isl_take isl_pw_multi_aff *pma1,
3441         __isl_take isl_pw_multi_aff *pma2)
3442 {
3443         return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3444                                                     &pw_multi_aff_union_lexmin);
3445 }
3446
3447 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
3448         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3449 {
3450         return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3451                                                 &isl_multi_aff_add);
3452 }
3453
3454 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
3455         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3456 {
3457         return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3458                                                 &pw_multi_aff_add);
3459 }
3460
3461 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
3462         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3463 {
3464         return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3465                                                 &isl_multi_aff_sub);
3466 }
3467
3468 /* Subtract "pma2" from "pma1" and return the result.
3469  */
3470 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
3471         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3472 {
3473         return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3474                                                 &pw_multi_aff_sub);
3475 }
3476
3477 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
3478         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3479 {
3480         return isl_pw_multi_aff_union_add_(pma1, pma2);
3481 }
3482
3483 /* Given two piecewise multi-affine expressions A -> B and C -> D,
3484  * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
3485  */
3486 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
3487         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3488 {
3489         int i, j, n;
3490         isl_space *space;
3491         isl_pw_multi_aff *res;
3492
3493         if (!pma1 || !pma2)
3494                 goto error;
3495
3496         n = pma1->n * pma2->n;
3497         space = isl_space_product(isl_space_copy(pma1->dim),
3498                                   isl_space_copy(pma2->dim));
3499         res = isl_pw_multi_aff_alloc_size(space, n);
3500
3501         for (i = 0; i < pma1->n; ++i) {
3502                 for (j = 0; j < pma2->n; ++j) {
3503                         isl_set *domain;
3504                         isl_multi_aff *ma;
3505
3506                         domain = isl_set_product(isl_set_copy(pma1->p[i].set),
3507                                                  isl_set_copy(pma2->p[j].set));
3508                         ma = isl_multi_aff_product(
3509                                         isl_multi_aff_copy(pma1->p[i].maff),
3510                                         isl_multi_aff_copy(pma2->p[i].maff));
3511                         res = isl_pw_multi_aff_add_piece(res, domain, ma);
3512                 }
3513         }
3514
3515         isl_pw_multi_aff_free(pma1);
3516         isl_pw_multi_aff_free(pma2);
3517         return res;
3518 error:
3519         isl_pw_multi_aff_free(pma1);
3520         isl_pw_multi_aff_free(pma2);
3521         return NULL;
3522 }
3523
3524 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
3525         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3526 {
3527         return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3528                                                 &pw_multi_aff_product);
3529 }
3530
3531 /* Construct a map mapping the domain of the piecewise multi-affine expression
3532  * to its range, with each dimension in the range equated to the
3533  * corresponding affine expression on its cell.
3534  */
3535 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3536 {
3537         int i;
3538         isl_map *map;
3539
3540         if (!pma)
3541                 return NULL;
3542
3543         map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
3544
3545         for (i = 0; i < pma->n; ++i) {
3546                 isl_multi_aff *maff;
3547                 isl_basic_map *bmap;
3548                 isl_map *map_i;
3549
3550                 maff = isl_multi_aff_copy(pma->p[i].maff);
3551                 bmap = isl_basic_map_from_multi_aff(maff);
3552                 map_i = isl_map_from_basic_map(bmap);
3553                 map_i = isl_map_intersect_domain(map_i,
3554                                                 isl_set_copy(pma->p[i].set));
3555                 map = isl_map_union_disjoint(map, map_i);
3556         }
3557
3558         isl_pw_multi_aff_free(pma);
3559         return map;
3560 }
3561
3562 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3563 {
3564         if (!pma)
3565                 return NULL;
3566
3567         if (!isl_space_is_set(pma->dim))
3568                 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
3569                         "isl_pw_multi_aff cannot be converted into an isl_set",
3570                         return isl_pw_multi_aff_free(pma));
3571
3572         return isl_map_from_pw_multi_aff(pma);
3573 }
3574
3575 /* Given a basic map with a single output dimension that is defined
3576  * in terms of the parameters and input dimensions using an equality,
3577  * extract an isl_aff that expresses the output dimension in terms
3578  * of the parameters and input dimensions.
3579  *
3580  * Since some applications expect the result of isl_pw_multi_aff_from_map
3581  * to only contain integer affine expressions, we compute the floor
3582  * of the expression before returning.
3583  *
3584  * This function shares some similarities with
3585  * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
3586  */
3587 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
3588         __isl_take isl_basic_map *bmap)
3589 {
3590         int i;
3591         unsigned offset;
3592         unsigned total;
3593         isl_local_space *ls;
3594         isl_aff *aff;
3595
3596         if (!bmap)
3597                 return NULL;
3598         if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
3599                 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3600                         "basic map should have a single output dimension",
3601                         goto error);
3602         offset = isl_basic_map_offset(bmap, isl_dim_out);
3603         total = isl_basic_map_total_dim(bmap);
3604         for (i = 0; i < bmap->n_eq; ++i) {
3605                 if (isl_int_is_zero(bmap->eq[i][offset]))
3606                         continue;
3607                 if (isl_seq_first_non_zero(bmap->eq[i] + offset + 1,
3608                                            1 + total - (offset + 1)) != -1)
3609                         continue;
3610                 break;
3611         }
3612         if (i >= bmap->n_eq)
3613                 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3614                         "unable to find suitable equality", goto error);
3615         ls = isl_basic_map_get_local_space(bmap);
3616         aff = isl_aff_alloc(isl_local_space_domain(ls));
3617         if (!aff)
3618                 goto error;
3619         if (isl_int_is_neg(bmap->eq[i][offset]))
3620                 isl_seq_cpy(aff->v->el + 1, bmap->eq[i], offset);
3621         else
3622                 isl_seq_neg(aff->v->el + 1, bmap->eq[i], offset);
3623         isl_seq_clr(aff->v->el + 1 + offset, aff->v->size - (1 + offset));
3624         isl_int_abs(aff->v->el[0], bmap->eq[i][offset]);
3625         isl_basic_map_free(bmap);
3626
3627         aff = isl_aff_remove_unused_divs(aff);
3628         aff = isl_aff_floor(aff);
3629         return aff;
3630 error:
3631         isl_basic_map_free(bmap);
3632         return NULL;
3633 }
3634
3635 /* Given a basic map where each output dimension is defined
3636  * in terms of the parameters and input dimensions using an equality,
3637  * extract an isl_multi_aff that expresses the output dimensions in terms
3638  * of the parameters and input dimensions.
3639  */
3640 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
3641         __isl_take isl_basic_map *bmap)
3642 {
3643         int i;
3644         unsigned n_out;
3645         isl_multi_aff *ma;
3646
3647         if (!bmap)
3648                 return NULL;
3649
3650         ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
3651         n_out = isl_basic_map_dim(bmap, isl_dim_out);
3652
3653         for (i = 0; i < n_out; ++i) {
3654                 isl_basic_map *bmap_i;
3655                 isl_aff *aff;
3656
3657                 bmap_i = isl_basic_map_copy(bmap);
3658                 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
3659                                                         i + 1, n_out - (1 + i));
3660                 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
3661                 aff = extract_isl_aff_from_basic_map(bmap_i);
3662                 ma = isl_multi_aff_set_aff(ma, i, aff);
3663         }
3664
3665         isl_basic_map_free(bmap);
3666
3667         return ma;
3668 }
3669
3670 /* Create an isl_pw_multi_aff that is equivalent to
3671  * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
3672  * The given basic map is such that each output dimension is defined
3673  * in terms of the parameters and input dimensions using an equality.
3674  */
3675 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
3676         __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
3677 {
3678         isl_multi_aff *ma;
3679
3680         ma = extract_isl_multi_aff_from_basic_map(bmap);
3681         return isl_pw_multi_aff_alloc(domain, ma);
3682 }
3683
3684 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3685  * This obviously only works if the input "map" is single-valued.
3686  * If so, we compute the lexicographic minimum of the image in the form
3687  * of an isl_pw_multi_aff.  Since the image is unique, it is equal
3688  * to its lexicographic minimum.
3689  * If the input is not single-valued, we produce an error.
3690  */
3691 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
3692         __isl_take isl_map *map)
3693 {
3694         int i;
3695         int sv;
3696         isl_pw_multi_aff *pma;
3697
3698         sv = isl_map_is_single_valued(map);
3699         if (sv < 0)
3700                 goto error;
3701         if (!sv)
3702                 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3703                         "map is not single-valued", goto error);
3704         map = isl_map_make_disjoint(map);
3705         if (!map)
3706                 return NULL;
3707
3708         pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
3709
3710         for (i = 0; i < map->n; ++i) {
3711                 isl_pw_multi_aff *pma_i;
3712                 isl_basic_map *bmap;
3713                 bmap = isl_basic_map_copy(map->p[i]);
3714                 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
3715                 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
3716         }
3717
3718         isl_map_free(map);
3719         return pma;
3720 error:
3721         isl_map_free(map);
3722         return NULL;
3723 }
3724
3725 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
3726  * taking into account that the output dimension at position "d"
3727  * can be represented as
3728  *
3729  *      x = floor((e(...) + c1) / m)
3730  *
3731  * given that constraint "i" is of the form
3732  *
3733  *      e(...) + c1 - m x >= 0
3734  *
3735  *
3736  * Let "map" be of the form
3737  *
3738  *      A -> B
3739  *
3740  * We construct a mapping
3741  *
3742  *      A -> [A -> x = floor(...)]
3743  *
3744  * apply that to the map, obtaining
3745  *
3746  *      [A -> x = floor(...)] -> B
3747  *
3748  * and equate dimension "d" to x.
3749  * We then compute a isl_pw_multi_aff representation of the resulting map
3750  * and plug in the mapping above.
3751  */
3752 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
3753         __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
3754 {
3755         isl_ctx *ctx;
3756         isl_space *space;
3757         isl_local_space *ls;
3758         isl_multi_aff *ma;
3759         isl_aff *aff;
3760         isl_vec *v;
3761         isl_map *insert;
3762         int offset;
3763         int n;
3764         int n_in;
3765         isl_pw_multi_aff *pma;
3766         int is_set;
3767
3768         is_set = isl_map_is_set(map);
3769
3770         offset = isl_basic_map_offset(hull, isl_dim_out);
3771         ctx = isl_map_get_ctx(map);
3772         space = isl_space_domain(isl_map_get_space(map));
3773         n_in = isl_space_dim(space, isl_dim_set);
3774         n = isl_space_dim(space, isl_dim_all);
3775
3776         v = isl_vec_alloc(ctx, 1 + 1 + n);
3777         if (v) {
3778                 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
3779                 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
3780         }
3781         isl_basic_map_free(hull);
3782
3783         ls = isl_local_space_from_space(isl_space_copy(space));
3784         aff = isl_aff_alloc_vec(ls, v);
3785         aff = isl_aff_floor(aff);
3786         if (is_set) {
3787                 isl_space_free(space);
3788                 ma = isl_multi_aff_from_aff(aff);
3789         } else {
3790                 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
3791                 ma = isl_multi_aff_range_product(ma,
3792                                                 isl_multi_aff_from_aff(aff));
3793         }
3794
3795         insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
3796         map = isl_map_apply_domain(map, insert);
3797         map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
3798         pma = isl_pw_multi_aff_from_map(map);
3799         pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
3800
3801         return pma;
3802 }
3803
3804 /* Is constraint "c" of the form
3805  *
3806  *      e(...) + c1 - m x >= 0
3807  *
3808  * or
3809  *
3810  *      -e(...) + c2 + m x >= 0
3811  *
3812  * where m > 1 and e only depends on parameters and input dimemnsions?
3813  *
3814  * "offset" is the offset of the output dimensions
3815  * "pos" is the position of output dimension x.
3816  */
3817 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
3818 {
3819         if (isl_int_is_zero(c[offset + d]))
3820                 return 0;
3821         if (isl_int_is_one(c[offset + d]))
3822                 return 0;
3823         if (isl_int_is_negone(c[offset + d]))
3824                 return 0;
3825         if (isl_seq_first_non_zero(c + offset, d) != -1)
3826                 return 0;
3827         if (isl_seq_first_non_zero(c + offset + d + 1,
3828                                     total - (offset + d + 1)) != -1)
3829                 return 0;
3830         return 1;
3831 }
3832
3833 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3834  *
3835  * As a special case, we first check if there is any pair of constraints,
3836  * shared by all the basic maps in "map" that force a given dimension
3837  * to be equal to the floor of some affine combination of the input dimensions.
3838  *
3839  * In particular, if we can find two constraints
3840  *
3841  *      e(...) + c1 - m x >= 0          i.e.,           m x <= e(...) + c1
3842  *
3843  * and
3844  *
3845  *      -e(...) + c2 + m x >= 0         i.e.,           m x >= e(...) - c2
3846  *
3847  * where m > 1 and e only depends on parameters and input dimemnsions,
3848  * and such that
3849  *
3850  *      c1 + c2 < m                     i.e.,           -c2 >= c1 - (m - 1)
3851  *
3852  * then we know that we can take
3853  *
3854  *      x = floor((e(...) + c1) / m)
3855  *
3856  * without having to perform any computation.
3857  *
3858  * Note that we know that
3859  *
3860  *      c1 + c2 >= 1
3861  *
3862  * If c1 + c2 were 0, then we would have detected an equality during
3863  * simplification.  If c1 + c2 were negative, then we would have detected
3864  * a contradiction.
3865  */
3866 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
3867         __isl_take isl_map *map)
3868 {
3869         int d, dim;
3870         int i, j, n;
3871         int offset, total;
3872         isl_int sum;
3873         isl_basic_map *hull;
3874
3875         hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
3876         if (!hull)
3877                 goto error;
3878
3879         isl_int_init(sum);
3880         dim = isl_map_dim(map, isl_dim_out);
3881         offset = isl_basic_map_offset(hull, isl_dim_out);
3882         total = 1 + isl_basic_map_total_dim(hull);
3883         n = hull->n_ineq;
3884         for (d = 0; d < dim; ++d) {
3885                 for (i = 0; i < n; ++i) {
3886                         if (!is_potential_div_constraint(hull->ineq[i],
3887                                                         offset, d, total))
3888                                 continue;
3889                         for (j = i + 1; j < n; ++j) {
3890                                 if (!isl_seq_is_neg(hull->ineq[i] + 1,
3891                                                 hull->ineq[j] + 1, total - 1))
3892                                         continue;
3893                                 isl_int_add(sum, hull->ineq[i][0],
3894                                                 hull->ineq[j][0]);
3895                                 if (isl_int_abs_lt(sum,
3896                                                     hull->ineq[i][offset + d]))
3897                                         break;
3898
3899                         }
3900                         if (j >= n)
3901                                 continue;
3902                         isl_int_clear(sum);
3903                         if (isl_int_is_pos(hull->ineq[j][offset + d]))
3904                                 j = i;
3905                         return pw_multi_aff_from_map_div(map, hull, d, j);
3906                 }
3907         }
3908         isl_int_clear(sum);
3909         isl_basic_map_free(hull);
3910         return pw_multi_aff_from_map_base(map);
3911 error:
3912         isl_map_free(map);
3913         isl_basic_map_free(hull);
3914         return NULL;
3915 }
3916
3917 /* Given an affine expression
3918  *
3919  *      [A -> B] -> f(A,B)
3920  *
3921  * construct an isl_multi_aff
3922  *
3923  *      [A -> B] -> B'
3924  *
3925  * such that dimension "d" in B' is set to "aff" and the remaining
3926  * dimensions are set equal to the corresponding dimensions in B.
3927  * "n_in" is the dimension of the space A.
3928  * "n_out" is the dimension of the space B.
3929  *
3930  * If "is_set" is set, then the affine expression is of the form
3931  *
3932  *      [B] -> f(B)
3933  *
3934  * and we construct an isl_multi_aff
3935  *
3936  *      B -> B'
3937  */
3938 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
3939         unsigned n_in, unsigned n_out, int is_set)
3940 {
3941         int i;
3942         isl_multi_aff *ma;
3943         isl_space *space, *space2;
3944         isl_local_space *ls;
3945
3946         space = isl_aff_get_domain_space(aff);
3947         ls = isl_local_space_from_space(isl_space_copy(space));
3948         space2 = isl_space_copy(space);
3949         if (!is_set)
3950                 space2 = isl_space_range(isl_space_unwrap(space2));
3951         space = isl_space_map_from_domain_and_range(space, space2);
3952         ma = isl_multi_aff_alloc(space);
3953         ma = isl_multi_aff_set_aff(ma, d, aff);
3954
3955         for (i = 0; i < n_out; ++i) {
3956                 if (i == d)
3957                         continue;
3958                 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3959                                                 isl_dim_set, n_in + i);
3960                 ma = isl_multi_aff_set_aff(ma, i, aff);
3961         }
3962
3963         isl_local_space_free(ls);
3964
3965         return ma;
3966 }
3967
3968 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
3969  * taking into account that the dimension at position "d" can be written as
3970  *
3971  *      x = m a + f(..)                                         (1)
3972  *
3973  * where m is equal to "gcd".
3974  * "i" is the index of the equality in "hull" that defines f(..).
3975  * In particular, the equality is of the form
3976  *
3977  *      f(..) - x + m g(existentials) = 0
3978  *
3979  * or
3980  *
3981  *      -f(..) + x + m g(existentials) = 0
3982  *
3983  * We basically plug (1) into "map", resulting in a map with "a"
3984  * in the range instead of "x".  The corresponding isl_pw_multi_aff
3985  * defining "a" is then plugged back into (1) to obtain a definition fro "x".
3986  *
3987  * Specifically, given the input map
3988  *
3989  *      A -> B
3990  *
3991  * We first wrap it into a set
3992  *
3993  *      [A -> B]
3994  *
3995  * and define (1) on top of the corresponding space, resulting in "aff".
3996  * We use this to create an isl_multi_aff that maps the output position "d"
3997  * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
3998  * We plug this into the wrapped map, unwrap the result and compute the
3999  * corresponding isl_pw_multi_aff.
4000  * The result is an expression
4001  *
4002  *      A -> T(A)
4003  *
4004  * We adjust that to
4005  *
4006  *      A -> [A -> T(A)]
4007  *
4008  * so that we can plug that into "aff", after extending the latter to
4009  * a mapping
4010  *
4011  *      [A -> B] -> B'
4012  *
4013  *
4014  * If "map" is actually a set, then there is no "A" space, meaning
4015  * that we do not need to perform any wrapping, and that the result
4016  * of the recursive call is of the form
4017  *
4018  *      [T]
4019  *
4020  * which is plugged into a mapping of the form
4021  *
4022  *      B -> B'
4023  */
4024 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
4025         __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
4026         isl_int gcd)
4027 {
4028         isl_set *set;
4029         isl_space *space;
4030         isl_local_space *ls;
4031         isl_aff *aff;
4032         isl_multi_aff *ma;
4033         isl_pw_multi_aff *pma, *id;
4034         unsigned n_in;
4035         unsigned o_out;
4036         unsigned n_out;
4037         int is_set;
4038
4039         is_set = isl_map_is_set(map);
4040
4041         n_in = isl_basic_map_dim(hull, isl_dim_in);
4042         n_out = isl_basic_map_dim(hull, isl_dim_out);
4043         o_out = isl_basic_map_offset(hull, isl_dim_out);
4044
4045         if (is_set)
4046                 set = map;
4047         else
4048                 set = isl_map_wrap(map);
4049         space = isl_space_map_from_set(isl_set_get_space(set));
4050         ma = isl_multi_aff_identity(space);
4051         ls = isl_local_space_from_space(isl_set_get_space(set));
4052         aff = isl_aff_alloc(ls);
4053         if (aff) {
4054                 isl_int_set_si(aff->v->el[0], 1);
4055                 if (isl_int_is_one(hull->eq[i][o_out + d]))
4056                         isl_seq_neg(aff->v->el + 1, hull->eq[i],
4057                                     aff->v->size - 1);
4058                 else
4059                         isl_seq_cpy(aff->v->el + 1, hull->eq[i],
4060                                     aff->v->size - 1);
4061                 isl_int_set(aff->v->el[1 + o_out + d], gcd);
4062         }
4063         ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
4064         set = isl_set_preimage_multi_aff(set, ma);
4065
4066         ma = range_map(aff, d, n_in, n_out, is_set);
4067
4068         if (is_set)
4069                 map = set;
4070         else
4071                 map = isl_set_unwrap(set);
4072         pma = isl_pw_multi_aff_from_map(set);
4073
4074         if (!is_set) {
4075                 space = isl_pw_multi_aff_get_domain_space(pma);
4076                 space = isl_space_map_from_set(space);
4077                 id = isl_pw_multi_aff_identity(space);
4078                 pma = isl_pw_multi_aff_range_product(id, pma);
4079         }
4080         id = isl_pw_multi_aff_from_multi_aff(ma);
4081         pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
4082
4083         isl_basic_map_free(hull);
4084         return pma;
4085 }
4086
4087 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4088  *
4089  * As a special case, we first check if all output dimensions are uniquely
4090  * defined in terms of the parameters and input dimensions over the entire
4091  * domain.  If so, we extract the desired isl_pw_multi_aff directly
4092  * from the affine hull of "map" and its domain.
4093  *
4094  * Otherwise, we check if any of the output dimensions is "strided".
4095  * That is, we check if can be written as
4096  *
4097  *      x = m a + f(..)
4098  *
4099  * with m greater than 1, a some combination of existentiall quantified
4100  * variables and f and expression in the parameters and input dimensions.
4101  * If so, we remove the stride in pw_multi_aff_from_map_stride.
4102  *
4103  * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4104  * special case.
4105  */
4106 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
4107 {
4108         int i, j;
4109         int sv;
4110         isl_basic_map *hull;
4111         unsigned n_out;
4112         unsigned o_out;
4113         unsigned n_div;
4114         unsigned o_div;
4115         isl_int gcd;
4116
4117         if (!map)
4118                 return NULL;
4119
4120         hull = isl_map_affine_hull(isl_map_copy(map));
4121         sv = isl_basic_map_plain_is_single_valued(hull);
4122         if (sv >= 0 && sv)
4123                 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
4124         if (sv < 0)
4125                 hull = isl_basic_map_free(hull);
4126         if (!hull)
4127                 goto error;
4128
4129         n_div = isl_basic_map_dim(hull, isl_dim_div);
4130         o_div = isl_basic_map_offset(hull, isl_dim_div);
4131
4132         if (n_div == 0) {
4133                 isl_basic_map_free(hull);
4134                 return pw_multi_aff_from_map_check_div(map);
4135         }
4136
4137         isl_int_init(gcd);
4138
4139         n_out = isl_basic_map_dim(hull, isl_dim_out);
4140         o_out = isl_basic_map_offset(hull, isl_dim_out);
4141
4142         for (i = 0; i < n_out; ++i) {
4143                 for (j = 0; j < hull->n_eq; ++j) {
4144                         isl_int *eq = hull->eq[j];
4145                         isl_pw_multi_aff *res;
4146
4147                         if (!isl_int_is_one(eq[o_out + i]) &&
4148                             !isl_int_is_negone(eq[o_out + i]))
4149                                 continue;
4150                         if (isl_seq_first_non_zero(eq + o_out, i) != -1)
4151                                 continue;
4152                         if (isl_seq_first_non_zero(eq + o_out + i + 1,
4153                                                     n_out - (i + 1)) != -1)
4154                                 continue;
4155                         isl_seq_gcd(eq + o_div, n_div, &gcd);
4156                         if (isl_int_is_zero(gcd))
4157                                 continue;
4158                         if (isl_int_is_one(gcd))
4159                                 continue;
4160
4161                         res = pw_multi_aff_from_map_stride(map, hull,
4162                                                                 i, j, gcd);
4163                         isl_int_clear(gcd);
4164                         return res;
4165                 }
4166         }
4167
4168         isl_int_clear(gcd);
4169         isl_basic_map_free(hull);
4170         return pw_multi_aff_from_map_check_div(map);
4171 error:
4172         isl_map_free(map);
4173         return NULL;
4174 }
4175
4176 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
4177 {
4178         return isl_pw_multi_aff_from_map(set);
4179 }
4180
4181 /* Convert "map" into an isl_pw_multi_aff (if possible) and
4182  * add it to *user.
4183  */
4184 static int pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
4185 {
4186         isl_union_pw_multi_aff **upma = user;
4187         isl_pw_multi_aff *pma;
4188
4189         pma = isl_pw_multi_aff_from_map(map);
4190         *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4191
4192         return *upma ? 0 : -1;
4193 }
4194
4195 /* Try and create an isl_union_pw_multi_aff that is equivalent
4196  * to the given isl_union_map.
4197  * The isl_union_map is required to be single-valued in each space.
4198  * Otherwise, an error is produced.
4199  */
4200 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
4201         __isl_take isl_union_map *umap)
4202 {
4203         isl_space *space;
4204         isl_union_pw_multi_aff *upma;
4205
4206         space = isl_union_map_get_space(umap);
4207         upma = isl_union_pw_multi_aff_empty(space);
4208         if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
4209                 upma = isl_union_pw_multi_aff_free(upma);
4210         isl_union_map_free(umap);
4211
4212         return upma;
4213 }
4214
4215 /* Try and create an isl_union_pw_multi_aff that is equivalent
4216  * to the given isl_union_set.
4217  * The isl_union_set is required to be a singleton in each space.
4218  * Otherwise, an error is produced.
4219  */
4220 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
4221         __isl_take isl_union_set *uset)
4222 {
4223         return isl_union_pw_multi_aff_from_union_map(uset);
4224 }
4225
4226 /* Return the piecewise affine expression "set ? 1 : 0".
4227  */
4228 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
4229 {
4230         isl_pw_aff *pa;
4231         isl_space *space = isl_set_get_space(set);
4232         isl_local_space *ls = isl_local_space_from_space(space);
4233         isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
4234         isl_aff *one = isl_aff_zero_on_domain(ls);
4235
4236         one = isl_aff_add_constant_si(one, 1);
4237         pa = isl_pw_aff_alloc(isl_set_copy(set), one);
4238         set = isl_set_complement(set);
4239         pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
4240
4241         return pa;
4242 }
4243
4244 /* Plug in "subs" for dimension "type", "pos" of "aff".
4245  *
4246  * Let i be the dimension to replace and let "subs" be of the form
4247  *
4248  *      f/d
4249  *
4250  * and "aff" of the form
4251  *
4252  *      (a i + g)/m
4253  *
4254  * The result is
4255  *
4256  *      (a f + d g')/(m d)
4257  *
4258  * where g' is the result of plugging in "subs" in each of the integer
4259  * divisions in g.
4260  */
4261 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
4262         enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
4263 {
4264         isl_ctx *ctx;
4265         isl_int v;
4266
4267         aff = isl_aff_cow(aff);
4268         if (!aff || !subs)
4269                 return isl_aff_free(aff);
4270
4271         ctx = isl_aff_get_ctx(aff);
4272         if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
4273                 isl_die(ctx, isl_error_invalid,
4274                         "spaces don't match", return isl_aff_free(aff));
4275         if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
4276                 isl_die(ctx, isl_error_unsupported,
4277                         "cannot handle divs yet", return isl_aff_free(aff));
4278
4279         aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
4280         if (!aff->ls)
4281                 return isl_aff_free(aff);
4282
4283         aff->v = isl_vec_cow(aff->v);
4284         if (!aff->v)
4285                 return isl_aff_free(aff);
4286
4287         pos += isl_local_space_offset(aff->ls, type);
4288
4289         isl_int_init(v);
4290         isl_seq_substitute(aff->v->el, pos, subs->v->el,
4291                             aff->v->size, subs->v->size, v);
4292         isl_int_clear(v);
4293
4294         return aff;
4295 }
4296
4297 /* Plug in "subs" for dimension "type", "pos" in each of the affine
4298  * expressions in "maff".
4299  */
4300 __isl_give isl_multi_aff *isl_multi_aff_substitute(
4301         __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
4302         __isl_keep isl_aff *subs)
4303 {
4304         int i;
4305
4306         maff = isl_multi_aff_cow(maff);
4307         if (!maff || !subs)
4308                 return isl_multi_aff_free(maff);
4309
4310         if (type == isl_dim_in)
4311                 type = isl_dim_set;
4312
4313         for (i = 0; i < maff->n; ++i) {
4314                 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
4315                 if (!maff->p[i])
4316                         return isl_multi_aff_free(maff);
4317         }
4318
4319         return maff;
4320 }
4321
4322 /* Plug in "subs" for dimension "type", "pos" of "pma".
4323  *
4324  * pma is of the form
4325  *
4326  *      A_i(v) -> M_i(v)
4327  *
4328  * while subs is of the form
4329  *
4330  *      v' = B_j(v) -> S_j
4331  *
4332  * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
4333  * has a contribution in the result, in particular
4334  *
4335  *      C_ij(S_j) -> M_i(S_j)
4336  *
4337  * Note that plugging in S_j in C_ij may also result in an empty set
4338  * and this contribution should simply be discarded.
4339  */
4340 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
4341         __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
4342         __isl_keep isl_pw_aff *subs)
4343 {
4344         int i, j, n;
4345         isl_pw_multi_aff *res;
4346
4347         if (!pma || !subs)
4348                 return isl_pw_multi_aff_free(pma);
4349
4350         n = pma->n * subs->n;
4351         res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
4352
4353         for (i = 0; i < pma->n; ++i) {
4354                 for (j = 0; j < subs->n; ++j) {
4355                         isl_set *common;
4356                         isl_multi_aff *res_ij;
4357                         int empty;
4358
4359                         common = isl_set_intersect(
4360                                         isl_set_copy(pma->p[i].set),
4361                                         isl_set_copy(subs->p[j].set));
4362                         common = isl_set_substitute(common,
4363                                         type, pos, subs->p[j].aff);
4364                         empty = isl_set_plain_is_empty(common);
4365                         if (empty < 0 || empty) {
4366                                 isl_set_free(common);
4367                                 if (empty < 0)
4368                                         goto error;
4369                                 continue;
4370                         }
4371
4372                         res_ij = isl_multi_aff_substitute(
4373                                         isl_multi_aff_copy(pma->p[i].maff),
4374                                         type, pos, subs->p[j].aff);
4375
4376                         res = isl_pw_multi_aff_add_piece(res, common, res_ij);
4377                 }
4378         }
4379
4380         isl_pw_multi_aff_free(pma);
4381         return res;
4382 error:
4383         isl_pw_multi_aff_free(pma);
4384         isl_pw_multi_aff_free(res);
4385         return NULL;
4386 }
4387
4388 /* Compute the preimage of a range of dimensions in the affine expression "src"
4389  * under "ma" and put the result in "dst".  The number of dimensions in "src"
4390  * that precede the range is given by "n_before".  The number of dimensions
4391  * in the range is given by the number of output dimensions of "ma".
4392  * The number of dimensions that follow the range is given by "n_after".
4393  * If "has_denom" is set (to one),
4394  * then "src" and "dst" have an extra initial denominator.
4395  * "n_div_ma" is the number of existentials in "ma"
4396  * "n_div_bset" is the number of existentials in "src"
4397  * The resulting "dst" (which is assumed to have been allocated by
4398  * the caller) contains coefficients for both sets of existentials,
4399  * first those in "ma" and then those in "src".
4400  * f, c1, c2 and g are temporary objects that have been initialized
4401  * by the caller.
4402  *
4403  * Let src represent the expression
4404  *
4405  *      (a(p) + f_u u + b v + f_w w + c(divs))/d
4406  *
4407  * and let ma represent the expressions
4408  *
4409  *      v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
4410  *
4411  * We start out with the following expression for dst:
4412  *
4413  *      (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
4414  *
4415  * with the multiplication factor f initially equal to 1
4416  * and f \sum_i b_i v_i kept separately.
4417  * For each x_i that we substitute, we multiply the numerator
4418  * (and denominator) of dst by c_1 = m_i and add the numerator
4419  * of the x_i expression multiplied by c_2 = f b_i,
4420  * after removing the common factors of c_1 and c_2.
4421  * The multiplication factor f also needs to be multiplied by c_1
4422  * for the next x_j, j > i.
4423  */
4424 void isl_seq_preimage(isl_int *dst, isl_int *src,
4425         __isl_keep isl_multi_aff *ma, int n_before, int n_after,
4426         int n_div_ma, int n_div_bmap,
4427         isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
4428 {
4429         int i;
4430         int n_param, n_in, n_out;
4431         int o_dst, o_src;
4432
4433         n_param = isl_multi_aff_dim(ma, isl_dim_param);
4434         n_in = isl_multi_aff_dim(ma, isl_dim_in);
4435         n_out = isl_multi_aff_dim(ma, isl_dim_out);
4436
4437         isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
4438         o_dst = o_src = has_denom + 1 + n_param + n_before;
4439         isl_seq_clr(dst + o_dst, n_in);
4440         o_dst += n_in;
4441         o_src += n_out;
4442         isl_seq_cpy(dst + o_dst, src + o_src, n_after);
4443         o_dst += n_after;
4444         o_src += n_after;
4445         isl_seq_clr(dst + o_dst, n_div_ma);
4446         o_dst += n_div_ma;
4447         isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
4448
4449         isl_int_set_si(f, 1);
4450
4451         for (i = 0; i < n_out; ++i) {
4452                 int offset = has_denom + 1 + n_param + n_before + i;
4453
4454                 if (isl_int_is_zero(src[offset]))
4455                         continue;
4456                 isl_int_set(c1, ma->p[i]->v->el[0]);
4457                 isl_int_mul(c2, f, src[offset]);
4458                 isl_int_gcd(g, c1, c2);
4459                 isl_int_divexact(c1, c1, g);
4460                 isl_int_divexact(c2, c2, g);
4461
4462                 isl_int_mul(f, f, c1);
4463                 o_dst = has_denom;
4464                 o_src = 1;
4465                 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4466                                 c2, ma->p[i]->v->el + o_src, 1 + n_param);
4467                 o_dst += 1 + n_param;
4468                 o_src += 1 + n_param;
4469                 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
4470                 o_dst += n_before;
4471                 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4472                                 c2, ma->p[i]->v->el + o_src, n_in);
4473                 o_dst += n_in;
4474                 o_src += n_in;
4475                 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
4476                 o_dst += n_after;
4477                 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4478                                 c2, ma->p[i]->v->el + o_src, n_div_ma);
4479                 o_dst += n_div_ma;
4480                 o_src += n_div_ma;
4481                 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
4482                 if (has_denom)
4483                         isl_int_mul(dst[0], dst[0], c1);
4484         }
4485 }
4486
4487 /* Compute the pullback of "aff" by the function represented by "ma".
4488  * In other words, plug in "ma" in "aff".  The result is an affine expression
4489  * defined over the domain space of "ma".
4490  *
4491  * If "aff" is represented by
4492  *
4493  *      (a(p) + b x + c(divs))/d
4494  *
4495  * and ma is represented by
4496  *
4497  *      x = D(p) + F(y) + G(divs')
4498  *
4499  * then the result is
4500  *
4501  *      (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
4502  *
4503  * The divs in the local space of the input are similarly adjusted
4504  * through a call to isl_local_space_preimage_multi_aff.
4505  */
4506 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
4507         __isl_take isl_multi_aff *ma)
4508 {
4509         isl_aff *res = NULL;
4510         isl_local_space *ls;
4511         int n_div_aff, n_div_ma;
4512         isl_int f, c1, c2, g;
4513
4514         ma = isl_multi_aff_align_divs(ma);
4515         if (!aff || !ma)
4516                 goto error;
4517
4518         n_div_aff = isl_aff_dim(aff, isl_dim_div);
4519         n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
4520
4521         ls = isl_aff_get_domain_local_space(aff);
4522         ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
4523         res = isl_aff_alloc(ls);
4524         if (!res)
4525                 goto error;
4526
4527         isl_int_init(f);
4528         isl_int_init(c1);
4529         isl_int_init(c2);
4530         isl_int_init(g);
4531
4532         isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
4533                         f, c1, c2, g, 1);
4534
4535         isl_int_clear(f);
4536         isl_int_clear(c1);
4537         isl_int_clear(c2);
4538         isl_int_clear(g);
4539
4540         isl_aff_free(aff);
4541         isl_multi_aff_free(ma);
4542         res = isl_aff_normalize(res);
4543         return res;
4544 error:
4545         isl_aff_free(aff);
4546         isl_multi_aff_free(ma);
4547         isl_aff_free(res);
4548         return NULL;
4549 }
4550
4551 /* Compute the pullback of "ma1" by the function represented by "ma2".
4552  * In other words, plug in "ma2" in "ma1".
4553  */
4554 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
4555         __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
4556 {
4557         int i;
4558         isl_space *space = NULL;
4559
4560         ma2 = isl_multi_aff_align_divs(ma2);
4561         ma1 = isl_multi_aff_cow(ma1);
4562         if (!ma1 || !ma2)
4563                 goto error;
4564
4565         space = isl_space_join(isl_multi_aff_get_space(ma2),
4566                                 isl_multi_aff_get_space(ma1));
4567
4568         for (i = 0; i < ma1->n; ++i) {
4569                 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
4570                                                     isl_multi_aff_copy(ma2));
4571                 if (!ma1->p[i])
4572                         goto error;
4573         }
4574
4575         ma1 = isl_multi_aff_reset_space(ma1, space);
4576         isl_multi_aff_free(ma2);
4577         return ma1;
4578 error:
4579         isl_space_free(space);
4580         isl_multi_aff_free(ma2);
4581         isl_multi_aff_free(ma1);
4582         return NULL;
4583 }
4584
4585 /* Extend the local space of "dst" to include the divs
4586  * in the local space of "src".
4587  */
4588 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
4589         __isl_keep isl_aff *src)
4590 {
4591         isl_ctx *ctx;
4592         int *exp1 = NULL;
4593         int *exp2 = NULL;
4594         isl_mat *div;
4595
4596         if (!src || !dst)
4597                 return isl_aff_free(dst);
4598
4599         ctx = isl_aff_get_ctx(src);
4600         if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
4601                 isl_die(ctx, isl_error_invalid,
4602                         "spaces don't match", goto error);
4603
4604         if (src->ls->div->n_row == 0)
4605                 return dst;
4606
4607         exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
4608         exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
4609         if (!exp1 || !exp2)
4610                 goto error;
4611
4612         div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
4613         dst = isl_aff_expand_divs(dst, div, exp2);
4614         free(exp1);
4615         free(exp2);
4616
4617         return dst;
4618 error:
4619         free(exp1);
4620         free(exp2);
4621         return isl_aff_free(dst);
4622 }
4623
4624 /* Adjust the local spaces of the affine expressions in "maff"
4625  * such that they all have the save divs.
4626  */
4627 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
4628         __isl_take isl_multi_aff *maff)
4629 {
4630         int i;
4631
4632         if (!maff)
4633                 return NULL;
4634         if (maff->n == 0)
4635                 return maff;
4636         maff = isl_multi_aff_cow(maff);
4637         if (!maff)
4638                 return NULL;
4639
4640         for (i = 1; i < maff->n; ++i)
4641                 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
4642         for (i = 1; i < maff->n; ++i) {
4643                 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
4644                 if (!maff->p[i])
4645                         return isl_multi_aff_free(maff);
4646         }
4647
4648         return maff;
4649 }
4650
4651 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
4652 {
4653         aff = isl_aff_cow(aff);
4654         if (!aff)
4655                 return NULL;
4656
4657         aff->ls = isl_local_space_lift(aff->ls);
4658         if (!aff->ls)
4659                 return isl_aff_free(aff);
4660
4661         return aff;
4662 }
4663
4664 /* Lift "maff" to a space with extra dimensions such that the result
4665  * has no more existentially quantified variables.
4666  * If "ls" is not NULL, then *ls is assigned the local space that lies
4667  * at the basis of the lifting applied to "maff".
4668  */
4669 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
4670         __isl_give isl_local_space **ls)
4671 {
4672         int i;
4673         isl_space *space;
4674         unsigned n_div;
4675
4676         if (ls)
4677                 *ls = NULL;
4678
4679         if (!maff)
4680                 return NULL;
4681
4682         if (maff->n == 0) {
4683                 if (ls) {
4684                         isl_space *space = isl_multi_aff_get_domain_space(maff);
4685                         *ls = isl_local_space_from_space(space);
4686                         if (!*ls)
4687                                 return isl_multi_aff_free(maff);
4688                 }
4689                 return maff;
4690         }
4691
4692         maff = isl_multi_aff_cow(maff);
4693         maff = isl_multi_aff_align_divs(maff);
4694         if (!maff)
4695                 return NULL;
4696
4697         n_div = isl_aff_dim(maff->p[0], isl_dim_div);
4698         space = isl_multi_aff_get_space(maff);
4699         space = isl_space_lift(isl_space_domain(space), n_div);
4700         space = isl_space_extend_domain_with_range(space,
4701                                                 isl_multi_aff_get_space(maff));
4702         if (!space)
4703                 return isl_multi_aff_free(maff);
4704         isl_space_free(maff->space);
4705         maff->space = space;
4706
4707         if (ls) {
4708                 *ls = isl_aff_get_domain_local_space(maff->p[0]);
4709                 if (!*ls)
4710                         return isl_multi_aff_free(maff);
4711         }
4712
4713         for (i = 0; i < maff->n; ++i) {
4714                 maff->p[i] = isl_aff_lift(maff->p[i]);
4715                 if (!maff->p[i])
4716                         goto error;
4717         }
4718
4719         return maff;
4720 error:
4721         if (ls)
4722                 isl_local_space_free(*ls);
4723         return isl_multi_aff_free(maff);
4724 }
4725
4726
4727 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
4728  */
4729 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
4730         __isl_keep isl_pw_multi_aff *pma, int pos)
4731 {
4732         int i;
4733         int n_out;
4734         isl_space *space;
4735         isl_pw_aff *pa;
4736
4737         if (!pma)
4738                 return NULL;
4739
4740         n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
4741         if (pos < 0 || pos >= n_out)
4742                 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4743                         "index out of bounds", return NULL);
4744
4745         space = isl_pw_multi_aff_get_space(pma);
4746         space = isl_space_drop_dims(space, isl_dim_out,
4747                                     pos + 1, n_out - pos - 1);
4748         space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
4749
4750         pa = isl_pw_aff_alloc_size(space, pma->n);
4751         for (i = 0; i < pma->n; ++i) {
4752                 isl_aff *aff;
4753                 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
4754                 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
4755         }
4756
4757         return pa;
4758 }
4759
4760 /* Return an isl_pw_multi_aff with the given "set" as domain and
4761  * an unnamed zero-dimensional range.
4762  */
4763 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
4764         __isl_take isl_set *set)
4765 {
4766         isl_multi_aff *ma;
4767         isl_space *space;
4768
4769         space = isl_set_get_space(set);
4770         space = isl_space_from_domain(space);
4771         ma = isl_multi_aff_zero(space);
4772         return isl_pw_multi_aff_alloc(set, ma);
4773 }
4774
4775 /* Add an isl_pw_multi_aff with the given "set" as domain and
4776  * an unnamed zero-dimensional range to *user.
4777  */
4778 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
4779 {
4780         isl_union_pw_multi_aff **upma = user;
4781         isl_pw_multi_aff *pma;
4782
4783         pma = isl_pw_multi_aff_from_domain(set);
4784         *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4785
4786         return 0;
4787 }
4788
4789 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
4790  * an unnamed zero-dimensional range.
4791  */
4792 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
4793         __isl_take isl_union_set *uset)
4794 {
4795         isl_space *space;
4796         isl_union_pw_multi_aff *upma;
4797
4798         if (!uset)
4799                 return NULL;
4800
4801         space = isl_union_set_get_space(uset);
4802         upma = isl_union_pw_multi_aff_empty(space);
4803
4804         if (isl_union_set_foreach_set(uset,
4805                                     &add_pw_multi_aff_from_domain, &upma) < 0)
4806                 goto error;
4807
4808         isl_union_set_free(uset);
4809         return upma;
4810 error:
4811         isl_union_set_free(uset);
4812         isl_union_pw_multi_aff_free(upma);
4813         return NULL;
4814 }
4815
4816 /* Convert "pma" to an isl_map and add it to *umap.
4817  */
4818 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
4819 {
4820         isl_union_map **umap = user;
4821         isl_map *map;
4822
4823         map = isl_map_from_pw_multi_aff(pma);
4824         *umap = isl_union_map_add_map(*umap, map);
4825
4826         return 0;
4827 }
4828
4829 /* Construct a union map mapping the domain of the union
4830  * piecewise multi-affine expression to its range, with each dimension
4831  * in the range equated to the corresponding affine expression on its cell.
4832  */
4833 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
4834         __isl_take isl_union_pw_multi_aff *upma)
4835 {
4836         isl_space *space;
4837         isl_union_map *umap;
4838
4839         if (!upma)
4840                 return NULL;
4841
4842         space = isl_union_pw_multi_aff_get_space(upma);
4843         umap = isl_union_map_empty(space);
4844
4845         if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
4846                                         &map_from_pw_multi_aff, &umap) < 0)
4847                 goto error;
4848
4849         isl_union_pw_multi_aff_free(upma);
4850         return umap;
4851 error:
4852         isl_union_pw_multi_aff_free(upma);
4853         isl_union_map_free(umap);
4854         return NULL;
4855 }
4856
4857 /* Local data for bin_entry and the callback "fn".
4858  */
4859 struct isl_union_pw_multi_aff_bin_data {
4860         isl_union_pw_multi_aff *upma2;
4861         isl_union_pw_multi_aff *res;
4862         isl_pw_multi_aff *pma;
4863         int (*fn)(void **entry, void *user);
4864 };
4865
4866 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
4867  * and call data->fn for each isl_pw_multi_aff in data->upma2.
4868  */
4869 static int bin_entry(void **entry, void *user)
4870 {
4871         struct isl_union_pw_multi_aff_bin_data *data = user;
4872         isl_pw_multi_aff *pma = *entry;
4873
4874         data->pma = pma;
4875         if (isl_hash_table_foreach(data->upma2->dim->ctx, &data->upma2->table,
4876                                    data->fn, data) < 0)
4877                 return -1;
4878
4879         return 0;
4880 }
4881
4882 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
4883  * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
4884  * passed as user field) and the isl_pw_multi_aff from upma2 is available
4885  * as *entry.  The callback should adjust data->res if desired.
4886  */
4887 static __isl_give isl_union_pw_multi_aff *bin_op(
4888         __isl_take isl_union_pw_multi_aff *upma1,
4889         __isl_take isl_union_pw_multi_aff *upma2,
4890         int (*fn)(void **entry, void *user))
4891 {
4892         isl_space *space;
4893         struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
4894
4895         space = isl_union_pw_multi_aff_get_space(upma2);
4896         upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
4897         space = isl_union_pw_multi_aff_get_space(upma1);
4898         upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
4899
4900         if (!upma1 || !upma2)
4901                 goto error;
4902
4903         data.upma2 = upma2;
4904         data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->dim),
4905                                        upma1->table.n);
4906         if (isl_hash_table_foreach(upma1->dim->ctx, &upma1->table,
4907                                    &bin_entry, &data) < 0)
4908                 goto error;
4909
4910         isl_union_pw_multi_aff_free(upma1);
4911         isl_union_pw_multi_aff_free(upma2);
4912         return data.res;
4913 error:
4914         isl_union_pw_multi_aff_free(upma1);
4915         isl_union_pw_multi_aff_free(upma2);
4916         isl_union_pw_multi_aff_free(data.res);
4917         return NULL;
4918 }
4919
4920 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
4921  * construct an isl_pw_multi_aff (A * C) -> [B -> D].
4922  */
4923 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
4924         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4925 {
4926         isl_space *space;
4927
4928         space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
4929                                         isl_pw_multi_aff_get_space(pma2));
4930         return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
4931                                             &isl_multi_aff_range_product);
4932 }
4933
4934 /* Given two isl_pw_multi_affs A -> B and C -> D,
4935  * construct an isl_pw_multi_aff (A * C) -> [B -> D].
4936  */
4937 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
4938         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4939 {
4940         return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4941                                             &pw_multi_aff_range_product);
4942 }
4943
4944 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
4945  * construct an isl_pw_multi_aff (A * C) -> (B, D).
4946  */
4947 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
4948         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4949 {
4950         isl_space *space;
4951
4952         space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
4953                                         isl_pw_multi_aff_get_space(pma2));
4954         space = isl_space_flatten_range(space);
4955         return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
4956                                             &isl_multi_aff_flat_range_product);
4957 }
4958
4959 /* Given two isl_pw_multi_affs A -> B and C -> D,
4960  * construct an isl_pw_multi_aff (A * C) -> (B, D).
4961  */
4962 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
4963         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4964 {
4965         return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4966                                             &pw_multi_aff_flat_range_product);
4967 }
4968
4969 /* If data->pma and *entry have the same domain space, then compute
4970  * their flat range product and the result to data->res.
4971  */
4972 static int flat_range_product_entry(void **entry, void *user)
4973 {
4974         struct isl_union_pw_multi_aff_bin_data *data = user;
4975         isl_pw_multi_aff *pma2 = *entry;
4976
4977         if (!isl_space_tuple_match(data->pma->dim, isl_dim_in,
4978                                  pma2->dim, isl_dim_in))
4979                 return 0;
4980
4981         pma2 = isl_pw_multi_aff_flat_range_product(
4982                                         isl_pw_multi_aff_copy(data->pma),
4983                                         isl_pw_multi_aff_copy(pma2));
4984
4985         data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
4986
4987         return 0;
4988 }
4989
4990 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
4991  * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
4992  */
4993 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
4994         __isl_take isl_union_pw_multi_aff *upma1,
4995         __isl_take isl_union_pw_multi_aff *upma2)
4996 {
4997         return bin_op(upma1, upma2, &flat_range_product_entry);
4998 }
4999
5000 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5001  * The parameters are assumed to have been aligned.
5002  *
5003  * The implementation essentially performs an isl_pw_*_on_shared_domain,
5004  * except that it works on two different isl_pw_* types.
5005  */
5006 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
5007         __isl_take isl_pw_multi_aff *pma, unsigned pos,
5008         __isl_take isl_pw_aff *pa)
5009 {
5010         int i, j, n;
5011         isl_pw_multi_aff *res = NULL;
5012
5013         if (!pma || !pa)
5014                 goto error;
5015
5016         if (!isl_space_tuple_match(pma->dim, isl_dim_in, pa->dim, isl_dim_in))
5017                 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5018                         "domains don't match", goto error);
5019         if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
5020                 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5021                         "index out of bounds", goto error);
5022
5023         n = pma->n * pa->n;
5024         res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
5025
5026         for (i = 0; i < pma->n; ++i) {
5027                 for (j = 0; j < pa->n; ++j) {
5028                         isl_set *common;
5029                         isl_multi_aff *res_ij;
5030                         int empty;
5031
5032                         common = isl_set_intersect(isl_set_copy(pma->p[i].set),
5033                                                    isl_set_copy(pa->p[j].set));
5034                         empty = isl_set_plain_is_empty(common);
5035                         if (empty < 0 || empty) {
5036                                 isl_set_free(common);
5037                                 if (empty < 0)
5038                                         goto error;
5039                                 continue;
5040                         }
5041
5042                         res_ij = isl_multi_aff_set_aff(
5043                                         isl_multi_aff_copy(pma->p[i].maff), pos,
5044                                         isl_aff_copy(pa->p[j].aff));
5045                         res_ij = isl_multi_aff_gist(res_ij,
5046                                         isl_set_copy(common));
5047
5048                         res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5049                 }
5050         }
5051
5052         isl_pw_multi_aff_free(pma);
5053         isl_pw_aff_free(pa);
5054         return res;
5055 error:
5056         isl_pw_multi_aff_free(pma);
5057         isl_pw_aff_free(pa);
5058         return isl_pw_multi_aff_free(res);
5059 }
5060
5061 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5062  */
5063 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
5064         __isl_take isl_pw_multi_aff *pma, unsigned pos,
5065         __isl_take isl_pw_aff *pa)
5066 {
5067         if (!pma || !pa)
5068                 goto error;
5069         if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
5070                 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5071         if (!isl_space_has_named_params(pma->dim) ||
5072             !isl_space_has_named_params(pa->dim))
5073                 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5074                         "unaligned unnamed parameters", goto error);
5075         pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
5076         pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
5077         return pw_multi_aff_set_pw_aff(pma, pos, pa);
5078 error:
5079         isl_pw_multi_aff_free(pma);
5080         isl_pw_aff_free(pa);
5081         return NULL;
5082 }
5083
5084 /* Check that the domain space of "pa" matches "space".
5085  *
5086  * Return 0 on success and -1 on error.
5087  */
5088 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
5089         __isl_keep isl_space *space)
5090 {
5091         isl_space *pa_space;
5092         int match;
5093
5094         if (!pa || !space)
5095                 return -1;
5096
5097         pa_space = isl_pw_aff_get_space(pa);
5098
5099         match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5100         if (match < 0)
5101                 goto error;
5102         if (!match)
5103                 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5104                         "parameters don't match", goto error);
5105         match = isl_space_tuple_match(space, isl_dim_in, pa_space, isl_dim_in);
5106         if (match < 0)
5107                 goto error;
5108         if (!match)
5109                 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5110                         "domains don't match", goto error);
5111         isl_space_free(pa_space);
5112         return 0;
5113 error:
5114         isl_space_free(pa_space);
5115         return -1;
5116 }
5117
5118 #undef BASE
5119 #define BASE pw_aff
5120
5121 #include <isl_multi_templ.c>
5122
5123 /* Scale the first elements of "ma" by the corresponding elements of "vec".
5124  */
5125 __isl_give isl_multi_aff *isl_multi_aff_scale_vec(__isl_take isl_multi_aff *ma,
5126         __isl_take isl_vec *vec)
5127 {
5128         int i, n;
5129         isl_int v;
5130
5131         if (!ma || !vec)
5132                 goto error;
5133
5134         n = isl_multi_aff_dim(ma, isl_dim_out);
5135         if (isl_vec_size(vec) < n)
5136                 n = isl_vec_size(vec);
5137
5138         isl_int_init(v);
5139         for (i = 0; i < n; ++i) {
5140                 isl_aff *aff;
5141
5142                 isl_vec_get_element(vec, i, &v);
5143
5144                 aff = isl_multi_aff_get_aff(ma, i);
5145                 aff = isl_aff_scale(aff, v);
5146                 ma = isl_multi_aff_set_aff(ma, i, aff);
5147         }
5148         isl_int_clear(v);
5149
5150         isl_vec_free(vec);
5151         return ma;
5152 error:
5153         isl_vec_free(vec);
5154         isl_multi_aff_free(ma);
5155         return NULL;
5156 }
5157
5158 /* Scale the first elements of "pma" by the corresponding elements of "vec".
5159  */
5160 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_vec(
5161         __isl_take isl_pw_multi_aff *pma, __isl_take isl_vec *v)
5162 {
5163         int i;
5164
5165         pma = isl_pw_multi_aff_cow(pma);
5166         if (!pma || !v)
5167                 goto error;
5168
5169         for (i = 0; i < pma->n; ++i) {
5170                 pma->p[i].maff = isl_multi_aff_scale_vec(pma->p[i].maff,
5171                                                         isl_vec_copy(v));
5172                 if (!pma->p[i].maff)
5173                         goto error;
5174         }
5175
5176         isl_vec_free(v);
5177         return pma;
5178 error:
5179         isl_vec_free(v);
5180         isl_pw_multi_aff_free(pma);
5181         return NULL;
5182 }
5183
5184 /* This function is called for each entry of an isl_union_pw_multi_aff.
5185  * Replace the entry by the result of applying isl_pw_multi_aff_scale_vec
5186  * to the original entry with the isl_vec in "user" as extra argument.
5187  */
5188 static int union_pw_multi_aff_scale_vec_entry(void **entry, void *user)
5189 {
5190         isl_pw_multi_aff **pma = (isl_pw_multi_aff **) entry;
5191         isl_vec *v = user;
5192
5193         *pma = isl_pw_multi_aff_scale_vec(*pma, isl_vec_copy(v));
5194         if (!*pma)
5195                 return -1;
5196
5197         return 0;
5198 }
5199
5200 /* Scale the first elements of "upma" by the corresponding elements of "vec".
5201  */
5202 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_vec(
5203         __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_vec *v)
5204 {
5205         upma = isl_union_pw_multi_aff_cow(upma);
5206         if (!upma || !v)
5207                 goto error;
5208
5209         if (isl_hash_table_foreach(upma->dim->ctx, &upma->table,
5210                                    &union_pw_multi_aff_scale_vec_entry, v) < 0)
5211                 goto error;
5212
5213         isl_vec_free(v);
5214         return upma;
5215 error:
5216         isl_vec_free(v);
5217         isl_union_pw_multi_aff_free(upma);
5218         return NULL;
5219 }