add isl_aff_neg_basic_set
[platform/upstream/isl.git] / isl_aff.c
1 /*
2  * Copyright 2011      INRIA Saclay
3  * Copyright 2011      Sven Verdoolaege
4  * Copyright 2012      Ecole Normale Superieure
5  *
6  * Use of this software is governed by the GNU LGPLv2.1 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_list_private.h>
23 #include <isl/constraint.h>
24 #include <isl/seq.h>
25 #include <isl/set.h>
26 #include <isl_config.h>
27
28 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
29         __isl_take isl_vec *v)
30 {
31         isl_aff *aff;
32
33         if (!ls || !v)
34                 goto error;
35
36         aff = isl_calloc_type(v->ctx, struct isl_aff);
37         if (!aff)
38                 goto error;
39
40         aff->ref = 1;
41         aff->ls = ls;
42         aff->v = v;
43
44         return aff;
45 error:
46         isl_local_space_free(ls);
47         isl_vec_free(v);
48         return NULL;
49 }
50
51 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
52 {
53         isl_ctx *ctx;
54         isl_vec *v;
55         unsigned total;
56
57         if (!ls)
58                 return NULL;
59
60         ctx = isl_local_space_get_ctx(ls);
61         if (!isl_local_space_divs_known(ls))
62                 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
63                         goto error);
64         if (!isl_local_space_is_set(ls))
65                 isl_die(ctx, isl_error_invalid,
66                         "domain of affine expression should be a set",
67                         goto error);
68
69         total = isl_local_space_dim(ls, isl_dim_all);
70         v = isl_vec_alloc(ctx, 1 + 1 + total);
71         return isl_aff_alloc_vec(ls, v);
72 error:
73         isl_local_space_free(ls);
74         return NULL;
75 }
76
77 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
78 {
79         isl_aff *aff;
80
81         aff = isl_aff_alloc(ls);
82         if (!aff)
83                 return NULL;
84
85         isl_int_set_si(aff->v->el[0], 1);
86         isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
87
88         return aff;
89 }
90
91 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
92 {
93         if (!aff)
94                 return NULL;
95
96         aff->ref++;
97         return aff;
98 }
99
100 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
101 {
102         if (!aff)
103                 return NULL;
104
105         return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
106                                  isl_vec_copy(aff->v));
107 }
108
109 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
110 {
111         if (!aff)
112                 return NULL;
113
114         if (aff->ref == 1)
115                 return aff;
116         aff->ref--;
117         return isl_aff_dup(aff);
118 }
119
120 void *isl_aff_free(__isl_take isl_aff *aff)
121 {
122         if (!aff)
123                 return NULL;
124
125         if (--aff->ref > 0)
126                 return NULL;
127
128         isl_local_space_free(aff->ls);
129         isl_vec_free(aff->v);
130
131         free(aff);
132
133         return NULL;
134 }
135
136 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
137 {
138         return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
139 }
140
141 /* Externally, an isl_aff has a map space, but internally, the
142  * ls field corresponds to the domain of that space.
143  */
144 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
145 {
146         if (!aff)
147                 return 0;
148         if (type == isl_dim_out)
149                 return 1;
150         if (type == isl_dim_in)
151                 type = isl_dim_set;
152         return isl_local_space_dim(aff->ls, type);
153 }
154
155 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
156 {
157         return aff ? isl_local_space_get_space(aff->ls) : NULL;
158 }
159
160 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
161 {
162         isl_space *space;
163         if (!aff)
164                 return NULL;
165         space = isl_local_space_get_space(aff->ls);
166         space = isl_space_from_domain(space);
167         space = isl_space_add_dims(space, isl_dim_out, 1);
168         return space;
169 }
170
171 __isl_give isl_local_space *isl_aff_get_domain_local_space(
172         __isl_keep isl_aff *aff)
173 {
174         return aff ? isl_local_space_copy(aff->ls) : NULL;
175 }
176
177 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
178 {
179         isl_local_space *ls;
180         if (!aff)
181                 return NULL;
182         ls = isl_local_space_copy(aff->ls);
183         ls = isl_local_space_from_domain(ls);
184         ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
185         return ls;
186 }
187
188 /* Externally, an isl_aff has a map space, but internally, the
189  * ls field corresponds to the domain of that space.
190  */
191 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
192         enum isl_dim_type type, unsigned pos)
193 {
194         if (!aff)
195                 return NULL;
196         if (type == isl_dim_out)
197                 return NULL;
198         if (type == isl_dim_in)
199                 type = isl_dim_set;
200         return isl_local_space_get_dim_name(aff->ls, type, pos);
201 }
202
203 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
204         __isl_take isl_space *dim)
205 {
206         aff = isl_aff_cow(aff);
207         if (!aff || !dim)
208                 goto error;
209
210         aff->ls = isl_local_space_reset_space(aff->ls, dim);
211         if (!aff->ls)
212                 return isl_aff_free(aff);
213
214         return aff;
215 error:
216         isl_aff_free(aff);
217         isl_space_free(dim);
218         return NULL;
219 }
220
221 /* Reset the space of "aff".  This function is called from isl_pw_templ.c
222  * and doesn't know if the space of an element object is represented
223  * directly or through its domain.  It therefore passes along both.
224  */
225 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
226         __isl_take isl_space *space, __isl_take isl_space *domain)
227 {
228         isl_space_free(space);
229         return isl_aff_reset_domain_space(aff, domain);
230 }
231
232 /* Reorder the coefficients of the affine expression based
233  * on the given reodering.
234  * The reordering r is assumed to have been extended with the local
235  * variables.
236  */
237 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
238         __isl_take isl_reordering *r, int n_div)
239 {
240         isl_vec *res;
241         int i;
242
243         if (!vec || !r)
244                 goto error;
245
246         res = isl_vec_alloc(vec->ctx,
247                             2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
248         isl_seq_cpy(res->el, vec->el, 2);
249         isl_seq_clr(res->el + 2, res->size - 2);
250         for (i = 0; i < r->len; ++i)
251                 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
252
253         isl_reordering_free(r);
254         isl_vec_free(vec);
255         return res;
256 error:
257         isl_vec_free(vec);
258         isl_reordering_free(r);
259         return NULL;
260 }
261
262 /* Reorder the dimensions of the domain of "aff" according
263  * to the given reordering.
264  */
265 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
266         __isl_take isl_reordering *r)
267 {
268         aff = isl_aff_cow(aff);
269         if (!aff)
270                 goto error;
271
272         r = isl_reordering_extend(r, aff->ls->div->n_row);
273         aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
274                                 aff->ls->div->n_row);
275         aff->ls = isl_local_space_realign(aff->ls, r);
276
277         if (!aff->v || !aff->ls)
278                 return isl_aff_free(aff);
279
280         return aff;
281 error:
282         isl_aff_free(aff);
283         isl_reordering_free(r);
284         return NULL;
285 }
286
287 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
288         __isl_take isl_space *model)
289 {
290         if (!aff || !model)
291                 goto error;
292
293         if (!isl_space_match(aff->ls->dim, isl_dim_param,
294                              model, isl_dim_param)) {
295                 isl_reordering *exp;
296
297                 model = isl_space_drop_dims(model, isl_dim_in,
298                                         0, isl_space_dim(model, isl_dim_in));
299                 model = isl_space_drop_dims(model, isl_dim_out,
300                                         0, isl_space_dim(model, isl_dim_out));
301                 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
302                 exp = isl_reordering_extend_space(exp,
303                                         isl_aff_get_domain_space(aff));
304                 aff = isl_aff_realign_domain(aff, exp);
305         }
306
307         isl_space_free(model);
308         return aff;
309 error:
310         isl_space_free(model);
311         isl_aff_free(aff);
312         return NULL;
313 }
314
315 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
316 {
317         if (!aff)
318                 return -1;
319
320         return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
321 }
322
323 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
324 {
325         int equal;
326
327         if (!aff1 || !aff2)
328                 return -1;
329
330         equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
331         if (equal < 0 || !equal)
332                 return equal;
333
334         return isl_vec_is_equal(aff1->v, aff2->v);
335 }
336
337 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
338 {
339         if (!aff)
340                 return -1;
341         isl_int_set(*v, aff->v->el[0]);
342         return 0;
343 }
344
345 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
346 {
347         if (!aff)
348                 return -1;
349         isl_int_set(*v, aff->v->el[1]);
350         return 0;
351 }
352
353 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
354         enum isl_dim_type type, int pos, isl_int *v)
355 {
356         if (!aff)
357                 return -1;
358
359         if (type == isl_dim_out)
360                 isl_die(aff->v->ctx, isl_error_invalid,
361                         "output/set dimension does not have a coefficient",
362                         return -1);
363         if (type == isl_dim_in)
364                 type = isl_dim_set;
365
366         if (pos >= isl_local_space_dim(aff->ls, type))
367                 isl_die(aff->v->ctx, isl_error_invalid,
368                         "position out of bounds", return -1);
369
370         pos += isl_local_space_offset(aff->ls, type);
371         isl_int_set(*v, aff->v->el[1 + pos]);
372
373         return 0;
374 }
375
376 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
377 {
378         aff = isl_aff_cow(aff);
379         if (!aff)
380                 return NULL;
381
382         aff->v = isl_vec_cow(aff->v);
383         if (!aff->v)
384                 return isl_aff_free(aff);
385
386         isl_int_set(aff->v->el[0], v);
387
388         return aff;
389 }
390
391 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
392 {
393         aff = isl_aff_cow(aff);
394         if (!aff)
395                 return NULL;
396
397         aff->v = isl_vec_cow(aff->v);
398         if (!aff->v)
399                 return isl_aff_free(aff);
400
401         isl_int_set(aff->v->el[1], v);
402
403         return aff;
404 }
405
406 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
407 {
408         if (isl_int_is_zero(v))
409                 return aff;
410
411         aff = isl_aff_cow(aff);
412         if (!aff)
413                 return NULL;
414
415         aff->v = isl_vec_cow(aff->v);
416         if (!aff->v)
417                 return isl_aff_free(aff);
418
419         isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
420
421         return aff;
422 }
423
424 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
425 {
426         isl_int t;
427
428         isl_int_init(t);
429         isl_int_set_si(t, v);
430         aff = isl_aff_add_constant(aff, t);
431         isl_int_clear(t);
432
433         return aff;
434 }
435
436 /* Add "v" to the numerator of the constant term of "aff".
437  */
438 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
439 {
440         if (isl_int_is_zero(v))
441                 return aff;
442
443         aff = isl_aff_cow(aff);
444         if (!aff)
445                 return NULL;
446
447         aff->v = isl_vec_cow(aff->v);
448         if (!aff->v)
449                 return isl_aff_free(aff);
450
451         isl_int_add(aff->v->el[1], aff->v->el[1], v);
452
453         return aff;
454 }
455
456 /* Add "v" to the numerator of the constant term of "aff".
457  */
458 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
459 {
460         isl_int t;
461
462         if (v == 0)
463                 return aff;
464
465         isl_int_init(t);
466         isl_int_set_si(t, v);
467         aff = isl_aff_add_constant_num(aff, t);
468         isl_int_clear(t);
469
470         return aff;
471 }
472
473 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
474 {
475         aff = isl_aff_cow(aff);
476         if (!aff)
477                 return NULL;
478
479         aff->v = isl_vec_cow(aff->v);
480         if (!aff->v)
481                 return isl_aff_free(aff);
482
483         isl_int_set_si(aff->v->el[1], v);
484
485         return aff;
486 }
487
488 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
489         enum isl_dim_type type, int pos, isl_int v)
490 {
491         if (!aff)
492                 return NULL;
493
494         if (type == isl_dim_out)
495                 isl_die(aff->v->ctx, isl_error_invalid,
496                         "output/set dimension does not have a coefficient",
497                         return isl_aff_free(aff));
498         if (type == isl_dim_in)
499                 type = isl_dim_set;
500
501         if (pos >= isl_local_space_dim(aff->ls, type))
502                 isl_die(aff->v->ctx, isl_error_invalid,
503                         "position out of bounds", return isl_aff_free(aff));
504
505         aff = isl_aff_cow(aff);
506         if (!aff)
507                 return NULL;
508
509         aff->v = isl_vec_cow(aff->v);
510         if (!aff->v)
511                 return isl_aff_free(aff);
512
513         pos += isl_local_space_offset(aff->ls, type);
514         isl_int_set(aff->v->el[1 + pos], v);
515
516         return aff;
517 }
518
519 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
520         enum isl_dim_type type, int pos, int v)
521 {
522         if (!aff)
523                 return NULL;
524
525         if (type == isl_dim_out)
526                 isl_die(aff->v->ctx, isl_error_invalid,
527                         "output/set dimension does not have a coefficient",
528                         return isl_aff_free(aff));
529         if (type == isl_dim_in)
530                 type = isl_dim_set;
531
532         if (pos >= isl_local_space_dim(aff->ls, type))
533                 isl_die(aff->v->ctx, isl_error_invalid,
534                         "position out of bounds", return isl_aff_free(aff));
535
536         aff = isl_aff_cow(aff);
537         if (!aff)
538                 return NULL;
539
540         aff->v = isl_vec_cow(aff->v);
541         if (!aff->v)
542                 return isl_aff_free(aff);
543
544         pos += isl_local_space_offset(aff->ls, type);
545         isl_int_set_si(aff->v->el[1 + pos], v);
546
547         return aff;
548 }
549
550 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
551         enum isl_dim_type type, int pos, isl_int v)
552 {
553         if (!aff)
554                 return NULL;
555
556         if (type == isl_dim_out)
557                 isl_die(aff->v->ctx, isl_error_invalid,
558                         "output/set dimension does not have a coefficient",
559                         return isl_aff_free(aff));
560         if (type == isl_dim_in)
561                 type = isl_dim_set;
562
563         if (pos >= isl_local_space_dim(aff->ls, type))
564                 isl_die(aff->v->ctx, isl_error_invalid,
565                         "position out of bounds", return isl_aff_free(aff));
566
567         aff = isl_aff_cow(aff);
568         if (!aff)
569                 return NULL;
570
571         aff->v = isl_vec_cow(aff->v);
572         if (!aff->v)
573                 return isl_aff_free(aff);
574
575         pos += isl_local_space_offset(aff->ls, type);
576         isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
577
578         return aff;
579 }
580
581 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
582         enum isl_dim_type type, int pos, int v)
583 {
584         isl_int t;
585
586         isl_int_init(t);
587         isl_int_set_si(t, v);
588         aff = isl_aff_add_coefficient(aff, type, pos, t);
589         isl_int_clear(t);
590
591         return aff;
592 }
593
594 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
595 {
596         if (!aff)
597                 return NULL;
598
599         return isl_local_space_get_div(aff->ls, pos);
600 }
601
602 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
603 {
604         aff = isl_aff_cow(aff);
605         if (!aff)
606                 return NULL;
607         aff->v = isl_vec_cow(aff->v);
608         if (!aff->v)
609                 return isl_aff_free(aff);
610
611         isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
612
613         return aff;
614 }
615
616 /* Remove divs from the local space that do not appear in the affine
617  * expression.
618  * We currently only remove divs at the end.
619  * Some intermediate divs may also not appear directly in the affine
620  * expression, but we would also need to check that no other divs are
621  * defined in terms of them.
622  */
623 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
624 {
625         int pos;
626         int off;
627         int n;
628
629         if (!aff)
630                 return NULL;
631
632         n = isl_local_space_dim(aff->ls, isl_dim_div);
633         off = isl_local_space_offset(aff->ls, isl_dim_div);
634
635         pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
636         if (pos == n)
637                 return aff;
638
639         aff = isl_aff_cow(aff);
640         if (!aff)
641                 return NULL;
642
643         aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
644         aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
645         if (!aff->ls || !aff->v)
646                 return isl_aff_free(aff);
647
648         return aff;
649 }
650
651 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
652 {
653         if (!aff)
654                 return NULL;
655         aff->v = isl_vec_normalize(aff->v);
656         if (!aff->v)
657                 return isl_aff_free(aff);
658         aff = isl_aff_remove_unused_divs(aff);
659         return aff;
660 }
661
662 /* Given f, return floor(f).
663  * If f is an integer expression, then just return f.
664  * If f is a constant, then return the constant floor(f).
665  * Otherwise, if f = g/m, write g = q m + r,
666  * create a new div d = [r/m] and return the expression q + d.
667  * The coefficients in r are taken to lie between -m/2 and m/2.
668  */
669 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
670 {
671         int i;
672         int size;
673         isl_ctx *ctx;
674         isl_vec *div;
675
676         if (!aff)
677                 return NULL;
678
679         if (isl_int_is_one(aff->v->el[0]))
680                 return aff;
681
682         aff = isl_aff_cow(aff);
683         if (!aff)
684                 return NULL;
685
686         aff->v = isl_vec_cow(aff->v);
687         if (!aff->v)
688                 return isl_aff_free(aff);
689
690         if (isl_aff_is_cst(aff)) {
691                 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
692                 isl_int_set_si(aff->v->el[0], 1);
693                 return aff;
694         }
695
696         div = isl_vec_copy(aff->v);
697         div = isl_vec_cow(div);
698         if (!div)
699                 return isl_aff_free(aff);
700
701         ctx = isl_aff_get_ctx(aff);
702         isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
703         for (i = 1; i < aff->v->size; ++i) {
704                 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
705                 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
706                 if (isl_int_gt(div->el[i], aff->v->el[0])) {
707                         isl_int_sub(div->el[i], div->el[i], div->el[0]);
708                         isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
709                 }
710         }
711
712         aff->ls = isl_local_space_add_div(aff->ls, div);
713         if (!aff->ls)
714                 return isl_aff_free(aff);
715
716         size = aff->v->size;
717         aff->v = isl_vec_extend(aff->v, size + 1);
718         if (!aff->v)
719                 return isl_aff_free(aff);
720         isl_int_set_si(aff->v->el[0], 1);
721         isl_int_set_si(aff->v->el[size], 1);
722
723         return aff;
724 }
725
726 /* Compute
727  *
728  *      aff mod m = aff - m * floor(aff/m)
729  */
730 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
731 {
732         isl_aff *res;
733
734         res = isl_aff_copy(aff);
735         aff = isl_aff_scale_down(aff, m);
736         aff = isl_aff_floor(aff);
737         aff = isl_aff_scale(aff, m);
738         res = isl_aff_sub(res, aff);
739
740         return res;
741 }
742
743 /* Compute
744  *
745  *      pwaff mod m = pwaff - m * floor(pwaff/m)
746  */
747 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
748 {
749         isl_pw_aff *res;
750
751         res = isl_pw_aff_copy(pwaff);
752         pwaff = isl_pw_aff_scale_down(pwaff, m);
753         pwaff = isl_pw_aff_floor(pwaff);
754         pwaff = isl_pw_aff_scale(pwaff, m);
755         res = isl_pw_aff_sub(res, pwaff);
756
757         return res;
758 }
759
760 /* Given f, return ceil(f).
761  * If f is an integer expression, then just return f.
762  * Otherwise, create a new div d = [-f] and return the expression -d.
763  */
764 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
765 {
766         if (!aff)
767                 return NULL;
768
769         if (isl_int_is_one(aff->v->el[0]))
770                 return aff;
771
772         aff = isl_aff_neg(aff);
773         aff = isl_aff_floor(aff);
774         aff = isl_aff_neg(aff);
775
776         return aff;
777 }
778
779 /* Apply the expansion computed by isl_merge_divs.
780  * The expansion itself is given by "exp" while the resulting
781  * list of divs is given by "div".
782  */
783 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
784         __isl_take isl_mat *div, int *exp)
785 {
786         int i, j;
787         int old_n_div;
788         int new_n_div;
789         int offset;
790
791         aff = isl_aff_cow(aff);
792         if (!aff || !div)
793                 goto error;
794
795         old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
796         new_n_div = isl_mat_rows(div);
797         if (new_n_div < old_n_div)
798                 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
799                         "not an expansion", goto error);
800
801         aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
802         if (!aff->v)
803                 goto error;
804
805         offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
806         j = old_n_div - 1;
807         for (i = new_n_div - 1; i >= 0; --i) {
808                 if (j >= 0 && exp[j] == i) {
809                         if (i != j)
810                                 isl_int_swap(aff->v->el[offset + i],
811                                              aff->v->el[offset + j]);
812                         j--;
813                 } else
814                         isl_int_set_si(aff->v->el[offset + i], 0);
815         }
816
817         aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
818         if (!aff->ls)
819                 goto error;
820         isl_mat_free(div);
821         return aff;
822 error:
823         isl_aff_free(aff);
824         isl_mat_free(div);
825         return NULL;
826 }
827
828 /* Add two affine expressions that live in the same local space.
829  */
830 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
831         __isl_take isl_aff *aff2)
832 {
833         isl_int gcd, f;
834
835         aff1 = isl_aff_cow(aff1);
836         if (!aff1 || !aff2)
837                 goto error;
838
839         aff1->v = isl_vec_cow(aff1->v);
840         if (!aff1->v)
841                 goto error;
842
843         isl_int_init(gcd);
844         isl_int_init(f);
845         isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
846         isl_int_divexact(f, aff2->v->el[0], gcd);
847         isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
848         isl_int_divexact(f, aff1->v->el[0], gcd);
849         isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
850         isl_int_divexact(f, aff2->v->el[0], gcd);
851         isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
852         isl_int_clear(f);
853         isl_int_clear(gcd);
854
855         isl_aff_free(aff2);
856         return aff1;
857 error:
858         isl_aff_free(aff1);
859         isl_aff_free(aff2);
860         return NULL;
861 }
862
863 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
864         __isl_take isl_aff *aff2)
865 {
866         isl_ctx *ctx;
867         int *exp1 = NULL;
868         int *exp2 = NULL;
869         isl_mat *div;
870
871         if (!aff1 || !aff2)
872                 goto error;
873
874         ctx = isl_aff_get_ctx(aff1);
875         if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
876                 isl_die(ctx, isl_error_invalid,
877                         "spaces don't match", goto error);
878
879         if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
880                 return add_expanded(aff1, aff2);
881
882         exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
883         exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
884         if (!exp1 || !exp2)
885                 goto error;
886
887         div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
888         aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
889         aff2 = isl_aff_expand_divs(aff2, div, exp2);
890         free(exp1);
891         free(exp2);
892
893         return add_expanded(aff1, aff2);
894 error:
895         free(exp1);
896         free(exp2);
897         isl_aff_free(aff1);
898         isl_aff_free(aff2);
899         return NULL;
900 }
901
902 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
903         __isl_take isl_aff *aff2)
904 {
905         return isl_aff_add(aff1, isl_aff_neg(aff2));
906 }
907
908 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
909 {
910         isl_int gcd;
911
912         if (isl_int_is_one(f))
913                 return aff;
914
915         aff = isl_aff_cow(aff);
916         if (!aff)
917                 return NULL;
918         aff->v = isl_vec_cow(aff->v);
919         if (!aff->v)
920                 return isl_aff_free(aff);
921
922         isl_int_init(gcd);
923         isl_int_gcd(gcd, aff->v->el[0], f);
924         isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
925         isl_int_divexact(gcd, f, gcd);
926         isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
927         isl_int_clear(gcd);
928
929         return aff;
930 }
931
932 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
933 {
934         isl_int gcd;
935
936         if (isl_int_is_one(f))
937                 return aff;
938
939         aff = isl_aff_cow(aff);
940         if (!aff)
941                 return NULL;
942         aff->v = isl_vec_cow(aff->v);
943         if (!aff->v)
944                 return isl_aff_free(aff);
945
946         isl_int_init(gcd);
947         isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
948         isl_int_gcd(gcd, gcd, f);
949         isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
950         isl_int_divexact(gcd, f, gcd);
951         isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
952         isl_int_clear(gcd);
953
954         return aff;
955 }
956
957 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
958 {
959         isl_int v;
960
961         if (f == 1)
962                 return aff;
963
964         isl_int_init(v);
965         isl_int_set_ui(v, f);
966         aff = isl_aff_scale_down(aff, v);
967         isl_int_clear(v);
968
969         return aff;
970 }
971
972 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
973         enum isl_dim_type type, unsigned pos, const char *s)
974 {
975         aff = isl_aff_cow(aff);
976         if (!aff)
977                 return NULL;
978         if (type == isl_dim_out)
979                 isl_die(aff->v->ctx, isl_error_invalid,
980                         "cannot set name of output/set dimension",
981                         return isl_aff_free(aff));
982         if (type == isl_dim_in)
983                 type = isl_dim_set;
984         aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
985         if (!aff->ls)
986                 return isl_aff_free(aff);
987
988         return aff;
989 }
990
991 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
992         enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
993 {
994         aff = isl_aff_cow(aff);
995         if (!aff)
996                 return isl_id_free(id);
997         if (type == isl_dim_out)
998                 isl_die(aff->v->ctx, isl_error_invalid,
999                         "cannot set name of output/set dimension",
1000                         goto error);
1001         if (type == isl_dim_in)
1002                 type = isl_dim_set;
1003         aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1004         if (!aff->ls)
1005                 return isl_aff_free(aff);
1006
1007         return aff;
1008 error:
1009         isl_id_free(id);
1010         isl_aff_free(aff);
1011         return NULL;
1012 }
1013
1014 /* Exploit the equalities in "eq" to simplify the affine expression
1015  * and the expressions of the integer divisions in the local space.
1016  * The integer divisions in this local space are assumed to appear
1017  * as regular dimensions in "eq".
1018  */
1019 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
1020         __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1021 {
1022         int i, j;
1023         unsigned total;
1024         unsigned n_div;
1025
1026         if (!eq)
1027                 goto error;
1028         if (eq->n_eq == 0) {
1029                 isl_basic_set_free(eq);
1030                 return aff;
1031         }
1032
1033         aff = isl_aff_cow(aff);
1034         if (!aff)
1035                 goto error;
1036
1037         aff->ls = isl_local_space_substitute_equalities(aff->ls,
1038                                                         isl_basic_set_copy(eq));
1039         if (!aff->ls)
1040                 goto error;
1041
1042         total = 1 + isl_space_dim(eq->dim, isl_dim_all);
1043         n_div = eq->n_div;
1044         for (i = 0; i < eq->n_eq; ++i) {
1045                 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1046                 if (j < 0 || j == 0 || j >= total)
1047                         continue;
1048
1049                 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
1050                                 &aff->v->el[0]);
1051         }
1052
1053         isl_basic_set_free(eq);
1054         aff = isl_aff_normalize(aff);
1055         return aff;
1056 error:
1057         isl_basic_set_free(eq);
1058         isl_aff_free(aff);
1059         return NULL;
1060 }
1061
1062 /* Exploit the equalities in "eq" to simplify the affine expression
1063  * and the expressions of the integer divisions in the local space.
1064  */
1065 static __isl_give isl_aff *isl_aff_substitute_equalities(
1066         __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1067 {
1068         int n_div;
1069
1070         if (!aff || !eq)
1071                 goto error;
1072         n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1073         if (n_div > 0)
1074                 eq = isl_basic_set_add(eq, isl_dim_set, n_div);
1075         return isl_aff_substitute_equalities_lifted(aff, eq);
1076 error:
1077         isl_basic_set_free(eq);
1078         isl_aff_free(aff);
1079         return NULL;
1080 }
1081
1082 /* Look for equalities among the variables shared by context and aff
1083  * and the integer divisions of aff, if any.
1084  * The equalities are then used to eliminate coefficients and/or integer
1085  * divisions from aff.
1086  */
1087 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1088         __isl_take isl_set *context)
1089 {
1090         isl_basic_set *hull;
1091         int n_div;
1092
1093         if (!aff)
1094                 goto error;
1095         n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1096         if (n_div > 0) {
1097                 isl_basic_set *bset;
1098                 isl_local_space *ls;
1099                 context = isl_set_add_dims(context, isl_dim_set, n_div);
1100                 ls = isl_aff_get_domain_local_space(aff);
1101                 bset = isl_basic_set_from_local_space(ls);
1102                 bset = isl_basic_set_lift(bset);
1103                 bset = isl_basic_set_flatten(bset);
1104                 context = isl_set_intersect(context,
1105                                             isl_set_from_basic_set(bset));
1106         }
1107
1108         hull = isl_set_affine_hull(context);
1109         return isl_aff_substitute_equalities_lifted(aff, hull);
1110 error:
1111         isl_aff_free(aff);
1112         isl_set_free(context);
1113         return NULL;
1114 }
1115
1116 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1117         __isl_take isl_set *context)
1118 {
1119         isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1120         dom_context = isl_set_intersect_params(dom_context, context);
1121         return isl_aff_gist(aff, dom_context);
1122 }
1123
1124 /* Return a basic set containing those elements in the space
1125  * of aff where it is non-negative.
1126  */
1127 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1128 {
1129         isl_constraint *ineq;
1130         isl_basic_set *bset;
1131
1132         ineq = isl_inequality_from_aff(aff);
1133
1134         bset = isl_basic_set_from_constraint(ineq);
1135         bset = isl_basic_set_simplify(bset);
1136         return bset;
1137 }
1138
1139 /* Return a basic set containing those elements in the domain space
1140  * of aff where it is negative.
1141  */
1142 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
1143 {
1144         aff = isl_aff_neg(aff);
1145         aff = isl_aff_add_constant_num_si(aff, -1);
1146         return isl_aff_nonneg_basic_set(aff);
1147 }
1148
1149 /* Return a basic set containing those elements in the space
1150  * of aff where it is zero.
1151  */
1152 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1153 {
1154         isl_constraint *ineq;
1155         isl_basic_set *bset;
1156
1157         ineq = isl_equality_from_aff(aff);
1158
1159         bset = isl_basic_set_from_constraint(ineq);
1160         bset = isl_basic_set_simplify(bset);
1161         return bset;
1162 }
1163
1164 /* Return a basic set containing those elements in the shared space
1165  * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1166  */
1167 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1168         __isl_take isl_aff *aff2)
1169 {
1170         aff1 = isl_aff_sub(aff1, aff2);
1171
1172         return isl_aff_nonneg_basic_set(aff1);
1173 }
1174
1175 /* Return a basic set containing those elements in the shared space
1176  * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1177  */
1178 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1179         __isl_take isl_aff *aff2)
1180 {
1181         return isl_aff_ge_basic_set(aff2, aff1);
1182 }
1183
1184 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1185         __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1186 {
1187         aff1 = isl_aff_add(aff1, aff2);
1188         aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1189         return aff1;
1190 }
1191
1192 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1193 {
1194         if (!aff)
1195                 return -1;
1196
1197         return 0;
1198 }
1199
1200 /* Check whether the given affine expression has non-zero coefficient
1201  * for any dimension in the given range or if any of these dimensions
1202  * appear with non-zero coefficients in any of the integer divisions
1203  * involved in the affine expression.
1204  */
1205 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1206         enum isl_dim_type type, unsigned first, unsigned n)
1207 {
1208         int i;
1209         isl_ctx *ctx;
1210         int *active = NULL;
1211         int involves = 0;
1212
1213         if (!aff)
1214                 return -1;
1215         if (n == 0)
1216                 return 0;
1217
1218         ctx = isl_aff_get_ctx(aff);
1219         if (first + n > isl_aff_dim(aff, type))
1220                 isl_die(ctx, isl_error_invalid,
1221                         "range out of bounds", return -1);
1222
1223         active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1224         if (!active)
1225                 goto error;
1226
1227         first += isl_local_space_offset(aff->ls, type) - 1;
1228         for (i = 0; i < n; ++i)
1229                 if (active[first + i]) {
1230                         involves = 1;
1231                         break;
1232                 }
1233
1234         free(active);
1235
1236         return involves;
1237 error:
1238         free(active);
1239         return -1;
1240 }
1241
1242 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1243         enum isl_dim_type type, unsigned first, unsigned n)
1244 {
1245         isl_ctx *ctx;
1246
1247         if (!aff)
1248                 return NULL;
1249         if (type == isl_dim_out)
1250                 isl_die(aff->v->ctx, isl_error_invalid,
1251                         "cannot drop output/set dimension",
1252                         return isl_aff_free(aff));
1253         if (type == isl_dim_in)
1254                 type = isl_dim_set;
1255         if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1256                 return aff;
1257
1258         ctx = isl_aff_get_ctx(aff);
1259         if (first + n > isl_local_space_dim(aff->ls, type))
1260                 isl_die(ctx, isl_error_invalid, "range out of bounds",
1261                         return isl_aff_free(aff));
1262
1263         aff = isl_aff_cow(aff);
1264         if (!aff)
1265                 return NULL;
1266
1267         aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1268         if (!aff->ls)
1269                 return isl_aff_free(aff);
1270
1271         first += 1 + isl_local_space_offset(aff->ls, type);
1272         aff->v = isl_vec_drop_els(aff->v, first, n);
1273         if (!aff->v)
1274                 return isl_aff_free(aff);
1275
1276         return aff;
1277 }
1278
1279 /* Project the domain of the affine expression onto its parameter space.
1280  * The affine expression may not involve any of the domain dimensions.
1281  */
1282 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
1283 {
1284         isl_space *space;
1285         unsigned n;
1286         int involves;
1287
1288         n = isl_aff_dim(aff, isl_dim_in);
1289         involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
1290         if (involves < 0)
1291                 return isl_aff_free(aff);
1292         if (involves)
1293                 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1294                     "affine expression involves some of the domain dimensions",
1295                     return isl_aff_free(aff));
1296         aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
1297         space = isl_aff_get_domain_space(aff);
1298         space = isl_space_params(space);
1299         aff = isl_aff_reset_domain_space(aff, space);
1300         return aff;
1301 }
1302
1303 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1304         enum isl_dim_type type, unsigned first, unsigned n)
1305 {
1306         isl_ctx *ctx;
1307
1308         if (!aff)
1309                 return NULL;
1310         if (type == isl_dim_out)
1311                 isl_die(aff->v->ctx, isl_error_invalid,
1312                         "cannot insert output/set dimensions",
1313                         return isl_aff_free(aff));
1314         if (type == isl_dim_in)
1315                 type = isl_dim_set;
1316         if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1317                 return aff;
1318
1319         ctx = isl_aff_get_ctx(aff);
1320         if (first > isl_local_space_dim(aff->ls, type))
1321                 isl_die(ctx, isl_error_invalid, "position out of bounds",
1322                         return isl_aff_free(aff));
1323
1324         aff = isl_aff_cow(aff);
1325         if (!aff)
1326                 return NULL;
1327
1328         aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1329         if (!aff->ls)
1330                 return isl_aff_free(aff);
1331
1332         first += 1 + isl_local_space_offset(aff->ls, type);
1333         aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1334         if (!aff->v)
1335                 return isl_aff_free(aff);
1336
1337         return aff;
1338 }
1339
1340 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1341         enum isl_dim_type type, unsigned n)
1342 {
1343         unsigned pos;
1344
1345         pos = isl_aff_dim(aff, type);
1346
1347         return isl_aff_insert_dims(aff, type, pos, n);
1348 }
1349
1350 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1351         enum isl_dim_type type, unsigned n)
1352 {
1353         unsigned pos;
1354
1355         pos = isl_pw_aff_dim(pwaff, type);
1356
1357         return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1358 }
1359
1360 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1361 {
1362         isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
1363         return isl_pw_aff_alloc(dom, aff);
1364 }
1365
1366 #undef PW
1367 #define PW isl_pw_aff
1368 #undef EL
1369 #define EL isl_aff
1370 #undef EL_IS_ZERO
1371 #define EL_IS_ZERO is_empty
1372 #undef ZERO
1373 #define ZERO empty
1374 #undef IS_ZERO
1375 #define IS_ZERO is_empty
1376 #undef FIELD
1377 #define FIELD aff
1378 #undef DEFAULT_IS_ZERO
1379 #define DEFAULT_IS_ZERO 0
1380
1381 #define NO_EVAL
1382 #define NO_OPT
1383 #define NO_MOVE_DIMS
1384 #define NO_LIFT
1385 #define NO_MORPH
1386
1387 #include <isl_pw_templ.c>
1388
1389 static __isl_give isl_set *align_params_pw_pw_set_and(
1390         __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1391         __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1392                                     __isl_take isl_pw_aff *pwaff2))
1393 {
1394         if (!pwaff1 || !pwaff2)
1395                 goto error;
1396         if (isl_space_match(pwaff1->dim, isl_dim_param,
1397                           pwaff2->dim, isl_dim_param))
1398                 return fn(pwaff1, pwaff2);
1399         if (!isl_space_has_named_params(pwaff1->dim) ||
1400             !isl_space_has_named_params(pwaff2->dim))
1401                 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1402                         "unaligned unnamed parameters", goto error);
1403         pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
1404         pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
1405         return fn(pwaff1, pwaff2);
1406 error:
1407         isl_pw_aff_free(pwaff1);
1408         isl_pw_aff_free(pwaff2);
1409         return NULL;
1410 }
1411
1412 /* Compute a piecewise quasi-affine expression with a domain that
1413  * is the union of those of pwaff1 and pwaff2 and such that on each
1414  * cell, the quasi-affine expression is the better (according to cmp)
1415  * of those of pwaff1 and pwaff2.  If only one of pwaff1 or pwaff2
1416  * is defined on a given cell, then the associated expression
1417  * is the defined one.
1418  */
1419 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1420         __isl_take isl_pw_aff *pwaff2,
1421         __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
1422                                         __isl_take isl_aff *aff2))
1423 {
1424         int i, j, n;
1425         isl_pw_aff *res;
1426         isl_ctx *ctx;
1427         isl_set *set;
1428
1429         if (!pwaff1 || !pwaff2)
1430                 goto error;
1431
1432         ctx = isl_space_get_ctx(pwaff1->dim);
1433         if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
1434                 isl_die(ctx, isl_error_invalid,
1435                         "arguments should live in same space", goto error);
1436
1437         if (isl_pw_aff_is_empty(pwaff1)) {
1438                 isl_pw_aff_free(pwaff1);
1439                 return pwaff2;
1440         }
1441
1442         if (isl_pw_aff_is_empty(pwaff2)) {
1443                 isl_pw_aff_free(pwaff2);
1444                 return pwaff1;
1445         }
1446
1447         n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1448         res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
1449
1450         for (i = 0; i < pwaff1->n; ++i) {
1451                 set = isl_set_copy(pwaff1->p[i].set);
1452                 for (j = 0; j < pwaff2->n; ++j) {
1453                         struct isl_set *common;
1454                         isl_set *better;
1455
1456                         common = isl_set_intersect(
1457                                         isl_set_copy(pwaff1->p[i].set),
1458                                         isl_set_copy(pwaff2->p[j].set));
1459                         better = isl_set_from_basic_set(cmp(
1460                                         isl_aff_copy(pwaff2->p[j].aff),
1461                                         isl_aff_copy(pwaff1->p[i].aff)));
1462                         better = isl_set_intersect(common, better);
1463                         if (isl_set_plain_is_empty(better)) {
1464                                 isl_set_free(better);
1465                                 continue;
1466                         }
1467                         set = isl_set_subtract(set, isl_set_copy(better));
1468
1469                         res = isl_pw_aff_add_piece(res, better,
1470                                                 isl_aff_copy(pwaff2->p[j].aff));
1471                 }
1472                 res = isl_pw_aff_add_piece(res, set,
1473                                                 isl_aff_copy(pwaff1->p[i].aff));
1474         }
1475
1476         for (j = 0; j < pwaff2->n; ++j) {
1477                 set = isl_set_copy(pwaff2->p[j].set);
1478                 for (i = 0; i < pwaff1->n; ++i)
1479                         set = isl_set_subtract(set,
1480                                         isl_set_copy(pwaff1->p[i].set));
1481                 res = isl_pw_aff_add_piece(res, set,
1482                                                 isl_aff_copy(pwaff2->p[j].aff));
1483         }
1484
1485         isl_pw_aff_free(pwaff1);
1486         isl_pw_aff_free(pwaff2);
1487
1488         return res;
1489 error:
1490         isl_pw_aff_free(pwaff1);
1491         isl_pw_aff_free(pwaff2);
1492         return NULL;
1493 }
1494
1495 /* Compute a piecewise quasi-affine expression with a domain that
1496  * is the union of those of pwaff1 and pwaff2 and such that on each
1497  * cell, the quasi-affine expression is the maximum of those of pwaff1
1498  * and pwaff2.  If only one of pwaff1 or pwaff2 is defined on a given
1499  * cell, then the associated expression is the defined one.
1500  */
1501 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1502         __isl_take isl_pw_aff *pwaff2)
1503 {
1504         return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
1505 }
1506
1507 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1508         __isl_take isl_pw_aff *pwaff2)
1509 {
1510         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1511                                                         &pw_aff_union_max);
1512 }
1513
1514 /* Compute a piecewise quasi-affine expression with a domain that
1515  * is the union of those of pwaff1 and pwaff2 and such that on each
1516  * cell, the quasi-affine expression is the minimum of those of pwaff1
1517  * and pwaff2.  If only one of pwaff1 or pwaff2 is defined on a given
1518  * cell, then the associated expression is the defined one.
1519  */
1520 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1521         __isl_take isl_pw_aff *pwaff2)
1522 {
1523         return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
1524 }
1525
1526 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1527         __isl_take isl_pw_aff *pwaff2)
1528 {
1529         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1530                                                         &pw_aff_union_min);
1531 }
1532
1533 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1534         __isl_take isl_pw_aff *pwaff2, int max)
1535 {
1536         if (max)
1537                 return isl_pw_aff_union_max(pwaff1, pwaff2);
1538         else
1539                 return isl_pw_aff_union_min(pwaff1, pwaff2);
1540 }
1541
1542 /* Construct a map with as domain the domain of pwaff and
1543  * one-dimensional range corresponding to the affine expressions.
1544  */
1545 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1546 {
1547         int i;
1548         isl_space *dim;
1549         isl_map *map;
1550
1551         if (!pwaff)
1552                 return NULL;
1553
1554         dim = isl_pw_aff_get_space(pwaff);
1555         map = isl_map_empty(dim);
1556
1557         for (i = 0; i < pwaff->n; ++i) {
1558                 isl_basic_map *bmap;
1559                 isl_map *map_i;
1560
1561                 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1562                 map_i = isl_map_from_basic_map(bmap);
1563                 map_i = isl_map_intersect_domain(map_i,
1564                                                 isl_set_copy(pwaff->p[i].set));
1565                 map = isl_map_union_disjoint(map, map_i);
1566         }
1567
1568         isl_pw_aff_free(pwaff);
1569
1570         return map;
1571 }
1572
1573 /* Construct a map with as domain the domain of pwaff and
1574  * one-dimensional range corresponding to the affine expressions.
1575  */
1576 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1577 {
1578         if (!pwaff)
1579                 return NULL;
1580         if (isl_space_is_set(pwaff->dim))
1581                 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1582                         "space of input is not a map",
1583                         return isl_pw_aff_free(pwaff));
1584         return map_from_pw_aff(pwaff);
1585 }
1586
1587 /* Construct a one-dimensional set with as parameter domain
1588  * the domain of pwaff and the single set dimension
1589  * corresponding to the affine expressions.
1590  */
1591 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1592 {
1593         if (!pwaff)
1594                 return NULL;
1595         if (!isl_space_is_set(pwaff->dim))
1596                 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1597                         "space of input is not a set",
1598                         return isl_pw_aff_free(pwaff));
1599         return map_from_pw_aff(pwaff);
1600 }
1601
1602 /* Return a set containing those elements in the domain
1603  * of pwaff where it is non-negative.
1604  */
1605 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1606 {
1607         int i;
1608         isl_set *set;
1609
1610         if (!pwaff)
1611                 return NULL;
1612
1613         set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1614
1615         for (i = 0; i < pwaff->n; ++i) {
1616                 isl_basic_set *bset;
1617                 isl_set *set_i;
1618
1619                 bset = isl_aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff));
1620                 set_i = isl_set_from_basic_set(bset);
1621                 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1622                 set = isl_set_union_disjoint(set, set_i);
1623         }
1624
1625         isl_pw_aff_free(pwaff);
1626
1627         return set;
1628 }
1629
1630 /* Return a set containing those elements in the domain
1631  * of pwaff where it is zero (if complement is 0) or not zero
1632  * (if complement is 1).
1633  */
1634 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
1635         int complement)
1636 {
1637         int i;
1638         isl_set *set;
1639
1640         if (!pwaff)
1641                 return NULL;
1642
1643         set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1644
1645         for (i = 0; i < pwaff->n; ++i) {
1646                 isl_basic_set *bset;
1647                 isl_set *set_i, *zero;
1648
1649                 bset = isl_aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff));
1650                 zero = isl_set_from_basic_set(bset);
1651                 set_i = isl_set_copy(pwaff->p[i].set);
1652                 if (complement)
1653                         set_i = isl_set_subtract(set_i, zero);
1654                 else
1655                         set_i = isl_set_intersect(set_i, zero);
1656                 set = isl_set_union_disjoint(set, set_i);
1657         }
1658
1659         isl_pw_aff_free(pwaff);
1660
1661         return set;
1662 }
1663
1664 /* Return a set containing those elements in the domain
1665  * of pwaff where it is zero.
1666  */
1667 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
1668 {
1669         return pw_aff_zero_set(pwaff, 0);
1670 }
1671
1672 /* Return a set containing those elements in the domain
1673  * of pwaff where it is not zero.
1674  */
1675 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
1676 {
1677         return pw_aff_zero_set(pwaff, 1);
1678 }
1679
1680 /* Return a set containing those elements in the shared domain
1681  * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1682  *
1683  * We compute the difference on the shared domain and then construct
1684  * the set of values where this difference is non-negative.
1685  * If strict is set, we first subtract 1 from the difference.
1686  * If equal is set, we only return the elements where pwaff1 and pwaff2
1687  * are equal.
1688  */
1689 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
1690         __isl_take isl_pw_aff *pwaff2, int strict, int equal)
1691 {
1692         isl_set *set1, *set2;
1693
1694         set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
1695         set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
1696         set1 = isl_set_intersect(set1, set2);
1697         pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
1698         pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
1699         pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
1700
1701         if (strict) {
1702                 isl_space *dim = isl_set_get_space(set1);
1703                 isl_aff *aff;
1704                 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1705                 aff = isl_aff_add_constant_si(aff, -1);
1706                 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
1707         } else
1708                 isl_set_free(set1);
1709
1710         if (equal)
1711                 return isl_pw_aff_zero_set(pwaff1);
1712         return isl_pw_aff_nonneg_set(pwaff1);
1713 }
1714
1715 /* Return a set containing those elements in the shared domain
1716  * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1717  */
1718 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1719         __isl_take isl_pw_aff *pwaff2)
1720 {
1721         return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
1722 }
1723
1724 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1725         __isl_take isl_pw_aff *pwaff2)
1726 {
1727         return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
1728 }
1729
1730 /* Return a set containing those elements in the shared domain
1731  * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1732  */
1733 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1734         __isl_take isl_pw_aff *pwaff2)
1735 {
1736         return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
1737 }
1738
1739 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1740         __isl_take isl_pw_aff *pwaff2)
1741 {
1742         return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
1743 }
1744
1745 /* Return a set containing those elements in the shared domain
1746  * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1747  */
1748 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1749         __isl_take isl_pw_aff *pwaff2)
1750 {
1751         return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
1752 }
1753
1754 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1755         __isl_take isl_pw_aff *pwaff2)
1756 {
1757         return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
1758 }
1759
1760 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
1761         __isl_take isl_pw_aff *pwaff2)
1762 {
1763         return isl_pw_aff_ge_set(pwaff2, pwaff1);
1764 }
1765
1766 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
1767         __isl_take isl_pw_aff *pwaff2)
1768 {
1769         return isl_pw_aff_gt_set(pwaff2, pwaff1);
1770 }
1771
1772 /* Return a set containing those elements in the shared domain
1773  * of the elements of list1 and list2 where each element in list1
1774  * has the relation specified by "fn" with each element in list2.
1775  */
1776 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
1777         __isl_take isl_pw_aff_list *list2,
1778         __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1779                                     __isl_take isl_pw_aff *pwaff2))
1780 {
1781         int i, j;
1782         isl_ctx *ctx;
1783         isl_set *set;
1784
1785         if (!list1 || !list2)
1786                 goto error;
1787
1788         ctx = isl_pw_aff_list_get_ctx(list1);
1789         if (list1->n < 1 || list2->n < 1)
1790                 isl_die(ctx, isl_error_invalid,
1791                         "list should contain at least one element", goto error);
1792
1793         set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
1794         for (i = 0; i < list1->n; ++i)
1795                 for (j = 0; j < list2->n; ++j) {
1796                         isl_set *set_ij;
1797
1798                         set_ij = fn(isl_pw_aff_copy(list1->p[i]),
1799                                     isl_pw_aff_copy(list2->p[j]));
1800                         set = isl_set_intersect(set, set_ij);
1801                 }
1802
1803         isl_pw_aff_list_free(list1);
1804         isl_pw_aff_list_free(list2);
1805         return set;
1806 error:
1807         isl_pw_aff_list_free(list1);
1808         isl_pw_aff_list_free(list2);
1809         return NULL;
1810 }
1811
1812 /* Return a set containing those elements in the shared domain
1813  * of the elements of list1 and list2 where each element in list1
1814  * is equal to each element in list2.
1815  */
1816 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
1817         __isl_take isl_pw_aff_list *list2)
1818 {
1819         return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
1820 }
1821
1822 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
1823         __isl_take isl_pw_aff_list *list2)
1824 {
1825         return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
1826 }
1827
1828 /* Return a set containing those elements in the shared domain
1829  * of the elements of list1 and list2 where each element in list1
1830  * is less than or equal to each element in list2.
1831  */
1832 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
1833         __isl_take isl_pw_aff_list *list2)
1834 {
1835         return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
1836 }
1837
1838 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
1839         __isl_take isl_pw_aff_list *list2)
1840 {
1841         return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
1842 }
1843
1844 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
1845         __isl_take isl_pw_aff_list *list2)
1846 {
1847         return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
1848 }
1849
1850 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
1851         __isl_take isl_pw_aff_list *list2)
1852 {
1853         return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
1854 }
1855
1856
1857 /* Return a set containing those elements in the shared domain
1858  * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1859  */
1860 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1861         __isl_take isl_pw_aff *pwaff2)
1862 {
1863         isl_set *set_lt, *set_gt;
1864
1865         set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
1866                                    isl_pw_aff_copy(pwaff2));
1867         set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
1868         return isl_set_union_disjoint(set_lt, set_gt);
1869 }
1870
1871 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1872         __isl_take isl_pw_aff *pwaff2)
1873 {
1874         return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
1875 }
1876
1877 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
1878         isl_int v)
1879 {
1880         int i;
1881
1882         if (isl_int_is_one(v))
1883                 return pwaff;
1884         if (!isl_int_is_pos(v))
1885                 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1886                         "factor needs to be positive",
1887                         return isl_pw_aff_free(pwaff));
1888         pwaff = isl_pw_aff_cow(pwaff);
1889         if (!pwaff)
1890                 return NULL;
1891         if (pwaff->n == 0)
1892                 return pwaff;
1893
1894         for (i = 0; i < pwaff->n; ++i) {
1895                 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
1896                 if (!pwaff->p[i].aff)
1897                         return isl_pw_aff_free(pwaff);
1898         }
1899
1900         return pwaff;
1901 }
1902
1903 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
1904 {
1905         int i;
1906
1907         pwaff = isl_pw_aff_cow(pwaff);
1908         if (!pwaff)
1909                 return NULL;
1910         if (pwaff->n == 0)
1911                 return pwaff;
1912
1913         for (i = 0; i < pwaff->n; ++i) {
1914                 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
1915                 if (!pwaff->p[i].aff)
1916                         return isl_pw_aff_free(pwaff);
1917         }
1918
1919         return pwaff;
1920 }
1921
1922 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
1923 {
1924         int i;
1925
1926         pwaff = isl_pw_aff_cow(pwaff);
1927         if (!pwaff)
1928                 return NULL;
1929         if (pwaff->n == 0)
1930                 return pwaff;
1931
1932         for (i = 0; i < pwaff->n; ++i) {
1933                 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
1934                 if (!pwaff->p[i].aff)
1935                         return isl_pw_aff_free(pwaff);
1936         }
1937
1938         return pwaff;
1939 }
1940
1941 /* Assuming that "cond1" and "cond2" are disjoint,
1942  * return an affine expression that is equal to pwaff1 on cond1
1943  * and to pwaff2 on cond2.
1944  */
1945 static __isl_give isl_pw_aff *isl_pw_aff_select(
1946         __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
1947         __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
1948 {
1949         pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
1950         pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
1951
1952         return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
1953 }
1954
1955 /* Return an affine expression that is equal to pwaff_true for elements
1956  * where "cond" is non-zero and to pwaff_false for elements where "cond"
1957  * is zero.
1958  * That is, return cond ? pwaff_true : pwaff_false;
1959  */
1960 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
1961         __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
1962 {
1963         isl_set *cond_true, *cond_false;
1964
1965         cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
1966         cond_false = isl_pw_aff_zero_set(cond);
1967         return isl_pw_aff_select(cond_true, pwaff_true,
1968                                  cond_false, pwaff_false);
1969 }
1970
1971 int isl_aff_is_cst(__isl_keep isl_aff *aff)
1972 {
1973         if (!aff)
1974                 return -1;
1975
1976         return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
1977 }
1978
1979 /* Check whether pwaff is a piecewise constant.
1980  */
1981 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
1982 {
1983         int i;
1984
1985         if (!pwaff)
1986                 return -1;
1987
1988         for (i = 0; i < pwaff->n; ++i) {
1989                 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
1990                 if (is_cst < 0 || !is_cst)
1991                         return is_cst;
1992         }
1993
1994         return 1;
1995 }
1996
1997 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
1998         __isl_take isl_aff *aff2)
1999 {
2000         if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
2001                 return isl_aff_mul(aff2, aff1);
2002
2003         if (!isl_aff_is_cst(aff2))
2004                 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
2005                         "at least one affine expression should be constant",
2006                         goto error);
2007
2008         aff1 = isl_aff_cow(aff1);
2009         if (!aff1 || !aff2)
2010                 goto error;
2011
2012         aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
2013         aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
2014
2015         isl_aff_free(aff2);
2016         return aff1;
2017 error:
2018         isl_aff_free(aff1);
2019         isl_aff_free(aff2);
2020         return NULL;
2021 }
2022
2023 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2024         __isl_take isl_pw_aff *pwaff2)
2025 {
2026         return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
2027 }
2028
2029 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2030         __isl_take isl_pw_aff *pwaff2)
2031 {
2032         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
2033 }
2034
2035 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
2036         __isl_take isl_pw_aff *pwaff2)
2037 {
2038         return isl_pw_aff_union_add_(pwaff1, pwaff2);
2039 }
2040
2041 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2042         __isl_take isl_pw_aff *pwaff2)
2043 {
2044         return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
2045 }
2046
2047 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2048         __isl_take isl_pw_aff *pwaff2)
2049 {
2050         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
2051 }
2052
2053 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2054         __isl_take isl_pw_aff *pwaff2)
2055 {
2056         isl_set *le;
2057         isl_set *dom;
2058
2059         dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2060                                 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2061         le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
2062                                 isl_pw_aff_copy(pwaff2));
2063         dom = isl_set_subtract(dom, isl_set_copy(le));
2064         return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
2065 }
2066
2067 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2068         __isl_take isl_pw_aff *pwaff2)
2069 {
2070         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
2071 }
2072
2073 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2074         __isl_take isl_pw_aff *pwaff2)
2075 {
2076         isl_set *ge;
2077         isl_set *dom;
2078
2079         dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2080                                 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2081         ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
2082                                 isl_pw_aff_copy(pwaff2));
2083         dom = isl_set_subtract(dom, isl_set_copy(ge));
2084         return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
2085 }
2086
2087 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2088         __isl_take isl_pw_aff *pwaff2)
2089 {
2090         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
2091 }
2092
2093 static __isl_give isl_pw_aff *pw_aff_list_reduce(
2094         __isl_take isl_pw_aff_list *list,
2095         __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
2096                                         __isl_take isl_pw_aff *pwaff2))
2097 {
2098         int i;
2099         isl_ctx *ctx;
2100         isl_pw_aff *res;
2101
2102         if (!list)
2103                 return NULL;
2104
2105         ctx = isl_pw_aff_list_get_ctx(list);
2106         if (list->n < 1)
2107                 isl_die(ctx, isl_error_invalid,
2108                         "list should contain at least one element",
2109                         return isl_pw_aff_list_free(list));
2110
2111         res = isl_pw_aff_copy(list->p[0]);
2112         for (i = 1; i < list->n; ++i)
2113                 res = fn(res, isl_pw_aff_copy(list->p[i]));
2114
2115         isl_pw_aff_list_free(list);
2116         return res;
2117 }
2118
2119 /* Return an isl_pw_aff that maps each element in the intersection of the
2120  * domains of the elements of list to the minimal corresponding affine
2121  * expression.
2122  */
2123 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
2124 {
2125         return pw_aff_list_reduce(list, &isl_pw_aff_min);
2126 }
2127
2128 /* Return an isl_pw_aff that maps each element in the intersection of the
2129  * domains of the elements of list to the maximal corresponding affine
2130  * expression.
2131  */
2132 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
2133 {
2134         return pw_aff_list_reduce(list, &isl_pw_aff_max);
2135 }
2136
2137 #undef BASE
2138 #define BASE aff
2139
2140 #include <isl_multi_templ.c>
2141
2142 /* Construct an isl_multi_aff in the given space with value zero in
2143  * each of the output dimensions.
2144  */
2145 __isl_give isl_multi_aff *isl_multi_aff_zero(__isl_take isl_space *space)
2146 {
2147         int n;
2148         isl_multi_aff *ma;
2149
2150         if (!space)
2151                 return NULL;
2152
2153         n = isl_space_dim(space , isl_dim_out);
2154         ma = isl_multi_aff_alloc(isl_space_copy(space));
2155
2156         if (!n)
2157                 isl_space_free(space);
2158         else {
2159                 int i;
2160                 isl_local_space *ls;
2161                 isl_aff *aff;
2162
2163                 space = isl_space_domain(space);
2164                 ls = isl_local_space_from_space(space);
2165                 aff = isl_aff_zero_on_domain(ls);
2166
2167                 for (i = 0; i < n; ++i)
2168                         ma = isl_multi_aff_set_aff(ma, i, isl_aff_copy(aff));
2169
2170                 isl_aff_free(aff);
2171         }
2172
2173         return ma;
2174 }
2175
2176 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
2177  * domain.
2178  */
2179 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
2180         __isl_take isl_multi_aff *ma)
2181 {
2182         isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
2183         return isl_pw_multi_aff_alloc(dom, ma);
2184 }
2185
2186 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
2187         __isl_take isl_multi_aff *maff2)
2188 {
2189         int i;
2190         isl_ctx *ctx;
2191
2192         maff1 = isl_multi_aff_cow(maff1);
2193         if (!maff1 || !maff2)
2194                 goto error;
2195
2196         ctx = isl_multi_aff_get_ctx(maff1);
2197         if (!isl_space_is_equal(maff1->space, maff2->space))
2198                 isl_die(ctx, isl_error_invalid,
2199                         "spaces don't match", goto error);
2200
2201         for (i = 0; i < maff1->n; ++i) {
2202                 maff1->p[i] = isl_aff_add(maff1->p[i],
2203                                             isl_aff_copy(maff2->p[i]));
2204                 if (!maff1->p[i])
2205                         goto error;
2206         }
2207
2208         isl_multi_aff_free(maff2);
2209         return maff1;
2210 error:
2211         isl_multi_aff_free(maff1);
2212         isl_multi_aff_free(maff2);
2213         return NULL;
2214 }
2215
2216 /* Exploit the equalities in "eq" to simplify the affine expressions.
2217  */
2218 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
2219         __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
2220 {
2221         int i;
2222
2223         maff = isl_multi_aff_cow(maff);
2224         if (!maff || !eq)
2225                 goto error;
2226
2227         for (i = 0; i < maff->n; ++i) {
2228                 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
2229                                                     isl_basic_set_copy(eq));
2230                 if (!maff->p[i])
2231                         goto error;
2232         }
2233
2234         isl_basic_set_free(eq);
2235         return maff;
2236 error:
2237         isl_basic_set_free(eq);
2238         isl_multi_aff_free(maff);
2239         return NULL;
2240 }
2241
2242 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
2243         isl_int f)
2244 {
2245         int i;
2246
2247         maff = isl_multi_aff_cow(maff);
2248         if (!maff)
2249                 return NULL;
2250
2251         for (i = 0; i < maff->n; ++i) {
2252                 maff->p[i] = isl_aff_scale(maff->p[i], f);
2253                 if (!maff->p[i])
2254                         return isl_multi_aff_free(maff);
2255         }
2256
2257         return maff;
2258 }
2259
2260 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
2261         __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
2262 {
2263         maff1 = isl_multi_aff_add(maff1, maff2);
2264         maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
2265         return maff1;
2266 }
2267
2268 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
2269 {
2270         if (!maff)
2271                 return -1;
2272
2273         return 0;
2274 }
2275
2276 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
2277         __isl_keep isl_multi_aff *maff2)
2278 {
2279         int i;
2280         int equal;
2281
2282         if (!maff1 || !maff2)
2283                 return -1;
2284         if (maff1->n != maff2->n)
2285                 return 0;
2286         equal = isl_space_is_equal(maff1->space, maff2->space);
2287         if (equal < 0 || !equal)
2288                 return equal;
2289
2290         for (i = 0; i < maff1->n; ++i) {
2291                 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
2292                 if (equal < 0 || !equal)
2293                         return equal;
2294         }
2295
2296         return 1;
2297 }
2298
2299 __isl_give isl_multi_aff *isl_multi_aff_set_dim_name(
2300         __isl_take isl_multi_aff *maff,
2301         enum isl_dim_type type, unsigned pos, const char *s)
2302 {
2303         int i;
2304
2305         maff = isl_multi_aff_cow(maff);
2306         if (!maff)
2307                 return NULL;
2308
2309         maff->space = isl_space_set_dim_name(maff->space, type, pos, s);
2310         if (!maff->space)
2311                 return isl_multi_aff_free(maff);
2312
2313         if (type == isl_dim_out)
2314                 return maff;
2315         for (i = 0; i < maff->n; ++i) {
2316                 maff->p[i] = isl_aff_set_dim_name(maff->p[i], type, pos, s);
2317                 if (!maff->p[i])
2318                         return isl_multi_aff_free(maff);
2319         }
2320
2321         return maff;
2322 }
2323
2324 __isl_give isl_multi_aff *isl_multi_aff_drop_dims(__isl_take isl_multi_aff *maff,
2325         enum isl_dim_type type, unsigned first, unsigned n)
2326 {
2327         int i;
2328
2329         maff = isl_multi_aff_cow(maff);
2330         if (!maff)
2331                 return NULL;
2332
2333         maff->space = isl_space_drop_dims(maff->space, type, first, n);
2334         if (!maff->space)
2335                 return isl_multi_aff_free(maff);
2336
2337         if (type == isl_dim_out) {
2338                 for (i = 0; i < n; ++i)
2339                         isl_aff_free(maff->p[first + i]);
2340                 for (i = first; i + n < maff->n; ++i)
2341                         maff->p[i] = maff->p[i + n];
2342                 maff->n -= n;
2343                 return maff;
2344         }
2345
2346         for (i = 0; i < maff->n; ++i) {
2347                 maff->p[i] = isl_aff_drop_dims(maff->p[i], type, first, n);
2348                 if (!maff->p[i])
2349                         return isl_multi_aff_free(maff);
2350         }
2351
2352         return maff;
2353 }
2354
2355 #undef PW
2356 #define PW isl_pw_multi_aff
2357 #undef EL
2358 #define EL isl_multi_aff
2359 #undef EL_IS_ZERO
2360 #define EL_IS_ZERO is_empty
2361 #undef ZERO
2362 #define ZERO empty
2363 #undef IS_ZERO
2364 #define IS_ZERO is_empty
2365 #undef FIELD
2366 #define FIELD maff
2367 #undef DEFAULT_IS_ZERO
2368 #define DEFAULT_IS_ZERO 0
2369
2370 #define NO_NEG
2371 #define NO_EVAL
2372 #define NO_OPT
2373 #define NO_INVOLVES_DIMS
2374 #define NO_MOVE_DIMS
2375 #define NO_INSERT_DIMS
2376 #define NO_LIFT
2377 #define NO_MORPH
2378
2379 #include <isl_pw_templ.c>
2380
2381 #undef UNION
2382 #define UNION isl_union_pw_multi_aff
2383 #undef PART
2384 #define PART isl_pw_multi_aff
2385 #undef PARTS
2386 #define PARTS pw_multi_aff
2387 #define ALIGN_DOMAIN
2388
2389 #define NO_EVAL
2390
2391 #include <isl_union_templ.c>
2392
2393 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
2394         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2395 {
2396         return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
2397                                                 &isl_multi_aff_add);
2398 }
2399
2400 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
2401         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2402 {
2403         return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
2404                                                 &pw_multi_aff_add);
2405 }
2406
2407 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
2408         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2409 {
2410         return isl_pw_multi_aff_union_add_(pma1, pma2);
2411 }
2412
2413 /* Construct a map mapping the domain of the piecewise multi-affine expression
2414  * to its range, with each dimension in the range equated to the
2415  * corresponding affine expression on its cell.
2416  */
2417 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2418 {
2419         int i;
2420         isl_map *map;
2421
2422         if (!pma)
2423                 return NULL;
2424
2425         map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
2426
2427         for (i = 0; i < pma->n; ++i) {
2428                 isl_multi_aff *maff;
2429                 isl_basic_map *bmap;
2430                 isl_map *map_i;
2431
2432                 maff = isl_multi_aff_copy(pma->p[i].maff);
2433                 bmap = isl_basic_map_from_multi_aff(maff);
2434                 map_i = isl_map_from_basic_map(bmap);
2435                 map_i = isl_map_intersect_domain(map_i,
2436                                                 isl_set_copy(pma->p[i].set));
2437                 map = isl_map_union_disjoint(map, map_i);
2438         }
2439
2440         isl_pw_multi_aff_free(pma);
2441         return map;
2442 }
2443
2444 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2445 {
2446         if (!isl_space_is_set(pma->dim))
2447                 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
2448                         "isl_pw_multi_aff cannot be converted into an isl_set",
2449                         return isl_pw_multi_aff_free(pma));
2450
2451         return isl_map_from_pw_multi_aff(pma);
2452 }
2453
2454 /* Given a basic map with a single output dimension that is defined
2455  * in terms of the parameters and input dimensions using an equality,
2456  * extract an isl_aff that expresses the output dimension in terms
2457  * of the parameters and input dimensions.
2458  *
2459  * Since some applications expect the result of isl_pw_multi_aff_from_map
2460  * to only contain integer affine expressions, we compute the floor
2461  * of the expression before returning.
2462  *
2463  * This function shares some similarities with
2464  * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
2465  */
2466 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
2467         __isl_take isl_basic_map *bmap)
2468 {
2469         int i;
2470         unsigned offset;
2471         unsigned total;
2472         isl_local_space *ls;
2473         isl_aff *aff;
2474
2475         if (!bmap)
2476                 return NULL;
2477         if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
2478                 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
2479                         "basic map should have a single output dimension",
2480                         goto error);
2481         offset = isl_basic_map_offset(bmap, isl_dim_out);
2482         total = isl_basic_map_total_dim(bmap);
2483         for (i = 0; i < bmap->n_eq; ++i) {
2484                 if (isl_int_is_zero(bmap->eq[i][offset]))
2485                         continue;
2486                 if (isl_seq_first_non_zero(bmap->eq[i] + offset + 1,
2487                                            1 + total - (offset + 1)) != -1)
2488                         continue;
2489                 break;
2490         }
2491         if (i >= bmap->n_eq)
2492                 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
2493                         "unable to find suitable equality", goto error);
2494         ls = isl_basic_map_get_local_space(bmap);
2495         aff = isl_aff_alloc(isl_local_space_domain(ls));
2496         if (!aff)
2497                 goto error;
2498         if (isl_int_is_neg(bmap->eq[i][offset]))
2499                 isl_seq_cpy(aff->v->el + 1, bmap->eq[i], offset);
2500         else
2501                 isl_seq_neg(aff->v->el + 1, bmap->eq[i], offset);
2502         isl_seq_clr(aff->v->el + 1 + offset, aff->v->size - (1 + offset));
2503         isl_int_abs(aff->v->el[0], bmap->eq[i][offset]);
2504         isl_basic_map_free(bmap);
2505
2506         aff = isl_aff_remove_unused_divs(aff);
2507         aff = isl_aff_floor(aff);
2508         return aff;
2509 error:
2510         isl_basic_map_free(bmap);
2511         return NULL;
2512 }
2513
2514 /* Given a basic map where each output dimension is defined
2515  * in terms of the parameters and input dimensions using an equality,
2516  * extract an isl_multi_aff that expresses the output dimensions in terms
2517  * of the parameters and input dimensions.
2518  */
2519 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
2520         __isl_take isl_basic_map *bmap)
2521 {
2522         int i;
2523         unsigned n_out;
2524         isl_multi_aff *ma;
2525
2526         if (!bmap)
2527                 return NULL;
2528
2529         ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
2530         n_out = isl_basic_map_dim(bmap, isl_dim_out);
2531
2532         for (i = 0; i < n_out; ++i) {
2533                 isl_basic_map *bmap_i;
2534                 isl_aff *aff;
2535
2536                 bmap_i = isl_basic_map_copy(bmap);
2537                 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
2538                                                         i + 1, n_out - (1 + i));
2539                 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
2540                 aff = extract_isl_aff_from_basic_map(bmap_i);
2541                 ma = isl_multi_aff_set_aff(ma, i, aff);
2542         }
2543
2544         isl_basic_map_free(bmap);
2545
2546         return ma;
2547 }
2548
2549 /* Create an isl_pw_multi_aff that is equivalent to
2550  * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
2551  * The given basic map is such that each output dimension is defined
2552  * in terms of the parameters and input dimensions using an equality.
2553  */
2554 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
2555         __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
2556 {
2557         isl_multi_aff *ma;
2558
2559         ma = extract_isl_multi_aff_from_basic_map(bmap);
2560         return isl_pw_multi_aff_alloc(domain, ma);
2561 }
2562
2563 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
2564  * This obivously only works if the input "map" is single-valued.
2565  * If so, we compute the lexicographic minimum of the image in the form
2566  * of an isl_pw_multi_aff.  Since the image is unique, it is equal
2567  * to its lexicographic minimum.
2568  * If the input is not single-valued, we produce an error.
2569  *
2570  * As a special case, we first check if all output dimensions are uniquely
2571  * defined in terms of the parameters and input dimensions over the entire
2572  * domain.  If so, we extract the desired isl_pw_multi_aff directly
2573  * from the affine hull of "map" and its domain.
2574  */
2575 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
2576 {
2577         int i;
2578         int sv;
2579         isl_pw_multi_aff *pma;
2580         isl_basic_map *hull;
2581
2582         if (!map)
2583                 return NULL;
2584
2585         hull = isl_map_affine_hull(isl_map_copy(map));
2586         sv = isl_basic_map_plain_is_single_valued(hull);
2587         if (sv >= 0 && sv)
2588                 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
2589         isl_basic_map_free(hull);
2590         if (sv < 0)
2591                 goto error;
2592
2593         sv = isl_map_is_single_valued(map);
2594         if (sv < 0)
2595                 goto error;
2596         if (!sv)
2597                 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2598                         "map is not single-valued", goto error);
2599         map = isl_map_make_disjoint(map);
2600         if (!map)
2601                 return NULL;
2602
2603         pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
2604
2605         for (i = 0; i < map->n; ++i) {
2606                 isl_pw_multi_aff *pma_i;
2607                 isl_basic_map *bmap;
2608                 bmap = isl_basic_map_copy(map->p[i]);
2609                 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
2610                 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
2611         }
2612
2613         isl_map_free(map);
2614         return pma;
2615 error:
2616         isl_map_free(map);
2617         return NULL;
2618 }
2619
2620 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
2621 {
2622         return isl_pw_multi_aff_from_map(set);
2623 }
2624
2625 /* Return the piecewise affine expression "set ? 1 : 0".
2626  */
2627 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
2628 {
2629         isl_pw_aff *pa;
2630         isl_space *space = isl_set_get_space(set);
2631         isl_local_space *ls = isl_local_space_from_space(space);
2632         isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
2633         isl_aff *one = isl_aff_zero_on_domain(ls);
2634
2635         one = isl_aff_add_constant_si(one, 1);
2636         pa = isl_pw_aff_alloc(isl_set_copy(set), one);
2637         set = isl_set_complement(set);
2638         pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
2639
2640         return pa;
2641 }
2642
2643 /* Plug in "subs" for dimension "type", "pos" of "aff".
2644  *
2645  * Let i be the dimension to replace and let "subs" be of the form
2646  *
2647  *      f/d
2648  *
2649  * and "aff" of the form
2650  *
2651  *      (a i + g)/m
2652  *
2653  * The result is
2654  *
2655  *      floor((a f + d g')/(m d))
2656  *
2657  * where g' is the result of plugging in "subs" in each of the integer
2658  * divisions in g.
2659  */
2660 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
2661         enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
2662 {
2663         isl_ctx *ctx;
2664         isl_int v;
2665
2666         aff = isl_aff_cow(aff);
2667         if (!aff || !subs)
2668                 return isl_aff_free(aff);
2669
2670         ctx = isl_aff_get_ctx(aff);
2671         if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
2672                 isl_die(ctx, isl_error_invalid,
2673                         "spaces don't match", return isl_aff_free(aff));
2674         if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
2675                 isl_die(ctx, isl_error_unsupported,
2676                         "cannot handle divs yet", return isl_aff_free(aff));
2677
2678         aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
2679         if (!aff->ls)
2680                 return isl_aff_free(aff);
2681
2682         aff->v = isl_vec_cow(aff->v);
2683         if (!aff->v)
2684                 return isl_aff_free(aff);
2685
2686         pos += isl_local_space_offset(aff->ls, type);
2687
2688         isl_int_init(v);
2689         isl_int_set(v, aff->v->el[1 + pos]);
2690         isl_int_set_si(aff->v->el[1 + pos], 0);
2691         isl_seq_combine(aff->v->el + 1, subs->v->el[0], aff->v->el + 1,
2692                         v, subs->v->el + 1, subs->v->size - 1);
2693         isl_int_mul(aff->v->el[0], aff->v->el[0], subs->v->el[0]);
2694         isl_int_clear(v);
2695
2696         return aff;
2697 }
2698
2699 /* Plug in "subs" for dimension "type", "pos" in each of the affine
2700  * expressions in "maff".
2701  */
2702 __isl_give isl_multi_aff *isl_multi_aff_substitute(
2703         __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
2704         __isl_keep isl_aff *subs)
2705 {
2706         int i;
2707
2708         maff = isl_multi_aff_cow(maff);
2709         if (!maff || !subs)
2710                 return isl_multi_aff_free(maff);
2711
2712         if (type == isl_dim_in)
2713                 type = isl_dim_set;
2714
2715         for (i = 0; i < maff->n; ++i) {
2716                 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
2717                 if (!maff->p[i])
2718                         return isl_multi_aff_free(maff);
2719         }
2720
2721         return maff;
2722 }
2723
2724 /* Plug in "subs" for dimension "type", "pos" of "pma".
2725  *
2726  * pma is of the form
2727  *
2728  *      A_i(v) -> M_i(v)
2729  *
2730  * while subs is of the form
2731  *
2732  *      v' = B_j(v) -> S_j
2733  *
2734  * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
2735  * has a contribution in the result, in particular
2736  *
2737  *      C_ij(S_j) -> M_i(S_j)
2738  *
2739  * Note that plugging in S_j in C_ij may also result in an empty set
2740  * and this contribution should simply be discarded.
2741  */
2742 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
2743         __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
2744         __isl_keep isl_pw_aff *subs)
2745 {
2746         int i, j, n;
2747         isl_pw_multi_aff *res;
2748
2749         if (!pma || !subs)
2750                 return isl_pw_multi_aff_free(pma);
2751
2752         n = pma->n * subs->n;
2753         res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
2754
2755         for (i = 0; i < pma->n; ++i) {
2756                 for (j = 0; j < subs->n; ++j) {
2757                         isl_set *common;
2758                         isl_multi_aff *res_ij;
2759                         common = isl_set_intersect(
2760                                         isl_set_copy(pma->p[i].set),
2761                                         isl_set_copy(subs->p[j].set));
2762                         common = isl_set_substitute(common,
2763                                         type, pos, subs->p[j].aff);
2764                         if (isl_set_plain_is_empty(common)) {
2765                                 isl_set_free(common);
2766                                 continue;
2767                         }
2768
2769                         res_ij = isl_multi_aff_substitute(
2770                                         isl_multi_aff_copy(pma->p[i].maff),
2771                                         type, pos, subs->p[j].aff);
2772
2773                         res = isl_pw_multi_aff_add_piece(res, common, res_ij);
2774                 }
2775         }
2776
2777         isl_pw_multi_aff_free(pma);
2778         return res;
2779 }
2780
2781 /* Extend the local space of "dst" to include the divs
2782  * in the local space of "src".
2783  */
2784 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
2785         __isl_keep isl_aff *src)
2786 {
2787         isl_ctx *ctx;
2788         int *exp1 = NULL;
2789         int *exp2 = NULL;
2790         isl_mat *div;
2791
2792         if (!src || !dst)
2793                 return isl_aff_free(dst);
2794
2795         ctx = isl_aff_get_ctx(src);
2796         if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
2797                 isl_die(ctx, isl_error_invalid,
2798                         "spaces don't match", goto error);
2799
2800         if (src->ls->div->n_row == 0)
2801                 return dst;
2802
2803         exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
2804         exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
2805         if (!exp1 || !exp2)
2806                 goto error;
2807
2808         div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
2809         dst = isl_aff_expand_divs(dst, div, exp2);
2810         free(exp1);
2811         free(exp2);
2812
2813         return dst;
2814 error:
2815         free(exp1);
2816         free(exp2);
2817         return isl_aff_free(dst);
2818 }
2819
2820 /* Adjust the local spaces of the affine expressions in "maff"
2821  * such that they all have the save divs.
2822  */
2823 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
2824         __isl_take isl_multi_aff *maff)
2825 {
2826         int i;
2827
2828         if (!maff)
2829                 return NULL;
2830         if (maff->n == 0)
2831                 return maff;
2832         maff = isl_multi_aff_cow(maff);
2833         if (!maff)
2834                 return NULL;
2835
2836         for (i = 1; i < maff->n; ++i)
2837                 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
2838         for (i = 1; i < maff->n; ++i) {
2839                 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
2840                 if (!maff->p[i])
2841                         return isl_multi_aff_free(maff);
2842         }
2843
2844         return maff;
2845 }
2846
2847 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
2848 {
2849         aff = isl_aff_cow(aff);
2850         if (!aff)
2851                 return NULL;
2852
2853         aff->ls = isl_local_space_lift(aff->ls);
2854         if (!aff->ls)
2855                 return isl_aff_free(aff);
2856
2857         return aff;
2858 }
2859
2860 /* Lift "maff" to a space with extra dimensions such that the result
2861  * has no more existentially quantified variables.
2862  * If "ls" is not NULL, then *ls is assigned the local space that lies
2863  * at the basis of the lifting applied to "maff".
2864  */
2865 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
2866         __isl_give isl_local_space **ls)
2867 {
2868         int i;
2869         isl_space *space;
2870         unsigned n_div;
2871
2872         if (ls)
2873                 *ls = NULL;
2874
2875         if (!maff)
2876                 return NULL;
2877
2878         if (maff->n == 0) {
2879                 if (ls) {
2880                         isl_space *space = isl_multi_aff_get_domain_space(maff);
2881                         *ls = isl_local_space_from_space(space);
2882                         if (!*ls)
2883                                 return isl_multi_aff_free(maff);
2884                 }
2885                 return maff;
2886         }
2887
2888         maff = isl_multi_aff_cow(maff);
2889         maff = isl_multi_aff_align_divs(maff);
2890         if (!maff)
2891                 return NULL;
2892
2893         n_div = isl_aff_dim(maff->p[0], isl_dim_div);
2894         space = isl_multi_aff_get_space(maff);
2895         space = isl_space_lift(isl_space_domain(space), n_div);
2896         space = isl_space_extend_domain_with_range(space,
2897                                                 isl_multi_aff_get_space(maff));
2898         if (!space)
2899                 return isl_multi_aff_free(maff);
2900         isl_space_free(maff->space);
2901         maff->space = space;
2902
2903         if (ls) {
2904                 *ls = isl_aff_get_domain_local_space(maff->p[0]);
2905                 if (!*ls)
2906                         return isl_multi_aff_free(maff);
2907         }
2908
2909         for (i = 0; i < maff->n; ++i) {
2910                 maff->p[i] = isl_aff_lift(maff->p[i]);
2911                 if (!maff->p[i])
2912                         goto error;
2913         }
2914
2915         return maff;
2916 error:
2917         if (ls)
2918                 isl_local_space_free(*ls);
2919         return isl_multi_aff_free(maff);
2920 }
2921
2922
2923 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
2924  */
2925 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
2926         __isl_keep isl_pw_multi_aff *pma, int pos)
2927 {
2928         int i;
2929         int n_out;
2930         isl_space *space;
2931         isl_pw_aff *pa;
2932
2933         if (!pma)
2934                 return NULL;
2935
2936         n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
2937         if (pos < 0 || pos >= n_out)
2938                 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
2939                         "index out of bounds", return NULL);
2940
2941         space = isl_pw_multi_aff_get_space(pma);
2942         space = isl_space_drop_dims(space, isl_dim_out,
2943                                     pos + 1, n_out - pos - 1);
2944         space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
2945
2946         pa = isl_pw_aff_alloc_size(space, pma->n);
2947         for (i = 0; i < pma->n; ++i) {
2948                 isl_aff *aff;
2949                 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
2950                 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
2951         }
2952
2953         return pa;
2954 }
2955
2956 /* Return an isl_pw_multi_aff with the given "set" as domain and
2957  * an unnamed zero-dimensional range.
2958  */
2959 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
2960         __isl_take isl_set *set)
2961 {
2962         isl_multi_aff *ma;
2963         isl_space *space;
2964
2965         space = isl_set_get_space(set);
2966         space = isl_space_from_domain(space);
2967         ma = isl_multi_aff_zero(space);
2968         return isl_pw_multi_aff_alloc(set, ma);
2969 }
2970
2971 /* Add an isl_pw_multi_aff with the given "set" as domain and
2972  * an unnamed zero-dimensional range to *user.
2973  */
2974 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
2975 {
2976         isl_union_pw_multi_aff **upma = user;
2977         isl_pw_multi_aff *pma;
2978
2979         pma = isl_pw_multi_aff_from_domain(set);
2980         *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
2981
2982         return 0;
2983 }
2984
2985 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
2986  * an unnamed zero-dimensional range.
2987  */
2988 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
2989         __isl_take isl_union_set *uset)
2990 {
2991         isl_space *space;
2992         isl_union_pw_multi_aff *upma;
2993
2994         if (!uset)
2995                 return NULL;
2996
2997         space = isl_union_set_get_space(uset);
2998         upma = isl_union_pw_multi_aff_empty(space);
2999
3000         if (isl_union_set_foreach_set(uset,
3001                                     &add_pw_multi_aff_from_domain, &upma) < 0)
3002                 goto error;
3003
3004         isl_union_set_free(uset);
3005         return upma;
3006 error:
3007         isl_union_set_free(uset);
3008         isl_union_pw_multi_aff_free(upma);
3009         return NULL;
3010 }
3011
3012 /* Convert "pma" to an isl_map and add it to *umap.
3013  */
3014 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
3015 {
3016         isl_union_map **umap = user;
3017         isl_map *map;
3018
3019         map = isl_map_from_pw_multi_aff(pma);
3020         *umap = isl_union_map_add_map(*umap, map);
3021
3022         return 0;
3023 }
3024
3025 /* Construct a union map mapping the domain of the union
3026  * piecewise multi-affine expression to its range, with each dimension
3027  * in the range equated to the corresponding affine expression on its cell.
3028  */
3029 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
3030         __isl_take isl_union_pw_multi_aff *upma)
3031 {
3032         isl_space *space;
3033         isl_union_map *umap;
3034
3035         if (!upma)
3036                 return NULL;
3037
3038         space = isl_union_pw_multi_aff_get_space(upma);
3039         umap = isl_union_map_empty(space);
3040
3041         if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3042                                         &map_from_pw_multi_aff, &umap) < 0)
3043                 goto error;
3044
3045         isl_union_pw_multi_aff_free(upma);
3046         return umap;
3047 error:
3048         isl_union_pw_multi_aff_free(upma);
3049         isl_union_map_free(umap);
3050         return NULL;
3051 }
3052
3053 /* Local data for bin_entry and the callback "fn".
3054  */
3055 struct isl_union_pw_multi_aff_bin_data {
3056         isl_union_pw_multi_aff *upma2;
3057         isl_union_pw_multi_aff *res;
3058         isl_pw_multi_aff *pma;
3059         int (*fn)(void **entry, void *user);
3060 };
3061
3062 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
3063  * and call data->fn for each isl_pw_multi_aff in data->upma2.
3064  */
3065 static int bin_entry(void **entry, void *user)
3066 {
3067         struct isl_union_pw_multi_aff_bin_data *data = user;
3068         isl_pw_multi_aff *pma = *entry;
3069
3070         data->pma = pma;
3071         if (isl_hash_table_foreach(data->upma2->dim->ctx, &data->upma2->table,
3072                                    data->fn, data) < 0)
3073                 return -1;
3074
3075         return 0;
3076 }
3077
3078 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
3079  * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
3080  * passed as user field) and the isl_pw_multi_aff from upma2 is available
3081  * as *entry.  The callback should adjust data->res if desired.
3082  */
3083 static __isl_give isl_union_pw_multi_aff *bin_op(
3084         __isl_take isl_union_pw_multi_aff *upma1,
3085         __isl_take isl_union_pw_multi_aff *upma2,
3086         int (*fn)(void **entry, void *user))
3087 {
3088         isl_space *space;
3089         struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
3090
3091         space = isl_union_pw_multi_aff_get_space(upma2);
3092         upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
3093         space = isl_union_pw_multi_aff_get_space(upma1);
3094         upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
3095
3096         if (!upma1 || !upma2)
3097                 goto error;
3098
3099         data.upma2 = upma2;
3100         data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->dim),
3101                                        upma1->table.n);
3102         if (isl_hash_table_foreach(upma1->dim->ctx, &upma1->table,
3103                                    &bin_entry, &data) < 0)
3104                 goto error;
3105
3106         isl_union_pw_multi_aff_free(upma1);
3107         isl_union_pw_multi_aff_free(upma2);
3108         return data.res;
3109 error:
3110         isl_union_pw_multi_aff_free(upma1);
3111         isl_union_pw_multi_aff_free(upma2);
3112         isl_union_pw_multi_aff_free(data.res);
3113         return NULL;
3114 }
3115
3116 /* Given two isl_multi_affs A -> B and C -> D,
3117  * construct an isl_multi_aff (A * C) -> (B, D).
3118  */
3119 __isl_give isl_multi_aff *isl_multi_aff_flat_range_product(
3120         __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
3121 {
3122         int i, n1, n2;
3123         isl_aff *aff;
3124         isl_space *space;
3125         isl_multi_aff *res;
3126
3127         if (!ma1 || !ma2)
3128                 goto error;
3129
3130         space = isl_space_range_product(isl_multi_aff_get_space(ma1),
3131                                         isl_multi_aff_get_space(ma2));
3132         space = isl_space_flatten_range(space);
3133         res = isl_multi_aff_alloc(space);
3134
3135         n1 = isl_multi_aff_dim(ma1, isl_dim_out);
3136         n2 = isl_multi_aff_dim(ma2, isl_dim_out);
3137
3138         for (i = 0; i < n1; ++i) {
3139                 aff = isl_multi_aff_get_aff(ma1, i);
3140                 res = isl_multi_aff_set_aff(res, i, aff);
3141         }
3142
3143         for (i = 0; i < n2; ++i) {
3144                 aff = isl_multi_aff_get_aff(ma2, i);
3145                 res = isl_multi_aff_set_aff(res, n1 + i, aff);
3146         }
3147
3148         isl_multi_aff_free(ma1);
3149         isl_multi_aff_free(ma2);
3150         return res;
3151 error:
3152         isl_multi_aff_free(ma1);
3153         isl_multi_aff_free(ma2);
3154         return NULL;
3155 }
3156
3157 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
3158  * construct an isl_pw_multi_aff (A * C) -> (B, D).
3159  */
3160 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
3161         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3162 {
3163         isl_space *space;
3164
3165         space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
3166                                         isl_pw_multi_aff_get_space(pma2));
3167         space = isl_space_flatten_range(space);
3168         return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
3169                                             &isl_multi_aff_flat_range_product);
3170 }
3171
3172 /* Given two isl_pw_multi_affs A -> B and C -> D,
3173  * construct an isl_pw_multi_aff (A * C) -> (B, D).
3174  */
3175 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
3176         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3177 {
3178         return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3179                                             &pw_multi_aff_flat_range_product);
3180 }
3181
3182 /* If data->pma and *entry have the same domain space, then compute
3183  * their flat range product and the result to data->res.
3184  */
3185 static int flat_range_product_entry(void **entry, void *user)
3186 {
3187         struct isl_union_pw_multi_aff_bin_data *data = user;
3188         isl_pw_multi_aff *pma2 = *entry;
3189
3190         if (!isl_space_tuple_match(data->pma->dim, isl_dim_in,
3191                                  pma2->dim, isl_dim_in))
3192                 return 0;
3193
3194         pma2 = isl_pw_multi_aff_flat_range_product(
3195                                         isl_pw_multi_aff_copy(data->pma),
3196                                         isl_pw_multi_aff_copy(pma2));
3197
3198         data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
3199
3200         return 0;
3201 }
3202
3203 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
3204  * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
3205  */
3206 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
3207         __isl_take isl_union_pw_multi_aff *upma1,
3208         __isl_take isl_union_pw_multi_aff *upma2)
3209 {
3210         return bin_op(upma1, upma2, &flat_range_product_entry);
3211 }